blob: 0f127a01b5e86d98d19064da6da9a4aaaaa77b05 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01002 * Driver for Solarflare network controllers and boards
Ben Hutchings8ceee662008-04-27 12:55:59 +01003 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01004 * Copyright 2005-2013 Solarflare Communications Inc.
Ben Hutchings8ceee662008-04-27 12:55:59 +01005 *
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 <linux/module.h>
12#include <linux/pci.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/delay.h>
16#include <linux/notifier.h>
17#include <linux/ip.h>
18#include <linux/tcp.h>
19#include <linux/in.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010020#include <linux/ethtool.h>
Ben Hutchingsaa6ef272008-07-18 19:03:10 +010021#include <linux/topology.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Alexandre Rames626950d2013-01-14 17:20:22 +000023#include <linux/aer.h>
Alexandre Ramesb28405b2013-03-21 16:41:43 +000024#include <linux/interrupt.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010025#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000027#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000028#include "selftest.h"
Shradha Shah7fa8d542015-05-06 00:55:13 +010029#include "sriov.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010030
Ben Hutchings8880f4e2009-11-29 15:15:41 +000031#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000032#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000033
Ben Hutchingsc4593022009-11-23 16:08:17 +000034/**************************************************************************
35 *
36 * Type name strings
37 *
38 **************************************************************************
39 */
40
41/* Loopback mode names (see LOOPBACK_MODE()) */
42const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000043const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000044 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000045 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000046 [LOOPBACK_GMAC] = "GMAC",
47 [LOOPBACK_XGMII] = "XGMII",
48 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000049 [LOOPBACK_XAUI] = "XAUI",
50 [LOOPBACK_GMII] = "GMII",
51 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000052 [LOOPBACK_XGBR] = "XGBR",
53 [LOOPBACK_XFI] = "XFI",
54 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
55 [LOOPBACK_GMII_FAR] = "GMII_FAR",
56 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
57 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000058 [LOOPBACK_GPHY] = "GPHY",
59 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000060 [LOOPBACK_PCS] = "PCS",
61 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000062 [LOOPBACK_XPORT] = "XPORT",
63 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000064 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000065 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
66 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000067 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000068 [LOOPBACK_XFI_WS] = "XFI_WS",
69 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000070 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000071};
72
Ben Hutchingsc4593022009-11-23 16:08:17 +000073const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000074const char *const efx_reset_type_names[] = {
Alexandre Rames626950d2013-01-14 17:20:22 +000075 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
76 [RESET_TYPE_ALL] = "ALL",
77 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
78 [RESET_TYPE_WORLD] = "WORLD",
79 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
Edward Creee2835462014-04-16 19:27:48 +010080 [RESET_TYPE_MC_BIST] = "MC_BIST",
Alexandre Rames626950d2013-01-14 17:20:22 +000081 [RESET_TYPE_DISABLE] = "DISABLE",
82 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
83 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
84 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
Alexandre Rames3de82b92013-06-13 11:36:15 +010085 [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
Alexandre Rames626950d2013-01-14 17:20:22 +000086 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
87 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Edward Creee2835462014-04-16 19:27:48 +010088 [RESET_TYPE_MCDI_TIMEOUT] = "MCDI_TIMEOUT (FLR)",
Ben Hutchingsc4593022009-11-23 16:08:17 +000089};
90
Steve Hodgson1ab00622008-12-12 21:33:02 -080091/* Reset workqueue. If any NIC has a hardware failure then a reset will be
92 * queued onto this work queue. This is not a per-nic work queue, because
93 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
94 */
95static struct workqueue_struct *reset_workqueue;
96
Jon Cooper74cd60a2013-09-16 14:18:51 +010097/* How often and how many times to poll for a reset while waiting for a
98 * BIST that another function started to complete.
99 */
100#define BIST_WAIT_DELAY_MS 100
101#define BIST_WAIT_DELAY_COUNT 100
102
Ben Hutchings8ceee662008-04-27 12:55:59 +0100103/**************************************************************************
104 *
105 * Configurable values
106 *
107 *************************************************************************/
108
109/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100110 * Use separate channels for TX and RX events
111 *
Neil Turton28b581a2008-12-12 21:41:06 -0800112 * Set this to 1 to use separate channels for TX and RX. It allows us
113 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100114 *
Neil Turton28b581a2008-12-12 21:41:06 -0800115 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100116 */
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000117static bool separate_tx_channels;
118module_param(separate_tx_channels, bool, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800119MODULE_PARM_DESC(separate_tx_channels,
120 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100121
122/* This is the weight assigned to each of the (per-channel) virtual
123 * NAPI devices.
124 */
125static int napi_weight = 64;
126
127/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000128 * monitor.
129 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000130 * - Check the on-board hardware monitor;
131 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000132 * On Siena-based NICs for power systems with EEH support, this will give EEH a
133 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100134 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000135static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100136
Ben Hutchings8ceee662008-04-27 12:55:59 +0100137/* Initial interrupt moderation settings. They can be modified after
138 * module load with ethtool.
139 *
140 * The default for RX should strike a balance between increasing the
141 * round-trip latency and reducing overhead.
142 */
143static unsigned int rx_irq_mod_usec = 60;
144
145/* Initial interrupt moderation settings. They can be modified after
146 * module load with ethtool.
147 *
148 * This default is chosen to ensure that a 10G link does not go idle
149 * while a TX queue is stopped after it has become full. A queue is
150 * restarted when it drops below half full. The time this takes (assuming
151 * worst case 3 descriptors per packet and 1024 descriptors) is
152 * 512 / 3 * 1.2 = 205 usec.
153 */
154static unsigned int tx_irq_mod_usec = 150;
155
156/* This is the first interrupt mode to try out of:
157 * 0 => MSI-X
158 * 1 => MSI
159 * 2 => legacy
160 */
161static unsigned int interrupt_mode;
162
163/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
164 * i.e. the number of CPUs among which we may distribute simultaneous
165 * interrupt handling.
166 *
167 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000168 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100169 */
170static unsigned int rss_cpus;
171module_param(rss_cpus, uint, 0444);
172MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
173
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000174static bool phy_flash_cfg;
175module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800176MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
177
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000178static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000179module_param(irq_adapt_low_thresh, uint, 0644);
180MODULE_PARM_DESC(irq_adapt_low_thresh,
181 "Threshold score for reducing IRQ moderation");
182
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000183static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000184module_param(irq_adapt_high_thresh, uint, 0644);
185MODULE_PARM_DESC(irq_adapt_high_thresh,
186 "Threshold score for increasing IRQ moderation");
187
Ben Hutchings62776d02010-06-23 11:30:07 +0000188static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
189 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
190 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
191 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
192module_param(debug, uint, 0);
193MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
194
Ben Hutchings8ceee662008-04-27 12:55:59 +0100195/**************************************************************************
196 *
197 * Utility functions and prototypes
198 *
199 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000200
Jon Cooper261e4d92013-04-15 18:51:54 +0100201static int efx_soft_enable_interrupts(struct efx_nic *efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100202static void efx_soft_disable_interrupts(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000203static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000204static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000205static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100206static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000207static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100208static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000209static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000210static void efx_fini_struct(struct efx_nic *efx);
211static void efx_start_all(struct efx_nic *efx);
212static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100213
214#define EFX_ASSERT_RESET_SERIALISED(efx) \
215 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100216 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000217 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000218 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100219 ASSERT_RTNL(); \
220 } while (0)
221
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100222static int efx_check_disabled(struct efx_nic *efx)
223{
Alexandre Rames626950d2013-01-14 17:20:22 +0000224 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100225 netif_err(efx, drv, efx->net_dev,
226 "device is disabled due to earlier errors\n");
227 return -EIO;
228 }
229 return 0;
230}
231
Ben Hutchings8ceee662008-04-27 12:55:59 +0100232/**************************************************************************
233 *
234 * Event queue processing
235 *
236 *************************************************************************/
237
238/* Process channel's event queue
239 *
240 * This function is responsible for processing the event queue of a
241 * single channel. The caller must guarantee that this function will
242 * never be concurrently called more than once on the same channel,
243 * though different channels may be being processed concurrently.
244 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000245static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100246{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000247 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100248
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000249 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100250 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100251
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000252 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000253 if (spent && efx_channel_has_rx_queue(channel)) {
254 struct efx_rx_queue *rx_queue =
255 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100256
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000257 efx_rx_flush_packet(channel);
Jon Coopercce28792013-10-02 11:04:14 +0100258 efx_fast_push_rx_descriptors(rx_queue, true);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100259 }
260
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000261 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100262}
263
Ben Hutchings8ceee662008-04-27 12:55:59 +0100264/* NAPI poll handler
265 *
266 * NAPI guarantees serialisation of polls of the same device, which
267 * provides the guarantee required by efx_process_channel().
268 */
269static int efx_poll(struct napi_struct *napi, int budget)
270{
271 struct efx_channel *channel =
272 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000273 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000274 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100275
Alexandre Rames36763262014-07-22 14:03:25 +0100276 if (!efx_channel_lock_napi(channel))
277 return budget;
278
Ben Hutchings62776d02010-06-23 11:30:07 +0000279 netif_vdbg(efx, intr, efx->net_dev,
280 "channel %d NAPI poll executing on CPU %d\n",
281 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100282
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000283 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100284
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000285 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000286 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000287 efx->irq_rx_adaptive &&
288 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000289 if (unlikely(channel->irq_mod_score <
290 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000291 if (channel->irq_moderation > 1) {
292 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000293 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000294 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000295 } else if (unlikely(channel->irq_mod_score >
296 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000297 if (channel->irq_moderation <
298 efx->irq_rx_moderation) {
299 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000300 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000301 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000302 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000303 channel->irq_count = 0;
304 channel->irq_mod_score = 0;
305 }
306
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000307 efx_filter_rfs_expire(channel);
308
Ben Hutchings8ceee662008-04-27 12:55:59 +0100309 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800310 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100311 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100312 * interrupts have already been disabled.
313 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800314 napi_complete(napi);
Ben Hutchings514bedb2012-10-05 19:30:16 +0100315 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100316 }
317
Alexandre Rames36763262014-07-22 14:03:25 +0100318 efx_channel_unlock_napi(channel);
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000319 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100320}
321
Ben Hutchings8ceee662008-04-27 12:55:59 +0100322/* Create event queue
323 * Event queue memory allocations are done only once. If the channel
324 * is reset, the memory buffer will be reused; this guards against
325 * errors during channel reset and also simplifies interrupt handling.
326 */
327static int efx_probe_eventq(struct efx_channel *channel)
328{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000329 struct efx_nic *efx = channel->efx;
330 unsigned long entries;
331
Ben Hutchings86ee5302012-01-09 19:51:22 +0000332 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000333 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100334
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000335 /* Build an event queue with room for one event per tx and rx buffer,
336 * plus some extra for link state events and MCDI completions. */
337 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
338 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
339 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
340
Ben Hutchings152b6a62009-11-29 03:43:56 +0000341 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100342}
343
344/* Prepare channel's event queue */
Jon Cooper261e4d92013-04-15 18:51:54 +0100345static int efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100346{
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100347 struct efx_nic *efx = channel->efx;
Jon Cooper261e4d92013-04-15 18:51:54 +0100348 int rc;
349
350 EFX_WARN_ON_PARANOID(channel->eventq_init);
351
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100352 netif_dbg(efx, drv, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000353 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100354
Jon Cooper261e4d92013-04-15 18:51:54 +0100355 rc = efx_nic_init_eventq(channel);
356 if (rc == 0) {
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100357 efx->type->push_irq_moderation(channel);
Jon Cooper261e4d92013-04-15 18:51:54 +0100358 channel->eventq_read_ptr = 0;
359 channel->eventq_init = true;
360 }
361 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100362}
363
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000364/* Enable event queue processing and NAPI */
Alexandre Rames36763262014-07-22 14:03:25 +0100365void efx_start_eventq(struct efx_channel *channel)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000366{
367 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
368 "chan %d start event queue\n", channel->channel);
369
Ben Hutchings514bedb2012-10-05 19:30:16 +0100370 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000371 channel->enabled = true;
372 smp_wmb();
373
Alexandre Rames36763262014-07-22 14:03:25 +0100374 efx_channel_enable(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000375 napi_enable(&channel->napi_str);
376 efx_nic_eventq_read_ack(channel);
377}
378
379/* Disable event queue processing and NAPI */
Alexandre Rames36763262014-07-22 14:03:25 +0100380void efx_stop_eventq(struct efx_channel *channel)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000381{
382 if (!channel->enabled)
383 return;
384
385 napi_disable(&channel->napi_str);
Alexandre Rames36763262014-07-22 14:03:25 +0100386 while (!efx_channel_disable(channel))
387 usleep_range(1000, 20000);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000388 channel->enabled = false;
389}
390
Ben Hutchings8ceee662008-04-27 12:55:59 +0100391static void efx_fini_eventq(struct efx_channel *channel)
392{
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100393 if (!channel->eventq_init)
394 return;
395
Ben Hutchings62776d02010-06-23 11:30:07 +0000396 netif_dbg(channel->efx, drv, channel->efx->net_dev,
397 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100398
Ben Hutchings152b6a62009-11-29 03:43:56 +0000399 efx_nic_fini_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100400 channel->eventq_init = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100401}
402
403static void efx_remove_eventq(struct efx_channel *channel)
404{
Ben Hutchings62776d02010-06-23 11:30:07 +0000405 netif_dbg(channel->efx, drv, channel->efx->net_dev,
406 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100407
Ben Hutchings152b6a62009-11-29 03:43:56 +0000408 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100409}
410
411/**************************************************************************
412 *
413 * Channel handling
414 *
415 *************************************************************************/
416
Ben Hutchings7f967c02012-02-13 23:45:02 +0000417/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000418static struct efx_channel *
419efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
420{
421 struct efx_channel *channel;
422 struct efx_rx_queue *rx_queue;
423 struct efx_tx_queue *tx_queue;
424 int j;
425
Ben Hutchings7f967c02012-02-13 23:45:02 +0000426 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
427 if (!channel)
428 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000429
Ben Hutchings7f967c02012-02-13 23:45:02 +0000430 channel->efx = efx;
431 channel->channel = i;
432 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000433
Ben Hutchings7f967c02012-02-13 23:45:02 +0000434 for (j = 0; j < EFX_TXQ_TYPES; j++) {
435 tx_queue = &channel->tx_queue[j];
436 tx_queue->efx = efx;
437 tx_queue->queue = i * EFX_TXQ_TYPES + j;
438 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000439 }
440
Ben Hutchings46426102010-09-10 06:42:33 +0000441 rx_queue = &channel->rx_queue;
442 rx_queue->efx = efx;
443 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
444 (unsigned long)rx_queue);
445
446 return channel;
447}
448
Ben Hutchings7f967c02012-02-13 23:45:02 +0000449/* Allocate and initialise a channel structure, copying parameters
450 * (but not resources) from an old channel structure.
451 */
452static struct efx_channel *
453efx_copy_channel(const struct efx_channel *old_channel)
454{
455 struct efx_channel *channel;
456 struct efx_rx_queue *rx_queue;
457 struct efx_tx_queue *tx_queue;
458 int j;
459
460 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
461 if (!channel)
462 return NULL;
463
464 *channel = *old_channel;
465
466 channel->napi_dev = NULL;
467 memset(&channel->eventq, 0, sizeof(channel->eventq));
468
469 for (j = 0; j < EFX_TXQ_TYPES; j++) {
470 tx_queue = &channel->tx_queue[j];
471 if (tx_queue->channel)
472 tx_queue->channel = channel;
473 tx_queue->buffer = NULL;
474 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
475 }
476
477 rx_queue = &channel->rx_queue;
478 rx_queue->buffer = NULL;
479 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
480 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
481 (unsigned long)rx_queue);
482
483 return channel;
484}
485
Ben Hutchings8ceee662008-04-27 12:55:59 +0100486static int efx_probe_channel(struct efx_channel *channel)
487{
488 struct efx_tx_queue *tx_queue;
489 struct efx_rx_queue *rx_queue;
490 int rc;
491
Ben Hutchings62776d02010-06-23 11:30:07 +0000492 netif_dbg(channel->efx, probe, channel->efx->net_dev,
493 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100494
Ben Hutchings7f967c02012-02-13 23:45:02 +0000495 rc = channel->type->pre_probe(channel);
496 if (rc)
497 goto fail;
498
Ben Hutchings8ceee662008-04-27 12:55:59 +0100499 rc = efx_probe_eventq(channel);
500 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000501 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100502
503 efx_for_each_channel_tx_queue(tx_queue, channel) {
504 rc = efx_probe_tx_queue(tx_queue);
505 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000506 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100507 }
508
509 efx_for_each_channel_rx_queue(rx_queue, channel) {
510 rc = efx_probe_rx_queue(rx_queue);
511 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000512 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100513 }
514
Ben Hutchings8ceee662008-04-27 12:55:59 +0100515 return 0;
516
Ben Hutchings7f967c02012-02-13 23:45:02 +0000517fail:
518 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100519 return rc;
520}
521
Ben Hutchings7f967c02012-02-13 23:45:02 +0000522static void
523efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
524{
525 struct efx_nic *efx = channel->efx;
526 const char *type;
527 int number;
528
529 number = channel->channel;
530 if (efx->tx_channel_offset == 0) {
531 type = "";
532 } else if (channel->channel < efx->tx_channel_offset) {
533 type = "-rx";
534 } else {
535 type = "-tx";
536 number -= efx->tx_channel_offset;
537 }
538 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
539}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100540
Ben Hutchings56536e92008-12-12 21:37:02 -0800541static void efx_set_channel_names(struct efx_nic *efx)
542{
543 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800544
Ben Hutchings7f967c02012-02-13 23:45:02 +0000545 efx_for_each_channel(channel, efx)
546 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100547 efx->msi_context[channel->channel].name,
548 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800549}
550
Ben Hutchings46426102010-09-10 06:42:33 +0000551static int efx_probe_channels(struct efx_nic *efx)
552{
553 struct efx_channel *channel;
554 int rc;
555
556 /* Restart special buffer allocation */
557 efx->next_buffer_table = 0;
558
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000559 /* Probe channels in reverse, so that any 'extra' channels
560 * use the start of the buffer table. This allows the traffic
561 * channels to be resized without moving them or wasting the
562 * entries before them.
563 */
564 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000565 rc = efx_probe_channel(channel);
566 if (rc) {
567 netif_err(efx, probe, efx->net_dev,
568 "failed to create channel %d\n",
569 channel->channel);
570 goto fail;
571 }
572 }
573 efx_set_channel_names(efx);
574
575 return 0;
576
577fail:
578 efx_remove_channels(efx);
579 return rc;
580}
581
Ben Hutchings8ceee662008-04-27 12:55:59 +0100582/* Channels are shutdown and reinitialised whilst the NIC is running
583 * to propagate configuration changes (mtu, checksum offload), or
584 * to clear hardware error conditions
585 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000586static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100587{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000588 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100589 struct efx_tx_queue *tx_queue;
590 struct efx_rx_queue *rx_queue;
591 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000592 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100593
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100594 /* Calculate the rx buffer allocation parameters required to
595 * support the current MTU, including padding for header
596 * alignment and overruns.
597 */
Jon Cooper43a37392012-10-18 15:49:54 +0100598 efx->rx_dma_len = (efx->rx_prefix_size +
Ben Hutchings272baee2013-01-29 23:33:14 +0000599 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
600 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000601 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Andrew Rybchenko2ec03012013-11-16 11:02:27 +0400602 efx->rx_ip_align + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000603 if (rx_buf_len <= PAGE_SIZE) {
Jon Coopere8c68c02013-03-08 10:18:28 +0000604 efx->rx_scatter = efx->type->always_rx_scatter;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000605 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000606 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000607 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000608 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000609 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
610 EFX_RX_BUF_ALIGNMENT) >
611 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000612 efx->rx_scatter = true;
613 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
614 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000615 } else {
616 efx->rx_scatter = false;
617 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000618 }
619
Daniel Pieczko1648a232013-02-13 10:54:41 +0000620 efx_rx_config_page_split(efx);
621 if (efx->rx_buffer_order)
622 netif_dbg(efx, drv, efx->net_dev,
623 "RX buf len=%u; page order=%u batch=%u\n",
624 efx->rx_dma_len, efx->rx_buffer_order,
625 efx->rx_pages_per_batch);
626 else
627 netif_dbg(efx, drv, efx->net_dev,
628 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
629 efx->rx_dma_len, efx->rx_page_buf_step,
630 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000631
Jon Coopere8c68c02013-03-08 10:18:28 +0000632 /* RX filters may also have scatter-enabled flags */
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000633 if (efx->rx_scatter != old_rx_scatter)
Ben Hutchingsadd72472012-11-08 01:46:53 +0000634 efx->type->filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100635
Ben Hutchings14bf7182012-05-22 01:27:58 +0100636 /* We must keep at least one descriptor in a TX ring empty.
637 * We could avoid this when the queue size does not exactly
638 * match the hardware ring size, but it's not that important.
639 * Therefore we stop the queue when one more skb might fill
640 * the ring completely. We wake it when half way back to
641 * empty.
642 */
643 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
644 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
645
Ben Hutchings8ceee662008-04-27 12:55:59 +0100646 /* Initialise the channels */
647 efx_for_each_channel(channel, efx) {
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100648 efx_for_each_channel_tx_queue(tx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100649 efx_init_tx_queue(tx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100650 atomic_inc(&efx->active_queues);
651 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100652
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000653 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100654 efx_init_rx_queue(rx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100655 atomic_inc(&efx->active_queues);
Jon Coopercce28792013-10-02 11:04:14 +0100656 efx_stop_eventq(channel);
657 efx_fast_push_rx_descriptors(rx_queue, false);
658 efx_start_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000659 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100660
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000661 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100662 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000663
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000664 efx_ptp_start_datapath(efx);
665
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000666 if (netif_device_present(efx->net_dev))
667 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100668}
669
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000670static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100671{
672 struct efx_channel *channel;
673 struct efx_tx_queue *tx_queue;
674 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100675 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100676
677 EFX_ASSERT_RESET_SERIALISED(efx);
678 BUG_ON(efx->port_enabled);
679
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000680 efx_ptp_stop_datapath(efx);
681
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100682 /* Stop RX refill */
683 efx_for_each_channel(channel, efx) {
684 efx_for_each_channel_rx_queue(rx_queue, channel)
685 rx_queue->refill_enabled = false;
686 }
687
Ben Hutchings8ceee662008-04-27 12:55:59 +0100688 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000689 /* RX packet processing is pipelined, so wait for the
690 * NAPI handler to complete. At least event queue 0
691 * might be kept active by non-data events, so don't
692 * use napi_synchronize() but actually disable NAPI
693 * temporarily.
694 */
695 if (efx_channel_has_rx_queue(channel)) {
696 efx_stop_eventq(channel);
697 efx_start_eventq(channel);
698 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100699 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100700
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100701 rc = efx->type->fini_dmaq(efx);
702 if (rc && EFX_WORKAROUND_7803(efx)) {
703 /* Schedule a reset to recover from the flush failure. The
704 * descriptor caches reference memory we're about to free,
705 * but falcon_reconfigure_mac_wrapper() won't reconnect
706 * the MACs because of the pending reset.
707 */
708 netif_err(efx, drv, efx->net_dev,
709 "Resetting to recover from flush failure\n");
710 efx_schedule_reset(efx, RESET_TYPE_ALL);
711 } else if (rc) {
712 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
713 } else {
714 netif_dbg(efx, drv, efx->net_dev,
715 "successfully flushed all queues\n");
716 }
717
718 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100719 efx_for_each_channel_rx_queue(rx_queue, channel)
720 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000721 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100722 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100723 }
724}
725
726static void efx_remove_channel(struct efx_channel *channel)
727{
728 struct efx_tx_queue *tx_queue;
729 struct efx_rx_queue *rx_queue;
730
Ben Hutchings62776d02010-06-23 11:30:07 +0000731 netif_dbg(channel->efx, drv, channel->efx->net_dev,
732 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100733
734 efx_for_each_channel_rx_queue(rx_queue, channel)
735 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000736 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100737 efx_remove_tx_queue(tx_queue);
738 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100739 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100740}
741
Ben Hutchings46426102010-09-10 06:42:33 +0000742static void efx_remove_channels(struct efx_nic *efx)
743{
744 struct efx_channel *channel;
745
746 efx_for_each_channel(channel, efx)
747 efx_remove_channel(channel);
748}
749
750int
751efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
752{
753 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
754 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000755 unsigned i, next_buffer_table = 0;
Jon Cooper261e4d92013-04-15 18:51:54 +0100756 int rc, rc2;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100757
758 rc = efx_check_disabled(efx);
759 if (rc)
760 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000761
762 /* Not all channels should be reallocated. We must avoid
763 * reallocating their buffer table entries.
764 */
765 efx_for_each_channel(channel, efx) {
766 struct efx_rx_queue *rx_queue;
767 struct efx_tx_queue *tx_queue;
768
769 if (channel->type->copy)
770 continue;
771 next_buffer_table = max(next_buffer_table,
772 channel->eventq.index +
773 channel->eventq.entries);
774 efx_for_each_channel_rx_queue(rx_queue, channel)
775 next_buffer_table = max(next_buffer_table,
776 rx_queue->rxd.index +
777 rx_queue->rxd.entries);
778 efx_for_each_channel_tx_queue(tx_queue, channel)
779 next_buffer_table = max(next_buffer_table,
780 tx_queue->txd.index +
781 tx_queue->txd.entries);
782 }
Ben Hutchings46426102010-09-10 06:42:33 +0000783
Ben Hutchings29c69a42013-01-28 19:01:06 +0000784 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000785 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100786 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000787
Ben Hutchings7f967c02012-02-13 23:45:02 +0000788 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000789 memset(other_channel, 0, sizeof(other_channel));
790 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000791 channel = efx->channel[i];
792 if (channel->type->copy)
793 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000794 if (!channel) {
795 rc = -ENOMEM;
796 goto out;
797 }
798 other_channel[i] = channel;
799 }
800
801 /* Swap entry counts and channel pointers */
802 old_rxq_entries = efx->rxq_entries;
803 old_txq_entries = efx->txq_entries;
804 efx->rxq_entries = rxq_entries;
805 efx->txq_entries = txq_entries;
806 for (i = 0; i < efx->n_channels; i++) {
807 channel = efx->channel[i];
808 efx->channel[i] = other_channel[i];
809 other_channel[i] = channel;
810 }
811
Ben Hutchings7f967c02012-02-13 23:45:02 +0000812 /* Restart buffer table allocation */
813 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000814
Ben Hutchingse8f14992010-12-07 19:47:34 +0000815 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000816 channel = efx->channel[i];
817 if (!channel->type->copy)
818 continue;
819 rc = efx_probe_channel(channel);
820 if (rc)
821 goto rollback;
822 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000823 }
Ben Hutchings46426102010-09-10 06:42:33 +0000824
Ben Hutchings7f967c02012-02-13 23:45:02 +0000825out:
826 /* Destroy unused channel structures */
827 for (i = 0; i < efx->n_channels; i++) {
828 channel = other_channel[i];
829 if (channel && channel->type->copy) {
830 efx_fini_napi_channel(channel);
831 efx_remove_channel(channel);
832 kfree(channel);
833 }
834 }
835
Jon Cooper261e4d92013-04-15 18:51:54 +0100836 rc2 = efx_soft_enable_interrupts(efx);
837 if (rc2) {
838 rc = rc ? rc : rc2;
839 netif_err(efx, drv, efx->net_dev,
840 "unable to restart interrupts on channel reallocation\n");
841 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
842 } else {
843 efx_start_all(efx);
844 netif_device_attach(efx->net_dev);
845 }
Ben Hutchings46426102010-09-10 06:42:33 +0000846 return rc;
847
848rollback:
849 /* Swap back */
850 efx->rxq_entries = old_rxq_entries;
851 efx->txq_entries = old_txq_entries;
852 for (i = 0; i < efx->n_channels; i++) {
853 channel = efx->channel[i];
854 efx->channel[i] = other_channel[i];
855 other_channel[i] = channel;
856 }
857 goto out;
858}
859
Steve Hodgson90d683a2010-06-01 11:19:39 +0000860void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100861{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000862 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100863}
864
Ben Hutchings7f967c02012-02-13 23:45:02 +0000865static const struct efx_channel_type efx_default_channel_type = {
866 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100867 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000868 .get_name = efx_get_channel_name,
869 .copy = efx_copy_channel,
870 .keep_eventq = false,
871};
872
873int efx_channel_dummy_op_int(struct efx_channel *channel)
874{
875 return 0;
876}
877
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100878void efx_channel_dummy_op_void(struct efx_channel *channel)
879{
880}
881
Ben Hutchings8ceee662008-04-27 12:55:59 +0100882/**************************************************************************
883 *
884 * Port handling
885 *
886 **************************************************************************/
887
888/* This ensures that the kernel is kept informed (via
889 * netif_carrier_on/off) of the link status, and also maintains the
890 * link status's stop on the port's TX queue.
891 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000892void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100893{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000894 struct efx_link_state *link_state = &efx->link_state;
895
Ben Hutchings8ceee662008-04-27 12:55:59 +0100896 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
897 * that no events are triggered between unregister_netdev() and the
898 * driver unloading. A more general condition is that NETDEV_CHANGE
899 * can only be generated between NETDEV_UP and NETDEV_DOWN */
900 if (!netif_running(efx->net_dev))
901 return;
902
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000903 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100904 efx->n_link_state_changes++;
905
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000906 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100907 netif_carrier_on(efx->net_dev);
908 else
909 netif_carrier_off(efx->net_dev);
910 }
911
912 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000913 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000914 netif_info(efx, link, efx->net_dev,
Ben Hutchings964e6132012-11-19 23:08:22 +0000915 "link up at %uMbps %s-duplex (MTU %d)\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000916 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings964e6132012-11-19 23:08:22 +0000917 efx->net_dev->mtu);
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000918 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000919 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100920}
921
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000922void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
923{
924 efx->link_advertising = advertising;
925 if (advertising) {
926 if (advertising & ADVERTISED_Pause)
927 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
928 else
929 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
930 if (advertising & ADVERTISED_Asym_Pause)
931 efx->wanted_fc ^= EFX_FC_TX;
932 }
933}
934
David S. Millerb56269462011-05-17 17:53:22 -0400935void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000936{
937 efx->wanted_fc = wanted_fc;
938 if (efx->link_advertising) {
939 if (wanted_fc & EFX_FC_RX)
940 efx->link_advertising |= (ADVERTISED_Pause |
941 ADVERTISED_Asym_Pause);
942 else
943 efx->link_advertising &= ~(ADVERTISED_Pause |
944 ADVERTISED_Asym_Pause);
945 if (wanted_fc & EFX_FC_TX)
946 efx->link_advertising ^= ADVERTISED_Asym_Pause;
947 }
948}
949
Ben Hutchings115122a2009-03-04 09:52:52 +0000950static void efx_fini_port(struct efx_nic *efx);
951
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000952/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
953 * the MAC appropriately. All other PHY configuration changes are pushed
954 * through phy_op->set_settings(), and pushed asynchronously to the MAC
955 * through efx_monitor().
956 *
957 * Callers must hold the mac_lock
958 */
959int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100960{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000961 enum efx_phy_mode phy_mode;
962 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100963
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000964 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100965
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000966 /* Disable PHY transmit in mac level loopbacks */
967 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800968 if (LOOPBACK_INTERNAL(efx))
969 efx->phy_mode |= PHY_MODE_TX_DISABLED;
970 else
971 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800972
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000973 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800974
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000975 if (rc)
976 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100977
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000978 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100979}
980
981/* Reinitialise the MAC to pick up new PHY settings, even if the port is
982 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000983int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100984{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000985 int rc;
986
Ben Hutchings8ceee662008-04-27 12:55:59 +0100987 EFX_ASSERT_RESET_SERIALISED(efx);
988
989 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000990 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100991 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000992
993 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100994}
995
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000996/* Asynchronous work item for changing MAC promiscuity and multicast
997 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
998 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800999static void efx_mac_work(struct work_struct *data)
1000{
1001 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
1002
1003 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +01001004 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +01001005 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001006 mutex_unlock(&efx->mac_lock);
1007}
1008
Ben Hutchings8ceee662008-04-27 12:55:59 +01001009static int efx_probe_port(struct efx_nic *efx)
1010{
1011 int rc;
1012
Ben Hutchings62776d02010-06-23 11:30:07 +00001013 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001014
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001015 if (phy_flash_cfg)
1016 efx->phy_mode = PHY_MODE_SPECIAL;
1017
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001018 /* Connect up MAC/PHY operations table */
1019 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001020 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001021 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001022
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001023 /* Initialise MAC address to permanent address */
Edward Creecd84ff42014-03-07 18:27:41 +00001024 ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001025
1026 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001027}
1028
1029static int efx_init_port(struct efx_nic *efx)
1030{
1031 int rc;
1032
Ben Hutchings62776d02010-06-23 11:30:07 +00001033 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001034
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001035 mutex_lock(&efx->mac_lock);
1036
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001037 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001038 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001039 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001040
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001041 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001042
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001043 /* Reconfigure the MAC before creating dma queues (required for
1044 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001045 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001046
1047 /* Ensure the PHY advertises the correct flow control settings */
1048 rc = efx->phy_op->reconfigure(efx);
Edward Cree267d9d72015-05-06 00:59:18 +01001049 if (rc && rc != -EPERM)
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001050 goto fail2;
1051
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001052 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001053 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001054
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001055fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001056 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001057fail1:
1058 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001059 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001060}
1061
Ben Hutchings8ceee662008-04-27 12:55:59 +01001062static void efx_start_port(struct efx_nic *efx)
1063{
Ben Hutchings62776d02010-06-23 11:30:07 +00001064 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001065 BUG_ON(efx->port_enabled);
1066
1067 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001068 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001069
Ben Hutchingsd615c032013-10-08 17:33:20 +01001070 /* Ensure MAC ingress/egress is enabled */
Ben Hutchings710b2082011-09-03 00:15:00 +01001071 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001072
Ben Hutchings8ceee662008-04-27 12:55:59 +01001073 mutex_unlock(&efx->mac_lock);
1074}
1075
Ben Hutchingsd615c032013-10-08 17:33:20 +01001076/* Cancel work for MAC reconfiguration, periodic hardware monitoring
1077 * and the async self-test, wait for them to finish and prevent them
1078 * being scheduled again. This doesn't cover online resets, which
1079 * should only be cancelled when removing the device.
1080 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001081static void efx_stop_port(struct efx_nic *efx)
1082{
Ben Hutchings62776d02010-06-23 11:30:07 +00001083 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001084
Ben Hutchingsd615c032013-10-08 17:33:20 +01001085 EFX_ASSERT_RESET_SERIALISED(efx);
1086
Ben Hutchings8ceee662008-04-27 12:55:59 +01001087 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001088 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001089 mutex_unlock(&efx->mac_lock);
1090
1091 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001092 netif_addr_lock_bh(efx->net_dev);
1093 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsd615c032013-10-08 17:33:20 +01001094
1095 cancel_delayed_work_sync(&efx->monitor_work);
1096 efx_selftest_async_cancel(efx);
1097 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001098}
1099
1100static void efx_fini_port(struct efx_nic *efx)
1101{
Ben Hutchings62776d02010-06-23 11:30:07 +00001102 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001103
1104 if (!efx->port_initialized)
1105 return;
1106
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001107 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001108 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001109
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001110 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001111 efx_link_status_changed(efx);
1112}
1113
1114static void efx_remove_port(struct efx_nic *efx)
1115{
Ben Hutchings62776d02010-06-23 11:30:07 +00001116 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001117
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001118 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001119}
1120
1121/**************************************************************************
1122 *
1123 * NIC handling
1124 *
1125 **************************************************************************/
1126
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01001127static LIST_HEAD(efx_primary_list);
1128static LIST_HEAD(efx_unassociated_list);
1129
1130static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
1131{
1132 return left->type == right->type &&
1133 left->vpd_sn && right->vpd_sn &&
1134 !strcmp(left->vpd_sn, right->vpd_sn);
1135}
1136
1137static void efx_associate(struct efx_nic *efx)
1138{
1139 struct efx_nic *other, *next;
1140
1141 if (efx->primary == efx) {
1142 /* Adding primary function; look for secondaries */
1143
1144 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
1145 list_add_tail(&efx->node, &efx_primary_list);
1146
1147 list_for_each_entry_safe(other, next, &efx_unassociated_list,
1148 node) {
1149 if (efx_same_controller(efx, other)) {
1150 list_del(&other->node);
1151 netif_dbg(other, probe, other->net_dev,
1152 "moving to secondary list of %s %s\n",
1153 pci_name(efx->pci_dev),
1154 efx->net_dev->name);
1155 list_add_tail(&other->node,
1156 &efx->secondary_list);
1157 other->primary = efx;
1158 }
1159 }
1160 } else {
1161 /* Adding secondary function; look for primary */
1162
1163 list_for_each_entry(other, &efx_primary_list, node) {
1164 if (efx_same_controller(efx, other)) {
1165 netif_dbg(efx, probe, efx->net_dev,
1166 "adding to secondary list of %s %s\n",
1167 pci_name(other->pci_dev),
1168 other->net_dev->name);
1169 list_add_tail(&efx->node,
1170 &other->secondary_list);
1171 efx->primary = other;
1172 return;
1173 }
1174 }
1175
1176 netif_dbg(efx, probe, efx->net_dev,
1177 "adding to unassociated list\n");
1178 list_add_tail(&efx->node, &efx_unassociated_list);
1179 }
1180}
1181
1182static void efx_dissociate(struct efx_nic *efx)
1183{
1184 struct efx_nic *other, *next;
1185
1186 list_del(&efx->node);
1187 efx->primary = NULL;
1188
1189 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
1190 list_del(&other->node);
1191 netif_dbg(other, probe, other->net_dev,
1192 "moving to unassociated list\n");
1193 list_add_tail(&other->node, &efx_unassociated_list);
1194 other->primary = NULL;
1195 }
1196}
1197
Ben Hutchings8ceee662008-04-27 12:55:59 +01001198/* This configures the PCI device to enable I/O and DMA. */
1199static int efx_init_io(struct efx_nic *efx)
1200{
1201 struct pci_dev *pci_dev = efx->pci_dev;
1202 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001203 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Shradha Shah02246a72015-05-06 00:58:14 +01001204 int rc, bar;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001205
Ben Hutchings62776d02010-06-23 11:30:07 +00001206 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001207
Shradha Shah02246a72015-05-06 00:58:14 +01001208 bar = efx->type->mem_bar;
1209
Ben Hutchings8ceee662008-04-27 12:55:59 +01001210 rc = pci_enable_device(pci_dev);
1211 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001212 netif_err(efx, probe, efx->net_dev,
1213 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001214 goto fail1;
1215 }
1216
1217 pci_set_master(pci_dev);
1218
1219 /* Set the PCI DMA mask. Try all possibilities from our
1220 * genuine mask down to 32 bits, because some architectures
1221 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1222 * masks event though they reject 46 bit masks.
1223 */
1224 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001225 if (dma_supported(&pci_dev->dev, dma_mask)) {
Russell King9663ded2013-06-26 23:49:11 +01001226 rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001227 if (rc == 0)
1228 break;
1229 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001230 dma_mask >>= 1;
1231 }
1232 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001233 netif_err(efx, probe, efx->net_dev,
1234 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001235 goto fail2;
1236 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001237 netif_dbg(efx, probe, efx->net_dev,
1238 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001239
Shradha Shah02246a72015-05-06 00:58:14 +01001240 efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
1241 rc = pci_request_region(pci_dev, bar, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001242 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001243 netif_err(efx, probe, efx->net_dev,
1244 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001245 rc = -EIO;
1246 goto fail3;
1247 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001248 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001249 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001250 netif_err(efx, probe, efx->net_dev,
1251 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001252 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001253 rc = -ENOMEM;
1254 goto fail4;
1255 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001256 netif_dbg(efx, probe, efx->net_dev,
1257 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001258 (unsigned long long)efx->membase_phys, mem_map_size,
1259 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001260
1261 return 0;
1262
1263 fail4:
Shradha Shah02246a72015-05-06 00:58:14 +01001264 pci_release_region(efx->pci_dev, bar);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001265 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001266 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001267 fail2:
1268 pci_disable_device(efx->pci_dev);
1269 fail1:
1270 return rc;
1271}
1272
1273static void efx_fini_io(struct efx_nic *efx)
1274{
Shradha Shah02246a72015-05-06 00:58:14 +01001275 int bar;
1276
Ben Hutchings62776d02010-06-23 11:30:07 +00001277 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001278
1279 if (efx->membase) {
1280 iounmap(efx->membase);
1281 efx->membase = NULL;
1282 }
1283
1284 if (efx->membase_phys) {
Shradha Shah02246a72015-05-06 00:58:14 +01001285 bar = efx->type->mem_bar;
1286 pci_release_region(efx->pci_dev, bar);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001287 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001288 }
1289
1290 pci_disable_device(efx->pci_dev);
1291}
1292
Jon Cooper267c0152015-05-06 00:59:38 +01001293void efx_set_default_rx_indir_table(struct efx_nic *efx)
1294{
1295 size_t i;
1296
1297 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
1298 efx->rx_indir_table[i] =
1299 ethtool_rxfh_indir_default(i, efx->rss_spread);
1300}
1301
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001302static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001303{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001304 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001305 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001306 int cpu;
1307
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001308 if (rss_cpus) {
1309 count = rss_cpus;
1310 } else {
1311 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1312 netif_warn(efx, probe, efx->net_dev,
1313 "RSS disabled due to allocation failure\n");
1314 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001315 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001316
1317 count = 0;
1318 for_each_online_cpu(cpu) {
1319 if (!cpumask_test_cpu(cpu, thread_mask)) {
1320 ++count;
1321 cpumask_or(thread_mask, thread_mask,
1322 topology_thread_cpumask(cpu));
1323 }
1324 }
1325
1326 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001327 }
1328
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001329 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1330 * table entries that are inaccessible to VFs
1331 */
Shradha Shah7fa8d542015-05-06 00:55:13 +01001332#ifdef CONFIG_SFC_SRIOV
1333 if (efx->type->sriov_wanted) {
1334 if (efx->type->sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1335 count > efx_vf_size(efx)) {
1336 netif_warn(efx, probe, efx->net_dev,
1337 "Reducing number of RSS channels from %u to %u for "
1338 "VF support. Increase vf-msix-limit to use more "
1339 "channels on the PF.\n",
1340 count, efx_vf_size(efx));
1341 count = efx_vf_size(efx);
1342 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001343 }
Shradha Shah7fa8d542015-05-06 00:55:13 +01001344#endif
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001345
Ben Hutchings46123d02008-09-01 12:47:33 +01001346 return count;
1347}
1348
1349/* Probe the number and type of interrupts we are able to obtain, and
1350 * the resulting numbers of channels and RX queues.
1351 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001352static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001353{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001354 unsigned int extra_channels = 0;
1355 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001356 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001357
Ben Hutchings7f967c02012-02-13 23:45:02 +00001358 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1359 if (efx->extra_channel_type[i])
1360 ++extra_channels;
1361
Ben Hutchings8ceee662008-04-27 12:55:59 +01001362 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001363 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001364 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001365
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001366 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001367 if (separate_tx_channels)
1368 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001369 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001370 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001371
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001372 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001373 xentries[i].entry = i;
Alexander Gordeev184603d2014-02-18 11:12:00 +01001374 rc = pci_enable_msix_range(efx->pci_dev,
1375 xentries, 1, n_channels);
1376 if (rc < 0) {
1377 /* Fall back to single channel MSI */
1378 efx->interrupt_mode = EFX_INT_MODE_MSI;
1379 netif_err(efx, drv, efx->net_dev,
1380 "could not enable MSI-X\n");
1381 } else if (rc < n_channels) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001382 netif_err(efx, drv, efx->net_dev,
1383 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001384 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001385 netif_err(efx, drv, efx->net_dev,
1386 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001387 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001388 }
1389
Alexander Gordeev184603d2014-02-18 11:12:00 +01001390 if (rc > 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001391 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001392 if (n_channels > extra_channels)
1393 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001394 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001395 efx->n_tx_channels = max(n_channels / 2, 1U);
1396 efx->n_rx_channels = max(n_channels -
1397 efx->n_tx_channels,
1398 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001399 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001400 efx->n_tx_channels = n_channels;
1401 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001402 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001403 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001404 efx_get_channel(efx, i)->irq =
1405 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001406 }
1407 }
1408
1409 /* Try single interrupt MSI */
1410 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001411 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001412 efx->n_rx_channels = 1;
1413 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001414 rc = pci_enable_msi(efx->pci_dev);
1415 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001416 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001417 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001418 netif_err(efx, drv, efx->net_dev,
1419 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001420 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1421 }
1422 }
1423
1424 /* Assume legacy interrupts */
1425 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001426 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001427 efx->n_rx_channels = 1;
1428 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001429 efx->legacy_irq = efx->pci_dev->irq;
1430 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001431
Ben Hutchings7f967c02012-02-13 23:45:02 +00001432 /* Assign extra channels if possible */
1433 j = efx->n_channels;
1434 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1435 if (!efx->extra_channel_type[i])
1436 continue;
1437 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1438 efx->n_channels <= extra_channels) {
1439 efx->extra_channel_type[i]->handle_no_channel(efx);
1440 } else {
1441 --j;
1442 efx_get_channel(efx, j)->type =
1443 efx->extra_channel_type[i];
1444 }
1445 }
1446
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001447 /* RSS might be usable on VFs even if it is disabled on the PF */
Shradha Shah7fa8d542015-05-06 00:55:13 +01001448#ifdef CONFIG_SFC_SRIOV
1449 if (efx->type->sriov_wanted) {
1450 efx->rss_spread = ((efx->n_rx_channels > 1 ||
1451 !efx->type->sriov_wanted(efx)) ?
1452 efx->n_rx_channels : efx_vf_size(efx));
1453 return 0;
1454 }
1455#endif
1456 efx->rss_spread = efx->n_rx_channels;
Shradha Shah6f7f8aa2015-05-06 01:00:07 +01001457
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001458 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001459}
1460
Jon Cooper261e4d92013-04-15 18:51:54 +01001461static int efx_soft_enable_interrupts(struct efx_nic *efx)
Ben Hutchingsd8291182012-10-05 23:35:41 +01001462{
Jon Cooper261e4d92013-04-15 18:51:54 +01001463 struct efx_channel *channel, *end_channel;
1464 int rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001465
1466 BUG_ON(efx->state == STATE_DISABLED);
1467
1468 efx->irq_soft_enabled = true;
1469 smp_wmb();
1470
1471 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001472 if (!channel->type->keep_eventq) {
1473 rc = efx_init_eventq(channel);
1474 if (rc)
1475 goto fail;
1476 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001477 efx_start_eventq(channel);
1478 }
1479
1480 efx_mcdi_mode_event(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +01001481
1482 return 0;
1483fail:
1484 end_channel = channel;
1485 efx_for_each_channel(channel, efx) {
1486 if (channel == end_channel)
1487 break;
1488 efx_stop_eventq(channel);
1489 if (!channel->type->keep_eventq)
1490 efx_fini_eventq(channel);
1491 }
1492
1493 return rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001494}
1495
1496static void efx_soft_disable_interrupts(struct efx_nic *efx)
1497{
1498 struct efx_channel *channel;
1499
1500 if (efx->state == STATE_DISABLED)
1501 return;
1502
1503 efx_mcdi_mode_poll(efx);
1504
1505 efx->irq_soft_enabled = false;
1506 smp_wmb();
1507
1508 if (efx->legacy_irq)
1509 synchronize_irq(efx->legacy_irq);
1510
1511 efx_for_each_channel(channel, efx) {
1512 if (channel->irq)
1513 synchronize_irq(channel->irq);
1514
1515 efx_stop_eventq(channel);
1516 if (!channel->type->keep_eventq)
1517 efx_fini_eventq(channel);
1518 }
Ben Hutchingscade7152013-08-27 23:12:31 +01001519
1520 /* Flush the asynchronous MCDI request queue */
1521 efx_mcdi_flush_async(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01001522}
1523
Jon Cooper261e4d92013-04-15 18:51:54 +01001524static int efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001525{
Jon Cooper261e4d92013-04-15 18:51:54 +01001526 struct efx_channel *channel, *end_channel;
1527 int rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001528
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001529 BUG_ON(efx->state == STATE_DISABLED);
1530
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001531 if (efx->eeh_disabled_legacy_irq) {
1532 enable_irq(efx->legacy_irq);
1533 efx->eeh_disabled_legacy_irq = false;
1534 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001535
Ben Hutchings86094f72013-08-21 19:51:04 +01001536 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001537
1538 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001539 if (channel->type->keep_eventq) {
1540 rc = efx_init_eventq(channel);
1541 if (rc)
1542 goto fail;
1543 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001544 }
1545
Jon Cooper261e4d92013-04-15 18:51:54 +01001546 rc = efx_soft_enable_interrupts(efx);
1547 if (rc)
1548 goto fail;
1549
1550 return 0;
1551
1552fail:
1553 end_channel = channel;
1554 efx_for_each_channel(channel, efx) {
1555 if (channel == end_channel)
1556 break;
1557 if (channel->type->keep_eventq)
1558 efx_fini_eventq(channel);
1559 }
1560
1561 efx->type->irq_disable_non_ev(efx);
1562
1563 return rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001564}
1565
Ben Hutchingsd8291182012-10-05 23:35:41 +01001566static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001567{
1568 struct efx_channel *channel;
1569
Ben Hutchingsd8291182012-10-05 23:35:41 +01001570 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001571
1572 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001573 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001574 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001575 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001576
Ben Hutchings86094f72013-08-21 19:51:04 +01001577 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001578}
1579
Ben Hutchings8ceee662008-04-27 12:55:59 +01001580static void efx_remove_interrupts(struct efx_nic *efx)
1581{
1582 struct efx_channel *channel;
1583
1584 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001585 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001586 channel->irq = 0;
1587 pci_disable_msi(efx->pci_dev);
1588 pci_disable_msix(efx->pci_dev);
1589
1590 /* Remove legacy interrupt */
1591 efx->legacy_irq = 0;
1592}
1593
Ben Hutchings8831da72008-09-01 12:47:48 +01001594static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001595{
Ben Hutchings602a5322011-05-16 17:32:39 +01001596 struct efx_channel *channel;
1597 struct efx_tx_queue *tx_queue;
1598
Ben Hutchings97653432011-01-12 18:26:56 +00001599 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001600 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001601
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001602 /* We need to mark which channels really have RX and TX
1603 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001604 * RX-only and TX-only channels.
1605 */
1606 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001607 if (channel->channel < efx->n_rx_channels)
1608 channel->rx_queue.core_index = channel->channel;
1609 else
1610 channel->rx_queue.core_index = -1;
1611
Ben Hutchings602a5322011-05-16 17:32:39 +01001612 efx_for_each_channel_tx_queue(tx_queue, channel)
1613 tx_queue->queue -= (efx->tx_channel_offset *
1614 EFX_TXQ_TYPES);
1615 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001616}
1617
1618static int efx_probe_nic(struct efx_nic *efx)
1619{
1620 int rc;
1621
Ben Hutchings62776d02010-06-23 11:30:07 +00001622 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001623
1624 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001625 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001626 if (rc)
1627 return rc;
1628
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001629 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001630 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001631 rc = efx_probe_interrupts(efx);
1632 if (rc)
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001633 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001634
Daniel Pieczko52ad7622014-04-01 13:10:34 +01001635 efx_set_channels(efx);
1636
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001637 rc = efx->type->dimension_resources(efx);
1638 if (rc)
1639 goto fail2;
Ben Hutchings28e47c42012-02-15 01:58:49 +00001640
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001641 if (efx->n_channels > 1)
Jon Cooper267c0152015-05-06 00:59:38 +01001642 netdev_rss_key_fill(&efx->rx_hash_key,
1643 sizeof(efx->rx_hash_key));
1644 efx_set_default_rx_indir_table(efx);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001645
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001646 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1647 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001648
1649 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001650 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1651 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001652
1653 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001654
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001655fail2:
1656 efx_remove_interrupts(efx);
1657fail1:
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001658 efx->type->remove(efx);
1659 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001660}
1661
1662static void efx_remove_nic(struct efx_nic *efx)
1663{
Ben Hutchings62776d02010-06-23 11:30:07 +00001664 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001665
1666 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001667 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001668}
1669
Ben Hutchingsadd72472012-11-08 01:46:53 +00001670static int efx_probe_filters(struct efx_nic *efx)
1671{
1672 int rc;
1673
1674 spin_lock_init(&efx->filter_lock);
1675
1676 rc = efx->type->filter_table_probe(efx);
1677 if (rc)
1678 return rc;
1679
1680#ifdef CONFIG_RFS_ACCEL
1681 if (efx->type->offload_features & NETIF_F_NTUPLE) {
1682 efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
1683 sizeof(*efx->rps_flow_id),
1684 GFP_KERNEL);
1685 if (!efx->rps_flow_id) {
1686 efx->type->filter_table_remove(efx);
1687 return -ENOMEM;
1688 }
1689 }
1690#endif
1691
1692 return 0;
1693}
1694
1695static void efx_remove_filters(struct efx_nic *efx)
1696{
1697#ifdef CONFIG_RFS_ACCEL
1698 kfree(efx->rps_flow_id);
1699#endif
1700 efx->type->filter_table_remove(efx);
1701}
1702
1703static void efx_restore_filters(struct efx_nic *efx)
1704{
1705 efx->type->filter_table_restore(efx);
1706}
1707
Ben Hutchings8ceee662008-04-27 12:55:59 +01001708/**************************************************************************
1709 *
1710 * NIC startup/shutdown
1711 *
1712 *************************************************************************/
1713
1714static int efx_probe_all(struct efx_nic *efx)
1715{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001716 int rc;
1717
Ben Hutchings8ceee662008-04-27 12:55:59 +01001718 rc = efx_probe_nic(efx);
1719 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001720 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001721 goto fail1;
1722 }
1723
Ben Hutchings8ceee662008-04-27 12:55:59 +01001724 rc = efx_probe_port(efx);
1725 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001726 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001727 goto fail2;
1728 }
1729
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001730 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1731 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1732 rc = -EINVAL;
1733 goto fail3;
1734 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001735 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001736
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001737#ifdef CONFIG_SFC_SRIOV
1738 rc = efx->type->vswitching_probe(efx);
1739 if (rc) /* not fatal; the PF will still work fine */
1740 netif_warn(efx, probe, efx->net_dev,
1741 "failed to setup vswitching rc=%d;"
1742 " VFs may not function\n", rc);
1743#endif
1744
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001745 rc = efx_probe_filters(efx);
1746 if (rc) {
1747 netif_err(efx, probe, efx->net_dev,
1748 "failed to create filter tables\n");
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001749 goto fail4;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001750 }
1751
Ben Hutchings7f967c02012-02-13 23:45:02 +00001752 rc = efx_probe_channels(efx);
1753 if (rc)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001754 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001755
Ben Hutchings8ceee662008-04-27 12:55:59 +01001756 return 0;
1757
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001758 fail5:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001759 efx_remove_filters(efx);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001760 fail4:
1761#ifdef CONFIG_SFC_SRIOV
1762 efx->type->vswitching_remove(efx);
1763#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01001764 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001765 efx_remove_port(efx);
1766 fail2:
1767 efx_remove_nic(efx);
1768 fail1:
1769 return rc;
1770}
1771
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001772/* If the interface is supposed to be running but is not, start
1773 * the hardware and software data path, regular activity for the port
1774 * (MAC statistics, link polling, etc.) and schedule the port to be
1775 * reconfigured. Interrupts must already be enabled. This function
1776 * is safe to call multiple times, so long as the NIC is not disabled.
1777 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001778 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001779static void efx_start_all(struct efx_nic *efx)
1780{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001781 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001782 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001783
1784 /* Check that it is appropriate to restart the interface. All
1785 * of these flags are safe to read under just the rtnl lock */
Edward Creee2835462014-04-16 19:27:48 +01001786 if (efx->port_enabled || !netif_running(efx->net_dev) ||
1787 efx->reset_pending)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001788 return;
1789
Ben Hutchings8ceee662008-04-27 12:55:59 +01001790 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001791 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001792
Alexandre Rames626950d2013-01-14 17:20:22 +00001793 /* Start the hardware monitor if there is one */
1794 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001795 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1796 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001797
1798 /* If link state detection is normally event-driven, we have
1799 * to poll now because we could have missed a change
1800 */
1801 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001802 mutex_lock(&efx->mac_lock);
1803 if (efx->phy_op->poll(efx))
1804 efx_link_status_changed(efx);
1805 mutex_unlock(&efx->mac_lock);
1806 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001807
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001808 efx->type->start_stats(efx);
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001809 efx->type->pull_stats(efx);
1810 spin_lock_bh(&efx->stats_lock);
1811 efx->type->update_stats(efx, NULL, NULL);
1812 spin_unlock_bh(&efx->stats_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001813}
1814
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001815/* Quiesce the hardware and software data path, and regular activity
1816 * for the port without bringing the link down. Safe to call multiple
1817 * times with the NIC in almost any state, but interrupts should be
1818 * enabled. Requires the RTNL lock.
1819 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001820static void efx_stop_all(struct efx_nic *efx)
1821{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001822 EFX_ASSERT_RESET_SERIALISED(efx);
1823
1824 /* port_enabled can be read safely under the rtnl lock */
1825 if (!efx->port_enabled)
1826 return;
1827
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001828 /* update stats before we go down so we can accurately count
1829 * rx_nodesc_drops
1830 */
1831 efx->type->pull_stats(efx);
1832 spin_lock_bh(&efx->stats_lock);
1833 efx->type->update_stats(efx, NULL, NULL);
1834 spin_unlock_bh(&efx->stats_lock);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001835 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001836 efx_stop_port(efx);
1837
Ben Hutchings29c69a42013-01-28 19:01:06 +00001838 /* Stop the kernel transmit interface. This is only valid if
1839 * the device is stopped or detached; otherwise the watchdog
1840 * may fire immediately.
1841 */
1842 WARN_ON(netif_running(efx->net_dev) &&
1843 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001844 netif_tx_disable(efx->net_dev);
1845
1846 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001847}
1848
1849static void efx_remove_all(struct efx_nic *efx)
1850{
Ben Hutchings46426102010-09-10 06:42:33 +00001851 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001852 efx_remove_filters(efx);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001853#ifdef CONFIG_SFC_SRIOV
1854 efx->type->vswitching_remove(efx);
1855#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01001856 efx_remove_port(efx);
1857 efx_remove_nic(efx);
1858}
1859
Ben Hutchings8ceee662008-04-27 12:55:59 +01001860/**************************************************************************
1861 *
1862 * Interrupt moderation
1863 *
1864 **************************************************************************/
1865
Ben Hutchingscc180b62011-12-08 19:51:47 +00001866static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001867{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001868 if (usecs == 0)
1869 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001870 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001871 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001872 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001873}
1874
Ben Hutchings8ceee662008-04-27 12:55:59 +01001875/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001876int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1877 unsigned int rx_usecs, bool rx_adaptive,
1878 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001879{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001880 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001881 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1882 efx->timer_quantum_ns,
1883 1000);
1884 unsigned int tx_ticks;
1885 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001886
1887 EFX_ASSERT_RESET_SERIALISED(efx);
1888
Ben Hutchingscc180b62011-12-08 19:51:47 +00001889 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001890 return -EINVAL;
1891
Ben Hutchingscc180b62011-12-08 19:51:47 +00001892 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1893 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1894
Ben Hutchings9e393b32011-09-05 07:43:04 +00001895 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1896 !rx_may_override_tx) {
1897 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1898 "RX and TX IRQ moderation must be equal\n");
1899 return -EINVAL;
1900 }
1901
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001902 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001903 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001904 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001905 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001906 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001907 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001908 channel->irq_moderation = tx_ticks;
1909 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001910
1911 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001912}
1913
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001914void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1915 unsigned int *rx_usecs, bool *rx_adaptive)
1916{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001917 /* We must round up when converting ticks to microseconds
1918 * because we round down when converting the other way.
1919 */
1920
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001921 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001922 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1923 efx->timer_quantum_ns,
1924 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001925
1926 /* If channels are shared between RX and TX, so is IRQ
1927 * moderation. Otherwise, IRQ moderation is the same for all
1928 * TX channels and is not adaptive.
1929 */
1930 if (efx->tx_channel_offset == 0)
1931 *tx_usecs = *rx_usecs;
1932 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001933 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001934 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001935 efx->timer_quantum_ns,
1936 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001937}
1938
Ben Hutchings8ceee662008-04-27 12:55:59 +01001939/**************************************************************************
1940 *
1941 * Hardware monitor
1942 *
1943 **************************************************************************/
1944
Ben Hutchingse254c272010-09-20 08:44:10 +00001945/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001946static void efx_monitor(struct work_struct *data)
1947{
1948 struct efx_nic *efx = container_of(data, struct efx_nic,
1949 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001950
Ben Hutchings62776d02010-06-23 11:30:07 +00001951 netif_vdbg(efx, timer, efx->net_dev,
1952 "hardware monitor executing on CPU %d\n",
1953 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001954 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001955
Ben Hutchings8ceee662008-04-27 12:55:59 +01001956 /* If the mac_lock is already held then it is likely a port
1957 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001958 * most of the work of monitor() anyway. */
1959 if (mutex_trylock(&efx->mac_lock)) {
1960 if (efx->port_enabled)
1961 efx->type->monitor(efx);
1962 mutex_unlock(&efx->mac_lock);
1963 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001964
Ben Hutchings8ceee662008-04-27 12:55:59 +01001965 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1966 efx_monitor_interval);
1967}
1968
1969/**************************************************************************
1970 *
1971 * ioctls
1972 *
1973 *************************************************************************/
1974
1975/* Net device ioctl
1976 * Context: process, rtnl_lock() held.
1977 */
1978static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1979{
Ben Hutchings767e4682008-09-01 12:43:14 +01001980 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001981 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001982
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001983 if (cmd == SIOCSHWTSTAMP)
Ben Hutchings433dc9b2013-11-14 01:26:21 +00001984 return efx_ptp_set_ts_config(efx, ifr);
1985 if (cmd == SIOCGHWTSTAMP)
1986 return efx_ptp_get_ts_config(efx, ifr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001987
Ben Hutchings68e7f452009-04-29 08:05:08 +00001988 /* Convert phy_id from older PRTAD/DEVAD format */
1989 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1990 (data->phy_id & 0xfc00) == 0x0400)
1991 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1992
1993 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001994}
1995
1996/**************************************************************************
1997 *
1998 * NAPI interface
1999 *
2000 **************************************************************************/
2001
Ben Hutchings7f967c02012-02-13 23:45:02 +00002002static void efx_init_napi_channel(struct efx_channel *channel)
2003{
2004 struct efx_nic *efx = channel->efx;
2005
2006 channel->napi_dev = efx->net_dev;
2007 netif_napi_add(channel->napi_dev, &channel->napi_str,
2008 efx_poll, napi_weight);
Alexandre Rames36763262014-07-22 14:03:25 +01002009 napi_hash_add(&channel->napi_str);
2010 efx_channel_init_lock(channel);
Ben Hutchings7f967c02012-02-13 23:45:02 +00002011}
2012
Ben Hutchingse8f14992010-12-07 19:47:34 +00002013static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002014{
2015 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002016
Ben Hutchings7f967c02012-02-13 23:45:02 +00002017 efx_for_each_channel(channel, efx)
2018 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00002019}
2020
2021static void efx_fini_napi_channel(struct efx_channel *channel)
2022{
Alexandre Rames36763262014-07-22 14:03:25 +01002023 if (channel->napi_dev) {
Ben Hutchingse8f14992010-12-07 19:47:34 +00002024 netif_napi_del(&channel->napi_str);
Alexandre Rames36763262014-07-22 14:03:25 +01002025 napi_hash_del(&channel->napi_str);
2026 }
Ben Hutchingse8f14992010-12-07 19:47:34 +00002027 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002028}
2029
2030static void efx_fini_napi(struct efx_nic *efx)
2031{
2032 struct efx_channel *channel;
2033
Ben Hutchingse8f14992010-12-07 19:47:34 +00002034 efx_for_each_channel(channel, efx)
2035 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002036}
2037
2038/**************************************************************************
2039 *
2040 * Kernel netpoll interface
2041 *
2042 *************************************************************************/
2043
2044#ifdef CONFIG_NET_POLL_CONTROLLER
2045
2046/* Although in the common case interrupts will be disabled, this is not
2047 * guaranteed. However, all our work happens inside the NAPI callback,
2048 * so no locking is required.
2049 */
2050static void efx_netpoll(struct net_device *net_dev)
2051{
Ben Hutchings767e4682008-09-01 12:43:14 +01002052 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002053 struct efx_channel *channel;
2054
Ben Hutchings64ee3122008-09-01 12:47:38 +01002055 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002056 efx_schedule_channel(channel);
2057}
2058
2059#endif
2060
Alexandre Rames36763262014-07-22 14:03:25 +01002061#ifdef CONFIG_NET_RX_BUSY_POLL
2062static int efx_busy_poll(struct napi_struct *napi)
2063{
2064 struct efx_channel *channel =
2065 container_of(napi, struct efx_channel, napi_str);
2066 struct efx_nic *efx = channel->efx;
2067 int budget = 4;
2068 int old_rx_packets, rx_packets;
2069
2070 if (!netif_running(efx->net_dev))
2071 return LL_FLUSH_FAILED;
2072
2073 if (!efx_channel_lock_poll(channel))
2074 return LL_FLUSH_BUSY;
2075
2076 old_rx_packets = channel->rx_queue.rx_packets;
2077 efx_process_channel(channel, budget);
2078
2079 rx_packets = channel->rx_queue.rx_packets - old_rx_packets;
2080
2081 /* There is no race condition with NAPI here.
2082 * NAPI will automatically be rescheduled if it yielded during busy
2083 * polling, because it was not able to take the lock and thus returned
2084 * the full budget.
2085 */
2086 efx_channel_unlock_poll(channel);
2087
2088 return rx_packets;
2089}
2090#endif
2091
Ben Hutchings8ceee662008-04-27 12:55:59 +01002092/**************************************************************************
2093 *
2094 * Kernel net device interface
2095 *
2096 *************************************************************************/
2097
2098/* Context: process, rtnl_lock() held. */
2099static int efx_net_open(struct net_device *net_dev)
2100{
Ben Hutchings767e4682008-09-01 12:43:14 +01002101 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002102 int rc;
2103
Ben Hutchings62776d02010-06-23 11:30:07 +00002104 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
2105 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01002106
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002107 rc = efx_check_disabled(efx);
2108 if (rc)
2109 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01002110 if (efx->phy_mode & PHY_MODE_SPECIAL)
2111 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002112 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
2113 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01002114
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00002115 /* Notify the kernel of the link state polled during driver load,
2116 * before the monitor starts running */
2117 efx_link_status_changed(efx);
2118
Ben Hutchings8ceee662008-04-27 12:55:59 +01002119 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002120 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002121 return 0;
2122}
2123
2124/* Context: process, rtnl_lock() held.
2125 * Note that the kernel will ignore our return code; this method
2126 * should really be a void.
2127 */
2128static int efx_net_stop(struct net_device *net_dev)
2129{
Ben Hutchings767e4682008-09-01 12:43:14 +01002130 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002131
Ben Hutchings62776d02010-06-23 11:30:07 +00002132 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
2133 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01002134
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002135 /* Stop the device and flush all the channels */
2136 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002137
2138 return 0;
2139}
2140
Ben Hutchings5b9e2072008-05-16 21:18:14 +01002141/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00002142static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
2143 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002144{
Ben Hutchings767e4682008-09-01 12:43:14 +01002145 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002146
Ben Hutchings55edc6e2009-11-25 16:11:35 +00002147 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00002148 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01002149 spin_unlock_bh(&efx->stats_lock);
2150
Ben Hutchings8ceee662008-04-27 12:55:59 +01002151 return stats;
2152}
2153
2154/* Context: netif_tx_lock held, BHs disabled. */
2155static void efx_watchdog(struct net_device *net_dev)
2156{
Ben Hutchings767e4682008-09-01 12:43:14 +01002157 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002158
Ben Hutchings62776d02010-06-23 11:30:07 +00002159 netif_err(efx, tx_err, efx->net_dev,
2160 "TX stuck with port_enabled=%d: resetting channels\n",
2161 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002162
Ben Hutchings739bb23d2008-11-04 20:35:36 +00002163 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002164}
2165
2166
2167/* Context: process, rtnl_lock() held. */
2168static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2169{
Ben Hutchings767e4682008-09-01 12:43:14 +01002170 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002171 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002172
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002173 rc = efx_check_disabled(efx);
2174 if (rc)
2175 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002176 if (new_mtu > EFX_MAX_MTU)
2177 return -EINVAL;
2178
Ben Hutchings62776d02010-06-23 11:30:07 +00002179 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002180
Ben Hutchings29c69a42013-01-28 19:01:06 +00002181 efx_device_detach_sync(efx);
2182 efx_stop_all(efx);
2183
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002184 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002185 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01002186 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002187 mutex_unlock(&efx->mac_lock);
2188
Ben Hutchings8ceee662008-04-27 12:55:59 +01002189 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00002190 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002191 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002192}
2193
2194static int efx_set_mac_address(struct net_device *net_dev, void *data)
2195{
Ben Hutchings767e4682008-09-01 12:43:14 +01002196 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002197 struct sockaddr *addr = data;
Ben Hutchingse0b3ae32014-02-12 18:59:54 +00002198 u8 *new_addr = addr->sa_data;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002199
Ben Hutchings8ceee662008-04-27 12:55:59 +01002200 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002201 netif_err(efx, drv, efx->net_dev,
2202 "invalid ethernet MAC address requested: %pM\n",
2203 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002204 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002205 }
2206
Edward Creecd84ff42014-03-07 18:27:41 +00002207 ether_addr_copy(net_dev->dev_addr, new_addr);
Shradha Shah7fa8d542015-05-06 00:55:13 +01002208 if (efx->type->sriov_mac_address_changed)
2209 efx->type->sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002210
2211 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002212 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002213 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002214 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002215
2216 return 0;
2217}
2218
Ben Hutchingsa816f752008-09-01 12:49:12 +01002219/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002220static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002221{
Ben Hutchings767e4682008-09-01 12:43:14 +01002222 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002223
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002224 if (efx->port_enabled)
2225 queue_work(efx->workqueue, &efx->mac_work);
2226 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002227}
2228
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002229static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002230{
2231 struct efx_nic *efx = netdev_priv(net_dev);
2232
2233 /* If disabling RX n-tuple filtering, clear existing filters */
2234 if (net_dev->features & ~data & NETIF_F_NTUPLE)
Ben Hutchingsfbd79122013-11-21 19:15:03 +00002235 return efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002236
2237 return 0;
2238}
2239
Shradha Shah7fa8d542015-05-06 00:55:13 +01002240static const struct net_device_ops efx_netdev_ops = {
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002241 .ndo_open = efx_net_open,
2242 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002243 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002244 .ndo_tx_timeout = efx_watchdog,
2245 .ndo_start_xmit = efx_hard_start_xmit,
2246 .ndo_validate_addr = eth_validate_addr,
2247 .ndo_do_ioctl = efx_ioctl,
2248 .ndo_change_mtu = efx_change_mtu,
2249 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002250 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002251 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002252#ifdef CONFIG_SFC_SRIOV
Shradha Shah7fa8d542015-05-06 00:55:13 +01002253 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2254 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2255 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2256 .ndo_get_vf_config = efx_sriov_get_vf_config,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002257#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002258#ifdef CONFIG_NET_POLL_CONTROLLER
2259 .ndo_poll_controller = efx_netpoll,
2260#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002261 .ndo_setup_tc = efx_setup_tc,
Alexandre Rames36763262014-07-22 14:03:25 +01002262#ifdef CONFIG_NET_RX_BUSY_POLL
2263 .ndo_busy_poll = efx_busy_poll,
2264#endif
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002265#ifdef CONFIG_RFS_ACCEL
2266 .ndo_rx_flow_steer = efx_filter_rfs,
2267#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002268};
2269
Ben Hutchings7dde5962008-12-12 22:09:38 -08002270static void efx_update_name(struct efx_nic *efx)
2271{
2272 strcpy(efx->name, efx->net_dev->name);
2273 efx_mtd_rename(efx);
2274 efx_set_channel_names(efx);
2275}
2276
Ben Hutchings8ceee662008-04-27 12:55:59 +01002277static int efx_netdev_event(struct notifier_block *this,
2278 unsigned long event, void *ptr)
2279{
Jiri Pirko351638e2013-05-28 01:30:21 +00002280 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002281
Shradha Shah7fa8d542015-05-06 00:55:13 +01002282 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
Ben Hutchings7dde5962008-12-12 22:09:38 -08002283 event == NETDEV_CHANGENAME)
2284 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002285
2286 return NOTIFY_DONE;
2287}
2288
2289static struct notifier_block efx_netdev_notifier = {
2290 .notifier_call = efx_netdev_event,
2291};
2292
Ben Hutchings06d5e192008-12-12 21:47:23 -08002293static ssize_t
2294show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2295{
2296 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2297 return sprintf(buf, "%d\n", efx->phy_type);
2298}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002299static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002300
Ben Hutchings8ceee662008-04-27 12:55:59 +01002301static int efx_register_netdev(struct efx_nic *efx)
2302{
2303 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002304 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002305 int rc;
2306
2307 net_dev->watchdog_timeo = 5 * HZ;
2308 net_dev->irq = efx->pci_dev->irq;
Shradha Shah7fa8d542015-05-06 00:55:13 +01002309 net_dev->netdev_ops = &efx_netdev_ops;
2310 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
Ben Hutchings8127d662013-08-29 19:19:29 +01002311 net_dev->priv_flags |= IFF_UNICAST_FLT;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002312 net_dev->ethtool_ops = &efx_ethtool_ops;
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002313 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002314
Ben Hutchings7dde5962008-12-12 22:09:38 -08002315 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002316
Ben Hutchings7153f622012-07-27 20:50:52 +01002317 /* Enable resets to be scheduled and check whether any were
2318 * already requested. If so, the NIC is probably hosed so we
2319 * abort.
2320 */
2321 efx->state = STATE_READY;
2322 smp_mb(); /* ensure we change state before checking reset_pending */
2323 if (efx->reset_pending) {
2324 netif_err(efx, probe, efx->net_dev,
2325 "aborting probe due to scheduled reset\n");
2326 rc = -EIO;
2327 goto fail_locked;
2328 }
2329
Ben Hutchingsaed06282009-08-26 08:16:27 +00002330 rc = dev_alloc_name(net_dev, net_dev->name);
2331 if (rc < 0)
2332 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002333 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002334
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002335 /* Always start with carrier off; PHY events will detect the link */
2336 netif_carrier_off(net_dev);
2337
Ben Hutchingsaed06282009-08-26 08:16:27 +00002338 rc = register_netdevice(net_dev);
2339 if (rc)
2340 goto fail_locked;
2341
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002342 efx_for_each_channel(channel, efx) {
2343 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002344 efx_for_each_channel_tx_queue(tx_queue, channel)
2345 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002346 }
2347
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002348 efx_associate(efx);
2349
Ben Hutchings7dde5962008-12-12 22:09:38 -08002350 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002351
Ben Hutchings06d5e192008-12-12 21:47:23 -08002352 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2353 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002354 netif_err(efx, drv, efx->net_dev,
2355 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002356 goto fail_registered;
2357 }
2358
Ben Hutchings8ceee662008-04-27 12:55:59 +01002359 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002360
Ben Hutchings7153f622012-07-27 20:50:52 +01002361fail_registered:
2362 rtnl_lock();
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002363 efx_dissociate(efx);
Ben Hutchings7153f622012-07-27 20:50:52 +01002364 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002365fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002366 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002367 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002368 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002369 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002370}
2371
2372static void efx_unregister_netdev(struct efx_nic *efx)
2373{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002374 if (!efx->net_dev)
2375 return;
2376
Ben Hutchings767e4682008-09-01 12:43:14 +01002377 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002378
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002379 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2380 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002381
2382 rtnl_lock();
2383 unregister_netdevice(efx->net_dev);
2384 efx->state = STATE_UNINIT;
2385 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002386}
2387
2388/**************************************************************************
2389 *
2390 * Device reset and suspend
2391 *
2392 **************************************************************************/
2393
Ben Hutchings2467ca42008-09-01 12:48:50 +01002394/* Tears down the entire software state and most of the hardware state
2395 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002396void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002397{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002398 EFX_ASSERT_RESET_SERIALISED(efx);
2399
Edward Creee2835462014-04-16 19:27:48 +01002400 if (method == RESET_TYPE_MCDI_TIMEOUT)
2401 efx->type->prepare_flr(efx);
2402
Ben Hutchings2467ca42008-09-01 12:48:50 +01002403 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002404 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002405
2406 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002407 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2408 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002409 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002410}
2411
Ben Hutchings2467ca42008-09-01 12:48:50 +01002412/* This function will always ensure that the locks acquired in
2413 * efx_reset_down() are released. A failure return code indicates
2414 * that we were unable to reinitialise the hardware, and the
2415 * driver should be disabled. If ok is false, then the rx and tx
2416 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002417int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002418{
2419 int rc;
2420
Ben Hutchings2467ca42008-09-01 12:48:50 +01002421 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002422
Edward Creee2835462014-04-16 19:27:48 +01002423 if (method == RESET_TYPE_MCDI_TIMEOUT)
2424 efx->type->finish_flr(efx);
2425
2426 /* Ensure that SRAM is initialised even if we're disabling the device */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002427 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002428 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002429 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002430 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002431 }
2432
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002433 if (!ok)
2434 goto fail;
2435
Steve Hodgson4b988282009-01-29 17:50:51 +00002436 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002437 rc = efx->phy_op->init(efx);
2438 if (rc)
2439 goto fail;
Edward Cree267d9d72015-05-06 00:59:18 +01002440 rc = efx->phy_op->reconfigure(efx);
2441 if (rc && rc != -EPERM)
Ben Hutchings62776d02010-06-23 11:30:07 +00002442 netif_err(efx, drv, efx->net_dev,
2443 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002444 }
2445
Jon Cooper261e4d92013-04-15 18:51:54 +01002446 rc = efx_enable_interrupts(efx);
2447 if (rc)
2448 goto fail;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01002449
2450#ifdef CONFIG_SFC_SRIOV
2451 rc = efx->type->vswitching_restore(efx);
2452 if (rc) /* not fatal; the PF will still work fine */
2453 netif_warn(efx, probe, efx->net_dev,
2454 "failed to restore vswitching rc=%d;"
2455 " VFs may not function\n", rc);
2456#endif
2457
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002458 efx_restore_filters(efx);
Shradha Shah7fa8d542015-05-06 00:55:13 +01002459 if (efx->type->sriov_reset)
2460 efx->type->sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002461
2462 mutex_unlock(&efx->mac_lock);
2463
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002464 efx_start_all(efx);
2465
2466 return 0;
2467
2468fail:
2469 efx->port_initialized = false;
2470
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002471 mutex_unlock(&efx->mac_lock);
2472
Ben Hutchings8ceee662008-04-27 12:55:59 +01002473 return rc;
2474}
2475
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002476/* Reset the NIC using the specified method. Note that the reset may
2477 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002478 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002479 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002480 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002481int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002482{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002483 int rc, rc2;
2484 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002485
Ben Hutchings62776d02010-06-23 11:30:07 +00002486 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2487 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002488
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002489 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002490 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002491
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002492 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002493 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002494 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002495 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002496 }
2497
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002498 /* Clear flags for the scopes we covered. We assume the NIC and
2499 * driver are now quiescent so that there is no race here.
2500 */
Edward Creee2835462014-04-16 19:27:48 +01002501 if (method < RESET_TYPE_MAX_METHOD)
2502 efx->reset_pending &= -(1 << (method + 1));
2503 else /* it doesn't fit into the well-ordered scope hierarchy */
2504 __clear_bit(method, &efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002505
2506 /* Reinitialise bus-mastering, which may have been turned off before
2507 * the reset was scheduled. This is still appropriate, even in the
2508 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2509 * can respond to requests. */
2510 pci_set_master(efx->pci_dev);
2511
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002512out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002513 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002514 disabled = rc ||
2515 method == RESET_TYPE_DISABLE ||
2516 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002517 rc2 = efx_reset_up(efx, method, !disabled);
2518 if (rc2) {
2519 disabled = true;
2520 if (!rc)
2521 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002522 }
2523
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002524 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002525 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002526 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002527 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002528 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002529 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002530 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002531 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002532 return rc;
2533}
2534
Alexandre Rames626950d2013-01-14 17:20:22 +00002535/* Try recovery mechanisms.
2536 * For now only EEH is supported.
2537 * Returns 0 if the recovery mechanisms are unsuccessful.
2538 * Returns a non-zero value otherwise.
2539 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002540int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002541{
2542#ifdef CONFIG_EEH
2543 /* A PCI error can occur and not be seen by EEH because nothing
2544 * happens on the PCI bus. In this case the driver may fail and
2545 * schedule a 'recover or reset', leading to this recovery handler.
2546 * Manually call the eeh failure check function.
2547 */
Benjamin Herrenschmidt12a89db2015-03-23 14:00:47 +11002548 struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002549 if (eeh_dev_check_failure(eehdev)) {
2550 /* The EEH mechanisms will handle the error and reset the
2551 * device if necessary.
2552 */
2553 return 1;
2554 }
2555#endif
2556 return 0;
2557}
2558
Jon Cooper74cd60a2013-09-16 14:18:51 +01002559static void efx_wait_for_bist_end(struct efx_nic *efx)
2560{
2561 int i;
2562
2563 for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
2564 if (efx_mcdi_poll_reboot(efx))
2565 goto out;
2566 msleep(BIST_WAIT_DELAY_MS);
2567 }
2568
2569 netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
2570out:
2571 /* Either way unset the BIST flag. If we found no reboot we probably
2572 * won't recover, but we should try.
2573 */
2574 efx->mc_bist_for_other_fn = false;
2575}
2576
Ben Hutchings8ceee662008-04-27 12:55:59 +01002577/* The worker thread exists so that code that cannot sleep can
2578 * schedule a reset for later.
2579 */
2580static void efx_reset_work(struct work_struct *data)
2581{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002582 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002583 unsigned long pending;
2584 enum reset_type method;
2585
2586 pending = ACCESS_ONCE(efx->reset_pending);
2587 method = fls(pending) - 1;
2588
Jon Cooper74cd60a2013-09-16 14:18:51 +01002589 if (method == RESET_TYPE_MC_BIST)
2590 efx_wait_for_bist_end(efx);
2591
Alexandre Rames626950d2013-01-14 17:20:22 +00002592 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2593 method == RESET_TYPE_RECOVER_OR_ALL) &&
2594 efx_try_recovery(efx))
2595 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002596
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002597 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002598 return;
2599
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002600 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002601
2602 /* We checked the state in efx_schedule_reset() but it may
2603 * have changed by now. Now that we have the RTNL lock,
2604 * it cannot change again.
2605 */
2606 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002607 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002608
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002609 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002610}
2611
2612void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2613{
2614 enum reset_type method;
2615
Alexandre Rames626950d2013-01-14 17:20:22 +00002616 if (efx->state == STATE_RECOVERY) {
2617 netif_dbg(efx, drv, efx->net_dev,
2618 "recovering: skip scheduling %s reset\n",
2619 RESET_TYPE(type));
2620 return;
2621 }
2622
Ben Hutchings8ceee662008-04-27 12:55:59 +01002623 switch (type) {
2624 case RESET_TYPE_INVISIBLE:
2625 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002626 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002627 case RESET_TYPE_WORLD:
2628 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002629 case RESET_TYPE_RECOVER_OR_DISABLE:
Jon Cooper74cd60a2013-09-16 14:18:51 +01002630 case RESET_TYPE_MC_BIST:
Edward Creee2835462014-04-16 19:27:48 +01002631 case RESET_TYPE_MCDI_TIMEOUT:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002632 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002633 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2634 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002635 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002636 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002637 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002638 netif_dbg(efx, drv, efx->net_dev,
2639 "scheduling %s reset for %s\n",
2640 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002641 break;
2642 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002643
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002644 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002645 smp_mb(); /* ensure we change reset_pending before checking state */
2646
2647 /* If we're not READY then just leave the flags set as the cue
2648 * to abort probing or reschedule the reset later.
2649 */
2650 if (ACCESS_ONCE(efx->state) != STATE_READY)
2651 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002652
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002653 /* efx_process_channel() will no longer read events once a
2654 * reset is scheduled. So switch back to poll'd MCDI completions. */
2655 efx_mcdi_mode_poll(efx);
2656
Steve Hodgson1ab00622008-12-12 21:33:02 -08002657 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002658}
2659
2660/**************************************************************************
2661 *
2662 * List of NICs we support
2663 *
2664 **************************************************************************/
2665
2666/* PCI device ID table */
Benoit Taine9baa3c32014-08-08 15:56:03 +02002667static const struct pci_device_id efx_pci_table[] = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002668 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2669 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002670 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002671 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2672 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002673 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002674 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002675 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002676 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002677 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8127d662013-08-29 19:19:29 +01002678 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
2679 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Shradha Shah6f7f8aa2015-05-06 01:00:07 +01002680 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
2681 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
Mateusz Wrzesinski3b06a002014-07-14 08:38:49 +01002682 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
2683 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002684 {0} /* end of list */
2685};
2686
2687/**************************************************************************
2688 *
Ben Hutchings37594332009-11-23 16:05:45 +00002689 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002690 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002691 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002692 * Needed so all function pointers are valid and do not have to be tested
2693 * before use
2694 *
2695 **************************************************************************/
2696int efx_port_dummy_op_int(struct efx_nic *efx)
2697{
2698 return 0;
2699}
2700void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002701
2702static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002703{
2704 return false;
2705}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002706
stephen hemminger6c8c2512011-04-14 05:50:12 +00002707static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002708 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002709 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002710 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002711 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002712};
2713
Ben Hutchings8ceee662008-04-27 12:55:59 +01002714/**************************************************************************
2715 *
2716 * Data housekeeping
2717 *
2718 **************************************************************************/
2719
2720/* This zeroes out and then fills in the invariants in a struct
2721 * efx_nic (including all sub-structures).
2722 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002723static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002724 struct pci_dev *pci_dev, struct net_device *net_dev)
2725{
Ben Hutchings46426102010-09-10 06:42:33 +00002726 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002727
2728 /* Initialise common structures */
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002729 INIT_LIST_HEAD(&efx->node);
2730 INIT_LIST_HEAD(&efx->secondary_list);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002731 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002732#ifdef CONFIG_SFC_MTD
2733 INIT_LIST_HEAD(&efx->mtd_list);
2734#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002735 INIT_WORK(&efx->reset_work, efx_reset_work);
2736 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002737 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002738 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002739 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002740 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002741 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002742
2743 efx->net_dev = net_dev;
Jon Cooper43a37392012-10-18 15:49:54 +01002744 efx->rx_prefix_size = efx->type->rx_prefix_size;
Andrew Rybchenko2ec03012013-11-16 11:02:27 +04002745 efx->rx_ip_align =
2746 NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
Jon Cooper43a37392012-10-18 15:49:54 +01002747 efx->rx_packet_hash_offset =
2748 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
Jon Cooperbd9a2652013-11-18 12:54:41 +00002749 efx->rx_packet_ts_offset =
2750 efx->type->rx_ts_offset - efx->type->rx_prefix_size;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002751 spin_lock_init(&efx->stats_lock);
2752 mutex_init(&efx->mac_lock);
2753 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002754 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002755 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002756 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002757
2758 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002759 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2760 if (!efx->channel[i])
2761 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002762 efx->msi_context[i].efx = efx;
2763 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002764 }
2765
Ben Hutchings8ceee662008-04-27 12:55:59 +01002766 /* Higher numbered interrupt modes are less capable! */
2767 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2768 interrupt_mode);
2769
Ben Hutchings6977dc62008-12-26 13:44:39 -08002770 /* Would be good to use the net_dev name, but we're too early */
2771 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2772 pci_name(pci_dev));
2773 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002774 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002775 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002776
Ben Hutchings8ceee662008-04-27 12:55:59 +01002777 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002778
2779fail:
2780 efx_fini_struct(efx);
2781 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002782}
2783
2784static void efx_fini_struct(struct efx_nic *efx)
2785{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002786 int i;
2787
2788 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2789 kfree(efx->channel[i]);
2790
Ben Hutchingsef215e62013-12-05 20:13:22 +00002791 kfree(efx->vpd_sn);
2792
Ben Hutchings8ceee662008-04-27 12:55:59 +01002793 if (efx->workqueue) {
2794 destroy_workqueue(efx->workqueue);
2795 efx->workqueue = NULL;
2796 }
2797}
2798
Edward Creee4d112e2014-07-15 11:58:12 +01002799void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
2800{
2801 u64 n_rx_nodesc_trunc = 0;
2802 struct efx_channel *channel;
2803
2804 efx_for_each_channel(channel, efx)
2805 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
2806 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
2807 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
2808}
2809
Ben Hutchings8ceee662008-04-27 12:55:59 +01002810/**************************************************************************
2811 *
2812 * PCI interface
2813 *
2814 **************************************************************************/
2815
2816/* Main body of final NIC shutdown code
2817 * This is called only at module unload (or hotplug removal).
2818 */
2819static void efx_pci_remove_main(struct efx_nic *efx)
2820{
Ben Hutchings7153f622012-07-27 20:50:52 +01002821 /* Flush reset_work. It can no longer be scheduled since we
2822 * are not READY.
2823 */
2824 BUG_ON(efx->state == STATE_READY);
2825 cancel_work_sync(&efx->reset_work);
2826
Ben Hutchingsd8291182012-10-05 23:35:41 +01002827 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002828 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002829 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002830 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002831 efx_fini_napi(efx);
2832 efx_remove_all(efx);
2833}
2834
2835/* Final NIC shutdown
2836 * This is called only at module unload (or hotplug removal).
2837 */
2838static void efx_pci_remove(struct pci_dev *pci_dev)
2839{
2840 struct efx_nic *efx;
2841
2842 efx = pci_get_drvdata(pci_dev);
2843 if (!efx)
2844 return;
2845
2846 /* Mark the NIC as fini, then stop the interface */
2847 rtnl_lock();
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002848 efx_dissociate(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002849 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002850 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002851 rtnl_unlock();
2852
Shradha Shah7fa8d542015-05-06 00:55:13 +01002853 if (efx->type->sriov_fini)
2854 efx->type->sriov_fini(efx);
2855
Ben Hutchings8ceee662008-04-27 12:55:59 +01002856 efx_unregister_netdev(efx);
2857
Ben Hutchings7dde5962008-12-12 22:09:38 -08002858 efx_mtd_remove(efx);
2859
Ben Hutchings8ceee662008-04-27 12:55:59 +01002860 efx_pci_remove_main(efx);
2861
Ben Hutchings8ceee662008-04-27 12:55:59 +01002862 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002863 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002864
Ben Hutchings8ceee662008-04-27 12:55:59 +01002865 efx_fini_struct(efx);
2866 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002867
2868 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002869};
2870
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002871/* NIC VPD information
2872 * Called during probe to display the part number of the
2873 * installed NIC. VPD is potentially very large but this should
2874 * always appear within the first 512 bytes.
2875 */
2876#define SFC_VPD_LEN 512
Ben Hutchingsef215e62013-12-05 20:13:22 +00002877static void efx_probe_vpd_strings(struct efx_nic *efx)
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002878{
2879 struct pci_dev *dev = efx->pci_dev;
2880 char vpd_data[SFC_VPD_LEN];
2881 ssize_t vpd_size;
Ben Hutchingsef215e62013-12-05 20:13:22 +00002882 int ro_start, ro_size, i, j;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002883
2884 /* Get the vpd data from the device */
2885 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2886 if (vpd_size <= 0) {
2887 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2888 return;
2889 }
2890
2891 /* Get the Read only section */
Ben Hutchingsef215e62013-12-05 20:13:22 +00002892 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2893 if (ro_start < 0) {
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002894 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2895 return;
2896 }
2897
Ben Hutchingsef215e62013-12-05 20:13:22 +00002898 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
2899 j = ro_size;
2900 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002901 if (i + j > vpd_size)
2902 j = vpd_size - i;
2903
2904 /* Get the Part number */
2905 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2906 if (i < 0) {
2907 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2908 return;
2909 }
2910
2911 j = pci_vpd_info_field_size(&vpd_data[i]);
2912 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2913 if (i + j > vpd_size) {
2914 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2915 return;
2916 }
2917
2918 netif_info(efx, drv, efx->net_dev,
2919 "Part Number : %.*s\n", j, &vpd_data[i]);
Ben Hutchingsef215e62013-12-05 20:13:22 +00002920
2921 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
2922 j = ro_size;
2923 i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
2924 if (i < 0) {
2925 netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
2926 return;
2927 }
2928
2929 j = pci_vpd_info_field_size(&vpd_data[i]);
2930 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2931 if (i + j > vpd_size) {
2932 netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
2933 return;
2934 }
2935
2936 efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
2937 if (!efx->vpd_sn)
2938 return;
2939
2940 snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002941}
2942
2943
Ben Hutchings8ceee662008-04-27 12:55:59 +01002944/* Main body of NIC initialisation
2945 * This is called at module load (or hotplug insertion, theoretically).
2946 */
2947static int efx_pci_probe_main(struct efx_nic *efx)
2948{
2949 int rc;
2950
2951 /* Do start-of-day initialisation */
2952 rc = efx_probe_all(efx);
2953 if (rc)
2954 goto fail1;
2955
Ben Hutchingse8f14992010-12-07 19:47:34 +00002956 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002957
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002958 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002959 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002960 netif_err(efx, probe, efx->net_dev,
2961 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002962 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002963 }
2964
2965 rc = efx_init_port(efx);
2966 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002967 netif_err(efx, probe, efx->net_dev,
2968 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002969 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002970 }
2971
Ben Hutchings152b6a62009-11-29 03:43:56 +00002972 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002973 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002974 goto fail5;
Jon Cooper261e4d92013-04-15 18:51:54 +01002975 rc = efx_enable_interrupts(efx);
2976 if (rc)
2977 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002978
2979 return 0;
2980
Jon Cooper261e4d92013-04-15 18:51:54 +01002981 fail6:
2982 efx_nic_fini_interrupt(efx);
Ben Hutchings278c0622009-11-23 16:05:12 +00002983 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002984 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002985 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002986 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002987 fail3:
2988 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002989 efx_remove_all(efx);
2990 fail1:
2991 return rc;
2992}
2993
2994/* NIC initialisation
2995 *
2996 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002997 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002998 * sets up and registers the network devices with the kernel and hooks
2999 * the interrupt service routine. It does not prepare the device for
3000 * transmission; this is left to the first time one of the network
3001 * interfaces is brought up (i.e. efx_net_open).
3002 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05003003static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00003004 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01003005{
Ben Hutchings8ceee662008-04-27 12:55:59 +01003006 struct net_device *net_dev;
3007 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003008 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003009
3010 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00003011 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
3012 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003013 if (!net_dev)
3014 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01003015 efx = netdev_priv(net_dev);
3016 efx->type = (const struct efx_nic_type *) entry->driver_data;
3017 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07003018 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01003019 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01003020 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00003021 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01003022 /* Mask for features that also apply to VLAN devices */
3023 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01003024 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
3025 NETIF_F_RXCSUM);
3026 /* All offloads can be toggled */
3027 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003028 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00003029 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01003030 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003031 if (rc)
3032 goto fail1;
3033
Ben Hutchings62776d02010-06-23 11:30:07 +00003034 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01003035 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003036
Shradha Shah6f7f8aa2015-05-06 01:00:07 +01003037 if (!efx->type->is_vf)
3038 efx_probe_vpd_strings(efx);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003039
Ben Hutchings8ceee662008-04-27 12:55:59 +01003040 /* Set up basic I/O (BAR mappings etc) */
3041 rc = efx_init_io(efx);
3042 if (rc)
3043 goto fail2;
3044
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003045 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003046 if (rc)
3047 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08003048
Ben Hutchings8ceee662008-04-27 12:55:59 +01003049 rc = efx_register_netdev(efx);
3050 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003051 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003052
Shradha Shah7fa8d542015-05-06 00:55:13 +01003053 if (efx->type->sriov_init) {
3054 rc = efx->type->sriov_init(efx);
3055 if (rc)
3056 netif_err(efx, probe, efx->net_dev,
3057 "SR-IOV can't be enabled rc %d\n", rc);
3058 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003059
Ben Hutchings62776d02010-06-23 11:30:07 +00003060 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003061
Ben Hutchings7c431612012-01-27 17:23:58 +00003062 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003063 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00003064 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003065 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00003066 if (rc)
3067 netif_warn(efx, probe, efx->net_dev,
3068 "failed to create MTDs (%d)\n", rc);
3069
Alexandre Rames626950d2013-01-14 17:20:22 +00003070 rc = pci_enable_pcie_error_reporting(pci_dev);
3071 if (rc && rc != -EINVAL)
3072 netif_warn(efx, probe, efx->net_dev,
3073 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
3074
Ben Hutchings8ceee662008-04-27 12:55:59 +01003075 return 0;
3076
Ben Hutchings8ceee662008-04-27 12:55:59 +01003077 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003078 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003079 fail3:
3080 efx_fini_io(efx);
3081 fail2:
3082 efx_fini_struct(efx);
3083 fail1:
Steve Hodgson5e2a9112010-02-12 12:32:27 -08003084 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00003085 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003086 free_netdev(net_dev);
3087 return rc;
3088}
3089
Shradha Shah834e23d2015-05-06 00:55:58 +01003090/* efx_pci_sriov_configure returns the actual number of Virtual Functions
3091 * enabled on success
3092 */
3093#ifdef CONFIG_SFC_SRIOV
3094static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
3095{
3096 int rc;
3097 struct efx_nic *efx = pci_get_drvdata(dev);
3098
3099 if (efx->type->sriov_configure) {
3100 rc = efx->type->sriov_configure(efx, num_vfs);
3101 if (rc)
3102 return rc;
3103 else
3104 return num_vfs;
3105 } else
3106 return -EOPNOTSUPP;
3107}
3108#endif
3109
Ben Hutchings89c758f2009-11-29 03:43:07 +00003110static int efx_pm_freeze(struct device *dev)
3111{
3112 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3113
Ben Hutchings61da0262012-07-27 19:35:39 +01003114 rtnl_lock();
3115
Ben Hutchings6032fb52012-07-27 19:35:47 +01003116 if (efx->state != STATE_DISABLED) {
3117 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003118
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01003119 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003120
Ben Hutchings6032fb52012-07-27 19:35:47 +01003121 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003122 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01003123 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00003124
Ben Hutchings61da0262012-07-27 19:35:39 +01003125 rtnl_unlock();
3126
Ben Hutchings89c758f2009-11-29 03:43:07 +00003127 return 0;
3128}
3129
3130static int efx_pm_thaw(struct device *dev)
3131{
Jon Cooper261e4d92013-04-15 18:51:54 +01003132 int rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003133 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3134
Ben Hutchings61da0262012-07-27 19:35:39 +01003135 rtnl_lock();
3136
Ben Hutchings6032fb52012-07-27 19:35:47 +01003137 if (efx->state != STATE_DISABLED) {
Jon Cooper261e4d92013-04-15 18:51:54 +01003138 rc = efx_enable_interrupts(efx);
3139 if (rc)
3140 goto fail;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003141
Ben Hutchings6032fb52012-07-27 19:35:47 +01003142 mutex_lock(&efx->mac_lock);
3143 efx->phy_op->reconfigure(efx);
3144 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003145
Ben Hutchings6032fb52012-07-27 19:35:47 +01003146 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003147
Ben Hutchings6032fb52012-07-27 19:35:47 +01003148 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003149
Ben Hutchings6032fb52012-07-27 19:35:47 +01003150 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003151
Ben Hutchings6032fb52012-07-27 19:35:47 +01003152 efx->type->resume_wol(efx);
3153 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00003154
Ben Hutchings61da0262012-07-27 19:35:39 +01003155 rtnl_unlock();
3156
Steve Hodgson319ba642010-06-01 11:17:24 +00003157 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
3158 queue_work(reset_workqueue, &efx->reset_work);
3159
Ben Hutchings89c758f2009-11-29 03:43:07 +00003160 return 0;
Jon Cooper261e4d92013-04-15 18:51:54 +01003161
3162fail:
3163 rtnl_unlock();
3164
3165 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003166}
3167
3168static int efx_pm_poweroff(struct device *dev)
3169{
3170 struct pci_dev *pci_dev = to_pci_dev(dev);
3171 struct efx_nic *efx = pci_get_drvdata(pci_dev);
3172
3173 efx->type->fini(efx);
3174
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01003175 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003176
3177 pci_save_state(pci_dev);
3178 return pci_set_power_state(pci_dev, PCI_D3hot);
3179}
3180
3181/* Used for both resume and restore */
3182static int efx_pm_resume(struct device *dev)
3183{
3184 struct pci_dev *pci_dev = to_pci_dev(dev);
3185 struct efx_nic *efx = pci_get_drvdata(pci_dev);
3186 int rc;
3187
3188 rc = pci_set_power_state(pci_dev, PCI_D0);
3189 if (rc)
3190 return rc;
3191 pci_restore_state(pci_dev);
3192 rc = pci_enable_device(pci_dev);
3193 if (rc)
3194 return rc;
3195 pci_set_master(efx->pci_dev);
3196 rc = efx->type->reset(efx, RESET_TYPE_ALL);
3197 if (rc)
3198 return rc;
3199 rc = efx->type->init(efx);
3200 if (rc)
3201 return rc;
Jon Cooper261e4d92013-04-15 18:51:54 +01003202 rc = efx_pm_thaw(dev);
3203 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003204}
3205
3206static int efx_pm_suspend(struct device *dev)
3207{
3208 int rc;
3209
3210 efx_pm_freeze(dev);
3211 rc = efx_pm_poweroff(dev);
3212 if (rc)
3213 efx_pm_resume(dev);
3214 return rc;
3215}
3216
Ben Hutchings18e83e42012-01-05 19:05:20 +00003217static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00003218 .suspend = efx_pm_suspend,
3219 .resume = efx_pm_resume,
3220 .freeze = efx_pm_freeze,
3221 .thaw = efx_pm_thaw,
3222 .poweroff = efx_pm_poweroff,
3223 .restore = efx_pm_resume,
3224};
3225
Alexandre Rames626950d2013-01-14 17:20:22 +00003226/* A PCI error affecting this device was detected.
3227 * At this point MMIO and DMA may be disabled.
3228 * Stop the software path and request a slot reset.
3229 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003230static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
3231 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00003232{
3233 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3234 struct efx_nic *efx = pci_get_drvdata(pdev);
3235
3236 if (state == pci_channel_io_perm_failure)
3237 return PCI_ERS_RESULT_DISCONNECT;
3238
3239 rtnl_lock();
3240
3241 if (efx->state != STATE_DISABLED) {
3242 efx->state = STATE_RECOVERY;
3243 efx->reset_pending = 0;
3244
3245 efx_device_detach_sync(efx);
3246
3247 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003248 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00003249
3250 status = PCI_ERS_RESULT_NEED_RESET;
3251 } else {
3252 /* If the interface is disabled we don't want to do anything
3253 * with it.
3254 */
3255 status = PCI_ERS_RESULT_RECOVERED;
3256 }
3257
3258 rtnl_unlock();
3259
3260 pci_disable_device(pdev);
3261
3262 return status;
3263}
3264
Joe Perchesdbedd442015-03-06 20:49:12 -08003265/* Fake a successful reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003266static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00003267{
3268 struct efx_nic *efx = pci_get_drvdata(pdev);
3269 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3270 int rc;
3271
3272 if (pci_enable_device(pdev)) {
3273 netif_err(efx, hw, efx->net_dev,
3274 "Cannot re-enable PCI device after reset.\n");
3275 status = PCI_ERS_RESULT_DISCONNECT;
3276 }
3277
3278 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
3279 if (rc) {
3280 netif_err(efx, hw, efx->net_dev,
3281 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3282 /* Non-fatal error. Continue. */
3283 }
3284
3285 return status;
3286}
3287
3288/* Perform the actual reset and resume I/O operations. */
3289static void efx_io_resume(struct pci_dev *pdev)
3290{
3291 struct efx_nic *efx = pci_get_drvdata(pdev);
3292 int rc;
3293
3294 rtnl_lock();
3295
3296 if (efx->state == STATE_DISABLED)
3297 goto out;
3298
3299 rc = efx_reset(efx, RESET_TYPE_ALL);
3300 if (rc) {
3301 netif_err(efx, hw, efx->net_dev,
3302 "efx_reset failed after PCI error (%d)\n", rc);
3303 } else {
3304 efx->state = STATE_READY;
3305 netif_dbg(efx, hw, efx->net_dev,
3306 "Done resetting and resuming IO after PCI error.\n");
3307 }
3308
3309out:
3310 rtnl_unlock();
3311}
3312
3313/* For simplicity and reliability, we always require a slot reset and try to
3314 * reset the hardware when a pci error affecting the device is detected.
3315 * We leave both the link_reset and mmio_enabled callback unimplemented:
3316 * with our request for slot reset the mmio_enabled callback will never be
3317 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3318 */
3319static struct pci_error_handlers efx_err_handlers = {
3320 .error_detected = efx_io_error_detected,
3321 .slot_reset = efx_io_slot_reset,
3322 .resume = efx_io_resume,
3323};
3324
Ben Hutchings8ceee662008-04-27 12:55:59 +01003325static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003326 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003327 .id_table = efx_pci_table,
3328 .probe = efx_pci_probe,
3329 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003330 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003331 .err_handler = &efx_err_handlers,
Shradha Shah834e23d2015-05-06 00:55:58 +01003332#ifdef CONFIG_SFC_SRIOV
3333 .sriov_configure = efx_pci_sriov_configure,
3334#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003335};
3336
3337/**************************************************************************
3338 *
3339 * Kernel module interface
3340 *
3341 *************************************************************************/
3342
3343module_param(interrupt_mode, uint, 0444);
3344MODULE_PARM_DESC(interrupt_mode,
3345 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3346
3347static int __init efx_init_module(void)
3348{
3349 int rc;
3350
3351 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3352
3353 rc = register_netdevice_notifier(&efx_netdev_notifier);
3354 if (rc)
3355 goto err_notifier;
3356
Shradha Shah7fa8d542015-05-06 00:55:13 +01003357#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003358 rc = efx_init_sriov();
3359 if (rc)
3360 goto err_sriov;
Shradha Shah7fa8d542015-05-06 00:55:13 +01003361#endif
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003362
Steve Hodgson1ab00622008-12-12 21:33:02 -08003363 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3364 if (!reset_workqueue) {
3365 rc = -ENOMEM;
3366 goto err_reset;
3367 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003368
3369 rc = pci_register_driver(&efx_pci_driver);
3370 if (rc < 0)
3371 goto err_pci;
3372
3373 return 0;
3374
3375 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003376 destroy_workqueue(reset_workqueue);
3377 err_reset:
Shradha Shah7fa8d542015-05-06 00:55:13 +01003378#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003379 efx_fini_sriov();
3380 err_sriov:
Shradha Shah7fa8d542015-05-06 00:55:13 +01003381#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003382 unregister_netdevice_notifier(&efx_netdev_notifier);
3383 err_notifier:
3384 return rc;
3385}
3386
3387static void __exit efx_exit_module(void)
3388{
3389 printk(KERN_INFO "Solarflare NET driver unloading\n");
3390
3391 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003392 destroy_workqueue(reset_workqueue);
Shradha Shah7fa8d542015-05-06 00:55:13 +01003393#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003394 efx_fini_sriov();
Shradha Shah7fa8d542015-05-06 00:55:13 +01003395#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003396 unregister_netdevice_notifier(&efx_netdev_notifier);
3397
3398}
3399
3400module_init(efx_init_module);
3401module_exit(efx_exit_module);
3402
Ben Hutchings906bb262009-11-29 15:16:19 +00003403MODULE_AUTHOR("Solarflare Communications and "
3404 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings6a350fd2014-02-12 19:00:07 +00003405MODULE_DESCRIPTION("Solarflare network driver");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003406MODULE_LICENSE("GPL");
3407MODULE_DEVICE_TABLE(pci, efx_pci_table);