blob: fd844b53e38565cffa3e267fe0229f01f522a0d6 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01002 * Driver for Solarflare network controllers and boards
Ben Hutchings8ceee662008-04-27 12:55:59 +01003 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01004 * Copyright 2005-2013 Solarflare Communications Inc.
Ben Hutchings8ceee662008-04-27 12:55:59 +01005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/delay.h>
16#include <linux/notifier.h>
17#include <linux/ip.h>
18#include <linux/tcp.h>
19#include <linux/in.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010020#include <linux/ethtool.h>
Ben Hutchingsaa6ef272008-07-18 19:03:10 +010021#include <linux/topology.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Alexandre Rames626950d2013-01-14 17:20:22 +000023#include <linux/aer.h>
Alexandre Ramesb28405b2013-03-21 16:41:43 +000024#include <linux/interrupt.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010025#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000027#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000028#include "selftest.h"
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) +
Andrew Rybchenko2ec03012013-11-16 11:02:27 +0400588 efx->rx_ip_align + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000589 if (rx_buf_len <= PAGE_SIZE) {
Jon Coopere8c68c02013-03-08 10:18:28 +0000590 efx->rx_scatter = efx->type->always_rx_scatter;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000591 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
Jon Coopere8c68c02013-03-08 10:18:28 +0000618 /* RX filters may also have scatter-enabled flags */
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000619 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) {
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100634 efx_for_each_channel_tx_queue(tx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100635 efx_init_tx_queue(tx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100636 atomic_inc(&efx->active_queues);
637 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100638
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000639 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100640 efx_init_rx_queue(rx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100641 atomic_inc(&efx->active_queues);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000642 efx_nic_generate_fill_event(rx_queue);
643 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100644
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000645 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100646 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000647
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000648 efx_ptp_start_datapath(efx);
649
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000650 if (netif_device_present(efx->net_dev))
651 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100652}
653
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000654static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100655{
656 struct efx_channel *channel;
657 struct efx_tx_queue *tx_queue;
658 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100659 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100660
661 EFX_ASSERT_RESET_SERIALISED(efx);
662 BUG_ON(efx->port_enabled);
663
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000664 efx_ptp_stop_datapath(efx);
665
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100666 /* Stop RX refill */
667 efx_for_each_channel(channel, efx) {
668 efx_for_each_channel_rx_queue(rx_queue, channel)
669 rx_queue->refill_enabled = false;
670 }
671
Ben Hutchings8ceee662008-04-27 12:55:59 +0100672 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000673 /* RX packet processing is pipelined, so wait for the
674 * NAPI handler to complete. At least event queue 0
675 * might be kept active by non-data events, so don't
676 * use napi_synchronize() but actually disable NAPI
677 * temporarily.
678 */
679 if (efx_channel_has_rx_queue(channel)) {
680 efx_stop_eventq(channel);
681 efx_start_eventq(channel);
682 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100683 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100684
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100685 rc = efx->type->fini_dmaq(efx);
686 if (rc && EFX_WORKAROUND_7803(efx)) {
687 /* Schedule a reset to recover from the flush failure. The
688 * descriptor caches reference memory we're about to free,
689 * but falcon_reconfigure_mac_wrapper() won't reconnect
690 * the MACs because of the pending reset.
691 */
692 netif_err(efx, drv, efx->net_dev,
693 "Resetting to recover from flush failure\n");
694 efx_schedule_reset(efx, RESET_TYPE_ALL);
695 } else if (rc) {
696 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
697 } else {
698 netif_dbg(efx, drv, efx->net_dev,
699 "successfully flushed all queues\n");
700 }
701
702 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100703 efx_for_each_channel_rx_queue(rx_queue, channel)
704 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000705 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100706 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100707 }
708}
709
710static void efx_remove_channel(struct efx_channel *channel)
711{
712 struct efx_tx_queue *tx_queue;
713 struct efx_rx_queue *rx_queue;
714
Ben Hutchings62776d02010-06-23 11:30:07 +0000715 netif_dbg(channel->efx, drv, channel->efx->net_dev,
716 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100717
718 efx_for_each_channel_rx_queue(rx_queue, channel)
719 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000720 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100721 efx_remove_tx_queue(tx_queue);
722 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100723 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100724}
725
Ben Hutchings46426102010-09-10 06:42:33 +0000726static void efx_remove_channels(struct efx_nic *efx)
727{
728 struct efx_channel *channel;
729
730 efx_for_each_channel(channel, efx)
731 efx_remove_channel(channel);
732}
733
734int
735efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
736{
737 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
738 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000739 unsigned i, next_buffer_table = 0;
Jon Cooper261e4d92013-04-15 18:51:54 +0100740 int rc, rc2;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100741
742 rc = efx_check_disabled(efx);
743 if (rc)
744 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000745
746 /* Not all channels should be reallocated. We must avoid
747 * reallocating their buffer table entries.
748 */
749 efx_for_each_channel(channel, efx) {
750 struct efx_rx_queue *rx_queue;
751 struct efx_tx_queue *tx_queue;
752
753 if (channel->type->copy)
754 continue;
755 next_buffer_table = max(next_buffer_table,
756 channel->eventq.index +
757 channel->eventq.entries);
758 efx_for_each_channel_rx_queue(rx_queue, channel)
759 next_buffer_table = max(next_buffer_table,
760 rx_queue->rxd.index +
761 rx_queue->rxd.entries);
762 efx_for_each_channel_tx_queue(tx_queue, channel)
763 next_buffer_table = max(next_buffer_table,
764 tx_queue->txd.index +
765 tx_queue->txd.entries);
766 }
Ben Hutchings46426102010-09-10 06:42:33 +0000767
Ben Hutchings29c69a42013-01-28 19:01:06 +0000768 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000769 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100770 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000771
Ben Hutchings7f967c02012-02-13 23:45:02 +0000772 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000773 memset(other_channel, 0, sizeof(other_channel));
774 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000775 channel = efx->channel[i];
776 if (channel->type->copy)
777 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000778 if (!channel) {
779 rc = -ENOMEM;
780 goto out;
781 }
782 other_channel[i] = channel;
783 }
784
785 /* Swap entry counts and channel pointers */
786 old_rxq_entries = efx->rxq_entries;
787 old_txq_entries = efx->txq_entries;
788 efx->rxq_entries = rxq_entries;
789 efx->txq_entries = txq_entries;
790 for (i = 0; i < efx->n_channels; i++) {
791 channel = efx->channel[i];
792 efx->channel[i] = other_channel[i];
793 other_channel[i] = channel;
794 }
795
Ben Hutchings7f967c02012-02-13 23:45:02 +0000796 /* Restart buffer table allocation */
797 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000798
Ben Hutchingse8f14992010-12-07 19:47:34 +0000799 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000800 channel = efx->channel[i];
801 if (!channel->type->copy)
802 continue;
803 rc = efx_probe_channel(channel);
804 if (rc)
805 goto rollback;
806 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000807 }
Ben Hutchings46426102010-09-10 06:42:33 +0000808
Ben Hutchings7f967c02012-02-13 23:45:02 +0000809out:
810 /* Destroy unused channel structures */
811 for (i = 0; i < efx->n_channels; i++) {
812 channel = other_channel[i];
813 if (channel && channel->type->copy) {
814 efx_fini_napi_channel(channel);
815 efx_remove_channel(channel);
816 kfree(channel);
817 }
818 }
819
Jon Cooper261e4d92013-04-15 18:51:54 +0100820 rc2 = efx_soft_enable_interrupts(efx);
821 if (rc2) {
822 rc = rc ? rc : rc2;
823 netif_err(efx, drv, efx->net_dev,
824 "unable to restart interrupts on channel reallocation\n");
825 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
826 } else {
827 efx_start_all(efx);
828 netif_device_attach(efx->net_dev);
829 }
Ben Hutchings46426102010-09-10 06:42:33 +0000830 return rc;
831
832rollback:
833 /* Swap back */
834 efx->rxq_entries = old_rxq_entries;
835 efx->txq_entries = old_txq_entries;
836 for (i = 0; i < efx->n_channels; i++) {
837 channel = efx->channel[i];
838 efx->channel[i] = other_channel[i];
839 other_channel[i] = channel;
840 }
841 goto out;
842}
843
Steve Hodgson90d683a2010-06-01 11:19:39 +0000844void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100845{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000846 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100847}
848
Ben Hutchings7f967c02012-02-13 23:45:02 +0000849static const struct efx_channel_type efx_default_channel_type = {
850 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100851 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000852 .get_name = efx_get_channel_name,
853 .copy = efx_copy_channel,
854 .keep_eventq = false,
855};
856
857int efx_channel_dummy_op_int(struct efx_channel *channel)
858{
859 return 0;
860}
861
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100862void efx_channel_dummy_op_void(struct efx_channel *channel)
863{
864}
865
Ben Hutchings8ceee662008-04-27 12:55:59 +0100866/**************************************************************************
867 *
868 * Port handling
869 *
870 **************************************************************************/
871
872/* This ensures that the kernel is kept informed (via
873 * netif_carrier_on/off) of the link status, and also maintains the
874 * link status's stop on the port's TX queue.
875 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000876void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100877{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000878 struct efx_link_state *link_state = &efx->link_state;
879
Ben Hutchings8ceee662008-04-27 12:55:59 +0100880 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
881 * that no events are triggered between unregister_netdev() and the
882 * driver unloading. A more general condition is that NETDEV_CHANGE
883 * can only be generated between NETDEV_UP and NETDEV_DOWN */
884 if (!netif_running(efx->net_dev))
885 return;
886
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000887 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100888 efx->n_link_state_changes++;
889
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000890 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100891 netif_carrier_on(efx->net_dev);
892 else
893 netif_carrier_off(efx->net_dev);
894 }
895
896 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000897 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000898 netif_info(efx, link, efx->net_dev,
Ben Hutchings964e6132012-11-19 23:08:22 +0000899 "link up at %uMbps %s-duplex (MTU %d)\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000900 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings964e6132012-11-19 23:08:22 +0000901 efx->net_dev->mtu);
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000902 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000903 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100904}
905
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000906void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
907{
908 efx->link_advertising = advertising;
909 if (advertising) {
910 if (advertising & ADVERTISED_Pause)
911 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
912 else
913 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
914 if (advertising & ADVERTISED_Asym_Pause)
915 efx->wanted_fc ^= EFX_FC_TX;
916 }
917}
918
David S. Millerb56269462011-05-17 17:53:22 -0400919void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000920{
921 efx->wanted_fc = wanted_fc;
922 if (efx->link_advertising) {
923 if (wanted_fc & EFX_FC_RX)
924 efx->link_advertising |= (ADVERTISED_Pause |
925 ADVERTISED_Asym_Pause);
926 else
927 efx->link_advertising &= ~(ADVERTISED_Pause |
928 ADVERTISED_Asym_Pause);
929 if (wanted_fc & EFX_FC_TX)
930 efx->link_advertising ^= ADVERTISED_Asym_Pause;
931 }
932}
933
Ben Hutchings115122a2009-03-04 09:52:52 +0000934static void efx_fini_port(struct efx_nic *efx);
935
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000936/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
937 * the MAC appropriately. All other PHY configuration changes are pushed
938 * through phy_op->set_settings(), and pushed asynchronously to the MAC
939 * through efx_monitor().
940 *
941 * Callers must hold the mac_lock
942 */
943int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100944{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000945 enum efx_phy_mode phy_mode;
946 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100947
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000948 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100949
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000950 /* Disable PHY transmit in mac level loopbacks */
951 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800952 if (LOOPBACK_INTERNAL(efx))
953 efx->phy_mode |= PHY_MODE_TX_DISABLED;
954 else
955 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800956
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000957 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800958
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000959 if (rc)
960 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100961
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000962 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100963}
964
965/* Reinitialise the MAC to pick up new PHY settings, even if the port is
966 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000967int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100968{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000969 int rc;
970
Ben Hutchings8ceee662008-04-27 12:55:59 +0100971 EFX_ASSERT_RESET_SERIALISED(efx);
972
973 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000974 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100975 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000976
977 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100978}
979
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000980/* Asynchronous work item for changing MAC promiscuity and multicast
981 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
982 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800983static void efx_mac_work(struct work_struct *data)
984{
985 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
986
987 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100988 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100989 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800990 mutex_unlock(&efx->mac_lock);
991}
992
Ben Hutchings8ceee662008-04-27 12:55:59 +0100993static int efx_probe_port(struct efx_nic *efx)
994{
995 int rc;
996
Ben Hutchings62776d02010-06-23 11:30:07 +0000997 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100998
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000999 if (phy_flash_cfg)
1000 efx->phy_mode = PHY_MODE_SPECIAL;
1001
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001002 /* Connect up MAC/PHY operations table */
1003 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001004 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001005 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001006
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001007 /* Initialise MAC address to permanent address */
1008 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001009
1010 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001011}
1012
1013static int efx_init_port(struct efx_nic *efx)
1014{
1015 int rc;
1016
Ben Hutchings62776d02010-06-23 11:30:07 +00001017 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001018
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001019 mutex_lock(&efx->mac_lock);
1020
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001021 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001022 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001023 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001024
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001025 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001026
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001027 /* Reconfigure the MAC before creating dma queues (required for
1028 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001029 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001030
1031 /* Ensure the PHY advertises the correct flow control settings */
1032 rc = efx->phy_op->reconfigure(efx);
1033 if (rc)
1034 goto fail2;
1035
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001036 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001037 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001038
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001039fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001040 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001041fail1:
1042 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001043 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001044}
1045
Ben Hutchings8ceee662008-04-27 12:55:59 +01001046static void efx_start_port(struct efx_nic *efx)
1047{
Ben Hutchings62776d02010-06-23 11:30:07 +00001048 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001049 BUG_ON(efx->port_enabled);
1050
1051 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001052 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001053
1054 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1055 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001056 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001057
Ben Hutchings8ceee662008-04-27 12:55:59 +01001058 mutex_unlock(&efx->mac_lock);
1059}
1060
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001061/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001062static void efx_stop_port(struct efx_nic *efx)
1063{
Ben Hutchings62776d02010-06-23 11:30:07 +00001064 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001065
1066 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001067 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001068 mutex_unlock(&efx->mac_lock);
1069
1070 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001071 netif_addr_lock_bh(efx->net_dev);
1072 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001073}
1074
1075static void efx_fini_port(struct efx_nic *efx)
1076{
Ben Hutchings62776d02010-06-23 11:30:07 +00001077 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001078
1079 if (!efx->port_initialized)
1080 return;
1081
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001082 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001083 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001084
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001085 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001086 efx_link_status_changed(efx);
1087}
1088
1089static void efx_remove_port(struct efx_nic *efx)
1090{
Ben Hutchings62776d02010-06-23 11:30:07 +00001091 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001092
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001093 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001094}
1095
1096/**************************************************************************
1097 *
1098 * NIC handling
1099 *
1100 **************************************************************************/
1101
1102/* This configures the PCI device to enable I/O and DMA. */
1103static int efx_init_io(struct efx_nic *efx)
1104{
1105 struct pci_dev *pci_dev = efx->pci_dev;
1106 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001107 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001108 int rc;
1109
Ben Hutchings62776d02010-06-23 11:30:07 +00001110 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001111
1112 rc = pci_enable_device(pci_dev);
1113 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001114 netif_err(efx, probe, efx->net_dev,
1115 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001116 goto fail1;
1117 }
1118
1119 pci_set_master(pci_dev);
1120
1121 /* Set the PCI DMA mask. Try all possibilities from our
1122 * genuine mask down to 32 bits, because some architectures
1123 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1124 * masks event though they reject 46 bit masks.
1125 */
1126 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001127 if (dma_supported(&pci_dev->dev, dma_mask)) {
Russell King9663ded2013-06-26 23:49:11 +01001128 rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001129 if (rc == 0)
1130 break;
1131 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001132 dma_mask >>= 1;
1133 }
1134 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001135 netif_err(efx, probe, efx->net_dev,
1136 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001137 goto fail2;
1138 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001139 netif_dbg(efx, probe, efx->net_dev,
1140 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001141
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001142 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1143 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001144 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001145 netif_err(efx, probe, efx->net_dev,
1146 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001147 rc = -EIO;
1148 goto fail3;
1149 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001150 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001151 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001152 netif_err(efx, probe, efx->net_dev,
1153 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001154 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001155 rc = -ENOMEM;
1156 goto fail4;
1157 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001158 netif_dbg(efx, probe, efx->net_dev,
1159 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001160 (unsigned long long)efx->membase_phys, mem_map_size,
1161 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001162
1163 return 0;
1164
1165 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001166 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001167 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001168 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001169 fail2:
1170 pci_disable_device(efx->pci_dev);
1171 fail1:
1172 return rc;
1173}
1174
1175static void efx_fini_io(struct efx_nic *efx)
1176{
Ben Hutchings62776d02010-06-23 11:30:07 +00001177 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001178
1179 if (efx->membase) {
1180 iounmap(efx->membase);
1181 efx->membase = NULL;
1182 }
1183
1184 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001185 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001186 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001187 }
1188
1189 pci_disable_device(efx->pci_dev);
1190}
1191
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001192static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001193{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001194 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001195 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001196 int cpu;
1197
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001198 if (rss_cpus) {
1199 count = rss_cpus;
1200 } else {
1201 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1202 netif_warn(efx, probe, efx->net_dev,
1203 "RSS disabled due to allocation failure\n");
1204 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001205 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001206
1207 count = 0;
1208 for_each_online_cpu(cpu) {
1209 if (!cpumask_test_cpu(cpu, thread_mask)) {
1210 ++count;
1211 cpumask_or(thread_mask, thread_mask,
1212 topology_thread_cpumask(cpu));
1213 }
1214 }
1215
1216 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001217 }
1218
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001219 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1220 * table entries that are inaccessible to VFs
1221 */
1222 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1223 count > efx_vf_size(efx)) {
1224 netif_warn(efx, probe, efx->net_dev,
1225 "Reducing number of RSS channels from %u to %u for "
1226 "VF support. Increase vf-msix-limit to use more "
1227 "channels on the PF.\n",
1228 count, efx_vf_size(efx));
1229 count = efx_vf_size(efx);
1230 }
1231
Ben Hutchings46123d02008-09-01 12:47:33 +01001232 return count;
1233}
1234
1235/* Probe the number and type of interrupts we are able to obtain, and
1236 * the resulting numbers of channels and RX queues.
1237 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001238static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001239{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001240 unsigned int extra_channels = 0;
1241 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001242 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001243
Ben Hutchings7f967c02012-02-13 23:45:02 +00001244 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1245 if (efx->extra_channel_type[i])
1246 ++extra_channels;
1247
Ben Hutchings8ceee662008-04-27 12:55:59 +01001248 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001249 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001250 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001251
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001252 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001253 if (separate_tx_channels)
1254 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001255 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001256 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001257
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001258 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001259 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001260 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001261 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001262 netif_err(efx, drv, efx->net_dev,
1263 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001264 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001265 netif_err(efx, drv, efx->net_dev,
1266 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001267 EFX_BUG_ON_PARANOID(rc >= n_channels);
1268 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001269 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001270 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001271 }
1272
1273 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001274 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001275 if (n_channels > extra_channels)
1276 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001277 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001278 efx->n_tx_channels = max(n_channels / 2, 1U);
1279 efx->n_rx_channels = max(n_channels -
1280 efx->n_tx_channels,
1281 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001282 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001283 efx->n_tx_channels = n_channels;
1284 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001285 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001286 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001287 efx_get_channel(efx, i)->irq =
1288 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001289 } else {
1290 /* Fall back to single channel MSI */
1291 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001292 netif_err(efx, drv, efx->net_dev,
1293 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001294 }
1295 }
1296
1297 /* Try single interrupt MSI */
1298 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001299 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001300 efx->n_rx_channels = 1;
1301 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001302 rc = pci_enable_msi(efx->pci_dev);
1303 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001304 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001305 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001306 netif_err(efx, drv, efx->net_dev,
1307 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001308 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1309 }
1310 }
1311
1312 /* Assume legacy interrupts */
1313 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001314 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001315 efx->n_rx_channels = 1;
1316 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001317 efx->legacy_irq = efx->pci_dev->irq;
1318 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001319
Ben Hutchings7f967c02012-02-13 23:45:02 +00001320 /* Assign extra channels if possible */
1321 j = efx->n_channels;
1322 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1323 if (!efx->extra_channel_type[i])
1324 continue;
1325 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1326 efx->n_channels <= extra_channels) {
1327 efx->extra_channel_type[i]->handle_no_channel(efx);
1328 } else {
1329 --j;
1330 efx_get_channel(efx, j)->type =
1331 efx->extra_channel_type[i];
1332 }
1333 }
1334
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001335 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001336 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001337 efx->n_rx_channels : efx_vf_size(efx));
1338
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001339 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001340}
1341
Jon Cooper261e4d92013-04-15 18:51:54 +01001342static int efx_soft_enable_interrupts(struct efx_nic *efx)
Ben Hutchingsd8291182012-10-05 23:35:41 +01001343{
Jon Cooper261e4d92013-04-15 18:51:54 +01001344 struct efx_channel *channel, *end_channel;
1345 int rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001346
1347 BUG_ON(efx->state == STATE_DISABLED);
1348
1349 efx->irq_soft_enabled = true;
1350 smp_wmb();
1351
1352 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001353 if (!channel->type->keep_eventq) {
1354 rc = efx_init_eventq(channel);
1355 if (rc)
1356 goto fail;
1357 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001358 efx_start_eventq(channel);
1359 }
1360
1361 efx_mcdi_mode_event(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +01001362
1363 return 0;
1364fail:
1365 end_channel = channel;
1366 efx_for_each_channel(channel, efx) {
1367 if (channel == end_channel)
1368 break;
1369 efx_stop_eventq(channel);
1370 if (!channel->type->keep_eventq)
1371 efx_fini_eventq(channel);
1372 }
1373
1374 return rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001375}
1376
1377static void efx_soft_disable_interrupts(struct efx_nic *efx)
1378{
1379 struct efx_channel *channel;
1380
1381 if (efx->state == STATE_DISABLED)
1382 return;
1383
1384 efx_mcdi_mode_poll(efx);
1385
1386 efx->irq_soft_enabled = false;
1387 smp_wmb();
1388
1389 if (efx->legacy_irq)
1390 synchronize_irq(efx->legacy_irq);
1391
1392 efx_for_each_channel(channel, efx) {
1393 if (channel->irq)
1394 synchronize_irq(channel->irq);
1395
1396 efx_stop_eventq(channel);
1397 if (!channel->type->keep_eventq)
1398 efx_fini_eventq(channel);
1399 }
Ben Hutchingscade7152013-08-27 23:12:31 +01001400
1401 /* Flush the asynchronous MCDI request queue */
1402 efx_mcdi_flush_async(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01001403}
1404
Jon Cooper261e4d92013-04-15 18:51:54 +01001405static int efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001406{
Jon Cooper261e4d92013-04-15 18:51:54 +01001407 struct efx_channel *channel, *end_channel;
1408 int rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001409
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001410 BUG_ON(efx->state == STATE_DISABLED);
1411
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001412 if (efx->eeh_disabled_legacy_irq) {
1413 enable_irq(efx->legacy_irq);
1414 efx->eeh_disabled_legacy_irq = false;
1415 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001416
Ben Hutchings86094f72013-08-21 19:51:04 +01001417 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001418
1419 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001420 if (channel->type->keep_eventq) {
1421 rc = efx_init_eventq(channel);
1422 if (rc)
1423 goto fail;
1424 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001425 }
1426
Jon Cooper261e4d92013-04-15 18:51:54 +01001427 rc = efx_soft_enable_interrupts(efx);
1428 if (rc)
1429 goto fail;
1430
1431 return 0;
1432
1433fail:
1434 end_channel = channel;
1435 efx_for_each_channel(channel, efx) {
1436 if (channel == end_channel)
1437 break;
1438 if (channel->type->keep_eventq)
1439 efx_fini_eventq(channel);
1440 }
1441
1442 efx->type->irq_disable_non_ev(efx);
1443
1444 return rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001445}
1446
Ben Hutchingsd8291182012-10-05 23:35:41 +01001447static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001448{
1449 struct efx_channel *channel;
1450
Ben Hutchingsd8291182012-10-05 23:35:41 +01001451 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001452
1453 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001454 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001455 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001456 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001457
Ben Hutchings86094f72013-08-21 19:51:04 +01001458 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001459}
1460
Ben Hutchings8ceee662008-04-27 12:55:59 +01001461static void efx_remove_interrupts(struct efx_nic *efx)
1462{
1463 struct efx_channel *channel;
1464
1465 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001466 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001467 channel->irq = 0;
1468 pci_disable_msi(efx->pci_dev);
1469 pci_disable_msix(efx->pci_dev);
1470
1471 /* Remove legacy interrupt */
1472 efx->legacy_irq = 0;
1473}
1474
Ben Hutchings8831da72008-09-01 12:47:48 +01001475static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001476{
Ben Hutchings602a5322011-05-16 17:32:39 +01001477 struct efx_channel *channel;
1478 struct efx_tx_queue *tx_queue;
1479
Ben Hutchings97653432011-01-12 18:26:56 +00001480 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001481 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001482
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001483 /* We need to mark which channels really have RX and TX
1484 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001485 * RX-only and TX-only channels.
1486 */
1487 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001488 if (channel->channel < efx->n_rx_channels)
1489 channel->rx_queue.core_index = channel->channel;
1490 else
1491 channel->rx_queue.core_index = -1;
1492
Ben Hutchings602a5322011-05-16 17:32:39 +01001493 efx_for_each_channel_tx_queue(tx_queue, channel)
1494 tx_queue->queue -= (efx->tx_channel_offset *
1495 EFX_TXQ_TYPES);
1496 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001497}
1498
1499static int efx_probe_nic(struct efx_nic *efx)
1500{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001501 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001502 int rc;
1503
Ben Hutchings62776d02010-06-23 11:30:07 +00001504 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001505
1506 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001507 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001508 if (rc)
1509 return rc;
1510
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001511 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001512 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001513 rc = efx_probe_interrupts(efx);
1514 if (rc)
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001515 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001516
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001517 rc = efx->type->dimension_resources(efx);
1518 if (rc)
1519 goto fail2;
Ben Hutchings28e47c42012-02-15 01:58:49 +00001520
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001521 if (efx->n_channels > 1)
1522 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001523 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001524 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001525 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001526
Ben Hutchings8831da72008-09-01 12:47:48 +01001527 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001528 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1529 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001530
1531 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001532 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1533 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001534
1535 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001536
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001537fail2:
1538 efx_remove_interrupts(efx);
1539fail1:
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001540 efx->type->remove(efx);
1541 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001542}
1543
1544static void efx_remove_nic(struct efx_nic *efx)
1545{
Ben Hutchings62776d02010-06-23 11:30:07 +00001546 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001547
1548 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001549 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001550}
1551
Ben Hutchingsadd72472012-11-08 01:46:53 +00001552static int efx_probe_filters(struct efx_nic *efx)
1553{
1554 int rc;
1555
1556 spin_lock_init(&efx->filter_lock);
1557
1558 rc = efx->type->filter_table_probe(efx);
1559 if (rc)
1560 return rc;
1561
1562#ifdef CONFIG_RFS_ACCEL
1563 if (efx->type->offload_features & NETIF_F_NTUPLE) {
1564 efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
1565 sizeof(*efx->rps_flow_id),
1566 GFP_KERNEL);
1567 if (!efx->rps_flow_id) {
1568 efx->type->filter_table_remove(efx);
1569 return -ENOMEM;
1570 }
1571 }
1572#endif
1573
1574 return 0;
1575}
1576
1577static void efx_remove_filters(struct efx_nic *efx)
1578{
1579#ifdef CONFIG_RFS_ACCEL
1580 kfree(efx->rps_flow_id);
1581#endif
1582 efx->type->filter_table_remove(efx);
1583}
1584
1585static void efx_restore_filters(struct efx_nic *efx)
1586{
1587 efx->type->filter_table_restore(efx);
1588}
1589
Ben Hutchings8ceee662008-04-27 12:55:59 +01001590/**************************************************************************
1591 *
1592 * NIC startup/shutdown
1593 *
1594 *************************************************************************/
1595
1596static int efx_probe_all(struct efx_nic *efx)
1597{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001598 int rc;
1599
Ben Hutchings8ceee662008-04-27 12:55:59 +01001600 rc = efx_probe_nic(efx);
1601 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001602 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001603 goto fail1;
1604 }
1605
Ben Hutchings8ceee662008-04-27 12:55:59 +01001606 rc = efx_probe_port(efx);
1607 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001608 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001609 goto fail2;
1610 }
1611
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001612 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1613 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1614 rc = -EINVAL;
1615 goto fail3;
1616 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001617 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001618
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001619 rc = efx_probe_filters(efx);
1620 if (rc) {
1621 netif_err(efx, probe, efx->net_dev,
1622 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001623 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001624 }
1625
Ben Hutchings7f967c02012-02-13 23:45:02 +00001626 rc = efx_probe_channels(efx);
1627 if (rc)
1628 goto fail4;
1629
Ben Hutchings8ceee662008-04-27 12:55:59 +01001630 return 0;
1631
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001632 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001633 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001634 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001635 efx_remove_port(efx);
1636 fail2:
1637 efx_remove_nic(efx);
1638 fail1:
1639 return rc;
1640}
1641
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001642/* If the interface is supposed to be running but is not, start
1643 * the hardware and software data path, regular activity for the port
1644 * (MAC statistics, link polling, etc.) and schedule the port to be
1645 * reconfigured. Interrupts must already be enabled. This function
1646 * is safe to call multiple times, so long as the NIC is not disabled.
1647 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001648 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001649static void efx_start_all(struct efx_nic *efx)
1650{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001651 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001652 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001653
1654 /* Check that it is appropriate to restart the interface. All
1655 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001656 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001657 return;
1658
Ben Hutchings8ceee662008-04-27 12:55:59 +01001659 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001660 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001661
Alexandre Rames626950d2013-01-14 17:20:22 +00001662 /* Start the hardware monitor if there is one */
1663 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001664 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1665 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001666
1667 /* If link state detection is normally event-driven, we have
1668 * to poll now because we could have missed a change
1669 */
1670 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001671 mutex_lock(&efx->mac_lock);
1672 if (efx->phy_op->poll(efx))
1673 efx_link_status_changed(efx);
1674 mutex_unlock(&efx->mac_lock);
1675 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001676
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001677 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001678}
1679
1680/* Flush all delayed work. Should only be called when no more delayed work
1681 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1682 * since we're holding the rtnl_lock at this point. */
1683static void efx_flush_all(struct efx_nic *efx)
1684{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001685 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001686 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001687 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001688 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001689 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001690}
1691
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001692/* Quiesce the hardware and software data path, and regular activity
1693 * for the port without bringing the link down. Safe to call multiple
1694 * times with the NIC in almost any state, but interrupts should be
1695 * enabled. Requires the RTNL lock.
1696 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001697static void efx_stop_all(struct efx_nic *efx)
1698{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001699 EFX_ASSERT_RESET_SERIALISED(efx);
1700
1701 /* port_enabled can be read safely under the rtnl lock */
1702 if (!efx->port_enabled)
1703 return;
1704
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001705 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001706 efx_stop_port(efx);
1707
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001708 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001709 efx_flush_all(efx);
1710
Ben Hutchings29c69a42013-01-28 19:01:06 +00001711 /* Stop the kernel transmit interface. This is only valid if
1712 * the device is stopped or detached; otherwise the watchdog
1713 * may fire immediately.
1714 */
1715 WARN_ON(netif_running(efx->net_dev) &&
1716 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001717 netif_tx_disable(efx->net_dev);
1718
1719 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001720}
1721
1722static void efx_remove_all(struct efx_nic *efx)
1723{
Ben Hutchings46426102010-09-10 06:42:33 +00001724 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001725 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001726 efx_remove_port(efx);
1727 efx_remove_nic(efx);
1728}
1729
Ben Hutchings8ceee662008-04-27 12:55:59 +01001730/**************************************************************************
1731 *
1732 * Interrupt moderation
1733 *
1734 **************************************************************************/
1735
Ben Hutchingscc180b62011-12-08 19:51:47 +00001736static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001737{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001738 if (usecs == 0)
1739 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001740 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001741 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001742 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001743}
1744
Ben Hutchings8ceee662008-04-27 12:55:59 +01001745/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001746int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1747 unsigned int rx_usecs, bool rx_adaptive,
1748 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001749{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001750 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001751 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1752 efx->timer_quantum_ns,
1753 1000);
1754 unsigned int tx_ticks;
1755 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001756
1757 EFX_ASSERT_RESET_SERIALISED(efx);
1758
Ben Hutchingscc180b62011-12-08 19:51:47 +00001759 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001760 return -EINVAL;
1761
Ben Hutchingscc180b62011-12-08 19:51:47 +00001762 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1763 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1764
Ben Hutchings9e393b32011-09-05 07:43:04 +00001765 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1766 !rx_may_override_tx) {
1767 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1768 "RX and TX IRQ moderation must be equal\n");
1769 return -EINVAL;
1770 }
1771
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001772 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001773 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001774 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001775 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001776 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001777 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001778 channel->irq_moderation = tx_ticks;
1779 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001780
1781 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001782}
1783
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001784void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1785 unsigned int *rx_usecs, bool *rx_adaptive)
1786{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001787 /* We must round up when converting ticks to microseconds
1788 * because we round down when converting the other way.
1789 */
1790
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001791 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001792 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1793 efx->timer_quantum_ns,
1794 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001795
1796 /* If channels are shared between RX and TX, so is IRQ
1797 * moderation. Otherwise, IRQ moderation is the same for all
1798 * TX channels and is not adaptive.
1799 */
1800 if (efx->tx_channel_offset == 0)
1801 *tx_usecs = *rx_usecs;
1802 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001803 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001804 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001805 efx->timer_quantum_ns,
1806 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001807}
1808
Ben Hutchings8ceee662008-04-27 12:55:59 +01001809/**************************************************************************
1810 *
1811 * Hardware monitor
1812 *
1813 **************************************************************************/
1814
Ben Hutchingse254c272010-09-20 08:44:10 +00001815/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001816static void efx_monitor(struct work_struct *data)
1817{
1818 struct efx_nic *efx = container_of(data, struct efx_nic,
1819 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001820
Ben Hutchings62776d02010-06-23 11:30:07 +00001821 netif_vdbg(efx, timer, efx->net_dev,
1822 "hardware monitor executing on CPU %d\n",
1823 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001824 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001825
Ben Hutchings8ceee662008-04-27 12:55:59 +01001826 /* If the mac_lock is already held then it is likely a port
1827 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001828 * most of the work of monitor() anyway. */
1829 if (mutex_trylock(&efx->mac_lock)) {
1830 if (efx->port_enabled)
1831 efx->type->monitor(efx);
1832 mutex_unlock(&efx->mac_lock);
1833 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001834
Ben Hutchings8ceee662008-04-27 12:55:59 +01001835 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1836 efx_monitor_interval);
1837}
1838
1839/**************************************************************************
1840 *
1841 * ioctls
1842 *
1843 *************************************************************************/
1844
1845/* Net device ioctl
1846 * Context: process, rtnl_lock() held.
1847 */
1848static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1849{
Ben Hutchings767e4682008-09-01 12:43:14 +01001850 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001851 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001852
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001853 if (cmd == SIOCSHWTSTAMP)
1854 return efx_ptp_ioctl(efx, ifr, cmd);
1855
Ben Hutchings68e7f452009-04-29 08:05:08 +00001856 /* Convert phy_id from older PRTAD/DEVAD format */
1857 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1858 (data->phy_id & 0xfc00) == 0x0400)
1859 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1860
1861 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001862}
1863
1864/**************************************************************************
1865 *
1866 * NAPI interface
1867 *
1868 **************************************************************************/
1869
Ben Hutchings7f967c02012-02-13 23:45:02 +00001870static void efx_init_napi_channel(struct efx_channel *channel)
1871{
1872 struct efx_nic *efx = channel->efx;
1873
1874 channel->napi_dev = efx->net_dev;
1875 netif_napi_add(channel->napi_dev, &channel->napi_str,
1876 efx_poll, napi_weight);
1877}
1878
Ben Hutchingse8f14992010-12-07 19:47:34 +00001879static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001880{
1881 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001882
Ben Hutchings7f967c02012-02-13 23:45:02 +00001883 efx_for_each_channel(channel, efx)
1884 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001885}
1886
1887static void efx_fini_napi_channel(struct efx_channel *channel)
1888{
1889 if (channel->napi_dev)
1890 netif_napi_del(&channel->napi_str);
1891 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001892}
1893
1894static void efx_fini_napi(struct efx_nic *efx)
1895{
1896 struct efx_channel *channel;
1897
Ben Hutchingse8f14992010-12-07 19:47:34 +00001898 efx_for_each_channel(channel, efx)
1899 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001900}
1901
1902/**************************************************************************
1903 *
1904 * Kernel netpoll interface
1905 *
1906 *************************************************************************/
1907
1908#ifdef CONFIG_NET_POLL_CONTROLLER
1909
1910/* Although in the common case interrupts will be disabled, this is not
1911 * guaranteed. However, all our work happens inside the NAPI callback,
1912 * so no locking is required.
1913 */
1914static void efx_netpoll(struct net_device *net_dev)
1915{
Ben Hutchings767e4682008-09-01 12:43:14 +01001916 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001917 struct efx_channel *channel;
1918
Ben Hutchings64ee3122008-09-01 12:47:38 +01001919 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001920 efx_schedule_channel(channel);
1921}
1922
1923#endif
1924
1925/**************************************************************************
1926 *
1927 * Kernel net device interface
1928 *
1929 *************************************************************************/
1930
1931/* Context: process, rtnl_lock() held. */
1932static int efx_net_open(struct net_device *net_dev)
1933{
Ben Hutchings767e4682008-09-01 12:43:14 +01001934 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001935 int rc;
1936
Ben Hutchings62776d02010-06-23 11:30:07 +00001937 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1938 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001939
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001940 rc = efx_check_disabled(efx);
1941 if (rc)
1942 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001943 if (efx->phy_mode & PHY_MODE_SPECIAL)
1944 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001945 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1946 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001947
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001948 /* Notify the kernel of the link state polled during driver load,
1949 * before the monitor starts running */
1950 efx_link_status_changed(efx);
1951
Ben Hutchings8ceee662008-04-27 12:55:59 +01001952 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001953 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001954 return 0;
1955}
1956
1957/* Context: process, rtnl_lock() held.
1958 * Note that the kernel will ignore our return code; this method
1959 * should really be a void.
1960 */
1961static int efx_net_stop(struct net_device *net_dev)
1962{
Ben Hutchings767e4682008-09-01 12:43:14 +01001963 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001964
Ben Hutchings62776d02010-06-23 11:30:07 +00001965 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1966 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001967
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001968 /* Stop the device and flush all the channels */
1969 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001970
1971 return 0;
1972}
1973
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001974/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001975static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1976 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001977{
Ben Hutchings767e4682008-09-01 12:43:14 +01001978 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001979
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001980 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00001981 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001982 spin_unlock_bh(&efx->stats_lock);
1983
Ben Hutchings8ceee662008-04-27 12:55:59 +01001984 return stats;
1985}
1986
1987/* Context: netif_tx_lock held, BHs disabled. */
1988static void efx_watchdog(struct net_device *net_dev)
1989{
Ben Hutchings767e4682008-09-01 12:43:14 +01001990 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001991
Ben Hutchings62776d02010-06-23 11:30:07 +00001992 netif_err(efx, tx_err, efx->net_dev,
1993 "TX stuck with port_enabled=%d: resetting channels\n",
1994 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001995
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001996 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001997}
1998
1999
2000/* Context: process, rtnl_lock() held. */
2001static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2002{
Ben Hutchings767e4682008-09-01 12:43:14 +01002003 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002004 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002005
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002006 rc = efx_check_disabled(efx);
2007 if (rc)
2008 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002009 if (new_mtu > EFX_MAX_MTU)
2010 return -EINVAL;
2011
Ben Hutchings62776d02010-06-23 11:30:07 +00002012 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002013
Ben Hutchings29c69a42013-01-28 19:01:06 +00002014 efx_device_detach_sync(efx);
2015 efx_stop_all(efx);
2016
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002017 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002018 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01002019 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002020 mutex_unlock(&efx->mac_lock);
2021
Ben Hutchings8ceee662008-04-27 12:55:59 +01002022 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00002023 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002024 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002025}
2026
2027static int efx_set_mac_address(struct net_device *net_dev, void *data)
2028{
Ben Hutchings767e4682008-09-01 12:43:14 +01002029 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002030 struct sockaddr *addr = data;
2031 char *new_addr = addr->sa_data;
2032
Ben Hutchings8ceee662008-04-27 12:55:59 +01002033 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002034 netif_err(efx, drv, efx->net_dev,
2035 "invalid ethernet MAC address requested: %pM\n",
2036 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002037 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002038 }
2039
2040 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002041 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002042
2043 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002044 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002045 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002046 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002047
2048 return 0;
2049}
2050
Ben Hutchingsa816f752008-09-01 12:49:12 +01002051/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002052static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002053{
Ben Hutchings767e4682008-09-01 12:43:14 +01002054 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002055
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002056 if (efx->port_enabled)
2057 queue_work(efx->workqueue, &efx->mac_work);
2058 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002059}
2060
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002061static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002062{
2063 struct efx_nic *efx = netdev_priv(net_dev);
2064
2065 /* If disabling RX n-tuple filtering, clear existing filters */
2066 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2067 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2068
2069 return 0;
2070}
2071
Ben Hutchings8127d662013-08-29 19:19:29 +01002072static const struct net_device_ops efx_farch_netdev_ops = {
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002073 .ndo_open = efx_net_open,
2074 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002075 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002076 .ndo_tx_timeout = efx_watchdog,
2077 .ndo_start_xmit = efx_hard_start_xmit,
2078 .ndo_validate_addr = eth_validate_addr,
2079 .ndo_do_ioctl = efx_ioctl,
2080 .ndo_change_mtu = efx_change_mtu,
2081 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002082 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002083 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002084#ifdef CONFIG_SFC_SRIOV
2085 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2086 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2087 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2088 .ndo_get_vf_config = efx_sriov_get_vf_config,
2089#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002090#ifdef CONFIG_NET_POLL_CONTROLLER
2091 .ndo_poll_controller = efx_netpoll,
2092#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002093 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002094#ifdef CONFIG_RFS_ACCEL
2095 .ndo_rx_flow_steer = efx_filter_rfs,
2096#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002097};
2098
Ben Hutchings8127d662013-08-29 19:19:29 +01002099static const struct net_device_ops efx_ef10_netdev_ops = {
2100 .ndo_open = efx_net_open,
2101 .ndo_stop = efx_net_stop,
2102 .ndo_get_stats64 = efx_net_stats,
2103 .ndo_tx_timeout = efx_watchdog,
2104 .ndo_start_xmit = efx_hard_start_xmit,
2105 .ndo_validate_addr = eth_validate_addr,
2106 .ndo_do_ioctl = efx_ioctl,
2107 .ndo_change_mtu = efx_change_mtu,
2108 .ndo_set_mac_address = efx_set_mac_address,
2109 .ndo_set_rx_mode = efx_set_rx_mode,
2110 .ndo_set_features = efx_set_features,
2111#ifdef CONFIG_NET_POLL_CONTROLLER
2112 .ndo_poll_controller = efx_netpoll,
2113#endif
2114#ifdef CONFIG_RFS_ACCEL
2115 .ndo_rx_flow_steer = efx_filter_rfs,
2116#endif
2117};
2118
Ben Hutchings7dde5962008-12-12 22:09:38 -08002119static void efx_update_name(struct efx_nic *efx)
2120{
2121 strcpy(efx->name, efx->net_dev->name);
2122 efx_mtd_rename(efx);
2123 efx_set_channel_names(efx);
2124}
2125
Ben Hutchings8ceee662008-04-27 12:55:59 +01002126static int efx_netdev_event(struct notifier_block *this,
2127 unsigned long event, void *ptr)
2128{
Jiri Pirko351638e2013-05-28 01:30:21 +00002129 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002130
Ben Hutchings8127d662013-08-29 19:19:29 +01002131 if ((net_dev->netdev_ops == &efx_farch_netdev_ops ||
2132 net_dev->netdev_ops == &efx_ef10_netdev_ops) &&
Ben Hutchings7dde5962008-12-12 22:09:38 -08002133 event == NETDEV_CHANGENAME)
2134 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002135
2136 return NOTIFY_DONE;
2137}
2138
2139static struct notifier_block efx_netdev_notifier = {
2140 .notifier_call = efx_netdev_event,
2141};
2142
Ben Hutchings06d5e192008-12-12 21:47:23 -08002143static ssize_t
2144show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2145{
2146 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2147 return sprintf(buf, "%d\n", efx->phy_type);
2148}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002149static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002150
Ben Hutchings8ceee662008-04-27 12:55:59 +01002151static int efx_register_netdev(struct efx_nic *efx)
2152{
2153 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002154 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002155 int rc;
2156
2157 net_dev->watchdog_timeo = 5 * HZ;
2158 net_dev->irq = efx->pci_dev->irq;
Ben Hutchings8127d662013-08-29 19:19:29 +01002159 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
2160 net_dev->netdev_ops = &efx_ef10_netdev_ops;
2161 net_dev->priv_flags |= IFF_UNICAST_FLT;
2162 } else {
2163 net_dev->netdev_ops = &efx_farch_netdev_ops;
2164 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002165 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002166 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002167
Ben Hutchings7dde5962008-12-12 22:09:38 -08002168 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002169
Ben Hutchings7153f622012-07-27 20:50:52 +01002170 /* Enable resets to be scheduled and check whether any were
2171 * already requested. If so, the NIC is probably hosed so we
2172 * abort.
2173 */
2174 efx->state = STATE_READY;
2175 smp_mb(); /* ensure we change state before checking reset_pending */
2176 if (efx->reset_pending) {
2177 netif_err(efx, probe, efx->net_dev,
2178 "aborting probe due to scheduled reset\n");
2179 rc = -EIO;
2180 goto fail_locked;
2181 }
2182
Ben Hutchingsaed06282009-08-26 08:16:27 +00002183 rc = dev_alloc_name(net_dev, net_dev->name);
2184 if (rc < 0)
2185 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002186 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002187
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002188 /* Always start with carrier off; PHY events will detect the link */
2189 netif_carrier_off(net_dev);
2190
Ben Hutchingsaed06282009-08-26 08:16:27 +00002191 rc = register_netdevice(net_dev);
2192 if (rc)
2193 goto fail_locked;
2194
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002195 efx_for_each_channel(channel, efx) {
2196 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002197 efx_for_each_channel_tx_queue(tx_queue, channel)
2198 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002199 }
2200
Ben Hutchings7dde5962008-12-12 22:09:38 -08002201 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002202
Ben Hutchings06d5e192008-12-12 21:47:23 -08002203 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2204 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002205 netif_err(efx, drv, efx->net_dev,
2206 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002207 goto fail_registered;
2208 }
2209
Ben Hutchings8ceee662008-04-27 12:55:59 +01002210 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002211
Ben Hutchings7153f622012-07-27 20:50:52 +01002212fail_registered:
2213 rtnl_lock();
2214 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002215fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002216 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002217 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002218 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002219 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002220}
2221
2222static void efx_unregister_netdev(struct efx_nic *efx)
2223{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002224 if (!efx->net_dev)
2225 return;
2226
Ben Hutchings767e4682008-09-01 12:43:14 +01002227 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002228
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002229 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2230 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002231
2232 rtnl_lock();
2233 unregister_netdevice(efx->net_dev);
2234 efx->state = STATE_UNINIT;
2235 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002236}
2237
2238/**************************************************************************
2239 *
2240 * Device reset and suspend
2241 *
2242 **************************************************************************/
2243
Ben Hutchings2467ca42008-09-01 12:48:50 +01002244/* Tears down the entire software state and most of the hardware state
2245 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002246void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002247{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002248 EFX_ASSERT_RESET_SERIALISED(efx);
2249
Ben Hutchings2467ca42008-09-01 12:48:50 +01002250 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002251 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002252
2253 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002254 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2255 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002256 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002257}
2258
Ben Hutchings2467ca42008-09-01 12:48:50 +01002259/* This function will always ensure that the locks acquired in
2260 * efx_reset_down() are released. A failure return code indicates
2261 * that we were unable to reinitialise the hardware, and the
2262 * driver should be disabled. If ok is false, then the rx and tx
2263 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002264int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002265{
2266 int rc;
2267
Ben Hutchings2467ca42008-09-01 12:48:50 +01002268 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002269
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002270 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002271 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002272 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002273 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002274 }
2275
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002276 if (!ok)
2277 goto fail;
2278
Steve Hodgson4b988282009-01-29 17:50:51 +00002279 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002280 rc = efx->phy_op->init(efx);
2281 if (rc)
2282 goto fail;
2283 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002284 netif_err(efx, drv, efx->net_dev,
2285 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002286 }
2287
Jon Cooper261e4d92013-04-15 18:51:54 +01002288 rc = efx_enable_interrupts(efx);
2289 if (rc)
2290 goto fail;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002291 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002292 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002293
2294 mutex_unlock(&efx->mac_lock);
2295
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002296 efx_start_all(efx);
2297
2298 return 0;
2299
2300fail:
2301 efx->port_initialized = false;
2302
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002303 mutex_unlock(&efx->mac_lock);
2304
Ben Hutchings8ceee662008-04-27 12:55:59 +01002305 return rc;
2306}
2307
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002308/* Reset the NIC using the specified method. Note that the reset may
2309 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002310 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002311 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002312 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002313int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002314{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002315 int rc, rc2;
2316 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002317
Ben Hutchings62776d02010-06-23 11:30:07 +00002318 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2319 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002320
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002321 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002322 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002323
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002324 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002325 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002326 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002327 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002328 }
2329
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002330 /* Clear flags for the scopes we covered. We assume the NIC and
2331 * driver are now quiescent so that there is no race here.
2332 */
2333 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002334
2335 /* Reinitialise bus-mastering, which may have been turned off before
2336 * the reset was scheduled. This is still appropriate, even in the
2337 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2338 * can respond to requests. */
2339 pci_set_master(efx->pci_dev);
2340
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002341out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002342 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002343 disabled = rc ||
2344 method == RESET_TYPE_DISABLE ||
2345 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002346 rc2 = efx_reset_up(efx, method, !disabled);
2347 if (rc2) {
2348 disabled = true;
2349 if (!rc)
2350 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002351 }
2352
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002353 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002354 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002355 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002356 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002357 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002358 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002359 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002360 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002361 return rc;
2362}
2363
Alexandre Rames626950d2013-01-14 17:20:22 +00002364/* Try recovery mechanisms.
2365 * For now only EEH is supported.
2366 * Returns 0 if the recovery mechanisms are unsuccessful.
2367 * Returns a non-zero value otherwise.
2368 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002369int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002370{
2371#ifdef CONFIG_EEH
2372 /* A PCI error can occur and not be seen by EEH because nothing
2373 * happens on the PCI bus. In this case the driver may fail and
2374 * schedule a 'recover or reset', leading to this recovery handler.
2375 * Manually call the eeh failure check function.
2376 */
2377 struct eeh_dev *eehdev =
2378 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2379
2380 if (eeh_dev_check_failure(eehdev)) {
2381 /* The EEH mechanisms will handle the error and reset the
2382 * device if necessary.
2383 */
2384 return 1;
2385 }
2386#endif
2387 return 0;
2388}
2389
Ben Hutchings8ceee662008-04-27 12:55:59 +01002390/* The worker thread exists so that code that cannot sleep can
2391 * schedule a reset for later.
2392 */
2393static void efx_reset_work(struct work_struct *data)
2394{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002395 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002396 unsigned long pending;
2397 enum reset_type method;
2398
2399 pending = ACCESS_ONCE(efx->reset_pending);
2400 method = fls(pending) - 1;
2401
2402 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2403 method == RESET_TYPE_RECOVER_OR_ALL) &&
2404 efx_try_recovery(efx))
2405 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002406
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002407 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002408 return;
2409
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002410 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002411
2412 /* We checked the state in efx_schedule_reset() but it may
2413 * have changed by now. Now that we have the RTNL lock,
2414 * it cannot change again.
2415 */
2416 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002417 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002418
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002419 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002420}
2421
2422void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2423{
2424 enum reset_type method;
2425
Alexandre Rames626950d2013-01-14 17:20:22 +00002426 if (efx->state == STATE_RECOVERY) {
2427 netif_dbg(efx, drv, efx->net_dev,
2428 "recovering: skip scheduling %s reset\n",
2429 RESET_TYPE(type));
2430 return;
2431 }
2432
Ben Hutchings8ceee662008-04-27 12:55:59 +01002433 switch (type) {
2434 case RESET_TYPE_INVISIBLE:
2435 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002436 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002437 case RESET_TYPE_WORLD:
2438 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002439 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002440 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002441 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2442 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002443 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002444 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002445 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002446 netif_dbg(efx, drv, efx->net_dev,
2447 "scheduling %s reset for %s\n",
2448 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002449 break;
2450 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002451
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002452 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002453 smp_mb(); /* ensure we change reset_pending before checking state */
2454
2455 /* If we're not READY then just leave the flags set as the cue
2456 * to abort probing or reschedule the reset later.
2457 */
2458 if (ACCESS_ONCE(efx->state) != STATE_READY)
2459 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002460
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002461 /* efx_process_channel() will no longer read events once a
2462 * reset is scheduled. So switch back to poll'd MCDI completions. */
2463 efx_mcdi_mode_poll(efx);
2464
Steve Hodgson1ab00622008-12-12 21:33:02 -08002465 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002466}
2467
2468/**************************************************************************
2469 *
2470 * List of NICs we support
2471 *
2472 **************************************************************************/
2473
2474/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002475static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002476 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2477 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002478 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002479 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2480 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002481 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002482 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002483 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002484 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002485 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8127d662013-08-29 19:19:29 +01002486 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
2487 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002488 {0} /* end of list */
2489};
2490
2491/**************************************************************************
2492 *
Ben Hutchings37594332009-11-23 16:05:45 +00002493 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002494 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002495 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002496 * Needed so all function pointers are valid and do not have to be tested
2497 * before use
2498 *
2499 **************************************************************************/
2500int efx_port_dummy_op_int(struct efx_nic *efx)
2501{
2502 return 0;
2503}
2504void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002505
2506static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002507{
2508 return false;
2509}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002510
stephen hemminger6c8c2512011-04-14 05:50:12 +00002511static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002512 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002513 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002514 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002515 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002516};
2517
Ben Hutchings8ceee662008-04-27 12:55:59 +01002518/**************************************************************************
2519 *
2520 * Data housekeeping
2521 *
2522 **************************************************************************/
2523
2524/* This zeroes out and then fills in the invariants in a struct
2525 * efx_nic (including all sub-structures).
2526 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002527static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002528 struct pci_dev *pci_dev, struct net_device *net_dev)
2529{
Ben Hutchings46426102010-09-10 06:42:33 +00002530 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002531
2532 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002533 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002534#ifdef CONFIG_SFC_MTD
2535 INIT_LIST_HEAD(&efx->mtd_list);
2536#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002537 INIT_WORK(&efx->reset_work, efx_reset_work);
2538 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002539 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002540 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002541 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002542 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002543 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002544
2545 efx->net_dev = net_dev;
Jon Cooper43a37392012-10-18 15:49:54 +01002546 efx->rx_prefix_size = efx->type->rx_prefix_size;
Andrew Rybchenko2ec03012013-11-16 11:02:27 +04002547 efx->rx_ip_align =
2548 NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
Jon Cooper43a37392012-10-18 15:49:54 +01002549 efx->rx_packet_hash_offset =
2550 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002551 spin_lock_init(&efx->stats_lock);
2552 mutex_init(&efx->mac_lock);
2553 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002554 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002555 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002556 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002557
2558 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002559 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2560 if (!efx->channel[i])
2561 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002562 efx->msi_context[i].efx = efx;
2563 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002564 }
2565
Ben Hutchings8ceee662008-04-27 12:55:59 +01002566 /* Higher numbered interrupt modes are less capable! */
2567 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2568 interrupt_mode);
2569
Ben Hutchings6977dc62008-12-26 13:44:39 -08002570 /* Would be good to use the net_dev name, but we're too early */
2571 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2572 pci_name(pci_dev));
2573 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002574 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002575 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002576
Ben Hutchings8ceee662008-04-27 12:55:59 +01002577 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002578
2579fail:
2580 efx_fini_struct(efx);
2581 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002582}
2583
2584static void efx_fini_struct(struct efx_nic *efx)
2585{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002586 int i;
2587
2588 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2589 kfree(efx->channel[i]);
2590
Ben Hutchings8ceee662008-04-27 12:55:59 +01002591 if (efx->workqueue) {
2592 destroy_workqueue(efx->workqueue);
2593 efx->workqueue = NULL;
2594 }
2595}
2596
2597/**************************************************************************
2598 *
2599 * PCI interface
2600 *
2601 **************************************************************************/
2602
2603/* Main body of final NIC shutdown code
2604 * This is called only at module unload (or hotplug removal).
2605 */
2606static void efx_pci_remove_main(struct efx_nic *efx)
2607{
Ben Hutchings7153f622012-07-27 20:50:52 +01002608 /* Flush reset_work. It can no longer be scheduled since we
2609 * are not READY.
2610 */
2611 BUG_ON(efx->state == STATE_READY);
2612 cancel_work_sync(&efx->reset_work);
2613
Ben Hutchingsd8291182012-10-05 23:35:41 +01002614 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002615 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002616 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002617 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002618 efx_fini_napi(efx);
2619 efx_remove_all(efx);
2620}
2621
2622/* Final NIC shutdown
2623 * This is called only at module unload (or hotplug removal).
2624 */
2625static void efx_pci_remove(struct pci_dev *pci_dev)
2626{
2627 struct efx_nic *efx;
2628
2629 efx = pci_get_drvdata(pci_dev);
2630 if (!efx)
2631 return;
2632
2633 /* Mark the NIC as fini, then stop the interface */
2634 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002635 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002636 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002637 rtnl_unlock();
2638
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002639 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002640 efx_unregister_netdev(efx);
2641
Ben Hutchings7dde5962008-12-12 22:09:38 -08002642 efx_mtd_remove(efx);
2643
Ben Hutchings8ceee662008-04-27 12:55:59 +01002644 efx_pci_remove_main(efx);
2645
Ben Hutchings8ceee662008-04-27 12:55:59 +01002646 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002647 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002648
Ben Hutchings8ceee662008-04-27 12:55:59 +01002649 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002650 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002651 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002652
2653 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002654};
2655
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002656/* NIC VPD information
2657 * Called during probe to display the part number of the
2658 * installed NIC. VPD is potentially very large but this should
2659 * always appear within the first 512 bytes.
2660 */
2661#define SFC_VPD_LEN 512
2662static void efx_print_product_vpd(struct efx_nic *efx)
2663{
2664 struct pci_dev *dev = efx->pci_dev;
2665 char vpd_data[SFC_VPD_LEN];
2666 ssize_t vpd_size;
2667 int i, j;
2668
2669 /* Get the vpd data from the device */
2670 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2671 if (vpd_size <= 0) {
2672 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2673 return;
2674 }
2675
2676 /* Get the Read only section */
2677 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2678 if (i < 0) {
2679 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2680 return;
2681 }
2682
2683 j = pci_vpd_lrdt_size(&vpd_data[i]);
2684 i += PCI_VPD_LRDT_TAG_SIZE;
2685 if (i + j > vpd_size)
2686 j = vpd_size - i;
2687
2688 /* Get the Part number */
2689 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2690 if (i < 0) {
2691 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2692 return;
2693 }
2694
2695 j = pci_vpd_info_field_size(&vpd_data[i]);
2696 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2697 if (i + j > vpd_size) {
2698 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2699 return;
2700 }
2701
2702 netif_info(efx, drv, efx->net_dev,
2703 "Part Number : %.*s\n", j, &vpd_data[i]);
2704}
2705
2706
Ben Hutchings8ceee662008-04-27 12:55:59 +01002707/* Main body of NIC initialisation
2708 * This is called at module load (or hotplug insertion, theoretically).
2709 */
2710static int efx_pci_probe_main(struct efx_nic *efx)
2711{
2712 int rc;
2713
2714 /* Do start-of-day initialisation */
2715 rc = efx_probe_all(efx);
2716 if (rc)
2717 goto fail1;
2718
Ben Hutchingse8f14992010-12-07 19:47:34 +00002719 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002720
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002721 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002722 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002723 netif_err(efx, probe, efx->net_dev,
2724 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002725 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002726 }
2727
2728 rc = efx_init_port(efx);
2729 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002730 netif_err(efx, probe, efx->net_dev,
2731 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002732 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002733 }
2734
Ben Hutchings152b6a62009-11-29 03:43:56 +00002735 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002736 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002737 goto fail5;
Jon Cooper261e4d92013-04-15 18:51:54 +01002738 rc = efx_enable_interrupts(efx);
2739 if (rc)
2740 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002741
2742 return 0;
2743
Jon Cooper261e4d92013-04-15 18:51:54 +01002744 fail6:
2745 efx_nic_fini_interrupt(efx);
Ben Hutchings278c0622009-11-23 16:05:12 +00002746 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002747 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002748 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002749 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002750 fail3:
2751 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002752 efx_remove_all(efx);
2753 fail1:
2754 return rc;
2755}
2756
2757/* NIC initialisation
2758 *
2759 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002760 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002761 * sets up and registers the network devices with the kernel and hooks
2762 * the interrupt service routine. It does not prepare the device for
2763 * transmission; this is left to the first time one of the network
2764 * interfaces is brought up (i.e. efx_net_open).
2765 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002766static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002767 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002768{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002769 struct net_device *net_dev;
2770 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002771 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002772
2773 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002774 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2775 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002776 if (!net_dev)
2777 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002778 efx = netdev_priv(net_dev);
2779 efx->type = (const struct efx_nic_type *) entry->driver_data;
2780 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002781 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002782 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002783 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002784 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002785 /* Mask for features that also apply to VLAN devices */
2786 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002787 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2788 NETIF_F_RXCSUM);
2789 /* All offloads can be toggled */
2790 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002791 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002792 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002793 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002794 if (rc)
2795 goto fail1;
2796
Ben Hutchings62776d02010-06-23 11:30:07 +00002797 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002798 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002799
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002800 efx_print_product_vpd(efx);
2801
Ben Hutchings8ceee662008-04-27 12:55:59 +01002802 /* Set up basic I/O (BAR mappings etc) */
2803 rc = efx_init_io(efx);
2804 if (rc)
2805 goto fail2;
2806
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002807 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002808 if (rc)
2809 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002810
Ben Hutchings8ceee662008-04-27 12:55:59 +01002811 rc = efx_register_netdev(efx);
2812 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002813 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002814
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002815 rc = efx_sriov_init(efx);
2816 if (rc)
2817 netif_err(efx, probe, efx->net_dev,
2818 "SR-IOV can't be enabled rc %d\n", rc);
2819
Ben Hutchings62776d02010-06-23 11:30:07 +00002820 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002821
Ben Hutchings7c431612012-01-27 17:23:58 +00002822 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002823 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002824 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002825 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002826 if (rc)
2827 netif_warn(efx, probe, efx->net_dev,
2828 "failed to create MTDs (%d)\n", rc);
2829
Alexandre Rames626950d2013-01-14 17:20:22 +00002830 rc = pci_enable_pcie_error_reporting(pci_dev);
2831 if (rc && rc != -EINVAL)
2832 netif_warn(efx, probe, efx->net_dev,
2833 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2834
Ben Hutchings8ceee662008-04-27 12:55:59 +01002835 return 0;
2836
Ben Hutchings8ceee662008-04-27 12:55:59 +01002837 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002838 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002839 fail3:
2840 efx_fini_io(efx);
2841 fail2:
2842 efx_fini_struct(efx);
2843 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002844 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002845 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002846 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002847 free_netdev(net_dev);
2848 return rc;
2849}
2850
Ben Hutchings89c758f2009-11-29 03:43:07 +00002851static int efx_pm_freeze(struct device *dev)
2852{
2853 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2854
Ben Hutchings61da0262012-07-27 19:35:39 +01002855 rtnl_lock();
2856
Ben Hutchings6032fb52012-07-27 19:35:47 +01002857 if (efx->state != STATE_DISABLED) {
2858 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002859
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002860 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002861
Ben Hutchings6032fb52012-07-27 19:35:47 +01002862 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002863 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01002864 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002865
Ben Hutchings61da0262012-07-27 19:35:39 +01002866 rtnl_unlock();
2867
Ben Hutchings89c758f2009-11-29 03:43:07 +00002868 return 0;
2869}
2870
2871static int efx_pm_thaw(struct device *dev)
2872{
Jon Cooper261e4d92013-04-15 18:51:54 +01002873 int rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002874 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2875
Ben Hutchings61da0262012-07-27 19:35:39 +01002876 rtnl_lock();
2877
Ben Hutchings6032fb52012-07-27 19:35:47 +01002878 if (efx->state != STATE_DISABLED) {
Jon Cooper261e4d92013-04-15 18:51:54 +01002879 rc = efx_enable_interrupts(efx);
2880 if (rc)
2881 goto fail;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002882
Ben Hutchings6032fb52012-07-27 19:35:47 +01002883 mutex_lock(&efx->mac_lock);
2884 efx->phy_op->reconfigure(efx);
2885 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002886
Ben Hutchings6032fb52012-07-27 19:35:47 +01002887 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002888
Ben Hutchings6032fb52012-07-27 19:35:47 +01002889 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002890
Ben Hutchings6032fb52012-07-27 19:35:47 +01002891 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002892
Ben Hutchings6032fb52012-07-27 19:35:47 +01002893 efx->type->resume_wol(efx);
2894 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002895
Ben Hutchings61da0262012-07-27 19:35:39 +01002896 rtnl_unlock();
2897
Steve Hodgson319ba642010-06-01 11:17:24 +00002898 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2899 queue_work(reset_workqueue, &efx->reset_work);
2900
Ben Hutchings89c758f2009-11-29 03:43:07 +00002901 return 0;
Jon Cooper261e4d92013-04-15 18:51:54 +01002902
2903fail:
2904 rtnl_unlock();
2905
2906 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002907}
2908
2909static int efx_pm_poweroff(struct device *dev)
2910{
2911 struct pci_dev *pci_dev = to_pci_dev(dev);
2912 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2913
2914 efx->type->fini(efx);
2915
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002916 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002917
2918 pci_save_state(pci_dev);
2919 return pci_set_power_state(pci_dev, PCI_D3hot);
2920}
2921
2922/* Used for both resume and restore */
2923static int efx_pm_resume(struct device *dev)
2924{
2925 struct pci_dev *pci_dev = to_pci_dev(dev);
2926 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2927 int rc;
2928
2929 rc = pci_set_power_state(pci_dev, PCI_D0);
2930 if (rc)
2931 return rc;
2932 pci_restore_state(pci_dev);
2933 rc = pci_enable_device(pci_dev);
2934 if (rc)
2935 return rc;
2936 pci_set_master(efx->pci_dev);
2937 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2938 if (rc)
2939 return rc;
2940 rc = efx->type->init(efx);
2941 if (rc)
2942 return rc;
Jon Cooper261e4d92013-04-15 18:51:54 +01002943 rc = efx_pm_thaw(dev);
2944 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002945}
2946
2947static int efx_pm_suspend(struct device *dev)
2948{
2949 int rc;
2950
2951 efx_pm_freeze(dev);
2952 rc = efx_pm_poweroff(dev);
2953 if (rc)
2954 efx_pm_resume(dev);
2955 return rc;
2956}
2957
Ben Hutchings18e83e42012-01-05 19:05:20 +00002958static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002959 .suspend = efx_pm_suspend,
2960 .resume = efx_pm_resume,
2961 .freeze = efx_pm_freeze,
2962 .thaw = efx_pm_thaw,
2963 .poweroff = efx_pm_poweroff,
2964 .restore = efx_pm_resume,
2965};
2966
Alexandre Rames626950d2013-01-14 17:20:22 +00002967/* A PCI error affecting this device was detected.
2968 * At this point MMIO and DMA may be disabled.
2969 * Stop the software path and request a slot reset.
2970 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002971static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2972 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00002973{
2974 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2975 struct efx_nic *efx = pci_get_drvdata(pdev);
2976
2977 if (state == pci_channel_io_perm_failure)
2978 return PCI_ERS_RESULT_DISCONNECT;
2979
2980 rtnl_lock();
2981
2982 if (efx->state != STATE_DISABLED) {
2983 efx->state = STATE_RECOVERY;
2984 efx->reset_pending = 0;
2985
2986 efx_device_detach_sync(efx);
2987
2988 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002989 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00002990
2991 status = PCI_ERS_RESULT_NEED_RESET;
2992 } else {
2993 /* If the interface is disabled we don't want to do anything
2994 * with it.
2995 */
2996 status = PCI_ERS_RESULT_RECOVERED;
2997 }
2998
2999 rtnl_unlock();
3000
3001 pci_disable_device(pdev);
3002
3003 return status;
3004}
3005
3006/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003007static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00003008{
3009 struct efx_nic *efx = pci_get_drvdata(pdev);
3010 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3011 int rc;
3012
3013 if (pci_enable_device(pdev)) {
3014 netif_err(efx, hw, efx->net_dev,
3015 "Cannot re-enable PCI device after reset.\n");
3016 status = PCI_ERS_RESULT_DISCONNECT;
3017 }
3018
3019 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
3020 if (rc) {
3021 netif_err(efx, hw, efx->net_dev,
3022 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3023 /* Non-fatal error. Continue. */
3024 }
3025
3026 return status;
3027}
3028
3029/* Perform the actual reset and resume I/O operations. */
3030static void efx_io_resume(struct pci_dev *pdev)
3031{
3032 struct efx_nic *efx = pci_get_drvdata(pdev);
3033 int rc;
3034
3035 rtnl_lock();
3036
3037 if (efx->state == STATE_DISABLED)
3038 goto out;
3039
3040 rc = efx_reset(efx, RESET_TYPE_ALL);
3041 if (rc) {
3042 netif_err(efx, hw, efx->net_dev,
3043 "efx_reset failed after PCI error (%d)\n", rc);
3044 } else {
3045 efx->state = STATE_READY;
3046 netif_dbg(efx, hw, efx->net_dev,
3047 "Done resetting and resuming IO after PCI error.\n");
3048 }
3049
3050out:
3051 rtnl_unlock();
3052}
3053
3054/* For simplicity and reliability, we always require a slot reset and try to
3055 * reset the hardware when a pci error affecting the device is detected.
3056 * We leave both the link_reset and mmio_enabled callback unimplemented:
3057 * with our request for slot reset the mmio_enabled callback will never be
3058 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3059 */
3060static struct pci_error_handlers efx_err_handlers = {
3061 .error_detected = efx_io_error_detected,
3062 .slot_reset = efx_io_slot_reset,
3063 .resume = efx_io_resume,
3064};
3065
Ben Hutchings8ceee662008-04-27 12:55:59 +01003066static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003067 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003068 .id_table = efx_pci_table,
3069 .probe = efx_pci_probe,
3070 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003071 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003072 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003073};
3074
3075/**************************************************************************
3076 *
3077 * Kernel module interface
3078 *
3079 *************************************************************************/
3080
3081module_param(interrupt_mode, uint, 0444);
3082MODULE_PARM_DESC(interrupt_mode,
3083 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3084
3085static int __init efx_init_module(void)
3086{
3087 int rc;
3088
3089 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3090
3091 rc = register_netdevice_notifier(&efx_netdev_notifier);
3092 if (rc)
3093 goto err_notifier;
3094
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003095 rc = efx_init_sriov();
3096 if (rc)
3097 goto err_sriov;
3098
Steve Hodgson1ab00622008-12-12 21:33:02 -08003099 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3100 if (!reset_workqueue) {
3101 rc = -ENOMEM;
3102 goto err_reset;
3103 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003104
3105 rc = pci_register_driver(&efx_pci_driver);
3106 if (rc < 0)
3107 goto err_pci;
3108
3109 return 0;
3110
3111 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003112 destroy_workqueue(reset_workqueue);
3113 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003114 efx_fini_sriov();
3115 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003116 unregister_netdevice_notifier(&efx_netdev_notifier);
3117 err_notifier:
3118 return rc;
3119}
3120
3121static void __exit efx_exit_module(void)
3122{
3123 printk(KERN_INFO "Solarflare NET driver unloading\n");
3124
3125 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003126 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003127 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003128 unregister_netdevice_notifier(&efx_netdev_notifier);
3129
3130}
3131
3132module_init(efx_init_module);
3133module_exit(efx_exit_module);
3134
Ben Hutchings906bb262009-11-29 15:16:19 +00003135MODULE_AUTHOR("Solarflare Communications and "
3136 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003137MODULE_DESCRIPTION("Solarflare Communications network driver");
3138MODULE_LICENSE("GPL");
3139MODULE_DEVICE_TABLE(pci, efx_pci_table);