blob: efad5f73e1ce398be6d4e7232e53188d78902e60 [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",
Alexandre Rames3de82b92013-06-13 11:36:15 +010083 [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
Alexandre Rames626950d2013-01-14 17:20:22 +000084 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
85 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Ben Hutchingsc4593022009-11-23 16:08:17 +000086};
87
Steve Hodgson1ab00622008-12-12 21:33:02 -080088/* Reset workqueue. If any NIC has a hardware failure then a reset will be
89 * queued onto this work queue. This is not a per-nic work queue, because
90 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
91 */
92static struct workqueue_struct *reset_workqueue;
93
Ben Hutchings8ceee662008-04-27 12:55:59 +010094/**************************************************************************
95 *
96 * Configurable values
97 *
98 *************************************************************************/
99
100/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100101 * Use separate channels for TX and RX events
102 *
Neil Turton28b581a2008-12-12 21:41:06 -0800103 * Set this to 1 to use separate channels for TX and RX. It allows us
104 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100105 *
Neil Turton28b581a2008-12-12 21:41:06 -0800106 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100107 */
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000108static bool separate_tx_channels;
109module_param(separate_tx_channels, bool, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800110MODULE_PARM_DESC(separate_tx_channels,
111 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100112
113/* This is the weight assigned to each of the (per-channel) virtual
114 * NAPI devices.
115 */
116static int napi_weight = 64;
117
118/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000119 * monitor.
120 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000121 * - Check the on-board hardware monitor;
122 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000123 * On Siena-based NICs for power systems with EEH support, this will give EEH a
124 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100125 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000126static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100127
Ben Hutchings8ceee662008-04-27 12:55:59 +0100128/* Initial interrupt moderation settings. They can be modified after
129 * module load with ethtool.
130 *
131 * The default for RX should strike a balance between increasing the
132 * round-trip latency and reducing overhead.
133 */
134static unsigned int rx_irq_mod_usec = 60;
135
136/* Initial interrupt moderation settings. They can be modified after
137 * module load with ethtool.
138 *
139 * This default is chosen to ensure that a 10G link does not go idle
140 * while a TX queue is stopped after it has become full. A queue is
141 * restarted when it drops below half full. The time this takes (assuming
142 * worst case 3 descriptors per packet and 1024 descriptors) is
143 * 512 / 3 * 1.2 = 205 usec.
144 */
145static unsigned int tx_irq_mod_usec = 150;
146
147/* This is the first interrupt mode to try out of:
148 * 0 => MSI-X
149 * 1 => MSI
150 * 2 => legacy
151 */
152static unsigned int interrupt_mode;
153
154/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
155 * i.e. the number of CPUs among which we may distribute simultaneous
156 * interrupt handling.
157 *
158 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000159 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100160 */
161static unsigned int rss_cpus;
162module_param(rss_cpus, uint, 0444);
163MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
164
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000165static bool phy_flash_cfg;
166module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800167MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
168
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000169static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000170module_param(irq_adapt_low_thresh, uint, 0644);
171MODULE_PARM_DESC(irq_adapt_low_thresh,
172 "Threshold score for reducing IRQ moderation");
173
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000174static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000175module_param(irq_adapt_high_thresh, uint, 0644);
176MODULE_PARM_DESC(irq_adapt_high_thresh,
177 "Threshold score for increasing IRQ moderation");
178
Ben Hutchings62776d02010-06-23 11:30:07 +0000179static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
180 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
181 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
182 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
183module_param(debug, uint, 0);
184MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
185
Ben Hutchings8ceee662008-04-27 12:55:59 +0100186/**************************************************************************
187 *
188 * Utility functions and prototypes
189 *
190 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000191
Ben Hutchingsd8291182012-10-05 23:35:41 +0100192static void efx_soft_enable_interrupts(struct efx_nic *efx);
193static void efx_soft_disable_interrupts(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000194static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000195static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000196static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100197static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000198static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100199static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000200static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000201static void efx_fini_struct(struct efx_nic *efx);
202static void efx_start_all(struct efx_nic *efx);
203static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100204
205#define EFX_ASSERT_RESET_SERIALISED(efx) \
206 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100207 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000208 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000209 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100210 ASSERT_RTNL(); \
211 } while (0)
212
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100213static int efx_check_disabled(struct efx_nic *efx)
214{
Alexandre Rames626950d2013-01-14 17:20:22 +0000215 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100216 netif_err(efx, drv, efx->net_dev,
217 "device is disabled due to earlier errors\n");
218 return -EIO;
219 }
220 return 0;
221}
222
Ben Hutchings8ceee662008-04-27 12:55:59 +0100223/**************************************************************************
224 *
225 * Event queue processing
226 *
227 *************************************************************************/
228
229/* Process channel's event queue
230 *
231 * This function is responsible for processing the event queue of a
232 * single channel. The caller must guarantee that this function will
233 * never be concurrently called more than once on the same channel,
234 * though different channels may be being processed concurrently.
235 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000236static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100237{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000238 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100239
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000240 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100241 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100242
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000243 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000244 if (spent && efx_channel_has_rx_queue(channel)) {
245 struct efx_rx_queue *rx_queue =
246 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100247
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000248 efx_rx_flush_packet(channel);
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100249 efx_fast_push_rx_descriptors(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100250 }
251
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000252 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100253}
254
Ben Hutchings8ceee662008-04-27 12:55:59 +0100255/* NAPI poll handler
256 *
257 * NAPI guarantees serialisation of polls of the same device, which
258 * provides the guarantee required by efx_process_channel().
259 */
260static int efx_poll(struct napi_struct *napi, int budget)
261{
262 struct efx_channel *channel =
263 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000264 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000265 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100266
Ben Hutchings62776d02010-06-23 11:30:07 +0000267 netif_vdbg(efx, intr, efx->net_dev,
268 "channel %d NAPI poll executing on CPU %d\n",
269 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100270
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000271 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100272
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000273 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000274 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000275 efx->irq_rx_adaptive &&
276 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000277 if (unlikely(channel->irq_mod_score <
278 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000279 if (channel->irq_moderation > 1) {
280 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000281 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000282 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000283 } else if (unlikely(channel->irq_mod_score >
284 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000285 if (channel->irq_moderation <
286 efx->irq_rx_moderation) {
287 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000288 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000289 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000290 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000291 channel->irq_count = 0;
292 channel->irq_mod_score = 0;
293 }
294
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000295 efx_filter_rfs_expire(channel);
296
Ben Hutchings8ceee662008-04-27 12:55:59 +0100297 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800298 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100299 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100300 * interrupts have already been disabled.
301 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800302 napi_complete(napi);
Ben Hutchings514bedb2012-10-05 19:30:16 +0100303 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100304 }
305
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000306 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100307}
308
Ben Hutchings8ceee662008-04-27 12:55:59 +0100309/* Create event queue
310 * Event queue memory allocations are done only once. If the channel
311 * is reset, the memory buffer will be reused; this guards against
312 * errors during channel reset and also simplifies interrupt handling.
313 */
314static int efx_probe_eventq(struct efx_channel *channel)
315{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000316 struct efx_nic *efx = channel->efx;
317 unsigned long entries;
318
Ben Hutchings86ee5302012-01-09 19:51:22 +0000319 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000320 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100321
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000322 /* Build an event queue with room for one event per tx and rx buffer,
323 * plus some extra for link state events and MCDI completions. */
324 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
325 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
326 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
327
Ben Hutchings152b6a62009-11-29 03:43:56 +0000328 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100329}
330
331/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100332static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100333{
Ben Hutchings62776d02010-06-23 11:30:07 +0000334 netif_dbg(channel->efx, drv, channel->efx->net_dev,
335 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100336
337 channel->eventq_read_ptr = 0;
338
Ben Hutchings152b6a62009-11-29 03:43:56 +0000339 efx_nic_init_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100340 channel->eventq_init = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100341}
342
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000343/* Enable event queue processing and NAPI */
344static void efx_start_eventq(struct efx_channel *channel)
345{
346 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
347 "chan %d start event queue\n", channel->channel);
348
Ben Hutchings514bedb2012-10-05 19:30:16 +0100349 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000350 channel->enabled = true;
351 smp_wmb();
352
353 napi_enable(&channel->napi_str);
354 efx_nic_eventq_read_ack(channel);
355}
356
357/* Disable event queue processing and NAPI */
358static void efx_stop_eventq(struct efx_channel *channel)
359{
360 if (!channel->enabled)
361 return;
362
363 napi_disable(&channel->napi_str);
364 channel->enabled = false;
365}
366
Ben Hutchings8ceee662008-04-27 12:55:59 +0100367static void efx_fini_eventq(struct efx_channel *channel)
368{
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100369 if (!channel->eventq_init)
370 return;
371
Ben Hutchings62776d02010-06-23 11:30:07 +0000372 netif_dbg(channel->efx, drv, channel->efx->net_dev,
373 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100374
Ben Hutchings152b6a62009-11-29 03:43:56 +0000375 efx_nic_fini_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100376 channel->eventq_init = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100377}
378
379static void efx_remove_eventq(struct efx_channel *channel)
380{
Ben Hutchings62776d02010-06-23 11:30:07 +0000381 netif_dbg(channel->efx, drv, channel->efx->net_dev,
382 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100383
Ben Hutchings152b6a62009-11-29 03:43:56 +0000384 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100385}
386
387/**************************************************************************
388 *
389 * Channel handling
390 *
391 *************************************************************************/
392
Ben Hutchings7f967c02012-02-13 23:45:02 +0000393/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000394static struct efx_channel *
395efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
396{
397 struct efx_channel *channel;
398 struct efx_rx_queue *rx_queue;
399 struct efx_tx_queue *tx_queue;
400 int j;
401
Ben Hutchings7f967c02012-02-13 23:45:02 +0000402 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
403 if (!channel)
404 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000405
Ben Hutchings7f967c02012-02-13 23:45:02 +0000406 channel->efx = efx;
407 channel->channel = i;
408 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000409
Ben Hutchings7f967c02012-02-13 23:45:02 +0000410 for (j = 0; j < EFX_TXQ_TYPES; j++) {
411 tx_queue = &channel->tx_queue[j];
412 tx_queue->efx = efx;
413 tx_queue->queue = i * EFX_TXQ_TYPES + j;
414 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000415 }
416
Ben Hutchings46426102010-09-10 06:42:33 +0000417 rx_queue = &channel->rx_queue;
418 rx_queue->efx = efx;
419 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
420 (unsigned long)rx_queue);
421
422 return channel;
423}
424
Ben Hutchings7f967c02012-02-13 23:45:02 +0000425/* Allocate and initialise a channel structure, copying parameters
426 * (but not resources) from an old channel structure.
427 */
428static struct efx_channel *
429efx_copy_channel(const struct efx_channel *old_channel)
430{
431 struct efx_channel *channel;
432 struct efx_rx_queue *rx_queue;
433 struct efx_tx_queue *tx_queue;
434 int j;
435
436 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
437 if (!channel)
438 return NULL;
439
440 *channel = *old_channel;
441
442 channel->napi_dev = NULL;
443 memset(&channel->eventq, 0, sizeof(channel->eventq));
444
445 for (j = 0; j < EFX_TXQ_TYPES; j++) {
446 tx_queue = &channel->tx_queue[j];
447 if (tx_queue->channel)
448 tx_queue->channel = channel;
449 tx_queue->buffer = NULL;
450 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
451 }
452
453 rx_queue = &channel->rx_queue;
454 rx_queue->buffer = NULL;
455 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
456 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
457 (unsigned long)rx_queue);
458
459 return channel;
460}
461
Ben Hutchings8ceee662008-04-27 12:55:59 +0100462static int efx_probe_channel(struct efx_channel *channel)
463{
464 struct efx_tx_queue *tx_queue;
465 struct efx_rx_queue *rx_queue;
466 int rc;
467
Ben Hutchings62776d02010-06-23 11:30:07 +0000468 netif_dbg(channel->efx, probe, channel->efx->net_dev,
469 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100470
Ben Hutchings7f967c02012-02-13 23:45:02 +0000471 rc = channel->type->pre_probe(channel);
472 if (rc)
473 goto fail;
474
Ben Hutchings8ceee662008-04-27 12:55:59 +0100475 rc = efx_probe_eventq(channel);
476 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000477 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100478
479 efx_for_each_channel_tx_queue(tx_queue, channel) {
480 rc = efx_probe_tx_queue(tx_queue);
481 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000482 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100483 }
484
485 efx_for_each_channel_rx_queue(rx_queue, channel) {
486 rc = efx_probe_rx_queue(rx_queue);
487 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000488 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100489 }
490
491 channel->n_rx_frm_trunc = 0;
492
493 return 0;
494
Ben Hutchings7f967c02012-02-13 23:45:02 +0000495fail:
496 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100497 return rc;
498}
499
Ben Hutchings7f967c02012-02-13 23:45:02 +0000500static void
501efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
502{
503 struct efx_nic *efx = channel->efx;
504 const char *type;
505 int number;
506
507 number = channel->channel;
508 if (efx->tx_channel_offset == 0) {
509 type = "";
510 } else if (channel->channel < efx->tx_channel_offset) {
511 type = "-rx";
512 } else {
513 type = "-tx";
514 number -= efx->tx_channel_offset;
515 }
516 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
517}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100518
Ben Hutchings56536e92008-12-12 21:37:02 -0800519static void efx_set_channel_names(struct efx_nic *efx)
520{
521 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800522
Ben Hutchings7f967c02012-02-13 23:45:02 +0000523 efx_for_each_channel(channel, efx)
524 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100525 efx->msi_context[channel->channel].name,
526 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800527}
528
Ben Hutchings46426102010-09-10 06:42:33 +0000529static int efx_probe_channels(struct efx_nic *efx)
530{
531 struct efx_channel *channel;
532 int rc;
533
534 /* Restart special buffer allocation */
535 efx->next_buffer_table = 0;
536
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000537 /* Probe channels in reverse, so that any 'extra' channels
538 * use the start of the buffer table. This allows the traffic
539 * channels to be resized without moving them or wasting the
540 * entries before them.
541 */
542 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000543 rc = efx_probe_channel(channel);
544 if (rc) {
545 netif_err(efx, probe, efx->net_dev,
546 "failed to create channel %d\n",
547 channel->channel);
548 goto fail;
549 }
550 }
551 efx_set_channel_names(efx);
552
553 return 0;
554
555fail:
556 efx_remove_channels(efx);
557 return rc;
558}
559
Ben Hutchings8ceee662008-04-27 12:55:59 +0100560/* Channels are shutdown and reinitialised whilst the NIC is running
561 * to propagate configuration changes (mtu, checksum offload), or
562 * to clear hardware error conditions
563 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000564static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100565{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000566 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100567 struct efx_tx_queue *tx_queue;
568 struct efx_rx_queue *rx_queue;
569 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000570 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100571
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100572 /* Calculate the rx buffer allocation parameters required to
573 * support the current MTU, including padding for header
574 * alignment and overruns.
575 */
Ben Hutchings272baee2013-01-29 23:33:14 +0000576 efx->rx_dma_len = (efx->type->rx_buffer_hash_size +
577 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
578 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000579 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Ben Hutchingsc14ff2e2013-05-13 11:58:31 +0000580 NET_IP_ALIGN + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000581 if (rx_buf_len <= PAGE_SIZE) {
582 efx->rx_scatter = false;
583 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000584 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000585 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000586 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000587 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
588 EFX_RX_BUF_ALIGNMENT) >
589 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000590 efx->rx_scatter = true;
591 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
592 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000593 } else {
594 efx->rx_scatter = false;
595 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000596 }
597
Daniel Pieczko1648a232013-02-13 10:54:41 +0000598 efx_rx_config_page_split(efx);
599 if (efx->rx_buffer_order)
600 netif_dbg(efx, drv, efx->net_dev,
601 "RX buf len=%u; page order=%u batch=%u\n",
602 efx->rx_dma_len, efx->rx_buffer_order,
603 efx->rx_pages_per_batch);
604 else
605 netif_dbg(efx, drv, efx->net_dev,
606 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
607 efx->rx_dma_len, efx->rx_page_buf_step,
608 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000609
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000610 /* RX filters also have scatter-enabled flags */
611 if (efx->rx_scatter != old_rx_scatter)
Ben Hutchingsadd72472012-11-08 01:46:53 +0000612 efx->type->filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100613
Ben Hutchings14bf7182012-05-22 01:27:58 +0100614 /* We must keep at least one descriptor in a TX ring empty.
615 * We could avoid this when the queue size does not exactly
616 * match the hardware ring size, but it's not that important.
617 * Therefore we stop the queue when one more skb might fill
618 * the ring completely. We wake it when half way back to
619 * empty.
620 */
621 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
622 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
623
Ben Hutchings8ceee662008-04-27 12:55:59 +0100624 /* Initialise the channels */
625 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100626 efx_for_each_channel_tx_queue(tx_queue, channel)
627 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100628
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000629 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100630 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000631 efx_nic_generate_fill_event(rx_queue);
632 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100633
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000634 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100635 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000636
637 if (netif_device_present(efx->net_dev))
638 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100639}
640
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000641static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100642{
643 struct efx_channel *channel;
644 struct efx_tx_queue *tx_queue;
645 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100646 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100647
648 EFX_ASSERT_RESET_SERIALISED(efx);
649 BUG_ON(efx->port_enabled);
650
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100651 /* Stop RX refill */
652 efx_for_each_channel(channel, efx) {
653 efx_for_each_channel_rx_queue(rx_queue, channel)
654 rx_queue->refill_enabled = false;
655 }
656
Ben Hutchings8ceee662008-04-27 12:55:59 +0100657 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000658 /* RX packet processing is pipelined, so wait for the
659 * NAPI handler to complete. At least event queue 0
660 * might be kept active by non-data events, so don't
661 * use napi_synchronize() but actually disable NAPI
662 * temporarily.
663 */
664 if (efx_channel_has_rx_queue(channel)) {
665 efx_stop_eventq(channel);
666 efx_start_eventq(channel);
667 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100668 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100669
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100670 rc = efx->type->fini_dmaq(efx);
671 if (rc && EFX_WORKAROUND_7803(efx)) {
672 /* Schedule a reset to recover from the flush failure. The
673 * descriptor caches reference memory we're about to free,
674 * but falcon_reconfigure_mac_wrapper() won't reconnect
675 * the MACs because of the pending reset.
676 */
677 netif_err(efx, drv, efx->net_dev,
678 "Resetting to recover from flush failure\n");
679 efx_schedule_reset(efx, RESET_TYPE_ALL);
680 } else if (rc) {
681 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
682 } else {
683 netif_dbg(efx, drv, efx->net_dev,
684 "successfully flushed all queues\n");
685 }
686
687 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100688 efx_for_each_channel_rx_queue(rx_queue, channel)
689 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000690 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100691 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100692 }
693}
694
695static void efx_remove_channel(struct efx_channel *channel)
696{
697 struct efx_tx_queue *tx_queue;
698 struct efx_rx_queue *rx_queue;
699
Ben Hutchings62776d02010-06-23 11:30:07 +0000700 netif_dbg(channel->efx, drv, channel->efx->net_dev,
701 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100702
703 efx_for_each_channel_rx_queue(rx_queue, channel)
704 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000705 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100706 efx_remove_tx_queue(tx_queue);
707 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100708 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100709}
710
Ben Hutchings46426102010-09-10 06:42:33 +0000711static void efx_remove_channels(struct efx_nic *efx)
712{
713 struct efx_channel *channel;
714
715 efx_for_each_channel(channel, efx)
716 efx_remove_channel(channel);
717}
718
719int
720efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
721{
722 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
723 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000724 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100725 int rc;
726
727 rc = efx_check_disabled(efx);
728 if (rc)
729 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000730
731 /* Not all channels should be reallocated. We must avoid
732 * reallocating their buffer table entries.
733 */
734 efx_for_each_channel(channel, efx) {
735 struct efx_rx_queue *rx_queue;
736 struct efx_tx_queue *tx_queue;
737
738 if (channel->type->copy)
739 continue;
740 next_buffer_table = max(next_buffer_table,
741 channel->eventq.index +
742 channel->eventq.entries);
743 efx_for_each_channel_rx_queue(rx_queue, channel)
744 next_buffer_table = max(next_buffer_table,
745 rx_queue->rxd.index +
746 rx_queue->rxd.entries);
747 efx_for_each_channel_tx_queue(tx_queue, channel)
748 next_buffer_table = max(next_buffer_table,
749 tx_queue->txd.index +
750 tx_queue->txd.entries);
751 }
Ben Hutchings46426102010-09-10 06:42:33 +0000752
Ben Hutchings29c69a42013-01-28 19:01:06 +0000753 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000754 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100755 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000756
Ben Hutchings7f967c02012-02-13 23:45:02 +0000757 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000758 memset(other_channel, 0, sizeof(other_channel));
759 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000760 channel = efx->channel[i];
761 if (channel->type->copy)
762 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000763 if (!channel) {
764 rc = -ENOMEM;
765 goto out;
766 }
767 other_channel[i] = channel;
768 }
769
770 /* Swap entry counts and channel pointers */
771 old_rxq_entries = efx->rxq_entries;
772 old_txq_entries = efx->txq_entries;
773 efx->rxq_entries = rxq_entries;
774 efx->txq_entries = txq_entries;
775 for (i = 0; i < efx->n_channels; i++) {
776 channel = efx->channel[i];
777 efx->channel[i] = other_channel[i];
778 other_channel[i] = channel;
779 }
780
Ben Hutchings7f967c02012-02-13 23:45:02 +0000781 /* Restart buffer table allocation */
782 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000783
Ben Hutchingse8f14992010-12-07 19:47:34 +0000784 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000785 channel = efx->channel[i];
786 if (!channel->type->copy)
787 continue;
788 rc = efx_probe_channel(channel);
789 if (rc)
790 goto rollback;
791 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000792 }
Ben Hutchings46426102010-09-10 06:42:33 +0000793
Ben Hutchings7f967c02012-02-13 23:45:02 +0000794out:
795 /* Destroy unused channel structures */
796 for (i = 0; i < efx->n_channels; i++) {
797 channel = other_channel[i];
798 if (channel && channel->type->copy) {
799 efx_fini_napi_channel(channel);
800 efx_remove_channel(channel);
801 kfree(channel);
802 }
803 }
804
Ben Hutchingsd8291182012-10-05 23:35:41 +0100805 efx_soft_enable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000806 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000807 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000808 return rc;
809
810rollback:
811 /* Swap back */
812 efx->rxq_entries = old_rxq_entries;
813 efx->txq_entries = old_txq_entries;
814 for (i = 0; i < efx->n_channels; i++) {
815 channel = efx->channel[i];
816 efx->channel[i] = other_channel[i];
817 other_channel[i] = channel;
818 }
819 goto out;
820}
821
Steve Hodgson90d683a2010-06-01 11:19:39 +0000822void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100823{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000824 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100825}
826
Ben Hutchings7f967c02012-02-13 23:45:02 +0000827static const struct efx_channel_type efx_default_channel_type = {
828 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100829 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000830 .get_name = efx_get_channel_name,
831 .copy = efx_copy_channel,
832 .keep_eventq = false,
833};
834
835int efx_channel_dummy_op_int(struct efx_channel *channel)
836{
837 return 0;
838}
839
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100840void efx_channel_dummy_op_void(struct efx_channel *channel)
841{
842}
843
Ben Hutchings8ceee662008-04-27 12:55:59 +0100844/**************************************************************************
845 *
846 * Port handling
847 *
848 **************************************************************************/
849
850/* This ensures that the kernel is kept informed (via
851 * netif_carrier_on/off) of the link status, and also maintains the
852 * link status's stop on the port's TX queue.
853 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000854void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100855{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000856 struct efx_link_state *link_state = &efx->link_state;
857
Ben Hutchings8ceee662008-04-27 12:55:59 +0100858 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
859 * that no events are triggered between unregister_netdev() and the
860 * driver unloading. A more general condition is that NETDEV_CHANGE
861 * can only be generated between NETDEV_UP and NETDEV_DOWN */
862 if (!netif_running(efx->net_dev))
863 return;
864
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000865 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100866 efx->n_link_state_changes++;
867
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000868 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100869 netif_carrier_on(efx->net_dev);
870 else
871 netif_carrier_off(efx->net_dev);
872 }
873
874 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000875 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000876 netif_info(efx, link, efx->net_dev,
Ben Hutchings964e6132012-11-19 23:08:22 +0000877 "link up at %uMbps %s-duplex (MTU %d)\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000878 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings964e6132012-11-19 23:08:22 +0000879 efx->net_dev->mtu);
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000880 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000881 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100882}
883
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000884void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
885{
886 efx->link_advertising = advertising;
887 if (advertising) {
888 if (advertising & ADVERTISED_Pause)
889 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
890 else
891 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
892 if (advertising & ADVERTISED_Asym_Pause)
893 efx->wanted_fc ^= EFX_FC_TX;
894 }
895}
896
David S. Millerb56269462011-05-17 17:53:22 -0400897void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000898{
899 efx->wanted_fc = wanted_fc;
900 if (efx->link_advertising) {
901 if (wanted_fc & EFX_FC_RX)
902 efx->link_advertising |= (ADVERTISED_Pause |
903 ADVERTISED_Asym_Pause);
904 else
905 efx->link_advertising &= ~(ADVERTISED_Pause |
906 ADVERTISED_Asym_Pause);
907 if (wanted_fc & EFX_FC_TX)
908 efx->link_advertising ^= ADVERTISED_Asym_Pause;
909 }
910}
911
Ben Hutchings115122a2009-03-04 09:52:52 +0000912static void efx_fini_port(struct efx_nic *efx);
913
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000914/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
915 * the MAC appropriately. All other PHY configuration changes are pushed
916 * through phy_op->set_settings(), and pushed asynchronously to the MAC
917 * through efx_monitor().
918 *
919 * Callers must hold the mac_lock
920 */
921int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100922{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000923 enum efx_phy_mode phy_mode;
924 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100925
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000926 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100927
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000928 /* Disable PHY transmit in mac level loopbacks */
929 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800930 if (LOOPBACK_INTERNAL(efx))
931 efx->phy_mode |= PHY_MODE_TX_DISABLED;
932 else
933 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800934
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000935 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800936
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000937 if (rc)
938 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100939
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000940 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100941}
942
943/* Reinitialise the MAC to pick up new PHY settings, even if the port is
944 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000945int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100946{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000947 int rc;
948
Ben Hutchings8ceee662008-04-27 12:55:59 +0100949 EFX_ASSERT_RESET_SERIALISED(efx);
950
951 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000952 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100953 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000954
955 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100956}
957
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000958/* Asynchronous work item for changing MAC promiscuity and multicast
959 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
960 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800961static void efx_mac_work(struct work_struct *data)
962{
963 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
964
965 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100966 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100967 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800968 mutex_unlock(&efx->mac_lock);
969}
970
Ben Hutchings8ceee662008-04-27 12:55:59 +0100971static int efx_probe_port(struct efx_nic *efx)
972{
973 int rc;
974
Ben Hutchings62776d02010-06-23 11:30:07 +0000975 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100976
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000977 if (phy_flash_cfg)
978 efx->phy_mode = PHY_MODE_SPECIAL;
979
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000980 /* Connect up MAC/PHY operations table */
981 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100982 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000983 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100984
Ben Hutchingse332bcb2011-12-20 01:22:51 +0000985 /* Initialise MAC address to permanent address */
986 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100987
988 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100989}
990
991static int efx_init_port(struct efx_nic *efx)
992{
993 int rc;
994
Ben Hutchings62776d02010-06-23 11:30:07 +0000995 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100996
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000997 mutex_lock(&efx->mac_lock);
998
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800999 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001000 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001001 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001002
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001003 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001004
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001005 /* Reconfigure the MAC before creating dma queues (required for
1006 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001007 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001008
1009 /* Ensure the PHY advertises the correct flow control settings */
1010 rc = efx->phy_op->reconfigure(efx);
1011 if (rc)
1012 goto fail2;
1013
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001014 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001015 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001016
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001017fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001018 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001019fail1:
1020 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001021 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001022}
1023
Ben Hutchings8ceee662008-04-27 12:55:59 +01001024static void efx_start_port(struct efx_nic *efx)
1025{
Ben Hutchings62776d02010-06-23 11:30:07 +00001026 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001027 BUG_ON(efx->port_enabled);
1028
1029 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001030 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001031
1032 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1033 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001034 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001035
Ben Hutchings8ceee662008-04-27 12:55:59 +01001036 mutex_unlock(&efx->mac_lock);
1037}
1038
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001039/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001040static void efx_stop_port(struct efx_nic *efx)
1041{
Ben Hutchings62776d02010-06-23 11:30:07 +00001042 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001043
1044 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001045 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001046 mutex_unlock(&efx->mac_lock);
1047
1048 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001049 netif_addr_lock_bh(efx->net_dev);
1050 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001051}
1052
1053static void efx_fini_port(struct efx_nic *efx)
1054{
Ben Hutchings62776d02010-06-23 11:30:07 +00001055 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001056
1057 if (!efx->port_initialized)
1058 return;
1059
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001060 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001061 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001062
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001063 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001064 efx_link_status_changed(efx);
1065}
1066
1067static void efx_remove_port(struct efx_nic *efx)
1068{
Ben Hutchings62776d02010-06-23 11:30:07 +00001069 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001070
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001071 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001072}
1073
1074/**************************************************************************
1075 *
1076 * NIC handling
1077 *
1078 **************************************************************************/
1079
1080/* This configures the PCI device to enable I/O and DMA. */
1081static int efx_init_io(struct efx_nic *efx)
1082{
1083 struct pci_dev *pci_dev = efx->pci_dev;
1084 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001085 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001086 int rc;
1087
Ben Hutchings62776d02010-06-23 11:30:07 +00001088 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001089
1090 rc = pci_enable_device(pci_dev);
1091 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001092 netif_err(efx, probe, efx->net_dev,
1093 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001094 goto fail1;
1095 }
1096
1097 pci_set_master(pci_dev);
1098
1099 /* Set the PCI DMA mask. Try all possibilities from our
1100 * genuine mask down to 32 bits, because some architectures
1101 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1102 * masks event though they reject 46 bit masks.
1103 */
1104 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001105 if (dma_supported(&pci_dev->dev, dma_mask)) {
1106 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001107 if (rc == 0)
1108 break;
1109 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001110 dma_mask >>= 1;
1111 }
1112 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001113 netif_err(efx, probe, efx->net_dev,
1114 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001115 goto fail2;
1116 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001117 netif_dbg(efx, probe, efx->net_dev,
1118 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001119 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001120 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001121 /* dma_set_coherent_mask() is not *allowed* to
1122 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001123 * but just in case...
1124 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001125 netif_err(efx, probe, efx->net_dev,
1126 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001127 goto fail2;
1128 }
1129
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001130 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1131 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001132 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001133 netif_err(efx, probe, efx->net_dev,
1134 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001135 rc = -EIO;
1136 goto fail3;
1137 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001138 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001139 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001140 netif_err(efx, probe, efx->net_dev,
1141 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001142 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001143 rc = -ENOMEM;
1144 goto fail4;
1145 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001146 netif_dbg(efx, probe, efx->net_dev,
1147 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001148 (unsigned long long)efx->membase_phys, mem_map_size,
1149 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001150
1151 return 0;
1152
1153 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001154 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001155 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001156 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001157 fail2:
1158 pci_disable_device(efx->pci_dev);
1159 fail1:
1160 return rc;
1161}
1162
1163static void efx_fini_io(struct efx_nic *efx)
1164{
Ben Hutchings62776d02010-06-23 11:30:07 +00001165 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001166
1167 if (efx->membase) {
1168 iounmap(efx->membase);
1169 efx->membase = NULL;
1170 }
1171
1172 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001173 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001174 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001175 }
1176
1177 pci_disable_device(efx->pci_dev);
1178}
1179
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001180static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001181{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001182 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001183 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001184 int cpu;
1185
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001186 if (rss_cpus) {
1187 count = rss_cpus;
1188 } else {
1189 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1190 netif_warn(efx, probe, efx->net_dev,
1191 "RSS disabled due to allocation failure\n");
1192 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001193 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001194
1195 count = 0;
1196 for_each_online_cpu(cpu) {
1197 if (!cpumask_test_cpu(cpu, thread_mask)) {
1198 ++count;
1199 cpumask_or(thread_mask, thread_mask,
1200 topology_thread_cpumask(cpu));
1201 }
1202 }
1203
1204 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001205 }
1206
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001207 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1208 * table entries that are inaccessible to VFs
1209 */
1210 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1211 count > efx_vf_size(efx)) {
1212 netif_warn(efx, probe, efx->net_dev,
1213 "Reducing number of RSS channels from %u to %u for "
1214 "VF support. Increase vf-msix-limit to use more "
1215 "channels on the PF.\n",
1216 count, efx_vf_size(efx));
1217 count = efx_vf_size(efx);
1218 }
1219
Ben Hutchings46123d02008-09-01 12:47:33 +01001220 return count;
1221}
1222
1223/* Probe the number and type of interrupts we are able to obtain, and
1224 * the resulting numbers of channels and RX queues.
1225 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001226static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001227{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001228 unsigned int extra_channels = 0;
1229 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001230 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001231
Ben Hutchings7f967c02012-02-13 23:45:02 +00001232 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1233 if (efx->extra_channel_type[i])
1234 ++extra_channels;
1235
Ben Hutchings8ceee662008-04-27 12:55:59 +01001236 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001237 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001238 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001239
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001240 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001241 if (separate_tx_channels)
1242 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001243 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001244 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001245
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001246 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001247 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001248 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001249 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001250 netif_err(efx, drv, efx->net_dev,
1251 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001252 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001253 netif_err(efx, drv, efx->net_dev,
1254 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001255 EFX_BUG_ON_PARANOID(rc >= n_channels);
1256 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001257 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001258 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001259 }
1260
1261 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001262 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001263 if (n_channels > extra_channels)
1264 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001265 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001266 efx->n_tx_channels = max(n_channels / 2, 1U);
1267 efx->n_rx_channels = max(n_channels -
1268 efx->n_tx_channels,
1269 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001270 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001271 efx->n_tx_channels = n_channels;
1272 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001273 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001274 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001275 efx_get_channel(efx, i)->irq =
1276 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001277 } else {
1278 /* Fall back to single channel MSI */
1279 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001280 netif_err(efx, drv, efx->net_dev,
1281 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001282 }
1283 }
1284
1285 /* Try single interrupt MSI */
1286 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001287 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001288 efx->n_rx_channels = 1;
1289 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001290 rc = pci_enable_msi(efx->pci_dev);
1291 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001292 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001293 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001294 netif_err(efx, drv, efx->net_dev,
1295 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001296 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1297 }
1298 }
1299
1300 /* Assume legacy interrupts */
1301 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001302 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001303 efx->n_rx_channels = 1;
1304 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001305 efx->legacy_irq = efx->pci_dev->irq;
1306 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001307
Ben Hutchings7f967c02012-02-13 23:45:02 +00001308 /* Assign extra channels if possible */
1309 j = efx->n_channels;
1310 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1311 if (!efx->extra_channel_type[i])
1312 continue;
1313 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1314 efx->n_channels <= extra_channels) {
1315 efx->extra_channel_type[i]->handle_no_channel(efx);
1316 } else {
1317 --j;
1318 efx_get_channel(efx, j)->type =
1319 efx->extra_channel_type[i];
1320 }
1321 }
1322
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001323 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001324 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001325 efx->n_rx_channels : efx_vf_size(efx));
1326
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001327 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001328}
1329
Ben Hutchingsd8291182012-10-05 23:35:41 +01001330static void efx_soft_enable_interrupts(struct efx_nic *efx)
1331{
1332 struct efx_channel *channel;
1333
1334 BUG_ON(efx->state == STATE_DISABLED);
1335
1336 efx->irq_soft_enabled = true;
1337 smp_wmb();
1338
1339 efx_for_each_channel(channel, efx) {
1340 if (!channel->type->keep_eventq)
1341 efx_init_eventq(channel);
1342 efx_start_eventq(channel);
1343 }
1344
1345 efx_mcdi_mode_event(efx);
1346}
1347
1348static void efx_soft_disable_interrupts(struct efx_nic *efx)
1349{
1350 struct efx_channel *channel;
1351
1352 if (efx->state == STATE_DISABLED)
1353 return;
1354
1355 efx_mcdi_mode_poll(efx);
1356
1357 efx->irq_soft_enabled = false;
1358 smp_wmb();
1359
1360 if (efx->legacy_irq)
1361 synchronize_irq(efx->legacy_irq);
1362
1363 efx_for_each_channel(channel, efx) {
1364 if (channel->irq)
1365 synchronize_irq(channel->irq);
1366
1367 efx_stop_eventq(channel);
1368 if (!channel->type->keep_eventq)
1369 efx_fini_eventq(channel);
1370 }
1371}
1372
1373static void efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001374{
1375 struct efx_channel *channel;
1376
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001377 BUG_ON(efx->state == STATE_DISABLED);
1378
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001379 if (efx->eeh_disabled_legacy_irq) {
1380 enable_irq(efx->legacy_irq);
1381 efx->eeh_disabled_legacy_irq = false;
1382 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001383
Ben Hutchings86094f72013-08-21 19:51:04 +01001384 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001385
1386 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001387 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001388 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001389 }
1390
Ben Hutchingsd8291182012-10-05 23:35:41 +01001391 efx_soft_enable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001392}
1393
Ben Hutchingsd8291182012-10-05 23:35:41 +01001394static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001395{
1396 struct efx_channel *channel;
1397
Ben Hutchingsd8291182012-10-05 23:35:41 +01001398 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001399
1400 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001401 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001402 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001403 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001404
Ben Hutchings86094f72013-08-21 19:51:04 +01001405 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001406}
1407
Ben Hutchings8ceee662008-04-27 12:55:59 +01001408static void efx_remove_interrupts(struct efx_nic *efx)
1409{
1410 struct efx_channel *channel;
1411
1412 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001413 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001414 channel->irq = 0;
1415 pci_disable_msi(efx->pci_dev);
1416 pci_disable_msix(efx->pci_dev);
1417
1418 /* Remove legacy interrupt */
1419 efx->legacy_irq = 0;
1420}
1421
Ben Hutchings8831da72008-09-01 12:47:48 +01001422static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001423{
Ben Hutchings602a5322011-05-16 17:32:39 +01001424 struct efx_channel *channel;
1425 struct efx_tx_queue *tx_queue;
1426
Ben Hutchings97653432011-01-12 18:26:56 +00001427 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001428 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001429
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001430 /* We need to mark which channels really have RX and TX
1431 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001432 * RX-only and TX-only channels.
1433 */
1434 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001435 if (channel->channel < efx->n_rx_channels)
1436 channel->rx_queue.core_index = channel->channel;
1437 else
1438 channel->rx_queue.core_index = -1;
1439
Ben Hutchings602a5322011-05-16 17:32:39 +01001440 efx_for_each_channel_tx_queue(tx_queue, channel)
1441 tx_queue->queue -= (efx->tx_channel_offset *
1442 EFX_TXQ_TYPES);
1443 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001444}
1445
1446static int efx_probe_nic(struct efx_nic *efx)
1447{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001448 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001449 int rc;
1450
Ben Hutchings62776d02010-06-23 11:30:07 +00001451 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001452
1453 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001454 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001455 if (rc)
1456 return rc;
1457
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001458 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001459 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001460 rc = efx_probe_interrupts(efx);
1461 if (rc)
1462 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001463
Ben Hutchings28e47c42012-02-15 01:58:49 +00001464 efx->type->dimension_resources(efx);
1465
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001466 if (efx->n_channels > 1)
1467 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001468 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001469 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001470 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001471
Ben Hutchings8831da72008-09-01 12:47:48 +01001472 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001473 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1474 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001475
1476 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001477 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1478 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001479
1480 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001481
1482fail:
1483 efx->type->remove(efx);
1484 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001485}
1486
1487static void efx_remove_nic(struct efx_nic *efx)
1488{
Ben Hutchings62776d02010-06-23 11:30:07 +00001489 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001490
1491 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001492 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001493}
1494
Ben Hutchingsadd72472012-11-08 01:46:53 +00001495static int efx_probe_filters(struct efx_nic *efx)
1496{
1497 int rc;
1498
1499 spin_lock_init(&efx->filter_lock);
1500
1501 rc = efx->type->filter_table_probe(efx);
1502 if (rc)
1503 return rc;
1504
1505#ifdef CONFIG_RFS_ACCEL
1506 if (efx->type->offload_features & NETIF_F_NTUPLE) {
1507 efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
1508 sizeof(*efx->rps_flow_id),
1509 GFP_KERNEL);
1510 if (!efx->rps_flow_id) {
1511 efx->type->filter_table_remove(efx);
1512 return -ENOMEM;
1513 }
1514 }
1515#endif
1516
1517 return 0;
1518}
1519
1520static void efx_remove_filters(struct efx_nic *efx)
1521{
1522#ifdef CONFIG_RFS_ACCEL
1523 kfree(efx->rps_flow_id);
1524#endif
1525 efx->type->filter_table_remove(efx);
1526}
1527
1528static void efx_restore_filters(struct efx_nic *efx)
1529{
1530 efx->type->filter_table_restore(efx);
1531}
1532
Ben Hutchings8ceee662008-04-27 12:55:59 +01001533/**************************************************************************
1534 *
1535 * NIC startup/shutdown
1536 *
1537 *************************************************************************/
1538
1539static int efx_probe_all(struct efx_nic *efx)
1540{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001541 int rc;
1542
Ben Hutchings8ceee662008-04-27 12:55:59 +01001543 rc = efx_probe_nic(efx);
1544 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001545 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001546 goto fail1;
1547 }
1548
Ben Hutchings8ceee662008-04-27 12:55:59 +01001549 rc = efx_probe_port(efx);
1550 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001551 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001552 goto fail2;
1553 }
1554
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001555 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1556 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1557 rc = -EINVAL;
1558 goto fail3;
1559 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001560 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001561
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001562 rc = efx_probe_filters(efx);
1563 if (rc) {
1564 netif_err(efx, probe, efx->net_dev,
1565 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001566 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001567 }
1568
Ben Hutchings7f967c02012-02-13 23:45:02 +00001569 rc = efx_probe_channels(efx);
1570 if (rc)
1571 goto fail4;
1572
Ben Hutchings8ceee662008-04-27 12:55:59 +01001573 return 0;
1574
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001575 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001576 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001577 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001578 efx_remove_port(efx);
1579 fail2:
1580 efx_remove_nic(efx);
1581 fail1:
1582 return rc;
1583}
1584
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001585/* If the interface is supposed to be running but is not, start
1586 * the hardware and software data path, regular activity for the port
1587 * (MAC statistics, link polling, etc.) and schedule the port to be
1588 * reconfigured. Interrupts must already be enabled. This function
1589 * is safe to call multiple times, so long as the NIC is not disabled.
1590 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001591 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001592static void efx_start_all(struct efx_nic *efx)
1593{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001594 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001595 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001596
1597 /* Check that it is appropriate to restart the interface. All
1598 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001599 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001600 return;
1601
Ben Hutchings8ceee662008-04-27 12:55:59 +01001602 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001603 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001604
Alexandre Rames626950d2013-01-14 17:20:22 +00001605 /* Start the hardware monitor if there is one */
1606 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001607 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1608 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001609
1610 /* If link state detection is normally event-driven, we have
1611 * to poll now because we could have missed a change
1612 */
1613 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001614 mutex_lock(&efx->mac_lock);
1615 if (efx->phy_op->poll(efx))
1616 efx_link_status_changed(efx);
1617 mutex_unlock(&efx->mac_lock);
1618 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001619
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001620 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001621}
1622
1623/* Flush all delayed work. Should only be called when no more delayed work
1624 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1625 * since we're holding the rtnl_lock at this point. */
1626static void efx_flush_all(struct efx_nic *efx)
1627{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001628 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001629 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001630 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001631 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001632 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001633}
1634
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001635/* Quiesce the hardware and software data path, and regular activity
1636 * for the port without bringing the link down. Safe to call multiple
1637 * times with the NIC in almost any state, but interrupts should be
1638 * enabled. Requires the RTNL lock.
1639 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001640static void efx_stop_all(struct efx_nic *efx)
1641{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001642 EFX_ASSERT_RESET_SERIALISED(efx);
1643
1644 /* port_enabled can be read safely under the rtnl lock */
1645 if (!efx->port_enabled)
1646 return;
1647
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001648 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001649 efx_stop_port(efx);
1650
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001651 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001652 efx_flush_all(efx);
1653
Ben Hutchings29c69a42013-01-28 19:01:06 +00001654 /* Stop the kernel transmit interface. This is only valid if
1655 * the device is stopped or detached; otherwise the watchdog
1656 * may fire immediately.
1657 */
1658 WARN_ON(netif_running(efx->net_dev) &&
1659 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001660 netif_tx_disable(efx->net_dev);
1661
1662 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001663}
1664
1665static void efx_remove_all(struct efx_nic *efx)
1666{
Ben Hutchings46426102010-09-10 06:42:33 +00001667 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001668 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001669 efx_remove_port(efx);
1670 efx_remove_nic(efx);
1671}
1672
Ben Hutchings8ceee662008-04-27 12:55:59 +01001673/**************************************************************************
1674 *
1675 * Interrupt moderation
1676 *
1677 **************************************************************************/
1678
Ben Hutchingscc180b62011-12-08 19:51:47 +00001679static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001680{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001681 if (usecs == 0)
1682 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001683 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001684 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001685 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001686}
1687
Ben Hutchings8ceee662008-04-27 12:55:59 +01001688/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001689int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1690 unsigned int rx_usecs, bool rx_adaptive,
1691 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001692{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001693 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001694 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1695 efx->timer_quantum_ns,
1696 1000);
1697 unsigned int tx_ticks;
1698 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001699
1700 EFX_ASSERT_RESET_SERIALISED(efx);
1701
Ben Hutchingscc180b62011-12-08 19:51:47 +00001702 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001703 return -EINVAL;
1704
Ben Hutchingscc180b62011-12-08 19:51:47 +00001705 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1706 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1707
Ben Hutchings9e393b32011-09-05 07:43:04 +00001708 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1709 !rx_may_override_tx) {
1710 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1711 "RX and TX IRQ moderation must be equal\n");
1712 return -EINVAL;
1713 }
1714
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001715 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001716 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001717 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001718 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001719 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001720 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001721 channel->irq_moderation = tx_ticks;
1722 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001723
1724 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001725}
1726
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001727void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1728 unsigned int *rx_usecs, bool *rx_adaptive)
1729{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001730 /* We must round up when converting ticks to microseconds
1731 * because we round down when converting the other way.
1732 */
1733
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001734 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001735 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1736 efx->timer_quantum_ns,
1737 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001738
1739 /* If channels are shared between RX and TX, so is IRQ
1740 * moderation. Otherwise, IRQ moderation is the same for all
1741 * TX channels and is not adaptive.
1742 */
1743 if (efx->tx_channel_offset == 0)
1744 *tx_usecs = *rx_usecs;
1745 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001746 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001747 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001748 efx->timer_quantum_ns,
1749 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001750}
1751
Ben Hutchings8ceee662008-04-27 12:55:59 +01001752/**************************************************************************
1753 *
1754 * Hardware monitor
1755 *
1756 **************************************************************************/
1757
Ben Hutchingse254c272010-09-20 08:44:10 +00001758/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001759static void efx_monitor(struct work_struct *data)
1760{
1761 struct efx_nic *efx = container_of(data, struct efx_nic,
1762 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001763
Ben Hutchings62776d02010-06-23 11:30:07 +00001764 netif_vdbg(efx, timer, efx->net_dev,
1765 "hardware monitor executing on CPU %d\n",
1766 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001767 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001768
Ben Hutchings8ceee662008-04-27 12:55:59 +01001769 /* If the mac_lock is already held then it is likely a port
1770 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001771 * most of the work of monitor() anyway. */
1772 if (mutex_trylock(&efx->mac_lock)) {
1773 if (efx->port_enabled)
1774 efx->type->monitor(efx);
1775 mutex_unlock(&efx->mac_lock);
1776 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001777
Ben Hutchings8ceee662008-04-27 12:55:59 +01001778 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1779 efx_monitor_interval);
1780}
1781
1782/**************************************************************************
1783 *
1784 * ioctls
1785 *
1786 *************************************************************************/
1787
1788/* Net device ioctl
1789 * Context: process, rtnl_lock() held.
1790 */
1791static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1792{
Ben Hutchings767e4682008-09-01 12:43:14 +01001793 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001794 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001795
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001796 if (cmd == SIOCSHWTSTAMP)
1797 return efx_ptp_ioctl(efx, ifr, cmd);
1798
Ben Hutchings68e7f452009-04-29 08:05:08 +00001799 /* Convert phy_id from older PRTAD/DEVAD format */
1800 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1801 (data->phy_id & 0xfc00) == 0x0400)
1802 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1803
1804 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001805}
1806
1807/**************************************************************************
1808 *
1809 * NAPI interface
1810 *
1811 **************************************************************************/
1812
Ben Hutchings7f967c02012-02-13 23:45:02 +00001813static void efx_init_napi_channel(struct efx_channel *channel)
1814{
1815 struct efx_nic *efx = channel->efx;
1816
1817 channel->napi_dev = efx->net_dev;
1818 netif_napi_add(channel->napi_dev, &channel->napi_str,
1819 efx_poll, napi_weight);
1820}
1821
Ben Hutchingse8f14992010-12-07 19:47:34 +00001822static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001823{
1824 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001825
Ben Hutchings7f967c02012-02-13 23:45:02 +00001826 efx_for_each_channel(channel, efx)
1827 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001828}
1829
1830static void efx_fini_napi_channel(struct efx_channel *channel)
1831{
1832 if (channel->napi_dev)
1833 netif_napi_del(&channel->napi_str);
1834 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001835}
1836
1837static void efx_fini_napi(struct efx_nic *efx)
1838{
1839 struct efx_channel *channel;
1840
Ben Hutchingse8f14992010-12-07 19:47:34 +00001841 efx_for_each_channel(channel, efx)
1842 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001843}
1844
1845/**************************************************************************
1846 *
1847 * Kernel netpoll interface
1848 *
1849 *************************************************************************/
1850
1851#ifdef CONFIG_NET_POLL_CONTROLLER
1852
1853/* Although in the common case interrupts will be disabled, this is not
1854 * guaranteed. However, all our work happens inside the NAPI callback,
1855 * so no locking is required.
1856 */
1857static void efx_netpoll(struct net_device *net_dev)
1858{
Ben Hutchings767e4682008-09-01 12:43:14 +01001859 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001860 struct efx_channel *channel;
1861
Ben Hutchings64ee3122008-09-01 12:47:38 +01001862 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001863 efx_schedule_channel(channel);
1864}
1865
1866#endif
1867
1868/**************************************************************************
1869 *
1870 * Kernel net device interface
1871 *
1872 *************************************************************************/
1873
1874/* Context: process, rtnl_lock() held. */
1875static int efx_net_open(struct net_device *net_dev)
1876{
Ben Hutchings767e4682008-09-01 12:43:14 +01001877 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001878 int rc;
1879
Ben Hutchings62776d02010-06-23 11:30:07 +00001880 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1881 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001882
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001883 rc = efx_check_disabled(efx);
1884 if (rc)
1885 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001886 if (efx->phy_mode & PHY_MODE_SPECIAL)
1887 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001888 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1889 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001890
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001891 /* Notify the kernel of the link state polled during driver load,
1892 * before the monitor starts running */
1893 efx_link_status_changed(efx);
1894
Ben Hutchings8ceee662008-04-27 12:55:59 +01001895 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001896 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001897 return 0;
1898}
1899
1900/* Context: process, rtnl_lock() held.
1901 * Note that the kernel will ignore our return code; this method
1902 * should really be a void.
1903 */
1904static int efx_net_stop(struct net_device *net_dev)
1905{
Ben Hutchings767e4682008-09-01 12:43:14 +01001906 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001907
Ben Hutchings62776d02010-06-23 11:30:07 +00001908 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1909 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001910
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001911 /* Stop the device and flush all the channels */
1912 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001913
1914 return 0;
1915}
1916
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001917/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001918static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1919 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001920{
Ben Hutchings767e4682008-09-01 12:43:14 +01001921 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001922
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001923 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00001924 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001925 spin_unlock_bh(&efx->stats_lock);
1926
Ben Hutchings8ceee662008-04-27 12:55:59 +01001927 return stats;
1928}
1929
1930/* Context: netif_tx_lock held, BHs disabled. */
1931static void efx_watchdog(struct net_device *net_dev)
1932{
Ben Hutchings767e4682008-09-01 12:43:14 +01001933 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001934
Ben Hutchings62776d02010-06-23 11:30:07 +00001935 netif_err(efx, tx_err, efx->net_dev,
1936 "TX stuck with port_enabled=%d: resetting channels\n",
1937 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001938
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001939 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001940}
1941
1942
1943/* Context: process, rtnl_lock() held. */
1944static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1945{
Ben Hutchings767e4682008-09-01 12:43:14 +01001946 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001947 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001948
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001949 rc = efx_check_disabled(efx);
1950 if (rc)
1951 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001952 if (new_mtu > EFX_MAX_MTU)
1953 return -EINVAL;
1954
Ben Hutchings62776d02010-06-23 11:30:07 +00001955 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001956
Ben Hutchings29c69a42013-01-28 19:01:06 +00001957 efx_device_detach_sync(efx);
1958 efx_stop_all(efx);
1959
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001960 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001961 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001962 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001963 mutex_unlock(&efx->mac_lock);
1964
Ben Hutchings8ceee662008-04-27 12:55:59 +01001965 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00001966 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001967 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001968}
1969
1970static int efx_set_mac_address(struct net_device *net_dev, void *data)
1971{
Ben Hutchings767e4682008-09-01 12:43:14 +01001972 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001973 struct sockaddr *addr = data;
1974 char *new_addr = addr->sa_data;
1975
Ben Hutchings8ceee662008-04-27 12:55:59 +01001976 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001977 netif_err(efx, drv, efx->net_dev,
1978 "invalid ethernet MAC address requested: %pM\n",
1979 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00001980 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001981 }
1982
1983 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001984 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001985
1986 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001987 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01001988 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001989 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001990
1991 return 0;
1992}
1993
Ben Hutchingsa816f752008-09-01 12:49:12 +01001994/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00001995static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001996{
Ben Hutchings767e4682008-09-01 12:43:14 +01001997 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001998
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001999 if (efx->port_enabled)
2000 queue_work(efx->workqueue, &efx->mac_work);
2001 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002002}
2003
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002004static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002005{
2006 struct efx_nic *efx = netdev_priv(net_dev);
2007
2008 /* If disabling RX n-tuple filtering, clear existing filters */
2009 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2010 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2011
2012 return 0;
2013}
2014
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002015static const struct net_device_ops efx_netdev_ops = {
2016 .ndo_open = efx_net_open,
2017 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002018 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002019 .ndo_tx_timeout = efx_watchdog,
2020 .ndo_start_xmit = efx_hard_start_xmit,
2021 .ndo_validate_addr = eth_validate_addr,
2022 .ndo_do_ioctl = efx_ioctl,
2023 .ndo_change_mtu = efx_change_mtu,
2024 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002025 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002026 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002027#ifdef CONFIG_SFC_SRIOV
2028 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2029 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2030 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2031 .ndo_get_vf_config = efx_sriov_get_vf_config,
2032#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002033#ifdef CONFIG_NET_POLL_CONTROLLER
2034 .ndo_poll_controller = efx_netpoll,
2035#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002036 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002037#ifdef CONFIG_RFS_ACCEL
2038 .ndo_rx_flow_steer = efx_filter_rfs,
2039#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002040};
2041
Ben Hutchings7dde5962008-12-12 22:09:38 -08002042static void efx_update_name(struct efx_nic *efx)
2043{
2044 strcpy(efx->name, efx->net_dev->name);
2045 efx_mtd_rename(efx);
2046 efx_set_channel_names(efx);
2047}
2048
Ben Hutchings8ceee662008-04-27 12:55:59 +01002049static int efx_netdev_event(struct notifier_block *this,
2050 unsigned long event, void *ptr)
2051{
Jiri Pirko351638e2013-05-28 01:30:21 +00002052 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002053
Ben Hutchings7dde5962008-12-12 22:09:38 -08002054 if (net_dev->netdev_ops == &efx_netdev_ops &&
2055 event == NETDEV_CHANGENAME)
2056 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002057
2058 return NOTIFY_DONE;
2059}
2060
2061static struct notifier_block efx_netdev_notifier = {
2062 .notifier_call = efx_netdev_event,
2063};
2064
Ben Hutchings06d5e192008-12-12 21:47:23 -08002065static ssize_t
2066show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2067{
2068 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2069 return sprintf(buf, "%d\n", efx->phy_type);
2070}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002071static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002072
Ben Hutchings8ceee662008-04-27 12:55:59 +01002073static int efx_register_netdev(struct efx_nic *efx)
2074{
2075 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002076 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002077 int rc;
2078
2079 net_dev->watchdog_timeo = 5 * HZ;
2080 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002081 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002082 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002083 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002084
Ben Hutchings7dde5962008-12-12 22:09:38 -08002085 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002086
Ben Hutchings7153f622012-07-27 20:50:52 +01002087 /* Enable resets to be scheduled and check whether any were
2088 * already requested. If so, the NIC is probably hosed so we
2089 * abort.
2090 */
2091 efx->state = STATE_READY;
2092 smp_mb(); /* ensure we change state before checking reset_pending */
2093 if (efx->reset_pending) {
2094 netif_err(efx, probe, efx->net_dev,
2095 "aborting probe due to scheduled reset\n");
2096 rc = -EIO;
2097 goto fail_locked;
2098 }
2099
Ben Hutchingsaed06282009-08-26 08:16:27 +00002100 rc = dev_alloc_name(net_dev, net_dev->name);
2101 if (rc < 0)
2102 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002103 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002104
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002105 /* Always start with carrier off; PHY events will detect the link */
2106 netif_carrier_off(net_dev);
2107
Ben Hutchingsaed06282009-08-26 08:16:27 +00002108 rc = register_netdevice(net_dev);
2109 if (rc)
2110 goto fail_locked;
2111
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002112 efx_for_each_channel(channel, efx) {
2113 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002114 efx_for_each_channel_tx_queue(tx_queue, channel)
2115 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002116 }
2117
Ben Hutchings7dde5962008-12-12 22:09:38 -08002118 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002119
Ben Hutchings06d5e192008-12-12 21:47:23 -08002120 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2121 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002122 netif_err(efx, drv, efx->net_dev,
2123 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002124 goto fail_registered;
2125 }
2126
Ben Hutchings8ceee662008-04-27 12:55:59 +01002127 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002128
Ben Hutchings7153f622012-07-27 20:50:52 +01002129fail_registered:
2130 rtnl_lock();
2131 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002132fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002133 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002134 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002135 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002136 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002137}
2138
2139static void efx_unregister_netdev(struct efx_nic *efx)
2140{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002141 if (!efx->net_dev)
2142 return;
2143
Ben Hutchings767e4682008-09-01 12:43:14 +01002144 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002145
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002146 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2147 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002148
2149 rtnl_lock();
2150 unregister_netdevice(efx->net_dev);
2151 efx->state = STATE_UNINIT;
2152 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002153}
2154
2155/**************************************************************************
2156 *
2157 * Device reset and suspend
2158 *
2159 **************************************************************************/
2160
Ben Hutchings2467ca42008-09-01 12:48:50 +01002161/* Tears down the entire software state and most of the hardware state
2162 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002163void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002164{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002165 EFX_ASSERT_RESET_SERIALISED(efx);
2166
Ben Hutchings2467ca42008-09-01 12:48:50 +01002167 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002168 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002169
2170 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002171 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2172 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002173 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002174}
2175
Ben Hutchings2467ca42008-09-01 12:48:50 +01002176/* This function will always ensure that the locks acquired in
2177 * efx_reset_down() are released. A failure return code indicates
2178 * that we were unable to reinitialise the hardware, and the
2179 * driver should be disabled. If ok is false, then the rx and tx
2180 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002181int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002182{
2183 int rc;
2184
Ben Hutchings2467ca42008-09-01 12:48:50 +01002185 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002186
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002187 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002188 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002189 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002190 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002191 }
2192
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002193 if (!ok)
2194 goto fail;
2195
Steve Hodgson4b988282009-01-29 17:50:51 +00002196 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002197 rc = efx->phy_op->init(efx);
2198 if (rc)
2199 goto fail;
2200 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002201 netif_err(efx, drv, efx->net_dev,
2202 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002203 }
2204
Ben Hutchings710b2082011-09-03 00:15:00 +01002205 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002206
Ben Hutchingsd8291182012-10-05 23:35:41 +01002207 efx_enable_interrupts(efx);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002208 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002209 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002210
2211 mutex_unlock(&efx->mac_lock);
2212
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002213 efx_start_all(efx);
2214
2215 return 0;
2216
2217fail:
2218 efx->port_initialized = false;
2219
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002220 mutex_unlock(&efx->mac_lock);
2221
Ben Hutchings8ceee662008-04-27 12:55:59 +01002222 return rc;
2223}
2224
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002225/* Reset the NIC using the specified method. Note that the reset may
2226 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002227 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002228 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002229 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002230int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002231{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002232 int rc, rc2;
2233 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002234
Ben Hutchings62776d02010-06-23 11:30:07 +00002235 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2236 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002237
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002238 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002239 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002240
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002241 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002242 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002243 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002244 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002245 }
2246
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002247 /* Clear flags for the scopes we covered. We assume the NIC and
2248 * driver are now quiescent so that there is no race here.
2249 */
2250 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002251
2252 /* Reinitialise bus-mastering, which may have been turned off before
2253 * the reset was scheduled. This is still appropriate, even in the
2254 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2255 * can respond to requests. */
2256 pci_set_master(efx->pci_dev);
2257
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002258out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002259 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002260 disabled = rc ||
2261 method == RESET_TYPE_DISABLE ||
2262 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002263 rc2 = efx_reset_up(efx, method, !disabled);
2264 if (rc2) {
2265 disabled = true;
2266 if (!rc)
2267 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002268 }
2269
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002270 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002271 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002272 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002273 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002274 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002275 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002276 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002277 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002278 return rc;
2279}
2280
Alexandre Rames626950d2013-01-14 17:20:22 +00002281/* Try recovery mechanisms.
2282 * For now only EEH is supported.
2283 * Returns 0 if the recovery mechanisms are unsuccessful.
2284 * Returns a non-zero value otherwise.
2285 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002286int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002287{
2288#ifdef CONFIG_EEH
2289 /* A PCI error can occur and not be seen by EEH because nothing
2290 * happens on the PCI bus. In this case the driver may fail and
2291 * schedule a 'recover or reset', leading to this recovery handler.
2292 * Manually call the eeh failure check function.
2293 */
2294 struct eeh_dev *eehdev =
2295 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2296
2297 if (eeh_dev_check_failure(eehdev)) {
2298 /* The EEH mechanisms will handle the error and reset the
2299 * device if necessary.
2300 */
2301 return 1;
2302 }
2303#endif
2304 return 0;
2305}
2306
Ben Hutchings8ceee662008-04-27 12:55:59 +01002307/* The worker thread exists so that code that cannot sleep can
2308 * schedule a reset for later.
2309 */
2310static void efx_reset_work(struct work_struct *data)
2311{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002312 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002313 unsigned long pending;
2314 enum reset_type method;
2315
2316 pending = ACCESS_ONCE(efx->reset_pending);
2317 method = fls(pending) - 1;
2318
2319 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2320 method == RESET_TYPE_RECOVER_OR_ALL) &&
2321 efx_try_recovery(efx))
2322 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002323
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002324 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002325 return;
2326
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002327 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002328
2329 /* We checked the state in efx_schedule_reset() but it may
2330 * have changed by now. Now that we have the RTNL lock,
2331 * it cannot change again.
2332 */
2333 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002334 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002335
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002336 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002337}
2338
2339void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2340{
2341 enum reset_type method;
2342
Alexandre Rames626950d2013-01-14 17:20:22 +00002343 if (efx->state == STATE_RECOVERY) {
2344 netif_dbg(efx, drv, efx->net_dev,
2345 "recovering: skip scheduling %s reset\n",
2346 RESET_TYPE(type));
2347 return;
2348 }
2349
Ben Hutchings8ceee662008-04-27 12:55:59 +01002350 switch (type) {
2351 case RESET_TYPE_INVISIBLE:
2352 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002353 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002354 case RESET_TYPE_WORLD:
2355 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002356 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002357 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002358 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2359 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002360 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002361 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002362 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002363 netif_dbg(efx, drv, efx->net_dev,
2364 "scheduling %s reset for %s\n",
2365 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002366 break;
2367 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002368
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002369 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002370 smp_mb(); /* ensure we change reset_pending before checking state */
2371
2372 /* If we're not READY then just leave the flags set as the cue
2373 * to abort probing or reschedule the reset later.
2374 */
2375 if (ACCESS_ONCE(efx->state) != STATE_READY)
2376 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002377
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002378 /* efx_process_channel() will no longer read events once a
2379 * reset is scheduled. So switch back to poll'd MCDI completions. */
2380 efx_mcdi_mode_poll(efx);
2381
Steve Hodgson1ab00622008-12-12 21:33:02 -08002382 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002383}
2384
2385/**************************************************************************
2386 *
2387 * List of NICs we support
2388 *
2389 **************************************************************************/
2390
2391/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002392static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002393 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2394 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002395 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002396 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2397 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002398 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002399 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002400 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002401 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002402 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002403 {0} /* end of list */
2404};
2405
2406/**************************************************************************
2407 *
Ben Hutchings37594332009-11-23 16:05:45 +00002408 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002409 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002410 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002411 * Needed so all function pointers are valid and do not have to be tested
2412 * before use
2413 *
2414 **************************************************************************/
2415int efx_port_dummy_op_int(struct efx_nic *efx)
2416{
2417 return 0;
2418}
2419void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002420
2421static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002422{
2423 return false;
2424}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002425
stephen hemminger6c8c2512011-04-14 05:50:12 +00002426static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002427 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002428 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002429 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002430 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002431};
2432
Ben Hutchings8ceee662008-04-27 12:55:59 +01002433/**************************************************************************
2434 *
2435 * Data housekeeping
2436 *
2437 **************************************************************************/
2438
2439/* This zeroes out and then fills in the invariants in a struct
2440 * efx_nic (including all sub-structures).
2441 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002442static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002443 struct pci_dev *pci_dev, struct net_device *net_dev)
2444{
Ben Hutchings46426102010-09-10 06:42:33 +00002445 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002446
2447 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002448 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002449#ifdef CONFIG_SFC_MTD
2450 INIT_LIST_HEAD(&efx->mtd_list);
2451#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002452 INIT_WORK(&efx->reset_work, efx_reset_work);
2453 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002454 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002455 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002456 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002457 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002458 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002459
2460 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002461 spin_lock_init(&efx->stats_lock);
2462 mutex_init(&efx->mac_lock);
2463 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002464 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002465 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002466 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002467
2468 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002469 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2470 if (!efx->channel[i])
2471 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002472 efx->msi_context[i].efx = efx;
2473 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002474 }
2475
Ben Hutchings8ceee662008-04-27 12:55:59 +01002476 /* Higher numbered interrupt modes are less capable! */
2477 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2478 interrupt_mode);
2479
Ben Hutchings6977dc62008-12-26 13:44:39 -08002480 /* Would be good to use the net_dev name, but we're too early */
2481 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2482 pci_name(pci_dev));
2483 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002484 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002485 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002486
Ben Hutchings8ceee662008-04-27 12:55:59 +01002487 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002488
2489fail:
2490 efx_fini_struct(efx);
2491 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002492}
2493
2494static void efx_fini_struct(struct efx_nic *efx)
2495{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002496 int i;
2497
2498 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2499 kfree(efx->channel[i]);
2500
Ben Hutchings8ceee662008-04-27 12:55:59 +01002501 if (efx->workqueue) {
2502 destroy_workqueue(efx->workqueue);
2503 efx->workqueue = NULL;
2504 }
2505}
2506
2507/**************************************************************************
2508 *
2509 * PCI interface
2510 *
2511 **************************************************************************/
2512
2513/* Main body of final NIC shutdown code
2514 * This is called only at module unload (or hotplug removal).
2515 */
2516static void efx_pci_remove_main(struct efx_nic *efx)
2517{
Ben Hutchings7153f622012-07-27 20:50:52 +01002518 /* Flush reset_work. It can no longer be scheduled since we
2519 * are not READY.
2520 */
2521 BUG_ON(efx->state == STATE_READY);
2522 cancel_work_sync(&efx->reset_work);
2523
Ben Hutchingsd8291182012-10-05 23:35:41 +01002524 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002525 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002526 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002527 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002528 efx_fini_napi(efx);
2529 efx_remove_all(efx);
2530}
2531
2532/* Final NIC shutdown
2533 * This is called only at module unload (or hotplug removal).
2534 */
2535static void efx_pci_remove(struct pci_dev *pci_dev)
2536{
2537 struct efx_nic *efx;
2538
2539 efx = pci_get_drvdata(pci_dev);
2540 if (!efx)
2541 return;
2542
2543 /* Mark the NIC as fini, then stop the interface */
2544 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002545 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002546 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002547 rtnl_unlock();
2548
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002549 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002550 efx_unregister_netdev(efx);
2551
Ben Hutchings7dde5962008-12-12 22:09:38 -08002552 efx_mtd_remove(efx);
2553
Ben Hutchings8ceee662008-04-27 12:55:59 +01002554 efx_pci_remove_main(efx);
2555
Ben Hutchings8ceee662008-04-27 12:55:59 +01002556 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002557 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002558
Ben Hutchings8ceee662008-04-27 12:55:59 +01002559 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002560 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002561 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002562
2563 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002564};
2565
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002566/* NIC VPD information
2567 * Called during probe to display the part number of the
2568 * installed NIC. VPD is potentially very large but this should
2569 * always appear within the first 512 bytes.
2570 */
2571#define SFC_VPD_LEN 512
2572static void efx_print_product_vpd(struct efx_nic *efx)
2573{
2574 struct pci_dev *dev = efx->pci_dev;
2575 char vpd_data[SFC_VPD_LEN];
2576 ssize_t vpd_size;
2577 int i, j;
2578
2579 /* Get the vpd data from the device */
2580 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2581 if (vpd_size <= 0) {
2582 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2583 return;
2584 }
2585
2586 /* Get the Read only section */
2587 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2588 if (i < 0) {
2589 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2590 return;
2591 }
2592
2593 j = pci_vpd_lrdt_size(&vpd_data[i]);
2594 i += PCI_VPD_LRDT_TAG_SIZE;
2595 if (i + j > vpd_size)
2596 j = vpd_size - i;
2597
2598 /* Get the Part number */
2599 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2600 if (i < 0) {
2601 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2602 return;
2603 }
2604
2605 j = pci_vpd_info_field_size(&vpd_data[i]);
2606 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2607 if (i + j > vpd_size) {
2608 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2609 return;
2610 }
2611
2612 netif_info(efx, drv, efx->net_dev,
2613 "Part Number : %.*s\n", j, &vpd_data[i]);
2614}
2615
2616
Ben Hutchings8ceee662008-04-27 12:55:59 +01002617/* Main body of NIC initialisation
2618 * This is called at module load (or hotplug insertion, theoretically).
2619 */
2620static int efx_pci_probe_main(struct efx_nic *efx)
2621{
2622 int rc;
2623
2624 /* Do start-of-day initialisation */
2625 rc = efx_probe_all(efx);
2626 if (rc)
2627 goto fail1;
2628
Ben Hutchingse8f14992010-12-07 19:47:34 +00002629 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002630
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002631 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002632 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002633 netif_err(efx, probe, efx->net_dev,
2634 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002635 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002636 }
2637
2638 rc = efx_init_port(efx);
2639 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002640 netif_err(efx, probe, efx->net_dev,
2641 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002642 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002643 }
2644
Ben Hutchings152b6a62009-11-29 03:43:56 +00002645 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002646 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002647 goto fail5;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002648 efx_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002649
2650 return 0;
2651
Ben Hutchings278c0622009-11-23 16:05:12 +00002652 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002653 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002654 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002655 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002656 fail3:
2657 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002658 efx_remove_all(efx);
2659 fail1:
2660 return rc;
2661}
2662
2663/* NIC initialisation
2664 *
2665 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002666 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002667 * sets up and registers the network devices with the kernel and hooks
2668 * the interrupt service routine. It does not prepare the device for
2669 * transmission; this is left to the first time one of the network
2670 * interfaces is brought up (i.e. efx_net_open).
2671 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002672static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002673 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002674{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002675 struct net_device *net_dev;
2676 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002677 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002678
2679 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002680 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2681 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002682 if (!net_dev)
2683 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002684 efx = netdev_priv(net_dev);
2685 efx->type = (const struct efx_nic_type *) entry->driver_data;
2686 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002687 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002688 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002689 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002690 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002691 /* Mask for features that also apply to VLAN devices */
2692 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002693 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2694 NETIF_F_RXCSUM);
2695 /* All offloads can be toggled */
2696 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002697 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002698 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002699 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002700 if (rc)
2701 goto fail1;
2702
Ben Hutchings62776d02010-06-23 11:30:07 +00002703 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002704 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002705
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002706 efx_print_product_vpd(efx);
2707
Ben Hutchings8ceee662008-04-27 12:55:59 +01002708 /* Set up basic I/O (BAR mappings etc) */
2709 rc = efx_init_io(efx);
2710 if (rc)
2711 goto fail2;
2712
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002713 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002714 if (rc)
2715 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002716
Ben Hutchings8ceee662008-04-27 12:55:59 +01002717 rc = efx_register_netdev(efx);
2718 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002719 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002720
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002721 rc = efx_sriov_init(efx);
2722 if (rc)
2723 netif_err(efx, probe, efx->net_dev,
2724 "SR-IOV can't be enabled rc %d\n", rc);
2725
Ben Hutchings62776d02010-06-23 11:30:07 +00002726 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002727
Ben Hutchings7c431612012-01-27 17:23:58 +00002728 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002729 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002730 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002731 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002732 if (rc)
2733 netif_warn(efx, probe, efx->net_dev,
2734 "failed to create MTDs (%d)\n", rc);
2735
Alexandre Rames626950d2013-01-14 17:20:22 +00002736 rc = pci_enable_pcie_error_reporting(pci_dev);
2737 if (rc && rc != -EINVAL)
2738 netif_warn(efx, probe, efx->net_dev,
2739 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2740
Ben Hutchings8ceee662008-04-27 12:55:59 +01002741 return 0;
2742
Ben Hutchings8ceee662008-04-27 12:55:59 +01002743 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002744 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002745 fail3:
2746 efx_fini_io(efx);
2747 fail2:
2748 efx_fini_struct(efx);
2749 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002750 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002751 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002752 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002753 free_netdev(net_dev);
2754 return rc;
2755}
2756
Ben Hutchings89c758f2009-11-29 03:43:07 +00002757static int efx_pm_freeze(struct device *dev)
2758{
2759 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2760
Ben Hutchings61da0262012-07-27 19:35:39 +01002761 rtnl_lock();
2762
Ben Hutchings6032fb52012-07-27 19:35:47 +01002763 if (efx->state != STATE_DISABLED) {
2764 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002765
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002766 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002767
Ben Hutchings6032fb52012-07-27 19:35:47 +01002768 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002769 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01002770 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002771
Ben Hutchings61da0262012-07-27 19:35:39 +01002772 rtnl_unlock();
2773
Ben Hutchings89c758f2009-11-29 03:43:07 +00002774 return 0;
2775}
2776
2777static int efx_pm_thaw(struct device *dev)
2778{
2779 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2780
Ben Hutchings61da0262012-07-27 19:35:39 +01002781 rtnl_lock();
2782
Ben Hutchings6032fb52012-07-27 19:35:47 +01002783 if (efx->state != STATE_DISABLED) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01002784 efx_enable_interrupts(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002785
Ben Hutchings6032fb52012-07-27 19:35:47 +01002786 mutex_lock(&efx->mac_lock);
2787 efx->phy_op->reconfigure(efx);
2788 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002789
Ben Hutchings6032fb52012-07-27 19:35:47 +01002790 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002791
Ben Hutchings6032fb52012-07-27 19:35:47 +01002792 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002793
Ben Hutchings6032fb52012-07-27 19:35:47 +01002794 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002795
Ben Hutchings6032fb52012-07-27 19:35:47 +01002796 efx->type->resume_wol(efx);
2797 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002798
Ben Hutchings61da0262012-07-27 19:35:39 +01002799 rtnl_unlock();
2800
Steve Hodgson319ba642010-06-01 11:17:24 +00002801 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2802 queue_work(reset_workqueue, &efx->reset_work);
2803
Ben Hutchings89c758f2009-11-29 03:43:07 +00002804 return 0;
2805}
2806
2807static int efx_pm_poweroff(struct device *dev)
2808{
2809 struct pci_dev *pci_dev = to_pci_dev(dev);
2810 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2811
2812 efx->type->fini(efx);
2813
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002814 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002815
2816 pci_save_state(pci_dev);
2817 return pci_set_power_state(pci_dev, PCI_D3hot);
2818}
2819
2820/* Used for both resume and restore */
2821static int efx_pm_resume(struct device *dev)
2822{
2823 struct pci_dev *pci_dev = to_pci_dev(dev);
2824 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2825 int rc;
2826
2827 rc = pci_set_power_state(pci_dev, PCI_D0);
2828 if (rc)
2829 return rc;
2830 pci_restore_state(pci_dev);
2831 rc = pci_enable_device(pci_dev);
2832 if (rc)
2833 return rc;
2834 pci_set_master(efx->pci_dev);
2835 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2836 if (rc)
2837 return rc;
2838 rc = efx->type->init(efx);
2839 if (rc)
2840 return rc;
2841 efx_pm_thaw(dev);
2842 return 0;
2843}
2844
2845static int efx_pm_suspend(struct device *dev)
2846{
2847 int rc;
2848
2849 efx_pm_freeze(dev);
2850 rc = efx_pm_poweroff(dev);
2851 if (rc)
2852 efx_pm_resume(dev);
2853 return rc;
2854}
2855
Ben Hutchings18e83e42012-01-05 19:05:20 +00002856static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002857 .suspend = efx_pm_suspend,
2858 .resume = efx_pm_resume,
2859 .freeze = efx_pm_freeze,
2860 .thaw = efx_pm_thaw,
2861 .poweroff = efx_pm_poweroff,
2862 .restore = efx_pm_resume,
2863};
2864
Alexandre Rames626950d2013-01-14 17:20:22 +00002865/* A PCI error affecting this device was detected.
2866 * At this point MMIO and DMA may be disabled.
2867 * Stop the software path and request a slot reset.
2868 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002869static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2870 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00002871{
2872 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2873 struct efx_nic *efx = pci_get_drvdata(pdev);
2874
2875 if (state == pci_channel_io_perm_failure)
2876 return PCI_ERS_RESULT_DISCONNECT;
2877
2878 rtnl_lock();
2879
2880 if (efx->state != STATE_DISABLED) {
2881 efx->state = STATE_RECOVERY;
2882 efx->reset_pending = 0;
2883
2884 efx_device_detach_sync(efx);
2885
2886 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002887 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00002888
2889 status = PCI_ERS_RESULT_NEED_RESET;
2890 } else {
2891 /* If the interface is disabled we don't want to do anything
2892 * with it.
2893 */
2894 status = PCI_ERS_RESULT_RECOVERED;
2895 }
2896
2897 rtnl_unlock();
2898
2899 pci_disable_device(pdev);
2900
2901 return status;
2902}
2903
2904/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002905static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00002906{
2907 struct efx_nic *efx = pci_get_drvdata(pdev);
2908 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2909 int rc;
2910
2911 if (pci_enable_device(pdev)) {
2912 netif_err(efx, hw, efx->net_dev,
2913 "Cannot re-enable PCI device after reset.\n");
2914 status = PCI_ERS_RESULT_DISCONNECT;
2915 }
2916
2917 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2918 if (rc) {
2919 netif_err(efx, hw, efx->net_dev,
2920 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2921 /* Non-fatal error. Continue. */
2922 }
2923
2924 return status;
2925}
2926
2927/* Perform the actual reset and resume I/O operations. */
2928static void efx_io_resume(struct pci_dev *pdev)
2929{
2930 struct efx_nic *efx = pci_get_drvdata(pdev);
2931 int rc;
2932
2933 rtnl_lock();
2934
2935 if (efx->state == STATE_DISABLED)
2936 goto out;
2937
2938 rc = efx_reset(efx, RESET_TYPE_ALL);
2939 if (rc) {
2940 netif_err(efx, hw, efx->net_dev,
2941 "efx_reset failed after PCI error (%d)\n", rc);
2942 } else {
2943 efx->state = STATE_READY;
2944 netif_dbg(efx, hw, efx->net_dev,
2945 "Done resetting and resuming IO after PCI error.\n");
2946 }
2947
2948out:
2949 rtnl_unlock();
2950}
2951
2952/* For simplicity and reliability, we always require a slot reset and try to
2953 * reset the hardware when a pci error affecting the device is detected.
2954 * We leave both the link_reset and mmio_enabled callback unimplemented:
2955 * with our request for slot reset the mmio_enabled callback will never be
2956 * called, and the link_reset callback is not used by AER or EEH mechanisms.
2957 */
2958static struct pci_error_handlers efx_err_handlers = {
2959 .error_detected = efx_io_error_detected,
2960 .slot_reset = efx_io_slot_reset,
2961 .resume = efx_io_resume,
2962};
2963
Ben Hutchings8ceee662008-04-27 12:55:59 +01002964static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00002965 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002966 .id_table = efx_pci_table,
2967 .probe = efx_pci_probe,
2968 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00002969 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00002970 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002971};
2972
2973/**************************************************************************
2974 *
2975 * Kernel module interface
2976 *
2977 *************************************************************************/
2978
2979module_param(interrupt_mode, uint, 0444);
2980MODULE_PARM_DESC(interrupt_mode,
2981 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2982
2983static int __init efx_init_module(void)
2984{
2985 int rc;
2986
2987 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2988
2989 rc = register_netdevice_notifier(&efx_netdev_notifier);
2990 if (rc)
2991 goto err_notifier;
2992
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002993 rc = efx_init_sriov();
2994 if (rc)
2995 goto err_sriov;
2996
Steve Hodgson1ab00622008-12-12 21:33:02 -08002997 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2998 if (!reset_workqueue) {
2999 rc = -ENOMEM;
3000 goto err_reset;
3001 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003002
3003 rc = pci_register_driver(&efx_pci_driver);
3004 if (rc < 0)
3005 goto err_pci;
3006
3007 return 0;
3008
3009 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003010 destroy_workqueue(reset_workqueue);
3011 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003012 efx_fini_sriov();
3013 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003014 unregister_netdevice_notifier(&efx_netdev_notifier);
3015 err_notifier:
3016 return rc;
3017}
3018
3019static void __exit efx_exit_module(void)
3020{
3021 printk(KERN_INFO "Solarflare NET driver unloading\n");
3022
3023 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003024 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003025 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003026 unregister_netdevice_notifier(&efx_netdev_notifier);
3027
3028}
3029
3030module_init(efx_init_module);
3031module_exit(efx_exit_module);
3032
Ben Hutchings906bb262009-11-29 15:16:19 +00003033MODULE_AUTHOR("Solarflare Communications and "
3034 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003035MODULE_DESCRIPTION("Solarflare Communications network driver");
3036MODULE_LICENSE("GPL");
3037MODULE_DEVICE_TABLE(pci, efx_pci_table);