blob: 9fe375f1710ae1c27107872cb0e7ac6442d1d156 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchings0a6f40c2011-02-25 00:01:34 +00004 * Copyright 2005-2011 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"
Ben Hutchings8ceee662008-04-27 12:55:59 +010029
Ben Hutchings8880f4e2009-11-29 15:15:41 +000030#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000031#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000032
Ben Hutchingsc4593022009-11-23 16:08:17 +000033/**************************************************************************
34 *
35 * Type name strings
36 *
37 **************************************************************************
38 */
39
40/* Loopback mode names (see LOOPBACK_MODE()) */
41const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000042const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000043 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000044 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000045 [LOOPBACK_GMAC] = "GMAC",
46 [LOOPBACK_XGMII] = "XGMII",
47 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000048 [LOOPBACK_XAUI] = "XAUI",
49 [LOOPBACK_GMII] = "GMII",
50 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000051 [LOOPBACK_XGBR] = "XGBR",
52 [LOOPBACK_XFI] = "XFI",
53 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
54 [LOOPBACK_GMII_FAR] = "GMII_FAR",
55 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
56 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000057 [LOOPBACK_GPHY] = "GPHY",
58 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000059 [LOOPBACK_PCS] = "PCS",
60 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000061 [LOOPBACK_XPORT] = "XPORT",
62 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000063 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000064 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
65 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000066 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000067 [LOOPBACK_XFI_WS] = "XFI_WS",
68 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000069 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000070};
71
Ben Hutchingsc4593022009-11-23 16:08:17 +000072const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000073const char *const efx_reset_type_names[] = {
Alexandre Rames626950d2013-01-14 17:20:22 +000074 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
75 [RESET_TYPE_ALL] = "ALL",
76 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
77 [RESET_TYPE_WORLD] = "WORLD",
78 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
79 [RESET_TYPE_DISABLE] = "DISABLE",
80 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
81 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
82 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
83 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
84 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
85 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
86 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Ben Hutchingsc4593022009-11-23 16:08:17 +000087};
88
Steve Hodgson1ab00622008-12-12 21:33:02 -080089/* Reset workqueue. If any NIC has a hardware failure then a reset will be
90 * queued onto this work queue. This is not a per-nic work queue, because
91 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
92 */
93static struct workqueue_struct *reset_workqueue;
94
Ben Hutchings8ceee662008-04-27 12:55:59 +010095/**************************************************************************
96 *
97 * Configurable values
98 *
99 *************************************************************************/
100
101/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100102 * Use separate channels for TX and RX events
103 *
Neil Turton28b581a2008-12-12 21:41:06 -0800104 * Set this to 1 to use separate channels for TX and RX. It allows us
105 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100106 *
Neil Turton28b581a2008-12-12 21:41:06 -0800107 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100108 */
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000109static bool separate_tx_channels;
110module_param(separate_tx_channels, bool, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800111MODULE_PARM_DESC(separate_tx_channels,
112 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100113
114/* This is the weight assigned to each of the (per-channel) virtual
115 * NAPI devices.
116 */
117static int napi_weight = 64;
118
119/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000120 * monitor.
121 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000122 * - Check the on-board hardware monitor;
123 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000124 * On Siena-based NICs for power systems with EEH support, this will give EEH a
125 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100126 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000127static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100128
Ben Hutchings8ceee662008-04-27 12:55:59 +0100129/* Initial interrupt moderation settings. They can be modified after
130 * module load with ethtool.
131 *
132 * The default for RX should strike a balance between increasing the
133 * round-trip latency and reducing overhead.
134 */
135static unsigned int rx_irq_mod_usec = 60;
136
137/* Initial interrupt moderation settings. They can be modified after
138 * module load with ethtool.
139 *
140 * This default is chosen to ensure that a 10G link does not go idle
141 * while a TX queue is stopped after it has become full. A queue is
142 * restarted when it drops below half full. The time this takes (assuming
143 * worst case 3 descriptors per packet and 1024 descriptors) is
144 * 512 / 3 * 1.2 = 205 usec.
145 */
146static unsigned int tx_irq_mod_usec = 150;
147
148/* This is the first interrupt mode to try out of:
149 * 0 => MSI-X
150 * 1 => MSI
151 * 2 => legacy
152 */
153static unsigned int interrupt_mode;
154
155/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
156 * i.e. the number of CPUs among which we may distribute simultaneous
157 * interrupt handling.
158 *
159 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000160 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100161 */
162static unsigned int rss_cpus;
163module_param(rss_cpus, uint, 0444);
164MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
165
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000166static bool phy_flash_cfg;
167module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800168MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
169
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000170static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000171module_param(irq_adapt_low_thresh, uint, 0644);
172MODULE_PARM_DESC(irq_adapt_low_thresh,
173 "Threshold score for reducing IRQ moderation");
174
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000175static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000176module_param(irq_adapt_high_thresh, uint, 0644);
177MODULE_PARM_DESC(irq_adapt_high_thresh,
178 "Threshold score for increasing IRQ moderation");
179
Ben Hutchings62776d02010-06-23 11:30:07 +0000180static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
181 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
182 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
183 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
184module_param(debug, uint, 0);
185MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
186
Ben Hutchings8ceee662008-04-27 12:55:59 +0100187/**************************************************************************
188 *
189 * Utility functions and prototypes
190 *
191 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000192
Ben Hutchingsd8291182012-10-05 23:35:41 +0100193static void efx_soft_enable_interrupts(struct efx_nic *efx);
194static void efx_soft_disable_interrupts(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000195static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000196static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000197static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100198static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000199static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100200static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000201static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000202static void efx_fini_struct(struct efx_nic *efx);
203static void efx_start_all(struct efx_nic *efx);
204static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100205
206#define EFX_ASSERT_RESET_SERIALISED(efx) \
207 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100208 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000209 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000210 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100211 ASSERT_RTNL(); \
212 } while (0)
213
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100214static int efx_check_disabled(struct efx_nic *efx)
215{
Alexandre Rames626950d2013-01-14 17:20:22 +0000216 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100217 netif_err(efx, drv, efx->net_dev,
218 "device is disabled due to earlier errors\n");
219 return -EIO;
220 }
221 return 0;
222}
223
Ben Hutchings8ceee662008-04-27 12:55:59 +0100224/**************************************************************************
225 *
226 * Event queue processing
227 *
228 *************************************************************************/
229
230/* Process channel's event queue
231 *
232 * This function is responsible for processing the event queue of a
233 * single channel. The caller must guarantee that this function will
234 * never be concurrently called more than once on the same channel,
235 * though different channels may be being processed concurrently.
236 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000237static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100238{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000239 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100240
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000241 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100242 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100243
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000244 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000245 if (spent && efx_channel_has_rx_queue(channel)) {
246 struct efx_rx_queue *rx_queue =
247 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100248
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000249 efx_rx_flush_packet(channel);
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100250 efx_fast_push_rx_descriptors(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100251 }
252
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000253 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100254}
255
Ben Hutchings8ceee662008-04-27 12:55:59 +0100256/* NAPI poll handler
257 *
258 * NAPI guarantees serialisation of polls of the same device, which
259 * provides the guarantee required by efx_process_channel().
260 */
261static int efx_poll(struct napi_struct *napi, int budget)
262{
263 struct efx_channel *channel =
264 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000265 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000266 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100267
Ben Hutchings62776d02010-06-23 11:30:07 +0000268 netif_vdbg(efx, intr, efx->net_dev,
269 "channel %d NAPI poll executing on CPU %d\n",
270 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100271
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000272 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100273
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000274 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000275 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000276 efx->irq_rx_adaptive &&
277 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000278 if (unlikely(channel->irq_mod_score <
279 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000280 if (channel->irq_moderation > 1) {
281 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000282 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000283 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000284 } else if (unlikely(channel->irq_mod_score >
285 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000286 if (channel->irq_moderation <
287 efx->irq_rx_moderation) {
288 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000289 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000290 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000291 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000292 channel->irq_count = 0;
293 channel->irq_mod_score = 0;
294 }
295
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000296 efx_filter_rfs_expire(channel);
297
Ben Hutchings8ceee662008-04-27 12:55:59 +0100298 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800299 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100300 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100301 * interrupts have already been disabled.
302 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800303 napi_complete(napi);
Ben Hutchings514bedb2012-10-05 19:30:16 +0100304 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100305 }
306
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000307 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100308}
309
Ben Hutchings8ceee662008-04-27 12:55:59 +0100310/* Create event queue
311 * Event queue memory allocations are done only once. If the channel
312 * is reset, the memory buffer will be reused; this guards against
313 * errors during channel reset and also simplifies interrupt handling.
314 */
315static int efx_probe_eventq(struct efx_channel *channel)
316{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000317 struct efx_nic *efx = channel->efx;
318 unsigned long entries;
319
Ben Hutchings86ee5302012-01-09 19:51:22 +0000320 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000321 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100322
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000323 /* Build an event queue with room for one event per tx and rx buffer,
324 * plus some extra for link state events and MCDI completions. */
325 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
326 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
327 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
328
Ben Hutchings152b6a62009-11-29 03:43:56 +0000329 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100330}
331
332/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100333static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100334{
Ben Hutchings62776d02010-06-23 11:30:07 +0000335 netif_dbg(channel->efx, drv, channel->efx->net_dev,
336 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100337
338 channel->eventq_read_ptr = 0;
339
Ben Hutchings152b6a62009-11-29 03:43:56 +0000340 efx_nic_init_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100341 channel->eventq_init = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100342}
343
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000344/* Enable event queue processing and NAPI */
345static void efx_start_eventq(struct efx_channel *channel)
346{
347 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
348 "chan %d start event queue\n", channel->channel);
349
Ben Hutchings514bedb2012-10-05 19:30:16 +0100350 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000351 channel->enabled = true;
352 smp_wmb();
353
354 napi_enable(&channel->napi_str);
355 efx_nic_eventq_read_ack(channel);
356}
357
358/* Disable event queue processing and NAPI */
359static void efx_stop_eventq(struct efx_channel *channel)
360{
361 if (!channel->enabled)
362 return;
363
364 napi_disable(&channel->napi_str);
365 channel->enabled = false;
366}
367
Ben Hutchings8ceee662008-04-27 12:55:59 +0100368static void efx_fini_eventq(struct efx_channel *channel)
369{
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100370 if (!channel->eventq_init)
371 return;
372
Ben Hutchings62776d02010-06-23 11:30:07 +0000373 netif_dbg(channel->efx, drv, channel->efx->net_dev,
374 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100375
Ben Hutchings152b6a62009-11-29 03:43:56 +0000376 efx_nic_fini_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100377 channel->eventq_init = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100378}
379
380static void efx_remove_eventq(struct efx_channel *channel)
381{
Ben Hutchings62776d02010-06-23 11:30:07 +0000382 netif_dbg(channel->efx, drv, channel->efx->net_dev,
383 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100384
Ben Hutchings152b6a62009-11-29 03:43:56 +0000385 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100386}
387
388/**************************************************************************
389 *
390 * Channel handling
391 *
392 *************************************************************************/
393
Ben Hutchings7f967c02012-02-13 23:45:02 +0000394/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000395static struct efx_channel *
396efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
397{
398 struct efx_channel *channel;
399 struct efx_rx_queue *rx_queue;
400 struct efx_tx_queue *tx_queue;
401 int j;
402
Ben Hutchings7f967c02012-02-13 23:45:02 +0000403 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
404 if (!channel)
405 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000406
Ben Hutchings7f967c02012-02-13 23:45:02 +0000407 channel->efx = efx;
408 channel->channel = i;
409 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000410
Ben Hutchings7f967c02012-02-13 23:45:02 +0000411 for (j = 0; j < EFX_TXQ_TYPES; j++) {
412 tx_queue = &channel->tx_queue[j];
413 tx_queue->efx = efx;
414 tx_queue->queue = i * EFX_TXQ_TYPES + j;
415 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000416 }
417
Ben Hutchings46426102010-09-10 06:42:33 +0000418 rx_queue = &channel->rx_queue;
419 rx_queue->efx = efx;
420 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
421 (unsigned long)rx_queue);
422
423 return channel;
424}
425
Ben Hutchings7f967c02012-02-13 23:45:02 +0000426/* Allocate and initialise a channel structure, copying parameters
427 * (but not resources) from an old channel structure.
428 */
429static struct efx_channel *
430efx_copy_channel(const struct efx_channel *old_channel)
431{
432 struct efx_channel *channel;
433 struct efx_rx_queue *rx_queue;
434 struct efx_tx_queue *tx_queue;
435 int j;
436
437 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
438 if (!channel)
439 return NULL;
440
441 *channel = *old_channel;
442
443 channel->napi_dev = NULL;
444 memset(&channel->eventq, 0, sizeof(channel->eventq));
445
446 for (j = 0; j < EFX_TXQ_TYPES; j++) {
447 tx_queue = &channel->tx_queue[j];
448 if (tx_queue->channel)
449 tx_queue->channel = channel;
450 tx_queue->buffer = NULL;
451 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
452 }
453
454 rx_queue = &channel->rx_queue;
455 rx_queue->buffer = NULL;
456 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
457 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
458 (unsigned long)rx_queue);
459
460 return channel;
461}
462
Ben Hutchings8ceee662008-04-27 12:55:59 +0100463static int efx_probe_channel(struct efx_channel *channel)
464{
465 struct efx_tx_queue *tx_queue;
466 struct efx_rx_queue *rx_queue;
467 int rc;
468
Ben Hutchings62776d02010-06-23 11:30:07 +0000469 netif_dbg(channel->efx, probe, channel->efx->net_dev,
470 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100471
Ben Hutchings7f967c02012-02-13 23:45:02 +0000472 rc = channel->type->pre_probe(channel);
473 if (rc)
474 goto fail;
475
Ben Hutchings8ceee662008-04-27 12:55:59 +0100476 rc = efx_probe_eventq(channel);
477 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000478 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100479
480 efx_for_each_channel_tx_queue(tx_queue, channel) {
481 rc = efx_probe_tx_queue(tx_queue);
482 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000483 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100484 }
485
486 efx_for_each_channel_rx_queue(rx_queue, channel) {
487 rc = efx_probe_rx_queue(rx_queue);
488 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000489 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100490 }
491
492 channel->n_rx_frm_trunc = 0;
493
494 return 0;
495
Ben Hutchings7f967c02012-02-13 23:45:02 +0000496fail:
497 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100498 return rc;
499}
500
Ben Hutchings7f967c02012-02-13 23:45:02 +0000501static void
502efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
503{
504 struct efx_nic *efx = channel->efx;
505 const char *type;
506 int number;
507
508 number = channel->channel;
509 if (efx->tx_channel_offset == 0) {
510 type = "";
511 } else if (channel->channel < efx->tx_channel_offset) {
512 type = "-rx";
513 } else {
514 type = "-tx";
515 number -= efx->tx_channel_offset;
516 }
517 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
518}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100519
Ben Hutchings56536e92008-12-12 21:37:02 -0800520static void efx_set_channel_names(struct efx_nic *efx)
521{
522 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800523
Ben Hutchings7f967c02012-02-13 23:45:02 +0000524 efx_for_each_channel(channel, efx)
525 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100526 efx->msi_context[channel->channel].name,
527 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800528}
529
Ben Hutchings46426102010-09-10 06:42:33 +0000530static int efx_probe_channels(struct efx_nic *efx)
531{
532 struct efx_channel *channel;
533 int rc;
534
535 /* Restart special buffer allocation */
536 efx->next_buffer_table = 0;
537
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000538 /* Probe channels in reverse, so that any 'extra' channels
539 * use the start of the buffer table. This allows the traffic
540 * channels to be resized without moving them or wasting the
541 * entries before them.
542 */
543 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000544 rc = efx_probe_channel(channel);
545 if (rc) {
546 netif_err(efx, probe, efx->net_dev,
547 "failed to create channel %d\n",
548 channel->channel);
549 goto fail;
550 }
551 }
552 efx_set_channel_names(efx);
553
554 return 0;
555
556fail:
557 efx_remove_channels(efx);
558 return rc;
559}
560
Ben Hutchings8ceee662008-04-27 12:55:59 +0100561/* Channels are shutdown and reinitialised whilst the NIC is running
562 * to propagate configuration changes (mtu, checksum offload), or
563 * to clear hardware error conditions
564 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000565static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100566{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000567 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100568 struct efx_tx_queue *tx_queue;
569 struct efx_rx_queue *rx_queue;
570 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000571 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100572
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100573 /* Calculate the rx buffer allocation parameters required to
574 * support the current MTU, including padding for header
575 * alignment and overruns.
576 */
Ben Hutchings272baee2013-01-29 23:33:14 +0000577 efx->rx_dma_len = (efx->type->rx_buffer_hash_size +
578 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
579 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000580 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Ben Hutchingsc14ff2e2013-05-13 11:58:31 +0000581 NET_IP_ALIGN + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000582 if (rx_buf_len <= PAGE_SIZE) {
583 efx->rx_scatter = false;
584 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000585 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000586 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000587 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000588 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
589 EFX_RX_BUF_ALIGNMENT) >
590 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000591 efx->rx_scatter = true;
592 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
593 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000594 } else {
595 efx->rx_scatter = false;
596 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000597 }
598
Daniel Pieczko1648a232013-02-13 10:54:41 +0000599 efx_rx_config_page_split(efx);
600 if (efx->rx_buffer_order)
601 netif_dbg(efx, drv, efx->net_dev,
602 "RX buf len=%u; page order=%u batch=%u\n",
603 efx->rx_dma_len, efx->rx_buffer_order,
604 efx->rx_pages_per_batch);
605 else
606 netif_dbg(efx, drv, efx->net_dev,
607 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
608 efx->rx_dma_len, efx->rx_page_buf_step,
609 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000610
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000611 /* RX filters also have scatter-enabled flags */
612 if (efx->rx_scatter != old_rx_scatter)
Ben Hutchingsadd72472012-11-08 01:46:53 +0000613 efx->type->filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100614
Ben Hutchings14bf7182012-05-22 01:27:58 +0100615 /* We must keep at least one descriptor in a TX ring empty.
616 * We could avoid this when the queue size does not exactly
617 * match the hardware ring size, but it's not that important.
618 * Therefore we stop the queue when one more skb might fill
619 * the ring completely. We wake it when half way back to
620 * empty.
621 */
622 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
623 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
624
Ben Hutchings8ceee662008-04-27 12:55:59 +0100625 /* Initialise the channels */
626 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100627 efx_for_each_channel_tx_queue(tx_queue, channel)
628 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100629
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000630 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100631 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000632 efx_nic_generate_fill_event(rx_queue);
633 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100634
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000635 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100636 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000637
638 if (netif_device_present(efx->net_dev))
639 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100640}
641
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000642static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100643{
644 struct efx_channel *channel;
645 struct efx_tx_queue *tx_queue;
646 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100647 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100648
649 EFX_ASSERT_RESET_SERIALISED(efx);
650 BUG_ON(efx->port_enabled);
651
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100652 /* Stop RX refill */
653 efx_for_each_channel(channel, efx) {
654 efx_for_each_channel_rx_queue(rx_queue, channel)
655 rx_queue->refill_enabled = false;
656 }
657
Ben Hutchings8ceee662008-04-27 12:55:59 +0100658 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000659 /* RX packet processing is pipelined, so wait for the
660 * NAPI handler to complete. At least event queue 0
661 * might be kept active by non-data events, so don't
662 * use napi_synchronize() but actually disable NAPI
663 * temporarily.
664 */
665 if (efx_channel_has_rx_queue(channel)) {
666 efx_stop_eventq(channel);
667 efx_start_eventq(channel);
668 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100669 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100670
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100671 rc = efx->type->fini_dmaq(efx);
672 if (rc && EFX_WORKAROUND_7803(efx)) {
673 /* Schedule a reset to recover from the flush failure. The
674 * descriptor caches reference memory we're about to free,
675 * but falcon_reconfigure_mac_wrapper() won't reconnect
676 * the MACs because of the pending reset.
677 */
678 netif_err(efx, drv, efx->net_dev,
679 "Resetting to recover from flush failure\n");
680 efx_schedule_reset(efx, RESET_TYPE_ALL);
681 } else if (rc) {
682 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
683 } else {
684 netif_dbg(efx, drv, efx->net_dev,
685 "successfully flushed all queues\n");
686 }
687
688 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100689 efx_for_each_channel_rx_queue(rx_queue, channel)
690 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000691 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100692 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100693 }
694}
695
696static void efx_remove_channel(struct efx_channel *channel)
697{
698 struct efx_tx_queue *tx_queue;
699 struct efx_rx_queue *rx_queue;
700
Ben Hutchings62776d02010-06-23 11:30:07 +0000701 netif_dbg(channel->efx, drv, channel->efx->net_dev,
702 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100703
704 efx_for_each_channel_rx_queue(rx_queue, channel)
705 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000706 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100707 efx_remove_tx_queue(tx_queue);
708 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100709 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100710}
711
Ben Hutchings46426102010-09-10 06:42:33 +0000712static void efx_remove_channels(struct efx_nic *efx)
713{
714 struct efx_channel *channel;
715
716 efx_for_each_channel(channel, efx)
717 efx_remove_channel(channel);
718}
719
720int
721efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
722{
723 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
724 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000725 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100726 int rc;
727
728 rc = efx_check_disabled(efx);
729 if (rc)
730 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000731
732 /* Not all channels should be reallocated. We must avoid
733 * reallocating their buffer table entries.
734 */
735 efx_for_each_channel(channel, efx) {
736 struct efx_rx_queue *rx_queue;
737 struct efx_tx_queue *tx_queue;
738
739 if (channel->type->copy)
740 continue;
741 next_buffer_table = max(next_buffer_table,
742 channel->eventq.index +
743 channel->eventq.entries);
744 efx_for_each_channel_rx_queue(rx_queue, channel)
745 next_buffer_table = max(next_buffer_table,
746 rx_queue->rxd.index +
747 rx_queue->rxd.entries);
748 efx_for_each_channel_tx_queue(tx_queue, channel)
749 next_buffer_table = max(next_buffer_table,
750 tx_queue->txd.index +
751 tx_queue->txd.entries);
752 }
Ben Hutchings46426102010-09-10 06:42:33 +0000753
Ben Hutchings29c69a42013-01-28 19:01:06 +0000754 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000755 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100756 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000757
Ben Hutchings7f967c02012-02-13 23:45:02 +0000758 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000759 memset(other_channel, 0, sizeof(other_channel));
760 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000761 channel = efx->channel[i];
762 if (channel->type->copy)
763 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000764 if (!channel) {
765 rc = -ENOMEM;
766 goto out;
767 }
768 other_channel[i] = channel;
769 }
770
771 /* Swap entry counts and channel pointers */
772 old_rxq_entries = efx->rxq_entries;
773 old_txq_entries = efx->txq_entries;
774 efx->rxq_entries = rxq_entries;
775 efx->txq_entries = txq_entries;
776 for (i = 0; i < efx->n_channels; i++) {
777 channel = efx->channel[i];
778 efx->channel[i] = other_channel[i];
779 other_channel[i] = channel;
780 }
781
Ben Hutchings7f967c02012-02-13 23:45:02 +0000782 /* Restart buffer table allocation */
783 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000784
Ben Hutchingse8f14992010-12-07 19:47:34 +0000785 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000786 channel = efx->channel[i];
787 if (!channel->type->copy)
788 continue;
789 rc = efx_probe_channel(channel);
790 if (rc)
791 goto rollback;
792 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000793 }
Ben Hutchings46426102010-09-10 06:42:33 +0000794
Ben Hutchings7f967c02012-02-13 23:45:02 +0000795out:
796 /* Destroy unused channel structures */
797 for (i = 0; i < efx->n_channels; i++) {
798 channel = other_channel[i];
799 if (channel && channel->type->copy) {
800 efx_fini_napi_channel(channel);
801 efx_remove_channel(channel);
802 kfree(channel);
803 }
804 }
805
Ben Hutchingsd8291182012-10-05 23:35:41 +0100806 efx_soft_enable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000807 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000808 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000809 return rc;
810
811rollback:
812 /* Swap back */
813 efx->rxq_entries = old_rxq_entries;
814 efx->txq_entries = old_txq_entries;
815 for (i = 0; i < efx->n_channels; i++) {
816 channel = efx->channel[i];
817 efx->channel[i] = other_channel[i];
818 other_channel[i] = channel;
819 }
820 goto out;
821}
822
Steve Hodgson90d683a2010-06-01 11:19:39 +0000823void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100824{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000825 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100826}
827
Ben Hutchings7f967c02012-02-13 23:45:02 +0000828static const struct efx_channel_type efx_default_channel_type = {
829 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100830 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000831 .get_name = efx_get_channel_name,
832 .copy = efx_copy_channel,
833 .keep_eventq = false,
834};
835
836int efx_channel_dummy_op_int(struct efx_channel *channel)
837{
838 return 0;
839}
840
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100841void efx_channel_dummy_op_void(struct efx_channel *channel)
842{
843}
844
Ben Hutchings8ceee662008-04-27 12:55:59 +0100845/**************************************************************************
846 *
847 * Port handling
848 *
849 **************************************************************************/
850
851/* This ensures that the kernel is kept informed (via
852 * netif_carrier_on/off) of the link status, and also maintains the
853 * link status's stop on the port's TX queue.
854 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000855void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100856{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000857 struct efx_link_state *link_state = &efx->link_state;
858
Ben Hutchings8ceee662008-04-27 12:55:59 +0100859 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
860 * that no events are triggered between unregister_netdev() and the
861 * driver unloading. A more general condition is that NETDEV_CHANGE
862 * can only be generated between NETDEV_UP and NETDEV_DOWN */
863 if (!netif_running(efx->net_dev))
864 return;
865
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000866 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100867 efx->n_link_state_changes++;
868
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000869 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100870 netif_carrier_on(efx->net_dev);
871 else
872 netif_carrier_off(efx->net_dev);
873 }
874
875 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000876 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000877 netif_info(efx, link, efx->net_dev,
Ben Hutchings964e6132012-11-19 23:08:22 +0000878 "link up at %uMbps %s-duplex (MTU %d)\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000879 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings964e6132012-11-19 23:08:22 +0000880 efx->net_dev->mtu);
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000881 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000882 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100883}
884
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000885void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
886{
887 efx->link_advertising = advertising;
888 if (advertising) {
889 if (advertising & ADVERTISED_Pause)
890 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
891 else
892 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
893 if (advertising & ADVERTISED_Asym_Pause)
894 efx->wanted_fc ^= EFX_FC_TX;
895 }
896}
897
David S. Millerb56269462011-05-17 17:53:22 -0400898void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000899{
900 efx->wanted_fc = wanted_fc;
901 if (efx->link_advertising) {
902 if (wanted_fc & EFX_FC_RX)
903 efx->link_advertising |= (ADVERTISED_Pause |
904 ADVERTISED_Asym_Pause);
905 else
906 efx->link_advertising &= ~(ADVERTISED_Pause |
907 ADVERTISED_Asym_Pause);
908 if (wanted_fc & EFX_FC_TX)
909 efx->link_advertising ^= ADVERTISED_Asym_Pause;
910 }
911}
912
Ben Hutchings115122a2009-03-04 09:52:52 +0000913static void efx_fini_port(struct efx_nic *efx);
914
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000915/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
916 * the MAC appropriately. All other PHY configuration changes are pushed
917 * through phy_op->set_settings(), and pushed asynchronously to the MAC
918 * through efx_monitor().
919 *
920 * Callers must hold the mac_lock
921 */
922int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100923{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000924 enum efx_phy_mode phy_mode;
925 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100926
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000927 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100928
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000929 /* Disable PHY transmit in mac level loopbacks */
930 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800931 if (LOOPBACK_INTERNAL(efx))
932 efx->phy_mode |= PHY_MODE_TX_DISABLED;
933 else
934 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800935
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000936 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800937
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000938 if (rc)
939 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100940
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000941 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100942}
943
944/* Reinitialise the MAC to pick up new PHY settings, even if the port is
945 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000946int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100947{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000948 int rc;
949
Ben Hutchings8ceee662008-04-27 12:55:59 +0100950 EFX_ASSERT_RESET_SERIALISED(efx);
951
952 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000953 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100954 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000955
956 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100957}
958
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000959/* Asynchronous work item for changing MAC promiscuity and multicast
960 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
961 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800962static void efx_mac_work(struct work_struct *data)
963{
964 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
965
966 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100967 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100968 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800969 mutex_unlock(&efx->mac_lock);
970}
971
Ben Hutchings8ceee662008-04-27 12:55:59 +0100972static int efx_probe_port(struct efx_nic *efx)
973{
974 int rc;
975
Ben Hutchings62776d02010-06-23 11:30:07 +0000976 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100977
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000978 if (phy_flash_cfg)
979 efx->phy_mode = PHY_MODE_SPECIAL;
980
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000981 /* Connect up MAC/PHY operations table */
982 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100983 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000984 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100985
Ben Hutchingse332bcb2011-12-20 01:22:51 +0000986 /* Initialise MAC address to permanent address */
987 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100988
989 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100990}
991
992static int efx_init_port(struct efx_nic *efx)
993{
994 int rc;
995
Ben Hutchings62776d02010-06-23 11:30:07 +0000996 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100997
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000998 mutex_lock(&efx->mac_lock);
999
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001000 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001001 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001002 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001003
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001004 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001005
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001006 /* Reconfigure the MAC before creating dma queues (required for
1007 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001008 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001009
1010 /* Ensure the PHY advertises the correct flow control settings */
1011 rc = efx->phy_op->reconfigure(efx);
1012 if (rc)
1013 goto fail2;
1014
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001015 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001016 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001017
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001018fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001019 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001020fail1:
1021 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001022 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001023}
1024
Ben Hutchings8ceee662008-04-27 12:55:59 +01001025static void efx_start_port(struct efx_nic *efx)
1026{
Ben Hutchings62776d02010-06-23 11:30:07 +00001027 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001028 BUG_ON(efx->port_enabled);
1029
1030 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001031 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001032
1033 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1034 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001035 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001036
Ben Hutchings8ceee662008-04-27 12:55:59 +01001037 mutex_unlock(&efx->mac_lock);
1038}
1039
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001040/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001041static void efx_stop_port(struct efx_nic *efx)
1042{
Ben Hutchings62776d02010-06-23 11:30:07 +00001043 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001044
1045 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001046 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001047 mutex_unlock(&efx->mac_lock);
1048
1049 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001050 netif_addr_lock_bh(efx->net_dev);
1051 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001052}
1053
1054static void efx_fini_port(struct efx_nic *efx)
1055{
Ben Hutchings62776d02010-06-23 11:30:07 +00001056 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001057
1058 if (!efx->port_initialized)
1059 return;
1060
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001061 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001062 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001063
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001064 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001065 efx_link_status_changed(efx);
1066}
1067
1068static void efx_remove_port(struct efx_nic *efx)
1069{
Ben Hutchings62776d02010-06-23 11:30:07 +00001070 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001071
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001072 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001073}
1074
1075/**************************************************************************
1076 *
1077 * NIC handling
1078 *
1079 **************************************************************************/
1080
1081/* This configures the PCI device to enable I/O and DMA. */
1082static int efx_init_io(struct efx_nic *efx)
1083{
1084 struct pci_dev *pci_dev = efx->pci_dev;
1085 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001086 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001087 int rc;
1088
Ben Hutchings62776d02010-06-23 11:30:07 +00001089 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001090
1091 rc = pci_enable_device(pci_dev);
1092 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001093 netif_err(efx, probe, efx->net_dev,
1094 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001095 goto fail1;
1096 }
1097
1098 pci_set_master(pci_dev);
1099
1100 /* Set the PCI DMA mask. Try all possibilities from our
1101 * genuine mask down to 32 bits, because some architectures
1102 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1103 * masks event though they reject 46 bit masks.
1104 */
1105 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001106 if (dma_supported(&pci_dev->dev, dma_mask)) {
1107 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001108 if (rc == 0)
1109 break;
1110 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001111 dma_mask >>= 1;
1112 }
1113 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001114 netif_err(efx, probe, efx->net_dev,
1115 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001116 goto fail2;
1117 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001118 netif_dbg(efx, probe, efx->net_dev,
1119 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001120 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001121 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001122 /* dma_set_coherent_mask() is not *allowed* to
1123 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001124 * but just in case...
1125 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001126 netif_err(efx, probe, efx->net_dev,
1127 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001128 goto fail2;
1129 }
1130
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001131 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1132 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001133 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001134 netif_err(efx, probe, efx->net_dev,
1135 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001136 rc = -EIO;
1137 goto fail3;
1138 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001139 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001140 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001141 netif_err(efx, probe, efx->net_dev,
1142 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001143 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001144 rc = -ENOMEM;
1145 goto fail4;
1146 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001147 netif_dbg(efx, probe, efx->net_dev,
1148 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001149 (unsigned long long)efx->membase_phys, mem_map_size,
1150 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001151
1152 return 0;
1153
1154 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001155 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001156 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001157 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001158 fail2:
1159 pci_disable_device(efx->pci_dev);
1160 fail1:
1161 return rc;
1162}
1163
1164static void efx_fini_io(struct efx_nic *efx)
1165{
Ben Hutchings62776d02010-06-23 11:30:07 +00001166 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001167
1168 if (efx->membase) {
1169 iounmap(efx->membase);
1170 efx->membase = NULL;
1171 }
1172
1173 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001174 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001175 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001176 }
1177
1178 pci_disable_device(efx->pci_dev);
1179}
1180
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001181static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001182{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001183 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001184 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001185 int cpu;
1186
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001187 if (rss_cpus) {
1188 count = rss_cpus;
1189 } else {
1190 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1191 netif_warn(efx, probe, efx->net_dev,
1192 "RSS disabled due to allocation failure\n");
1193 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001194 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001195
1196 count = 0;
1197 for_each_online_cpu(cpu) {
1198 if (!cpumask_test_cpu(cpu, thread_mask)) {
1199 ++count;
1200 cpumask_or(thread_mask, thread_mask,
1201 topology_thread_cpumask(cpu));
1202 }
1203 }
1204
1205 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001206 }
1207
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001208 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1209 * table entries that are inaccessible to VFs
1210 */
1211 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1212 count > efx_vf_size(efx)) {
1213 netif_warn(efx, probe, efx->net_dev,
1214 "Reducing number of RSS channels from %u to %u for "
1215 "VF support. Increase vf-msix-limit to use more "
1216 "channels on the PF.\n",
1217 count, efx_vf_size(efx));
1218 count = efx_vf_size(efx);
1219 }
1220
Ben Hutchings46123d02008-09-01 12:47:33 +01001221 return count;
1222}
1223
1224/* Probe the number and type of interrupts we are able to obtain, and
1225 * the resulting numbers of channels and RX queues.
1226 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001227static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001228{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001229 unsigned int extra_channels = 0;
1230 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001231 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001232
Ben Hutchings7f967c02012-02-13 23:45:02 +00001233 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1234 if (efx->extra_channel_type[i])
1235 ++extra_channels;
1236
Ben Hutchings8ceee662008-04-27 12:55:59 +01001237 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001238 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001239 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001240
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001241 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001242 if (separate_tx_channels)
1243 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001244 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001245 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001246
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001247 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001248 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001249 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001250 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001251 netif_err(efx, drv, efx->net_dev,
1252 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001253 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001254 netif_err(efx, drv, efx->net_dev,
1255 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001256 EFX_BUG_ON_PARANOID(rc >= n_channels);
1257 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001258 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001259 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001260 }
1261
1262 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001263 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001264 if (n_channels > extra_channels)
1265 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001266 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001267 efx->n_tx_channels = max(n_channels / 2, 1U);
1268 efx->n_rx_channels = max(n_channels -
1269 efx->n_tx_channels,
1270 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001271 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001272 efx->n_tx_channels = n_channels;
1273 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001274 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001275 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001276 efx_get_channel(efx, i)->irq =
1277 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001278 } else {
1279 /* Fall back to single channel MSI */
1280 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001281 netif_err(efx, drv, efx->net_dev,
1282 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001283 }
1284 }
1285
1286 /* Try single interrupt MSI */
1287 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001288 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001289 efx->n_rx_channels = 1;
1290 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001291 rc = pci_enable_msi(efx->pci_dev);
1292 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001293 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001294 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001295 netif_err(efx, drv, efx->net_dev,
1296 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001297 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1298 }
1299 }
1300
1301 /* Assume legacy interrupts */
1302 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001303 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001304 efx->n_rx_channels = 1;
1305 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001306 efx->legacy_irq = efx->pci_dev->irq;
1307 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001308
Ben Hutchings7f967c02012-02-13 23:45:02 +00001309 /* Assign extra channels if possible */
1310 j = efx->n_channels;
1311 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1312 if (!efx->extra_channel_type[i])
1313 continue;
1314 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1315 efx->n_channels <= extra_channels) {
1316 efx->extra_channel_type[i]->handle_no_channel(efx);
1317 } else {
1318 --j;
1319 efx_get_channel(efx, j)->type =
1320 efx->extra_channel_type[i];
1321 }
1322 }
1323
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001324 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001325 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001326 efx->n_rx_channels : efx_vf_size(efx));
1327
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001328 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001329}
1330
Ben Hutchingsd8291182012-10-05 23:35:41 +01001331static void efx_soft_enable_interrupts(struct efx_nic *efx)
1332{
1333 struct efx_channel *channel;
1334
1335 BUG_ON(efx->state == STATE_DISABLED);
1336
1337 efx->irq_soft_enabled = true;
1338 smp_wmb();
1339
1340 efx_for_each_channel(channel, efx) {
1341 if (!channel->type->keep_eventq)
1342 efx_init_eventq(channel);
1343 efx_start_eventq(channel);
1344 }
1345
1346 efx_mcdi_mode_event(efx);
1347}
1348
1349static void efx_soft_disable_interrupts(struct efx_nic *efx)
1350{
1351 struct efx_channel *channel;
1352
1353 if (efx->state == STATE_DISABLED)
1354 return;
1355
1356 efx_mcdi_mode_poll(efx);
1357
1358 efx->irq_soft_enabled = false;
1359 smp_wmb();
1360
1361 if (efx->legacy_irq)
1362 synchronize_irq(efx->legacy_irq);
1363
1364 efx_for_each_channel(channel, efx) {
1365 if (channel->irq)
1366 synchronize_irq(channel->irq);
1367
1368 efx_stop_eventq(channel);
1369 if (!channel->type->keep_eventq)
1370 efx_fini_eventq(channel);
1371 }
1372}
1373
1374static void efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001375{
1376 struct efx_channel *channel;
1377
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001378 BUG_ON(efx->state == STATE_DISABLED);
1379
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001380 if (efx->eeh_disabled_legacy_irq) {
1381 enable_irq(efx->legacy_irq);
1382 efx->eeh_disabled_legacy_irq = false;
1383 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001384
Ben Hutchings86094f72013-08-21 19:51:04 +01001385 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001386
1387 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001388 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001389 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001390 }
1391
Ben Hutchingsd8291182012-10-05 23:35:41 +01001392 efx_soft_enable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001393}
1394
Ben Hutchingsd8291182012-10-05 23:35:41 +01001395static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001396{
1397 struct efx_channel *channel;
1398
Ben Hutchingsd8291182012-10-05 23:35:41 +01001399 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001400
1401 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001402 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001403 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001404 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001405
Ben Hutchings86094f72013-08-21 19:51:04 +01001406 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001407}
1408
Ben Hutchings8ceee662008-04-27 12:55:59 +01001409static void efx_remove_interrupts(struct efx_nic *efx)
1410{
1411 struct efx_channel *channel;
1412
1413 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001414 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001415 channel->irq = 0;
1416 pci_disable_msi(efx->pci_dev);
1417 pci_disable_msix(efx->pci_dev);
1418
1419 /* Remove legacy interrupt */
1420 efx->legacy_irq = 0;
1421}
1422
Ben Hutchings8831da72008-09-01 12:47:48 +01001423static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001424{
Ben Hutchings602a5322011-05-16 17:32:39 +01001425 struct efx_channel *channel;
1426 struct efx_tx_queue *tx_queue;
1427
Ben Hutchings97653432011-01-12 18:26:56 +00001428 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001429 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001430
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001431 /* We need to mark which channels really have RX and TX
1432 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001433 * RX-only and TX-only channels.
1434 */
1435 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001436 if (channel->channel < efx->n_rx_channels)
1437 channel->rx_queue.core_index = channel->channel;
1438 else
1439 channel->rx_queue.core_index = -1;
1440
Ben Hutchings602a5322011-05-16 17:32:39 +01001441 efx_for_each_channel_tx_queue(tx_queue, channel)
1442 tx_queue->queue -= (efx->tx_channel_offset *
1443 EFX_TXQ_TYPES);
1444 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001445}
1446
1447static int efx_probe_nic(struct efx_nic *efx)
1448{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001449 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001450 int rc;
1451
Ben Hutchings62776d02010-06-23 11:30:07 +00001452 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001453
1454 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001455 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001456 if (rc)
1457 return rc;
1458
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001459 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001460 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001461 rc = efx_probe_interrupts(efx);
1462 if (rc)
1463 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001464
Ben Hutchings28e47c42012-02-15 01:58:49 +00001465 efx->type->dimension_resources(efx);
1466
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001467 if (efx->n_channels > 1)
1468 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001469 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001470 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001471 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001472
Ben Hutchings8831da72008-09-01 12:47:48 +01001473 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001474 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1475 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001476
1477 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001478 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1479 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001480
1481 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001482
1483fail:
1484 efx->type->remove(efx);
1485 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001486}
1487
1488static void efx_remove_nic(struct efx_nic *efx)
1489{
Ben Hutchings62776d02010-06-23 11:30:07 +00001490 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001491
1492 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001493 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001494}
1495
Ben Hutchingsadd72472012-11-08 01:46:53 +00001496static int efx_probe_filters(struct efx_nic *efx)
1497{
1498 int rc;
1499
1500 spin_lock_init(&efx->filter_lock);
1501
1502 rc = efx->type->filter_table_probe(efx);
1503 if (rc)
1504 return rc;
1505
1506#ifdef CONFIG_RFS_ACCEL
1507 if (efx->type->offload_features & NETIF_F_NTUPLE) {
1508 efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
1509 sizeof(*efx->rps_flow_id),
1510 GFP_KERNEL);
1511 if (!efx->rps_flow_id) {
1512 efx->type->filter_table_remove(efx);
1513 return -ENOMEM;
1514 }
1515 }
1516#endif
1517
1518 return 0;
1519}
1520
1521static void efx_remove_filters(struct efx_nic *efx)
1522{
1523#ifdef CONFIG_RFS_ACCEL
1524 kfree(efx->rps_flow_id);
1525#endif
1526 efx->type->filter_table_remove(efx);
1527}
1528
1529static void efx_restore_filters(struct efx_nic *efx)
1530{
1531 efx->type->filter_table_restore(efx);
1532}
1533
Ben Hutchings8ceee662008-04-27 12:55:59 +01001534/**************************************************************************
1535 *
1536 * NIC startup/shutdown
1537 *
1538 *************************************************************************/
1539
1540static int efx_probe_all(struct efx_nic *efx)
1541{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001542 int rc;
1543
Ben Hutchings8ceee662008-04-27 12:55:59 +01001544 rc = efx_probe_nic(efx);
1545 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001546 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001547 goto fail1;
1548 }
1549
Ben Hutchings8ceee662008-04-27 12:55:59 +01001550 rc = efx_probe_port(efx);
1551 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001552 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001553 goto fail2;
1554 }
1555
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001556 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1557 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1558 rc = -EINVAL;
1559 goto fail3;
1560 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001561 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001562
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001563 rc = efx_probe_filters(efx);
1564 if (rc) {
1565 netif_err(efx, probe, efx->net_dev,
1566 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001567 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001568 }
1569
Ben Hutchings7f967c02012-02-13 23:45:02 +00001570 rc = efx_probe_channels(efx);
1571 if (rc)
1572 goto fail4;
1573
Ben Hutchings8ceee662008-04-27 12:55:59 +01001574 return 0;
1575
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001576 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001577 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001578 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001579 efx_remove_port(efx);
1580 fail2:
1581 efx_remove_nic(efx);
1582 fail1:
1583 return rc;
1584}
1585
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001586/* If the interface is supposed to be running but is not, start
1587 * the hardware and software data path, regular activity for the port
1588 * (MAC statistics, link polling, etc.) and schedule the port to be
1589 * reconfigured. Interrupts must already be enabled. This function
1590 * is safe to call multiple times, so long as the NIC is not disabled.
1591 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001592 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001593static void efx_start_all(struct efx_nic *efx)
1594{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001595 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001596 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001597
1598 /* Check that it is appropriate to restart the interface. All
1599 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001600 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001601 return;
1602
Ben Hutchings8ceee662008-04-27 12:55:59 +01001603 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001604 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001605
Alexandre Rames626950d2013-01-14 17:20:22 +00001606 /* Start the hardware monitor if there is one */
1607 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001608 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1609 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001610
1611 /* If link state detection is normally event-driven, we have
1612 * to poll now because we could have missed a change
1613 */
1614 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001615 mutex_lock(&efx->mac_lock);
1616 if (efx->phy_op->poll(efx))
1617 efx_link_status_changed(efx);
1618 mutex_unlock(&efx->mac_lock);
1619 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001620
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001621 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001622}
1623
1624/* Flush all delayed work. Should only be called when no more delayed work
1625 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1626 * since we're holding the rtnl_lock at this point. */
1627static void efx_flush_all(struct efx_nic *efx)
1628{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001629 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001630 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001631 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001632 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001633 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001634}
1635
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001636/* Quiesce the hardware and software data path, and regular activity
1637 * for the port without bringing the link down. Safe to call multiple
1638 * times with the NIC in almost any state, but interrupts should be
1639 * enabled. Requires the RTNL lock.
1640 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001641static void efx_stop_all(struct efx_nic *efx)
1642{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001643 EFX_ASSERT_RESET_SERIALISED(efx);
1644
1645 /* port_enabled can be read safely under the rtnl lock */
1646 if (!efx->port_enabled)
1647 return;
1648
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001649 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001650 efx_stop_port(efx);
1651
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001652 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001653 efx_flush_all(efx);
1654
Ben Hutchings29c69a42013-01-28 19:01:06 +00001655 /* Stop the kernel transmit interface. This is only valid if
1656 * the device is stopped or detached; otherwise the watchdog
1657 * may fire immediately.
1658 */
1659 WARN_ON(netif_running(efx->net_dev) &&
1660 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001661 netif_tx_disable(efx->net_dev);
1662
1663 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001664}
1665
1666static void efx_remove_all(struct efx_nic *efx)
1667{
Ben Hutchings46426102010-09-10 06:42:33 +00001668 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001669 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001670 efx_remove_port(efx);
1671 efx_remove_nic(efx);
1672}
1673
Ben Hutchings8ceee662008-04-27 12:55:59 +01001674/**************************************************************************
1675 *
1676 * Interrupt moderation
1677 *
1678 **************************************************************************/
1679
Ben Hutchingscc180b62011-12-08 19:51:47 +00001680static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001681{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001682 if (usecs == 0)
1683 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001684 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001685 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001686 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001687}
1688
Ben Hutchings8ceee662008-04-27 12:55:59 +01001689/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001690int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1691 unsigned int rx_usecs, bool rx_adaptive,
1692 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001693{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001694 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001695 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1696 efx->timer_quantum_ns,
1697 1000);
1698 unsigned int tx_ticks;
1699 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001700
1701 EFX_ASSERT_RESET_SERIALISED(efx);
1702
Ben Hutchingscc180b62011-12-08 19:51:47 +00001703 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001704 return -EINVAL;
1705
Ben Hutchingscc180b62011-12-08 19:51:47 +00001706 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1707 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1708
Ben Hutchings9e393b32011-09-05 07:43:04 +00001709 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1710 !rx_may_override_tx) {
1711 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1712 "RX and TX IRQ moderation must be equal\n");
1713 return -EINVAL;
1714 }
1715
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001716 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001717 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001718 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001719 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001720 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001721 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001722 channel->irq_moderation = tx_ticks;
1723 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001724
1725 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001726}
1727
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001728void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1729 unsigned int *rx_usecs, bool *rx_adaptive)
1730{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001731 /* We must round up when converting ticks to microseconds
1732 * because we round down when converting the other way.
1733 */
1734
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001735 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001736 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1737 efx->timer_quantum_ns,
1738 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001739
1740 /* If channels are shared between RX and TX, so is IRQ
1741 * moderation. Otherwise, IRQ moderation is the same for all
1742 * TX channels and is not adaptive.
1743 */
1744 if (efx->tx_channel_offset == 0)
1745 *tx_usecs = *rx_usecs;
1746 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001747 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001748 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001749 efx->timer_quantum_ns,
1750 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001751}
1752
Ben Hutchings8ceee662008-04-27 12:55:59 +01001753/**************************************************************************
1754 *
1755 * Hardware monitor
1756 *
1757 **************************************************************************/
1758
Ben Hutchingse254c272010-09-20 08:44:10 +00001759/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001760static void efx_monitor(struct work_struct *data)
1761{
1762 struct efx_nic *efx = container_of(data, struct efx_nic,
1763 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001764
Ben Hutchings62776d02010-06-23 11:30:07 +00001765 netif_vdbg(efx, timer, efx->net_dev,
1766 "hardware monitor executing on CPU %d\n",
1767 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001768 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001769
Ben Hutchings8ceee662008-04-27 12:55:59 +01001770 /* If the mac_lock is already held then it is likely a port
1771 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001772 * most of the work of monitor() anyway. */
1773 if (mutex_trylock(&efx->mac_lock)) {
1774 if (efx->port_enabled)
1775 efx->type->monitor(efx);
1776 mutex_unlock(&efx->mac_lock);
1777 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001778
Ben Hutchings8ceee662008-04-27 12:55:59 +01001779 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1780 efx_monitor_interval);
1781}
1782
1783/**************************************************************************
1784 *
1785 * ioctls
1786 *
1787 *************************************************************************/
1788
1789/* Net device ioctl
1790 * Context: process, rtnl_lock() held.
1791 */
1792static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1793{
Ben Hutchings767e4682008-09-01 12:43:14 +01001794 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001795 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001796
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001797 if (cmd == SIOCSHWTSTAMP)
1798 return efx_ptp_ioctl(efx, ifr, cmd);
1799
Ben Hutchings68e7f452009-04-29 08:05:08 +00001800 /* Convert phy_id from older PRTAD/DEVAD format */
1801 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1802 (data->phy_id & 0xfc00) == 0x0400)
1803 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1804
1805 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001806}
1807
1808/**************************************************************************
1809 *
1810 * NAPI interface
1811 *
1812 **************************************************************************/
1813
Ben Hutchings7f967c02012-02-13 23:45:02 +00001814static void efx_init_napi_channel(struct efx_channel *channel)
1815{
1816 struct efx_nic *efx = channel->efx;
1817
1818 channel->napi_dev = efx->net_dev;
1819 netif_napi_add(channel->napi_dev, &channel->napi_str,
1820 efx_poll, napi_weight);
1821}
1822
Ben Hutchingse8f14992010-12-07 19:47:34 +00001823static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001824{
1825 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001826
Ben Hutchings7f967c02012-02-13 23:45:02 +00001827 efx_for_each_channel(channel, efx)
1828 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001829}
1830
1831static void efx_fini_napi_channel(struct efx_channel *channel)
1832{
1833 if (channel->napi_dev)
1834 netif_napi_del(&channel->napi_str);
1835 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001836}
1837
1838static void efx_fini_napi(struct efx_nic *efx)
1839{
1840 struct efx_channel *channel;
1841
Ben Hutchingse8f14992010-12-07 19:47:34 +00001842 efx_for_each_channel(channel, efx)
1843 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001844}
1845
1846/**************************************************************************
1847 *
1848 * Kernel netpoll interface
1849 *
1850 *************************************************************************/
1851
1852#ifdef CONFIG_NET_POLL_CONTROLLER
1853
1854/* Although in the common case interrupts will be disabled, this is not
1855 * guaranteed. However, all our work happens inside the NAPI callback,
1856 * so no locking is required.
1857 */
1858static void efx_netpoll(struct net_device *net_dev)
1859{
Ben Hutchings767e4682008-09-01 12:43:14 +01001860 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001861 struct efx_channel *channel;
1862
Ben Hutchings64ee3122008-09-01 12:47:38 +01001863 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001864 efx_schedule_channel(channel);
1865}
1866
1867#endif
1868
1869/**************************************************************************
1870 *
1871 * Kernel net device interface
1872 *
1873 *************************************************************************/
1874
1875/* Context: process, rtnl_lock() held. */
1876static int efx_net_open(struct net_device *net_dev)
1877{
Ben Hutchings767e4682008-09-01 12:43:14 +01001878 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001879 int rc;
1880
Ben Hutchings62776d02010-06-23 11:30:07 +00001881 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1882 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001883
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001884 rc = efx_check_disabled(efx);
1885 if (rc)
1886 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001887 if (efx->phy_mode & PHY_MODE_SPECIAL)
1888 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001889 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1890 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001891
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001892 /* Notify the kernel of the link state polled during driver load,
1893 * before the monitor starts running */
1894 efx_link_status_changed(efx);
1895
Ben Hutchings8ceee662008-04-27 12:55:59 +01001896 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001897 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001898 return 0;
1899}
1900
1901/* Context: process, rtnl_lock() held.
1902 * Note that the kernel will ignore our return code; this method
1903 * should really be a void.
1904 */
1905static int efx_net_stop(struct net_device *net_dev)
1906{
Ben Hutchings767e4682008-09-01 12:43:14 +01001907 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001908
Ben Hutchings62776d02010-06-23 11:30:07 +00001909 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1910 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001911
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001912 /* Stop the device and flush all the channels */
1913 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001914
1915 return 0;
1916}
1917
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001918/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001919static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1920 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001921{
Ben Hutchings767e4682008-09-01 12:43:14 +01001922 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001923
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001924 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00001925 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001926 spin_unlock_bh(&efx->stats_lock);
1927
Ben Hutchings8ceee662008-04-27 12:55:59 +01001928 return stats;
1929}
1930
1931/* Context: netif_tx_lock held, BHs disabled. */
1932static void efx_watchdog(struct net_device *net_dev)
1933{
Ben Hutchings767e4682008-09-01 12:43:14 +01001934 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001935
Ben Hutchings62776d02010-06-23 11:30:07 +00001936 netif_err(efx, tx_err, efx->net_dev,
1937 "TX stuck with port_enabled=%d: resetting channels\n",
1938 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001939
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001940 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001941}
1942
1943
1944/* Context: process, rtnl_lock() held. */
1945static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1946{
Ben Hutchings767e4682008-09-01 12:43:14 +01001947 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001948 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001949
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001950 rc = efx_check_disabled(efx);
1951 if (rc)
1952 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001953 if (new_mtu > EFX_MAX_MTU)
1954 return -EINVAL;
1955
Ben Hutchings62776d02010-06-23 11:30:07 +00001956 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001957
Ben Hutchings29c69a42013-01-28 19:01:06 +00001958 efx_device_detach_sync(efx);
1959 efx_stop_all(efx);
1960
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001961 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001962 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001963 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001964 mutex_unlock(&efx->mac_lock);
1965
Ben Hutchings8ceee662008-04-27 12:55:59 +01001966 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00001967 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001968 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001969}
1970
1971static int efx_set_mac_address(struct net_device *net_dev, void *data)
1972{
Ben Hutchings767e4682008-09-01 12:43:14 +01001973 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001974 struct sockaddr *addr = data;
1975 char *new_addr = addr->sa_data;
1976
Ben Hutchings8ceee662008-04-27 12:55:59 +01001977 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001978 netif_err(efx, drv, efx->net_dev,
1979 "invalid ethernet MAC address requested: %pM\n",
1980 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00001981 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001982 }
1983
1984 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001985 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001986
1987 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001988 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01001989 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001990 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001991
1992 return 0;
1993}
1994
Ben Hutchingsa816f752008-09-01 12:49:12 +01001995/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00001996static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001997{
Ben Hutchings767e4682008-09-01 12:43:14 +01001998 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001999
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002000 if (efx->port_enabled)
2001 queue_work(efx->workqueue, &efx->mac_work);
2002 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002003}
2004
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002005static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002006{
2007 struct efx_nic *efx = netdev_priv(net_dev);
2008
2009 /* If disabling RX n-tuple filtering, clear existing filters */
2010 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2011 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2012
2013 return 0;
2014}
2015
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002016static const struct net_device_ops efx_netdev_ops = {
2017 .ndo_open = efx_net_open,
2018 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002019 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002020 .ndo_tx_timeout = efx_watchdog,
2021 .ndo_start_xmit = efx_hard_start_xmit,
2022 .ndo_validate_addr = eth_validate_addr,
2023 .ndo_do_ioctl = efx_ioctl,
2024 .ndo_change_mtu = efx_change_mtu,
2025 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002026 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002027 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002028#ifdef CONFIG_SFC_SRIOV
2029 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2030 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2031 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2032 .ndo_get_vf_config = efx_sriov_get_vf_config,
2033#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002034#ifdef CONFIG_NET_POLL_CONTROLLER
2035 .ndo_poll_controller = efx_netpoll,
2036#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002037 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002038#ifdef CONFIG_RFS_ACCEL
2039 .ndo_rx_flow_steer = efx_filter_rfs,
2040#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002041};
2042
Ben Hutchings7dde5962008-12-12 22:09:38 -08002043static void efx_update_name(struct efx_nic *efx)
2044{
2045 strcpy(efx->name, efx->net_dev->name);
2046 efx_mtd_rename(efx);
2047 efx_set_channel_names(efx);
2048}
2049
Ben Hutchings8ceee662008-04-27 12:55:59 +01002050static int efx_netdev_event(struct notifier_block *this,
2051 unsigned long event, void *ptr)
2052{
Jiri Pirko351638e2013-05-28 01:30:21 +00002053 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002054
Ben Hutchings7dde5962008-12-12 22:09:38 -08002055 if (net_dev->netdev_ops == &efx_netdev_ops &&
2056 event == NETDEV_CHANGENAME)
2057 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002058
2059 return NOTIFY_DONE;
2060}
2061
2062static struct notifier_block efx_netdev_notifier = {
2063 .notifier_call = efx_netdev_event,
2064};
2065
Ben Hutchings06d5e192008-12-12 21:47:23 -08002066static ssize_t
2067show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2068{
2069 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2070 return sprintf(buf, "%d\n", efx->phy_type);
2071}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002072static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002073
Ben Hutchings8ceee662008-04-27 12:55:59 +01002074static int efx_register_netdev(struct efx_nic *efx)
2075{
2076 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002077 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002078 int rc;
2079
2080 net_dev->watchdog_timeo = 5 * HZ;
2081 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002082 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002083 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002084 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002085
Ben Hutchings7dde5962008-12-12 22:09:38 -08002086 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002087
Ben Hutchings7153f622012-07-27 20:50:52 +01002088 /* Enable resets to be scheduled and check whether any were
2089 * already requested. If so, the NIC is probably hosed so we
2090 * abort.
2091 */
2092 efx->state = STATE_READY;
2093 smp_mb(); /* ensure we change state before checking reset_pending */
2094 if (efx->reset_pending) {
2095 netif_err(efx, probe, efx->net_dev,
2096 "aborting probe due to scheduled reset\n");
2097 rc = -EIO;
2098 goto fail_locked;
2099 }
2100
Ben Hutchingsaed06282009-08-26 08:16:27 +00002101 rc = dev_alloc_name(net_dev, net_dev->name);
2102 if (rc < 0)
2103 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002104 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002105
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002106 /* Always start with carrier off; PHY events will detect the link */
2107 netif_carrier_off(net_dev);
2108
Ben Hutchingsaed06282009-08-26 08:16:27 +00002109 rc = register_netdevice(net_dev);
2110 if (rc)
2111 goto fail_locked;
2112
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002113 efx_for_each_channel(channel, efx) {
2114 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002115 efx_for_each_channel_tx_queue(tx_queue, channel)
2116 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002117 }
2118
Ben Hutchings7dde5962008-12-12 22:09:38 -08002119 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002120
Ben Hutchings06d5e192008-12-12 21:47:23 -08002121 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2122 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002123 netif_err(efx, drv, efx->net_dev,
2124 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002125 goto fail_registered;
2126 }
2127
Ben Hutchings8ceee662008-04-27 12:55:59 +01002128 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002129
Ben Hutchings7153f622012-07-27 20:50:52 +01002130fail_registered:
2131 rtnl_lock();
2132 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002133fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002134 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002135 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002136 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002137 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002138}
2139
2140static void efx_unregister_netdev(struct efx_nic *efx)
2141{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002142 if (!efx->net_dev)
2143 return;
2144
Ben Hutchings767e4682008-09-01 12:43:14 +01002145 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002146
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002147 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2148 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002149
2150 rtnl_lock();
2151 unregister_netdevice(efx->net_dev);
2152 efx->state = STATE_UNINIT;
2153 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002154}
2155
2156/**************************************************************************
2157 *
2158 * Device reset and suspend
2159 *
2160 **************************************************************************/
2161
Ben Hutchings2467ca42008-09-01 12:48:50 +01002162/* Tears down the entire software state and most of the hardware state
2163 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002164void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002165{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002166 EFX_ASSERT_RESET_SERIALISED(efx);
2167
Ben Hutchings2467ca42008-09-01 12:48:50 +01002168 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002169 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002170
2171 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002172 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2173 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002174 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002175}
2176
Ben Hutchings2467ca42008-09-01 12:48:50 +01002177/* This function will always ensure that the locks acquired in
2178 * efx_reset_down() are released. A failure return code indicates
2179 * that we were unable to reinitialise the hardware, and the
2180 * driver should be disabled. If ok is false, then the rx and tx
2181 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002182int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002183{
2184 int rc;
2185
Ben Hutchings2467ca42008-09-01 12:48:50 +01002186 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002187
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002188 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002189 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002190 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002191 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002192 }
2193
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002194 if (!ok)
2195 goto fail;
2196
Steve Hodgson4b988282009-01-29 17:50:51 +00002197 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002198 rc = efx->phy_op->init(efx);
2199 if (rc)
2200 goto fail;
2201 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002202 netif_err(efx, drv, efx->net_dev,
2203 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002204 }
2205
Ben Hutchings710b2082011-09-03 00:15:00 +01002206 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002207
Ben Hutchingsd8291182012-10-05 23:35:41 +01002208 efx_enable_interrupts(efx);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002209 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002210 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002211
2212 mutex_unlock(&efx->mac_lock);
2213
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002214 efx_start_all(efx);
2215
2216 return 0;
2217
2218fail:
2219 efx->port_initialized = false;
2220
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002221 mutex_unlock(&efx->mac_lock);
2222
Ben Hutchings8ceee662008-04-27 12:55:59 +01002223 return rc;
2224}
2225
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002226/* Reset the NIC using the specified method. Note that the reset may
2227 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002228 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002229 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002230 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002231int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002232{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002233 int rc, rc2;
2234 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002235
Ben Hutchings62776d02010-06-23 11:30:07 +00002236 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2237 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002238
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002239 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002240 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002241
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002242 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002243 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002244 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002245 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002246 }
2247
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002248 /* Clear flags for the scopes we covered. We assume the NIC and
2249 * driver are now quiescent so that there is no race here.
2250 */
2251 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002252
2253 /* Reinitialise bus-mastering, which may have been turned off before
2254 * the reset was scheduled. This is still appropriate, even in the
2255 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2256 * can respond to requests. */
2257 pci_set_master(efx->pci_dev);
2258
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002259out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002260 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002261 disabled = rc ||
2262 method == RESET_TYPE_DISABLE ||
2263 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002264 rc2 = efx_reset_up(efx, method, !disabled);
2265 if (rc2) {
2266 disabled = true;
2267 if (!rc)
2268 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002269 }
2270
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002271 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002272 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002273 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002274 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002275 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002276 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002277 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002278 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002279 return rc;
2280}
2281
Alexandre Rames626950d2013-01-14 17:20:22 +00002282/* Try recovery mechanisms.
2283 * For now only EEH is supported.
2284 * Returns 0 if the recovery mechanisms are unsuccessful.
2285 * Returns a non-zero value otherwise.
2286 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002287int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002288{
2289#ifdef CONFIG_EEH
2290 /* A PCI error can occur and not be seen by EEH because nothing
2291 * happens on the PCI bus. In this case the driver may fail and
2292 * schedule a 'recover or reset', leading to this recovery handler.
2293 * Manually call the eeh failure check function.
2294 */
2295 struct eeh_dev *eehdev =
2296 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2297
2298 if (eeh_dev_check_failure(eehdev)) {
2299 /* The EEH mechanisms will handle the error and reset the
2300 * device if necessary.
2301 */
2302 return 1;
2303 }
2304#endif
2305 return 0;
2306}
2307
Ben Hutchings8ceee662008-04-27 12:55:59 +01002308/* The worker thread exists so that code that cannot sleep can
2309 * schedule a reset for later.
2310 */
2311static void efx_reset_work(struct work_struct *data)
2312{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002313 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002314 unsigned long pending;
2315 enum reset_type method;
2316
2317 pending = ACCESS_ONCE(efx->reset_pending);
2318 method = fls(pending) - 1;
2319
2320 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2321 method == RESET_TYPE_RECOVER_OR_ALL) &&
2322 efx_try_recovery(efx))
2323 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002324
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002325 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002326 return;
2327
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002328 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002329
2330 /* We checked the state in efx_schedule_reset() but it may
2331 * have changed by now. Now that we have the RTNL lock,
2332 * it cannot change again.
2333 */
2334 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002335 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002336
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002337 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002338}
2339
2340void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2341{
2342 enum reset_type method;
2343
Alexandre Rames626950d2013-01-14 17:20:22 +00002344 if (efx->state == STATE_RECOVERY) {
2345 netif_dbg(efx, drv, efx->net_dev,
2346 "recovering: skip scheduling %s reset\n",
2347 RESET_TYPE(type));
2348 return;
2349 }
2350
Ben Hutchings8ceee662008-04-27 12:55:59 +01002351 switch (type) {
2352 case RESET_TYPE_INVISIBLE:
2353 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002354 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002355 case RESET_TYPE_WORLD:
2356 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002357 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002358 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002359 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2360 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002361 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002362 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002363 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002364 netif_dbg(efx, drv, efx->net_dev,
2365 "scheduling %s reset for %s\n",
2366 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002367 break;
2368 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002369
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002370 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002371 smp_mb(); /* ensure we change reset_pending before checking state */
2372
2373 /* If we're not READY then just leave the flags set as the cue
2374 * to abort probing or reschedule the reset later.
2375 */
2376 if (ACCESS_ONCE(efx->state) != STATE_READY)
2377 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002378
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002379 /* efx_process_channel() will no longer read events once a
2380 * reset is scheduled. So switch back to poll'd MCDI completions. */
2381 efx_mcdi_mode_poll(efx);
2382
Steve Hodgson1ab00622008-12-12 21:33:02 -08002383 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002384}
2385
2386/**************************************************************************
2387 *
2388 * List of NICs we support
2389 *
2390 **************************************************************************/
2391
2392/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002393static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002394 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2395 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002396 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002397 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2398 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002399 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002400 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002401 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002402 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002403 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002404 {0} /* end of list */
2405};
2406
2407/**************************************************************************
2408 *
Ben Hutchings37594332009-11-23 16:05:45 +00002409 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002410 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002411 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002412 * Needed so all function pointers are valid and do not have to be tested
2413 * before use
2414 *
2415 **************************************************************************/
2416int efx_port_dummy_op_int(struct efx_nic *efx)
2417{
2418 return 0;
2419}
2420void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002421
2422static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002423{
2424 return false;
2425}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002426
stephen hemminger6c8c2512011-04-14 05:50:12 +00002427static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002428 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002429 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002430 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002431 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002432};
2433
Ben Hutchings8ceee662008-04-27 12:55:59 +01002434/**************************************************************************
2435 *
2436 * Data housekeeping
2437 *
2438 **************************************************************************/
2439
2440/* This zeroes out and then fills in the invariants in a struct
2441 * efx_nic (including all sub-structures).
2442 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002443static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002444 struct pci_dev *pci_dev, struct net_device *net_dev)
2445{
Ben Hutchings46426102010-09-10 06:42:33 +00002446 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002447
2448 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002449 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002450#ifdef CONFIG_SFC_MTD
2451 INIT_LIST_HEAD(&efx->mtd_list);
2452#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002453 INIT_WORK(&efx->reset_work, efx_reset_work);
2454 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002455 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002456 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002457 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002458 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002459 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002460
2461 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002462 spin_lock_init(&efx->stats_lock);
2463 mutex_init(&efx->mac_lock);
2464 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002465 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002466 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002467 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002468
2469 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002470 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2471 if (!efx->channel[i])
2472 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002473 efx->msi_context[i].efx = efx;
2474 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002475 }
2476
Ben Hutchings8ceee662008-04-27 12:55:59 +01002477 /* Higher numbered interrupt modes are less capable! */
2478 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2479 interrupt_mode);
2480
Ben Hutchings6977dc62008-12-26 13:44:39 -08002481 /* Would be good to use the net_dev name, but we're too early */
2482 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2483 pci_name(pci_dev));
2484 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002485 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002486 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002487
Ben Hutchings8ceee662008-04-27 12:55:59 +01002488 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002489
2490fail:
2491 efx_fini_struct(efx);
2492 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002493}
2494
2495static void efx_fini_struct(struct efx_nic *efx)
2496{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002497 int i;
2498
2499 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2500 kfree(efx->channel[i]);
2501
Ben Hutchings8ceee662008-04-27 12:55:59 +01002502 if (efx->workqueue) {
2503 destroy_workqueue(efx->workqueue);
2504 efx->workqueue = NULL;
2505 }
2506}
2507
2508/**************************************************************************
2509 *
2510 * PCI interface
2511 *
2512 **************************************************************************/
2513
2514/* Main body of final NIC shutdown code
2515 * This is called only at module unload (or hotplug removal).
2516 */
2517static void efx_pci_remove_main(struct efx_nic *efx)
2518{
Ben Hutchings7153f622012-07-27 20:50:52 +01002519 /* Flush reset_work. It can no longer be scheduled since we
2520 * are not READY.
2521 */
2522 BUG_ON(efx->state == STATE_READY);
2523 cancel_work_sync(&efx->reset_work);
2524
Ben Hutchingsd8291182012-10-05 23:35:41 +01002525 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002526 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002527 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002528 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002529 efx_fini_napi(efx);
2530 efx_remove_all(efx);
2531}
2532
2533/* Final NIC shutdown
2534 * This is called only at module unload (or hotplug removal).
2535 */
2536static void efx_pci_remove(struct pci_dev *pci_dev)
2537{
2538 struct efx_nic *efx;
2539
2540 efx = pci_get_drvdata(pci_dev);
2541 if (!efx)
2542 return;
2543
2544 /* Mark the NIC as fini, then stop the interface */
2545 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002546 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002547 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002548 rtnl_unlock();
2549
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002550 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002551 efx_unregister_netdev(efx);
2552
Ben Hutchings7dde5962008-12-12 22:09:38 -08002553 efx_mtd_remove(efx);
2554
Ben Hutchings8ceee662008-04-27 12:55:59 +01002555 efx_pci_remove_main(efx);
2556
Ben Hutchings8ceee662008-04-27 12:55:59 +01002557 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002558 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002559
Ben Hutchings8ceee662008-04-27 12:55:59 +01002560 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002561 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002562 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002563
2564 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002565};
2566
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002567/* NIC VPD information
2568 * Called during probe to display the part number of the
2569 * installed NIC. VPD is potentially very large but this should
2570 * always appear within the first 512 bytes.
2571 */
2572#define SFC_VPD_LEN 512
2573static void efx_print_product_vpd(struct efx_nic *efx)
2574{
2575 struct pci_dev *dev = efx->pci_dev;
2576 char vpd_data[SFC_VPD_LEN];
2577 ssize_t vpd_size;
2578 int i, j;
2579
2580 /* Get the vpd data from the device */
2581 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2582 if (vpd_size <= 0) {
2583 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2584 return;
2585 }
2586
2587 /* Get the Read only section */
2588 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2589 if (i < 0) {
2590 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2591 return;
2592 }
2593
2594 j = pci_vpd_lrdt_size(&vpd_data[i]);
2595 i += PCI_VPD_LRDT_TAG_SIZE;
2596 if (i + j > vpd_size)
2597 j = vpd_size - i;
2598
2599 /* Get the Part number */
2600 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2601 if (i < 0) {
2602 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2603 return;
2604 }
2605
2606 j = pci_vpd_info_field_size(&vpd_data[i]);
2607 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2608 if (i + j > vpd_size) {
2609 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2610 return;
2611 }
2612
2613 netif_info(efx, drv, efx->net_dev,
2614 "Part Number : %.*s\n", j, &vpd_data[i]);
2615}
2616
2617
Ben Hutchings8ceee662008-04-27 12:55:59 +01002618/* Main body of NIC initialisation
2619 * This is called at module load (or hotplug insertion, theoretically).
2620 */
2621static int efx_pci_probe_main(struct efx_nic *efx)
2622{
2623 int rc;
2624
2625 /* Do start-of-day initialisation */
2626 rc = efx_probe_all(efx);
2627 if (rc)
2628 goto fail1;
2629
Ben Hutchingse8f14992010-12-07 19:47:34 +00002630 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002631
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002632 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002633 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002634 netif_err(efx, probe, efx->net_dev,
2635 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002636 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002637 }
2638
2639 rc = efx_init_port(efx);
2640 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002641 netif_err(efx, probe, efx->net_dev,
2642 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002643 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002644 }
2645
Ben Hutchings152b6a62009-11-29 03:43:56 +00002646 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002647 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002648 goto fail5;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002649 efx_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002650
2651 return 0;
2652
Ben Hutchings278c0622009-11-23 16:05:12 +00002653 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002654 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002655 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002656 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002657 fail3:
2658 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002659 efx_remove_all(efx);
2660 fail1:
2661 return rc;
2662}
2663
2664/* NIC initialisation
2665 *
2666 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002667 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002668 * sets up and registers the network devices with the kernel and hooks
2669 * the interrupt service routine. It does not prepare the device for
2670 * transmission; this is left to the first time one of the network
2671 * interfaces is brought up (i.e. efx_net_open).
2672 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002673static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002674 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002675{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002676 struct net_device *net_dev;
2677 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002678 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002679
2680 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002681 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2682 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002683 if (!net_dev)
2684 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002685 efx = netdev_priv(net_dev);
2686 efx->type = (const struct efx_nic_type *) entry->driver_data;
2687 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002688 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002689 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002690 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002691 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002692 /* Mask for features that also apply to VLAN devices */
2693 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002694 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2695 NETIF_F_RXCSUM);
2696 /* All offloads can be toggled */
2697 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002698 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002699 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002700 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002701 if (rc)
2702 goto fail1;
2703
Ben Hutchings62776d02010-06-23 11:30:07 +00002704 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002705 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002706
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002707 efx_print_product_vpd(efx);
2708
Ben Hutchings8ceee662008-04-27 12:55:59 +01002709 /* Set up basic I/O (BAR mappings etc) */
2710 rc = efx_init_io(efx);
2711 if (rc)
2712 goto fail2;
2713
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002714 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002715 if (rc)
2716 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002717
Ben Hutchings8ceee662008-04-27 12:55:59 +01002718 rc = efx_register_netdev(efx);
2719 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002720 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002721
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002722 rc = efx_sriov_init(efx);
2723 if (rc)
2724 netif_err(efx, probe, efx->net_dev,
2725 "SR-IOV can't be enabled rc %d\n", rc);
2726
Ben Hutchings62776d02010-06-23 11:30:07 +00002727 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002728
Ben Hutchings7c431612012-01-27 17:23:58 +00002729 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002730 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002731 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002732 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002733 if (rc)
2734 netif_warn(efx, probe, efx->net_dev,
2735 "failed to create MTDs (%d)\n", rc);
2736
Alexandre Rames626950d2013-01-14 17:20:22 +00002737 rc = pci_enable_pcie_error_reporting(pci_dev);
2738 if (rc && rc != -EINVAL)
2739 netif_warn(efx, probe, efx->net_dev,
2740 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2741
Ben Hutchings8ceee662008-04-27 12:55:59 +01002742 return 0;
2743
Ben Hutchings8ceee662008-04-27 12:55:59 +01002744 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002745 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002746 fail3:
2747 efx_fini_io(efx);
2748 fail2:
2749 efx_fini_struct(efx);
2750 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002751 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002752 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002753 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002754 free_netdev(net_dev);
2755 return rc;
2756}
2757
Ben Hutchings89c758f2009-11-29 03:43:07 +00002758static int efx_pm_freeze(struct device *dev)
2759{
2760 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2761
Ben Hutchings61da0262012-07-27 19:35:39 +01002762 rtnl_lock();
2763
Ben Hutchings6032fb52012-07-27 19:35:47 +01002764 if (efx->state != STATE_DISABLED) {
2765 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002766
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002767 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002768
Ben Hutchings6032fb52012-07-27 19:35:47 +01002769 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002770 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01002771 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002772
Ben Hutchings61da0262012-07-27 19:35:39 +01002773 rtnl_unlock();
2774
Ben Hutchings89c758f2009-11-29 03:43:07 +00002775 return 0;
2776}
2777
2778static int efx_pm_thaw(struct device *dev)
2779{
2780 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2781
Ben Hutchings61da0262012-07-27 19:35:39 +01002782 rtnl_lock();
2783
Ben Hutchings6032fb52012-07-27 19:35:47 +01002784 if (efx->state != STATE_DISABLED) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01002785 efx_enable_interrupts(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002786
Ben Hutchings6032fb52012-07-27 19:35:47 +01002787 mutex_lock(&efx->mac_lock);
2788 efx->phy_op->reconfigure(efx);
2789 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002790
Ben Hutchings6032fb52012-07-27 19:35:47 +01002791 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002792
Ben Hutchings6032fb52012-07-27 19:35:47 +01002793 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002794
Ben Hutchings6032fb52012-07-27 19:35:47 +01002795 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002796
Ben Hutchings6032fb52012-07-27 19:35:47 +01002797 efx->type->resume_wol(efx);
2798 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002799
Ben Hutchings61da0262012-07-27 19:35:39 +01002800 rtnl_unlock();
2801
Steve Hodgson319ba642010-06-01 11:17:24 +00002802 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2803 queue_work(reset_workqueue, &efx->reset_work);
2804
Ben Hutchings89c758f2009-11-29 03:43:07 +00002805 return 0;
2806}
2807
2808static int efx_pm_poweroff(struct device *dev)
2809{
2810 struct pci_dev *pci_dev = to_pci_dev(dev);
2811 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2812
2813 efx->type->fini(efx);
2814
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002815 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002816
2817 pci_save_state(pci_dev);
2818 return pci_set_power_state(pci_dev, PCI_D3hot);
2819}
2820
2821/* Used for both resume and restore */
2822static int efx_pm_resume(struct device *dev)
2823{
2824 struct pci_dev *pci_dev = to_pci_dev(dev);
2825 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2826 int rc;
2827
2828 rc = pci_set_power_state(pci_dev, PCI_D0);
2829 if (rc)
2830 return rc;
2831 pci_restore_state(pci_dev);
2832 rc = pci_enable_device(pci_dev);
2833 if (rc)
2834 return rc;
2835 pci_set_master(efx->pci_dev);
2836 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2837 if (rc)
2838 return rc;
2839 rc = efx->type->init(efx);
2840 if (rc)
2841 return rc;
2842 efx_pm_thaw(dev);
2843 return 0;
2844}
2845
2846static int efx_pm_suspend(struct device *dev)
2847{
2848 int rc;
2849
2850 efx_pm_freeze(dev);
2851 rc = efx_pm_poweroff(dev);
2852 if (rc)
2853 efx_pm_resume(dev);
2854 return rc;
2855}
2856
Ben Hutchings18e83e42012-01-05 19:05:20 +00002857static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002858 .suspend = efx_pm_suspend,
2859 .resume = efx_pm_resume,
2860 .freeze = efx_pm_freeze,
2861 .thaw = efx_pm_thaw,
2862 .poweroff = efx_pm_poweroff,
2863 .restore = efx_pm_resume,
2864};
2865
Alexandre Rames626950d2013-01-14 17:20:22 +00002866/* A PCI error affecting this device was detected.
2867 * At this point MMIO and DMA may be disabled.
2868 * Stop the software path and request a slot reset.
2869 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002870static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2871 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00002872{
2873 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2874 struct efx_nic *efx = pci_get_drvdata(pdev);
2875
2876 if (state == pci_channel_io_perm_failure)
2877 return PCI_ERS_RESULT_DISCONNECT;
2878
2879 rtnl_lock();
2880
2881 if (efx->state != STATE_DISABLED) {
2882 efx->state = STATE_RECOVERY;
2883 efx->reset_pending = 0;
2884
2885 efx_device_detach_sync(efx);
2886
2887 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002888 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00002889
2890 status = PCI_ERS_RESULT_NEED_RESET;
2891 } else {
2892 /* If the interface is disabled we don't want to do anything
2893 * with it.
2894 */
2895 status = PCI_ERS_RESULT_RECOVERED;
2896 }
2897
2898 rtnl_unlock();
2899
2900 pci_disable_device(pdev);
2901
2902 return status;
2903}
2904
2905/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002906static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00002907{
2908 struct efx_nic *efx = pci_get_drvdata(pdev);
2909 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2910 int rc;
2911
2912 if (pci_enable_device(pdev)) {
2913 netif_err(efx, hw, efx->net_dev,
2914 "Cannot re-enable PCI device after reset.\n");
2915 status = PCI_ERS_RESULT_DISCONNECT;
2916 }
2917
2918 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2919 if (rc) {
2920 netif_err(efx, hw, efx->net_dev,
2921 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2922 /* Non-fatal error. Continue. */
2923 }
2924
2925 return status;
2926}
2927
2928/* Perform the actual reset and resume I/O operations. */
2929static void efx_io_resume(struct pci_dev *pdev)
2930{
2931 struct efx_nic *efx = pci_get_drvdata(pdev);
2932 int rc;
2933
2934 rtnl_lock();
2935
2936 if (efx->state == STATE_DISABLED)
2937 goto out;
2938
2939 rc = efx_reset(efx, RESET_TYPE_ALL);
2940 if (rc) {
2941 netif_err(efx, hw, efx->net_dev,
2942 "efx_reset failed after PCI error (%d)\n", rc);
2943 } else {
2944 efx->state = STATE_READY;
2945 netif_dbg(efx, hw, efx->net_dev,
2946 "Done resetting and resuming IO after PCI error.\n");
2947 }
2948
2949out:
2950 rtnl_unlock();
2951}
2952
2953/* For simplicity and reliability, we always require a slot reset and try to
2954 * reset the hardware when a pci error affecting the device is detected.
2955 * We leave both the link_reset and mmio_enabled callback unimplemented:
2956 * with our request for slot reset the mmio_enabled callback will never be
2957 * called, and the link_reset callback is not used by AER or EEH mechanisms.
2958 */
2959static struct pci_error_handlers efx_err_handlers = {
2960 .error_detected = efx_io_error_detected,
2961 .slot_reset = efx_io_slot_reset,
2962 .resume = efx_io_resume,
2963};
2964
Ben Hutchings8ceee662008-04-27 12:55:59 +01002965static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00002966 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002967 .id_table = efx_pci_table,
2968 .probe = efx_pci_probe,
2969 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00002970 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00002971 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002972};
2973
2974/**************************************************************************
2975 *
2976 * Kernel module interface
2977 *
2978 *************************************************************************/
2979
2980module_param(interrupt_mode, uint, 0444);
2981MODULE_PARM_DESC(interrupt_mode,
2982 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2983
2984static int __init efx_init_module(void)
2985{
2986 int rc;
2987
2988 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2989
2990 rc = register_netdevice_notifier(&efx_netdev_notifier);
2991 if (rc)
2992 goto err_notifier;
2993
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002994 rc = efx_init_sriov();
2995 if (rc)
2996 goto err_sriov;
2997
Steve Hodgson1ab00622008-12-12 21:33:02 -08002998 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2999 if (!reset_workqueue) {
3000 rc = -ENOMEM;
3001 goto err_reset;
3002 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003003
3004 rc = pci_register_driver(&efx_pci_driver);
3005 if (rc < 0)
3006 goto err_pci;
3007
3008 return 0;
3009
3010 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003011 destroy_workqueue(reset_workqueue);
3012 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003013 efx_fini_sriov();
3014 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003015 unregister_netdevice_notifier(&efx_netdev_notifier);
3016 err_notifier:
3017 return rc;
3018}
3019
3020static void __exit efx_exit_module(void)
3021{
3022 printk(KERN_INFO "Solarflare NET driver unloading\n");
3023
3024 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003025 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003026 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003027 unregister_netdevice_notifier(&efx_netdev_notifier);
3028
3029}
3030
3031module_init(efx_init_module);
3032module_exit(efx_exit_module);
3033
Ben Hutchings906bb262009-11-29 15:16:19 +00003034MODULE_AUTHOR("Solarflare Communications and "
3035 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003036MODULE_DESCRIPTION("Solarflare Communications network driver");
3037MODULE_LICENSE("GPL");
3038MODULE_DEVICE_TABLE(pci, efx_pci_table);