]> Git Repo - linux.git/blob - drivers/net/ethernet/intel/fm10k/fm10k_pci.c
Merge branch 'smp-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / drivers / net / ethernet / intel / fm10k / fm10k_pci.c
1 /* Intel(R) Ethernet Switch Host Interface Driver
2  * Copyright(c) 2013 - 2017 Intel Corporation.
3  *
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.
7  *
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
11  * more details.
12  *
13  * The full GNU General Public License is included in this distribution in
14  * the file called "COPYING".
15  *
16  * Contact Information:
17  * e1000-devel Mailing List <[email protected]>
18  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
19  */
20
21 #include <linux/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/aer.h>
24
25 #include "fm10k.h"
26
27 static const struct fm10k_info *fm10k_info_tbl[] = {
28         [fm10k_device_pf] = &fm10k_pf_info,
29         [fm10k_device_vf] = &fm10k_vf_info,
30 };
31
32 /**
33  * fm10k_pci_tbl - PCI Device ID Table
34  *
35  * Wildcard entries (PCI_ANY_ID) should come last
36  * Last entry must be all 0s
37  *
38  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
39  *   Class, Class Mask, private data (not used) }
40  */
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 */
45         { 0, }
46 };
47 MODULE_DEVICE_TABLE(pci, fm10k_pci_tbl);
48
49 u16 fm10k_read_pci_cfg_word(struct fm10k_hw *hw, u32 reg)
50 {
51         struct fm10k_intfc *interface = hw->back;
52         u16 value = 0;
53
54         if (FM10K_REMOVED(hw->hw_addr))
55                 return ~value;
56
57         pci_read_config_word(interface->pdev, reg, &value);
58         if (value == 0xFFFF)
59                 fm10k_write_flush(hw);
60
61         return value;
62 }
63
64 u32 fm10k_read_reg(struct fm10k_hw *hw, int reg)
65 {
66         u32 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
67         u32 value = 0;
68
69         if (FM10K_REMOVED(hw_addr))
70                 return ~value;
71
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;
76
77                 hw->hw_addr = NULL;
78                 netif_device_detach(netdev);
79                 netdev_err(netdev, "PCIe link lost, device now detached\n");
80         }
81
82         return value;
83 }
84
85 static int fm10k_hw_ready(struct fm10k_intfc *interface)
86 {
87         struct fm10k_hw *hw = &interface->hw;
88
89         fm10k_write_flush(hw);
90
91         return FM10K_REMOVED(hw->hw_addr) ? -ENODEV : 0;
92 }
93
94 /**
95  * fm10k_macvlan_schedule - Schedule MAC/VLAN queue task
96  * @interface: fm10k private interface structure
97  *
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.
100  */
101 void fm10k_macvlan_schedule(struct fm10k_intfc *interface)
102 {
103         /* Avoid processing the MAC/VLAN queue when the service task is
104          * disabled, or when we're resetting the device.
105          */
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
112                  * between runs.
113                  */
114                 queue_delayed_work(fm10k_workqueue,
115                                    &interface->macvlan_task, 10);
116         } else {
117                 set_bit(__FM10K_MACVLAN_REQUEST, interface->state);
118         }
119 }
120
121 /**
122  * fm10k_stop_macvlan_task - Stop the MAC/VLAN queue monitor
123  * @interface: fm10k private interface structure
124  *
125  * Wait until the MAC/VLAN queue task has stopped, and cancel any future
126  * requests.
127  */
128 static void fm10k_stop_macvlan_task(struct fm10k_intfc *interface)
129 {
130         /* Disable the MAC/VLAN work item */
131         set_bit(__FM10K_MACVLAN_DISABLE, interface->state);
132
133         /* Make sure we waited until any current invocations have stopped */
134         cancel_delayed_work_sync(&interface->macvlan_task);
135
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
140          * gets unset.
141          */
142         clear_bit(__FM10K_MACVLAN_SCHED, interface->state);
143 }
144
145 /**
146  * fm10k_resume_macvlan_task - Restart the MAC/VLAN queue monitor
147  * @interface: fm10k private interface structure
148  *
149  * Clear the __FM10K_MACVLAN_DISABLE bit and, if a request occurred, schedule
150  * the MAC/VLAN work monitor.
151  */
152 static void fm10k_resume_macvlan_task(struct fm10k_intfc *interface)
153 {
154         /* Re-enable the MAC/VLAN work item */
155         clear_bit(__FM10K_MACVLAN_DISABLE, interface->state);
156
157         /* We might have received a MAC/VLAN request while disabled. If so,
158          * kick off the queue now.
159          */
160         if (test_bit(__FM10K_MACVLAN_REQUEST, interface->state))
161                 fm10k_macvlan_schedule(interface);
162 }
163
164 void fm10k_service_event_schedule(struct fm10k_intfc *interface)
165 {
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);
170         } else {
171                 set_bit(__FM10K_SERVICE_REQUEST, interface->state);
172         }
173 }
174
175 static void fm10k_service_event_complete(struct fm10k_intfc *interface)
176 {
177         WARN_ON(!test_bit(__FM10K_SERVICE_SCHED, interface->state));
178
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);
182
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
185          * next timer event.
186          */
187         if (test_bit(__FM10K_SERVICE_REQUEST, interface->state))
188                 fm10k_service_event_schedule(interface);
189 }
190
191 static void fm10k_stop_service_event(struct fm10k_intfc *interface)
192 {
193         set_bit(__FM10K_SERVICE_DISABLE, interface->state);
194         cancel_work_sync(&interface->service_task);
195
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
201          * restarted.
202          */
203         clear_bit(__FM10K_SERVICE_SCHED, interface->state);
204 }
205
206 static void fm10k_start_service_event(struct fm10k_intfc *interface)
207 {
208         clear_bit(__FM10K_SERVICE_DISABLE, interface->state);
209         fm10k_service_event_schedule(interface);
210 }
211
212 /**
213  * fm10k_service_timer - Timer Call-back
214  * @data: pointer to interface cast into an unsigned long
215  **/
216 static void fm10k_service_timer(struct timer_list *t)
217 {
218         struct fm10k_intfc *interface = from_timer(interface, t,
219                                                    service_timer);
220
221         /* Reset the timer */
222         mod_timer(&interface->service_timer, (HZ * 2) + jiffies);
223
224         fm10k_service_event_schedule(interface);
225 }
226
227 /**
228  * fm10k_prepare_for_reset - Prepare the driver and device for a pending reset
229  * @interface: fm10k private data structure
230  *
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.
234  */
235 static bool fm10k_prepare_for_reset(struct fm10k_intfc *interface)
236 {
237         struct net_device *netdev = interface->netdev;
238
239         WARN_ON(in_interrupt());
240
241         /* put off any impending NetWatchDogTimeout */
242         netif_trans_update(netdev);
243
244         /* Nothing to do if a reset is already in progress */
245         if (test_and_set_bit(__FM10K_RESETTING, interface->state))
246                 return false;
247
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
251          */
252         fm10k_stop_macvlan_task(interface);
253
254         rtnl_lock();
255
256         fm10k_iov_suspend(interface->pdev);
257
258         if (netif_running(netdev))
259                 fm10k_close(netdev);
260
261         fm10k_mbx_free_irq(interface);
262
263         /* free interrupts */
264         fm10k_clear_queueing_scheme(interface);
265
266         /* delay any future reset requests */
267         interface->last_reset = jiffies + (10 * HZ);
268
269         rtnl_unlock();
270
271         return true;
272 }
273
274 static int fm10k_handle_reset(struct fm10k_intfc *interface)
275 {
276         struct net_device *netdev = interface->netdev;
277         struct fm10k_hw *hw = &interface->hw;
278         int err;
279
280         WARN_ON(!test_bit(__FM10K_RESETTING, interface->state));
281
282         rtnl_lock();
283
284         pci_set_master(interface->pdev);
285
286         /* reset and initialize the hardware so it is in a known state */
287         err = hw->mac.ops.reset_hw(hw);
288         if (err) {
289                 dev_err(&interface->pdev->dev, "reset_hw failed: %d\n", err);
290                 goto reinit_err;
291         }
292
293         err = hw->mac.ops.init_hw(hw);
294         if (err) {
295                 dev_err(&interface->pdev->dev, "init_hw failed: %d\n", err);
296                 goto reinit_err;
297         }
298
299         err = fm10k_init_queueing_scheme(interface);
300         if (err) {
301                 dev_err(&interface->pdev->dev,
302                         "init_queueing_scheme failed: %d\n", err);
303                 goto reinit_err;
304         }
305
306         /* re-associate interrupts */
307         err = fm10k_mbx_request_irq(interface);
308         if (err)
309                 goto err_mbx_irq;
310
311         err = fm10k_hw_ready(interface);
312         if (err)
313                 goto err_open;
314
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;
322                 }
323
324                 if (hw->mac.vlan_override)
325                         netdev->features &= ~NETIF_F_HW_VLAN_CTAG_RX;
326                 else
327                         netdev->features |= NETIF_F_HW_VLAN_CTAG_RX;
328         }
329
330         err = netif_running(netdev) ? fm10k_open(netdev) : 0;
331         if (err)
332                 goto err_open;
333
334         fm10k_iov_resume(interface->pdev);
335
336         rtnl_unlock();
337
338         fm10k_resume_macvlan_task(interface);
339
340         clear_bit(__FM10K_RESETTING, interface->state);
341
342         return err;
343 err_open:
344         fm10k_mbx_free_irq(interface);
345 err_mbx_irq:
346         fm10k_clear_queueing_scheme(interface);
347 reinit_err:
348         netif_device_detach(netdev);
349
350         rtnl_unlock();
351
352         clear_bit(__FM10K_RESETTING, interface->state);
353
354         return err;
355 }
356
357 static void fm10k_detach_subtask(struct fm10k_intfc *interface)
358 {
359         struct net_device *netdev = interface->netdev;
360         u32 __iomem *hw_addr;
361         u32 value;
362         int err;
363
364         /* do nothing if netdev is still present or hw_addr is set */
365         if (netif_device_present(netdev) || interface->hw.hw_addr)
366                 return;
367
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.
372          */
373         if (fm10k_prepare_for_reset(interface))
374                 set_bit(__FM10K_RESET_DETACHED, interface->state);
375
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);
379         if (~value) {
380                 /* Make sure the reset was initiated because we detached,
381                  * otherwise we might race with a different reset flow.
382                  */
383                 if (!test_and_clear_bit(__FM10K_RESET_DETACHED,
384                                         interface->state))
385                         return;
386
387                 /* Restore the hardware address */
388                 interface->hw.hw_addr = interface->uc_addr;
389
390                 /* PCIe link has been restored, and the device is active
391                  * again. Restore everything and reset the device.
392                  */
393                 err = fm10k_handle_reset(interface);
394                 if (err) {
395                         netdev_err(netdev, "Unable to reset device: %d\n", err);
396                         interface->hw.hw_addr = NULL;
397                         return;
398                 }
399
400                 /* Re-attach the netdev */
401                 netif_device_attach(netdev);
402                 netdev_warn(netdev, "PCIe link restored, device now attached\n");
403                 return;
404         }
405 }
406
407 static void fm10k_reset_subtask(struct fm10k_intfc *interface)
408 {
409         int err;
410
411         if (!test_and_clear_bit(FM10K_FLAG_RESET_REQUESTED,
412                                 interface->flags))
413                 return;
414
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.
421          */
422         if (!fm10k_prepare_for_reset(interface))
423                 return;
424
425         netdev_err(interface->netdev, "Reset interface\n");
426
427         err = fm10k_handle_reset(interface);
428         if (err)
429                 dev_err(&interface->pdev->dev,
430                         "fm10k_handle_reset failed: %d\n", err);
431 }
432
433 /**
434  * fm10k_configure_swpri_map - Configure Receive SWPRI to PC mapping
435  * @interface: board private structure
436  *
437  * Configure the SWPRI to PC mapping for the port.
438  **/
439 static void fm10k_configure_swpri_map(struct fm10k_intfc *interface)
440 {
441         struct net_device *netdev = interface->netdev;
442         struct fm10k_hw *hw = &interface->hw;
443         int i;
444
445         /* clear flag indicating update is needed */
446         clear_bit(FM10K_FLAG_SWPRI_CONFIG, interface->flags);
447
448         /* these registers are only available on the PF */
449         if (hw->mac.type != fm10k_mac_pf)
450                 return;
451
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));
456 }
457
458 /**
459  * fm10k_watchdog_update_host_state - Update the link status based on host.
460  * @interface: board private structure
461  **/
462 static void fm10k_watchdog_update_host_state(struct fm10k_intfc *interface)
463 {
464         struct fm10k_hw *hw = &interface->hw;
465         s32 err;
466
467         if (test_bit(__FM10K_LINK_DOWN, interface->state)) {
468                 interface->host_ready = false;
469                 if (time_is_after_jiffies(interface->link_down_event))
470                         return;
471                 clear_bit(__FM10K_LINK_DOWN, interface->state);
472         }
473
474         if (test_bit(FM10K_FLAG_SWPRI_CONFIG, interface->flags)) {
475                 if (rtnl_trylock()) {
476                         fm10k_configure_swpri_map(interface);
477                         rtnl_unlock();
478                 }
479         }
480
481         /* lock the mailbox for transmit and receive */
482         fm10k_mbx_lock(interface);
483
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);
487
488         /* free the lock */
489         fm10k_mbx_unlock(interface);
490 }
491
492 /**
493  * fm10k_mbx_subtask - Process upstream and downstream mailboxes
494  * @interface: board private structure
495  *
496  * This function will process both the upstream and downstream mailboxes.
497  **/
498 static void fm10k_mbx_subtask(struct fm10k_intfc *interface)
499 {
500         /* If we're resetting, bail out */
501         if (test_bit(__FM10K_RESETTING, interface->state))
502                 return;
503
504         /* process upstream mailbox and update device state */
505         fm10k_watchdog_update_host_state(interface);
506
507         /* process downstream mailboxes */
508         fm10k_iov_mbx(interface);
509 }
510
511 /**
512  * fm10k_watchdog_host_is_ready - Update netdev status based on host ready
513  * @interface: board private structure
514  **/
515 static void fm10k_watchdog_host_is_ready(struct fm10k_intfc *interface)
516 {
517         struct net_device *netdev = interface->netdev;
518
519         /* only continue if link state is currently down */
520         if (netif_carrier_ok(netdev))
521                 return;
522
523         netif_info(interface, drv, netdev, "NIC Link is up\n");
524
525         netif_carrier_on(netdev);
526         netif_tx_wake_all_queues(netdev);
527 }
528
529 /**
530  * fm10k_watchdog_host_not_ready - Update netdev status based on host not ready
531  * @interface: board private structure
532  **/
533 static void fm10k_watchdog_host_not_ready(struct fm10k_intfc *interface)
534 {
535         struct net_device *netdev = interface->netdev;
536
537         /* only continue if link state is currently up */
538         if (!netif_carrier_ok(netdev))
539                 return;
540
541         netif_info(interface, drv, netdev, "NIC Link is down\n");
542
543         netif_carrier_off(netdev);
544         netif_tx_stop_all_queues(netdev);
545 }
546
547 /**
548  * fm10k_update_stats - Update the board statistics counters.
549  * @interface: board private structure
550  **/
551 void fm10k_update_stats(struct fm10k_intfc *interface)
552 {
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;
562         u64 bytes, pkts;
563         int i;
564
565         /* ensure only one thread updates stats at a time */
566         if (test_and_set_bit(__FM10K_UPDATING_STATS, interface->state))
567                 return;
568
569         /* do not allow stats update via service task for next second */
570         interface->next_stats_update = jiffies + HZ;
571
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]);
575
576                 if (!tx_ring)
577                         continue;
578
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;
585         }
586
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;
593
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]);
597
598                 if (!rx_ring)
599                         continue;
600
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;
612         }
613
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;
624
625         hw->mac.ops.update_hw_stats(hw, &interface->stats);
626
627         for (i = 0; i < hw->mac.max_queues; i++) {
628                 struct fm10k_hw_stats_q *q = &interface->stats.q[i];
629
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;
635         }
636
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;
642
643         /* Fill out the OS statistics structure */
644         net_stats->rx_errors = rx_errors;
645         net_stats->rx_dropped = interface->stats.nodesc_drop.count;
646
647         clear_bit(__FM10K_UPDATING_STATS, interface->state);
648 }
649
650 /**
651  * fm10k_watchdog_flush_tx - flush queues on host not ready
652  * @interface - pointer to the device interface structure
653  **/
654 static void fm10k_watchdog_flush_tx(struct fm10k_intfc *interface)
655 {
656         int some_tx_pending = 0;
657         int i;
658
659         /* nothing to do if carrier is up */
660         if (netif_carrier_ok(interface->netdev))
661                 return;
662
663         for (i = 0; i < interface->num_tx_queues; i++) {
664                 struct fm10k_ring *tx_ring = interface->tx_ring[i];
665
666                 if (tx_ring->next_to_use != tx_ring->next_to_clean) {
667                         some_tx_pending = 1;
668                         break;
669                 }
670         }
671
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.
675          */
676         if (some_tx_pending)
677                 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
678 }
679
680 /**
681  * fm10k_watchdog_subtask - check and bring link up
682  * @interface - pointer to the device interface structure
683  **/
684 static void fm10k_watchdog_subtask(struct fm10k_intfc *interface)
685 {
686         /* if interface is down do nothing */
687         if (test_bit(__FM10K_DOWN, interface->state) ||
688             test_bit(__FM10K_RESETTING, interface->state))
689                 return;
690
691         if (interface->host_ready)
692                 fm10k_watchdog_host_is_ready(interface);
693         else
694                 fm10k_watchdog_host_not_ready(interface);
695
696         /* update stats only once every second */
697         if (time_is_before_jiffies(interface->next_stats_update))
698                 fm10k_update_stats(interface);
699
700         /* flush any uncompleted work */
701         fm10k_watchdog_flush_tx(interface);
702 }
703
704 /**
705  * fm10k_check_hang_subtask - check for hung queues and dropped interrupts
706  * @interface - pointer to the device interface structure
707  *
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.
712  */
713 static void fm10k_check_hang_subtask(struct fm10k_intfc *interface)
714 {
715         int i;
716
717         /* If we're down or resetting, just bail */
718         if (test_bit(__FM10K_DOWN, interface->state) ||
719             test_bit(__FM10K_RESETTING, interface->state))
720                 return;
721
722         /* rate limit tx hang checks to only once every 2 seconds */
723         if (time_is_after_eq_jiffies(interface->next_tx_hang_check))
724                 return;
725         interface->next_tx_hang_check = jiffies + (2 * HZ);
726
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]);
731
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];
735
736                         if (!qv->tx.count && !qv->rx.count)
737                                 continue;
738                         writel(FM10K_ITR_ENABLE | FM10K_ITR_PENDING2, qv->itr);
739                 }
740         }
741 }
742
743 /**
744  * fm10k_service_task - manages and runs subtasks
745  * @work: pointer to work_struct containing our data
746  **/
747 static void fm10k_service_task(struct work_struct *work)
748 {
749         struct fm10k_intfc *interface;
750
751         interface = container_of(work, struct fm10k_intfc, service_task);
752
753         /* Check whether we're detached first */
754         fm10k_detach_subtask(interface);
755
756         /* tasks run even when interface is down */
757         fm10k_mbx_subtask(interface);
758         fm10k_reset_subtask(interface);
759
760         /* tasks only run when interface is up */
761         fm10k_watchdog_subtask(interface);
762         fm10k_check_hang_subtask(interface);
763
764         /* release lock on service events to allow scheduling next event */
765         fm10k_service_event_complete(interface);
766 }
767
768 /**
769  * fm10k_macvlan_task - send queued MAC/VLAN requests to switch manager
770  * @work: pointer to work_struct containing our data
771  *
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.
778  **/
779 static void fm10k_macvlan_task(struct work_struct *work)
780 {
781         struct fm10k_macvlan_request *item;
782         struct fm10k_intfc *interface;
783         struct delayed_work *dwork;
784         struct list_head *requests;
785         struct fm10k_hw *hw;
786         unsigned long flags;
787
788         dwork = to_delayed_work(work);
789         interface = container_of(dwork, struct fm10k_intfc, macvlan_task);
790         hw = &interface->hw;
791         requests = &interface->macvlan_requests;
792
793         do {
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,
798                                                 list);
799                 if (item)
800                         list_del_init(&item->list);
801
802                 spin_unlock_irqrestore(&interface->macvlan_lock, flags);
803
804                 /* We have no more items to process */
805                 if (!item)
806                         goto done;
807
808                 fm10k_mbx_lock(interface);
809
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.
814                  */
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);
819
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);
824                         break;
825                 }
826
827                 switch (item->type) {
828                 case FM10K_MC_MAC_REQUEST:
829                         hw->mac.ops.update_mc_addr(hw,
830                                                    item->mac.glort,
831                                                    item->mac.addr,
832                                                    item->mac.vid,
833                                                    item->set);
834                         break;
835                 case FM10K_UC_MAC_REQUEST:
836                         hw->mac.ops.update_uc_addr(hw,
837                                                    item->mac.glort,
838                                                    item->mac.addr,
839                                                    item->mac.vid,
840                                                    item->set,
841                                                    0);
842                         break;
843                 case FM10K_VLAN_REQUEST:
844                         hw->mac.ops.update_vlan(hw,
845                                                 item->vlan.vid,
846                                                 item->vlan.vsi,
847                                                 item->set);
848                         break;
849                 default:
850                         break;
851                 }
852
853                 fm10k_mbx_unlock(interface);
854
855                 /* Free the item now that we've sent the update */
856                 kfree(item);
857         } while (true);
858
859 done:
860         WARN_ON(!test_bit(__FM10K_MACVLAN_SCHED, interface->state));
861
862         /* flush memory to make sure state is correct */
863         smp_mb__before_atomic();
864         clear_bit(__FM10K_MACVLAN_SCHED, interface->state);
865
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
868          * no work to do.
869          */
870         if (test_bit(__FM10K_MACVLAN_REQUEST, interface->state))
871                 fm10k_macvlan_schedule(interface);
872 }
873
874 /**
875  * fm10k_configure_tx_ring - Configure Tx ring after Reset
876  * @interface: board private structure
877  * @ring: structure containing ring specific data
878  *
879  * Configure the Tx descriptor ring after a reset.
880  **/
881 static void fm10k_configure_tx_ring(struct fm10k_intfc *interface,
882                                     struct fm10k_ring *ring)
883 {
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;
890
891         /* disable queue to avoid issues while updating state */
892         fm10k_write_reg(hw, FM10K_TXDCTL(reg_idx), 0);
893         fm10k_write_flush(hw);
894
895         /* possible poll here to verify ring resources have been cleaned */
896
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);
901
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);
905
906         /* store tail pointer */
907         ring->tail = &interface->uc_addr[FM10K_TDT(reg_idx)];
908
909         /* reset ntu and ntc to place SW in sync with hardware */
910         ring->next_to_clean = 0;
911         ring->next_to_use = 0;
912
913         /* Map interrupt */
914         if (ring->q_vector) {
915                 txint = ring->q_vector->v_idx + NON_Q_VECTORS(hw);
916                 txint |= FM10K_INT_MAP_TIMER0;
917         }
918
919         fm10k_write_reg(hw, FM10K_TXINT(reg_idx), txint);
920
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);
924
925         /* Initialize XPS */
926         if (!test_and_set_bit(__FM10K_TX_XPS_INIT_DONE, ring->state) &&
927             ring->q_vector)
928                 netif_set_xps_queue(ring->netdev,
929                                     &ring->q_vector->affinity_mask,
930                                     ring->queue_index);
931
932         /* enable queue */
933         fm10k_write_reg(hw, FM10K_TXDCTL(reg_idx), txdctl);
934 }
935
936 /**
937  * fm10k_enable_tx_ring - Verify Tx ring is enabled after configuration
938  * @interface: board private structure
939  * @ring: structure containing ring specific data
940  *
941  * Verify the Tx descriptor ring is ready for transmit.
942  **/
943 static void fm10k_enable_tx_ring(struct fm10k_intfc *interface,
944                                  struct fm10k_ring *ring)
945 {
946         struct fm10k_hw *hw = &interface->hw;
947         int wait_loop = 10;
948         u32 txdctl;
949         u8 reg_idx = ring->reg_idx;
950
951         /* if we are already enabled just exit */
952         if (fm10k_read_reg(hw, FM10K_TXDCTL(reg_idx)) & FM10K_TXDCTL_ENABLE)
953                 return;
954
955         /* poll to verify queue is enabled */
956         do {
957                 usleep_range(1000, 2000);
958                 txdctl = fm10k_read_reg(hw, FM10K_TXDCTL(reg_idx));
959         } while (!(txdctl & FM10K_TXDCTL_ENABLE) && --wait_loop);
960         if (!wait_loop)
961                 netif_err(interface, drv, interface->netdev,
962                           "Could not enable Tx Queue %d\n", reg_idx);
963 }
964
965 /**
966  * fm10k_configure_tx - Configure Transmit Unit after Reset
967  * @interface: board private structure
968  *
969  * Configure the Tx unit of the MAC after a reset.
970  **/
971 static void fm10k_configure_tx(struct fm10k_intfc *interface)
972 {
973         int i;
974
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]);
978
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]);
982 }
983
984 /**
985  * fm10k_configure_rx_ring - Configure Rx ring after Reset
986  * @interface: board private structure
987  * @ring: structure containing ring specific data
988  *
989  * Configure the Rx descriptor ring after a reset.
990  **/
991 static void fm10k_configure_rx_ring(struct fm10k_intfc *interface,
992                                     struct fm10k_ring *ring)
993 {
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;
1002
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);
1008
1009         /* possible poll here to verify ring resources have been cleaned */
1010
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);
1015
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);
1019
1020         /* store tail pointer */
1021         ring->tail = &interface->uc_addr[FM10K_RDT(reg_idx)];
1022
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;
1027
1028         /* Configure the Rx buffer size for one buff without split */
1029         srrctl |= FM10K_RX_BUFSZ >> FM10K_SRRCTL_BSIZEPKT_SHIFT;
1030
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);
1034
1035         /* Enable drop on empty */
1036 #ifdef CONFIG_DCB
1037         if (interface->pfc_en)
1038                 rx_pause = interface->pfc_en;
1039 #endif
1040         if (!(rx_pause & BIT(ring->qos_pc)))
1041                 rxdctl |= FM10K_RXDCTL_DROP_ON_EMPTY;
1042
1043         fm10k_write_reg(hw, FM10K_RXDCTL(reg_idx), rxdctl);
1044
1045         /* assign default VLAN to queue */
1046         ring->vid = hw->mac.default_vid;
1047
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;
1051
1052         /* Map interrupt */
1053         if (ring->q_vector) {
1054                 rxint = ring->q_vector->v_idx + NON_Q_VECTORS(hw);
1055                 rxint |= FM10K_INT_MAP_TIMER1;
1056         }
1057
1058         fm10k_write_reg(hw, FM10K_RXINT(reg_idx), rxint);
1059
1060         /* enable queue */
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);
1064
1065         /* place buffers on ring for receive data */
1066         fm10k_alloc_rx_buffers(ring, fm10k_desc_unused(ring));
1067 }
1068
1069 /**
1070  * fm10k_update_rx_drop_en - Configures the drop enable bits for Rx rings
1071  * @interface: board private structure
1072  *
1073  * Configure the drop enable bits for the Rx rings.
1074  **/
1075 void fm10k_update_rx_drop_en(struct fm10k_intfc *interface)
1076 {
1077         struct fm10k_hw *hw = &interface->hw;
1078         u8 rx_pause = interface->rx_pause;
1079         int i;
1080
1081 #ifdef CONFIG_DCB
1082         if (interface->pfc_en)
1083                 rx_pause = interface->pfc_en;
1084
1085 #endif
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;
1090
1091                 if (!(rx_pause & BIT(ring->qos_pc)))
1092                         rxdctl |= FM10K_RXDCTL_DROP_ON_EMPTY;
1093
1094                 fm10k_write_reg(hw, FM10K_RXDCTL(reg_idx), rxdctl);
1095         }
1096 }
1097
1098 /**
1099  * fm10k_configure_dglort - Configure Receive DGLORT after reset
1100  * @interface: board private structure
1101  *
1102  * Configure the DGLORT description and RSS tables.
1103  **/
1104 static void fm10k_configure_dglort(struct fm10k_intfc *interface)
1105 {
1106         struct fm10k_dglort_cfg dglort = { 0 };
1107         struct fm10k_hw *hw = &interface->hw;
1108         int i;
1109         u32 mrqc;
1110
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]);
1114
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]);
1118
1119         /* Generate RSS hash based on packet types, TCP/UDP
1120          * port numbers and/or IPv4/v6 src and dst addresses
1121          */
1122         mrqc = FM10K_MRQC_IPV4 |
1123                FM10K_MRQC_TCP_IPV4 |
1124                FM10K_MRQC_IPV6 |
1125                FM10K_MRQC_TCP_IPV6;
1126
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;
1131
1132         fm10k_write_reg(hw, FM10K_MRQC(0), mrqc);
1133
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);
1139
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);
1148         }
1149
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);
1161 }
1162
1163 /**
1164  * fm10k_configure_rx - Configure Receive Unit after Reset
1165  * @interface: board private structure
1166  *
1167  * Configure the Rx unit of the MAC after a reset.
1168  **/
1169 static void fm10k_configure_rx(struct fm10k_intfc *interface)
1170 {
1171         int i;
1172
1173         /* Configure SWPRI to PC map */
1174         fm10k_configure_swpri_map(interface);
1175
1176         /* Configure RSS and DGLORT map */
1177         fm10k_configure_dglort(interface);
1178
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]);
1182
1183         /* possible poll here to verify that Rx rings are now enabled */
1184 }
1185
1186 static void fm10k_napi_enable_all(struct fm10k_intfc *interface)
1187 {
1188         struct fm10k_q_vector *q_vector;
1189         int q_idx;
1190
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);
1194         }
1195 }
1196
1197 static irqreturn_t fm10k_msix_clean_rings(int __always_unused irq, void *data)
1198 {
1199         struct fm10k_q_vector *q_vector = data;
1200
1201         if (q_vector->rx.count || q_vector->tx.count)
1202                 napi_schedule_irqoff(&q_vector->napi);
1203
1204         return IRQ_HANDLED;
1205 }
1206
1207 static irqreturn_t fm10k_msix_mbx_vf(int __always_unused irq, void *data)
1208 {
1209         struct fm10k_intfc *interface = data;
1210         struct fm10k_hw *hw = &interface->hw;
1211         struct fm10k_mbx_info *mbx = &hw->mbx;
1212
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) |
1216                         FM10K_ITR_ENABLE);
1217
1218         /* service upstream mailbox */
1219         if (fm10k_mbx_trylock(interface)) {
1220                 mbx->ops.process(hw, mbx);
1221                 fm10k_mbx_unlock(interface);
1222         }
1223
1224         hw->mac.get_host_state = true;
1225         fm10k_service_event_schedule(interface);
1226
1227         return IRQ_HANDLED;
1228 }
1229
1230 #ifdef CONFIG_NET_POLL_CONTROLLER
1231 /**
1232  *  fm10k_netpoll - A Polling 'interrupt' handler
1233  *  @netdev: network interface device structure
1234  *
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.
1237  **/
1238 void fm10k_netpoll(struct net_device *netdev)
1239 {
1240         struct fm10k_intfc *interface = netdev_priv(netdev);
1241         int i;
1242
1243         /* if interface is down do nothing */
1244         if (test_bit(__FM10K_DOWN, interface->state))
1245                 return;
1246
1247         for (i = 0; i < interface->num_q_vectors; i++)
1248                 fm10k_msix_clean_rings(0, interface->q_vector[i]);
1249 }
1250
1251 #endif
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)
1255 {
1256         struct pci_dev *pdev = interface->pdev;
1257         struct fm10k_hw *hw = &interface->hw;
1258         struct fm10k_iov_data *iov_data = interface->iov_data;
1259         char *error;
1260
1261         switch (type) {
1262         case FM10K_PCA_FAULT:
1263                 switch (fault->type) {
1264                 default:
1265                         error = "Unknown PCA error";
1266                         break;
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);
1274                 }
1275                 break;
1276         case FM10K_THI_FAULT:
1277                 switch (fault->type) {
1278                 default:
1279                         error = "Unknown THI error";
1280                         break;
1281                 FM10K_ERR_MSG(THI_NO_FAULT);
1282                 FM10K_ERR_MSG(THI_MAL_DIS_Q_FAULT);
1283                 }
1284                 break;
1285         case FM10K_FUM_FAULT:
1286                 switch (fault->type) {
1287                 default:
1288                         error = "Unknown FUM error";
1289                         break;
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);
1301                 }
1302                 break;
1303         default:
1304                 error = "Undocumented fault";
1305                 break;
1306         }
1307
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));
1312
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.
1321          */
1322         if (fault->func && iov_data) {
1323                 int vf = fault->func - 1;
1324                 struct fm10k_vf_info *vf_info = &iov_data->vf_info[vf];
1325
1326                 hw->iov.ops.reset_lport(hw, vf_info);
1327                 hw->iov.ops.reset_resources(hw, vf_info);
1328
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);
1332
1333                 /* reset_resources will disconnect from the mbx  */
1334                 vf_info->mbx.ops.connect(hw, &vf_info->mbx);
1335         }
1336 }
1337
1338 static void fm10k_report_fault(struct fm10k_intfc *interface, u32 eicr)
1339 {
1340         struct fm10k_hw *hw = &interface->hw;
1341         struct fm10k_fault fault = { 0 };
1342         int type, err;
1343
1344         for (eicr &= FM10K_EICR_FAULT_MASK, type = FM10K_PCA_FAULT;
1345              eicr;
1346              eicr >>= 1, type += FM10K_FAULT_SIZE) {
1347                 /* only check if there is an error reported */
1348                 if (!(eicr & 0x1))
1349                         continue;
1350
1351                 /* retrieve fault info */
1352                 err = hw->mac.ops.get_fault(hw, type, &fault);
1353                 if (err) {
1354                         dev_err(&interface->pdev->dev,
1355                                 "error reading fault\n");
1356                         continue;
1357                 }
1358
1359                 fm10k_handle_fault(interface, type, &fault);
1360         }
1361 }
1362
1363 static void fm10k_reset_drop_on_empty(struct fm10k_intfc *interface, u32 eicr)
1364 {
1365         struct fm10k_hw *hw = &interface->hw;
1366         const u32 rxdctl = FM10K_RXDCTL_WRITE_BACK_MIN_DELAY;
1367         u32 maxholdq;
1368         int q;
1369
1370         if (!(eicr & FM10K_EICR_MAXHOLDTIME))
1371                 return;
1372
1373         maxholdq = fm10k_read_reg(hw, FM10K_MAXHOLDQ(7));
1374         if (maxholdq)
1375                 fm10k_write_reg(hw, FM10K_MAXHOLDQ(7), maxholdq);
1376         for (q = 255;;) {
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);
1381                         } else {
1382                                 interface->rx_overrun_vf++;
1383                         }
1384                 }
1385
1386                 maxholdq *= 2;
1387                 if (!maxholdq)
1388                         q &= ~(32 - 1);
1389
1390                 if (!q)
1391                         break;
1392
1393                 if (q-- % 32)
1394                         continue;
1395
1396                 maxholdq = fm10k_read_reg(hw, FM10K_MAXHOLDQ(q / 32));
1397                 if (maxholdq)
1398                         fm10k_write_reg(hw, FM10K_MAXHOLDQ(q / 32), maxholdq);
1399         }
1400 }
1401
1402 static irqreturn_t fm10k_msix_mbx_pf(int __always_unused irq, void *data)
1403 {
1404         struct fm10k_intfc *interface = data;
1405         struct fm10k_hw *hw = &interface->hw;
1406         struct fm10k_mbx_info *mbx = &hw->mbx;
1407         u32 eicr;
1408         s32 err = 0;
1409
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));
1415
1416         /* report any faults found to the message log */
1417         fm10k_report_fault(interface, eicr);
1418
1419         /* reset any queues disabled due to receiver overrun */
1420         fm10k_reset_drop_on_empty(interface, eicr);
1421
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);
1428         }
1429
1430         if (err == FM10K_ERR_RESET_REQUESTED)
1431                 set_bit(FM10K_FLAG_RESET_REQUESTED, interface->flags);
1432
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);
1438
1439                 /* reset dglort_map back to no config */
1440                 hw->mac.dglort_map = FM10K_DGLORTMAP_NONE;
1441         }
1442
1443         /* we should validate host state after interrupt event */
1444         hw->mac.get_host_state = true;
1445
1446         /* validate host state, and handle VF mailboxes in the service task */
1447         fm10k_service_event_schedule(interface);
1448
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) |
1452                         FM10K_ITR_ENABLE);
1453
1454         return IRQ_HANDLED;
1455 }
1456
1457 void fm10k_mbx_free_irq(struct fm10k_intfc *interface)
1458 {
1459         struct fm10k_hw *hw = &interface->hw;
1460         struct msix_entry *entry;
1461         int itr_reg;
1462
1463         /* no mailbox IRQ to free if MSI-X is not enabled */
1464         if (!interface->msix_entries)
1465                 return;
1466
1467         entry = &interface->msix_entries[FM10K_MBX_VECTOR];
1468
1469         /* disconnect the mailbox */
1470         hw->mbx.ops.disconnect(hw, &hw->mbx);
1471
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);
1484         } else {
1485                 itr_reg = FM10K_VFITR(FM10K_MBX_VECTOR);
1486         }
1487
1488         fm10k_write_reg(hw, itr_reg, FM10K_ITR_MASK_SET);
1489
1490         free_irq(entry->vector, interface);
1491 }
1492
1493 static s32 fm10k_mbx_mac_addr(struct fm10k_hw *hw, u32 **results,
1494                               struct fm10k_mbx_info *mbx)
1495 {
1496         bool vlan_override = hw->mac.vlan_override;
1497         u16 default_vid = hw->mac.default_vid;
1498         struct fm10k_intfc *interface;
1499         s32 err;
1500
1501         err = fm10k_msg_mac_vlan_vf(hw, results, mbx);
1502         if (err)
1503                 return err;
1504
1505         interface = container_of(hw, struct fm10k_intfc, hw);
1506
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);
1511
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);
1516
1517         return 0;
1518 }
1519
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)
1523 {
1524         struct fm10k_intfc *interface;
1525         struct pci_dev *pdev;
1526
1527         interface = container_of(hw, struct fm10k_intfc, hw);
1528         pdev = interface->pdev;
1529
1530         dev_err(&pdev->dev, "Unknown message ID %u\n",
1531                 **results & FM10K_TLV_ID_MASK);
1532
1533         return 0;
1534 }
1535
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),
1541 };
1542
1543 static int fm10k_mbx_request_irq_vf(struct fm10k_intfc *interface)
1544 {
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;
1548         int err;
1549
1550         /* Use timer0 for interrupt moderation on the mailbox */
1551         u32 itr = entry->entry | FM10K_INT_MAP_TIMER0;
1552
1553         /* register mailbox handlers */
1554         err = hw->mbx.ops.register_handlers(&hw->mbx, vf_mbx_data);
1555         if (err)
1556                 return err;
1557
1558         /* request the IRQ */
1559         err = request_irq(entry->vector, fm10k_msix_mbx_vf, 0,
1560                           dev->name, interface);
1561         if (err) {
1562                 netif_err(interface, probe, dev,
1563                           "request_irq for msix_mbx failed: %d\n", err);
1564                 return err;
1565         }
1566
1567         /* map all of the interrupt sources */
1568         fm10k_write_reg(hw, FM10K_VFINT_MAP, itr);
1569
1570         /* enable interrupt */
1571         fm10k_write_reg(hw, FM10K_VFITR(entry->entry), FM10K_ITR_ENABLE);
1572
1573         return 0;
1574 }
1575
1576 static s32 fm10k_lport_map(struct fm10k_hw *hw, u32 **results,
1577                            struct fm10k_mbx_info *mbx)
1578 {
1579         struct fm10k_intfc *interface;
1580         u32 dglort_map = hw->mac.dglort_map;
1581         s32 err;
1582
1583         interface = container_of(hw, struct fm10k_intfc, hw);
1584
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);
1590
1591                 /* reset dglort_map back to no config */
1592                 hw->mac.dglort_map = FM10K_DGLORTMAP_NONE;
1593
1594                 fm10k_service_event_schedule(interface);
1595
1596                 /* prevent overloading kernel message buffer */
1597                 if (interface->lport_map_failed)
1598                         return 0;
1599
1600                 interface->lport_map_failed = true;
1601
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",
1607                          hw->swapi.status);
1608
1609                 return 0;
1610         }
1611
1612         err = fm10k_msg_lport_map_pf(hw, results, mbx);
1613         if (err)
1614                 return err;
1615
1616         interface->lport_map_failed = false;
1617
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);
1621
1622         return 0;
1623 }
1624
1625 static s32 fm10k_update_pvid(struct fm10k_hw *hw, u32 **results,
1626                              struct fm10k_mbx_info __always_unused *mbx)
1627 {
1628         struct fm10k_intfc *interface;
1629         u16 glort, pvid;
1630         u32 pvid_update;
1631         s32 err;
1632
1633         err = fm10k_tlv_attr_get_u32(results[FM10K_PF_ATTR_ID_UPDATE_PVID],
1634                                      &pvid_update);
1635         if (err)
1636                 return err;
1637
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);
1641
1642         /* if glort is not valid return error */
1643         if (!fm10k_glort_valid_pf(hw, glort))
1644                 return FM10K_ERR_PARAM;
1645
1646         /* verify VLAN ID is valid */
1647         if (pvid >= FM10K_VLAN_TABLE_VID_MAX)
1648                 return FM10K_ERR_PARAM;
1649
1650         interface = container_of(hw, struct fm10k_intfc, hw);
1651
1652         /* check to see if this belongs to one of the VFs */
1653         err = fm10k_iov_update_pvid(interface, glort, pvid);
1654         if (!err)
1655                 return 0;
1656
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);
1660
1661         hw->mac.default_vid = pvid;
1662
1663         return 0;
1664 }
1665
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),
1674 };
1675
1676 static int fm10k_mbx_request_irq_pf(struct fm10k_intfc *interface)
1677 {
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;
1681         int err;
1682
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;
1686
1687         /* register mailbox handlers */
1688         err = hw->mbx.ops.register_handlers(&hw->mbx, pf_mbx_data);
1689         if (err)
1690                 return err;
1691
1692         /* request the IRQ */
1693         err = request_irq(entry->vector, fm10k_msix_mbx_pf, 0,
1694                           dev->name, interface);
1695         if (err) {
1696                 netif_err(interface, probe, dev,
1697                           "request_irq for msix_mbx failed: %d\n", err);
1698                 return err;
1699         }
1700
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);
1707
1708         /* Enable interrupts w/ moderation for mailbox */
1709         fm10k_write_reg(hw, FM10K_INT_MAP(fm10k_int_mailbox), mbx_itr);
1710
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));
1720
1721         /* enable interrupt */
1722         fm10k_write_reg(hw, FM10K_ITR(entry->entry), FM10K_ITR_ENABLE);
1723
1724         return 0;
1725 }
1726
1727 int fm10k_mbx_request_irq(struct fm10k_intfc *interface)
1728 {
1729         struct fm10k_hw *hw = &interface->hw;
1730         int err;
1731
1732         /* enable Mailbox cause */
1733         if (hw->mac.type == fm10k_mac_pf)
1734                 err = fm10k_mbx_request_irq_pf(interface);
1735         else
1736                 err = fm10k_mbx_request_irq_vf(interface);
1737         if (err)
1738                 return err;
1739
1740         /* connect mailbox */
1741         err = hw->mbx.ops.connect(hw, &hw->mbx);
1742
1743         /* if the mailbox failed to connect, then free IRQ */
1744         if (err)
1745                 fm10k_mbx_free_irq(interface);
1746
1747         return err;
1748 }
1749
1750 /**
1751  * fm10k_qv_free_irq - release interrupts associated with queue vectors
1752  * @interface: board private structure
1753  *
1754  * Release all interrupts associated with this interface
1755  **/
1756 void fm10k_qv_free_irq(struct fm10k_intfc *interface)
1757 {
1758         int vector = interface->num_q_vectors;
1759         struct fm10k_hw *hw = &interface->hw;
1760         struct msix_entry *entry;
1761
1762         entry = &interface->msix_entries[NON_Q_VECTORS(hw) + vector];
1763
1764         while (vector) {
1765                 struct fm10k_q_vector *q_vector;
1766
1767                 vector--;
1768                 entry--;
1769                 q_vector = interface->q_vector[vector];
1770
1771                 if (!q_vector->tx.count && !q_vector->rx.count)
1772                         continue;
1773
1774                 /* clear the affinity_mask in the IRQ descriptor */
1775                 irq_set_affinity_hint(entry->vector, NULL);
1776
1777                 /* disable interrupts */
1778                 writel(FM10K_ITR_MASK_SET, q_vector->itr);
1779
1780                 free_irq(entry->vector, q_vector);
1781         }
1782 }
1783
1784 /**
1785  * fm10k_qv_request_irq - initialize interrupts for queue vectors
1786  * @interface: board private structure
1787  *
1788  * Attempts to configure interrupts using the best available
1789  * capabilities of the hardware and kernel.
1790  **/
1791 int fm10k_qv_request_irq(struct fm10k_intfc *interface)
1792 {
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;
1797         int vector, err;
1798
1799         entry = &interface->msix_entries[NON_Q_VECTORS(hw)];
1800
1801         for (vector = 0; vector < interface->num_q_vectors; vector++) {
1802                 struct fm10k_q_vector *q_vector = interface->q_vector[vector];
1803
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++);
1808                         ti++;
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++);
1815                 } else {
1816                         /* skip this unused q_vector */
1817                         continue;
1818                 }
1819
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)];
1824
1825                 /* request the IRQ */
1826                 err = request_irq(entry->vector, &fm10k_msix_clean_rings, 0,
1827                                   q_vector->name, q_vector);
1828                 if (err) {
1829                         netif_err(interface, probe, dev,
1830                                   "request_irq failed for MSIX interrupt Error: %d\n",
1831                                   err);
1832                         goto err_out;
1833                 }
1834
1835                 /* assign the mask for this irq */
1836                 irq_set_affinity_hint(entry->vector, &q_vector->affinity_mask);
1837
1838                 /* Enable q_vector */
1839                 writel(FM10K_ITR_ENABLE, q_vector->itr);
1840
1841                 entry++;
1842         }
1843
1844         return 0;
1845
1846 err_out:
1847         /* wind through the ring freeing all entries and vectors */
1848         while (vector) {
1849                 struct fm10k_q_vector *q_vector;
1850
1851                 entry--;
1852                 vector--;
1853                 q_vector = interface->q_vector[vector];
1854
1855                 if (!q_vector->tx.count && !q_vector->rx.count)
1856                         continue;
1857
1858                 /* clear the affinity_mask in the IRQ descriptor */
1859                 irq_set_affinity_hint(entry->vector, NULL);
1860
1861                 /* disable interrupts */
1862                 writel(FM10K_ITR_MASK_SET, q_vector->itr);
1863
1864                 free_irq(entry->vector, q_vector);
1865         }
1866
1867         return err;
1868 }
1869
1870 void fm10k_up(struct fm10k_intfc *interface)
1871 {
1872         struct fm10k_hw *hw = &interface->hw;
1873
1874         /* Enable Tx/Rx DMA */
1875         hw->mac.ops.start_hw(hw);
1876
1877         /* configure Tx descriptor rings */
1878         fm10k_configure_tx(interface);
1879
1880         /* configure Rx descriptor rings */
1881         fm10k_configure_rx(interface);
1882
1883         /* configure interrupts */
1884         hw->mac.ops.update_int_moderator(hw);
1885
1886         /* enable statistics capture again */
1887         clear_bit(__FM10K_UPDATING_STATS, interface->state);
1888
1889         /* clear down bit to indicate we are ready to go */
1890         clear_bit(__FM10K_DOWN, interface->state);
1891
1892         /* enable polling cleanups */
1893         fm10k_napi_enable_all(interface);
1894
1895         /* re-establish Rx filters */
1896         fm10k_restore_rx_state(interface);
1897
1898         /* enable transmits */
1899         netif_tx_start_all_queues(interface->netdev);
1900
1901         /* kick off the service timer now */
1902         hw->mac.get_host_state = true;
1903         mod_timer(&interface->service_timer, jiffies);
1904 }
1905
1906 static void fm10k_napi_disable_all(struct fm10k_intfc *interface)
1907 {
1908         struct fm10k_q_vector *q_vector;
1909         int q_idx;
1910
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);
1914         }
1915 }
1916
1917 void fm10k_down(struct fm10k_intfc *interface)
1918 {
1919         struct net_device *netdev = interface->netdev;
1920         struct fm10k_hw *hw = &interface->hw;
1921         int err, i = 0, count = 0;
1922
1923         /* signal that we are down to the interrupt handler and service task */
1924         if (test_and_set_bit(__FM10K_DOWN, interface->state))
1925                 return;
1926
1927         /* call carrier off first to avoid false dev_watchdog timeouts */
1928         netif_carrier_off(netdev);
1929
1930         /* disable transmits */
1931         netif_tx_stop_all_queues(netdev);
1932         netif_tx_disable(netdev);
1933
1934         /* reset Rx filters */
1935         fm10k_reset_rx_state(interface);
1936
1937         /* disable polling routines */
1938         fm10k_napi_disable_all(interface);
1939
1940         /* capture stats one last time before stopping interface */
1941         fm10k_update_stats(interface);
1942
1943         /* prevent updating statistics while we're down */
1944         while (test_and_set_bit(__FM10K_UPDATING_STATS, interface->state))
1945                 usleep_range(1000, 2000);
1946
1947         /* skip waiting for TX DMA if we lost PCIe link */
1948         if (FM10K_REMOVED(hw->hw_addr))
1949                 goto skip_tx_dma_drain;
1950
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.
1958          */
1959         err = hw->mac.ops.stop_hw(hw);
1960         if (err != FM10K_ERR_REQUESTS_PENDING)
1961                 goto skip_tx_dma_drain;
1962
1963 #define TX_DMA_DRAIN_RETRIES 25
1964         for (count = 0; count < TX_DMA_DRAIN_RETRIES; count++) {
1965                 usleep_range(10000, 20000);
1966
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))
1970                                 break;
1971
1972                 /* if all the queues are drained, we can break now */
1973                 if (i == interface->num_tx_queues)
1974                         break;
1975         }
1976
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",
1980                         count);
1981 skip_tx_dma_drain:
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");
1987         else if (err)
1988                 dev_err(&interface->pdev->dev, "stop_hw failed: %d\n", err);
1989
1990         /* free any buffers still on the rings */
1991         fm10k_clean_all_tx_rings(interface);
1992         fm10k_clean_all_rx_rings(interface);
1993 }
1994
1995 /**
1996  * fm10k_sw_init - Initialize general software structures
1997  * @interface: host interface private structure to initialize
1998  *
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).
2002  **/
2003 static int fm10k_sw_init(struct fm10k_intfc *interface,
2004                          const struct pci_device_id *ent)
2005 {
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];
2011         unsigned int rss;
2012         int err;
2013
2014         /* initialize back pointer */
2015         hw->back = interface;
2016         hw->hw_addr = interface->uc_addr;
2017
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;
2024
2025         /* Setup hw api */
2026         memcpy(&hw->mac.ops, fi->mac_ops, sizeof(hw->mac.ops));
2027         hw->mac.type = fi->mac;
2028
2029         /* Setup IOV handlers */
2030         if (fi->iov_ops)
2031                 memcpy(&hw->iov.ops, fi->iov_ops, sizeof(hw->iov.ops));
2032
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);
2037
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);
2041
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));
2045
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;
2050         }
2051
2052         /* reset and initialize the hardware so it is in a known state */
2053         err = hw->mac.ops.reset_hw(hw);
2054         if (err) {
2055                 dev_err(&pdev->dev, "reset_hw failed: %d\n", err);
2056                 return err;
2057         }
2058
2059         err = hw->mac.ops.init_hw(hw);
2060         if (err) {
2061                 dev_err(&pdev->dev, "init_hw failed: %d\n", err);
2062                 return err;
2063         }
2064
2065         /* initialize hardware statistics */
2066         hw->mac.ops.update_hw_stats(hw, &interface->stats);
2067
2068         /* Set upper limit on IOV VFs that can be allocated */
2069         pci_sriov_set_totalvfs(pdev, hw->iov.total_vfs);
2070
2071         /* Start with random Ethernet address */
2072         eth_random_addr(hw->mac.addr);
2073
2074         /* Initialize MAC address from hardware */
2075         err = hw->mac.ops.read_mac_addr(hw);
2076         if (err) {
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;
2081         }
2082
2083         ether_addr_copy(netdev->dev_addr, hw->mac.addr);
2084         ether_addr_copy(netdev->perm_addr, hw->mac.addr);
2085
2086         if (!is_valid_ether_addr(netdev->perm_addr)) {
2087                 dev_err(&pdev->dev, "Invalid MAC Address\n");
2088                 return -EIO;
2089         }
2090
2091         /* initialize DCBNL interface */
2092         fm10k_dcbnl_set_ops(netdev);
2093
2094         /* set default ring sizes */
2095         interface->tx_ring_count = FM10K_DEFAULT_TXD;
2096         interface->rx_ring_count = FM10K_DEFAULT_RXD;
2097
2098         /* set default interrupt moderation */
2099         interface->tx_itr = FM10K_TX_ITR_DEFAULT;
2100         interface->rx_itr = FM10K_ITR_ADAPTIVE | FM10K_RX_ITR_DEFAULT;
2101
2102         /* initialize udp port lists */
2103         INIT_LIST_HEAD(&interface->vxlan_port);
2104         INIT_LIST_HEAD(&interface->geneve_port);
2105
2106         /* Initialize the MAC/VLAN queue */
2107         INIT_LIST_HEAD(&interface->macvlan_requests);
2108
2109         netdev_rss_key_fill(rss_key, sizeof(rss_key));
2110         memcpy(interface->rssrk, rss_key, sizeof(rss_key));
2111
2112         /* Initialize the mailbox lock */
2113         spin_lock_init(&interface->mbx_lock);
2114         spin_lock_init(&interface->macvlan_lock);
2115
2116         /* Start off interface as being down */
2117         set_bit(__FM10K_DOWN, interface->state);
2118         set_bit(__FM10K_UPDATING_STATS, interface->state);
2119
2120         return 0;
2121 }
2122
2123 static void fm10k_slot_warn(struct fm10k_intfc *interface)
2124 {
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;
2129
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");
2134                 return;
2135         }
2136
2137         switch (speed) {
2138         case PCIE_SPEED_2_5GT:
2139                 /* 8b/10b encoding reduces max throughput by 20% */
2140                 max_gts = 2 * width;
2141                 break;
2142         case PCIE_SPEED_5_0GT:
2143                 /* 8b/10b encoding reduces max throughput by 20% */
2144                 max_gts = 4 * width;
2145                 break;
2146         case PCIE_SPEED_8_0GT:
2147                 /* 128b/130b encoding has less than 2% impact on throughput */
2148                 max_gts = 8 * width;
2149                 break;
2150         default:
2151                 dev_warn(&interface->pdev->dev,
2152                          "Unable to determine PCI Express bandwidth.\n");
2153                 return;
2154         }
2155
2156         dev_info(&interface->pdev->dev,
2157                  "PCI Express bandwidth of %dGT/s available\n",
2158                  max_gts);
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" :
2164                   "Unknown"),
2165                  hw->bus.width,
2166                  (speed == PCIE_SPEED_2_5GT ? "20%" :
2167                   speed == PCIE_SPEED_5_0GT ? "20%" :
2168                   speed == PCIE_SPEED_8_0GT ? "<2%" :
2169                   "Unknown"),
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" :
2173                   "Unknown"));
2174
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;
2179                 break;
2180         case fm10k_bus_speed_5000:
2181                 /* 8b/10b encoding reduces max throughput by 20% */
2182                 expected_gts = 4 * hw->bus_caps.width;
2183                 break;
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;
2187                 break;
2188         default:
2189                 dev_warn(&interface->pdev->dev,
2190                          "Unable to determine expected PCI Express bandwidth.\n");
2191                 return;
2192         }
2193
2194         if (max_gts >= expected_gts)
2195                 return;
2196
2197         dev_warn(&interface->pdev->dev,
2198                  "This device requires %dGT/s of bandwidth for optimal performance.\n",
2199                  expected_gts);
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);
2206 }
2207
2208 /**
2209  * fm10k_probe - Device Initialization Routine
2210  * @pdev: PCI device information struct
2211  * @ent: entry in fm10k_pci_tbl
2212  *
2213  * Returns 0 on success, negative on failure
2214  *
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.
2218  **/
2219 static int fm10k_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
2220 {
2221         struct net_device *netdev;
2222         struct fm10k_intfc *interface;
2223         int err;
2224
2225         if (pdev->error_state != pci_channel_io_normal) {
2226                 dev_err(&pdev->dev,
2227                         "PCI device still in an error state. Unable to load...\n");
2228                 return -EIO;
2229         }
2230
2231         err = pci_enable_device_mem(pdev);
2232         if (err) {
2233                 dev_err(&pdev->dev,
2234                         "PCI enable device failed: %d\n", err);
2235                 return err;
2236         }
2237
2238         err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(48));
2239         if (err)
2240                 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
2241         if (err) {
2242                 dev_err(&pdev->dev,
2243                         "DMA configuration failed: %d\n", err);
2244                 goto err_dma;
2245         }
2246
2247         err = pci_request_mem_regions(pdev, fm10k_driver_name);
2248         if (err) {
2249                 dev_err(&pdev->dev,
2250                         "pci_request_selected_regions failed: %d\n", err);
2251                 goto err_pci_reg;
2252         }
2253
2254         pci_enable_pcie_error_reporting(pdev);
2255
2256         pci_set_master(pdev);
2257         pci_save_state(pdev);
2258
2259         netdev = fm10k_alloc_netdev(fm10k_info_tbl[ent->driver_data]);
2260         if (!netdev) {
2261                 err = -ENOMEM;
2262                 goto err_alloc_netdev;
2263         }
2264
2265         SET_NETDEV_DEV(netdev, &pdev->dev);
2266
2267         interface = netdev_priv(netdev);
2268         pci_set_drvdata(pdev, interface);
2269
2270         interface->netdev = netdev;
2271         interface->pdev = pdev;
2272
2273         interface->uc_addr = ioremap(pci_resource_start(pdev, 0),
2274                                      FM10K_UC_ADDR_SIZE);
2275         if (!interface->uc_addr) {
2276                 err = -EIO;
2277                 goto err_ioremap;
2278         }
2279
2280         err = fm10k_sw_init(interface, ent);
2281         if (err)
2282                 goto err_sw_init;
2283
2284         /* enable debugfs support */
2285         fm10k_dbg_intfc_init(interface);
2286
2287         err = fm10k_init_queueing_scheme(interface);
2288         if (err)
2289                 goto err_sw_init;
2290
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
2293          * or work item.
2294          */
2295         set_bit(__FM10K_SERVICE_DISABLE, interface->state);
2296
2297         err = fm10k_mbx_request_irq(interface);
2298         if (err)
2299                 goto err_mbx_interrupt;
2300
2301         /* final check of hardware state before registering the interface */
2302         err = fm10k_hw_ready(interface);
2303         if (err)
2304                 goto err_register;
2305
2306         err = register_netdev(netdev);
2307         if (err)
2308                 goto err_register;
2309
2310         /* carrier off reporting is important to ethtool even BEFORE open */
2311         netif_carrier_off(netdev);
2312
2313         /* stop all the transmit queues from transmitting until link is up */
2314         netif_tx_stop_all_queues(netdev);
2315
2316         /* Initialize service timer and service task late in order to avoid
2317          * cleanup issues.
2318          */
2319         timer_setup(&interface->service_timer, fm10k_service_timer, 0);
2320         INIT_WORK(&interface->service_task, fm10k_service_task);
2321
2322         /* Setup the MAC/VLAN queue */
2323         INIT_DELAYED_WORK(&interface->macvlan_task, fm10k_macvlan_task);
2324
2325         /* kick off service timer now, even when interface is down */
2326         mod_timer(&interface->service_timer, (HZ * 2) + jiffies);
2327
2328         /* print warning for non-optimal configurations */
2329         fm10k_slot_warn(interface);
2330
2331         /* report MAC address for logging */
2332         dev_info(&pdev->dev, "%pM\n", netdev->dev_addr);
2333
2334         /* enable SR-IOV after registering netdev to enforce PF/VF ordering */
2335         fm10k_iov_configure(pdev, 0);
2336
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);
2340
2341         return 0;
2342
2343 err_register:
2344         fm10k_mbx_free_irq(interface);
2345 err_mbx_interrupt:
2346         fm10k_clear_queueing_scheme(interface);
2347 err_sw_init:
2348         if (interface->sw_addr)
2349                 iounmap(interface->sw_addr);
2350         iounmap(interface->uc_addr);
2351 err_ioremap:
2352         free_netdev(netdev);
2353 err_alloc_netdev:
2354         pci_release_mem_regions(pdev);
2355 err_pci_reg:
2356 err_dma:
2357         pci_disable_device(pdev);
2358         return err;
2359 }
2360
2361 /**
2362  * fm10k_remove - Device Removal Routine
2363  * @pdev: PCI device information struct
2364  *
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
2368  * memory.
2369  **/
2370 static void fm10k_remove(struct pci_dev *pdev)
2371 {
2372         struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2373         struct net_device *netdev = interface->netdev;
2374
2375         del_timer_sync(&interface->service_timer);
2376
2377         fm10k_stop_service_event(interface);
2378         fm10k_stop_macvlan_task(interface);
2379
2380         /* Remove all pending MAC/VLAN requests */
2381         fm10k_clear_macvlan_queue(interface, interface->glort, true);
2382
2383         /* free netdev, this may bounce the interrupts due to setup_tc */
2384         if (netdev->reg_state == NETREG_REGISTERED)
2385                 unregister_netdev(netdev);
2386
2387         /* release VFs */
2388         fm10k_iov_disable(pdev);
2389
2390         /* disable mailbox interrupt */
2391         fm10k_mbx_free_irq(interface);
2392
2393         /* free interrupts */
2394         fm10k_clear_queueing_scheme(interface);
2395
2396         /* remove any debugfs interfaces */
2397         fm10k_dbg_intfc_exit(interface);
2398
2399         if (interface->sw_addr)
2400                 iounmap(interface->sw_addr);
2401         iounmap(interface->uc_addr);
2402
2403         free_netdev(netdev);
2404
2405         pci_release_mem_regions(pdev);
2406
2407         pci_disable_pcie_error_reporting(pdev);
2408
2409         pci_disable_device(pdev);
2410 }
2411
2412 static void fm10k_prepare_suspend(struct fm10k_intfc *interface)
2413 {
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
2417          * activity.
2418          *
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.
2421          */
2422         fm10k_stop_service_event(interface);
2423
2424         if (fm10k_prepare_for_reset(interface))
2425                 set_bit(__FM10K_RESET_SUSPENDED, interface->state);
2426 }
2427
2428 static int fm10k_handle_resume(struct fm10k_intfc *interface)
2429 {
2430         struct fm10k_hw *hw = &interface->hw;
2431         int err;
2432
2433         /* Even if we didn't properly prepare for reset in
2434          * fm10k_prepare_suspend, we'll attempt to resume anyways.
2435          */
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");
2439
2440         /* reset statistics starting values */
2441         hw->mac.ops.rebind_hw_stats(hw, &interface->stats);
2442
2443         err = fm10k_handle_reset(interface);
2444         if (err)
2445                 return err;
2446
2447         /* assume host is not ready, to prevent race with watchdog in case we
2448          * actually don't have connection to the switch
2449          */
2450         interface->host_ready = false;
2451         fm10k_watchdog_host_not_ready(interface);
2452
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);
2456
2457         /* restart the service task */
2458         fm10k_start_service_event(interface);
2459
2460         /* Restart the MAC/VLAN request queue in-case of outstanding events */
2461         fm10k_macvlan_schedule(interface);
2462
2463         return err;
2464 }
2465
2466 #ifdef CONFIG_PM
2467 /**
2468  * fm10k_resume - Generic PM resume hook
2469  * @dev: generic device structure
2470  *
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.
2474  **/
2475 static int fm10k_resume(struct device *dev)
2476 {
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;
2480         int err;
2481
2482         /* refresh hw_addr in case it was dropped */
2483         hw->hw_addr = interface->uc_addr;
2484
2485         err = fm10k_handle_resume(interface);
2486         if (err)
2487                 return err;
2488
2489         netif_device_attach(netdev);
2490
2491         return 0;
2492 }
2493
2494 /**
2495  * fm10k_suspend - Generic PM suspend hook
2496  * @dev: generic device structure
2497  *
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.
2501  **/
2502 static int fm10k_suspend(struct device *dev)
2503 {
2504         struct fm10k_intfc *interface = pci_get_drvdata(to_pci_dev(dev));
2505         struct net_device *netdev = interface->netdev;
2506
2507         netif_device_detach(netdev);
2508
2509         fm10k_prepare_suspend(interface);
2510
2511         return 0;
2512 }
2513
2514 #endif /* CONFIG_PM */
2515
2516 /**
2517  * fm10k_io_error_detected - called when PCI error is detected
2518  * @pdev: Pointer to PCI device
2519  * @state: The current pci connection state
2520  *
2521  * This function is called after a PCI bus error affecting
2522  * this device has been detected.
2523  */
2524 static pci_ers_result_t fm10k_io_error_detected(struct pci_dev *pdev,
2525                                                 pci_channel_state_t state)
2526 {
2527         struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2528         struct net_device *netdev = interface->netdev;
2529
2530         netif_device_detach(netdev);
2531
2532         if (state == pci_channel_io_perm_failure)
2533                 return PCI_ERS_RESULT_DISCONNECT;
2534
2535         fm10k_prepare_suspend(interface);
2536
2537         /* Request a slot reset. */
2538         return PCI_ERS_RESULT_NEED_RESET;
2539 }
2540
2541 /**
2542  * fm10k_io_slot_reset - called after the pci bus has been reset.
2543  * @pdev: Pointer to PCI device
2544  *
2545  * Restart the card from scratch, as if from a cold-boot.
2546  */
2547 static pci_ers_result_t fm10k_io_slot_reset(struct pci_dev *pdev)
2548 {
2549         pci_ers_result_t result;
2550
2551         if (pci_reenable_device(pdev)) {
2552                 dev_err(&pdev->dev,
2553                         "Cannot re-enable PCI device after reset.\n");
2554                 result = PCI_ERS_RESULT_DISCONNECT;
2555         } else {
2556                 pci_set_master(pdev);
2557                 pci_restore_state(pdev);
2558
2559                 /* After second error pci->state_saved is false, this
2560                  * resets it so EEH doesn't break.
2561                  */
2562                 pci_save_state(pdev);
2563
2564                 pci_wake_from_d3(pdev, false);
2565
2566                 result = PCI_ERS_RESULT_RECOVERED;
2567         }
2568
2569         pci_cleanup_aer_uncorrect_error_status(pdev);
2570
2571         return result;
2572 }
2573
2574 /**
2575  * fm10k_io_resume - called when traffic can start flowing again.
2576  * @pdev: Pointer to PCI device
2577  *
2578  * This callback is called when the error recovery driver tells us that
2579  * its OK to resume normal operation.
2580  */
2581 static void fm10k_io_resume(struct pci_dev *pdev)
2582 {
2583         struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2584         struct net_device *netdev = interface->netdev;
2585         int err;
2586
2587         err = fm10k_handle_resume(interface);
2588
2589         if (err)
2590                 dev_warn(&pdev->dev,
2591                          "%s failed: %d\n", __func__, err);
2592         else
2593                 netif_device_attach(netdev);
2594 }
2595
2596 /**
2597  * fm10k_io_reset_prepare - called when PCI function is about to be reset
2598  * @pdev: Pointer to PCI device
2599  *
2600  * This callback is called when the PCI function is about to be reset,
2601  * allowing the device driver to prepare for it.
2602  */
2603 static void fm10k_io_reset_prepare(struct pci_dev *pdev)
2604 {
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));
2610 }
2611
2612 /**
2613  * fm10k_io_reset_done - called when PCI function has finished resetting
2614  * @pdev: Pointer to PCI device
2615  *
2616  * This callback is called just after the PCI function is reset, such as via
2617  * /sys/class/net/<enpX>/device/reset or similar.
2618  */
2619 static void fm10k_io_reset_done(struct pci_dev *pdev)
2620 {
2621         struct fm10k_intfc *interface = pci_get_drvdata(pdev);
2622         int err = fm10k_handle_resume(interface);
2623
2624         if (err) {
2625                 dev_warn(&pdev->dev,
2626                          "%s failed: %d\n", __func__, err);
2627                 netif_device_detach(interface->netdev);
2628         }
2629 }
2630
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,
2637 };
2638
2639 static SIMPLE_DEV_PM_OPS(fm10k_pm_ops, fm10k_suspend, fm10k_resume);
2640
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,
2646 #ifdef CONFIG_PM
2647         .driver = {
2648                 .pm             = &fm10k_pm_ops,
2649         },
2650 #endif /* CONFIG_PM */
2651         .sriov_configure        = fm10k_iov_configure,
2652         .err_handler            = &fm10k_err_handler
2653 };
2654
2655 /**
2656  * fm10k_register_pci_driver - register driver interface
2657  *
2658  * This function is called on module load in order to register the driver.
2659  **/
2660 int fm10k_register_pci_driver(void)
2661 {
2662         return pci_register_driver(&fm10k_driver);
2663 }
2664
2665 /**
2666  * fm10k_unregister_pci_driver - unregister driver interface
2667  *
2668  * This function is called on module unload in order to remove the driver.
2669  **/
2670 void fm10k_unregister_pci_driver(void)
2671 {
2672         pci_unregister_driver(&fm10k_driver);
2673 }
This page took 0.190369 seconds and 4 git commands to generate.