blob: 34788fbb4c663d1763ff6f4f7ba2a009191ff930 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchings0a6f40c2011-02-25 00:01:34 +00004 * Copyright 2005-2011 Solarflare Communications Inc.
Ben Hutchings8ceee662008-04-27 12:55:59 +01005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/delay.h>
16#include <linux/notifier.h>
17#include <linux/ip.h>
18#include <linux/tcp.h>
19#include <linux/in.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010020#include <linux/ethtool.h>
Ben Hutchingsaa6ef272008-07-18 19:03:10 +010021#include <linux/topology.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Alexandre Rames626950d2013-01-14 17:20:22 +000023#include <linux/aer.h>
Alexandre Ramesb28405b2013-03-21 16:41:43 +000024#include <linux/interrupt.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010025#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000027#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000028#include "selftest.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010029
Ben Hutchings8880f4e2009-11-29 15:15:41 +000030#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000031#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000032
Ben Hutchingsc4593022009-11-23 16:08:17 +000033/**************************************************************************
34 *
35 * Type name strings
36 *
37 **************************************************************************
38 */
39
40/* Loopback mode names (see LOOPBACK_MODE()) */
41const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000042const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000043 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000044 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000045 [LOOPBACK_GMAC] = "GMAC",
46 [LOOPBACK_XGMII] = "XGMII",
47 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000048 [LOOPBACK_XAUI] = "XAUI",
49 [LOOPBACK_GMII] = "GMII",
50 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000051 [LOOPBACK_XGBR] = "XGBR",
52 [LOOPBACK_XFI] = "XFI",
53 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
54 [LOOPBACK_GMII_FAR] = "GMII_FAR",
55 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
56 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000057 [LOOPBACK_GPHY] = "GPHY",
58 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000059 [LOOPBACK_PCS] = "PCS",
60 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000061 [LOOPBACK_XPORT] = "XPORT",
62 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000063 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000064 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
65 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000066 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000067 [LOOPBACK_XFI_WS] = "XFI_WS",
68 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000069 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000070};
71
Ben Hutchingsc4593022009-11-23 16:08:17 +000072const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000073const char *const efx_reset_type_names[] = {
Alexandre Rames626950d2013-01-14 17:20:22 +000074 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
75 [RESET_TYPE_ALL] = "ALL",
76 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
77 [RESET_TYPE_WORLD] = "WORLD",
78 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
79 [RESET_TYPE_DISABLE] = "DISABLE",
80 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
81 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
82 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
Alexandre Rames3de82b92013-06-13 11:36:15 +010083 [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
Alexandre Rames626950d2013-01-14 17:20:22 +000084 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
85 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Ben Hutchingsc4593022009-11-23 16:08:17 +000086};
87
Steve Hodgson1ab00622008-12-12 21:33:02 -080088/* Reset workqueue. If any NIC has a hardware failure then a reset will be
89 * queued onto this work queue. This is not a per-nic work queue, because
90 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
91 */
92static struct workqueue_struct *reset_workqueue;
93
Ben Hutchings8ceee662008-04-27 12:55:59 +010094/**************************************************************************
95 *
96 * Configurable values
97 *
98 *************************************************************************/
99
100/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100101 * Use separate channels for TX and RX events
102 *
Neil Turton28b581a2008-12-12 21:41:06 -0800103 * Set this to 1 to use separate channels for TX and RX. It allows us
104 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100105 *
Neil Turton28b581a2008-12-12 21:41:06 -0800106 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100107 */
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000108static bool separate_tx_channels;
109module_param(separate_tx_channels, bool, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800110MODULE_PARM_DESC(separate_tx_channels,
111 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100112
113/* This is the weight assigned to each of the (per-channel) virtual
114 * NAPI devices.
115 */
116static int napi_weight = 64;
117
118/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000119 * monitor.
120 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000121 * - Check the on-board hardware monitor;
122 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000123 * On Siena-based NICs for power systems with EEH support, this will give EEH a
124 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100125 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000126static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100127
Ben Hutchings8ceee662008-04-27 12:55:59 +0100128/* Initial interrupt moderation settings. They can be modified after
129 * module load with ethtool.
130 *
131 * The default for RX should strike a balance between increasing the
132 * round-trip latency and reducing overhead.
133 */
134static unsigned int rx_irq_mod_usec = 60;
135
136/* Initial interrupt moderation settings. They can be modified after
137 * module load with ethtool.
138 *
139 * This default is chosen to ensure that a 10G link does not go idle
140 * while a TX queue is stopped after it has become full. A queue is
141 * restarted when it drops below half full. The time this takes (assuming
142 * worst case 3 descriptors per packet and 1024 descriptors) is
143 * 512 / 3 * 1.2 = 205 usec.
144 */
145static unsigned int tx_irq_mod_usec = 150;
146
147/* This is the first interrupt mode to try out of:
148 * 0 => MSI-X
149 * 1 => MSI
150 * 2 => legacy
151 */
152static unsigned int interrupt_mode;
153
154/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
155 * i.e. the number of CPUs among which we may distribute simultaneous
156 * interrupt handling.
157 *
158 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000159 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100160 */
161static unsigned int rss_cpus;
162module_param(rss_cpus, uint, 0444);
163MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
164
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000165static bool phy_flash_cfg;
166module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800167MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
168
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000169static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000170module_param(irq_adapt_low_thresh, uint, 0644);
171MODULE_PARM_DESC(irq_adapt_low_thresh,
172 "Threshold score for reducing IRQ moderation");
173
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000174static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000175module_param(irq_adapt_high_thresh, uint, 0644);
176MODULE_PARM_DESC(irq_adapt_high_thresh,
177 "Threshold score for increasing IRQ moderation");
178
Ben Hutchings62776d02010-06-23 11:30:07 +0000179static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
180 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
181 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
182 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
183module_param(debug, uint, 0);
184MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
185
Ben Hutchings8ceee662008-04-27 12:55:59 +0100186/**************************************************************************
187 *
188 * Utility functions and prototypes
189 *
190 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000191
Jon Cooper261e4d92013-04-15 18:51:54 +0100192static int efx_soft_enable_interrupts(struct efx_nic *efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100193static void efx_soft_disable_interrupts(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000194static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000195static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000196static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100197static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000198static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100199static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000200static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000201static void efx_fini_struct(struct efx_nic *efx);
202static void efx_start_all(struct efx_nic *efx);
203static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100204
205#define EFX_ASSERT_RESET_SERIALISED(efx) \
206 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100207 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000208 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000209 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100210 ASSERT_RTNL(); \
211 } while (0)
212
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100213static int efx_check_disabled(struct efx_nic *efx)
214{
Alexandre Rames626950d2013-01-14 17:20:22 +0000215 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100216 netif_err(efx, drv, efx->net_dev,
217 "device is disabled due to earlier errors\n");
218 return -EIO;
219 }
220 return 0;
221}
222
Ben Hutchings8ceee662008-04-27 12:55:59 +0100223/**************************************************************************
224 *
225 * Event queue processing
226 *
227 *************************************************************************/
228
229/* Process channel's event queue
230 *
231 * This function is responsible for processing the event queue of a
232 * single channel. The caller must guarantee that this function will
233 * never be concurrently called more than once on the same channel,
234 * though different channels may be being processed concurrently.
235 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000236static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100237{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000238 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100239
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000240 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100241 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100242
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000243 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000244 if (spent && efx_channel_has_rx_queue(channel)) {
245 struct efx_rx_queue *rx_queue =
246 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100247
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000248 efx_rx_flush_packet(channel);
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100249 efx_fast_push_rx_descriptors(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100250 }
251
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000252 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100253}
254
Ben Hutchings8ceee662008-04-27 12:55:59 +0100255/* NAPI poll handler
256 *
257 * NAPI guarantees serialisation of polls of the same device, which
258 * provides the guarantee required by efx_process_channel().
259 */
260static int efx_poll(struct napi_struct *napi, int budget)
261{
262 struct efx_channel *channel =
263 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000264 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000265 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100266
Ben Hutchings62776d02010-06-23 11:30:07 +0000267 netif_vdbg(efx, intr, efx->net_dev,
268 "channel %d NAPI poll executing on CPU %d\n",
269 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100270
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000271 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100272
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000273 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000274 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000275 efx->irq_rx_adaptive &&
276 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000277 if (unlikely(channel->irq_mod_score <
278 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000279 if (channel->irq_moderation > 1) {
280 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000281 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000282 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000283 } else if (unlikely(channel->irq_mod_score >
284 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000285 if (channel->irq_moderation <
286 efx->irq_rx_moderation) {
287 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000288 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000289 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000290 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000291 channel->irq_count = 0;
292 channel->irq_mod_score = 0;
293 }
294
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000295 efx_filter_rfs_expire(channel);
296
Ben Hutchings8ceee662008-04-27 12:55:59 +0100297 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800298 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100299 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100300 * interrupts have already been disabled.
301 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800302 napi_complete(napi);
Ben Hutchings514bedb2012-10-05 19:30:16 +0100303 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100304 }
305
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000306 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100307}
308
Ben Hutchings8ceee662008-04-27 12:55:59 +0100309/* Create event queue
310 * Event queue memory allocations are done only once. If the channel
311 * is reset, the memory buffer will be reused; this guards against
312 * errors during channel reset and also simplifies interrupt handling.
313 */
314static int efx_probe_eventq(struct efx_channel *channel)
315{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000316 struct efx_nic *efx = channel->efx;
317 unsigned long entries;
318
Ben Hutchings86ee5302012-01-09 19:51:22 +0000319 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000320 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100321
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000322 /* Build an event queue with room for one event per tx and rx buffer,
323 * plus some extra for link state events and MCDI completions. */
324 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
325 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
326 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
327
Ben Hutchings152b6a62009-11-29 03:43:56 +0000328 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100329}
330
331/* Prepare channel's event queue */
Jon Cooper261e4d92013-04-15 18:51:54 +0100332static int efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100333{
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100334 struct efx_nic *efx = channel->efx;
Jon Cooper261e4d92013-04-15 18:51:54 +0100335 int rc;
336
337 EFX_WARN_ON_PARANOID(channel->eventq_init);
338
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100339 netif_dbg(efx, drv, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000340 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100341
Jon Cooper261e4d92013-04-15 18:51:54 +0100342 rc = efx_nic_init_eventq(channel);
343 if (rc == 0) {
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100344 efx->type->push_irq_moderation(channel);
Jon Cooper261e4d92013-04-15 18:51:54 +0100345 channel->eventq_read_ptr = 0;
346 channel->eventq_init = true;
347 }
348 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100349}
350
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000351/* Enable event queue processing and NAPI */
352static void efx_start_eventq(struct efx_channel *channel)
353{
354 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
355 "chan %d start event queue\n", channel->channel);
356
Ben Hutchings514bedb2012-10-05 19:30:16 +0100357 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000358 channel->enabled = true;
359 smp_wmb();
360
361 napi_enable(&channel->napi_str);
362 efx_nic_eventq_read_ack(channel);
363}
364
365/* Disable event queue processing and NAPI */
366static void efx_stop_eventq(struct efx_channel *channel)
367{
368 if (!channel->enabled)
369 return;
370
371 napi_disable(&channel->napi_str);
372 channel->enabled = false;
373}
374
Ben Hutchings8ceee662008-04-27 12:55:59 +0100375static void efx_fini_eventq(struct efx_channel *channel)
376{
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100377 if (!channel->eventq_init)
378 return;
379
Ben Hutchings62776d02010-06-23 11:30:07 +0000380 netif_dbg(channel->efx, drv, channel->efx->net_dev,
381 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100382
Ben Hutchings152b6a62009-11-29 03:43:56 +0000383 efx_nic_fini_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100384 channel->eventq_init = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100385}
386
387static void efx_remove_eventq(struct efx_channel *channel)
388{
Ben Hutchings62776d02010-06-23 11:30:07 +0000389 netif_dbg(channel->efx, drv, channel->efx->net_dev,
390 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100391
Ben Hutchings152b6a62009-11-29 03:43:56 +0000392 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100393}
394
395/**************************************************************************
396 *
397 * Channel handling
398 *
399 *************************************************************************/
400
Ben Hutchings7f967c02012-02-13 23:45:02 +0000401/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000402static struct efx_channel *
403efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
404{
405 struct efx_channel *channel;
406 struct efx_rx_queue *rx_queue;
407 struct efx_tx_queue *tx_queue;
408 int j;
409
Ben Hutchings7f967c02012-02-13 23:45:02 +0000410 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
411 if (!channel)
412 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000413
Ben Hutchings7f967c02012-02-13 23:45:02 +0000414 channel->efx = efx;
415 channel->channel = i;
416 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000417
Ben Hutchings7f967c02012-02-13 23:45:02 +0000418 for (j = 0; j < EFX_TXQ_TYPES; j++) {
419 tx_queue = &channel->tx_queue[j];
420 tx_queue->efx = efx;
421 tx_queue->queue = i * EFX_TXQ_TYPES + j;
422 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000423 }
424
Ben Hutchings46426102010-09-10 06:42:33 +0000425 rx_queue = &channel->rx_queue;
426 rx_queue->efx = efx;
427 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
428 (unsigned long)rx_queue);
429
430 return channel;
431}
432
Ben Hutchings7f967c02012-02-13 23:45:02 +0000433/* Allocate and initialise a channel structure, copying parameters
434 * (but not resources) from an old channel structure.
435 */
436static struct efx_channel *
437efx_copy_channel(const struct efx_channel *old_channel)
438{
439 struct efx_channel *channel;
440 struct efx_rx_queue *rx_queue;
441 struct efx_tx_queue *tx_queue;
442 int j;
443
444 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
445 if (!channel)
446 return NULL;
447
448 *channel = *old_channel;
449
450 channel->napi_dev = NULL;
451 memset(&channel->eventq, 0, sizeof(channel->eventq));
452
453 for (j = 0; j < EFX_TXQ_TYPES; j++) {
454 tx_queue = &channel->tx_queue[j];
455 if (tx_queue->channel)
456 tx_queue->channel = channel;
457 tx_queue->buffer = NULL;
458 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
459 }
460
461 rx_queue = &channel->rx_queue;
462 rx_queue->buffer = NULL;
463 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
464 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
465 (unsigned long)rx_queue);
466
467 return channel;
468}
469
Ben Hutchings8ceee662008-04-27 12:55:59 +0100470static int efx_probe_channel(struct efx_channel *channel)
471{
472 struct efx_tx_queue *tx_queue;
473 struct efx_rx_queue *rx_queue;
474 int rc;
475
Ben Hutchings62776d02010-06-23 11:30:07 +0000476 netif_dbg(channel->efx, probe, channel->efx->net_dev,
477 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100478
Ben Hutchings7f967c02012-02-13 23:45:02 +0000479 rc = channel->type->pre_probe(channel);
480 if (rc)
481 goto fail;
482
Ben Hutchings8ceee662008-04-27 12:55:59 +0100483 rc = efx_probe_eventq(channel);
484 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000485 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100486
487 efx_for_each_channel_tx_queue(tx_queue, channel) {
488 rc = efx_probe_tx_queue(tx_queue);
489 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000490 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100491 }
492
493 efx_for_each_channel_rx_queue(rx_queue, channel) {
494 rc = efx_probe_rx_queue(rx_queue);
495 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000496 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100497 }
498
499 channel->n_rx_frm_trunc = 0;
500
501 return 0;
502
Ben Hutchings7f967c02012-02-13 23:45:02 +0000503fail:
504 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100505 return rc;
506}
507
Ben Hutchings7f967c02012-02-13 23:45:02 +0000508static void
509efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
510{
511 struct efx_nic *efx = channel->efx;
512 const char *type;
513 int number;
514
515 number = channel->channel;
516 if (efx->tx_channel_offset == 0) {
517 type = "";
518 } else if (channel->channel < efx->tx_channel_offset) {
519 type = "-rx";
520 } else {
521 type = "-tx";
522 number -= efx->tx_channel_offset;
523 }
524 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
525}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100526
Ben Hutchings56536e92008-12-12 21:37:02 -0800527static void efx_set_channel_names(struct efx_nic *efx)
528{
529 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800530
Ben Hutchings7f967c02012-02-13 23:45:02 +0000531 efx_for_each_channel(channel, efx)
532 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100533 efx->msi_context[channel->channel].name,
534 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800535}
536
Ben Hutchings46426102010-09-10 06:42:33 +0000537static int efx_probe_channels(struct efx_nic *efx)
538{
539 struct efx_channel *channel;
540 int rc;
541
542 /* Restart special buffer allocation */
543 efx->next_buffer_table = 0;
544
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000545 /* Probe channels in reverse, so that any 'extra' channels
546 * use the start of the buffer table. This allows the traffic
547 * channels to be resized without moving them or wasting the
548 * entries before them.
549 */
550 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000551 rc = efx_probe_channel(channel);
552 if (rc) {
553 netif_err(efx, probe, efx->net_dev,
554 "failed to create channel %d\n",
555 channel->channel);
556 goto fail;
557 }
558 }
559 efx_set_channel_names(efx);
560
561 return 0;
562
563fail:
564 efx_remove_channels(efx);
565 return rc;
566}
567
Ben Hutchings8ceee662008-04-27 12:55:59 +0100568/* Channels are shutdown and reinitialised whilst the NIC is running
569 * to propagate configuration changes (mtu, checksum offload), or
570 * to clear hardware error conditions
571 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000572static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100573{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000574 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100575 struct efx_tx_queue *tx_queue;
576 struct efx_rx_queue *rx_queue;
577 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000578 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100579
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100580 /* Calculate the rx buffer allocation parameters required to
581 * support the current MTU, including padding for header
582 * alignment and overruns.
583 */
Jon Cooper43a37392012-10-18 15:49:54 +0100584 efx->rx_dma_len = (efx->rx_prefix_size +
Ben Hutchings272baee2013-01-29 23:33:14 +0000585 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
586 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000587 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Ben Hutchingsc14ff2e2013-05-13 11:58:31 +0000588 NET_IP_ALIGN + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000589 if (rx_buf_len <= PAGE_SIZE) {
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
648 if (netif_device_present(efx->net_dev))
649 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100650}
651
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000652static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100653{
654 struct efx_channel *channel;
655 struct efx_tx_queue *tx_queue;
656 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100657 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100658
659 EFX_ASSERT_RESET_SERIALISED(efx);
660 BUG_ON(efx->port_enabled);
661
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100662 /* Stop RX refill */
663 efx_for_each_channel(channel, efx) {
664 efx_for_each_channel_rx_queue(rx_queue, channel)
665 rx_queue->refill_enabled = false;
666 }
667
Ben Hutchings8ceee662008-04-27 12:55:59 +0100668 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000669 /* RX packet processing is pipelined, so wait for the
670 * NAPI handler to complete. At least event queue 0
671 * might be kept active by non-data events, so don't
672 * use napi_synchronize() but actually disable NAPI
673 * temporarily.
674 */
675 if (efx_channel_has_rx_queue(channel)) {
676 efx_stop_eventq(channel);
677 efx_start_eventq(channel);
678 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100679 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100680
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100681 rc = efx->type->fini_dmaq(efx);
682 if (rc && EFX_WORKAROUND_7803(efx)) {
683 /* Schedule a reset to recover from the flush failure. The
684 * descriptor caches reference memory we're about to free,
685 * but falcon_reconfigure_mac_wrapper() won't reconnect
686 * the MACs because of the pending reset.
687 */
688 netif_err(efx, drv, efx->net_dev,
689 "Resetting to recover from flush failure\n");
690 efx_schedule_reset(efx, RESET_TYPE_ALL);
691 } else if (rc) {
692 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
693 } else {
694 netif_dbg(efx, drv, efx->net_dev,
695 "successfully flushed all queues\n");
696 }
697
698 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100699 efx_for_each_channel_rx_queue(rx_queue, channel)
700 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000701 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100702 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100703 }
704}
705
706static void efx_remove_channel(struct efx_channel *channel)
707{
708 struct efx_tx_queue *tx_queue;
709 struct efx_rx_queue *rx_queue;
710
Ben Hutchings62776d02010-06-23 11:30:07 +0000711 netif_dbg(channel->efx, drv, channel->efx->net_dev,
712 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100713
714 efx_for_each_channel_rx_queue(rx_queue, channel)
715 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000716 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100717 efx_remove_tx_queue(tx_queue);
718 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100719 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100720}
721
Ben Hutchings46426102010-09-10 06:42:33 +0000722static void efx_remove_channels(struct efx_nic *efx)
723{
724 struct efx_channel *channel;
725
726 efx_for_each_channel(channel, efx)
727 efx_remove_channel(channel);
728}
729
730int
731efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
732{
733 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
734 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000735 unsigned i, next_buffer_table = 0;
Jon Cooper261e4d92013-04-15 18:51:54 +0100736 int rc, rc2;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100737
738 rc = efx_check_disabled(efx);
739 if (rc)
740 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000741
742 /* Not all channels should be reallocated. We must avoid
743 * reallocating their buffer table entries.
744 */
745 efx_for_each_channel(channel, efx) {
746 struct efx_rx_queue *rx_queue;
747 struct efx_tx_queue *tx_queue;
748
749 if (channel->type->copy)
750 continue;
751 next_buffer_table = max(next_buffer_table,
752 channel->eventq.index +
753 channel->eventq.entries);
754 efx_for_each_channel_rx_queue(rx_queue, channel)
755 next_buffer_table = max(next_buffer_table,
756 rx_queue->rxd.index +
757 rx_queue->rxd.entries);
758 efx_for_each_channel_tx_queue(tx_queue, channel)
759 next_buffer_table = max(next_buffer_table,
760 tx_queue->txd.index +
761 tx_queue->txd.entries);
762 }
Ben Hutchings46426102010-09-10 06:42:33 +0000763
Ben Hutchings29c69a42013-01-28 19:01:06 +0000764 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000765 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100766 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000767
Ben Hutchings7f967c02012-02-13 23:45:02 +0000768 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000769 memset(other_channel, 0, sizeof(other_channel));
770 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000771 channel = efx->channel[i];
772 if (channel->type->copy)
773 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000774 if (!channel) {
775 rc = -ENOMEM;
776 goto out;
777 }
778 other_channel[i] = channel;
779 }
780
781 /* Swap entry counts and channel pointers */
782 old_rxq_entries = efx->rxq_entries;
783 old_txq_entries = efx->txq_entries;
784 efx->rxq_entries = rxq_entries;
785 efx->txq_entries = txq_entries;
786 for (i = 0; i < efx->n_channels; i++) {
787 channel = efx->channel[i];
788 efx->channel[i] = other_channel[i];
789 other_channel[i] = channel;
790 }
791
Ben Hutchings7f967c02012-02-13 23:45:02 +0000792 /* Restart buffer table allocation */
793 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000794
Ben Hutchingse8f14992010-12-07 19:47:34 +0000795 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000796 channel = efx->channel[i];
797 if (!channel->type->copy)
798 continue;
799 rc = efx_probe_channel(channel);
800 if (rc)
801 goto rollback;
802 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000803 }
Ben Hutchings46426102010-09-10 06:42:33 +0000804
Ben Hutchings7f967c02012-02-13 23:45:02 +0000805out:
806 /* Destroy unused channel structures */
807 for (i = 0; i < efx->n_channels; i++) {
808 channel = other_channel[i];
809 if (channel && channel->type->copy) {
810 efx_fini_napi_channel(channel);
811 efx_remove_channel(channel);
812 kfree(channel);
813 }
814 }
815
Jon Cooper261e4d92013-04-15 18:51:54 +0100816 rc2 = efx_soft_enable_interrupts(efx);
817 if (rc2) {
818 rc = rc ? rc : rc2;
819 netif_err(efx, drv, efx->net_dev,
820 "unable to restart interrupts on channel reallocation\n");
821 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
822 } else {
823 efx_start_all(efx);
824 netif_device_attach(efx->net_dev);
825 }
Ben Hutchings46426102010-09-10 06:42:33 +0000826 return rc;
827
828rollback:
829 /* Swap back */
830 efx->rxq_entries = old_rxq_entries;
831 efx->txq_entries = old_txq_entries;
832 for (i = 0; i < efx->n_channels; i++) {
833 channel = efx->channel[i];
834 efx->channel[i] = other_channel[i];
835 other_channel[i] = channel;
836 }
837 goto out;
838}
839
Steve Hodgson90d683a2010-06-01 11:19:39 +0000840void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100841{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000842 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100843}
844
Ben Hutchings7f967c02012-02-13 23:45:02 +0000845static const struct efx_channel_type efx_default_channel_type = {
846 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100847 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000848 .get_name = efx_get_channel_name,
849 .copy = efx_copy_channel,
850 .keep_eventq = false,
851};
852
853int efx_channel_dummy_op_int(struct efx_channel *channel)
854{
855 return 0;
856}
857
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100858void efx_channel_dummy_op_void(struct efx_channel *channel)
859{
860}
861
Ben Hutchings8ceee662008-04-27 12:55:59 +0100862/**************************************************************************
863 *
864 * Port handling
865 *
866 **************************************************************************/
867
868/* This ensures that the kernel is kept informed (via
869 * netif_carrier_on/off) of the link status, and also maintains the
870 * link status's stop on the port's TX queue.
871 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000872void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100873{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000874 struct efx_link_state *link_state = &efx->link_state;
875
Ben Hutchings8ceee662008-04-27 12:55:59 +0100876 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
877 * that no events are triggered between unregister_netdev() and the
878 * driver unloading. A more general condition is that NETDEV_CHANGE
879 * can only be generated between NETDEV_UP and NETDEV_DOWN */
880 if (!netif_running(efx->net_dev))
881 return;
882
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000883 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100884 efx->n_link_state_changes++;
885
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000886 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100887 netif_carrier_on(efx->net_dev);
888 else
889 netif_carrier_off(efx->net_dev);
890 }
891
892 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000893 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000894 netif_info(efx, link, efx->net_dev,
Ben Hutchings964e6132012-11-19 23:08:22 +0000895 "link up at %uMbps %s-duplex (MTU %d)\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000896 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings964e6132012-11-19 23:08:22 +0000897 efx->net_dev->mtu);
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000898 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000899 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100900}
901
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000902void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
903{
904 efx->link_advertising = advertising;
905 if (advertising) {
906 if (advertising & ADVERTISED_Pause)
907 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
908 else
909 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
910 if (advertising & ADVERTISED_Asym_Pause)
911 efx->wanted_fc ^= EFX_FC_TX;
912 }
913}
914
David S. Millerb56269462011-05-17 17:53:22 -0400915void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000916{
917 efx->wanted_fc = wanted_fc;
918 if (efx->link_advertising) {
919 if (wanted_fc & EFX_FC_RX)
920 efx->link_advertising |= (ADVERTISED_Pause |
921 ADVERTISED_Asym_Pause);
922 else
923 efx->link_advertising &= ~(ADVERTISED_Pause |
924 ADVERTISED_Asym_Pause);
925 if (wanted_fc & EFX_FC_TX)
926 efx->link_advertising ^= ADVERTISED_Asym_Pause;
927 }
928}
929
Ben Hutchings115122a2009-03-04 09:52:52 +0000930static void efx_fini_port(struct efx_nic *efx);
931
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000932/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
933 * the MAC appropriately. All other PHY configuration changes are pushed
934 * through phy_op->set_settings(), and pushed asynchronously to the MAC
935 * through efx_monitor().
936 *
937 * Callers must hold the mac_lock
938 */
939int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100940{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000941 enum efx_phy_mode phy_mode;
942 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100943
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000944 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100945
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000946 /* Disable PHY transmit in mac level loopbacks */
947 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800948 if (LOOPBACK_INTERNAL(efx))
949 efx->phy_mode |= PHY_MODE_TX_DISABLED;
950 else
951 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800952
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000953 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800954
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000955 if (rc)
956 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100957
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000958 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100959}
960
961/* Reinitialise the MAC to pick up new PHY settings, even if the port is
962 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000963int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100964{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000965 int rc;
966
Ben Hutchings8ceee662008-04-27 12:55:59 +0100967 EFX_ASSERT_RESET_SERIALISED(efx);
968
969 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000970 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100971 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000972
973 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100974}
975
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000976/* Asynchronous work item for changing MAC promiscuity and multicast
977 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
978 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800979static void efx_mac_work(struct work_struct *data)
980{
981 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
982
983 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100984 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100985 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800986 mutex_unlock(&efx->mac_lock);
987}
988
Ben Hutchings8ceee662008-04-27 12:55:59 +0100989static int efx_probe_port(struct efx_nic *efx)
990{
991 int rc;
992
Ben Hutchings62776d02010-06-23 11:30:07 +0000993 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100994
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000995 if (phy_flash_cfg)
996 efx->phy_mode = PHY_MODE_SPECIAL;
997
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000998 /* Connect up MAC/PHY operations table */
999 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001000 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001001 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001002
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001003 /* Initialise MAC address to permanent address */
1004 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001005
1006 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001007}
1008
1009static int efx_init_port(struct efx_nic *efx)
1010{
1011 int rc;
1012
Ben Hutchings62776d02010-06-23 11:30:07 +00001013 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001014
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001015 mutex_lock(&efx->mac_lock);
1016
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001017 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001018 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001019 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001020
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001021 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001022
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001023 /* Reconfigure the MAC before creating dma queues (required for
1024 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001025 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001026
1027 /* Ensure the PHY advertises the correct flow control settings */
1028 rc = efx->phy_op->reconfigure(efx);
1029 if (rc)
1030 goto fail2;
1031
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001032 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001033 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001034
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001035fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001036 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001037fail1:
1038 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001039 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001040}
1041
Ben Hutchings8ceee662008-04-27 12:55:59 +01001042static void efx_start_port(struct efx_nic *efx)
1043{
Ben Hutchings62776d02010-06-23 11:30:07 +00001044 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001045 BUG_ON(efx->port_enabled);
1046
1047 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001048 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001049
1050 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1051 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001052 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001053
Ben Hutchings8ceee662008-04-27 12:55:59 +01001054 mutex_unlock(&efx->mac_lock);
1055}
1056
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001057/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001058static void efx_stop_port(struct efx_nic *efx)
1059{
Ben Hutchings62776d02010-06-23 11:30:07 +00001060 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001061
1062 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001063 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001064 mutex_unlock(&efx->mac_lock);
1065
1066 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001067 netif_addr_lock_bh(efx->net_dev);
1068 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001069}
1070
1071static void efx_fini_port(struct efx_nic *efx)
1072{
Ben Hutchings62776d02010-06-23 11:30:07 +00001073 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001074
1075 if (!efx->port_initialized)
1076 return;
1077
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001078 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001079 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001080
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001081 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001082 efx_link_status_changed(efx);
1083}
1084
1085static void efx_remove_port(struct efx_nic *efx)
1086{
Ben Hutchings62776d02010-06-23 11:30:07 +00001087 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001088
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001089 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001090}
1091
1092/**************************************************************************
1093 *
1094 * NIC handling
1095 *
1096 **************************************************************************/
1097
1098/* This configures the PCI device to enable I/O and DMA. */
1099static int efx_init_io(struct efx_nic *efx)
1100{
1101 struct pci_dev *pci_dev = efx->pci_dev;
1102 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001103 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001104 int rc;
1105
Ben Hutchings62776d02010-06-23 11:30:07 +00001106 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001107
1108 rc = pci_enable_device(pci_dev);
1109 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001110 netif_err(efx, probe, efx->net_dev,
1111 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001112 goto fail1;
1113 }
1114
1115 pci_set_master(pci_dev);
1116
1117 /* Set the PCI DMA mask. Try all possibilities from our
1118 * genuine mask down to 32 bits, because some architectures
1119 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1120 * masks event though they reject 46 bit masks.
1121 */
1122 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001123 if (dma_supported(&pci_dev->dev, dma_mask)) {
1124 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001125 if (rc == 0)
1126 break;
1127 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001128 dma_mask >>= 1;
1129 }
1130 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001131 netif_err(efx, probe, efx->net_dev,
1132 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001133 goto fail2;
1134 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001135 netif_dbg(efx, probe, efx->net_dev,
1136 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001137 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001138 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001139 /* dma_set_coherent_mask() is not *allowed* to
1140 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001141 * but just in case...
1142 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001143 netif_err(efx, probe, efx->net_dev,
1144 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001145 goto fail2;
1146 }
1147
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001148 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1149 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001150 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001151 netif_err(efx, probe, efx->net_dev,
1152 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001153 rc = -EIO;
1154 goto fail3;
1155 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001156 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001157 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001158 netif_err(efx, probe, efx->net_dev,
1159 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001160 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001161 rc = -ENOMEM;
1162 goto fail4;
1163 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001164 netif_dbg(efx, probe, efx->net_dev,
1165 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001166 (unsigned long long)efx->membase_phys, mem_map_size,
1167 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001168
1169 return 0;
1170
1171 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001172 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001173 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001174 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001175 fail2:
1176 pci_disable_device(efx->pci_dev);
1177 fail1:
1178 return rc;
1179}
1180
1181static void efx_fini_io(struct efx_nic *efx)
1182{
Ben Hutchings62776d02010-06-23 11:30:07 +00001183 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001184
1185 if (efx->membase) {
1186 iounmap(efx->membase);
1187 efx->membase = NULL;
1188 }
1189
1190 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001191 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001192 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001193 }
1194
1195 pci_disable_device(efx->pci_dev);
1196}
1197
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001198static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001199{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001200 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001201 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001202 int cpu;
1203
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001204 if (rss_cpus) {
1205 count = rss_cpus;
1206 } else {
1207 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1208 netif_warn(efx, probe, efx->net_dev,
1209 "RSS disabled due to allocation failure\n");
1210 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001211 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001212
1213 count = 0;
1214 for_each_online_cpu(cpu) {
1215 if (!cpumask_test_cpu(cpu, thread_mask)) {
1216 ++count;
1217 cpumask_or(thread_mask, thread_mask,
1218 topology_thread_cpumask(cpu));
1219 }
1220 }
1221
1222 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001223 }
1224
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001225 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1226 * table entries that are inaccessible to VFs
1227 */
1228 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1229 count > efx_vf_size(efx)) {
1230 netif_warn(efx, probe, efx->net_dev,
1231 "Reducing number of RSS channels from %u to %u for "
1232 "VF support. Increase vf-msix-limit to use more "
1233 "channels on the PF.\n",
1234 count, efx_vf_size(efx));
1235 count = efx_vf_size(efx);
1236 }
1237
Ben Hutchings46123d02008-09-01 12:47:33 +01001238 return count;
1239}
1240
1241/* Probe the number and type of interrupts we are able to obtain, and
1242 * the resulting numbers of channels and RX queues.
1243 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001244static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001245{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001246 unsigned int extra_channels = 0;
1247 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001248 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001249
Ben Hutchings7f967c02012-02-13 23:45:02 +00001250 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1251 if (efx->extra_channel_type[i])
1252 ++extra_channels;
1253
Ben Hutchings8ceee662008-04-27 12:55:59 +01001254 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001255 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001256 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001257
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001258 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001259 if (separate_tx_channels)
1260 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001261 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001262 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001263
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001264 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001265 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001266 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001267 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001268 netif_err(efx, drv, efx->net_dev,
1269 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001270 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001271 netif_err(efx, drv, efx->net_dev,
1272 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001273 EFX_BUG_ON_PARANOID(rc >= n_channels);
1274 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001275 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001276 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001277 }
1278
1279 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001280 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001281 if (n_channels > extra_channels)
1282 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001283 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001284 efx->n_tx_channels = max(n_channels / 2, 1U);
1285 efx->n_rx_channels = max(n_channels -
1286 efx->n_tx_channels,
1287 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001288 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001289 efx->n_tx_channels = n_channels;
1290 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001291 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001292 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001293 efx_get_channel(efx, i)->irq =
1294 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001295 } else {
1296 /* Fall back to single channel MSI */
1297 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001298 netif_err(efx, drv, efx->net_dev,
1299 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001300 }
1301 }
1302
1303 /* Try single interrupt MSI */
1304 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001305 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001306 efx->n_rx_channels = 1;
1307 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001308 rc = pci_enable_msi(efx->pci_dev);
1309 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001310 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001311 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001312 netif_err(efx, drv, efx->net_dev,
1313 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001314 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1315 }
1316 }
1317
1318 /* Assume legacy interrupts */
1319 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001320 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001321 efx->n_rx_channels = 1;
1322 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001323 efx->legacy_irq = efx->pci_dev->irq;
1324 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001325
Ben Hutchings7f967c02012-02-13 23:45:02 +00001326 /* Assign extra channels if possible */
1327 j = efx->n_channels;
1328 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1329 if (!efx->extra_channel_type[i])
1330 continue;
1331 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1332 efx->n_channels <= extra_channels) {
1333 efx->extra_channel_type[i]->handle_no_channel(efx);
1334 } else {
1335 --j;
1336 efx_get_channel(efx, j)->type =
1337 efx->extra_channel_type[i];
1338 }
1339 }
1340
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001341 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001342 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001343 efx->n_rx_channels : efx_vf_size(efx));
1344
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001345 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001346}
1347
Jon Cooper261e4d92013-04-15 18:51:54 +01001348static int efx_soft_enable_interrupts(struct efx_nic *efx)
Ben Hutchingsd8291182012-10-05 23:35:41 +01001349{
Jon Cooper261e4d92013-04-15 18:51:54 +01001350 struct efx_channel *channel, *end_channel;
1351 int rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001352
1353 BUG_ON(efx->state == STATE_DISABLED);
1354
1355 efx->irq_soft_enabled = true;
1356 smp_wmb();
1357
1358 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001359 if (!channel->type->keep_eventq) {
1360 rc = efx_init_eventq(channel);
1361 if (rc)
1362 goto fail;
1363 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001364 efx_start_eventq(channel);
1365 }
1366
1367 efx_mcdi_mode_event(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +01001368
1369 return 0;
1370fail:
1371 end_channel = channel;
1372 efx_for_each_channel(channel, efx) {
1373 if (channel == end_channel)
1374 break;
1375 efx_stop_eventq(channel);
1376 if (!channel->type->keep_eventq)
1377 efx_fini_eventq(channel);
1378 }
1379
1380 return rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001381}
1382
1383static void efx_soft_disable_interrupts(struct efx_nic *efx)
1384{
1385 struct efx_channel *channel;
1386
1387 if (efx->state == STATE_DISABLED)
1388 return;
1389
1390 efx_mcdi_mode_poll(efx);
1391
1392 efx->irq_soft_enabled = false;
1393 smp_wmb();
1394
1395 if (efx->legacy_irq)
1396 synchronize_irq(efx->legacy_irq);
1397
1398 efx_for_each_channel(channel, efx) {
1399 if (channel->irq)
1400 synchronize_irq(channel->irq);
1401
1402 efx_stop_eventq(channel);
1403 if (!channel->type->keep_eventq)
1404 efx_fini_eventq(channel);
1405 }
Ben Hutchingscade7152013-08-27 23:12:31 +01001406
1407 /* Flush the asynchronous MCDI request queue */
1408 efx_mcdi_flush_async(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01001409}
1410
Jon Cooper261e4d92013-04-15 18:51:54 +01001411static int efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001412{
Jon Cooper261e4d92013-04-15 18:51:54 +01001413 struct efx_channel *channel, *end_channel;
1414 int rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001415
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001416 BUG_ON(efx->state == STATE_DISABLED);
1417
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001418 if (efx->eeh_disabled_legacy_irq) {
1419 enable_irq(efx->legacy_irq);
1420 efx->eeh_disabled_legacy_irq = false;
1421 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001422
Ben Hutchings86094f72013-08-21 19:51:04 +01001423 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001424
1425 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001426 if (channel->type->keep_eventq) {
1427 rc = efx_init_eventq(channel);
1428 if (rc)
1429 goto fail;
1430 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001431 }
1432
Jon Cooper261e4d92013-04-15 18:51:54 +01001433 rc = efx_soft_enable_interrupts(efx);
1434 if (rc)
1435 goto fail;
1436
1437 return 0;
1438
1439fail:
1440 end_channel = channel;
1441 efx_for_each_channel(channel, efx) {
1442 if (channel == end_channel)
1443 break;
1444 if (channel->type->keep_eventq)
1445 efx_fini_eventq(channel);
1446 }
1447
1448 efx->type->irq_disable_non_ev(efx);
1449
1450 return rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001451}
1452
Ben Hutchingsd8291182012-10-05 23:35:41 +01001453static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001454{
1455 struct efx_channel *channel;
1456
Ben Hutchingsd8291182012-10-05 23:35:41 +01001457 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001458
1459 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001460 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001461 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001462 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001463
Ben Hutchings86094f72013-08-21 19:51:04 +01001464 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001465}
1466
Ben Hutchings8ceee662008-04-27 12:55:59 +01001467static void efx_remove_interrupts(struct efx_nic *efx)
1468{
1469 struct efx_channel *channel;
1470
1471 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001472 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001473 channel->irq = 0;
1474 pci_disable_msi(efx->pci_dev);
1475 pci_disable_msix(efx->pci_dev);
1476
1477 /* Remove legacy interrupt */
1478 efx->legacy_irq = 0;
1479}
1480
Ben Hutchings8831da72008-09-01 12:47:48 +01001481static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001482{
Ben Hutchings602a5322011-05-16 17:32:39 +01001483 struct efx_channel *channel;
1484 struct efx_tx_queue *tx_queue;
1485
Ben Hutchings97653432011-01-12 18:26:56 +00001486 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001487 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001488
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001489 /* We need to mark which channels really have RX and TX
1490 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001491 * RX-only and TX-only channels.
1492 */
1493 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001494 if (channel->channel < efx->n_rx_channels)
1495 channel->rx_queue.core_index = channel->channel;
1496 else
1497 channel->rx_queue.core_index = -1;
1498
Ben Hutchings602a5322011-05-16 17:32:39 +01001499 efx_for_each_channel_tx_queue(tx_queue, channel)
1500 tx_queue->queue -= (efx->tx_channel_offset *
1501 EFX_TXQ_TYPES);
1502 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001503}
1504
1505static int efx_probe_nic(struct efx_nic *efx)
1506{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001507 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001508 int rc;
1509
Ben Hutchings62776d02010-06-23 11:30:07 +00001510 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001511
1512 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001513 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001514 if (rc)
1515 return rc;
1516
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001517 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001518 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001519 rc = efx_probe_interrupts(efx);
1520 if (rc)
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001521 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001522
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001523 rc = efx->type->dimension_resources(efx);
1524 if (rc)
1525 goto fail2;
Ben Hutchings28e47c42012-02-15 01:58:49 +00001526
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001527 if (efx->n_channels > 1)
1528 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001529 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001530 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001531 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001532
Ben Hutchings8831da72008-09-01 12:47:48 +01001533 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001534 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1535 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001536
1537 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001538 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1539 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001540
1541 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001542
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001543fail2:
1544 efx_remove_interrupts(efx);
1545fail1:
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001546 efx->type->remove(efx);
1547 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001548}
1549
1550static void efx_remove_nic(struct efx_nic *efx)
1551{
Ben Hutchings62776d02010-06-23 11:30:07 +00001552 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001553
1554 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001555 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001556}
1557
Ben Hutchingsadd72472012-11-08 01:46:53 +00001558static int efx_probe_filters(struct efx_nic *efx)
1559{
1560 int rc;
1561
1562 spin_lock_init(&efx->filter_lock);
1563
1564 rc = efx->type->filter_table_probe(efx);
1565 if (rc)
1566 return rc;
1567
1568#ifdef CONFIG_RFS_ACCEL
1569 if (efx->type->offload_features & NETIF_F_NTUPLE) {
1570 efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
1571 sizeof(*efx->rps_flow_id),
1572 GFP_KERNEL);
1573 if (!efx->rps_flow_id) {
1574 efx->type->filter_table_remove(efx);
1575 return -ENOMEM;
1576 }
1577 }
1578#endif
1579
1580 return 0;
1581}
1582
1583static void efx_remove_filters(struct efx_nic *efx)
1584{
1585#ifdef CONFIG_RFS_ACCEL
1586 kfree(efx->rps_flow_id);
1587#endif
1588 efx->type->filter_table_remove(efx);
1589}
1590
1591static void efx_restore_filters(struct efx_nic *efx)
1592{
1593 efx->type->filter_table_restore(efx);
1594}
1595
Ben Hutchings8ceee662008-04-27 12:55:59 +01001596/**************************************************************************
1597 *
1598 * NIC startup/shutdown
1599 *
1600 *************************************************************************/
1601
1602static int efx_probe_all(struct efx_nic *efx)
1603{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001604 int rc;
1605
Ben Hutchings8ceee662008-04-27 12:55:59 +01001606 rc = efx_probe_nic(efx);
1607 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001608 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001609 goto fail1;
1610 }
1611
Ben Hutchings8ceee662008-04-27 12:55:59 +01001612 rc = efx_probe_port(efx);
1613 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001614 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001615 goto fail2;
1616 }
1617
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001618 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1619 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1620 rc = -EINVAL;
1621 goto fail3;
1622 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001623 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001624
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001625 rc = efx_probe_filters(efx);
1626 if (rc) {
1627 netif_err(efx, probe, efx->net_dev,
1628 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001629 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001630 }
1631
Ben Hutchings7f967c02012-02-13 23:45:02 +00001632 rc = efx_probe_channels(efx);
1633 if (rc)
1634 goto fail4;
1635
Ben Hutchings8ceee662008-04-27 12:55:59 +01001636 return 0;
1637
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001638 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001639 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001640 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001641 efx_remove_port(efx);
1642 fail2:
1643 efx_remove_nic(efx);
1644 fail1:
1645 return rc;
1646}
1647
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001648/* If the interface is supposed to be running but is not, start
1649 * the hardware and software data path, regular activity for the port
1650 * (MAC statistics, link polling, etc.) and schedule the port to be
1651 * reconfigured. Interrupts must already be enabled. This function
1652 * is safe to call multiple times, so long as the NIC is not disabled.
1653 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001654 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001655static void efx_start_all(struct efx_nic *efx)
1656{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001657 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001658 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001659
1660 /* Check that it is appropriate to restart the interface. All
1661 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001662 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001663 return;
1664
Ben Hutchings8ceee662008-04-27 12:55:59 +01001665 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001666 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001667
Alexandre Rames626950d2013-01-14 17:20:22 +00001668 /* Start the hardware monitor if there is one */
1669 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001670 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1671 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001672
1673 /* If link state detection is normally event-driven, we have
1674 * to poll now because we could have missed a change
1675 */
1676 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001677 mutex_lock(&efx->mac_lock);
1678 if (efx->phy_op->poll(efx))
1679 efx_link_status_changed(efx);
1680 mutex_unlock(&efx->mac_lock);
1681 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001682
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001683 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001684}
1685
1686/* Flush all delayed work. Should only be called when no more delayed work
1687 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1688 * since we're holding the rtnl_lock at this point. */
1689static void efx_flush_all(struct efx_nic *efx)
1690{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001691 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001692 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001693 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001694 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001695 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001696}
1697
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001698/* Quiesce the hardware and software data path, and regular activity
1699 * for the port without bringing the link down. Safe to call multiple
1700 * times with the NIC in almost any state, but interrupts should be
1701 * enabled. Requires the RTNL lock.
1702 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001703static void efx_stop_all(struct efx_nic *efx)
1704{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001705 EFX_ASSERT_RESET_SERIALISED(efx);
1706
1707 /* port_enabled can be read safely under the rtnl lock */
1708 if (!efx->port_enabled)
1709 return;
1710
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001711 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001712 efx_stop_port(efx);
1713
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001714 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001715 efx_flush_all(efx);
1716
Ben Hutchings29c69a42013-01-28 19:01:06 +00001717 /* Stop the kernel transmit interface. This is only valid if
1718 * the device is stopped or detached; otherwise the watchdog
1719 * may fire immediately.
1720 */
1721 WARN_ON(netif_running(efx->net_dev) &&
1722 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001723 netif_tx_disable(efx->net_dev);
1724
1725 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001726}
1727
1728static void efx_remove_all(struct efx_nic *efx)
1729{
Ben Hutchings46426102010-09-10 06:42:33 +00001730 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001731 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001732 efx_remove_port(efx);
1733 efx_remove_nic(efx);
1734}
1735
Ben Hutchings8ceee662008-04-27 12:55:59 +01001736/**************************************************************************
1737 *
1738 * Interrupt moderation
1739 *
1740 **************************************************************************/
1741
Ben Hutchingscc180b62011-12-08 19:51:47 +00001742static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001743{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001744 if (usecs == 0)
1745 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001746 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001747 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001748 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001749}
1750
Ben Hutchings8ceee662008-04-27 12:55:59 +01001751/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001752int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1753 unsigned int rx_usecs, bool rx_adaptive,
1754 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001755{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001756 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001757 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1758 efx->timer_quantum_ns,
1759 1000);
1760 unsigned int tx_ticks;
1761 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001762
1763 EFX_ASSERT_RESET_SERIALISED(efx);
1764
Ben Hutchingscc180b62011-12-08 19:51:47 +00001765 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001766 return -EINVAL;
1767
Ben Hutchingscc180b62011-12-08 19:51:47 +00001768 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1769 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1770
Ben Hutchings9e393b32011-09-05 07:43:04 +00001771 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1772 !rx_may_override_tx) {
1773 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1774 "RX and TX IRQ moderation must be equal\n");
1775 return -EINVAL;
1776 }
1777
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001778 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001779 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001780 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001781 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001782 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001783 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001784 channel->irq_moderation = tx_ticks;
1785 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001786
1787 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001788}
1789
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001790void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1791 unsigned int *rx_usecs, bool *rx_adaptive)
1792{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001793 /* We must round up when converting ticks to microseconds
1794 * because we round down when converting the other way.
1795 */
1796
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001797 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001798 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1799 efx->timer_quantum_ns,
1800 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001801
1802 /* If channels are shared between RX and TX, so is IRQ
1803 * moderation. Otherwise, IRQ moderation is the same for all
1804 * TX channels and is not adaptive.
1805 */
1806 if (efx->tx_channel_offset == 0)
1807 *tx_usecs = *rx_usecs;
1808 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001809 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001810 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001811 efx->timer_quantum_ns,
1812 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001813}
1814
Ben Hutchings8ceee662008-04-27 12:55:59 +01001815/**************************************************************************
1816 *
1817 * Hardware monitor
1818 *
1819 **************************************************************************/
1820
Ben Hutchingse254c272010-09-20 08:44:10 +00001821/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001822static void efx_monitor(struct work_struct *data)
1823{
1824 struct efx_nic *efx = container_of(data, struct efx_nic,
1825 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001826
Ben Hutchings62776d02010-06-23 11:30:07 +00001827 netif_vdbg(efx, timer, efx->net_dev,
1828 "hardware monitor executing on CPU %d\n",
1829 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001830 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001831
Ben Hutchings8ceee662008-04-27 12:55:59 +01001832 /* If the mac_lock is already held then it is likely a port
1833 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001834 * most of the work of monitor() anyway. */
1835 if (mutex_trylock(&efx->mac_lock)) {
1836 if (efx->port_enabled)
1837 efx->type->monitor(efx);
1838 mutex_unlock(&efx->mac_lock);
1839 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001840
Ben Hutchings8ceee662008-04-27 12:55:59 +01001841 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1842 efx_monitor_interval);
1843}
1844
1845/**************************************************************************
1846 *
1847 * ioctls
1848 *
1849 *************************************************************************/
1850
1851/* Net device ioctl
1852 * Context: process, rtnl_lock() held.
1853 */
1854static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1855{
Ben Hutchings767e4682008-09-01 12:43:14 +01001856 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001857 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001858
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001859 if (cmd == SIOCSHWTSTAMP)
1860 return efx_ptp_ioctl(efx, ifr, cmd);
1861
Ben Hutchings68e7f452009-04-29 08:05:08 +00001862 /* Convert phy_id from older PRTAD/DEVAD format */
1863 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1864 (data->phy_id & 0xfc00) == 0x0400)
1865 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1866
1867 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001868}
1869
1870/**************************************************************************
1871 *
1872 * NAPI interface
1873 *
1874 **************************************************************************/
1875
Ben Hutchings7f967c02012-02-13 23:45:02 +00001876static void efx_init_napi_channel(struct efx_channel *channel)
1877{
1878 struct efx_nic *efx = channel->efx;
1879
1880 channel->napi_dev = efx->net_dev;
1881 netif_napi_add(channel->napi_dev, &channel->napi_str,
1882 efx_poll, napi_weight);
1883}
1884
Ben Hutchingse8f14992010-12-07 19:47:34 +00001885static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001886{
1887 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001888
Ben Hutchings7f967c02012-02-13 23:45:02 +00001889 efx_for_each_channel(channel, efx)
1890 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001891}
1892
1893static void efx_fini_napi_channel(struct efx_channel *channel)
1894{
1895 if (channel->napi_dev)
1896 netif_napi_del(&channel->napi_str);
1897 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001898}
1899
1900static void efx_fini_napi(struct efx_nic *efx)
1901{
1902 struct efx_channel *channel;
1903
Ben Hutchingse8f14992010-12-07 19:47:34 +00001904 efx_for_each_channel(channel, efx)
1905 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001906}
1907
1908/**************************************************************************
1909 *
1910 * Kernel netpoll interface
1911 *
1912 *************************************************************************/
1913
1914#ifdef CONFIG_NET_POLL_CONTROLLER
1915
1916/* Although in the common case interrupts will be disabled, this is not
1917 * guaranteed. However, all our work happens inside the NAPI callback,
1918 * so no locking is required.
1919 */
1920static void efx_netpoll(struct net_device *net_dev)
1921{
Ben Hutchings767e4682008-09-01 12:43:14 +01001922 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001923 struct efx_channel *channel;
1924
Ben Hutchings64ee3122008-09-01 12:47:38 +01001925 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001926 efx_schedule_channel(channel);
1927}
1928
1929#endif
1930
1931/**************************************************************************
1932 *
1933 * Kernel net device interface
1934 *
1935 *************************************************************************/
1936
1937/* Context: process, rtnl_lock() held. */
1938static int efx_net_open(struct net_device *net_dev)
1939{
Ben Hutchings767e4682008-09-01 12:43:14 +01001940 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001941 int rc;
1942
Ben Hutchings62776d02010-06-23 11:30:07 +00001943 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1944 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001945
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001946 rc = efx_check_disabled(efx);
1947 if (rc)
1948 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001949 if (efx->phy_mode & PHY_MODE_SPECIAL)
1950 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001951 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1952 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001953
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001954 /* Notify the kernel of the link state polled during driver load,
1955 * before the monitor starts running */
1956 efx_link_status_changed(efx);
1957
Ben Hutchings8ceee662008-04-27 12:55:59 +01001958 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001959 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001960 return 0;
1961}
1962
1963/* Context: process, rtnl_lock() held.
1964 * Note that the kernel will ignore our return code; this method
1965 * should really be a void.
1966 */
1967static int efx_net_stop(struct net_device *net_dev)
1968{
Ben Hutchings767e4682008-09-01 12:43:14 +01001969 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001970
Ben Hutchings62776d02010-06-23 11:30:07 +00001971 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1972 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001973
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001974 /* Stop the device and flush all the channels */
1975 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001976
1977 return 0;
1978}
1979
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001980/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001981static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1982 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001983{
Ben Hutchings767e4682008-09-01 12:43:14 +01001984 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001985
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001986 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00001987 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001988 spin_unlock_bh(&efx->stats_lock);
1989
Ben Hutchings8ceee662008-04-27 12:55:59 +01001990 return stats;
1991}
1992
1993/* Context: netif_tx_lock held, BHs disabled. */
1994static void efx_watchdog(struct net_device *net_dev)
1995{
Ben Hutchings767e4682008-09-01 12:43:14 +01001996 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001997
Ben Hutchings62776d02010-06-23 11:30:07 +00001998 netif_err(efx, tx_err, efx->net_dev,
1999 "TX stuck with port_enabled=%d: resetting channels\n",
2000 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002001
Ben Hutchings739bb23d2008-11-04 20:35:36 +00002002 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002003}
2004
2005
2006/* Context: process, rtnl_lock() held. */
2007static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2008{
Ben Hutchings767e4682008-09-01 12:43:14 +01002009 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002010 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002011
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002012 rc = efx_check_disabled(efx);
2013 if (rc)
2014 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002015 if (new_mtu > EFX_MAX_MTU)
2016 return -EINVAL;
2017
Ben Hutchings62776d02010-06-23 11:30:07 +00002018 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002019
Ben Hutchings29c69a42013-01-28 19:01:06 +00002020 efx_device_detach_sync(efx);
2021 efx_stop_all(efx);
2022
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002023 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002024 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01002025 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002026 mutex_unlock(&efx->mac_lock);
2027
Ben Hutchings8ceee662008-04-27 12:55:59 +01002028 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00002029 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002030 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002031}
2032
2033static int efx_set_mac_address(struct net_device *net_dev, void *data)
2034{
Ben Hutchings767e4682008-09-01 12:43:14 +01002035 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002036 struct sockaddr *addr = data;
2037 char *new_addr = addr->sa_data;
2038
Ben Hutchings8ceee662008-04-27 12:55:59 +01002039 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002040 netif_err(efx, drv, efx->net_dev,
2041 "invalid ethernet MAC address requested: %pM\n",
2042 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002043 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002044 }
2045
2046 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002047 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002048
2049 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002050 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002051 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002052 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002053
2054 return 0;
2055}
2056
Ben Hutchingsa816f752008-09-01 12:49:12 +01002057/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002058static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002059{
Ben Hutchings767e4682008-09-01 12:43:14 +01002060 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002061
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002062 if (efx->port_enabled)
2063 queue_work(efx->workqueue, &efx->mac_work);
2064 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002065}
2066
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002067static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002068{
2069 struct efx_nic *efx = netdev_priv(net_dev);
2070
2071 /* If disabling RX n-tuple filtering, clear existing filters */
2072 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2073 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2074
2075 return 0;
2076}
2077
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002078static const struct net_device_ops efx_netdev_ops = {
2079 .ndo_open = efx_net_open,
2080 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002081 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002082 .ndo_tx_timeout = efx_watchdog,
2083 .ndo_start_xmit = efx_hard_start_xmit,
2084 .ndo_validate_addr = eth_validate_addr,
2085 .ndo_do_ioctl = efx_ioctl,
2086 .ndo_change_mtu = efx_change_mtu,
2087 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002088 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002089 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002090#ifdef CONFIG_SFC_SRIOV
2091 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2092 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2093 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2094 .ndo_get_vf_config = efx_sriov_get_vf_config,
2095#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002096#ifdef CONFIG_NET_POLL_CONTROLLER
2097 .ndo_poll_controller = efx_netpoll,
2098#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002099 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002100#ifdef CONFIG_RFS_ACCEL
2101 .ndo_rx_flow_steer = efx_filter_rfs,
2102#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002103};
2104
Ben Hutchings7dde5962008-12-12 22:09:38 -08002105static void efx_update_name(struct efx_nic *efx)
2106{
2107 strcpy(efx->name, efx->net_dev->name);
2108 efx_mtd_rename(efx);
2109 efx_set_channel_names(efx);
2110}
2111
Ben Hutchings8ceee662008-04-27 12:55:59 +01002112static int efx_netdev_event(struct notifier_block *this,
2113 unsigned long event, void *ptr)
2114{
Jiri Pirko351638e2013-05-28 01:30:21 +00002115 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002116
Ben Hutchings7dde5962008-12-12 22:09:38 -08002117 if (net_dev->netdev_ops == &efx_netdev_ops &&
2118 event == NETDEV_CHANGENAME)
2119 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002120
2121 return NOTIFY_DONE;
2122}
2123
2124static struct notifier_block efx_netdev_notifier = {
2125 .notifier_call = efx_netdev_event,
2126};
2127
Ben Hutchings06d5e192008-12-12 21:47:23 -08002128static ssize_t
2129show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2130{
2131 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2132 return sprintf(buf, "%d\n", efx->phy_type);
2133}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002134static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002135
Ben Hutchings8ceee662008-04-27 12:55:59 +01002136static int efx_register_netdev(struct efx_nic *efx)
2137{
2138 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002139 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002140 int rc;
2141
2142 net_dev->watchdog_timeo = 5 * HZ;
2143 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002144 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002145 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002146 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002147
Ben Hutchings7dde5962008-12-12 22:09:38 -08002148 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002149
Ben Hutchings7153f622012-07-27 20:50:52 +01002150 /* Enable resets to be scheduled and check whether any were
2151 * already requested. If so, the NIC is probably hosed so we
2152 * abort.
2153 */
2154 efx->state = STATE_READY;
2155 smp_mb(); /* ensure we change state before checking reset_pending */
2156 if (efx->reset_pending) {
2157 netif_err(efx, probe, efx->net_dev,
2158 "aborting probe due to scheduled reset\n");
2159 rc = -EIO;
2160 goto fail_locked;
2161 }
2162
Ben Hutchingsaed06282009-08-26 08:16:27 +00002163 rc = dev_alloc_name(net_dev, net_dev->name);
2164 if (rc < 0)
2165 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002166 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002167
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002168 /* Always start with carrier off; PHY events will detect the link */
2169 netif_carrier_off(net_dev);
2170
Ben Hutchingsaed06282009-08-26 08:16:27 +00002171 rc = register_netdevice(net_dev);
2172 if (rc)
2173 goto fail_locked;
2174
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002175 efx_for_each_channel(channel, efx) {
2176 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002177 efx_for_each_channel_tx_queue(tx_queue, channel)
2178 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002179 }
2180
Ben Hutchings7dde5962008-12-12 22:09:38 -08002181 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002182
Ben Hutchings06d5e192008-12-12 21:47:23 -08002183 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2184 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002185 netif_err(efx, drv, efx->net_dev,
2186 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002187 goto fail_registered;
2188 }
2189
Ben Hutchings8ceee662008-04-27 12:55:59 +01002190 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002191
Ben Hutchings7153f622012-07-27 20:50:52 +01002192fail_registered:
2193 rtnl_lock();
2194 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002195fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002196 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002197 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002198 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002199 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002200}
2201
2202static void efx_unregister_netdev(struct efx_nic *efx)
2203{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002204 if (!efx->net_dev)
2205 return;
2206
Ben Hutchings767e4682008-09-01 12:43:14 +01002207 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002208
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002209 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2210 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002211
2212 rtnl_lock();
2213 unregister_netdevice(efx->net_dev);
2214 efx->state = STATE_UNINIT;
2215 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002216}
2217
2218/**************************************************************************
2219 *
2220 * Device reset and suspend
2221 *
2222 **************************************************************************/
2223
Ben Hutchings2467ca42008-09-01 12:48:50 +01002224/* Tears down the entire software state and most of the hardware state
2225 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002226void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002227{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002228 EFX_ASSERT_RESET_SERIALISED(efx);
2229
Ben Hutchings2467ca42008-09-01 12:48:50 +01002230 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002231 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002232
2233 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002234 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2235 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002236 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002237}
2238
Ben Hutchings2467ca42008-09-01 12:48:50 +01002239/* This function will always ensure that the locks acquired in
2240 * efx_reset_down() are released. A failure return code indicates
2241 * that we were unable to reinitialise the hardware, and the
2242 * driver should be disabled. If ok is false, then the rx and tx
2243 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002244int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002245{
2246 int rc;
2247
Ben Hutchings2467ca42008-09-01 12:48:50 +01002248 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002249
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002250 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002251 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002252 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002253 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002254 }
2255
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002256 if (!ok)
2257 goto fail;
2258
Steve Hodgson4b988282009-01-29 17:50:51 +00002259 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002260 rc = efx->phy_op->init(efx);
2261 if (rc)
2262 goto fail;
2263 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002264 netif_err(efx, drv, efx->net_dev,
2265 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002266 }
2267
Jon Cooper261e4d92013-04-15 18:51:54 +01002268 rc = efx_enable_interrupts(efx);
2269 if (rc)
2270 goto fail;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002271 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002272 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002273
2274 mutex_unlock(&efx->mac_lock);
2275
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002276 efx_start_all(efx);
2277
2278 return 0;
2279
2280fail:
2281 efx->port_initialized = false;
2282
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002283 mutex_unlock(&efx->mac_lock);
2284
Ben Hutchings8ceee662008-04-27 12:55:59 +01002285 return rc;
2286}
2287
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002288/* Reset the NIC using the specified method. Note that the reset may
2289 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002290 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002291 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002292 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002293int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002294{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002295 int rc, rc2;
2296 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002297
Ben Hutchings62776d02010-06-23 11:30:07 +00002298 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2299 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002300
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002301 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002302 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002303
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002304 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002305 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002306 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002307 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002308 }
2309
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002310 /* Clear flags for the scopes we covered. We assume the NIC and
2311 * driver are now quiescent so that there is no race here.
2312 */
2313 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002314
2315 /* Reinitialise bus-mastering, which may have been turned off before
2316 * the reset was scheduled. This is still appropriate, even in the
2317 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2318 * can respond to requests. */
2319 pci_set_master(efx->pci_dev);
2320
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002321out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002322 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002323 disabled = rc ||
2324 method == RESET_TYPE_DISABLE ||
2325 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002326 rc2 = efx_reset_up(efx, method, !disabled);
2327 if (rc2) {
2328 disabled = true;
2329 if (!rc)
2330 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002331 }
2332
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002333 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002334 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002335 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002336 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002337 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002338 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002339 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002340 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002341 return rc;
2342}
2343
Alexandre Rames626950d2013-01-14 17:20:22 +00002344/* Try recovery mechanisms.
2345 * For now only EEH is supported.
2346 * Returns 0 if the recovery mechanisms are unsuccessful.
2347 * Returns a non-zero value otherwise.
2348 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002349int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002350{
2351#ifdef CONFIG_EEH
2352 /* A PCI error can occur and not be seen by EEH because nothing
2353 * happens on the PCI bus. In this case the driver may fail and
2354 * schedule a 'recover or reset', leading to this recovery handler.
2355 * Manually call the eeh failure check function.
2356 */
2357 struct eeh_dev *eehdev =
2358 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2359
2360 if (eeh_dev_check_failure(eehdev)) {
2361 /* The EEH mechanisms will handle the error and reset the
2362 * device if necessary.
2363 */
2364 return 1;
2365 }
2366#endif
2367 return 0;
2368}
2369
Ben Hutchings8ceee662008-04-27 12:55:59 +01002370/* The worker thread exists so that code that cannot sleep can
2371 * schedule a reset for later.
2372 */
2373static void efx_reset_work(struct work_struct *data)
2374{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002375 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002376 unsigned long pending;
2377 enum reset_type method;
2378
2379 pending = ACCESS_ONCE(efx->reset_pending);
2380 method = fls(pending) - 1;
2381
2382 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2383 method == RESET_TYPE_RECOVER_OR_ALL) &&
2384 efx_try_recovery(efx))
2385 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002386
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002387 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002388 return;
2389
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002390 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002391
2392 /* We checked the state in efx_schedule_reset() but it may
2393 * have changed by now. Now that we have the RTNL lock,
2394 * it cannot change again.
2395 */
2396 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002397 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002398
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002399 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002400}
2401
2402void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2403{
2404 enum reset_type method;
2405
Alexandre Rames626950d2013-01-14 17:20:22 +00002406 if (efx->state == STATE_RECOVERY) {
2407 netif_dbg(efx, drv, efx->net_dev,
2408 "recovering: skip scheduling %s reset\n",
2409 RESET_TYPE(type));
2410 return;
2411 }
2412
Ben Hutchings8ceee662008-04-27 12:55:59 +01002413 switch (type) {
2414 case RESET_TYPE_INVISIBLE:
2415 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002416 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002417 case RESET_TYPE_WORLD:
2418 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002419 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002420 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002421 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2422 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002423 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002424 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002425 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002426 netif_dbg(efx, drv, efx->net_dev,
2427 "scheduling %s reset for %s\n",
2428 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002429 break;
2430 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002431
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002432 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002433 smp_mb(); /* ensure we change reset_pending before checking state */
2434
2435 /* If we're not READY then just leave the flags set as the cue
2436 * to abort probing or reschedule the reset later.
2437 */
2438 if (ACCESS_ONCE(efx->state) != STATE_READY)
2439 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002440
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002441 /* efx_process_channel() will no longer read events once a
2442 * reset is scheduled. So switch back to poll'd MCDI completions. */
2443 efx_mcdi_mode_poll(efx);
2444
Steve Hodgson1ab00622008-12-12 21:33:02 -08002445 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002446}
2447
2448/**************************************************************************
2449 *
2450 * List of NICs we support
2451 *
2452 **************************************************************************/
2453
2454/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002455static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002456 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2457 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002458 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002459 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2460 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002461 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002462 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002463 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002464 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002465 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002466 {0} /* end of list */
2467};
2468
2469/**************************************************************************
2470 *
Ben Hutchings37594332009-11-23 16:05:45 +00002471 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002472 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002473 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002474 * Needed so all function pointers are valid and do not have to be tested
2475 * before use
2476 *
2477 **************************************************************************/
2478int efx_port_dummy_op_int(struct efx_nic *efx)
2479{
2480 return 0;
2481}
2482void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002483
2484static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002485{
2486 return false;
2487}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002488
stephen hemminger6c8c2512011-04-14 05:50:12 +00002489static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002490 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002491 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002492 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002493 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002494};
2495
Ben Hutchings8ceee662008-04-27 12:55:59 +01002496/**************************************************************************
2497 *
2498 * Data housekeeping
2499 *
2500 **************************************************************************/
2501
2502/* This zeroes out and then fills in the invariants in a struct
2503 * efx_nic (including all sub-structures).
2504 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002505static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002506 struct pci_dev *pci_dev, struct net_device *net_dev)
2507{
Ben Hutchings46426102010-09-10 06:42:33 +00002508 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002509
2510 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002511 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002512#ifdef CONFIG_SFC_MTD
2513 INIT_LIST_HEAD(&efx->mtd_list);
2514#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002515 INIT_WORK(&efx->reset_work, efx_reset_work);
2516 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002517 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002518 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002519 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002520 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002521 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002522
2523 efx->net_dev = net_dev;
Jon Cooper43a37392012-10-18 15:49:54 +01002524 efx->rx_prefix_size = efx->type->rx_prefix_size;
2525 efx->rx_packet_hash_offset =
2526 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002527 spin_lock_init(&efx->stats_lock);
2528 mutex_init(&efx->mac_lock);
2529 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002530 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002531 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002532 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002533
2534 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002535 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2536 if (!efx->channel[i])
2537 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002538 efx->msi_context[i].efx = efx;
2539 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002540 }
2541
Ben Hutchings8ceee662008-04-27 12:55:59 +01002542 /* Higher numbered interrupt modes are less capable! */
2543 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2544 interrupt_mode);
2545
Ben Hutchings6977dc62008-12-26 13:44:39 -08002546 /* Would be good to use the net_dev name, but we're too early */
2547 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2548 pci_name(pci_dev));
2549 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002550 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002551 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002552
Ben Hutchings8ceee662008-04-27 12:55:59 +01002553 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002554
2555fail:
2556 efx_fini_struct(efx);
2557 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002558}
2559
2560static void efx_fini_struct(struct efx_nic *efx)
2561{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002562 int i;
2563
2564 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2565 kfree(efx->channel[i]);
2566
Ben Hutchings8ceee662008-04-27 12:55:59 +01002567 if (efx->workqueue) {
2568 destroy_workqueue(efx->workqueue);
2569 efx->workqueue = NULL;
2570 }
2571}
2572
2573/**************************************************************************
2574 *
2575 * PCI interface
2576 *
2577 **************************************************************************/
2578
2579/* Main body of final NIC shutdown code
2580 * This is called only at module unload (or hotplug removal).
2581 */
2582static void efx_pci_remove_main(struct efx_nic *efx)
2583{
Ben Hutchings7153f622012-07-27 20:50:52 +01002584 /* Flush reset_work. It can no longer be scheduled since we
2585 * are not READY.
2586 */
2587 BUG_ON(efx->state == STATE_READY);
2588 cancel_work_sync(&efx->reset_work);
2589
Ben Hutchingsd8291182012-10-05 23:35:41 +01002590 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002591 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002592 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002593 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002594 efx_fini_napi(efx);
2595 efx_remove_all(efx);
2596}
2597
2598/* Final NIC shutdown
2599 * This is called only at module unload (or hotplug removal).
2600 */
2601static void efx_pci_remove(struct pci_dev *pci_dev)
2602{
2603 struct efx_nic *efx;
2604
2605 efx = pci_get_drvdata(pci_dev);
2606 if (!efx)
2607 return;
2608
2609 /* Mark the NIC as fini, then stop the interface */
2610 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002611 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002612 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002613 rtnl_unlock();
2614
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002615 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002616 efx_unregister_netdev(efx);
2617
Ben Hutchings7dde5962008-12-12 22:09:38 -08002618 efx_mtd_remove(efx);
2619
Ben Hutchings8ceee662008-04-27 12:55:59 +01002620 efx_pci_remove_main(efx);
2621
Ben Hutchings8ceee662008-04-27 12:55:59 +01002622 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002623 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002624
Ben Hutchings8ceee662008-04-27 12:55:59 +01002625 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002626 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002627 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002628
2629 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002630};
2631
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002632/* NIC VPD information
2633 * Called during probe to display the part number of the
2634 * installed NIC. VPD is potentially very large but this should
2635 * always appear within the first 512 bytes.
2636 */
2637#define SFC_VPD_LEN 512
2638static void efx_print_product_vpd(struct efx_nic *efx)
2639{
2640 struct pci_dev *dev = efx->pci_dev;
2641 char vpd_data[SFC_VPD_LEN];
2642 ssize_t vpd_size;
2643 int i, j;
2644
2645 /* Get the vpd data from the device */
2646 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2647 if (vpd_size <= 0) {
2648 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2649 return;
2650 }
2651
2652 /* Get the Read only section */
2653 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2654 if (i < 0) {
2655 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2656 return;
2657 }
2658
2659 j = pci_vpd_lrdt_size(&vpd_data[i]);
2660 i += PCI_VPD_LRDT_TAG_SIZE;
2661 if (i + j > vpd_size)
2662 j = vpd_size - i;
2663
2664 /* Get the Part number */
2665 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2666 if (i < 0) {
2667 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2668 return;
2669 }
2670
2671 j = pci_vpd_info_field_size(&vpd_data[i]);
2672 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2673 if (i + j > vpd_size) {
2674 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2675 return;
2676 }
2677
2678 netif_info(efx, drv, efx->net_dev,
2679 "Part Number : %.*s\n", j, &vpd_data[i]);
2680}
2681
2682
Ben Hutchings8ceee662008-04-27 12:55:59 +01002683/* Main body of NIC initialisation
2684 * This is called at module load (or hotplug insertion, theoretically).
2685 */
2686static int efx_pci_probe_main(struct efx_nic *efx)
2687{
2688 int rc;
2689
2690 /* Do start-of-day initialisation */
2691 rc = efx_probe_all(efx);
2692 if (rc)
2693 goto fail1;
2694
Ben Hutchingse8f14992010-12-07 19:47:34 +00002695 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002696
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002697 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002698 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002699 netif_err(efx, probe, efx->net_dev,
2700 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002701 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002702 }
2703
2704 rc = efx_init_port(efx);
2705 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002706 netif_err(efx, probe, efx->net_dev,
2707 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002708 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002709 }
2710
Ben Hutchings152b6a62009-11-29 03:43:56 +00002711 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002712 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002713 goto fail5;
Jon Cooper261e4d92013-04-15 18:51:54 +01002714 rc = efx_enable_interrupts(efx);
2715 if (rc)
2716 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002717
2718 return 0;
2719
Jon Cooper261e4d92013-04-15 18:51:54 +01002720 fail6:
2721 efx_nic_fini_interrupt(efx);
Ben Hutchings278c0622009-11-23 16:05:12 +00002722 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002723 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002724 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002725 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002726 fail3:
2727 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002728 efx_remove_all(efx);
2729 fail1:
2730 return rc;
2731}
2732
2733/* NIC initialisation
2734 *
2735 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002736 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002737 * sets up and registers the network devices with the kernel and hooks
2738 * the interrupt service routine. It does not prepare the device for
2739 * transmission; this is left to the first time one of the network
2740 * interfaces is brought up (i.e. efx_net_open).
2741 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002742static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002743 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002744{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002745 struct net_device *net_dev;
2746 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002747 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002748
2749 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002750 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2751 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002752 if (!net_dev)
2753 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002754 efx = netdev_priv(net_dev);
2755 efx->type = (const struct efx_nic_type *) entry->driver_data;
2756 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002757 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002758 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002759 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002760 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002761 /* Mask for features that also apply to VLAN devices */
2762 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002763 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2764 NETIF_F_RXCSUM);
2765 /* All offloads can be toggled */
2766 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002767 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002768 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002769 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002770 if (rc)
2771 goto fail1;
2772
Ben Hutchings62776d02010-06-23 11:30:07 +00002773 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002774 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002775
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002776 efx_print_product_vpd(efx);
2777
Ben Hutchings8ceee662008-04-27 12:55:59 +01002778 /* Set up basic I/O (BAR mappings etc) */
2779 rc = efx_init_io(efx);
2780 if (rc)
2781 goto fail2;
2782
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002783 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002784 if (rc)
2785 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002786
Ben Hutchings8ceee662008-04-27 12:55:59 +01002787 rc = efx_register_netdev(efx);
2788 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002789 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002790
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002791 rc = efx_sriov_init(efx);
2792 if (rc)
2793 netif_err(efx, probe, efx->net_dev,
2794 "SR-IOV can't be enabled rc %d\n", rc);
2795
Ben Hutchings62776d02010-06-23 11:30:07 +00002796 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002797
Ben Hutchings7c431612012-01-27 17:23:58 +00002798 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002799 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002800 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002801 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002802 if (rc)
2803 netif_warn(efx, probe, efx->net_dev,
2804 "failed to create MTDs (%d)\n", rc);
2805
Alexandre Rames626950d2013-01-14 17:20:22 +00002806 rc = pci_enable_pcie_error_reporting(pci_dev);
2807 if (rc && rc != -EINVAL)
2808 netif_warn(efx, probe, efx->net_dev,
2809 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2810
Ben Hutchings8ceee662008-04-27 12:55:59 +01002811 return 0;
2812
Ben Hutchings8ceee662008-04-27 12:55:59 +01002813 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002814 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002815 fail3:
2816 efx_fini_io(efx);
2817 fail2:
2818 efx_fini_struct(efx);
2819 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002820 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002821 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002822 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002823 free_netdev(net_dev);
2824 return rc;
2825}
2826
Ben Hutchings89c758f2009-11-29 03:43:07 +00002827static int efx_pm_freeze(struct device *dev)
2828{
2829 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2830
Ben Hutchings61da0262012-07-27 19:35:39 +01002831 rtnl_lock();
2832
Ben Hutchings6032fb52012-07-27 19:35:47 +01002833 if (efx->state != STATE_DISABLED) {
2834 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002835
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002836 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002837
Ben Hutchings6032fb52012-07-27 19:35:47 +01002838 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002839 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01002840 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002841
Ben Hutchings61da0262012-07-27 19:35:39 +01002842 rtnl_unlock();
2843
Ben Hutchings89c758f2009-11-29 03:43:07 +00002844 return 0;
2845}
2846
2847static int efx_pm_thaw(struct device *dev)
2848{
Jon Cooper261e4d92013-04-15 18:51:54 +01002849 int rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002850 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2851
Ben Hutchings61da0262012-07-27 19:35:39 +01002852 rtnl_lock();
2853
Ben Hutchings6032fb52012-07-27 19:35:47 +01002854 if (efx->state != STATE_DISABLED) {
Jon Cooper261e4d92013-04-15 18:51:54 +01002855 rc = efx_enable_interrupts(efx);
2856 if (rc)
2857 goto fail;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002858
Ben Hutchings6032fb52012-07-27 19:35:47 +01002859 mutex_lock(&efx->mac_lock);
2860 efx->phy_op->reconfigure(efx);
2861 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002862
Ben Hutchings6032fb52012-07-27 19:35:47 +01002863 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002864
Ben Hutchings6032fb52012-07-27 19:35:47 +01002865 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002866
Ben Hutchings6032fb52012-07-27 19:35:47 +01002867 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002868
Ben Hutchings6032fb52012-07-27 19:35:47 +01002869 efx->type->resume_wol(efx);
2870 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002871
Ben Hutchings61da0262012-07-27 19:35:39 +01002872 rtnl_unlock();
2873
Steve Hodgson319ba642010-06-01 11:17:24 +00002874 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2875 queue_work(reset_workqueue, &efx->reset_work);
2876
Ben Hutchings89c758f2009-11-29 03:43:07 +00002877 return 0;
Jon Cooper261e4d92013-04-15 18:51:54 +01002878
2879fail:
2880 rtnl_unlock();
2881
2882 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002883}
2884
2885static int efx_pm_poweroff(struct device *dev)
2886{
2887 struct pci_dev *pci_dev = to_pci_dev(dev);
2888 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2889
2890 efx->type->fini(efx);
2891
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002892 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002893
2894 pci_save_state(pci_dev);
2895 return pci_set_power_state(pci_dev, PCI_D3hot);
2896}
2897
2898/* Used for both resume and restore */
2899static int efx_pm_resume(struct device *dev)
2900{
2901 struct pci_dev *pci_dev = to_pci_dev(dev);
2902 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2903 int rc;
2904
2905 rc = pci_set_power_state(pci_dev, PCI_D0);
2906 if (rc)
2907 return rc;
2908 pci_restore_state(pci_dev);
2909 rc = pci_enable_device(pci_dev);
2910 if (rc)
2911 return rc;
2912 pci_set_master(efx->pci_dev);
2913 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2914 if (rc)
2915 return rc;
2916 rc = efx->type->init(efx);
2917 if (rc)
2918 return rc;
Jon Cooper261e4d92013-04-15 18:51:54 +01002919 rc = efx_pm_thaw(dev);
2920 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002921}
2922
2923static int efx_pm_suspend(struct device *dev)
2924{
2925 int rc;
2926
2927 efx_pm_freeze(dev);
2928 rc = efx_pm_poweroff(dev);
2929 if (rc)
2930 efx_pm_resume(dev);
2931 return rc;
2932}
2933
Ben Hutchings18e83e42012-01-05 19:05:20 +00002934static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002935 .suspend = efx_pm_suspend,
2936 .resume = efx_pm_resume,
2937 .freeze = efx_pm_freeze,
2938 .thaw = efx_pm_thaw,
2939 .poweroff = efx_pm_poweroff,
2940 .restore = efx_pm_resume,
2941};
2942
Alexandre Rames626950d2013-01-14 17:20:22 +00002943/* A PCI error affecting this device was detected.
2944 * At this point MMIO and DMA may be disabled.
2945 * Stop the software path and request a slot reset.
2946 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002947static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2948 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00002949{
2950 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2951 struct efx_nic *efx = pci_get_drvdata(pdev);
2952
2953 if (state == pci_channel_io_perm_failure)
2954 return PCI_ERS_RESULT_DISCONNECT;
2955
2956 rtnl_lock();
2957
2958 if (efx->state != STATE_DISABLED) {
2959 efx->state = STATE_RECOVERY;
2960 efx->reset_pending = 0;
2961
2962 efx_device_detach_sync(efx);
2963
2964 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002965 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00002966
2967 status = PCI_ERS_RESULT_NEED_RESET;
2968 } else {
2969 /* If the interface is disabled we don't want to do anything
2970 * with it.
2971 */
2972 status = PCI_ERS_RESULT_RECOVERED;
2973 }
2974
2975 rtnl_unlock();
2976
2977 pci_disable_device(pdev);
2978
2979 return status;
2980}
2981
2982/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002983static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00002984{
2985 struct efx_nic *efx = pci_get_drvdata(pdev);
2986 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2987 int rc;
2988
2989 if (pci_enable_device(pdev)) {
2990 netif_err(efx, hw, efx->net_dev,
2991 "Cannot re-enable PCI device after reset.\n");
2992 status = PCI_ERS_RESULT_DISCONNECT;
2993 }
2994
2995 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2996 if (rc) {
2997 netif_err(efx, hw, efx->net_dev,
2998 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2999 /* Non-fatal error. Continue. */
3000 }
3001
3002 return status;
3003}
3004
3005/* Perform the actual reset and resume I/O operations. */
3006static void efx_io_resume(struct pci_dev *pdev)
3007{
3008 struct efx_nic *efx = pci_get_drvdata(pdev);
3009 int rc;
3010
3011 rtnl_lock();
3012
3013 if (efx->state == STATE_DISABLED)
3014 goto out;
3015
3016 rc = efx_reset(efx, RESET_TYPE_ALL);
3017 if (rc) {
3018 netif_err(efx, hw, efx->net_dev,
3019 "efx_reset failed after PCI error (%d)\n", rc);
3020 } else {
3021 efx->state = STATE_READY;
3022 netif_dbg(efx, hw, efx->net_dev,
3023 "Done resetting and resuming IO after PCI error.\n");
3024 }
3025
3026out:
3027 rtnl_unlock();
3028}
3029
3030/* For simplicity and reliability, we always require a slot reset and try to
3031 * reset the hardware when a pci error affecting the device is detected.
3032 * We leave both the link_reset and mmio_enabled callback unimplemented:
3033 * with our request for slot reset the mmio_enabled callback will never be
3034 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3035 */
3036static struct pci_error_handlers efx_err_handlers = {
3037 .error_detected = efx_io_error_detected,
3038 .slot_reset = efx_io_slot_reset,
3039 .resume = efx_io_resume,
3040};
3041
Ben Hutchings8ceee662008-04-27 12:55:59 +01003042static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003043 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003044 .id_table = efx_pci_table,
3045 .probe = efx_pci_probe,
3046 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003047 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003048 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003049};
3050
3051/**************************************************************************
3052 *
3053 * Kernel module interface
3054 *
3055 *************************************************************************/
3056
3057module_param(interrupt_mode, uint, 0444);
3058MODULE_PARM_DESC(interrupt_mode,
3059 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3060
3061static int __init efx_init_module(void)
3062{
3063 int rc;
3064
3065 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3066
3067 rc = register_netdevice_notifier(&efx_netdev_notifier);
3068 if (rc)
3069 goto err_notifier;
3070
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003071 rc = efx_init_sriov();
3072 if (rc)
3073 goto err_sriov;
3074
Steve Hodgson1ab00622008-12-12 21:33:02 -08003075 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3076 if (!reset_workqueue) {
3077 rc = -ENOMEM;
3078 goto err_reset;
3079 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003080
3081 rc = pci_register_driver(&efx_pci_driver);
3082 if (rc < 0)
3083 goto err_pci;
3084
3085 return 0;
3086
3087 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003088 destroy_workqueue(reset_workqueue);
3089 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003090 efx_fini_sriov();
3091 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003092 unregister_netdevice_notifier(&efx_netdev_notifier);
3093 err_notifier:
3094 return rc;
3095}
3096
3097static void __exit efx_exit_module(void)
3098{
3099 printk(KERN_INFO "Solarflare NET driver unloading\n");
3100
3101 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003102 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003103 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003104 unregister_netdevice_notifier(&efx_netdev_notifier);
3105
3106}
3107
3108module_init(efx_init_module);
3109module_exit(efx_exit_module);
3110
Ben Hutchings906bb262009-11-29 15:16:19 +00003111MODULE_AUTHOR("Solarflare Communications and "
3112 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003113MODULE_DESCRIPTION("Solarflare Communications network driver");
3114MODULE_LICENSE("GPL");
3115MODULE_DEVICE_TABLE(pci, efx_pci_table);