blob: 69150fa1459b9fd738f4084f00bd7a4b6b047e87 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchings0a6f40c2011-02-25 00:01:34 +00004 * Copyright 2005-2011 Solarflare Communications Inc.
Ben Hutchings8ceee662008-04-27 12:55:59 +01005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/delay.h>
16#include <linux/notifier.h>
17#include <linux/ip.h>
18#include <linux/tcp.h>
19#include <linux/in.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010020#include <linux/ethtool.h>
Ben Hutchingsaa6ef272008-07-18 19:03:10 +010021#include <linux/topology.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Alexandre Rames626950d2013-01-14 17:20:22 +000023#include <linux/aer.h>
Alexandre Ramesb28405b2013-03-21 16:41:43 +000024#include <linux/interrupt.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010025#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000027#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000028#include "selftest.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010029
Ben Hutchings8880f4e2009-11-29 15:15:41 +000030#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000031#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000032
Ben Hutchingsc4593022009-11-23 16:08:17 +000033/**************************************************************************
34 *
35 * Type name strings
36 *
37 **************************************************************************
38 */
39
40/* Loopback mode names (see LOOPBACK_MODE()) */
41const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000042const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000043 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000044 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000045 [LOOPBACK_GMAC] = "GMAC",
46 [LOOPBACK_XGMII] = "XGMII",
47 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000048 [LOOPBACK_XAUI] = "XAUI",
49 [LOOPBACK_GMII] = "GMII",
50 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000051 [LOOPBACK_XGBR] = "XGBR",
52 [LOOPBACK_XFI] = "XFI",
53 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
54 [LOOPBACK_GMII_FAR] = "GMII_FAR",
55 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
56 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000057 [LOOPBACK_GPHY] = "GPHY",
58 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000059 [LOOPBACK_PCS] = "PCS",
60 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000061 [LOOPBACK_XPORT] = "XPORT",
62 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000063 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000064 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
65 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000066 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000067 [LOOPBACK_XFI_WS] = "XFI_WS",
68 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000069 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000070};
71
Ben Hutchingsc4593022009-11-23 16:08:17 +000072const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000073const char *const efx_reset_type_names[] = {
Alexandre Rames626950d2013-01-14 17:20:22 +000074 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
75 [RESET_TYPE_ALL] = "ALL",
76 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
77 [RESET_TYPE_WORLD] = "WORLD",
78 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
79 [RESET_TYPE_DISABLE] = "DISABLE",
80 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
81 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
82 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
Alexandre Rames3de82b92013-06-13 11:36:15 +010083 [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
Alexandre Rames626950d2013-01-14 17:20:22 +000084 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
85 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Ben Hutchingsc4593022009-11-23 16:08:17 +000086};
87
Steve Hodgson1ab00622008-12-12 21:33:02 -080088/* Reset workqueue. If any NIC has a hardware failure then a reset will be
89 * queued onto this work queue. This is not a per-nic work queue, because
90 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
91 */
92static struct workqueue_struct *reset_workqueue;
93
Ben Hutchings8ceee662008-04-27 12:55:59 +010094/**************************************************************************
95 *
96 * Configurable values
97 *
98 *************************************************************************/
99
100/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100101 * Use separate channels for TX and RX events
102 *
Neil Turton28b581a2008-12-12 21:41:06 -0800103 * Set this to 1 to use separate channels for TX and RX. It allows us
104 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100105 *
Neil Turton28b581a2008-12-12 21:41:06 -0800106 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100107 */
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000108static bool separate_tx_channels;
109module_param(separate_tx_channels, bool, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800110MODULE_PARM_DESC(separate_tx_channels,
111 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100112
113/* This is the weight assigned to each of the (per-channel) virtual
114 * NAPI devices.
115 */
116static int napi_weight = 64;
117
118/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000119 * monitor.
120 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000121 * - Check the on-board hardware monitor;
122 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000123 * On Siena-based NICs for power systems with EEH support, this will give EEH a
124 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100125 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000126static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100127
Ben Hutchings8ceee662008-04-27 12:55:59 +0100128/* Initial interrupt moderation settings. They can be modified after
129 * module load with ethtool.
130 *
131 * The default for RX should strike a balance between increasing the
132 * round-trip latency and reducing overhead.
133 */
134static unsigned int rx_irq_mod_usec = 60;
135
136/* Initial interrupt moderation settings. They can be modified after
137 * module load with ethtool.
138 *
139 * This default is chosen to ensure that a 10G link does not go idle
140 * while a TX queue is stopped after it has become full. A queue is
141 * restarted when it drops below half full. The time this takes (assuming
142 * worst case 3 descriptors per packet and 1024 descriptors) is
143 * 512 / 3 * 1.2 = 205 usec.
144 */
145static unsigned int tx_irq_mod_usec = 150;
146
147/* This is the first interrupt mode to try out of:
148 * 0 => MSI-X
149 * 1 => MSI
150 * 2 => legacy
151 */
152static unsigned int interrupt_mode;
153
154/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
155 * i.e. the number of CPUs among which we may distribute simultaneous
156 * interrupt handling.
157 *
158 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000159 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100160 */
161static unsigned int rss_cpus;
162module_param(rss_cpus, uint, 0444);
163MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
164
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000165static bool phy_flash_cfg;
166module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800167MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
168
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000169static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000170module_param(irq_adapt_low_thresh, uint, 0644);
171MODULE_PARM_DESC(irq_adapt_low_thresh,
172 "Threshold score for reducing IRQ moderation");
173
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000174static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000175module_param(irq_adapt_high_thresh, uint, 0644);
176MODULE_PARM_DESC(irq_adapt_high_thresh,
177 "Threshold score for increasing IRQ moderation");
178
Ben Hutchings62776d02010-06-23 11:30:07 +0000179static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
180 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
181 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
182 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
183module_param(debug, uint, 0);
184MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
185
Ben Hutchings8ceee662008-04-27 12:55:59 +0100186/**************************************************************************
187 *
188 * Utility functions and prototypes
189 *
190 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000191
Ben Hutchingsd8291182012-10-05 23:35:41 +0100192static void efx_soft_enable_interrupts(struct efx_nic *efx);
193static void efx_soft_disable_interrupts(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000194static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000195static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000196static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100197static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000198static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100199static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000200static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000201static void efx_fini_struct(struct efx_nic *efx);
202static void efx_start_all(struct efx_nic *efx);
203static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100204
205#define EFX_ASSERT_RESET_SERIALISED(efx) \
206 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100207 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000208 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000209 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100210 ASSERT_RTNL(); \
211 } while (0)
212
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100213static int efx_check_disabled(struct efx_nic *efx)
214{
Alexandre Rames626950d2013-01-14 17:20:22 +0000215 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100216 netif_err(efx, drv, efx->net_dev,
217 "device is disabled due to earlier errors\n");
218 return -EIO;
219 }
220 return 0;
221}
222
Ben Hutchings8ceee662008-04-27 12:55:59 +0100223/**************************************************************************
224 *
225 * Event queue processing
226 *
227 *************************************************************************/
228
229/* Process channel's event queue
230 *
231 * This function is responsible for processing the event queue of a
232 * single channel. The caller must guarantee that this function will
233 * never be concurrently called more than once on the same channel,
234 * though different channels may be being processed concurrently.
235 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000236static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100237{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000238 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100239
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000240 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100241 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100242
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000243 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000244 if (spent && efx_channel_has_rx_queue(channel)) {
245 struct efx_rx_queue *rx_queue =
246 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100247
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000248 efx_rx_flush_packet(channel);
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100249 efx_fast_push_rx_descriptors(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100250 }
251
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000252 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100253}
254
Ben Hutchings8ceee662008-04-27 12:55:59 +0100255/* NAPI poll handler
256 *
257 * NAPI guarantees serialisation of polls of the same device, which
258 * provides the guarantee required by efx_process_channel().
259 */
260static int efx_poll(struct napi_struct *napi, int budget)
261{
262 struct efx_channel *channel =
263 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000264 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000265 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100266
Ben Hutchings62776d02010-06-23 11:30:07 +0000267 netif_vdbg(efx, intr, efx->net_dev,
268 "channel %d NAPI poll executing on CPU %d\n",
269 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100270
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000271 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100272
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000273 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000274 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000275 efx->irq_rx_adaptive &&
276 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000277 if (unlikely(channel->irq_mod_score <
278 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000279 if (channel->irq_moderation > 1) {
280 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000281 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000282 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000283 } else if (unlikely(channel->irq_mod_score >
284 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000285 if (channel->irq_moderation <
286 efx->irq_rx_moderation) {
287 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000288 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000289 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000290 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000291 channel->irq_count = 0;
292 channel->irq_mod_score = 0;
293 }
294
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000295 efx_filter_rfs_expire(channel);
296
Ben Hutchings8ceee662008-04-27 12:55:59 +0100297 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800298 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100299 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100300 * interrupts have already been disabled.
301 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800302 napi_complete(napi);
Ben Hutchings514bedb2012-10-05 19:30:16 +0100303 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100304 }
305
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000306 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100307}
308
Ben Hutchings8ceee662008-04-27 12:55:59 +0100309/* Create event queue
310 * Event queue memory allocations are done only once. If the channel
311 * is reset, the memory buffer will be reused; this guards against
312 * errors during channel reset and also simplifies interrupt handling.
313 */
314static int efx_probe_eventq(struct efx_channel *channel)
315{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000316 struct efx_nic *efx = channel->efx;
317 unsigned long entries;
318
Ben Hutchings86ee5302012-01-09 19:51:22 +0000319 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000320 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100321
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000322 /* Build an event queue with room for one event per tx and rx buffer,
323 * plus some extra for link state events and MCDI completions. */
324 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
325 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
326 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
327
Ben Hutchings152b6a62009-11-29 03:43:56 +0000328 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100329}
330
331/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100332static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100333{
Ben Hutchings62776d02010-06-23 11:30:07 +0000334 netif_dbg(channel->efx, drv, channel->efx->net_dev,
335 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100336
337 channel->eventq_read_ptr = 0;
338
Ben Hutchings152b6a62009-11-29 03:43:56 +0000339 efx_nic_init_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100340 channel->eventq_init = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100341}
342
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000343/* Enable event queue processing and NAPI */
344static void efx_start_eventq(struct efx_channel *channel)
345{
346 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
347 "chan %d start event queue\n", channel->channel);
348
Ben Hutchings514bedb2012-10-05 19:30:16 +0100349 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000350 channel->enabled = true;
351 smp_wmb();
352
353 napi_enable(&channel->napi_str);
354 efx_nic_eventq_read_ack(channel);
355}
356
357/* Disable event queue processing and NAPI */
358static void efx_stop_eventq(struct efx_channel *channel)
359{
360 if (!channel->enabled)
361 return;
362
363 napi_disable(&channel->napi_str);
364 channel->enabled = false;
365}
366
Ben Hutchings8ceee662008-04-27 12:55:59 +0100367static void efx_fini_eventq(struct efx_channel *channel)
368{
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100369 if (!channel->eventq_init)
370 return;
371
Ben Hutchings62776d02010-06-23 11:30:07 +0000372 netif_dbg(channel->efx, drv, channel->efx->net_dev,
373 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100374
Ben Hutchings152b6a62009-11-29 03:43:56 +0000375 efx_nic_fini_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100376 channel->eventq_init = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100377}
378
379static void efx_remove_eventq(struct efx_channel *channel)
380{
Ben Hutchings62776d02010-06-23 11:30:07 +0000381 netif_dbg(channel->efx, drv, channel->efx->net_dev,
382 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100383
Ben Hutchings152b6a62009-11-29 03:43:56 +0000384 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100385}
386
387/**************************************************************************
388 *
389 * Channel handling
390 *
391 *************************************************************************/
392
Ben Hutchings7f967c02012-02-13 23:45:02 +0000393/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000394static struct efx_channel *
395efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
396{
397 struct efx_channel *channel;
398 struct efx_rx_queue *rx_queue;
399 struct efx_tx_queue *tx_queue;
400 int j;
401
Ben Hutchings7f967c02012-02-13 23:45:02 +0000402 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
403 if (!channel)
404 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000405
Ben Hutchings7f967c02012-02-13 23:45:02 +0000406 channel->efx = efx;
407 channel->channel = i;
408 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000409
Ben Hutchings7f967c02012-02-13 23:45:02 +0000410 for (j = 0; j < EFX_TXQ_TYPES; j++) {
411 tx_queue = &channel->tx_queue[j];
412 tx_queue->efx = efx;
413 tx_queue->queue = i * EFX_TXQ_TYPES + j;
414 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000415 }
416
Ben Hutchings46426102010-09-10 06:42:33 +0000417 rx_queue = &channel->rx_queue;
418 rx_queue->efx = efx;
419 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
420 (unsigned long)rx_queue);
421
422 return channel;
423}
424
Ben Hutchings7f967c02012-02-13 23:45:02 +0000425/* Allocate and initialise a channel structure, copying parameters
426 * (but not resources) from an old channel structure.
427 */
428static struct efx_channel *
429efx_copy_channel(const struct efx_channel *old_channel)
430{
431 struct efx_channel *channel;
432 struct efx_rx_queue *rx_queue;
433 struct efx_tx_queue *tx_queue;
434 int j;
435
436 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
437 if (!channel)
438 return NULL;
439
440 *channel = *old_channel;
441
442 channel->napi_dev = NULL;
443 memset(&channel->eventq, 0, sizeof(channel->eventq));
444
445 for (j = 0; j < EFX_TXQ_TYPES; j++) {
446 tx_queue = &channel->tx_queue[j];
447 if (tx_queue->channel)
448 tx_queue->channel = channel;
449 tx_queue->buffer = NULL;
450 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
451 }
452
453 rx_queue = &channel->rx_queue;
454 rx_queue->buffer = NULL;
455 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
456 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
457 (unsigned long)rx_queue);
458
459 return channel;
460}
461
Ben Hutchings8ceee662008-04-27 12:55:59 +0100462static int efx_probe_channel(struct efx_channel *channel)
463{
464 struct efx_tx_queue *tx_queue;
465 struct efx_rx_queue *rx_queue;
466 int rc;
467
Ben Hutchings62776d02010-06-23 11:30:07 +0000468 netif_dbg(channel->efx, probe, channel->efx->net_dev,
469 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100470
Ben Hutchings7f967c02012-02-13 23:45:02 +0000471 rc = channel->type->pre_probe(channel);
472 if (rc)
473 goto fail;
474
Ben Hutchings8ceee662008-04-27 12:55:59 +0100475 rc = efx_probe_eventq(channel);
476 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000477 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100478
479 efx_for_each_channel_tx_queue(tx_queue, channel) {
480 rc = efx_probe_tx_queue(tx_queue);
481 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000482 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100483 }
484
485 efx_for_each_channel_rx_queue(rx_queue, channel) {
486 rc = efx_probe_rx_queue(rx_queue);
487 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000488 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100489 }
490
491 channel->n_rx_frm_trunc = 0;
492
493 return 0;
494
Ben Hutchings7f967c02012-02-13 23:45:02 +0000495fail:
496 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100497 return rc;
498}
499
Ben Hutchings7f967c02012-02-13 23:45:02 +0000500static void
501efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
502{
503 struct efx_nic *efx = channel->efx;
504 const char *type;
505 int number;
506
507 number = channel->channel;
508 if (efx->tx_channel_offset == 0) {
509 type = "";
510 } else if (channel->channel < efx->tx_channel_offset) {
511 type = "-rx";
512 } else {
513 type = "-tx";
514 number -= efx->tx_channel_offset;
515 }
516 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
517}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100518
Ben Hutchings56536e92008-12-12 21:37:02 -0800519static void efx_set_channel_names(struct efx_nic *efx)
520{
521 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800522
Ben Hutchings7f967c02012-02-13 23:45:02 +0000523 efx_for_each_channel(channel, efx)
524 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100525 efx->msi_context[channel->channel].name,
526 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800527}
528
Ben Hutchings46426102010-09-10 06:42:33 +0000529static int efx_probe_channels(struct efx_nic *efx)
530{
531 struct efx_channel *channel;
532 int rc;
533
534 /* Restart special buffer allocation */
535 efx->next_buffer_table = 0;
536
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000537 /* Probe channels in reverse, so that any 'extra' channels
538 * use the start of the buffer table. This allows the traffic
539 * channels to be resized without moving them or wasting the
540 * entries before them.
541 */
542 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000543 rc = efx_probe_channel(channel);
544 if (rc) {
545 netif_err(efx, probe, efx->net_dev,
546 "failed to create channel %d\n",
547 channel->channel);
548 goto fail;
549 }
550 }
551 efx_set_channel_names(efx);
552
553 return 0;
554
555fail:
556 efx_remove_channels(efx);
557 return rc;
558}
559
Ben Hutchings8ceee662008-04-27 12:55:59 +0100560/* Channels are shutdown and reinitialised whilst the NIC is running
561 * to propagate configuration changes (mtu, checksum offload), or
562 * to clear hardware error conditions
563 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000564static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100565{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000566 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100567 struct efx_tx_queue *tx_queue;
568 struct efx_rx_queue *rx_queue;
569 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000570 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100571
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100572 /* Calculate the rx buffer allocation parameters required to
573 * support the current MTU, including padding for header
574 * alignment and overruns.
575 */
Jon Cooper43a37392012-10-18 15:49:54 +0100576 efx->rx_dma_len = (efx->rx_prefix_size +
Ben Hutchings272baee2013-01-29 23:33:14 +0000577 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
578 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000579 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Ben Hutchingsc14ff2e2013-05-13 11:58:31 +0000580 NET_IP_ALIGN + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000581 if (rx_buf_len <= PAGE_SIZE) {
582 efx->rx_scatter = false;
583 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000584 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000585 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000586 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000587 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
588 EFX_RX_BUF_ALIGNMENT) >
589 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000590 efx->rx_scatter = true;
591 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
592 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000593 } else {
594 efx->rx_scatter = false;
595 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000596 }
597
Daniel Pieczko1648a232013-02-13 10:54:41 +0000598 efx_rx_config_page_split(efx);
599 if (efx->rx_buffer_order)
600 netif_dbg(efx, drv, efx->net_dev,
601 "RX buf len=%u; page order=%u batch=%u\n",
602 efx->rx_dma_len, efx->rx_buffer_order,
603 efx->rx_pages_per_batch);
604 else
605 netif_dbg(efx, drv, efx->net_dev,
606 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
607 efx->rx_dma_len, efx->rx_page_buf_step,
608 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000609
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000610 /* RX filters also have scatter-enabled flags */
611 if (efx->rx_scatter != old_rx_scatter)
Ben Hutchingsadd72472012-11-08 01:46:53 +0000612 efx->type->filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100613
Ben Hutchings14bf7182012-05-22 01:27:58 +0100614 /* We must keep at least one descriptor in a TX ring empty.
615 * We could avoid this when the queue size does not exactly
616 * match the hardware ring size, but it's not that important.
617 * Therefore we stop the queue when one more skb might fill
618 * the ring completely. We wake it when half way back to
619 * empty.
620 */
621 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
622 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
623
Ben Hutchings8ceee662008-04-27 12:55:59 +0100624 /* Initialise the channels */
625 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100626 efx_for_each_channel_tx_queue(tx_queue, channel)
627 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100628
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000629 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100630 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000631 efx_nic_generate_fill_event(rx_queue);
632 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100633
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000634 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100635 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000636
637 if (netif_device_present(efx->net_dev))
638 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100639}
640
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000641static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100642{
643 struct efx_channel *channel;
644 struct efx_tx_queue *tx_queue;
645 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100646 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100647
648 EFX_ASSERT_RESET_SERIALISED(efx);
649 BUG_ON(efx->port_enabled);
650
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100651 /* Stop RX refill */
652 efx_for_each_channel(channel, efx) {
653 efx_for_each_channel_rx_queue(rx_queue, channel)
654 rx_queue->refill_enabled = false;
655 }
656
Ben Hutchings8ceee662008-04-27 12:55:59 +0100657 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000658 /* RX packet processing is pipelined, so wait for the
659 * NAPI handler to complete. At least event queue 0
660 * might be kept active by non-data events, so don't
661 * use napi_synchronize() but actually disable NAPI
662 * temporarily.
663 */
664 if (efx_channel_has_rx_queue(channel)) {
665 efx_stop_eventq(channel);
666 efx_start_eventq(channel);
667 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100668 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100669
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100670 rc = efx->type->fini_dmaq(efx);
671 if (rc && EFX_WORKAROUND_7803(efx)) {
672 /* Schedule a reset to recover from the flush failure. The
673 * descriptor caches reference memory we're about to free,
674 * but falcon_reconfigure_mac_wrapper() won't reconnect
675 * the MACs because of the pending reset.
676 */
677 netif_err(efx, drv, efx->net_dev,
678 "Resetting to recover from flush failure\n");
679 efx_schedule_reset(efx, RESET_TYPE_ALL);
680 } else if (rc) {
681 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
682 } else {
683 netif_dbg(efx, drv, efx->net_dev,
684 "successfully flushed all queues\n");
685 }
686
687 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100688 efx_for_each_channel_rx_queue(rx_queue, channel)
689 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000690 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100691 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100692 }
693}
694
695static void efx_remove_channel(struct efx_channel *channel)
696{
697 struct efx_tx_queue *tx_queue;
698 struct efx_rx_queue *rx_queue;
699
Ben Hutchings62776d02010-06-23 11:30:07 +0000700 netif_dbg(channel->efx, drv, channel->efx->net_dev,
701 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100702
703 efx_for_each_channel_rx_queue(rx_queue, channel)
704 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000705 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100706 efx_remove_tx_queue(tx_queue);
707 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100708 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100709}
710
Ben Hutchings46426102010-09-10 06:42:33 +0000711static void efx_remove_channels(struct efx_nic *efx)
712{
713 struct efx_channel *channel;
714
715 efx_for_each_channel(channel, efx)
716 efx_remove_channel(channel);
717}
718
719int
720efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
721{
722 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
723 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000724 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100725 int rc;
726
727 rc = efx_check_disabled(efx);
728 if (rc)
729 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000730
731 /* Not all channels should be reallocated. We must avoid
732 * reallocating their buffer table entries.
733 */
734 efx_for_each_channel(channel, efx) {
735 struct efx_rx_queue *rx_queue;
736 struct efx_tx_queue *tx_queue;
737
738 if (channel->type->copy)
739 continue;
740 next_buffer_table = max(next_buffer_table,
741 channel->eventq.index +
742 channel->eventq.entries);
743 efx_for_each_channel_rx_queue(rx_queue, channel)
744 next_buffer_table = max(next_buffer_table,
745 rx_queue->rxd.index +
746 rx_queue->rxd.entries);
747 efx_for_each_channel_tx_queue(tx_queue, channel)
748 next_buffer_table = max(next_buffer_table,
749 tx_queue->txd.index +
750 tx_queue->txd.entries);
751 }
Ben Hutchings46426102010-09-10 06:42:33 +0000752
Ben Hutchings29c69a42013-01-28 19:01:06 +0000753 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000754 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100755 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000756
Ben Hutchings7f967c02012-02-13 23:45:02 +0000757 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000758 memset(other_channel, 0, sizeof(other_channel));
759 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000760 channel = efx->channel[i];
761 if (channel->type->copy)
762 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000763 if (!channel) {
764 rc = -ENOMEM;
765 goto out;
766 }
767 other_channel[i] = channel;
768 }
769
770 /* Swap entry counts and channel pointers */
771 old_rxq_entries = efx->rxq_entries;
772 old_txq_entries = efx->txq_entries;
773 efx->rxq_entries = rxq_entries;
774 efx->txq_entries = txq_entries;
775 for (i = 0; i < efx->n_channels; i++) {
776 channel = efx->channel[i];
777 efx->channel[i] = other_channel[i];
778 other_channel[i] = channel;
779 }
780
Ben Hutchings7f967c02012-02-13 23:45:02 +0000781 /* Restart buffer table allocation */
782 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000783
Ben Hutchingse8f14992010-12-07 19:47:34 +0000784 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000785 channel = efx->channel[i];
786 if (!channel->type->copy)
787 continue;
788 rc = efx_probe_channel(channel);
789 if (rc)
790 goto rollback;
791 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000792 }
Ben Hutchings46426102010-09-10 06:42:33 +0000793
Ben Hutchings7f967c02012-02-13 23:45:02 +0000794out:
795 /* Destroy unused channel structures */
796 for (i = 0; i < efx->n_channels; i++) {
797 channel = other_channel[i];
798 if (channel && channel->type->copy) {
799 efx_fini_napi_channel(channel);
800 efx_remove_channel(channel);
801 kfree(channel);
802 }
803 }
804
Ben Hutchingsd8291182012-10-05 23:35:41 +0100805 efx_soft_enable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000806 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000807 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000808 return rc;
809
810rollback:
811 /* Swap back */
812 efx->rxq_entries = old_rxq_entries;
813 efx->txq_entries = old_txq_entries;
814 for (i = 0; i < efx->n_channels; i++) {
815 channel = efx->channel[i];
816 efx->channel[i] = other_channel[i];
817 other_channel[i] = channel;
818 }
819 goto out;
820}
821
Steve Hodgson90d683a2010-06-01 11:19:39 +0000822void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100823{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000824 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100825}
826
Ben Hutchings7f967c02012-02-13 23:45:02 +0000827static const struct efx_channel_type efx_default_channel_type = {
828 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100829 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000830 .get_name = efx_get_channel_name,
831 .copy = efx_copy_channel,
832 .keep_eventq = false,
833};
834
835int efx_channel_dummy_op_int(struct efx_channel *channel)
836{
837 return 0;
838}
839
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100840void efx_channel_dummy_op_void(struct efx_channel *channel)
841{
842}
843
Ben Hutchings8ceee662008-04-27 12:55:59 +0100844/**************************************************************************
845 *
846 * Port handling
847 *
848 **************************************************************************/
849
850/* This ensures that the kernel is kept informed (via
851 * netif_carrier_on/off) of the link status, and also maintains the
852 * link status's stop on the port's TX queue.
853 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000854void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100855{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000856 struct efx_link_state *link_state = &efx->link_state;
857
Ben Hutchings8ceee662008-04-27 12:55:59 +0100858 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
859 * that no events are triggered between unregister_netdev() and the
860 * driver unloading. A more general condition is that NETDEV_CHANGE
861 * can only be generated between NETDEV_UP and NETDEV_DOWN */
862 if (!netif_running(efx->net_dev))
863 return;
864
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000865 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100866 efx->n_link_state_changes++;
867
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000868 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100869 netif_carrier_on(efx->net_dev);
870 else
871 netif_carrier_off(efx->net_dev);
872 }
873
874 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000875 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000876 netif_info(efx, link, efx->net_dev,
Ben Hutchings964e6132012-11-19 23:08:22 +0000877 "link up at %uMbps %s-duplex (MTU %d)\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000878 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings964e6132012-11-19 23:08:22 +0000879 efx->net_dev->mtu);
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000880 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000881 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100882}
883
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000884void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
885{
886 efx->link_advertising = advertising;
887 if (advertising) {
888 if (advertising & ADVERTISED_Pause)
889 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
890 else
891 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
892 if (advertising & ADVERTISED_Asym_Pause)
893 efx->wanted_fc ^= EFX_FC_TX;
894 }
895}
896
David S. Millerb5626942011-05-17 17:53:22 -0400897void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000898{
899 efx->wanted_fc = wanted_fc;
900 if (efx->link_advertising) {
901 if (wanted_fc & EFX_FC_RX)
902 efx->link_advertising |= (ADVERTISED_Pause |
903 ADVERTISED_Asym_Pause);
904 else
905 efx->link_advertising &= ~(ADVERTISED_Pause |
906 ADVERTISED_Asym_Pause);
907 if (wanted_fc & EFX_FC_TX)
908 efx->link_advertising ^= ADVERTISED_Asym_Pause;
909 }
910}
911
Ben Hutchings115122a2009-03-04 09:52:52 +0000912static void efx_fini_port(struct efx_nic *efx);
913
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000914/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
915 * the MAC appropriately. All other PHY configuration changes are pushed
916 * through phy_op->set_settings(), and pushed asynchronously to the MAC
917 * through efx_monitor().
918 *
919 * Callers must hold the mac_lock
920 */
921int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100922{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000923 enum efx_phy_mode phy_mode;
924 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100925
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000926 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100927
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000928 /* Disable PHY transmit in mac level loopbacks */
929 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800930 if (LOOPBACK_INTERNAL(efx))
931 efx->phy_mode |= PHY_MODE_TX_DISABLED;
932 else
933 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800934
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000935 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800936
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000937 if (rc)
938 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100939
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000940 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100941}
942
943/* Reinitialise the MAC to pick up new PHY settings, even if the port is
944 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000945int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100946{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000947 int rc;
948
Ben Hutchings8ceee662008-04-27 12:55:59 +0100949 EFX_ASSERT_RESET_SERIALISED(efx);
950
951 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000952 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100953 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000954
955 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100956}
957
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000958/* Asynchronous work item for changing MAC promiscuity and multicast
959 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
960 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800961static void efx_mac_work(struct work_struct *data)
962{
963 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
964
965 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100966 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100967 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800968 mutex_unlock(&efx->mac_lock);
969}
970
Ben Hutchings8ceee662008-04-27 12:55:59 +0100971static int efx_probe_port(struct efx_nic *efx)
972{
973 int rc;
974
Ben Hutchings62776d02010-06-23 11:30:07 +0000975 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100976
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000977 if (phy_flash_cfg)
978 efx->phy_mode = PHY_MODE_SPECIAL;
979
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000980 /* Connect up MAC/PHY operations table */
981 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100982 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000983 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100984
Ben Hutchingse332bcb2011-12-20 01:22:51 +0000985 /* Initialise MAC address to permanent address */
986 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100987
988 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100989}
990
991static int efx_init_port(struct efx_nic *efx)
992{
993 int rc;
994
Ben Hutchings62776d02010-06-23 11:30:07 +0000995 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100996
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000997 mutex_lock(&efx->mac_lock);
998
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800999 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001000 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001001 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001002
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001003 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001004
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001005 /* Reconfigure the MAC before creating dma queues (required for
1006 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001007 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001008
1009 /* Ensure the PHY advertises the correct flow control settings */
1010 rc = efx->phy_op->reconfigure(efx);
1011 if (rc)
1012 goto fail2;
1013
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001014 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001015 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001016
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001017fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001018 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001019fail1:
1020 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001021 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001022}
1023
Ben Hutchings8ceee662008-04-27 12:55:59 +01001024static void efx_start_port(struct efx_nic *efx)
1025{
Ben Hutchings62776d02010-06-23 11:30:07 +00001026 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001027 BUG_ON(efx->port_enabled);
1028
1029 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001030 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001031
1032 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1033 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001034 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001035
Ben Hutchings8ceee662008-04-27 12:55:59 +01001036 mutex_unlock(&efx->mac_lock);
1037}
1038
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001039/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001040static void efx_stop_port(struct efx_nic *efx)
1041{
Ben Hutchings62776d02010-06-23 11:30:07 +00001042 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001043
1044 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001045 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001046 mutex_unlock(&efx->mac_lock);
1047
1048 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001049 netif_addr_lock_bh(efx->net_dev);
1050 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001051}
1052
1053static void efx_fini_port(struct efx_nic *efx)
1054{
Ben Hutchings62776d02010-06-23 11:30:07 +00001055 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001056
1057 if (!efx->port_initialized)
1058 return;
1059
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001060 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001061 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001062
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001063 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001064 efx_link_status_changed(efx);
1065}
1066
1067static void efx_remove_port(struct efx_nic *efx)
1068{
Ben Hutchings62776d02010-06-23 11:30:07 +00001069 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001070
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001071 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001072}
1073
1074/**************************************************************************
1075 *
1076 * NIC handling
1077 *
1078 **************************************************************************/
1079
1080/* This configures the PCI device to enable I/O and DMA. */
1081static int efx_init_io(struct efx_nic *efx)
1082{
1083 struct pci_dev *pci_dev = efx->pci_dev;
1084 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001085 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001086 int rc;
1087
Ben Hutchings62776d02010-06-23 11:30:07 +00001088 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001089
1090 rc = pci_enable_device(pci_dev);
1091 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001092 netif_err(efx, probe, efx->net_dev,
1093 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001094 goto fail1;
1095 }
1096
1097 pci_set_master(pci_dev);
1098
1099 /* Set the PCI DMA mask. Try all possibilities from our
1100 * genuine mask down to 32 bits, because some architectures
1101 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1102 * masks event though they reject 46 bit masks.
1103 */
1104 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001105 if (dma_supported(&pci_dev->dev, dma_mask)) {
1106 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001107 if (rc == 0)
1108 break;
1109 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001110 dma_mask >>= 1;
1111 }
1112 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001113 netif_err(efx, probe, efx->net_dev,
1114 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001115 goto fail2;
1116 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001117 netif_dbg(efx, probe, efx->net_dev,
1118 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001119 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001120 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001121 /* dma_set_coherent_mask() is not *allowed* to
1122 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001123 * but just in case...
1124 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001125 netif_err(efx, probe, efx->net_dev,
1126 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001127 goto fail2;
1128 }
1129
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001130 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1131 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001132 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001133 netif_err(efx, probe, efx->net_dev,
1134 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001135 rc = -EIO;
1136 goto fail3;
1137 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001138 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001139 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001140 netif_err(efx, probe, efx->net_dev,
1141 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001142 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001143 rc = -ENOMEM;
1144 goto fail4;
1145 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001146 netif_dbg(efx, probe, efx->net_dev,
1147 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001148 (unsigned long long)efx->membase_phys, mem_map_size,
1149 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001150
1151 return 0;
1152
1153 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001154 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001155 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001156 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001157 fail2:
1158 pci_disable_device(efx->pci_dev);
1159 fail1:
1160 return rc;
1161}
1162
1163static void efx_fini_io(struct efx_nic *efx)
1164{
Ben Hutchings62776d02010-06-23 11:30:07 +00001165 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001166
1167 if (efx->membase) {
1168 iounmap(efx->membase);
1169 efx->membase = NULL;
1170 }
1171
1172 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001173 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001174 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001175 }
1176
1177 pci_disable_device(efx->pci_dev);
1178}
1179
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001180static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001181{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001182 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001183 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001184 int cpu;
1185
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001186 if (rss_cpus) {
1187 count = rss_cpus;
1188 } else {
1189 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1190 netif_warn(efx, probe, efx->net_dev,
1191 "RSS disabled due to allocation failure\n");
1192 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001193 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001194
1195 count = 0;
1196 for_each_online_cpu(cpu) {
1197 if (!cpumask_test_cpu(cpu, thread_mask)) {
1198 ++count;
1199 cpumask_or(thread_mask, thread_mask,
1200 topology_thread_cpumask(cpu));
1201 }
1202 }
1203
1204 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001205 }
1206
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001207 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1208 * table entries that are inaccessible to VFs
1209 */
1210 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1211 count > efx_vf_size(efx)) {
1212 netif_warn(efx, probe, efx->net_dev,
1213 "Reducing number of RSS channels from %u to %u for "
1214 "VF support. Increase vf-msix-limit to use more "
1215 "channels on the PF.\n",
1216 count, efx_vf_size(efx));
1217 count = efx_vf_size(efx);
1218 }
1219
Ben Hutchings46123d02008-09-01 12:47:33 +01001220 return count;
1221}
1222
1223/* Probe the number and type of interrupts we are able to obtain, and
1224 * the resulting numbers of channels and RX queues.
1225 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001226static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001227{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001228 unsigned int extra_channels = 0;
1229 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001230 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001231
Ben Hutchings7f967c02012-02-13 23:45:02 +00001232 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1233 if (efx->extra_channel_type[i])
1234 ++extra_channels;
1235
Ben Hutchings8ceee662008-04-27 12:55:59 +01001236 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001237 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001238 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001239
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001240 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001241 if (separate_tx_channels)
1242 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001243 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001244 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001245
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001246 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001247 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001248 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001249 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001250 netif_err(efx, drv, efx->net_dev,
1251 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001252 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001253 netif_err(efx, drv, efx->net_dev,
1254 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001255 EFX_BUG_ON_PARANOID(rc >= n_channels);
1256 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001257 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001258 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001259 }
1260
1261 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001262 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001263 if (n_channels > extra_channels)
1264 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001265 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001266 efx->n_tx_channels = max(n_channels / 2, 1U);
1267 efx->n_rx_channels = max(n_channels -
1268 efx->n_tx_channels,
1269 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001270 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001271 efx->n_tx_channels = n_channels;
1272 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001273 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001274 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001275 efx_get_channel(efx, i)->irq =
1276 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001277 } else {
1278 /* Fall back to single channel MSI */
1279 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001280 netif_err(efx, drv, efx->net_dev,
1281 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001282 }
1283 }
1284
1285 /* Try single interrupt MSI */
1286 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001287 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001288 efx->n_rx_channels = 1;
1289 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001290 rc = pci_enable_msi(efx->pci_dev);
1291 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001292 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001293 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001294 netif_err(efx, drv, efx->net_dev,
1295 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001296 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1297 }
1298 }
1299
1300 /* Assume legacy interrupts */
1301 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001302 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001303 efx->n_rx_channels = 1;
1304 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001305 efx->legacy_irq = efx->pci_dev->irq;
1306 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001307
Ben Hutchings7f967c02012-02-13 23:45:02 +00001308 /* Assign extra channels if possible */
1309 j = efx->n_channels;
1310 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1311 if (!efx->extra_channel_type[i])
1312 continue;
1313 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1314 efx->n_channels <= extra_channels) {
1315 efx->extra_channel_type[i]->handle_no_channel(efx);
1316 } else {
1317 --j;
1318 efx_get_channel(efx, j)->type =
1319 efx->extra_channel_type[i];
1320 }
1321 }
1322
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001323 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001324 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001325 efx->n_rx_channels : efx_vf_size(efx));
1326
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001327 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001328}
1329
Ben Hutchingsd8291182012-10-05 23:35:41 +01001330static void efx_soft_enable_interrupts(struct efx_nic *efx)
1331{
1332 struct efx_channel *channel;
1333
1334 BUG_ON(efx->state == STATE_DISABLED);
1335
1336 efx->irq_soft_enabled = true;
1337 smp_wmb();
1338
1339 efx_for_each_channel(channel, efx) {
1340 if (!channel->type->keep_eventq)
1341 efx_init_eventq(channel);
1342 efx_start_eventq(channel);
1343 }
1344
1345 efx_mcdi_mode_event(efx);
1346}
1347
1348static void efx_soft_disable_interrupts(struct efx_nic *efx)
1349{
1350 struct efx_channel *channel;
1351
1352 if (efx->state == STATE_DISABLED)
1353 return;
1354
1355 efx_mcdi_mode_poll(efx);
1356
1357 efx->irq_soft_enabled = false;
1358 smp_wmb();
1359
1360 if (efx->legacy_irq)
1361 synchronize_irq(efx->legacy_irq);
1362
1363 efx_for_each_channel(channel, efx) {
1364 if (channel->irq)
1365 synchronize_irq(channel->irq);
1366
1367 efx_stop_eventq(channel);
1368 if (!channel->type->keep_eventq)
1369 efx_fini_eventq(channel);
1370 }
Ben Hutchingscade7152013-08-27 23:12:31 +01001371
1372 /* Flush the asynchronous MCDI request queue */
1373 efx_mcdi_flush_async(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01001374}
1375
1376static void efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001377{
1378 struct efx_channel *channel;
1379
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001380 BUG_ON(efx->state == STATE_DISABLED);
1381
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001382 if (efx->eeh_disabled_legacy_irq) {
1383 enable_irq(efx->legacy_irq);
1384 efx->eeh_disabled_legacy_irq = false;
1385 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001386
Ben Hutchings86094f72013-08-21 19:51:04 +01001387 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001388
1389 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001390 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001391 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001392 }
1393
Ben Hutchingsd8291182012-10-05 23:35:41 +01001394 efx_soft_enable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001395}
1396
Ben Hutchingsd8291182012-10-05 23:35:41 +01001397static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001398{
1399 struct efx_channel *channel;
1400
Ben Hutchingsd8291182012-10-05 23:35:41 +01001401 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001402
1403 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001404 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001405 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001406 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001407
Ben Hutchings86094f72013-08-21 19:51:04 +01001408 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001409}
1410
Ben Hutchings8ceee662008-04-27 12:55:59 +01001411static void efx_remove_interrupts(struct efx_nic *efx)
1412{
1413 struct efx_channel *channel;
1414
1415 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001416 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001417 channel->irq = 0;
1418 pci_disable_msi(efx->pci_dev);
1419 pci_disable_msix(efx->pci_dev);
1420
1421 /* Remove legacy interrupt */
1422 efx->legacy_irq = 0;
1423}
1424
Ben Hutchings8831da72008-09-01 12:47:48 +01001425static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001426{
Ben Hutchings602a5322011-05-16 17:32:39 +01001427 struct efx_channel *channel;
1428 struct efx_tx_queue *tx_queue;
1429
Ben Hutchings97653432011-01-12 18:26:56 +00001430 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001431 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001432
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001433 /* We need to mark which channels really have RX and TX
1434 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001435 * RX-only and TX-only channels.
1436 */
1437 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001438 if (channel->channel < efx->n_rx_channels)
1439 channel->rx_queue.core_index = channel->channel;
1440 else
1441 channel->rx_queue.core_index = -1;
1442
Ben Hutchings602a5322011-05-16 17:32:39 +01001443 efx_for_each_channel_tx_queue(tx_queue, channel)
1444 tx_queue->queue -= (efx->tx_channel_offset *
1445 EFX_TXQ_TYPES);
1446 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001447}
1448
1449static int efx_probe_nic(struct efx_nic *efx)
1450{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001451 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001452 int rc;
1453
Ben Hutchings62776d02010-06-23 11:30:07 +00001454 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001455
1456 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001457 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001458 if (rc)
1459 return rc;
1460
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001461 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001462 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001463 rc = efx_probe_interrupts(efx);
1464 if (rc)
1465 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001466
Ben Hutchings28e47c42012-02-15 01:58:49 +00001467 efx->type->dimension_resources(efx);
1468
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001469 if (efx->n_channels > 1)
1470 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001471 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001472 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001473 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001474
Ben Hutchings8831da72008-09-01 12:47:48 +01001475 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001476 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1477 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001478
1479 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001480 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1481 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001482
1483 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001484
1485fail:
1486 efx->type->remove(efx);
1487 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001488}
1489
1490static void efx_remove_nic(struct efx_nic *efx)
1491{
Ben Hutchings62776d02010-06-23 11:30:07 +00001492 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001493
1494 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001495 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001496}
1497
Ben Hutchingsadd72472012-11-08 01:46:53 +00001498static int efx_probe_filters(struct efx_nic *efx)
1499{
1500 int rc;
1501
1502 spin_lock_init(&efx->filter_lock);
1503
1504 rc = efx->type->filter_table_probe(efx);
1505 if (rc)
1506 return rc;
1507
1508#ifdef CONFIG_RFS_ACCEL
1509 if (efx->type->offload_features & NETIF_F_NTUPLE) {
1510 efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
1511 sizeof(*efx->rps_flow_id),
1512 GFP_KERNEL);
1513 if (!efx->rps_flow_id) {
1514 efx->type->filter_table_remove(efx);
1515 return -ENOMEM;
1516 }
1517 }
1518#endif
1519
1520 return 0;
1521}
1522
1523static void efx_remove_filters(struct efx_nic *efx)
1524{
1525#ifdef CONFIG_RFS_ACCEL
1526 kfree(efx->rps_flow_id);
1527#endif
1528 efx->type->filter_table_remove(efx);
1529}
1530
1531static void efx_restore_filters(struct efx_nic *efx)
1532{
1533 efx->type->filter_table_restore(efx);
1534}
1535
Ben Hutchings8ceee662008-04-27 12:55:59 +01001536/**************************************************************************
1537 *
1538 * NIC startup/shutdown
1539 *
1540 *************************************************************************/
1541
1542static int efx_probe_all(struct efx_nic *efx)
1543{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001544 int rc;
1545
Ben Hutchings8ceee662008-04-27 12:55:59 +01001546 rc = efx_probe_nic(efx);
1547 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001548 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001549 goto fail1;
1550 }
1551
Ben Hutchings8ceee662008-04-27 12:55:59 +01001552 rc = efx_probe_port(efx);
1553 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001554 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001555 goto fail2;
1556 }
1557
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001558 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1559 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1560 rc = -EINVAL;
1561 goto fail3;
1562 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001563 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001564
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001565 rc = efx_probe_filters(efx);
1566 if (rc) {
1567 netif_err(efx, probe, efx->net_dev,
1568 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001569 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001570 }
1571
Ben Hutchings7f967c02012-02-13 23:45:02 +00001572 rc = efx_probe_channels(efx);
1573 if (rc)
1574 goto fail4;
1575
Ben Hutchings8ceee662008-04-27 12:55:59 +01001576 return 0;
1577
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001578 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001579 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001580 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001581 efx_remove_port(efx);
1582 fail2:
1583 efx_remove_nic(efx);
1584 fail1:
1585 return rc;
1586}
1587
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001588/* If the interface is supposed to be running but is not, start
1589 * the hardware and software data path, regular activity for the port
1590 * (MAC statistics, link polling, etc.) and schedule the port to be
1591 * reconfigured. Interrupts must already be enabled. This function
1592 * is safe to call multiple times, so long as the NIC is not disabled.
1593 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001594 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001595static void efx_start_all(struct efx_nic *efx)
1596{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001597 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001598 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001599
1600 /* Check that it is appropriate to restart the interface. All
1601 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001602 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001603 return;
1604
Ben Hutchings8ceee662008-04-27 12:55:59 +01001605 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001606 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001607
Alexandre Rames626950d2013-01-14 17:20:22 +00001608 /* Start the hardware monitor if there is one */
1609 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001610 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1611 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001612
1613 /* If link state detection is normally event-driven, we have
1614 * to poll now because we could have missed a change
1615 */
1616 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001617 mutex_lock(&efx->mac_lock);
1618 if (efx->phy_op->poll(efx))
1619 efx_link_status_changed(efx);
1620 mutex_unlock(&efx->mac_lock);
1621 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001622
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001623 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001624}
1625
1626/* Flush all delayed work. Should only be called when no more delayed work
1627 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1628 * since we're holding the rtnl_lock at this point. */
1629static void efx_flush_all(struct efx_nic *efx)
1630{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001631 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001632 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001633 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001634 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001635 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001636}
1637
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001638/* Quiesce the hardware and software data path, and regular activity
1639 * for the port without bringing the link down. Safe to call multiple
1640 * times with the NIC in almost any state, but interrupts should be
1641 * enabled. Requires the RTNL lock.
1642 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001643static void efx_stop_all(struct efx_nic *efx)
1644{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001645 EFX_ASSERT_RESET_SERIALISED(efx);
1646
1647 /* port_enabled can be read safely under the rtnl lock */
1648 if (!efx->port_enabled)
1649 return;
1650
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001651 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001652 efx_stop_port(efx);
1653
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001654 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001655 efx_flush_all(efx);
1656
Ben Hutchings29c69a42013-01-28 19:01:06 +00001657 /* Stop the kernel transmit interface. This is only valid if
1658 * the device is stopped or detached; otherwise the watchdog
1659 * may fire immediately.
1660 */
1661 WARN_ON(netif_running(efx->net_dev) &&
1662 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001663 netif_tx_disable(efx->net_dev);
1664
1665 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001666}
1667
1668static void efx_remove_all(struct efx_nic *efx)
1669{
Ben Hutchings46426102010-09-10 06:42:33 +00001670 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001671 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001672 efx_remove_port(efx);
1673 efx_remove_nic(efx);
1674}
1675
Ben Hutchings8ceee662008-04-27 12:55:59 +01001676/**************************************************************************
1677 *
1678 * Interrupt moderation
1679 *
1680 **************************************************************************/
1681
Ben Hutchingscc180b62011-12-08 19:51:47 +00001682static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001683{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001684 if (usecs == 0)
1685 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001686 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001687 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001688 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001689}
1690
Ben Hutchings8ceee662008-04-27 12:55:59 +01001691/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001692int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1693 unsigned int rx_usecs, bool rx_adaptive,
1694 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001695{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001696 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001697 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1698 efx->timer_quantum_ns,
1699 1000);
1700 unsigned int tx_ticks;
1701 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001702
1703 EFX_ASSERT_RESET_SERIALISED(efx);
1704
Ben Hutchingscc180b62011-12-08 19:51:47 +00001705 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001706 return -EINVAL;
1707
Ben Hutchingscc180b62011-12-08 19:51:47 +00001708 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1709 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1710
Ben Hutchings9e393b32011-09-05 07:43:04 +00001711 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1712 !rx_may_override_tx) {
1713 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1714 "RX and TX IRQ moderation must be equal\n");
1715 return -EINVAL;
1716 }
1717
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001718 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001719 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001720 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001721 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001722 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001723 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001724 channel->irq_moderation = tx_ticks;
1725 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001726
1727 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001728}
1729
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001730void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1731 unsigned int *rx_usecs, bool *rx_adaptive)
1732{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001733 /* We must round up when converting ticks to microseconds
1734 * because we round down when converting the other way.
1735 */
1736
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001737 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001738 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1739 efx->timer_quantum_ns,
1740 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001741
1742 /* If channels are shared between RX and TX, so is IRQ
1743 * moderation. Otherwise, IRQ moderation is the same for all
1744 * TX channels and is not adaptive.
1745 */
1746 if (efx->tx_channel_offset == 0)
1747 *tx_usecs = *rx_usecs;
1748 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001749 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001750 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001751 efx->timer_quantum_ns,
1752 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001753}
1754
Ben Hutchings8ceee662008-04-27 12:55:59 +01001755/**************************************************************************
1756 *
1757 * Hardware monitor
1758 *
1759 **************************************************************************/
1760
Ben Hutchingse254c272010-09-20 08:44:10 +00001761/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001762static void efx_monitor(struct work_struct *data)
1763{
1764 struct efx_nic *efx = container_of(data, struct efx_nic,
1765 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001766
Ben Hutchings62776d02010-06-23 11:30:07 +00001767 netif_vdbg(efx, timer, efx->net_dev,
1768 "hardware monitor executing on CPU %d\n",
1769 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001770 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001771
Ben Hutchings8ceee662008-04-27 12:55:59 +01001772 /* If the mac_lock is already held then it is likely a port
1773 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001774 * most of the work of monitor() anyway. */
1775 if (mutex_trylock(&efx->mac_lock)) {
1776 if (efx->port_enabled)
1777 efx->type->monitor(efx);
1778 mutex_unlock(&efx->mac_lock);
1779 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001780
Ben Hutchings8ceee662008-04-27 12:55:59 +01001781 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1782 efx_monitor_interval);
1783}
1784
1785/**************************************************************************
1786 *
1787 * ioctls
1788 *
1789 *************************************************************************/
1790
1791/* Net device ioctl
1792 * Context: process, rtnl_lock() held.
1793 */
1794static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1795{
Ben Hutchings767e4682008-09-01 12:43:14 +01001796 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001797 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001798
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001799 if (cmd == SIOCSHWTSTAMP)
1800 return efx_ptp_ioctl(efx, ifr, cmd);
1801
Ben Hutchings68e7f452009-04-29 08:05:08 +00001802 /* Convert phy_id from older PRTAD/DEVAD format */
1803 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1804 (data->phy_id & 0xfc00) == 0x0400)
1805 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1806
1807 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001808}
1809
1810/**************************************************************************
1811 *
1812 * NAPI interface
1813 *
1814 **************************************************************************/
1815
Ben Hutchings7f967c02012-02-13 23:45:02 +00001816static void efx_init_napi_channel(struct efx_channel *channel)
1817{
1818 struct efx_nic *efx = channel->efx;
1819
1820 channel->napi_dev = efx->net_dev;
1821 netif_napi_add(channel->napi_dev, &channel->napi_str,
1822 efx_poll, napi_weight);
1823}
1824
Ben Hutchingse8f14992010-12-07 19:47:34 +00001825static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001826{
1827 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001828
Ben Hutchings7f967c02012-02-13 23:45:02 +00001829 efx_for_each_channel(channel, efx)
1830 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001831}
1832
1833static void efx_fini_napi_channel(struct efx_channel *channel)
1834{
1835 if (channel->napi_dev)
1836 netif_napi_del(&channel->napi_str);
1837 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001838}
1839
1840static void efx_fini_napi(struct efx_nic *efx)
1841{
1842 struct efx_channel *channel;
1843
Ben Hutchingse8f14992010-12-07 19:47:34 +00001844 efx_for_each_channel(channel, efx)
1845 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001846}
1847
1848/**************************************************************************
1849 *
1850 * Kernel netpoll interface
1851 *
1852 *************************************************************************/
1853
1854#ifdef CONFIG_NET_POLL_CONTROLLER
1855
1856/* Although in the common case interrupts will be disabled, this is not
1857 * guaranteed. However, all our work happens inside the NAPI callback,
1858 * so no locking is required.
1859 */
1860static void efx_netpoll(struct net_device *net_dev)
1861{
Ben Hutchings767e4682008-09-01 12:43:14 +01001862 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001863 struct efx_channel *channel;
1864
Ben Hutchings64ee3122008-09-01 12:47:38 +01001865 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001866 efx_schedule_channel(channel);
1867}
1868
1869#endif
1870
1871/**************************************************************************
1872 *
1873 * Kernel net device interface
1874 *
1875 *************************************************************************/
1876
1877/* Context: process, rtnl_lock() held. */
1878static int efx_net_open(struct net_device *net_dev)
1879{
Ben Hutchings767e4682008-09-01 12:43:14 +01001880 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001881 int rc;
1882
Ben Hutchings62776d02010-06-23 11:30:07 +00001883 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1884 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001885
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001886 rc = efx_check_disabled(efx);
1887 if (rc)
1888 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001889 if (efx->phy_mode & PHY_MODE_SPECIAL)
1890 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001891 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1892 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001893
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001894 /* Notify the kernel of the link state polled during driver load,
1895 * before the monitor starts running */
1896 efx_link_status_changed(efx);
1897
Ben Hutchings8ceee662008-04-27 12:55:59 +01001898 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001899 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001900 return 0;
1901}
1902
1903/* Context: process, rtnl_lock() held.
1904 * Note that the kernel will ignore our return code; this method
1905 * should really be a void.
1906 */
1907static int efx_net_stop(struct net_device *net_dev)
1908{
Ben Hutchings767e4682008-09-01 12:43:14 +01001909 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001910
Ben Hutchings62776d02010-06-23 11:30:07 +00001911 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1912 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001913
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001914 /* Stop the device and flush all the channels */
1915 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001916
1917 return 0;
1918}
1919
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001920/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001921static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1922 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001923{
Ben Hutchings767e4682008-09-01 12:43:14 +01001924 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001925
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001926 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00001927 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001928 spin_unlock_bh(&efx->stats_lock);
1929
Ben Hutchings8ceee662008-04-27 12:55:59 +01001930 return stats;
1931}
1932
1933/* Context: netif_tx_lock held, BHs disabled. */
1934static void efx_watchdog(struct net_device *net_dev)
1935{
Ben Hutchings767e4682008-09-01 12:43:14 +01001936 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001937
Ben Hutchings62776d02010-06-23 11:30:07 +00001938 netif_err(efx, tx_err, efx->net_dev,
1939 "TX stuck with port_enabled=%d: resetting channels\n",
1940 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001941
Ben Hutchings739bb232008-11-04 20:35:36 +00001942 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001943}
1944
1945
1946/* Context: process, rtnl_lock() held. */
1947static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1948{
Ben Hutchings767e4682008-09-01 12:43:14 +01001949 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001950 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001951
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001952 rc = efx_check_disabled(efx);
1953 if (rc)
1954 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001955 if (new_mtu > EFX_MAX_MTU)
1956 return -EINVAL;
1957
Ben Hutchings62776d02010-06-23 11:30:07 +00001958 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001959
Ben Hutchings29c69a42013-01-28 19:01:06 +00001960 efx_device_detach_sync(efx);
1961 efx_stop_all(efx);
1962
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001963 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001964 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001965 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001966 mutex_unlock(&efx->mac_lock);
1967
Ben Hutchings8ceee662008-04-27 12:55:59 +01001968 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00001969 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001970 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001971}
1972
1973static int efx_set_mac_address(struct net_device *net_dev, void *data)
1974{
Ben Hutchings767e4682008-09-01 12:43:14 +01001975 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001976 struct sockaddr *addr = data;
1977 char *new_addr = addr->sa_data;
1978
Ben Hutchings8ceee662008-04-27 12:55:59 +01001979 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001980 netif_err(efx, drv, efx->net_dev,
1981 "invalid ethernet MAC address requested: %pM\n",
1982 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00001983 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001984 }
1985
1986 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001987 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001988
1989 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001990 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01001991 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001992 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001993
1994 return 0;
1995}
1996
Ben Hutchingsa816f752008-09-01 12:49:12 +01001997/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00001998static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001999{
Ben Hutchings767e4682008-09-01 12:43:14 +01002000 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002001
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002002 if (efx->port_enabled)
2003 queue_work(efx->workqueue, &efx->mac_work);
2004 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002005}
2006
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002007static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002008{
2009 struct efx_nic *efx = netdev_priv(net_dev);
2010
2011 /* If disabling RX n-tuple filtering, clear existing filters */
2012 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2013 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2014
2015 return 0;
2016}
2017
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002018static const struct net_device_ops efx_netdev_ops = {
2019 .ndo_open = efx_net_open,
2020 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002021 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002022 .ndo_tx_timeout = efx_watchdog,
2023 .ndo_start_xmit = efx_hard_start_xmit,
2024 .ndo_validate_addr = eth_validate_addr,
2025 .ndo_do_ioctl = efx_ioctl,
2026 .ndo_change_mtu = efx_change_mtu,
2027 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002028 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002029 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002030#ifdef CONFIG_SFC_SRIOV
2031 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2032 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2033 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2034 .ndo_get_vf_config = efx_sriov_get_vf_config,
2035#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002036#ifdef CONFIG_NET_POLL_CONTROLLER
2037 .ndo_poll_controller = efx_netpoll,
2038#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002039 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002040#ifdef CONFIG_RFS_ACCEL
2041 .ndo_rx_flow_steer = efx_filter_rfs,
2042#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002043};
2044
Ben Hutchings7dde5962008-12-12 22:09:38 -08002045static void efx_update_name(struct efx_nic *efx)
2046{
2047 strcpy(efx->name, efx->net_dev->name);
2048 efx_mtd_rename(efx);
2049 efx_set_channel_names(efx);
2050}
2051
Ben Hutchings8ceee662008-04-27 12:55:59 +01002052static int efx_netdev_event(struct notifier_block *this,
2053 unsigned long event, void *ptr)
2054{
Jiri Pirko351638e2013-05-28 01:30:21 +00002055 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002056
Ben Hutchings7dde5962008-12-12 22:09:38 -08002057 if (net_dev->netdev_ops == &efx_netdev_ops &&
2058 event == NETDEV_CHANGENAME)
2059 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002060
2061 return NOTIFY_DONE;
2062}
2063
2064static struct notifier_block efx_netdev_notifier = {
2065 .notifier_call = efx_netdev_event,
2066};
2067
Ben Hutchings06d5e192008-12-12 21:47:23 -08002068static ssize_t
2069show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2070{
2071 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2072 return sprintf(buf, "%d\n", efx->phy_type);
2073}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002074static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002075
Ben Hutchings8ceee662008-04-27 12:55:59 +01002076static int efx_register_netdev(struct efx_nic *efx)
2077{
2078 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002079 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002080 int rc;
2081
2082 net_dev->watchdog_timeo = 5 * HZ;
2083 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002084 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002085 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002086 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002087
Ben Hutchings7dde5962008-12-12 22:09:38 -08002088 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002089
Ben Hutchings7153f622012-07-27 20:50:52 +01002090 /* Enable resets to be scheduled and check whether any were
2091 * already requested. If so, the NIC is probably hosed so we
2092 * abort.
2093 */
2094 efx->state = STATE_READY;
2095 smp_mb(); /* ensure we change state before checking reset_pending */
2096 if (efx->reset_pending) {
2097 netif_err(efx, probe, efx->net_dev,
2098 "aborting probe due to scheduled reset\n");
2099 rc = -EIO;
2100 goto fail_locked;
2101 }
2102
Ben Hutchingsaed06282009-08-26 08:16:27 +00002103 rc = dev_alloc_name(net_dev, net_dev->name);
2104 if (rc < 0)
2105 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002106 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002107
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002108 /* Always start with carrier off; PHY events will detect the link */
2109 netif_carrier_off(net_dev);
2110
Ben Hutchingsaed06282009-08-26 08:16:27 +00002111 rc = register_netdevice(net_dev);
2112 if (rc)
2113 goto fail_locked;
2114
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002115 efx_for_each_channel(channel, efx) {
2116 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002117 efx_for_each_channel_tx_queue(tx_queue, channel)
2118 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002119 }
2120
Ben Hutchings7dde5962008-12-12 22:09:38 -08002121 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002122
Ben Hutchings06d5e192008-12-12 21:47:23 -08002123 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2124 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002125 netif_err(efx, drv, efx->net_dev,
2126 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002127 goto fail_registered;
2128 }
2129
Ben Hutchings8ceee662008-04-27 12:55:59 +01002130 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002131
Ben Hutchings7153f622012-07-27 20:50:52 +01002132fail_registered:
2133 rtnl_lock();
2134 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002135fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002136 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002137 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002138 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002139 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002140}
2141
2142static void efx_unregister_netdev(struct efx_nic *efx)
2143{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002144 if (!efx->net_dev)
2145 return;
2146
Ben Hutchings767e4682008-09-01 12:43:14 +01002147 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002148
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002149 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2150 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002151
2152 rtnl_lock();
2153 unregister_netdevice(efx->net_dev);
2154 efx->state = STATE_UNINIT;
2155 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002156}
2157
2158/**************************************************************************
2159 *
2160 * Device reset and suspend
2161 *
2162 **************************************************************************/
2163
Ben Hutchings2467ca42008-09-01 12:48:50 +01002164/* Tears down the entire software state and most of the hardware state
2165 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002166void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002167{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002168 EFX_ASSERT_RESET_SERIALISED(efx);
2169
Ben Hutchings2467ca42008-09-01 12:48:50 +01002170 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002171 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002172
2173 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002174 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2175 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002176 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002177}
2178
Ben Hutchings2467ca42008-09-01 12:48:50 +01002179/* This function will always ensure that the locks acquired in
2180 * efx_reset_down() are released. A failure return code indicates
2181 * that we were unable to reinitialise the hardware, and the
2182 * driver should be disabled. If ok is false, then the rx and tx
2183 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002184int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002185{
2186 int rc;
2187
Ben Hutchings2467ca42008-09-01 12:48:50 +01002188 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002189
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002190 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002191 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002192 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002193 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002194 }
2195
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002196 if (!ok)
2197 goto fail;
2198
Steve Hodgson4b988282009-01-29 17:50:51 +00002199 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002200 rc = efx->phy_op->init(efx);
2201 if (rc)
2202 goto fail;
2203 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002204 netif_err(efx, drv, efx->net_dev,
2205 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002206 }
2207
Ben Hutchingsd8291182012-10-05 23:35:41 +01002208 efx_enable_interrupts(efx);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002209 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002210 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002211
2212 mutex_unlock(&efx->mac_lock);
2213
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002214 efx_start_all(efx);
2215
2216 return 0;
2217
2218fail:
2219 efx->port_initialized = false;
2220
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002221 mutex_unlock(&efx->mac_lock);
2222
Ben Hutchings8ceee662008-04-27 12:55:59 +01002223 return rc;
2224}
2225
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002226/* Reset the NIC using the specified method. Note that the reset may
2227 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002228 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002229 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002230 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002231int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002232{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002233 int rc, rc2;
2234 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002235
Ben Hutchings62776d02010-06-23 11:30:07 +00002236 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2237 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002238
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002239 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002240 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002241
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002242 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002243 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002244 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002245 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002246 }
2247
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002248 /* Clear flags for the scopes we covered. We assume the NIC and
2249 * driver are now quiescent so that there is no race here.
2250 */
2251 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002252
2253 /* Reinitialise bus-mastering, which may have been turned off before
2254 * the reset was scheduled. This is still appropriate, even in the
2255 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2256 * can respond to requests. */
2257 pci_set_master(efx->pci_dev);
2258
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002259out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002260 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002261 disabled = rc ||
2262 method == RESET_TYPE_DISABLE ||
2263 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002264 rc2 = efx_reset_up(efx, method, !disabled);
2265 if (rc2) {
2266 disabled = true;
2267 if (!rc)
2268 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002269 }
2270
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002271 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002272 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002273 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002274 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002275 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002276 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002277 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002278 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002279 return rc;
2280}
2281
Alexandre Rames626950d2013-01-14 17:20:22 +00002282/* Try recovery mechanisms.
2283 * For now only EEH is supported.
2284 * Returns 0 if the recovery mechanisms are unsuccessful.
2285 * Returns a non-zero value otherwise.
2286 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002287int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002288{
2289#ifdef CONFIG_EEH
2290 /* A PCI error can occur and not be seen by EEH because nothing
2291 * happens on the PCI bus. In this case the driver may fail and
2292 * schedule a 'recover or reset', leading to this recovery handler.
2293 * Manually call the eeh failure check function.
2294 */
2295 struct eeh_dev *eehdev =
2296 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2297
2298 if (eeh_dev_check_failure(eehdev)) {
2299 /* The EEH mechanisms will handle the error and reset the
2300 * device if necessary.
2301 */
2302 return 1;
2303 }
2304#endif
2305 return 0;
2306}
2307
Ben Hutchings8ceee662008-04-27 12:55:59 +01002308/* The worker thread exists so that code that cannot sleep can
2309 * schedule a reset for later.
2310 */
2311static void efx_reset_work(struct work_struct *data)
2312{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002313 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002314 unsigned long pending;
2315 enum reset_type method;
2316
2317 pending = ACCESS_ONCE(efx->reset_pending);
2318 method = fls(pending) - 1;
2319
2320 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2321 method == RESET_TYPE_RECOVER_OR_ALL) &&
2322 efx_try_recovery(efx))
2323 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002324
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002325 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002326 return;
2327
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002328 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002329
2330 /* We checked the state in efx_schedule_reset() but it may
2331 * have changed by now. Now that we have the RTNL lock,
2332 * it cannot change again.
2333 */
2334 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002335 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002336
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002337 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002338}
2339
2340void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2341{
2342 enum reset_type method;
2343
Alexandre Rames626950d2013-01-14 17:20:22 +00002344 if (efx->state == STATE_RECOVERY) {
2345 netif_dbg(efx, drv, efx->net_dev,
2346 "recovering: skip scheduling %s reset\n",
2347 RESET_TYPE(type));
2348 return;
2349 }
2350
Ben Hutchings8ceee662008-04-27 12:55:59 +01002351 switch (type) {
2352 case RESET_TYPE_INVISIBLE:
2353 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002354 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002355 case RESET_TYPE_WORLD:
2356 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002357 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002358 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002359 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2360 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002361 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002362 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002363 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002364 netif_dbg(efx, drv, efx->net_dev,
2365 "scheduling %s reset for %s\n",
2366 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002367 break;
2368 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002369
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002370 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002371 smp_mb(); /* ensure we change reset_pending before checking state */
2372
2373 /* If we're not READY then just leave the flags set as the cue
2374 * to abort probing or reschedule the reset later.
2375 */
2376 if (ACCESS_ONCE(efx->state) != STATE_READY)
2377 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002378
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002379 /* efx_process_channel() will no longer read events once a
2380 * reset is scheduled. So switch back to poll'd MCDI completions. */
2381 efx_mcdi_mode_poll(efx);
2382
Steve Hodgson1ab00622008-12-12 21:33:02 -08002383 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002384}
2385
2386/**************************************************************************
2387 *
2388 * List of NICs we support
2389 *
2390 **************************************************************************/
2391
2392/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002393static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e2011-10-28 14:20:44 -07002394 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2395 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002396 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e2011-10-28 14:20:44 -07002397 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2398 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002399 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002400 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002401 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002402 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002403 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002404 {0} /* end of list */
2405};
2406
2407/**************************************************************************
2408 *
Ben Hutchings37594332009-11-23 16:05:45 +00002409 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002410 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002411 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002412 * Needed so all function pointers are valid and do not have to be tested
2413 * before use
2414 *
2415 **************************************************************************/
2416int efx_port_dummy_op_int(struct efx_nic *efx)
2417{
2418 return 0;
2419}
2420void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002421
2422static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002423{
2424 return false;
2425}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002426
stephen hemminger6c8c2512011-04-14 05:50:12 +00002427static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002428 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002429 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002430 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002431 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002432};
2433
Ben Hutchings8ceee662008-04-27 12:55:59 +01002434/**************************************************************************
2435 *
2436 * Data housekeeping
2437 *
2438 **************************************************************************/
2439
2440/* This zeroes out and then fills in the invariants in a struct
2441 * efx_nic (including all sub-structures).
2442 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002443static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002444 struct pci_dev *pci_dev, struct net_device *net_dev)
2445{
Ben Hutchings46426102010-09-10 06:42:33 +00002446 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002447
2448 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002449 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002450#ifdef CONFIG_SFC_MTD
2451 INIT_LIST_HEAD(&efx->mtd_list);
2452#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002453 INIT_WORK(&efx->reset_work, efx_reset_work);
2454 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002455 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002456 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002457 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002458 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002459 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002460
2461 efx->net_dev = net_dev;
Jon Cooper43a37392012-10-18 15:49:54 +01002462 efx->rx_prefix_size = efx->type->rx_prefix_size;
2463 efx->rx_packet_hash_offset =
2464 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002465 spin_lock_init(&efx->stats_lock);
2466 mutex_init(&efx->mac_lock);
2467 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002468 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002469 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002470 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002471
2472 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002473 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2474 if (!efx->channel[i])
2475 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002476 efx->msi_context[i].efx = efx;
2477 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002478 }
2479
Ben Hutchings8ceee662008-04-27 12:55:59 +01002480 /* Higher numbered interrupt modes are less capable! */
2481 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2482 interrupt_mode);
2483
Ben Hutchings6977dc62008-12-26 13:44:39 -08002484 /* Would be good to use the net_dev name, but we're too early */
2485 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2486 pci_name(pci_dev));
2487 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002488 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002489 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002490
Ben Hutchings8ceee662008-04-27 12:55:59 +01002491 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002492
2493fail:
2494 efx_fini_struct(efx);
2495 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002496}
2497
2498static void efx_fini_struct(struct efx_nic *efx)
2499{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002500 int i;
2501
2502 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2503 kfree(efx->channel[i]);
2504
Ben Hutchings8ceee662008-04-27 12:55:59 +01002505 if (efx->workqueue) {
2506 destroy_workqueue(efx->workqueue);
2507 efx->workqueue = NULL;
2508 }
2509}
2510
2511/**************************************************************************
2512 *
2513 * PCI interface
2514 *
2515 **************************************************************************/
2516
2517/* Main body of final NIC shutdown code
2518 * This is called only at module unload (or hotplug removal).
2519 */
2520static void efx_pci_remove_main(struct efx_nic *efx)
2521{
Ben Hutchings7153f622012-07-27 20:50:52 +01002522 /* Flush reset_work. It can no longer be scheduled since we
2523 * are not READY.
2524 */
2525 BUG_ON(efx->state == STATE_READY);
2526 cancel_work_sync(&efx->reset_work);
2527
Ben Hutchingsd8291182012-10-05 23:35:41 +01002528 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002529 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002530 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002531 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002532 efx_fini_napi(efx);
2533 efx_remove_all(efx);
2534}
2535
2536/* Final NIC shutdown
2537 * This is called only at module unload (or hotplug removal).
2538 */
2539static void efx_pci_remove(struct pci_dev *pci_dev)
2540{
2541 struct efx_nic *efx;
2542
2543 efx = pci_get_drvdata(pci_dev);
2544 if (!efx)
2545 return;
2546
2547 /* Mark the NIC as fini, then stop the interface */
2548 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002549 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002550 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002551 rtnl_unlock();
2552
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002553 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002554 efx_unregister_netdev(efx);
2555
Ben Hutchings7dde5962008-12-12 22:09:38 -08002556 efx_mtd_remove(efx);
2557
Ben Hutchings8ceee662008-04-27 12:55:59 +01002558 efx_pci_remove_main(efx);
2559
Ben Hutchings8ceee662008-04-27 12:55:59 +01002560 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002561 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002562
Ben Hutchings8ceee662008-04-27 12:55:59 +01002563 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002564 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002565 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002566
2567 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002568};
2569
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002570/* NIC VPD information
2571 * Called during probe to display the part number of the
2572 * installed NIC. VPD is potentially very large but this should
2573 * always appear within the first 512 bytes.
2574 */
2575#define SFC_VPD_LEN 512
2576static void efx_print_product_vpd(struct efx_nic *efx)
2577{
2578 struct pci_dev *dev = efx->pci_dev;
2579 char vpd_data[SFC_VPD_LEN];
2580 ssize_t vpd_size;
2581 int i, j;
2582
2583 /* Get the vpd data from the device */
2584 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2585 if (vpd_size <= 0) {
2586 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2587 return;
2588 }
2589
2590 /* Get the Read only section */
2591 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2592 if (i < 0) {
2593 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2594 return;
2595 }
2596
2597 j = pci_vpd_lrdt_size(&vpd_data[i]);
2598 i += PCI_VPD_LRDT_TAG_SIZE;
2599 if (i + j > vpd_size)
2600 j = vpd_size - i;
2601
2602 /* Get the Part number */
2603 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2604 if (i < 0) {
2605 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2606 return;
2607 }
2608
2609 j = pci_vpd_info_field_size(&vpd_data[i]);
2610 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2611 if (i + j > vpd_size) {
2612 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2613 return;
2614 }
2615
2616 netif_info(efx, drv, efx->net_dev,
2617 "Part Number : %.*s\n", j, &vpd_data[i]);
2618}
2619
2620
Ben Hutchings8ceee662008-04-27 12:55:59 +01002621/* Main body of NIC initialisation
2622 * This is called at module load (or hotplug insertion, theoretically).
2623 */
2624static int efx_pci_probe_main(struct efx_nic *efx)
2625{
2626 int rc;
2627
2628 /* Do start-of-day initialisation */
2629 rc = efx_probe_all(efx);
2630 if (rc)
2631 goto fail1;
2632
Ben Hutchingse8f14992010-12-07 19:47:34 +00002633 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002634
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002635 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002636 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002637 netif_err(efx, probe, efx->net_dev,
2638 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002639 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002640 }
2641
2642 rc = efx_init_port(efx);
2643 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002644 netif_err(efx, probe, efx->net_dev,
2645 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002646 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002647 }
2648
Ben Hutchings152b6a62009-11-29 03:43:56 +00002649 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002650 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002651 goto fail5;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002652 efx_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002653
2654 return 0;
2655
Ben Hutchings278c0622009-11-23 16:05:12 +00002656 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002657 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002658 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002659 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002660 fail3:
2661 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002662 efx_remove_all(efx);
2663 fail1:
2664 return rc;
2665}
2666
2667/* NIC initialisation
2668 *
2669 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002670 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002671 * sets up and registers the network devices with the kernel and hooks
2672 * the interrupt service routine. It does not prepare the device for
2673 * transmission; this is left to the first time one of the network
2674 * interfaces is brought up (i.e. efx_net_open).
2675 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002676static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002677 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002678{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002679 struct net_device *net_dev;
2680 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002681 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002682
2683 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002684 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2685 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002686 if (!net_dev)
2687 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002688 efx = netdev_priv(net_dev);
2689 efx->type = (const struct efx_nic_type *) entry->driver_data;
2690 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002691 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002692 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002693 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002694 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002695 /* Mask for features that also apply to VLAN devices */
2696 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002697 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2698 NETIF_F_RXCSUM);
2699 /* All offloads can be toggled */
2700 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002701 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002702 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002703 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002704 if (rc)
2705 goto fail1;
2706
Ben Hutchings62776d02010-06-23 11:30:07 +00002707 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002708 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002709
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002710 efx_print_product_vpd(efx);
2711
Ben Hutchings8ceee662008-04-27 12:55:59 +01002712 /* Set up basic I/O (BAR mappings etc) */
2713 rc = efx_init_io(efx);
2714 if (rc)
2715 goto fail2;
2716
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002717 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002718 if (rc)
2719 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002720
Ben Hutchings8ceee662008-04-27 12:55:59 +01002721 rc = efx_register_netdev(efx);
2722 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002723 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002724
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002725 rc = efx_sriov_init(efx);
2726 if (rc)
2727 netif_err(efx, probe, efx->net_dev,
2728 "SR-IOV can't be enabled rc %d\n", rc);
2729
Ben Hutchings62776d02010-06-23 11:30:07 +00002730 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002731
Ben Hutchings7c431612012-01-27 17:23:58 +00002732 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002733 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002734 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002735 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002736 if (rc)
2737 netif_warn(efx, probe, efx->net_dev,
2738 "failed to create MTDs (%d)\n", rc);
2739
Alexandre Rames626950d2013-01-14 17:20:22 +00002740 rc = pci_enable_pcie_error_reporting(pci_dev);
2741 if (rc && rc != -EINVAL)
2742 netif_warn(efx, probe, efx->net_dev,
2743 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2744
Ben Hutchings8ceee662008-04-27 12:55:59 +01002745 return 0;
2746
Ben Hutchings8ceee662008-04-27 12:55:59 +01002747 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002748 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002749 fail3:
2750 efx_fini_io(efx);
2751 fail2:
2752 efx_fini_struct(efx);
2753 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002754 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002755 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002756 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002757 free_netdev(net_dev);
2758 return rc;
2759}
2760
Ben Hutchings89c758f2009-11-29 03:43:07 +00002761static int efx_pm_freeze(struct device *dev)
2762{
2763 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2764
Ben Hutchings61da0262012-07-27 19:35:39 +01002765 rtnl_lock();
2766
Ben Hutchings6032fb52012-07-27 19:35:47 +01002767 if (efx->state != STATE_DISABLED) {
2768 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002769
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002770 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002771
Ben Hutchings6032fb52012-07-27 19:35:47 +01002772 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002773 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01002774 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002775
Ben Hutchings61da0262012-07-27 19:35:39 +01002776 rtnl_unlock();
2777
Ben Hutchings89c758f2009-11-29 03:43:07 +00002778 return 0;
2779}
2780
2781static int efx_pm_thaw(struct device *dev)
2782{
2783 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2784
Ben Hutchings61da0262012-07-27 19:35:39 +01002785 rtnl_lock();
2786
Ben Hutchings6032fb52012-07-27 19:35:47 +01002787 if (efx->state != STATE_DISABLED) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01002788 efx_enable_interrupts(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002789
Ben Hutchings6032fb52012-07-27 19:35:47 +01002790 mutex_lock(&efx->mac_lock);
2791 efx->phy_op->reconfigure(efx);
2792 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002793
Ben Hutchings6032fb52012-07-27 19:35:47 +01002794 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002795
Ben Hutchings6032fb52012-07-27 19:35:47 +01002796 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002797
Ben Hutchings6032fb52012-07-27 19:35:47 +01002798 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002799
Ben Hutchings6032fb52012-07-27 19:35:47 +01002800 efx->type->resume_wol(efx);
2801 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002802
Ben Hutchings61da0262012-07-27 19:35:39 +01002803 rtnl_unlock();
2804
Steve Hodgson319ba642010-06-01 11:17:24 +00002805 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2806 queue_work(reset_workqueue, &efx->reset_work);
2807
Ben Hutchings89c758f2009-11-29 03:43:07 +00002808 return 0;
2809}
2810
2811static int efx_pm_poweroff(struct device *dev)
2812{
2813 struct pci_dev *pci_dev = to_pci_dev(dev);
2814 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2815
2816 efx->type->fini(efx);
2817
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002818 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002819
2820 pci_save_state(pci_dev);
2821 return pci_set_power_state(pci_dev, PCI_D3hot);
2822}
2823
2824/* Used for both resume and restore */
2825static int efx_pm_resume(struct device *dev)
2826{
2827 struct pci_dev *pci_dev = to_pci_dev(dev);
2828 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2829 int rc;
2830
2831 rc = pci_set_power_state(pci_dev, PCI_D0);
2832 if (rc)
2833 return rc;
2834 pci_restore_state(pci_dev);
2835 rc = pci_enable_device(pci_dev);
2836 if (rc)
2837 return rc;
2838 pci_set_master(efx->pci_dev);
2839 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2840 if (rc)
2841 return rc;
2842 rc = efx->type->init(efx);
2843 if (rc)
2844 return rc;
2845 efx_pm_thaw(dev);
2846 return 0;
2847}
2848
2849static int efx_pm_suspend(struct device *dev)
2850{
2851 int rc;
2852
2853 efx_pm_freeze(dev);
2854 rc = efx_pm_poweroff(dev);
2855 if (rc)
2856 efx_pm_resume(dev);
2857 return rc;
2858}
2859
Ben Hutchings18e83e42012-01-05 19:05:20 +00002860static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002861 .suspend = efx_pm_suspend,
2862 .resume = efx_pm_resume,
2863 .freeze = efx_pm_freeze,
2864 .thaw = efx_pm_thaw,
2865 .poweroff = efx_pm_poweroff,
2866 .restore = efx_pm_resume,
2867};
2868
Alexandre Rames626950d2013-01-14 17:20:22 +00002869/* A PCI error affecting this device was detected.
2870 * At this point MMIO and DMA may be disabled.
2871 * Stop the software path and request a slot reset.
2872 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002873static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2874 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00002875{
2876 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2877 struct efx_nic *efx = pci_get_drvdata(pdev);
2878
2879 if (state == pci_channel_io_perm_failure)
2880 return PCI_ERS_RESULT_DISCONNECT;
2881
2882 rtnl_lock();
2883
2884 if (efx->state != STATE_DISABLED) {
2885 efx->state = STATE_RECOVERY;
2886 efx->reset_pending = 0;
2887
2888 efx_device_detach_sync(efx);
2889
2890 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002891 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00002892
2893 status = PCI_ERS_RESULT_NEED_RESET;
2894 } else {
2895 /* If the interface is disabled we don't want to do anything
2896 * with it.
2897 */
2898 status = PCI_ERS_RESULT_RECOVERED;
2899 }
2900
2901 rtnl_unlock();
2902
2903 pci_disable_device(pdev);
2904
2905 return status;
2906}
2907
2908/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002909static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00002910{
2911 struct efx_nic *efx = pci_get_drvdata(pdev);
2912 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2913 int rc;
2914
2915 if (pci_enable_device(pdev)) {
2916 netif_err(efx, hw, efx->net_dev,
2917 "Cannot re-enable PCI device after reset.\n");
2918 status = PCI_ERS_RESULT_DISCONNECT;
2919 }
2920
2921 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2922 if (rc) {
2923 netif_err(efx, hw, efx->net_dev,
2924 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2925 /* Non-fatal error. Continue. */
2926 }
2927
2928 return status;
2929}
2930
2931/* Perform the actual reset and resume I/O operations. */
2932static void efx_io_resume(struct pci_dev *pdev)
2933{
2934 struct efx_nic *efx = pci_get_drvdata(pdev);
2935 int rc;
2936
2937 rtnl_lock();
2938
2939 if (efx->state == STATE_DISABLED)
2940 goto out;
2941
2942 rc = efx_reset(efx, RESET_TYPE_ALL);
2943 if (rc) {
2944 netif_err(efx, hw, efx->net_dev,
2945 "efx_reset failed after PCI error (%d)\n", rc);
2946 } else {
2947 efx->state = STATE_READY;
2948 netif_dbg(efx, hw, efx->net_dev,
2949 "Done resetting and resuming IO after PCI error.\n");
2950 }
2951
2952out:
2953 rtnl_unlock();
2954}
2955
2956/* For simplicity and reliability, we always require a slot reset and try to
2957 * reset the hardware when a pci error affecting the device is detected.
2958 * We leave both the link_reset and mmio_enabled callback unimplemented:
2959 * with our request for slot reset the mmio_enabled callback will never be
2960 * called, and the link_reset callback is not used by AER or EEH mechanisms.
2961 */
2962static struct pci_error_handlers efx_err_handlers = {
2963 .error_detected = efx_io_error_detected,
2964 .slot_reset = efx_io_slot_reset,
2965 .resume = efx_io_resume,
2966};
2967
Ben Hutchings8ceee662008-04-27 12:55:59 +01002968static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00002969 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002970 .id_table = efx_pci_table,
2971 .probe = efx_pci_probe,
2972 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00002973 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00002974 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002975};
2976
2977/**************************************************************************
2978 *
2979 * Kernel module interface
2980 *
2981 *************************************************************************/
2982
2983module_param(interrupt_mode, uint, 0444);
2984MODULE_PARM_DESC(interrupt_mode,
2985 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2986
2987static int __init efx_init_module(void)
2988{
2989 int rc;
2990
2991 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2992
2993 rc = register_netdevice_notifier(&efx_netdev_notifier);
2994 if (rc)
2995 goto err_notifier;
2996
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002997 rc = efx_init_sriov();
2998 if (rc)
2999 goto err_sriov;
3000
Steve Hodgson1ab00622008-12-12 21:33:02 -08003001 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3002 if (!reset_workqueue) {
3003 rc = -ENOMEM;
3004 goto err_reset;
3005 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003006
3007 rc = pci_register_driver(&efx_pci_driver);
3008 if (rc < 0)
3009 goto err_pci;
3010
3011 return 0;
3012
3013 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003014 destroy_workqueue(reset_workqueue);
3015 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003016 efx_fini_sriov();
3017 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003018 unregister_netdevice_notifier(&efx_netdev_notifier);
3019 err_notifier:
3020 return rc;
3021}
3022
3023static void __exit efx_exit_module(void)
3024{
3025 printk(KERN_INFO "Solarflare NET driver unloading\n");
3026
3027 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003028 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003029 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003030 unregister_netdevice_notifier(&efx_netdev_notifier);
3031
3032}
3033
3034module_init(efx_init_module);
3035module_exit(efx_exit_module);
3036
Ben Hutchings906bb262009-11-29 15:16:19 +00003037MODULE_AUTHOR("Solarflare Communications and "
3038 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003039MODULE_DESCRIPTION("Solarflare Communications network driver");
3040MODULE_LICENSE("GPL");
3041MODULE_DEVICE_TABLE(pci, efx_pci_table);