blob: aec62139420e884eb36cce55e75b72349ade557c [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
Jon Cooper261e4d92013-04-15 18:51:54 +0100192static int efx_soft_enable_interrupts(struct efx_nic *efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100193static 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 */
Jon Cooper261e4d92013-04-15 18:51:54 +0100332static int efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100333{
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100334 struct efx_nic *efx = channel->efx;
Jon Cooper261e4d92013-04-15 18:51:54 +0100335 int rc;
336
337 EFX_WARN_ON_PARANOID(channel->eventq_init);
338
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100339 netif_dbg(efx, drv, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000340 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100341
Jon Cooper261e4d92013-04-15 18:51:54 +0100342 rc = efx_nic_init_eventq(channel);
343 if (rc == 0) {
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100344 efx->type->push_irq_moderation(channel);
Jon Cooper261e4d92013-04-15 18:51:54 +0100345 channel->eventq_read_ptr = 0;
346 channel->eventq_init = true;
347 }
348 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100349}
350
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000351/* Enable event queue processing and NAPI */
352static void efx_start_eventq(struct efx_channel *channel)
353{
354 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
355 "chan %d start event queue\n", channel->channel);
356
Ben Hutchings514bedb2012-10-05 19:30:16 +0100357 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000358 channel->enabled = true;
359 smp_wmb();
360
361 napi_enable(&channel->napi_str);
362 efx_nic_eventq_read_ack(channel);
363}
364
365/* Disable event queue processing and NAPI */
366static void efx_stop_eventq(struct efx_channel *channel)
367{
368 if (!channel->enabled)
369 return;
370
371 napi_disable(&channel->napi_str);
372 channel->enabled = false;
373}
374
Ben Hutchings8ceee662008-04-27 12:55:59 +0100375static void efx_fini_eventq(struct efx_channel *channel)
376{
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100377 if (!channel->eventq_init)
378 return;
379
Ben Hutchings62776d02010-06-23 11:30:07 +0000380 netif_dbg(channel->efx, drv, channel->efx->net_dev,
381 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100382
Ben Hutchings152b6a62009-11-29 03:43:56 +0000383 efx_nic_fini_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100384 channel->eventq_init = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100385}
386
387static void efx_remove_eventq(struct efx_channel *channel)
388{
Ben Hutchings62776d02010-06-23 11:30:07 +0000389 netif_dbg(channel->efx, drv, channel->efx->net_dev,
390 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100391
Ben Hutchings152b6a62009-11-29 03:43:56 +0000392 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100393}
394
395/**************************************************************************
396 *
397 * Channel handling
398 *
399 *************************************************************************/
400
Ben Hutchings7f967c02012-02-13 23:45:02 +0000401/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000402static struct efx_channel *
403efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
404{
405 struct efx_channel *channel;
406 struct efx_rx_queue *rx_queue;
407 struct efx_tx_queue *tx_queue;
408 int j;
409
Ben Hutchings7f967c02012-02-13 23:45:02 +0000410 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
411 if (!channel)
412 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000413
Ben Hutchings7f967c02012-02-13 23:45:02 +0000414 channel->efx = efx;
415 channel->channel = i;
416 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000417
Ben Hutchings7f967c02012-02-13 23:45:02 +0000418 for (j = 0; j < EFX_TXQ_TYPES; j++) {
419 tx_queue = &channel->tx_queue[j];
420 tx_queue->efx = efx;
421 tx_queue->queue = i * EFX_TXQ_TYPES + j;
422 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000423 }
424
Ben Hutchings46426102010-09-10 06:42:33 +0000425 rx_queue = &channel->rx_queue;
426 rx_queue->efx = efx;
427 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
428 (unsigned long)rx_queue);
429
430 return channel;
431}
432
Ben Hutchings7f967c02012-02-13 23:45:02 +0000433/* Allocate and initialise a channel structure, copying parameters
434 * (but not resources) from an old channel structure.
435 */
436static struct efx_channel *
437efx_copy_channel(const struct efx_channel *old_channel)
438{
439 struct efx_channel *channel;
440 struct efx_rx_queue *rx_queue;
441 struct efx_tx_queue *tx_queue;
442 int j;
443
444 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
445 if (!channel)
446 return NULL;
447
448 *channel = *old_channel;
449
450 channel->napi_dev = NULL;
451 memset(&channel->eventq, 0, sizeof(channel->eventq));
452
453 for (j = 0; j < EFX_TXQ_TYPES; j++) {
454 tx_queue = &channel->tx_queue[j];
455 if (tx_queue->channel)
456 tx_queue->channel = channel;
457 tx_queue->buffer = NULL;
458 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
459 }
460
461 rx_queue = &channel->rx_queue;
462 rx_queue->buffer = NULL;
463 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
464 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
465 (unsigned long)rx_queue);
466
467 return channel;
468}
469
Ben Hutchings8ceee662008-04-27 12:55:59 +0100470static int efx_probe_channel(struct efx_channel *channel)
471{
472 struct efx_tx_queue *tx_queue;
473 struct efx_rx_queue *rx_queue;
474 int rc;
475
Ben Hutchings62776d02010-06-23 11:30:07 +0000476 netif_dbg(channel->efx, probe, channel->efx->net_dev,
477 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100478
Ben Hutchings7f967c02012-02-13 23:45:02 +0000479 rc = channel->type->pre_probe(channel);
480 if (rc)
481 goto fail;
482
Ben Hutchings8ceee662008-04-27 12:55:59 +0100483 rc = efx_probe_eventq(channel);
484 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000485 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100486
487 efx_for_each_channel_tx_queue(tx_queue, channel) {
488 rc = efx_probe_tx_queue(tx_queue);
489 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000490 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100491 }
492
493 efx_for_each_channel_rx_queue(rx_queue, channel) {
494 rc = efx_probe_rx_queue(rx_queue);
495 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000496 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100497 }
498
499 channel->n_rx_frm_trunc = 0;
500
501 return 0;
502
Ben Hutchings7f967c02012-02-13 23:45:02 +0000503fail:
504 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100505 return rc;
506}
507
Ben Hutchings7f967c02012-02-13 23:45:02 +0000508static void
509efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
510{
511 struct efx_nic *efx = channel->efx;
512 const char *type;
513 int number;
514
515 number = channel->channel;
516 if (efx->tx_channel_offset == 0) {
517 type = "";
518 } else if (channel->channel < efx->tx_channel_offset) {
519 type = "-rx";
520 } else {
521 type = "-tx";
522 number -= efx->tx_channel_offset;
523 }
524 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
525}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100526
Ben Hutchings56536e92008-12-12 21:37:02 -0800527static void efx_set_channel_names(struct efx_nic *efx)
528{
529 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800530
Ben Hutchings7f967c02012-02-13 23:45:02 +0000531 efx_for_each_channel(channel, efx)
532 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100533 efx->msi_context[channel->channel].name,
534 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800535}
536
Ben Hutchings46426102010-09-10 06:42:33 +0000537static int efx_probe_channels(struct efx_nic *efx)
538{
539 struct efx_channel *channel;
540 int rc;
541
542 /* Restart special buffer allocation */
543 efx->next_buffer_table = 0;
544
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000545 /* Probe channels in reverse, so that any 'extra' channels
546 * use the start of the buffer table. This allows the traffic
547 * channels to be resized without moving them or wasting the
548 * entries before them.
549 */
550 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000551 rc = efx_probe_channel(channel);
552 if (rc) {
553 netif_err(efx, probe, efx->net_dev,
554 "failed to create channel %d\n",
555 channel->channel);
556 goto fail;
557 }
558 }
559 efx_set_channel_names(efx);
560
561 return 0;
562
563fail:
564 efx_remove_channels(efx);
565 return rc;
566}
567
Ben Hutchings8ceee662008-04-27 12:55:59 +0100568/* Channels are shutdown and reinitialised whilst the NIC is running
569 * to propagate configuration changes (mtu, checksum offload), or
570 * to clear hardware error conditions
571 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000572static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100573{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000574 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100575 struct efx_tx_queue *tx_queue;
576 struct efx_rx_queue *rx_queue;
577 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000578 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100579
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100580 /* Calculate the rx buffer allocation parameters required to
581 * support the current MTU, including padding for header
582 * alignment and overruns.
583 */
Jon Cooper43a37392012-10-18 15:49:54 +0100584 efx->rx_dma_len = (efx->rx_prefix_size +
Ben Hutchings272baee2013-01-29 23:33:14 +0000585 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
586 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000587 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Ben Hutchingsc14ff2e2013-05-13 11:58:31 +0000588 NET_IP_ALIGN + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000589 if (rx_buf_len <= PAGE_SIZE) {
590 efx->rx_scatter = false;
591 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000592 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000593 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000594 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000595 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
596 EFX_RX_BUF_ALIGNMENT) >
597 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000598 efx->rx_scatter = true;
599 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
600 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000601 } else {
602 efx->rx_scatter = false;
603 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000604 }
605
Daniel Pieczko1648a232013-02-13 10:54:41 +0000606 efx_rx_config_page_split(efx);
607 if (efx->rx_buffer_order)
608 netif_dbg(efx, drv, efx->net_dev,
609 "RX buf len=%u; page order=%u batch=%u\n",
610 efx->rx_dma_len, efx->rx_buffer_order,
611 efx->rx_pages_per_batch);
612 else
613 netif_dbg(efx, drv, efx->net_dev,
614 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
615 efx->rx_dma_len, efx->rx_page_buf_step,
616 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000617
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000618 /* RX filters also have scatter-enabled flags */
619 if (efx->rx_scatter != old_rx_scatter)
Ben Hutchingsadd72472012-11-08 01:46:53 +0000620 efx->type->filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100621
Ben Hutchings14bf7182012-05-22 01:27:58 +0100622 /* We must keep at least one descriptor in a TX ring empty.
623 * We could avoid this when the queue size does not exactly
624 * match the hardware ring size, but it's not that important.
625 * Therefore we stop the queue when one more skb might fill
626 * the ring completely. We wake it when half way back to
627 * empty.
628 */
629 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
630 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
631
Ben Hutchings8ceee662008-04-27 12:55:59 +0100632 /* Initialise the channels */
633 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100634 efx_for_each_channel_tx_queue(tx_queue, channel)
635 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100636
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000637 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100638 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000639 efx_nic_generate_fill_event(rx_queue);
640 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100641
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000642 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100643 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000644
645 if (netif_device_present(efx->net_dev))
646 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100647}
648
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000649static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100650{
651 struct efx_channel *channel;
652 struct efx_tx_queue *tx_queue;
653 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100654 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100655
656 EFX_ASSERT_RESET_SERIALISED(efx);
657 BUG_ON(efx->port_enabled);
658
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100659 /* Stop RX refill */
660 efx_for_each_channel(channel, efx) {
661 efx_for_each_channel_rx_queue(rx_queue, channel)
662 rx_queue->refill_enabled = false;
663 }
664
Ben Hutchings8ceee662008-04-27 12:55:59 +0100665 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000666 /* RX packet processing is pipelined, so wait for the
667 * NAPI handler to complete. At least event queue 0
668 * might be kept active by non-data events, so don't
669 * use napi_synchronize() but actually disable NAPI
670 * temporarily.
671 */
672 if (efx_channel_has_rx_queue(channel)) {
673 efx_stop_eventq(channel);
674 efx_start_eventq(channel);
675 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100676 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100677
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100678 rc = efx->type->fini_dmaq(efx);
679 if (rc && EFX_WORKAROUND_7803(efx)) {
680 /* Schedule a reset to recover from the flush failure. The
681 * descriptor caches reference memory we're about to free,
682 * but falcon_reconfigure_mac_wrapper() won't reconnect
683 * the MACs because of the pending reset.
684 */
685 netif_err(efx, drv, efx->net_dev,
686 "Resetting to recover from flush failure\n");
687 efx_schedule_reset(efx, RESET_TYPE_ALL);
688 } else if (rc) {
689 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
690 } else {
691 netif_dbg(efx, drv, efx->net_dev,
692 "successfully flushed all queues\n");
693 }
694
695 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100696 efx_for_each_channel_rx_queue(rx_queue, channel)
697 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000698 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100699 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100700 }
701}
702
703static void efx_remove_channel(struct efx_channel *channel)
704{
705 struct efx_tx_queue *tx_queue;
706 struct efx_rx_queue *rx_queue;
707
Ben Hutchings62776d02010-06-23 11:30:07 +0000708 netif_dbg(channel->efx, drv, channel->efx->net_dev,
709 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100710
711 efx_for_each_channel_rx_queue(rx_queue, channel)
712 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000713 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100714 efx_remove_tx_queue(tx_queue);
715 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100716 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100717}
718
Ben Hutchings46426102010-09-10 06:42:33 +0000719static void efx_remove_channels(struct efx_nic *efx)
720{
721 struct efx_channel *channel;
722
723 efx_for_each_channel(channel, efx)
724 efx_remove_channel(channel);
725}
726
727int
728efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
729{
730 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
731 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000732 unsigned i, next_buffer_table = 0;
Jon Cooper261e4d92013-04-15 18:51:54 +0100733 int rc, rc2;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100734
735 rc = efx_check_disabled(efx);
736 if (rc)
737 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000738
739 /* Not all channels should be reallocated. We must avoid
740 * reallocating their buffer table entries.
741 */
742 efx_for_each_channel(channel, efx) {
743 struct efx_rx_queue *rx_queue;
744 struct efx_tx_queue *tx_queue;
745
746 if (channel->type->copy)
747 continue;
748 next_buffer_table = max(next_buffer_table,
749 channel->eventq.index +
750 channel->eventq.entries);
751 efx_for_each_channel_rx_queue(rx_queue, channel)
752 next_buffer_table = max(next_buffer_table,
753 rx_queue->rxd.index +
754 rx_queue->rxd.entries);
755 efx_for_each_channel_tx_queue(tx_queue, channel)
756 next_buffer_table = max(next_buffer_table,
757 tx_queue->txd.index +
758 tx_queue->txd.entries);
759 }
Ben Hutchings46426102010-09-10 06:42:33 +0000760
Ben Hutchings29c69a42013-01-28 19:01:06 +0000761 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000762 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100763 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000764
Ben Hutchings7f967c02012-02-13 23:45:02 +0000765 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000766 memset(other_channel, 0, sizeof(other_channel));
767 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000768 channel = efx->channel[i];
769 if (channel->type->copy)
770 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000771 if (!channel) {
772 rc = -ENOMEM;
773 goto out;
774 }
775 other_channel[i] = channel;
776 }
777
778 /* Swap entry counts and channel pointers */
779 old_rxq_entries = efx->rxq_entries;
780 old_txq_entries = efx->txq_entries;
781 efx->rxq_entries = rxq_entries;
782 efx->txq_entries = txq_entries;
783 for (i = 0; i < efx->n_channels; i++) {
784 channel = efx->channel[i];
785 efx->channel[i] = other_channel[i];
786 other_channel[i] = channel;
787 }
788
Ben Hutchings7f967c02012-02-13 23:45:02 +0000789 /* Restart buffer table allocation */
790 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000791
Ben Hutchingse8f14992010-12-07 19:47:34 +0000792 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000793 channel = efx->channel[i];
794 if (!channel->type->copy)
795 continue;
796 rc = efx_probe_channel(channel);
797 if (rc)
798 goto rollback;
799 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000800 }
Ben Hutchings46426102010-09-10 06:42:33 +0000801
Ben Hutchings7f967c02012-02-13 23:45:02 +0000802out:
803 /* Destroy unused channel structures */
804 for (i = 0; i < efx->n_channels; i++) {
805 channel = other_channel[i];
806 if (channel && channel->type->copy) {
807 efx_fini_napi_channel(channel);
808 efx_remove_channel(channel);
809 kfree(channel);
810 }
811 }
812
Jon Cooper261e4d92013-04-15 18:51:54 +0100813 rc2 = efx_soft_enable_interrupts(efx);
814 if (rc2) {
815 rc = rc ? rc : rc2;
816 netif_err(efx, drv, efx->net_dev,
817 "unable to restart interrupts on channel reallocation\n");
818 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
819 } else {
820 efx_start_all(efx);
821 netif_device_attach(efx->net_dev);
822 }
Ben Hutchings46426102010-09-10 06:42:33 +0000823 return rc;
824
825rollback:
826 /* Swap back */
827 efx->rxq_entries = old_rxq_entries;
828 efx->txq_entries = old_txq_entries;
829 for (i = 0; i < efx->n_channels; i++) {
830 channel = efx->channel[i];
831 efx->channel[i] = other_channel[i];
832 other_channel[i] = channel;
833 }
834 goto out;
835}
836
Steve Hodgson90d683a2010-06-01 11:19:39 +0000837void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100838{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000839 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100840}
841
Ben Hutchings7f967c02012-02-13 23:45:02 +0000842static const struct efx_channel_type efx_default_channel_type = {
843 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100844 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000845 .get_name = efx_get_channel_name,
846 .copy = efx_copy_channel,
847 .keep_eventq = false,
848};
849
850int efx_channel_dummy_op_int(struct efx_channel *channel)
851{
852 return 0;
853}
854
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100855void efx_channel_dummy_op_void(struct efx_channel *channel)
856{
857}
858
Ben Hutchings8ceee662008-04-27 12:55:59 +0100859/**************************************************************************
860 *
861 * Port handling
862 *
863 **************************************************************************/
864
865/* This ensures that the kernel is kept informed (via
866 * netif_carrier_on/off) of the link status, and also maintains the
867 * link status's stop on the port's TX queue.
868 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000869void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100870{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000871 struct efx_link_state *link_state = &efx->link_state;
872
Ben Hutchings8ceee662008-04-27 12:55:59 +0100873 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
874 * that no events are triggered between unregister_netdev() and the
875 * driver unloading. A more general condition is that NETDEV_CHANGE
876 * can only be generated between NETDEV_UP and NETDEV_DOWN */
877 if (!netif_running(efx->net_dev))
878 return;
879
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000880 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100881 efx->n_link_state_changes++;
882
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000883 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100884 netif_carrier_on(efx->net_dev);
885 else
886 netif_carrier_off(efx->net_dev);
887 }
888
889 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000890 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000891 netif_info(efx, link, efx->net_dev,
Ben Hutchings964e6132012-11-19 23:08:22 +0000892 "link up at %uMbps %s-duplex (MTU %d)\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000893 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings964e6132012-11-19 23:08:22 +0000894 efx->net_dev->mtu);
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000895 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000896 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100897}
898
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000899void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
900{
901 efx->link_advertising = advertising;
902 if (advertising) {
903 if (advertising & ADVERTISED_Pause)
904 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
905 else
906 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
907 if (advertising & ADVERTISED_Asym_Pause)
908 efx->wanted_fc ^= EFX_FC_TX;
909 }
910}
911
David S. Millerb56269462011-05-17 17:53:22 -0400912void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000913{
914 efx->wanted_fc = wanted_fc;
915 if (efx->link_advertising) {
916 if (wanted_fc & EFX_FC_RX)
917 efx->link_advertising |= (ADVERTISED_Pause |
918 ADVERTISED_Asym_Pause);
919 else
920 efx->link_advertising &= ~(ADVERTISED_Pause |
921 ADVERTISED_Asym_Pause);
922 if (wanted_fc & EFX_FC_TX)
923 efx->link_advertising ^= ADVERTISED_Asym_Pause;
924 }
925}
926
Ben Hutchings115122a2009-03-04 09:52:52 +0000927static void efx_fini_port(struct efx_nic *efx);
928
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000929/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
930 * the MAC appropriately. All other PHY configuration changes are pushed
931 * through phy_op->set_settings(), and pushed asynchronously to the MAC
932 * through efx_monitor().
933 *
934 * Callers must hold the mac_lock
935 */
936int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100937{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000938 enum efx_phy_mode phy_mode;
939 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100940
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000941 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100942
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000943 /* Disable PHY transmit in mac level loopbacks */
944 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800945 if (LOOPBACK_INTERNAL(efx))
946 efx->phy_mode |= PHY_MODE_TX_DISABLED;
947 else
948 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800949
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000950 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800951
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000952 if (rc)
953 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100954
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000955 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100956}
957
958/* Reinitialise the MAC to pick up new PHY settings, even if the port is
959 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000960int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100961{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000962 int rc;
963
Ben Hutchings8ceee662008-04-27 12:55:59 +0100964 EFX_ASSERT_RESET_SERIALISED(efx);
965
966 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000967 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100968 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000969
970 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100971}
972
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000973/* Asynchronous work item for changing MAC promiscuity and multicast
974 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
975 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800976static void efx_mac_work(struct work_struct *data)
977{
978 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
979
980 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100981 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100982 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800983 mutex_unlock(&efx->mac_lock);
984}
985
Ben Hutchings8ceee662008-04-27 12:55:59 +0100986static int efx_probe_port(struct efx_nic *efx)
987{
988 int rc;
989
Ben Hutchings62776d02010-06-23 11:30:07 +0000990 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100991
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000992 if (phy_flash_cfg)
993 efx->phy_mode = PHY_MODE_SPECIAL;
994
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000995 /* Connect up MAC/PHY operations table */
996 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100997 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000998 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100999
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001000 /* Initialise MAC address to permanent address */
1001 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001002
1003 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001004}
1005
1006static int efx_init_port(struct efx_nic *efx)
1007{
1008 int rc;
1009
Ben Hutchings62776d02010-06-23 11:30:07 +00001010 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001011
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001012 mutex_lock(&efx->mac_lock);
1013
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001014 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001015 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001016 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001017
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001018 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001019
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001020 /* Reconfigure the MAC before creating dma queues (required for
1021 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001022 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001023
1024 /* Ensure the PHY advertises the correct flow control settings */
1025 rc = efx->phy_op->reconfigure(efx);
1026 if (rc)
1027 goto fail2;
1028
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001029 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001030 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001031
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001032fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001033 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001034fail1:
1035 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001036 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001037}
1038
Ben Hutchings8ceee662008-04-27 12:55:59 +01001039static void efx_start_port(struct efx_nic *efx)
1040{
Ben Hutchings62776d02010-06-23 11:30:07 +00001041 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001042 BUG_ON(efx->port_enabled);
1043
1044 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001045 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001046
1047 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1048 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001049 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001050
Ben Hutchings8ceee662008-04-27 12:55:59 +01001051 mutex_unlock(&efx->mac_lock);
1052}
1053
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001054/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001055static void efx_stop_port(struct efx_nic *efx)
1056{
Ben Hutchings62776d02010-06-23 11:30:07 +00001057 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001058
1059 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001060 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001061 mutex_unlock(&efx->mac_lock);
1062
1063 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001064 netif_addr_lock_bh(efx->net_dev);
1065 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001066}
1067
1068static void efx_fini_port(struct efx_nic *efx)
1069{
Ben Hutchings62776d02010-06-23 11:30:07 +00001070 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001071
1072 if (!efx->port_initialized)
1073 return;
1074
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001075 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001076 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001077
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001078 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001079 efx_link_status_changed(efx);
1080}
1081
1082static void efx_remove_port(struct efx_nic *efx)
1083{
Ben Hutchings62776d02010-06-23 11:30:07 +00001084 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001085
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001086 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001087}
1088
1089/**************************************************************************
1090 *
1091 * NIC handling
1092 *
1093 **************************************************************************/
1094
1095/* This configures the PCI device to enable I/O and DMA. */
1096static int efx_init_io(struct efx_nic *efx)
1097{
1098 struct pci_dev *pci_dev = efx->pci_dev;
1099 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001100 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001101 int rc;
1102
Ben Hutchings62776d02010-06-23 11:30:07 +00001103 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001104
1105 rc = pci_enable_device(pci_dev);
1106 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001107 netif_err(efx, probe, efx->net_dev,
1108 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001109 goto fail1;
1110 }
1111
1112 pci_set_master(pci_dev);
1113
1114 /* Set the PCI DMA mask. Try all possibilities from our
1115 * genuine mask down to 32 bits, because some architectures
1116 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1117 * masks event though they reject 46 bit masks.
1118 */
1119 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001120 if (dma_supported(&pci_dev->dev, dma_mask)) {
1121 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001122 if (rc == 0)
1123 break;
1124 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001125 dma_mask >>= 1;
1126 }
1127 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001128 netif_err(efx, probe, efx->net_dev,
1129 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001130 goto fail2;
1131 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001132 netif_dbg(efx, probe, efx->net_dev,
1133 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001134 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001135 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001136 /* dma_set_coherent_mask() is not *allowed* to
1137 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001138 * but just in case...
1139 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001140 netif_err(efx, probe, efx->net_dev,
1141 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001142 goto fail2;
1143 }
1144
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001145 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1146 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001147 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001148 netif_err(efx, probe, efx->net_dev,
1149 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001150 rc = -EIO;
1151 goto fail3;
1152 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001153 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001154 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001155 netif_err(efx, probe, efx->net_dev,
1156 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001157 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001158 rc = -ENOMEM;
1159 goto fail4;
1160 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001161 netif_dbg(efx, probe, efx->net_dev,
1162 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001163 (unsigned long long)efx->membase_phys, mem_map_size,
1164 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001165
1166 return 0;
1167
1168 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001169 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001170 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001171 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001172 fail2:
1173 pci_disable_device(efx->pci_dev);
1174 fail1:
1175 return rc;
1176}
1177
1178static void efx_fini_io(struct efx_nic *efx)
1179{
Ben Hutchings62776d02010-06-23 11:30:07 +00001180 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001181
1182 if (efx->membase) {
1183 iounmap(efx->membase);
1184 efx->membase = NULL;
1185 }
1186
1187 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001188 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001189 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001190 }
1191
1192 pci_disable_device(efx->pci_dev);
1193}
1194
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001195static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001196{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001197 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001198 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001199 int cpu;
1200
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001201 if (rss_cpus) {
1202 count = rss_cpus;
1203 } else {
1204 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1205 netif_warn(efx, probe, efx->net_dev,
1206 "RSS disabled due to allocation failure\n");
1207 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001208 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001209
1210 count = 0;
1211 for_each_online_cpu(cpu) {
1212 if (!cpumask_test_cpu(cpu, thread_mask)) {
1213 ++count;
1214 cpumask_or(thread_mask, thread_mask,
1215 topology_thread_cpumask(cpu));
1216 }
1217 }
1218
1219 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001220 }
1221
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001222 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1223 * table entries that are inaccessible to VFs
1224 */
1225 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1226 count > efx_vf_size(efx)) {
1227 netif_warn(efx, probe, efx->net_dev,
1228 "Reducing number of RSS channels from %u to %u for "
1229 "VF support. Increase vf-msix-limit to use more "
1230 "channels on the PF.\n",
1231 count, efx_vf_size(efx));
1232 count = efx_vf_size(efx);
1233 }
1234
Ben Hutchings46123d02008-09-01 12:47:33 +01001235 return count;
1236}
1237
1238/* Probe the number and type of interrupts we are able to obtain, and
1239 * the resulting numbers of channels and RX queues.
1240 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001241static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001242{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001243 unsigned int extra_channels = 0;
1244 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001245 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001246
Ben Hutchings7f967c02012-02-13 23:45:02 +00001247 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1248 if (efx->extra_channel_type[i])
1249 ++extra_channels;
1250
Ben Hutchings8ceee662008-04-27 12:55:59 +01001251 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001252 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001253 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001254
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001255 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001256 if (separate_tx_channels)
1257 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001258 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001259 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001260
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001261 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001262 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001263 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001264 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001265 netif_err(efx, drv, efx->net_dev,
1266 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001267 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001268 netif_err(efx, drv, efx->net_dev,
1269 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001270 EFX_BUG_ON_PARANOID(rc >= n_channels);
1271 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001272 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001273 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001274 }
1275
1276 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001277 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001278 if (n_channels > extra_channels)
1279 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001280 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001281 efx->n_tx_channels = max(n_channels / 2, 1U);
1282 efx->n_rx_channels = max(n_channels -
1283 efx->n_tx_channels,
1284 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001285 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001286 efx->n_tx_channels = n_channels;
1287 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001288 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001289 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001290 efx_get_channel(efx, i)->irq =
1291 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001292 } else {
1293 /* Fall back to single channel MSI */
1294 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001295 netif_err(efx, drv, efx->net_dev,
1296 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001297 }
1298 }
1299
1300 /* Try single interrupt MSI */
1301 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001302 efx->n_channels = 1;
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 rc = pci_enable_msi(efx->pci_dev);
1306 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001307 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001308 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001309 netif_err(efx, drv, efx->net_dev,
1310 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001311 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1312 }
1313 }
1314
1315 /* Assume legacy interrupts */
1316 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001317 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001318 efx->n_rx_channels = 1;
1319 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001320 efx->legacy_irq = efx->pci_dev->irq;
1321 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001322
Ben Hutchings7f967c02012-02-13 23:45:02 +00001323 /* Assign extra channels if possible */
1324 j = efx->n_channels;
1325 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1326 if (!efx->extra_channel_type[i])
1327 continue;
1328 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1329 efx->n_channels <= extra_channels) {
1330 efx->extra_channel_type[i]->handle_no_channel(efx);
1331 } else {
1332 --j;
1333 efx_get_channel(efx, j)->type =
1334 efx->extra_channel_type[i];
1335 }
1336 }
1337
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001338 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001339 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001340 efx->n_rx_channels : efx_vf_size(efx));
1341
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001342 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001343}
1344
Jon Cooper261e4d92013-04-15 18:51:54 +01001345static int efx_soft_enable_interrupts(struct efx_nic *efx)
Ben Hutchingsd8291182012-10-05 23:35:41 +01001346{
Jon Cooper261e4d92013-04-15 18:51:54 +01001347 struct efx_channel *channel, *end_channel;
1348 int rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001349
1350 BUG_ON(efx->state == STATE_DISABLED);
1351
1352 efx->irq_soft_enabled = true;
1353 smp_wmb();
1354
1355 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001356 if (!channel->type->keep_eventq) {
1357 rc = efx_init_eventq(channel);
1358 if (rc)
1359 goto fail;
1360 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001361 efx_start_eventq(channel);
1362 }
1363
1364 efx_mcdi_mode_event(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +01001365
1366 return 0;
1367fail:
1368 end_channel = channel;
1369 efx_for_each_channel(channel, efx) {
1370 if (channel == end_channel)
1371 break;
1372 efx_stop_eventq(channel);
1373 if (!channel->type->keep_eventq)
1374 efx_fini_eventq(channel);
1375 }
1376
1377 return rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001378}
1379
1380static void efx_soft_disable_interrupts(struct efx_nic *efx)
1381{
1382 struct efx_channel *channel;
1383
1384 if (efx->state == STATE_DISABLED)
1385 return;
1386
1387 efx_mcdi_mode_poll(efx);
1388
1389 efx->irq_soft_enabled = false;
1390 smp_wmb();
1391
1392 if (efx->legacy_irq)
1393 synchronize_irq(efx->legacy_irq);
1394
1395 efx_for_each_channel(channel, efx) {
1396 if (channel->irq)
1397 synchronize_irq(channel->irq);
1398
1399 efx_stop_eventq(channel);
1400 if (!channel->type->keep_eventq)
1401 efx_fini_eventq(channel);
1402 }
Ben Hutchingscade7152013-08-27 23:12:31 +01001403
1404 /* Flush the asynchronous MCDI request queue */
1405 efx_mcdi_flush_async(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01001406}
1407
Jon Cooper261e4d92013-04-15 18:51:54 +01001408static int efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001409{
Jon Cooper261e4d92013-04-15 18:51:54 +01001410 struct efx_channel *channel, *end_channel;
1411 int rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001412
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001413 BUG_ON(efx->state == STATE_DISABLED);
1414
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001415 if (efx->eeh_disabled_legacy_irq) {
1416 enable_irq(efx->legacy_irq);
1417 efx->eeh_disabled_legacy_irq = false;
1418 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001419
Ben Hutchings86094f72013-08-21 19:51:04 +01001420 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001421
1422 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001423 if (channel->type->keep_eventq) {
1424 rc = efx_init_eventq(channel);
1425 if (rc)
1426 goto fail;
1427 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001428 }
1429
Jon Cooper261e4d92013-04-15 18:51:54 +01001430 rc = efx_soft_enable_interrupts(efx);
1431 if (rc)
1432 goto fail;
1433
1434 return 0;
1435
1436fail:
1437 end_channel = channel;
1438 efx_for_each_channel(channel, efx) {
1439 if (channel == end_channel)
1440 break;
1441 if (channel->type->keep_eventq)
1442 efx_fini_eventq(channel);
1443 }
1444
1445 efx->type->irq_disable_non_ev(efx);
1446
1447 return rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001448}
1449
Ben Hutchingsd8291182012-10-05 23:35:41 +01001450static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001451{
1452 struct efx_channel *channel;
1453
Ben Hutchingsd8291182012-10-05 23:35:41 +01001454 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001455
1456 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001457 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001458 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001459 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001460
Ben Hutchings86094f72013-08-21 19:51:04 +01001461 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001462}
1463
Ben Hutchings8ceee662008-04-27 12:55:59 +01001464static void efx_remove_interrupts(struct efx_nic *efx)
1465{
1466 struct efx_channel *channel;
1467
1468 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001469 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001470 channel->irq = 0;
1471 pci_disable_msi(efx->pci_dev);
1472 pci_disable_msix(efx->pci_dev);
1473
1474 /* Remove legacy interrupt */
1475 efx->legacy_irq = 0;
1476}
1477
Ben Hutchings8831da72008-09-01 12:47:48 +01001478static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001479{
Ben Hutchings602a5322011-05-16 17:32:39 +01001480 struct efx_channel *channel;
1481 struct efx_tx_queue *tx_queue;
1482
Ben Hutchings97653432011-01-12 18:26:56 +00001483 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001484 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001485
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001486 /* We need to mark which channels really have RX and TX
1487 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001488 * RX-only and TX-only channels.
1489 */
1490 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001491 if (channel->channel < efx->n_rx_channels)
1492 channel->rx_queue.core_index = channel->channel;
1493 else
1494 channel->rx_queue.core_index = -1;
1495
Ben Hutchings602a5322011-05-16 17:32:39 +01001496 efx_for_each_channel_tx_queue(tx_queue, channel)
1497 tx_queue->queue -= (efx->tx_channel_offset *
1498 EFX_TXQ_TYPES);
1499 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001500}
1501
1502static int efx_probe_nic(struct efx_nic *efx)
1503{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001504 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001505 int rc;
1506
Ben Hutchings62776d02010-06-23 11:30:07 +00001507 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001508
1509 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001510 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001511 if (rc)
1512 return rc;
1513
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001514 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001515 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001516 rc = efx_probe_interrupts(efx);
1517 if (rc)
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001518 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001519
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001520 rc = efx->type->dimension_resources(efx);
1521 if (rc)
1522 goto fail2;
Ben Hutchings28e47c42012-02-15 01:58:49 +00001523
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001524 if (efx->n_channels > 1)
1525 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001526 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001527 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001528 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001529
Ben Hutchings8831da72008-09-01 12:47:48 +01001530 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001531 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1532 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001533
1534 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001535 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1536 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001537
1538 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001539
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001540fail2:
1541 efx_remove_interrupts(efx);
1542fail1:
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001543 efx->type->remove(efx);
1544 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001545}
1546
1547static void efx_remove_nic(struct efx_nic *efx)
1548{
Ben Hutchings62776d02010-06-23 11:30:07 +00001549 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001550
1551 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001552 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001553}
1554
Ben Hutchingsadd72472012-11-08 01:46:53 +00001555static int efx_probe_filters(struct efx_nic *efx)
1556{
1557 int rc;
1558
1559 spin_lock_init(&efx->filter_lock);
1560
1561 rc = efx->type->filter_table_probe(efx);
1562 if (rc)
1563 return rc;
1564
1565#ifdef CONFIG_RFS_ACCEL
1566 if (efx->type->offload_features & NETIF_F_NTUPLE) {
1567 efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
1568 sizeof(*efx->rps_flow_id),
1569 GFP_KERNEL);
1570 if (!efx->rps_flow_id) {
1571 efx->type->filter_table_remove(efx);
1572 return -ENOMEM;
1573 }
1574 }
1575#endif
1576
1577 return 0;
1578}
1579
1580static void efx_remove_filters(struct efx_nic *efx)
1581{
1582#ifdef CONFIG_RFS_ACCEL
1583 kfree(efx->rps_flow_id);
1584#endif
1585 efx->type->filter_table_remove(efx);
1586}
1587
1588static void efx_restore_filters(struct efx_nic *efx)
1589{
1590 efx->type->filter_table_restore(efx);
1591}
1592
Ben Hutchings8ceee662008-04-27 12:55:59 +01001593/**************************************************************************
1594 *
1595 * NIC startup/shutdown
1596 *
1597 *************************************************************************/
1598
1599static int efx_probe_all(struct efx_nic *efx)
1600{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001601 int rc;
1602
Ben Hutchings8ceee662008-04-27 12:55:59 +01001603 rc = efx_probe_nic(efx);
1604 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001605 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001606 goto fail1;
1607 }
1608
Ben Hutchings8ceee662008-04-27 12:55:59 +01001609 rc = efx_probe_port(efx);
1610 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001611 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001612 goto fail2;
1613 }
1614
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001615 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1616 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1617 rc = -EINVAL;
1618 goto fail3;
1619 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001620 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001621
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001622 rc = efx_probe_filters(efx);
1623 if (rc) {
1624 netif_err(efx, probe, efx->net_dev,
1625 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001626 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001627 }
1628
Ben Hutchings7f967c02012-02-13 23:45:02 +00001629 rc = efx_probe_channels(efx);
1630 if (rc)
1631 goto fail4;
1632
Ben Hutchings8ceee662008-04-27 12:55:59 +01001633 return 0;
1634
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001635 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001636 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001637 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001638 efx_remove_port(efx);
1639 fail2:
1640 efx_remove_nic(efx);
1641 fail1:
1642 return rc;
1643}
1644
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001645/* If the interface is supposed to be running but is not, start
1646 * the hardware and software data path, regular activity for the port
1647 * (MAC statistics, link polling, etc.) and schedule the port to be
1648 * reconfigured. Interrupts must already be enabled. This function
1649 * is safe to call multiple times, so long as the NIC is not disabled.
1650 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001651 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001652static void efx_start_all(struct efx_nic *efx)
1653{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001654 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001655 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001656
1657 /* Check that it is appropriate to restart the interface. All
1658 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001659 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001660 return;
1661
Ben Hutchings8ceee662008-04-27 12:55:59 +01001662 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001663 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001664
Alexandre Rames626950d2013-01-14 17:20:22 +00001665 /* Start the hardware monitor if there is one */
1666 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001667 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1668 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001669
1670 /* If link state detection is normally event-driven, we have
1671 * to poll now because we could have missed a change
1672 */
1673 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001674 mutex_lock(&efx->mac_lock);
1675 if (efx->phy_op->poll(efx))
1676 efx_link_status_changed(efx);
1677 mutex_unlock(&efx->mac_lock);
1678 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001679
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001680 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001681}
1682
1683/* Flush all delayed work. Should only be called when no more delayed work
1684 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1685 * since we're holding the rtnl_lock at this point. */
1686static void efx_flush_all(struct efx_nic *efx)
1687{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001688 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001689 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001690 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001691 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001692 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001693}
1694
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001695/* Quiesce the hardware and software data path, and regular activity
1696 * for the port without bringing the link down. Safe to call multiple
1697 * times with the NIC in almost any state, but interrupts should be
1698 * enabled. Requires the RTNL lock.
1699 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001700static void efx_stop_all(struct efx_nic *efx)
1701{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001702 EFX_ASSERT_RESET_SERIALISED(efx);
1703
1704 /* port_enabled can be read safely under the rtnl lock */
1705 if (!efx->port_enabled)
1706 return;
1707
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001708 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001709 efx_stop_port(efx);
1710
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001711 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001712 efx_flush_all(efx);
1713
Ben Hutchings29c69a42013-01-28 19:01:06 +00001714 /* Stop the kernel transmit interface. This is only valid if
1715 * the device is stopped or detached; otherwise the watchdog
1716 * may fire immediately.
1717 */
1718 WARN_ON(netif_running(efx->net_dev) &&
1719 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001720 netif_tx_disable(efx->net_dev);
1721
1722 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001723}
1724
1725static void efx_remove_all(struct efx_nic *efx)
1726{
Ben Hutchings46426102010-09-10 06:42:33 +00001727 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001728 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001729 efx_remove_port(efx);
1730 efx_remove_nic(efx);
1731}
1732
Ben Hutchings8ceee662008-04-27 12:55:59 +01001733/**************************************************************************
1734 *
1735 * Interrupt moderation
1736 *
1737 **************************************************************************/
1738
Ben Hutchingscc180b62011-12-08 19:51:47 +00001739static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001740{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001741 if (usecs == 0)
1742 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001743 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001744 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001745 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001746}
1747
Ben Hutchings8ceee662008-04-27 12:55:59 +01001748/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001749int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1750 unsigned int rx_usecs, bool rx_adaptive,
1751 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001752{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001753 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001754 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1755 efx->timer_quantum_ns,
1756 1000);
1757 unsigned int tx_ticks;
1758 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001759
1760 EFX_ASSERT_RESET_SERIALISED(efx);
1761
Ben Hutchingscc180b62011-12-08 19:51:47 +00001762 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001763 return -EINVAL;
1764
Ben Hutchingscc180b62011-12-08 19:51:47 +00001765 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1766 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1767
Ben Hutchings9e393b32011-09-05 07:43:04 +00001768 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1769 !rx_may_override_tx) {
1770 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1771 "RX and TX IRQ moderation must be equal\n");
1772 return -EINVAL;
1773 }
1774
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001775 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001776 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001777 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001778 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001779 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001780 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001781 channel->irq_moderation = tx_ticks;
1782 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001783
1784 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001785}
1786
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001787void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1788 unsigned int *rx_usecs, bool *rx_adaptive)
1789{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001790 /* We must round up when converting ticks to microseconds
1791 * because we round down when converting the other way.
1792 */
1793
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001794 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001795 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1796 efx->timer_quantum_ns,
1797 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001798
1799 /* If channels are shared between RX and TX, so is IRQ
1800 * moderation. Otherwise, IRQ moderation is the same for all
1801 * TX channels and is not adaptive.
1802 */
1803 if (efx->tx_channel_offset == 0)
1804 *tx_usecs = *rx_usecs;
1805 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001806 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001807 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001808 efx->timer_quantum_ns,
1809 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001810}
1811
Ben Hutchings8ceee662008-04-27 12:55:59 +01001812/**************************************************************************
1813 *
1814 * Hardware monitor
1815 *
1816 **************************************************************************/
1817
Ben Hutchingse254c272010-09-20 08:44:10 +00001818/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001819static void efx_monitor(struct work_struct *data)
1820{
1821 struct efx_nic *efx = container_of(data, struct efx_nic,
1822 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001823
Ben Hutchings62776d02010-06-23 11:30:07 +00001824 netif_vdbg(efx, timer, efx->net_dev,
1825 "hardware monitor executing on CPU %d\n",
1826 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001827 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001828
Ben Hutchings8ceee662008-04-27 12:55:59 +01001829 /* If the mac_lock is already held then it is likely a port
1830 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001831 * most of the work of monitor() anyway. */
1832 if (mutex_trylock(&efx->mac_lock)) {
1833 if (efx->port_enabled)
1834 efx->type->monitor(efx);
1835 mutex_unlock(&efx->mac_lock);
1836 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001837
Ben Hutchings8ceee662008-04-27 12:55:59 +01001838 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1839 efx_monitor_interval);
1840}
1841
1842/**************************************************************************
1843 *
1844 * ioctls
1845 *
1846 *************************************************************************/
1847
1848/* Net device ioctl
1849 * Context: process, rtnl_lock() held.
1850 */
1851static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1852{
Ben Hutchings767e4682008-09-01 12:43:14 +01001853 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001854 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001855
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001856 if (cmd == SIOCSHWTSTAMP)
1857 return efx_ptp_ioctl(efx, ifr, cmd);
1858
Ben Hutchings68e7f452009-04-29 08:05:08 +00001859 /* Convert phy_id from older PRTAD/DEVAD format */
1860 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1861 (data->phy_id & 0xfc00) == 0x0400)
1862 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1863
1864 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001865}
1866
1867/**************************************************************************
1868 *
1869 * NAPI interface
1870 *
1871 **************************************************************************/
1872
Ben Hutchings7f967c02012-02-13 23:45:02 +00001873static void efx_init_napi_channel(struct efx_channel *channel)
1874{
1875 struct efx_nic *efx = channel->efx;
1876
1877 channel->napi_dev = efx->net_dev;
1878 netif_napi_add(channel->napi_dev, &channel->napi_str,
1879 efx_poll, napi_weight);
1880}
1881
Ben Hutchingse8f14992010-12-07 19:47:34 +00001882static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001883{
1884 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001885
Ben Hutchings7f967c02012-02-13 23:45:02 +00001886 efx_for_each_channel(channel, efx)
1887 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001888}
1889
1890static void efx_fini_napi_channel(struct efx_channel *channel)
1891{
1892 if (channel->napi_dev)
1893 netif_napi_del(&channel->napi_str);
1894 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001895}
1896
1897static void efx_fini_napi(struct efx_nic *efx)
1898{
1899 struct efx_channel *channel;
1900
Ben Hutchingse8f14992010-12-07 19:47:34 +00001901 efx_for_each_channel(channel, efx)
1902 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001903}
1904
1905/**************************************************************************
1906 *
1907 * Kernel netpoll interface
1908 *
1909 *************************************************************************/
1910
1911#ifdef CONFIG_NET_POLL_CONTROLLER
1912
1913/* Although in the common case interrupts will be disabled, this is not
1914 * guaranteed. However, all our work happens inside the NAPI callback,
1915 * so no locking is required.
1916 */
1917static void efx_netpoll(struct net_device *net_dev)
1918{
Ben Hutchings767e4682008-09-01 12:43:14 +01001919 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001920 struct efx_channel *channel;
1921
Ben Hutchings64ee3122008-09-01 12:47:38 +01001922 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001923 efx_schedule_channel(channel);
1924}
1925
1926#endif
1927
1928/**************************************************************************
1929 *
1930 * Kernel net device interface
1931 *
1932 *************************************************************************/
1933
1934/* Context: process, rtnl_lock() held. */
1935static int efx_net_open(struct net_device *net_dev)
1936{
Ben Hutchings767e4682008-09-01 12:43:14 +01001937 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001938 int rc;
1939
Ben Hutchings62776d02010-06-23 11:30:07 +00001940 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1941 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001942
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001943 rc = efx_check_disabled(efx);
1944 if (rc)
1945 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001946 if (efx->phy_mode & PHY_MODE_SPECIAL)
1947 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001948 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1949 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001950
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001951 /* Notify the kernel of the link state polled during driver load,
1952 * before the monitor starts running */
1953 efx_link_status_changed(efx);
1954
Ben Hutchings8ceee662008-04-27 12:55:59 +01001955 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001956 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001957 return 0;
1958}
1959
1960/* Context: process, rtnl_lock() held.
1961 * Note that the kernel will ignore our return code; this method
1962 * should really be a void.
1963 */
1964static int efx_net_stop(struct net_device *net_dev)
1965{
Ben Hutchings767e4682008-09-01 12:43:14 +01001966 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001967
Ben Hutchings62776d02010-06-23 11:30:07 +00001968 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1969 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001970
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001971 /* Stop the device and flush all the channels */
1972 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001973
1974 return 0;
1975}
1976
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001977/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001978static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1979 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001980{
Ben Hutchings767e4682008-09-01 12:43:14 +01001981 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001982
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001983 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00001984 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001985 spin_unlock_bh(&efx->stats_lock);
1986
Ben Hutchings8ceee662008-04-27 12:55:59 +01001987 return stats;
1988}
1989
1990/* Context: netif_tx_lock held, BHs disabled. */
1991static void efx_watchdog(struct net_device *net_dev)
1992{
Ben Hutchings767e4682008-09-01 12:43:14 +01001993 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001994
Ben Hutchings62776d02010-06-23 11:30:07 +00001995 netif_err(efx, tx_err, efx->net_dev,
1996 "TX stuck with port_enabled=%d: resetting channels\n",
1997 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001998
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001999 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002000}
2001
2002
2003/* Context: process, rtnl_lock() held. */
2004static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2005{
Ben Hutchings767e4682008-09-01 12:43:14 +01002006 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002007 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002008
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002009 rc = efx_check_disabled(efx);
2010 if (rc)
2011 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002012 if (new_mtu > EFX_MAX_MTU)
2013 return -EINVAL;
2014
Ben Hutchings62776d02010-06-23 11:30:07 +00002015 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002016
Ben Hutchings29c69a42013-01-28 19:01:06 +00002017 efx_device_detach_sync(efx);
2018 efx_stop_all(efx);
2019
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002020 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002021 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01002022 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002023 mutex_unlock(&efx->mac_lock);
2024
Ben Hutchings8ceee662008-04-27 12:55:59 +01002025 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00002026 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002027 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002028}
2029
2030static int efx_set_mac_address(struct net_device *net_dev, void *data)
2031{
Ben Hutchings767e4682008-09-01 12:43:14 +01002032 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002033 struct sockaddr *addr = data;
2034 char *new_addr = addr->sa_data;
2035
Ben Hutchings8ceee662008-04-27 12:55:59 +01002036 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002037 netif_err(efx, drv, efx->net_dev,
2038 "invalid ethernet MAC address requested: %pM\n",
2039 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002040 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002041 }
2042
2043 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002044 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002045
2046 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002047 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002048 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002049 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002050
2051 return 0;
2052}
2053
Ben Hutchingsa816f752008-09-01 12:49:12 +01002054/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002055static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002056{
Ben Hutchings767e4682008-09-01 12:43:14 +01002057 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002058
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002059 if (efx->port_enabled)
2060 queue_work(efx->workqueue, &efx->mac_work);
2061 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002062}
2063
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002064static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002065{
2066 struct efx_nic *efx = netdev_priv(net_dev);
2067
2068 /* If disabling RX n-tuple filtering, clear existing filters */
2069 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2070 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2071
2072 return 0;
2073}
2074
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002075static const struct net_device_ops efx_netdev_ops = {
2076 .ndo_open = efx_net_open,
2077 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002078 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002079 .ndo_tx_timeout = efx_watchdog,
2080 .ndo_start_xmit = efx_hard_start_xmit,
2081 .ndo_validate_addr = eth_validate_addr,
2082 .ndo_do_ioctl = efx_ioctl,
2083 .ndo_change_mtu = efx_change_mtu,
2084 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002085 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002086 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002087#ifdef CONFIG_SFC_SRIOV
2088 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2089 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2090 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2091 .ndo_get_vf_config = efx_sriov_get_vf_config,
2092#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002093#ifdef CONFIG_NET_POLL_CONTROLLER
2094 .ndo_poll_controller = efx_netpoll,
2095#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002096 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002097#ifdef CONFIG_RFS_ACCEL
2098 .ndo_rx_flow_steer = efx_filter_rfs,
2099#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002100};
2101
Ben Hutchings7dde5962008-12-12 22:09:38 -08002102static void efx_update_name(struct efx_nic *efx)
2103{
2104 strcpy(efx->name, efx->net_dev->name);
2105 efx_mtd_rename(efx);
2106 efx_set_channel_names(efx);
2107}
2108
Ben Hutchings8ceee662008-04-27 12:55:59 +01002109static int efx_netdev_event(struct notifier_block *this,
2110 unsigned long event, void *ptr)
2111{
Jiri Pirko351638e2013-05-28 01:30:21 +00002112 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002113
Ben Hutchings7dde5962008-12-12 22:09:38 -08002114 if (net_dev->netdev_ops == &efx_netdev_ops &&
2115 event == NETDEV_CHANGENAME)
2116 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002117
2118 return NOTIFY_DONE;
2119}
2120
2121static struct notifier_block efx_netdev_notifier = {
2122 .notifier_call = efx_netdev_event,
2123};
2124
Ben Hutchings06d5e192008-12-12 21:47:23 -08002125static ssize_t
2126show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2127{
2128 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2129 return sprintf(buf, "%d\n", efx->phy_type);
2130}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002131static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002132
Ben Hutchings8ceee662008-04-27 12:55:59 +01002133static int efx_register_netdev(struct efx_nic *efx)
2134{
2135 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002136 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002137 int rc;
2138
2139 net_dev->watchdog_timeo = 5 * HZ;
2140 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002141 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002142 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002143 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002144
Ben Hutchings7dde5962008-12-12 22:09:38 -08002145 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002146
Ben Hutchings7153f622012-07-27 20:50:52 +01002147 /* Enable resets to be scheduled and check whether any were
2148 * already requested. If so, the NIC is probably hosed so we
2149 * abort.
2150 */
2151 efx->state = STATE_READY;
2152 smp_mb(); /* ensure we change state before checking reset_pending */
2153 if (efx->reset_pending) {
2154 netif_err(efx, probe, efx->net_dev,
2155 "aborting probe due to scheduled reset\n");
2156 rc = -EIO;
2157 goto fail_locked;
2158 }
2159
Ben Hutchingsaed06282009-08-26 08:16:27 +00002160 rc = dev_alloc_name(net_dev, net_dev->name);
2161 if (rc < 0)
2162 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002163 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002164
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002165 /* Always start with carrier off; PHY events will detect the link */
2166 netif_carrier_off(net_dev);
2167
Ben Hutchingsaed06282009-08-26 08:16:27 +00002168 rc = register_netdevice(net_dev);
2169 if (rc)
2170 goto fail_locked;
2171
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002172 efx_for_each_channel(channel, efx) {
2173 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002174 efx_for_each_channel_tx_queue(tx_queue, channel)
2175 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002176 }
2177
Ben Hutchings7dde5962008-12-12 22:09:38 -08002178 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002179
Ben Hutchings06d5e192008-12-12 21:47:23 -08002180 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2181 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002182 netif_err(efx, drv, efx->net_dev,
2183 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002184 goto fail_registered;
2185 }
2186
Ben Hutchings8ceee662008-04-27 12:55:59 +01002187 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002188
Ben Hutchings7153f622012-07-27 20:50:52 +01002189fail_registered:
2190 rtnl_lock();
2191 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002192fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002193 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002194 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002195 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002196 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002197}
2198
2199static void efx_unregister_netdev(struct efx_nic *efx)
2200{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002201 if (!efx->net_dev)
2202 return;
2203
Ben Hutchings767e4682008-09-01 12:43:14 +01002204 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002205
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002206 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2207 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002208
2209 rtnl_lock();
2210 unregister_netdevice(efx->net_dev);
2211 efx->state = STATE_UNINIT;
2212 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002213}
2214
2215/**************************************************************************
2216 *
2217 * Device reset and suspend
2218 *
2219 **************************************************************************/
2220
Ben Hutchings2467ca42008-09-01 12:48:50 +01002221/* Tears down the entire software state and most of the hardware state
2222 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002223void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002224{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002225 EFX_ASSERT_RESET_SERIALISED(efx);
2226
Ben Hutchings2467ca42008-09-01 12:48:50 +01002227 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002228 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002229
2230 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002231 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2232 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002233 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002234}
2235
Ben Hutchings2467ca42008-09-01 12:48:50 +01002236/* This function will always ensure that the locks acquired in
2237 * efx_reset_down() are released. A failure return code indicates
2238 * that we were unable to reinitialise the hardware, and the
2239 * driver should be disabled. If ok is false, then the rx and tx
2240 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002241int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002242{
2243 int rc;
2244
Ben Hutchings2467ca42008-09-01 12:48:50 +01002245 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002246
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002247 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002248 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002249 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002250 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002251 }
2252
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002253 if (!ok)
2254 goto fail;
2255
Steve Hodgson4b988282009-01-29 17:50:51 +00002256 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002257 rc = efx->phy_op->init(efx);
2258 if (rc)
2259 goto fail;
2260 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002261 netif_err(efx, drv, efx->net_dev,
2262 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002263 }
2264
Jon Cooper261e4d92013-04-15 18:51:54 +01002265 rc = efx_enable_interrupts(efx);
2266 if (rc)
2267 goto fail;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002268 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002269 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002270
2271 mutex_unlock(&efx->mac_lock);
2272
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002273 efx_start_all(efx);
2274
2275 return 0;
2276
2277fail:
2278 efx->port_initialized = false;
2279
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002280 mutex_unlock(&efx->mac_lock);
2281
Ben Hutchings8ceee662008-04-27 12:55:59 +01002282 return rc;
2283}
2284
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002285/* Reset the NIC using the specified method. Note that the reset may
2286 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002287 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002288 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002289 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002290int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002291{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002292 int rc, rc2;
2293 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002294
Ben Hutchings62776d02010-06-23 11:30:07 +00002295 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2296 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002297
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002298 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002299 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002300
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002301 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002302 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002303 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002304 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002305 }
2306
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002307 /* Clear flags for the scopes we covered. We assume the NIC and
2308 * driver are now quiescent so that there is no race here.
2309 */
2310 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002311
2312 /* Reinitialise bus-mastering, which may have been turned off before
2313 * the reset was scheduled. This is still appropriate, even in the
2314 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2315 * can respond to requests. */
2316 pci_set_master(efx->pci_dev);
2317
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002318out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002319 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002320 disabled = rc ||
2321 method == RESET_TYPE_DISABLE ||
2322 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002323 rc2 = efx_reset_up(efx, method, !disabled);
2324 if (rc2) {
2325 disabled = true;
2326 if (!rc)
2327 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002328 }
2329
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002330 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002331 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002332 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002333 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002334 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002335 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002336 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002337 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002338 return rc;
2339}
2340
Alexandre Rames626950d2013-01-14 17:20:22 +00002341/* Try recovery mechanisms.
2342 * For now only EEH is supported.
2343 * Returns 0 if the recovery mechanisms are unsuccessful.
2344 * Returns a non-zero value otherwise.
2345 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002346int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002347{
2348#ifdef CONFIG_EEH
2349 /* A PCI error can occur and not be seen by EEH because nothing
2350 * happens on the PCI bus. In this case the driver may fail and
2351 * schedule a 'recover or reset', leading to this recovery handler.
2352 * Manually call the eeh failure check function.
2353 */
2354 struct eeh_dev *eehdev =
2355 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2356
2357 if (eeh_dev_check_failure(eehdev)) {
2358 /* The EEH mechanisms will handle the error and reset the
2359 * device if necessary.
2360 */
2361 return 1;
2362 }
2363#endif
2364 return 0;
2365}
2366
Ben Hutchings8ceee662008-04-27 12:55:59 +01002367/* The worker thread exists so that code that cannot sleep can
2368 * schedule a reset for later.
2369 */
2370static void efx_reset_work(struct work_struct *data)
2371{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002372 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002373 unsigned long pending;
2374 enum reset_type method;
2375
2376 pending = ACCESS_ONCE(efx->reset_pending);
2377 method = fls(pending) - 1;
2378
2379 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2380 method == RESET_TYPE_RECOVER_OR_ALL) &&
2381 efx_try_recovery(efx))
2382 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002383
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002384 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002385 return;
2386
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002387 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002388
2389 /* We checked the state in efx_schedule_reset() but it may
2390 * have changed by now. Now that we have the RTNL lock,
2391 * it cannot change again.
2392 */
2393 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002394 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002395
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002396 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002397}
2398
2399void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2400{
2401 enum reset_type method;
2402
Alexandre Rames626950d2013-01-14 17:20:22 +00002403 if (efx->state == STATE_RECOVERY) {
2404 netif_dbg(efx, drv, efx->net_dev,
2405 "recovering: skip scheduling %s reset\n",
2406 RESET_TYPE(type));
2407 return;
2408 }
2409
Ben Hutchings8ceee662008-04-27 12:55:59 +01002410 switch (type) {
2411 case RESET_TYPE_INVISIBLE:
2412 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002413 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002414 case RESET_TYPE_WORLD:
2415 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002416 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002417 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002418 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2419 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002420 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002421 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002422 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002423 netif_dbg(efx, drv, efx->net_dev,
2424 "scheduling %s reset for %s\n",
2425 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002426 break;
2427 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002428
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002429 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002430 smp_mb(); /* ensure we change reset_pending before checking state */
2431
2432 /* If we're not READY then just leave the flags set as the cue
2433 * to abort probing or reschedule the reset later.
2434 */
2435 if (ACCESS_ONCE(efx->state) != STATE_READY)
2436 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002437
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002438 /* efx_process_channel() will no longer read events once a
2439 * reset is scheduled. So switch back to poll'd MCDI completions. */
2440 efx_mcdi_mode_poll(efx);
2441
Steve Hodgson1ab00622008-12-12 21:33:02 -08002442 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002443}
2444
2445/**************************************************************************
2446 *
2447 * List of NICs we support
2448 *
2449 **************************************************************************/
2450
2451/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002452static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002453 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2454 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002455 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002456 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2457 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002458 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002459 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002460 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002461 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002462 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002463 {0} /* end of list */
2464};
2465
2466/**************************************************************************
2467 *
Ben Hutchings37594332009-11-23 16:05:45 +00002468 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002469 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002470 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002471 * Needed so all function pointers are valid and do not have to be tested
2472 * before use
2473 *
2474 **************************************************************************/
2475int efx_port_dummy_op_int(struct efx_nic *efx)
2476{
2477 return 0;
2478}
2479void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002480
2481static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002482{
2483 return false;
2484}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002485
stephen hemminger6c8c2512011-04-14 05:50:12 +00002486static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002487 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002488 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002489 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002490 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002491};
2492
Ben Hutchings8ceee662008-04-27 12:55:59 +01002493/**************************************************************************
2494 *
2495 * Data housekeeping
2496 *
2497 **************************************************************************/
2498
2499/* This zeroes out and then fills in the invariants in a struct
2500 * efx_nic (including all sub-structures).
2501 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002502static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002503 struct pci_dev *pci_dev, struct net_device *net_dev)
2504{
Ben Hutchings46426102010-09-10 06:42:33 +00002505 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002506
2507 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002508 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002509#ifdef CONFIG_SFC_MTD
2510 INIT_LIST_HEAD(&efx->mtd_list);
2511#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002512 INIT_WORK(&efx->reset_work, efx_reset_work);
2513 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002514 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002515 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002516 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002517 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002518 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002519
2520 efx->net_dev = net_dev;
Jon Cooper43a37392012-10-18 15:49:54 +01002521 efx->rx_prefix_size = efx->type->rx_prefix_size;
2522 efx->rx_packet_hash_offset =
2523 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002524 spin_lock_init(&efx->stats_lock);
2525 mutex_init(&efx->mac_lock);
2526 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002527 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002528 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002529 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002530
2531 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002532 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2533 if (!efx->channel[i])
2534 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002535 efx->msi_context[i].efx = efx;
2536 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002537 }
2538
Ben Hutchings8ceee662008-04-27 12:55:59 +01002539 /* Higher numbered interrupt modes are less capable! */
2540 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2541 interrupt_mode);
2542
Ben Hutchings6977dc62008-12-26 13:44:39 -08002543 /* Would be good to use the net_dev name, but we're too early */
2544 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2545 pci_name(pci_dev));
2546 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002547 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002548 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002549
Ben Hutchings8ceee662008-04-27 12:55:59 +01002550 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002551
2552fail:
2553 efx_fini_struct(efx);
2554 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002555}
2556
2557static void efx_fini_struct(struct efx_nic *efx)
2558{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002559 int i;
2560
2561 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2562 kfree(efx->channel[i]);
2563
Ben Hutchings8ceee662008-04-27 12:55:59 +01002564 if (efx->workqueue) {
2565 destroy_workqueue(efx->workqueue);
2566 efx->workqueue = NULL;
2567 }
2568}
2569
2570/**************************************************************************
2571 *
2572 * PCI interface
2573 *
2574 **************************************************************************/
2575
2576/* Main body of final NIC shutdown code
2577 * This is called only at module unload (or hotplug removal).
2578 */
2579static void efx_pci_remove_main(struct efx_nic *efx)
2580{
Ben Hutchings7153f622012-07-27 20:50:52 +01002581 /* Flush reset_work. It can no longer be scheduled since we
2582 * are not READY.
2583 */
2584 BUG_ON(efx->state == STATE_READY);
2585 cancel_work_sync(&efx->reset_work);
2586
Ben Hutchingsd8291182012-10-05 23:35:41 +01002587 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002588 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002589 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002590 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002591 efx_fini_napi(efx);
2592 efx_remove_all(efx);
2593}
2594
2595/* Final NIC shutdown
2596 * This is called only at module unload (or hotplug removal).
2597 */
2598static void efx_pci_remove(struct pci_dev *pci_dev)
2599{
2600 struct efx_nic *efx;
2601
2602 efx = pci_get_drvdata(pci_dev);
2603 if (!efx)
2604 return;
2605
2606 /* Mark the NIC as fini, then stop the interface */
2607 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002608 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002609 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002610 rtnl_unlock();
2611
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002612 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002613 efx_unregister_netdev(efx);
2614
Ben Hutchings7dde5962008-12-12 22:09:38 -08002615 efx_mtd_remove(efx);
2616
Ben Hutchings8ceee662008-04-27 12:55:59 +01002617 efx_pci_remove_main(efx);
2618
Ben Hutchings8ceee662008-04-27 12:55:59 +01002619 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002620 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002621
Ben Hutchings8ceee662008-04-27 12:55:59 +01002622 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002623 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002624 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002625
2626 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002627};
2628
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002629/* NIC VPD information
2630 * Called during probe to display the part number of the
2631 * installed NIC. VPD is potentially very large but this should
2632 * always appear within the first 512 bytes.
2633 */
2634#define SFC_VPD_LEN 512
2635static void efx_print_product_vpd(struct efx_nic *efx)
2636{
2637 struct pci_dev *dev = efx->pci_dev;
2638 char vpd_data[SFC_VPD_LEN];
2639 ssize_t vpd_size;
2640 int i, j;
2641
2642 /* Get the vpd data from the device */
2643 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2644 if (vpd_size <= 0) {
2645 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2646 return;
2647 }
2648
2649 /* Get the Read only section */
2650 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2651 if (i < 0) {
2652 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2653 return;
2654 }
2655
2656 j = pci_vpd_lrdt_size(&vpd_data[i]);
2657 i += PCI_VPD_LRDT_TAG_SIZE;
2658 if (i + j > vpd_size)
2659 j = vpd_size - i;
2660
2661 /* Get the Part number */
2662 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2663 if (i < 0) {
2664 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2665 return;
2666 }
2667
2668 j = pci_vpd_info_field_size(&vpd_data[i]);
2669 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2670 if (i + j > vpd_size) {
2671 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2672 return;
2673 }
2674
2675 netif_info(efx, drv, efx->net_dev,
2676 "Part Number : %.*s\n", j, &vpd_data[i]);
2677}
2678
2679
Ben Hutchings8ceee662008-04-27 12:55:59 +01002680/* Main body of NIC initialisation
2681 * This is called at module load (or hotplug insertion, theoretically).
2682 */
2683static int efx_pci_probe_main(struct efx_nic *efx)
2684{
2685 int rc;
2686
2687 /* Do start-of-day initialisation */
2688 rc = efx_probe_all(efx);
2689 if (rc)
2690 goto fail1;
2691
Ben Hutchingse8f14992010-12-07 19:47:34 +00002692 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002693
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002694 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002695 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002696 netif_err(efx, probe, efx->net_dev,
2697 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002698 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002699 }
2700
2701 rc = efx_init_port(efx);
2702 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002703 netif_err(efx, probe, efx->net_dev,
2704 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002705 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002706 }
2707
Ben Hutchings152b6a62009-11-29 03:43:56 +00002708 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002709 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002710 goto fail5;
Jon Cooper261e4d92013-04-15 18:51:54 +01002711 rc = efx_enable_interrupts(efx);
2712 if (rc)
2713 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002714
2715 return 0;
2716
Jon Cooper261e4d92013-04-15 18:51:54 +01002717 fail6:
2718 efx_nic_fini_interrupt(efx);
Ben Hutchings278c0622009-11-23 16:05:12 +00002719 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002720 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002721 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002722 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002723 fail3:
2724 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002725 efx_remove_all(efx);
2726 fail1:
2727 return rc;
2728}
2729
2730/* NIC initialisation
2731 *
2732 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002733 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002734 * sets up and registers the network devices with the kernel and hooks
2735 * the interrupt service routine. It does not prepare the device for
2736 * transmission; this is left to the first time one of the network
2737 * interfaces is brought up (i.e. efx_net_open).
2738 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002739static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002740 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002741{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002742 struct net_device *net_dev;
2743 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002744 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002745
2746 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002747 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2748 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002749 if (!net_dev)
2750 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002751 efx = netdev_priv(net_dev);
2752 efx->type = (const struct efx_nic_type *) entry->driver_data;
2753 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002754 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002755 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002756 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002757 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002758 /* Mask for features that also apply to VLAN devices */
2759 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002760 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2761 NETIF_F_RXCSUM);
2762 /* All offloads can be toggled */
2763 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002764 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002765 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002766 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002767 if (rc)
2768 goto fail1;
2769
Ben Hutchings62776d02010-06-23 11:30:07 +00002770 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002771 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002772
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002773 efx_print_product_vpd(efx);
2774
Ben Hutchings8ceee662008-04-27 12:55:59 +01002775 /* Set up basic I/O (BAR mappings etc) */
2776 rc = efx_init_io(efx);
2777 if (rc)
2778 goto fail2;
2779
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002780 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002781 if (rc)
2782 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002783
Ben Hutchings8ceee662008-04-27 12:55:59 +01002784 rc = efx_register_netdev(efx);
2785 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002786 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002787
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002788 rc = efx_sriov_init(efx);
2789 if (rc)
2790 netif_err(efx, probe, efx->net_dev,
2791 "SR-IOV can't be enabled rc %d\n", rc);
2792
Ben Hutchings62776d02010-06-23 11:30:07 +00002793 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002794
Ben Hutchings7c431612012-01-27 17:23:58 +00002795 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002796 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002797 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002798 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002799 if (rc)
2800 netif_warn(efx, probe, efx->net_dev,
2801 "failed to create MTDs (%d)\n", rc);
2802
Alexandre Rames626950d2013-01-14 17:20:22 +00002803 rc = pci_enable_pcie_error_reporting(pci_dev);
2804 if (rc && rc != -EINVAL)
2805 netif_warn(efx, probe, efx->net_dev,
2806 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2807
Ben Hutchings8ceee662008-04-27 12:55:59 +01002808 return 0;
2809
Ben Hutchings8ceee662008-04-27 12:55:59 +01002810 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002811 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002812 fail3:
2813 efx_fini_io(efx);
2814 fail2:
2815 efx_fini_struct(efx);
2816 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002817 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002818 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002819 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002820 free_netdev(net_dev);
2821 return rc;
2822}
2823
Ben Hutchings89c758f2009-11-29 03:43:07 +00002824static int efx_pm_freeze(struct device *dev)
2825{
2826 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2827
Ben Hutchings61da0262012-07-27 19:35:39 +01002828 rtnl_lock();
2829
Ben Hutchings6032fb52012-07-27 19:35:47 +01002830 if (efx->state != STATE_DISABLED) {
2831 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002832
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002833 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002834
Ben Hutchings6032fb52012-07-27 19:35:47 +01002835 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002836 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01002837 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002838
Ben Hutchings61da0262012-07-27 19:35:39 +01002839 rtnl_unlock();
2840
Ben Hutchings89c758f2009-11-29 03:43:07 +00002841 return 0;
2842}
2843
2844static int efx_pm_thaw(struct device *dev)
2845{
Jon Cooper261e4d92013-04-15 18:51:54 +01002846 int rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002847 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2848
Ben Hutchings61da0262012-07-27 19:35:39 +01002849 rtnl_lock();
2850
Ben Hutchings6032fb52012-07-27 19:35:47 +01002851 if (efx->state != STATE_DISABLED) {
Jon Cooper261e4d92013-04-15 18:51:54 +01002852 rc = efx_enable_interrupts(efx);
2853 if (rc)
2854 goto fail;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002855
Ben Hutchings6032fb52012-07-27 19:35:47 +01002856 mutex_lock(&efx->mac_lock);
2857 efx->phy_op->reconfigure(efx);
2858 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002859
Ben Hutchings6032fb52012-07-27 19:35:47 +01002860 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002861
Ben Hutchings6032fb52012-07-27 19:35:47 +01002862 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002863
Ben Hutchings6032fb52012-07-27 19:35:47 +01002864 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002865
Ben Hutchings6032fb52012-07-27 19:35:47 +01002866 efx->type->resume_wol(efx);
2867 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002868
Ben Hutchings61da0262012-07-27 19:35:39 +01002869 rtnl_unlock();
2870
Steve Hodgson319ba642010-06-01 11:17:24 +00002871 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2872 queue_work(reset_workqueue, &efx->reset_work);
2873
Ben Hutchings89c758f2009-11-29 03:43:07 +00002874 return 0;
Jon Cooper261e4d92013-04-15 18:51:54 +01002875
2876fail:
2877 rtnl_unlock();
2878
2879 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002880}
2881
2882static int efx_pm_poweroff(struct device *dev)
2883{
2884 struct pci_dev *pci_dev = to_pci_dev(dev);
2885 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2886
2887 efx->type->fini(efx);
2888
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002889 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002890
2891 pci_save_state(pci_dev);
2892 return pci_set_power_state(pci_dev, PCI_D3hot);
2893}
2894
2895/* Used for both resume and restore */
2896static int efx_pm_resume(struct device *dev)
2897{
2898 struct pci_dev *pci_dev = to_pci_dev(dev);
2899 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2900 int rc;
2901
2902 rc = pci_set_power_state(pci_dev, PCI_D0);
2903 if (rc)
2904 return rc;
2905 pci_restore_state(pci_dev);
2906 rc = pci_enable_device(pci_dev);
2907 if (rc)
2908 return rc;
2909 pci_set_master(efx->pci_dev);
2910 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2911 if (rc)
2912 return rc;
2913 rc = efx->type->init(efx);
2914 if (rc)
2915 return rc;
Jon Cooper261e4d92013-04-15 18:51:54 +01002916 rc = efx_pm_thaw(dev);
2917 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002918}
2919
2920static int efx_pm_suspend(struct device *dev)
2921{
2922 int rc;
2923
2924 efx_pm_freeze(dev);
2925 rc = efx_pm_poweroff(dev);
2926 if (rc)
2927 efx_pm_resume(dev);
2928 return rc;
2929}
2930
Ben Hutchings18e83e42012-01-05 19:05:20 +00002931static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002932 .suspend = efx_pm_suspend,
2933 .resume = efx_pm_resume,
2934 .freeze = efx_pm_freeze,
2935 .thaw = efx_pm_thaw,
2936 .poweroff = efx_pm_poweroff,
2937 .restore = efx_pm_resume,
2938};
2939
Alexandre Rames626950d2013-01-14 17:20:22 +00002940/* A PCI error affecting this device was detected.
2941 * At this point MMIO and DMA may be disabled.
2942 * Stop the software path and request a slot reset.
2943 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002944static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2945 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00002946{
2947 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2948 struct efx_nic *efx = pci_get_drvdata(pdev);
2949
2950 if (state == pci_channel_io_perm_failure)
2951 return PCI_ERS_RESULT_DISCONNECT;
2952
2953 rtnl_lock();
2954
2955 if (efx->state != STATE_DISABLED) {
2956 efx->state = STATE_RECOVERY;
2957 efx->reset_pending = 0;
2958
2959 efx_device_detach_sync(efx);
2960
2961 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002962 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00002963
2964 status = PCI_ERS_RESULT_NEED_RESET;
2965 } else {
2966 /* If the interface is disabled we don't want to do anything
2967 * with it.
2968 */
2969 status = PCI_ERS_RESULT_RECOVERED;
2970 }
2971
2972 rtnl_unlock();
2973
2974 pci_disable_device(pdev);
2975
2976 return status;
2977}
2978
2979/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002980static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00002981{
2982 struct efx_nic *efx = pci_get_drvdata(pdev);
2983 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2984 int rc;
2985
2986 if (pci_enable_device(pdev)) {
2987 netif_err(efx, hw, efx->net_dev,
2988 "Cannot re-enable PCI device after reset.\n");
2989 status = PCI_ERS_RESULT_DISCONNECT;
2990 }
2991
2992 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2993 if (rc) {
2994 netif_err(efx, hw, efx->net_dev,
2995 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2996 /* Non-fatal error. Continue. */
2997 }
2998
2999 return status;
3000}
3001
3002/* Perform the actual reset and resume I/O operations. */
3003static void efx_io_resume(struct pci_dev *pdev)
3004{
3005 struct efx_nic *efx = pci_get_drvdata(pdev);
3006 int rc;
3007
3008 rtnl_lock();
3009
3010 if (efx->state == STATE_DISABLED)
3011 goto out;
3012
3013 rc = efx_reset(efx, RESET_TYPE_ALL);
3014 if (rc) {
3015 netif_err(efx, hw, efx->net_dev,
3016 "efx_reset failed after PCI error (%d)\n", rc);
3017 } else {
3018 efx->state = STATE_READY;
3019 netif_dbg(efx, hw, efx->net_dev,
3020 "Done resetting and resuming IO after PCI error.\n");
3021 }
3022
3023out:
3024 rtnl_unlock();
3025}
3026
3027/* For simplicity and reliability, we always require a slot reset and try to
3028 * reset the hardware when a pci error affecting the device is detected.
3029 * We leave both the link_reset and mmio_enabled callback unimplemented:
3030 * with our request for slot reset the mmio_enabled callback will never be
3031 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3032 */
3033static struct pci_error_handlers efx_err_handlers = {
3034 .error_detected = efx_io_error_detected,
3035 .slot_reset = efx_io_slot_reset,
3036 .resume = efx_io_resume,
3037};
3038
Ben Hutchings8ceee662008-04-27 12:55:59 +01003039static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003040 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003041 .id_table = efx_pci_table,
3042 .probe = efx_pci_probe,
3043 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003044 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003045 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003046};
3047
3048/**************************************************************************
3049 *
3050 * Kernel module interface
3051 *
3052 *************************************************************************/
3053
3054module_param(interrupt_mode, uint, 0444);
3055MODULE_PARM_DESC(interrupt_mode,
3056 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3057
3058static int __init efx_init_module(void)
3059{
3060 int rc;
3061
3062 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3063
3064 rc = register_netdevice_notifier(&efx_netdev_notifier);
3065 if (rc)
3066 goto err_notifier;
3067
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003068 rc = efx_init_sriov();
3069 if (rc)
3070 goto err_sriov;
3071
Steve Hodgson1ab00622008-12-12 21:33:02 -08003072 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3073 if (!reset_workqueue) {
3074 rc = -ENOMEM;
3075 goto err_reset;
3076 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003077
3078 rc = pci_register_driver(&efx_pci_driver);
3079 if (rc < 0)
3080 goto err_pci;
3081
3082 return 0;
3083
3084 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003085 destroy_workqueue(reset_workqueue);
3086 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003087 efx_fini_sriov();
3088 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003089 unregister_netdevice_notifier(&efx_netdev_notifier);
3090 err_notifier:
3091 return rc;
3092}
3093
3094static void __exit efx_exit_module(void)
3095{
3096 printk(KERN_INFO "Solarflare NET driver unloading\n");
3097
3098 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003099 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003100 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003101 unregister_netdevice_notifier(&efx_netdev_notifier);
3102
3103}
3104
3105module_init(efx_init_module);
3106module_exit(efx_exit_module);
3107
Ben Hutchings906bb262009-11-29 15:16:19 +00003108MODULE_AUTHOR("Solarflare Communications and "
3109 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003110MODULE_DESCRIPTION("Solarflare Communications network driver");
3111MODULE_LICENSE("GPL");
3112MODULE_DEVICE_TABLE(pci, efx_pci_table);