blob: ee9242cf5fd777be463e93d005da0dfd7f414c28 [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>
20#include <linux/crc32.h>
21#include <linux/ethtool.h>
Ben Hutchingsaa6ef272008-07-18 19:03:10 +010022#include <linux/topology.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/gfp.h>
Alexandre Rames626950d2013-01-14 17:20:22 +000024#include <linux/aer.h>
Alexandre Ramesb28405b2013-03-21 16:41:43 +000025#include <linux/interrupt.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010027#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000028#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000029#include "selftest.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010030
Ben Hutchings8880f4e2009-11-29 15:15:41 +000031#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000032#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000033
Ben Hutchingsc4593022009-11-23 16:08:17 +000034/**************************************************************************
35 *
36 * Type name strings
37 *
38 **************************************************************************
39 */
40
41/* Loopback mode names (see LOOPBACK_MODE()) */
42const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000043const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000044 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000045 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000046 [LOOPBACK_GMAC] = "GMAC",
47 [LOOPBACK_XGMII] = "XGMII",
48 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000049 [LOOPBACK_XAUI] = "XAUI",
50 [LOOPBACK_GMII] = "GMII",
51 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000052 [LOOPBACK_XGBR] = "XGBR",
53 [LOOPBACK_XFI] = "XFI",
54 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
55 [LOOPBACK_GMII_FAR] = "GMII_FAR",
56 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
57 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000058 [LOOPBACK_GPHY] = "GPHY",
59 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000060 [LOOPBACK_PCS] = "PCS",
61 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000062 [LOOPBACK_XPORT] = "XPORT",
63 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000064 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000065 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
66 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000067 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000068 [LOOPBACK_XFI_WS] = "XFI_WS",
69 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000070 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000071};
72
Ben Hutchingsc4593022009-11-23 16:08:17 +000073const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000074const char *const efx_reset_type_names[] = {
Alexandre Rames626950d2013-01-14 17:20:22 +000075 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
76 [RESET_TYPE_ALL] = "ALL",
77 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
78 [RESET_TYPE_WORLD] = "WORLD",
79 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
80 [RESET_TYPE_DISABLE] = "DISABLE",
81 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
82 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
83 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
84 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
85 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
86 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
87 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Ben Hutchingsc4593022009-11-23 16:08:17 +000088};
89
Steve Hodgson1ab00622008-12-12 21:33:02 -080090/* Reset workqueue. If any NIC has a hardware failure then a reset will be
91 * queued onto this work queue. This is not a per-nic work queue, because
92 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
93 */
94static struct workqueue_struct *reset_workqueue;
95
Ben Hutchings8ceee662008-04-27 12:55:59 +010096/**************************************************************************
97 *
98 * Configurable values
99 *
100 *************************************************************************/
101
102/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100103 * Use separate channels for TX and RX events
104 *
Neil Turton28b581a2008-12-12 21:41:06 -0800105 * Set this to 1 to use separate channels for TX and RX. It allows us
106 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100107 *
Neil Turton28b581a2008-12-12 21:41:06 -0800108 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100109 */
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000110static bool separate_tx_channels;
111module_param(separate_tx_channels, bool, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800112MODULE_PARM_DESC(separate_tx_channels,
113 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100114
115/* This is the weight assigned to each of the (per-channel) virtual
116 * NAPI devices.
117 */
118static int napi_weight = 64;
119
120/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000121 * monitor.
122 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000123 * - Check the on-board hardware monitor;
124 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000125 * On Siena-based NICs for power systems with EEH support, this will give EEH a
126 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100127 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000128static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100129
Ben Hutchings8ceee662008-04-27 12:55:59 +0100130/* Initial interrupt moderation settings. They can be modified after
131 * module load with ethtool.
132 *
133 * The default for RX should strike a balance between increasing the
134 * round-trip latency and reducing overhead.
135 */
136static unsigned int rx_irq_mod_usec = 60;
137
138/* Initial interrupt moderation settings. They can be modified after
139 * module load with ethtool.
140 *
141 * This default is chosen to ensure that a 10G link does not go idle
142 * while a TX queue is stopped after it has become full. A queue is
143 * restarted when it drops below half full. The time this takes (assuming
144 * worst case 3 descriptors per packet and 1024 descriptors) is
145 * 512 / 3 * 1.2 = 205 usec.
146 */
147static unsigned int tx_irq_mod_usec = 150;
148
149/* This is the first interrupt mode to try out of:
150 * 0 => MSI-X
151 * 1 => MSI
152 * 2 => legacy
153 */
154static unsigned int interrupt_mode;
155
156/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
157 * i.e. the number of CPUs among which we may distribute simultaneous
158 * interrupt handling.
159 *
160 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000161 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100162 */
163static unsigned int rss_cpus;
164module_param(rss_cpus, uint, 0444);
165MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
166
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000167static bool phy_flash_cfg;
168module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800169MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
170
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000171static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000172module_param(irq_adapt_low_thresh, uint, 0644);
173MODULE_PARM_DESC(irq_adapt_low_thresh,
174 "Threshold score for reducing IRQ moderation");
175
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000176static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000177module_param(irq_adapt_high_thresh, uint, 0644);
178MODULE_PARM_DESC(irq_adapt_high_thresh,
179 "Threshold score for increasing IRQ moderation");
180
Ben Hutchings62776d02010-06-23 11:30:07 +0000181static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
182 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
183 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
184 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
185module_param(debug, uint, 0);
186MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
187
Ben Hutchings8ceee662008-04-27 12:55:59 +0100188/**************************************************************************
189 *
190 * Utility functions and prototypes
191 *
192 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000193
Ben Hutchingsd8291182012-10-05 23:35:41 +0100194static void efx_soft_enable_interrupts(struct efx_nic *efx);
195static void efx_soft_disable_interrupts(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000196static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000197static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000198static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100199static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000200static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100201static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000202static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000203static void efx_fini_struct(struct efx_nic *efx);
204static void efx_start_all(struct efx_nic *efx);
205static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100206
207#define EFX_ASSERT_RESET_SERIALISED(efx) \
208 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100209 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000210 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000211 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100212 ASSERT_RTNL(); \
213 } while (0)
214
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100215static int efx_check_disabled(struct efx_nic *efx)
216{
Alexandre Rames626950d2013-01-14 17:20:22 +0000217 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100218 netif_err(efx, drv, efx->net_dev,
219 "device is disabled due to earlier errors\n");
220 return -EIO;
221 }
222 return 0;
223}
224
Ben Hutchings8ceee662008-04-27 12:55:59 +0100225/**************************************************************************
226 *
227 * Event queue processing
228 *
229 *************************************************************************/
230
231/* Process channel's event queue
232 *
233 * This function is responsible for processing the event queue of a
234 * single channel. The caller must guarantee that this function will
235 * never be concurrently called more than once on the same channel,
236 * though different channels may be being processed concurrently.
237 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000238static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100239{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000240 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100241
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000242 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100243 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100244
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000245 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000246 if (spent && efx_channel_has_rx_queue(channel)) {
247 struct efx_rx_queue *rx_queue =
248 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100249
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000250 efx_rx_flush_packet(channel);
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100251 efx_fast_push_rx_descriptors(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100252 }
253
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000254 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100255}
256
Ben Hutchings8ceee662008-04-27 12:55:59 +0100257/* NAPI poll handler
258 *
259 * NAPI guarantees serialisation of polls of the same device, which
260 * provides the guarantee required by efx_process_channel().
261 */
262static int efx_poll(struct napi_struct *napi, int budget)
263{
264 struct efx_channel *channel =
265 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000266 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000267 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100268
Ben Hutchings62776d02010-06-23 11:30:07 +0000269 netif_vdbg(efx, intr, efx->net_dev,
270 "channel %d NAPI poll executing on CPU %d\n",
271 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100272
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000273 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100274
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000275 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000276 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000277 efx->irq_rx_adaptive &&
278 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000279 if (unlikely(channel->irq_mod_score <
280 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000281 if (channel->irq_moderation > 1) {
282 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000283 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000284 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000285 } else if (unlikely(channel->irq_mod_score >
286 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000287 if (channel->irq_moderation <
288 efx->irq_rx_moderation) {
289 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000290 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000291 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000292 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000293 channel->irq_count = 0;
294 channel->irq_mod_score = 0;
295 }
296
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000297 efx_filter_rfs_expire(channel);
298
Ben Hutchings8ceee662008-04-27 12:55:59 +0100299 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800300 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100301 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100302 * interrupts have already been disabled.
303 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800304 napi_complete(napi);
Ben Hutchings514bedb2012-10-05 19:30:16 +0100305 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100306 }
307
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000308 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100309}
310
Ben Hutchings8ceee662008-04-27 12:55:59 +0100311/* Create event queue
312 * Event queue memory allocations are done only once. If the channel
313 * is reset, the memory buffer will be reused; this guards against
314 * errors during channel reset and also simplifies interrupt handling.
315 */
316static int efx_probe_eventq(struct efx_channel *channel)
317{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000318 struct efx_nic *efx = channel->efx;
319 unsigned long entries;
320
Ben Hutchings86ee5302012-01-09 19:51:22 +0000321 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000322 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100323
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000324 /* Build an event queue with room for one event per tx and rx buffer,
325 * plus some extra for link state events and MCDI completions. */
326 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
327 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
328 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
329
Ben Hutchings152b6a62009-11-29 03:43:56 +0000330 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100331}
332
333/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100334static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100335{
Ben Hutchings62776d02010-06-23 11:30:07 +0000336 netif_dbg(channel->efx, drv, channel->efx->net_dev,
337 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100338
339 channel->eventq_read_ptr = 0;
340
Ben Hutchings152b6a62009-11-29 03:43:56 +0000341 efx_nic_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100342}
343
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000344/* Enable event queue processing and NAPI */
345static void efx_start_eventq(struct efx_channel *channel)
346{
347 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
348 "chan %d start event queue\n", channel->channel);
349
Ben Hutchings514bedb2012-10-05 19:30:16 +0100350 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000351 channel->enabled = true;
352 smp_wmb();
353
354 napi_enable(&channel->napi_str);
355 efx_nic_eventq_read_ack(channel);
356}
357
358/* Disable event queue processing and NAPI */
359static void efx_stop_eventq(struct efx_channel *channel)
360{
361 if (!channel->enabled)
362 return;
363
364 napi_disable(&channel->napi_str);
365 channel->enabled = false;
366}
367
Ben Hutchings8ceee662008-04-27 12:55:59 +0100368static void efx_fini_eventq(struct efx_channel *channel)
369{
Ben Hutchings62776d02010-06-23 11:30:07 +0000370 netif_dbg(channel->efx, drv, channel->efx->net_dev,
371 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100372
Ben Hutchings152b6a62009-11-29 03:43:56 +0000373 efx_nic_fini_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100374}
375
376static void efx_remove_eventq(struct efx_channel *channel)
377{
Ben Hutchings62776d02010-06-23 11:30:07 +0000378 netif_dbg(channel->efx, drv, channel->efx->net_dev,
379 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100380
Ben Hutchings152b6a62009-11-29 03:43:56 +0000381 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100382}
383
384/**************************************************************************
385 *
386 * Channel handling
387 *
388 *************************************************************************/
389
Ben Hutchings7f967c02012-02-13 23:45:02 +0000390/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000391static struct efx_channel *
392efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
393{
394 struct efx_channel *channel;
395 struct efx_rx_queue *rx_queue;
396 struct efx_tx_queue *tx_queue;
397 int j;
398
Ben Hutchings7f967c02012-02-13 23:45:02 +0000399 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
400 if (!channel)
401 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000402
Ben Hutchings7f967c02012-02-13 23:45:02 +0000403 channel->efx = efx;
404 channel->channel = i;
405 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000406
Ben Hutchings7f967c02012-02-13 23:45:02 +0000407 for (j = 0; j < EFX_TXQ_TYPES; j++) {
408 tx_queue = &channel->tx_queue[j];
409 tx_queue->efx = efx;
410 tx_queue->queue = i * EFX_TXQ_TYPES + j;
411 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000412 }
413
Ben Hutchings46426102010-09-10 06:42:33 +0000414 rx_queue = &channel->rx_queue;
415 rx_queue->efx = efx;
416 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
417 (unsigned long)rx_queue);
418
419 return channel;
420}
421
Ben Hutchings7f967c02012-02-13 23:45:02 +0000422/* Allocate and initialise a channel structure, copying parameters
423 * (but not resources) from an old channel structure.
424 */
425static struct efx_channel *
426efx_copy_channel(const struct efx_channel *old_channel)
427{
428 struct efx_channel *channel;
429 struct efx_rx_queue *rx_queue;
430 struct efx_tx_queue *tx_queue;
431 int j;
432
433 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
434 if (!channel)
435 return NULL;
436
437 *channel = *old_channel;
438
439 channel->napi_dev = NULL;
440 memset(&channel->eventq, 0, sizeof(channel->eventq));
441
442 for (j = 0; j < EFX_TXQ_TYPES; j++) {
443 tx_queue = &channel->tx_queue[j];
444 if (tx_queue->channel)
445 tx_queue->channel = channel;
446 tx_queue->buffer = NULL;
447 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
448 }
449
450 rx_queue = &channel->rx_queue;
451 rx_queue->buffer = NULL;
452 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
453 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
454 (unsigned long)rx_queue);
455
456 return channel;
457}
458
Ben Hutchings8ceee662008-04-27 12:55:59 +0100459static int efx_probe_channel(struct efx_channel *channel)
460{
461 struct efx_tx_queue *tx_queue;
462 struct efx_rx_queue *rx_queue;
463 int rc;
464
Ben Hutchings62776d02010-06-23 11:30:07 +0000465 netif_dbg(channel->efx, probe, channel->efx->net_dev,
466 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100467
Ben Hutchings7f967c02012-02-13 23:45:02 +0000468 rc = channel->type->pre_probe(channel);
469 if (rc)
470 goto fail;
471
Ben Hutchings8ceee662008-04-27 12:55:59 +0100472 rc = efx_probe_eventq(channel);
473 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000474 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100475
476 efx_for_each_channel_tx_queue(tx_queue, channel) {
477 rc = efx_probe_tx_queue(tx_queue);
478 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000479 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100480 }
481
482 efx_for_each_channel_rx_queue(rx_queue, channel) {
483 rc = efx_probe_rx_queue(rx_queue);
484 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000485 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100486 }
487
488 channel->n_rx_frm_trunc = 0;
489
490 return 0;
491
Ben Hutchings7f967c02012-02-13 23:45:02 +0000492fail:
493 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100494 return rc;
495}
496
Ben Hutchings7f967c02012-02-13 23:45:02 +0000497static void
498efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
499{
500 struct efx_nic *efx = channel->efx;
501 const char *type;
502 int number;
503
504 number = channel->channel;
505 if (efx->tx_channel_offset == 0) {
506 type = "";
507 } else if (channel->channel < efx->tx_channel_offset) {
508 type = "-rx";
509 } else {
510 type = "-tx";
511 number -= efx->tx_channel_offset;
512 }
513 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
514}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100515
Ben Hutchings56536e92008-12-12 21:37:02 -0800516static void efx_set_channel_names(struct efx_nic *efx)
517{
518 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800519
Ben Hutchings7f967c02012-02-13 23:45:02 +0000520 efx_for_each_channel(channel, efx)
521 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100522 efx->msi_context[channel->channel].name,
523 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800524}
525
Ben Hutchings46426102010-09-10 06:42:33 +0000526static int efx_probe_channels(struct efx_nic *efx)
527{
528 struct efx_channel *channel;
529 int rc;
530
531 /* Restart special buffer allocation */
532 efx->next_buffer_table = 0;
533
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000534 /* Probe channels in reverse, so that any 'extra' channels
535 * use the start of the buffer table. This allows the traffic
536 * channels to be resized without moving them or wasting the
537 * entries before them.
538 */
539 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000540 rc = efx_probe_channel(channel);
541 if (rc) {
542 netif_err(efx, probe, efx->net_dev,
543 "failed to create channel %d\n",
544 channel->channel);
545 goto fail;
546 }
547 }
548 efx_set_channel_names(efx);
549
550 return 0;
551
552fail:
553 efx_remove_channels(efx);
554 return rc;
555}
556
Ben Hutchings8ceee662008-04-27 12:55:59 +0100557/* Channels are shutdown and reinitialised whilst the NIC is running
558 * to propagate configuration changes (mtu, checksum offload), or
559 * to clear hardware error conditions
560 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000561static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100562{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000563 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100564 struct efx_tx_queue *tx_queue;
565 struct efx_rx_queue *rx_queue;
566 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000567 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100568
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100569 /* Calculate the rx buffer allocation parameters required to
570 * support the current MTU, including padding for header
571 * alignment and overruns.
572 */
Ben Hutchings272baee2013-01-29 23:33:14 +0000573 efx->rx_dma_len = (efx->type->rx_buffer_hash_size +
574 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
575 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000576 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Ben Hutchingsc14ff2e2013-05-13 11:58:31 +0000577 NET_IP_ALIGN + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000578 if (rx_buf_len <= PAGE_SIZE) {
579 efx->rx_scatter = false;
580 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000581 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000582 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000583 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000584 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
585 EFX_RX_BUF_ALIGNMENT) >
586 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000587 efx->rx_scatter = true;
588 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
589 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000590 } else {
591 efx->rx_scatter = false;
592 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000593 }
594
Daniel Pieczko1648a232013-02-13 10:54:41 +0000595 efx_rx_config_page_split(efx);
596 if (efx->rx_buffer_order)
597 netif_dbg(efx, drv, efx->net_dev,
598 "RX buf len=%u; page order=%u batch=%u\n",
599 efx->rx_dma_len, efx->rx_buffer_order,
600 efx->rx_pages_per_batch);
601 else
602 netif_dbg(efx, drv, efx->net_dev,
603 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
604 efx->rx_dma_len, efx->rx_page_buf_step,
605 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000606
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000607 /* RX filters also have scatter-enabled flags */
608 if (efx->rx_scatter != old_rx_scatter)
609 efx_filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100610
Ben Hutchings14bf7182012-05-22 01:27:58 +0100611 /* We must keep at least one descriptor in a TX ring empty.
612 * We could avoid this when the queue size does not exactly
613 * match the hardware ring size, but it's not that important.
614 * Therefore we stop the queue when one more skb might fill
615 * the ring completely. We wake it when half way back to
616 * empty.
617 */
618 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
619 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
620
Ben Hutchings8ceee662008-04-27 12:55:59 +0100621 /* Initialise the channels */
622 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100623 efx_for_each_channel_tx_queue(tx_queue, channel)
624 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100625
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000626 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100627 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000628 efx_nic_generate_fill_event(rx_queue);
629 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100630
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000631 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100632 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000633
634 if (netif_device_present(efx->net_dev))
635 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100636}
637
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000638static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100639{
640 struct efx_channel *channel;
641 struct efx_tx_queue *tx_queue;
642 struct efx_rx_queue *rx_queue;
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100643 struct pci_dev *dev = efx->pci_dev;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100644 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100645
646 EFX_ASSERT_RESET_SERIALISED(efx);
647 BUG_ON(efx->port_enabled);
648
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100649 /* Stop RX refill */
650 efx_for_each_channel(channel, efx) {
651 efx_for_each_channel_rx_queue(rx_queue, channel)
652 rx_queue->refill_enabled = false;
653 }
654
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100655 /* Only perform flush if dma is enabled */
Alexandre Rames626950d2013-01-14 17:20:22 +0000656 if (dev->is_busmaster && efx->state != STATE_RECOVERY) {
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100657 rc = efx_nic_flush_queues(efx);
658
659 if (rc && EFX_WORKAROUND_7803(efx)) {
660 /* Schedule a reset to recover from the flush failure. The
661 * descriptor caches reference memory we're about to free,
662 * but falcon_reconfigure_mac_wrapper() won't reconnect
663 * the MACs because of the pending reset. */
664 netif_err(efx, drv, efx->net_dev,
665 "Resetting to recover from flush failure\n");
666 efx_schedule_reset(efx, RESET_TYPE_ALL);
667 } else if (rc) {
668 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
669 } else {
670 netif_dbg(efx, drv, efx->net_dev,
671 "successfully flushed all queues\n");
672 }
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000673 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100674
Ben Hutchings8ceee662008-04-27 12:55:59 +0100675 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000676 /* RX packet processing is pipelined, so wait for the
677 * NAPI handler to complete. At least event queue 0
678 * might be kept active by non-data events, so don't
679 * use napi_synchronize() but actually disable NAPI
680 * temporarily.
681 */
682 if (efx_channel_has_rx_queue(channel)) {
683 efx_stop_eventq(channel);
684 efx_start_eventq(channel);
685 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100686
687 efx_for_each_channel_rx_queue(rx_queue, channel)
688 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000689 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100690 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100691 }
692}
693
694static void efx_remove_channel(struct efx_channel *channel)
695{
696 struct efx_tx_queue *tx_queue;
697 struct efx_rx_queue *rx_queue;
698
Ben Hutchings62776d02010-06-23 11:30:07 +0000699 netif_dbg(channel->efx, drv, channel->efx->net_dev,
700 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100701
702 efx_for_each_channel_rx_queue(rx_queue, channel)
703 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000704 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100705 efx_remove_tx_queue(tx_queue);
706 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100707 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100708}
709
Ben Hutchings46426102010-09-10 06:42:33 +0000710static void efx_remove_channels(struct efx_nic *efx)
711{
712 struct efx_channel *channel;
713
714 efx_for_each_channel(channel, efx)
715 efx_remove_channel(channel);
716}
717
718int
719efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
720{
721 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
722 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000723 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100724 int rc;
725
726 rc = efx_check_disabled(efx);
727 if (rc)
728 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000729
730 /* Not all channels should be reallocated. We must avoid
731 * reallocating their buffer table entries.
732 */
733 efx_for_each_channel(channel, efx) {
734 struct efx_rx_queue *rx_queue;
735 struct efx_tx_queue *tx_queue;
736
737 if (channel->type->copy)
738 continue;
739 next_buffer_table = max(next_buffer_table,
740 channel->eventq.index +
741 channel->eventq.entries);
742 efx_for_each_channel_rx_queue(rx_queue, channel)
743 next_buffer_table = max(next_buffer_table,
744 rx_queue->rxd.index +
745 rx_queue->rxd.entries);
746 efx_for_each_channel_tx_queue(tx_queue, channel)
747 next_buffer_table = max(next_buffer_table,
748 tx_queue->txd.index +
749 tx_queue->txd.entries);
750 }
Ben Hutchings46426102010-09-10 06:42:33 +0000751
Ben Hutchings29c69a42013-01-28 19:01:06 +0000752 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000753 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100754 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000755
Ben Hutchings7f967c02012-02-13 23:45:02 +0000756 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000757 memset(other_channel, 0, sizeof(other_channel));
758 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000759 channel = efx->channel[i];
760 if (channel->type->copy)
761 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000762 if (!channel) {
763 rc = -ENOMEM;
764 goto out;
765 }
766 other_channel[i] = channel;
767 }
768
769 /* Swap entry counts and channel pointers */
770 old_rxq_entries = efx->rxq_entries;
771 old_txq_entries = efx->txq_entries;
772 efx->rxq_entries = rxq_entries;
773 efx->txq_entries = txq_entries;
774 for (i = 0; i < efx->n_channels; i++) {
775 channel = efx->channel[i];
776 efx->channel[i] = other_channel[i];
777 other_channel[i] = channel;
778 }
779
Ben Hutchings7f967c02012-02-13 23:45:02 +0000780 /* Restart buffer table allocation */
781 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000782
Ben Hutchingse8f14992010-12-07 19:47:34 +0000783 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000784 channel = efx->channel[i];
785 if (!channel->type->copy)
786 continue;
787 rc = efx_probe_channel(channel);
788 if (rc)
789 goto rollback;
790 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000791 }
Ben Hutchings46426102010-09-10 06:42:33 +0000792
Ben Hutchings7f967c02012-02-13 23:45:02 +0000793out:
794 /* Destroy unused channel structures */
795 for (i = 0; i < efx->n_channels; i++) {
796 channel = other_channel[i];
797 if (channel && channel->type->copy) {
798 efx_fini_napi_channel(channel);
799 efx_remove_channel(channel);
800 kfree(channel);
801 }
802 }
803
Ben Hutchingsd8291182012-10-05 23:35:41 +0100804 efx_soft_enable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000805 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000806 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000807 return rc;
808
809rollback:
810 /* Swap back */
811 efx->rxq_entries = old_rxq_entries;
812 efx->txq_entries = old_txq_entries;
813 for (i = 0; i < efx->n_channels; i++) {
814 channel = efx->channel[i];
815 efx->channel[i] = other_channel[i];
816 other_channel[i] = channel;
817 }
818 goto out;
819}
820
Steve Hodgson90d683a2010-06-01 11:19:39 +0000821void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100822{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000823 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100824}
825
Ben Hutchings7f967c02012-02-13 23:45:02 +0000826static const struct efx_channel_type efx_default_channel_type = {
827 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100828 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000829 .get_name = efx_get_channel_name,
830 .copy = efx_copy_channel,
831 .keep_eventq = false,
832};
833
834int efx_channel_dummy_op_int(struct efx_channel *channel)
835{
836 return 0;
837}
838
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100839void efx_channel_dummy_op_void(struct efx_channel *channel)
840{
841}
842
Ben Hutchings8ceee662008-04-27 12:55:59 +0100843/**************************************************************************
844 *
845 * Port handling
846 *
847 **************************************************************************/
848
849/* This ensures that the kernel is kept informed (via
850 * netif_carrier_on/off) of the link status, and also maintains the
851 * link status's stop on the port's TX queue.
852 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000853void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100854{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000855 struct efx_link_state *link_state = &efx->link_state;
856
Ben Hutchings8ceee662008-04-27 12:55:59 +0100857 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
858 * that no events are triggered between unregister_netdev() and the
859 * driver unloading. A more general condition is that NETDEV_CHANGE
860 * can only be generated between NETDEV_UP and NETDEV_DOWN */
861 if (!netif_running(efx->net_dev))
862 return;
863
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000864 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100865 efx->n_link_state_changes++;
866
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000867 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100868 netif_carrier_on(efx->net_dev);
869 else
870 netif_carrier_off(efx->net_dev);
871 }
872
873 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000874 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000875 netif_info(efx, link, efx->net_dev,
876 "link up at %uMbps %s-duplex (MTU %d)%s\n",
877 link_state->speed, link_state->fd ? "full" : "half",
878 efx->net_dev->mtu,
879 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000880 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000881 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100882}
883
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000884void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
885{
886 efx->link_advertising = advertising;
887 if (advertising) {
888 if (advertising & ADVERTISED_Pause)
889 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
890 else
891 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
892 if (advertising & ADVERTISED_Asym_Pause)
893 efx->wanted_fc ^= EFX_FC_TX;
894 }
895}
896
David S. Millerb56269462011-05-17 17:53:22 -0400897void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000898{
899 efx->wanted_fc = wanted_fc;
900 if (efx->link_advertising) {
901 if (wanted_fc & EFX_FC_RX)
902 efx->link_advertising |= (ADVERTISED_Pause |
903 ADVERTISED_Asym_Pause);
904 else
905 efx->link_advertising &= ~(ADVERTISED_Pause |
906 ADVERTISED_Asym_Pause);
907 if (wanted_fc & EFX_FC_TX)
908 efx->link_advertising ^= ADVERTISED_Asym_Pause;
909 }
910}
911
Ben Hutchings115122a2009-03-04 09:52:52 +0000912static void efx_fini_port(struct efx_nic *efx);
913
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000914/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
915 * the MAC appropriately. All other PHY configuration changes are pushed
916 * through phy_op->set_settings(), and pushed asynchronously to the MAC
917 * through efx_monitor().
918 *
919 * Callers must hold the mac_lock
920 */
921int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100922{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000923 enum efx_phy_mode phy_mode;
924 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100925
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000926 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100927
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000928 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000929 netif_addr_lock_bh(efx->net_dev);
930 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100931
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000932 /* Disable PHY transmit in mac level loopbacks */
933 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800934 if (LOOPBACK_INTERNAL(efx))
935 efx->phy_mode |= PHY_MODE_TX_DISABLED;
936 else
937 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800938
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000939 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800940
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000941 if (rc)
942 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100943
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000944 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100945}
946
947/* Reinitialise the MAC to pick up new PHY settings, even if the port is
948 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000949int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100950{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000951 int rc;
952
Ben Hutchings8ceee662008-04-27 12:55:59 +0100953 EFX_ASSERT_RESET_SERIALISED(efx);
954
955 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000956 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100957 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000958
959 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100960}
961
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000962/* Asynchronous work item for changing MAC promiscuity and multicast
963 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
964 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800965static void efx_mac_work(struct work_struct *data)
966{
967 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
968
969 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100970 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100971 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800972 mutex_unlock(&efx->mac_lock);
973}
974
Ben Hutchings8ceee662008-04-27 12:55:59 +0100975static int efx_probe_port(struct efx_nic *efx)
976{
977 int rc;
978
Ben Hutchings62776d02010-06-23 11:30:07 +0000979 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100980
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000981 if (phy_flash_cfg)
982 efx->phy_mode = PHY_MODE_SPECIAL;
983
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000984 /* Connect up MAC/PHY operations table */
985 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100986 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000987 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100988
Ben Hutchingse332bcb2011-12-20 01:22:51 +0000989 /* Initialise MAC address to permanent address */
990 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100991
992 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100993}
994
995static int efx_init_port(struct efx_nic *efx)
996{
997 int rc;
998
Ben Hutchings62776d02010-06-23 11:30:07 +0000999 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001000
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001001 mutex_lock(&efx->mac_lock);
1002
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001003 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001004 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001005 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001006
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001007 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001008
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001009 /* Reconfigure the MAC before creating dma queues (required for
1010 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001011 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001012
1013 /* Ensure the PHY advertises the correct flow control settings */
1014 rc = efx->phy_op->reconfigure(efx);
1015 if (rc)
1016 goto fail2;
1017
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001018 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001019 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001020
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001021fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001022 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001023fail1:
1024 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001025 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001026}
1027
Ben Hutchings8ceee662008-04-27 12:55:59 +01001028static void efx_start_port(struct efx_nic *efx)
1029{
Ben Hutchings62776d02010-06-23 11:30:07 +00001030 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001031 BUG_ON(efx->port_enabled);
1032
1033 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001034 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001035
1036 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1037 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001038 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001039
Ben Hutchings8ceee662008-04-27 12:55:59 +01001040 mutex_unlock(&efx->mac_lock);
1041}
1042
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001043/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001044static void efx_stop_port(struct efx_nic *efx)
1045{
Ben Hutchings62776d02010-06-23 11:30:07 +00001046 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001047
1048 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001049 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001050 mutex_unlock(&efx->mac_lock);
1051
1052 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001053 netif_addr_lock_bh(efx->net_dev);
1054 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001055}
1056
1057static void efx_fini_port(struct efx_nic *efx)
1058{
Ben Hutchings62776d02010-06-23 11:30:07 +00001059 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001060
1061 if (!efx->port_initialized)
1062 return;
1063
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001064 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001065 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001066
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001067 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001068 efx_link_status_changed(efx);
1069}
1070
1071static void efx_remove_port(struct efx_nic *efx)
1072{
Ben Hutchings62776d02010-06-23 11:30:07 +00001073 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001074
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001075 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001076}
1077
1078/**************************************************************************
1079 *
1080 * NIC handling
1081 *
1082 **************************************************************************/
1083
1084/* This configures the PCI device to enable I/O and DMA. */
1085static int efx_init_io(struct efx_nic *efx)
1086{
1087 struct pci_dev *pci_dev = efx->pci_dev;
1088 dma_addr_t dma_mask = efx->type->max_dma_mask;
1089 int rc;
1090
Ben Hutchings62776d02010-06-23 11:30:07 +00001091 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001092
1093 rc = pci_enable_device(pci_dev);
1094 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001095 netif_err(efx, probe, efx->net_dev,
1096 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001097 goto fail1;
1098 }
1099
1100 pci_set_master(pci_dev);
1101
1102 /* Set the PCI DMA mask. Try all possibilities from our
1103 * genuine mask down to 32 bits, because some architectures
1104 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1105 * masks event though they reject 46 bit masks.
1106 */
1107 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001108 if (dma_supported(&pci_dev->dev, dma_mask)) {
1109 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001110 if (rc == 0)
1111 break;
1112 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001113 dma_mask >>= 1;
1114 }
1115 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001116 netif_err(efx, probe, efx->net_dev,
1117 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001118 goto fail2;
1119 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001120 netif_dbg(efx, probe, efx->net_dev,
1121 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001122 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001123 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001124 /* dma_set_coherent_mask() is not *allowed* to
1125 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001126 * but just in case...
1127 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001128 netif_err(efx, probe, efx->net_dev,
1129 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001130 goto fail2;
1131 }
1132
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001133 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1134 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001135 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001136 netif_err(efx, probe, efx->net_dev,
1137 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001138 rc = -EIO;
1139 goto fail3;
1140 }
David S. Miller8decf862011-09-22 03:23:13 -04001141 efx->membase = ioremap_nocache(efx->membase_phys,
1142 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001143 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001144 netif_err(efx, probe, efx->net_dev,
1145 "could not map memory BAR at %llx+%x\n",
1146 (unsigned long long)efx->membase_phys,
1147 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001148 rc = -ENOMEM;
1149 goto fail4;
1150 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001151 netif_dbg(efx, probe, efx->net_dev,
1152 "memory BAR at %llx+%x (virtual %p)\n",
1153 (unsigned long long)efx->membase_phys,
1154 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001155
1156 return 0;
1157
1158 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001159 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001160 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001161 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001162 fail2:
1163 pci_disable_device(efx->pci_dev);
1164 fail1:
1165 return rc;
1166}
1167
1168static void efx_fini_io(struct efx_nic *efx)
1169{
Ben Hutchings62776d02010-06-23 11:30:07 +00001170 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001171
1172 if (efx->membase) {
1173 iounmap(efx->membase);
1174 efx->membase = NULL;
1175 }
1176
1177 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001178 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001179 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001180 }
1181
1182 pci_disable_device(efx->pci_dev);
1183}
1184
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001185static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001186{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001187 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001188 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001189 int cpu;
1190
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001191 if (rss_cpus) {
1192 count = rss_cpus;
1193 } else {
1194 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1195 netif_warn(efx, probe, efx->net_dev,
1196 "RSS disabled due to allocation failure\n");
1197 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001198 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001199
1200 count = 0;
1201 for_each_online_cpu(cpu) {
1202 if (!cpumask_test_cpu(cpu, thread_mask)) {
1203 ++count;
1204 cpumask_or(thread_mask, thread_mask,
1205 topology_thread_cpumask(cpu));
1206 }
1207 }
1208
1209 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001210 }
1211
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001212 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1213 * table entries that are inaccessible to VFs
1214 */
1215 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1216 count > efx_vf_size(efx)) {
1217 netif_warn(efx, probe, efx->net_dev,
1218 "Reducing number of RSS channels from %u to %u for "
1219 "VF support. Increase vf-msix-limit to use more "
1220 "channels on the PF.\n",
1221 count, efx_vf_size(efx));
1222 count = efx_vf_size(efx);
1223 }
1224
Ben Hutchings46123d02008-09-01 12:47:33 +01001225 return count;
1226}
1227
1228/* Probe the number and type of interrupts we are able to obtain, and
1229 * the resulting numbers of channels and RX queues.
1230 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001231static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001232{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001233 unsigned int max_channels =
1234 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001235 unsigned int extra_channels = 0;
1236 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001237 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001238
Ben Hutchings7f967c02012-02-13 23:45:02 +00001239 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1240 if (efx->extra_channel_type[i])
1241 ++extra_channels;
1242
Ben Hutchings8ceee662008-04-27 12:55:59 +01001243 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001244 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001245 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001246
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001247 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001248 if (separate_tx_channels)
1249 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001250 n_channels += extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001251 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001252
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001253 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001254 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001255 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001256 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001257 netif_err(efx, drv, efx->net_dev,
1258 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001259 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001260 netif_err(efx, drv, efx->net_dev,
1261 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001262 EFX_BUG_ON_PARANOID(rc >= n_channels);
1263 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001264 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001265 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001266 }
1267
1268 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001269 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001270 if (n_channels > extra_channels)
1271 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001272 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001273 efx->n_tx_channels = max(n_channels / 2, 1U);
1274 efx->n_rx_channels = max(n_channels -
1275 efx->n_tx_channels,
1276 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001277 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001278 efx->n_tx_channels = n_channels;
1279 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001280 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001281 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001282 efx_get_channel(efx, i)->irq =
1283 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001284 } else {
1285 /* Fall back to single channel MSI */
1286 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001287 netif_err(efx, drv, efx->net_dev,
1288 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001289 }
1290 }
1291
1292 /* Try single interrupt MSI */
1293 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001294 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001295 efx->n_rx_channels = 1;
1296 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001297 rc = pci_enable_msi(efx->pci_dev);
1298 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001299 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001300 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001301 netif_err(efx, drv, efx->net_dev,
1302 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001303 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1304 }
1305 }
1306
1307 /* Assume legacy interrupts */
1308 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001309 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001310 efx->n_rx_channels = 1;
1311 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001312 efx->legacy_irq = efx->pci_dev->irq;
1313 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001314
Ben Hutchings7f967c02012-02-13 23:45:02 +00001315 /* Assign extra channels if possible */
1316 j = efx->n_channels;
1317 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1318 if (!efx->extra_channel_type[i])
1319 continue;
1320 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1321 efx->n_channels <= extra_channels) {
1322 efx->extra_channel_type[i]->handle_no_channel(efx);
1323 } else {
1324 --j;
1325 efx_get_channel(efx, j)->type =
1326 efx->extra_channel_type[i];
1327 }
1328 }
1329
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001330 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001331 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001332 efx->n_rx_channels : efx_vf_size(efx));
1333
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001334 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001335}
1336
Ben Hutchingsd8291182012-10-05 23:35:41 +01001337static void efx_soft_enable_interrupts(struct efx_nic *efx)
1338{
1339 struct efx_channel *channel;
1340
1341 BUG_ON(efx->state == STATE_DISABLED);
1342
1343 efx->irq_soft_enabled = true;
1344 smp_wmb();
1345
1346 efx_for_each_channel(channel, efx) {
1347 if (!channel->type->keep_eventq)
1348 efx_init_eventq(channel);
1349 efx_start_eventq(channel);
1350 }
1351
1352 efx_mcdi_mode_event(efx);
1353}
1354
1355static void efx_soft_disable_interrupts(struct efx_nic *efx)
1356{
1357 struct efx_channel *channel;
1358
1359 if (efx->state == STATE_DISABLED)
1360 return;
1361
1362 efx_mcdi_mode_poll(efx);
1363
1364 efx->irq_soft_enabled = false;
1365 smp_wmb();
1366
1367 if (efx->legacy_irq)
1368 synchronize_irq(efx->legacy_irq);
1369
1370 efx_for_each_channel(channel, efx) {
1371 if (channel->irq)
1372 synchronize_irq(channel->irq);
1373
1374 efx_stop_eventq(channel);
1375 if (!channel->type->keep_eventq)
1376 efx_fini_eventq(channel);
1377 }
1378}
1379
1380static void efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001381{
1382 struct efx_channel *channel;
1383
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001384 BUG_ON(efx->state == STATE_DISABLED);
1385
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001386 if (efx->eeh_disabled_legacy_irq) {
1387 enable_irq(efx->legacy_irq);
1388 efx->eeh_disabled_legacy_irq = false;
1389 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001390
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001391 efx_nic_enable_interrupts(efx);
1392
1393 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001394 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001395 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001396 }
1397
Ben Hutchingsd8291182012-10-05 23:35:41 +01001398 efx_soft_enable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001399}
1400
Ben Hutchingsd8291182012-10-05 23:35:41 +01001401static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001402{
1403 struct efx_channel *channel;
1404
Ben Hutchingsd8291182012-10-05 23:35:41 +01001405 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001406
1407 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001408 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001409 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001410 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001411
1412 efx_nic_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001413}
1414
Ben Hutchings8ceee662008-04-27 12:55:59 +01001415static void efx_remove_interrupts(struct efx_nic *efx)
1416{
1417 struct efx_channel *channel;
1418
1419 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001420 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001421 channel->irq = 0;
1422 pci_disable_msi(efx->pci_dev);
1423 pci_disable_msix(efx->pci_dev);
1424
1425 /* Remove legacy interrupt */
1426 efx->legacy_irq = 0;
1427}
1428
Ben Hutchings8831da72008-09-01 12:47:48 +01001429static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001430{
Ben Hutchings602a5322011-05-16 17:32:39 +01001431 struct efx_channel *channel;
1432 struct efx_tx_queue *tx_queue;
1433
Ben Hutchings97653432011-01-12 18:26:56 +00001434 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001435 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001436
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001437 /* We need to mark which channels really have RX and TX
1438 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001439 * RX-only and TX-only channels.
1440 */
1441 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001442 if (channel->channel < efx->n_rx_channels)
1443 channel->rx_queue.core_index = channel->channel;
1444 else
1445 channel->rx_queue.core_index = -1;
1446
Ben Hutchings602a5322011-05-16 17:32:39 +01001447 efx_for_each_channel_tx_queue(tx_queue, channel)
1448 tx_queue->queue -= (efx->tx_channel_offset *
1449 EFX_TXQ_TYPES);
1450 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001451}
1452
1453static int efx_probe_nic(struct efx_nic *efx)
1454{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001455 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001456 int rc;
1457
Ben Hutchings62776d02010-06-23 11:30:07 +00001458 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001459
1460 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001461 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001462 if (rc)
1463 return rc;
1464
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001465 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001466 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001467 rc = efx_probe_interrupts(efx);
1468 if (rc)
1469 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001470
Ben Hutchings28e47c42012-02-15 01:58:49 +00001471 efx->type->dimension_resources(efx);
1472
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001473 if (efx->n_channels > 1)
1474 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001475 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001476 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001477 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001478
Ben Hutchings8831da72008-09-01 12:47:48 +01001479 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001480 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1481 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001482
1483 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001484 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1485 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001486
1487 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001488
1489fail:
1490 efx->type->remove(efx);
1491 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001492}
1493
1494static void efx_remove_nic(struct efx_nic *efx)
1495{
Ben Hutchings62776d02010-06-23 11:30:07 +00001496 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001497
1498 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001499 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001500}
1501
1502/**************************************************************************
1503 *
1504 * NIC startup/shutdown
1505 *
1506 *************************************************************************/
1507
1508static int efx_probe_all(struct efx_nic *efx)
1509{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001510 int rc;
1511
Ben Hutchings8ceee662008-04-27 12:55:59 +01001512 rc = efx_probe_nic(efx);
1513 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001514 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001515 goto fail1;
1516 }
1517
Ben Hutchings8ceee662008-04-27 12:55:59 +01001518 rc = efx_probe_port(efx);
1519 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001520 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001521 goto fail2;
1522 }
1523
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001524 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1525 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1526 rc = -EINVAL;
1527 goto fail3;
1528 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001529 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001530
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001531 rc = efx_probe_filters(efx);
1532 if (rc) {
1533 netif_err(efx, probe, efx->net_dev,
1534 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001535 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001536 }
1537
Ben Hutchings7f967c02012-02-13 23:45:02 +00001538 rc = efx_probe_channels(efx);
1539 if (rc)
1540 goto fail4;
1541
Ben Hutchings8ceee662008-04-27 12:55:59 +01001542 return 0;
1543
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001544 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001545 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001546 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001547 efx_remove_port(efx);
1548 fail2:
1549 efx_remove_nic(efx);
1550 fail1:
1551 return rc;
1552}
1553
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001554/* If the interface is supposed to be running but is not, start
1555 * the hardware and software data path, regular activity for the port
1556 * (MAC statistics, link polling, etc.) and schedule the port to be
1557 * reconfigured. Interrupts must already be enabled. This function
1558 * is safe to call multiple times, so long as the NIC is not disabled.
1559 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001560 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001561static void efx_start_all(struct efx_nic *efx)
1562{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001563 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001564 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001565
1566 /* Check that it is appropriate to restart the interface. All
1567 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001568 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001569 return;
1570
Ben Hutchings8ceee662008-04-27 12:55:59 +01001571 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001572 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001573
Alexandre Rames626950d2013-01-14 17:20:22 +00001574 /* Start the hardware monitor if there is one */
1575 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001576 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1577 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001578
1579 /* If link state detection is normally event-driven, we have
1580 * to poll now because we could have missed a change
1581 */
1582 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001583 mutex_lock(&efx->mac_lock);
1584 if (efx->phy_op->poll(efx))
1585 efx_link_status_changed(efx);
1586 mutex_unlock(&efx->mac_lock);
1587 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001588
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001589 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001590}
1591
1592/* Flush all delayed work. Should only be called when no more delayed work
1593 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1594 * since we're holding the rtnl_lock at this point. */
1595static void efx_flush_all(struct efx_nic *efx)
1596{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001597 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001598 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001599 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001600 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001601 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001602}
1603
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001604/* Quiesce the hardware and software data path, and regular activity
1605 * for the port without bringing the link down. Safe to call multiple
1606 * times with the NIC in almost any state, but interrupts should be
1607 * enabled. Requires the RTNL lock.
1608 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001609static void efx_stop_all(struct efx_nic *efx)
1610{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001611 EFX_ASSERT_RESET_SERIALISED(efx);
1612
1613 /* port_enabled can be read safely under the rtnl lock */
1614 if (!efx->port_enabled)
1615 return;
1616
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001617 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001618 efx_stop_port(efx);
1619
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001620 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001621 efx_flush_all(efx);
1622
Ben Hutchings29c69a42013-01-28 19:01:06 +00001623 /* Stop the kernel transmit interface. This is only valid if
1624 * the device is stopped or detached; otherwise the watchdog
1625 * may fire immediately.
1626 */
1627 WARN_ON(netif_running(efx->net_dev) &&
1628 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001629 netif_tx_disable(efx->net_dev);
1630
1631 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001632}
1633
1634static void efx_remove_all(struct efx_nic *efx)
1635{
Ben Hutchings46426102010-09-10 06:42:33 +00001636 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001637 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001638 efx_remove_port(efx);
1639 efx_remove_nic(efx);
1640}
1641
Ben Hutchings8ceee662008-04-27 12:55:59 +01001642/**************************************************************************
1643 *
1644 * Interrupt moderation
1645 *
1646 **************************************************************************/
1647
Ben Hutchingscc180b62011-12-08 19:51:47 +00001648static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001649{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001650 if (usecs == 0)
1651 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001652 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001653 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001654 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001655}
1656
Ben Hutchings8ceee662008-04-27 12:55:59 +01001657/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001658int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1659 unsigned int rx_usecs, bool rx_adaptive,
1660 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001661{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001662 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001663 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1664 efx->timer_quantum_ns,
1665 1000);
1666 unsigned int tx_ticks;
1667 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001668
1669 EFX_ASSERT_RESET_SERIALISED(efx);
1670
Ben Hutchingscc180b62011-12-08 19:51:47 +00001671 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001672 return -EINVAL;
1673
Ben Hutchingscc180b62011-12-08 19:51:47 +00001674 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1675 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1676
Ben Hutchings9e393b32011-09-05 07:43:04 +00001677 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1678 !rx_may_override_tx) {
1679 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1680 "RX and TX IRQ moderation must be equal\n");
1681 return -EINVAL;
1682 }
1683
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001684 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001685 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001686 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001687 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001688 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001689 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001690 channel->irq_moderation = tx_ticks;
1691 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001692
1693 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001694}
1695
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001696void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1697 unsigned int *rx_usecs, bool *rx_adaptive)
1698{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001699 /* We must round up when converting ticks to microseconds
1700 * because we round down when converting the other way.
1701 */
1702
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001703 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001704 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1705 efx->timer_quantum_ns,
1706 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001707
1708 /* If channels are shared between RX and TX, so is IRQ
1709 * moderation. Otherwise, IRQ moderation is the same for all
1710 * TX channels and is not adaptive.
1711 */
1712 if (efx->tx_channel_offset == 0)
1713 *tx_usecs = *rx_usecs;
1714 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001715 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001716 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001717 efx->timer_quantum_ns,
1718 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001719}
1720
Ben Hutchings8ceee662008-04-27 12:55:59 +01001721/**************************************************************************
1722 *
1723 * Hardware monitor
1724 *
1725 **************************************************************************/
1726
Ben Hutchingse254c272010-09-20 08:44:10 +00001727/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001728static void efx_monitor(struct work_struct *data)
1729{
1730 struct efx_nic *efx = container_of(data, struct efx_nic,
1731 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001732
Ben Hutchings62776d02010-06-23 11:30:07 +00001733 netif_vdbg(efx, timer, efx->net_dev,
1734 "hardware monitor executing on CPU %d\n",
1735 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001736 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001737
Ben Hutchings8ceee662008-04-27 12:55:59 +01001738 /* If the mac_lock is already held then it is likely a port
1739 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001740 * most of the work of monitor() anyway. */
1741 if (mutex_trylock(&efx->mac_lock)) {
1742 if (efx->port_enabled)
1743 efx->type->monitor(efx);
1744 mutex_unlock(&efx->mac_lock);
1745 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001746
Ben Hutchings8ceee662008-04-27 12:55:59 +01001747 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1748 efx_monitor_interval);
1749}
1750
1751/**************************************************************************
1752 *
1753 * ioctls
1754 *
1755 *************************************************************************/
1756
1757/* Net device ioctl
1758 * Context: process, rtnl_lock() held.
1759 */
1760static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1761{
Ben Hutchings767e4682008-09-01 12:43:14 +01001762 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001763 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001764
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001765 if (cmd == SIOCSHWTSTAMP)
1766 return efx_ptp_ioctl(efx, ifr, cmd);
1767
Ben Hutchings68e7f452009-04-29 08:05:08 +00001768 /* Convert phy_id from older PRTAD/DEVAD format */
1769 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1770 (data->phy_id & 0xfc00) == 0x0400)
1771 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1772
1773 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001774}
1775
1776/**************************************************************************
1777 *
1778 * NAPI interface
1779 *
1780 **************************************************************************/
1781
Ben Hutchings7f967c02012-02-13 23:45:02 +00001782static void efx_init_napi_channel(struct efx_channel *channel)
1783{
1784 struct efx_nic *efx = channel->efx;
1785
1786 channel->napi_dev = efx->net_dev;
1787 netif_napi_add(channel->napi_dev, &channel->napi_str,
1788 efx_poll, napi_weight);
1789}
1790
Ben Hutchingse8f14992010-12-07 19:47:34 +00001791static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001792{
1793 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001794
Ben Hutchings7f967c02012-02-13 23:45:02 +00001795 efx_for_each_channel(channel, efx)
1796 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001797}
1798
1799static void efx_fini_napi_channel(struct efx_channel *channel)
1800{
1801 if (channel->napi_dev)
1802 netif_napi_del(&channel->napi_str);
1803 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001804}
1805
1806static void efx_fini_napi(struct efx_nic *efx)
1807{
1808 struct efx_channel *channel;
1809
Ben Hutchingse8f14992010-12-07 19:47:34 +00001810 efx_for_each_channel(channel, efx)
1811 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001812}
1813
1814/**************************************************************************
1815 *
1816 * Kernel netpoll interface
1817 *
1818 *************************************************************************/
1819
1820#ifdef CONFIG_NET_POLL_CONTROLLER
1821
1822/* Although in the common case interrupts will be disabled, this is not
1823 * guaranteed. However, all our work happens inside the NAPI callback,
1824 * so no locking is required.
1825 */
1826static void efx_netpoll(struct net_device *net_dev)
1827{
Ben Hutchings767e4682008-09-01 12:43:14 +01001828 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001829 struct efx_channel *channel;
1830
Ben Hutchings64ee3122008-09-01 12:47:38 +01001831 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001832 efx_schedule_channel(channel);
1833}
1834
1835#endif
1836
1837/**************************************************************************
1838 *
1839 * Kernel net device interface
1840 *
1841 *************************************************************************/
1842
1843/* Context: process, rtnl_lock() held. */
1844static int efx_net_open(struct net_device *net_dev)
1845{
Ben Hutchings767e4682008-09-01 12:43:14 +01001846 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001847 int rc;
1848
Ben Hutchings62776d02010-06-23 11:30:07 +00001849 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1850 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001851
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001852 rc = efx_check_disabled(efx);
1853 if (rc)
1854 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001855 if (efx->phy_mode & PHY_MODE_SPECIAL)
1856 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001857 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1858 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001859
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001860 /* Notify the kernel of the link state polled during driver load,
1861 * before the monitor starts running */
1862 efx_link_status_changed(efx);
1863
Ben Hutchings8ceee662008-04-27 12:55:59 +01001864 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001865 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001866 return 0;
1867}
1868
1869/* Context: process, rtnl_lock() held.
1870 * Note that the kernel will ignore our return code; this method
1871 * should really be a void.
1872 */
1873static int efx_net_stop(struct net_device *net_dev)
1874{
Ben Hutchings767e4682008-09-01 12:43:14 +01001875 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001876
Ben Hutchings62776d02010-06-23 11:30:07 +00001877 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1878 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001879
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001880 /* Stop the device and flush all the channels */
1881 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001882
1883 return 0;
1884}
1885
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001886/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001887static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1888 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001889{
Ben Hutchings767e4682008-09-01 12:43:14 +01001890 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001891 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001892
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001893 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001894
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001895 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001896
1897 stats->rx_packets = mac_stats->rx_packets;
1898 stats->tx_packets = mac_stats->tx_packets;
1899 stats->rx_bytes = mac_stats->rx_bytes;
1900 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001901 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001902 stats->multicast = mac_stats->rx_multicast;
1903 stats->collisions = mac_stats->tx_collision;
1904 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1905 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001906 stats->rx_crc_errors = mac_stats->rx_bad;
1907 stats->rx_frame_errors = mac_stats->rx_align_error;
1908 stats->rx_fifo_errors = mac_stats->rx_overflow;
1909 stats->rx_missed_errors = mac_stats->rx_missed;
1910 stats->tx_window_errors = mac_stats->tx_late_collision;
1911
1912 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001913 stats->rx_crc_errors +
1914 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001915 mac_stats->rx_symbol_error);
1916 stats->tx_errors = (stats->tx_window_errors +
1917 mac_stats->tx_bad);
1918
Ben Hutchings1cb34522011-09-02 23:23:00 +01001919 spin_unlock_bh(&efx->stats_lock);
1920
Ben Hutchings8ceee662008-04-27 12:55:59 +01001921 return stats;
1922}
1923
1924/* Context: netif_tx_lock held, BHs disabled. */
1925static void efx_watchdog(struct net_device *net_dev)
1926{
Ben Hutchings767e4682008-09-01 12:43:14 +01001927 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001928
Ben Hutchings62776d02010-06-23 11:30:07 +00001929 netif_err(efx, tx_err, efx->net_dev,
1930 "TX stuck with port_enabled=%d: resetting channels\n",
1931 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001932
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001933 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001934}
1935
1936
1937/* Context: process, rtnl_lock() held. */
1938static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1939{
Ben Hutchings767e4682008-09-01 12:43:14 +01001940 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001941 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001942
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001943 rc = efx_check_disabled(efx);
1944 if (rc)
1945 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001946 if (new_mtu > EFX_MAX_MTU)
1947 return -EINVAL;
1948
Ben Hutchings62776d02010-06-23 11:30:07 +00001949 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001950
Ben Hutchings29c69a42013-01-28 19:01:06 +00001951 efx_device_detach_sync(efx);
1952 efx_stop_all(efx);
1953
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001954 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001955 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001956 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001957 mutex_unlock(&efx->mac_lock);
1958
Ben Hutchings8ceee662008-04-27 12:55:59 +01001959 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00001960 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001961 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001962}
1963
1964static int efx_set_mac_address(struct net_device *net_dev, void *data)
1965{
Ben Hutchings767e4682008-09-01 12:43:14 +01001966 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001967 struct sockaddr *addr = data;
1968 char *new_addr = addr->sa_data;
1969
Ben Hutchings8ceee662008-04-27 12:55:59 +01001970 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001971 netif_err(efx, drv, efx->net_dev,
1972 "invalid ethernet MAC address requested: %pM\n",
1973 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00001974 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001975 }
1976
1977 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001978 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001979
1980 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001981 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01001982 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001983 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001984
1985 return 0;
1986}
1987
Ben Hutchingsa816f752008-09-01 12:49:12 +01001988/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00001989static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001990{
Ben Hutchings767e4682008-09-01 12:43:14 +01001991 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001992 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001993 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001994 u32 crc;
1995 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001996
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001997 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001998
1999 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002000 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002001 memset(mc_hash, 0xff, sizeof(*mc_hash));
2002 } else {
2003 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00002004 netdev_for_each_mc_addr(ha, net_dev) {
2005 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002006 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
Ben Hutchings32766ec2012-10-04 17:13:03 -07002007 __set_bit_le(bit, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002008 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002009
2010 /* Broadcast packets go through the multicast hash filter.
2011 * ether_crc_le() of the broadcast address is 0xbe2612ff
2012 * so we always add bit 0xff to the mask.
2013 */
Ben Hutchings32766ec2012-10-04 17:13:03 -07002014 __set_bit_le(0xff, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002015 }
2016
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002017 if (efx->port_enabled)
2018 queue_work(efx->workqueue, &efx->mac_work);
2019 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002020}
2021
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002022static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002023{
2024 struct efx_nic *efx = netdev_priv(net_dev);
2025
2026 /* If disabling RX n-tuple filtering, clear existing filters */
2027 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2028 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2029
2030 return 0;
2031}
2032
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002033static const struct net_device_ops efx_netdev_ops = {
2034 .ndo_open = efx_net_open,
2035 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002036 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002037 .ndo_tx_timeout = efx_watchdog,
2038 .ndo_start_xmit = efx_hard_start_xmit,
2039 .ndo_validate_addr = eth_validate_addr,
2040 .ndo_do_ioctl = efx_ioctl,
2041 .ndo_change_mtu = efx_change_mtu,
2042 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002043 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002044 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002045#ifdef CONFIG_SFC_SRIOV
2046 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2047 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2048 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2049 .ndo_get_vf_config = efx_sriov_get_vf_config,
2050#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002051#ifdef CONFIG_NET_POLL_CONTROLLER
2052 .ndo_poll_controller = efx_netpoll,
2053#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002054 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002055#ifdef CONFIG_RFS_ACCEL
2056 .ndo_rx_flow_steer = efx_filter_rfs,
2057#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002058};
2059
Ben Hutchings7dde5962008-12-12 22:09:38 -08002060static void efx_update_name(struct efx_nic *efx)
2061{
2062 strcpy(efx->name, efx->net_dev->name);
2063 efx_mtd_rename(efx);
2064 efx_set_channel_names(efx);
2065}
2066
Ben Hutchings8ceee662008-04-27 12:55:59 +01002067static int efx_netdev_event(struct notifier_block *this,
2068 unsigned long event, void *ptr)
2069{
Jiri Pirko351638e2013-05-28 01:30:21 +00002070 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002071
Ben Hutchings7dde5962008-12-12 22:09:38 -08002072 if (net_dev->netdev_ops == &efx_netdev_ops &&
2073 event == NETDEV_CHANGENAME)
2074 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002075
2076 return NOTIFY_DONE;
2077}
2078
2079static struct notifier_block efx_netdev_notifier = {
2080 .notifier_call = efx_netdev_event,
2081};
2082
Ben Hutchings06d5e192008-12-12 21:47:23 -08002083static ssize_t
2084show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2085{
2086 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2087 return sprintf(buf, "%d\n", efx->phy_type);
2088}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002089static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002090
Ben Hutchings8ceee662008-04-27 12:55:59 +01002091static int efx_register_netdev(struct efx_nic *efx)
2092{
2093 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002094 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002095 int rc;
2096
2097 net_dev->watchdog_timeo = 5 * HZ;
2098 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002099 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002100 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002101 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002102
Ben Hutchings7dde5962008-12-12 22:09:38 -08002103 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002104
Ben Hutchings7153f622012-07-27 20:50:52 +01002105 /* Enable resets to be scheduled and check whether any were
2106 * already requested. If so, the NIC is probably hosed so we
2107 * abort.
2108 */
2109 efx->state = STATE_READY;
2110 smp_mb(); /* ensure we change state before checking reset_pending */
2111 if (efx->reset_pending) {
2112 netif_err(efx, probe, efx->net_dev,
2113 "aborting probe due to scheduled reset\n");
2114 rc = -EIO;
2115 goto fail_locked;
2116 }
2117
Ben Hutchingsaed06282009-08-26 08:16:27 +00002118 rc = dev_alloc_name(net_dev, net_dev->name);
2119 if (rc < 0)
2120 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002121 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002122
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002123 /* Always start with carrier off; PHY events will detect the link */
2124 netif_carrier_off(net_dev);
2125
Ben Hutchingsaed06282009-08-26 08:16:27 +00002126 rc = register_netdevice(net_dev);
2127 if (rc)
2128 goto fail_locked;
2129
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002130 efx_for_each_channel(channel, efx) {
2131 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002132 efx_for_each_channel_tx_queue(tx_queue, channel)
2133 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002134 }
2135
Ben Hutchings7dde5962008-12-12 22:09:38 -08002136 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002137
Ben Hutchings06d5e192008-12-12 21:47:23 -08002138 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2139 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002140 netif_err(efx, drv, efx->net_dev,
2141 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002142 goto fail_registered;
2143 }
2144
Ben Hutchings8ceee662008-04-27 12:55:59 +01002145 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002146
Ben Hutchings7153f622012-07-27 20:50:52 +01002147fail_registered:
2148 rtnl_lock();
2149 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002150fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002151 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002152 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002153 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002154 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002155}
2156
2157static void efx_unregister_netdev(struct efx_nic *efx)
2158{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002159 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002160 struct efx_tx_queue *tx_queue;
2161
2162 if (!efx->net_dev)
2163 return;
2164
Ben Hutchings767e4682008-09-01 12:43:14 +01002165 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002166
2167 /* Free up any skbs still remaining. This has to happen before
2168 * we try to unregister the netdev as running their destructors
2169 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002170 efx_for_each_channel(channel, efx) {
2171 efx_for_each_channel_tx_queue(tx_queue, channel)
2172 efx_release_tx_buffers(tx_queue);
2173 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002174
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002175 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2176 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002177
2178 rtnl_lock();
2179 unregister_netdevice(efx->net_dev);
2180 efx->state = STATE_UNINIT;
2181 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002182}
2183
2184/**************************************************************************
2185 *
2186 * Device reset and suspend
2187 *
2188 **************************************************************************/
2189
Ben Hutchings2467ca42008-09-01 12:48:50 +01002190/* Tears down the entire software state and most of the hardware state
2191 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002192void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002193{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002194 EFX_ASSERT_RESET_SERIALISED(efx);
2195
Ben Hutchings2467ca42008-09-01 12:48:50 +01002196 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002197 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002198
2199 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002200 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2201 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002202 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002203}
2204
Ben Hutchings2467ca42008-09-01 12:48:50 +01002205/* This function will always ensure that the locks acquired in
2206 * efx_reset_down() are released. A failure return code indicates
2207 * that we were unable to reinitialise the hardware, and the
2208 * driver should be disabled. If ok is false, then the rx and tx
2209 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002210int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002211{
2212 int rc;
2213
Ben Hutchings2467ca42008-09-01 12:48:50 +01002214 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002215
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002216 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002217 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002218 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002219 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002220 }
2221
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002222 if (!ok)
2223 goto fail;
2224
Steve Hodgson4b988282009-01-29 17:50:51 +00002225 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002226 rc = efx->phy_op->init(efx);
2227 if (rc)
2228 goto fail;
2229 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002230 netif_err(efx, drv, efx->net_dev,
2231 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002232 }
2233
Ben Hutchings710b2082011-09-03 00:15:00 +01002234 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002235
Ben Hutchingsd8291182012-10-05 23:35:41 +01002236 efx_enable_interrupts(efx);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002237 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002238 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002239
2240 mutex_unlock(&efx->mac_lock);
2241
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002242 efx_start_all(efx);
2243
2244 return 0;
2245
2246fail:
2247 efx->port_initialized = false;
2248
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002249 mutex_unlock(&efx->mac_lock);
2250
Ben Hutchings8ceee662008-04-27 12:55:59 +01002251 return rc;
2252}
2253
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002254/* Reset the NIC using the specified method. Note that the reset may
2255 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002256 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002257 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002258 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002259int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002260{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002261 int rc, rc2;
2262 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002263
Ben Hutchings62776d02010-06-23 11:30:07 +00002264 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2265 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002266
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002267 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002268 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002269
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002270 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002271 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002272 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002273 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002274 }
2275
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002276 /* Clear flags for the scopes we covered. We assume the NIC and
2277 * driver are now quiescent so that there is no race here.
2278 */
2279 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002280
2281 /* Reinitialise bus-mastering, which may have been turned off before
2282 * the reset was scheduled. This is still appropriate, even in the
2283 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2284 * can respond to requests. */
2285 pci_set_master(efx->pci_dev);
2286
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002287out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002288 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002289 disabled = rc ||
2290 method == RESET_TYPE_DISABLE ||
2291 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002292 rc2 = efx_reset_up(efx, method, !disabled);
2293 if (rc2) {
2294 disabled = true;
2295 if (!rc)
2296 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002297 }
2298
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002299 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002300 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002301 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002302 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002303 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002304 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002305 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002306 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002307 return rc;
2308}
2309
Alexandre Rames626950d2013-01-14 17:20:22 +00002310/* Try recovery mechanisms.
2311 * For now only EEH is supported.
2312 * Returns 0 if the recovery mechanisms are unsuccessful.
2313 * Returns a non-zero value otherwise.
2314 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002315int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002316{
2317#ifdef CONFIG_EEH
2318 /* A PCI error can occur and not be seen by EEH because nothing
2319 * happens on the PCI bus. In this case the driver may fail and
2320 * schedule a 'recover or reset', leading to this recovery handler.
2321 * Manually call the eeh failure check function.
2322 */
2323 struct eeh_dev *eehdev =
2324 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2325
2326 if (eeh_dev_check_failure(eehdev)) {
2327 /* The EEH mechanisms will handle the error and reset the
2328 * device if necessary.
2329 */
2330 return 1;
2331 }
2332#endif
2333 return 0;
2334}
2335
Ben Hutchings8ceee662008-04-27 12:55:59 +01002336/* The worker thread exists so that code that cannot sleep can
2337 * schedule a reset for later.
2338 */
2339static void efx_reset_work(struct work_struct *data)
2340{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002341 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002342 unsigned long pending;
2343 enum reset_type method;
2344
2345 pending = ACCESS_ONCE(efx->reset_pending);
2346 method = fls(pending) - 1;
2347
2348 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2349 method == RESET_TYPE_RECOVER_OR_ALL) &&
2350 efx_try_recovery(efx))
2351 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002352
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002353 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002354 return;
2355
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002356 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002357
2358 /* We checked the state in efx_schedule_reset() but it may
2359 * have changed by now. Now that we have the RTNL lock,
2360 * it cannot change again.
2361 */
2362 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002363 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002364
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002365 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002366}
2367
2368void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2369{
2370 enum reset_type method;
2371
Alexandre Rames626950d2013-01-14 17:20:22 +00002372 if (efx->state == STATE_RECOVERY) {
2373 netif_dbg(efx, drv, efx->net_dev,
2374 "recovering: skip scheduling %s reset\n",
2375 RESET_TYPE(type));
2376 return;
2377 }
2378
Ben Hutchings8ceee662008-04-27 12:55:59 +01002379 switch (type) {
2380 case RESET_TYPE_INVISIBLE:
2381 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002382 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002383 case RESET_TYPE_WORLD:
2384 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002385 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002386 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002387 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2388 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002389 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002390 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002391 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002392 netif_dbg(efx, drv, efx->net_dev,
2393 "scheduling %s reset for %s\n",
2394 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002395 break;
2396 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002397
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002398 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002399 smp_mb(); /* ensure we change reset_pending before checking state */
2400
2401 /* If we're not READY then just leave the flags set as the cue
2402 * to abort probing or reschedule the reset later.
2403 */
2404 if (ACCESS_ONCE(efx->state) != STATE_READY)
2405 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002406
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002407 /* efx_process_channel() will no longer read events once a
2408 * reset is scheduled. So switch back to poll'd MCDI completions. */
2409 efx_mcdi_mode_poll(efx);
2410
Steve Hodgson1ab00622008-12-12 21:33:02 -08002411 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002412}
2413
2414/**************************************************************************
2415 *
2416 * List of NICs we support
2417 *
2418 **************************************************************************/
2419
2420/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002421static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002422 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2423 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002424 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002425 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2426 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002427 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002428 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002429 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002430 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002431 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002432 {0} /* end of list */
2433};
2434
2435/**************************************************************************
2436 *
Ben Hutchings37594332009-11-23 16:05:45 +00002437 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002438 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002439 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002440 * Needed so all function pointers are valid and do not have to be tested
2441 * before use
2442 *
2443 **************************************************************************/
2444int efx_port_dummy_op_int(struct efx_nic *efx)
2445{
2446 return 0;
2447}
2448void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002449
2450static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002451{
2452 return false;
2453}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002454
stephen hemminger6c8c2512011-04-14 05:50:12 +00002455static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002456 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002457 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002458 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002459 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002460};
2461
Ben Hutchings8ceee662008-04-27 12:55:59 +01002462/**************************************************************************
2463 *
2464 * Data housekeeping
2465 *
2466 **************************************************************************/
2467
2468/* This zeroes out and then fills in the invariants in a struct
2469 * efx_nic (including all sub-structures).
2470 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002471static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002472 struct pci_dev *pci_dev, struct net_device *net_dev)
2473{
Ben Hutchings46426102010-09-10 06:42:33 +00002474 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002475
2476 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002477 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002478#ifdef CONFIG_SFC_MTD
2479 INIT_LIST_HEAD(&efx->mtd_list);
2480#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002481 INIT_WORK(&efx->reset_work, efx_reset_work);
2482 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002483 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002484 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002485 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002486 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002487 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002488
2489 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002490 spin_lock_init(&efx->stats_lock);
2491 mutex_init(&efx->mac_lock);
2492 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002493 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002494 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002495 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002496
2497 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002498 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2499 if (!efx->channel[i])
2500 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002501 efx->msi_context[i].efx = efx;
2502 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002503 }
2504
Ben Hutchings8ceee662008-04-27 12:55:59 +01002505 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2506
2507 /* Higher numbered interrupt modes are less capable! */
2508 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2509 interrupt_mode);
2510
Ben Hutchings6977dc62008-12-26 13:44:39 -08002511 /* Would be good to use the net_dev name, but we're too early */
2512 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2513 pci_name(pci_dev));
2514 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002515 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002516 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002517
Ben Hutchings8ceee662008-04-27 12:55:59 +01002518 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002519
2520fail:
2521 efx_fini_struct(efx);
2522 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002523}
2524
2525static void efx_fini_struct(struct efx_nic *efx)
2526{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002527 int i;
2528
2529 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2530 kfree(efx->channel[i]);
2531
Ben Hutchings8ceee662008-04-27 12:55:59 +01002532 if (efx->workqueue) {
2533 destroy_workqueue(efx->workqueue);
2534 efx->workqueue = NULL;
2535 }
2536}
2537
2538/**************************************************************************
2539 *
2540 * PCI interface
2541 *
2542 **************************************************************************/
2543
2544/* Main body of final NIC shutdown code
2545 * This is called only at module unload (or hotplug removal).
2546 */
2547static void efx_pci_remove_main(struct efx_nic *efx)
2548{
Ben Hutchings7153f622012-07-27 20:50:52 +01002549 /* Flush reset_work. It can no longer be scheduled since we
2550 * are not READY.
2551 */
2552 BUG_ON(efx->state == STATE_READY);
2553 cancel_work_sync(&efx->reset_work);
2554
Ben Hutchingsd8291182012-10-05 23:35:41 +01002555 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002556 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002557 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002558 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002559 efx_fini_napi(efx);
2560 efx_remove_all(efx);
2561}
2562
2563/* Final NIC shutdown
2564 * This is called only at module unload (or hotplug removal).
2565 */
2566static void efx_pci_remove(struct pci_dev *pci_dev)
2567{
2568 struct efx_nic *efx;
2569
2570 efx = pci_get_drvdata(pci_dev);
2571 if (!efx)
2572 return;
2573
2574 /* Mark the NIC as fini, then stop the interface */
2575 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002576 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002577 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002578 rtnl_unlock();
2579
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002580 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002581 efx_unregister_netdev(efx);
2582
Ben Hutchings7dde5962008-12-12 22:09:38 -08002583 efx_mtd_remove(efx);
2584
Ben Hutchings8ceee662008-04-27 12:55:59 +01002585 efx_pci_remove_main(efx);
2586
Ben Hutchings8ceee662008-04-27 12:55:59 +01002587 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002588 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002589
Ben Hutchings8ceee662008-04-27 12:55:59 +01002590 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002591 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002592 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002593
2594 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002595};
2596
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002597/* NIC VPD information
2598 * Called during probe to display the part number of the
2599 * installed NIC. VPD is potentially very large but this should
2600 * always appear within the first 512 bytes.
2601 */
2602#define SFC_VPD_LEN 512
2603static void efx_print_product_vpd(struct efx_nic *efx)
2604{
2605 struct pci_dev *dev = efx->pci_dev;
2606 char vpd_data[SFC_VPD_LEN];
2607 ssize_t vpd_size;
2608 int i, j;
2609
2610 /* Get the vpd data from the device */
2611 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2612 if (vpd_size <= 0) {
2613 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2614 return;
2615 }
2616
2617 /* Get the Read only section */
2618 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2619 if (i < 0) {
2620 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2621 return;
2622 }
2623
2624 j = pci_vpd_lrdt_size(&vpd_data[i]);
2625 i += PCI_VPD_LRDT_TAG_SIZE;
2626 if (i + j > vpd_size)
2627 j = vpd_size - i;
2628
2629 /* Get the Part number */
2630 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2631 if (i < 0) {
2632 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2633 return;
2634 }
2635
2636 j = pci_vpd_info_field_size(&vpd_data[i]);
2637 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2638 if (i + j > vpd_size) {
2639 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2640 return;
2641 }
2642
2643 netif_info(efx, drv, efx->net_dev,
2644 "Part Number : %.*s\n", j, &vpd_data[i]);
2645}
2646
2647
Ben Hutchings8ceee662008-04-27 12:55:59 +01002648/* Main body of NIC initialisation
2649 * This is called at module load (or hotplug insertion, theoretically).
2650 */
2651static int efx_pci_probe_main(struct efx_nic *efx)
2652{
2653 int rc;
2654
2655 /* Do start-of-day initialisation */
2656 rc = efx_probe_all(efx);
2657 if (rc)
2658 goto fail1;
2659
Ben Hutchingse8f14992010-12-07 19:47:34 +00002660 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002661
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002662 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002663 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002664 netif_err(efx, probe, efx->net_dev,
2665 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002666 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002667 }
2668
2669 rc = efx_init_port(efx);
2670 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002671 netif_err(efx, probe, efx->net_dev,
2672 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002673 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002674 }
2675
Ben Hutchings152b6a62009-11-29 03:43:56 +00002676 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002677 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002678 goto fail5;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002679 efx_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002680
2681 return 0;
2682
Ben Hutchings278c0622009-11-23 16:05:12 +00002683 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002684 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002685 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002686 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002687 fail3:
2688 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002689 efx_remove_all(efx);
2690 fail1:
2691 return rc;
2692}
2693
2694/* NIC initialisation
2695 *
2696 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002697 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002698 * sets up and registers the network devices with the kernel and hooks
2699 * the interrupt service routine. It does not prepare the device for
2700 * transmission; this is left to the first time one of the network
2701 * interfaces is brought up (i.e. efx_net_open).
2702 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002703static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002704 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002705{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002706 struct net_device *net_dev;
2707 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002708 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002709
2710 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002711 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2712 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002713 if (!net_dev)
2714 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002715 efx = netdev_priv(net_dev);
2716 efx->type = (const struct efx_nic_type *) entry->driver_data;
2717 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002718 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002719 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002720 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002721 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002722 /* Mask for features that also apply to VLAN devices */
2723 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002724 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2725 NETIF_F_RXCSUM);
2726 /* All offloads can be toggled */
2727 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002728 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002729 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002730 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002731 if (rc)
2732 goto fail1;
2733
Ben Hutchings62776d02010-06-23 11:30:07 +00002734 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002735 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002736
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002737 efx_print_product_vpd(efx);
2738
Ben Hutchings8ceee662008-04-27 12:55:59 +01002739 /* Set up basic I/O (BAR mappings etc) */
2740 rc = efx_init_io(efx);
2741 if (rc)
2742 goto fail2;
2743
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002744 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002745 if (rc)
2746 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002747
Ben Hutchings8ceee662008-04-27 12:55:59 +01002748 rc = efx_register_netdev(efx);
2749 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002750 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002751
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002752 rc = efx_sriov_init(efx);
2753 if (rc)
2754 netif_err(efx, probe, efx->net_dev,
2755 "SR-IOV can't be enabled rc %d\n", rc);
2756
Ben Hutchings62776d02010-06-23 11:30:07 +00002757 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002758
Ben Hutchings7c431612012-01-27 17:23:58 +00002759 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002760 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002761 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002762 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002763 if (rc)
2764 netif_warn(efx, probe, efx->net_dev,
2765 "failed to create MTDs (%d)\n", rc);
2766
Alexandre Rames626950d2013-01-14 17:20:22 +00002767 rc = pci_enable_pcie_error_reporting(pci_dev);
2768 if (rc && rc != -EINVAL)
2769 netif_warn(efx, probe, efx->net_dev,
2770 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2771
Ben Hutchings8ceee662008-04-27 12:55:59 +01002772 return 0;
2773
Ben Hutchings8ceee662008-04-27 12:55:59 +01002774 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002775 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002776 fail3:
2777 efx_fini_io(efx);
2778 fail2:
2779 efx_fini_struct(efx);
2780 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002781 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002782 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002783 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002784 free_netdev(net_dev);
2785 return rc;
2786}
2787
Ben Hutchings89c758f2009-11-29 03:43:07 +00002788static int efx_pm_freeze(struct device *dev)
2789{
2790 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2791
Ben Hutchings61da0262012-07-27 19:35:39 +01002792 rtnl_lock();
2793
Ben Hutchings6032fb52012-07-27 19:35:47 +01002794 if (efx->state != STATE_DISABLED) {
2795 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002796
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002797 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002798
Ben Hutchings6032fb52012-07-27 19:35:47 +01002799 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002800 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01002801 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002802
Ben Hutchings61da0262012-07-27 19:35:39 +01002803 rtnl_unlock();
2804
Ben Hutchings89c758f2009-11-29 03:43:07 +00002805 return 0;
2806}
2807
2808static int efx_pm_thaw(struct device *dev)
2809{
2810 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2811
Ben Hutchings61da0262012-07-27 19:35:39 +01002812 rtnl_lock();
2813
Ben Hutchings6032fb52012-07-27 19:35:47 +01002814 if (efx->state != STATE_DISABLED) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01002815 efx_enable_interrupts(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002816
Ben Hutchings6032fb52012-07-27 19:35:47 +01002817 mutex_lock(&efx->mac_lock);
2818 efx->phy_op->reconfigure(efx);
2819 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002820
Ben Hutchings6032fb52012-07-27 19:35:47 +01002821 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002822
Ben Hutchings6032fb52012-07-27 19:35:47 +01002823 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002824
Ben Hutchings6032fb52012-07-27 19:35:47 +01002825 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002826
Ben Hutchings6032fb52012-07-27 19:35:47 +01002827 efx->type->resume_wol(efx);
2828 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002829
Ben Hutchings61da0262012-07-27 19:35:39 +01002830 rtnl_unlock();
2831
Steve Hodgson319ba642010-06-01 11:17:24 +00002832 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2833 queue_work(reset_workqueue, &efx->reset_work);
2834
Ben Hutchings89c758f2009-11-29 03:43:07 +00002835 return 0;
2836}
2837
2838static int efx_pm_poweroff(struct device *dev)
2839{
2840 struct pci_dev *pci_dev = to_pci_dev(dev);
2841 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2842
2843 efx->type->fini(efx);
2844
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002845 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002846
2847 pci_save_state(pci_dev);
2848 return pci_set_power_state(pci_dev, PCI_D3hot);
2849}
2850
2851/* Used for both resume and restore */
2852static int efx_pm_resume(struct device *dev)
2853{
2854 struct pci_dev *pci_dev = to_pci_dev(dev);
2855 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2856 int rc;
2857
2858 rc = pci_set_power_state(pci_dev, PCI_D0);
2859 if (rc)
2860 return rc;
2861 pci_restore_state(pci_dev);
2862 rc = pci_enable_device(pci_dev);
2863 if (rc)
2864 return rc;
2865 pci_set_master(efx->pci_dev);
2866 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2867 if (rc)
2868 return rc;
2869 rc = efx->type->init(efx);
2870 if (rc)
2871 return rc;
2872 efx_pm_thaw(dev);
2873 return 0;
2874}
2875
2876static int efx_pm_suspend(struct device *dev)
2877{
2878 int rc;
2879
2880 efx_pm_freeze(dev);
2881 rc = efx_pm_poweroff(dev);
2882 if (rc)
2883 efx_pm_resume(dev);
2884 return rc;
2885}
2886
Ben Hutchings18e83e42012-01-05 19:05:20 +00002887static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002888 .suspend = efx_pm_suspend,
2889 .resume = efx_pm_resume,
2890 .freeze = efx_pm_freeze,
2891 .thaw = efx_pm_thaw,
2892 .poweroff = efx_pm_poweroff,
2893 .restore = efx_pm_resume,
2894};
2895
Alexandre Rames626950d2013-01-14 17:20:22 +00002896/* A PCI error affecting this device was detected.
2897 * At this point MMIO and DMA may be disabled.
2898 * Stop the software path and request a slot reset.
2899 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002900static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2901 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00002902{
2903 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2904 struct efx_nic *efx = pci_get_drvdata(pdev);
2905
2906 if (state == pci_channel_io_perm_failure)
2907 return PCI_ERS_RESULT_DISCONNECT;
2908
2909 rtnl_lock();
2910
2911 if (efx->state != STATE_DISABLED) {
2912 efx->state = STATE_RECOVERY;
2913 efx->reset_pending = 0;
2914
2915 efx_device_detach_sync(efx);
2916
2917 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002918 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00002919
2920 status = PCI_ERS_RESULT_NEED_RESET;
2921 } else {
2922 /* If the interface is disabled we don't want to do anything
2923 * with it.
2924 */
2925 status = PCI_ERS_RESULT_RECOVERED;
2926 }
2927
2928 rtnl_unlock();
2929
2930 pci_disable_device(pdev);
2931
2932 return status;
2933}
2934
2935/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002936static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00002937{
2938 struct efx_nic *efx = pci_get_drvdata(pdev);
2939 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2940 int rc;
2941
2942 if (pci_enable_device(pdev)) {
2943 netif_err(efx, hw, efx->net_dev,
2944 "Cannot re-enable PCI device after reset.\n");
2945 status = PCI_ERS_RESULT_DISCONNECT;
2946 }
2947
2948 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2949 if (rc) {
2950 netif_err(efx, hw, efx->net_dev,
2951 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2952 /* Non-fatal error. Continue. */
2953 }
2954
2955 return status;
2956}
2957
2958/* Perform the actual reset and resume I/O operations. */
2959static void efx_io_resume(struct pci_dev *pdev)
2960{
2961 struct efx_nic *efx = pci_get_drvdata(pdev);
2962 int rc;
2963
2964 rtnl_lock();
2965
2966 if (efx->state == STATE_DISABLED)
2967 goto out;
2968
2969 rc = efx_reset(efx, RESET_TYPE_ALL);
2970 if (rc) {
2971 netif_err(efx, hw, efx->net_dev,
2972 "efx_reset failed after PCI error (%d)\n", rc);
2973 } else {
2974 efx->state = STATE_READY;
2975 netif_dbg(efx, hw, efx->net_dev,
2976 "Done resetting and resuming IO after PCI error.\n");
2977 }
2978
2979out:
2980 rtnl_unlock();
2981}
2982
2983/* For simplicity and reliability, we always require a slot reset and try to
2984 * reset the hardware when a pci error affecting the device is detected.
2985 * We leave both the link_reset and mmio_enabled callback unimplemented:
2986 * with our request for slot reset the mmio_enabled callback will never be
2987 * called, and the link_reset callback is not used by AER or EEH mechanisms.
2988 */
2989static struct pci_error_handlers efx_err_handlers = {
2990 .error_detected = efx_io_error_detected,
2991 .slot_reset = efx_io_slot_reset,
2992 .resume = efx_io_resume,
2993};
2994
Ben Hutchings8ceee662008-04-27 12:55:59 +01002995static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00002996 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002997 .id_table = efx_pci_table,
2998 .probe = efx_pci_probe,
2999 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003000 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003001 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003002};
3003
3004/**************************************************************************
3005 *
3006 * Kernel module interface
3007 *
3008 *************************************************************************/
3009
3010module_param(interrupt_mode, uint, 0444);
3011MODULE_PARM_DESC(interrupt_mode,
3012 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3013
3014static int __init efx_init_module(void)
3015{
3016 int rc;
3017
3018 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3019
3020 rc = register_netdevice_notifier(&efx_netdev_notifier);
3021 if (rc)
3022 goto err_notifier;
3023
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003024 rc = efx_init_sriov();
3025 if (rc)
3026 goto err_sriov;
3027
Steve Hodgson1ab00622008-12-12 21:33:02 -08003028 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3029 if (!reset_workqueue) {
3030 rc = -ENOMEM;
3031 goto err_reset;
3032 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003033
3034 rc = pci_register_driver(&efx_pci_driver);
3035 if (rc < 0)
3036 goto err_pci;
3037
3038 return 0;
3039
3040 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003041 destroy_workqueue(reset_workqueue);
3042 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003043 efx_fini_sriov();
3044 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003045 unregister_netdevice_notifier(&efx_netdev_notifier);
3046 err_notifier:
3047 return rc;
3048}
3049
3050static void __exit efx_exit_module(void)
3051{
3052 printk(KERN_INFO "Solarflare NET driver unloading\n");
3053
3054 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003055 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003056 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003057 unregister_netdevice_notifier(&efx_netdev_notifier);
3058
3059}
3060
3061module_init(efx_init_module);
3062module_exit(efx_exit_module);
3063
Ben Hutchings906bb262009-11-29 15:16:19 +00003064MODULE_AUTHOR("Solarflare Communications and "
3065 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003066MODULE_DESCRIPTION("Solarflare Communications network driver");
3067MODULE_LICENSE("GPL");
3068MODULE_DEVICE_TABLE(pci, efx_pci_table);