1 /* Intel(R) Ethernet Switch Host Interface Driver
2 * Copyright(c) 2013 - 2017 Intel Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * The full GNU General Public License is included in this distribution in
14 * the file called "COPYING".
16 * Contact Information:
17 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
18 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/aer.h>
27 static const struct fm10k_info *fm10k_info_tbl[] = {
28 [fm10k_device_pf] = &fm10k_pf_info,
29 [fm10k_device_vf] = &fm10k_vf_info,
33 * fm10k_pci_tbl - PCI Device ID Table
35 * Wildcard entries (PCI_ANY_ID) should come last
36 * Last entry must be all 0s
38 * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
39 * Class, Class Mask, private data (not used) }
41 static const struct pci_device_id fm10k_pci_tbl[] = {
42 { PCI_VDEVICE(INTEL, FM10K_DEV_ID_PF), fm10k_device_pf },
43 { PCI_VDEVICE(INTEL, FM10K_DEV_ID_VF), fm10k_device_vf },
44 /* required last entry */
47 MODULE_DEVICE_TABLE(pci, fm10k_pci_tbl);
49 u16 fm10k_read_pci_cfg_word(struct fm10k_hw *hw, u32 reg)
51 struct fm10k_intfc *interface = hw->back;
54 if (FM10K_REMOVED(hw->hw_addr))
57 pci_read_config_word(interface->pdev, reg, &value);
59 fm10k_write_flush(hw);
64 u32 fm10k_read_reg(struct fm10k_hw *hw, int reg)
66 u32 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
69 if (FM10K_REMOVED(hw_addr))
72 value = readl(&hw_addr[reg]);
73 if (!(~value) && (!reg || !(~readl(hw_addr)))) {
74 struct fm10k_intfc *interface = hw->back;
75 struct net_device *netdev = interface->netdev;
78 netif_device_detach(netdev);
79 netdev_err(netdev, "PCIe link lost, device now detached\n");
85 static int fm10k_hw_ready(struct fm10k_intfc *interface)
87 struct fm10k_hw *hw = &interface->hw;
89 fm10k_write_flush(hw);
91 return FM10K_REMOVED(hw->hw_addr) ? -ENODEV : 0;
94 void fm10k_service_event_schedule(struct fm10k_intfc *interface)
96 if (!test_bit(__FM10K_SERVICE_DISABLE, interface->state) &&
97 !test_and_set_bit(__FM10K_SERVICE_SCHED, interface->state)) {
98 clear_bit(__FM10K_SERVICE_REQUEST, interface->state);
99 queue_work(fm10k_workqueue, &interface->service_task);
101 set_bit(__FM10K_SERVICE_REQUEST, interface->state);
105 static void fm10k_service_event_complete(struct fm10k_intfc *interface)
107 WARN_ON(!test_bit(__FM10K_SERVICE_SCHED, interface->state));
109 /* flush memory to make sure state is correct before next watchog */
110 smp_mb__before_atomic();
111 clear_bit(__FM10K_SERVICE_SCHED, interface->state);
113 /* If a service event was requested since we started, immediately
114 * re-schedule now. This ensures we don't drop a request until the
117 if (test_bit(__FM10K_SERVICE_REQUEST, interface->state))
118 fm10k_service_event_schedule(interface);
122 * fm10k_service_timer - Timer Call-back
123 * @data: pointer to interface cast into an unsigned long
125 static void fm10k_service_timer(unsigned long data)
127 struct fm10k_intfc *interface = (struct fm10k_intfc *)data;
129 /* Reset the timer */
130 mod_timer(&interface->service_timer, (HZ * 2) + jiffies);
132 fm10k_service_event_schedule(interface);
135 static void fm10k_detach_subtask(struct fm10k_intfc *interface)
137 struct net_device *netdev = interface->netdev;
138 u32 __iomem *hw_addr;
141 /* do nothing if device is still present or hw_addr is set */
142 if (netif_device_present(netdev) || interface->hw.hw_addr)
145 /* check the real address space to see if we've recovered */
146 hw_addr = READ_ONCE(interface->uc_addr);
147 value = readl(hw_addr);
149 interface->hw.hw_addr = interface->uc_addr;
150 netif_device_attach(netdev);
151 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
152 netdev_warn(netdev, "PCIe link restored, device now attached\n");
158 if (netif_running(netdev))
164 static void fm10k_prepare_for_reset(struct fm10k_intfc *interface)
166 struct net_device *netdev = interface->netdev;
168 WARN_ON(in_interrupt());
170 /* put off any impending NetWatchDogTimeout */
171 netif_trans_update(netdev);
173 while (test_and_set_bit(__FM10K_RESETTING, interface->state))
174 usleep_range(1000, 2000);
178 fm10k_iov_suspend(interface->pdev);
180 if (netif_running(netdev))
183 fm10k_mbx_free_irq(interface);
185 /* free interrupts */
186 fm10k_clear_queueing_scheme(interface);
188 /* delay any future reset requests */
189 interface->last_reset = jiffies + (10 * HZ);
194 static int fm10k_handle_reset(struct fm10k_intfc *interface)
196 struct net_device *netdev = interface->netdev;
197 struct fm10k_hw *hw = &interface->hw;
202 pci_set_master(interface->pdev);
204 /* reset and initialize the hardware so it is in a known state */
205 err = hw->mac.ops.reset_hw(hw);
207 dev_err(&interface->pdev->dev, "reset_hw failed: %d\n", err);
211 err = hw->mac.ops.init_hw(hw);
213 dev_err(&interface->pdev->dev, "init_hw failed: %d\n", err);
217 err = fm10k_init_queueing_scheme(interface);
219 dev_err(&interface->pdev->dev,
220 "init_queueing_scheme failed: %d\n", err);
224 /* re-associate interrupts */
225 err = fm10k_mbx_request_irq(interface);
229 err = fm10k_hw_ready(interface);
233 /* update hardware address for VFs if perm_addr has changed */
234 if (hw->mac.type == fm10k_mac_vf) {
235 if (is_valid_ether_addr(hw->mac.perm_addr)) {
236 ether_addr_copy(hw->mac.addr, hw->mac.perm_addr);
237 ether_addr_copy(netdev->perm_addr, hw->mac.perm_addr);
238 ether_addr_copy(netdev->dev_addr, hw->mac.perm_addr);
239 netdev->addr_assign_type &= ~NET_ADDR_RANDOM;
242 if (hw->mac.vlan_override)
243 netdev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
245 netdev->features |= NETIF_F_HW_VLAN_CTAG_RX;
248 err = netif_running(netdev) ? fm10k_open(netdev) : 0;
252 fm10k_iov_resume(interface->pdev);
256 clear_bit(__FM10K_RESETTING, interface->state);
260 fm10k_mbx_free_irq(interface);
262 fm10k_clear_queueing_scheme(interface);
264 netif_device_detach(netdev);
268 clear_bit(__FM10K_RESETTING, interface->state);
273 static void fm10k_reinit(struct fm10k_intfc *interface)
277 fm10k_prepare_for_reset(interface);
279 err = fm10k_handle_reset(interface);
281 dev_err(&interface->pdev->dev,
282 "fm10k_handle_reset failed: %d\n", err);
285 static void fm10k_reset_subtask(struct fm10k_intfc *interface)
287 if (!test_and_clear_bit(FM10K_FLAG_RESET_REQUESTED,
291 netdev_err(interface->netdev, "Reset interface\n");
293 fm10k_reinit(interface);
297 * fm10k_configure_swpri_map - Configure Receive SWPRI to PC mapping
298 * @interface: board private structure
300 * Configure the SWPRI to PC mapping for the port.
302 static void fm10k_configure_swpri_map(struct fm10k_intfc *interface)
304 struct net_device *netdev = interface->netdev;
305 struct fm10k_hw *hw = &interface->hw;
308 /* clear flag indicating update is needed */
309 clear_bit(FM10K_FLAG_SWPRI_CONFIG, interface->flags);
311 /* these registers are only available on the PF */
312 if (hw->mac.type != fm10k_mac_pf)
315 /* configure SWPRI to PC map */
316 for (i = 0; i < FM10K_SWPRI_MAX; i++)
317 fm10k_write_reg(hw, FM10K_SWPRI_MAP(i),
318 netdev_get_prio_tc_map(netdev, i));
322 * fm10k_watchdog_update_host_state - Update the link status based on host.
323 * @interface: board private structure
325 static void fm10k_watchdog_update_host_state(struct fm10k_intfc *interface)
327 struct fm10k_hw *hw = &interface->hw;
330 if (test_bit(__FM10K_LINK_DOWN, interface->state)) {
331 interface->host_ready = false;
332 if (time_is_after_jiffies(interface->link_down_event))
334 clear_bit(__FM10K_LINK_DOWN, interface->state);
337 if (test_bit(FM10K_FLAG_SWPRI_CONFIG, interface->flags)) {
338 if (rtnl_trylock()) {
339 fm10k_configure_swpri_map(interface);
344 /* lock the mailbox for transmit and receive */
345 fm10k_mbx_lock(interface);
347 err = hw->mac.ops.get_host_state(hw, &interface->host_ready);
348 if (err && time_is_before_jiffies(interface->last_reset))
349 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
352 fm10k_mbx_unlock(interface);
356 * fm10k_mbx_subtask - Process upstream and downstream mailboxes
357 * @interface: board private structure
359 * This function will process both the upstream and downstream mailboxes.
361 static void fm10k_mbx_subtask(struct fm10k_intfc *interface)
363 /* process upstream mailbox and update device state */
364 fm10k_watchdog_update_host_state(interface);
366 /* process downstream mailboxes */
367 fm10k_iov_mbx(interface);
371 * fm10k_watchdog_host_is_ready - Update netdev status based on host ready
372 * @interface: board private structure
374 static void fm10k_watchdog_host_is_ready(struct fm10k_intfc *interface)
376 struct net_device *netdev = interface->netdev;
378 /* only continue if link state is currently down */
379 if (netif_carrier_ok(netdev))
382 netif_info(interface, drv, netdev, "NIC Link is up\n");
384 netif_carrier_on(netdev);
385 netif_tx_wake_all_queues(netdev);
389 * fm10k_watchdog_host_not_ready - Update netdev status based on host not ready
390 * @interface: board private structure
392 static void fm10k_watchdog_host_not_ready(struct fm10k_intfc *interface)
394 struct net_device *netdev = interface->netdev;
396 /* only continue if link state is currently up */
397 if (!netif_carrier_ok(netdev))
400 netif_info(interface, drv, netdev, "NIC Link is down\n");
402 netif_carrier_off(netdev);
403 netif_tx_stop_all_queues(netdev);
407 * fm10k_update_stats - Update the board statistics counters.
408 * @interface: board private structure
410 void fm10k_update_stats(struct fm10k_intfc *interface)
412 struct net_device_stats *net_stats = &interface->netdev->stats;
413 struct fm10k_hw *hw = &interface->hw;
414 u64 hw_csum_tx_good = 0, hw_csum_rx_good = 0, rx_length_errors = 0;
415 u64 rx_switch_errors = 0, rx_drops = 0, rx_pp_errors = 0;
416 u64 rx_link_errors = 0;
417 u64 rx_errors = 0, rx_csum_errors = 0, tx_csum_errors = 0;
418 u64 restart_queue = 0, tx_busy = 0, alloc_failed = 0;
419 u64 rx_bytes_nic = 0, rx_pkts_nic = 0, rx_drops_nic = 0;
420 u64 tx_bytes_nic = 0, tx_pkts_nic = 0;
424 /* ensure only one thread updates stats at a time */
425 if (test_and_set_bit(__FM10K_UPDATING_STATS, interface->state))
428 /* do not allow stats update via service task for next second */
429 interface->next_stats_update = jiffies + HZ;
431 /* gather some stats to the interface struct that are per queue */
432 for (bytes = 0, pkts = 0, i = 0; i < interface->num_tx_queues; i++) {
433 struct fm10k_ring *tx_ring = READ_ONCE(interface->tx_ring[i]);
438 restart_queue += tx_ring->tx_stats.restart_queue;
439 tx_busy += tx_ring->tx_stats.tx_busy;
440 tx_csum_errors += tx_ring->tx_stats.csum_err;
441 bytes += tx_ring->stats.bytes;
442 pkts += tx_ring->stats.packets;
443 hw_csum_tx_good += tx_ring->tx_stats.csum_good;
446 interface->restart_queue = restart_queue;
447 interface->tx_busy = tx_busy;
448 net_stats->tx_bytes = bytes;
449 net_stats->tx_packets = pkts;
450 interface->tx_csum_errors = tx_csum_errors;
451 interface->hw_csum_tx_good = hw_csum_tx_good;
453 /* gather some stats to the interface struct that are per queue */
454 for (bytes = 0, pkts = 0, i = 0; i < interface->num_rx_queues; i++) {
455 struct fm10k_ring *rx_ring = READ_ONCE(interface->rx_ring[i]);
460 bytes += rx_ring->stats.bytes;
461 pkts += rx_ring->stats.packets;
462 alloc_failed += rx_ring->rx_stats.alloc_failed;
463 rx_csum_errors += rx_ring->rx_stats.csum_err;
464 rx_errors += rx_ring->rx_stats.errors;
465 hw_csum_rx_good += rx_ring->rx_stats.csum_good;
466 rx_switch_errors += rx_ring->rx_stats.switch_errors;
467 rx_drops += rx_ring->rx_stats.drops;
468 rx_pp_errors += rx_ring->rx_stats.pp_errors;
469 rx_link_errors += rx_ring->rx_stats.link_errors;
470 rx_length_errors += rx_ring->rx_stats.length_errors;
473 net_stats->rx_bytes = bytes;
474 net_stats->rx_packets = pkts;
475 interface->alloc_failed = alloc_failed;
476 interface->rx_csum_errors = rx_csum_errors;
477 interface->hw_csum_rx_good = hw_csum_rx_good;
478 interface->rx_switch_errors = rx_switch_errors;
479 interface->rx_drops = rx_drops;
480 interface->rx_pp_errors = rx_pp_errors;
481 interface->rx_link_errors = rx_link_errors;
482 interface->rx_length_errors = rx_length_errors;
484 hw->mac.ops.update_hw_stats(hw, &interface->stats);
486 for (i = 0; i < hw->mac.max_queues; i++) {
487 struct fm10k_hw_stats_q *q = &interface->stats.q[i];
489 tx_bytes_nic += q->tx_bytes.count;
490 tx_pkts_nic += q->tx_packets.count;
491 rx_bytes_nic += q->rx_bytes.count;
492 rx_pkts_nic += q->rx_packets.count;
493 rx_drops_nic += q->rx_drops.count;
496 interface->tx_bytes_nic = tx_bytes_nic;
497 interface->tx_packets_nic = tx_pkts_nic;
498 interface->rx_bytes_nic = rx_bytes_nic;
499 interface->rx_packets_nic = rx_pkts_nic;
500 interface->rx_drops_nic = rx_drops_nic;
502 /* Fill out the OS statistics structure */
503 net_stats->rx_errors = rx_errors;
504 net_stats->rx_dropped = interface->stats.nodesc_drop.count;
506 clear_bit(__FM10K_UPDATING_STATS, interface->state);
510 * fm10k_watchdog_flush_tx - flush queues on host not ready
511 * @interface - pointer to the device interface structure
513 static void fm10k_watchdog_flush_tx(struct fm10k_intfc *interface)
515 int some_tx_pending = 0;
518 /* nothing to do if carrier is up */
519 if (netif_carrier_ok(interface->netdev))
522 for (i = 0; i < interface->num_tx_queues; i++) {
523 struct fm10k_ring *tx_ring = interface->tx_ring[i];
525 if (tx_ring->next_to_use != tx_ring->next_to_clean) {
531 /* We've lost link, so the controller stops DMA, but we've got
532 * queued Tx work that's never going to get done, so reset
533 * controller to flush Tx.
536 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
540 * fm10k_watchdog_subtask - check and bring link up
541 * @interface - pointer to the device interface structure
543 static void fm10k_watchdog_subtask(struct fm10k_intfc *interface)
545 /* if interface is down do nothing */
546 if (test_bit(__FM10K_DOWN, interface->state) ||
547 test_bit(__FM10K_RESETTING, interface->state))
550 if (interface->host_ready)
551 fm10k_watchdog_host_is_ready(interface);
553 fm10k_watchdog_host_not_ready(interface);
555 /* update stats only once every second */
556 if (time_is_before_jiffies(interface->next_stats_update))
557 fm10k_update_stats(interface);
559 /* flush any uncompleted work */
560 fm10k_watchdog_flush_tx(interface);
564 * fm10k_check_hang_subtask - check for hung queues and dropped interrupts
565 * @interface - pointer to the device interface structure
567 * This function serves two purposes. First it strobes the interrupt lines
568 * in order to make certain interrupts are occurring. Secondly it sets the
569 * bits needed to check for TX hangs. As a result we should immediately
570 * determine if a hang has occurred.
572 static void fm10k_check_hang_subtask(struct fm10k_intfc *interface)
576 /* If we're down or resetting, just bail */
577 if (test_bit(__FM10K_DOWN, interface->state) ||
578 test_bit(__FM10K_RESETTING, interface->state))
581 /* rate limit tx hang checks to only once every 2 seconds */
582 if (time_is_after_eq_jiffies(interface->next_tx_hang_check))
584 interface->next_tx_hang_check = jiffies + (2 * HZ);
586 if (netif_carrier_ok(interface->netdev)) {
587 /* Force detection of hung controller */
588 for (i = 0; i < interface->num_tx_queues; i++)
589 set_check_for_tx_hang(interface->tx_ring[i]);
591 /* Rearm all in-use q_vectors for immediate firing */
592 for (i = 0; i < interface->num_q_vectors; i++) {
593 struct fm10k_q_vector *qv = interface->q_vector[i];
595 if (!qv->tx.count && !qv->rx.count)
597 writel(FM10K_ITR_ENABLE | FM10K_ITR_PENDING2, qv->itr);
603 * fm10k_service_task - manages and runs subtasks
604 * @work: pointer to work_struct containing our data
606 static void fm10k_service_task(struct work_struct *work)
608 struct fm10k_intfc *interface;
610 interface = container_of(work, struct fm10k_intfc, service_task);
612 /* tasks run even when interface is down */
613 fm10k_mbx_subtask(interface);
614 fm10k_detach_subtask(interface);
615 fm10k_reset_subtask(interface);
617 /* tasks only run when interface is up */
618 fm10k_watchdog_subtask(interface);
619 fm10k_check_hang_subtask(interface);
621 /* release lock on service events to allow scheduling next event */
622 fm10k_service_event_complete(interface);
626 * fm10k_configure_tx_ring - Configure Tx ring after Reset
627 * @interface: board private structure
628 * @ring: structure containing ring specific data
630 * Configure the Tx descriptor ring after a reset.
632 static void fm10k_configure_tx_ring(struct fm10k_intfc *interface,
633 struct fm10k_ring *ring)
635 struct fm10k_hw *hw = &interface->hw;
636 u64 tdba = ring->dma;
637 u32 size = ring->count * sizeof(struct fm10k_tx_desc);
638 u32 txint = FM10K_INT_MAP_DISABLE;
639 u32 txdctl = BIT(FM10K_TXDCTL_MAX_TIME_SHIFT) | FM10K_TXDCTL_ENABLE;
640 u8 reg_idx = ring->reg_idx;
642 /* disable queue to avoid issues while updating state */
643 fm10k_write_reg(hw, FM10K_TXDCTL(reg_idx), 0);
644 fm10k_write_flush(hw);
646 /* possible poll here to verify ring resources have been cleaned */
648 /* set location and size for descriptor ring */
649 fm10k_write_reg(hw, FM10K_TDBAL(reg_idx), tdba & DMA_BIT_MASK(32));
650 fm10k_write_reg(hw, FM10K_TDBAH(reg_idx), tdba >> 32);
651 fm10k_write_reg(hw, FM10K_TDLEN(reg_idx), size);
653 /* reset head and tail pointers */
654 fm10k_write_reg(hw, FM10K_TDH(reg_idx), 0);
655 fm10k_write_reg(hw, FM10K_TDT(reg_idx), 0);
657 /* store tail pointer */
658 ring->tail = &interface->uc_addr[FM10K_TDT(reg_idx)];
660 /* reset ntu and ntc to place SW in sync with hardware */
661 ring->next_to_clean = 0;
662 ring->next_to_use = 0;
665 if (ring->q_vector) {
666 txint = ring->q_vector->v_idx + NON_Q_VECTORS(hw);
667 txint |= FM10K_INT_MAP_TIMER0;
670 fm10k_write_reg(hw, FM10K_TXINT(reg_idx), txint);
672 /* enable use of FTAG bit in Tx descriptor, register is RO for VF */
673 fm10k_write_reg(hw, FM10K_PFVTCTL(reg_idx),
674 FM10K_PFVTCTL_FTAG_DESC_ENABLE);
677 if (!test_and_set_bit(__FM10K_TX_XPS_INIT_DONE, ring->state) &&
679 netif_set_xps_queue(ring->netdev,
680 &ring->q_vector->affinity_mask,
684 fm10k_write_reg(hw, FM10K_TXDCTL(reg_idx), txdctl);
688 * fm10k_enable_tx_ring - Verify Tx ring is enabled after configuration
689 * @interface: board private structure
690 * @ring: structure containing ring specific data
692 * Verify the Tx descriptor ring is ready for transmit.
694 static void fm10k_enable_tx_ring(struct fm10k_intfc *interface,
695 struct fm10k_ring *ring)
697 struct fm10k_hw *hw = &interface->hw;
700 u8 reg_idx = ring->reg_idx;
702 /* if we are already enabled just exit */
703 if (fm10k_read_reg(hw, FM10K_TXDCTL(reg_idx)) & FM10K_TXDCTL_ENABLE)
706 /* poll to verify queue is enabled */
708 usleep_range(1000, 2000);
709 txdctl = fm10k_read_reg(hw, FM10K_TXDCTL(reg_idx));
710 } while (!(txdctl & FM10K_TXDCTL_ENABLE) && --wait_loop);
712 netif_err(interface, drv, interface->netdev,
713 "Could not enable Tx Queue %d\n", reg_idx);
717 * fm10k_configure_tx - Configure Transmit Unit after Reset
718 * @interface: board private structure
720 * Configure the Tx unit of the MAC after a reset.
722 static void fm10k_configure_tx(struct fm10k_intfc *interface)
726 /* Setup the HW Tx Head and Tail descriptor pointers */
727 for (i = 0; i < interface->num_tx_queues; i++)
728 fm10k_configure_tx_ring(interface, interface->tx_ring[i]);
730 /* poll here to verify that Tx rings are now enabled */
731 for (i = 0; i < interface->num_tx_queues; i++)
732 fm10k_enable_tx_ring(interface, interface->tx_ring[i]);
736 * fm10k_configure_rx_ring - Configure Rx ring after Reset
737 * @interface: board private structure
738 * @ring: structure containing ring specific data
740 * Configure the Rx descriptor ring after a reset.
742 static void fm10k_configure_rx_ring(struct fm10k_intfc *interface,
743 struct fm10k_ring *ring)
745 u64 rdba = ring->dma;
746 struct fm10k_hw *hw = &interface->hw;
747 u32 size = ring->count * sizeof(union fm10k_rx_desc);
748 u32 rxqctl, rxdctl = FM10K_RXDCTL_WRITE_BACK_MIN_DELAY;
749 u32 srrctl = FM10K_SRRCTL_BUFFER_CHAINING_EN;
750 u32 rxint = FM10K_INT_MAP_DISABLE;
751 u8 rx_pause = interface->rx_pause;
752 u8 reg_idx = ring->reg_idx;
754 /* disable queue to avoid issues while updating state */
755 rxqctl = fm10k_read_reg(hw, FM10K_RXQCTL(reg_idx));
756 rxqctl &= ~FM10K_RXQCTL_ENABLE;
757 fm10k_write_reg(hw, FM10K_RXQCTL(reg_idx), rxqctl);
758 fm10k_write_flush(hw);
760 /* possible poll here to verify ring resources have been cleaned */
762 /* set location and size for descriptor ring */
763 fm10k_write_reg(hw, FM10K_RDBAL(reg_idx), rdba & DMA_BIT_MASK(32));
764 fm10k_write_reg(hw, FM10K_RDBAH(reg_idx), rdba >> 32);
765 fm10k_write_reg(hw, FM10K_RDLEN(reg_idx), size);
767 /* reset head and tail pointers */
768 fm10k_write_reg(hw, FM10K_RDH(reg_idx), 0);
769 fm10k_write_reg(hw, FM10K_RDT(reg_idx), 0);
771 /* store tail pointer */
772 ring->tail = &interface->uc_addr[FM10K_RDT(reg_idx)];
774 /* reset ntu and ntc to place SW in sync with hardware */
775 ring->next_to_clean = 0;
776 ring->next_to_use = 0;
777 ring->next_to_alloc = 0;
779 /* Configure the Rx buffer size for one buff without split */
780 srrctl |= FM10K_RX_BUFSZ >> FM10K_SRRCTL_BSIZEPKT_SHIFT;
782 /* Configure the Rx ring to suppress loopback packets */
783 srrctl |= FM10K_SRRCTL_LOOPBACK_SUPPRESS;
784 fm10k_write_reg(hw, FM10K_SRRCTL(reg_idx), srrctl);
786 /* Enable drop on empty */
788 if (interface->pfc_en)
789 rx_pause = interface->pfc_en;
791 if (!(rx_pause & BIT(ring->qos_pc)))
792 rxdctl |= FM10K_RXDCTL_DROP_ON_EMPTY;
794 fm10k_write_reg(hw, FM10K_RXDCTL(reg_idx), rxdctl);
796 /* assign default VLAN to queue */
797 ring->vid = hw->mac.default_vid;
799 /* if we have an active VLAN, disable default VLAN ID */
800 if (test_bit(hw->mac.default_vid, interface->active_vlans))
801 ring->vid |= FM10K_VLAN_CLEAR;
804 if (ring->q_vector) {
805 rxint = ring->q_vector->v_idx + NON_Q_VECTORS(hw);
806 rxint |= FM10K_INT_MAP_TIMER1;
809 fm10k_write_reg(hw, FM10K_RXINT(reg_idx), rxint);
812 rxqctl = fm10k_read_reg(hw, FM10K_RXQCTL(reg_idx));
813 rxqctl |= FM10K_RXQCTL_ENABLE;
814 fm10k_write_reg(hw, FM10K_RXQCTL(reg_idx), rxqctl);
816 /* place buffers on ring for receive data */
817 fm10k_alloc_rx_buffers(ring, fm10k_desc_unused(ring));
821 * fm10k_update_rx_drop_en - Configures the drop enable bits for Rx rings
822 * @interface: board private structure
824 * Configure the drop enable bits for the Rx rings.
826 void fm10k_update_rx_drop_en(struct fm10k_intfc *interface)
828 struct fm10k_hw *hw = &interface->hw;
829 u8 rx_pause = interface->rx_pause;
833 if (interface->pfc_en)
834 rx_pause = interface->pfc_en;
837 for (i = 0; i < interface->num_rx_queues; i++) {
838 struct fm10k_ring *ring = interface->rx_ring[i];
839 u32 rxdctl = FM10K_RXDCTL_WRITE_BACK_MIN_DELAY;
840 u8 reg_idx = ring->reg_idx;
842 if (!(rx_pause & BIT(ring->qos_pc)))
843 rxdctl |= FM10K_RXDCTL_DROP_ON_EMPTY;
845 fm10k_write_reg(hw, FM10K_RXDCTL(reg_idx), rxdctl);
850 * fm10k_configure_dglort - Configure Receive DGLORT after reset
851 * @interface: board private structure
853 * Configure the DGLORT description and RSS tables.
855 static void fm10k_configure_dglort(struct fm10k_intfc *interface)
857 struct fm10k_dglort_cfg dglort = { 0 };
858 struct fm10k_hw *hw = &interface->hw;
862 /* Fill out hash function seeds */
863 for (i = 0; i < FM10K_RSSRK_SIZE; i++)
864 fm10k_write_reg(hw, FM10K_RSSRK(0, i), interface->rssrk[i]);
866 /* Write RETA table to hardware */
867 for (i = 0; i < FM10K_RETA_SIZE; i++)
868 fm10k_write_reg(hw, FM10K_RETA(0, i), interface->reta[i]);
870 /* Generate RSS hash based on packet types, TCP/UDP
871 * port numbers and/or IPv4/v6 src and dst addresses
873 mrqc = FM10K_MRQC_IPV4 |
874 FM10K_MRQC_TCP_IPV4 |
878 if (test_bit(FM10K_FLAG_RSS_FIELD_IPV4_UDP, interface->flags))
879 mrqc |= FM10K_MRQC_UDP_IPV4;
880 if (test_bit(FM10K_FLAG_RSS_FIELD_IPV6_UDP, interface->flags))
881 mrqc |= FM10K_MRQC_UDP_IPV6;
883 fm10k_write_reg(hw, FM10K_MRQC(0), mrqc);
885 /* configure default DGLORT mapping for RSS/DCB */
886 dglort.inner_rss = 1;
887 dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
888 dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
889 hw->mac.ops.configure_dglort_map(hw, &dglort);
891 /* assign GLORT per queue for queue mapped testing */
892 if (interface->glort_count > 64) {
893 memset(&dglort, 0, sizeof(dglort));
894 dglort.inner_rss = 1;
895 dglort.glort = interface->glort + 64;
896 dglort.idx = fm10k_dglort_pf_queue;
897 dglort.queue_l = fls(interface->num_rx_queues - 1);
898 hw->mac.ops.configure_dglort_map(hw, &dglort);
901 /* assign glort value for RSS/DCB specific to this interface */
902 memset(&dglort, 0, sizeof(dglort));
903 dglort.inner_rss = 1;
904 dglort.glort = interface->glort;
905 dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
906 dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
907 /* configure DGLORT mapping for RSS/DCB */
908 dglort.idx = fm10k_dglort_pf_rss;
909 if (interface->l2_accel)
910 dglort.shared_l = fls(interface->l2_accel->size);
911 hw->mac.ops.configure_dglort_map(hw, &dglort);
915 * fm10k_configure_rx - Configure Receive Unit after Reset
916 * @interface: board private structure
918 * Configure the Rx unit of the MAC after a reset.
920 static void fm10k_configure_rx(struct fm10k_intfc *interface)
924 /* Configure SWPRI to PC map */
925 fm10k_configure_swpri_map(interface);
927 /* Configure RSS and DGLORT map */
928 fm10k_configure_dglort(interface);
930 /* Setup the HW Rx Head and Tail descriptor pointers */
931 for (i = 0; i < interface->num_rx_queues; i++)
932 fm10k_configure_rx_ring(interface, interface->rx_ring[i]);
934 /* possible poll here to verify that Rx rings are now enabled */
937 static void fm10k_napi_enable_all(struct fm10k_intfc *interface)
939 struct fm10k_q_vector *q_vector;
942 for (q_idx = 0; q_idx < interface->num_q_vectors; q_idx++) {
943 q_vector = interface->q_vector[q_idx];
944 napi_enable(&q_vector->napi);
948 static irqreturn_t fm10k_msix_clean_rings(int __always_unused irq, void *data)
950 struct fm10k_q_vector *q_vector = data;
952 if (q_vector->rx.count || q_vector->tx.count)
953 napi_schedule_irqoff(&q_vector->napi);
958 static irqreturn_t fm10k_msix_mbx_vf(int __always_unused irq, void *data)
960 struct fm10k_intfc *interface = data;
961 struct fm10k_hw *hw = &interface->hw;
962 struct fm10k_mbx_info *mbx = &hw->mbx;
964 /* re-enable mailbox interrupt and indicate 20us delay */
965 fm10k_write_reg(hw, FM10K_VFITR(FM10K_MBX_VECTOR),
966 (FM10K_MBX_INT_DELAY >> hw->mac.itr_scale) |
969 /* service upstream mailbox */
970 if (fm10k_mbx_trylock(interface)) {
971 mbx->ops.process(hw, mbx);
972 fm10k_mbx_unlock(interface);
975 hw->mac.get_host_state = true;
976 fm10k_service_event_schedule(interface);
981 #ifdef CONFIG_NET_POLL_CONTROLLER
983 * fm10k_netpoll - A Polling 'interrupt' handler
984 * @netdev: network interface device structure
986 * This is used by netconsole to send skbs without having to re-enable
987 * interrupts. It's not called while the normal interrupt routine is executing.
989 void fm10k_netpoll(struct net_device *netdev)
991 struct fm10k_intfc *interface = netdev_priv(netdev);
994 /* if interface is down do nothing */
995 if (test_bit(__FM10K_DOWN, interface->state))
998 for (i = 0; i < interface->num_q_vectors; i++)
999 fm10k_msix_clean_rings(0, interface->q_vector[i]);
1003 #define FM10K_ERR_MSG(type) case (type): error = #type; break
1004 static void fm10k_handle_fault(struct fm10k_intfc *interface, int type,
1005 struct fm10k_fault *fault)
1007 struct pci_dev *pdev = interface->pdev;
1008 struct fm10k_hw *hw = &interface->hw;
1009 struct fm10k_iov_data *iov_data = interface->iov_data;
1013 case FM10K_PCA_FAULT:
1014 switch (fault->type) {
1016 error = "Unknown PCA error";
1018 FM10K_ERR_MSG(PCA_NO_FAULT);
1019 FM10K_ERR_MSG(PCA_UNMAPPED_ADDR);
1020 FM10K_ERR_MSG(PCA_BAD_QACCESS_PF);
1021 FM10K_ERR_MSG(PCA_BAD_QACCESS_VF);
1022 FM10K_ERR_MSG(PCA_MALICIOUS_REQ);
1023 FM10K_ERR_MSG(PCA_POISONED_TLP);
1024 FM10K_ERR_MSG(PCA_TLP_ABORT);
1027 case FM10K_THI_FAULT:
1028 switch (fault->type) {
1030 error = "Unknown THI error";
1032 FM10K_ERR_MSG(THI_NO_FAULT);
1033 FM10K_ERR_MSG(THI_MAL_DIS_Q_FAULT);
1036 case FM10K_FUM_FAULT:
1037 switch (fault->type) {
1039 error = "Unknown FUM error";
1041 FM10K_ERR_MSG(FUM_NO_FAULT);
1042 FM10K_ERR_MSG(FUM_UNMAPPED_ADDR);
1043 FM10K_ERR_MSG(FUM_BAD_VF_QACCESS);
1044 FM10K_ERR_MSG(FUM_ADD_DECODE_ERR);
1045 FM10K_ERR_MSG(FUM_RO_ERROR);
1046 FM10K_ERR_MSG(FUM_QPRC_CRC_ERROR);
1047 FM10K_ERR_MSG(FUM_CSR_TIMEOUT);
1048 FM10K_ERR_MSG(FUM_INVALID_TYPE);
1049 FM10K_ERR_MSG(FUM_INVALID_LENGTH);
1050 FM10K_ERR_MSG(FUM_INVALID_BE);
1051 FM10K_ERR_MSG(FUM_INVALID_ALIGN);
1055 error = "Undocumented fault";
1059 dev_warn(&pdev->dev,
1060 "%s Address: 0x%llx SpecInfo: 0x%x Func: %02x.%0x\n",
1061 error, fault->address, fault->specinfo,
1062 PCI_SLOT(fault->func), PCI_FUNC(fault->func));
1064 /* For VF faults, clear out the respective LPORT, reset the queue
1065 * resources, and then reconnect to the mailbox. This allows the
1066 * VF in question to resume behavior. For transient faults that are
1067 * the result of non-malicious behavior this will log the fault and
1068 * allow the VF to resume functionality. Obviously for malicious VFs
1069 * they will be able to attempt malicious behavior again. In this
1070 * case, the system administrator will need to step in and manually
1071 * remove or disable the VF in question.
1073 if (fault->func && iov_data) {
1074 int vf = fault->func - 1;
1075 struct fm10k_vf_info *vf_info = &iov_data->vf_info[vf];
1077 hw->iov.ops.reset_lport(hw, vf_info);
1078 hw->iov.ops.reset_resources(hw, vf_info);
1080 /* reset_lport disables the VF, so re-enable it */
1081 hw->iov.ops.set_lport(hw, vf_info, vf,
1082 FM10K_VF_FLAG_MULTI_CAPABLE);
1084 /* reset_resources will disconnect from the mbx */
1085 vf_info->mbx.ops.connect(hw, &vf_info->mbx);
1089 static void fm10k_report_fault(struct fm10k_intfc *interface, u32 eicr)
1091 struct fm10k_hw *hw = &interface->hw;
1092 struct fm10k_fault fault = { 0 };
1095 for (eicr &= FM10K_EICR_FAULT_MASK, type = FM10K_PCA_FAULT;
1097 eicr >>= 1, type += FM10K_FAULT_SIZE) {
1098 /* only check if there is an error reported */
1102 /* retrieve fault info */
1103 err = hw->mac.ops.get_fault(hw, type, &fault);
1105 dev_err(&interface->pdev->dev,
1106 "error reading fault\n");
1110 fm10k_handle_fault(interface, type, &fault);
1114 static void fm10k_reset_drop_on_empty(struct fm10k_intfc *interface, u32 eicr)
1116 struct fm10k_hw *hw = &interface->hw;
1117 const u32 rxdctl = FM10K_RXDCTL_WRITE_BACK_MIN_DELAY;
1121 if (!(eicr & FM10K_EICR_MAXHOLDTIME))
1124 maxholdq = fm10k_read_reg(hw, FM10K_MAXHOLDQ(7));
1126 fm10k_write_reg(hw, FM10K_MAXHOLDQ(7), maxholdq);
1128 if (maxholdq & BIT(31)) {
1129 if (q < FM10K_MAX_QUEUES_PF) {
1130 interface->rx_overrun_pf++;
1131 fm10k_write_reg(hw, FM10K_RXDCTL(q), rxdctl);
1133 interface->rx_overrun_vf++;
1147 maxholdq = fm10k_read_reg(hw, FM10K_MAXHOLDQ(q / 32));
1149 fm10k_write_reg(hw, FM10K_MAXHOLDQ(q / 32), maxholdq);
1153 static irqreturn_t fm10k_msix_mbx_pf(int __always_unused irq, void *data)
1155 struct fm10k_intfc *interface = data;
1156 struct fm10k_hw *hw = &interface->hw;
1157 struct fm10k_mbx_info *mbx = &hw->mbx;
1161 /* unmask any set bits related to this interrupt */
1162 eicr = fm10k_read_reg(hw, FM10K_EICR);
1163 fm10k_write_reg(hw, FM10K_EICR, eicr & (FM10K_EICR_MAILBOX |
1164 FM10K_EICR_SWITCHREADY |
1165 FM10K_EICR_SWITCHNOTREADY));
1167 /* report any faults found to the message log */
1168 fm10k_report_fault(interface, eicr);
1170 /* reset any queues disabled due to receiver overrun */
1171 fm10k_reset_drop_on_empty(interface, eicr);
1173 /* service mailboxes */
1174 if (fm10k_mbx_trylock(interface)) {
1175 err = mbx->ops.process(hw, mbx);
1176 /* handle VFLRE events */
1177 fm10k_iov_event(interface);
1178 fm10k_mbx_unlock(interface);
1181 if (err == FM10K_ERR_RESET_REQUESTED)
1182 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1184 /* if switch toggled state we should reset GLORTs */
1185 if (eicr & FM10K_EICR_SWITCHNOTREADY) {
1186 /* force link down for at least 4 seconds */
1187 interface->link_down_event = jiffies + (4 * HZ);
1188 set_bit(__FM10K_LINK_DOWN, interface->state);
1190 /* reset dglort_map back to no config */
1191 hw->mac.dglort_map = FM10K_DGLORTMAP_NONE;
1194 /* we should validate host state after interrupt event */
1195 hw->mac.get_host_state = true;
1197 /* validate host state, and handle VF mailboxes in the service task */
1198 fm10k_service_event_schedule(interface);
1200 /* re-enable mailbox interrupt and indicate 20us delay */
1201 fm10k_write_reg(hw, FM10K_ITR(FM10K_MBX_VECTOR),
1202 (FM10K_MBX_INT_DELAY >> hw->mac.itr_scale) |
1208 void fm10k_mbx_free_irq(struct fm10k_intfc *interface)
1210 struct fm10k_hw *hw = &interface->hw;
1211 struct msix_entry *entry;
1214 /* no mailbox IRQ to free if MSI-X is not enabled */
1215 if (!interface->msix_entries)
1218 entry = &interface->msix_entries[FM10K_MBX_VECTOR];
1220 /* disconnect the mailbox */
1221 hw->mbx.ops.disconnect(hw, &hw->mbx);
1223 /* disable Mailbox cause */
1224 if (hw->mac.type == fm10k_mac_pf) {
1225 fm10k_write_reg(hw, FM10K_EIMR,
1226 FM10K_EIMR_DISABLE(PCA_FAULT) |
1227 FM10K_EIMR_DISABLE(FUM_FAULT) |
1228 FM10K_EIMR_DISABLE(MAILBOX) |
1229 FM10K_EIMR_DISABLE(SWITCHREADY) |
1230 FM10K_EIMR_DISABLE(SWITCHNOTREADY) |
1231 FM10K_EIMR_DISABLE(SRAMERROR) |
1232 FM10K_EIMR_DISABLE(VFLR) |
1233 FM10K_EIMR_DISABLE(MAXHOLDTIME));
1234 itr_reg = FM10K_ITR(FM10K_MBX_VECTOR);
1236 itr_reg = FM10K_VFITR(FM10K_MBX_VECTOR);
1239 fm10k_write_reg(hw, itr_reg, FM10K_ITR_MASK_SET);
1241 free_irq(entry->vector, interface);
1244 static s32 fm10k_mbx_mac_addr(struct fm10k_hw *hw, u32 **results,
1245 struct fm10k_mbx_info *mbx)
1247 bool vlan_override = hw->mac.vlan_override;
1248 u16 default_vid = hw->mac.default_vid;
1249 struct fm10k_intfc *interface;
1252 err = fm10k_msg_mac_vlan_vf(hw, results, mbx);
1256 interface = container_of(hw, struct fm10k_intfc, hw);
1258 /* MAC was changed so we need reset */
1259 if (is_valid_ether_addr(hw->mac.perm_addr) &&
1260 !ether_addr_equal(hw->mac.perm_addr, hw->mac.addr))
1261 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1263 /* VLAN override was changed, or default VLAN changed */
1264 if ((vlan_override != hw->mac.vlan_override) ||
1265 (default_vid != hw->mac.default_vid))
1266 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1271 /* generic error handler for mailbox issues */
1272 static s32 fm10k_mbx_error(struct fm10k_hw *hw, u32 **results,
1273 struct fm10k_mbx_info __always_unused *mbx)
1275 struct fm10k_intfc *interface;
1276 struct pci_dev *pdev;
1278 interface = container_of(hw, struct fm10k_intfc, hw);
1279 pdev = interface->pdev;
1281 dev_err(&pdev->dev, "Unknown message ID %u\n",
1282 **results & FM10K_TLV_ID_MASK);
1287 static const struct fm10k_msg_data vf_mbx_data[] = {
1288 FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test),
1289 FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_mbx_mac_addr),
1290 FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_msg_lport_state_vf),
1291 FM10K_TLV_MSG_ERROR_HANDLER(fm10k_mbx_error),
1294 static int fm10k_mbx_request_irq_vf(struct fm10k_intfc *interface)
1296 struct msix_entry *entry = &interface->msix_entries[FM10K_MBX_VECTOR];
1297 struct net_device *dev = interface->netdev;
1298 struct fm10k_hw *hw = &interface->hw;
1301 /* Use timer0 for interrupt moderation on the mailbox */
1302 u32 itr = entry->entry | FM10K_INT_MAP_TIMER0;
1304 /* register mailbox handlers */
1305 err = hw->mbx.ops.register_handlers(&hw->mbx, vf_mbx_data);
1309 /* request the IRQ */
1310 err = request_irq(entry->vector, fm10k_msix_mbx_vf, 0,
1311 dev->name, interface);
1313 netif_err(interface, probe, dev,
1314 "request_irq for msix_mbx failed: %d\n", err);
1318 /* map all of the interrupt sources */
1319 fm10k_write_reg(hw, FM10K_VFINT_MAP, itr);
1321 /* enable interrupt */
1322 fm10k_write_reg(hw, FM10K_VFITR(entry->entry), FM10K_ITR_ENABLE);
1327 static s32 fm10k_lport_map(struct fm10k_hw *hw, u32 **results,
1328 struct fm10k_mbx_info *mbx)
1330 struct fm10k_intfc *interface;
1331 u32 dglort_map = hw->mac.dglort_map;
1334 interface = container_of(hw, struct fm10k_intfc, hw);
1336 err = fm10k_msg_err_pf(hw, results, mbx);
1337 if (!err && hw->swapi.status) {
1338 /* force link down for a reasonable delay */
1339 interface->link_down_event = jiffies + (2 * HZ);
1340 set_bit(__FM10K_LINK_DOWN, interface->state);
1342 /* reset dglort_map back to no config */
1343 hw->mac.dglort_map = FM10K_DGLORTMAP_NONE;
1345 fm10k_service_event_schedule(interface);
1347 /* prevent overloading kernel message buffer */
1348 if (interface->lport_map_failed)
1351 interface->lport_map_failed = true;
1353 if (hw->swapi.status == FM10K_MSG_ERR_PEP_NOT_SCHEDULED)
1354 dev_warn(&interface->pdev->dev,
1355 "cannot obtain link because the host interface is configured for a PCIe host interface bandwidth of zero\n");
1356 dev_warn(&interface->pdev->dev,
1357 "request logical port map failed: %d\n",
1363 err = fm10k_msg_lport_map_pf(hw, results, mbx);
1367 interface->lport_map_failed = false;
1369 /* we need to reset if port count was just updated */
1370 if (dglort_map != hw->mac.dglort_map)
1371 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1376 static s32 fm10k_update_pvid(struct fm10k_hw *hw, u32 **results,
1377 struct fm10k_mbx_info __always_unused *mbx)
1379 struct fm10k_intfc *interface;
1384 err = fm10k_tlv_attr_get_u32(results[FM10K_PF_ATTR_ID_UPDATE_PVID],
1389 /* extract values from the pvid update */
1390 glort = FM10K_MSG_HDR_FIELD_GET(pvid_update, UPDATE_PVID_GLORT);
1391 pvid = FM10K_MSG_HDR_FIELD_GET(pvid_update, UPDATE_PVID_PVID);
1393 /* if glort is not valid return error */
1394 if (!fm10k_glort_valid_pf(hw, glort))
1395 return FM10K_ERR_PARAM;
1397 /* verify VLAN ID is valid */
1398 if (pvid >= FM10K_VLAN_TABLE_VID_MAX)
1399 return FM10K_ERR_PARAM;
1401 interface = container_of(hw, struct fm10k_intfc, hw);
1403 /* check to see if this belongs to one of the VFs */
1404 err = fm10k_iov_update_pvid(interface, glort, pvid);
1408 /* we need to reset if default VLAN was just updated */
1409 if (pvid != hw->mac.default_vid)
1410 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1412 hw->mac.default_vid = pvid;
1417 static const struct fm10k_msg_data pf_mbx_data[] = {
1418 FM10K_PF_MSG_ERR_HANDLER(XCAST_MODES, fm10k_msg_err_pf),
1419 FM10K_PF_MSG_ERR_HANDLER(UPDATE_MAC_FWD_RULE, fm10k_msg_err_pf),
1420 FM10K_PF_MSG_LPORT_MAP_HANDLER(fm10k_lport_map),
1421 FM10K_PF_MSG_ERR_HANDLER(LPORT_CREATE, fm10k_msg_err_pf),
1422 FM10K_PF_MSG_ERR_HANDLER(LPORT_DELETE, fm10k_msg_err_pf),
1423 FM10K_PF_MSG_UPDATE_PVID_HANDLER(fm10k_update_pvid),
1424 FM10K_TLV_MSG_ERROR_HANDLER(fm10k_mbx_error),
1427 static int fm10k_mbx_request_irq_pf(struct fm10k_intfc *interface)
1429 struct msix_entry *entry = &interface->msix_entries[FM10K_MBX_VECTOR];
1430 struct net_device *dev = interface->netdev;
1431 struct fm10k_hw *hw = &interface->hw;
1434 /* Use timer0 for interrupt moderation on the mailbox */
1435 u32 mbx_itr = entry->entry | FM10K_INT_MAP_TIMER0;
1436 u32 other_itr = entry->entry | FM10K_INT_MAP_IMMEDIATE;
1438 /* register mailbox handlers */
1439 err = hw->mbx.ops.register_handlers(&hw->mbx, pf_mbx_data);
1443 /* request the IRQ */
1444 err = request_irq(entry->vector, fm10k_msix_mbx_pf, 0,
1445 dev->name, interface);
1447 netif_err(interface, probe, dev,
1448 "request_irq for msix_mbx failed: %d\n", err);
1452 /* Enable interrupts w/ no moderation for "other" interrupts */
1453 fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_pcie_fault), other_itr);
1454 fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_switch_up_down), other_itr);
1455 fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_sram), other_itr);
1456 fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_max_hold_time), other_itr);
1457 fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_vflr), other_itr);
1459 /* Enable interrupts w/ moderation for mailbox */
1460 fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_mailbox), mbx_itr);
1462 /* Enable individual interrupt causes */
1463 fm10k_write_reg(hw, FM10K_EIMR, FM10K_EIMR_ENABLE(PCA_FAULT) |
1464 FM10K_EIMR_ENABLE(FUM_FAULT) |
1465 FM10K_EIMR_ENABLE(MAILBOX) |
1466 FM10K_EIMR_ENABLE(SWITCHREADY) |
1467 FM10K_EIMR_ENABLE(SWITCHNOTREADY) |
1468 FM10K_EIMR_ENABLE(SRAMERROR) |
1469 FM10K_EIMR_ENABLE(VFLR) |
1470 FM10K_EIMR_ENABLE(MAXHOLDTIME));
1472 /* enable interrupt */
1473 fm10k_write_reg(hw, FM10K_ITR(entry->entry), FM10K_ITR_ENABLE);
1478 int fm10k_mbx_request_irq(struct fm10k_intfc *interface)
1480 struct fm10k_hw *hw = &interface->hw;
1483 /* enable Mailbox cause */
1484 if (hw->mac.type == fm10k_mac_pf)
1485 err = fm10k_mbx_request_irq_pf(interface);
1487 err = fm10k_mbx_request_irq_vf(interface);
1491 /* connect mailbox */
1492 err = hw->mbx.ops.connect(hw, &hw->mbx);
1494 /* if the mailbox failed to connect, then free IRQ */
1496 fm10k_mbx_free_irq(interface);
1502 * fm10k_qv_free_irq - release interrupts associated with queue vectors
1503 * @interface: board private structure
1505 * Release all interrupts associated with this interface
1507 void fm10k_qv_free_irq(struct fm10k_intfc *interface)
1509 int vector = interface->num_q_vectors;
1510 struct fm10k_hw *hw = &interface->hw;
1511 struct msix_entry *entry;
1513 entry = &interface->msix_entries[NON_Q_VECTORS(hw) + vector];
1516 struct fm10k_q_vector *q_vector;
1520 q_vector = interface->q_vector[vector];
1522 if (!q_vector->tx.count && !q_vector->rx.count)
1525 /* clear the affinity_mask in the IRQ descriptor */
1526 irq_set_affinity_hint(entry->vector, NULL);
1528 /* disable interrupts */
1529 writel(FM10K_ITR_MASK_SET, q_vector->itr);
1531 free_irq(entry->vector, q_vector);
1536 * fm10k_qv_request_irq - initialize interrupts for queue vectors
1537 * @interface: board private structure
1539 * Attempts to configure interrupts using the best available
1540 * capabilities of the hardware and kernel.
1542 int fm10k_qv_request_irq(struct fm10k_intfc *interface)
1544 struct net_device *dev = interface->netdev;
1545 struct fm10k_hw *hw = &interface->hw;
1546 struct msix_entry *entry;
1550 entry = &interface->msix_entries[NON_Q_VECTORS(hw)];
1552 for (vector = 0; vector < interface->num_q_vectors; vector++) {
1553 struct fm10k_q_vector *q_vector = interface->q_vector[vector];
1555 /* name the vector */
1556 if (q_vector->tx.count && q_vector->rx.count) {
1557 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1558 "%s-TxRx-%d", dev->name, ri++);
1560 } else if (q_vector->rx.count) {
1561 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1562 "%s-rx-%d", dev->name, ri++);
1563 } else if (q_vector->tx.count) {
1564 snprintf(q_vector->name, sizeof(q_vector->name) - 1,
1565 "%s-tx-%d", dev->name, ti++);
1567 /* skip this unused q_vector */
1571 /* Assign ITR register to q_vector */
1572 q_vector->itr = (hw->mac.type == fm10k_mac_pf) ?
1573 &interface->uc_addr[FM10K_ITR(entry->entry)] :
1574 &interface->uc_addr[FM10K_VFITR(entry->entry)];
1576 /* request the IRQ */
1577 err = request_irq(entry->vector, &fm10k_msix_clean_rings, 0,
1578 q_vector->name, q_vector);
1580 netif_err(interface, probe, dev,
1581 "request_irq failed for MSIX interrupt Error: %d\n",
1586 /* assign the mask for this irq */
1587 irq_set_affinity_hint(entry->vector, &q_vector->affinity_mask);
1589 /* Enable q_vector */
1590 writel(FM10K_ITR_ENABLE, q_vector->itr);
1598 /* wind through the ring freeing all entries and vectors */
1600 struct fm10k_q_vector *q_vector;
1604 q_vector = interface->q_vector[vector];
1606 if (!q_vector->tx.count && !q_vector->rx.count)
1609 /* clear the affinity_mask in the IRQ descriptor */
1610 irq_set_affinity_hint(entry->vector, NULL);
1612 /* disable interrupts */
1613 writel(FM10K_ITR_MASK_SET, q_vector->itr);
1615 free_irq(entry->vector, q_vector);
1621 void fm10k_up(struct fm10k_intfc *interface)
1623 struct fm10k_hw *hw = &interface->hw;
1625 /* Enable Tx/Rx DMA */
1626 hw->mac.ops.start_hw(hw);
1628 /* configure Tx descriptor rings */
1629 fm10k_configure_tx(interface);
1631 /* configure Rx descriptor rings */
1632 fm10k_configure_rx(interface);
1634 /* configure interrupts */
1635 hw->mac.ops.update_int_moderator(hw);
1637 /* enable statistics capture again */
1638 clear_bit(__FM10K_UPDATING_STATS, interface->state);
1640 /* clear down bit to indicate we are ready to go */
1641 clear_bit(__FM10K_DOWN, interface->state);
1643 /* enable polling cleanups */
1644 fm10k_napi_enable_all(interface);
1646 /* re-establish Rx filters */
1647 fm10k_restore_rx_state(interface);
1649 /* enable transmits */
1650 netif_tx_start_all_queues(interface->netdev);
1652 /* kick off the service timer now */
1653 hw->mac.get_host_state = true;
1654 mod_timer(&interface->service_timer, jiffies);
1657 static void fm10k_napi_disable_all(struct fm10k_intfc *interface)
1659 struct fm10k_q_vector *q_vector;
1662 for (q_idx = 0; q_idx < interface->num_q_vectors; q_idx++) {
1663 q_vector = interface->q_vector[q_idx];
1664 napi_disable(&q_vector->napi);
1668 void fm10k_down(struct fm10k_intfc *interface)
1670 struct net_device *netdev = interface->netdev;
1671 struct fm10k_hw *hw = &interface->hw;
1672 int err, i = 0, count = 0;
1674 /* signal that we are down to the interrupt handler and service task */
1675 if (test_and_set_bit(__FM10K_DOWN, interface->state))
1678 /* call carrier off first to avoid false dev_watchdog timeouts */
1679 netif_carrier_off(netdev);
1681 /* disable transmits */
1682 netif_tx_stop_all_queues(netdev);
1683 netif_tx_disable(netdev);
1685 /* reset Rx filters */
1686 fm10k_reset_rx_state(interface);
1688 /* disable polling routines */
1689 fm10k_napi_disable_all(interface);
1691 /* capture stats one last time before stopping interface */
1692 fm10k_update_stats(interface);
1694 /* prevent updating statistics while we're down */
1695 while (test_and_set_bit(__FM10K_UPDATING_STATS, interface->state))
1696 usleep_range(1000, 2000);
1698 /* skip waiting for TX DMA if we lost PCIe link */
1699 if (FM10K_REMOVED(hw->hw_addr))
1700 goto skip_tx_dma_drain;
1702 /* In some rare circumstances it can take a while for Tx queues to
1703 * quiesce and be fully disabled. Attempt to .stop_hw() first, and
1704 * then if we get ERR_REQUESTS_PENDING, go ahead and wait in a loop
1705 * until the Tx queues have emptied, or until a number of retries. If
1706 * we fail to clear within the retry loop, we will issue a warning
1707 * indicating that Tx DMA is probably hung. Note this means we call
1708 * .stop_hw() twice but this shouldn't cause any problems.
1710 err = hw->mac.ops.stop_hw(hw);
1711 if (err != FM10K_ERR_REQUESTS_PENDING)
1712 goto skip_tx_dma_drain;
1714 #define TX_DMA_DRAIN_RETRIES 25
1715 for (count = 0; count < TX_DMA_DRAIN_RETRIES; count++) {
1716 usleep_range(10000, 20000);
1718 /* start checking at the last ring to have pending Tx */
1719 for (; i < interface->num_tx_queues; i++)
1720 if (fm10k_get_tx_pending(interface->tx_ring[i], false))
1723 /* if all the queues are drained, we can break now */
1724 if (i == interface->num_tx_queues)
1728 if (count >= TX_DMA_DRAIN_RETRIES)
1729 dev_err(&interface->pdev->dev,
1730 "Tx queues failed to drain after %d tries. Tx DMA is probably hung.\n",
1733 /* Disable DMA engine for Tx/Rx */
1734 err = hw->mac.ops.stop_hw(hw);
1735 if (err == FM10K_ERR_REQUESTS_PENDING)
1736 dev_err(&interface->pdev->dev,
1737 "due to pending requests hw was not shut down gracefully\n");
1739 dev_err(&interface->pdev->dev, "stop_hw failed: %d\n", err);
1741 /* free any buffers still on the rings */
1742 fm10k_clean_all_tx_rings(interface);
1743 fm10k_clean_all_rx_rings(interface);
1747 * fm10k_sw_init - Initialize general software structures
1748 * @interface: host interface private structure to initialize
1750 * fm10k_sw_init initializes the interface private data structure.
1751 * Fields are initialized based on PCI device information and
1752 * OS network device settings (MTU size).
1754 static int fm10k_sw_init(struct fm10k_intfc *interface,
1755 const struct pci_device_id *ent)
1757 const struct fm10k_info *fi = fm10k_info_tbl[ent->driver_data];
1758 struct fm10k_hw *hw = &interface->hw;
1759 struct pci_dev *pdev = interface->pdev;
1760 struct net_device *netdev = interface->netdev;
1761 u32 rss_key[FM10K_RSSRK_SIZE];
1765 /* initialize back pointer */
1766 hw->back = interface;
1767 hw->hw_addr = interface->uc_addr;
1769 /* PCI config space info */
1770 hw->vendor_id = pdev->vendor;
1771 hw->device_id = pdev->device;
1772 hw->revision_id = pdev->revision;
1773 hw->subsystem_vendor_id = pdev->subsystem_vendor;
1774 hw->subsystem_device_id = pdev->subsystem_device;
1777 memcpy(&hw->mac.ops, fi->mac_ops, sizeof(hw->mac.ops));
1778 hw->mac.type = fi->mac;
1780 /* Setup IOV handlers */
1782 memcpy(&hw->iov.ops, fi->iov_ops, sizeof(hw->iov.ops));
1784 /* Set common capability flags and settings */
1785 rss = min_t(int, FM10K_MAX_RSS_INDICES, num_online_cpus());
1786 interface->ring_feature[RING_F_RSS].limit = rss;
1787 fi->get_invariants(hw);
1789 /* pick up the PCIe bus settings for reporting later */
1790 if (hw->mac.ops.get_bus_info)
1791 hw->mac.ops.get_bus_info(hw);
1793 /* limit the usable DMA range */
1794 if (hw->mac.ops.set_dma_mask)
1795 hw->mac.ops.set_dma_mask(hw, dma_get_mask(&pdev->dev));
1797 /* update netdev with DMA restrictions */
1798 if (dma_get_mask(&pdev->dev) > DMA_BIT_MASK(32)) {
1799 netdev->features |= NETIF_F_HIGHDMA;
1800 netdev->vlan_features |= NETIF_F_HIGHDMA;
1803 /* delay any future reset requests */
1804 interface->last_reset = jiffies + (10 * HZ);
1806 /* reset and initialize the hardware so it is in a known state */
1807 err = hw->mac.ops.reset_hw(hw);
1809 dev_err(&pdev->dev, "reset_hw failed: %d\n", err);
1813 err = hw->mac.ops.init_hw(hw);
1815 dev_err(&pdev->dev, "init_hw failed: %d\n", err);
1819 /* initialize hardware statistics */
1820 hw->mac.ops.update_hw_stats(hw, &interface->stats);
1822 /* Set upper limit on IOV VFs that can be allocated */
1823 pci_sriov_set_totalvfs(pdev, hw->iov.total_vfs);
1825 /* Start with random Ethernet address */
1826 eth_random_addr(hw->mac.addr);
1828 /* Initialize MAC address from hardware */
1829 err = hw->mac.ops.read_mac_addr(hw);
1831 dev_warn(&pdev->dev,
1832 "Failed to obtain MAC address defaulting to random\n");
1833 /* tag address assignment as random */
1834 netdev->addr_assign_type |= NET_ADDR_RANDOM;
1837 ether_addr_copy(netdev->dev_addr, hw->mac.addr);
1838 ether_addr_copy(netdev->perm_addr, hw->mac.addr);
1840 if (!is_valid_ether_addr(netdev->perm_addr)) {
1841 dev_err(&pdev->dev, "Invalid MAC Address\n");
1845 /* initialize DCBNL interface */
1846 fm10k_dcbnl_set_ops(netdev);
1848 /* set default ring sizes */
1849 interface->tx_ring_count = FM10K_DEFAULT_TXD;
1850 interface->rx_ring_count = FM10K_DEFAULT_RXD;
1852 /* set default interrupt moderation */
1853 interface->tx_itr = FM10K_TX_ITR_DEFAULT;
1854 interface->rx_itr = FM10K_ITR_ADAPTIVE | FM10K_RX_ITR_DEFAULT;
1856 /* initialize udp port lists */
1857 INIT_LIST_HEAD(&interface->vxlan_port);
1858 INIT_LIST_HEAD(&interface->geneve_port);
1860 netdev_rss_key_fill(rss_key, sizeof(rss_key));
1861 memcpy(interface->rssrk, rss_key, sizeof(rss_key));
1863 /* Start off interface as being down */
1864 set_bit(__FM10K_DOWN, interface->state);
1865 set_bit(__FM10K_UPDATING_STATS, interface->state);
1870 static void fm10k_slot_warn(struct fm10k_intfc *interface)
1872 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
1873 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
1874 struct fm10k_hw *hw = &interface->hw;
1875 int max_gts = 0, expected_gts = 0;
1877 if (pcie_get_minimum_link(interface->pdev, &speed, &width) ||
1878 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
1879 dev_warn(&interface->pdev->dev,
1880 "Unable to determine PCI Express bandwidth.\n");
1885 case PCIE_SPEED_2_5GT:
1886 /* 8b/10b encoding reduces max throughput by 20% */
1887 max_gts = 2 * width;
1889 case PCIE_SPEED_5_0GT:
1890 /* 8b/10b encoding reduces max throughput by 20% */
1891 max_gts = 4 * width;
1893 case PCIE_SPEED_8_0GT:
1894 /* 128b/130b encoding has less than 2% impact on throughput */
1895 max_gts = 8 * width;
1898 dev_warn(&interface->pdev->dev,
1899 "Unable to determine PCI Express bandwidth.\n");
1903 dev_info(&interface->pdev->dev,
1904 "PCI Express bandwidth of %dGT/s available\n",
1906 dev_info(&interface->pdev->dev,
1907 "(Speed:%s, Width: x%d, Encoding Loss:%s, Payload:%s)\n",
1908 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
1909 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
1910 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
1913 (speed == PCIE_SPEED_2_5GT ? "20%" :
1914 speed == PCIE_SPEED_5_0GT ? "20%" :
1915 speed == PCIE_SPEED_8_0GT ? "<2%" :
1917 (hw->bus.payload == fm10k_bus_payload_128 ? "128B" :
1918 hw->bus.payload == fm10k_bus_payload_256 ? "256B" :
1919 hw->bus.payload == fm10k_bus_payload_512 ? "512B" :
1922 switch (hw->bus_caps.speed) {
1923 case fm10k_bus_speed_2500:
1924 /* 8b/10b encoding reduces max throughput by 20% */
1925 expected_gts = 2 * hw->bus_caps.width;
1927 case fm10k_bus_speed_5000:
1928 /* 8b/10b encoding reduces max throughput by 20% */
1929 expected_gts = 4 * hw->bus_caps.width;
1931 case fm10k_bus_speed_8000:
1932 /* 128b/130b encoding has less than 2% impact on throughput */
1933 expected_gts = 8 * hw->bus_caps.width;
1936 dev_warn(&interface->pdev->dev,
1937 "Unable to determine expected PCI Express bandwidth.\n");
1941 if (max_gts >= expected_gts)
1944 dev_warn(&interface->pdev->dev,
1945 "This device requires %dGT/s of bandwidth for optimal performance.\n",
1947 dev_warn(&interface->pdev->dev,
1948 "A %sslot with x%d lanes is suggested.\n",
1949 (hw->bus_caps.speed == fm10k_bus_speed_2500 ? "2.5GT/s " :
1950 hw->bus_caps.speed == fm10k_bus_speed_5000 ? "5.0GT/s " :
1951 hw->bus_caps.speed == fm10k_bus_speed_8000 ? "8.0GT/s " : ""),
1952 hw->bus_caps.width);
1956 * fm10k_probe - Device Initialization Routine
1957 * @pdev: PCI device information struct
1958 * @ent: entry in fm10k_pci_tbl
1960 * Returns 0 on success, negative on failure
1962 * fm10k_probe initializes an interface identified by a pci_dev structure.
1963 * The OS initialization, configuring of the interface private structure,
1964 * and a hardware reset occur.
1966 static int fm10k_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1968 struct net_device *netdev;
1969 struct fm10k_intfc *interface;
1972 if (pdev->error_state != pci_channel_io_normal) {
1974 "PCI device still in an error state. Unable to load...\n");
1978 err = pci_enable_device_mem(pdev);
1981 "PCI enable device failed: %d\n", err);
1985 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));
1987 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1990 "DMA configuration failed: %d\n", err);
1994 err = pci_request_mem_regions(pdev, fm10k_driver_name);
1997 "pci_request_selected_regions failed: %d\n", err);
2001 pci_enable_pcie_error_reporting(pdev);
2003 pci_set_master(pdev);
2004 pci_save_state(pdev);
2006 netdev = fm10k_alloc_netdev(fm10k_info_tbl[ent->driver_data]);
2009 goto err_alloc_netdev;
2012 SET_NETDEV_DEV(netdev, &pdev->dev);
2014 interface = netdev_priv(netdev);
2015 pci_set_drvdata(pdev, interface);
2017 interface->netdev = netdev;
2018 interface->pdev = pdev;
2020 interface->uc_addr = ioremap(pci_resource_start(pdev, 0),
2021 FM10K_UC_ADDR_SIZE);
2022 if (!interface->uc_addr) {
2027 err = fm10k_sw_init(interface, ent);
2031 /* enable debugfs support */
2032 fm10k_dbg_intfc_init(interface);
2034 err = fm10k_init_queueing_scheme(interface);
2038 /* the mbx interrupt might attempt to schedule the service task, so we
2039 * must ensure it is disabled since we haven't yet requested the timer
2042 set_bit(__FM10K_SERVICE_DISABLE, interface->state);
2044 err = fm10k_mbx_request_irq(interface);
2046 goto err_mbx_interrupt;
2048 /* final check of hardware state before registering the interface */
2049 err = fm10k_hw_ready(interface);
2053 err = register_netdev(netdev);
2057 /* carrier off reporting is important to ethtool even BEFORE open */
2058 netif_carrier_off(netdev);
2060 /* stop all the transmit queues from transmitting until link is up */
2061 netif_tx_stop_all_queues(netdev);
2063 /* Initialize service timer and service task late in order to avoid
2066 setup_timer(&interface->service_timer, &fm10k_service_timer,
2067 (unsigned long)interface);
2068 INIT_WORK(&interface->service_task, fm10k_service_task);
2070 /* kick off service timer now, even when interface is down */
2071 mod_timer(&interface->service_timer, (HZ * 2) + jiffies);
2073 /* print warning for non-optimal configurations */
2074 fm10k_slot_warn(interface);
2076 /* report MAC address for logging */
2077 dev_info(&pdev->dev, "%pM\n", netdev->dev_addr);
2079 /* enable SR-IOV after registering netdev to enforce PF/VF ordering */
2080 fm10k_iov_configure(pdev, 0);
2082 /* clear the service task disable bit to allow service task to start */
2083 clear_bit(__FM10K_SERVICE_DISABLE, interface->state);
2088 fm10k_mbx_free_irq(interface);
2090 fm10k_clear_queueing_scheme(interface);
2092 if (interface->sw_addr)
2093 iounmap(interface->sw_addr);
2094 iounmap(interface->uc_addr);
2096 free_netdev(netdev);
2098 pci_disable_pcie_error_reporting(pdev);
2099 pci_release_mem_regions(pdev);
2102 pci_disable_device(pdev);
2107 * fm10k_remove - Device Removal Routine
2108 * @pdev: PCI device information struct
2110 * fm10k_remove is called by the PCI subsystem to alert the driver
2111 * that it should release a PCI device. The could be caused by a
2112 * Hot-Plug event, or because the driver is going to be removed from
2115 static void fm10k_remove(struct pci_dev *pdev)
2117 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2118 struct net_device *netdev = interface->netdev;
2120 del_timer_sync(&interface->service_timer);
2122 set_bit(__FM10K_SERVICE_DISABLE, interface->state);
2123 cancel_work_sync(&interface->service_task);
2125 /* free netdev, this may bounce the interrupts due to setup_tc */
2126 if (netdev->reg_state == NETREG_REGISTERED)
2127 unregister_netdev(netdev);
2130 fm10k_iov_disable(pdev);
2132 /* disable mailbox interrupt */
2133 fm10k_mbx_free_irq(interface);
2135 /* free interrupts */
2136 fm10k_clear_queueing_scheme(interface);
2138 /* remove any debugfs interfaces */
2139 fm10k_dbg_intfc_exit(interface);
2141 if (interface->sw_addr)
2142 iounmap(interface->sw_addr);
2143 iounmap(interface->uc_addr);
2145 free_netdev(netdev);
2147 pci_release_mem_regions(pdev);
2149 pci_disable_pcie_error_reporting(pdev);
2151 pci_disable_device(pdev);
2154 static void fm10k_prepare_suspend(struct fm10k_intfc *interface)
2156 /* the watchdog task reads from registers, which might appear like
2157 * a surprise remove if the PCIe device is disabled while we're
2158 * stopped. We stop the watchdog task until after we resume software
2161 set_bit(__FM10K_SERVICE_DISABLE, interface->state);
2162 cancel_work_sync(&interface->service_task);
2164 fm10k_prepare_for_reset(interface);
2167 static int fm10k_handle_resume(struct fm10k_intfc *interface)
2169 struct fm10k_hw *hw = &interface->hw;
2172 /* reset statistics starting values */
2173 hw->mac.ops.rebind_hw_stats(hw, &interface->stats);
2175 err = fm10k_handle_reset(interface);
2179 /* assume host is not ready, to prevent race with watchdog in case we
2180 * actually don't have connection to the switch
2182 interface->host_ready = false;
2183 fm10k_watchdog_host_not_ready(interface);
2185 /* force link to stay down for a second to prevent link flutter */
2186 interface->link_down_event = jiffies + (HZ);
2187 set_bit(__FM10K_LINK_DOWN, interface->state);
2189 /* clear the service task disable bit to allow service task to start */
2190 clear_bit(__FM10K_SERVICE_DISABLE, interface->state);
2191 fm10k_service_event_schedule(interface);
2198 * fm10k_resume - Restore device to pre-sleep state
2199 * @pdev: PCI device information struct
2201 * fm10k_resume is called after the system has powered back up from a sleep
2202 * state and is ready to resume operation. This function is meant to restore
2203 * the device back to its pre-sleep state.
2205 static int fm10k_resume(struct pci_dev *pdev)
2207 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2208 struct net_device *netdev = interface->netdev;
2209 struct fm10k_hw *hw = &interface->hw;
2212 pci_set_power_state(pdev, PCI_D0);
2213 pci_restore_state(pdev);
2215 /* pci_restore_state clears dev->state_saved so call
2216 * pci_save_state to restore it.
2218 pci_save_state(pdev);
2220 err = pci_enable_device_mem(pdev);
2222 dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
2225 pci_set_master(pdev);
2227 pci_wake_from_d3(pdev, false);
2229 /* refresh hw_addr in case it was dropped */
2230 hw->hw_addr = interface->uc_addr;
2232 err = fm10k_handle_resume(interface);
2236 netif_device_attach(netdev);
2242 * fm10k_suspend - Prepare the device for a system sleep state
2243 * @pdev: PCI device information struct
2245 * fm10k_suspend is meant to shutdown the device prior to the system entering
2246 * a sleep state. The fm10k hardware does not support wake on lan so the
2247 * driver simply needs to shut down the device so it is in a low power state.
2249 static int fm10k_suspend(struct pci_dev *pdev,
2250 pm_message_t __always_unused state)
2252 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2253 struct net_device *netdev = interface->netdev;
2256 netif_device_detach(netdev);
2258 fm10k_prepare_suspend(interface);
2260 err = pci_save_state(pdev);
2264 pci_disable_device(pdev);
2265 pci_wake_from_d3(pdev, false);
2266 pci_set_power_state(pdev, PCI_D3hot);
2271 #endif /* CONFIG_PM */
2273 * fm10k_io_error_detected - called when PCI error is detected
2274 * @pdev: Pointer to PCI device
2275 * @state: The current pci connection state
2277 * This function is called after a PCI bus error affecting
2278 * this device has been detected.
2280 static pci_ers_result_t fm10k_io_error_detected(struct pci_dev *pdev,
2281 pci_channel_state_t state)
2283 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2284 struct net_device *netdev = interface->netdev;
2286 netif_device_detach(netdev);
2288 if (state == pci_channel_io_perm_failure)
2289 return PCI_ERS_RESULT_DISCONNECT;
2291 fm10k_prepare_suspend(interface);
2293 /* Request a slot reset. */
2294 return PCI_ERS_RESULT_NEED_RESET;
2298 * fm10k_io_slot_reset - called after the pci bus has been reset.
2299 * @pdev: Pointer to PCI device
2301 * Restart the card from scratch, as if from a cold-boot.
2303 static pci_ers_result_t fm10k_io_slot_reset(struct pci_dev *pdev)
2305 pci_ers_result_t result;
2307 if (pci_reenable_device(pdev)) {
2309 "Cannot re-enable PCI device after reset.\n");
2310 result = PCI_ERS_RESULT_DISCONNECT;
2312 pci_set_master(pdev);
2313 pci_restore_state(pdev);
2315 /* After second error pci->state_saved is false, this
2316 * resets it so EEH doesn't break.
2318 pci_save_state(pdev);
2320 pci_wake_from_d3(pdev, false);
2322 result = PCI_ERS_RESULT_RECOVERED;
2325 pci_cleanup_aer_uncorrect_error_status(pdev);
2331 * fm10k_io_resume - called when traffic can start flowing again.
2332 * @pdev: Pointer to PCI device
2334 * This callback is called when the error recovery driver tells us that
2335 * its OK to resume normal operation.
2337 static void fm10k_io_resume(struct pci_dev *pdev)
2339 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2340 struct net_device *netdev = interface->netdev;
2343 err = fm10k_handle_resume(interface);
2346 dev_warn(&pdev->dev,
2347 "fm10k_io_resume failed: %d\n", err);
2349 netif_device_attach(netdev);
2352 static void fm10k_io_reset_prepare(struct pci_dev *pdev)
2354 /* warn incase we have any active VF devices */
2355 if (pci_num_vf(pdev))
2356 dev_warn(&pdev->dev,
2357 "PCIe FLR may cause issues for any active VF devices\n");
2358 fm10k_prepare_suspend(pci_get_drvdata(pdev));
2361 static void fm10k_io_reset_done(struct pci_dev *pdev)
2363 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2364 int err = fm10k_handle_resume(interface);
2367 dev_warn(&pdev->dev,
2368 "fm10k_io_reset_notify failed: %d\n", err);
2369 netif_device_detach(interface->netdev);
2373 static const struct pci_error_handlers fm10k_err_handler = {
2374 .error_detected = fm10k_io_error_detected,
2375 .slot_reset = fm10k_io_slot_reset,
2376 .resume = fm10k_io_resume,
2377 .reset_prepare = fm10k_io_reset_prepare,
2378 .reset_done = fm10k_io_reset_done,
2381 static struct pci_driver fm10k_driver = {
2382 .name = fm10k_driver_name,
2383 .id_table = fm10k_pci_tbl,
2384 .probe = fm10k_probe,
2385 .remove = fm10k_remove,
2387 .suspend = fm10k_suspend,
2388 .resume = fm10k_resume,
2390 .sriov_configure = fm10k_iov_configure,
2391 .err_handler = &fm10k_err_handler
2395 * fm10k_register_pci_driver - register driver interface
2397 * This function is called on module load in order to register the driver.
2399 int fm10k_register_pci_driver(void)
2401 return pci_register_driver(&fm10k_driver);
2405 * fm10k_unregister_pci_driver - unregister driver interface
2407 * This function is called on module unload in order to remove the driver.
2409 void fm10k_unregister_pci_driver(void)
2411 pci_unregister_driver(&fm10k_driver);