blob: 59aa73ccefed421f298aff4140b6cad5c893694f [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchings0a6f40c2011-02-25 00:01:34 +00004 * Copyright 2005-2011 Solarflare Communications Inc.
Ben Hutchings8ceee662008-04-27 12:55:59 +01005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/delay.h>
16#include <linux/notifier.h>
17#include <linux/ip.h>
18#include <linux/tcp.h>
19#include <linux/in.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010020#include <linux/ethtool.h>
Ben Hutchingsaa6ef272008-07-18 19:03:10 +010021#include <linux/topology.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Alexandre Rames626950d2013-01-14 17:20:22 +000023#include <linux/aer.h>
Alexandre Ramesb28405b2013-03-21 16:41:43 +000024#include <linux/interrupt.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010025#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000027#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000028#include "selftest.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010029
Ben Hutchings8880f4e2009-11-29 15:15:41 +000030#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000031#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000032
Ben Hutchingsc4593022009-11-23 16:08:17 +000033/**************************************************************************
34 *
35 * Type name strings
36 *
37 **************************************************************************
38 */
39
40/* Loopback mode names (see LOOPBACK_MODE()) */
41const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000042const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000043 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000044 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000045 [LOOPBACK_GMAC] = "GMAC",
46 [LOOPBACK_XGMII] = "XGMII",
47 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000048 [LOOPBACK_XAUI] = "XAUI",
49 [LOOPBACK_GMII] = "GMII",
50 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000051 [LOOPBACK_XGBR] = "XGBR",
52 [LOOPBACK_XFI] = "XFI",
53 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
54 [LOOPBACK_GMII_FAR] = "GMII_FAR",
55 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
56 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000057 [LOOPBACK_GPHY] = "GPHY",
58 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000059 [LOOPBACK_PCS] = "PCS",
60 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000061 [LOOPBACK_XPORT] = "XPORT",
62 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000063 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000064 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
65 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000066 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000067 [LOOPBACK_XFI_WS] = "XFI_WS",
68 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000069 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000070};
71
Ben Hutchingsc4593022009-11-23 16:08:17 +000072const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000073const char *const efx_reset_type_names[] = {
Alexandre Rames626950d2013-01-14 17:20:22 +000074 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
75 [RESET_TYPE_ALL] = "ALL",
76 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
77 [RESET_TYPE_WORLD] = "WORLD",
78 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
79 [RESET_TYPE_DISABLE] = "DISABLE",
80 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
81 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
82 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
Alexandre Rames3de82b92013-06-13 11:36:15 +010083 [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
Alexandre Rames626950d2013-01-14 17:20:22 +000084 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
85 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Ben Hutchingsc4593022009-11-23 16:08:17 +000086};
87
Steve Hodgson1ab00622008-12-12 21:33:02 -080088/* Reset workqueue. If any NIC has a hardware failure then a reset will be
89 * queued onto this work queue. This is not a per-nic work queue, because
90 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
91 */
92static struct workqueue_struct *reset_workqueue;
93
Ben Hutchings8ceee662008-04-27 12:55:59 +010094/**************************************************************************
95 *
96 * Configurable values
97 *
98 *************************************************************************/
99
100/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100101 * Use separate channels for TX and RX events
102 *
Neil Turton28b581a2008-12-12 21:41:06 -0800103 * Set this to 1 to use separate channels for TX and RX. It allows us
104 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100105 *
Neil Turton28b581a2008-12-12 21:41:06 -0800106 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100107 */
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000108static bool separate_tx_channels;
109module_param(separate_tx_channels, bool, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800110MODULE_PARM_DESC(separate_tx_channels,
111 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100112
113/* This is the weight assigned to each of the (per-channel) virtual
114 * NAPI devices.
115 */
116static int napi_weight = 64;
117
118/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000119 * monitor.
120 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000121 * - Check the on-board hardware monitor;
122 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000123 * On Siena-based NICs for power systems with EEH support, this will give EEH a
124 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100125 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000126static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100127
Ben Hutchings8ceee662008-04-27 12:55:59 +0100128/* Initial interrupt moderation settings. They can be modified after
129 * module load with ethtool.
130 *
131 * The default for RX should strike a balance between increasing the
132 * round-trip latency and reducing overhead.
133 */
134static unsigned int rx_irq_mod_usec = 60;
135
136/* Initial interrupt moderation settings. They can be modified after
137 * module load with ethtool.
138 *
139 * This default is chosen to ensure that a 10G link does not go idle
140 * while a TX queue is stopped after it has become full. A queue is
141 * restarted when it drops below half full. The time this takes (assuming
142 * worst case 3 descriptors per packet and 1024 descriptors) is
143 * 512 / 3 * 1.2 = 205 usec.
144 */
145static unsigned int tx_irq_mod_usec = 150;
146
147/* This is the first interrupt mode to try out of:
148 * 0 => MSI-X
149 * 1 => MSI
150 * 2 => legacy
151 */
152static unsigned int interrupt_mode;
153
154/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
155 * i.e. the number of CPUs among which we may distribute simultaneous
156 * interrupt handling.
157 *
158 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000159 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100160 */
161static unsigned int rss_cpus;
162module_param(rss_cpus, uint, 0444);
163MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
164
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000165static bool phy_flash_cfg;
166module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800167MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
168
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000169static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000170module_param(irq_adapt_low_thresh, uint, 0644);
171MODULE_PARM_DESC(irq_adapt_low_thresh,
172 "Threshold score for reducing IRQ moderation");
173
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000174static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000175module_param(irq_adapt_high_thresh, uint, 0644);
176MODULE_PARM_DESC(irq_adapt_high_thresh,
177 "Threshold score for increasing IRQ moderation");
178
Ben Hutchings62776d02010-06-23 11:30:07 +0000179static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
180 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
181 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
182 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
183module_param(debug, uint, 0);
184MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
185
Ben Hutchings8ceee662008-04-27 12:55:59 +0100186/**************************************************************************
187 *
188 * Utility functions and prototypes
189 *
190 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000191
Jon Cooper261e4d92013-04-15 18:51:54 +0100192static int efx_soft_enable_interrupts(struct efx_nic *efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100193static void efx_soft_disable_interrupts(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000194static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000195static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000196static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100197static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000198static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100199static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000200static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000201static void efx_fini_struct(struct efx_nic *efx);
202static void efx_start_all(struct efx_nic *efx);
203static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100204
205#define EFX_ASSERT_RESET_SERIALISED(efx) \
206 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100207 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000208 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000209 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100210 ASSERT_RTNL(); \
211 } while (0)
212
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100213static int efx_check_disabled(struct efx_nic *efx)
214{
Alexandre Rames626950d2013-01-14 17:20:22 +0000215 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100216 netif_err(efx, drv, efx->net_dev,
217 "device is disabled due to earlier errors\n");
218 return -EIO;
219 }
220 return 0;
221}
222
Ben Hutchings8ceee662008-04-27 12:55:59 +0100223/**************************************************************************
224 *
225 * Event queue processing
226 *
227 *************************************************************************/
228
229/* Process channel's event queue
230 *
231 * This function is responsible for processing the event queue of a
232 * single channel. The caller must guarantee that this function will
233 * never be concurrently called more than once on the same channel,
234 * though different channels may be being processed concurrently.
235 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000236static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100237{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000238 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100239
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000240 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100241 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100242
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000243 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000244 if (spent && efx_channel_has_rx_queue(channel)) {
245 struct efx_rx_queue *rx_queue =
246 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100247
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000248 efx_rx_flush_packet(channel);
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100249 efx_fast_push_rx_descriptors(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100250 }
251
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000252 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100253}
254
Ben Hutchings8ceee662008-04-27 12:55:59 +0100255/* NAPI poll handler
256 *
257 * NAPI guarantees serialisation of polls of the same device, which
258 * provides the guarantee required by efx_process_channel().
259 */
260static int efx_poll(struct napi_struct *napi, int budget)
261{
262 struct efx_channel *channel =
263 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000264 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000265 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100266
Ben Hutchings62776d02010-06-23 11:30:07 +0000267 netif_vdbg(efx, intr, efx->net_dev,
268 "channel %d NAPI poll executing on CPU %d\n",
269 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100270
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000271 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100272
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000273 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000274 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000275 efx->irq_rx_adaptive &&
276 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000277 if (unlikely(channel->irq_mod_score <
278 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000279 if (channel->irq_moderation > 1) {
280 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000281 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000282 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000283 } else if (unlikely(channel->irq_mod_score >
284 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000285 if (channel->irq_moderation <
286 efx->irq_rx_moderation) {
287 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000288 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000289 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000290 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000291 channel->irq_count = 0;
292 channel->irq_mod_score = 0;
293 }
294
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000295 efx_filter_rfs_expire(channel);
296
Ben Hutchings8ceee662008-04-27 12:55:59 +0100297 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800298 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100299 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100300 * interrupts have already been disabled.
301 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800302 napi_complete(napi);
Ben Hutchings514bedb2012-10-05 19:30:16 +0100303 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100304 }
305
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000306 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100307}
308
Ben Hutchings8ceee662008-04-27 12:55:59 +0100309/* Create event queue
310 * Event queue memory allocations are done only once. If the channel
311 * is reset, the memory buffer will be reused; this guards against
312 * errors during channel reset and also simplifies interrupt handling.
313 */
314static int efx_probe_eventq(struct efx_channel *channel)
315{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000316 struct efx_nic *efx = channel->efx;
317 unsigned long entries;
318
Ben Hutchings86ee5302012-01-09 19:51:22 +0000319 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000320 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100321
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000322 /* Build an event queue with room for one event per tx and rx buffer,
323 * plus some extra for link state events and MCDI completions. */
324 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
325 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
326 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
327
Ben Hutchings152b6a62009-11-29 03:43:56 +0000328 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100329}
330
331/* Prepare channel's event queue */
Jon Cooper261e4d92013-04-15 18:51:54 +0100332static int efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100333{
Jon Cooper261e4d92013-04-15 18:51:54 +0100334 int rc;
335
336 EFX_WARN_ON_PARANOID(channel->eventq_init);
337
Ben Hutchings62776d02010-06-23 11:30:07 +0000338 netif_dbg(channel->efx, drv, channel->efx->net_dev,
339 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100340
Jon Cooper261e4d92013-04-15 18:51:54 +0100341 rc = efx_nic_init_eventq(channel);
342 if (rc == 0) {
343 channel->eventq_read_ptr = 0;
344 channel->eventq_init = true;
345 }
346 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100347}
348
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000349/* Enable event queue processing and NAPI */
350static void efx_start_eventq(struct efx_channel *channel)
351{
352 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
353 "chan %d start event queue\n", channel->channel);
354
Ben Hutchings514bedb2012-10-05 19:30:16 +0100355 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000356 channel->enabled = true;
357 smp_wmb();
358
359 napi_enable(&channel->napi_str);
360 efx_nic_eventq_read_ack(channel);
361}
362
363/* Disable event queue processing and NAPI */
364static void efx_stop_eventq(struct efx_channel *channel)
365{
366 if (!channel->enabled)
367 return;
368
369 napi_disable(&channel->napi_str);
370 channel->enabled = false;
371}
372
Ben Hutchings8ceee662008-04-27 12:55:59 +0100373static void efx_fini_eventq(struct efx_channel *channel)
374{
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100375 if (!channel->eventq_init)
376 return;
377
Ben Hutchings62776d02010-06-23 11:30:07 +0000378 netif_dbg(channel->efx, drv, channel->efx->net_dev,
379 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100380
Ben Hutchings152b6a62009-11-29 03:43:56 +0000381 efx_nic_fini_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100382 channel->eventq_init = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100383}
384
385static void efx_remove_eventq(struct efx_channel *channel)
386{
Ben Hutchings62776d02010-06-23 11:30:07 +0000387 netif_dbg(channel->efx, drv, channel->efx->net_dev,
388 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100389
Ben Hutchings152b6a62009-11-29 03:43:56 +0000390 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100391}
392
393/**************************************************************************
394 *
395 * Channel handling
396 *
397 *************************************************************************/
398
Ben Hutchings7f967c02012-02-13 23:45:02 +0000399/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000400static struct efx_channel *
401efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
402{
403 struct efx_channel *channel;
404 struct efx_rx_queue *rx_queue;
405 struct efx_tx_queue *tx_queue;
406 int j;
407
Ben Hutchings7f967c02012-02-13 23:45:02 +0000408 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
409 if (!channel)
410 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000411
Ben Hutchings7f967c02012-02-13 23:45:02 +0000412 channel->efx = efx;
413 channel->channel = i;
414 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000415
Ben Hutchings7f967c02012-02-13 23:45:02 +0000416 for (j = 0; j < EFX_TXQ_TYPES; j++) {
417 tx_queue = &channel->tx_queue[j];
418 tx_queue->efx = efx;
419 tx_queue->queue = i * EFX_TXQ_TYPES + j;
420 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000421 }
422
Ben Hutchings46426102010-09-10 06:42:33 +0000423 rx_queue = &channel->rx_queue;
424 rx_queue->efx = efx;
425 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
426 (unsigned long)rx_queue);
427
428 return channel;
429}
430
Ben Hutchings7f967c02012-02-13 23:45:02 +0000431/* Allocate and initialise a channel structure, copying parameters
432 * (but not resources) from an old channel structure.
433 */
434static struct efx_channel *
435efx_copy_channel(const struct efx_channel *old_channel)
436{
437 struct efx_channel *channel;
438 struct efx_rx_queue *rx_queue;
439 struct efx_tx_queue *tx_queue;
440 int j;
441
442 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
443 if (!channel)
444 return NULL;
445
446 *channel = *old_channel;
447
448 channel->napi_dev = NULL;
449 memset(&channel->eventq, 0, sizeof(channel->eventq));
450
451 for (j = 0; j < EFX_TXQ_TYPES; j++) {
452 tx_queue = &channel->tx_queue[j];
453 if (tx_queue->channel)
454 tx_queue->channel = channel;
455 tx_queue->buffer = NULL;
456 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
457 }
458
459 rx_queue = &channel->rx_queue;
460 rx_queue->buffer = NULL;
461 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
462 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
463 (unsigned long)rx_queue);
464
465 return channel;
466}
467
Ben Hutchings8ceee662008-04-27 12:55:59 +0100468static int efx_probe_channel(struct efx_channel *channel)
469{
470 struct efx_tx_queue *tx_queue;
471 struct efx_rx_queue *rx_queue;
472 int rc;
473
Ben Hutchings62776d02010-06-23 11:30:07 +0000474 netif_dbg(channel->efx, probe, channel->efx->net_dev,
475 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100476
Ben Hutchings7f967c02012-02-13 23:45:02 +0000477 rc = channel->type->pre_probe(channel);
478 if (rc)
479 goto fail;
480
Ben Hutchings8ceee662008-04-27 12:55:59 +0100481 rc = efx_probe_eventq(channel);
482 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000483 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100484
485 efx_for_each_channel_tx_queue(tx_queue, channel) {
486 rc = efx_probe_tx_queue(tx_queue);
487 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000488 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100489 }
490
491 efx_for_each_channel_rx_queue(rx_queue, channel) {
492 rc = efx_probe_rx_queue(rx_queue);
493 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000494 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100495 }
496
497 channel->n_rx_frm_trunc = 0;
498
499 return 0;
500
Ben Hutchings7f967c02012-02-13 23:45:02 +0000501fail:
502 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100503 return rc;
504}
505
Ben Hutchings7f967c02012-02-13 23:45:02 +0000506static void
507efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
508{
509 struct efx_nic *efx = channel->efx;
510 const char *type;
511 int number;
512
513 number = channel->channel;
514 if (efx->tx_channel_offset == 0) {
515 type = "";
516 } else if (channel->channel < efx->tx_channel_offset) {
517 type = "-rx";
518 } else {
519 type = "-tx";
520 number -= efx->tx_channel_offset;
521 }
522 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
523}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100524
Ben Hutchings56536e92008-12-12 21:37:02 -0800525static void efx_set_channel_names(struct efx_nic *efx)
526{
527 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800528
Ben Hutchings7f967c02012-02-13 23:45:02 +0000529 efx_for_each_channel(channel, efx)
530 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100531 efx->msi_context[channel->channel].name,
532 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800533}
534
Ben Hutchings46426102010-09-10 06:42:33 +0000535static int efx_probe_channels(struct efx_nic *efx)
536{
537 struct efx_channel *channel;
538 int rc;
539
540 /* Restart special buffer allocation */
541 efx->next_buffer_table = 0;
542
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000543 /* Probe channels in reverse, so that any 'extra' channels
544 * use the start of the buffer table. This allows the traffic
545 * channels to be resized without moving them or wasting the
546 * entries before them.
547 */
548 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000549 rc = efx_probe_channel(channel);
550 if (rc) {
551 netif_err(efx, probe, efx->net_dev,
552 "failed to create channel %d\n",
553 channel->channel);
554 goto fail;
555 }
556 }
557 efx_set_channel_names(efx);
558
559 return 0;
560
561fail:
562 efx_remove_channels(efx);
563 return rc;
564}
565
Ben Hutchings8ceee662008-04-27 12:55:59 +0100566/* Channels are shutdown and reinitialised whilst the NIC is running
567 * to propagate configuration changes (mtu, checksum offload), or
568 * to clear hardware error conditions
569 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000570static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100571{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000572 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100573 struct efx_tx_queue *tx_queue;
574 struct efx_rx_queue *rx_queue;
575 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000576 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100577
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100578 /* Calculate the rx buffer allocation parameters required to
579 * support the current MTU, including padding for header
580 * alignment and overruns.
581 */
Jon Cooper43a37392012-10-18 15:49:54 +0100582 efx->rx_dma_len = (efx->rx_prefix_size +
Ben Hutchings272baee2013-01-29 23:33:14 +0000583 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
584 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000585 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Ben Hutchingsc14ff2e2013-05-13 11:58:31 +0000586 NET_IP_ALIGN + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000587 if (rx_buf_len <= PAGE_SIZE) {
588 efx->rx_scatter = false;
589 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000590 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000591 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000592 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000593 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
594 EFX_RX_BUF_ALIGNMENT) >
595 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000596 efx->rx_scatter = true;
597 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
598 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000599 } else {
600 efx->rx_scatter = false;
601 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000602 }
603
Daniel Pieczko1648a232013-02-13 10:54:41 +0000604 efx_rx_config_page_split(efx);
605 if (efx->rx_buffer_order)
606 netif_dbg(efx, drv, efx->net_dev,
607 "RX buf len=%u; page order=%u batch=%u\n",
608 efx->rx_dma_len, efx->rx_buffer_order,
609 efx->rx_pages_per_batch);
610 else
611 netif_dbg(efx, drv, efx->net_dev,
612 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
613 efx->rx_dma_len, efx->rx_page_buf_step,
614 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000615
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000616 /* RX filters also have scatter-enabled flags */
617 if (efx->rx_scatter != old_rx_scatter)
Ben Hutchingsadd72472012-11-08 01:46:53 +0000618 efx->type->filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100619
Ben Hutchings14bf7182012-05-22 01:27:58 +0100620 /* We must keep at least one descriptor in a TX ring empty.
621 * We could avoid this when the queue size does not exactly
622 * match the hardware ring size, but it's not that important.
623 * Therefore we stop the queue when one more skb might fill
624 * the ring completely. We wake it when half way back to
625 * empty.
626 */
627 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
628 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
629
Ben Hutchings8ceee662008-04-27 12:55:59 +0100630 /* Initialise the channels */
631 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100632 efx_for_each_channel_tx_queue(tx_queue, channel)
633 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100634
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000635 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100636 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000637 efx_nic_generate_fill_event(rx_queue);
638 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100639
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000640 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100641 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000642
643 if (netif_device_present(efx->net_dev))
644 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100645}
646
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000647static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100648{
649 struct efx_channel *channel;
650 struct efx_tx_queue *tx_queue;
651 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100652 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100653
654 EFX_ASSERT_RESET_SERIALISED(efx);
655 BUG_ON(efx->port_enabled);
656
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100657 /* Stop RX refill */
658 efx_for_each_channel(channel, efx) {
659 efx_for_each_channel_rx_queue(rx_queue, channel)
660 rx_queue->refill_enabled = false;
661 }
662
Ben Hutchings8ceee662008-04-27 12:55:59 +0100663 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000664 /* RX packet processing is pipelined, so wait for the
665 * NAPI handler to complete. At least event queue 0
666 * might be kept active by non-data events, so don't
667 * use napi_synchronize() but actually disable NAPI
668 * temporarily.
669 */
670 if (efx_channel_has_rx_queue(channel)) {
671 efx_stop_eventq(channel);
672 efx_start_eventq(channel);
673 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100674 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100675
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100676 rc = efx->type->fini_dmaq(efx);
677 if (rc && EFX_WORKAROUND_7803(efx)) {
678 /* Schedule a reset to recover from the flush failure. The
679 * descriptor caches reference memory we're about to free,
680 * but falcon_reconfigure_mac_wrapper() won't reconnect
681 * the MACs because of the pending reset.
682 */
683 netif_err(efx, drv, efx->net_dev,
684 "Resetting to recover from flush failure\n");
685 efx_schedule_reset(efx, RESET_TYPE_ALL);
686 } else if (rc) {
687 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
688 } else {
689 netif_dbg(efx, drv, efx->net_dev,
690 "successfully flushed all queues\n");
691 }
692
693 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100694 efx_for_each_channel_rx_queue(rx_queue, channel)
695 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000696 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100697 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100698 }
699}
700
701static void efx_remove_channel(struct efx_channel *channel)
702{
703 struct efx_tx_queue *tx_queue;
704 struct efx_rx_queue *rx_queue;
705
Ben Hutchings62776d02010-06-23 11:30:07 +0000706 netif_dbg(channel->efx, drv, channel->efx->net_dev,
707 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100708
709 efx_for_each_channel_rx_queue(rx_queue, channel)
710 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000711 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100712 efx_remove_tx_queue(tx_queue);
713 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100714 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100715}
716
Ben Hutchings46426102010-09-10 06:42:33 +0000717static void efx_remove_channels(struct efx_nic *efx)
718{
719 struct efx_channel *channel;
720
721 efx_for_each_channel(channel, efx)
722 efx_remove_channel(channel);
723}
724
725int
726efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
727{
728 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
729 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000730 unsigned i, next_buffer_table = 0;
Jon Cooper261e4d92013-04-15 18:51:54 +0100731 int rc, rc2;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100732
733 rc = efx_check_disabled(efx);
734 if (rc)
735 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000736
737 /* Not all channels should be reallocated. We must avoid
738 * reallocating their buffer table entries.
739 */
740 efx_for_each_channel(channel, efx) {
741 struct efx_rx_queue *rx_queue;
742 struct efx_tx_queue *tx_queue;
743
744 if (channel->type->copy)
745 continue;
746 next_buffer_table = max(next_buffer_table,
747 channel->eventq.index +
748 channel->eventq.entries);
749 efx_for_each_channel_rx_queue(rx_queue, channel)
750 next_buffer_table = max(next_buffer_table,
751 rx_queue->rxd.index +
752 rx_queue->rxd.entries);
753 efx_for_each_channel_tx_queue(tx_queue, channel)
754 next_buffer_table = max(next_buffer_table,
755 tx_queue->txd.index +
756 tx_queue->txd.entries);
757 }
Ben Hutchings46426102010-09-10 06:42:33 +0000758
Ben Hutchings29c69a42013-01-28 19:01:06 +0000759 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000760 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100761 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000762
Ben Hutchings7f967c02012-02-13 23:45:02 +0000763 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000764 memset(other_channel, 0, sizeof(other_channel));
765 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000766 channel = efx->channel[i];
767 if (channel->type->copy)
768 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000769 if (!channel) {
770 rc = -ENOMEM;
771 goto out;
772 }
773 other_channel[i] = channel;
774 }
775
776 /* Swap entry counts and channel pointers */
777 old_rxq_entries = efx->rxq_entries;
778 old_txq_entries = efx->txq_entries;
779 efx->rxq_entries = rxq_entries;
780 efx->txq_entries = txq_entries;
781 for (i = 0; i < efx->n_channels; i++) {
782 channel = efx->channel[i];
783 efx->channel[i] = other_channel[i];
784 other_channel[i] = channel;
785 }
786
Ben Hutchings7f967c02012-02-13 23:45:02 +0000787 /* Restart buffer table allocation */
788 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000789
Ben Hutchingse8f14992010-12-07 19:47:34 +0000790 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000791 channel = efx->channel[i];
792 if (!channel->type->copy)
793 continue;
794 rc = efx_probe_channel(channel);
795 if (rc)
796 goto rollback;
797 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000798 }
Ben Hutchings46426102010-09-10 06:42:33 +0000799
Ben Hutchings7f967c02012-02-13 23:45:02 +0000800out:
801 /* Destroy unused channel structures */
802 for (i = 0; i < efx->n_channels; i++) {
803 channel = other_channel[i];
804 if (channel && channel->type->copy) {
805 efx_fini_napi_channel(channel);
806 efx_remove_channel(channel);
807 kfree(channel);
808 }
809 }
810
Jon Cooper261e4d92013-04-15 18:51:54 +0100811 rc2 = efx_soft_enable_interrupts(efx);
812 if (rc2) {
813 rc = rc ? rc : rc2;
814 netif_err(efx, drv, efx->net_dev,
815 "unable to restart interrupts on channel reallocation\n");
816 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
817 } else {
818 efx_start_all(efx);
819 netif_device_attach(efx->net_dev);
820 }
Ben Hutchings46426102010-09-10 06:42:33 +0000821 return rc;
822
823rollback:
824 /* Swap back */
825 efx->rxq_entries = old_rxq_entries;
826 efx->txq_entries = old_txq_entries;
827 for (i = 0; i < efx->n_channels; i++) {
828 channel = efx->channel[i];
829 efx->channel[i] = other_channel[i];
830 other_channel[i] = channel;
831 }
832 goto out;
833}
834
Steve Hodgson90d683a2010-06-01 11:19:39 +0000835void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100836{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000837 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100838}
839
Ben Hutchings7f967c02012-02-13 23:45:02 +0000840static const struct efx_channel_type efx_default_channel_type = {
841 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100842 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000843 .get_name = efx_get_channel_name,
844 .copy = efx_copy_channel,
845 .keep_eventq = false,
846};
847
848int efx_channel_dummy_op_int(struct efx_channel *channel)
849{
850 return 0;
851}
852
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100853void efx_channel_dummy_op_void(struct efx_channel *channel)
854{
855}
856
Ben Hutchings8ceee662008-04-27 12:55:59 +0100857/**************************************************************************
858 *
859 * Port handling
860 *
861 **************************************************************************/
862
863/* This ensures that the kernel is kept informed (via
864 * netif_carrier_on/off) of the link status, and also maintains the
865 * link status's stop on the port's TX queue.
866 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000867void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100868{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000869 struct efx_link_state *link_state = &efx->link_state;
870
Ben Hutchings8ceee662008-04-27 12:55:59 +0100871 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
872 * that no events are triggered between unregister_netdev() and the
873 * driver unloading. A more general condition is that NETDEV_CHANGE
874 * can only be generated between NETDEV_UP and NETDEV_DOWN */
875 if (!netif_running(efx->net_dev))
876 return;
877
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000878 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100879 efx->n_link_state_changes++;
880
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000881 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100882 netif_carrier_on(efx->net_dev);
883 else
884 netif_carrier_off(efx->net_dev);
885 }
886
887 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000888 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000889 netif_info(efx, link, efx->net_dev,
Ben Hutchings964e6132012-11-19 23:08:22 +0000890 "link up at %uMbps %s-duplex (MTU %d)\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000891 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings964e6132012-11-19 23:08:22 +0000892 efx->net_dev->mtu);
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000893 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000894 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100895}
896
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000897void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
898{
899 efx->link_advertising = advertising;
900 if (advertising) {
901 if (advertising & ADVERTISED_Pause)
902 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
903 else
904 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
905 if (advertising & ADVERTISED_Asym_Pause)
906 efx->wanted_fc ^= EFX_FC_TX;
907 }
908}
909
David S. Millerb56269462011-05-17 17:53:22 -0400910void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000911{
912 efx->wanted_fc = wanted_fc;
913 if (efx->link_advertising) {
914 if (wanted_fc & EFX_FC_RX)
915 efx->link_advertising |= (ADVERTISED_Pause |
916 ADVERTISED_Asym_Pause);
917 else
918 efx->link_advertising &= ~(ADVERTISED_Pause |
919 ADVERTISED_Asym_Pause);
920 if (wanted_fc & EFX_FC_TX)
921 efx->link_advertising ^= ADVERTISED_Asym_Pause;
922 }
923}
924
Ben Hutchings115122a2009-03-04 09:52:52 +0000925static void efx_fini_port(struct efx_nic *efx);
926
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000927/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
928 * the MAC appropriately. All other PHY configuration changes are pushed
929 * through phy_op->set_settings(), and pushed asynchronously to the MAC
930 * through efx_monitor().
931 *
932 * Callers must hold the mac_lock
933 */
934int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100935{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000936 enum efx_phy_mode phy_mode;
937 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100938
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000939 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100940
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000941 /* Disable PHY transmit in mac level loopbacks */
942 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800943 if (LOOPBACK_INTERNAL(efx))
944 efx->phy_mode |= PHY_MODE_TX_DISABLED;
945 else
946 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800947
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000948 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800949
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000950 if (rc)
951 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100952
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000953 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100954}
955
956/* Reinitialise the MAC to pick up new PHY settings, even if the port is
957 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000958int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100959{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000960 int rc;
961
Ben Hutchings8ceee662008-04-27 12:55:59 +0100962 EFX_ASSERT_RESET_SERIALISED(efx);
963
964 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000965 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100966 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000967
968 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100969}
970
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000971/* Asynchronous work item for changing MAC promiscuity and multicast
972 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
973 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800974static void efx_mac_work(struct work_struct *data)
975{
976 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
977
978 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100979 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100980 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800981 mutex_unlock(&efx->mac_lock);
982}
983
Ben Hutchings8ceee662008-04-27 12:55:59 +0100984static int efx_probe_port(struct efx_nic *efx)
985{
986 int rc;
987
Ben Hutchings62776d02010-06-23 11:30:07 +0000988 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100989
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000990 if (phy_flash_cfg)
991 efx->phy_mode = PHY_MODE_SPECIAL;
992
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000993 /* Connect up MAC/PHY operations table */
994 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100995 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000996 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100997
Ben Hutchingse332bcb2011-12-20 01:22:51 +0000998 /* Initialise MAC address to permanent address */
999 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001000
1001 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001002}
1003
1004static int efx_init_port(struct efx_nic *efx)
1005{
1006 int rc;
1007
Ben Hutchings62776d02010-06-23 11:30:07 +00001008 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001009
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001010 mutex_lock(&efx->mac_lock);
1011
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001012 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001013 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001014 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001015
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001016 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001017
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001018 /* Reconfigure the MAC before creating dma queues (required for
1019 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001020 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001021
1022 /* Ensure the PHY advertises the correct flow control settings */
1023 rc = efx->phy_op->reconfigure(efx);
1024 if (rc)
1025 goto fail2;
1026
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001027 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001028 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001029
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001030fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001031 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001032fail1:
1033 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001034 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001035}
1036
Ben Hutchings8ceee662008-04-27 12:55:59 +01001037static void efx_start_port(struct efx_nic *efx)
1038{
Ben Hutchings62776d02010-06-23 11:30:07 +00001039 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001040 BUG_ON(efx->port_enabled);
1041
1042 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001043 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001044
1045 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1046 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001047 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001048
Ben Hutchings8ceee662008-04-27 12:55:59 +01001049 mutex_unlock(&efx->mac_lock);
1050}
1051
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001052/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001053static void efx_stop_port(struct efx_nic *efx)
1054{
Ben Hutchings62776d02010-06-23 11:30:07 +00001055 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001056
1057 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001058 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001059 mutex_unlock(&efx->mac_lock);
1060
1061 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001062 netif_addr_lock_bh(efx->net_dev);
1063 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001064}
1065
1066static void efx_fini_port(struct efx_nic *efx)
1067{
Ben Hutchings62776d02010-06-23 11:30:07 +00001068 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001069
1070 if (!efx->port_initialized)
1071 return;
1072
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001073 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001074 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001075
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001076 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001077 efx_link_status_changed(efx);
1078}
1079
1080static void efx_remove_port(struct efx_nic *efx)
1081{
Ben Hutchings62776d02010-06-23 11:30:07 +00001082 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001083
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001084 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001085}
1086
1087/**************************************************************************
1088 *
1089 * NIC handling
1090 *
1091 **************************************************************************/
1092
1093/* This configures the PCI device to enable I/O and DMA. */
1094static int efx_init_io(struct efx_nic *efx)
1095{
1096 struct pci_dev *pci_dev = efx->pci_dev;
1097 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001098 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001099 int rc;
1100
Ben Hutchings62776d02010-06-23 11:30:07 +00001101 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001102
1103 rc = pci_enable_device(pci_dev);
1104 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001105 netif_err(efx, probe, efx->net_dev,
1106 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001107 goto fail1;
1108 }
1109
1110 pci_set_master(pci_dev);
1111
1112 /* Set the PCI DMA mask. Try all possibilities from our
1113 * genuine mask down to 32 bits, because some architectures
1114 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1115 * masks event though they reject 46 bit masks.
1116 */
1117 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001118 if (dma_supported(&pci_dev->dev, dma_mask)) {
1119 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001120 if (rc == 0)
1121 break;
1122 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001123 dma_mask >>= 1;
1124 }
1125 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001126 netif_err(efx, probe, efx->net_dev,
1127 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001128 goto fail2;
1129 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001130 netif_dbg(efx, probe, efx->net_dev,
1131 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001132 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001133 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001134 /* dma_set_coherent_mask() is not *allowed* to
1135 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001136 * but just in case...
1137 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001138 netif_err(efx, probe, efx->net_dev,
1139 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001140 goto fail2;
1141 }
1142
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001143 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1144 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001145 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001146 netif_err(efx, probe, efx->net_dev,
1147 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001148 rc = -EIO;
1149 goto fail3;
1150 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001151 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001152 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001153 netif_err(efx, probe, efx->net_dev,
1154 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001155 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001156 rc = -ENOMEM;
1157 goto fail4;
1158 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001159 netif_dbg(efx, probe, efx->net_dev,
1160 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001161 (unsigned long long)efx->membase_phys, mem_map_size,
1162 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001163
1164 return 0;
1165
1166 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001167 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001168 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001169 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001170 fail2:
1171 pci_disable_device(efx->pci_dev);
1172 fail1:
1173 return rc;
1174}
1175
1176static void efx_fini_io(struct efx_nic *efx)
1177{
Ben Hutchings62776d02010-06-23 11:30:07 +00001178 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001179
1180 if (efx->membase) {
1181 iounmap(efx->membase);
1182 efx->membase = NULL;
1183 }
1184
1185 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001186 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001187 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001188 }
1189
1190 pci_disable_device(efx->pci_dev);
1191}
1192
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001193static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001194{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001195 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001196 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001197 int cpu;
1198
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001199 if (rss_cpus) {
1200 count = rss_cpus;
1201 } else {
1202 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1203 netif_warn(efx, probe, efx->net_dev,
1204 "RSS disabled due to allocation failure\n");
1205 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001206 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001207
1208 count = 0;
1209 for_each_online_cpu(cpu) {
1210 if (!cpumask_test_cpu(cpu, thread_mask)) {
1211 ++count;
1212 cpumask_or(thread_mask, thread_mask,
1213 topology_thread_cpumask(cpu));
1214 }
1215 }
1216
1217 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001218 }
1219
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001220 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1221 * table entries that are inaccessible to VFs
1222 */
1223 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1224 count > efx_vf_size(efx)) {
1225 netif_warn(efx, probe, efx->net_dev,
1226 "Reducing number of RSS channels from %u to %u for "
1227 "VF support. Increase vf-msix-limit to use more "
1228 "channels on the PF.\n",
1229 count, efx_vf_size(efx));
1230 count = efx_vf_size(efx);
1231 }
1232
Ben Hutchings46123d02008-09-01 12:47:33 +01001233 return count;
1234}
1235
1236/* Probe the number and type of interrupts we are able to obtain, and
1237 * the resulting numbers of channels and RX queues.
1238 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001239static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001240{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001241 unsigned int extra_channels = 0;
1242 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001243 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001244
Ben Hutchings7f967c02012-02-13 23:45:02 +00001245 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1246 if (efx->extra_channel_type[i])
1247 ++extra_channels;
1248
Ben Hutchings8ceee662008-04-27 12:55:59 +01001249 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001250 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001251 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001252
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001253 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001254 if (separate_tx_channels)
1255 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001256 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001257 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001258
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001259 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001260 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001261 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001262 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001263 netif_err(efx, drv, efx->net_dev,
1264 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001265 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001266 netif_err(efx, drv, efx->net_dev,
1267 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001268 EFX_BUG_ON_PARANOID(rc >= n_channels);
1269 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001270 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001271 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001272 }
1273
1274 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001275 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001276 if (n_channels > extra_channels)
1277 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001278 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001279 efx->n_tx_channels = max(n_channels / 2, 1U);
1280 efx->n_rx_channels = max(n_channels -
1281 efx->n_tx_channels,
1282 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001283 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001284 efx->n_tx_channels = n_channels;
1285 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001286 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001287 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001288 efx_get_channel(efx, i)->irq =
1289 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001290 } else {
1291 /* Fall back to single channel MSI */
1292 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001293 netif_err(efx, drv, efx->net_dev,
1294 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001295 }
1296 }
1297
1298 /* Try single interrupt MSI */
1299 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001300 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001301 efx->n_rx_channels = 1;
1302 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001303 rc = pci_enable_msi(efx->pci_dev);
1304 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001305 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001306 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001307 netif_err(efx, drv, efx->net_dev,
1308 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001309 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1310 }
1311 }
1312
1313 /* Assume legacy interrupts */
1314 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001315 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001316 efx->n_rx_channels = 1;
1317 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001318 efx->legacy_irq = efx->pci_dev->irq;
1319 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001320
Ben Hutchings7f967c02012-02-13 23:45:02 +00001321 /* Assign extra channels if possible */
1322 j = efx->n_channels;
1323 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1324 if (!efx->extra_channel_type[i])
1325 continue;
1326 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1327 efx->n_channels <= extra_channels) {
1328 efx->extra_channel_type[i]->handle_no_channel(efx);
1329 } else {
1330 --j;
1331 efx_get_channel(efx, j)->type =
1332 efx->extra_channel_type[i];
1333 }
1334 }
1335
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001336 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001337 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001338 efx->n_rx_channels : efx_vf_size(efx));
1339
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001340 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001341}
1342
Jon Cooper261e4d92013-04-15 18:51:54 +01001343static int efx_soft_enable_interrupts(struct efx_nic *efx)
Ben Hutchingsd8291182012-10-05 23:35:41 +01001344{
Jon Cooper261e4d92013-04-15 18:51:54 +01001345 struct efx_channel *channel, *end_channel;
1346 int rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001347
1348 BUG_ON(efx->state == STATE_DISABLED);
1349
1350 efx->irq_soft_enabled = true;
1351 smp_wmb();
1352
1353 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001354 if (!channel->type->keep_eventq) {
1355 rc = efx_init_eventq(channel);
1356 if (rc)
1357 goto fail;
1358 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001359 efx_start_eventq(channel);
1360 }
1361
1362 efx_mcdi_mode_event(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +01001363
1364 return 0;
1365fail:
1366 end_channel = channel;
1367 efx_for_each_channel(channel, efx) {
1368 if (channel == end_channel)
1369 break;
1370 efx_stop_eventq(channel);
1371 if (!channel->type->keep_eventq)
1372 efx_fini_eventq(channel);
1373 }
1374
1375 return rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001376}
1377
1378static void efx_soft_disable_interrupts(struct efx_nic *efx)
1379{
1380 struct efx_channel *channel;
1381
1382 if (efx->state == STATE_DISABLED)
1383 return;
1384
1385 efx_mcdi_mode_poll(efx);
1386
1387 efx->irq_soft_enabled = false;
1388 smp_wmb();
1389
1390 if (efx->legacy_irq)
1391 synchronize_irq(efx->legacy_irq);
1392
1393 efx_for_each_channel(channel, efx) {
1394 if (channel->irq)
1395 synchronize_irq(channel->irq);
1396
1397 efx_stop_eventq(channel);
1398 if (!channel->type->keep_eventq)
1399 efx_fini_eventq(channel);
1400 }
Ben Hutchingscade7152013-08-27 23:12:31 +01001401
1402 /* Flush the asynchronous MCDI request queue */
1403 efx_mcdi_flush_async(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01001404}
1405
Jon Cooper261e4d92013-04-15 18:51:54 +01001406static int efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001407{
Jon Cooper261e4d92013-04-15 18:51:54 +01001408 struct efx_channel *channel, *end_channel;
1409 int rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001410
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001411 BUG_ON(efx->state == STATE_DISABLED);
1412
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001413 if (efx->eeh_disabled_legacy_irq) {
1414 enable_irq(efx->legacy_irq);
1415 efx->eeh_disabled_legacy_irq = false;
1416 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001417
Ben Hutchings86094f72013-08-21 19:51:04 +01001418 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001419
1420 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001421 if (channel->type->keep_eventq) {
1422 rc = efx_init_eventq(channel);
1423 if (rc)
1424 goto fail;
1425 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001426 }
1427
Jon Cooper261e4d92013-04-15 18:51:54 +01001428 rc = efx_soft_enable_interrupts(efx);
1429 if (rc)
1430 goto fail;
1431
1432 return 0;
1433
1434fail:
1435 end_channel = channel;
1436 efx_for_each_channel(channel, efx) {
1437 if (channel == end_channel)
1438 break;
1439 if (channel->type->keep_eventq)
1440 efx_fini_eventq(channel);
1441 }
1442
1443 efx->type->irq_disable_non_ev(efx);
1444
1445 return rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001446}
1447
Ben Hutchingsd8291182012-10-05 23:35:41 +01001448static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001449{
1450 struct efx_channel *channel;
1451
Ben Hutchingsd8291182012-10-05 23:35:41 +01001452 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001453
1454 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001455 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001456 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001457 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001458
Ben Hutchings86094f72013-08-21 19:51:04 +01001459 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001460}
1461
Ben Hutchings8ceee662008-04-27 12:55:59 +01001462static void efx_remove_interrupts(struct efx_nic *efx)
1463{
1464 struct efx_channel *channel;
1465
1466 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001467 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001468 channel->irq = 0;
1469 pci_disable_msi(efx->pci_dev);
1470 pci_disable_msix(efx->pci_dev);
1471
1472 /* Remove legacy interrupt */
1473 efx->legacy_irq = 0;
1474}
1475
Ben Hutchings8831da72008-09-01 12:47:48 +01001476static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001477{
Ben Hutchings602a5322011-05-16 17:32:39 +01001478 struct efx_channel *channel;
1479 struct efx_tx_queue *tx_queue;
1480
Ben Hutchings97653432011-01-12 18:26:56 +00001481 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001482 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001483
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001484 /* We need to mark which channels really have RX and TX
1485 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001486 * RX-only and TX-only channels.
1487 */
1488 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001489 if (channel->channel < efx->n_rx_channels)
1490 channel->rx_queue.core_index = channel->channel;
1491 else
1492 channel->rx_queue.core_index = -1;
1493
Ben Hutchings602a5322011-05-16 17:32:39 +01001494 efx_for_each_channel_tx_queue(tx_queue, channel)
1495 tx_queue->queue -= (efx->tx_channel_offset *
1496 EFX_TXQ_TYPES);
1497 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001498}
1499
1500static int efx_probe_nic(struct efx_nic *efx)
1501{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001502 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001503 int rc;
1504
Ben Hutchings62776d02010-06-23 11:30:07 +00001505 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001506
1507 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001508 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001509 if (rc)
1510 return rc;
1511
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001512 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001513 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001514 rc = efx_probe_interrupts(efx);
1515 if (rc)
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001516 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001517
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001518 rc = efx->type->dimension_resources(efx);
1519 if (rc)
1520 goto fail2;
Ben Hutchings28e47c42012-02-15 01:58:49 +00001521
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001522 if (efx->n_channels > 1)
1523 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001524 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001525 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001526 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001527
Ben Hutchings8831da72008-09-01 12:47:48 +01001528 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001529 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1530 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001531
1532 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001533 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1534 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001535
1536 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001537
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001538fail2:
1539 efx_remove_interrupts(efx);
1540fail1:
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001541 efx->type->remove(efx);
1542 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001543}
1544
1545static void efx_remove_nic(struct efx_nic *efx)
1546{
Ben Hutchings62776d02010-06-23 11:30:07 +00001547 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001548
1549 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001550 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001551}
1552
Ben Hutchingsadd72472012-11-08 01:46:53 +00001553static int efx_probe_filters(struct efx_nic *efx)
1554{
1555 int rc;
1556
1557 spin_lock_init(&efx->filter_lock);
1558
1559 rc = efx->type->filter_table_probe(efx);
1560 if (rc)
1561 return rc;
1562
1563#ifdef CONFIG_RFS_ACCEL
1564 if (efx->type->offload_features & NETIF_F_NTUPLE) {
1565 efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
1566 sizeof(*efx->rps_flow_id),
1567 GFP_KERNEL);
1568 if (!efx->rps_flow_id) {
1569 efx->type->filter_table_remove(efx);
1570 return -ENOMEM;
1571 }
1572 }
1573#endif
1574
1575 return 0;
1576}
1577
1578static void efx_remove_filters(struct efx_nic *efx)
1579{
1580#ifdef CONFIG_RFS_ACCEL
1581 kfree(efx->rps_flow_id);
1582#endif
1583 efx->type->filter_table_remove(efx);
1584}
1585
1586static void efx_restore_filters(struct efx_nic *efx)
1587{
1588 efx->type->filter_table_restore(efx);
1589}
1590
Ben Hutchings8ceee662008-04-27 12:55:59 +01001591/**************************************************************************
1592 *
1593 * NIC startup/shutdown
1594 *
1595 *************************************************************************/
1596
1597static int efx_probe_all(struct efx_nic *efx)
1598{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001599 int rc;
1600
Ben Hutchings8ceee662008-04-27 12:55:59 +01001601 rc = efx_probe_nic(efx);
1602 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001603 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001604 goto fail1;
1605 }
1606
Ben Hutchings8ceee662008-04-27 12:55:59 +01001607 rc = efx_probe_port(efx);
1608 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001609 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001610 goto fail2;
1611 }
1612
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001613 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1614 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1615 rc = -EINVAL;
1616 goto fail3;
1617 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001618 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001619
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001620 rc = efx_probe_filters(efx);
1621 if (rc) {
1622 netif_err(efx, probe, efx->net_dev,
1623 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001624 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001625 }
1626
Ben Hutchings7f967c02012-02-13 23:45:02 +00001627 rc = efx_probe_channels(efx);
1628 if (rc)
1629 goto fail4;
1630
Ben Hutchings8ceee662008-04-27 12:55:59 +01001631 return 0;
1632
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001633 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001634 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001635 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001636 efx_remove_port(efx);
1637 fail2:
1638 efx_remove_nic(efx);
1639 fail1:
1640 return rc;
1641}
1642
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001643/* If the interface is supposed to be running but is not, start
1644 * the hardware and software data path, regular activity for the port
1645 * (MAC statistics, link polling, etc.) and schedule the port to be
1646 * reconfigured. Interrupts must already be enabled. This function
1647 * is safe to call multiple times, so long as the NIC is not disabled.
1648 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001649 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001650static void efx_start_all(struct efx_nic *efx)
1651{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001652 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001653 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001654
1655 /* Check that it is appropriate to restart the interface. All
1656 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001657 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001658 return;
1659
Ben Hutchings8ceee662008-04-27 12:55:59 +01001660 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001661 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001662
Alexandre Rames626950d2013-01-14 17:20:22 +00001663 /* Start the hardware monitor if there is one */
1664 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001665 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1666 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001667
1668 /* If link state detection is normally event-driven, we have
1669 * to poll now because we could have missed a change
1670 */
1671 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001672 mutex_lock(&efx->mac_lock);
1673 if (efx->phy_op->poll(efx))
1674 efx_link_status_changed(efx);
1675 mutex_unlock(&efx->mac_lock);
1676 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001677
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001678 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001679}
1680
1681/* Flush all delayed work. Should only be called when no more delayed work
1682 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1683 * since we're holding the rtnl_lock at this point. */
1684static void efx_flush_all(struct efx_nic *efx)
1685{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001686 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001687 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001688 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001689 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001690 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001691}
1692
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001693/* Quiesce the hardware and software data path, and regular activity
1694 * for the port without bringing the link down. Safe to call multiple
1695 * times with the NIC in almost any state, but interrupts should be
1696 * enabled. Requires the RTNL lock.
1697 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001698static void efx_stop_all(struct efx_nic *efx)
1699{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001700 EFX_ASSERT_RESET_SERIALISED(efx);
1701
1702 /* port_enabled can be read safely under the rtnl lock */
1703 if (!efx->port_enabled)
1704 return;
1705
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001706 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001707 efx_stop_port(efx);
1708
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001709 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001710 efx_flush_all(efx);
1711
Ben Hutchings29c69a42013-01-28 19:01:06 +00001712 /* Stop the kernel transmit interface. This is only valid if
1713 * the device is stopped or detached; otherwise the watchdog
1714 * may fire immediately.
1715 */
1716 WARN_ON(netif_running(efx->net_dev) &&
1717 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001718 netif_tx_disable(efx->net_dev);
1719
1720 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001721}
1722
1723static void efx_remove_all(struct efx_nic *efx)
1724{
Ben Hutchings46426102010-09-10 06:42:33 +00001725 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001726 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001727 efx_remove_port(efx);
1728 efx_remove_nic(efx);
1729}
1730
Ben Hutchings8ceee662008-04-27 12:55:59 +01001731/**************************************************************************
1732 *
1733 * Interrupt moderation
1734 *
1735 **************************************************************************/
1736
Ben Hutchingscc180b62011-12-08 19:51:47 +00001737static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001738{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001739 if (usecs == 0)
1740 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001741 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001742 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001743 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001744}
1745
Ben Hutchings8ceee662008-04-27 12:55:59 +01001746/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001747int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1748 unsigned int rx_usecs, bool rx_adaptive,
1749 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001750{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001751 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001752 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1753 efx->timer_quantum_ns,
1754 1000);
1755 unsigned int tx_ticks;
1756 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001757
1758 EFX_ASSERT_RESET_SERIALISED(efx);
1759
Ben Hutchingscc180b62011-12-08 19:51:47 +00001760 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001761 return -EINVAL;
1762
Ben Hutchingscc180b62011-12-08 19:51:47 +00001763 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1764 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1765
Ben Hutchings9e393b32011-09-05 07:43:04 +00001766 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1767 !rx_may_override_tx) {
1768 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1769 "RX and TX IRQ moderation must be equal\n");
1770 return -EINVAL;
1771 }
1772
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001773 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001774 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001775 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001776 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001777 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001778 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001779 channel->irq_moderation = tx_ticks;
1780 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001781
1782 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001783}
1784
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001785void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1786 unsigned int *rx_usecs, bool *rx_adaptive)
1787{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001788 /* We must round up when converting ticks to microseconds
1789 * because we round down when converting the other way.
1790 */
1791
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001792 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001793 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1794 efx->timer_quantum_ns,
1795 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001796
1797 /* If channels are shared between RX and TX, so is IRQ
1798 * moderation. Otherwise, IRQ moderation is the same for all
1799 * TX channels and is not adaptive.
1800 */
1801 if (efx->tx_channel_offset == 0)
1802 *tx_usecs = *rx_usecs;
1803 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001804 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001805 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001806 efx->timer_quantum_ns,
1807 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001808}
1809
Ben Hutchings8ceee662008-04-27 12:55:59 +01001810/**************************************************************************
1811 *
1812 * Hardware monitor
1813 *
1814 **************************************************************************/
1815
Ben Hutchingse254c272010-09-20 08:44:10 +00001816/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001817static void efx_monitor(struct work_struct *data)
1818{
1819 struct efx_nic *efx = container_of(data, struct efx_nic,
1820 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001821
Ben Hutchings62776d02010-06-23 11:30:07 +00001822 netif_vdbg(efx, timer, efx->net_dev,
1823 "hardware monitor executing on CPU %d\n",
1824 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001825 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001826
Ben Hutchings8ceee662008-04-27 12:55:59 +01001827 /* If the mac_lock is already held then it is likely a port
1828 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001829 * most of the work of monitor() anyway. */
1830 if (mutex_trylock(&efx->mac_lock)) {
1831 if (efx->port_enabled)
1832 efx->type->monitor(efx);
1833 mutex_unlock(&efx->mac_lock);
1834 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001835
Ben Hutchings8ceee662008-04-27 12:55:59 +01001836 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1837 efx_monitor_interval);
1838}
1839
1840/**************************************************************************
1841 *
1842 * ioctls
1843 *
1844 *************************************************************************/
1845
1846/* Net device ioctl
1847 * Context: process, rtnl_lock() held.
1848 */
1849static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1850{
Ben Hutchings767e4682008-09-01 12:43:14 +01001851 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001852 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001853
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001854 if (cmd == SIOCSHWTSTAMP)
1855 return efx_ptp_ioctl(efx, ifr, cmd);
1856
Ben Hutchings68e7f452009-04-29 08:05:08 +00001857 /* Convert phy_id from older PRTAD/DEVAD format */
1858 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1859 (data->phy_id & 0xfc00) == 0x0400)
1860 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1861
1862 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001863}
1864
1865/**************************************************************************
1866 *
1867 * NAPI interface
1868 *
1869 **************************************************************************/
1870
Ben Hutchings7f967c02012-02-13 23:45:02 +00001871static void efx_init_napi_channel(struct efx_channel *channel)
1872{
1873 struct efx_nic *efx = channel->efx;
1874
1875 channel->napi_dev = efx->net_dev;
1876 netif_napi_add(channel->napi_dev, &channel->napi_str,
1877 efx_poll, napi_weight);
1878}
1879
Ben Hutchingse8f14992010-12-07 19:47:34 +00001880static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001881{
1882 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001883
Ben Hutchings7f967c02012-02-13 23:45:02 +00001884 efx_for_each_channel(channel, efx)
1885 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001886}
1887
1888static void efx_fini_napi_channel(struct efx_channel *channel)
1889{
1890 if (channel->napi_dev)
1891 netif_napi_del(&channel->napi_str);
1892 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001893}
1894
1895static void efx_fini_napi(struct efx_nic *efx)
1896{
1897 struct efx_channel *channel;
1898
Ben Hutchingse8f14992010-12-07 19:47:34 +00001899 efx_for_each_channel(channel, efx)
1900 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001901}
1902
1903/**************************************************************************
1904 *
1905 * Kernel netpoll interface
1906 *
1907 *************************************************************************/
1908
1909#ifdef CONFIG_NET_POLL_CONTROLLER
1910
1911/* Although in the common case interrupts will be disabled, this is not
1912 * guaranteed. However, all our work happens inside the NAPI callback,
1913 * so no locking is required.
1914 */
1915static void efx_netpoll(struct net_device *net_dev)
1916{
Ben Hutchings767e4682008-09-01 12:43:14 +01001917 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001918 struct efx_channel *channel;
1919
Ben Hutchings64ee3122008-09-01 12:47:38 +01001920 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001921 efx_schedule_channel(channel);
1922}
1923
1924#endif
1925
1926/**************************************************************************
1927 *
1928 * Kernel net device interface
1929 *
1930 *************************************************************************/
1931
1932/* Context: process, rtnl_lock() held. */
1933static int efx_net_open(struct net_device *net_dev)
1934{
Ben Hutchings767e4682008-09-01 12:43:14 +01001935 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001936 int rc;
1937
Ben Hutchings62776d02010-06-23 11:30:07 +00001938 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1939 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001940
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001941 rc = efx_check_disabled(efx);
1942 if (rc)
1943 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001944 if (efx->phy_mode & PHY_MODE_SPECIAL)
1945 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001946 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1947 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001948
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001949 /* Notify the kernel of the link state polled during driver load,
1950 * before the monitor starts running */
1951 efx_link_status_changed(efx);
1952
Ben Hutchings8ceee662008-04-27 12:55:59 +01001953 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001954 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001955 return 0;
1956}
1957
1958/* Context: process, rtnl_lock() held.
1959 * Note that the kernel will ignore our return code; this method
1960 * should really be a void.
1961 */
1962static int efx_net_stop(struct net_device *net_dev)
1963{
Ben Hutchings767e4682008-09-01 12:43:14 +01001964 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001965
Ben Hutchings62776d02010-06-23 11:30:07 +00001966 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1967 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001968
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001969 /* Stop the device and flush all the channels */
1970 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001971
1972 return 0;
1973}
1974
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001975/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001976static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1977 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001978{
Ben Hutchings767e4682008-09-01 12:43:14 +01001979 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001980
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001981 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00001982 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001983 spin_unlock_bh(&efx->stats_lock);
1984
Ben Hutchings8ceee662008-04-27 12:55:59 +01001985 return stats;
1986}
1987
1988/* Context: netif_tx_lock held, BHs disabled. */
1989static void efx_watchdog(struct net_device *net_dev)
1990{
Ben Hutchings767e4682008-09-01 12:43:14 +01001991 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001992
Ben Hutchings62776d02010-06-23 11:30:07 +00001993 netif_err(efx, tx_err, efx->net_dev,
1994 "TX stuck with port_enabled=%d: resetting channels\n",
1995 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001996
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001997 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001998}
1999
2000
2001/* Context: process, rtnl_lock() held. */
2002static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2003{
Ben Hutchings767e4682008-09-01 12:43:14 +01002004 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002005 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002006
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002007 rc = efx_check_disabled(efx);
2008 if (rc)
2009 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002010 if (new_mtu > EFX_MAX_MTU)
2011 return -EINVAL;
2012
Ben Hutchings62776d02010-06-23 11:30:07 +00002013 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002014
Ben Hutchings29c69a42013-01-28 19:01:06 +00002015 efx_device_detach_sync(efx);
2016 efx_stop_all(efx);
2017
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002018 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002019 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01002020 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002021 mutex_unlock(&efx->mac_lock);
2022
Ben Hutchings8ceee662008-04-27 12:55:59 +01002023 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00002024 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002025 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002026}
2027
2028static int efx_set_mac_address(struct net_device *net_dev, void *data)
2029{
Ben Hutchings767e4682008-09-01 12:43:14 +01002030 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002031 struct sockaddr *addr = data;
2032 char *new_addr = addr->sa_data;
2033
Ben Hutchings8ceee662008-04-27 12:55:59 +01002034 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002035 netif_err(efx, drv, efx->net_dev,
2036 "invalid ethernet MAC address requested: %pM\n",
2037 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002038 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002039 }
2040
2041 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002042 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002043
2044 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002045 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002046 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002047 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002048
2049 return 0;
2050}
2051
Ben Hutchingsa816f752008-09-01 12:49:12 +01002052/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002053static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002054{
Ben Hutchings767e4682008-09-01 12:43:14 +01002055 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002056
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002057 if (efx->port_enabled)
2058 queue_work(efx->workqueue, &efx->mac_work);
2059 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002060}
2061
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002062static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002063{
2064 struct efx_nic *efx = netdev_priv(net_dev);
2065
2066 /* If disabling RX n-tuple filtering, clear existing filters */
2067 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2068 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2069
2070 return 0;
2071}
2072
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002073static const struct net_device_ops efx_netdev_ops = {
2074 .ndo_open = efx_net_open,
2075 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002076 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002077 .ndo_tx_timeout = efx_watchdog,
2078 .ndo_start_xmit = efx_hard_start_xmit,
2079 .ndo_validate_addr = eth_validate_addr,
2080 .ndo_do_ioctl = efx_ioctl,
2081 .ndo_change_mtu = efx_change_mtu,
2082 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002083 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002084 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002085#ifdef CONFIG_SFC_SRIOV
2086 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2087 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2088 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2089 .ndo_get_vf_config = efx_sriov_get_vf_config,
2090#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002091#ifdef CONFIG_NET_POLL_CONTROLLER
2092 .ndo_poll_controller = efx_netpoll,
2093#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002094 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002095#ifdef CONFIG_RFS_ACCEL
2096 .ndo_rx_flow_steer = efx_filter_rfs,
2097#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002098};
2099
Ben Hutchings7dde5962008-12-12 22:09:38 -08002100static void efx_update_name(struct efx_nic *efx)
2101{
2102 strcpy(efx->name, efx->net_dev->name);
2103 efx_mtd_rename(efx);
2104 efx_set_channel_names(efx);
2105}
2106
Ben Hutchings8ceee662008-04-27 12:55:59 +01002107static int efx_netdev_event(struct notifier_block *this,
2108 unsigned long event, void *ptr)
2109{
Jiri Pirko351638e2013-05-28 01:30:21 +00002110 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002111
Ben Hutchings7dde5962008-12-12 22:09:38 -08002112 if (net_dev->netdev_ops == &efx_netdev_ops &&
2113 event == NETDEV_CHANGENAME)
2114 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002115
2116 return NOTIFY_DONE;
2117}
2118
2119static struct notifier_block efx_netdev_notifier = {
2120 .notifier_call = efx_netdev_event,
2121};
2122
Ben Hutchings06d5e192008-12-12 21:47:23 -08002123static ssize_t
2124show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2125{
2126 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2127 return sprintf(buf, "%d\n", efx->phy_type);
2128}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002129static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002130
Ben Hutchings8ceee662008-04-27 12:55:59 +01002131static int efx_register_netdev(struct efx_nic *efx)
2132{
2133 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002134 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002135 int rc;
2136
2137 net_dev->watchdog_timeo = 5 * HZ;
2138 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002139 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002140 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002141 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002142
Ben Hutchings7dde5962008-12-12 22:09:38 -08002143 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002144
Ben Hutchings7153f622012-07-27 20:50:52 +01002145 /* Enable resets to be scheduled and check whether any were
2146 * already requested. If so, the NIC is probably hosed so we
2147 * abort.
2148 */
2149 efx->state = STATE_READY;
2150 smp_mb(); /* ensure we change state before checking reset_pending */
2151 if (efx->reset_pending) {
2152 netif_err(efx, probe, efx->net_dev,
2153 "aborting probe due to scheduled reset\n");
2154 rc = -EIO;
2155 goto fail_locked;
2156 }
2157
Ben Hutchingsaed06282009-08-26 08:16:27 +00002158 rc = dev_alloc_name(net_dev, net_dev->name);
2159 if (rc < 0)
2160 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002161 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002162
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002163 /* Always start with carrier off; PHY events will detect the link */
2164 netif_carrier_off(net_dev);
2165
Ben Hutchingsaed06282009-08-26 08:16:27 +00002166 rc = register_netdevice(net_dev);
2167 if (rc)
2168 goto fail_locked;
2169
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002170 efx_for_each_channel(channel, efx) {
2171 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002172 efx_for_each_channel_tx_queue(tx_queue, channel)
2173 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002174 }
2175
Ben Hutchings7dde5962008-12-12 22:09:38 -08002176 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002177
Ben Hutchings06d5e192008-12-12 21:47:23 -08002178 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2179 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002180 netif_err(efx, drv, efx->net_dev,
2181 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002182 goto fail_registered;
2183 }
2184
Ben Hutchings8ceee662008-04-27 12:55:59 +01002185 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002186
Ben Hutchings7153f622012-07-27 20:50:52 +01002187fail_registered:
2188 rtnl_lock();
2189 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002190fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002191 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002192 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002193 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002194 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002195}
2196
2197static void efx_unregister_netdev(struct efx_nic *efx)
2198{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002199 if (!efx->net_dev)
2200 return;
2201
Ben Hutchings767e4682008-09-01 12:43:14 +01002202 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002203
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002204 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2205 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002206
2207 rtnl_lock();
2208 unregister_netdevice(efx->net_dev);
2209 efx->state = STATE_UNINIT;
2210 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002211}
2212
2213/**************************************************************************
2214 *
2215 * Device reset and suspend
2216 *
2217 **************************************************************************/
2218
Ben Hutchings2467ca42008-09-01 12:48:50 +01002219/* Tears down the entire software state and most of the hardware state
2220 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002221void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002222{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002223 EFX_ASSERT_RESET_SERIALISED(efx);
2224
Ben Hutchings2467ca42008-09-01 12:48:50 +01002225 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002226 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002227
2228 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002229 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2230 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002231 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002232}
2233
Ben Hutchings2467ca42008-09-01 12:48:50 +01002234/* This function will always ensure that the locks acquired in
2235 * efx_reset_down() are released. A failure return code indicates
2236 * that we were unable to reinitialise the hardware, and the
2237 * driver should be disabled. If ok is false, then the rx and tx
2238 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002239int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002240{
2241 int rc;
2242
Ben Hutchings2467ca42008-09-01 12:48:50 +01002243 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002244
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002245 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002246 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002247 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002248 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002249 }
2250
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002251 if (!ok)
2252 goto fail;
2253
Steve Hodgson4b988282009-01-29 17:50:51 +00002254 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002255 rc = efx->phy_op->init(efx);
2256 if (rc)
2257 goto fail;
2258 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002259 netif_err(efx, drv, efx->net_dev,
2260 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002261 }
2262
Jon Cooper261e4d92013-04-15 18:51:54 +01002263 rc = efx_enable_interrupts(efx);
2264 if (rc)
2265 goto fail;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002266 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002267 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002268
2269 mutex_unlock(&efx->mac_lock);
2270
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002271 efx_start_all(efx);
2272
2273 return 0;
2274
2275fail:
2276 efx->port_initialized = false;
2277
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002278 mutex_unlock(&efx->mac_lock);
2279
Ben Hutchings8ceee662008-04-27 12:55:59 +01002280 return rc;
2281}
2282
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002283/* Reset the NIC using the specified method. Note that the reset may
2284 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002285 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002286 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002287 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002288int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002289{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002290 int rc, rc2;
2291 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002292
Ben Hutchings62776d02010-06-23 11:30:07 +00002293 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2294 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002295
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002296 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002297 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002298
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002299 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002300 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002301 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002302 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002303 }
2304
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002305 /* Clear flags for the scopes we covered. We assume the NIC and
2306 * driver are now quiescent so that there is no race here.
2307 */
2308 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002309
2310 /* Reinitialise bus-mastering, which may have been turned off before
2311 * the reset was scheduled. This is still appropriate, even in the
2312 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2313 * can respond to requests. */
2314 pci_set_master(efx->pci_dev);
2315
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002316out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002317 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002318 disabled = rc ||
2319 method == RESET_TYPE_DISABLE ||
2320 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002321 rc2 = efx_reset_up(efx, method, !disabled);
2322 if (rc2) {
2323 disabled = true;
2324 if (!rc)
2325 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002326 }
2327
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002328 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002329 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002330 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002331 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002332 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002333 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002334 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002335 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002336 return rc;
2337}
2338
Alexandre Rames626950d2013-01-14 17:20:22 +00002339/* Try recovery mechanisms.
2340 * For now only EEH is supported.
2341 * Returns 0 if the recovery mechanisms are unsuccessful.
2342 * Returns a non-zero value otherwise.
2343 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002344int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002345{
2346#ifdef CONFIG_EEH
2347 /* A PCI error can occur and not be seen by EEH because nothing
2348 * happens on the PCI bus. In this case the driver may fail and
2349 * schedule a 'recover or reset', leading to this recovery handler.
2350 * Manually call the eeh failure check function.
2351 */
2352 struct eeh_dev *eehdev =
2353 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2354
2355 if (eeh_dev_check_failure(eehdev)) {
2356 /* The EEH mechanisms will handle the error and reset the
2357 * device if necessary.
2358 */
2359 return 1;
2360 }
2361#endif
2362 return 0;
2363}
2364
Ben Hutchings8ceee662008-04-27 12:55:59 +01002365/* The worker thread exists so that code that cannot sleep can
2366 * schedule a reset for later.
2367 */
2368static void efx_reset_work(struct work_struct *data)
2369{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002370 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002371 unsigned long pending;
2372 enum reset_type method;
2373
2374 pending = ACCESS_ONCE(efx->reset_pending);
2375 method = fls(pending) - 1;
2376
2377 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2378 method == RESET_TYPE_RECOVER_OR_ALL) &&
2379 efx_try_recovery(efx))
2380 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002381
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002382 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002383 return;
2384
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002385 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002386
2387 /* We checked the state in efx_schedule_reset() but it may
2388 * have changed by now. Now that we have the RTNL lock,
2389 * it cannot change again.
2390 */
2391 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002392 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002393
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002394 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002395}
2396
2397void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2398{
2399 enum reset_type method;
2400
Alexandre Rames626950d2013-01-14 17:20:22 +00002401 if (efx->state == STATE_RECOVERY) {
2402 netif_dbg(efx, drv, efx->net_dev,
2403 "recovering: skip scheduling %s reset\n",
2404 RESET_TYPE(type));
2405 return;
2406 }
2407
Ben Hutchings8ceee662008-04-27 12:55:59 +01002408 switch (type) {
2409 case RESET_TYPE_INVISIBLE:
2410 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002411 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002412 case RESET_TYPE_WORLD:
2413 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002414 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002415 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002416 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2417 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002418 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002419 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002420 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002421 netif_dbg(efx, drv, efx->net_dev,
2422 "scheduling %s reset for %s\n",
2423 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002424 break;
2425 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002426
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002427 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002428 smp_mb(); /* ensure we change reset_pending before checking state */
2429
2430 /* If we're not READY then just leave the flags set as the cue
2431 * to abort probing or reschedule the reset later.
2432 */
2433 if (ACCESS_ONCE(efx->state) != STATE_READY)
2434 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002435
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002436 /* efx_process_channel() will no longer read events once a
2437 * reset is scheduled. So switch back to poll'd MCDI completions. */
2438 efx_mcdi_mode_poll(efx);
2439
Steve Hodgson1ab00622008-12-12 21:33:02 -08002440 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002441}
2442
2443/**************************************************************************
2444 *
2445 * List of NICs we support
2446 *
2447 **************************************************************************/
2448
2449/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002450static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002451 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2452 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002453 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002454 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2455 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002456 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002457 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002458 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002459 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002460 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002461 {0} /* end of list */
2462};
2463
2464/**************************************************************************
2465 *
Ben Hutchings37594332009-11-23 16:05:45 +00002466 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002467 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002468 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002469 * Needed so all function pointers are valid and do not have to be tested
2470 * before use
2471 *
2472 **************************************************************************/
2473int efx_port_dummy_op_int(struct efx_nic *efx)
2474{
2475 return 0;
2476}
2477void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002478
2479static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002480{
2481 return false;
2482}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002483
stephen hemminger6c8c2512011-04-14 05:50:12 +00002484static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002485 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002486 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002487 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002488 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002489};
2490
Ben Hutchings8ceee662008-04-27 12:55:59 +01002491/**************************************************************************
2492 *
2493 * Data housekeeping
2494 *
2495 **************************************************************************/
2496
2497/* This zeroes out and then fills in the invariants in a struct
2498 * efx_nic (including all sub-structures).
2499 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002500static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002501 struct pci_dev *pci_dev, struct net_device *net_dev)
2502{
Ben Hutchings46426102010-09-10 06:42:33 +00002503 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002504
2505 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002506 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002507#ifdef CONFIG_SFC_MTD
2508 INIT_LIST_HEAD(&efx->mtd_list);
2509#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002510 INIT_WORK(&efx->reset_work, efx_reset_work);
2511 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002512 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002513 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002514 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002515 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002516 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002517
2518 efx->net_dev = net_dev;
Jon Cooper43a37392012-10-18 15:49:54 +01002519 efx->rx_prefix_size = efx->type->rx_prefix_size;
2520 efx->rx_packet_hash_offset =
2521 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002522 spin_lock_init(&efx->stats_lock);
2523 mutex_init(&efx->mac_lock);
2524 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002525 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002526 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002527 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002528
2529 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002530 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2531 if (!efx->channel[i])
2532 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002533 efx->msi_context[i].efx = efx;
2534 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002535 }
2536
Ben Hutchings8ceee662008-04-27 12:55:59 +01002537 /* Higher numbered interrupt modes are less capable! */
2538 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2539 interrupt_mode);
2540
Ben Hutchings6977dc62008-12-26 13:44:39 -08002541 /* Would be good to use the net_dev name, but we're too early */
2542 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2543 pci_name(pci_dev));
2544 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002545 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002546 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002547
Ben Hutchings8ceee662008-04-27 12:55:59 +01002548 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002549
2550fail:
2551 efx_fini_struct(efx);
2552 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002553}
2554
2555static void efx_fini_struct(struct efx_nic *efx)
2556{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002557 int i;
2558
2559 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2560 kfree(efx->channel[i]);
2561
Ben Hutchings8ceee662008-04-27 12:55:59 +01002562 if (efx->workqueue) {
2563 destroy_workqueue(efx->workqueue);
2564 efx->workqueue = NULL;
2565 }
2566}
2567
2568/**************************************************************************
2569 *
2570 * PCI interface
2571 *
2572 **************************************************************************/
2573
2574/* Main body of final NIC shutdown code
2575 * This is called only at module unload (or hotplug removal).
2576 */
2577static void efx_pci_remove_main(struct efx_nic *efx)
2578{
Ben Hutchings7153f622012-07-27 20:50:52 +01002579 /* Flush reset_work. It can no longer be scheduled since we
2580 * are not READY.
2581 */
2582 BUG_ON(efx->state == STATE_READY);
2583 cancel_work_sync(&efx->reset_work);
2584
Ben Hutchingsd8291182012-10-05 23:35:41 +01002585 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002586 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002587 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002588 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002589 efx_fini_napi(efx);
2590 efx_remove_all(efx);
2591}
2592
2593/* Final NIC shutdown
2594 * This is called only at module unload (or hotplug removal).
2595 */
2596static void efx_pci_remove(struct pci_dev *pci_dev)
2597{
2598 struct efx_nic *efx;
2599
2600 efx = pci_get_drvdata(pci_dev);
2601 if (!efx)
2602 return;
2603
2604 /* Mark the NIC as fini, then stop the interface */
2605 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002606 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002607 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002608 rtnl_unlock();
2609
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002610 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002611 efx_unregister_netdev(efx);
2612
Ben Hutchings7dde5962008-12-12 22:09:38 -08002613 efx_mtd_remove(efx);
2614
Ben Hutchings8ceee662008-04-27 12:55:59 +01002615 efx_pci_remove_main(efx);
2616
Ben Hutchings8ceee662008-04-27 12:55:59 +01002617 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002618 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002619
Ben Hutchings8ceee662008-04-27 12:55:59 +01002620 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002621 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002622 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002623
2624 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002625};
2626
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002627/* NIC VPD information
2628 * Called during probe to display the part number of the
2629 * installed NIC. VPD is potentially very large but this should
2630 * always appear within the first 512 bytes.
2631 */
2632#define SFC_VPD_LEN 512
2633static void efx_print_product_vpd(struct efx_nic *efx)
2634{
2635 struct pci_dev *dev = efx->pci_dev;
2636 char vpd_data[SFC_VPD_LEN];
2637 ssize_t vpd_size;
2638 int i, j;
2639
2640 /* Get the vpd data from the device */
2641 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2642 if (vpd_size <= 0) {
2643 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2644 return;
2645 }
2646
2647 /* Get the Read only section */
2648 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2649 if (i < 0) {
2650 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2651 return;
2652 }
2653
2654 j = pci_vpd_lrdt_size(&vpd_data[i]);
2655 i += PCI_VPD_LRDT_TAG_SIZE;
2656 if (i + j > vpd_size)
2657 j = vpd_size - i;
2658
2659 /* Get the Part number */
2660 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2661 if (i < 0) {
2662 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2663 return;
2664 }
2665
2666 j = pci_vpd_info_field_size(&vpd_data[i]);
2667 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2668 if (i + j > vpd_size) {
2669 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2670 return;
2671 }
2672
2673 netif_info(efx, drv, efx->net_dev,
2674 "Part Number : %.*s\n", j, &vpd_data[i]);
2675}
2676
2677
Ben Hutchings8ceee662008-04-27 12:55:59 +01002678/* Main body of NIC initialisation
2679 * This is called at module load (or hotplug insertion, theoretically).
2680 */
2681static int efx_pci_probe_main(struct efx_nic *efx)
2682{
2683 int rc;
2684
2685 /* Do start-of-day initialisation */
2686 rc = efx_probe_all(efx);
2687 if (rc)
2688 goto fail1;
2689
Ben Hutchingse8f14992010-12-07 19:47:34 +00002690 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002691
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002692 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002693 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002694 netif_err(efx, probe, efx->net_dev,
2695 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002696 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002697 }
2698
2699 rc = efx_init_port(efx);
2700 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002701 netif_err(efx, probe, efx->net_dev,
2702 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002703 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002704 }
2705
Ben Hutchings152b6a62009-11-29 03:43:56 +00002706 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002707 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002708 goto fail5;
Jon Cooper261e4d92013-04-15 18:51:54 +01002709 rc = efx_enable_interrupts(efx);
2710 if (rc)
2711 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002712
2713 return 0;
2714
Jon Cooper261e4d92013-04-15 18:51:54 +01002715 fail6:
2716 efx_nic_fini_interrupt(efx);
Ben Hutchings278c0622009-11-23 16:05:12 +00002717 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002718 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002719 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002720 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002721 fail3:
2722 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002723 efx_remove_all(efx);
2724 fail1:
2725 return rc;
2726}
2727
2728/* NIC initialisation
2729 *
2730 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002731 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002732 * sets up and registers the network devices with the kernel and hooks
2733 * the interrupt service routine. It does not prepare the device for
2734 * transmission; this is left to the first time one of the network
2735 * interfaces is brought up (i.e. efx_net_open).
2736 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002737static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002738 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002739{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002740 struct net_device *net_dev;
2741 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002742 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002743
2744 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002745 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2746 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002747 if (!net_dev)
2748 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002749 efx = netdev_priv(net_dev);
2750 efx->type = (const struct efx_nic_type *) entry->driver_data;
2751 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002752 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002753 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002754 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002755 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002756 /* Mask for features that also apply to VLAN devices */
2757 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002758 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2759 NETIF_F_RXCSUM);
2760 /* All offloads can be toggled */
2761 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002762 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002763 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002764 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002765 if (rc)
2766 goto fail1;
2767
Ben Hutchings62776d02010-06-23 11:30:07 +00002768 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002769 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002770
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002771 efx_print_product_vpd(efx);
2772
Ben Hutchings8ceee662008-04-27 12:55:59 +01002773 /* Set up basic I/O (BAR mappings etc) */
2774 rc = efx_init_io(efx);
2775 if (rc)
2776 goto fail2;
2777
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002778 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002779 if (rc)
2780 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002781
Ben Hutchings8ceee662008-04-27 12:55:59 +01002782 rc = efx_register_netdev(efx);
2783 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002784 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002785
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002786 rc = efx_sriov_init(efx);
2787 if (rc)
2788 netif_err(efx, probe, efx->net_dev,
2789 "SR-IOV can't be enabled rc %d\n", rc);
2790
Ben Hutchings62776d02010-06-23 11:30:07 +00002791 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002792
Ben Hutchings7c431612012-01-27 17:23:58 +00002793 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002794 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002795 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002796 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002797 if (rc)
2798 netif_warn(efx, probe, efx->net_dev,
2799 "failed to create MTDs (%d)\n", rc);
2800
Alexandre Rames626950d2013-01-14 17:20:22 +00002801 rc = pci_enable_pcie_error_reporting(pci_dev);
2802 if (rc && rc != -EINVAL)
2803 netif_warn(efx, probe, efx->net_dev,
2804 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2805
Ben Hutchings8ceee662008-04-27 12:55:59 +01002806 return 0;
2807
Ben Hutchings8ceee662008-04-27 12:55:59 +01002808 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002809 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002810 fail3:
2811 efx_fini_io(efx);
2812 fail2:
2813 efx_fini_struct(efx);
2814 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002815 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002816 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002817 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002818 free_netdev(net_dev);
2819 return rc;
2820}
2821
Ben Hutchings89c758f2009-11-29 03:43:07 +00002822static int efx_pm_freeze(struct device *dev)
2823{
2824 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2825
Ben Hutchings61da0262012-07-27 19:35:39 +01002826 rtnl_lock();
2827
Ben Hutchings6032fb52012-07-27 19:35:47 +01002828 if (efx->state != STATE_DISABLED) {
2829 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002830
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002831 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002832
Ben Hutchings6032fb52012-07-27 19:35:47 +01002833 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002834 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01002835 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002836
Ben Hutchings61da0262012-07-27 19:35:39 +01002837 rtnl_unlock();
2838
Ben Hutchings89c758f2009-11-29 03:43:07 +00002839 return 0;
2840}
2841
2842static int efx_pm_thaw(struct device *dev)
2843{
Jon Cooper261e4d92013-04-15 18:51:54 +01002844 int rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002845 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2846
Ben Hutchings61da0262012-07-27 19:35:39 +01002847 rtnl_lock();
2848
Ben Hutchings6032fb52012-07-27 19:35:47 +01002849 if (efx->state != STATE_DISABLED) {
Jon Cooper261e4d92013-04-15 18:51:54 +01002850 rc = efx_enable_interrupts(efx);
2851 if (rc)
2852 goto fail;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002853
Ben Hutchings6032fb52012-07-27 19:35:47 +01002854 mutex_lock(&efx->mac_lock);
2855 efx->phy_op->reconfigure(efx);
2856 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002857
Ben Hutchings6032fb52012-07-27 19:35:47 +01002858 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002859
Ben Hutchings6032fb52012-07-27 19:35:47 +01002860 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002861
Ben Hutchings6032fb52012-07-27 19:35:47 +01002862 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002863
Ben Hutchings6032fb52012-07-27 19:35:47 +01002864 efx->type->resume_wol(efx);
2865 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002866
Ben Hutchings61da0262012-07-27 19:35:39 +01002867 rtnl_unlock();
2868
Steve Hodgson319ba642010-06-01 11:17:24 +00002869 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2870 queue_work(reset_workqueue, &efx->reset_work);
2871
Ben Hutchings89c758f2009-11-29 03:43:07 +00002872 return 0;
Jon Cooper261e4d92013-04-15 18:51:54 +01002873
2874fail:
2875 rtnl_unlock();
2876
2877 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002878}
2879
2880static int efx_pm_poweroff(struct device *dev)
2881{
2882 struct pci_dev *pci_dev = to_pci_dev(dev);
2883 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2884
2885 efx->type->fini(efx);
2886
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002887 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002888
2889 pci_save_state(pci_dev);
2890 return pci_set_power_state(pci_dev, PCI_D3hot);
2891}
2892
2893/* Used for both resume and restore */
2894static int efx_pm_resume(struct device *dev)
2895{
2896 struct pci_dev *pci_dev = to_pci_dev(dev);
2897 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2898 int rc;
2899
2900 rc = pci_set_power_state(pci_dev, PCI_D0);
2901 if (rc)
2902 return rc;
2903 pci_restore_state(pci_dev);
2904 rc = pci_enable_device(pci_dev);
2905 if (rc)
2906 return rc;
2907 pci_set_master(efx->pci_dev);
2908 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2909 if (rc)
2910 return rc;
2911 rc = efx->type->init(efx);
2912 if (rc)
2913 return rc;
Jon Cooper261e4d92013-04-15 18:51:54 +01002914 rc = efx_pm_thaw(dev);
2915 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002916}
2917
2918static int efx_pm_suspend(struct device *dev)
2919{
2920 int rc;
2921
2922 efx_pm_freeze(dev);
2923 rc = efx_pm_poweroff(dev);
2924 if (rc)
2925 efx_pm_resume(dev);
2926 return rc;
2927}
2928
Ben Hutchings18e83e42012-01-05 19:05:20 +00002929static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002930 .suspend = efx_pm_suspend,
2931 .resume = efx_pm_resume,
2932 .freeze = efx_pm_freeze,
2933 .thaw = efx_pm_thaw,
2934 .poweroff = efx_pm_poweroff,
2935 .restore = efx_pm_resume,
2936};
2937
Alexandre Rames626950d2013-01-14 17:20:22 +00002938/* A PCI error affecting this device was detected.
2939 * At this point MMIO and DMA may be disabled.
2940 * Stop the software path and request a slot reset.
2941 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002942static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2943 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00002944{
2945 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2946 struct efx_nic *efx = pci_get_drvdata(pdev);
2947
2948 if (state == pci_channel_io_perm_failure)
2949 return PCI_ERS_RESULT_DISCONNECT;
2950
2951 rtnl_lock();
2952
2953 if (efx->state != STATE_DISABLED) {
2954 efx->state = STATE_RECOVERY;
2955 efx->reset_pending = 0;
2956
2957 efx_device_detach_sync(efx);
2958
2959 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002960 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00002961
2962 status = PCI_ERS_RESULT_NEED_RESET;
2963 } else {
2964 /* If the interface is disabled we don't want to do anything
2965 * with it.
2966 */
2967 status = PCI_ERS_RESULT_RECOVERED;
2968 }
2969
2970 rtnl_unlock();
2971
2972 pci_disable_device(pdev);
2973
2974 return status;
2975}
2976
2977/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002978static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00002979{
2980 struct efx_nic *efx = pci_get_drvdata(pdev);
2981 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2982 int rc;
2983
2984 if (pci_enable_device(pdev)) {
2985 netif_err(efx, hw, efx->net_dev,
2986 "Cannot re-enable PCI device after reset.\n");
2987 status = PCI_ERS_RESULT_DISCONNECT;
2988 }
2989
2990 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2991 if (rc) {
2992 netif_err(efx, hw, efx->net_dev,
2993 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2994 /* Non-fatal error. Continue. */
2995 }
2996
2997 return status;
2998}
2999
3000/* Perform the actual reset and resume I/O operations. */
3001static void efx_io_resume(struct pci_dev *pdev)
3002{
3003 struct efx_nic *efx = pci_get_drvdata(pdev);
3004 int rc;
3005
3006 rtnl_lock();
3007
3008 if (efx->state == STATE_DISABLED)
3009 goto out;
3010
3011 rc = efx_reset(efx, RESET_TYPE_ALL);
3012 if (rc) {
3013 netif_err(efx, hw, efx->net_dev,
3014 "efx_reset failed after PCI error (%d)\n", rc);
3015 } else {
3016 efx->state = STATE_READY;
3017 netif_dbg(efx, hw, efx->net_dev,
3018 "Done resetting and resuming IO after PCI error.\n");
3019 }
3020
3021out:
3022 rtnl_unlock();
3023}
3024
3025/* For simplicity and reliability, we always require a slot reset and try to
3026 * reset the hardware when a pci error affecting the device is detected.
3027 * We leave both the link_reset and mmio_enabled callback unimplemented:
3028 * with our request for slot reset the mmio_enabled callback will never be
3029 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3030 */
3031static struct pci_error_handlers efx_err_handlers = {
3032 .error_detected = efx_io_error_detected,
3033 .slot_reset = efx_io_slot_reset,
3034 .resume = efx_io_resume,
3035};
3036
Ben Hutchings8ceee662008-04-27 12:55:59 +01003037static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003038 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003039 .id_table = efx_pci_table,
3040 .probe = efx_pci_probe,
3041 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003042 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003043 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003044};
3045
3046/**************************************************************************
3047 *
3048 * Kernel module interface
3049 *
3050 *************************************************************************/
3051
3052module_param(interrupt_mode, uint, 0444);
3053MODULE_PARM_DESC(interrupt_mode,
3054 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3055
3056static int __init efx_init_module(void)
3057{
3058 int rc;
3059
3060 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3061
3062 rc = register_netdevice_notifier(&efx_netdev_notifier);
3063 if (rc)
3064 goto err_notifier;
3065
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003066 rc = efx_init_sriov();
3067 if (rc)
3068 goto err_sriov;
3069
Steve Hodgson1ab00622008-12-12 21:33:02 -08003070 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3071 if (!reset_workqueue) {
3072 rc = -ENOMEM;
3073 goto err_reset;
3074 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003075
3076 rc = pci_register_driver(&efx_pci_driver);
3077 if (rc < 0)
3078 goto err_pci;
3079
3080 return 0;
3081
3082 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003083 destroy_workqueue(reset_workqueue);
3084 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003085 efx_fini_sriov();
3086 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003087 unregister_netdevice_notifier(&efx_netdev_notifier);
3088 err_notifier:
3089 return rc;
3090}
3091
3092static void __exit efx_exit_module(void)
3093{
3094 printk(KERN_INFO "Solarflare NET driver unloading\n");
3095
3096 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003097 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003098 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003099 unregister_netdevice_notifier(&efx_netdev_notifier);
3100
3101}
3102
3103module_init(efx_init_module);
3104module_exit(efx_exit_module);
3105
Ben Hutchings906bb262009-11-29 15:16:19 +00003106MODULE_AUTHOR("Solarflare Communications and "
3107 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003108MODULE_DESCRIPTION("Solarflare Communications network driver");
3109MODULE_LICENSE("GPL");
3110MODULE_DEVICE_TABLE(pci, efx_pci_table);