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:
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;
95 * fm10k_macvlan_schedule - Schedule MAC/VLAN queue task
96 * @interface: fm10k private interface structure
98 * Schedule the MAC/VLAN queue monitor task. If the MAC/VLAN task cannot be
99 * started immediately, request that it be restarted when possible.
101 void fm10k_macvlan_schedule(struct fm10k_intfc *interface)
103 /* Avoid processing the MAC/VLAN queue when the service task is
104 * disabled, or when we're resetting the device.
106 if (!test_bit(__FM10K_MACVLAN_DISABLE, interface->state) &&
107 !test_and_set_bit(__FM10K_MACVLAN_SCHED, interface->state)) {
108 clear_bit(__FM10K_MACVLAN_REQUEST, interface->state);
109 /* We delay the actual start of execution in order to allow
110 * multiple MAC/VLAN updates to accumulate before handling
111 * them, and to allow some time to let the mailbox drain
114 queue_delayed_work(fm10k_workqueue,
115 &interface->macvlan_task, 10);
117 set_bit(__FM10K_MACVLAN_REQUEST, interface->state);
122 * fm10k_stop_macvlan_task - Stop the MAC/VLAN queue monitor
123 * @interface: fm10k private interface structure
125 * Wait until the MAC/VLAN queue task has stopped, and cancel any future
128 static void fm10k_stop_macvlan_task(struct fm10k_intfc *interface)
130 /* Disable the MAC/VLAN work item */
131 set_bit(__FM10K_MACVLAN_DISABLE, interface->state);
133 /* Make sure we waited until any current invocations have stopped */
134 cancel_delayed_work_sync(&interface->macvlan_task);
136 /* We set the __FM10K_MACVLAN_SCHED bit when we schedule the task.
137 * However, it may not be unset of the MAC/VLAN task never actually
138 * got a chance to run. Since we've canceled the task here, and it
139 * cannot be rescheuled right now, we need to ensure the scheduled bit
142 clear_bit(__FM10K_MACVLAN_SCHED, interface->state);
146 * fm10k_resume_macvlan_task - Restart the MAC/VLAN queue monitor
147 * @interface: fm10k private interface structure
149 * Clear the __FM10K_MACVLAN_DISABLE bit and, if a request occurred, schedule
150 * the MAC/VLAN work monitor.
152 static void fm10k_resume_macvlan_task(struct fm10k_intfc *interface)
154 /* Re-enable the MAC/VLAN work item */
155 clear_bit(__FM10K_MACVLAN_DISABLE, interface->state);
157 /* We might have received a MAC/VLAN request while disabled. If so,
158 * kick off the queue now.
160 if (test_bit(__FM10K_MACVLAN_REQUEST, interface->state))
161 fm10k_macvlan_schedule(interface);
164 void fm10k_service_event_schedule(struct fm10k_intfc *interface)
166 if (!test_bit(__FM10K_SERVICE_DISABLE, interface->state) &&
167 !test_and_set_bit(__FM10K_SERVICE_SCHED, interface->state)) {
168 clear_bit(__FM10K_SERVICE_REQUEST, interface->state);
169 queue_work(fm10k_workqueue, &interface->service_task);
171 set_bit(__FM10K_SERVICE_REQUEST, interface->state);
175 static void fm10k_service_event_complete(struct fm10k_intfc *interface)
177 WARN_ON(!test_bit(__FM10K_SERVICE_SCHED, interface->state));
179 /* flush memory to make sure state is correct before next watchog */
180 smp_mb__before_atomic();
181 clear_bit(__FM10K_SERVICE_SCHED, interface->state);
183 /* If a service event was requested since we started, immediately
184 * re-schedule now. This ensures we don't drop a request until the
187 if (test_bit(__FM10K_SERVICE_REQUEST, interface->state))
188 fm10k_service_event_schedule(interface);
191 static void fm10k_stop_service_event(struct fm10k_intfc *interface)
193 set_bit(__FM10K_SERVICE_DISABLE, interface->state);
194 cancel_work_sync(&interface->service_task);
196 /* It's possible that cancel_work_sync stopped the service task from
197 * running before it could actually start. In this case the
198 * __FM10K_SERVICE_SCHED bit will never be cleared. Since we know that
199 * the service task cannot be running at this point, we need to clear
200 * the scheduled bit, as otherwise the service task may never be
203 clear_bit(__FM10K_SERVICE_SCHED, interface->state);
206 static void fm10k_start_service_event(struct fm10k_intfc *interface)
208 clear_bit(__FM10K_SERVICE_DISABLE, interface->state);
209 fm10k_service_event_schedule(interface);
213 * fm10k_service_timer - Timer Call-back
214 * @data: pointer to interface cast into an unsigned long
216 static void fm10k_service_timer(struct timer_list *t)
218 struct fm10k_intfc *interface = from_timer(interface, t,
221 /* Reset the timer */
222 mod_timer(&interface->service_timer, (HZ * 2) + jiffies);
224 fm10k_service_event_schedule(interface);
228 * fm10k_prepare_for_reset - Prepare the driver and device for a pending reset
229 * @interface: fm10k private data structure
231 * This function prepares for a device reset by shutting as much down as we
232 * can. It does nothing and returns false if __FM10K_RESETTING was already set
233 * prior to calling this function. It returns true if it actually did work.
235 static bool fm10k_prepare_for_reset(struct fm10k_intfc *interface)
237 struct net_device *netdev = interface->netdev;
239 WARN_ON(in_interrupt());
241 /* put off any impending NetWatchDogTimeout */
242 netif_trans_update(netdev);
244 /* Nothing to do if a reset is already in progress */
245 if (test_and_set_bit(__FM10K_RESETTING, interface->state))
248 /* As the MAC/VLAN task will be accessing registers it must not be
249 * running while we reset. Although the task will not be scheduled
250 * once we start resetting it may already be running
252 fm10k_stop_macvlan_task(interface);
256 fm10k_iov_suspend(interface->pdev);
258 if (netif_running(netdev))
261 fm10k_mbx_free_irq(interface);
263 /* free interrupts */
264 fm10k_clear_queueing_scheme(interface);
266 /* delay any future reset requests */
267 interface->last_reset = jiffies + (10 * HZ);
274 static int fm10k_handle_reset(struct fm10k_intfc *interface)
276 struct net_device *netdev = interface->netdev;
277 struct fm10k_hw *hw = &interface->hw;
280 WARN_ON(!test_bit(__FM10K_RESETTING, interface->state));
284 pci_set_master(interface->pdev);
286 /* reset and initialize the hardware so it is in a known state */
287 err = hw->mac.ops.reset_hw(hw);
289 dev_err(&interface->pdev->dev, "reset_hw failed: %d\n", err);
293 err = hw->mac.ops.init_hw(hw);
295 dev_err(&interface->pdev->dev, "init_hw failed: %d\n", err);
299 err = fm10k_init_queueing_scheme(interface);
301 dev_err(&interface->pdev->dev,
302 "init_queueing_scheme failed: %d\n", err);
306 /* re-associate interrupts */
307 err = fm10k_mbx_request_irq(interface);
311 err = fm10k_hw_ready(interface);
315 /* update hardware address for VFs if perm_addr has changed */
316 if (hw->mac.type == fm10k_mac_vf) {
317 if (is_valid_ether_addr(hw->mac.perm_addr)) {
318 ether_addr_copy(hw->mac.addr, hw->mac.perm_addr);
319 ether_addr_copy(netdev->perm_addr, hw->mac.perm_addr);
320 ether_addr_copy(netdev->dev_addr, hw->mac.perm_addr);
321 netdev->addr_assign_type &= ~NET_ADDR_RANDOM;
324 if (hw->mac.vlan_override)
325 netdev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
327 netdev->features |= NETIF_F_HW_VLAN_CTAG_RX;
330 err = netif_running(netdev) ? fm10k_open(netdev) : 0;
334 fm10k_iov_resume(interface->pdev);
338 fm10k_resume_macvlan_task(interface);
340 clear_bit(__FM10K_RESETTING, interface->state);
344 fm10k_mbx_free_irq(interface);
346 fm10k_clear_queueing_scheme(interface);
348 netif_device_detach(netdev);
352 clear_bit(__FM10K_RESETTING, interface->state);
357 static void fm10k_detach_subtask(struct fm10k_intfc *interface)
359 struct net_device *netdev = interface->netdev;
360 u32 __iomem *hw_addr;
364 /* do nothing if netdev is still present or hw_addr is set */
365 if (netif_device_present(netdev) || interface->hw.hw_addr)
368 /* We've lost the PCIe register space, and can no longer access the
369 * device. Shut everything except the detach subtask down and prepare
370 * to reset the device in case we recover. If we actually prepare for
371 * reset, indicate that we're detached.
373 if (fm10k_prepare_for_reset(interface))
374 set_bit(__FM10K_RESET_DETACHED, interface->state);
376 /* check the real address space to see if we've recovered */
377 hw_addr = READ_ONCE(interface->uc_addr);
378 value = readl(hw_addr);
380 /* Make sure the reset was initiated because we detached,
381 * otherwise we might race with a different reset flow.
383 if (!test_and_clear_bit(__FM10K_RESET_DETACHED,
387 /* Restore the hardware address */
388 interface->hw.hw_addr = interface->uc_addr;
390 /* PCIe link has been restored, and the device is active
391 * again. Restore everything and reset the device.
393 err = fm10k_handle_reset(interface);
395 netdev_err(netdev, "Unable to reset device: %d\n", err);
396 interface->hw.hw_addr = NULL;
400 /* Re-attach the netdev */
401 netif_device_attach(netdev);
402 netdev_warn(netdev, "PCIe link restored, device now attached\n");
407 static void fm10k_reset_subtask(struct fm10k_intfc *interface)
411 if (!test_and_clear_bit(FM10K_FLAG_RESET_REQUESTED,
415 /* If another thread has already prepared to reset the device, we
416 * should not attempt to handle a reset here, since we'd race with
417 * that thread. This may happen if we suspend the device or if the
418 * PCIe link is lost. In this case, we'll just ignore the RESET
419 * request, as it will (eventually) be taken care of when the thread
420 * which actually started the reset is finished.
422 if (!fm10k_prepare_for_reset(interface))
425 netdev_err(interface->netdev, "Reset interface\n");
427 err = fm10k_handle_reset(interface);
429 dev_err(&interface->pdev->dev,
430 "fm10k_handle_reset failed: %d\n", err);
434 * fm10k_configure_swpri_map - Configure Receive SWPRI to PC mapping
435 * @interface: board private structure
437 * Configure the SWPRI to PC mapping for the port.
439 static void fm10k_configure_swpri_map(struct fm10k_intfc *interface)
441 struct net_device *netdev = interface->netdev;
442 struct fm10k_hw *hw = &interface->hw;
445 /* clear flag indicating update is needed */
446 clear_bit(FM10K_FLAG_SWPRI_CONFIG, interface->flags);
448 /* these registers are only available on the PF */
449 if (hw->mac.type != fm10k_mac_pf)
452 /* configure SWPRI to PC map */
453 for (i = 0; i < FM10K_SWPRI_MAX; i++)
454 fm10k_write_reg(hw, FM10K_SWPRI_MAP(i),
455 netdev_get_prio_tc_map(netdev, i));
459 * fm10k_watchdog_update_host_state - Update the link status based on host.
460 * @interface: board private structure
462 static void fm10k_watchdog_update_host_state(struct fm10k_intfc *interface)
464 struct fm10k_hw *hw = &interface->hw;
467 if (test_bit(__FM10K_LINK_DOWN, interface->state)) {
468 interface->host_ready = false;
469 if (time_is_after_jiffies(interface->link_down_event))
471 clear_bit(__FM10K_LINK_DOWN, interface->state);
474 if (test_bit(FM10K_FLAG_SWPRI_CONFIG, interface->flags)) {
475 if (rtnl_trylock()) {
476 fm10k_configure_swpri_map(interface);
481 /* lock the mailbox for transmit and receive */
482 fm10k_mbx_lock(interface);
484 err = hw->mac.ops.get_host_state(hw, &interface->host_ready);
485 if (err && time_is_before_jiffies(interface->last_reset))
486 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
489 fm10k_mbx_unlock(interface);
493 * fm10k_mbx_subtask - Process upstream and downstream mailboxes
494 * @interface: board private structure
496 * This function will process both the upstream and downstream mailboxes.
498 static void fm10k_mbx_subtask(struct fm10k_intfc *interface)
500 /* If we're resetting, bail out */
501 if (test_bit(__FM10K_RESETTING, interface->state))
504 /* process upstream mailbox and update device state */
505 fm10k_watchdog_update_host_state(interface);
507 /* process downstream mailboxes */
508 fm10k_iov_mbx(interface);
512 * fm10k_watchdog_host_is_ready - Update netdev status based on host ready
513 * @interface: board private structure
515 static void fm10k_watchdog_host_is_ready(struct fm10k_intfc *interface)
517 struct net_device *netdev = interface->netdev;
519 /* only continue if link state is currently down */
520 if (netif_carrier_ok(netdev))
523 netif_info(interface, drv, netdev, "NIC Link is up\n");
525 netif_carrier_on(netdev);
526 netif_tx_wake_all_queues(netdev);
530 * fm10k_watchdog_host_not_ready - Update netdev status based on host not ready
531 * @interface: board private structure
533 static void fm10k_watchdog_host_not_ready(struct fm10k_intfc *interface)
535 struct net_device *netdev = interface->netdev;
537 /* only continue if link state is currently up */
538 if (!netif_carrier_ok(netdev))
541 netif_info(interface, drv, netdev, "NIC Link is down\n");
543 netif_carrier_off(netdev);
544 netif_tx_stop_all_queues(netdev);
548 * fm10k_update_stats - Update the board statistics counters.
549 * @interface: board private structure
551 void fm10k_update_stats(struct fm10k_intfc *interface)
553 struct net_device_stats *net_stats = &interface->netdev->stats;
554 struct fm10k_hw *hw = &interface->hw;
555 u64 hw_csum_tx_good = 0, hw_csum_rx_good = 0, rx_length_errors = 0;
556 u64 rx_switch_errors = 0, rx_drops = 0, rx_pp_errors = 0;
557 u64 rx_link_errors = 0;
558 u64 rx_errors = 0, rx_csum_errors = 0, tx_csum_errors = 0;
559 u64 restart_queue = 0, tx_busy = 0, alloc_failed = 0;
560 u64 rx_bytes_nic = 0, rx_pkts_nic = 0, rx_drops_nic = 0;
561 u64 tx_bytes_nic = 0, tx_pkts_nic = 0;
565 /* ensure only one thread updates stats at a time */
566 if (test_and_set_bit(__FM10K_UPDATING_STATS, interface->state))
569 /* do not allow stats update via service task for next second */
570 interface->next_stats_update = jiffies + HZ;
572 /* gather some stats to the interface struct that are per queue */
573 for (bytes = 0, pkts = 0, i = 0; i < interface->num_tx_queues; i++) {
574 struct fm10k_ring *tx_ring = READ_ONCE(interface->tx_ring[i]);
579 restart_queue += tx_ring->tx_stats.restart_queue;
580 tx_busy += tx_ring->tx_stats.tx_busy;
581 tx_csum_errors += tx_ring->tx_stats.csum_err;
582 bytes += tx_ring->stats.bytes;
583 pkts += tx_ring->stats.packets;
584 hw_csum_tx_good += tx_ring->tx_stats.csum_good;
587 interface->restart_queue = restart_queue;
588 interface->tx_busy = tx_busy;
589 net_stats->tx_bytes = bytes;
590 net_stats->tx_packets = pkts;
591 interface->tx_csum_errors = tx_csum_errors;
592 interface->hw_csum_tx_good = hw_csum_tx_good;
594 /* gather some stats to the interface struct that are per queue */
595 for (bytes = 0, pkts = 0, i = 0; i < interface->num_rx_queues; i++) {
596 struct fm10k_ring *rx_ring = READ_ONCE(interface->rx_ring[i]);
601 bytes += rx_ring->stats.bytes;
602 pkts += rx_ring->stats.packets;
603 alloc_failed += rx_ring->rx_stats.alloc_failed;
604 rx_csum_errors += rx_ring->rx_stats.csum_err;
605 rx_errors += rx_ring->rx_stats.errors;
606 hw_csum_rx_good += rx_ring->rx_stats.csum_good;
607 rx_switch_errors += rx_ring->rx_stats.switch_errors;
608 rx_drops += rx_ring->rx_stats.drops;
609 rx_pp_errors += rx_ring->rx_stats.pp_errors;
610 rx_link_errors += rx_ring->rx_stats.link_errors;
611 rx_length_errors += rx_ring->rx_stats.length_errors;
614 net_stats->rx_bytes = bytes;
615 net_stats->rx_packets = pkts;
616 interface->alloc_failed = alloc_failed;
617 interface->rx_csum_errors = rx_csum_errors;
618 interface->hw_csum_rx_good = hw_csum_rx_good;
619 interface->rx_switch_errors = rx_switch_errors;
620 interface->rx_drops = rx_drops;
621 interface->rx_pp_errors = rx_pp_errors;
622 interface->rx_link_errors = rx_link_errors;
623 interface->rx_length_errors = rx_length_errors;
625 hw->mac.ops.update_hw_stats(hw, &interface->stats);
627 for (i = 0; i < hw->mac.max_queues; i++) {
628 struct fm10k_hw_stats_q *q = &interface->stats.q[i];
630 tx_bytes_nic += q->tx_bytes.count;
631 tx_pkts_nic += q->tx_packets.count;
632 rx_bytes_nic += q->rx_bytes.count;
633 rx_pkts_nic += q->rx_packets.count;
634 rx_drops_nic += q->rx_drops.count;
637 interface->tx_bytes_nic = tx_bytes_nic;
638 interface->tx_packets_nic = tx_pkts_nic;
639 interface->rx_bytes_nic = rx_bytes_nic;
640 interface->rx_packets_nic = rx_pkts_nic;
641 interface->rx_drops_nic = rx_drops_nic;
643 /* Fill out the OS statistics structure */
644 net_stats->rx_errors = rx_errors;
645 net_stats->rx_dropped = interface->stats.nodesc_drop.count;
647 clear_bit(__FM10K_UPDATING_STATS, interface->state);
651 * fm10k_watchdog_flush_tx - flush queues on host not ready
652 * @interface - pointer to the device interface structure
654 static void fm10k_watchdog_flush_tx(struct fm10k_intfc *interface)
656 int some_tx_pending = 0;
659 /* nothing to do if carrier is up */
660 if (netif_carrier_ok(interface->netdev))
663 for (i = 0; i < interface->num_tx_queues; i++) {
664 struct fm10k_ring *tx_ring = interface->tx_ring[i];
666 if (tx_ring->next_to_use != tx_ring->next_to_clean) {
672 /* We've lost link, so the controller stops DMA, but we've got
673 * queued Tx work that's never going to get done, so reset
674 * controller to flush Tx.
677 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
681 * fm10k_watchdog_subtask - check and bring link up
682 * @interface - pointer to the device interface structure
684 static void fm10k_watchdog_subtask(struct fm10k_intfc *interface)
686 /* if interface is down do nothing */
687 if (test_bit(__FM10K_DOWN, interface->state) ||
688 test_bit(__FM10K_RESETTING, interface->state))
691 if (interface->host_ready)
692 fm10k_watchdog_host_is_ready(interface);
694 fm10k_watchdog_host_not_ready(interface);
696 /* update stats only once every second */
697 if (time_is_before_jiffies(interface->next_stats_update))
698 fm10k_update_stats(interface);
700 /* flush any uncompleted work */
701 fm10k_watchdog_flush_tx(interface);
705 * fm10k_check_hang_subtask - check for hung queues and dropped interrupts
706 * @interface - pointer to the device interface structure
708 * This function serves two purposes. First it strobes the interrupt lines
709 * in order to make certain interrupts are occurring. Secondly it sets the
710 * bits needed to check for TX hangs. As a result we should immediately
711 * determine if a hang has occurred.
713 static void fm10k_check_hang_subtask(struct fm10k_intfc *interface)
717 /* If we're down or resetting, just bail */
718 if (test_bit(__FM10K_DOWN, interface->state) ||
719 test_bit(__FM10K_RESETTING, interface->state))
722 /* rate limit tx hang checks to only once every 2 seconds */
723 if (time_is_after_eq_jiffies(interface->next_tx_hang_check))
725 interface->next_tx_hang_check = jiffies + (2 * HZ);
727 if (netif_carrier_ok(interface->netdev)) {
728 /* Force detection of hung controller */
729 for (i = 0; i < interface->num_tx_queues; i++)
730 set_check_for_tx_hang(interface->tx_ring[i]);
732 /* Rearm all in-use q_vectors for immediate firing */
733 for (i = 0; i < interface->num_q_vectors; i++) {
734 struct fm10k_q_vector *qv = interface->q_vector[i];
736 if (!qv->tx.count && !qv->rx.count)
738 writel(FM10K_ITR_ENABLE | FM10K_ITR_PENDING2, qv->itr);
744 * fm10k_service_task - manages and runs subtasks
745 * @work: pointer to work_struct containing our data
747 static void fm10k_service_task(struct work_struct *work)
749 struct fm10k_intfc *interface;
751 interface = container_of(work, struct fm10k_intfc, service_task);
753 /* Check whether we're detached first */
754 fm10k_detach_subtask(interface);
756 /* tasks run even when interface is down */
757 fm10k_mbx_subtask(interface);
758 fm10k_reset_subtask(interface);
760 /* tasks only run when interface is up */
761 fm10k_watchdog_subtask(interface);
762 fm10k_check_hang_subtask(interface);
764 /* release lock on service events to allow scheduling next event */
765 fm10k_service_event_complete(interface);
769 * fm10k_macvlan_task - send queued MAC/VLAN requests to switch manager
770 * @work: pointer to work_struct containing our data
772 * This work item handles sending MAC/VLAN updates to the switch manager. When
773 * the interface is up, it will attempt to queue mailbox messages to the
774 * switch manager requesting updates for MAC/VLAN pairs. If the Tx fifo of the
775 * mailbox is full, it will reschedule itself to try again in a short while.
776 * This ensures that the driver does not overload the switch mailbox with too
777 * many simultaneous requests, causing an unnecessary reset.
779 static void fm10k_macvlan_task(struct work_struct *work)
781 struct fm10k_macvlan_request *item;
782 struct fm10k_intfc *interface;
783 struct delayed_work *dwork;
784 struct list_head *requests;
788 dwork = to_delayed_work(work);
789 interface = container_of(dwork, struct fm10k_intfc, macvlan_task);
791 requests = &interface->macvlan_requests;
794 /* Pop the first item off the list */
795 spin_lock_irqsave(&interface->macvlan_lock, flags);
796 item = list_first_entry_or_null(requests,
797 struct fm10k_macvlan_request,
800 list_del_init(&item->list);
802 spin_unlock_irqrestore(&interface->macvlan_lock, flags);
804 /* We have no more items to process */
808 fm10k_mbx_lock(interface);
810 /* Check that we have plenty of space to send the message. We
811 * want to ensure that the mailbox stays low enough to avoid a
812 * change in the host state, otherwise we may see spurious
813 * link up / link down notifications.
815 if (!hw->mbx.ops.tx_ready(&hw->mbx, FM10K_VFMBX_MSG_MTU + 5)) {
816 hw->mbx.ops.process(hw, &hw->mbx);
817 set_bit(__FM10K_MACVLAN_REQUEST, interface->state);
818 fm10k_mbx_unlock(interface);
820 /* Put the request back on the list */
821 spin_lock_irqsave(&interface->macvlan_lock, flags);
822 list_add(&item->list, requests);
823 spin_unlock_irqrestore(&interface->macvlan_lock, flags);
827 switch (item->type) {
828 case FM10K_MC_MAC_REQUEST:
829 hw->mac.ops.update_mc_addr(hw,
835 case FM10K_UC_MAC_REQUEST:
836 hw->mac.ops.update_uc_addr(hw,
843 case FM10K_VLAN_REQUEST:
844 hw->mac.ops.update_vlan(hw,
853 fm10k_mbx_unlock(interface);
855 /* Free the item now that we've sent the update */
860 WARN_ON(!test_bit(__FM10K_MACVLAN_SCHED, interface->state));
862 /* flush memory to make sure state is correct */
863 smp_mb__before_atomic();
864 clear_bit(__FM10K_MACVLAN_SCHED, interface->state);
866 /* If a MAC/VLAN request was scheduled since we started, we should
867 * re-schedule. However, there is no reason to re-schedule if there is
870 if (test_bit(__FM10K_MACVLAN_REQUEST, interface->state))
871 fm10k_macvlan_schedule(interface);
875 * fm10k_configure_tx_ring - Configure Tx ring after Reset
876 * @interface: board private structure
877 * @ring: structure containing ring specific data
879 * Configure the Tx descriptor ring after a reset.
881 static void fm10k_configure_tx_ring(struct fm10k_intfc *interface,
882 struct fm10k_ring *ring)
884 struct fm10k_hw *hw = &interface->hw;
885 u64 tdba = ring->dma;
886 u32 size = ring->count * sizeof(struct fm10k_tx_desc);
887 u32 txint = FM10K_INT_MAP_DISABLE;
888 u32 txdctl = BIT(FM10K_TXDCTL_MAX_TIME_SHIFT) | FM10K_TXDCTL_ENABLE;
889 u8 reg_idx = ring->reg_idx;
891 /* disable queue to avoid issues while updating state */
892 fm10k_write_reg(hw, FM10K_TXDCTL(reg_idx), 0);
893 fm10k_write_flush(hw);
895 /* possible poll here to verify ring resources have been cleaned */
897 /* set location and size for descriptor ring */
898 fm10k_write_reg(hw, FM10K_TDBAL(reg_idx), tdba & DMA_BIT_MASK(32));
899 fm10k_write_reg(hw, FM10K_TDBAH(reg_idx), tdba >> 32);
900 fm10k_write_reg(hw, FM10K_TDLEN(reg_idx), size);
902 /* reset head and tail pointers */
903 fm10k_write_reg(hw, FM10K_TDH(reg_idx), 0);
904 fm10k_write_reg(hw, FM10K_TDT(reg_idx), 0);
906 /* store tail pointer */
907 ring->tail = &interface->uc_addr[FM10K_TDT(reg_idx)];
909 /* reset ntu and ntc to place SW in sync with hardware */
910 ring->next_to_clean = 0;
911 ring->next_to_use = 0;
914 if (ring->q_vector) {
915 txint = ring->q_vector->v_idx + NON_Q_VECTORS(hw);
916 txint |= FM10K_INT_MAP_TIMER0;
919 fm10k_write_reg(hw, FM10K_TXINT(reg_idx), txint);
921 /* enable use of FTAG bit in Tx descriptor, register is RO for VF */
922 fm10k_write_reg(hw, FM10K_PFVTCTL(reg_idx),
923 FM10K_PFVTCTL_FTAG_DESC_ENABLE);
926 if (!test_and_set_bit(__FM10K_TX_XPS_INIT_DONE, ring->state) &&
928 netif_set_xps_queue(ring->netdev,
929 &ring->q_vector->affinity_mask,
933 fm10k_write_reg(hw, FM10K_TXDCTL(reg_idx), txdctl);
937 * fm10k_enable_tx_ring - Verify Tx ring is enabled after configuration
938 * @interface: board private structure
939 * @ring: structure containing ring specific data
941 * Verify the Tx descriptor ring is ready for transmit.
943 static void fm10k_enable_tx_ring(struct fm10k_intfc *interface,
944 struct fm10k_ring *ring)
946 struct fm10k_hw *hw = &interface->hw;
949 u8 reg_idx = ring->reg_idx;
951 /* if we are already enabled just exit */
952 if (fm10k_read_reg(hw, FM10K_TXDCTL(reg_idx)) & FM10K_TXDCTL_ENABLE)
955 /* poll to verify queue is enabled */
957 usleep_range(1000, 2000);
958 txdctl = fm10k_read_reg(hw, FM10K_TXDCTL(reg_idx));
959 } while (!(txdctl & FM10K_TXDCTL_ENABLE) && --wait_loop);
961 netif_err(interface, drv, interface->netdev,
962 "Could not enable Tx Queue %d\n", reg_idx);
966 * fm10k_configure_tx - Configure Transmit Unit after Reset
967 * @interface: board private structure
969 * Configure the Tx unit of the MAC after a reset.
971 static void fm10k_configure_tx(struct fm10k_intfc *interface)
975 /* Setup the HW Tx Head and Tail descriptor pointers */
976 for (i = 0; i < interface->num_tx_queues; i++)
977 fm10k_configure_tx_ring(interface, interface->tx_ring[i]);
979 /* poll here to verify that Tx rings are now enabled */
980 for (i = 0; i < interface->num_tx_queues; i++)
981 fm10k_enable_tx_ring(interface, interface->tx_ring[i]);
985 * fm10k_configure_rx_ring - Configure Rx ring after Reset
986 * @interface: board private structure
987 * @ring: structure containing ring specific data
989 * Configure the Rx descriptor ring after a reset.
991 static void fm10k_configure_rx_ring(struct fm10k_intfc *interface,
992 struct fm10k_ring *ring)
994 u64 rdba = ring->dma;
995 struct fm10k_hw *hw = &interface->hw;
996 u32 size = ring->count * sizeof(union fm10k_rx_desc);
997 u32 rxqctl, rxdctl = FM10K_RXDCTL_WRITE_BACK_MIN_DELAY;
998 u32 srrctl = FM10K_SRRCTL_BUFFER_CHAINING_EN;
999 u32 rxint = FM10K_INT_MAP_DISABLE;
1000 u8 rx_pause = interface->rx_pause;
1001 u8 reg_idx = ring->reg_idx;
1003 /* disable queue to avoid issues while updating state */
1004 rxqctl = fm10k_read_reg(hw, FM10K_RXQCTL(reg_idx));
1005 rxqctl &= ~FM10K_RXQCTL_ENABLE;
1006 fm10k_write_reg(hw, FM10K_RXQCTL(reg_idx), rxqctl);
1007 fm10k_write_flush(hw);
1009 /* possible poll here to verify ring resources have been cleaned */
1011 /* set location and size for descriptor ring */
1012 fm10k_write_reg(hw, FM10K_RDBAL(reg_idx), rdba & DMA_BIT_MASK(32));
1013 fm10k_write_reg(hw, FM10K_RDBAH(reg_idx), rdba >> 32);
1014 fm10k_write_reg(hw, FM10K_RDLEN(reg_idx), size);
1016 /* reset head and tail pointers */
1017 fm10k_write_reg(hw, FM10K_RDH(reg_idx), 0);
1018 fm10k_write_reg(hw, FM10K_RDT(reg_idx), 0);
1020 /* store tail pointer */
1021 ring->tail = &interface->uc_addr[FM10K_RDT(reg_idx)];
1023 /* reset ntu and ntc to place SW in sync with hardware */
1024 ring->next_to_clean = 0;
1025 ring->next_to_use = 0;
1026 ring->next_to_alloc = 0;
1028 /* Configure the Rx buffer size for one buff without split */
1029 srrctl |= FM10K_RX_BUFSZ >> FM10K_SRRCTL_BSIZEPKT_SHIFT;
1031 /* Configure the Rx ring to suppress loopback packets */
1032 srrctl |= FM10K_SRRCTL_LOOPBACK_SUPPRESS;
1033 fm10k_write_reg(hw, FM10K_SRRCTL(reg_idx), srrctl);
1035 /* Enable drop on empty */
1037 if (interface->pfc_en)
1038 rx_pause = interface->pfc_en;
1040 if (!(rx_pause & BIT(ring->qos_pc)))
1041 rxdctl |= FM10K_RXDCTL_DROP_ON_EMPTY;
1043 fm10k_write_reg(hw, FM10K_RXDCTL(reg_idx), rxdctl);
1045 /* assign default VLAN to queue */
1046 ring->vid = hw->mac.default_vid;
1048 /* if we have an active VLAN, disable default VLAN ID */
1049 if (test_bit(hw->mac.default_vid, interface->active_vlans))
1050 ring->vid |= FM10K_VLAN_CLEAR;
1053 if (ring->q_vector) {
1054 rxint = ring->q_vector->v_idx + NON_Q_VECTORS(hw);
1055 rxint |= FM10K_INT_MAP_TIMER1;
1058 fm10k_write_reg(hw, FM10K_RXINT(reg_idx), rxint);
1061 rxqctl = fm10k_read_reg(hw, FM10K_RXQCTL(reg_idx));
1062 rxqctl |= FM10K_RXQCTL_ENABLE;
1063 fm10k_write_reg(hw, FM10K_RXQCTL(reg_idx), rxqctl);
1065 /* place buffers on ring for receive data */
1066 fm10k_alloc_rx_buffers(ring, fm10k_desc_unused(ring));
1070 * fm10k_update_rx_drop_en - Configures the drop enable bits for Rx rings
1071 * @interface: board private structure
1073 * Configure the drop enable bits for the Rx rings.
1075 void fm10k_update_rx_drop_en(struct fm10k_intfc *interface)
1077 struct fm10k_hw *hw = &interface->hw;
1078 u8 rx_pause = interface->rx_pause;
1082 if (interface->pfc_en)
1083 rx_pause = interface->pfc_en;
1086 for (i = 0; i < interface->num_rx_queues; i++) {
1087 struct fm10k_ring *ring = interface->rx_ring[i];
1088 u32 rxdctl = FM10K_RXDCTL_WRITE_BACK_MIN_DELAY;
1089 u8 reg_idx = ring->reg_idx;
1091 if (!(rx_pause & BIT(ring->qos_pc)))
1092 rxdctl |= FM10K_RXDCTL_DROP_ON_EMPTY;
1094 fm10k_write_reg(hw, FM10K_RXDCTL(reg_idx), rxdctl);
1099 * fm10k_configure_dglort - Configure Receive DGLORT after reset
1100 * @interface: board private structure
1102 * Configure the DGLORT description and RSS tables.
1104 static void fm10k_configure_dglort(struct fm10k_intfc *interface)
1106 struct fm10k_dglort_cfg dglort = { 0 };
1107 struct fm10k_hw *hw = &interface->hw;
1111 /* Fill out hash function seeds */
1112 for (i = 0; i < FM10K_RSSRK_SIZE; i++)
1113 fm10k_write_reg(hw, FM10K_RSSRK(0, i), interface->rssrk[i]);
1115 /* Write RETA table to hardware */
1116 for (i = 0; i < FM10K_RETA_SIZE; i++)
1117 fm10k_write_reg(hw, FM10K_RETA(0, i), interface->reta[i]);
1119 /* Generate RSS hash based on packet types, TCP/UDP
1120 * port numbers and/or IPv4/v6 src and dst addresses
1122 mrqc = FM10K_MRQC_IPV4 |
1123 FM10K_MRQC_TCP_IPV4 |
1125 FM10K_MRQC_TCP_IPV6;
1127 if (test_bit(FM10K_FLAG_RSS_FIELD_IPV4_UDP, interface->flags))
1128 mrqc |= FM10K_MRQC_UDP_IPV4;
1129 if (test_bit(FM10K_FLAG_RSS_FIELD_IPV6_UDP, interface->flags))
1130 mrqc |= FM10K_MRQC_UDP_IPV6;
1132 fm10k_write_reg(hw, FM10K_MRQC(0), mrqc);
1134 /* configure default DGLORT mapping for RSS/DCB */
1135 dglort.inner_rss = 1;
1136 dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
1137 dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
1138 hw->mac.ops.configure_dglort_map(hw, &dglort);
1140 /* assign GLORT per queue for queue mapped testing */
1141 if (interface->glort_count > 64) {
1142 memset(&dglort, 0, sizeof(dglort));
1143 dglort.inner_rss = 1;
1144 dglort.glort = interface->glort + 64;
1145 dglort.idx = fm10k_dglort_pf_queue;
1146 dglort.queue_l = fls(interface->num_rx_queues - 1);
1147 hw->mac.ops.configure_dglort_map(hw, &dglort);
1150 /* assign glort value for RSS/DCB specific to this interface */
1151 memset(&dglort, 0, sizeof(dglort));
1152 dglort.inner_rss = 1;
1153 dglort.glort = interface->glort;
1154 dglort.rss_l = fls(interface->ring_feature[RING_F_RSS].mask);
1155 dglort.pc_l = fls(interface->ring_feature[RING_F_QOS].mask);
1156 /* configure DGLORT mapping for RSS/DCB */
1157 dglort.idx = fm10k_dglort_pf_rss;
1158 if (interface->l2_accel)
1159 dglort.shared_l = fls(interface->l2_accel->size);
1160 hw->mac.ops.configure_dglort_map(hw, &dglort);
1164 * fm10k_configure_rx - Configure Receive Unit after Reset
1165 * @interface: board private structure
1167 * Configure the Rx unit of the MAC after a reset.
1169 static void fm10k_configure_rx(struct fm10k_intfc *interface)
1173 /* Configure SWPRI to PC map */
1174 fm10k_configure_swpri_map(interface);
1176 /* Configure RSS and DGLORT map */
1177 fm10k_configure_dglort(interface);
1179 /* Setup the HW Rx Head and Tail descriptor pointers */
1180 for (i = 0; i < interface->num_rx_queues; i++)
1181 fm10k_configure_rx_ring(interface, interface->rx_ring[i]);
1183 /* possible poll here to verify that Rx rings are now enabled */
1186 static void fm10k_napi_enable_all(struct fm10k_intfc *interface)
1188 struct fm10k_q_vector *q_vector;
1191 for (q_idx = 0; q_idx < interface->num_q_vectors; q_idx++) {
1192 q_vector = interface->q_vector[q_idx];
1193 napi_enable(&q_vector->napi);
1197 static irqreturn_t fm10k_msix_clean_rings(int __always_unused irq, void *data)
1199 struct fm10k_q_vector *q_vector = data;
1201 if (q_vector->rx.count || q_vector->tx.count)
1202 napi_schedule_irqoff(&q_vector->napi);
1207 static irqreturn_t fm10k_msix_mbx_vf(int __always_unused irq, void *data)
1209 struct fm10k_intfc *interface = data;
1210 struct fm10k_hw *hw = &interface->hw;
1211 struct fm10k_mbx_info *mbx = &hw->mbx;
1213 /* re-enable mailbox interrupt and indicate 20us delay */
1214 fm10k_write_reg(hw, FM10K_VFITR(FM10K_MBX_VECTOR),
1215 (FM10K_MBX_INT_DELAY >> hw->mac.itr_scale) |
1218 /* service upstream mailbox */
1219 if (fm10k_mbx_trylock(interface)) {
1220 mbx->ops.process(hw, mbx);
1221 fm10k_mbx_unlock(interface);
1224 hw->mac.get_host_state = true;
1225 fm10k_service_event_schedule(interface);
1230 #ifdef CONFIG_NET_POLL_CONTROLLER
1232 * fm10k_netpoll - A Polling 'interrupt' handler
1233 * @netdev: network interface device structure
1235 * This is used by netconsole to send skbs without having to re-enable
1236 * interrupts. It's not called while the normal interrupt routine is executing.
1238 void fm10k_netpoll(struct net_device *netdev)
1240 struct fm10k_intfc *interface = netdev_priv(netdev);
1243 /* if interface is down do nothing */
1244 if (test_bit(__FM10K_DOWN, interface->state))
1247 for (i = 0; i < interface->num_q_vectors; i++)
1248 fm10k_msix_clean_rings(0, interface->q_vector[i]);
1252 #define FM10K_ERR_MSG(type) case (type): error = #type; break
1253 static void fm10k_handle_fault(struct fm10k_intfc *interface, int type,
1254 struct fm10k_fault *fault)
1256 struct pci_dev *pdev = interface->pdev;
1257 struct fm10k_hw *hw = &interface->hw;
1258 struct fm10k_iov_data *iov_data = interface->iov_data;
1262 case FM10K_PCA_FAULT:
1263 switch (fault->type) {
1265 error = "Unknown PCA error";
1267 FM10K_ERR_MSG(PCA_NO_FAULT);
1268 FM10K_ERR_MSG(PCA_UNMAPPED_ADDR);
1269 FM10K_ERR_MSG(PCA_BAD_QACCESS_PF);
1270 FM10K_ERR_MSG(PCA_BAD_QACCESS_VF);
1271 FM10K_ERR_MSG(PCA_MALICIOUS_REQ);
1272 FM10K_ERR_MSG(PCA_POISONED_TLP);
1273 FM10K_ERR_MSG(PCA_TLP_ABORT);
1276 case FM10K_THI_FAULT:
1277 switch (fault->type) {
1279 error = "Unknown THI error";
1281 FM10K_ERR_MSG(THI_NO_FAULT);
1282 FM10K_ERR_MSG(THI_MAL_DIS_Q_FAULT);
1285 case FM10K_FUM_FAULT:
1286 switch (fault->type) {
1288 error = "Unknown FUM error";
1290 FM10K_ERR_MSG(FUM_NO_FAULT);
1291 FM10K_ERR_MSG(FUM_UNMAPPED_ADDR);
1292 FM10K_ERR_MSG(FUM_BAD_VF_QACCESS);
1293 FM10K_ERR_MSG(FUM_ADD_DECODE_ERR);
1294 FM10K_ERR_MSG(FUM_RO_ERROR);
1295 FM10K_ERR_MSG(FUM_QPRC_CRC_ERROR);
1296 FM10K_ERR_MSG(FUM_CSR_TIMEOUT);
1297 FM10K_ERR_MSG(FUM_INVALID_TYPE);
1298 FM10K_ERR_MSG(FUM_INVALID_LENGTH);
1299 FM10K_ERR_MSG(FUM_INVALID_BE);
1300 FM10K_ERR_MSG(FUM_INVALID_ALIGN);
1304 error = "Undocumented fault";
1308 dev_warn(&pdev->dev,
1309 "%s Address: 0x%llx SpecInfo: 0x%x Func: %02x.%0x\n",
1310 error, fault->address, fault->specinfo,
1311 PCI_SLOT(fault->func), PCI_FUNC(fault->func));
1313 /* For VF faults, clear out the respective LPORT, reset the queue
1314 * resources, and then reconnect to the mailbox. This allows the
1315 * VF in question to resume behavior. For transient faults that are
1316 * the result of non-malicious behavior this will log the fault and
1317 * allow the VF to resume functionality. Obviously for malicious VFs
1318 * they will be able to attempt malicious behavior again. In this
1319 * case, the system administrator will need to step in and manually
1320 * remove or disable the VF in question.
1322 if (fault->func && iov_data) {
1323 int vf = fault->func - 1;
1324 struct fm10k_vf_info *vf_info = &iov_data->vf_info[vf];
1326 hw->iov.ops.reset_lport(hw, vf_info);
1327 hw->iov.ops.reset_resources(hw, vf_info);
1329 /* reset_lport disables the VF, so re-enable it */
1330 hw->iov.ops.set_lport(hw, vf_info, vf,
1331 FM10K_VF_FLAG_MULTI_CAPABLE);
1333 /* reset_resources will disconnect from the mbx */
1334 vf_info->mbx.ops.connect(hw, &vf_info->mbx);
1338 static void fm10k_report_fault(struct fm10k_intfc *interface, u32 eicr)
1340 struct fm10k_hw *hw = &interface->hw;
1341 struct fm10k_fault fault = { 0 };
1344 for (eicr &= FM10K_EICR_FAULT_MASK, type = FM10K_PCA_FAULT;
1346 eicr >>= 1, type += FM10K_FAULT_SIZE) {
1347 /* only check if there is an error reported */
1351 /* retrieve fault info */
1352 err = hw->mac.ops.get_fault(hw, type, &fault);
1354 dev_err(&interface->pdev->dev,
1355 "error reading fault\n");
1359 fm10k_handle_fault(interface, type, &fault);
1363 static void fm10k_reset_drop_on_empty(struct fm10k_intfc *interface, u32 eicr)
1365 struct fm10k_hw *hw = &interface->hw;
1366 const u32 rxdctl = FM10K_RXDCTL_WRITE_BACK_MIN_DELAY;
1370 if (!(eicr & FM10K_EICR_MAXHOLDTIME))
1373 maxholdq = fm10k_read_reg(hw, FM10K_MAXHOLDQ(7));
1375 fm10k_write_reg(hw, FM10K_MAXHOLDQ(7), maxholdq);
1377 if (maxholdq & BIT(31)) {
1378 if (q < FM10K_MAX_QUEUES_PF) {
1379 interface->rx_overrun_pf++;
1380 fm10k_write_reg(hw, FM10K_RXDCTL(q), rxdctl);
1382 interface->rx_overrun_vf++;
1396 maxholdq = fm10k_read_reg(hw, FM10K_MAXHOLDQ(q / 32));
1398 fm10k_write_reg(hw, FM10K_MAXHOLDQ(q / 32), maxholdq);
1402 static irqreturn_t fm10k_msix_mbx_pf(int __always_unused irq, void *data)
1404 struct fm10k_intfc *interface = data;
1405 struct fm10k_hw *hw = &interface->hw;
1406 struct fm10k_mbx_info *mbx = &hw->mbx;
1410 /* unmask any set bits related to this interrupt */
1411 eicr = fm10k_read_reg(hw, FM10K_EICR);
1412 fm10k_write_reg(hw, FM10K_EICR, eicr & (FM10K_EICR_MAILBOX |
1413 FM10K_EICR_SWITCHREADY |
1414 FM10K_EICR_SWITCHNOTREADY));
1416 /* report any faults found to the message log */
1417 fm10k_report_fault(interface, eicr);
1419 /* reset any queues disabled due to receiver overrun */
1420 fm10k_reset_drop_on_empty(interface, eicr);
1422 /* service mailboxes */
1423 if (fm10k_mbx_trylock(interface)) {
1424 err = mbx->ops.process(hw, mbx);
1425 /* handle VFLRE events */
1426 fm10k_iov_event(interface);
1427 fm10k_mbx_unlock(interface);
1430 if (err == FM10K_ERR_RESET_REQUESTED)
1431 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1433 /* if switch toggled state we should reset GLORTs */
1434 if (eicr & FM10K_EICR_SWITCHNOTREADY) {
1435 /* force link down for at least 4 seconds */
1436 interface->link_down_event = jiffies + (4 * HZ);
1437 set_bit(__FM10K_LINK_DOWN, interface->state);
1439 /* reset dglort_map back to no config */
1440 hw->mac.dglort_map = FM10K_DGLORTMAP_NONE;
1443 /* we should validate host state after interrupt event */
1444 hw->mac.get_host_state = true;
1446 /* validate host state, and handle VF mailboxes in the service task */
1447 fm10k_service_event_schedule(interface);
1449 /* re-enable mailbox interrupt and indicate 20us delay */
1450 fm10k_write_reg(hw, FM10K_ITR(FM10K_MBX_VECTOR),
1451 (FM10K_MBX_INT_DELAY >> hw->mac.itr_scale) |
1457 void fm10k_mbx_free_irq(struct fm10k_intfc *interface)
1459 struct fm10k_hw *hw = &interface->hw;
1460 struct msix_entry *entry;
1463 /* no mailbox IRQ to free if MSI-X is not enabled */
1464 if (!interface->msix_entries)
1467 entry = &interface->msix_entries[FM10K_MBX_VECTOR];
1469 /* disconnect the mailbox */
1470 hw->mbx.ops.disconnect(hw, &hw->mbx);
1472 /* disable Mailbox cause */
1473 if (hw->mac.type == fm10k_mac_pf) {
1474 fm10k_write_reg(hw, FM10K_EIMR,
1475 FM10K_EIMR_DISABLE(PCA_FAULT) |
1476 FM10K_EIMR_DISABLE(FUM_FAULT) |
1477 FM10K_EIMR_DISABLE(MAILBOX) |
1478 FM10K_EIMR_DISABLE(SWITCHREADY) |
1479 FM10K_EIMR_DISABLE(SWITCHNOTREADY) |
1480 FM10K_EIMR_DISABLE(SRAMERROR) |
1481 FM10K_EIMR_DISABLE(VFLR) |
1482 FM10K_EIMR_DISABLE(MAXHOLDTIME));
1483 itr_reg = FM10K_ITR(FM10K_MBX_VECTOR);
1485 itr_reg = FM10K_VFITR(FM10K_MBX_VECTOR);
1488 fm10k_write_reg(hw, itr_reg, FM10K_ITR_MASK_SET);
1490 free_irq(entry->vector, interface);
1493 static s32 fm10k_mbx_mac_addr(struct fm10k_hw *hw, u32 **results,
1494 struct fm10k_mbx_info *mbx)
1496 bool vlan_override = hw->mac.vlan_override;
1497 u16 default_vid = hw->mac.default_vid;
1498 struct fm10k_intfc *interface;
1501 err = fm10k_msg_mac_vlan_vf(hw, results, mbx);
1505 interface = container_of(hw, struct fm10k_intfc, hw);
1507 /* MAC was changed so we need reset */
1508 if (is_valid_ether_addr(hw->mac.perm_addr) &&
1509 !ether_addr_equal(hw->mac.perm_addr, hw->mac.addr))
1510 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1512 /* VLAN override was changed, or default VLAN changed */
1513 if ((vlan_override != hw->mac.vlan_override) ||
1514 (default_vid != hw->mac.default_vid))
1515 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1520 /* generic error handler for mailbox issues */
1521 static s32 fm10k_mbx_error(struct fm10k_hw *hw, u32 **results,
1522 struct fm10k_mbx_info __always_unused *mbx)
1524 struct fm10k_intfc *interface;
1525 struct pci_dev *pdev;
1527 interface = container_of(hw, struct fm10k_intfc, hw);
1528 pdev = interface->pdev;
1530 dev_err(&pdev->dev, "Unknown message ID %u\n",
1531 **results & FM10K_TLV_ID_MASK);
1536 static const struct fm10k_msg_data vf_mbx_data[] = {
1537 FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test),
1538 FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_mbx_mac_addr),
1539 FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_msg_lport_state_vf),
1540 FM10K_TLV_MSG_ERROR_HANDLER(fm10k_mbx_error),
1543 static int fm10k_mbx_request_irq_vf(struct fm10k_intfc *interface)
1545 struct msix_entry *entry = &interface->msix_entries[FM10K_MBX_VECTOR];
1546 struct net_device *dev = interface->netdev;
1547 struct fm10k_hw *hw = &interface->hw;
1550 /* Use timer0 for interrupt moderation on the mailbox */
1551 u32 itr = entry->entry | FM10K_INT_MAP_TIMER0;
1553 /* register mailbox handlers */
1554 err = hw->mbx.ops.register_handlers(&hw->mbx, vf_mbx_data);
1558 /* request the IRQ */
1559 err = request_irq(entry->vector, fm10k_msix_mbx_vf, 0,
1560 dev->name, interface);
1562 netif_err(interface, probe, dev,
1563 "request_irq for msix_mbx failed: %d\n", err);
1567 /* map all of the interrupt sources */
1568 fm10k_write_reg(hw, FM10K_VFINT_MAP, itr);
1570 /* enable interrupt */
1571 fm10k_write_reg(hw, FM10K_VFITR(entry->entry), FM10K_ITR_ENABLE);
1576 static s32 fm10k_lport_map(struct fm10k_hw *hw, u32 **results,
1577 struct fm10k_mbx_info *mbx)
1579 struct fm10k_intfc *interface;
1580 u32 dglort_map = hw->mac.dglort_map;
1583 interface = container_of(hw, struct fm10k_intfc, hw);
1585 err = fm10k_msg_err_pf(hw, results, mbx);
1586 if (!err && hw->swapi.status) {
1587 /* force link down for a reasonable delay */
1588 interface->link_down_event = jiffies + (2 * HZ);
1589 set_bit(__FM10K_LINK_DOWN, interface->state);
1591 /* reset dglort_map back to no config */
1592 hw->mac.dglort_map = FM10K_DGLORTMAP_NONE;
1594 fm10k_service_event_schedule(interface);
1596 /* prevent overloading kernel message buffer */
1597 if (interface->lport_map_failed)
1600 interface->lport_map_failed = true;
1602 if (hw->swapi.status == FM10K_MSG_ERR_PEP_NOT_SCHEDULED)
1603 dev_warn(&interface->pdev->dev,
1604 "cannot obtain link because the host interface is configured for a PCIe host interface bandwidth of zero\n");
1605 dev_warn(&interface->pdev->dev,
1606 "request logical port map failed: %d\n",
1612 err = fm10k_msg_lport_map_pf(hw, results, mbx);
1616 interface->lport_map_failed = false;
1618 /* we need to reset if port count was just updated */
1619 if (dglort_map != hw->mac.dglort_map)
1620 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1625 static s32 fm10k_update_pvid(struct fm10k_hw *hw, u32 **results,
1626 struct fm10k_mbx_info __always_unused *mbx)
1628 struct fm10k_intfc *interface;
1633 err = fm10k_tlv_attr_get_u32(results[FM10K_PF_ATTR_ID_UPDATE_PVID],
1638 /* extract values from the pvid update */
1639 glort = FM10K_MSG_HDR_FIELD_GET(pvid_update, UPDATE_PVID_GLORT);
1640 pvid = FM10K_MSG_HDR_FIELD_GET(pvid_update, UPDATE_PVID_PVID);
1642 /* if glort is not valid return error */
1643 if (!fm10k_glort_valid_pf(hw, glort))
1644 return FM10K_ERR_PARAM;
1646 /* verify VLAN ID is valid */
1647 if (pvid >= FM10K_VLAN_TABLE_VID_MAX)
1648 return FM10K_ERR_PARAM;
1650 interface = container_of(hw, struct fm10k_intfc, hw);
1652 /* check to see if this belongs to one of the VFs */
1653 err = fm10k_iov_update_pvid(interface, glort, pvid);
1657 /* we need to reset if default VLAN was just updated */
1658 if (pvid != hw->mac.default_vid)
1659 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1661 hw->mac.default_vid = pvid;
1666 static const struct fm10k_msg_data pf_mbx_data[] = {
1667 FM10K_PF_MSG_ERR_HANDLER(XCAST_MODES, fm10k_msg_err_pf),
1668 FM10K_PF_MSG_ERR_HANDLER(UPDATE_MAC_FWD_RULE, fm10k_msg_err_pf),
1669 FM10K_PF_MSG_LPORT_MAP_HANDLER(fm10k_lport_map),
1670 FM10K_PF_MSG_ERR_HANDLER(LPORT_CREATE, fm10k_msg_err_pf),
1671 FM10K_PF_MSG_ERR_HANDLER(LPORT_DELETE, fm10k_msg_err_pf),
1672 FM10K_PF_MSG_UPDATE_PVID_HANDLER(fm10k_update_pvid),
1673 FM10K_TLV_MSG_ERROR_HANDLER(fm10k_mbx_error),
1676 static int fm10k_mbx_request_irq_pf(struct fm10k_intfc *interface)
1678 struct msix_entry *entry = &interface->msix_entries[FM10K_MBX_VECTOR];
1679 struct net_device *dev = interface->netdev;
1680 struct fm10k_hw *hw = &interface->hw;
1683 /* Use timer0 for interrupt moderation on the mailbox */
1684 u32 mbx_itr = entry->entry | FM10K_INT_MAP_TIMER0;
1685 u32 other_itr = entry->entry | FM10K_INT_MAP_IMMEDIATE;
1687 /* register mailbox handlers */
1688 err = hw->mbx.ops.register_handlers(&hw->mbx, pf_mbx_data);
1692 /* request the IRQ */
1693 err = request_irq(entry->vector, fm10k_msix_mbx_pf, 0,
1694 dev->name, interface);
1696 netif_err(interface, probe, dev,
1697 "request_irq for msix_mbx failed: %d\n", err);
1701 /* Enable interrupts w/ no moderation for "other" interrupts */
1702 fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_pcie_fault), other_itr);
1703 fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_switch_up_down), other_itr);
1704 fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_sram), other_itr);
1705 fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_max_hold_time), other_itr);
1706 fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_vflr), other_itr);
1708 /* Enable interrupts w/ moderation for mailbox */
1709 fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_mailbox), mbx_itr);
1711 /* Enable individual interrupt causes */
1712 fm10k_write_reg(hw, FM10K_EIMR, FM10K_EIMR_ENABLE(PCA_FAULT) |
1713 FM10K_EIMR_ENABLE(FUM_FAULT) |
1714 FM10K_EIMR_ENABLE(MAILBOX) |
1715 FM10K_EIMR_ENABLE(SWITCHREADY) |
1716 FM10K_EIMR_ENABLE(SWITCHNOTREADY) |
1717 FM10K_EIMR_ENABLE(SRAMERROR) |
1718 FM10K_EIMR_ENABLE(VFLR) |
1719 FM10K_EIMR_ENABLE(MAXHOLDTIME));
1721 /* enable interrupt */
1722 fm10k_write_reg(hw, FM10K_ITR(entry->entry), FM10K_ITR_ENABLE);
1727 int fm10k_mbx_request_irq(struct fm10k_intfc *interface)
1729 struct fm10k_hw *hw = &interface->hw;
1732 /* enable Mailbox cause */
1733 if (hw->mac.type == fm10k_mac_pf)
1734 err = fm10k_mbx_request_irq_pf(interface);
1736 err = fm10k_mbx_request_irq_vf(interface);
1740 /* connect mailbox */
1741 err = hw->mbx.ops.connect(hw, &hw->mbx);
1743 /* if the mailbox failed to connect, then free IRQ */
1745 fm10k_mbx_free_irq(interface);
1751 * fm10k_qv_free_irq - release interrupts associated with queue vectors
1752 * @interface: board private structure
1754 * Release all interrupts associated with this interface
1756 void fm10k_qv_free_irq(struct fm10k_intfc *interface)
1758 int vector = interface->num_q_vectors;
1759 struct fm10k_hw *hw = &interface->hw;
1760 struct msix_entry *entry;
1762 entry = &interface->msix_entries[NON_Q_VECTORS(hw) + vector];
1765 struct fm10k_q_vector *q_vector;
1769 q_vector = interface->q_vector[vector];
1771 if (!q_vector->tx.count && !q_vector->rx.count)
1774 /* clear the affinity_mask in the IRQ descriptor */
1775 irq_set_affinity_hint(entry->vector, NULL);
1777 /* disable interrupts */
1778 writel(FM10K_ITR_MASK_SET, q_vector->itr);
1780 free_irq(entry->vector, q_vector);
1785 * fm10k_qv_request_irq - initialize interrupts for queue vectors
1786 * @interface: board private structure
1788 * Attempts to configure interrupts using the best available
1789 * capabilities of the hardware and kernel.
1791 int fm10k_qv_request_irq(struct fm10k_intfc *interface)
1793 struct net_device *dev = interface->netdev;
1794 struct fm10k_hw *hw = &interface->hw;
1795 struct msix_entry *entry;
1796 unsigned int ri = 0, ti = 0;
1799 entry = &interface->msix_entries[NON_Q_VECTORS(hw)];
1801 for (vector = 0; vector < interface->num_q_vectors; vector++) {
1802 struct fm10k_q_vector *q_vector = interface->q_vector[vector];
1804 /* name the vector */
1805 if (q_vector->tx.count && q_vector->rx.count) {
1806 snprintf(q_vector->name, sizeof(q_vector->name),
1807 "%s-TxRx-%u", dev->name, ri++);
1809 } else if (q_vector->rx.count) {
1810 snprintf(q_vector->name, sizeof(q_vector->name),
1811 "%s-rx-%u", dev->name, ri++);
1812 } else if (q_vector->tx.count) {
1813 snprintf(q_vector->name, sizeof(q_vector->name),
1814 "%s-tx-%u", dev->name, ti++);
1816 /* skip this unused q_vector */
1820 /* Assign ITR register to q_vector */
1821 q_vector->itr = (hw->mac.type == fm10k_mac_pf) ?
1822 &interface->uc_addr[FM10K_ITR(entry->entry)] :
1823 &interface->uc_addr[FM10K_VFITR(entry->entry)];
1825 /* request the IRQ */
1826 err = request_irq(entry->vector, &fm10k_msix_clean_rings, 0,
1827 q_vector->name, q_vector);
1829 netif_err(interface, probe, dev,
1830 "request_irq failed for MSIX interrupt Error: %d\n",
1835 /* assign the mask for this irq */
1836 irq_set_affinity_hint(entry->vector, &q_vector->affinity_mask);
1838 /* Enable q_vector */
1839 writel(FM10K_ITR_ENABLE, q_vector->itr);
1847 /* wind through the ring freeing all entries and vectors */
1849 struct fm10k_q_vector *q_vector;
1853 q_vector = interface->q_vector[vector];
1855 if (!q_vector->tx.count && !q_vector->rx.count)
1858 /* clear the affinity_mask in the IRQ descriptor */
1859 irq_set_affinity_hint(entry->vector, NULL);
1861 /* disable interrupts */
1862 writel(FM10K_ITR_MASK_SET, q_vector->itr);
1864 free_irq(entry->vector, q_vector);
1870 void fm10k_up(struct fm10k_intfc *interface)
1872 struct fm10k_hw *hw = &interface->hw;
1874 /* Enable Tx/Rx DMA */
1875 hw->mac.ops.start_hw(hw);
1877 /* configure Tx descriptor rings */
1878 fm10k_configure_tx(interface);
1880 /* configure Rx descriptor rings */
1881 fm10k_configure_rx(interface);
1883 /* configure interrupts */
1884 hw->mac.ops.update_int_moderator(hw);
1886 /* enable statistics capture again */
1887 clear_bit(__FM10K_UPDATING_STATS, interface->state);
1889 /* clear down bit to indicate we are ready to go */
1890 clear_bit(__FM10K_DOWN, interface->state);
1892 /* enable polling cleanups */
1893 fm10k_napi_enable_all(interface);
1895 /* re-establish Rx filters */
1896 fm10k_restore_rx_state(interface);
1898 /* enable transmits */
1899 netif_tx_start_all_queues(interface->netdev);
1901 /* kick off the service timer now */
1902 hw->mac.get_host_state = true;
1903 mod_timer(&interface->service_timer, jiffies);
1906 static void fm10k_napi_disable_all(struct fm10k_intfc *interface)
1908 struct fm10k_q_vector *q_vector;
1911 for (q_idx = 0; q_idx < interface->num_q_vectors; q_idx++) {
1912 q_vector = interface->q_vector[q_idx];
1913 napi_disable(&q_vector->napi);
1917 void fm10k_down(struct fm10k_intfc *interface)
1919 struct net_device *netdev = interface->netdev;
1920 struct fm10k_hw *hw = &interface->hw;
1921 int err, i = 0, count = 0;
1923 /* signal that we are down to the interrupt handler and service task */
1924 if (test_and_set_bit(__FM10K_DOWN, interface->state))
1927 /* call carrier off first to avoid false dev_watchdog timeouts */
1928 netif_carrier_off(netdev);
1930 /* disable transmits */
1931 netif_tx_stop_all_queues(netdev);
1932 netif_tx_disable(netdev);
1934 /* reset Rx filters */
1935 fm10k_reset_rx_state(interface);
1937 /* disable polling routines */
1938 fm10k_napi_disable_all(interface);
1940 /* capture stats one last time before stopping interface */
1941 fm10k_update_stats(interface);
1943 /* prevent updating statistics while we're down */
1944 while (test_and_set_bit(__FM10K_UPDATING_STATS, interface->state))
1945 usleep_range(1000, 2000);
1947 /* skip waiting for TX DMA if we lost PCIe link */
1948 if (FM10K_REMOVED(hw->hw_addr))
1949 goto skip_tx_dma_drain;
1951 /* In some rare circumstances it can take a while for Tx queues to
1952 * quiesce and be fully disabled. Attempt to .stop_hw() first, and
1953 * then if we get ERR_REQUESTS_PENDING, go ahead and wait in a loop
1954 * until the Tx queues have emptied, or until a number of retries. If
1955 * we fail to clear within the retry loop, we will issue a warning
1956 * indicating that Tx DMA is probably hung. Note this means we call
1957 * .stop_hw() twice but this shouldn't cause any problems.
1959 err = hw->mac.ops.stop_hw(hw);
1960 if (err != FM10K_ERR_REQUESTS_PENDING)
1961 goto skip_tx_dma_drain;
1963 #define TX_DMA_DRAIN_RETRIES 25
1964 for (count = 0; count < TX_DMA_DRAIN_RETRIES; count++) {
1965 usleep_range(10000, 20000);
1967 /* start checking at the last ring to have pending Tx */
1968 for (; i < interface->num_tx_queues; i++)
1969 if (fm10k_get_tx_pending(interface->tx_ring[i], false))
1972 /* if all the queues are drained, we can break now */
1973 if (i == interface->num_tx_queues)
1977 if (count >= TX_DMA_DRAIN_RETRIES)
1978 dev_err(&interface->pdev->dev,
1979 "Tx queues failed to drain after %d tries. Tx DMA is probably hung.\n",
1982 /* Disable DMA engine for Tx/Rx */
1983 err = hw->mac.ops.stop_hw(hw);
1984 if (err == FM10K_ERR_REQUESTS_PENDING)
1985 dev_err(&interface->pdev->dev,
1986 "due to pending requests hw was not shut down gracefully\n");
1988 dev_err(&interface->pdev->dev, "stop_hw failed: %d\n", err);
1990 /* free any buffers still on the rings */
1991 fm10k_clean_all_tx_rings(interface);
1992 fm10k_clean_all_rx_rings(interface);
1996 * fm10k_sw_init - Initialize general software structures
1997 * @interface: host interface private structure to initialize
1999 * fm10k_sw_init initializes the interface private data structure.
2000 * Fields are initialized based on PCI device information and
2001 * OS network device settings (MTU size).
2003 static int fm10k_sw_init(struct fm10k_intfc *interface,
2004 const struct pci_device_id *ent)
2006 const struct fm10k_info *fi = fm10k_info_tbl[ent->driver_data];
2007 struct fm10k_hw *hw = &interface->hw;
2008 struct pci_dev *pdev = interface->pdev;
2009 struct net_device *netdev = interface->netdev;
2010 u32 rss_key[FM10K_RSSRK_SIZE];
2014 /* initialize back pointer */
2015 hw->back = interface;
2016 hw->hw_addr = interface->uc_addr;
2018 /* PCI config space info */
2019 hw->vendor_id = pdev->vendor;
2020 hw->device_id = pdev->device;
2021 hw->revision_id = pdev->revision;
2022 hw->subsystem_vendor_id = pdev->subsystem_vendor;
2023 hw->subsystem_device_id = pdev->subsystem_device;
2026 memcpy(&hw->mac.ops, fi->mac_ops, sizeof(hw->mac.ops));
2027 hw->mac.type = fi->mac;
2029 /* Setup IOV handlers */
2031 memcpy(&hw->iov.ops, fi->iov_ops, sizeof(hw->iov.ops));
2033 /* Set common capability flags and settings */
2034 rss = min_t(int, FM10K_MAX_RSS_INDICES, num_online_cpus());
2035 interface->ring_feature[RING_F_RSS].limit = rss;
2036 fi->get_invariants(hw);
2038 /* pick up the PCIe bus settings for reporting later */
2039 if (hw->mac.ops.get_bus_info)
2040 hw->mac.ops.get_bus_info(hw);
2042 /* limit the usable DMA range */
2043 if (hw->mac.ops.set_dma_mask)
2044 hw->mac.ops.set_dma_mask(hw, dma_get_mask(&pdev->dev));
2046 /* update netdev with DMA restrictions */
2047 if (dma_get_mask(&pdev->dev) > DMA_BIT_MASK(32)) {
2048 netdev->features |= NETIF_F_HIGHDMA;
2049 netdev->vlan_features |= NETIF_F_HIGHDMA;
2052 /* reset and initialize the hardware so it is in a known state */
2053 err = hw->mac.ops.reset_hw(hw);
2055 dev_err(&pdev->dev, "reset_hw failed: %d\n", err);
2059 err = hw->mac.ops.init_hw(hw);
2061 dev_err(&pdev->dev, "init_hw failed: %d\n", err);
2065 /* initialize hardware statistics */
2066 hw->mac.ops.update_hw_stats(hw, &interface->stats);
2068 /* Set upper limit on IOV VFs that can be allocated */
2069 pci_sriov_set_totalvfs(pdev, hw->iov.total_vfs);
2071 /* Start with random Ethernet address */
2072 eth_random_addr(hw->mac.addr);
2074 /* Initialize MAC address from hardware */
2075 err = hw->mac.ops.read_mac_addr(hw);
2077 dev_warn(&pdev->dev,
2078 "Failed to obtain MAC address defaulting to random\n");
2079 /* tag address assignment as random */
2080 netdev->addr_assign_type |= NET_ADDR_RANDOM;
2083 ether_addr_copy(netdev->dev_addr, hw->mac.addr);
2084 ether_addr_copy(netdev->perm_addr, hw->mac.addr);
2086 if (!is_valid_ether_addr(netdev->perm_addr)) {
2087 dev_err(&pdev->dev, "Invalid MAC Address\n");
2091 /* initialize DCBNL interface */
2092 fm10k_dcbnl_set_ops(netdev);
2094 /* set default ring sizes */
2095 interface->tx_ring_count = FM10K_DEFAULT_TXD;
2096 interface->rx_ring_count = FM10K_DEFAULT_RXD;
2098 /* set default interrupt moderation */
2099 interface->tx_itr = FM10K_TX_ITR_DEFAULT;
2100 interface->rx_itr = FM10K_ITR_ADAPTIVE | FM10K_RX_ITR_DEFAULT;
2102 /* initialize udp port lists */
2103 INIT_LIST_HEAD(&interface->vxlan_port);
2104 INIT_LIST_HEAD(&interface->geneve_port);
2106 /* Initialize the MAC/VLAN queue */
2107 INIT_LIST_HEAD(&interface->macvlan_requests);
2109 netdev_rss_key_fill(rss_key, sizeof(rss_key));
2110 memcpy(interface->rssrk, rss_key, sizeof(rss_key));
2112 /* Initialize the mailbox lock */
2113 spin_lock_init(&interface->mbx_lock);
2114 spin_lock_init(&interface->macvlan_lock);
2116 /* Start off interface as being down */
2117 set_bit(__FM10K_DOWN, interface->state);
2118 set_bit(__FM10K_UPDATING_STATS, interface->state);
2123 static void fm10k_slot_warn(struct fm10k_intfc *interface)
2125 enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
2126 enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
2127 struct fm10k_hw *hw = &interface->hw;
2128 int max_gts = 0, expected_gts = 0;
2130 if (pcie_get_minimum_link(interface->pdev, &speed, &width) ||
2131 speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN) {
2132 dev_warn(&interface->pdev->dev,
2133 "Unable to determine PCI Express bandwidth.\n");
2138 case PCIE_SPEED_2_5GT:
2139 /* 8b/10b encoding reduces max throughput by 20% */
2140 max_gts = 2 * width;
2142 case PCIE_SPEED_5_0GT:
2143 /* 8b/10b encoding reduces max throughput by 20% */
2144 max_gts = 4 * width;
2146 case PCIE_SPEED_8_0GT:
2147 /* 128b/130b encoding has less than 2% impact on throughput */
2148 max_gts = 8 * width;
2151 dev_warn(&interface->pdev->dev,
2152 "Unable to determine PCI Express bandwidth.\n");
2156 dev_info(&interface->pdev->dev,
2157 "PCI Express bandwidth of %dGT/s available\n",
2159 dev_info(&interface->pdev->dev,
2160 "(Speed:%s, Width: x%d, Encoding Loss:%s, Payload:%s)\n",
2161 (speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
2162 speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
2163 speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
2166 (speed == PCIE_SPEED_2_5GT ? "20%" :
2167 speed == PCIE_SPEED_5_0GT ? "20%" :
2168 speed == PCIE_SPEED_8_0GT ? "<2%" :
2170 (hw->bus.payload == fm10k_bus_payload_128 ? "128B" :
2171 hw->bus.payload == fm10k_bus_payload_256 ? "256B" :
2172 hw->bus.payload == fm10k_bus_payload_512 ? "512B" :
2175 switch (hw->bus_caps.speed) {
2176 case fm10k_bus_speed_2500:
2177 /* 8b/10b encoding reduces max throughput by 20% */
2178 expected_gts = 2 * hw->bus_caps.width;
2180 case fm10k_bus_speed_5000:
2181 /* 8b/10b encoding reduces max throughput by 20% */
2182 expected_gts = 4 * hw->bus_caps.width;
2184 case fm10k_bus_speed_8000:
2185 /* 128b/130b encoding has less than 2% impact on throughput */
2186 expected_gts = 8 * hw->bus_caps.width;
2189 dev_warn(&interface->pdev->dev,
2190 "Unable to determine expected PCI Express bandwidth.\n");
2194 if (max_gts >= expected_gts)
2197 dev_warn(&interface->pdev->dev,
2198 "This device requires %dGT/s of bandwidth for optimal performance.\n",
2200 dev_warn(&interface->pdev->dev,
2201 "A %sslot with x%d lanes is suggested.\n",
2202 (hw->bus_caps.speed == fm10k_bus_speed_2500 ? "2.5GT/s " :
2203 hw->bus_caps.speed == fm10k_bus_speed_5000 ? "5.0GT/s " :
2204 hw->bus_caps.speed == fm10k_bus_speed_8000 ? "8.0GT/s " : ""),
2205 hw->bus_caps.width);
2209 * fm10k_probe - Device Initialization Routine
2210 * @pdev: PCI device information struct
2211 * @ent: entry in fm10k_pci_tbl
2213 * Returns 0 on success, negative on failure
2215 * fm10k_probe initializes an interface identified by a pci_dev structure.
2216 * The OS initialization, configuring of the interface private structure,
2217 * and a hardware reset occur.
2219 static int fm10k_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2221 struct net_device *netdev;
2222 struct fm10k_intfc *interface;
2225 if (pdev->error_state != pci_channel_io_normal) {
2227 "PCI device still in an error state. Unable to load...\n");
2231 err = pci_enable_device_mem(pdev);
2234 "PCI enable device failed: %d\n", err);
2238 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));
2240 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2243 "DMA configuration failed: %d\n", err);
2247 err = pci_request_mem_regions(pdev, fm10k_driver_name);
2250 "pci_request_selected_regions failed: %d\n", err);
2254 pci_enable_pcie_error_reporting(pdev);
2256 pci_set_master(pdev);
2257 pci_save_state(pdev);
2259 netdev = fm10k_alloc_netdev(fm10k_info_tbl[ent->driver_data]);
2262 goto err_alloc_netdev;
2265 SET_NETDEV_DEV(netdev, &pdev->dev);
2267 interface = netdev_priv(netdev);
2268 pci_set_drvdata(pdev, interface);
2270 interface->netdev = netdev;
2271 interface->pdev = pdev;
2273 interface->uc_addr = ioremap(pci_resource_start(pdev, 0),
2274 FM10K_UC_ADDR_SIZE);
2275 if (!interface->uc_addr) {
2280 err = fm10k_sw_init(interface, ent);
2284 /* enable debugfs support */
2285 fm10k_dbg_intfc_init(interface);
2287 err = fm10k_init_queueing_scheme(interface);
2291 /* the mbx interrupt might attempt to schedule the service task, so we
2292 * must ensure it is disabled since we haven't yet requested the timer
2295 set_bit(__FM10K_SERVICE_DISABLE, interface->state);
2297 err = fm10k_mbx_request_irq(interface);
2299 goto err_mbx_interrupt;
2301 /* final check of hardware state before registering the interface */
2302 err = fm10k_hw_ready(interface);
2306 err = register_netdev(netdev);
2310 /* carrier off reporting is important to ethtool even BEFORE open */
2311 netif_carrier_off(netdev);
2313 /* stop all the transmit queues from transmitting until link is up */
2314 netif_tx_stop_all_queues(netdev);
2316 /* Initialize service timer and service task late in order to avoid
2319 timer_setup(&interface->service_timer, fm10k_service_timer, 0);
2320 INIT_WORK(&interface->service_task, fm10k_service_task);
2322 /* Setup the MAC/VLAN queue */
2323 INIT_DELAYED_WORK(&interface->macvlan_task, fm10k_macvlan_task);
2325 /* kick off service timer now, even when interface is down */
2326 mod_timer(&interface->service_timer, (HZ * 2) + jiffies);
2328 /* print warning for non-optimal configurations */
2329 fm10k_slot_warn(interface);
2331 /* report MAC address for logging */
2332 dev_info(&pdev->dev, "%pM\n", netdev->dev_addr);
2334 /* enable SR-IOV after registering netdev to enforce PF/VF ordering */
2335 fm10k_iov_configure(pdev, 0);
2337 /* clear the service task disable bit and kick off service task */
2338 clear_bit(__FM10K_SERVICE_DISABLE, interface->state);
2339 fm10k_service_event_schedule(interface);
2344 fm10k_mbx_free_irq(interface);
2346 fm10k_clear_queueing_scheme(interface);
2348 if (interface->sw_addr)
2349 iounmap(interface->sw_addr);
2350 iounmap(interface->uc_addr);
2352 free_netdev(netdev);
2354 pci_release_mem_regions(pdev);
2357 pci_disable_device(pdev);
2362 * fm10k_remove - Device Removal Routine
2363 * @pdev: PCI device information struct
2365 * fm10k_remove is called by the PCI subsystem to alert the driver
2366 * that it should release a PCI device. The could be caused by a
2367 * Hot-Plug event, or because the driver is going to be removed from
2370 static void fm10k_remove(struct pci_dev *pdev)
2372 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2373 struct net_device *netdev = interface->netdev;
2375 del_timer_sync(&interface->service_timer);
2377 fm10k_stop_service_event(interface);
2378 fm10k_stop_macvlan_task(interface);
2380 /* Remove all pending MAC/VLAN requests */
2381 fm10k_clear_macvlan_queue(interface, interface->glort, true);
2383 /* free netdev, this may bounce the interrupts due to setup_tc */
2384 if (netdev->reg_state == NETREG_REGISTERED)
2385 unregister_netdev(netdev);
2388 fm10k_iov_disable(pdev);
2390 /* disable mailbox interrupt */
2391 fm10k_mbx_free_irq(interface);
2393 /* free interrupts */
2394 fm10k_clear_queueing_scheme(interface);
2396 /* remove any debugfs interfaces */
2397 fm10k_dbg_intfc_exit(interface);
2399 if (interface->sw_addr)
2400 iounmap(interface->sw_addr);
2401 iounmap(interface->uc_addr);
2403 free_netdev(netdev);
2405 pci_release_mem_regions(pdev);
2407 pci_disable_pcie_error_reporting(pdev);
2409 pci_disable_device(pdev);
2412 static void fm10k_prepare_suspend(struct fm10k_intfc *interface)
2414 /* the watchdog task reads from registers, which might appear like
2415 * a surprise remove if the PCIe device is disabled while we're
2416 * stopped. We stop the watchdog task until after we resume software
2419 * Note that the MAC/VLAN task will be stopped as part of preparing
2420 * for reset so we don't need to handle it here.
2422 fm10k_stop_service_event(interface);
2424 if (fm10k_prepare_for_reset(interface))
2425 set_bit(__FM10K_RESET_SUSPENDED, interface->state);
2428 static int fm10k_handle_resume(struct fm10k_intfc *interface)
2430 struct fm10k_hw *hw = &interface->hw;
2433 /* Even if we didn't properly prepare for reset in
2434 * fm10k_prepare_suspend, we'll attempt to resume anyways.
2436 if (!test_and_clear_bit(__FM10K_RESET_SUSPENDED, interface->state))
2437 dev_warn(&interface->pdev->dev,
2438 "Device was shut down as part of suspend... Attempting to recover\n");
2440 /* reset statistics starting values */
2441 hw->mac.ops.rebind_hw_stats(hw, &interface->stats);
2443 err = fm10k_handle_reset(interface);
2447 /* assume host is not ready, to prevent race with watchdog in case we
2448 * actually don't have connection to the switch
2450 interface->host_ready = false;
2451 fm10k_watchdog_host_not_ready(interface);
2453 /* force link to stay down for a second to prevent link flutter */
2454 interface->link_down_event = jiffies + (HZ);
2455 set_bit(__FM10K_LINK_DOWN, interface->state);
2457 /* restart the service task */
2458 fm10k_start_service_event(interface);
2460 /* Restart the MAC/VLAN request queue in-case of outstanding events */
2461 fm10k_macvlan_schedule(interface);
2468 * fm10k_resume - Generic PM resume hook
2469 * @dev: generic device structure
2471 * Generic PM hook used when waking the device from a low power state after
2472 * suspend or hibernation. This function does not need to handle lower PCIe
2473 * device state as the stack takes care of that for us.
2475 static int fm10k_resume(struct device *dev)
2477 struct fm10k_intfc *interface = pci_get_drvdata(to_pci_dev(dev));
2478 struct net_device *netdev = interface->netdev;
2479 struct fm10k_hw *hw = &interface->hw;
2482 /* refresh hw_addr in case it was dropped */
2483 hw->hw_addr = interface->uc_addr;
2485 err = fm10k_handle_resume(interface);
2489 netif_device_attach(netdev);
2495 * fm10k_suspend - Generic PM suspend hook
2496 * @dev: generic device structure
2498 * Generic PM hook used when setting the device into a low power state for
2499 * system suspend or hibernation. This function does not need to handle lower
2500 * PCIe device state as the stack takes care of that for us.
2502 static int fm10k_suspend(struct device *dev)
2504 struct fm10k_intfc *interface = pci_get_drvdata(to_pci_dev(dev));
2505 struct net_device *netdev = interface->netdev;
2507 netif_device_detach(netdev);
2509 fm10k_prepare_suspend(interface);
2514 #endif /* CONFIG_PM */
2517 * fm10k_io_error_detected - called when PCI error is detected
2518 * @pdev: Pointer to PCI device
2519 * @state: The current pci connection state
2521 * This function is called after a PCI bus error affecting
2522 * this device has been detected.
2524 static pci_ers_result_t fm10k_io_error_detected(struct pci_dev *pdev,
2525 pci_channel_state_t state)
2527 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2528 struct net_device *netdev = interface->netdev;
2530 netif_device_detach(netdev);
2532 if (state == pci_channel_io_perm_failure)
2533 return PCI_ERS_RESULT_DISCONNECT;
2535 fm10k_prepare_suspend(interface);
2537 /* Request a slot reset. */
2538 return PCI_ERS_RESULT_NEED_RESET;
2542 * fm10k_io_slot_reset - called after the pci bus has been reset.
2543 * @pdev: Pointer to PCI device
2545 * Restart the card from scratch, as if from a cold-boot.
2547 static pci_ers_result_t fm10k_io_slot_reset(struct pci_dev *pdev)
2549 pci_ers_result_t result;
2551 if (pci_reenable_device(pdev)) {
2553 "Cannot re-enable PCI device after reset.\n");
2554 result = PCI_ERS_RESULT_DISCONNECT;
2556 pci_set_master(pdev);
2557 pci_restore_state(pdev);
2559 /* After second error pci->state_saved is false, this
2560 * resets it so EEH doesn't break.
2562 pci_save_state(pdev);
2564 pci_wake_from_d3(pdev, false);
2566 result = PCI_ERS_RESULT_RECOVERED;
2569 pci_cleanup_aer_uncorrect_error_status(pdev);
2575 * fm10k_io_resume - called when traffic can start flowing again.
2576 * @pdev: Pointer to PCI device
2578 * This callback is called when the error recovery driver tells us that
2579 * its OK to resume normal operation.
2581 static void fm10k_io_resume(struct pci_dev *pdev)
2583 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2584 struct net_device *netdev = interface->netdev;
2587 err = fm10k_handle_resume(interface);
2590 dev_warn(&pdev->dev,
2591 "%s failed: %d\n", __func__, err);
2593 netif_device_attach(netdev);
2597 * fm10k_io_reset_prepare - called when PCI function is about to be reset
2598 * @pdev: Pointer to PCI device
2600 * This callback is called when the PCI function is about to be reset,
2601 * allowing the device driver to prepare for it.
2603 static void fm10k_io_reset_prepare(struct pci_dev *pdev)
2605 /* warn incase we have any active VF devices */
2606 if (pci_num_vf(pdev))
2607 dev_warn(&pdev->dev,
2608 "PCIe FLR may cause issues for any active VF devices\n");
2609 fm10k_prepare_suspend(pci_get_drvdata(pdev));
2613 * fm10k_io_reset_done - called when PCI function has finished resetting
2614 * @pdev: Pointer to PCI device
2616 * This callback is called just after the PCI function is reset, such as via
2617 * /sys/class/net/<enpX>/device/reset or similar.
2619 static void fm10k_io_reset_done(struct pci_dev *pdev)
2621 struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2622 int err = fm10k_handle_resume(interface);
2625 dev_warn(&pdev->dev,
2626 "%s failed: %d\n", __func__, err);
2627 netif_device_detach(interface->netdev);
2631 static const struct pci_error_handlers fm10k_err_handler = {
2632 .error_detected = fm10k_io_error_detected,
2633 .slot_reset = fm10k_io_slot_reset,
2634 .resume = fm10k_io_resume,
2635 .reset_prepare = fm10k_io_reset_prepare,
2636 .reset_done = fm10k_io_reset_done,
2639 static SIMPLE_DEV_PM_OPS(fm10k_pm_ops, fm10k_suspend, fm10k_resume);
2641 static struct pci_driver fm10k_driver = {
2642 .name = fm10k_driver_name,
2643 .id_table = fm10k_pci_tbl,
2644 .probe = fm10k_probe,
2645 .remove = fm10k_remove,
2648 .pm = &fm10k_pm_ops,
2650 #endif /* CONFIG_PM */
2651 .sriov_configure = fm10k_iov_configure,
2652 .err_handler = &fm10k_err_handler
2656 * fm10k_register_pci_driver - register driver interface
2658 * This function is called on module load in order to register the driver.
2660 int fm10k_register_pci_driver(void)
2662 return pci_register_driver(&fm10k_driver);
2666 * fm10k_unregister_pci_driver - unregister driver interface
2668 * This function is called on module unload in order to remove the driver.
2670 void fm10k_unregister_pci_driver(void)
2672 pci_unregister_driver(&fm10k_driver);