1 /*******************************************************************************
3 * Intel Ethernet Controller XL710 Family Linux Driver
4 * Copyright(c) 2013 - 2014 Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 * The full GNU General Public License is included in this distribution in
19 * the file called "COPYING".
21 * Contact Information:
22 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25 ******************************************************************************/
29 #ifdef CONFIG_I40E_VXLAN
30 #include <net/vxlan.h>
33 const char i40e_driver_name[] = "i40e";
34 static const char i40e_driver_string[] =
35 "Intel(R) Ethernet Connection XL710 Network Driver";
39 #define DRV_VERSION_MAJOR 0
40 #define DRV_VERSION_MINOR 3
41 #define DRV_VERSION_BUILD 27
42 #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
43 __stringify(DRV_VERSION_MINOR) "." \
44 __stringify(DRV_VERSION_BUILD) DRV_KERN
45 const char i40e_driver_version_str[] = DRV_VERSION;
46 static const char i40e_copyright[] = "Copyright (c) 2013 Intel Corporation.";
48 /* a bit of forward declarations */
49 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
50 static void i40e_handle_reset_warning(struct i40e_pf *pf);
51 static int i40e_add_vsi(struct i40e_vsi *vsi);
52 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
53 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
54 static int i40e_setup_misc_vector(struct i40e_pf *pf);
55 static void i40e_determine_queue_usage(struct i40e_pf *pf);
56 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
58 /* i40e_pci_tbl - PCI Device ID Table
60 * Last entry must be all 0s
62 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
63 * Class, Class Mask, private data (not used) }
65 static DEFINE_PCI_DEVICE_TABLE(i40e_pci_tbl) = {
66 {PCI_VDEVICE(INTEL, I40E_SFP_XL710_DEVICE_ID), 0},
67 {PCI_VDEVICE(INTEL, I40E_SFP_X710_DEVICE_ID), 0},
68 {PCI_VDEVICE(INTEL, I40E_QEMU_DEVICE_ID), 0},
69 {PCI_VDEVICE(INTEL, I40E_KX_A_DEVICE_ID), 0},
70 {PCI_VDEVICE(INTEL, I40E_KX_B_DEVICE_ID), 0},
71 {PCI_VDEVICE(INTEL, I40E_KX_C_DEVICE_ID), 0},
72 {PCI_VDEVICE(INTEL, I40E_KX_D_DEVICE_ID), 0},
73 {PCI_VDEVICE(INTEL, I40E_QSFP_A_DEVICE_ID), 0},
74 {PCI_VDEVICE(INTEL, I40E_QSFP_B_DEVICE_ID), 0},
75 {PCI_VDEVICE(INTEL, I40E_QSFP_C_DEVICE_ID), 0},
76 /* required last entry */
79 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
81 #define I40E_MAX_VF_COUNT 128
82 static int debug = -1;
83 module_param(debug, int, 0);
84 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
86 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
87 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
88 MODULE_LICENSE("GPL");
89 MODULE_VERSION(DRV_VERSION);
92 * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
93 * @hw: pointer to the HW structure
94 * @mem: ptr to mem struct to fill out
95 * @size: size of memory requested
96 * @alignment: what to align the allocation to
98 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
99 u64 size, u32 alignment)
101 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
103 mem->size = ALIGN(size, alignment);
104 mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
105 &mem->pa, GFP_KERNEL);
113 * i40e_free_dma_mem_d - OS specific memory free for shared code
114 * @hw: pointer to the HW structure
115 * @mem: ptr to mem struct to free
117 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
119 struct i40e_pf *pf = (struct i40e_pf *)hw->back;
121 dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
130 * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
131 * @hw: pointer to the HW structure
132 * @mem: ptr to mem struct to fill out
133 * @size: size of memory requested
135 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
139 mem->va = kzalloc(size, GFP_KERNEL);
148 * i40e_free_virt_mem_d - OS specific memory free for shared code
149 * @hw: pointer to the HW structure
150 * @mem: ptr to mem struct to free
152 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
154 /* it's ok to kfree a NULL pointer */
163 * i40e_get_lump - find a lump of free generic resource
164 * @pf: board private structure
165 * @pile: the pile of resource to search
166 * @needed: the number of items needed
167 * @id: an owner id to stick on the items assigned
169 * Returns the base item index of the lump, or negative for error
171 * The search_hint trick and lack of advanced fit-finding only work
172 * because we're highly likely to have all the same size lump requests.
173 * Linear search time and any fragmentation should be minimal.
175 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
181 if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
182 dev_info(&pf->pdev->dev,
183 "param err: pile=%p needed=%d id=0x%04x\n",
188 /* start the linear search with an imperfect hint */
189 i = pile->search_hint;
190 while (i < pile->num_entries) {
191 /* skip already allocated entries */
192 if (pile->list[i] & I40E_PILE_VALID_BIT) {
197 /* do we have enough in this lump? */
198 for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
199 if (pile->list[i+j] & I40E_PILE_VALID_BIT)
204 /* there was enough, so assign it to the requestor */
205 for (j = 0; j < needed; j++)
206 pile->list[i+j] = id | I40E_PILE_VALID_BIT;
208 pile->search_hint = i + j;
211 /* not enough, so skip over it and continue looking */
220 * i40e_put_lump - return a lump of generic resource
221 * @pile: the pile of resource to search
222 * @index: the base item index
223 * @id: the owner id of the items assigned
225 * Returns the count of items in the lump
227 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
229 int valid_id = (id | I40E_PILE_VALID_BIT);
233 if (!pile || index >= pile->num_entries)
237 i < pile->num_entries && pile->list[i] == valid_id;
243 if (count && index < pile->search_hint)
244 pile->search_hint = index;
250 * i40e_service_event_schedule - Schedule the service task to wake up
251 * @pf: board private structure
253 * If not already scheduled, this puts the task into the work queue
255 static void i40e_service_event_schedule(struct i40e_pf *pf)
257 if (!test_bit(__I40E_DOWN, &pf->state) &&
258 !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
259 !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
260 schedule_work(&pf->service_task);
264 * i40e_tx_timeout - Respond to a Tx Hang
265 * @netdev: network interface device structure
267 * If any port has noticed a Tx timeout, it is likely that the whole
268 * device is munged, not just the one netdev port, so go for the full
271 static void i40e_tx_timeout(struct net_device *netdev)
273 struct i40e_netdev_priv *np = netdev_priv(netdev);
274 struct i40e_vsi *vsi = np->vsi;
275 struct i40e_pf *pf = vsi->back;
277 pf->tx_timeout_count++;
279 if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
280 pf->tx_timeout_recovery_level = 0;
281 pf->tx_timeout_last_recovery = jiffies;
282 netdev_info(netdev, "tx_timeout recovery level %d\n",
283 pf->tx_timeout_recovery_level);
285 switch (pf->tx_timeout_recovery_level) {
287 /* disable and re-enable queues for the VSI */
288 if (in_interrupt()) {
289 set_bit(__I40E_REINIT_REQUESTED, &pf->state);
290 set_bit(__I40E_REINIT_REQUESTED, &vsi->state);
292 i40e_vsi_reinit_locked(vsi);
296 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
299 set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
302 set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
305 netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
309 i40e_service_event_schedule(pf);
310 pf->tx_timeout_recovery_level++;
314 * i40e_release_rx_desc - Store the new tail and head values
315 * @rx_ring: ring to bump
316 * @val: new head index
318 static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
320 rx_ring->next_to_use = val;
322 /* Force memory writes to complete before letting h/w
323 * know there are new descriptors to fetch. (Only
324 * applicable for weak-ordered memory model archs,
328 writel(val, rx_ring->tail);
332 * i40e_get_vsi_stats_struct - Get System Network Statistics
333 * @vsi: the VSI we care about
335 * Returns the address of the device statistics structure.
336 * The statistics are actually updated from the service task.
338 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
340 return &vsi->net_stats;
344 * i40e_get_netdev_stats_struct - Get statistics for netdev interface
345 * @netdev: network interface device structure
347 * Returns the address of the device statistics structure.
348 * The statistics are actually updated from the service task.
350 static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
351 struct net_device *netdev,
352 struct rtnl_link_stats64 *stats)
354 struct i40e_netdev_priv *np = netdev_priv(netdev);
355 struct i40e_vsi *vsi = np->vsi;
356 struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
360 if (test_bit(__I40E_DOWN, &vsi->state))
367 for (i = 0; i < vsi->num_queue_pairs; i++) {
368 struct i40e_ring *tx_ring, *rx_ring;
372 tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
377 start = u64_stats_fetch_begin_bh(&tx_ring->syncp);
378 packets = tx_ring->stats.packets;
379 bytes = tx_ring->stats.bytes;
380 } while (u64_stats_fetch_retry_bh(&tx_ring->syncp, start));
382 stats->tx_packets += packets;
383 stats->tx_bytes += bytes;
384 rx_ring = &tx_ring[1];
387 start = u64_stats_fetch_begin_bh(&rx_ring->syncp);
388 packets = rx_ring->stats.packets;
389 bytes = rx_ring->stats.bytes;
390 } while (u64_stats_fetch_retry_bh(&rx_ring->syncp, start));
392 stats->rx_packets += packets;
393 stats->rx_bytes += bytes;
397 /* following stats updated by ixgbe_watchdog_task() */
398 stats->multicast = vsi_stats->multicast;
399 stats->tx_errors = vsi_stats->tx_errors;
400 stats->tx_dropped = vsi_stats->tx_dropped;
401 stats->rx_errors = vsi_stats->rx_errors;
402 stats->rx_crc_errors = vsi_stats->rx_crc_errors;
403 stats->rx_length_errors = vsi_stats->rx_length_errors;
409 * i40e_vsi_reset_stats - Resets all stats of the given vsi
410 * @vsi: the VSI to have its stats reset
412 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
414 struct rtnl_link_stats64 *ns;
420 ns = i40e_get_vsi_stats_struct(vsi);
421 memset(ns, 0, sizeof(*ns));
422 memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
423 memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
424 memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
425 if (vsi->rx_rings && vsi->rx_rings[0]) {
426 for (i = 0; i < vsi->num_queue_pairs; i++) {
427 memset(&vsi->rx_rings[i]->stats, 0 ,
428 sizeof(vsi->rx_rings[i]->stats));
429 memset(&vsi->rx_rings[i]->rx_stats, 0 ,
430 sizeof(vsi->rx_rings[i]->rx_stats));
431 memset(&vsi->tx_rings[i]->stats, 0 ,
432 sizeof(vsi->tx_rings[i]->stats));
433 memset(&vsi->tx_rings[i]->tx_stats, 0,
434 sizeof(vsi->tx_rings[i]->tx_stats));
437 vsi->stat_offsets_loaded = false;
441 * i40e_pf_reset_stats - Reset all of the stats for the given pf
442 * @pf: the PF to be reset
444 void i40e_pf_reset_stats(struct i40e_pf *pf)
446 memset(&pf->stats, 0, sizeof(pf->stats));
447 memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
448 pf->stat_offsets_loaded = false;
452 * i40e_stat_update48 - read and update a 48 bit stat from the chip
453 * @hw: ptr to the hardware info
454 * @hireg: the high 32 bit reg to read
455 * @loreg: the low 32 bit reg to read
456 * @offset_loaded: has the initial offset been loaded yet
457 * @offset: ptr to current offset value
458 * @stat: ptr to the stat
460 * Since the device stats are not reset at PFReset, they likely will not
461 * be zeroed when the driver starts. We'll save the first values read
462 * and use them as offsets to be subtracted from the raw values in order
463 * to report stats that count from zero. In the process, we also manage
464 * the potential roll-over.
466 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
467 bool offset_loaded, u64 *offset, u64 *stat)
471 if (hw->device_id == I40E_QEMU_DEVICE_ID) {
472 new_data = rd32(hw, loreg);
473 new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
475 new_data = rd64(hw, loreg);
479 if (likely(new_data >= *offset))
480 *stat = new_data - *offset;
482 *stat = (new_data + ((u64)1 << 48)) - *offset;
483 *stat &= 0xFFFFFFFFFFFFULL;
487 * i40e_stat_update32 - read and update a 32 bit stat from the chip
488 * @hw: ptr to the hardware info
489 * @reg: the hw reg to read
490 * @offset_loaded: has the initial offset been loaded yet
491 * @offset: ptr to current offset value
492 * @stat: ptr to the stat
494 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
495 bool offset_loaded, u64 *offset, u64 *stat)
499 new_data = rd32(hw, reg);
502 if (likely(new_data >= *offset))
503 *stat = (u32)(new_data - *offset);
505 *stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
509 * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
510 * @vsi: the VSI to be updated
512 void i40e_update_eth_stats(struct i40e_vsi *vsi)
514 int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
515 struct i40e_pf *pf = vsi->back;
516 struct i40e_hw *hw = &pf->hw;
517 struct i40e_eth_stats *oes;
518 struct i40e_eth_stats *es; /* device's eth stats */
520 es = &vsi->eth_stats;
521 oes = &vsi->eth_stats_offsets;
523 /* Gather up the stats that the hw collects */
524 i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
525 vsi->stat_offsets_loaded,
526 &oes->tx_errors, &es->tx_errors);
527 i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
528 vsi->stat_offsets_loaded,
529 &oes->rx_discards, &es->rx_discards);
531 i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
532 I40E_GLV_GORCL(stat_idx),
533 vsi->stat_offsets_loaded,
534 &oes->rx_bytes, &es->rx_bytes);
535 i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
536 I40E_GLV_UPRCL(stat_idx),
537 vsi->stat_offsets_loaded,
538 &oes->rx_unicast, &es->rx_unicast);
539 i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
540 I40E_GLV_MPRCL(stat_idx),
541 vsi->stat_offsets_loaded,
542 &oes->rx_multicast, &es->rx_multicast);
543 i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
544 I40E_GLV_BPRCL(stat_idx),
545 vsi->stat_offsets_loaded,
546 &oes->rx_broadcast, &es->rx_broadcast);
548 i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
549 I40E_GLV_GOTCL(stat_idx),
550 vsi->stat_offsets_loaded,
551 &oes->tx_bytes, &es->tx_bytes);
552 i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
553 I40E_GLV_UPTCL(stat_idx),
554 vsi->stat_offsets_loaded,
555 &oes->tx_unicast, &es->tx_unicast);
556 i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
557 I40E_GLV_MPTCL(stat_idx),
558 vsi->stat_offsets_loaded,
559 &oes->tx_multicast, &es->tx_multicast);
560 i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
561 I40E_GLV_BPTCL(stat_idx),
562 vsi->stat_offsets_loaded,
563 &oes->tx_broadcast, &es->tx_broadcast);
564 vsi->stat_offsets_loaded = true;
568 * i40e_update_veb_stats - Update Switch component statistics
569 * @veb: the VEB being updated
571 static void i40e_update_veb_stats(struct i40e_veb *veb)
573 struct i40e_pf *pf = veb->pf;
574 struct i40e_hw *hw = &pf->hw;
575 struct i40e_eth_stats *oes;
576 struct i40e_eth_stats *es; /* device's eth stats */
579 idx = veb->stats_idx;
581 oes = &veb->stats_offsets;
583 /* Gather up the stats that the hw collects */
584 i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
585 veb->stat_offsets_loaded,
586 &oes->tx_discards, &es->tx_discards);
587 if (hw->revision_id > 0)
588 i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
589 veb->stat_offsets_loaded,
590 &oes->rx_unknown_protocol,
591 &es->rx_unknown_protocol);
592 i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
593 veb->stat_offsets_loaded,
594 &oes->rx_bytes, &es->rx_bytes);
595 i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
596 veb->stat_offsets_loaded,
597 &oes->rx_unicast, &es->rx_unicast);
598 i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
599 veb->stat_offsets_loaded,
600 &oes->rx_multicast, &es->rx_multicast);
601 i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
602 veb->stat_offsets_loaded,
603 &oes->rx_broadcast, &es->rx_broadcast);
605 i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
606 veb->stat_offsets_loaded,
607 &oes->tx_bytes, &es->tx_bytes);
608 i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
609 veb->stat_offsets_loaded,
610 &oes->tx_unicast, &es->tx_unicast);
611 i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
612 veb->stat_offsets_loaded,
613 &oes->tx_multicast, &es->tx_multicast);
614 i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
615 veb->stat_offsets_loaded,
616 &oes->tx_broadcast, &es->tx_broadcast);
617 veb->stat_offsets_loaded = true;
621 * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
622 * @pf: the corresponding PF
624 * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
626 static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
628 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
629 struct i40e_hw_port_stats *nsd = &pf->stats;
630 struct i40e_hw *hw = &pf->hw;
634 if ((hw->fc.current_mode != I40E_FC_FULL) &&
635 (hw->fc.current_mode != I40E_FC_RX_PAUSE))
638 xoff = nsd->link_xoff_rx;
639 i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
640 pf->stat_offsets_loaded,
641 &osd->link_xoff_rx, &nsd->link_xoff_rx);
643 /* No new LFC xoff rx */
644 if (!(nsd->link_xoff_rx - xoff))
647 /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
648 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
649 struct i40e_vsi *vsi = pf->vsi[v];
654 for (i = 0; i < vsi->num_queue_pairs; i++) {
655 struct i40e_ring *ring = vsi->tx_rings[i];
656 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
662 * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
663 * @pf: the corresponding PF
665 * Update the Rx XOFF counter (PAUSE frames) in PFC mode
667 static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
669 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
670 struct i40e_hw_port_stats *nsd = &pf->stats;
671 bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
672 struct i40e_dcbx_config *dcb_cfg;
673 struct i40e_hw *hw = &pf->hw;
677 dcb_cfg = &hw->local_dcbx_config;
679 /* See if DCB enabled with PFC TC */
680 if (!(pf->flags & I40E_FLAG_DCB_ENABLED) ||
681 !(dcb_cfg->pfc.pfcenable)) {
682 i40e_update_link_xoff_rx(pf);
686 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
687 u64 prio_xoff = nsd->priority_xoff_rx[i];
688 i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
689 pf->stat_offsets_loaded,
690 &osd->priority_xoff_rx[i],
691 &nsd->priority_xoff_rx[i]);
693 /* No new PFC xoff rx */
694 if (!(nsd->priority_xoff_rx[i] - prio_xoff))
696 /* Get the TC for given priority */
697 tc = dcb_cfg->etscfg.prioritytable[i];
701 /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
702 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
703 struct i40e_vsi *vsi = pf->vsi[v];
708 for (i = 0; i < vsi->num_queue_pairs; i++) {
709 struct i40e_ring *ring = vsi->tx_rings[i];
713 clear_bit(__I40E_HANG_CHECK_ARMED,
720 * i40e_update_stats - Update the board statistics counters.
721 * @vsi: the VSI to be updated
723 * There are a few instances where we store the same stat in a
724 * couple of different structs. This is partly because we have
725 * the netdev stats that need to be filled out, which is slightly
726 * different from the "eth_stats" defined by the chip and used in
727 * VF communications. We sort it all out here in a central place.
729 void i40e_update_stats(struct i40e_vsi *vsi)
731 struct i40e_pf *pf = vsi->back;
732 struct i40e_hw *hw = &pf->hw;
733 struct rtnl_link_stats64 *ons;
734 struct rtnl_link_stats64 *ns; /* netdev stats */
735 struct i40e_eth_stats *oes;
736 struct i40e_eth_stats *es; /* device's eth stats */
737 u32 tx_restart, tx_busy;
744 if (test_bit(__I40E_DOWN, &vsi->state) ||
745 test_bit(__I40E_CONFIG_BUSY, &pf->state))
748 ns = i40e_get_vsi_stats_struct(vsi);
749 ons = &vsi->net_stats_offsets;
750 es = &vsi->eth_stats;
751 oes = &vsi->eth_stats_offsets;
753 /* Gather up the netdev and vsi stats that the driver collects
754 * on the fly during packet processing
758 tx_restart = tx_busy = 0;
762 for (q = 0; q < vsi->num_queue_pairs; q++) {
768 p = ACCESS_ONCE(vsi->tx_rings[q]);
771 start = u64_stats_fetch_begin_bh(&p->syncp);
772 packets = p->stats.packets;
773 bytes = p->stats.bytes;
774 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
777 tx_restart += p->tx_stats.restart_queue;
778 tx_busy += p->tx_stats.tx_busy;
780 /* Rx queue is part of the same block as Tx queue */
783 start = u64_stats_fetch_begin_bh(&p->syncp);
784 packets = p->stats.packets;
785 bytes = p->stats.bytes;
786 } while (u64_stats_fetch_retry_bh(&p->syncp, start));
789 rx_buf += p->rx_stats.alloc_buff_failed;
790 rx_page += p->rx_stats.alloc_page_failed;
793 vsi->tx_restart = tx_restart;
794 vsi->tx_busy = tx_busy;
795 vsi->rx_page_failed = rx_page;
796 vsi->rx_buf_failed = rx_buf;
798 ns->rx_packets = rx_p;
800 ns->tx_packets = tx_p;
803 i40e_update_eth_stats(vsi);
804 /* update netdev stats from eth stats */
805 ons->rx_errors = oes->rx_errors;
806 ns->rx_errors = es->rx_errors;
807 ons->tx_errors = oes->tx_errors;
808 ns->tx_errors = es->tx_errors;
809 ons->multicast = oes->rx_multicast;
810 ns->multicast = es->rx_multicast;
811 ons->tx_dropped = oes->tx_discards;
812 ns->tx_dropped = es->tx_discards;
814 /* Get the port data only if this is the main PF VSI */
815 if (vsi == pf->vsi[pf->lan_vsi]) {
816 struct i40e_hw_port_stats *nsd = &pf->stats;
817 struct i40e_hw_port_stats *osd = &pf->stats_offsets;
819 i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
820 I40E_GLPRT_GORCL(hw->port),
821 pf->stat_offsets_loaded,
822 &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
823 i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
824 I40E_GLPRT_GOTCL(hw->port),
825 pf->stat_offsets_loaded,
826 &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
827 i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
828 pf->stat_offsets_loaded,
829 &osd->eth.rx_discards,
830 &nsd->eth.rx_discards);
831 i40e_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
832 pf->stat_offsets_loaded,
833 &osd->eth.tx_discards,
834 &nsd->eth.tx_discards);
835 i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
836 I40E_GLPRT_MPRCL(hw->port),
837 pf->stat_offsets_loaded,
838 &osd->eth.rx_multicast,
839 &nsd->eth.rx_multicast);
841 i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
842 pf->stat_offsets_loaded,
843 &osd->tx_dropped_link_down,
844 &nsd->tx_dropped_link_down);
846 i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
847 pf->stat_offsets_loaded,
848 &osd->crc_errors, &nsd->crc_errors);
849 ns->rx_crc_errors = nsd->crc_errors;
851 i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
852 pf->stat_offsets_loaded,
853 &osd->illegal_bytes, &nsd->illegal_bytes);
854 ns->rx_errors = nsd->crc_errors
855 + nsd->illegal_bytes;
857 i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
858 pf->stat_offsets_loaded,
859 &osd->mac_local_faults,
860 &nsd->mac_local_faults);
861 i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
862 pf->stat_offsets_loaded,
863 &osd->mac_remote_faults,
864 &nsd->mac_remote_faults);
866 i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
867 pf->stat_offsets_loaded,
868 &osd->rx_length_errors,
869 &nsd->rx_length_errors);
870 ns->rx_length_errors = nsd->rx_length_errors;
872 i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
873 pf->stat_offsets_loaded,
874 &osd->link_xon_rx, &nsd->link_xon_rx);
875 i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
876 pf->stat_offsets_loaded,
877 &osd->link_xon_tx, &nsd->link_xon_tx);
878 i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
879 i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
880 pf->stat_offsets_loaded,
881 &osd->link_xoff_tx, &nsd->link_xoff_tx);
883 for (i = 0; i < 8; i++) {
884 i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
885 pf->stat_offsets_loaded,
886 &osd->priority_xon_rx[i],
887 &nsd->priority_xon_rx[i]);
888 i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
889 pf->stat_offsets_loaded,
890 &osd->priority_xon_tx[i],
891 &nsd->priority_xon_tx[i]);
892 i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
893 pf->stat_offsets_loaded,
894 &osd->priority_xoff_tx[i],
895 &nsd->priority_xoff_tx[i]);
896 i40e_stat_update32(hw,
897 I40E_GLPRT_RXON2OFFCNT(hw->port, i),
898 pf->stat_offsets_loaded,
899 &osd->priority_xon_2_xoff[i],
900 &nsd->priority_xon_2_xoff[i]);
903 i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
904 I40E_GLPRT_PRC64L(hw->port),
905 pf->stat_offsets_loaded,
906 &osd->rx_size_64, &nsd->rx_size_64);
907 i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
908 I40E_GLPRT_PRC127L(hw->port),
909 pf->stat_offsets_loaded,
910 &osd->rx_size_127, &nsd->rx_size_127);
911 i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
912 I40E_GLPRT_PRC255L(hw->port),
913 pf->stat_offsets_loaded,
914 &osd->rx_size_255, &nsd->rx_size_255);
915 i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
916 I40E_GLPRT_PRC511L(hw->port),
917 pf->stat_offsets_loaded,
918 &osd->rx_size_511, &nsd->rx_size_511);
919 i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
920 I40E_GLPRT_PRC1023L(hw->port),
921 pf->stat_offsets_loaded,
922 &osd->rx_size_1023, &nsd->rx_size_1023);
923 i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
924 I40E_GLPRT_PRC1522L(hw->port),
925 pf->stat_offsets_loaded,
926 &osd->rx_size_1522, &nsd->rx_size_1522);
927 i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
928 I40E_GLPRT_PRC9522L(hw->port),
929 pf->stat_offsets_loaded,
930 &osd->rx_size_big, &nsd->rx_size_big);
932 i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
933 I40E_GLPRT_PTC64L(hw->port),
934 pf->stat_offsets_loaded,
935 &osd->tx_size_64, &nsd->tx_size_64);
936 i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
937 I40E_GLPRT_PTC127L(hw->port),
938 pf->stat_offsets_loaded,
939 &osd->tx_size_127, &nsd->tx_size_127);
940 i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
941 I40E_GLPRT_PTC255L(hw->port),
942 pf->stat_offsets_loaded,
943 &osd->tx_size_255, &nsd->tx_size_255);
944 i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
945 I40E_GLPRT_PTC511L(hw->port),
946 pf->stat_offsets_loaded,
947 &osd->tx_size_511, &nsd->tx_size_511);
948 i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
949 I40E_GLPRT_PTC1023L(hw->port),
950 pf->stat_offsets_loaded,
951 &osd->tx_size_1023, &nsd->tx_size_1023);
952 i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
953 I40E_GLPRT_PTC1522L(hw->port),
954 pf->stat_offsets_loaded,
955 &osd->tx_size_1522, &nsd->tx_size_1522);
956 i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
957 I40E_GLPRT_PTC9522L(hw->port),
958 pf->stat_offsets_loaded,
959 &osd->tx_size_big, &nsd->tx_size_big);
961 i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
962 pf->stat_offsets_loaded,
963 &osd->rx_undersize, &nsd->rx_undersize);
964 i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
965 pf->stat_offsets_loaded,
966 &osd->rx_fragments, &nsd->rx_fragments);
967 i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
968 pf->stat_offsets_loaded,
969 &osd->rx_oversize, &nsd->rx_oversize);
970 i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
971 pf->stat_offsets_loaded,
972 &osd->rx_jabber, &nsd->rx_jabber);
975 pf->stat_offsets_loaded = true;
979 * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
980 * @vsi: the VSI to be searched
981 * @macaddr: the MAC address
983 * @is_vf: make sure its a vf filter, else doesn't matter
984 * @is_netdev: make sure its a netdev filter, else doesn't matter
986 * Returns ptr to the filter object or NULL
988 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
989 u8 *macaddr, s16 vlan,
990 bool is_vf, bool is_netdev)
992 struct i40e_mac_filter *f;
994 if (!vsi || !macaddr)
997 list_for_each_entry(f, &vsi->mac_filter_list, list) {
998 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1000 (!is_vf || f->is_vf) &&
1001 (!is_netdev || f->is_netdev))
1008 * i40e_find_mac - Find a mac addr in the macvlan filters list
1009 * @vsi: the VSI to be searched
1010 * @macaddr: the MAC address we are searching for
1011 * @is_vf: make sure its a vf filter, else doesn't matter
1012 * @is_netdev: make sure its a netdev filter, else doesn't matter
1014 * Returns the first filter with the provided MAC address or NULL if
1015 * MAC address was not found
1017 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
1018 bool is_vf, bool is_netdev)
1020 struct i40e_mac_filter *f;
1022 if (!vsi || !macaddr)
1025 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1026 if ((ether_addr_equal(macaddr, f->macaddr)) &&
1027 (!is_vf || f->is_vf) &&
1028 (!is_netdev || f->is_netdev))
1035 * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1036 * @vsi: the VSI to be searched
1038 * Returns true if VSI is in vlan mode or false otherwise
1040 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1042 struct i40e_mac_filter *f;
1044 /* Only -1 for all the filters denotes not in vlan mode
1045 * so we have to go through all the list in order to make sure
1047 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1056 * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
1057 * @vsi: the VSI to be searched
1058 * @macaddr: the mac address to be filtered
1059 * @is_vf: true if it is a vf
1060 * @is_netdev: true if it is a netdev
1062 * Goes through all the macvlan filters and adds a
1063 * macvlan filter for each unique vlan that already exists
1065 * Returns first filter found on success, else NULL
1067 struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
1068 bool is_vf, bool is_netdev)
1070 struct i40e_mac_filter *f;
1072 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1073 if (!i40e_find_filter(vsi, macaddr, f->vlan,
1074 is_vf, is_netdev)) {
1075 if (!i40e_add_filter(vsi, macaddr, f->vlan,
1081 return list_first_entry_or_null(&vsi->mac_filter_list,
1082 struct i40e_mac_filter, list);
1086 * i40e_add_filter - Add a mac/vlan filter to the VSI
1087 * @vsi: the VSI to be searched
1088 * @macaddr: the MAC address
1090 * @is_vf: make sure its a vf filter, else doesn't matter
1091 * @is_netdev: make sure its a netdev filter, else doesn't matter
1093 * Returns ptr to the filter object or NULL when no memory available.
1095 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1096 u8 *macaddr, s16 vlan,
1097 bool is_vf, bool is_netdev)
1099 struct i40e_mac_filter *f;
1101 if (!vsi || !macaddr)
1104 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1106 f = kzalloc(sizeof(*f), GFP_ATOMIC);
1108 goto add_filter_out;
1110 memcpy(f->macaddr, macaddr, ETH_ALEN);
1114 INIT_LIST_HEAD(&f->list);
1115 list_add(&f->list, &vsi->mac_filter_list);
1118 /* increment counter and add a new flag if needed */
1124 } else if (is_netdev) {
1125 if (!f->is_netdev) {
1126 f->is_netdev = true;
1133 /* changed tells sync_filters_subtask to
1134 * push the filter down to the firmware
1137 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1138 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1146 * i40e_del_filter - Remove a mac/vlan filter from the VSI
1147 * @vsi: the VSI to be searched
1148 * @macaddr: the MAC address
1150 * @is_vf: make sure it's a vf filter, else doesn't matter
1151 * @is_netdev: make sure it's a netdev filter, else doesn't matter
1153 void i40e_del_filter(struct i40e_vsi *vsi,
1154 u8 *macaddr, s16 vlan,
1155 bool is_vf, bool is_netdev)
1157 struct i40e_mac_filter *f;
1159 if (!vsi || !macaddr)
1162 f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
1163 if (!f || f->counter == 0)
1171 } else if (is_netdev) {
1173 f->is_netdev = false;
1177 /* make sure we don't remove a filter in use by vf or netdev */
1179 min_f += (f->is_vf ? 1 : 0);
1180 min_f += (f->is_netdev ? 1 : 0);
1182 if (f->counter > min_f)
1186 /* counter == 0 tells sync_filters_subtask to
1187 * remove the filter from the firmware's list
1189 if (f->counter == 0) {
1191 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1192 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1197 * i40e_set_mac - NDO callback to set mac address
1198 * @netdev: network interface device structure
1199 * @p: pointer to an address structure
1201 * Returns 0 on success, negative on failure
1203 static int i40e_set_mac(struct net_device *netdev, void *p)
1205 struct i40e_netdev_priv *np = netdev_priv(netdev);
1206 struct i40e_vsi *vsi = np->vsi;
1207 struct sockaddr *addr = p;
1208 struct i40e_mac_filter *f;
1210 if (!is_valid_ether_addr(addr->sa_data))
1211 return -EADDRNOTAVAIL;
1213 netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
1215 if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
1218 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1219 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1220 return -EADDRNOTAVAIL;
1222 if (vsi->type == I40E_VSI_MAIN) {
1224 ret = i40e_aq_mac_address_write(&vsi->back->hw,
1225 I40E_AQC_WRITE_TYPE_LAA_ONLY,
1226 addr->sa_data, NULL);
1229 "Addr change for Main VSI failed: %d\n",
1231 return -EADDRNOTAVAIL;
1234 memcpy(vsi->back->hw.mac.addr, addr->sa_data, netdev->addr_len);
1237 /* In order to be sure to not drop any packets, add the new address
1238 * then delete the old one.
1240 f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
1244 i40e_sync_vsi_filters(vsi);
1245 i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
1246 i40e_sync_vsi_filters(vsi);
1248 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
1254 * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1255 * @vsi: the VSI being setup
1256 * @ctxt: VSI context structure
1257 * @enabled_tc: Enabled TCs bitmap
1258 * @is_add: True if called before Add VSI
1260 * Setup VSI queue mapping for enabled traffic classes.
1262 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
1263 struct i40e_vsi_context *ctxt,
1267 struct i40e_pf *pf = vsi->back;
1276 sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1279 if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
1280 /* Find numtc from enabled TC bitmap */
1281 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1282 if (enabled_tc & (1 << i)) /* TC is enabled */
1286 dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
1290 /* At least TC0 is enabled in case of non-DCB case */
1294 vsi->tc_config.numtc = numtc;
1295 vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1297 /* Setup queue offset/count for all TCs for given VSI */
1298 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1299 /* See if the given TC is enabled for the given VSI */
1300 if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
1303 vsi->tc_config.tc_info[i].qoffset = offset;
1304 switch (vsi->type) {
1307 qcount = pf->rss_size;
1309 qcount = pf->num_tc_qps;
1310 vsi->tc_config.tc_info[i].qcount = qcount;
1313 case I40E_VSI_SRIOV:
1314 case I40E_VSI_VMDQ2:
1316 qcount = vsi->alloc_queue_pairs;
1317 vsi->tc_config.tc_info[i].qcount = qcount;
1322 /* find the power-of-2 of the number of queue pairs */
1323 num_qps = vsi->tc_config.tc_info[i].qcount;
1326 ((1 << pow) < vsi->tc_config.tc_info[i].qcount)) {
1331 vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1333 (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1334 (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1336 offset += vsi->tc_config.tc_info[i].qcount;
1338 /* TC is not enabled so set the offset to
1339 * default queue and allocate one queue
1342 vsi->tc_config.tc_info[i].qoffset = 0;
1343 vsi->tc_config.tc_info[i].qcount = 1;
1344 vsi->tc_config.tc_info[i].netdev_tc = 0;
1348 ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
1351 /* Set actual Tx/Rx queue pairs */
1352 vsi->num_queue_pairs = offset;
1354 /* Scheduler section valid can only be set for ADD VSI */
1356 sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1358 ctxt->info.up_enable_bits = enabled_tc;
1360 if (vsi->type == I40E_VSI_SRIOV) {
1361 ctxt->info.mapping_flags |=
1362 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
1363 for (i = 0; i < vsi->num_queue_pairs; i++)
1364 ctxt->info.queue_mapping[i] =
1365 cpu_to_le16(vsi->base_queue + i);
1367 ctxt->info.mapping_flags |=
1368 cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1369 ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1371 ctxt->info.valid_sections |= cpu_to_le16(sections);
1375 * i40e_set_rx_mode - NDO callback to set the netdev filters
1376 * @netdev: network interface device structure
1378 static void i40e_set_rx_mode(struct net_device *netdev)
1380 struct i40e_netdev_priv *np = netdev_priv(netdev);
1381 struct i40e_mac_filter *f, *ftmp;
1382 struct i40e_vsi *vsi = np->vsi;
1383 struct netdev_hw_addr *uca;
1384 struct netdev_hw_addr *mca;
1385 struct netdev_hw_addr *ha;
1387 /* add addr if not already in the filter list */
1388 netdev_for_each_uc_addr(uca, netdev) {
1389 if (!i40e_find_mac(vsi, uca->addr, false, true)) {
1390 if (i40e_is_vsi_in_vlan(vsi))
1391 i40e_put_mac_in_vlan(vsi, uca->addr,
1394 i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
1399 netdev_for_each_mc_addr(mca, netdev) {
1400 if (!i40e_find_mac(vsi, mca->addr, false, true)) {
1401 if (i40e_is_vsi_in_vlan(vsi))
1402 i40e_put_mac_in_vlan(vsi, mca->addr,
1405 i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
1410 /* remove filter if not in netdev list */
1411 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1417 if (is_multicast_ether_addr(f->macaddr)) {
1418 netdev_for_each_mc_addr(mca, netdev) {
1419 if (ether_addr_equal(mca->addr, f->macaddr)) {
1425 netdev_for_each_uc_addr(uca, netdev) {
1426 if (ether_addr_equal(uca->addr, f->macaddr)) {
1432 for_each_dev_addr(netdev, ha) {
1433 if (ether_addr_equal(ha->addr, f->macaddr)) {
1441 vsi, f->macaddr, I40E_VLAN_ANY, false, true);
1444 /* check for other flag changes */
1445 if (vsi->current_netdev_flags != vsi->netdev->flags) {
1446 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1447 vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
1452 * i40e_sync_vsi_filters - Update the VSI filter list to the HW
1453 * @vsi: ptr to the VSI
1455 * Push any outstanding VSI filter changes through the AdminQ.
1457 * Returns 0 or error value
1459 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
1461 struct i40e_mac_filter *f, *ftmp;
1462 bool promisc_forced_on = false;
1463 bool add_happened = false;
1464 int filter_list_len = 0;
1465 u32 changed_flags = 0;
1466 i40e_status aq_ret = 0;
1472 /* empty array typed pointers, kcalloc later */
1473 struct i40e_aqc_add_macvlan_element_data *add_list;
1474 struct i40e_aqc_remove_macvlan_element_data *del_list;
1476 while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
1477 usleep_range(1000, 2000);
1481 changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
1482 vsi->current_netdev_flags = vsi->netdev->flags;
1485 if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
1486 vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
1488 filter_list_len = pf->hw.aq.asq_buf_size /
1489 sizeof(struct i40e_aqc_remove_macvlan_element_data);
1490 del_list = kcalloc(filter_list_len,
1491 sizeof(struct i40e_aqc_remove_macvlan_element_data),
1496 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1500 if (f->counter != 0)
1505 /* add to delete list */
1506 memcpy(del_list[num_del].mac_addr,
1507 f->macaddr, ETH_ALEN);
1508 del_list[num_del].vlan_tag =
1509 cpu_to_le16((u16)(f->vlan ==
1510 I40E_VLAN_ANY ? 0 : f->vlan));
1512 cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1513 del_list[num_del].flags = cmd_flags;
1516 /* unlink from filter list */
1520 /* flush a full buffer */
1521 if (num_del == filter_list_len) {
1522 aq_ret = i40e_aq_remove_macvlan(&pf->hw,
1523 vsi->seid, del_list, num_del,
1526 memset(del_list, 0, sizeof(*del_list));
1529 dev_info(&pf->pdev->dev,
1530 "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
1532 pf->hw.aq.asq_last_status);
1536 aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
1537 del_list, num_del, NULL);
1541 dev_info(&pf->pdev->dev,
1542 "ignoring delete macvlan error, err %d, aq_err %d\n",
1543 aq_ret, pf->hw.aq.asq_last_status);
1549 /* do all the adds now */
1550 filter_list_len = pf->hw.aq.asq_buf_size /
1551 sizeof(struct i40e_aqc_add_macvlan_element_data),
1552 add_list = kcalloc(filter_list_len,
1553 sizeof(struct i40e_aqc_add_macvlan_element_data),
1558 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
1562 if (f->counter == 0)
1565 add_happened = true;
1568 /* add to add array */
1569 memcpy(add_list[num_add].mac_addr,
1570 f->macaddr, ETH_ALEN);
1571 add_list[num_add].vlan_tag =
1573 (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
1574 add_list[num_add].queue_number = 0;
1576 cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
1577 add_list[num_add].flags = cpu_to_le16(cmd_flags);
1580 /* flush a full buffer */
1581 if (num_add == filter_list_len) {
1582 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1589 memset(add_list, 0, sizeof(*add_list));
1593 aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
1594 add_list, num_add, NULL);
1600 if (add_happened && (!aq_ret)) {
1602 } else if (add_happened && (aq_ret)) {
1603 dev_info(&pf->pdev->dev,
1604 "add filter failed, err %d, aq_err %d\n",
1605 aq_ret, pf->hw.aq.asq_last_status);
1606 if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
1607 !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1609 promisc_forced_on = true;
1610 set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1612 dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
1617 /* check for changes in promiscuous modes */
1618 if (changed_flags & IFF_ALLMULTI) {
1619 bool cur_multipromisc;
1620 cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
1621 aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
1626 dev_info(&pf->pdev->dev,
1627 "set multi promisc failed, err %d, aq_err %d\n",
1628 aq_ret, pf->hw.aq.asq_last_status);
1630 if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
1632 cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
1633 test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
1635 aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
1639 dev_info(&pf->pdev->dev,
1640 "set uni promisc failed, err %d, aq_err %d\n",
1641 aq_ret, pf->hw.aq.asq_last_status);
1642 aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
1646 dev_info(&pf->pdev->dev,
1647 "set brdcast promisc failed, err %d, aq_err %d\n",
1648 aq_ret, pf->hw.aq.asq_last_status);
1651 clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
1656 * i40e_sync_filters_subtask - Sync the VSI filter list with HW
1657 * @pf: board private structure
1659 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
1663 if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
1665 pf->flags &= ~I40E_FLAG_FILTER_SYNC;
1667 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
1669 (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
1670 i40e_sync_vsi_filters(pf->vsi[v]);
1675 * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
1676 * @netdev: network interface device structure
1677 * @new_mtu: new value for maximum frame size
1679 * Returns 0 on success, negative on failure
1681 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
1683 struct i40e_netdev_priv *np = netdev_priv(netdev);
1684 int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
1685 struct i40e_vsi *vsi = np->vsi;
1687 /* MTU < 68 is an error and causes problems on some kernels */
1688 if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
1691 netdev_info(netdev, "changing MTU from %d to %d\n",
1692 netdev->mtu, new_mtu);
1693 netdev->mtu = new_mtu;
1694 if (netif_running(netdev))
1695 i40e_vsi_reinit_locked(vsi);
1701 * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
1702 * @vsi: the vsi being adjusted
1704 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
1706 struct i40e_vsi_context ctxt;
1709 if ((vsi->info.valid_sections &
1710 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1711 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
1712 return; /* already enabled */
1714 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1715 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1716 I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
1718 ctxt.seid = vsi->seid;
1719 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1720 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1722 dev_info(&vsi->back->pdev->dev,
1723 "%s: update vsi failed, aq_err=%d\n",
1724 __func__, vsi->back->hw.aq.asq_last_status);
1729 * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
1730 * @vsi: the vsi being adjusted
1732 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
1734 struct i40e_vsi_context ctxt;
1737 if ((vsi->info.valid_sections &
1738 cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
1739 ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
1740 I40E_AQ_VSI_PVLAN_EMOD_MASK))
1741 return; /* already disabled */
1743 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
1744 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
1745 I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
1747 ctxt.seid = vsi->seid;
1748 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
1749 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
1751 dev_info(&vsi->back->pdev->dev,
1752 "%s: update vsi failed, aq_err=%d\n",
1753 __func__, vsi->back->hw.aq.asq_last_status);
1758 * i40e_vlan_rx_register - Setup or shutdown vlan offload
1759 * @netdev: network interface to be adjusted
1760 * @features: netdev features to test if VLAN offload is enabled or not
1762 static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
1764 struct i40e_netdev_priv *np = netdev_priv(netdev);
1765 struct i40e_vsi *vsi = np->vsi;
1767 if (features & NETIF_F_HW_VLAN_CTAG_RX)
1768 i40e_vlan_stripping_enable(vsi);
1770 i40e_vlan_stripping_disable(vsi);
1774 * i40e_vsi_add_vlan - Add vsi membership for given vlan
1775 * @vsi: the vsi being configured
1776 * @vid: vlan id to be added (0 = untagged only , -1 = any)
1778 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
1780 struct i40e_mac_filter *f, *add_f;
1781 bool is_netdev, is_vf;
1783 is_vf = (vsi->type == I40E_VSI_SRIOV);
1784 is_netdev = !!(vsi->netdev);
1787 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
1790 dev_info(&vsi->back->pdev->dev,
1791 "Could not add vlan filter %d for %pM\n",
1792 vid, vsi->netdev->dev_addr);
1797 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1798 add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1800 dev_info(&vsi->back->pdev->dev,
1801 "Could not add vlan filter %d for %pM\n",
1807 /* Now if we add a vlan tag, make sure to check if it is the first
1808 * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
1809 * with 0, so we now accept untagged and specified tagged traffic
1810 * (and not any taged and untagged)
1813 if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
1815 is_vf, is_netdev)) {
1816 i40e_del_filter(vsi, vsi->netdev->dev_addr,
1817 I40E_VLAN_ANY, is_vf, is_netdev);
1818 add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
1821 dev_info(&vsi->back->pdev->dev,
1822 "Could not add filter 0 for %pM\n",
1823 vsi->netdev->dev_addr);
1828 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1829 if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1830 is_vf, is_netdev)) {
1831 i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1833 add_f = i40e_add_filter(vsi, f->macaddr,
1834 0, is_vf, is_netdev);
1836 dev_info(&vsi->back->pdev->dev,
1837 "Could not add filter 0 for %pM\n",
1845 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1846 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1849 return i40e_sync_vsi_filters(vsi);
1853 * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
1854 * @vsi: the vsi being configured
1855 * @vid: vlan id to be removed (0 = untagged only , -1 = any)
1857 * Return: 0 on success or negative otherwise
1859 int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
1861 struct net_device *netdev = vsi->netdev;
1862 struct i40e_mac_filter *f, *add_f;
1863 bool is_vf, is_netdev;
1864 int filter_count = 0;
1866 is_vf = (vsi->type == I40E_VSI_SRIOV);
1867 is_netdev = !!(netdev);
1870 i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
1872 list_for_each_entry(f, &vsi->mac_filter_list, list)
1873 i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
1875 /* go through all the filters for this VSI and if there is only
1876 * vid == 0 it means there are no other filters, so vid 0 must
1877 * be replaced with -1. This signifies that we should from now
1878 * on accept any traffic (with any tag present, or untagged)
1880 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1883 ether_addr_equal(netdev->dev_addr, f->macaddr))
1891 if (!filter_count && is_netdev) {
1892 i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
1893 f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
1896 dev_info(&vsi->back->pdev->dev,
1897 "Could not add filter %d for %pM\n",
1898 I40E_VLAN_ANY, netdev->dev_addr);
1903 if (!filter_count) {
1904 list_for_each_entry(f, &vsi->mac_filter_list, list) {
1905 i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
1906 add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
1909 dev_info(&vsi->back->pdev->dev,
1910 "Could not add filter %d for %pM\n",
1911 I40E_VLAN_ANY, f->macaddr);
1917 if (test_bit(__I40E_DOWN, &vsi->back->state) ||
1918 test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
1921 return i40e_sync_vsi_filters(vsi);
1925 * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
1926 * @netdev: network interface to be adjusted
1927 * @vid: vlan id to be added
1929 * net_device_ops implementation for adding vlan ids
1931 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
1932 __always_unused __be16 proto, u16 vid)
1934 struct i40e_netdev_priv *np = netdev_priv(netdev);
1935 struct i40e_vsi *vsi = np->vsi;
1941 netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
1943 /* If the network stack called us with vid = 0, we should
1944 * indicate to i40e_vsi_add_vlan() that we want to receive
1945 * any traffic (i.e. with any vlan tag, or untagged)
1947 ret = i40e_vsi_add_vlan(vsi, vid ? vid : I40E_VLAN_ANY);
1949 if (!ret && (vid < VLAN_N_VID))
1950 set_bit(vid, vsi->active_vlans);
1956 * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
1957 * @netdev: network interface to be adjusted
1958 * @vid: vlan id to be removed
1960 * net_device_ops implementation for adding vlan ids
1962 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
1963 __always_unused __be16 proto, u16 vid)
1965 struct i40e_netdev_priv *np = netdev_priv(netdev);
1966 struct i40e_vsi *vsi = np->vsi;
1968 netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
1970 /* return code is ignored as there is nothing a user
1971 * can do about failure to remove and a log message was
1972 * already printed from the other function
1974 i40e_vsi_kill_vlan(vsi, vid);
1976 clear_bit(vid, vsi->active_vlans);
1982 * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
1983 * @vsi: the vsi being brought back up
1985 static void i40e_restore_vlan(struct i40e_vsi *vsi)
1992 i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
1994 for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
1995 i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
2000 * i40e_vsi_add_pvid - Add pvid for the VSI
2001 * @vsi: the vsi being adjusted
2002 * @vid: the vlan id to set as a PVID
2004 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
2006 struct i40e_vsi_context ctxt;
2009 vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2010 vsi->info.pvid = cpu_to_le16(vid);
2011 vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
2012 I40E_AQ_VSI_PVLAN_INSERT_PVID |
2013 I40E_AQ_VSI_PVLAN_EMOD_STR;
2015 ctxt.seid = vsi->seid;
2016 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
2017 aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2019 dev_info(&vsi->back->pdev->dev,
2020 "%s: update vsi failed, aq_err=%d\n",
2021 __func__, vsi->back->hw.aq.asq_last_status);
2029 * i40e_vsi_remove_pvid - Remove the pvid from the VSI
2030 * @vsi: the vsi being adjusted
2032 * Just use the vlan_rx_register() service to put it back to normal
2034 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
2036 i40e_vlan_stripping_disable(vsi);
2042 * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
2043 * @vsi: ptr to the VSI
2045 * If this function returns with an error, then it's possible one or
2046 * more of the rings is populated (while the rest are not). It is the
2047 * callers duty to clean those orphaned rings.
2049 * Return 0 on success, negative on failure
2051 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
2055 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2056 err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
2062 * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
2063 * @vsi: ptr to the VSI
2065 * Free VSI's transmit software resources
2067 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
2074 for (i = 0; i < vsi->num_queue_pairs; i++)
2075 if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
2076 i40e_free_tx_resources(vsi->tx_rings[i]);
2080 * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
2081 * @vsi: ptr to the VSI
2083 * If this function returns with an error, then it's possible one or
2084 * more of the rings is populated (while the rest are not). It is the
2085 * callers duty to clean those orphaned rings.
2087 * Return 0 on success, negative on failure
2089 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
2093 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2094 err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
2099 * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
2100 * @vsi: ptr to the VSI
2102 * Free all receive software resources
2104 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
2111 for (i = 0; i < vsi->num_queue_pairs; i++)
2112 if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
2113 i40e_free_rx_resources(vsi->rx_rings[i]);
2117 * i40e_configure_tx_ring - Configure a transmit ring context and rest
2118 * @ring: The Tx ring to configure
2120 * Configure the Tx descriptor ring in the HMC context.
2122 static int i40e_configure_tx_ring(struct i40e_ring *ring)
2124 struct i40e_vsi *vsi = ring->vsi;
2125 u16 pf_q = vsi->base_queue + ring->queue_index;
2126 struct i40e_hw *hw = &vsi->back->hw;
2127 struct i40e_hmc_obj_txq tx_ctx;
2128 i40e_status err = 0;
2131 /* some ATR related tx ring init */
2132 if (vsi->back->flags & I40E_FLAG_FDIR_ATR_ENABLED) {
2133 ring->atr_sample_rate = vsi->back->atr_sample_rate;
2134 ring->atr_count = 0;
2136 ring->atr_sample_rate = 0;
2139 /* initialize XPS */
2140 if (ring->q_vector && ring->netdev &&
2141 !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
2142 netif_set_xps_queue(ring->netdev,
2143 &ring->q_vector->affinity_mask,
2146 /* clear the context structure first */
2147 memset(&tx_ctx, 0, sizeof(tx_ctx));
2149 tx_ctx.new_context = 1;
2150 tx_ctx.base = (ring->dma / 128);
2151 tx_ctx.qlen = ring->count;
2152 tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FDIR_ENABLED |
2153 I40E_FLAG_FDIR_ATR_ENABLED));
2155 /* As part of VSI creation/update, FW allocates certain
2156 * Tx arbitration queue sets for each TC enabled for
2157 * the VSI. The FW returns the handles to these queue
2158 * sets as part of the response buffer to Add VSI,
2159 * Update VSI, etc. AQ commands. It is expected that
2160 * these queue set handles be associated with the Tx
2161 * queues by the driver as part of the TX queue context
2162 * initialization. This has to be done regardless of
2163 * DCB as by default everything is mapped to TC0.
2165 tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
2166 tx_ctx.rdylist_act = 0;
2168 /* clear the context in the HMC */
2169 err = i40e_clear_lan_tx_queue_context(hw, pf_q);
2171 dev_info(&vsi->back->pdev->dev,
2172 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
2173 ring->queue_index, pf_q, err);
2177 /* set the context in the HMC */
2178 err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
2180 dev_info(&vsi->back->pdev->dev,
2181 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
2182 ring->queue_index, pf_q, err);
2186 /* Now associate this queue with this PCI function */
2187 qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
2188 qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
2189 I40E_QTX_CTL_PF_INDX_MASK);
2190 wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
2193 clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
2195 /* cache tail off for easier writes later */
2196 ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
2202 * i40e_configure_rx_ring - Configure a receive ring context
2203 * @ring: The Rx ring to configure
2205 * Configure the Rx descriptor ring in the HMC context.
2207 static int i40e_configure_rx_ring(struct i40e_ring *ring)
2209 struct i40e_vsi *vsi = ring->vsi;
2210 u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
2211 u16 pf_q = vsi->base_queue + ring->queue_index;
2212 struct i40e_hw *hw = &vsi->back->hw;
2213 struct i40e_hmc_obj_rxq rx_ctx;
2214 i40e_status err = 0;
2218 /* clear the context structure first */
2219 memset(&rx_ctx, 0, sizeof(rx_ctx));
2221 ring->rx_buf_len = vsi->rx_buf_len;
2222 ring->rx_hdr_len = vsi->rx_hdr_len;
2224 rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
2225 rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
2227 rx_ctx.base = (ring->dma / 128);
2228 rx_ctx.qlen = ring->count;
2230 if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
2231 set_ring_16byte_desc_enabled(ring);
2237 rx_ctx.dtype = vsi->dtype;
2239 set_ring_ps_enabled(ring);
2240 rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
2242 I40E_RX_SPLIT_TCP_UDP |
2245 rx_ctx.hsplit_0 = 0;
2248 rx_ctx.rxmax = min_t(u16, vsi->max_frame,
2249 (chain_len * ring->rx_buf_len));
2250 rx_ctx.tphrdesc_ena = 1;
2251 rx_ctx.tphwdesc_ena = 1;
2252 rx_ctx.tphdata_ena = 1;
2253 rx_ctx.tphhead_ena = 1;
2254 if (hw->revision_id == 0)
2255 rx_ctx.lrxqthresh = 0;
2257 rx_ctx.lrxqthresh = 2;
2258 rx_ctx.crcstrip = 1;
2262 /* clear the context in the HMC */
2263 err = i40e_clear_lan_rx_queue_context(hw, pf_q);
2265 dev_info(&vsi->back->pdev->dev,
2266 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2267 ring->queue_index, pf_q, err);
2271 /* set the context in the HMC */
2272 err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
2274 dev_info(&vsi->back->pdev->dev,
2275 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
2276 ring->queue_index, pf_q, err);
2280 /* cache tail for quicker writes, and clear the reg before use */
2281 ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
2282 writel(0, ring->tail);
2284 i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
2290 * i40e_vsi_configure_tx - Configure the VSI for Tx
2291 * @vsi: VSI structure describing this set of rings and resources
2293 * Configure the Tx VSI for operation.
2295 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
2300 for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
2301 err = i40e_configure_tx_ring(vsi->tx_rings[i]);
2307 * i40e_vsi_configure_rx - Configure the VSI for Rx
2308 * @vsi: the VSI being configured
2310 * Configure the Rx VSI for operation.
2312 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
2317 if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
2318 vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
2319 + ETH_FCS_LEN + VLAN_HLEN;
2321 vsi->max_frame = I40E_RXBUFFER_2048;
2323 /* figure out correct receive buffer length */
2324 switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
2325 I40E_FLAG_RX_PS_ENABLED)) {
2326 case I40E_FLAG_RX_1BUF_ENABLED:
2327 vsi->rx_hdr_len = 0;
2328 vsi->rx_buf_len = vsi->max_frame;
2329 vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
2331 case I40E_FLAG_RX_PS_ENABLED:
2332 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2333 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2334 vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
2337 vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
2338 vsi->rx_buf_len = I40E_RXBUFFER_2048;
2339 vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
2343 /* round up for the chip's needs */
2344 vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
2345 (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
2346 vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
2347 (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
2349 /* set up individual rings */
2350 for (i = 0; i < vsi->num_queue_pairs && !err; i++)
2351 err = i40e_configure_rx_ring(vsi->rx_rings[i]);
2357 * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
2358 * @vsi: ptr to the VSI
2360 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
2362 u16 qoffset, qcount;
2365 if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
2368 for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
2369 if (!(vsi->tc_config.enabled_tc & (1 << n)))
2372 qoffset = vsi->tc_config.tc_info[n].qoffset;
2373 qcount = vsi->tc_config.tc_info[n].qcount;
2374 for (i = qoffset; i < (qoffset + qcount); i++) {
2375 struct i40e_ring *rx_ring = vsi->rx_rings[i];
2376 struct i40e_ring *tx_ring = vsi->tx_rings[i];
2377 rx_ring->dcb_tc = n;
2378 tx_ring->dcb_tc = n;
2384 * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
2385 * @vsi: ptr to the VSI
2387 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
2390 i40e_set_rx_mode(vsi->netdev);
2394 * i40e_vsi_configure - Set up the VSI for action
2395 * @vsi: the VSI being configured
2397 static int i40e_vsi_configure(struct i40e_vsi *vsi)
2401 i40e_set_vsi_rx_mode(vsi);
2402 i40e_restore_vlan(vsi);
2403 i40e_vsi_config_dcb_rings(vsi);
2404 err = i40e_vsi_configure_tx(vsi);
2406 err = i40e_vsi_configure_rx(vsi);
2412 * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
2413 * @vsi: the VSI being configured
2415 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
2417 struct i40e_pf *pf = vsi->back;
2418 struct i40e_q_vector *q_vector;
2419 struct i40e_hw *hw = &pf->hw;
2425 /* The interrupt indexing is offset by 1 in the PFINT_ITRn
2426 * and PFINT_LNKLSTn registers, e.g.:
2427 * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
2429 qp = vsi->base_queue;
2430 vector = vsi->base_vector;
2431 for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
2432 q_vector = vsi->q_vectors[i];
2433 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2434 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2435 wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
2437 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2438 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2439 wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
2442 /* Linked list for the queuepairs assigned to this vector */
2443 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
2444 for (q = 0; q < q_vector->num_ringpairs; q++) {
2445 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2446 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2447 (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
2448 (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
2450 << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
2452 wr32(hw, I40E_QINT_RQCTL(qp), val);
2454 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2455 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2456 (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
2457 ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
2459 << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2461 /* Terminate the linked list */
2462 if (q == (q_vector->num_ringpairs - 1))
2463 val |= (I40E_QUEUE_END_OF_LIST
2464 << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2466 wr32(hw, I40E_QINT_TQCTL(qp), val);
2475 * i40e_enable_misc_int_causes - enable the non-queue interrupts
2476 * @hw: ptr to the hardware info
2478 static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
2482 /* clear things first */
2483 wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
2484 rd32(hw, I40E_PFINT_ICR0); /* read to clear */
2486 val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
2487 I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
2488 I40E_PFINT_ICR0_ENA_GRST_MASK |
2489 I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
2490 I40E_PFINT_ICR0_ENA_GPIO_MASK |
2491 I40E_PFINT_ICR0_ENA_STORM_DETECT_MASK |
2492 I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
2493 I40E_PFINT_ICR0_ENA_VFLR_MASK |
2494 I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2496 wr32(hw, I40E_PFINT_ICR0_ENA, val);
2498 /* SW_ITR_IDX = 0, but don't change INTENA */
2499 wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
2500 I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
2502 /* OTHER_ITR_IDX = 0 */
2503 wr32(hw, I40E_PFINT_STAT_CTL0, 0);
2507 * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
2508 * @vsi: the VSI being configured
2510 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
2512 struct i40e_q_vector *q_vector = vsi->q_vectors[0];
2513 struct i40e_pf *pf = vsi->back;
2514 struct i40e_hw *hw = &pf->hw;
2517 /* set the ITR configuration */
2518 q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
2519 q_vector->rx.latency_range = I40E_LOW_LATENCY;
2520 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
2521 q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
2522 q_vector->tx.latency_range = I40E_LOW_LATENCY;
2523 wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
2525 i40e_enable_misc_int_causes(hw);
2527 /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
2528 wr32(hw, I40E_PFINT_LNKLST0, 0);
2530 /* Associate the queue pair to the vector and enable the q int */
2531 val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
2532 (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
2533 (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
2535 wr32(hw, I40E_QINT_RQCTL(0), val);
2537 val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
2538 (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
2539 (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
2541 wr32(hw, I40E_QINT_TQCTL(0), val);
2546 * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
2547 * @pf: board private structure
2549 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
2551 struct i40e_hw *hw = &pf->hw;
2553 wr32(hw, I40E_PFINT_DYN_CTL0,
2554 I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2559 * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
2560 * @pf: board private structure
2562 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
2564 struct i40e_hw *hw = &pf->hw;
2567 val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
2568 I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
2569 (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
2571 wr32(hw, I40E_PFINT_DYN_CTL0, val);
2576 * i40e_irq_dynamic_enable - Enable default interrupt generation settings
2577 * @vsi: pointer to a vsi
2578 * @vector: enable a particular Hw Interrupt vector
2580 void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
2582 struct i40e_pf *pf = vsi->back;
2583 struct i40e_hw *hw = &pf->hw;
2586 val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
2587 I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
2588 (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
2589 wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
2590 /* skip the flush */
2594 * i40e_msix_clean_rings - MSIX mode Interrupt Handler
2595 * @irq: interrupt number
2596 * @data: pointer to a q_vector
2598 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
2600 struct i40e_q_vector *q_vector = data;
2602 if (!q_vector->tx.ring && !q_vector->rx.ring)
2605 napi_schedule(&q_vector->napi);
2611 * i40e_fdir_clean_rings - Interrupt Handler for FDIR rings
2612 * @irq: interrupt number
2613 * @data: pointer to a q_vector
2615 static irqreturn_t i40e_fdir_clean_rings(int irq, void *data)
2617 struct i40e_q_vector *q_vector = data;
2619 if (!q_vector->tx.ring && !q_vector->rx.ring)
2622 pr_info("fdir ring cleaning needed\n");
2628 * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
2629 * @vsi: the VSI being configured
2630 * @basename: name for the vector
2632 * Allocates MSI-X vectors and requests interrupts from the kernel.
2634 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
2636 int q_vectors = vsi->num_q_vectors;
2637 struct i40e_pf *pf = vsi->back;
2638 int base = vsi->base_vector;
2643 for (vector = 0; vector < q_vectors; vector++) {
2644 struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
2646 if (q_vector->tx.ring && q_vector->rx.ring) {
2647 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2648 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
2650 } else if (q_vector->rx.ring) {
2651 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2652 "%s-%s-%d", basename, "rx", rx_int_idx++);
2653 } else if (q_vector->tx.ring) {
2654 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
2655 "%s-%s-%d", basename, "tx", tx_int_idx++);
2657 /* skip this unused q_vector */
2660 err = request_irq(pf->msix_entries[base + vector].vector,
2666 dev_info(&pf->pdev->dev,
2667 "%s: request_irq failed, error: %d\n",
2669 goto free_queue_irqs;
2671 /* assign the mask for this irq */
2672 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2673 &q_vector->affinity_mask);
2681 irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
2683 free_irq(pf->msix_entries[base + vector].vector,
2684 &(vsi->q_vectors[vector]));
2690 * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
2691 * @vsi: the VSI being un-configured
2693 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
2695 struct i40e_pf *pf = vsi->back;
2696 struct i40e_hw *hw = &pf->hw;
2697 int base = vsi->base_vector;
2700 for (i = 0; i < vsi->num_queue_pairs; i++) {
2701 wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
2702 wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
2705 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2706 for (i = vsi->base_vector;
2707 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2708 wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
2711 for (i = 0; i < vsi->num_q_vectors; i++)
2712 synchronize_irq(pf->msix_entries[i + base].vector);
2714 /* Legacy and MSI mode - this stops all interrupt handling */
2715 wr32(hw, I40E_PFINT_ICR0_ENA, 0);
2716 wr32(hw, I40E_PFINT_DYN_CTL0, 0);
2718 synchronize_irq(pf->pdev->irq);
2723 * i40e_vsi_enable_irq - Enable IRQ for the given VSI
2724 * @vsi: the VSI being configured
2726 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
2728 struct i40e_pf *pf = vsi->back;
2731 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2732 for (i = vsi->base_vector;
2733 i < (vsi->num_q_vectors + vsi->base_vector); i++)
2734 i40e_irq_dynamic_enable(vsi, i);
2736 i40e_irq_dynamic_enable_icr0(pf);
2739 i40e_flush(&pf->hw);
2744 * i40e_stop_misc_vector - Stop the vector that handles non-queue events
2745 * @pf: board private structure
2747 static void i40e_stop_misc_vector(struct i40e_pf *pf)
2750 wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
2751 i40e_flush(&pf->hw);
2755 * i40e_intr - MSI/Legacy and non-queue interrupt handler
2756 * @irq: interrupt number
2757 * @data: pointer to a q_vector
2759 * This is the handler used for all MSI/Legacy interrupts, and deals
2760 * with both queue and non-queue interrupts. This is also used in
2761 * MSIX mode to handle the non-queue interrupts.
2763 static irqreturn_t i40e_intr(int irq, void *data)
2765 struct i40e_pf *pf = (struct i40e_pf *)data;
2766 struct i40e_hw *hw = &pf->hw;
2767 irqreturn_t ret = IRQ_NONE;
2768 u32 icr0, icr0_remaining;
2771 icr0 = rd32(hw, I40E_PFINT_ICR0);
2772 ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
2774 /* if sharing a legacy IRQ, we might get called w/o an intr pending */
2775 if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
2778 /* if interrupt but no bits showing, must be SWINT */
2779 if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
2780 (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
2783 /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
2784 if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
2786 /* temporarily disable queue cause for NAPI processing */
2787 u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
2788 qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
2789 wr32(hw, I40E_QINT_RQCTL(0), qval);
2791 qval = rd32(hw, I40E_QINT_TQCTL(0));
2792 qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
2793 wr32(hw, I40E_QINT_TQCTL(0), qval);
2795 if (!test_bit(__I40E_DOWN, &pf->state))
2796 napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
2799 if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
2800 ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
2801 set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
2804 if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
2805 ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
2806 set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
2809 if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
2810 ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
2811 set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
2814 if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
2815 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
2816 set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
2817 ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
2818 val = rd32(hw, I40E_GLGEN_RSTAT);
2819 val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
2820 >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
2821 if (val == I40E_RESET_CORER)
2823 else if (val == I40E_RESET_GLOBR)
2825 else if (val == I40E_RESET_EMPR)
2829 if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
2830 icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
2831 dev_info(&pf->pdev->dev, "HMC error interrupt\n");
2834 /* If a critical error is pending we have no choice but to reset the
2836 * Report and mask out any remaining unexpected interrupts.
2838 icr0_remaining = icr0 & ena_mask;
2839 if (icr0_remaining) {
2840 dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
2842 if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
2843 (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
2844 (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK) ||
2845 (icr0_remaining & I40E_PFINT_ICR0_MAL_DETECT_MASK)) {
2846 dev_info(&pf->pdev->dev, "device will be reset\n");
2847 set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
2848 i40e_service_event_schedule(pf);
2850 ena_mask &= ~icr0_remaining;
2855 /* re-enable interrupt causes */
2856 wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
2857 if (!test_bit(__I40E_DOWN, &pf->state)) {
2858 i40e_service_event_schedule(pf);
2859 i40e_irq_dynamic_enable_icr0(pf);
2866 * i40e_map_vector_to_qp - Assigns the queue pair to the vector
2867 * @vsi: the VSI being configured
2868 * @v_idx: vector index
2869 * @qp_idx: queue pair index
2871 static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
2873 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
2874 struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
2875 struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
2877 tx_ring->q_vector = q_vector;
2878 tx_ring->next = q_vector->tx.ring;
2879 q_vector->tx.ring = tx_ring;
2880 q_vector->tx.count++;
2882 rx_ring->q_vector = q_vector;
2883 rx_ring->next = q_vector->rx.ring;
2884 q_vector->rx.ring = rx_ring;
2885 q_vector->rx.count++;
2889 * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
2890 * @vsi: the VSI being configured
2892 * This function maps descriptor rings to the queue-specific vectors
2893 * we were allotted through the MSI-X enabling code. Ideally, we'd have
2894 * one vector per queue pair, but on a constrained vector budget, we
2895 * group the queue pairs as "efficiently" as possible.
2897 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
2899 int qp_remaining = vsi->num_queue_pairs;
2900 int q_vectors = vsi->num_q_vectors;
2905 /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
2906 * group them so there are multiple queues per vector.
2908 for (; v_start < q_vectors && qp_remaining; v_start++) {
2909 struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
2911 num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
2913 q_vector->num_ringpairs = num_ringpairs;
2915 q_vector->rx.count = 0;
2916 q_vector->tx.count = 0;
2917 q_vector->rx.ring = NULL;
2918 q_vector->tx.ring = NULL;
2920 while (num_ringpairs--) {
2921 map_vector_to_qp(vsi, v_start, qp_idx);
2929 * i40e_vsi_request_irq - Request IRQ from the OS
2930 * @vsi: the VSI being configured
2931 * @basename: name for the vector
2933 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
2935 struct i40e_pf *pf = vsi->back;
2938 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2939 err = i40e_vsi_request_irq_msix(vsi, basename);
2940 else if (pf->flags & I40E_FLAG_MSI_ENABLED)
2941 err = request_irq(pf->pdev->irq, i40e_intr, 0,
2942 pf->misc_int_name, pf);
2944 err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
2945 pf->misc_int_name, pf);
2948 dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
2953 #ifdef CONFIG_NET_POLL_CONTROLLER
2955 * i40e_netpoll - A Polling 'interrupt'handler
2956 * @netdev: network interface device structure
2958 * This is used by netconsole to send skbs without having to re-enable
2959 * interrupts. It's not called while the normal interrupt routine is executing.
2961 static void i40e_netpoll(struct net_device *netdev)
2963 struct i40e_netdev_priv *np = netdev_priv(netdev);
2964 struct i40e_vsi *vsi = np->vsi;
2965 struct i40e_pf *pf = vsi->back;
2968 /* if interface is down do nothing */
2969 if (test_bit(__I40E_DOWN, &vsi->state))
2972 pf->flags |= I40E_FLAG_IN_NETPOLL;
2973 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
2974 for (i = 0; i < vsi->num_q_vectors; i++)
2975 i40e_msix_clean_rings(0, vsi->q_vectors[i]);
2977 i40e_intr(pf->pdev->irq, netdev);
2979 pf->flags &= ~I40E_FLAG_IN_NETPOLL;
2984 * i40e_vsi_control_tx - Start or stop a VSI's rings
2985 * @vsi: the VSI being configured
2986 * @enable: start or stop the rings
2988 static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
2990 struct i40e_pf *pf = vsi->back;
2991 struct i40e_hw *hw = &pf->hw;
2995 pf_q = vsi->base_queue;
2996 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
2999 usleep_range(1000, 2000);
3000 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
3001 } while (j-- && ((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT)
3002 ^ (tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT)) & 1);
3004 /* Skip if the queue is already in the requested state */
3005 if (enable && (tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3007 if (!enable && !(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3010 /* turn on/off the queue */
3012 tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK |
3013 I40E_QTX_ENA_QENA_STAT_MASK;
3015 tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
3017 wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
3019 /* wait for the change to finish */
3020 for (j = 0; j < 10; j++) {
3021 tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
3023 if ((tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3026 if (!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
3033 dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
3034 pf_q, (enable ? "en" : "dis"));
3039 if (hw->revision_id == 0)
3046 * i40e_vsi_control_rx - Start or stop a VSI's rings
3047 * @vsi: the VSI being configured
3048 * @enable: start or stop the rings
3050 static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
3052 struct i40e_pf *pf = vsi->back;
3053 struct i40e_hw *hw = &pf->hw;
3057 pf_q = vsi->base_queue;
3058 for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
3061 usleep_range(1000, 2000);
3062 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3063 } while (j-- && ((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT)
3064 ^ (rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT)) & 1);
3068 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3071 /* is !STAT set ? */
3072 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3076 /* turn on/off the queue */
3078 rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK |
3079 I40E_QRX_ENA_QENA_STAT_MASK;
3081 rx_reg &= ~(I40E_QRX_ENA_QENA_REQ_MASK |
3082 I40E_QRX_ENA_QENA_STAT_MASK);
3083 wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
3085 /* wait for the change to finish */
3086 for (j = 0; j < 10; j++) {
3087 rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
3090 if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3093 if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
3100 dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
3101 pf_q, (enable ? "en" : "dis"));
3110 * i40e_vsi_control_rings - Start or stop a VSI's rings
3111 * @vsi: the VSI being configured
3112 * @enable: start or stop the rings
3114 int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
3118 /* do rx first for enable and last for disable */
3120 ret = i40e_vsi_control_rx(vsi, request);
3123 ret = i40e_vsi_control_tx(vsi, request);
3125 ret = i40e_vsi_control_tx(vsi, request);
3128 ret = i40e_vsi_control_rx(vsi, request);
3135 * i40e_vsi_free_irq - Free the irq association with the OS
3136 * @vsi: the VSI being configured
3138 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
3140 struct i40e_pf *pf = vsi->back;
3141 struct i40e_hw *hw = &pf->hw;
3142 int base = vsi->base_vector;
3146 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3147 if (!vsi->q_vectors)
3150 for (i = 0; i < vsi->num_q_vectors; i++) {
3151 u16 vector = i + base;
3153 /* free only the irqs that were actually requested */
3154 if (!vsi->q_vectors[i] ||
3155 !vsi->q_vectors[i]->num_ringpairs)
3158 /* clear the affinity_mask in the IRQ descriptor */
3159 irq_set_affinity_hint(pf->msix_entries[vector].vector,
3161 free_irq(pf->msix_entries[vector].vector,
3164 /* Tear down the interrupt queue link list
3166 * We know that they come in pairs and always
3167 * the Rx first, then the Tx. To clear the
3168 * link list, stick the EOL value into the
3169 * next_q field of the registers.
3171 val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
3172 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3173 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3174 val |= I40E_QUEUE_END_OF_LIST
3175 << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3176 wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
3178 while (qp != I40E_QUEUE_END_OF_LIST) {
3181 val = rd32(hw, I40E_QINT_RQCTL(qp));
3183 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3184 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3185 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3186 I40E_QINT_RQCTL_INTEVENT_MASK);
3188 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3189 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3191 wr32(hw, I40E_QINT_RQCTL(qp), val);
3193 val = rd32(hw, I40E_QINT_TQCTL(qp));
3195 next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
3196 >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
3198 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3199 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3200 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3201 I40E_QINT_TQCTL_INTEVENT_MASK);
3203 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3204 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3206 wr32(hw, I40E_QINT_TQCTL(qp), val);
3211 free_irq(pf->pdev->irq, pf);
3213 val = rd32(hw, I40E_PFINT_LNKLST0);
3214 qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
3215 >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
3216 val |= I40E_QUEUE_END_OF_LIST
3217 << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
3218 wr32(hw, I40E_PFINT_LNKLST0, val);
3220 val = rd32(hw, I40E_QINT_RQCTL(qp));
3221 val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
3222 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
3223 I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3224 I40E_QINT_RQCTL_INTEVENT_MASK);
3226 val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
3227 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
3229 wr32(hw, I40E_QINT_RQCTL(qp), val);
3231 val = rd32(hw, I40E_QINT_TQCTL(qp));
3233 val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
3234 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
3235 I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3236 I40E_QINT_TQCTL_INTEVENT_MASK);
3238 val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
3239 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
3241 wr32(hw, I40E_QINT_TQCTL(qp), val);
3246 * i40e_free_q_vector - Free memory allocated for specific interrupt vector
3247 * @vsi: the VSI being configured
3248 * @v_idx: Index of vector to be freed
3250 * This function frees the memory allocated to the q_vector. In addition if
3251 * NAPI is enabled it will delete any references to the NAPI struct prior
3252 * to freeing the q_vector.
3254 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
3256 struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
3257 struct i40e_ring *ring;
3262 /* disassociate q_vector from rings */
3263 i40e_for_each_ring(ring, q_vector->tx)
3264 ring->q_vector = NULL;
3266 i40e_for_each_ring(ring, q_vector->rx)
3267 ring->q_vector = NULL;
3269 /* only VSI w/ an associated netdev is set up w/ NAPI */
3271 netif_napi_del(&q_vector->napi);
3273 vsi->q_vectors[v_idx] = NULL;
3275 kfree_rcu(q_vector, rcu);
3279 * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
3280 * @vsi: the VSI being un-configured
3282 * This frees the memory allocated to the q_vectors and
3283 * deletes references to the NAPI struct.
3285 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
3289 for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
3290 i40e_free_q_vector(vsi, v_idx);
3294 * i40e_reset_interrupt_capability - Disable interrupt setup in OS
3295 * @pf: board private structure
3297 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
3299 /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
3300 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
3301 pci_disable_msix(pf->pdev);
3302 kfree(pf->msix_entries);
3303 pf->msix_entries = NULL;
3304 } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
3305 pci_disable_msi(pf->pdev);
3307 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
3311 * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
3312 * @pf: board private structure
3314 * We go through and clear interrupt specific resources and reset the structure
3315 * to pre-load conditions
3317 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
3321 i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
3322 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
3324 i40e_vsi_free_q_vectors(pf->vsi[i]);
3325 i40e_reset_interrupt_capability(pf);
3329 * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
3330 * @vsi: the VSI being configured
3332 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
3339 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
3340 napi_enable(&vsi->q_vectors[q_idx]->napi);
3344 * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
3345 * @vsi: the VSI being configured
3347 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
3354 for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
3355 napi_disable(&vsi->q_vectors[q_idx]->napi);
3359 * i40e_quiesce_vsi - Pause a given VSI
3360 * @vsi: the VSI being paused
3362 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
3364 if (test_bit(__I40E_DOWN, &vsi->state))
3367 set_bit(__I40E_NEEDS_RESTART, &vsi->state);
3368 if (vsi->netdev && netif_running(vsi->netdev)) {
3369 vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
3371 set_bit(__I40E_DOWN, &vsi->state);
3377 * i40e_unquiesce_vsi - Resume a given VSI
3378 * @vsi: the VSI being resumed
3380 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
3382 if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
3385 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
3386 if (vsi->netdev && netif_running(vsi->netdev))
3387 vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
3389 i40e_up(vsi); /* this clears the DOWN bit */
3393 * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
3396 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
3400 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3402 i40e_quiesce_vsi(pf->vsi[v]);
3407 * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
3410 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
3414 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
3416 i40e_unquiesce_vsi(pf->vsi[v]);
3421 * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
3422 * @dcbcfg: the corresponding DCBx configuration structure
3424 * Return the number of TCs from given DCBx configuration
3426 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
3431 /* Scan the ETS Config Priority Table to find
3432 * traffic class enabled for a given priority
3433 * and use the traffic class index to get the
3434 * number of traffic classes enabled
3436 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3437 if (dcbcfg->etscfg.prioritytable[i] > num_tc)
3438 num_tc = dcbcfg->etscfg.prioritytable[i];
3441 /* Traffic class index starts from zero so
3442 * increment to return the actual count
3448 * i40e_dcb_get_enabled_tc - Get enabled traffic classes
3449 * @dcbcfg: the corresponding DCBx configuration structure
3451 * Query the current DCB configuration and return the number of
3452 * traffic classes enabled from the given DCBX config
3454 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
3456 u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
3460 for (i = 0; i < num_tc; i++)
3461 enabled_tc |= 1 << i;
3467 * i40e_pf_get_num_tc - Get enabled traffic classes for PF
3468 * @pf: PF being queried
3470 * Return number of traffic classes enabled for the given PF
3472 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
3474 struct i40e_hw *hw = &pf->hw;
3477 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3479 /* If DCB is not enabled then always in single TC */
3480 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3483 /* MFP mode return count of enabled TCs for this PF */
3484 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3485 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3486 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3487 if (enabled_tc & (1 << i))
3493 /* SFP mode will be enabled for all TCs on port */
3494 return i40e_dcb_get_num_tc(dcbcfg);
3498 * i40e_pf_get_default_tc - Get bitmap for first enabled TC
3499 * @pf: PF being queried
3501 * Return a bitmap for first enabled traffic class for this PF.
3503 static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
3505 u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
3509 return 0x1; /* TC0 */
3511 /* Find the first enabled TC */
3512 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3513 if (enabled_tc & (1 << i))
3521 * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
3522 * @pf: PF being queried
3524 * Return a bitmap for enabled traffic classes for this PF.
3526 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
3528 /* If DCB is not enabled for this PF then just return default TC */
3529 if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
3530 return i40e_pf_get_default_tc(pf);
3532 /* MFP mode will have enabled TCs set by FW */
3533 if (pf->flags & I40E_FLAG_MFP_ENABLED)
3534 return pf->hw.func_caps.enabled_tcmap;
3536 /* SFP mode we want PF to be enabled for all TCs */
3537 return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
3541 * i40e_vsi_get_bw_info - Query VSI BW Information
3542 * @vsi: the VSI being queried
3544 * Returns 0 on success, negative value on failure
3546 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
3548 struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
3549 struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
3550 struct i40e_pf *pf = vsi->back;
3551 struct i40e_hw *hw = &pf->hw;
3556 /* Get the VSI level BW configuration */
3557 aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
3559 dev_info(&pf->pdev->dev,
3560 "couldn't get pf vsi bw config, err %d, aq_err %d\n",
3561 aq_ret, pf->hw.aq.asq_last_status);
3565 /* Get the VSI level BW configuration per TC */
3566 aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
3569 dev_info(&pf->pdev->dev,
3570 "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
3571 aq_ret, pf->hw.aq.asq_last_status);
3575 if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
3576 dev_info(&pf->pdev->dev,
3577 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
3578 bw_config.tc_valid_bits,
3579 bw_ets_config.tc_valid_bits);
3580 /* Still continuing */
3583 vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
3584 vsi->bw_max_quanta = bw_config.max_bw;
3585 tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
3586 (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
3587 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3588 vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
3589 vsi->bw_ets_limit_credits[i] =
3590 le16_to_cpu(bw_ets_config.credits[i]);
3591 /* 3 bits out of 4 for each TC */
3592 vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
3599 * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
3600 * @vsi: the VSI being configured
3601 * @enabled_tc: TC bitmap
3602 * @bw_credits: BW shared credits per TC
3604 * Returns 0 on success, negative value on failure
3606 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
3609 struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
3613 bw_data.tc_valid_bits = enabled_tc;
3614 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3615 bw_data.tc_bw_credits[i] = bw_share[i];
3617 aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
3620 dev_info(&vsi->back->pdev->dev,
3621 "%s: AQ command Config VSI BW allocation per TC failed = %d\n",
3622 __func__, vsi->back->hw.aq.asq_last_status);
3626 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
3627 vsi->info.qs_handle[i] = bw_data.qs_handles[i];
3633 * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
3634 * @vsi: the VSI being configured
3635 * @enabled_tc: TC map to be enabled
3638 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3640 struct net_device *netdev = vsi->netdev;
3641 struct i40e_pf *pf = vsi->back;
3642 struct i40e_hw *hw = &pf->hw;
3645 struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
3651 netdev_reset_tc(netdev);
3655 /* Set up actual enabled TCs on the VSI */
3656 if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
3659 /* set per TC queues for the VSI */
3660 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3661 /* Only set TC queues for enabled tcs
3663 * e.g. For a VSI that has TC0 and TC3 enabled the
3664 * enabled_tc bitmap would be 0x00001001; the driver
3665 * will set the numtc for netdev as 2 that will be
3666 * referenced by the netdev layer as TC 0 and 1.
3668 if (vsi->tc_config.enabled_tc & (1 << i))
3669 netdev_set_tc_queue(netdev,
3670 vsi->tc_config.tc_info[i].netdev_tc,
3671 vsi->tc_config.tc_info[i].qcount,
3672 vsi->tc_config.tc_info[i].qoffset);
3675 /* Assign UP2TC map for the VSI */
3676 for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
3677 /* Get the actual TC# for the UP */
3678 u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
3679 /* Get the mapped netdev TC# for the UP */
3680 netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
3681 netdev_set_prio_tc_map(netdev, i, netdev_tc);
3686 * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
3687 * @vsi: the VSI being configured
3688 * @ctxt: the ctxt buffer returned from AQ VSI update param command
3690 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
3691 struct i40e_vsi_context *ctxt)
3693 /* copy just the sections touched not the entire info
3694 * since not all sections are valid as returned by
3697 vsi->info.mapping_flags = ctxt->info.mapping_flags;
3698 memcpy(&vsi->info.queue_mapping,
3699 &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
3700 memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
3701 sizeof(vsi->info.tc_mapping));
3705 * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
3706 * @vsi: VSI to be configured
3707 * @enabled_tc: TC bitmap
3709 * This configures a particular VSI for TCs that are mapped to the
3710 * given TC bitmap. It uses default bandwidth share for TCs across
3711 * VSIs to configure TC for a particular VSI.
3714 * It is expected that the VSI queues have been quisced before calling
3717 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
3719 u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
3720 struct i40e_vsi_context ctxt;
3724 /* Check if enabled_tc is same as existing or new TCs */
3725 if (vsi->tc_config.enabled_tc == enabled_tc)
3728 /* Enable ETS TCs with equal BW Share for now across all VSIs */
3729 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
3730 if (enabled_tc & (1 << i))
3734 ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
3736 dev_info(&vsi->back->pdev->dev,
3737 "Failed configuring TC map %d for VSI %d\n",
3738 enabled_tc, vsi->seid);
3742 /* Update Queue Pairs Mapping for currently enabled UPs */
3743 ctxt.seid = vsi->seid;
3744 ctxt.pf_num = vsi->back->hw.pf_id;
3746 ctxt.uplink_seid = vsi->uplink_seid;
3747 memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
3748 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
3750 /* Update the VSI after updating the VSI queue-mapping information */
3751 ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3753 dev_info(&vsi->back->pdev->dev,
3754 "update vsi failed, aq_err=%d\n",
3755 vsi->back->hw.aq.asq_last_status);
3758 /* update the local VSI info with updated queue map */
3759 i40e_vsi_update_queue_map(vsi, &ctxt);
3760 vsi->info.valid_sections = 0;
3762 /* Update current VSI BW information */
3763 ret = i40e_vsi_get_bw_info(vsi);
3765 dev_info(&vsi->back->pdev->dev,
3766 "Failed updating vsi bw info, aq_err=%d\n",
3767 vsi->back->hw.aq.asq_last_status);
3771 /* Update the netdev TC setup */
3772 i40e_vsi_config_netdev_tc(vsi, enabled_tc);
3778 * i40e_up_complete - Finish the last steps of bringing up a connection
3779 * @vsi: the VSI being configured
3781 static int i40e_up_complete(struct i40e_vsi *vsi)
3783 struct i40e_pf *pf = vsi->back;
3786 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
3787 i40e_vsi_configure_msix(vsi);
3789 i40e_configure_msi_and_legacy(vsi);
3792 err = i40e_vsi_control_rings(vsi, true);
3796 clear_bit(__I40E_DOWN, &vsi->state);
3797 i40e_napi_enable_all(vsi);
3798 i40e_vsi_enable_irq(vsi);
3800 if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
3802 netdev_info(vsi->netdev, "NIC Link is Up\n");
3803 netif_tx_start_all_queues(vsi->netdev);
3804 netif_carrier_on(vsi->netdev);
3805 } else if (vsi->netdev) {
3806 netdev_info(vsi->netdev, "NIC Link is Down\n");
3808 i40e_service_event_schedule(pf);
3814 * i40e_vsi_reinit_locked - Reset the VSI
3815 * @vsi: the VSI being configured
3817 * Rebuild the ring structs after some configuration
3818 * has changed, e.g. MTU size.
3820 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
3822 struct i40e_pf *pf = vsi->back;
3824 WARN_ON(in_interrupt());
3825 while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
3826 usleep_range(1000, 2000);
3829 /* Give a VF some time to respond to the reset. The
3830 * two second wait is based upon the watchdog cycle in
3833 if (vsi->type == I40E_VSI_SRIOV)
3836 clear_bit(__I40E_CONFIG_BUSY, &pf->state);
3840 * i40e_up - Bring the connection back up after being down
3841 * @vsi: the VSI being configured
3843 int i40e_up(struct i40e_vsi *vsi)
3847 err = i40e_vsi_configure(vsi);
3849 err = i40e_up_complete(vsi);
3855 * i40e_down - Shutdown the connection processing
3856 * @vsi: the VSI being stopped
3858 void i40e_down(struct i40e_vsi *vsi)
3862 /* It is assumed that the caller of this function
3863 * sets the vsi->state __I40E_DOWN bit.
3866 netif_carrier_off(vsi->netdev);
3867 netif_tx_disable(vsi->netdev);
3869 i40e_vsi_disable_irq(vsi);
3870 i40e_vsi_control_rings(vsi, false);
3871 i40e_napi_disable_all(vsi);
3873 for (i = 0; i < vsi->num_queue_pairs; i++) {
3874 i40e_clean_tx_ring(vsi->tx_rings[i]);
3875 i40e_clean_rx_ring(vsi->rx_rings[i]);
3880 * i40e_setup_tc - configure multiple traffic classes
3881 * @netdev: net device to configure
3882 * @tc: number of traffic classes to enable
3884 static int i40e_setup_tc(struct net_device *netdev, u8 tc)
3886 struct i40e_netdev_priv *np = netdev_priv(netdev);
3887 struct i40e_vsi *vsi = np->vsi;
3888 struct i40e_pf *pf = vsi->back;
3893 /* Check if DCB enabled to continue */
3894 if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
3895 netdev_info(netdev, "DCB is not enabled for adapter\n");
3899 /* Check if MFP enabled */
3900 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
3901 netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
3905 /* Check whether tc count is within enabled limit */
3906 if (tc > i40e_pf_get_num_tc(pf)) {
3907 netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
3911 /* Generate TC map for number of tc requested */
3912 for (i = 0; i < tc; i++)
3913 enabled_tc |= (1 << i);
3915 /* Requesting same TC configuration as already enabled */
3916 if (enabled_tc == vsi->tc_config.enabled_tc)
3919 /* Quiesce VSI queues */
3920 i40e_quiesce_vsi(vsi);
3922 /* Configure VSI for enabled TCs */
3923 ret = i40e_vsi_config_tc(vsi, enabled_tc);
3925 netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
3931 i40e_unquiesce_vsi(vsi);
3938 * i40e_open - Called when a network interface is made active
3939 * @netdev: network interface device structure
3941 * The open entry point is called when a network interface is made
3942 * active by the system (IFF_UP). At this point all resources needed
3943 * for transmit and receive operations are allocated, the interrupt
3944 * handler is registered with the OS, the netdev watchdog subtask is
3945 * enabled, and the stack is notified that the interface is ready.
3947 * Returns 0 on success, negative value on failure
3949 static int i40e_open(struct net_device *netdev)
3951 struct i40e_netdev_priv *np = netdev_priv(netdev);
3952 struct i40e_vsi *vsi = np->vsi;
3953 struct i40e_pf *pf = vsi->back;
3954 char int_name[IFNAMSIZ];
3957 /* disallow open during test */
3958 if (test_bit(__I40E_TESTING, &pf->state))
3961 netif_carrier_off(netdev);
3963 /* allocate descriptors */
3964 err = i40e_vsi_setup_tx_resources(vsi);
3967 err = i40e_vsi_setup_rx_resources(vsi);
3971 err = i40e_vsi_configure(vsi);
3975 snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
3976 dev_driver_string(&pf->pdev->dev), netdev->name);
3977 err = i40e_vsi_request_irq(vsi, int_name);
3981 /* Notify the stack of the actual queue counts. */
3982 err = netif_set_real_num_tx_queues(netdev, vsi->num_queue_pairs);
3984 goto err_set_queues;
3986 err = netif_set_real_num_rx_queues(netdev, vsi->num_queue_pairs);
3988 goto err_set_queues;
3990 err = i40e_up_complete(vsi);
3992 goto err_up_complete;
3994 #ifdef CONFIG_I40E_VXLAN
3995 vxlan_get_rx_port(netdev);
4003 i40e_vsi_free_irq(vsi);
4005 i40e_vsi_free_rx_resources(vsi);
4007 i40e_vsi_free_tx_resources(vsi);
4008 if (vsi == pf->vsi[pf->lan_vsi])
4009 i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
4015 * i40e_close - Disables a network interface
4016 * @netdev: network interface device structure
4018 * The close entry point is called when an interface is de-activated
4019 * by the OS. The hardware is still under the driver's control, but
4020 * this netdev interface is disabled.
4022 * Returns 0, this is not allowed to fail
4024 static int i40e_close(struct net_device *netdev)
4026 struct i40e_netdev_priv *np = netdev_priv(netdev);
4027 struct i40e_vsi *vsi = np->vsi;
4029 if (test_and_set_bit(__I40E_DOWN, &vsi->state))
4033 i40e_vsi_free_irq(vsi);
4035 i40e_vsi_free_tx_resources(vsi);
4036 i40e_vsi_free_rx_resources(vsi);
4042 * i40e_do_reset - Start a PF or Core Reset sequence
4043 * @pf: board private structure
4044 * @reset_flags: which reset is requested
4046 * The essential difference in resets is that the PF Reset
4047 * doesn't clear the packet buffers, doesn't reset the PE
4048 * firmware, and doesn't bother the other PFs on the chip.
4050 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
4054 WARN_ON(in_interrupt());
4056 /* do the biggest reset indicated */
4057 if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
4059 /* Request a Global Reset
4061 * This will start the chip's countdown to the actual full
4062 * chip reset event, and a warning interrupt to be sent
4063 * to all PFs, including the requestor. Our handler
4064 * for the warning interrupt will deal with the shutdown
4065 * and recovery of the switch setup.
4067 dev_info(&pf->pdev->dev, "GlobalR requested\n");
4068 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4069 val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
4070 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4072 } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
4074 /* Request a Core Reset
4076 * Same as Global Reset, except does *not* include the MAC/PHY
4078 dev_info(&pf->pdev->dev, "CoreR requested\n");
4079 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4080 val |= I40E_GLGEN_RTRIG_CORER_MASK;
4081 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4082 i40e_flush(&pf->hw);
4084 } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
4086 /* Request a Firmware Reset
4088 * Same as Global reset, plus restarting the
4089 * embedded firmware engine.
4091 /* enable EMP Reset */
4092 val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
4093 val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
4094 wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
4096 /* force the reset */
4097 val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
4098 val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
4099 wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
4100 i40e_flush(&pf->hw);
4102 } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
4104 /* Request a PF Reset
4106 * Resets only the PF-specific registers
4108 * This goes directly to the tear-down and rebuild of
4109 * the switch, since we need to do all the recovery as
4110 * for the Core Reset.
4112 dev_info(&pf->pdev->dev, "PFR requested\n");
4113 i40e_handle_reset_warning(pf);
4115 } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
4118 /* Find the VSI(s) that requested a re-init */
4119 dev_info(&pf->pdev->dev,
4120 "VSI reinit requested\n");
4121 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4122 struct i40e_vsi *vsi = pf->vsi[v];
4124 test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
4125 i40e_vsi_reinit_locked(pf->vsi[v]);
4126 clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
4130 /* no further action needed, so return now */
4133 dev_info(&pf->pdev->dev,
4134 "bad reset request 0x%08x\n", reset_flags);
4140 * i40e_do_reset_safe - Protected reset path for userland calls.
4141 * @pf: board private structure
4142 * @reset_flags: which reset is requested
4145 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
4148 i40e_do_reset(pf, reset_flags);
4153 * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
4154 * @pf: board private structure
4155 * @e: event info posted on ARQ
4157 * Handler for LAN Queue Overflow Event generated by the firmware for PF
4160 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
4161 struct i40e_arq_event_info *e)
4163 struct i40e_aqc_lan_overflow *data =
4164 (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
4165 u32 queue = le32_to_cpu(data->prtdcb_rupto);
4166 u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
4167 struct i40e_hw *hw = &pf->hw;
4171 dev_info(&pf->pdev->dev, "%s: Rx Queue Number = %d QTX_CTL=0x%08x\n",
4172 __func__, queue, qtx_ctl);
4174 /* Queue belongs to VF, find the VF and issue VF reset */
4175 if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
4176 >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
4177 vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
4178 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
4179 vf_id -= hw->func_caps.vf_base_id;
4180 vf = &pf->vf[vf_id];
4181 i40e_vc_notify_vf_reset(vf);
4182 /* Allow VF to process pending reset notification */
4184 i40e_reset_vf(vf, false);
4189 * i40e_service_event_complete - Finish up the service event
4190 * @pf: board private structure
4192 static void i40e_service_event_complete(struct i40e_pf *pf)
4194 BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
4196 /* flush memory to make sure state is correct before next watchog */
4197 smp_mb__before_clear_bit();
4198 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
4202 * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
4203 * @pf: board private structure
4205 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
4207 if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
4210 pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
4212 /* if interface is down do nothing */
4213 if (test_bit(__I40E_DOWN, &pf->state))
4218 * i40e_vsi_link_event - notify VSI of a link event
4219 * @vsi: vsi to be notified
4220 * @link_up: link up or down
4222 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
4227 switch (vsi->type) {
4229 if (!vsi->netdev || !vsi->netdev_registered)
4233 netif_carrier_on(vsi->netdev);
4234 netif_tx_wake_all_queues(vsi->netdev);
4236 netif_carrier_off(vsi->netdev);
4237 netif_tx_stop_all_queues(vsi->netdev);
4241 case I40E_VSI_SRIOV:
4244 case I40E_VSI_VMDQ2:
4246 case I40E_VSI_MIRROR:
4248 /* there is no notification for other VSIs */
4254 * i40e_veb_link_event - notify elements on the veb of a link event
4255 * @veb: veb to be notified
4256 * @link_up: link up or down
4258 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
4263 if (!veb || !veb->pf)
4267 /* depth first... */
4268 for (i = 0; i < I40E_MAX_VEB; i++)
4269 if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
4270 i40e_veb_link_event(pf->veb[i], link_up);
4272 /* ... now the local VSIs */
4273 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4274 if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
4275 i40e_vsi_link_event(pf->vsi[i], link_up);
4279 * i40e_link_event - Update netif_carrier status
4280 * @pf: board private structure
4282 static void i40e_link_event(struct i40e_pf *pf)
4284 bool new_link, old_link;
4286 new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
4287 old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
4289 if (new_link == old_link)
4292 if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
4293 netdev_info(pf->vsi[pf->lan_vsi]->netdev,
4294 "NIC Link is %s\n", (new_link ? "Up" : "Down"));
4296 /* Notify the base of the switch tree connected to
4297 * the link. Floating VEBs are not notified.
4299 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
4300 i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
4302 i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
4305 i40e_vc_notify_link_state(pf);
4309 * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
4310 * @pf: board private structure
4312 * Set the per-queue flags to request a check for stuck queues in the irq
4313 * clean functions, then force interrupts to be sure the irq clean is called.
4315 static void i40e_check_hang_subtask(struct i40e_pf *pf)
4319 /* If we're down or resetting, just bail */
4320 if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
4323 /* for each VSI/netdev
4325 * set the check flag
4327 * force an interrupt
4329 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4330 struct i40e_vsi *vsi = pf->vsi[v];
4334 test_bit(__I40E_DOWN, &vsi->state) ||
4335 (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
4338 for (i = 0; i < vsi->num_queue_pairs; i++) {
4339 set_check_for_tx_hang(vsi->tx_rings[i]);
4340 if (test_bit(__I40E_HANG_CHECK_ARMED,
4341 &vsi->tx_rings[i]->state))
4346 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
4347 wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
4348 (I40E_PFINT_DYN_CTL0_INTENA_MASK |
4349 I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
4351 u16 vec = vsi->base_vector - 1;
4352 u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
4353 I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
4354 for (i = 0; i < vsi->num_q_vectors; i++, vec++)
4355 wr32(&vsi->back->hw,
4356 I40E_PFINT_DYN_CTLN(vec), val);
4358 i40e_flush(&vsi->back->hw);
4364 * i40e_watchdog_subtask - Check and bring link up
4365 * @pf: board private structure
4367 static void i40e_watchdog_subtask(struct i40e_pf *pf)
4371 /* if interface is down do nothing */
4372 if (test_bit(__I40E_DOWN, &pf->state) ||
4373 test_bit(__I40E_CONFIG_BUSY, &pf->state))
4376 /* Update the stats for active netdevs so the network stack
4377 * can look at updated numbers whenever it cares to
4379 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4380 if (pf->vsi[i] && pf->vsi[i]->netdev)
4381 i40e_update_stats(pf->vsi[i]);
4383 /* Update the stats for the active switching components */
4384 for (i = 0; i < I40E_MAX_VEB; i++)
4386 i40e_update_veb_stats(pf->veb[i]);
4390 * i40e_reset_subtask - Set up for resetting the device and driver
4391 * @pf: board private structure
4393 static void i40e_reset_subtask(struct i40e_pf *pf)
4395 u32 reset_flags = 0;
4398 if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
4399 reset_flags |= (1 << __I40E_REINIT_REQUESTED);
4400 clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
4402 if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
4403 reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
4404 clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
4406 if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
4407 reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
4408 clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
4410 if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
4411 reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
4412 clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
4415 /* If there's a recovery already waiting, it takes
4416 * precedence before starting a new reset sequence.
4418 if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
4419 i40e_handle_reset_warning(pf);
4423 /* If we're already down or resetting, just bail */
4425 !test_bit(__I40E_DOWN, &pf->state) &&
4426 !test_bit(__I40E_CONFIG_BUSY, &pf->state))
4427 i40e_do_reset(pf, reset_flags);
4434 * i40e_handle_link_event - Handle link event
4435 * @pf: board private structure
4436 * @e: event info posted on ARQ
4438 static void i40e_handle_link_event(struct i40e_pf *pf,
4439 struct i40e_arq_event_info *e)
4441 struct i40e_hw *hw = &pf->hw;
4442 struct i40e_aqc_get_link_status *status =
4443 (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
4444 struct i40e_link_status *hw_link_info = &hw->phy.link_info;
4446 /* save off old link status information */
4447 memcpy(&pf->hw.phy.link_info_old, hw_link_info,
4448 sizeof(pf->hw.phy.link_info_old));
4450 /* update link status */
4451 hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
4452 hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
4453 hw_link_info->link_info = status->link_info;
4454 hw_link_info->an_info = status->an_info;
4455 hw_link_info->ext_info = status->ext_info;
4456 hw_link_info->lse_enable =
4457 le16_to_cpu(status->command_flags) &
4460 /* process the event */
4461 i40e_link_event(pf);
4463 /* Do a new status request to re-enable LSE reporting
4464 * and load new status information into the hw struct,
4465 * then see if the status changed while processing the
4468 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
4469 i40e_link_event(pf);
4473 * i40e_clean_adminq_subtask - Clean the AdminQ rings
4474 * @pf: board private structure
4476 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
4478 struct i40e_arq_event_info event;
4479 struct i40e_hw *hw = &pf->hw;
4485 if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
4488 event.msg_size = I40E_MAX_AQ_BUF_SIZE;
4489 event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
4494 event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
4495 ret = i40e_clean_arq_element(hw, &event, &pending);
4496 if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
4497 dev_info(&pf->pdev->dev, "No ARQ event found\n");
4500 dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
4504 opcode = le16_to_cpu(event.desc.opcode);
4507 case i40e_aqc_opc_get_link_status:
4508 i40e_handle_link_event(pf, &event);
4510 case i40e_aqc_opc_send_msg_to_pf:
4511 ret = i40e_vc_process_vf_msg(pf,
4512 le16_to_cpu(event.desc.retval),
4513 le32_to_cpu(event.desc.cookie_high),
4514 le32_to_cpu(event.desc.cookie_low),
4518 case i40e_aqc_opc_lldp_update_mib:
4519 dev_info(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
4521 case i40e_aqc_opc_event_lan_overflow:
4522 dev_info(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
4523 i40e_handle_lan_overflow_event(pf, &event);
4525 case i40e_aqc_opc_send_msg_to_peer:
4526 dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
4529 dev_info(&pf->pdev->dev,
4530 "ARQ Error: Unknown event 0x%04x received\n",
4534 } while (pending && (i++ < pf->adminq_work_limit));
4536 clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
4537 /* re-enable Admin queue interrupt cause */
4538 val = rd32(hw, I40E_PFINT_ICR0_ENA);
4539 val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4540 wr32(hw, I40E_PFINT_ICR0_ENA, val);
4543 kfree(event.msg_buf);
4547 * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
4548 * @veb: pointer to the VEB instance
4550 * This is a recursive function that first builds the attached VSIs then
4551 * recurses in to build the next layer of VEB. We track the connections
4552 * through our own index numbers because the seid's from the HW could
4553 * change across the reset.
4555 static int i40e_reconstitute_veb(struct i40e_veb *veb)
4557 struct i40e_vsi *ctl_vsi = NULL;
4558 struct i40e_pf *pf = veb->pf;
4562 /* build VSI that owns this VEB, temporarily attached to base VEB */
4563 for (v = 0; v < pf->hw.func_caps.num_vsis && !ctl_vsi; v++) {
4565 pf->vsi[v]->veb_idx == veb->idx &&
4566 pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
4567 ctl_vsi = pf->vsi[v];
4572 dev_info(&pf->pdev->dev,
4573 "missing owner VSI for veb_idx %d\n", veb->idx);
4575 goto end_reconstitute;
4577 if (ctl_vsi != pf->vsi[pf->lan_vsi])
4578 ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
4579 ret = i40e_add_vsi(ctl_vsi);
4581 dev_info(&pf->pdev->dev,
4582 "rebuild of owner VSI failed: %d\n", ret);
4583 goto end_reconstitute;
4585 i40e_vsi_reset_stats(ctl_vsi);
4587 /* create the VEB in the switch and move the VSI onto the VEB */
4588 ret = i40e_add_veb(veb, ctl_vsi);
4590 goto end_reconstitute;
4592 /* create the remaining VSIs attached to this VEB */
4593 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4594 if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
4597 if (pf->vsi[v]->veb_idx == veb->idx) {
4598 struct i40e_vsi *vsi = pf->vsi[v];
4599 vsi->uplink_seid = veb->seid;
4600 ret = i40e_add_vsi(vsi);
4602 dev_info(&pf->pdev->dev,
4603 "rebuild of vsi_idx %d failed: %d\n",
4605 goto end_reconstitute;
4607 i40e_vsi_reset_stats(vsi);
4611 /* create any VEBs attached to this VEB - RECURSION */
4612 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
4613 if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
4614 pf->veb[veb_idx]->uplink_seid = veb->seid;
4615 ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
4626 * i40e_get_capabilities - get info about the HW
4627 * @pf: the PF struct
4629 static int i40e_get_capabilities(struct i40e_pf *pf)
4631 struct i40e_aqc_list_capabilities_element_resp *cap_buf;
4636 buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
4638 cap_buf = kzalloc(buf_len, GFP_KERNEL);
4642 /* this loads the data into the hw struct for us */
4643 err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
4645 i40e_aqc_opc_list_func_capabilities,
4647 /* data loaded, buffer no longer needed */
4650 if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
4651 /* retry with a larger buffer */
4652 buf_len = data_size;
4653 } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
4654 dev_info(&pf->pdev->dev,
4655 "capability discovery failed: aq=%d\n",
4656 pf->hw.aq.asq_last_status);
4661 if (pf->hw.revision_id == 0 && (pf->flags & I40E_FLAG_MFP_ENABLED)) {
4662 pf->hw.func_caps.num_msix_vectors += 1;
4663 pf->hw.func_caps.num_tx_qp =
4664 min_t(int, pf->hw.func_caps.num_tx_qp,
4668 if (pf->hw.debug_mask & I40E_DEBUG_USER)
4669 dev_info(&pf->pdev->dev,
4670 "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",
4671 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
4672 pf->hw.func_caps.num_msix_vectors,
4673 pf->hw.func_caps.num_msix_vectors_vf,
4674 pf->hw.func_caps.fd_filters_guaranteed,
4675 pf->hw.func_caps.fd_filters_best_effort,
4676 pf->hw.func_caps.num_tx_qp,
4677 pf->hw.func_caps.num_vsis);
4679 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
4680 + pf->hw.func_caps.num_vfs)
4681 if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
4682 dev_info(&pf->pdev->dev,
4683 "got num_vsis %d, setting num_vsis to %d\n",
4684 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
4685 pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
4692 * i40e_fdir_setup - initialize the Flow Director resources
4693 * @pf: board private structure
4695 static void i40e_fdir_setup(struct i40e_pf *pf)
4697 struct i40e_vsi *vsi;
4698 bool new_vsi = false;
4701 if (!(pf->flags & (I40E_FLAG_FDIR_ENABLED |
4702 I40E_FLAG_FDIR_ATR_ENABLED)))
4705 pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
4707 /* find existing or make new FDIR VSI */
4709 for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
4710 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR)
4713 vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, pf->mac_seid, 0);
4715 dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
4716 pf->flags &= ~I40E_FLAG_FDIR_ENABLED;
4721 WARN_ON(vsi->base_queue != I40E_FDIR_RING);
4722 i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_rings);
4724 err = i40e_vsi_setup_tx_resources(vsi);
4726 err = i40e_vsi_setup_rx_resources(vsi);
4728 err = i40e_vsi_configure(vsi);
4729 if (!err && new_vsi) {
4730 char int_name[IFNAMSIZ + 9];
4731 snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
4732 dev_driver_string(&pf->pdev->dev));
4733 err = i40e_vsi_request_irq(vsi, int_name);
4736 err = i40e_up_complete(vsi);
4738 clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
4742 * i40e_fdir_teardown - release the Flow Director resources
4743 * @pf: board private structure
4745 static void i40e_fdir_teardown(struct i40e_pf *pf)
4749 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
4750 if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
4751 i40e_vsi_release(pf->vsi[i]);
4758 * i40e_prep_for_reset - prep for the core to reset
4759 * @pf: board private structure
4761 * Close up the VFs and other things in prep for pf Reset.
4763 static int i40e_prep_for_reset(struct i40e_pf *pf)
4765 struct i40e_hw *hw = &pf->hw;
4769 clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
4770 if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
4773 dev_info(&pf->pdev->dev, "Tearing down internal switch for reset\n");
4775 if (i40e_check_asq_alive(hw))
4776 i40e_vc_notify_reset(pf);
4778 /* quiesce the VSIs and their queues that are not already DOWN */
4779 i40e_pf_quiesce_all_vsi(pf);
4781 for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
4783 pf->vsi[v]->seid = 0;
4786 i40e_shutdown_adminq(&pf->hw);
4788 /* call shutdown HMC */
4789 ret = i40e_shutdown_lan_hmc(hw);
4791 dev_info(&pf->pdev->dev, "shutdown_lan_hmc failed: %d\n", ret);
4792 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
4798 * i40e_reset_and_rebuild - reset and rebuild using a saved config
4799 * @pf: board private structure
4800 * @reinit: if the Main VSI needs to re-initialized.
4802 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
4804 struct i40e_driver_version dv;
4805 struct i40e_hw *hw = &pf->hw;
4809 /* Now we wait for GRST to settle out.
4810 * We don't have to delete the VEBs or VSIs from the hw switch
4811 * because the reset will make them disappear.
4813 ret = i40e_pf_reset(hw);
4815 dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
4818 if (test_bit(__I40E_DOWN, &pf->state))
4819 goto end_core_reset;
4820 dev_info(&pf->pdev->dev, "Rebuilding internal switch\n");
4822 /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
4823 ret = i40e_init_adminq(&pf->hw);
4825 dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
4826 goto end_core_reset;
4829 ret = i40e_get_capabilities(pf);
4831 dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
4833 goto end_core_reset;
4836 ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
4837 hw->func_caps.num_rx_qp,
4838 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
4840 dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
4841 goto end_core_reset;
4843 ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
4845 dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
4846 goto end_core_reset;
4849 /* do basic switch setup */
4850 ret = i40e_setup_pf_switch(pf, reinit);
4852 goto end_core_reset;
4854 /* Rebuild the VSIs and VEBs that existed before reset.
4855 * They are still in our local switch element arrays, so only
4856 * need to rebuild the switch model in the HW.
4858 * If there were VEBs but the reconstitution failed, we'll try
4859 * try to recover minimal use by getting the basic PF VSI working.
4861 if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
4862 dev_info(&pf->pdev->dev, "attempting to rebuild switch\n");
4863 /* find the one VEB connected to the MAC, and find orphans */
4864 for (v = 0; v < I40E_MAX_VEB; v++) {
4868 if (pf->veb[v]->uplink_seid == pf->mac_seid ||
4869 pf->veb[v]->uplink_seid == 0) {
4870 ret = i40e_reconstitute_veb(pf->veb[v]);
4875 /* If Main VEB failed, we're in deep doodoo,
4876 * so give up rebuilding the switch and set up
4877 * for minimal rebuild of PF VSI.
4878 * If orphan failed, we'll report the error
4879 * but try to keep going.
4881 if (pf->veb[v]->uplink_seid == pf->mac_seid) {
4882 dev_info(&pf->pdev->dev,
4883 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
4885 pf->vsi[pf->lan_vsi]->uplink_seid
4888 } else if (pf->veb[v]->uplink_seid == 0) {
4889 dev_info(&pf->pdev->dev,
4890 "rebuild of orphan VEB failed: %d\n",
4897 if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
4898 dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
4899 /* no VEB, so rebuild only the Main VSI */
4900 ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
4902 dev_info(&pf->pdev->dev,
4903 "rebuild of Main VSI failed: %d\n", ret);
4904 goto end_core_reset;
4908 /* reinit the misc interrupt */
4909 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4910 ret = i40e_setup_misc_vector(pf);
4912 /* restart the VSIs that were rebuilt and running before the reset */
4913 i40e_pf_unquiesce_all_vsi(pf);
4915 /* tell the firmware that we're starting */
4916 dv.major_version = DRV_VERSION_MAJOR;
4917 dv.minor_version = DRV_VERSION_MINOR;
4918 dv.build_version = DRV_VERSION_BUILD;
4919 dv.subbuild_version = 0;
4920 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
4922 dev_info(&pf->pdev->dev, "PF reset done\n");
4925 clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
4929 * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
4930 * @pf: board private structure
4932 * Close up the VFs and other things in prep for a Core Reset,
4933 * then get ready to rebuild the world.
4935 static void i40e_handle_reset_warning(struct i40e_pf *pf)
4939 ret = i40e_prep_for_reset(pf);
4941 i40e_reset_and_rebuild(pf, false);
4945 * i40e_handle_mdd_event
4946 * @pf: pointer to the pf structure
4948 * Called from the MDD irq handler to identify possibly malicious vfs
4950 static void i40e_handle_mdd_event(struct i40e_pf *pf)
4952 struct i40e_hw *hw = &pf->hw;
4953 bool mdd_detected = false;
4958 if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
4961 /* find what triggered the MDD event */
4962 reg = rd32(hw, I40E_GL_MDET_TX);
4963 if (reg & I40E_GL_MDET_TX_VALID_MASK) {
4964 u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
4965 >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
4966 u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
4967 >> I40E_GL_MDET_TX_EVENT_SHIFT;
4968 u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
4969 >> I40E_GL_MDET_TX_QUEUE_SHIFT;
4970 dev_info(&pf->pdev->dev,
4971 "Malicious Driver Detection TX event 0x%02x on q %d of function 0x%02x\n",
4972 event, queue, func);
4973 wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
4974 mdd_detected = true;
4976 reg = rd32(hw, I40E_GL_MDET_RX);
4977 if (reg & I40E_GL_MDET_RX_VALID_MASK) {
4978 u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
4979 >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
4980 u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
4981 >> I40E_GL_MDET_RX_EVENT_SHIFT;
4982 u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
4983 >> I40E_GL_MDET_RX_QUEUE_SHIFT;
4984 dev_info(&pf->pdev->dev,
4985 "Malicious Driver Detection RX event 0x%02x on q %d of function 0x%02x\n",
4986 event, queue, func);
4987 wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
4988 mdd_detected = true;
4991 /* see if one of the VFs needs its hand slapped */
4992 for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
4994 reg = rd32(hw, I40E_VP_MDET_TX(i));
4995 if (reg & I40E_VP_MDET_TX_VALID_MASK) {
4996 wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
4997 vf->num_mdd_events++;
4998 dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
5001 reg = rd32(hw, I40E_VP_MDET_RX(i));
5002 if (reg & I40E_VP_MDET_RX_VALID_MASK) {
5003 wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
5004 vf->num_mdd_events++;
5005 dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
5008 if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
5009 dev_info(&pf->pdev->dev,
5010 "Too many MDD events on VF %d, disabled\n", i);
5011 dev_info(&pf->pdev->dev,
5012 "Use PF Control I/F to re-enable the VF\n");
5013 set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
5017 /* re-enable mdd interrupt cause */
5018 clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
5019 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
5020 reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
5021 wr32(hw, I40E_PFINT_ICR0_ENA, reg);
5025 #ifdef CONFIG_I40E_VXLAN
5027 * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
5028 * @pf: board private structure
5030 static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
5032 const int vxlan_hdr_qwords = 4;
5033 struct i40e_hw *hw = &pf->hw;
5039 if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
5042 pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
5044 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5045 if (pf->pending_vxlan_bitmap & (1 << i)) {
5046 pf->pending_vxlan_bitmap &= ~(1 << i);
5047 port = pf->vxlan_ports[i];
5049 i40e_aq_add_udp_tunnel(hw, ntohs(port),
5051 I40E_AQC_TUNNEL_TYPE_VXLAN,
5052 &filter_index, NULL)
5053 : i40e_aq_del_udp_tunnel(hw, i, NULL);
5056 dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
5057 port ? "adding" : "deleting",
5058 ntohs(port), port ? i : i);
5060 pf->vxlan_ports[i] = 0;
5062 dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
5063 port ? "Added" : "Deleted",
5064 ntohs(port), port ? i : filter_index);
5072 * i40e_service_task - Run the driver's async subtasks
5073 * @work: pointer to work_struct containing our data
5075 static void i40e_service_task(struct work_struct *work)
5077 struct i40e_pf *pf = container_of(work,
5080 unsigned long start_time = jiffies;
5082 i40e_reset_subtask(pf);
5083 i40e_handle_mdd_event(pf);
5084 i40e_vc_process_vflr_event(pf);
5085 i40e_watchdog_subtask(pf);
5086 i40e_fdir_reinit_subtask(pf);
5087 i40e_check_hang_subtask(pf);
5088 i40e_sync_filters_subtask(pf);
5089 #ifdef CONFIG_I40E_VXLAN
5090 i40e_sync_vxlan_filters_subtask(pf);
5092 i40e_clean_adminq_subtask(pf);
5094 i40e_service_event_complete(pf);
5096 /* If the tasks have taken longer than one timer cycle or there
5097 * is more work to be done, reschedule the service task now
5098 * rather than wait for the timer to tick again.
5100 if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
5101 test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
5102 test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
5103 test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
5104 i40e_service_event_schedule(pf);
5108 * i40e_service_timer - timer callback
5109 * @data: pointer to PF struct
5111 static void i40e_service_timer(unsigned long data)
5113 struct i40e_pf *pf = (struct i40e_pf *)data;
5115 mod_timer(&pf->service_timer,
5116 round_jiffies(jiffies + pf->service_timer_period));
5117 i40e_service_event_schedule(pf);
5121 * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
5122 * @vsi: the VSI being configured
5124 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
5126 struct i40e_pf *pf = vsi->back;
5128 switch (vsi->type) {
5130 vsi->alloc_queue_pairs = pf->num_lan_qps;
5131 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5132 I40E_REQ_DESCRIPTOR_MULTIPLE);
5133 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5134 vsi->num_q_vectors = pf->num_lan_msix;
5136 vsi->num_q_vectors = 1;
5141 vsi->alloc_queue_pairs = 1;
5142 vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
5143 I40E_REQ_DESCRIPTOR_MULTIPLE);
5144 vsi->num_q_vectors = 1;
5147 case I40E_VSI_VMDQ2:
5148 vsi->alloc_queue_pairs = pf->num_vmdq_qps;
5149 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5150 I40E_REQ_DESCRIPTOR_MULTIPLE);
5151 vsi->num_q_vectors = pf->num_vmdq_msix;
5154 case I40E_VSI_SRIOV:
5155 vsi->alloc_queue_pairs = pf->num_vf_qps;
5156 vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
5157 I40E_REQ_DESCRIPTOR_MULTIPLE);
5169 * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
5170 * @type: VSI pointer
5171 * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
5173 * On error: returns error code (negative)
5174 * On success: returns 0
5176 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
5181 /* allocate memory for both Tx and Rx ring pointers */
5182 size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
5183 vsi->tx_rings = kzalloc(size, GFP_KERNEL);
5186 vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
5188 if (alloc_qvectors) {
5189 /* allocate memory for q_vector pointers */
5190 size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
5191 vsi->q_vectors = kzalloc(size, GFP_KERNEL);
5192 if (!vsi->q_vectors) {
5200 kfree(vsi->tx_rings);
5205 * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
5206 * @pf: board private structure
5207 * @type: type of VSI
5209 * On error: returns error code (negative)
5210 * On success: returns vsi index in PF (positive)
5212 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
5215 struct i40e_vsi *vsi;
5219 /* Need to protect the allocation of the VSIs at the PF level */
5220 mutex_lock(&pf->switch_mutex);
5222 /* VSI list may be fragmented if VSI creation/destruction has
5223 * been happening. We can afford to do a quick scan to look
5224 * for any free VSIs in the list.
5226 * find next empty vsi slot, looping back around if necessary
5229 while (i < pf->hw.func_caps.num_vsis && pf->vsi[i])
5231 if (i >= pf->hw.func_caps.num_vsis) {
5233 while (i < pf->next_vsi && pf->vsi[i])
5237 if (i < pf->hw.func_caps.num_vsis && !pf->vsi[i]) {
5238 vsi_idx = i; /* Found one! */
5241 goto unlock_pf; /* out of VSI slots! */
5245 vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
5252 set_bit(__I40E_DOWN, &vsi->state);
5255 vsi->rx_itr_setting = pf->rx_itr_default;
5256 vsi->tx_itr_setting = pf->tx_itr_default;
5257 vsi->netdev_registered = false;
5258 vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
5259 INIT_LIST_HEAD(&vsi->mac_filter_list);
5261 ret = i40e_set_num_rings_in_vsi(vsi);
5265 ret = i40e_vsi_alloc_arrays(vsi, true);
5269 /* Setup default MSIX irq handler for VSI */
5270 i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
5272 pf->vsi[vsi_idx] = vsi;
5277 pf->next_vsi = i - 1;
5280 mutex_unlock(&pf->switch_mutex);
5285 * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
5286 * @type: VSI pointer
5287 * @free_qvectors: a bool to specify if q_vectors need to be freed.
5289 * On error: returns error code (negative)
5290 * On success: returns 0
5292 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
5294 /* free the ring and vector containers */
5295 if (free_qvectors) {
5296 kfree(vsi->q_vectors);
5297 vsi->q_vectors = NULL;
5299 kfree(vsi->tx_rings);
5300 vsi->tx_rings = NULL;
5301 vsi->rx_rings = NULL;
5305 * i40e_vsi_clear - Deallocate the VSI provided
5306 * @vsi: the VSI being un-configured
5308 static int i40e_vsi_clear(struct i40e_vsi *vsi)
5319 mutex_lock(&pf->switch_mutex);
5320 if (!pf->vsi[vsi->idx]) {
5321 dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
5322 vsi->idx, vsi->idx, vsi, vsi->type);
5326 if (pf->vsi[vsi->idx] != vsi) {
5327 dev_err(&pf->pdev->dev,
5328 "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
5329 pf->vsi[vsi->idx]->idx,
5331 pf->vsi[vsi->idx]->type,
5332 vsi->idx, vsi, vsi->type);
5336 /* updates the pf for this cleared vsi */
5337 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
5338 i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
5340 i40e_vsi_free_arrays(vsi, true);
5342 pf->vsi[vsi->idx] = NULL;
5343 if (vsi->idx < pf->next_vsi)
5344 pf->next_vsi = vsi->idx;
5347 mutex_unlock(&pf->switch_mutex);
5355 * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
5356 * @vsi: the VSI being cleaned
5358 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
5362 if (vsi->tx_rings && vsi->tx_rings[0]) {
5363 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
5364 kfree_rcu(vsi->tx_rings[i], rcu);
5365 vsi->tx_rings[i] = NULL;
5366 vsi->rx_rings[i] = NULL;
5372 * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
5373 * @vsi: the VSI being configured
5375 static int i40e_alloc_rings(struct i40e_vsi *vsi)
5377 struct i40e_pf *pf = vsi->back;
5380 /* Set basic values in the rings to be used later during open() */
5381 for (i = 0; i < vsi->alloc_queue_pairs; i++) {
5382 struct i40e_ring *tx_ring;
5383 struct i40e_ring *rx_ring;
5385 /* allocate space for both Tx and Rx in one shot */
5386 tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
5390 tx_ring->queue_index = i;
5391 tx_ring->reg_idx = vsi->base_queue + i;
5392 tx_ring->ring_active = false;
5394 tx_ring->netdev = vsi->netdev;
5395 tx_ring->dev = &pf->pdev->dev;
5396 tx_ring->count = vsi->num_desc;
5398 tx_ring->dcb_tc = 0;
5399 vsi->tx_rings[i] = tx_ring;
5401 rx_ring = &tx_ring[1];
5402 rx_ring->queue_index = i;
5403 rx_ring->reg_idx = vsi->base_queue + i;
5404 rx_ring->ring_active = false;
5406 rx_ring->netdev = vsi->netdev;
5407 rx_ring->dev = &pf->pdev->dev;
5408 rx_ring->count = vsi->num_desc;
5410 rx_ring->dcb_tc = 0;
5411 if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
5412 set_ring_16byte_desc_enabled(rx_ring);
5414 clear_ring_16byte_desc_enabled(rx_ring);
5415 vsi->rx_rings[i] = rx_ring;
5421 i40e_vsi_clear_rings(vsi);
5426 * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
5427 * @pf: board private structure
5428 * @vectors: the number of MSI-X vectors to request
5430 * Returns the number of vectors reserved, or error
5432 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
5436 pf->num_msix_entries = 0;
5437 while (vectors >= I40E_MIN_MSIX) {
5438 err = pci_enable_msix(pf->pdev, pf->msix_entries, vectors);
5441 pf->num_msix_entries = vectors;
5443 } else if (err < 0) {
5445 dev_info(&pf->pdev->dev,
5446 "MSI-X vector reservation failed: %d\n", err);
5450 /* err > 0 is the hint for retry */
5451 dev_info(&pf->pdev->dev,
5452 "MSI-X vectors wanted %d, retrying with %d\n",
5458 if (vectors > 0 && vectors < I40E_MIN_MSIX) {
5459 dev_info(&pf->pdev->dev,
5460 "Couldn't get enough vectors, only %d available\n",
5469 * i40e_init_msix - Setup the MSIX capability
5470 * @pf: board private structure
5472 * Work with the OS to set up the MSIX vectors needed.
5474 * Returns 0 on success, negative on failure
5476 static int i40e_init_msix(struct i40e_pf *pf)
5478 i40e_status err = 0;
5479 struct i40e_hw *hw = &pf->hw;
5483 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
5486 /* The number of vectors we'll request will be comprised of:
5487 * - Add 1 for "other" cause for Admin Queue events, etc.
5488 * - The number of LAN queue pairs
5489 * - Queues being used for RSS.
5490 * We don't need as many as max_rss_size vectors.
5491 * use rss_size instead in the calculation since that
5492 * is governed by number of cpus in the system.
5493 * - assumes symmetric Tx/Rx pairing
5494 * - The number of VMDq pairs
5495 * Once we count this up, try the request.
5497 * If we can't get what we want, we'll simplify to nearly nothing
5498 * and try again. If that still fails, we punt.
5500 pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
5501 pf->num_vmdq_msix = pf->num_vmdq_qps;
5502 v_budget = 1 + pf->num_lan_msix;
5503 v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
5504 if (pf->flags & I40E_FLAG_FDIR_ENABLED)
5507 /* Scale down if necessary, and the rings will share vectors */
5508 v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
5510 pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
5512 if (!pf->msix_entries)
5515 for (i = 0; i < v_budget; i++)
5516 pf->msix_entries[i].entry = i;
5517 vec = i40e_reserve_msix_vectors(pf, v_budget);
5518 if (vec < I40E_MIN_MSIX) {
5519 pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
5520 kfree(pf->msix_entries);
5521 pf->msix_entries = NULL;
5524 } else if (vec == I40E_MIN_MSIX) {
5525 /* Adjust for minimal MSIX use */
5526 dev_info(&pf->pdev->dev, "Features disabled, not enough MSIX vectors\n");
5527 pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
5528 pf->num_vmdq_vsis = 0;
5529 pf->num_vmdq_qps = 0;
5530 pf->num_vmdq_msix = 0;
5531 pf->num_lan_qps = 1;
5532 pf->num_lan_msix = 1;
5534 } else if (vec != v_budget) {
5535 /* Scale vector usage down */
5536 pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
5537 vec--; /* reserve the misc vector */
5539 /* partition out the remaining vectors */
5542 pf->num_vmdq_vsis = 1;
5543 pf->num_lan_msix = 1;
5546 pf->num_vmdq_vsis = 1;
5547 pf->num_lan_msix = 2;
5550 pf->num_lan_msix = min_t(int, (vec / 2),
5552 pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
5553 I40E_DEFAULT_NUM_VMDQ_VSI);
5562 * i40e_alloc_q_vector - Allocate memory for a single interrupt vector
5563 * @vsi: the VSI being configured
5564 * @v_idx: index of the vector in the vsi struct
5566 * We allocate one q_vector. If allocation fails we return -ENOMEM.
5568 static int i40e_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
5570 struct i40e_q_vector *q_vector;
5572 /* allocate q_vector */
5573 q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
5577 q_vector->vsi = vsi;
5578 q_vector->v_idx = v_idx;
5579 cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
5581 netif_napi_add(vsi->netdev, &q_vector->napi,
5582 i40e_napi_poll, vsi->work_limit);
5584 q_vector->rx.latency_range = I40E_LOW_LATENCY;
5585 q_vector->tx.latency_range = I40E_LOW_LATENCY;
5587 /* tie q_vector and vsi together */
5588 vsi->q_vectors[v_idx] = q_vector;
5594 * i40e_alloc_q_vectors - Allocate memory for interrupt vectors
5595 * @vsi: the VSI being configured
5597 * We allocate one q_vector per queue interrupt. If allocation fails we
5600 static int i40e_alloc_q_vectors(struct i40e_vsi *vsi)
5602 struct i40e_pf *pf = vsi->back;
5603 int v_idx, num_q_vectors;
5606 /* if not MSIX, give the one vector only to the LAN VSI */
5607 if (pf->flags & I40E_FLAG_MSIX_ENABLED)
5608 num_q_vectors = vsi->num_q_vectors;
5609 else if (vsi == pf->vsi[pf->lan_vsi])
5614 for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
5615 err = i40e_alloc_q_vector(vsi, v_idx);
5624 i40e_free_q_vector(vsi, v_idx);
5630 * i40e_init_interrupt_scheme - Determine proper interrupt scheme
5631 * @pf: board private structure to initialize
5633 static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
5637 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5638 err = i40e_init_msix(pf);
5640 pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
5641 I40E_FLAG_RSS_ENABLED |
5642 I40E_FLAG_DCB_ENABLED |
5643 I40E_FLAG_SRIOV_ENABLED |
5644 I40E_FLAG_FDIR_ENABLED |
5645 I40E_FLAG_FDIR_ATR_ENABLED |
5646 I40E_FLAG_VMDQ_ENABLED);
5648 /* rework the queue expectations without MSIX */
5649 i40e_determine_queue_usage(pf);
5653 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
5654 (pf->flags & I40E_FLAG_MSI_ENABLED)) {
5655 dev_info(&pf->pdev->dev, "MSIX not available, trying MSI\n");
5656 err = pci_enable_msi(pf->pdev);
5658 dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
5659 pf->flags &= ~I40E_FLAG_MSI_ENABLED;
5663 if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
5664 dev_info(&pf->pdev->dev, "MSIX and MSI not available, falling back to Legacy IRQ\n");
5666 /* track first vector for misc interrupts */
5667 err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
5671 * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
5672 * @pf: board private structure
5674 * This sets up the handler for MSIX 0, which is used to manage the
5675 * non-queue interrupts, e.g. AdminQ and errors. This is not used
5676 * when in MSI or Legacy interrupt mode.
5678 static int i40e_setup_misc_vector(struct i40e_pf *pf)
5680 struct i40e_hw *hw = &pf->hw;
5683 /* Only request the irq if this is the first time through, and
5684 * not when we're rebuilding after a Reset
5686 if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
5687 err = request_irq(pf->msix_entries[0].vector,
5688 i40e_intr, 0, pf->misc_int_name, pf);
5690 dev_info(&pf->pdev->dev,
5691 "request_irq for msix_misc failed: %d\n", err);
5696 i40e_enable_misc_int_causes(hw);
5698 /* associate no queues to the misc vector */
5699 wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
5700 wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
5704 i40e_irq_dynamic_enable_icr0(pf);
5710 * i40e_config_rss - Prepare for RSS if used
5711 * @pf: board private structure
5713 static int i40e_config_rss(struct i40e_pf *pf)
5715 /* Set of random keys generated using kernel random number generator */
5716 static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
5717 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
5718 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
5719 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
5720 struct i40e_hw *hw = &pf->hw;
5725 /* Fill out hash function seed */
5726 for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
5727 wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
5729 /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
5730 hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
5731 ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
5732 hena |= I40E_DEFAULT_RSS_HENA;
5733 wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
5734 wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
5736 /* Populate the LUT with max no. of queues in round robin fashion */
5737 for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
5739 /* The assumption is that lan qp count will be the highest
5740 * qp count for any PF VSI that needs RSS.
5741 * If multiple VSIs need RSS support, all the qp counts
5742 * for those VSIs should be a power of 2 for RSS to work.
5743 * If LAN VSI is the only consumer for RSS then this requirement
5746 if (j == pf->rss_size)
5748 /* lut = 4-byte sliding window of 4 lut entries */
5749 lut = (lut << 8) | (j &
5750 ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
5751 /* On i = 3, we have 4 entries in lut; write to the register */
5753 wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
5761 * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
5762 * @pf: board private structure
5763 * @queue_count: the requested queue count for rss.
5765 * returns 0 if rss is not enabled, if enabled returns the final rss queue
5766 * count which may be different from the requested queue count.
5768 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
5770 if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
5773 queue_count = min_t(int, queue_count, pf->rss_size_max);
5774 queue_count = rounddown_pow_of_two(queue_count);
5776 if (queue_count != pf->rss_size) {
5777 i40e_prep_for_reset(pf);
5779 pf->rss_size = queue_count;
5781 i40e_reset_and_rebuild(pf, true);
5782 i40e_config_rss(pf);
5784 dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
5785 return pf->rss_size;
5789 * i40e_sw_init - Initialize general software structures (struct i40e_pf)
5790 * @pf: board private structure to initialize
5792 * i40e_sw_init initializes the Adapter private data structure.
5793 * Fields are initialized based on PCI device information and
5794 * OS network device settings (MTU size).
5796 static int i40e_sw_init(struct i40e_pf *pf)
5801 pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
5802 (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
5803 pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
5804 if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
5805 if (I40E_DEBUG_USER & debug)
5806 pf->hw.debug_mask = debug;
5807 pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
5808 I40E_DEFAULT_MSG_ENABLE);
5811 /* Set default capability flags */
5812 pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
5813 I40E_FLAG_MSI_ENABLED |
5814 I40E_FLAG_MSIX_ENABLED |
5815 I40E_FLAG_RX_1BUF_ENABLED;
5817 /* Depending on PF configurations, it is possible that the RSS
5818 * maximum might end up larger than the available queues
5820 pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
5821 pf->rss_size_max = min_t(int, pf->rss_size_max,
5822 pf->hw.func_caps.num_tx_qp);
5823 if (pf->hw.func_caps.rss) {
5824 pf->flags |= I40E_FLAG_RSS_ENABLED;
5825 pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
5830 /* MFP mode enabled */
5831 if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
5832 pf->flags |= I40E_FLAG_MFP_ENABLED;
5833 dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
5836 if (pf->hw.func_caps.dcb)
5837 pf->num_tc_qps = I40E_DEFAULT_QUEUES_PER_TC;
5841 if (pf->hw.func_caps.fd) {
5842 /* FW/NVM is not yet fixed in this regard */
5843 if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
5844 (pf->hw.func_caps.fd_filters_best_effort > 0)) {
5845 pf->flags |= I40E_FLAG_FDIR_ATR_ENABLED;
5846 dev_info(&pf->pdev->dev,
5847 "Flow Director ATR mode Enabled\n");
5848 pf->flags |= I40E_FLAG_FDIR_ENABLED;
5849 dev_info(&pf->pdev->dev,
5850 "Flow Director Side Band mode Enabled\n");
5851 pf->fdir_pf_filter_count =
5852 pf->hw.func_caps.fd_filters_guaranteed;
5855 pf->fdir_pf_filter_count = 0;
5858 if (pf->hw.func_caps.vmdq) {
5859 pf->flags |= I40E_FLAG_VMDQ_ENABLED;
5860 pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
5861 pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
5864 #ifdef CONFIG_PCI_IOV
5865 if (pf->hw.func_caps.num_vfs) {
5866 pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
5867 pf->flags |= I40E_FLAG_SRIOV_ENABLED;
5868 pf->num_req_vfs = min_t(int,
5869 pf->hw.func_caps.num_vfs,
5871 dev_info(&pf->pdev->dev,
5872 "Number of VFs being requested for PF[%d] = %d\n",
5873 pf->hw.pf_id, pf->num_req_vfs);
5875 #endif /* CONFIG_PCI_IOV */
5876 pf->eeprom_version = 0xDEAD;
5877 pf->lan_veb = I40E_NO_VEB;
5878 pf->lan_vsi = I40E_NO_VSI;
5880 /* set up queue assignment tracking */
5881 size = sizeof(struct i40e_lump_tracking)
5882 + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
5883 pf->qp_pile = kzalloc(size, GFP_KERNEL);
5888 pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
5889 pf->qp_pile->search_hint = 0;
5891 /* set up vector assignment tracking */
5892 size = sizeof(struct i40e_lump_tracking)
5893 + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
5894 pf->irq_pile = kzalloc(size, GFP_KERNEL);
5895 if (!pf->irq_pile) {
5900 pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
5901 pf->irq_pile->search_hint = 0;
5903 mutex_init(&pf->switch_mutex);
5910 * i40e_set_features - set the netdev feature flags
5911 * @netdev: ptr to the netdev being adjusted
5912 * @features: the feature set that the stack is suggesting
5914 static int i40e_set_features(struct net_device *netdev,
5915 netdev_features_t features)
5917 struct i40e_netdev_priv *np = netdev_priv(netdev);
5918 struct i40e_vsi *vsi = np->vsi;
5920 if (features & NETIF_F_HW_VLAN_CTAG_RX)
5921 i40e_vlan_stripping_enable(vsi);
5923 i40e_vlan_stripping_disable(vsi);
5928 #ifdef CONFIG_I40E_VXLAN
5930 * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
5931 * @pf: board private structure
5932 * @port: The UDP port to look up
5934 * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
5936 static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
5940 for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
5941 if (pf->vxlan_ports[i] == port)
5949 * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
5950 * @netdev: This physical port's netdev
5951 * @sa_family: Socket Family that VXLAN is notifying us about
5952 * @port: New UDP port number that VXLAN started listening to
5954 static void i40e_add_vxlan_port(struct net_device *netdev,
5955 sa_family_t sa_family, __be16 port)
5957 struct i40e_netdev_priv *np = netdev_priv(netdev);
5958 struct i40e_vsi *vsi = np->vsi;
5959 struct i40e_pf *pf = vsi->back;
5963 if (sa_family == AF_INET6)
5966 idx = i40e_get_vxlan_port_idx(pf, port);
5968 /* Check if port already exists */
5969 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
5970 netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
5974 /* Now check if there is space to add the new port */
5975 next_idx = i40e_get_vxlan_port_idx(pf, 0);
5977 if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
5978 netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
5983 /* New port: add it and mark its index in the bitmap */
5984 pf->vxlan_ports[next_idx] = port;
5985 pf->pending_vxlan_bitmap |= (1 << next_idx);
5987 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
5991 * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
5992 * @netdev: This physical port's netdev
5993 * @sa_family: Socket Family that VXLAN is notifying us about
5994 * @port: UDP port number that VXLAN stopped listening to
5996 static void i40e_del_vxlan_port(struct net_device *netdev,
5997 sa_family_t sa_family, __be16 port)
5999 struct i40e_netdev_priv *np = netdev_priv(netdev);
6000 struct i40e_vsi *vsi = np->vsi;
6001 struct i40e_pf *pf = vsi->back;
6004 if (sa_family == AF_INET6)
6007 idx = i40e_get_vxlan_port_idx(pf, port);
6009 /* Check if port already exists */
6010 if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
6011 /* if port exists, set it to 0 (mark for deletion)
6012 * and make it pending
6014 pf->vxlan_ports[idx] = 0;
6016 pf->pending_vxlan_bitmap |= (1 << idx);
6018 pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
6020 netdev_warn(netdev, "Port %d was not found, not deleting\n",
6026 static const struct net_device_ops i40e_netdev_ops = {
6027 .ndo_open = i40e_open,
6028 .ndo_stop = i40e_close,
6029 .ndo_start_xmit = i40e_lan_xmit_frame,
6030 .ndo_get_stats64 = i40e_get_netdev_stats_struct,
6031 .ndo_set_rx_mode = i40e_set_rx_mode,
6032 .ndo_validate_addr = eth_validate_addr,
6033 .ndo_set_mac_address = i40e_set_mac,
6034 .ndo_change_mtu = i40e_change_mtu,
6035 .ndo_tx_timeout = i40e_tx_timeout,
6036 .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
6037 .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
6038 #ifdef CONFIG_NET_POLL_CONTROLLER
6039 .ndo_poll_controller = i40e_netpoll,
6041 .ndo_setup_tc = i40e_setup_tc,
6042 .ndo_set_features = i40e_set_features,
6043 .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
6044 .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
6045 .ndo_set_vf_tx_rate = i40e_ndo_set_vf_bw,
6046 .ndo_get_vf_config = i40e_ndo_get_vf_config,
6047 #ifdef CONFIG_I40E_VXLAN
6048 .ndo_add_vxlan_port = i40e_add_vxlan_port,
6049 .ndo_del_vxlan_port = i40e_del_vxlan_port,
6054 * i40e_config_netdev - Setup the netdev flags
6055 * @vsi: the VSI being configured
6057 * Returns 0 on success, negative value on failure
6059 static int i40e_config_netdev(struct i40e_vsi *vsi)
6061 u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
6062 struct i40e_pf *pf = vsi->back;
6063 struct i40e_hw *hw = &pf->hw;
6064 struct i40e_netdev_priv *np;
6065 struct net_device *netdev;
6066 u8 mac_addr[ETH_ALEN];
6069 etherdev_size = sizeof(struct i40e_netdev_priv);
6070 netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
6074 vsi->netdev = netdev;
6075 np = netdev_priv(netdev);
6078 netdev->hw_enc_features = NETIF_F_IP_CSUM |
6079 NETIF_F_GSO_UDP_TUNNEL |
6083 netdev->features = NETIF_F_SG |
6087 NETIF_F_GSO_UDP_TUNNEL |
6088 NETIF_F_HW_VLAN_CTAG_TX |
6089 NETIF_F_HW_VLAN_CTAG_RX |
6090 NETIF_F_HW_VLAN_CTAG_FILTER |
6098 /* copy netdev features into list of user selectable features */
6099 netdev->hw_features |= netdev->features;
6101 if (vsi->type == I40E_VSI_MAIN) {
6102 SET_NETDEV_DEV(netdev, &pf->pdev->dev);
6103 memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
6105 /* relate the VSI_VMDQ name to the VSI_MAIN name */
6106 snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
6107 pf->vsi[pf->lan_vsi]->netdev->name);
6108 random_ether_addr(mac_addr);
6109 i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
6111 i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
6113 memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
6114 memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
6115 /* vlan gets same features (except vlan offload)
6116 * after any tweaks for specific VSI types
6118 netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
6119 NETIF_F_HW_VLAN_CTAG_RX |
6120 NETIF_F_HW_VLAN_CTAG_FILTER);
6121 netdev->priv_flags |= IFF_UNICAST_FLT;
6122 netdev->priv_flags |= IFF_SUPP_NOFCS;
6123 /* Setup netdev TC information */
6124 i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
6126 netdev->netdev_ops = &i40e_netdev_ops;
6127 netdev->watchdog_timeo = 5 * HZ;
6128 i40e_set_ethtool_ops(netdev);
6134 * i40e_vsi_delete - Delete a VSI from the switch
6135 * @vsi: the VSI being removed
6137 * Returns 0 on success, negative value on failure
6139 static void i40e_vsi_delete(struct i40e_vsi *vsi)
6141 /* remove default VSI is not allowed */
6142 if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
6145 /* there is no HW VSI for FDIR */
6146 if (vsi->type == I40E_VSI_FDIR)
6149 i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
6154 * i40e_add_vsi - Add a VSI to the switch
6155 * @vsi: the VSI being configured
6157 * This initializes a VSI context depending on the VSI type to be added and
6158 * passes it down to the add_vsi aq command.
6160 static int i40e_add_vsi(struct i40e_vsi *vsi)
6163 struct i40e_mac_filter *f, *ftmp;
6164 struct i40e_pf *pf = vsi->back;
6165 struct i40e_hw *hw = &pf->hw;
6166 struct i40e_vsi_context ctxt;
6167 u8 enabled_tc = 0x1; /* TC0 enabled */
6170 memset(&ctxt, 0, sizeof(ctxt));
6171 switch (vsi->type) {
6173 /* The PF's main VSI is already setup as part of the
6174 * device initialization, so we'll not bother with
6175 * the add_vsi call, but we will retrieve the current
6178 ctxt.seid = pf->main_vsi_seid;
6179 ctxt.pf_num = pf->hw.pf_id;
6181 ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
6182 ctxt.flags = I40E_AQ_VSI_TYPE_PF;
6184 dev_info(&pf->pdev->dev,
6185 "couldn't get pf vsi config, err %d, aq_err %d\n",
6186 ret, pf->hw.aq.asq_last_status);
6189 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6190 vsi->info.valid_sections = 0;
6192 vsi->seid = ctxt.seid;
6193 vsi->id = ctxt.vsi_number;
6195 enabled_tc = i40e_pf_get_tc_map(pf);
6197 /* MFP mode setup queue map and update VSI */
6198 if (pf->flags & I40E_FLAG_MFP_ENABLED) {
6199 memset(&ctxt, 0, sizeof(ctxt));
6200 ctxt.seid = pf->main_vsi_seid;
6201 ctxt.pf_num = pf->hw.pf_id;
6203 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
6204 ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
6206 dev_info(&pf->pdev->dev,
6207 "update vsi failed, aq_err=%d\n",
6208 pf->hw.aq.asq_last_status);
6212 /* update the local VSI info queue map */
6213 i40e_vsi_update_queue_map(vsi, &ctxt);
6214 vsi->info.valid_sections = 0;
6216 /* Default/Main VSI is only enabled for TC0
6217 * reconfigure it to enable all TCs that are
6218 * available on the port in SFP mode.
6220 ret = i40e_vsi_config_tc(vsi, enabled_tc);
6222 dev_info(&pf->pdev->dev,
6223 "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
6225 pf->hw.aq.asq_last_status);
6232 /* no queue mapping or actual HW VSI needed */
6233 vsi->info.valid_sections = 0;
6236 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6240 case I40E_VSI_VMDQ2:
6241 ctxt.pf_num = hw->pf_id;
6243 ctxt.uplink_seid = vsi->uplink_seid;
6244 ctxt.connection_type = 0x1; /* regular data port */
6245 ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6247 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6249 /* This VSI is connected to VEB so the switch_id
6250 * should be set to zero by default.
6252 ctxt.info.switch_id = 0;
6253 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
6254 ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6256 /* Setup the VSI tx/rx queue map for TC0 only for now */
6257 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6260 case I40E_VSI_SRIOV:
6261 ctxt.pf_num = hw->pf_id;
6262 ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
6263 ctxt.uplink_seid = vsi->uplink_seid;
6264 ctxt.connection_type = 0x1; /* regular data port */
6265 ctxt.flags = I40E_AQ_VSI_TYPE_VF;
6267 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6269 /* This VSI is connected to VEB so the switch_id
6270 * should be set to zero by default.
6272 ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6274 ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
6275 ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
6276 /* Setup the VSI tx/rx queue map for TC0 only for now */
6277 i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
6284 if (vsi->type != I40E_VSI_MAIN) {
6285 ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6287 dev_info(&vsi->back->pdev->dev,
6288 "add vsi failed, aq_err=%d\n",
6289 vsi->back->hw.aq.asq_last_status);
6293 memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
6294 vsi->info.valid_sections = 0;
6295 vsi->seid = ctxt.seid;
6296 vsi->id = ctxt.vsi_number;
6299 /* If macvlan filters already exist, force them to get loaded */
6300 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
6305 vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
6306 pf->flags |= I40E_FLAG_FILTER_SYNC;
6309 /* Update VSI BW information */
6310 ret = i40e_vsi_get_bw_info(vsi);
6312 dev_info(&pf->pdev->dev,
6313 "couldn't get vsi bw info, err %d, aq_err %d\n",
6314 ret, pf->hw.aq.asq_last_status);
6315 /* VSI is already added so not tearing that up */
6324 * i40e_vsi_release - Delete a VSI and free its resources
6325 * @vsi: the VSI being removed
6327 * Returns 0 on success or < 0 on error
6329 int i40e_vsi_release(struct i40e_vsi *vsi)
6331 struct i40e_mac_filter *f, *ftmp;
6332 struct i40e_veb *veb = NULL;
6339 /* release of a VEB-owner or last VSI is not allowed */
6340 if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
6341 dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
6342 vsi->seid, vsi->uplink_seid);
6345 if (vsi == pf->vsi[pf->lan_vsi] &&
6346 !test_bit(__I40E_DOWN, &pf->state)) {
6347 dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
6351 uplink_seid = vsi->uplink_seid;
6352 if (vsi->type != I40E_VSI_SRIOV) {
6353 if (vsi->netdev_registered) {
6354 vsi->netdev_registered = false;
6356 /* results in a call to i40e_close() */
6357 unregister_netdev(vsi->netdev);
6358 free_netdev(vsi->netdev);
6362 if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
6364 i40e_vsi_free_irq(vsi);
6365 i40e_vsi_free_tx_resources(vsi);
6366 i40e_vsi_free_rx_resources(vsi);
6368 i40e_vsi_disable_irq(vsi);
6371 list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
6372 i40e_del_filter(vsi, f->macaddr, f->vlan,
6373 f->is_vf, f->is_netdev);
6374 i40e_sync_vsi_filters(vsi);
6376 i40e_vsi_delete(vsi);
6377 i40e_vsi_free_q_vectors(vsi);
6378 i40e_vsi_clear_rings(vsi);
6379 i40e_vsi_clear(vsi);
6381 /* If this was the last thing on the VEB, except for the
6382 * controlling VSI, remove the VEB, which puts the controlling
6383 * VSI onto the next level down in the switch.
6385 * Well, okay, there's one more exception here: don't remove
6386 * the orphan VEBs yet. We'll wait for an explicit remove request
6387 * from up the network stack.
6389 for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6391 pf->vsi[i]->uplink_seid == uplink_seid &&
6392 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6393 n++; /* count the VSIs */
6396 for (i = 0; i < I40E_MAX_VEB; i++) {
6399 if (pf->veb[i]->uplink_seid == uplink_seid)
6400 n++; /* count the VEBs */
6401 if (pf->veb[i]->seid == uplink_seid)
6404 if (n == 0 && veb && veb->uplink_seid != 0)
6405 i40e_veb_release(veb);
6411 * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
6412 * @vsi: ptr to the VSI
6414 * This should only be called after i40e_vsi_mem_alloc() which allocates the
6415 * corresponding SW VSI structure and initializes num_queue_pairs for the
6416 * newly allocated VSI.
6418 * Returns 0 on success or negative on failure
6420 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
6423 struct i40e_pf *pf = vsi->back;
6425 if (vsi->q_vectors[0]) {
6426 dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
6431 if (vsi->base_vector) {
6432 dev_info(&pf->pdev->dev,
6433 "VSI %d has non-zero base vector %d\n",
6434 vsi->seid, vsi->base_vector);
6438 ret = i40e_alloc_q_vectors(vsi);
6440 dev_info(&pf->pdev->dev,
6441 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
6442 vsi->num_q_vectors, vsi->seid, ret);
6443 vsi->num_q_vectors = 0;
6444 goto vector_setup_out;
6447 if (vsi->num_q_vectors)
6448 vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
6449 vsi->num_q_vectors, vsi->idx);
6450 if (vsi->base_vector < 0) {
6451 dev_info(&pf->pdev->dev,
6452 "failed to get q tracking for VSI %d, err=%d\n",
6453 vsi->seid, vsi->base_vector);
6454 i40e_vsi_free_q_vectors(vsi);
6456 goto vector_setup_out;
6464 * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
6465 * @vsi: pointer to the vsi.
6467 * This re-allocates a vsi's queue resources.
6469 * Returns pointer to the successfully allocated and configured VSI sw struct
6470 * on success, otherwise returns NULL on failure.
6472 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
6474 struct i40e_pf *pf = vsi->back;
6478 i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
6479 i40e_vsi_clear_rings(vsi);
6481 i40e_vsi_free_arrays(vsi, false);
6482 i40e_set_num_rings_in_vsi(vsi);
6483 ret = i40e_vsi_alloc_arrays(vsi, false);
6487 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
6489 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
6493 vsi->base_queue = ret;
6495 /* Update the FW view of the VSI. Force a reset of TC and queue
6496 * layout configurations.
6498 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
6499 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
6500 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
6501 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
6503 /* assign it some queues */
6504 ret = i40e_alloc_rings(vsi);
6508 /* map all of the rings to the q_vectors */
6509 i40e_vsi_map_rings_to_vectors(vsi);
6513 i40e_vsi_free_q_vectors(vsi);
6514 if (vsi->netdev_registered) {
6515 vsi->netdev_registered = false;
6516 unregister_netdev(vsi->netdev);
6517 free_netdev(vsi->netdev);
6520 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
6522 i40e_vsi_clear(vsi);
6527 * i40e_vsi_setup - Set up a VSI by a given type
6528 * @pf: board private structure
6530 * @uplink_seid: the switch element to link to
6531 * @param1: usage depends upon VSI type. For VF types, indicates VF id
6533 * This allocates the sw VSI structure and its queue resources, then add a VSI
6534 * to the identified VEB.
6536 * Returns pointer to the successfully allocated and configure VSI sw struct on
6537 * success, otherwise returns NULL on failure.
6539 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
6540 u16 uplink_seid, u32 param1)
6542 struct i40e_vsi *vsi = NULL;
6543 struct i40e_veb *veb = NULL;
6547 /* The requested uplink_seid must be either
6548 * - the PF's port seid
6549 * no VEB is needed because this is the PF
6550 * or this is a Flow Director special case VSI
6551 * - seid of an existing VEB
6552 * - seid of a VSI that owns an existing VEB
6553 * - seid of a VSI that doesn't own a VEB
6554 * a new VEB is created and the VSI becomes the owner
6555 * - seid of the PF VSI, which is what creates the first VEB
6556 * this is a special case of the previous
6558 * Find which uplink_seid we were given and create a new VEB if needed
6560 for (i = 0; i < I40E_MAX_VEB; i++) {
6561 if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
6567 if (!veb && uplink_seid != pf->mac_seid) {
6569 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6570 if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
6576 dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
6581 if (vsi->uplink_seid == pf->mac_seid)
6582 veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
6583 vsi->tc_config.enabled_tc);
6584 else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
6585 veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
6586 vsi->tc_config.enabled_tc);
6588 for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
6589 if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
6593 dev_info(&pf->pdev->dev, "couldn't add VEB\n");
6597 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
6598 uplink_seid = veb->seid;
6601 /* get vsi sw struct */
6602 v_idx = i40e_vsi_mem_alloc(pf, type);
6605 vsi = pf->vsi[v_idx];
6607 vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
6609 if (type == I40E_VSI_MAIN)
6610 pf->lan_vsi = v_idx;
6611 else if (type == I40E_VSI_SRIOV)
6612 vsi->vf_id = param1;
6613 /* assign it some queues */
6614 ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
6616 dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
6620 vsi->base_queue = ret;
6622 /* get a VSI from the hardware */
6623 vsi->uplink_seid = uplink_seid;
6624 ret = i40e_add_vsi(vsi);
6628 switch (vsi->type) {
6629 /* setup the netdev if needed */
6631 case I40E_VSI_VMDQ2:
6632 ret = i40e_config_netdev(vsi);
6635 ret = register_netdev(vsi->netdev);
6638 vsi->netdev_registered = true;
6639 netif_carrier_off(vsi->netdev);
6643 /* set up vectors and rings if needed */
6644 ret = i40e_vsi_setup_vectors(vsi);
6648 ret = i40e_alloc_rings(vsi);
6652 /* map all of the rings to the q_vectors */
6653 i40e_vsi_map_rings_to_vectors(vsi);
6655 i40e_vsi_reset_stats(vsi);
6659 /* no netdev or rings for the other VSI types */
6666 i40e_vsi_free_q_vectors(vsi);
6668 if (vsi->netdev_registered) {
6669 vsi->netdev_registered = false;
6670 unregister_netdev(vsi->netdev);
6671 free_netdev(vsi->netdev);
6675 i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
6677 i40e_vsi_clear(vsi);
6683 * i40e_veb_get_bw_info - Query VEB BW information
6684 * @veb: the veb to query
6686 * Query the Tx scheduler BW configuration data for given VEB
6688 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
6690 struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
6691 struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
6692 struct i40e_pf *pf = veb->pf;
6693 struct i40e_hw *hw = &pf->hw;
6698 ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
6701 dev_info(&pf->pdev->dev,
6702 "query veb bw config failed, aq_err=%d\n",
6703 hw->aq.asq_last_status);
6707 ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
6710 dev_info(&pf->pdev->dev,
6711 "query veb bw ets config failed, aq_err=%d\n",
6712 hw->aq.asq_last_status);
6716 veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
6717 veb->bw_max_quanta = ets_data.tc_bw_max;
6718 veb->is_abs_credits = bw_data.absolute_credits_enable;
6719 tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
6720 (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
6721 for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6722 veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
6723 veb->bw_tc_limit_credits[i] =
6724 le16_to_cpu(bw_data.tc_bw_limits[i]);
6725 veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
6733 * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
6734 * @pf: board private structure
6736 * On error: returns error code (negative)
6737 * On success: returns vsi index in PF (positive)
6739 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
6742 struct i40e_veb *veb;
6745 /* Need to protect the allocation of switch elements at the PF level */
6746 mutex_lock(&pf->switch_mutex);
6748 /* VEB list may be fragmented if VEB creation/destruction has
6749 * been happening. We can afford to do a quick scan to look
6750 * for any free slots in the list.
6752 * find next empty veb slot, looping back around if necessary
6755 while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
6757 if (i >= I40E_MAX_VEB) {
6759 goto err_alloc_veb; /* out of VEB slots! */
6762 veb = kzalloc(sizeof(*veb), GFP_KERNEL);
6769 veb->enabled_tc = 1;
6774 mutex_unlock(&pf->switch_mutex);
6779 * i40e_switch_branch_release - Delete a branch of the switch tree
6780 * @branch: where to start deleting
6782 * This uses recursion to find the tips of the branch to be
6783 * removed, deleting until we get back to and can delete this VEB.
6785 static void i40e_switch_branch_release(struct i40e_veb *branch)
6787 struct i40e_pf *pf = branch->pf;
6788 u16 branch_seid = branch->seid;
6789 u16 veb_idx = branch->idx;
6792 /* release any VEBs on this VEB - RECURSION */
6793 for (i = 0; i < I40E_MAX_VEB; i++) {
6796 if (pf->veb[i]->uplink_seid == branch->seid)
6797 i40e_switch_branch_release(pf->veb[i]);
6800 /* Release the VSIs on this VEB, but not the owner VSI.
6802 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
6803 * the VEB itself, so don't use (*branch) after this loop.
6805 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6808 if (pf->vsi[i]->uplink_seid == branch_seid &&
6809 (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
6810 i40e_vsi_release(pf->vsi[i]);
6814 /* There's one corner case where the VEB might not have been
6815 * removed, so double check it here and remove it if needed.
6816 * This case happens if the veb was created from the debugfs
6817 * commands and no VSIs were added to it.
6819 if (pf->veb[veb_idx])
6820 i40e_veb_release(pf->veb[veb_idx]);
6824 * i40e_veb_clear - remove veb struct
6825 * @veb: the veb to remove
6827 static void i40e_veb_clear(struct i40e_veb *veb)
6833 struct i40e_pf *pf = veb->pf;
6835 mutex_lock(&pf->switch_mutex);
6836 if (pf->veb[veb->idx] == veb)
6837 pf->veb[veb->idx] = NULL;
6838 mutex_unlock(&pf->switch_mutex);
6845 * i40e_veb_release - Delete a VEB and free its resources
6846 * @veb: the VEB being removed
6848 void i40e_veb_release(struct i40e_veb *veb)
6850 struct i40e_vsi *vsi = NULL;
6856 /* find the remaining VSI and check for extras */
6857 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
6858 if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
6864 dev_info(&pf->pdev->dev,
6865 "can't remove VEB %d with %d VSIs left\n",
6870 /* move the remaining VSI to uplink veb */
6871 vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
6872 if (veb->uplink_seid) {
6873 vsi->uplink_seid = veb->uplink_seid;
6874 if (veb->uplink_seid == pf->mac_seid)
6875 vsi->veb_idx = I40E_NO_VEB;
6877 vsi->veb_idx = veb->veb_idx;
6880 vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6881 vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
6884 i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
6885 i40e_veb_clear(veb);
6891 * i40e_add_veb - create the VEB in the switch
6892 * @veb: the VEB to be instantiated
6893 * @vsi: the controlling VSI
6895 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
6897 bool is_default = false;
6898 bool is_cloud = false;
6901 /* get a VEB from the hardware */
6902 ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
6903 veb->enabled_tc, is_default,
6904 is_cloud, &veb->seid, NULL);
6906 dev_info(&veb->pf->pdev->dev,
6907 "couldn't add VEB, err %d, aq_err %d\n",
6908 ret, veb->pf->hw.aq.asq_last_status);
6912 /* get statistics counter */
6913 ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
6914 &veb->stats_idx, NULL, NULL, NULL);
6916 dev_info(&veb->pf->pdev->dev,
6917 "couldn't get VEB statistics idx, err %d, aq_err %d\n",
6918 ret, veb->pf->hw.aq.asq_last_status);
6921 ret = i40e_veb_get_bw_info(veb);
6923 dev_info(&veb->pf->pdev->dev,
6924 "couldn't get VEB bw info, err %d, aq_err %d\n",
6925 ret, veb->pf->hw.aq.asq_last_status);
6926 i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
6930 vsi->uplink_seid = veb->seid;
6931 vsi->veb_idx = veb->idx;
6932 vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
6938 * i40e_veb_setup - Set up a VEB
6939 * @pf: board private structure
6940 * @flags: VEB setup flags
6941 * @uplink_seid: the switch element to link to
6942 * @vsi_seid: the initial VSI seid
6943 * @enabled_tc: Enabled TC bit-map
6945 * This allocates the sw VEB structure and links it into the switch
6946 * It is possible and legal for this to be a duplicate of an already
6947 * existing VEB. It is also possible for both uplink and vsi seids
6948 * to be zero, in order to create a floating VEB.
6950 * Returns pointer to the successfully allocated VEB sw struct on
6951 * success, otherwise returns NULL on failure.
6953 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
6954 u16 uplink_seid, u16 vsi_seid,
6957 struct i40e_veb *veb, *uplink_veb = NULL;
6958 int vsi_idx, veb_idx;
6961 /* if one seid is 0, the other must be 0 to create a floating relay */
6962 if ((uplink_seid == 0 || vsi_seid == 0) &&
6963 (uplink_seid + vsi_seid != 0)) {
6964 dev_info(&pf->pdev->dev,
6965 "one, not both seid's are 0: uplink=%d vsi=%d\n",
6966 uplink_seid, vsi_seid);
6970 /* make sure there is such a vsi and uplink */
6971 for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
6972 if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
6974 if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
6975 dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
6980 if (uplink_seid && uplink_seid != pf->mac_seid) {
6981 for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
6982 if (pf->veb[veb_idx] &&
6983 pf->veb[veb_idx]->seid == uplink_seid) {
6984 uplink_veb = pf->veb[veb_idx];
6989 dev_info(&pf->pdev->dev,
6990 "uplink seid %d not found\n", uplink_seid);
6995 /* get veb sw struct */
6996 veb_idx = i40e_veb_mem_alloc(pf);
6999 veb = pf->veb[veb_idx];
7001 veb->uplink_seid = uplink_seid;
7002 veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
7003 veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
7005 /* create the VEB in the switch */
7006 ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
7013 i40e_veb_clear(veb);
7019 * i40e_setup_pf_switch_element - set pf vars based on switch type
7020 * @pf: board private structure
7021 * @ele: element we are building info from
7022 * @num_reported: total number of elements
7023 * @printconfig: should we print the contents
7025 * helper function to assist in extracting a few useful SEID values.
7027 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
7028 struct i40e_aqc_switch_config_element_resp *ele,
7029 u16 num_reported, bool printconfig)
7031 u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
7032 u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
7033 u8 element_type = ele->element_type;
7034 u16 seid = le16_to_cpu(ele->seid);
7037 dev_info(&pf->pdev->dev,
7038 "type=%d seid=%d uplink=%d downlink=%d\n",
7039 element_type, seid, uplink_seid, downlink_seid);
7041 switch (element_type) {
7042 case I40E_SWITCH_ELEMENT_TYPE_MAC:
7043 pf->mac_seid = seid;
7045 case I40E_SWITCH_ELEMENT_TYPE_VEB:
7047 if (uplink_seid != pf->mac_seid)
7049 if (pf->lan_veb == I40E_NO_VEB) {
7052 /* find existing or else empty VEB */
7053 for (v = 0; v < I40E_MAX_VEB; v++) {
7054 if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
7059 if (pf->lan_veb == I40E_NO_VEB) {
7060 v = i40e_veb_mem_alloc(pf);
7067 pf->veb[pf->lan_veb]->seid = seid;
7068 pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
7069 pf->veb[pf->lan_veb]->pf = pf;
7070 pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
7072 case I40E_SWITCH_ELEMENT_TYPE_VSI:
7073 if (num_reported != 1)
7075 /* This is immediately after a reset so we can assume this is
7078 pf->mac_seid = uplink_seid;
7079 pf->pf_seid = downlink_seid;
7080 pf->main_vsi_seid = seid;
7082 dev_info(&pf->pdev->dev,
7083 "pf_seid=%d main_vsi_seid=%d\n",
7084 pf->pf_seid, pf->main_vsi_seid);
7086 case I40E_SWITCH_ELEMENT_TYPE_PF:
7087 case I40E_SWITCH_ELEMENT_TYPE_VF:
7088 case I40E_SWITCH_ELEMENT_TYPE_EMP:
7089 case I40E_SWITCH_ELEMENT_TYPE_BMC:
7090 case I40E_SWITCH_ELEMENT_TYPE_PE:
7091 case I40E_SWITCH_ELEMENT_TYPE_PA:
7092 /* ignore these for now */
7095 dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
7096 element_type, seid);
7102 * i40e_fetch_switch_configuration - Get switch config from firmware
7103 * @pf: board private structure
7104 * @printconfig: should we print the contents
7106 * Get the current switch configuration from the device and
7107 * extract a few useful SEID values.
7109 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
7111 struct i40e_aqc_get_switch_config_resp *sw_config;
7117 aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
7121 sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
7123 u16 num_reported, num_total;
7125 ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
7129 dev_info(&pf->pdev->dev,
7130 "get switch config failed %d aq_err=%x\n",
7131 ret, pf->hw.aq.asq_last_status);
7136 num_reported = le16_to_cpu(sw_config->header.num_reported);
7137 num_total = le16_to_cpu(sw_config->header.num_total);
7140 dev_info(&pf->pdev->dev,
7141 "header: %d reported %d total\n",
7142 num_reported, num_total);
7145 int sz = sizeof(*sw_config) * num_reported;
7147 kfree(pf->sw_config);
7148 pf->sw_config = kzalloc(sz, GFP_KERNEL);
7150 memcpy(pf->sw_config, sw_config, sz);
7153 for (i = 0; i < num_reported; i++) {
7154 struct i40e_aqc_switch_config_element_resp *ele =
7155 &sw_config->element[i];
7157 i40e_setup_pf_switch_element(pf, ele, num_reported,
7160 } while (next_seid != 0);
7167 * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
7168 * @pf: board private structure
7169 * @reinit: if the Main VSI needs to re-initialized.
7171 * Returns 0 on success, negative value on failure
7173 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
7175 u32 rxfc = 0, txfc = 0, rxfc_reg;
7178 /* find out what's out there already */
7179 ret = i40e_fetch_switch_configuration(pf, false);
7181 dev_info(&pf->pdev->dev,
7182 "couldn't fetch switch config, err %d, aq_err %d\n",
7183 ret, pf->hw.aq.asq_last_status);
7186 i40e_pf_reset_stats(pf);
7188 /* fdir VSI must happen first to be sure it gets queue 0, but only
7189 * if there is enough room for the fdir VSI
7191 if (pf->num_lan_qps > 1)
7192 i40e_fdir_setup(pf);
7194 /* first time setup */
7195 if (pf->lan_vsi == I40E_NO_VSI || reinit) {
7196 struct i40e_vsi *vsi = NULL;
7199 /* Set up the PF VSI associated with the PF's main VSI
7200 * that is already in the HW switch
7202 if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
7203 uplink_seid = pf->veb[pf->lan_veb]->seid;
7205 uplink_seid = pf->mac_seid;
7206 if (pf->lan_vsi == I40E_NO_VSI)
7207 vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
7209 vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
7211 dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
7212 i40e_fdir_teardown(pf);
7216 /* force a reset of TC and queue layout configurations */
7217 u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
7218 pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
7219 pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
7220 i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
7222 i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
7224 /* Setup static PF queue filter control settings */
7225 ret = i40e_setup_pf_filter_control(pf);
7227 dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
7229 /* Failure here should not stop continuing other steps */
7232 /* enable RSS in the HW, even for only one queue, as the stack can use
7235 if ((pf->flags & I40E_FLAG_RSS_ENABLED))
7236 i40e_config_rss(pf);
7238 /* fill in link information and enable LSE reporting */
7239 i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
7240 i40e_link_event(pf);
7242 /* Initialize user-specific link properties */
7243 pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
7244 I40E_AQ_AN_COMPLETED) ? true : false);
7245 /* requested_mode is set in probe or by ethtool */
7246 if (!pf->fc_autoneg_status)
7249 if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
7250 (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
7251 pf->hw.fc.current_mode = I40E_FC_FULL;
7252 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
7253 pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
7254 else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
7255 pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
7257 pf->hw.fc.current_mode = I40E_FC_NONE;
7259 /* sync the flow control settings with the auto-neg values */
7260 switch (pf->hw.fc.current_mode) {
7265 case I40E_FC_TX_PAUSE:
7269 case I40E_FC_RX_PAUSE:
7274 case I40E_FC_DEFAULT:
7281 /* no default case, we have to handle all possibilities here */
7284 wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
7286 rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
7287 ~I40E_PRTDCB_MFLCN_RFCE_MASK;
7288 rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
7290 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
7295 /* disable L2 flow control, user can turn it on if they wish */
7296 wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
7297 wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
7298 ~I40E_PRTDCB_MFLCN_RFCE_MASK);
7305 * i40e_set_rss_size - helper to set rss_size
7306 * @pf: board private structure
7307 * @queues_left: how many queues
7309 static u16 i40e_set_rss_size(struct i40e_pf *pf, int queues_left)
7313 num_tc0 = min_t(int, queues_left, pf->rss_size_max);
7314 num_tc0 = min_t(int, num_tc0, num_online_cpus());
7315 num_tc0 = rounddown_pow_of_two(num_tc0);
7321 * i40e_determine_queue_usage - Work out queue distribution
7322 * @pf: board private structure
7324 static void i40e_determine_queue_usage(struct i40e_pf *pf)
7329 pf->num_lan_qps = 0;
7330 pf->num_tc_qps = rounddown_pow_of_two(pf->num_tc_qps);
7331 accum_tc_size = (I40E_MAX_TRAFFIC_CLASS - 1) * pf->num_tc_qps;
7333 /* Find the max queues to be put into basic use. We'll always be
7334 * using TC0, whether or not DCB is running, and TC0 will get the
7337 queues_left = pf->hw.func_caps.num_tx_qp;
7339 if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) ||
7340 !(pf->flags & (I40E_FLAG_RSS_ENABLED |
7341 I40E_FLAG_FDIR_ENABLED | I40E_FLAG_DCB_ENABLED)) ||
7342 (queues_left == 1)) {
7344 /* one qp for PF, no queues for anything else */
7346 pf->rss_size = pf->num_lan_qps = 1;
7348 /* make sure all the fancies are disabled */
7349 pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
7350 I40E_FLAG_FDIR_ENABLED |
7351 I40E_FLAG_FDIR_ATR_ENABLED |
7352 I40E_FLAG_DCB_ENABLED |
7353 I40E_FLAG_SRIOV_ENABLED |
7354 I40E_FLAG_VMDQ_ENABLED);
7356 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
7357 !(pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7358 !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7360 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7362 queues_left -= pf->rss_size;
7363 pf->num_lan_qps = pf->rss_size_max;
7365 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
7366 !(pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7367 (pf->flags & I40E_FLAG_DCB_ENABLED)) {
7369 /* save num_tc_qps queues for TCs 1 thru 7 and the rest
7370 * are set up for RSS in TC0
7372 queues_left -= accum_tc_size;
7374 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7376 queues_left -= pf->rss_size;
7377 if (queues_left < 0) {
7378 dev_info(&pf->pdev->dev, "not enough queues for DCB\n");
7382 pf->num_lan_qps = pf->rss_size_max + accum_tc_size;
7384 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
7385 (pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7386 !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
7388 queues_left -= 1; /* save 1 queue for FD */
7390 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7392 queues_left -= pf->rss_size;
7393 if (queues_left < 0) {
7394 dev_info(&pf->pdev->dev, "not enough queues for Flow Director\n");
7398 pf->num_lan_qps = pf->rss_size_max;
7400 } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
7401 (pf->flags & I40E_FLAG_FDIR_ENABLED) &&
7402 (pf->flags & I40E_FLAG_DCB_ENABLED)) {
7404 /* save 1 queue for TCs 1 thru 7,
7405 * 1 queue for flow director,
7406 * and the rest are set up for RSS in TC0
7409 queues_left -= accum_tc_size;
7411 pf->rss_size = i40e_set_rss_size(pf, queues_left);
7412 queues_left -= pf->rss_size;
7413 if (queues_left < 0) {
7414 dev_info(&pf->pdev->dev, "not enough queues for DCB and Flow Director\n");
7418 pf->num_lan_qps = pf->rss_size_max + accum_tc_size;
7421 dev_info(&pf->pdev->dev,
7422 "Invalid configuration, flags=0x%08llx\n", pf->flags);
7426 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7427 pf->num_vf_qps && pf->num_req_vfs && queues_left) {
7428 pf->num_req_vfs = min_t(int, pf->num_req_vfs, (queues_left /
7430 queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
7433 if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
7434 pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
7435 pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
7436 (queues_left / pf->num_vmdq_qps));
7437 queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
7440 pf->queues_left = queues_left;
7445 * i40e_setup_pf_filter_control - Setup PF static filter control
7446 * @pf: PF to be setup
7448 * i40e_setup_pf_filter_control sets up a pf's initial filter control
7449 * settings. If PE/FCoE are enabled then it will also set the per PF
7450 * based filter sizes required for them. It also enables Flow director,
7451 * ethertype and macvlan type filter settings for the pf.
7453 * Returns 0 on success, negative on failure
7455 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
7457 struct i40e_filter_control_settings *settings = &pf->filter_settings;
7459 settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
7461 /* Flow Director is enabled */
7462 if (pf->flags & (I40E_FLAG_FDIR_ENABLED | I40E_FLAG_FDIR_ATR_ENABLED))
7463 settings->enable_fdir = true;
7465 /* Ethtype and MACVLAN filters enabled for PF */
7466 settings->enable_ethtype = true;
7467 settings->enable_macvlan = true;
7469 if (i40e_set_filter_control(&pf->hw, settings))
7476 * i40e_probe - Device initialization routine
7477 * @pdev: PCI device information struct
7478 * @ent: entry in i40e_pci_tbl
7480 * i40e_probe initializes a pf identified by a pci_dev structure.
7481 * The OS initialization, configuring of the pf private structure,
7482 * and a hardware reset occur.
7484 * Returns 0 on success, negative on failure
7486 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
7488 struct i40e_driver_version dv;
7491 static u16 pfs_found;
7496 err = pci_enable_device_mem(pdev);
7500 /* set up for high or low dma */
7501 if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
7502 /* coherent mask for the same size will always succeed if
7505 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
7506 } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
7507 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
7509 dev_err(&pdev->dev, "DMA configuration failed: %d\n", err);
7514 /* set up pci connections */
7515 err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
7516 IORESOURCE_MEM), i40e_driver_name);
7518 dev_info(&pdev->dev,
7519 "pci_request_selected_regions failed %d\n", err);
7523 pci_enable_pcie_error_reporting(pdev);
7524 pci_set_master(pdev);
7526 /* Now that we have a PCI connection, we need to do the
7527 * low level device setup. This is primarily setting up
7528 * the Admin Queue structures and then querying for the
7529 * device's current profile information.
7531 pf = kzalloc(sizeof(*pf), GFP_KERNEL);
7538 set_bit(__I40E_DOWN, &pf->state);
7542 hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
7543 pci_resource_len(pdev, 0));
7546 dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
7547 (unsigned int)pci_resource_start(pdev, 0),
7548 (unsigned int)pci_resource_len(pdev, 0), err);
7551 hw->vendor_id = pdev->vendor;
7552 hw->device_id = pdev->device;
7553 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
7554 hw->subsystem_vendor_id = pdev->subsystem_vendor;
7555 hw->subsystem_device_id = pdev->subsystem_device;
7556 hw->bus.device = PCI_SLOT(pdev->devfn);
7557 hw->bus.func = PCI_FUNC(pdev->devfn);
7558 pf->instance = pfs_found;
7560 /* do a special CORER for clearing PXE mode once at init */
7561 if (hw->revision_id == 0 &&
7562 (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
7563 wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
7568 i40e_clear_pxe_mode(hw);
7571 /* Reset here to make sure all is clean and to define PF 'n' */
7572 err = i40e_pf_reset(hw);
7574 dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
7579 hw->aq.num_arq_entries = I40E_AQ_LEN;
7580 hw->aq.num_asq_entries = I40E_AQ_LEN;
7581 hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
7582 hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
7583 pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
7584 snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
7586 dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
7588 err = i40e_init_shared_code(hw);
7590 dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
7594 /* set up a default setting for link flow control */
7595 pf->hw.fc.requested_mode = I40E_FC_NONE;
7597 err = i40e_init_adminq(hw);
7598 dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
7599 if (((hw->nvm.version & I40E_NVM_VERSION_HI_MASK)
7600 >> I40E_NVM_VERSION_HI_SHIFT) != I40E_CURRENT_NVM_VERSION_HI) {
7601 dev_info(&pdev->dev,
7602 "warning: NVM version not supported, supported version: %02x.%02x\n",
7603 I40E_CURRENT_NVM_VERSION_HI,
7604 I40E_CURRENT_NVM_VERSION_LO);
7607 dev_info(&pdev->dev,
7608 "init_adminq failed: %d expecting API %02x.%02x\n",
7610 I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
7614 err = i40e_get_capabilities(pf);
7616 goto err_adminq_setup;
7618 err = i40e_sw_init(pf);
7620 dev_info(&pdev->dev, "sw_init failed: %d\n", err);
7624 err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
7625 hw->func_caps.num_rx_qp,
7626 pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
7628 dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
7629 goto err_init_lan_hmc;
7632 err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
7634 dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
7636 goto err_configure_lan_hmc;
7639 i40e_get_mac_addr(hw, hw->mac.addr);
7640 if (!is_valid_ether_addr(hw->mac.addr)) {
7641 dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
7645 dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
7646 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
7648 pci_set_drvdata(pdev, pf);
7649 pci_save_state(pdev);
7651 /* set up periodic task facility */
7652 setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
7653 pf->service_timer_period = HZ;
7655 INIT_WORK(&pf->service_task, i40e_service_task);
7656 clear_bit(__I40E_SERVICE_SCHED, &pf->state);
7657 pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
7658 pf->link_check_timeout = jiffies;
7660 /* WoL defaults to disabled */
7662 device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
7664 /* set up the main switch operations */
7665 i40e_determine_queue_usage(pf);
7666 i40e_init_interrupt_scheme(pf);
7668 /* Set up the *vsi struct based on the number of VSIs in the HW,
7669 * and set up our local tracking of the MAIN PF vsi.
7671 len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
7672 pf->vsi = kzalloc(len, GFP_KERNEL);
7675 goto err_switch_setup;
7678 err = i40e_setup_pf_switch(pf, false);
7680 dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
7684 /* The main driver is (mostly) up and happy. We need to set this state
7685 * before setting up the misc vector or we get a race and the vector
7686 * ends up disabled forever.
7688 clear_bit(__I40E_DOWN, &pf->state);
7690 /* In case of MSIX we are going to setup the misc vector right here
7691 * to handle admin queue events etc. In case of legacy and MSI
7692 * the misc functionality and queue processing is combined in
7693 * the same vector and that gets setup at open.
7695 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7696 err = i40e_setup_misc_vector(pf);
7698 dev_info(&pdev->dev,
7699 "setup of misc vector failed: %d\n", err);
7704 /* prep for VF support */
7705 if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
7706 (pf->flags & I40E_FLAG_MSIX_ENABLED)) {
7709 /* disable link interrupts for VFs */
7710 val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
7711 val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
7712 wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
7718 i40e_dbg_pf_init(pf);
7720 /* tell the firmware that we're starting */
7721 dv.major_version = DRV_VERSION_MAJOR;
7722 dv.minor_version = DRV_VERSION_MINOR;
7723 dv.build_version = DRV_VERSION_BUILD;
7724 dv.subbuild_version = 0;
7725 i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
7727 /* since everything's happy, start the service_task timer */
7728 mod_timer(&pf->service_timer,
7729 round_jiffies(jiffies + pf->service_timer_period));
7731 /* Get the negotiated link width and speed from PCI config space */
7732 pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
7734 i40e_set_pci_config_data(hw, link_status);
7736 dev_info(&pdev->dev, "PCI Express: %s %s\n",
7737 (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
7738 hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
7739 hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
7741 (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
7742 hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
7743 hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
7744 hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
7747 if (hw->bus.width < i40e_bus_width_pcie_x8 ||
7748 hw->bus.speed < i40e_bus_speed_8000) {
7749 dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
7750 dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
7755 /* Unwind what we've done if something failed in the setup */
7757 set_bit(__I40E_DOWN, &pf->state);
7758 i40e_clear_interrupt_scheme(pf);
7761 i40e_reset_interrupt_capability(pf);
7762 del_timer_sync(&pf->service_timer);
7764 err_configure_lan_hmc:
7765 (void)i40e_shutdown_lan_hmc(hw);
7768 kfree(pf->irq_pile);
7771 (void)i40e_shutdown_adminq(hw);
7773 iounmap(hw->hw_addr);
7777 pci_disable_pcie_error_reporting(pdev);
7778 pci_release_selected_regions(pdev,
7779 pci_select_bars(pdev, IORESOURCE_MEM));
7782 pci_disable_device(pdev);
7787 * i40e_remove - Device removal routine
7788 * @pdev: PCI device information struct
7790 * i40e_remove is called by the PCI subsystem to alert the driver
7791 * that is should release a PCI device. This could be caused by a
7792 * Hot-Plug event, or because the driver is going to be removed from
7795 static void i40e_remove(struct pci_dev *pdev)
7797 struct i40e_pf *pf = pci_get_drvdata(pdev);
7798 i40e_status ret_code;
7802 i40e_dbg_pf_exit(pf);
7804 if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
7806 pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
7809 /* no more scheduling of any task */
7810 set_bit(__I40E_DOWN, &pf->state);
7811 del_timer_sync(&pf->service_timer);
7812 cancel_work_sync(&pf->service_task);
7814 i40e_fdir_teardown(pf);
7816 /* If there is a switch structure or any orphans, remove them.
7817 * This will leave only the PF's VSI remaining.
7819 for (i = 0; i < I40E_MAX_VEB; i++) {
7823 if (pf->veb[i]->uplink_seid == pf->mac_seid ||
7824 pf->veb[i]->uplink_seid == 0)
7825 i40e_switch_branch_release(pf->veb[i]);
7828 /* Now we can shutdown the PF's VSI, just before we kill
7831 if (pf->vsi[pf->lan_vsi])
7832 i40e_vsi_release(pf->vsi[pf->lan_vsi]);
7834 i40e_stop_misc_vector(pf);
7835 if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
7836 synchronize_irq(pf->msix_entries[0].vector);
7837 free_irq(pf->msix_entries[0].vector, pf);
7840 /* shutdown and destroy the HMC */
7841 ret_code = i40e_shutdown_lan_hmc(&pf->hw);
7843 dev_warn(&pdev->dev,
7844 "Failed to destroy the HMC resources: %d\n", ret_code);
7846 /* shutdown the adminq */
7847 ret_code = i40e_shutdown_adminq(&pf->hw);
7849 dev_warn(&pdev->dev,
7850 "Failed to destroy the Admin Queue resources: %d\n",
7853 /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
7854 i40e_clear_interrupt_scheme(pf);
7855 for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
7857 i40e_vsi_clear_rings(pf->vsi[i]);
7858 i40e_vsi_clear(pf->vsi[i]);
7863 for (i = 0; i < I40E_MAX_VEB; i++) {
7869 kfree(pf->irq_pile);
7870 kfree(pf->sw_config);
7873 /* force a PF reset to clean anything leftover */
7874 reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
7875 wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
7876 i40e_flush(&pf->hw);
7878 iounmap(pf->hw.hw_addr);
7880 pci_release_selected_regions(pdev,
7881 pci_select_bars(pdev, IORESOURCE_MEM));
7883 pci_disable_pcie_error_reporting(pdev);
7884 pci_disable_device(pdev);
7888 * i40e_pci_error_detected - warning that something funky happened in PCI land
7889 * @pdev: PCI device information struct
7891 * Called to warn that something happened and the error handling steps
7892 * are in progress. Allows the driver to quiesce things, be ready for
7895 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
7896 enum pci_channel_state error)
7898 struct i40e_pf *pf = pci_get_drvdata(pdev);
7900 dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
7902 /* shutdown all operations */
7903 if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
7905 i40e_prep_for_reset(pf);
7909 /* Request a slot reset */
7910 return PCI_ERS_RESULT_NEED_RESET;
7914 * i40e_pci_error_slot_reset - a PCI slot reset just happened
7915 * @pdev: PCI device information struct
7917 * Called to find if the driver can work with the device now that
7918 * the pci slot has been reset. If a basic connection seems good
7919 * (registers are readable and have sane content) then return a
7920 * happy little PCI_ERS_RESULT_xxx.
7922 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
7924 struct i40e_pf *pf = pci_get_drvdata(pdev);
7925 pci_ers_result_t result;
7929 dev_info(&pdev->dev, "%s\n", __func__);
7930 if (pci_enable_device_mem(pdev)) {
7931 dev_info(&pdev->dev,
7932 "Cannot re-enable PCI device after reset.\n");
7933 result = PCI_ERS_RESULT_DISCONNECT;
7935 pci_set_master(pdev);
7936 pci_restore_state(pdev);
7937 pci_save_state(pdev);
7938 pci_wake_from_d3(pdev, false);
7940 reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
7942 result = PCI_ERS_RESULT_RECOVERED;
7944 result = PCI_ERS_RESULT_DISCONNECT;
7947 err = pci_cleanup_aer_uncorrect_error_status(pdev);
7949 dev_info(&pdev->dev,
7950 "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
7952 /* non-fatal, continue */
7959 * i40e_pci_error_resume - restart operations after PCI error recovery
7960 * @pdev: PCI device information struct
7962 * Called to allow the driver to bring things back up after PCI error
7963 * and/or reset recovery has finished.
7965 static void i40e_pci_error_resume(struct pci_dev *pdev)
7967 struct i40e_pf *pf = pci_get_drvdata(pdev);
7969 dev_info(&pdev->dev, "%s\n", __func__);
7970 if (test_bit(__I40E_SUSPENDED, &pf->state))
7974 i40e_handle_reset_warning(pf);
7979 * i40e_shutdown - PCI callback for shutting down
7980 * @pdev: PCI device information struct
7982 static void i40e_shutdown(struct pci_dev *pdev)
7984 struct i40e_pf *pf = pci_get_drvdata(pdev);
7985 struct i40e_hw *hw = &pf->hw;
7987 set_bit(__I40E_SUSPENDED, &pf->state);
7988 set_bit(__I40E_DOWN, &pf->state);
7990 i40e_prep_for_reset(pf);
7993 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
7994 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
7996 if (system_state == SYSTEM_POWER_OFF) {
7997 pci_wake_from_d3(pdev, pf->wol_en);
7998 pci_set_power_state(pdev, PCI_D3hot);
8004 * i40e_suspend - PCI callback for moving to D3
8005 * @pdev: PCI device information struct
8007 static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
8009 struct i40e_pf *pf = pci_get_drvdata(pdev);
8010 struct i40e_hw *hw = &pf->hw;
8012 set_bit(__I40E_SUSPENDED, &pf->state);
8013 set_bit(__I40E_DOWN, &pf->state);
8015 i40e_prep_for_reset(pf);
8018 wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
8019 wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
8021 pci_wake_from_d3(pdev, pf->wol_en);
8022 pci_set_power_state(pdev, PCI_D3hot);
8028 * i40e_resume - PCI callback for waking up from D3
8029 * @pdev: PCI device information struct
8031 static int i40e_resume(struct pci_dev *pdev)
8033 struct i40e_pf *pf = pci_get_drvdata(pdev);
8036 pci_set_power_state(pdev, PCI_D0);
8037 pci_restore_state(pdev);
8038 /* pci_restore_state() clears dev->state_saves, so
8039 * call pci_save_state() again to restore it.
8041 pci_save_state(pdev);
8043 err = pci_enable_device_mem(pdev);
8046 "%s: Cannot enable PCI device from suspend\n",
8050 pci_set_master(pdev);
8052 /* no wakeup events while running */
8053 pci_wake_from_d3(pdev, false);
8055 /* handling the reset will rebuild the device state */
8056 if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
8057 clear_bit(__I40E_DOWN, &pf->state);
8059 i40e_reset_and_rebuild(pf, false);
8067 static const struct pci_error_handlers i40e_err_handler = {
8068 .error_detected = i40e_pci_error_detected,
8069 .slot_reset = i40e_pci_error_slot_reset,
8070 .resume = i40e_pci_error_resume,
8073 static struct pci_driver i40e_driver = {
8074 .name = i40e_driver_name,
8075 .id_table = i40e_pci_tbl,
8076 .probe = i40e_probe,
8077 .remove = i40e_remove,
8079 .suspend = i40e_suspend,
8080 .resume = i40e_resume,
8082 .shutdown = i40e_shutdown,
8083 .err_handler = &i40e_err_handler,
8084 .sriov_configure = i40e_pci_sriov_configure,
8088 * i40e_init_module - Driver registration routine
8090 * i40e_init_module is the first routine called when the driver is
8091 * loaded. All it does is register with the PCI subsystem.
8093 static int __init i40e_init_module(void)
8095 pr_info("%s: %s - version %s\n", i40e_driver_name,
8096 i40e_driver_string, i40e_driver_version_str);
8097 pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
8099 return pci_register_driver(&i40e_driver);
8101 module_init(i40e_init_module);
8104 * i40e_exit_module - Driver exit cleanup routine
8106 * i40e_exit_module is called just before the driver is removed
8109 static void __exit i40e_exit_module(void)
8111 pci_unregister_driver(&i40e_driver);
8114 module_exit(i40e_exit_module);