blob: 49d06ca79d7d1e69b8738549541900b7ad53978c [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 Hutchingsbe3fc092012-10-08 18:21:51 +0100342 channel->eventq_init = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100343}
344
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000345/* Enable event queue processing and NAPI */
346static void efx_start_eventq(struct efx_channel *channel)
347{
348 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
349 "chan %d start event queue\n", channel->channel);
350
Ben Hutchings514bedb2012-10-05 19:30:16 +0100351 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000352 channel->enabled = true;
353 smp_wmb();
354
355 napi_enable(&channel->napi_str);
356 efx_nic_eventq_read_ack(channel);
357}
358
359/* Disable event queue processing and NAPI */
360static void efx_stop_eventq(struct efx_channel *channel)
361{
362 if (!channel->enabled)
363 return;
364
365 napi_disable(&channel->napi_str);
366 channel->enabled = false;
367}
368
Ben Hutchings8ceee662008-04-27 12:55:59 +0100369static void efx_fini_eventq(struct efx_channel *channel)
370{
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100371 if (!channel->eventq_init)
372 return;
373
Ben Hutchings62776d02010-06-23 11:30:07 +0000374 netif_dbg(channel->efx, drv, channel->efx->net_dev,
375 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100376
Ben Hutchings152b6a62009-11-29 03:43:56 +0000377 efx_nic_fini_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100378 channel->eventq_init = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100379}
380
381static void efx_remove_eventq(struct efx_channel *channel)
382{
Ben Hutchings62776d02010-06-23 11:30:07 +0000383 netif_dbg(channel->efx, drv, channel->efx->net_dev,
384 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100385
Ben Hutchings152b6a62009-11-29 03:43:56 +0000386 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100387}
388
389/**************************************************************************
390 *
391 * Channel handling
392 *
393 *************************************************************************/
394
Ben Hutchings7f967c02012-02-13 23:45:02 +0000395/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000396static struct efx_channel *
397efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
398{
399 struct efx_channel *channel;
400 struct efx_rx_queue *rx_queue;
401 struct efx_tx_queue *tx_queue;
402 int j;
403
Ben Hutchings7f967c02012-02-13 23:45:02 +0000404 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
405 if (!channel)
406 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000407
Ben Hutchings7f967c02012-02-13 23:45:02 +0000408 channel->efx = efx;
409 channel->channel = i;
410 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000411
Ben Hutchings7f967c02012-02-13 23:45:02 +0000412 for (j = 0; j < EFX_TXQ_TYPES; j++) {
413 tx_queue = &channel->tx_queue[j];
414 tx_queue->efx = efx;
415 tx_queue->queue = i * EFX_TXQ_TYPES + j;
416 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000417 }
418
Ben Hutchings46426102010-09-10 06:42:33 +0000419 rx_queue = &channel->rx_queue;
420 rx_queue->efx = efx;
421 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
422 (unsigned long)rx_queue);
423
424 return channel;
425}
426
Ben Hutchings7f967c02012-02-13 23:45:02 +0000427/* Allocate and initialise a channel structure, copying parameters
428 * (but not resources) from an old channel structure.
429 */
430static struct efx_channel *
431efx_copy_channel(const struct efx_channel *old_channel)
432{
433 struct efx_channel *channel;
434 struct efx_rx_queue *rx_queue;
435 struct efx_tx_queue *tx_queue;
436 int j;
437
438 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
439 if (!channel)
440 return NULL;
441
442 *channel = *old_channel;
443
444 channel->napi_dev = NULL;
445 memset(&channel->eventq, 0, sizeof(channel->eventq));
446
447 for (j = 0; j < EFX_TXQ_TYPES; j++) {
448 tx_queue = &channel->tx_queue[j];
449 if (tx_queue->channel)
450 tx_queue->channel = channel;
451 tx_queue->buffer = NULL;
452 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
453 }
454
455 rx_queue = &channel->rx_queue;
456 rx_queue->buffer = NULL;
457 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
458 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
459 (unsigned long)rx_queue);
460
461 return channel;
462}
463
Ben Hutchings8ceee662008-04-27 12:55:59 +0100464static int efx_probe_channel(struct efx_channel *channel)
465{
466 struct efx_tx_queue *tx_queue;
467 struct efx_rx_queue *rx_queue;
468 int rc;
469
Ben Hutchings62776d02010-06-23 11:30:07 +0000470 netif_dbg(channel->efx, probe, channel->efx->net_dev,
471 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100472
Ben Hutchings7f967c02012-02-13 23:45:02 +0000473 rc = channel->type->pre_probe(channel);
474 if (rc)
475 goto fail;
476
Ben Hutchings8ceee662008-04-27 12:55:59 +0100477 rc = efx_probe_eventq(channel);
478 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000479 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100480
481 efx_for_each_channel_tx_queue(tx_queue, channel) {
482 rc = efx_probe_tx_queue(tx_queue);
483 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000484 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100485 }
486
487 efx_for_each_channel_rx_queue(rx_queue, channel) {
488 rc = efx_probe_rx_queue(rx_queue);
489 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000490 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100491 }
492
493 channel->n_rx_frm_trunc = 0;
494
495 return 0;
496
Ben Hutchings7f967c02012-02-13 23:45:02 +0000497fail:
498 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100499 return rc;
500}
501
Ben Hutchings7f967c02012-02-13 23:45:02 +0000502static void
503efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
504{
505 struct efx_nic *efx = channel->efx;
506 const char *type;
507 int number;
508
509 number = channel->channel;
510 if (efx->tx_channel_offset == 0) {
511 type = "";
512 } else if (channel->channel < efx->tx_channel_offset) {
513 type = "-rx";
514 } else {
515 type = "-tx";
516 number -= efx->tx_channel_offset;
517 }
518 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
519}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100520
Ben Hutchings56536e92008-12-12 21:37:02 -0800521static void efx_set_channel_names(struct efx_nic *efx)
522{
523 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800524
Ben Hutchings7f967c02012-02-13 23:45:02 +0000525 efx_for_each_channel(channel, efx)
526 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100527 efx->msi_context[channel->channel].name,
528 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800529}
530
Ben Hutchings46426102010-09-10 06:42:33 +0000531static int efx_probe_channels(struct efx_nic *efx)
532{
533 struct efx_channel *channel;
534 int rc;
535
536 /* Restart special buffer allocation */
537 efx->next_buffer_table = 0;
538
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000539 /* Probe channels in reverse, so that any 'extra' channels
540 * use the start of the buffer table. This allows the traffic
541 * channels to be resized without moving them or wasting the
542 * entries before them.
543 */
544 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000545 rc = efx_probe_channel(channel);
546 if (rc) {
547 netif_err(efx, probe, efx->net_dev,
548 "failed to create channel %d\n",
549 channel->channel);
550 goto fail;
551 }
552 }
553 efx_set_channel_names(efx);
554
555 return 0;
556
557fail:
558 efx_remove_channels(efx);
559 return rc;
560}
561
Ben Hutchings8ceee662008-04-27 12:55:59 +0100562/* Channels are shutdown and reinitialised whilst the NIC is running
563 * to propagate configuration changes (mtu, checksum offload), or
564 * to clear hardware error conditions
565 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000566static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100567{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000568 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100569 struct efx_tx_queue *tx_queue;
570 struct efx_rx_queue *rx_queue;
571 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000572 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100573
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100574 /* Calculate the rx buffer allocation parameters required to
575 * support the current MTU, including padding for header
576 * alignment and overruns.
577 */
Ben Hutchings272baee2013-01-29 23:33:14 +0000578 efx->rx_dma_len = (efx->type->rx_buffer_hash_size +
579 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
580 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000581 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Ben Hutchingsc14ff2e2013-05-13 11:58:31 +0000582 NET_IP_ALIGN + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000583 if (rx_buf_len <= PAGE_SIZE) {
584 efx->rx_scatter = false;
585 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000586 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000587 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000588 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000589 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
590 EFX_RX_BUF_ALIGNMENT) >
591 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000592 efx->rx_scatter = true;
593 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
594 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000595 } else {
596 efx->rx_scatter = false;
597 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000598 }
599
Daniel Pieczko1648a232013-02-13 10:54:41 +0000600 efx_rx_config_page_split(efx);
601 if (efx->rx_buffer_order)
602 netif_dbg(efx, drv, efx->net_dev,
603 "RX buf len=%u; page order=%u batch=%u\n",
604 efx->rx_dma_len, efx->rx_buffer_order,
605 efx->rx_pages_per_batch);
606 else
607 netif_dbg(efx, drv, efx->net_dev,
608 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
609 efx->rx_dma_len, efx->rx_page_buf_step,
610 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000611
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000612 /* RX filters also have scatter-enabled flags */
613 if (efx->rx_scatter != old_rx_scatter)
614 efx_filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100615
Ben Hutchings14bf7182012-05-22 01:27:58 +0100616 /* We must keep at least one descriptor in a TX ring empty.
617 * We could avoid this when the queue size does not exactly
618 * match the hardware ring size, but it's not that important.
619 * Therefore we stop the queue when one more skb might fill
620 * the ring completely. We wake it when half way back to
621 * empty.
622 */
623 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
624 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
625
Ben Hutchings8ceee662008-04-27 12:55:59 +0100626 /* Initialise the channels */
627 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100628 efx_for_each_channel_tx_queue(tx_queue, channel)
629 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100630
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000631 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100632 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000633 efx_nic_generate_fill_event(rx_queue);
634 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100635
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000636 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100637 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000638
639 if (netif_device_present(efx->net_dev))
640 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100641}
642
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000643static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100644{
645 struct efx_channel *channel;
646 struct efx_tx_queue *tx_queue;
647 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100648 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100649
650 EFX_ASSERT_RESET_SERIALISED(efx);
651 BUG_ON(efx->port_enabled);
652
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100653 /* Stop RX refill */
654 efx_for_each_channel(channel, efx) {
655 efx_for_each_channel_rx_queue(rx_queue, channel)
656 rx_queue->refill_enabled = false;
657 }
658
Ben Hutchings8ceee662008-04-27 12:55:59 +0100659 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000660 /* RX packet processing is pipelined, so wait for the
661 * NAPI handler to complete. At least event queue 0
662 * might be kept active by non-data events, so don't
663 * use napi_synchronize() but actually disable NAPI
664 * temporarily.
665 */
666 if (efx_channel_has_rx_queue(channel)) {
667 efx_stop_eventq(channel);
668 efx_start_eventq(channel);
669 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100670 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100671
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100672 rc = efx->type->fini_dmaq(efx);
673 if (rc && EFX_WORKAROUND_7803(efx)) {
674 /* Schedule a reset to recover from the flush failure. The
675 * descriptor caches reference memory we're about to free,
676 * but falcon_reconfigure_mac_wrapper() won't reconnect
677 * the MACs because of the pending reset.
678 */
679 netif_err(efx, drv, efx->net_dev,
680 "Resetting to recover from flush failure\n");
681 efx_schedule_reset(efx, RESET_TYPE_ALL);
682 } else if (rc) {
683 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
684 } else {
685 netif_dbg(efx, drv, efx->net_dev,
686 "successfully flushed all queues\n");
687 }
688
689 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100690 efx_for_each_channel_rx_queue(rx_queue, channel)
691 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000692 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100693 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100694 }
695}
696
697static void efx_remove_channel(struct efx_channel *channel)
698{
699 struct efx_tx_queue *tx_queue;
700 struct efx_rx_queue *rx_queue;
701
Ben Hutchings62776d02010-06-23 11:30:07 +0000702 netif_dbg(channel->efx, drv, channel->efx->net_dev,
703 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100704
705 efx_for_each_channel_rx_queue(rx_queue, channel)
706 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000707 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100708 efx_remove_tx_queue(tx_queue);
709 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100710 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100711}
712
Ben Hutchings46426102010-09-10 06:42:33 +0000713static void efx_remove_channels(struct efx_nic *efx)
714{
715 struct efx_channel *channel;
716
717 efx_for_each_channel(channel, efx)
718 efx_remove_channel(channel);
719}
720
721int
722efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
723{
724 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
725 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000726 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100727 int rc;
728
729 rc = efx_check_disabled(efx);
730 if (rc)
731 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000732
733 /* Not all channels should be reallocated. We must avoid
734 * reallocating their buffer table entries.
735 */
736 efx_for_each_channel(channel, efx) {
737 struct efx_rx_queue *rx_queue;
738 struct efx_tx_queue *tx_queue;
739
740 if (channel->type->copy)
741 continue;
742 next_buffer_table = max(next_buffer_table,
743 channel->eventq.index +
744 channel->eventq.entries);
745 efx_for_each_channel_rx_queue(rx_queue, channel)
746 next_buffer_table = max(next_buffer_table,
747 rx_queue->rxd.index +
748 rx_queue->rxd.entries);
749 efx_for_each_channel_tx_queue(tx_queue, channel)
750 next_buffer_table = max(next_buffer_table,
751 tx_queue->txd.index +
752 tx_queue->txd.entries);
753 }
Ben Hutchings46426102010-09-10 06:42:33 +0000754
Ben Hutchings29c69a42013-01-28 19:01:06 +0000755 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000756 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100757 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000758
Ben Hutchings7f967c02012-02-13 23:45:02 +0000759 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000760 memset(other_channel, 0, sizeof(other_channel));
761 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000762 channel = efx->channel[i];
763 if (channel->type->copy)
764 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000765 if (!channel) {
766 rc = -ENOMEM;
767 goto out;
768 }
769 other_channel[i] = channel;
770 }
771
772 /* Swap entry counts and channel pointers */
773 old_rxq_entries = efx->rxq_entries;
774 old_txq_entries = efx->txq_entries;
775 efx->rxq_entries = rxq_entries;
776 efx->txq_entries = txq_entries;
777 for (i = 0; i < efx->n_channels; i++) {
778 channel = efx->channel[i];
779 efx->channel[i] = other_channel[i];
780 other_channel[i] = channel;
781 }
782
Ben Hutchings7f967c02012-02-13 23:45:02 +0000783 /* Restart buffer table allocation */
784 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000785
Ben Hutchingse8f14992010-12-07 19:47:34 +0000786 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000787 channel = efx->channel[i];
788 if (!channel->type->copy)
789 continue;
790 rc = efx_probe_channel(channel);
791 if (rc)
792 goto rollback;
793 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000794 }
Ben Hutchings46426102010-09-10 06:42:33 +0000795
Ben Hutchings7f967c02012-02-13 23:45:02 +0000796out:
797 /* Destroy unused channel structures */
798 for (i = 0; i < efx->n_channels; i++) {
799 channel = other_channel[i];
800 if (channel && channel->type->copy) {
801 efx_fini_napi_channel(channel);
802 efx_remove_channel(channel);
803 kfree(channel);
804 }
805 }
806
Ben Hutchingsd8291182012-10-05 23:35:41 +0100807 efx_soft_enable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000808 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000809 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000810 return rc;
811
812rollback:
813 /* Swap back */
814 efx->rxq_entries = old_rxq_entries;
815 efx->txq_entries = old_txq_entries;
816 for (i = 0; i < efx->n_channels; i++) {
817 channel = efx->channel[i];
818 efx->channel[i] = other_channel[i];
819 other_channel[i] = channel;
820 }
821 goto out;
822}
823
Steve Hodgson90d683a2010-06-01 11:19:39 +0000824void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100825{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000826 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100827}
828
Ben Hutchings7f967c02012-02-13 23:45:02 +0000829static const struct efx_channel_type efx_default_channel_type = {
830 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100831 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000832 .get_name = efx_get_channel_name,
833 .copy = efx_copy_channel,
834 .keep_eventq = false,
835};
836
837int efx_channel_dummy_op_int(struct efx_channel *channel)
838{
839 return 0;
840}
841
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100842void efx_channel_dummy_op_void(struct efx_channel *channel)
843{
844}
845
Ben Hutchings8ceee662008-04-27 12:55:59 +0100846/**************************************************************************
847 *
848 * Port handling
849 *
850 **************************************************************************/
851
852/* This ensures that the kernel is kept informed (via
853 * netif_carrier_on/off) of the link status, and also maintains the
854 * link status's stop on the port's TX queue.
855 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000856void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100857{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000858 struct efx_link_state *link_state = &efx->link_state;
859
Ben Hutchings8ceee662008-04-27 12:55:59 +0100860 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
861 * that no events are triggered between unregister_netdev() and the
862 * driver unloading. A more general condition is that NETDEV_CHANGE
863 * can only be generated between NETDEV_UP and NETDEV_DOWN */
864 if (!netif_running(efx->net_dev))
865 return;
866
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000867 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100868 efx->n_link_state_changes++;
869
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000870 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100871 netif_carrier_on(efx->net_dev);
872 else
873 netif_carrier_off(efx->net_dev);
874 }
875
876 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000877 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000878 netif_info(efx, link, efx->net_dev,
879 "link up at %uMbps %s-duplex (MTU %d)%s\n",
880 link_state->speed, link_state->fd ? "full" : "half",
881 efx->net_dev->mtu,
882 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000883 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000884 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100885}
886
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000887void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
888{
889 efx->link_advertising = advertising;
890 if (advertising) {
891 if (advertising & ADVERTISED_Pause)
892 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
893 else
894 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
895 if (advertising & ADVERTISED_Asym_Pause)
896 efx->wanted_fc ^= EFX_FC_TX;
897 }
898}
899
David S. Millerb56269462011-05-17 17:53:22 -0400900void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000901{
902 efx->wanted_fc = wanted_fc;
903 if (efx->link_advertising) {
904 if (wanted_fc & EFX_FC_RX)
905 efx->link_advertising |= (ADVERTISED_Pause |
906 ADVERTISED_Asym_Pause);
907 else
908 efx->link_advertising &= ~(ADVERTISED_Pause |
909 ADVERTISED_Asym_Pause);
910 if (wanted_fc & EFX_FC_TX)
911 efx->link_advertising ^= ADVERTISED_Asym_Pause;
912 }
913}
914
Ben Hutchings115122a2009-03-04 09:52:52 +0000915static void efx_fini_port(struct efx_nic *efx);
916
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000917/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
918 * the MAC appropriately. All other PHY configuration changes are pushed
919 * through phy_op->set_settings(), and pushed asynchronously to the MAC
920 * through efx_monitor().
921 *
922 * Callers must hold the mac_lock
923 */
924int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100925{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000926 enum efx_phy_mode phy_mode;
927 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100928
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000929 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100930
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000931 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000932 netif_addr_lock_bh(efx->net_dev);
933 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100934
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000935 /* Disable PHY transmit in mac level loopbacks */
936 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800937 if (LOOPBACK_INTERNAL(efx))
938 efx->phy_mode |= PHY_MODE_TX_DISABLED;
939 else
940 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800941
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000942 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800943
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000944 if (rc)
945 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100946
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000947 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100948}
949
950/* Reinitialise the MAC to pick up new PHY settings, even if the port is
951 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000952int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100953{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000954 int rc;
955
Ben Hutchings8ceee662008-04-27 12:55:59 +0100956 EFX_ASSERT_RESET_SERIALISED(efx);
957
958 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000959 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100960 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000961
962 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100963}
964
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000965/* Asynchronous work item for changing MAC promiscuity and multicast
966 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
967 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800968static void efx_mac_work(struct work_struct *data)
969{
970 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
971
972 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100973 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100974 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800975 mutex_unlock(&efx->mac_lock);
976}
977
Ben Hutchings8ceee662008-04-27 12:55:59 +0100978static int efx_probe_port(struct efx_nic *efx)
979{
980 int rc;
981
Ben Hutchings62776d02010-06-23 11:30:07 +0000982 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100983
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000984 if (phy_flash_cfg)
985 efx->phy_mode = PHY_MODE_SPECIAL;
986
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000987 /* Connect up MAC/PHY operations table */
988 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100989 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000990 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100991
Ben Hutchingse332bcb2011-12-20 01:22:51 +0000992 /* Initialise MAC address to permanent address */
993 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100994
995 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100996}
997
998static int efx_init_port(struct efx_nic *efx)
999{
1000 int rc;
1001
Ben Hutchings62776d02010-06-23 11:30:07 +00001002 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001003
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001004 mutex_lock(&efx->mac_lock);
1005
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001006 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001007 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001008 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001009
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001010 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001011
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001012 /* Reconfigure the MAC before creating dma queues (required for
1013 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001014 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001015
1016 /* Ensure the PHY advertises the correct flow control settings */
1017 rc = efx->phy_op->reconfigure(efx);
1018 if (rc)
1019 goto fail2;
1020
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001021 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001022 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001023
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001024fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001025 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001026fail1:
1027 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001028 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001029}
1030
Ben Hutchings8ceee662008-04-27 12:55:59 +01001031static void efx_start_port(struct efx_nic *efx)
1032{
Ben Hutchings62776d02010-06-23 11:30:07 +00001033 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001034 BUG_ON(efx->port_enabled);
1035
1036 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001037 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001038
1039 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1040 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001041 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001042
Ben Hutchings8ceee662008-04-27 12:55:59 +01001043 mutex_unlock(&efx->mac_lock);
1044}
1045
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001046/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001047static void efx_stop_port(struct efx_nic *efx)
1048{
Ben Hutchings62776d02010-06-23 11:30:07 +00001049 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001050
1051 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001052 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001053 mutex_unlock(&efx->mac_lock);
1054
1055 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001056 netif_addr_lock_bh(efx->net_dev);
1057 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001058}
1059
1060static void efx_fini_port(struct efx_nic *efx)
1061{
Ben Hutchings62776d02010-06-23 11:30:07 +00001062 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001063
1064 if (!efx->port_initialized)
1065 return;
1066
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001067 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001068 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001069
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001070 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001071 efx_link_status_changed(efx);
1072}
1073
1074static void efx_remove_port(struct efx_nic *efx)
1075{
Ben Hutchings62776d02010-06-23 11:30:07 +00001076 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001077
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001078 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001079}
1080
1081/**************************************************************************
1082 *
1083 * NIC handling
1084 *
1085 **************************************************************************/
1086
1087/* This configures the PCI device to enable I/O and DMA. */
1088static int efx_init_io(struct efx_nic *efx)
1089{
1090 struct pci_dev *pci_dev = efx->pci_dev;
1091 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001092 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001093 int rc;
1094
Ben Hutchings62776d02010-06-23 11:30:07 +00001095 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001096
1097 rc = pci_enable_device(pci_dev);
1098 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001099 netif_err(efx, probe, efx->net_dev,
1100 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001101 goto fail1;
1102 }
1103
1104 pci_set_master(pci_dev);
1105
1106 /* Set the PCI DMA mask. Try all possibilities from our
1107 * genuine mask down to 32 bits, because some architectures
1108 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1109 * masks event though they reject 46 bit masks.
1110 */
1111 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001112 if (dma_supported(&pci_dev->dev, dma_mask)) {
1113 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001114 if (rc == 0)
1115 break;
1116 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001117 dma_mask >>= 1;
1118 }
1119 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001120 netif_err(efx, probe, efx->net_dev,
1121 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001122 goto fail2;
1123 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001124 netif_dbg(efx, probe, efx->net_dev,
1125 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001126 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001127 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001128 /* dma_set_coherent_mask() is not *allowed* to
1129 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001130 * but just in case...
1131 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001132 netif_err(efx, probe, efx->net_dev,
1133 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001134 goto fail2;
1135 }
1136
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001137 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1138 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001139 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001140 netif_err(efx, probe, efx->net_dev,
1141 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001142 rc = -EIO;
1143 goto fail3;
1144 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001145 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001146 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001147 netif_err(efx, probe, efx->net_dev,
1148 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001149 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001150 rc = -ENOMEM;
1151 goto fail4;
1152 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001153 netif_dbg(efx, probe, efx->net_dev,
1154 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001155 (unsigned long long)efx->membase_phys, mem_map_size,
1156 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001157
1158 return 0;
1159
1160 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001161 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001162 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001163 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001164 fail2:
1165 pci_disable_device(efx->pci_dev);
1166 fail1:
1167 return rc;
1168}
1169
1170static void efx_fini_io(struct efx_nic *efx)
1171{
Ben Hutchings62776d02010-06-23 11:30:07 +00001172 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001173
1174 if (efx->membase) {
1175 iounmap(efx->membase);
1176 efx->membase = NULL;
1177 }
1178
1179 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001180 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001181 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001182 }
1183
1184 pci_disable_device(efx->pci_dev);
1185}
1186
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001187static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001188{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001189 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001190 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001191 int cpu;
1192
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001193 if (rss_cpus) {
1194 count = rss_cpus;
1195 } else {
1196 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1197 netif_warn(efx, probe, efx->net_dev,
1198 "RSS disabled due to allocation failure\n");
1199 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001200 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001201
1202 count = 0;
1203 for_each_online_cpu(cpu) {
1204 if (!cpumask_test_cpu(cpu, thread_mask)) {
1205 ++count;
1206 cpumask_or(thread_mask, thread_mask,
1207 topology_thread_cpumask(cpu));
1208 }
1209 }
1210
1211 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001212 }
1213
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001214 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1215 * table entries that are inaccessible to VFs
1216 */
1217 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1218 count > efx_vf_size(efx)) {
1219 netif_warn(efx, probe, efx->net_dev,
1220 "Reducing number of RSS channels from %u to %u for "
1221 "VF support. Increase vf-msix-limit to use more "
1222 "channels on the PF.\n",
1223 count, efx_vf_size(efx));
1224 count = efx_vf_size(efx);
1225 }
1226
Ben Hutchings46123d02008-09-01 12:47:33 +01001227 return count;
1228}
1229
1230/* Probe the number and type of interrupts we are able to obtain, and
1231 * the resulting numbers of channels and RX queues.
1232 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001233static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001234{
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 Hutchingsb1057982012-09-19 00:56:47 +01001251 n_channels = min(n_channels, efx->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 Hutchings86094f72013-08-21 19:51:04 +01001391 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001392
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
Ben Hutchings86094f72013-08-21 19:51:04 +01001412 efx->type->irq_disable_non_ev(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 Hutchings8ceee662008-04-27 12:55:59 +01002159 if (!efx->net_dev)
2160 return;
2161
Ben Hutchings767e4682008-09-01 12:43:14 +01002162 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002163
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002164 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2165 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002166
2167 rtnl_lock();
2168 unregister_netdevice(efx->net_dev);
2169 efx->state = STATE_UNINIT;
2170 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002171}
2172
2173/**************************************************************************
2174 *
2175 * Device reset and suspend
2176 *
2177 **************************************************************************/
2178
Ben Hutchings2467ca42008-09-01 12:48:50 +01002179/* Tears down the entire software state and most of the hardware state
2180 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002181void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002182{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002183 EFX_ASSERT_RESET_SERIALISED(efx);
2184
Ben Hutchings2467ca42008-09-01 12:48:50 +01002185 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002186 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002187
2188 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002189 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2190 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002191 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002192}
2193
Ben Hutchings2467ca42008-09-01 12:48:50 +01002194/* This function will always ensure that the locks acquired in
2195 * efx_reset_down() are released. A failure return code indicates
2196 * that we were unable to reinitialise the hardware, and the
2197 * driver should be disabled. If ok is false, then the rx and tx
2198 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002199int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002200{
2201 int rc;
2202
Ben Hutchings2467ca42008-09-01 12:48:50 +01002203 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002204
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002205 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002206 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002207 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002208 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002209 }
2210
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002211 if (!ok)
2212 goto fail;
2213
Steve Hodgson4b988282009-01-29 17:50:51 +00002214 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002215 rc = efx->phy_op->init(efx);
2216 if (rc)
2217 goto fail;
2218 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002219 netif_err(efx, drv, efx->net_dev,
2220 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002221 }
2222
Ben Hutchings710b2082011-09-03 00:15:00 +01002223 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002224
Ben Hutchingsd8291182012-10-05 23:35:41 +01002225 efx_enable_interrupts(efx);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002226 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002227 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002228
2229 mutex_unlock(&efx->mac_lock);
2230
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002231 efx_start_all(efx);
2232
2233 return 0;
2234
2235fail:
2236 efx->port_initialized = false;
2237
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002238 mutex_unlock(&efx->mac_lock);
2239
Ben Hutchings8ceee662008-04-27 12:55:59 +01002240 return rc;
2241}
2242
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002243/* Reset the NIC using the specified method. Note that the reset may
2244 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002245 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002246 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002247 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002248int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002249{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002250 int rc, rc2;
2251 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002252
Ben Hutchings62776d02010-06-23 11:30:07 +00002253 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2254 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002255
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002256 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002257 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002258
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002259 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002260 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002261 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002262 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002263 }
2264
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002265 /* Clear flags for the scopes we covered. We assume the NIC and
2266 * driver are now quiescent so that there is no race here.
2267 */
2268 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002269
2270 /* Reinitialise bus-mastering, which may have been turned off before
2271 * the reset was scheduled. This is still appropriate, even in the
2272 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2273 * can respond to requests. */
2274 pci_set_master(efx->pci_dev);
2275
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002276out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002277 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002278 disabled = rc ||
2279 method == RESET_TYPE_DISABLE ||
2280 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002281 rc2 = efx_reset_up(efx, method, !disabled);
2282 if (rc2) {
2283 disabled = true;
2284 if (!rc)
2285 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002286 }
2287
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002288 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002289 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002290 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002291 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002292 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002293 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002294 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002295 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002296 return rc;
2297}
2298
Alexandre Rames626950d2013-01-14 17:20:22 +00002299/* Try recovery mechanisms.
2300 * For now only EEH is supported.
2301 * Returns 0 if the recovery mechanisms are unsuccessful.
2302 * Returns a non-zero value otherwise.
2303 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002304int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002305{
2306#ifdef CONFIG_EEH
2307 /* A PCI error can occur and not be seen by EEH because nothing
2308 * happens on the PCI bus. In this case the driver may fail and
2309 * schedule a 'recover or reset', leading to this recovery handler.
2310 * Manually call the eeh failure check function.
2311 */
2312 struct eeh_dev *eehdev =
2313 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2314
2315 if (eeh_dev_check_failure(eehdev)) {
2316 /* The EEH mechanisms will handle the error and reset the
2317 * device if necessary.
2318 */
2319 return 1;
2320 }
2321#endif
2322 return 0;
2323}
2324
Ben Hutchings8ceee662008-04-27 12:55:59 +01002325/* The worker thread exists so that code that cannot sleep can
2326 * schedule a reset for later.
2327 */
2328static void efx_reset_work(struct work_struct *data)
2329{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002330 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002331 unsigned long pending;
2332 enum reset_type method;
2333
2334 pending = ACCESS_ONCE(efx->reset_pending);
2335 method = fls(pending) - 1;
2336
2337 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2338 method == RESET_TYPE_RECOVER_OR_ALL) &&
2339 efx_try_recovery(efx))
2340 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002341
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002342 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002343 return;
2344
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002345 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002346
2347 /* We checked the state in efx_schedule_reset() but it may
2348 * have changed by now. Now that we have the RTNL lock,
2349 * it cannot change again.
2350 */
2351 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002352 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002353
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002354 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002355}
2356
2357void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2358{
2359 enum reset_type method;
2360
Alexandre Rames626950d2013-01-14 17:20:22 +00002361 if (efx->state == STATE_RECOVERY) {
2362 netif_dbg(efx, drv, efx->net_dev,
2363 "recovering: skip scheduling %s reset\n",
2364 RESET_TYPE(type));
2365 return;
2366 }
2367
Ben Hutchings8ceee662008-04-27 12:55:59 +01002368 switch (type) {
2369 case RESET_TYPE_INVISIBLE:
2370 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002371 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002372 case RESET_TYPE_WORLD:
2373 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002374 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002375 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002376 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2377 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002378 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002379 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002380 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002381 netif_dbg(efx, drv, efx->net_dev,
2382 "scheduling %s reset for %s\n",
2383 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002384 break;
2385 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002386
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002387 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002388 smp_mb(); /* ensure we change reset_pending before checking state */
2389
2390 /* If we're not READY then just leave the flags set as the cue
2391 * to abort probing or reschedule the reset later.
2392 */
2393 if (ACCESS_ONCE(efx->state) != STATE_READY)
2394 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002395
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002396 /* efx_process_channel() will no longer read events once a
2397 * reset is scheduled. So switch back to poll'd MCDI completions. */
2398 efx_mcdi_mode_poll(efx);
2399
Steve Hodgson1ab00622008-12-12 21:33:02 -08002400 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002401}
2402
2403/**************************************************************************
2404 *
2405 * List of NICs we support
2406 *
2407 **************************************************************************/
2408
2409/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002410static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002411 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2412 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002413 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002414 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2415 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002416 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002417 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002418 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002419 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002420 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002421 {0} /* end of list */
2422};
2423
2424/**************************************************************************
2425 *
Ben Hutchings37594332009-11-23 16:05:45 +00002426 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002427 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002428 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002429 * Needed so all function pointers are valid and do not have to be tested
2430 * before use
2431 *
2432 **************************************************************************/
2433int efx_port_dummy_op_int(struct efx_nic *efx)
2434{
2435 return 0;
2436}
2437void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002438
2439static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002440{
2441 return false;
2442}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002443
stephen hemminger6c8c2512011-04-14 05:50:12 +00002444static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002445 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002446 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002447 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002448 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002449};
2450
Ben Hutchings8ceee662008-04-27 12:55:59 +01002451/**************************************************************************
2452 *
2453 * Data housekeeping
2454 *
2455 **************************************************************************/
2456
2457/* This zeroes out and then fills in the invariants in a struct
2458 * efx_nic (including all sub-structures).
2459 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002460static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002461 struct pci_dev *pci_dev, struct net_device *net_dev)
2462{
Ben Hutchings46426102010-09-10 06:42:33 +00002463 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002464
2465 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002466 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002467#ifdef CONFIG_SFC_MTD
2468 INIT_LIST_HEAD(&efx->mtd_list);
2469#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002470 INIT_WORK(&efx->reset_work, efx_reset_work);
2471 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002472 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002473 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002474 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002475 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002476 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002477
2478 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002479 spin_lock_init(&efx->stats_lock);
2480 mutex_init(&efx->mac_lock);
2481 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002482 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002483 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002484 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002485
2486 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002487 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2488 if (!efx->channel[i])
2489 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002490 efx->msi_context[i].efx = efx;
2491 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002492 }
2493
Ben Hutchings8ceee662008-04-27 12:55:59 +01002494 /* Higher numbered interrupt modes are less capable! */
2495 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2496 interrupt_mode);
2497
Ben Hutchings6977dc62008-12-26 13:44:39 -08002498 /* Would be good to use the net_dev name, but we're too early */
2499 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2500 pci_name(pci_dev));
2501 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002502 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002503 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002504
Ben Hutchings8ceee662008-04-27 12:55:59 +01002505 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002506
2507fail:
2508 efx_fini_struct(efx);
2509 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002510}
2511
2512static void efx_fini_struct(struct efx_nic *efx)
2513{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002514 int i;
2515
2516 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2517 kfree(efx->channel[i]);
2518
Ben Hutchings8ceee662008-04-27 12:55:59 +01002519 if (efx->workqueue) {
2520 destroy_workqueue(efx->workqueue);
2521 efx->workqueue = NULL;
2522 }
2523}
2524
2525/**************************************************************************
2526 *
2527 * PCI interface
2528 *
2529 **************************************************************************/
2530
2531/* Main body of final NIC shutdown code
2532 * This is called only at module unload (or hotplug removal).
2533 */
2534static void efx_pci_remove_main(struct efx_nic *efx)
2535{
Ben Hutchings7153f622012-07-27 20:50:52 +01002536 /* Flush reset_work. It can no longer be scheduled since we
2537 * are not READY.
2538 */
2539 BUG_ON(efx->state == STATE_READY);
2540 cancel_work_sync(&efx->reset_work);
2541
Ben Hutchingsd8291182012-10-05 23:35:41 +01002542 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002543 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002544 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002545 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002546 efx_fini_napi(efx);
2547 efx_remove_all(efx);
2548}
2549
2550/* Final NIC shutdown
2551 * This is called only at module unload (or hotplug removal).
2552 */
2553static void efx_pci_remove(struct pci_dev *pci_dev)
2554{
2555 struct efx_nic *efx;
2556
2557 efx = pci_get_drvdata(pci_dev);
2558 if (!efx)
2559 return;
2560
2561 /* Mark the NIC as fini, then stop the interface */
2562 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002563 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002564 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002565 rtnl_unlock();
2566
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002567 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002568 efx_unregister_netdev(efx);
2569
Ben Hutchings7dde5962008-12-12 22:09:38 -08002570 efx_mtd_remove(efx);
2571
Ben Hutchings8ceee662008-04-27 12:55:59 +01002572 efx_pci_remove_main(efx);
2573
Ben Hutchings8ceee662008-04-27 12:55:59 +01002574 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002575 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002576
Ben Hutchings8ceee662008-04-27 12:55:59 +01002577 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002578 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002579 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002580
2581 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002582};
2583
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002584/* NIC VPD information
2585 * Called during probe to display the part number of the
2586 * installed NIC. VPD is potentially very large but this should
2587 * always appear within the first 512 bytes.
2588 */
2589#define SFC_VPD_LEN 512
2590static void efx_print_product_vpd(struct efx_nic *efx)
2591{
2592 struct pci_dev *dev = efx->pci_dev;
2593 char vpd_data[SFC_VPD_LEN];
2594 ssize_t vpd_size;
2595 int i, j;
2596
2597 /* Get the vpd data from the device */
2598 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2599 if (vpd_size <= 0) {
2600 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2601 return;
2602 }
2603
2604 /* Get the Read only section */
2605 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2606 if (i < 0) {
2607 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2608 return;
2609 }
2610
2611 j = pci_vpd_lrdt_size(&vpd_data[i]);
2612 i += PCI_VPD_LRDT_TAG_SIZE;
2613 if (i + j > vpd_size)
2614 j = vpd_size - i;
2615
2616 /* Get the Part number */
2617 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2618 if (i < 0) {
2619 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2620 return;
2621 }
2622
2623 j = pci_vpd_info_field_size(&vpd_data[i]);
2624 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2625 if (i + j > vpd_size) {
2626 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2627 return;
2628 }
2629
2630 netif_info(efx, drv, efx->net_dev,
2631 "Part Number : %.*s\n", j, &vpd_data[i]);
2632}
2633
2634
Ben Hutchings8ceee662008-04-27 12:55:59 +01002635/* Main body of NIC initialisation
2636 * This is called at module load (or hotplug insertion, theoretically).
2637 */
2638static int efx_pci_probe_main(struct efx_nic *efx)
2639{
2640 int rc;
2641
2642 /* Do start-of-day initialisation */
2643 rc = efx_probe_all(efx);
2644 if (rc)
2645 goto fail1;
2646
Ben Hutchingse8f14992010-12-07 19:47:34 +00002647 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002648
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002649 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002650 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002651 netif_err(efx, probe, efx->net_dev,
2652 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002653 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002654 }
2655
2656 rc = efx_init_port(efx);
2657 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002658 netif_err(efx, probe, efx->net_dev,
2659 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002660 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002661 }
2662
Ben Hutchings152b6a62009-11-29 03:43:56 +00002663 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002664 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002665 goto fail5;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002666 efx_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002667
2668 return 0;
2669
Ben Hutchings278c0622009-11-23 16:05:12 +00002670 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002671 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002672 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002673 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002674 fail3:
2675 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002676 efx_remove_all(efx);
2677 fail1:
2678 return rc;
2679}
2680
2681/* NIC initialisation
2682 *
2683 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002684 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002685 * sets up and registers the network devices with the kernel and hooks
2686 * the interrupt service routine. It does not prepare the device for
2687 * transmission; this is left to the first time one of the network
2688 * interfaces is brought up (i.e. efx_net_open).
2689 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002690static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002691 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002692{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002693 struct net_device *net_dev;
2694 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002695 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002696
2697 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002698 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2699 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002700 if (!net_dev)
2701 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002702 efx = netdev_priv(net_dev);
2703 efx->type = (const struct efx_nic_type *) entry->driver_data;
2704 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002705 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002706 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002707 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002708 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002709 /* Mask for features that also apply to VLAN devices */
2710 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002711 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2712 NETIF_F_RXCSUM);
2713 /* All offloads can be toggled */
2714 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002715 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002716 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002717 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002718 if (rc)
2719 goto fail1;
2720
Ben Hutchings62776d02010-06-23 11:30:07 +00002721 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002722 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002723
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002724 efx_print_product_vpd(efx);
2725
Ben Hutchings8ceee662008-04-27 12:55:59 +01002726 /* Set up basic I/O (BAR mappings etc) */
2727 rc = efx_init_io(efx);
2728 if (rc)
2729 goto fail2;
2730
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002731 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002732 if (rc)
2733 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002734
Ben Hutchings8ceee662008-04-27 12:55:59 +01002735 rc = efx_register_netdev(efx);
2736 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002737 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002738
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002739 rc = efx_sriov_init(efx);
2740 if (rc)
2741 netif_err(efx, probe, efx->net_dev,
2742 "SR-IOV can't be enabled rc %d\n", rc);
2743
Ben Hutchings62776d02010-06-23 11:30:07 +00002744 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002745
Ben Hutchings7c431612012-01-27 17:23:58 +00002746 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002747 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002748 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002749 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002750 if (rc)
2751 netif_warn(efx, probe, efx->net_dev,
2752 "failed to create MTDs (%d)\n", rc);
2753
Alexandre Rames626950d2013-01-14 17:20:22 +00002754 rc = pci_enable_pcie_error_reporting(pci_dev);
2755 if (rc && rc != -EINVAL)
2756 netif_warn(efx, probe, efx->net_dev,
2757 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2758
Ben Hutchings8ceee662008-04-27 12:55:59 +01002759 return 0;
2760
Ben Hutchings8ceee662008-04-27 12:55:59 +01002761 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002762 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002763 fail3:
2764 efx_fini_io(efx);
2765 fail2:
2766 efx_fini_struct(efx);
2767 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002768 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002769 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002770 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002771 free_netdev(net_dev);
2772 return rc;
2773}
2774
Ben Hutchings89c758f2009-11-29 03:43:07 +00002775static int efx_pm_freeze(struct device *dev)
2776{
2777 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2778
Ben Hutchings61da0262012-07-27 19:35:39 +01002779 rtnl_lock();
2780
Ben Hutchings6032fb52012-07-27 19:35:47 +01002781 if (efx->state != STATE_DISABLED) {
2782 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002783
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002784 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002785
Ben Hutchings6032fb52012-07-27 19:35:47 +01002786 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002787 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01002788 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002789
Ben Hutchings61da0262012-07-27 19:35:39 +01002790 rtnl_unlock();
2791
Ben Hutchings89c758f2009-11-29 03:43:07 +00002792 return 0;
2793}
2794
2795static int efx_pm_thaw(struct device *dev)
2796{
2797 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2798
Ben Hutchings61da0262012-07-27 19:35:39 +01002799 rtnl_lock();
2800
Ben Hutchings6032fb52012-07-27 19:35:47 +01002801 if (efx->state != STATE_DISABLED) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01002802 efx_enable_interrupts(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002803
Ben Hutchings6032fb52012-07-27 19:35:47 +01002804 mutex_lock(&efx->mac_lock);
2805 efx->phy_op->reconfigure(efx);
2806 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002807
Ben Hutchings6032fb52012-07-27 19:35:47 +01002808 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002809
Ben Hutchings6032fb52012-07-27 19:35:47 +01002810 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002811
Ben Hutchings6032fb52012-07-27 19:35:47 +01002812 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002813
Ben Hutchings6032fb52012-07-27 19:35:47 +01002814 efx->type->resume_wol(efx);
2815 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002816
Ben Hutchings61da0262012-07-27 19:35:39 +01002817 rtnl_unlock();
2818
Steve Hodgson319ba642010-06-01 11:17:24 +00002819 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2820 queue_work(reset_workqueue, &efx->reset_work);
2821
Ben Hutchings89c758f2009-11-29 03:43:07 +00002822 return 0;
2823}
2824
2825static int efx_pm_poweroff(struct device *dev)
2826{
2827 struct pci_dev *pci_dev = to_pci_dev(dev);
2828 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2829
2830 efx->type->fini(efx);
2831
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002832 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002833
2834 pci_save_state(pci_dev);
2835 return pci_set_power_state(pci_dev, PCI_D3hot);
2836}
2837
2838/* Used for both resume and restore */
2839static int efx_pm_resume(struct device *dev)
2840{
2841 struct pci_dev *pci_dev = to_pci_dev(dev);
2842 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2843 int rc;
2844
2845 rc = pci_set_power_state(pci_dev, PCI_D0);
2846 if (rc)
2847 return rc;
2848 pci_restore_state(pci_dev);
2849 rc = pci_enable_device(pci_dev);
2850 if (rc)
2851 return rc;
2852 pci_set_master(efx->pci_dev);
2853 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2854 if (rc)
2855 return rc;
2856 rc = efx->type->init(efx);
2857 if (rc)
2858 return rc;
2859 efx_pm_thaw(dev);
2860 return 0;
2861}
2862
2863static int efx_pm_suspend(struct device *dev)
2864{
2865 int rc;
2866
2867 efx_pm_freeze(dev);
2868 rc = efx_pm_poweroff(dev);
2869 if (rc)
2870 efx_pm_resume(dev);
2871 return rc;
2872}
2873
Ben Hutchings18e83e42012-01-05 19:05:20 +00002874static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002875 .suspend = efx_pm_suspend,
2876 .resume = efx_pm_resume,
2877 .freeze = efx_pm_freeze,
2878 .thaw = efx_pm_thaw,
2879 .poweroff = efx_pm_poweroff,
2880 .restore = efx_pm_resume,
2881};
2882
Alexandre Rames626950d2013-01-14 17:20:22 +00002883/* A PCI error affecting this device was detected.
2884 * At this point MMIO and DMA may be disabled.
2885 * Stop the software path and request a slot reset.
2886 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002887static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2888 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00002889{
2890 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2891 struct efx_nic *efx = pci_get_drvdata(pdev);
2892
2893 if (state == pci_channel_io_perm_failure)
2894 return PCI_ERS_RESULT_DISCONNECT;
2895
2896 rtnl_lock();
2897
2898 if (efx->state != STATE_DISABLED) {
2899 efx->state = STATE_RECOVERY;
2900 efx->reset_pending = 0;
2901
2902 efx_device_detach_sync(efx);
2903
2904 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002905 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00002906
2907 status = PCI_ERS_RESULT_NEED_RESET;
2908 } else {
2909 /* If the interface is disabled we don't want to do anything
2910 * with it.
2911 */
2912 status = PCI_ERS_RESULT_RECOVERED;
2913 }
2914
2915 rtnl_unlock();
2916
2917 pci_disable_device(pdev);
2918
2919 return status;
2920}
2921
2922/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002923static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00002924{
2925 struct efx_nic *efx = pci_get_drvdata(pdev);
2926 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2927 int rc;
2928
2929 if (pci_enable_device(pdev)) {
2930 netif_err(efx, hw, efx->net_dev,
2931 "Cannot re-enable PCI device after reset.\n");
2932 status = PCI_ERS_RESULT_DISCONNECT;
2933 }
2934
2935 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2936 if (rc) {
2937 netif_err(efx, hw, efx->net_dev,
2938 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2939 /* Non-fatal error. Continue. */
2940 }
2941
2942 return status;
2943}
2944
2945/* Perform the actual reset and resume I/O operations. */
2946static void efx_io_resume(struct pci_dev *pdev)
2947{
2948 struct efx_nic *efx = pci_get_drvdata(pdev);
2949 int rc;
2950
2951 rtnl_lock();
2952
2953 if (efx->state == STATE_DISABLED)
2954 goto out;
2955
2956 rc = efx_reset(efx, RESET_TYPE_ALL);
2957 if (rc) {
2958 netif_err(efx, hw, efx->net_dev,
2959 "efx_reset failed after PCI error (%d)\n", rc);
2960 } else {
2961 efx->state = STATE_READY;
2962 netif_dbg(efx, hw, efx->net_dev,
2963 "Done resetting and resuming IO after PCI error.\n");
2964 }
2965
2966out:
2967 rtnl_unlock();
2968}
2969
2970/* For simplicity and reliability, we always require a slot reset and try to
2971 * reset the hardware when a pci error affecting the device is detected.
2972 * We leave both the link_reset and mmio_enabled callback unimplemented:
2973 * with our request for slot reset the mmio_enabled callback will never be
2974 * called, and the link_reset callback is not used by AER or EEH mechanisms.
2975 */
2976static struct pci_error_handlers efx_err_handlers = {
2977 .error_detected = efx_io_error_detected,
2978 .slot_reset = efx_io_slot_reset,
2979 .resume = efx_io_resume,
2980};
2981
Ben Hutchings8ceee662008-04-27 12:55:59 +01002982static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00002983 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002984 .id_table = efx_pci_table,
2985 .probe = efx_pci_probe,
2986 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00002987 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00002988 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002989};
2990
2991/**************************************************************************
2992 *
2993 * Kernel module interface
2994 *
2995 *************************************************************************/
2996
2997module_param(interrupt_mode, uint, 0444);
2998MODULE_PARM_DESC(interrupt_mode,
2999 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3000
3001static int __init efx_init_module(void)
3002{
3003 int rc;
3004
3005 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3006
3007 rc = register_netdevice_notifier(&efx_netdev_notifier);
3008 if (rc)
3009 goto err_notifier;
3010
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003011 rc = efx_init_sriov();
3012 if (rc)
3013 goto err_sriov;
3014
Steve Hodgson1ab00622008-12-12 21:33:02 -08003015 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3016 if (!reset_workqueue) {
3017 rc = -ENOMEM;
3018 goto err_reset;
3019 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003020
3021 rc = pci_register_driver(&efx_pci_driver);
3022 if (rc < 0)
3023 goto err_pci;
3024
3025 return 0;
3026
3027 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003028 destroy_workqueue(reset_workqueue);
3029 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003030 efx_fini_sriov();
3031 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003032 unregister_netdevice_notifier(&efx_netdev_notifier);
3033 err_notifier:
3034 return rc;
3035}
3036
3037static void __exit efx_exit_module(void)
3038{
3039 printk(KERN_INFO "Solarflare NET driver unloading\n");
3040
3041 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003042 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003043 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003044 unregister_netdevice_notifier(&efx_netdev_notifier);
3045
3046}
3047
3048module_init(efx_init_module);
3049module_exit(efx_exit_module);
3050
Ben Hutchings906bb262009-11-29 15:16:19 +00003051MODULE_AUTHOR("Solarflare Communications and "
3052 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003053MODULE_DESCRIPTION("Solarflare Communications network driver");
3054MODULE_LICENSE("GPL");
3055MODULE_DEVICE_TABLE(pci, efx_pci_table);