]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/sfc/efx_common.c
sfc: move MCDI logging device attribute
[linux.git] / drivers / net / ethernet / sfc / efx_common.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2018 Solarflare Communications Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published
8  * by the Free Software Foundation, incorporated herein by reference.
9  */
10
11 #include "net_driver.h"
12 #include <linux/module.h>
13 #include <linux/netdevice.h>
14 #include "efx_common.h"
15 #include "efx_channels.h"
16 #include "efx.h"
17 #include "mcdi.h"
18 #include "selftest.h"
19 #include "rx_common.h"
20 #include "tx_common.h"
21 #include "nic.h"
22 #include "io.h"
23 #include "mcdi_pcol.h"
24
25 static unsigned int debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
26                              NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
27                              NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
28                              NETIF_MSG_TX_ERR | NETIF_MSG_HW);
29 module_param(debug, uint, 0);
30 MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
31
32 /* This is the time (in jiffies) between invocations of the hardware
33  * monitor.
34  * On Falcon-based NICs, this will:
35  * - Check the on-board hardware monitor;
36  * - Poll the link state and reconfigure the hardware as necessary.
37  * On Siena-based NICs for power systems with EEH support, this will give EEH a
38  * chance to start.
39  */
40 static unsigned int efx_monitor_interval = 1 * HZ;
41
42 /* How often and how many times to poll for a reset while waiting for a
43  * BIST that another function started to complete.
44  */
45 #define BIST_WAIT_DELAY_MS      100
46 #define BIST_WAIT_DELAY_COUNT   100
47
48 /* Default stats update time */
49 #define STATS_PERIOD_MS_DEFAULT 1000
50
51 const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
52 const char *const efx_reset_type_names[] = {
53         [RESET_TYPE_INVISIBLE]          = "INVISIBLE",
54         [RESET_TYPE_ALL]                = "ALL",
55         [RESET_TYPE_RECOVER_OR_ALL]     = "RECOVER_OR_ALL",
56         [RESET_TYPE_WORLD]              = "WORLD",
57         [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
58         [RESET_TYPE_DATAPATH]           = "DATAPATH",
59         [RESET_TYPE_MC_BIST]            = "MC_BIST",
60         [RESET_TYPE_DISABLE]            = "DISABLE",
61         [RESET_TYPE_TX_WATCHDOG]        = "TX_WATCHDOG",
62         [RESET_TYPE_INT_ERROR]          = "INT_ERROR",
63         [RESET_TYPE_DMA_ERROR]          = "DMA_ERROR",
64         [RESET_TYPE_TX_SKIP]            = "TX_SKIP",
65         [RESET_TYPE_MC_FAILURE]         = "MC_FAILURE",
66         [RESET_TYPE_MCDI_TIMEOUT]       = "MCDI_TIMEOUT (FLR)",
67 };
68
69 #define RESET_TYPE(type) \
70         STRING_TABLE_LOOKUP(type, efx_reset_type)
71
72 /* Loopback mode names (see LOOPBACK_MODE()) */
73 const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
74 const char *const efx_loopback_mode_names[] = {
75         [LOOPBACK_NONE]         = "NONE",
76         [LOOPBACK_DATA]         = "DATAPATH",
77         [LOOPBACK_GMAC]         = "GMAC",
78         [LOOPBACK_XGMII]        = "XGMII",
79         [LOOPBACK_XGXS]         = "XGXS",
80         [LOOPBACK_XAUI]         = "XAUI",
81         [LOOPBACK_GMII]         = "GMII",
82         [LOOPBACK_SGMII]        = "SGMII",
83         [LOOPBACK_XGBR]         = "XGBR",
84         [LOOPBACK_XFI]          = "XFI",
85         [LOOPBACK_XAUI_FAR]     = "XAUI_FAR",
86         [LOOPBACK_GMII_FAR]     = "GMII_FAR",
87         [LOOPBACK_SGMII_FAR]    = "SGMII_FAR",
88         [LOOPBACK_XFI_FAR]      = "XFI_FAR",
89         [LOOPBACK_GPHY]         = "GPHY",
90         [LOOPBACK_PHYXS]        = "PHYXS",
91         [LOOPBACK_PCS]          = "PCS",
92         [LOOPBACK_PMAPMD]       = "PMA/PMD",
93         [LOOPBACK_XPORT]        = "XPORT",
94         [LOOPBACK_XGMII_WS]     = "XGMII_WS",
95         [LOOPBACK_XAUI_WS]      = "XAUI_WS",
96         [LOOPBACK_XAUI_WS_FAR]  = "XAUI_WS_FAR",
97         [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
98         [LOOPBACK_GMII_WS]      = "GMII_WS",
99         [LOOPBACK_XFI_WS]       = "XFI_WS",
100         [LOOPBACK_XFI_WS_FAR]   = "XFI_WS_FAR",
101         [LOOPBACK_PHYXS_WS]     = "PHYXS_WS",
102 };
103
104 /* Reset workqueue. If any NIC has a hardware failure then a reset will be
105  * queued onto this work queue. This is not a per-nic work queue, because
106  * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
107  */
108 static struct workqueue_struct *reset_workqueue;
109
110 int efx_create_reset_workqueue(void)
111 {
112         reset_workqueue = create_singlethread_workqueue("sfc_reset");
113         if (!reset_workqueue) {
114                 printk(KERN_ERR "Failed to create reset workqueue\n");
115                 return -ENOMEM;
116         }
117
118         return 0;
119 }
120
121 void efx_queue_reset_work(struct efx_nic *efx)
122 {
123         queue_work(reset_workqueue, &efx->reset_work);
124 }
125
126 void efx_flush_reset_workqueue(struct efx_nic *efx)
127 {
128         cancel_work_sync(&efx->reset_work);
129 }
130
131 void efx_destroy_reset_workqueue(void)
132 {
133         if (reset_workqueue) {
134                 destroy_workqueue(reset_workqueue);
135                 reset_workqueue = NULL;
136         }
137 }
138
139 /* We assume that efx->type->reconfigure_mac will always try to sync RX
140  * filters and therefore needs to read-lock the filter table against freeing
141  */
142 void efx_mac_reconfigure(struct efx_nic *efx)
143 {
144         if (efx->type->reconfigure_mac) {
145                 down_read(&efx->filter_sem);
146                 efx->type->reconfigure_mac(efx);
147                 up_read(&efx->filter_sem);
148         }
149 }
150
151 /* Asynchronous work item for changing MAC promiscuity and multicast
152  * hash.  Avoid a drain/rx_ingress enable by reconfiguring the current
153  * MAC directly.
154  */
155 static void efx_mac_work(struct work_struct *data)
156 {
157         struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
158
159         mutex_lock(&efx->mac_lock);
160         if (efx->port_enabled)
161                 efx_mac_reconfigure(efx);
162         mutex_unlock(&efx->mac_lock);
163 }
164
165 /* This ensures that the kernel is kept informed (via
166  * netif_carrier_on/off) of the link status, and also maintains the
167  * link status's stop on the port's TX queue.
168  */
169 void efx_link_status_changed(struct efx_nic *efx)
170 {
171         struct efx_link_state *link_state = &efx->link_state;
172
173         /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
174          * that no events are triggered between unregister_netdev() and the
175          * driver unloading. A more general condition is that NETDEV_CHANGE
176          * can only be generated between NETDEV_UP and NETDEV_DOWN
177          */
178         if (!netif_running(efx->net_dev))
179                 return;
180
181         if (link_state->up != netif_carrier_ok(efx->net_dev)) {
182                 efx->n_link_state_changes++;
183
184                 if (link_state->up)
185                         netif_carrier_on(efx->net_dev);
186                 else
187                         netif_carrier_off(efx->net_dev);
188         }
189
190         /* Status message for kernel log */
191         if (link_state->up)
192                 netif_info(efx, link, efx->net_dev,
193                            "link up at %uMbps %s-duplex (MTU %d)\n",
194                            link_state->speed, link_state->fd ? "full" : "half",
195                            efx->net_dev->mtu);
196         else
197                 netif_info(efx, link, efx->net_dev, "link down\n");
198 }
199
200 /**************************************************************************
201  *
202  * Hardware monitor
203  *
204  **************************************************************************/
205
206 /* Run periodically off the general workqueue */
207 static void efx_monitor(struct work_struct *data)
208 {
209         struct efx_nic *efx = container_of(data, struct efx_nic,
210                                            monitor_work.work);
211
212         netif_vdbg(efx, timer, efx->net_dev,
213                    "hardware monitor executing on CPU %d\n",
214                    raw_smp_processor_id());
215         BUG_ON(efx->type->monitor == NULL);
216
217         /* If the mac_lock is already held then it is likely a port
218          * reconfiguration is already in place, which will likely do
219          * most of the work of monitor() anyway.
220          */
221         if (mutex_trylock(&efx->mac_lock)) {
222                 if (efx->port_enabled && efx->type->monitor)
223                         efx->type->monitor(efx);
224                 mutex_unlock(&efx->mac_lock);
225         }
226
227         efx_start_monitor(efx);
228 }
229
230 void efx_start_monitor(struct efx_nic *efx)
231 {
232         if (efx->type->monitor)
233                 queue_delayed_work(efx->workqueue, &efx->monitor_work,
234                                    efx_monitor_interval);
235 }
236
237 /**************************************************************************
238  *
239  * Event queue processing
240  *
241  *************************************************************************/
242
243 /* Channels are shutdown and reinitialised whilst the NIC is running
244  * to propagate configuration changes (mtu, checksum offload), or
245  * to clear hardware error conditions
246  */
247 static void efx_start_datapath(struct efx_nic *efx)
248 {
249         netdev_features_t old_features = efx->net_dev->features;
250         bool old_rx_scatter = efx->rx_scatter;
251         size_t rx_buf_len;
252
253         /* Calculate the rx buffer allocation parameters required to
254          * support the current MTU, including padding for header
255          * alignment and overruns.
256          */
257         efx->rx_dma_len = (efx->rx_prefix_size +
258                            EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
259                            efx->type->rx_buffer_padding);
260         rx_buf_len = (sizeof(struct efx_rx_page_state) + XDP_PACKET_HEADROOM +
261                       efx->rx_ip_align + efx->rx_dma_len);
262         if (rx_buf_len <= PAGE_SIZE) {
263                 efx->rx_scatter = efx->type->always_rx_scatter;
264                 efx->rx_buffer_order = 0;
265         } else if (efx->type->can_rx_scatter) {
266                 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
267                 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
268                              2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
269                                        EFX_RX_BUF_ALIGNMENT) >
270                              PAGE_SIZE);
271                 efx->rx_scatter = true;
272                 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
273                 efx->rx_buffer_order = 0;
274         } else {
275                 efx->rx_scatter = false;
276                 efx->rx_buffer_order = get_order(rx_buf_len);
277         }
278
279         efx_rx_config_page_split(efx);
280         if (efx->rx_buffer_order)
281                 netif_dbg(efx, drv, efx->net_dev,
282                           "RX buf len=%u; page order=%u batch=%u\n",
283                           efx->rx_dma_len, efx->rx_buffer_order,
284                           efx->rx_pages_per_batch);
285         else
286                 netif_dbg(efx, drv, efx->net_dev,
287                           "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
288                           efx->rx_dma_len, efx->rx_page_buf_step,
289                           efx->rx_bufs_per_page, efx->rx_pages_per_batch);
290
291         /* Restore previously fixed features in hw_features and remove
292          * features which are fixed now
293          */
294         efx->net_dev->hw_features |= efx->net_dev->features;
295         efx->net_dev->hw_features &= ~efx->fixed_features;
296         efx->net_dev->features |= efx->fixed_features;
297         if (efx->net_dev->features != old_features)
298                 netdev_features_change(efx->net_dev);
299
300         /* RX filters may also have scatter-enabled flags */
301         if ((efx->rx_scatter != old_rx_scatter) &&
302             efx->type->filter_update_rx_scatter)
303                 efx->type->filter_update_rx_scatter(efx);
304
305         /* We must keep at least one descriptor in a TX ring empty.
306          * We could avoid this when the queue size does not exactly
307          * match the hardware ring size, but it's not that important.
308          * Therefore we stop the queue when one more skb might fill
309          * the ring completely.  We wake it when half way back to
310          * empty.
311          */
312         efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
313         efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
314
315         /* Initialise the channels */
316         efx_start_channels(efx);
317
318         efx_ptp_start_datapath(efx);
319
320         if (netif_device_present(efx->net_dev))
321                 netif_tx_wake_all_queues(efx->net_dev);
322 }
323
324 static void efx_stop_datapath(struct efx_nic *efx)
325 {
326         EFX_ASSERT_RESET_SERIALISED(efx);
327         BUG_ON(efx->port_enabled);
328
329         efx_ptp_stop_datapath(efx);
330
331         efx_stop_channels(efx);
332 }
333
334 /**************************************************************************
335  *
336  * Port handling
337  *
338  **************************************************************************/
339
340 static void efx_start_port(struct efx_nic *efx)
341 {
342         netif_dbg(efx, ifup, efx->net_dev, "start port\n");
343         BUG_ON(efx->port_enabled);
344
345         mutex_lock(&efx->mac_lock);
346         efx->port_enabled = true;
347
348         /* Ensure MAC ingress/egress is enabled */
349         efx_mac_reconfigure(efx);
350
351         mutex_unlock(&efx->mac_lock);
352 }
353
354 /* Cancel work for MAC reconfiguration, periodic hardware monitoring
355  * and the async self-test, wait for them to finish and prevent them
356  * being scheduled again.  This doesn't cover online resets, which
357  * should only be cancelled when removing the device.
358  */
359 static void efx_stop_port(struct efx_nic *efx)
360 {
361         netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
362
363         EFX_ASSERT_RESET_SERIALISED(efx);
364
365         mutex_lock(&efx->mac_lock);
366         efx->port_enabled = false;
367         mutex_unlock(&efx->mac_lock);
368
369         /* Serialise against efx_set_multicast_list() */
370         netif_addr_lock_bh(efx->net_dev);
371         netif_addr_unlock_bh(efx->net_dev);
372
373         cancel_delayed_work_sync(&efx->monitor_work);
374         efx_selftest_async_cancel(efx);
375         cancel_work_sync(&efx->mac_work);
376 }
377
378 /* If the interface is supposed to be running but is not, start
379  * the hardware and software data path, regular activity for the port
380  * (MAC statistics, link polling, etc.) and schedule the port to be
381  * reconfigured.  Interrupts must already be enabled.  This function
382  * is safe to call multiple times, so long as the NIC is not disabled.
383  * Requires the RTNL lock.
384  */
385 void efx_start_all(struct efx_nic *efx)
386 {
387         EFX_ASSERT_RESET_SERIALISED(efx);
388         BUG_ON(efx->state == STATE_DISABLED);
389
390         /* Check that it is appropriate to restart the interface. All
391          * of these flags are safe to read under just the rtnl lock
392          */
393         if (efx->port_enabled || !netif_running(efx->net_dev) ||
394             efx->reset_pending)
395                 return;
396
397         efx_start_port(efx);
398         efx_start_datapath(efx);
399
400         /* Start the hardware monitor if there is one */
401         efx_start_monitor(efx);
402
403         /* Link state detection is normally event-driven; we have
404          * to poll now because we could have missed a change
405          */
406         mutex_lock(&efx->mac_lock);
407         if (efx->phy_op->poll(efx))
408                 efx_link_status_changed(efx);
409         mutex_unlock(&efx->mac_lock);
410
411         if (efx->type->start_stats) {
412                 efx->type->start_stats(efx);
413                 efx->type->pull_stats(efx);
414                 spin_lock_bh(&efx->stats_lock);
415                 efx->type->update_stats(efx, NULL, NULL);
416                 spin_unlock_bh(&efx->stats_lock);
417         }
418 }
419
420 /* Quiesce the hardware and software data path, and regular activity
421  * for the port without bringing the link down.  Safe to call multiple
422  * times with the NIC in almost any state, but interrupts should be
423  * enabled.  Requires the RTNL lock.
424  */
425 void efx_stop_all(struct efx_nic *efx)
426 {
427         EFX_ASSERT_RESET_SERIALISED(efx);
428
429         /* port_enabled can be read safely under the rtnl lock */
430         if (!efx->port_enabled)
431                 return;
432
433         if (efx->type->update_stats) {
434                 /* update stats before we go down so we can accurately count
435                  * rx_nodesc_drops
436                  */
437                 efx->type->pull_stats(efx);
438                 spin_lock_bh(&efx->stats_lock);
439                 efx->type->update_stats(efx, NULL, NULL);
440                 spin_unlock_bh(&efx->stats_lock);
441                 efx->type->stop_stats(efx);
442         }
443
444         efx_stop_port(efx);
445
446         /* Stop the kernel transmit interface.  This is only valid if
447          * the device is stopped or detached; otherwise the watchdog
448          * may fire immediately.
449          */
450         WARN_ON(netif_running(efx->net_dev) &&
451                 netif_device_present(efx->net_dev));
452         netif_tx_disable(efx->net_dev);
453
454         efx_stop_datapath(efx);
455 }
456
457 /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
458  * the MAC appropriately. All other PHY configuration changes are pushed
459  * through phy_op->set_settings(), and pushed asynchronously to the MAC
460  * through efx_monitor().
461  *
462  * Callers must hold the mac_lock
463  */
464 int __efx_reconfigure_port(struct efx_nic *efx)
465 {
466         enum efx_phy_mode phy_mode;
467         int rc = 0;
468
469         WARN_ON(!mutex_is_locked(&efx->mac_lock));
470
471         /* Disable PHY transmit in mac level loopbacks */
472         phy_mode = efx->phy_mode;
473         if (LOOPBACK_INTERNAL(efx))
474                 efx->phy_mode |= PHY_MODE_TX_DISABLED;
475         else
476                 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
477
478         if (efx->type->reconfigure_port)
479                 rc = efx->type->reconfigure_port(efx);
480
481         if (rc)
482                 efx->phy_mode = phy_mode;
483
484         return rc;
485 }
486
487 /* Reinitialise the MAC to pick up new PHY settings, even if the port is
488  * disabled.
489  */
490 int efx_reconfigure_port(struct efx_nic *efx)
491 {
492         int rc;
493
494         EFX_ASSERT_RESET_SERIALISED(efx);
495
496         mutex_lock(&efx->mac_lock);
497         rc = __efx_reconfigure_port(efx);
498         mutex_unlock(&efx->mac_lock);
499
500         return rc;
501 }
502
503 /**************************************************************************
504  *
505  * Device reset and suspend
506  *
507  **************************************************************************/
508
509 static void efx_wait_for_bist_end(struct efx_nic *efx)
510 {
511         int i;
512
513         for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
514                 if (efx_mcdi_poll_reboot(efx))
515                         goto out;
516                 msleep(BIST_WAIT_DELAY_MS);
517         }
518
519         netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
520 out:
521         /* Either way unset the BIST flag. If we found no reboot we probably
522          * won't recover, but we should try.
523          */
524         efx->mc_bist_for_other_fn = false;
525 }
526
527 /* Try recovery mechanisms.
528  * For now only EEH is supported.
529  * Returns 0 if the recovery mechanisms are unsuccessful.
530  * Returns a non-zero value otherwise.
531  */
532 int efx_try_recovery(struct efx_nic *efx)
533 {
534 #ifdef CONFIG_EEH
535         /* A PCI error can occur and not be seen by EEH because nothing
536          * happens on the PCI bus. In this case the driver may fail and
537          * schedule a 'recover or reset', leading to this recovery handler.
538          * Manually call the eeh failure check function.
539          */
540         struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
541         if (eeh_dev_check_failure(eehdev)) {
542                 /* The EEH mechanisms will handle the error and reset the
543                  * device if necessary.
544                  */
545                 return 1;
546         }
547 #endif
548         return 0;
549 }
550
551 /* Tears down the entire software state and most of the hardware state
552  * before reset.
553  */
554 void efx_reset_down(struct efx_nic *efx, enum reset_type method)
555 {
556         EFX_ASSERT_RESET_SERIALISED(efx);
557
558         if (method == RESET_TYPE_MCDI_TIMEOUT)
559                 efx->type->prepare_flr(efx);
560
561         efx_stop_all(efx);
562         efx_disable_interrupts(efx);
563
564         mutex_lock(&efx->mac_lock);
565         down_write(&efx->filter_sem);
566         mutex_lock(&efx->rss_lock);
567         if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
568             method != RESET_TYPE_DATAPATH)
569                 efx->phy_op->fini(efx);
570         efx->type->fini(efx);
571 }
572
573 /* This function will always ensure that the locks acquired in
574  * efx_reset_down() are released. A failure return code indicates
575  * that we were unable to reinitialise the hardware, and the
576  * driver should be disabled. If ok is false, then the rx and tx
577  * engines are not restarted, pending a RESET_DISABLE.
578  */
579 int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
580 {
581         int rc;
582
583         EFX_ASSERT_RESET_SERIALISED(efx);
584
585         if (method == RESET_TYPE_MCDI_TIMEOUT)
586                 efx->type->finish_flr(efx);
587
588         /* Ensure that SRAM is initialised even if we're disabling the device */
589         rc = efx->type->init(efx);
590         if (rc) {
591                 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
592                 goto fail;
593         }
594
595         if (!ok)
596                 goto fail;
597
598         if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
599             method != RESET_TYPE_DATAPATH) {
600                 rc = efx->phy_op->init(efx);
601                 if (rc)
602                         goto fail;
603                 rc = efx->phy_op->reconfigure(efx);
604                 if (rc && rc != -EPERM)
605                         netif_err(efx, drv, efx->net_dev,
606                                   "could not restore PHY settings\n");
607         }
608
609         rc = efx_enable_interrupts(efx);
610         if (rc)
611                 goto fail;
612
613 #ifdef CONFIG_SFC_SRIOV
614         rc = efx->type->vswitching_restore(efx);
615         if (rc) /* not fatal; the PF will still work fine */
616                 netif_warn(efx, probe, efx->net_dev,
617                            "failed to restore vswitching rc=%d;"
618                            " VFs may not function\n", rc);
619 #endif
620
621         if (efx->type->rx_restore_rss_contexts)
622                 efx->type->rx_restore_rss_contexts(efx);
623         mutex_unlock(&efx->rss_lock);
624         efx->type->filter_table_restore(efx);
625         up_write(&efx->filter_sem);
626         if (efx->type->sriov_reset)
627                 efx->type->sriov_reset(efx);
628
629         mutex_unlock(&efx->mac_lock);
630
631         efx_start_all(efx);
632
633         if (efx->type->udp_tnl_push_ports)
634                 efx->type->udp_tnl_push_ports(efx);
635
636         return 0;
637
638 fail:
639         efx->port_initialized = false;
640
641         mutex_unlock(&efx->rss_lock);
642         up_write(&efx->filter_sem);
643         mutex_unlock(&efx->mac_lock);
644
645         return rc;
646 }
647
648 /* Reset the NIC using the specified method.  Note that the reset may
649  * fail, in which case the card will be left in an unusable state.
650  *
651  * Caller must hold the rtnl_lock.
652  */
653 int efx_reset(struct efx_nic *efx, enum reset_type method)
654 {
655         bool disabled;
656         int rc, rc2;
657
658         netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
659                    RESET_TYPE(method));
660
661         efx_device_detach_sync(efx);
662         efx_reset_down(efx, method);
663
664         rc = efx->type->reset(efx, method);
665         if (rc) {
666                 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
667                 goto out;
668         }
669
670         /* Clear flags for the scopes we covered.  We assume the NIC and
671          * driver are now quiescent so that there is no race here.
672          */
673         if (method < RESET_TYPE_MAX_METHOD)
674                 efx->reset_pending &= -(1 << (method + 1));
675         else /* it doesn't fit into the well-ordered scope hierarchy */
676                 __clear_bit(method, &efx->reset_pending);
677
678         /* Reinitialise bus-mastering, which may have been turned off before
679          * the reset was scheduled. This is still appropriate, even in the
680          * RESET_TYPE_DISABLE since this driver generally assumes the hardware
681          * can respond to requests.
682          */
683         pci_set_master(efx->pci_dev);
684
685 out:
686         /* Leave device stopped if necessary */
687         disabled = rc ||
688                 method == RESET_TYPE_DISABLE ||
689                 method == RESET_TYPE_RECOVER_OR_DISABLE;
690         rc2 = efx_reset_up(efx, method, !disabled);
691         if (rc2) {
692                 disabled = true;
693                 if (!rc)
694                         rc = rc2;
695         }
696
697         if (disabled) {
698                 dev_close(efx->net_dev);
699                 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
700                 efx->state = STATE_DISABLED;
701         } else {
702                 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
703                 efx_device_attach_if_not_resetting(efx);
704         }
705         return rc;
706 }
707
708 /* The worker thread exists so that code that cannot sleep can
709  * schedule a reset for later.
710  */
711 static void efx_reset_work(struct work_struct *data)
712 {
713         struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
714         unsigned long pending;
715         enum reset_type method;
716
717         pending = READ_ONCE(efx->reset_pending);
718         method = fls(pending) - 1;
719
720         if (method == RESET_TYPE_MC_BIST)
721                 efx_wait_for_bist_end(efx);
722
723         if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
724              method == RESET_TYPE_RECOVER_OR_ALL) &&
725             efx_try_recovery(efx))
726                 return;
727
728         if (!pending)
729                 return;
730
731         rtnl_lock();
732
733         /* We checked the state in efx_schedule_reset() but it may
734          * have changed by now.  Now that we have the RTNL lock,
735          * it cannot change again.
736          */
737         if (efx->state == STATE_READY)
738                 (void)efx_reset(efx, method);
739
740         rtnl_unlock();
741 }
742
743 void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
744 {
745         enum reset_type method;
746
747         if (efx->state == STATE_RECOVERY) {
748                 netif_dbg(efx, drv, efx->net_dev,
749                           "recovering: skip scheduling %s reset\n",
750                           RESET_TYPE(type));
751                 return;
752         }
753
754         switch (type) {
755         case RESET_TYPE_INVISIBLE:
756         case RESET_TYPE_ALL:
757         case RESET_TYPE_RECOVER_OR_ALL:
758         case RESET_TYPE_WORLD:
759         case RESET_TYPE_DISABLE:
760         case RESET_TYPE_RECOVER_OR_DISABLE:
761         case RESET_TYPE_DATAPATH:
762         case RESET_TYPE_MC_BIST:
763         case RESET_TYPE_MCDI_TIMEOUT:
764                 method = type;
765                 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
766                           RESET_TYPE(method));
767                 break;
768         default:
769                 method = efx->type->map_reset_reason(type);
770                 netif_dbg(efx, drv, efx->net_dev,
771                           "scheduling %s reset for %s\n",
772                           RESET_TYPE(method), RESET_TYPE(type));
773                 break;
774         }
775
776         set_bit(method, &efx->reset_pending);
777         smp_mb(); /* ensure we change reset_pending before checking state */
778
779         /* If we're not READY then just leave the flags set as the cue
780          * to abort probing or reschedule the reset later.
781          */
782         if (READ_ONCE(efx->state) != STATE_READY)
783                 return;
784
785         /* efx_process_channel() will no longer read events once a
786          * reset is scheduled. So switch back to poll'd MCDI completions.
787          */
788         efx_mcdi_mode_poll(efx);
789
790         efx_queue_reset_work(efx);
791 }
792
793 /**************************************************************************
794  *
795  * Dummy PHY/MAC operations
796  *
797  * Can be used for some unimplemented operations
798  * Needed so all function pointers are valid and do not have to be tested
799  * before use
800  *
801  **************************************************************************/
802 int efx_port_dummy_op_int(struct efx_nic *efx)
803 {
804         return 0;
805 }
806 void efx_port_dummy_op_void(struct efx_nic *efx) {}
807
808 static bool efx_port_dummy_op_poll(struct efx_nic *efx)
809 {
810         return false;
811 }
812
813 static const struct efx_phy_operations efx_dummy_phy_operations = {
814         .init            = efx_port_dummy_op_int,
815         .reconfigure     = efx_port_dummy_op_int,
816         .poll            = efx_port_dummy_op_poll,
817         .fini            = efx_port_dummy_op_void,
818 };
819
820 /**************************************************************************
821  *
822  * Data housekeeping
823  *
824  **************************************************************************/
825
826 /* This zeroes out and then fills in the invariants in a struct
827  * efx_nic (including all sub-structures).
828  */
829 int efx_init_struct(struct efx_nic *efx,
830                     struct pci_dev *pci_dev, struct net_device *net_dev)
831 {
832         int rc = -ENOMEM;
833
834         /* Initialise common structures */
835         INIT_LIST_HEAD(&efx->node);
836         INIT_LIST_HEAD(&efx->secondary_list);
837         spin_lock_init(&efx->biu_lock);
838 #ifdef CONFIG_SFC_MTD
839         INIT_LIST_HEAD(&efx->mtd_list);
840 #endif
841         INIT_WORK(&efx->reset_work, efx_reset_work);
842         INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
843         INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
844         efx->pci_dev = pci_dev;
845         efx->msg_enable = debug;
846         efx->state = STATE_UNINIT;
847         strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
848
849         efx->net_dev = net_dev;
850         efx->rx_prefix_size = efx->type->rx_prefix_size;
851         efx->rx_ip_align =
852                 NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
853         efx->rx_packet_hash_offset =
854                 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
855         efx->rx_packet_ts_offset =
856                 efx->type->rx_ts_offset - efx->type->rx_prefix_size;
857         INIT_LIST_HEAD(&efx->rss_context.list);
858         mutex_init(&efx->rss_lock);
859         spin_lock_init(&efx->stats_lock);
860         efx->vi_stride = EFX_DEFAULT_VI_STRIDE;
861         efx->num_mac_stats = MC_CMD_MAC_NSTATS;
862         BUILD_BUG_ON(MC_CMD_MAC_NSTATS - 1 != MC_CMD_MAC_GENERATION_END);
863         mutex_init(&efx->mac_lock);
864 #ifdef CONFIG_RFS_ACCEL
865         mutex_init(&efx->rps_mutex);
866         spin_lock_init(&efx->rps_hash_lock);
867         /* Failure to allocate is not fatal, but may degrade ARFS performance */
868         efx->rps_hash_table = kcalloc(EFX_ARFS_HASH_TABLE_SIZE,
869                                       sizeof(*efx->rps_hash_table), GFP_KERNEL);
870 #endif
871         efx->phy_op = &efx_dummy_phy_operations;
872         efx->mdio.dev = net_dev;
873         INIT_WORK(&efx->mac_work, efx_mac_work);
874         init_waitqueue_head(&efx->flush_wq);
875
876         rc = efx_init_channels(efx);
877         if (rc)
878                 goto fail;
879
880         /* Would be good to use the net_dev name, but we're too early */
881         snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
882                  pci_name(pci_dev));
883         efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
884         if (!efx->workqueue) {
885                 rc = -ENOMEM;
886                 goto fail;
887         }
888
889         return 0;
890
891 fail:
892         efx_fini_struct(efx);
893         return rc;
894 }
895
896 void efx_fini_struct(struct efx_nic *efx)
897 {
898 #ifdef CONFIG_RFS_ACCEL
899         kfree(efx->rps_hash_table);
900 #endif
901
902         efx_fini_channels(efx);
903
904         kfree(efx->vpd_sn);
905
906         if (efx->workqueue) {
907                 destroy_workqueue(efx->workqueue);
908                 efx->workqueue = NULL;
909         }
910 }
911
912 /* This configures the PCI device to enable I/O and DMA. */
913 int efx_init_io(struct efx_nic *efx, int bar, dma_addr_t dma_mask,
914                 unsigned int mem_map_size)
915 {
916         struct pci_dev *pci_dev = efx->pci_dev;
917         int rc;
918
919         netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
920
921         rc = pci_enable_device(pci_dev);
922         if (rc) {
923                 netif_err(efx, probe, efx->net_dev,
924                           "failed to enable PCI device\n");
925                 goto fail1;
926         }
927
928         pci_set_master(pci_dev);
929
930         /* Set the PCI DMA mask.  Try all possibilities from our
931          * genuine mask down to 32 bits, because some architectures
932          * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
933          * masks event though they reject 46 bit masks.
934          */
935         while (dma_mask > 0x7fffffffUL) {
936                 rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
937                 if (rc == 0)
938                         break;
939                 dma_mask >>= 1;
940         }
941         if (rc) {
942                 netif_err(efx, probe, efx->net_dev,
943                           "could not find a suitable DMA mask\n");
944                 goto fail2;
945         }
946         netif_dbg(efx, probe, efx->net_dev,
947                   "using DMA mask %llx\n", (unsigned long long)dma_mask);
948
949         efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
950         if (!efx->membase_phys) {
951                 netif_err(efx, probe, efx->net_dev,
952                           "ERROR: No BAR%d mapping from the BIOS. "
953                           "Try pci=realloc on the kernel command line\n", bar);
954                 rc = -ENODEV;
955                 goto fail3;
956         }
957
958         rc = pci_request_region(pci_dev, bar, "sfc");
959         if (rc) {
960                 netif_err(efx, probe, efx->net_dev,
961                           "request for memory BAR failed\n");
962                 rc = -EIO;
963                 goto fail3;
964         }
965
966         efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
967         if (!efx->membase) {
968                 netif_err(efx, probe, efx->net_dev,
969                           "could not map memory BAR at %llx+%x\n",
970                           (unsigned long long)efx->membase_phys, mem_map_size);
971                 rc = -ENOMEM;
972                 goto fail4;
973         }
974         netif_dbg(efx, probe, efx->net_dev,
975                   "memory BAR at %llx+%x (virtual %p)\n",
976                   (unsigned long long)efx->membase_phys, mem_map_size,
977                   efx->membase);
978
979         return 0;
980
981 fail4:
982         pci_release_region(efx->pci_dev, bar);
983 fail3:
984         efx->membase_phys = 0;
985 fail2:
986         pci_disable_device(efx->pci_dev);
987 fail1:
988         return rc;
989 }
990
991 void efx_fini_io(struct efx_nic *efx, int bar)
992 {
993         netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
994
995         if (efx->membase) {
996                 iounmap(efx->membase);
997                 efx->membase = NULL;
998         }
999
1000         if (efx->membase_phys) {
1001                 pci_release_region(efx->pci_dev, bar);
1002                 efx->membase_phys = 0;
1003         }
1004
1005         /* Don't disable bus-mastering if VFs are assigned */
1006         if (!pci_vfs_assigned(efx->pci_dev))
1007                 pci_disable_device(efx->pci_dev);
1008 }
1009
1010 #ifdef CONFIG_SFC_MCDI_LOGGING
1011 static ssize_t show_mcdi_log(struct device *dev, struct device_attribute *attr,
1012                              char *buf)
1013 {
1014         struct efx_nic *efx = dev_get_drvdata(dev);
1015         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
1016
1017         return scnprintf(buf, PAGE_SIZE, "%d\n", mcdi->logging_enabled);
1018 }
1019
1020 static ssize_t set_mcdi_log(struct device *dev, struct device_attribute *attr,
1021                             const char *buf, size_t count)
1022 {
1023         struct efx_nic *efx = dev_get_drvdata(dev);
1024         struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
1025         bool enable = count > 0 && *buf != '0';
1026
1027         mcdi->logging_enabled = enable;
1028         return count;
1029 }
1030
1031 static DEVICE_ATTR(mcdi_logging, 0644, show_mcdi_log, set_mcdi_log);
1032
1033 void efx_init_mcdi_logging(struct efx_nic *efx)
1034 {
1035         int rc = device_create_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
1036
1037         if (rc) {
1038                 netif_warn(efx, drv, efx->net_dev,
1039                            "failed to init net dev attributes\n");
1040         }
1041 }
1042
1043 void efx_fini_mcdi_logging(struct efx_nic *efx)
1044 {
1045         device_remove_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
1046 }
1047 #endif