blob: 5745a9fd034e107b877141ae3e98d58487436033 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01002 * Driver for Solarflare network controllers and boards
Ben Hutchings8ceee662008-04-27 12:55:59 +01003 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01004 * Copyright 2005-2013 Solarflare Communications Inc.
Ben Hutchings8ceee662008-04-27 12:55:59 +01005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/delay.h>
16#include <linux/notifier.h>
17#include <linux/ip.h>
18#include <linux/tcp.h>
19#include <linux/in.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010020#include <linux/ethtool.h>
Ben Hutchingsaa6ef272008-07-18 19:03:10 +010021#include <linux/topology.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Alexandre Rames626950d2013-01-14 17:20:22 +000023#include <linux/aer.h>
Alexandre Ramesb28405b2013-03-21 16:41:43 +000024#include <linux/interrupt.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010025#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000027#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000028#include "selftest.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010029
Ben Hutchings8880f4e2009-11-29 15:15:41 +000030#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000031#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000032
Ben Hutchingsc4593022009-11-23 16:08:17 +000033/**************************************************************************
34 *
35 * Type name strings
36 *
37 **************************************************************************
38 */
39
40/* Loopback mode names (see LOOPBACK_MODE()) */
41const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000042const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000043 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000044 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000045 [LOOPBACK_GMAC] = "GMAC",
46 [LOOPBACK_XGMII] = "XGMII",
47 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000048 [LOOPBACK_XAUI] = "XAUI",
49 [LOOPBACK_GMII] = "GMII",
50 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000051 [LOOPBACK_XGBR] = "XGBR",
52 [LOOPBACK_XFI] = "XFI",
53 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
54 [LOOPBACK_GMII_FAR] = "GMII_FAR",
55 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
56 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000057 [LOOPBACK_GPHY] = "GPHY",
58 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000059 [LOOPBACK_PCS] = "PCS",
60 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000061 [LOOPBACK_XPORT] = "XPORT",
62 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000063 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000064 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
65 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000066 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000067 [LOOPBACK_XFI_WS] = "XFI_WS",
68 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000069 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000070};
71
Ben Hutchingsc4593022009-11-23 16:08:17 +000072const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000073const char *const efx_reset_type_names[] = {
Alexandre Rames626950d2013-01-14 17:20:22 +000074 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
75 [RESET_TYPE_ALL] = "ALL",
76 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
77 [RESET_TYPE_WORLD] = "WORLD",
78 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
79 [RESET_TYPE_DISABLE] = "DISABLE",
80 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
81 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
82 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
Alexandre Rames3de82b92013-06-13 11:36:15 +010083 [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
Alexandre Rames626950d2013-01-14 17:20:22 +000084 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
85 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Jon Cooper74cd60a2013-09-16 14:18:51 +010086 [RESET_TYPE_MC_BIST] = "MC_BIST",
Ben Hutchingsc4593022009-11-23 16:08:17 +000087};
88
Steve Hodgson1ab00622008-12-12 21:33:02 -080089/* Reset workqueue. If any NIC has a hardware failure then a reset will be
90 * queued onto this work queue. This is not a per-nic work queue, because
91 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
92 */
93static struct workqueue_struct *reset_workqueue;
94
Jon Cooper74cd60a2013-09-16 14:18:51 +010095/* How often and how many times to poll for a reset while waiting for a
96 * BIST that another function started to complete.
97 */
98#define BIST_WAIT_DELAY_MS 100
99#define BIST_WAIT_DELAY_COUNT 100
100
Ben Hutchings8ceee662008-04-27 12:55:59 +0100101/**************************************************************************
102 *
103 * Configurable values
104 *
105 *************************************************************************/
106
107/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100108 * Use separate channels for TX and RX events
109 *
Neil Turton28b581a2008-12-12 21:41:06 -0800110 * Set this to 1 to use separate channels for TX and RX. It allows us
111 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100112 *
Neil Turton28b581a2008-12-12 21:41:06 -0800113 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100114 */
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000115static bool separate_tx_channels;
116module_param(separate_tx_channels, bool, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800117MODULE_PARM_DESC(separate_tx_channels,
118 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100119
120/* This is the weight assigned to each of the (per-channel) virtual
121 * NAPI devices.
122 */
123static int napi_weight = 64;
124
125/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000126 * monitor.
127 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000128 * - Check the on-board hardware monitor;
129 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000130 * On Siena-based NICs for power systems with EEH support, this will give EEH a
131 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100132 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000133static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100134
Ben Hutchings8ceee662008-04-27 12:55:59 +0100135/* Initial interrupt moderation settings. They can be modified after
136 * module load with ethtool.
137 *
138 * The default for RX should strike a balance between increasing the
139 * round-trip latency and reducing overhead.
140 */
141static unsigned int rx_irq_mod_usec = 60;
142
143/* Initial interrupt moderation settings. They can be modified after
144 * module load with ethtool.
145 *
146 * This default is chosen to ensure that a 10G link does not go idle
147 * while a TX queue is stopped after it has become full. A queue is
148 * restarted when it drops below half full. The time this takes (assuming
149 * worst case 3 descriptors per packet and 1024 descriptors) is
150 * 512 / 3 * 1.2 = 205 usec.
151 */
152static unsigned int tx_irq_mod_usec = 150;
153
154/* This is the first interrupt mode to try out of:
155 * 0 => MSI-X
156 * 1 => MSI
157 * 2 => legacy
158 */
159static unsigned int interrupt_mode;
160
161/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
162 * i.e. the number of CPUs among which we may distribute simultaneous
163 * interrupt handling.
164 *
165 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000166 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100167 */
168static unsigned int rss_cpus;
169module_param(rss_cpus, uint, 0444);
170MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
171
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000172static bool phy_flash_cfg;
173module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800174MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
175
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000176static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000177module_param(irq_adapt_low_thresh, uint, 0644);
178MODULE_PARM_DESC(irq_adapt_low_thresh,
179 "Threshold score for reducing IRQ moderation");
180
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000181static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000182module_param(irq_adapt_high_thresh, uint, 0644);
183MODULE_PARM_DESC(irq_adapt_high_thresh,
184 "Threshold score for increasing IRQ moderation");
185
Ben Hutchings62776d02010-06-23 11:30:07 +0000186static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
187 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
188 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
189 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
190module_param(debug, uint, 0);
191MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
192
Ben Hutchings8ceee662008-04-27 12:55:59 +0100193/**************************************************************************
194 *
195 * Utility functions and prototypes
196 *
197 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000198
Jon Cooper261e4d92013-04-15 18:51:54 +0100199static int efx_soft_enable_interrupts(struct efx_nic *efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100200static void efx_soft_disable_interrupts(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000201static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000202static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000203static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100204static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000205static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100206static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000207static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000208static void efx_fini_struct(struct efx_nic *efx);
209static void efx_start_all(struct efx_nic *efx);
210static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100211
212#define EFX_ASSERT_RESET_SERIALISED(efx) \
213 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100214 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000215 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000216 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100217 ASSERT_RTNL(); \
218 } while (0)
219
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100220static int efx_check_disabled(struct efx_nic *efx)
221{
Alexandre Rames626950d2013-01-14 17:20:22 +0000222 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100223 netif_err(efx, drv, efx->net_dev,
224 "device is disabled due to earlier errors\n");
225 return -EIO;
226 }
227 return 0;
228}
229
Ben Hutchings8ceee662008-04-27 12:55:59 +0100230/**************************************************************************
231 *
232 * Event queue processing
233 *
234 *************************************************************************/
235
236/* Process channel's event queue
237 *
238 * This function is responsible for processing the event queue of a
239 * single channel. The caller must guarantee that this function will
240 * never be concurrently called more than once on the same channel,
241 * though different channels may be being processed concurrently.
242 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000243static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100244{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000245 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100246
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000247 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100248 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100249
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000250 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000251 if (spent && efx_channel_has_rx_queue(channel)) {
252 struct efx_rx_queue *rx_queue =
253 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100254
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000255 efx_rx_flush_packet(channel);
Jon Coopercce28792013-10-02 11:04:14 +0100256 efx_fast_push_rx_descriptors(rx_queue, true);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100257 }
258
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000259 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100260}
261
Ben Hutchings8ceee662008-04-27 12:55:59 +0100262/* NAPI poll handler
263 *
264 * NAPI guarantees serialisation of polls of the same device, which
265 * provides the guarantee required by efx_process_channel().
266 */
267static int efx_poll(struct napi_struct *napi, int budget)
268{
269 struct efx_channel *channel =
270 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000271 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000272 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100273
Ben Hutchings62776d02010-06-23 11:30:07 +0000274 netif_vdbg(efx, intr, efx->net_dev,
275 "channel %d NAPI poll executing on CPU %d\n",
276 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100277
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000278 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100279
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000280 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000281 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000282 efx->irq_rx_adaptive &&
283 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000284 if (unlikely(channel->irq_mod_score <
285 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000286 if (channel->irq_moderation > 1) {
287 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000288 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000289 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000290 } else if (unlikely(channel->irq_mod_score >
291 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000292 if (channel->irq_moderation <
293 efx->irq_rx_moderation) {
294 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000295 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000296 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000297 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000298 channel->irq_count = 0;
299 channel->irq_mod_score = 0;
300 }
301
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000302 efx_filter_rfs_expire(channel);
303
Ben Hutchings8ceee662008-04-27 12:55:59 +0100304 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800305 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100306 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100307 * interrupts have already been disabled.
308 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800309 napi_complete(napi);
Ben Hutchings514bedb2012-10-05 19:30:16 +0100310 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100311 }
312
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000313 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100314}
315
Ben Hutchings8ceee662008-04-27 12:55:59 +0100316/* Create event queue
317 * Event queue memory allocations are done only once. If the channel
318 * is reset, the memory buffer will be reused; this guards against
319 * errors during channel reset and also simplifies interrupt handling.
320 */
321static int efx_probe_eventq(struct efx_channel *channel)
322{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000323 struct efx_nic *efx = channel->efx;
324 unsigned long entries;
325
Ben Hutchings86ee5302012-01-09 19:51:22 +0000326 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000327 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100328
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000329 /* Build an event queue with room for one event per tx and rx buffer,
330 * plus some extra for link state events and MCDI completions. */
331 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
332 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
333 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
334
Ben Hutchings152b6a62009-11-29 03:43:56 +0000335 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100336}
337
338/* Prepare channel's event queue */
Jon Cooper261e4d92013-04-15 18:51:54 +0100339static int efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100340{
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100341 struct efx_nic *efx = channel->efx;
Jon Cooper261e4d92013-04-15 18:51:54 +0100342 int rc;
343
344 EFX_WARN_ON_PARANOID(channel->eventq_init);
345
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100346 netif_dbg(efx, drv, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000347 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100348
Jon Cooper261e4d92013-04-15 18:51:54 +0100349 rc = efx_nic_init_eventq(channel);
350 if (rc == 0) {
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100351 efx->type->push_irq_moderation(channel);
Jon Cooper261e4d92013-04-15 18:51:54 +0100352 channel->eventq_read_ptr = 0;
353 channel->eventq_init = true;
354 }
355 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100356}
357
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000358/* Enable event queue processing and NAPI */
359static void efx_start_eventq(struct efx_channel *channel)
360{
361 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
362 "chan %d start event queue\n", channel->channel);
363
Ben Hutchings514bedb2012-10-05 19:30:16 +0100364 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000365 channel->enabled = true;
366 smp_wmb();
367
368 napi_enable(&channel->napi_str);
369 efx_nic_eventq_read_ack(channel);
370}
371
372/* Disable event queue processing and NAPI */
373static void efx_stop_eventq(struct efx_channel *channel)
374{
375 if (!channel->enabled)
376 return;
377
378 napi_disable(&channel->napi_str);
379 channel->enabled = false;
380}
381
Ben Hutchings8ceee662008-04-27 12:55:59 +0100382static void efx_fini_eventq(struct efx_channel *channel)
383{
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100384 if (!channel->eventq_init)
385 return;
386
Ben Hutchings62776d02010-06-23 11:30:07 +0000387 netif_dbg(channel->efx, drv, channel->efx->net_dev,
388 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100389
Ben Hutchings152b6a62009-11-29 03:43:56 +0000390 efx_nic_fini_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100391 channel->eventq_init = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100392}
393
394static void efx_remove_eventq(struct efx_channel *channel)
395{
Ben Hutchings62776d02010-06-23 11:30:07 +0000396 netif_dbg(channel->efx, drv, channel->efx->net_dev,
397 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100398
Ben Hutchings152b6a62009-11-29 03:43:56 +0000399 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100400}
401
402/**************************************************************************
403 *
404 * Channel handling
405 *
406 *************************************************************************/
407
Ben Hutchings7f967c02012-02-13 23:45:02 +0000408/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000409static struct efx_channel *
410efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
411{
412 struct efx_channel *channel;
413 struct efx_rx_queue *rx_queue;
414 struct efx_tx_queue *tx_queue;
415 int j;
416
Ben Hutchings7f967c02012-02-13 23:45:02 +0000417 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
418 if (!channel)
419 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000420
Ben Hutchings7f967c02012-02-13 23:45:02 +0000421 channel->efx = efx;
422 channel->channel = i;
423 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000424
Ben Hutchings7f967c02012-02-13 23:45:02 +0000425 for (j = 0; j < EFX_TXQ_TYPES; j++) {
426 tx_queue = &channel->tx_queue[j];
427 tx_queue->efx = efx;
428 tx_queue->queue = i * EFX_TXQ_TYPES + j;
429 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000430 }
431
Ben Hutchings46426102010-09-10 06:42:33 +0000432 rx_queue = &channel->rx_queue;
433 rx_queue->efx = efx;
434 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
435 (unsigned long)rx_queue);
436
437 return channel;
438}
439
Ben Hutchings7f967c02012-02-13 23:45:02 +0000440/* Allocate and initialise a channel structure, copying parameters
441 * (but not resources) from an old channel structure.
442 */
443static struct efx_channel *
444efx_copy_channel(const struct efx_channel *old_channel)
445{
446 struct efx_channel *channel;
447 struct efx_rx_queue *rx_queue;
448 struct efx_tx_queue *tx_queue;
449 int j;
450
451 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
452 if (!channel)
453 return NULL;
454
455 *channel = *old_channel;
456
457 channel->napi_dev = NULL;
458 memset(&channel->eventq, 0, sizeof(channel->eventq));
459
460 for (j = 0; j < EFX_TXQ_TYPES; j++) {
461 tx_queue = &channel->tx_queue[j];
462 if (tx_queue->channel)
463 tx_queue->channel = channel;
464 tx_queue->buffer = NULL;
465 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
466 }
467
468 rx_queue = &channel->rx_queue;
469 rx_queue->buffer = NULL;
470 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
471 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
472 (unsigned long)rx_queue);
473
474 return channel;
475}
476
Ben Hutchings8ceee662008-04-27 12:55:59 +0100477static int efx_probe_channel(struct efx_channel *channel)
478{
479 struct efx_tx_queue *tx_queue;
480 struct efx_rx_queue *rx_queue;
481 int rc;
482
Ben Hutchings62776d02010-06-23 11:30:07 +0000483 netif_dbg(channel->efx, probe, channel->efx->net_dev,
484 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100485
Ben Hutchings7f967c02012-02-13 23:45:02 +0000486 rc = channel->type->pre_probe(channel);
487 if (rc)
488 goto fail;
489
Ben Hutchings8ceee662008-04-27 12:55:59 +0100490 rc = efx_probe_eventq(channel);
491 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000492 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100493
494 efx_for_each_channel_tx_queue(tx_queue, channel) {
495 rc = efx_probe_tx_queue(tx_queue);
496 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000497 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100498 }
499
500 efx_for_each_channel_rx_queue(rx_queue, channel) {
501 rc = efx_probe_rx_queue(rx_queue);
502 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000503 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100504 }
505
506 channel->n_rx_frm_trunc = 0;
507
508 return 0;
509
Ben Hutchings7f967c02012-02-13 23:45:02 +0000510fail:
511 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100512 return rc;
513}
514
Ben Hutchings7f967c02012-02-13 23:45:02 +0000515static void
516efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
517{
518 struct efx_nic *efx = channel->efx;
519 const char *type;
520 int number;
521
522 number = channel->channel;
523 if (efx->tx_channel_offset == 0) {
524 type = "";
525 } else if (channel->channel < efx->tx_channel_offset) {
526 type = "-rx";
527 } else {
528 type = "-tx";
529 number -= efx->tx_channel_offset;
530 }
531 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
532}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100533
Ben Hutchings56536e92008-12-12 21:37:02 -0800534static void efx_set_channel_names(struct efx_nic *efx)
535{
536 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800537
Ben Hutchings7f967c02012-02-13 23:45:02 +0000538 efx_for_each_channel(channel, efx)
539 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100540 efx->msi_context[channel->channel].name,
541 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800542}
543
Ben Hutchings46426102010-09-10 06:42:33 +0000544static int efx_probe_channels(struct efx_nic *efx)
545{
546 struct efx_channel *channel;
547 int rc;
548
549 /* Restart special buffer allocation */
550 efx->next_buffer_table = 0;
551
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000552 /* Probe channels in reverse, so that any 'extra' channels
553 * use the start of the buffer table. This allows the traffic
554 * channels to be resized without moving them or wasting the
555 * entries before them.
556 */
557 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000558 rc = efx_probe_channel(channel);
559 if (rc) {
560 netif_err(efx, probe, efx->net_dev,
561 "failed to create channel %d\n",
562 channel->channel);
563 goto fail;
564 }
565 }
566 efx_set_channel_names(efx);
567
568 return 0;
569
570fail:
571 efx_remove_channels(efx);
572 return rc;
573}
574
Ben Hutchings8ceee662008-04-27 12:55:59 +0100575/* Channels are shutdown and reinitialised whilst the NIC is running
576 * to propagate configuration changes (mtu, checksum offload), or
577 * to clear hardware error conditions
578 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000579static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100580{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000581 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100582 struct efx_tx_queue *tx_queue;
583 struct efx_rx_queue *rx_queue;
584 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000585 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100586
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100587 /* Calculate the rx buffer allocation parameters required to
588 * support the current MTU, including padding for header
589 * alignment and overruns.
590 */
Jon Cooper43a37392012-10-18 15:49:54 +0100591 efx->rx_dma_len = (efx->rx_prefix_size +
Ben Hutchings272baee2013-01-29 23:33:14 +0000592 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
593 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000594 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Andrew Rybchenko2ec03012013-11-16 11:02:27 +0400595 efx->rx_ip_align + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000596 if (rx_buf_len <= PAGE_SIZE) {
Jon Coopere8c68c02013-03-08 10:18:28 +0000597 efx->rx_scatter = efx->type->always_rx_scatter;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000598 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000599 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000600 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000601 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000602 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
603 EFX_RX_BUF_ALIGNMENT) >
604 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000605 efx->rx_scatter = true;
606 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
607 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000608 } else {
609 efx->rx_scatter = false;
610 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000611 }
612
Daniel Pieczko1648a232013-02-13 10:54:41 +0000613 efx_rx_config_page_split(efx);
614 if (efx->rx_buffer_order)
615 netif_dbg(efx, drv, efx->net_dev,
616 "RX buf len=%u; page order=%u batch=%u\n",
617 efx->rx_dma_len, efx->rx_buffer_order,
618 efx->rx_pages_per_batch);
619 else
620 netif_dbg(efx, drv, efx->net_dev,
621 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
622 efx->rx_dma_len, efx->rx_page_buf_step,
623 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000624
Jon Coopere8c68c02013-03-08 10:18:28 +0000625 /* RX filters may also have scatter-enabled flags */
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000626 if (efx->rx_scatter != old_rx_scatter)
Ben Hutchingsadd72472012-11-08 01:46:53 +0000627 efx->type->filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100628
Ben Hutchings14bf7182012-05-22 01:27:58 +0100629 /* We must keep at least one descriptor in a TX ring empty.
630 * We could avoid this when the queue size does not exactly
631 * match the hardware ring size, but it's not that important.
632 * Therefore we stop the queue when one more skb might fill
633 * the ring completely. We wake it when half way back to
634 * empty.
635 */
636 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
637 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
638
Ben Hutchings8ceee662008-04-27 12:55:59 +0100639 /* Initialise the channels */
640 efx_for_each_channel(channel, efx) {
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100641 efx_for_each_channel_tx_queue(tx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100642 efx_init_tx_queue(tx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100643 atomic_inc(&efx->active_queues);
644 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100645
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000646 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100647 efx_init_rx_queue(rx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100648 atomic_inc(&efx->active_queues);
Jon Coopercce28792013-10-02 11:04:14 +0100649 efx_stop_eventq(channel);
650 efx_fast_push_rx_descriptors(rx_queue, false);
651 efx_start_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000652 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100653
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000654 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100655 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000656
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000657 efx_ptp_start_datapath(efx);
658
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000659 if (netif_device_present(efx->net_dev))
660 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100661}
662
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000663static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100664{
665 struct efx_channel *channel;
666 struct efx_tx_queue *tx_queue;
667 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100668 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100669
670 EFX_ASSERT_RESET_SERIALISED(efx);
671 BUG_ON(efx->port_enabled);
672
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000673 efx_ptp_stop_datapath(efx);
674
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100675 /* Stop RX refill */
676 efx_for_each_channel(channel, efx) {
677 efx_for_each_channel_rx_queue(rx_queue, channel)
678 rx_queue->refill_enabled = false;
679 }
680
Ben Hutchings8ceee662008-04-27 12:55:59 +0100681 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000682 /* RX packet processing is pipelined, so wait for the
683 * NAPI handler to complete. At least event queue 0
684 * might be kept active by non-data events, so don't
685 * use napi_synchronize() but actually disable NAPI
686 * temporarily.
687 */
688 if (efx_channel_has_rx_queue(channel)) {
689 efx_stop_eventq(channel);
690 efx_start_eventq(channel);
691 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100692 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100693
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100694 rc = efx->type->fini_dmaq(efx);
695 if (rc && EFX_WORKAROUND_7803(efx)) {
696 /* Schedule a reset to recover from the flush failure. The
697 * descriptor caches reference memory we're about to free,
698 * but falcon_reconfigure_mac_wrapper() won't reconnect
699 * the MACs because of the pending reset.
700 */
701 netif_err(efx, drv, efx->net_dev,
702 "Resetting to recover from flush failure\n");
703 efx_schedule_reset(efx, RESET_TYPE_ALL);
704 } else if (rc) {
705 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
706 } else {
707 netif_dbg(efx, drv, efx->net_dev,
708 "successfully flushed all queues\n");
709 }
710
711 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100712 efx_for_each_channel_rx_queue(rx_queue, channel)
713 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000714 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100715 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100716 }
717}
718
719static void efx_remove_channel(struct efx_channel *channel)
720{
721 struct efx_tx_queue *tx_queue;
722 struct efx_rx_queue *rx_queue;
723
Ben Hutchings62776d02010-06-23 11:30:07 +0000724 netif_dbg(channel->efx, drv, channel->efx->net_dev,
725 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100726
727 efx_for_each_channel_rx_queue(rx_queue, channel)
728 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000729 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100730 efx_remove_tx_queue(tx_queue);
731 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100732 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100733}
734
Ben Hutchings46426102010-09-10 06:42:33 +0000735static void efx_remove_channels(struct efx_nic *efx)
736{
737 struct efx_channel *channel;
738
739 efx_for_each_channel(channel, efx)
740 efx_remove_channel(channel);
741}
742
743int
744efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
745{
746 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
747 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000748 unsigned i, next_buffer_table = 0;
Jon Cooper261e4d92013-04-15 18:51:54 +0100749 int rc, rc2;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100750
751 rc = efx_check_disabled(efx);
752 if (rc)
753 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000754
755 /* Not all channels should be reallocated. We must avoid
756 * reallocating their buffer table entries.
757 */
758 efx_for_each_channel(channel, efx) {
759 struct efx_rx_queue *rx_queue;
760 struct efx_tx_queue *tx_queue;
761
762 if (channel->type->copy)
763 continue;
764 next_buffer_table = max(next_buffer_table,
765 channel->eventq.index +
766 channel->eventq.entries);
767 efx_for_each_channel_rx_queue(rx_queue, channel)
768 next_buffer_table = max(next_buffer_table,
769 rx_queue->rxd.index +
770 rx_queue->rxd.entries);
771 efx_for_each_channel_tx_queue(tx_queue, channel)
772 next_buffer_table = max(next_buffer_table,
773 tx_queue->txd.index +
774 tx_queue->txd.entries);
775 }
Ben Hutchings46426102010-09-10 06:42:33 +0000776
Ben Hutchings29c69a42013-01-28 19:01:06 +0000777 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000778 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100779 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000780
Ben Hutchings7f967c02012-02-13 23:45:02 +0000781 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000782 memset(other_channel, 0, sizeof(other_channel));
783 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000784 channel = efx->channel[i];
785 if (channel->type->copy)
786 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000787 if (!channel) {
788 rc = -ENOMEM;
789 goto out;
790 }
791 other_channel[i] = channel;
792 }
793
794 /* Swap entry counts and channel pointers */
795 old_rxq_entries = efx->rxq_entries;
796 old_txq_entries = efx->txq_entries;
797 efx->rxq_entries = rxq_entries;
798 efx->txq_entries = txq_entries;
799 for (i = 0; i < efx->n_channels; i++) {
800 channel = efx->channel[i];
801 efx->channel[i] = other_channel[i];
802 other_channel[i] = channel;
803 }
804
Ben Hutchings7f967c02012-02-13 23:45:02 +0000805 /* Restart buffer table allocation */
806 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000807
Ben Hutchingse8f14992010-12-07 19:47:34 +0000808 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000809 channel = efx->channel[i];
810 if (!channel->type->copy)
811 continue;
812 rc = efx_probe_channel(channel);
813 if (rc)
814 goto rollback;
815 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000816 }
Ben Hutchings46426102010-09-10 06:42:33 +0000817
Ben Hutchings7f967c02012-02-13 23:45:02 +0000818out:
819 /* Destroy unused channel structures */
820 for (i = 0; i < efx->n_channels; i++) {
821 channel = other_channel[i];
822 if (channel && channel->type->copy) {
823 efx_fini_napi_channel(channel);
824 efx_remove_channel(channel);
825 kfree(channel);
826 }
827 }
828
Jon Cooper261e4d92013-04-15 18:51:54 +0100829 rc2 = efx_soft_enable_interrupts(efx);
830 if (rc2) {
831 rc = rc ? rc : rc2;
832 netif_err(efx, drv, efx->net_dev,
833 "unable to restart interrupts on channel reallocation\n");
834 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
835 } else {
836 efx_start_all(efx);
837 netif_device_attach(efx->net_dev);
838 }
Ben Hutchings46426102010-09-10 06:42:33 +0000839 return rc;
840
841rollback:
842 /* Swap back */
843 efx->rxq_entries = old_rxq_entries;
844 efx->txq_entries = old_txq_entries;
845 for (i = 0; i < efx->n_channels; i++) {
846 channel = efx->channel[i];
847 efx->channel[i] = other_channel[i];
848 other_channel[i] = channel;
849 }
850 goto out;
851}
852
Steve Hodgson90d683a2010-06-01 11:19:39 +0000853void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100854{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000855 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100856}
857
Ben Hutchings7f967c02012-02-13 23:45:02 +0000858static const struct efx_channel_type efx_default_channel_type = {
859 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100860 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000861 .get_name = efx_get_channel_name,
862 .copy = efx_copy_channel,
863 .keep_eventq = false,
864};
865
866int efx_channel_dummy_op_int(struct efx_channel *channel)
867{
868 return 0;
869}
870
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100871void efx_channel_dummy_op_void(struct efx_channel *channel)
872{
873}
874
Ben Hutchings8ceee662008-04-27 12:55:59 +0100875/**************************************************************************
876 *
877 * Port handling
878 *
879 **************************************************************************/
880
881/* This ensures that the kernel is kept informed (via
882 * netif_carrier_on/off) of the link status, and also maintains the
883 * link status's stop on the port's TX queue.
884 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000885void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100886{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000887 struct efx_link_state *link_state = &efx->link_state;
888
Ben Hutchings8ceee662008-04-27 12:55:59 +0100889 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
890 * that no events are triggered between unregister_netdev() and the
891 * driver unloading. A more general condition is that NETDEV_CHANGE
892 * can only be generated between NETDEV_UP and NETDEV_DOWN */
893 if (!netif_running(efx->net_dev))
894 return;
895
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000896 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100897 efx->n_link_state_changes++;
898
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000899 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100900 netif_carrier_on(efx->net_dev);
901 else
902 netif_carrier_off(efx->net_dev);
903 }
904
905 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000906 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000907 netif_info(efx, link, efx->net_dev,
Ben Hutchings964e6132012-11-19 23:08:22 +0000908 "link up at %uMbps %s-duplex (MTU %d)\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000909 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings964e6132012-11-19 23:08:22 +0000910 efx->net_dev->mtu);
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000911 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000912 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100913}
914
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000915void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
916{
917 efx->link_advertising = advertising;
918 if (advertising) {
919 if (advertising & ADVERTISED_Pause)
920 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
921 else
922 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
923 if (advertising & ADVERTISED_Asym_Pause)
924 efx->wanted_fc ^= EFX_FC_TX;
925 }
926}
927
David S. Millerb56269462011-05-17 17:53:22 -0400928void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000929{
930 efx->wanted_fc = wanted_fc;
931 if (efx->link_advertising) {
932 if (wanted_fc & EFX_FC_RX)
933 efx->link_advertising |= (ADVERTISED_Pause |
934 ADVERTISED_Asym_Pause);
935 else
936 efx->link_advertising &= ~(ADVERTISED_Pause |
937 ADVERTISED_Asym_Pause);
938 if (wanted_fc & EFX_FC_TX)
939 efx->link_advertising ^= ADVERTISED_Asym_Pause;
940 }
941}
942
Ben Hutchings115122a2009-03-04 09:52:52 +0000943static void efx_fini_port(struct efx_nic *efx);
944
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000945/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
946 * the MAC appropriately. All other PHY configuration changes are pushed
947 * through phy_op->set_settings(), and pushed asynchronously to the MAC
948 * through efx_monitor().
949 *
950 * Callers must hold the mac_lock
951 */
952int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100953{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000954 enum efx_phy_mode phy_mode;
955 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100956
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000957 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100958
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000959 /* Disable PHY transmit in mac level loopbacks */
960 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800961 if (LOOPBACK_INTERNAL(efx))
962 efx->phy_mode |= PHY_MODE_TX_DISABLED;
963 else
964 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800965
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000966 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800967
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000968 if (rc)
969 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100970
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000971 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100972}
973
974/* Reinitialise the MAC to pick up new PHY settings, even if the port is
975 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000976int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100977{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000978 int rc;
979
Ben Hutchings8ceee662008-04-27 12:55:59 +0100980 EFX_ASSERT_RESET_SERIALISED(efx);
981
982 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000983 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100984 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000985
986 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100987}
988
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000989/* Asynchronous work item for changing MAC promiscuity and multicast
990 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
991 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800992static void efx_mac_work(struct work_struct *data)
993{
994 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
995
996 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100997 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100998 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800999 mutex_unlock(&efx->mac_lock);
1000}
1001
Ben Hutchings8ceee662008-04-27 12:55:59 +01001002static int efx_probe_port(struct efx_nic *efx)
1003{
1004 int rc;
1005
Ben Hutchings62776d02010-06-23 11:30:07 +00001006 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001007
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001008 if (phy_flash_cfg)
1009 efx->phy_mode = PHY_MODE_SPECIAL;
1010
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001011 /* Connect up MAC/PHY operations table */
1012 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001013 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001014 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001015
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001016 /* Initialise MAC address to permanent address */
1017 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001018
1019 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001020}
1021
1022static int efx_init_port(struct efx_nic *efx)
1023{
1024 int rc;
1025
Ben Hutchings62776d02010-06-23 11:30:07 +00001026 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001027
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001028 mutex_lock(&efx->mac_lock);
1029
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001030 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001031 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001032 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001033
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001034 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001035
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001036 /* Reconfigure the MAC before creating dma queues (required for
1037 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001038 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001039
1040 /* Ensure the PHY advertises the correct flow control settings */
1041 rc = efx->phy_op->reconfigure(efx);
1042 if (rc)
1043 goto fail2;
1044
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001045 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001046 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001047
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001048fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001049 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001050fail1:
1051 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001052 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001053}
1054
Ben Hutchings8ceee662008-04-27 12:55:59 +01001055static void efx_start_port(struct efx_nic *efx)
1056{
Ben Hutchings62776d02010-06-23 11:30:07 +00001057 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001058 BUG_ON(efx->port_enabled);
1059
1060 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001061 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001062
Ben Hutchingsd615c032013-10-08 17:33:20 +01001063 /* Ensure MAC ingress/egress is enabled */
Ben Hutchings710b2082011-09-03 00:15:00 +01001064 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001065
Ben Hutchings8ceee662008-04-27 12:55:59 +01001066 mutex_unlock(&efx->mac_lock);
1067}
1068
Ben Hutchingsd615c032013-10-08 17:33:20 +01001069/* Cancel work for MAC reconfiguration, periodic hardware monitoring
1070 * and the async self-test, wait for them to finish and prevent them
1071 * being scheduled again. This doesn't cover online resets, which
1072 * should only be cancelled when removing the device.
1073 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001074static void efx_stop_port(struct efx_nic *efx)
1075{
Ben Hutchings62776d02010-06-23 11:30:07 +00001076 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001077
Ben Hutchingsd615c032013-10-08 17:33:20 +01001078 EFX_ASSERT_RESET_SERIALISED(efx);
1079
Ben Hutchings8ceee662008-04-27 12:55:59 +01001080 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001081 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001082 mutex_unlock(&efx->mac_lock);
1083
1084 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001085 netif_addr_lock_bh(efx->net_dev);
1086 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsd615c032013-10-08 17:33:20 +01001087
1088 cancel_delayed_work_sync(&efx->monitor_work);
1089 efx_selftest_async_cancel(efx);
1090 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001091}
1092
1093static void efx_fini_port(struct efx_nic *efx)
1094{
Ben Hutchings62776d02010-06-23 11:30:07 +00001095 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001096
1097 if (!efx->port_initialized)
1098 return;
1099
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001100 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001101 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001102
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001103 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001104 efx_link_status_changed(efx);
1105}
1106
1107static void efx_remove_port(struct efx_nic *efx)
1108{
Ben Hutchings62776d02010-06-23 11:30:07 +00001109 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001110
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001111 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001112}
1113
1114/**************************************************************************
1115 *
1116 * NIC handling
1117 *
1118 **************************************************************************/
1119
1120/* This configures the PCI device to enable I/O and DMA. */
1121static int efx_init_io(struct efx_nic *efx)
1122{
1123 struct pci_dev *pci_dev = efx->pci_dev;
1124 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001125 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001126 int rc;
1127
Ben Hutchings62776d02010-06-23 11:30:07 +00001128 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001129
1130 rc = pci_enable_device(pci_dev);
1131 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001132 netif_err(efx, probe, efx->net_dev,
1133 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001134 goto fail1;
1135 }
1136
1137 pci_set_master(pci_dev);
1138
1139 /* Set the PCI DMA mask. Try all possibilities from our
1140 * genuine mask down to 32 bits, because some architectures
1141 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1142 * masks event though they reject 46 bit masks.
1143 */
1144 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001145 if (dma_supported(&pci_dev->dev, dma_mask)) {
Russell King9663ded2013-06-26 23:49:11 +01001146 rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001147 if (rc == 0)
1148 break;
1149 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001150 dma_mask >>= 1;
1151 }
1152 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001153 netif_err(efx, probe, efx->net_dev,
1154 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001155 goto fail2;
1156 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001157 netif_dbg(efx, probe, efx->net_dev,
1158 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001159
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001160 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1161 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001162 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001163 netif_err(efx, probe, efx->net_dev,
1164 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001165 rc = -EIO;
1166 goto fail3;
1167 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001168 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001169 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001170 netif_err(efx, probe, efx->net_dev,
1171 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001172 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001173 rc = -ENOMEM;
1174 goto fail4;
1175 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001176 netif_dbg(efx, probe, efx->net_dev,
1177 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001178 (unsigned long long)efx->membase_phys, mem_map_size,
1179 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001180
1181 return 0;
1182
1183 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001184 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001185 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001186 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001187 fail2:
1188 pci_disable_device(efx->pci_dev);
1189 fail1:
1190 return rc;
1191}
1192
1193static void efx_fini_io(struct efx_nic *efx)
1194{
Ben Hutchings62776d02010-06-23 11:30:07 +00001195 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001196
1197 if (efx->membase) {
1198 iounmap(efx->membase);
1199 efx->membase = NULL;
1200 }
1201
1202 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001203 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001204 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001205 }
1206
1207 pci_disable_device(efx->pci_dev);
1208}
1209
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001210static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001211{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001212 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001213 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001214 int cpu;
1215
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001216 if (rss_cpus) {
1217 count = rss_cpus;
1218 } else {
1219 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1220 netif_warn(efx, probe, efx->net_dev,
1221 "RSS disabled due to allocation failure\n");
1222 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001223 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001224
1225 count = 0;
1226 for_each_online_cpu(cpu) {
1227 if (!cpumask_test_cpu(cpu, thread_mask)) {
1228 ++count;
1229 cpumask_or(thread_mask, thread_mask,
1230 topology_thread_cpumask(cpu));
1231 }
1232 }
1233
1234 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001235 }
1236
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001237 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1238 * table entries that are inaccessible to VFs
1239 */
1240 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1241 count > efx_vf_size(efx)) {
1242 netif_warn(efx, probe, efx->net_dev,
1243 "Reducing number of RSS channels from %u to %u for "
1244 "VF support. Increase vf-msix-limit to use more "
1245 "channels on the PF.\n",
1246 count, efx_vf_size(efx));
1247 count = efx_vf_size(efx);
1248 }
1249
Ben Hutchings46123d02008-09-01 12:47:33 +01001250 return count;
1251}
1252
1253/* Probe the number and type of interrupts we are able to obtain, and
1254 * the resulting numbers of channels and RX queues.
1255 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001256static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001257{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001258 unsigned int extra_channels = 0;
1259 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001260 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001261
Ben Hutchings7f967c02012-02-13 23:45:02 +00001262 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1263 if (efx->extra_channel_type[i])
1264 ++extra_channels;
1265
Ben Hutchings8ceee662008-04-27 12:55:59 +01001266 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001267 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001268 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001269
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001270 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001271 if (separate_tx_channels)
1272 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001273 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001274 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001275
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001276 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001277 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001278 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001279 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001280 netif_err(efx, drv, efx->net_dev,
1281 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001282 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001283 netif_err(efx, drv, efx->net_dev,
1284 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001285 EFX_BUG_ON_PARANOID(rc >= n_channels);
1286 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001287 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001288 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001289 }
1290
1291 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001292 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001293 if (n_channels > extra_channels)
1294 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001295 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001296 efx->n_tx_channels = max(n_channels / 2, 1U);
1297 efx->n_rx_channels = max(n_channels -
1298 efx->n_tx_channels,
1299 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001300 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001301 efx->n_tx_channels = n_channels;
1302 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001303 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001304 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001305 efx_get_channel(efx, i)->irq =
1306 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001307 } else {
1308 /* Fall back to single channel MSI */
1309 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001310 netif_err(efx, drv, efx->net_dev,
1311 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001312 }
1313 }
1314
1315 /* Try single interrupt MSI */
1316 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001317 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001318 efx->n_rx_channels = 1;
1319 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001320 rc = pci_enable_msi(efx->pci_dev);
1321 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001322 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001323 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001324 netif_err(efx, drv, efx->net_dev,
1325 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001326 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1327 }
1328 }
1329
1330 /* Assume legacy interrupts */
1331 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001332 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001333 efx->n_rx_channels = 1;
1334 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001335 efx->legacy_irq = efx->pci_dev->irq;
1336 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001337
Ben Hutchings7f967c02012-02-13 23:45:02 +00001338 /* Assign extra channels if possible */
1339 j = efx->n_channels;
1340 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1341 if (!efx->extra_channel_type[i])
1342 continue;
1343 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1344 efx->n_channels <= extra_channels) {
1345 efx->extra_channel_type[i]->handle_no_channel(efx);
1346 } else {
1347 --j;
1348 efx_get_channel(efx, j)->type =
1349 efx->extra_channel_type[i];
1350 }
1351 }
1352
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001353 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001354 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001355 efx->n_rx_channels : efx_vf_size(efx));
1356
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001357 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001358}
1359
Jon Cooper261e4d92013-04-15 18:51:54 +01001360static int efx_soft_enable_interrupts(struct efx_nic *efx)
Ben Hutchingsd8291182012-10-05 23:35:41 +01001361{
Jon Cooper261e4d92013-04-15 18:51:54 +01001362 struct efx_channel *channel, *end_channel;
1363 int rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001364
1365 BUG_ON(efx->state == STATE_DISABLED);
1366
1367 efx->irq_soft_enabled = true;
1368 smp_wmb();
1369
1370 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001371 if (!channel->type->keep_eventq) {
1372 rc = efx_init_eventq(channel);
1373 if (rc)
1374 goto fail;
1375 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001376 efx_start_eventq(channel);
1377 }
1378
1379 efx_mcdi_mode_event(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +01001380
1381 return 0;
1382fail:
1383 end_channel = channel;
1384 efx_for_each_channel(channel, efx) {
1385 if (channel == end_channel)
1386 break;
1387 efx_stop_eventq(channel);
1388 if (!channel->type->keep_eventq)
1389 efx_fini_eventq(channel);
1390 }
1391
1392 return rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001393}
1394
1395static void efx_soft_disable_interrupts(struct efx_nic *efx)
1396{
1397 struct efx_channel *channel;
1398
1399 if (efx->state == STATE_DISABLED)
1400 return;
1401
1402 efx_mcdi_mode_poll(efx);
1403
1404 efx->irq_soft_enabled = false;
1405 smp_wmb();
1406
1407 if (efx->legacy_irq)
1408 synchronize_irq(efx->legacy_irq);
1409
1410 efx_for_each_channel(channel, efx) {
1411 if (channel->irq)
1412 synchronize_irq(channel->irq);
1413
1414 efx_stop_eventq(channel);
1415 if (!channel->type->keep_eventq)
1416 efx_fini_eventq(channel);
1417 }
Ben Hutchingscade7152013-08-27 23:12:31 +01001418
1419 /* Flush the asynchronous MCDI request queue */
1420 efx_mcdi_flush_async(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01001421}
1422
Jon Cooper261e4d92013-04-15 18:51:54 +01001423static int efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001424{
Jon Cooper261e4d92013-04-15 18:51:54 +01001425 struct efx_channel *channel, *end_channel;
1426 int rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001427
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001428 BUG_ON(efx->state == STATE_DISABLED);
1429
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001430 if (efx->eeh_disabled_legacy_irq) {
1431 enable_irq(efx->legacy_irq);
1432 efx->eeh_disabled_legacy_irq = false;
1433 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001434
Ben Hutchings86094f72013-08-21 19:51:04 +01001435 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001436
1437 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001438 if (channel->type->keep_eventq) {
1439 rc = efx_init_eventq(channel);
1440 if (rc)
1441 goto fail;
1442 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001443 }
1444
Jon Cooper261e4d92013-04-15 18:51:54 +01001445 rc = efx_soft_enable_interrupts(efx);
1446 if (rc)
1447 goto fail;
1448
1449 return 0;
1450
1451fail:
1452 end_channel = channel;
1453 efx_for_each_channel(channel, efx) {
1454 if (channel == end_channel)
1455 break;
1456 if (channel->type->keep_eventq)
1457 efx_fini_eventq(channel);
1458 }
1459
1460 efx->type->irq_disable_non_ev(efx);
1461
1462 return rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001463}
1464
Ben Hutchingsd8291182012-10-05 23:35:41 +01001465static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001466{
1467 struct efx_channel *channel;
1468
Ben Hutchingsd8291182012-10-05 23:35:41 +01001469 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001470
1471 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001472 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001473 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001474 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001475
Ben Hutchings86094f72013-08-21 19:51:04 +01001476 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001477}
1478
Ben Hutchings8ceee662008-04-27 12:55:59 +01001479static void efx_remove_interrupts(struct efx_nic *efx)
1480{
1481 struct efx_channel *channel;
1482
1483 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001484 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001485 channel->irq = 0;
1486 pci_disable_msi(efx->pci_dev);
1487 pci_disable_msix(efx->pci_dev);
1488
1489 /* Remove legacy interrupt */
1490 efx->legacy_irq = 0;
1491}
1492
Ben Hutchings8831da72008-09-01 12:47:48 +01001493static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001494{
Ben Hutchings602a5322011-05-16 17:32:39 +01001495 struct efx_channel *channel;
1496 struct efx_tx_queue *tx_queue;
1497
Ben Hutchings97653432011-01-12 18:26:56 +00001498 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001499 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001500
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001501 /* We need to mark which channels really have RX and TX
1502 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001503 * RX-only and TX-only channels.
1504 */
1505 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001506 if (channel->channel < efx->n_rx_channels)
1507 channel->rx_queue.core_index = channel->channel;
1508 else
1509 channel->rx_queue.core_index = -1;
1510
Ben Hutchings602a5322011-05-16 17:32:39 +01001511 efx_for_each_channel_tx_queue(tx_queue, channel)
1512 tx_queue->queue -= (efx->tx_channel_offset *
1513 EFX_TXQ_TYPES);
1514 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001515}
1516
1517static int efx_probe_nic(struct efx_nic *efx)
1518{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001519 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001520 int rc;
1521
Ben Hutchings62776d02010-06-23 11:30:07 +00001522 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001523
1524 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001525 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001526 if (rc)
1527 return rc;
1528
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001529 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001530 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001531 rc = efx_probe_interrupts(efx);
1532 if (rc)
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001533 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001534
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001535 rc = efx->type->dimension_resources(efx);
1536 if (rc)
1537 goto fail2;
Ben Hutchings28e47c42012-02-15 01:58:49 +00001538
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001539 if (efx->n_channels > 1)
1540 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001541 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001542 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001543 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001544
Ben Hutchings8831da72008-09-01 12:47:48 +01001545 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001546 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1547 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001548
1549 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001550 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1551 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001552
1553 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001554
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001555fail2:
1556 efx_remove_interrupts(efx);
1557fail1:
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001558 efx->type->remove(efx);
1559 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001560}
1561
1562static void efx_remove_nic(struct efx_nic *efx)
1563{
Ben Hutchings62776d02010-06-23 11:30:07 +00001564 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001565
1566 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001567 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001568}
1569
Ben Hutchingsadd72472012-11-08 01:46:53 +00001570static int efx_probe_filters(struct efx_nic *efx)
1571{
1572 int rc;
1573
1574 spin_lock_init(&efx->filter_lock);
1575
1576 rc = efx->type->filter_table_probe(efx);
1577 if (rc)
1578 return rc;
1579
1580#ifdef CONFIG_RFS_ACCEL
1581 if (efx->type->offload_features & NETIF_F_NTUPLE) {
1582 efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
1583 sizeof(*efx->rps_flow_id),
1584 GFP_KERNEL);
1585 if (!efx->rps_flow_id) {
1586 efx->type->filter_table_remove(efx);
1587 return -ENOMEM;
1588 }
1589 }
1590#endif
1591
1592 return 0;
1593}
1594
1595static void efx_remove_filters(struct efx_nic *efx)
1596{
1597#ifdef CONFIG_RFS_ACCEL
1598 kfree(efx->rps_flow_id);
1599#endif
1600 efx->type->filter_table_remove(efx);
1601}
1602
1603static void efx_restore_filters(struct efx_nic *efx)
1604{
1605 efx->type->filter_table_restore(efx);
1606}
1607
Ben Hutchings8ceee662008-04-27 12:55:59 +01001608/**************************************************************************
1609 *
1610 * NIC startup/shutdown
1611 *
1612 *************************************************************************/
1613
1614static int efx_probe_all(struct efx_nic *efx)
1615{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001616 int rc;
1617
Ben Hutchings8ceee662008-04-27 12:55:59 +01001618 rc = efx_probe_nic(efx);
1619 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001620 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001621 goto fail1;
1622 }
1623
Ben Hutchings8ceee662008-04-27 12:55:59 +01001624 rc = efx_probe_port(efx);
1625 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001626 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001627 goto fail2;
1628 }
1629
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001630 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1631 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1632 rc = -EINVAL;
1633 goto fail3;
1634 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001635 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001636
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001637 rc = efx_probe_filters(efx);
1638 if (rc) {
1639 netif_err(efx, probe, efx->net_dev,
1640 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001641 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001642 }
1643
Ben Hutchings7f967c02012-02-13 23:45:02 +00001644 rc = efx_probe_channels(efx);
1645 if (rc)
1646 goto fail4;
1647
Ben Hutchings8ceee662008-04-27 12:55:59 +01001648 return 0;
1649
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001650 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001651 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001652 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001653 efx_remove_port(efx);
1654 fail2:
1655 efx_remove_nic(efx);
1656 fail1:
1657 return rc;
1658}
1659
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001660/* If the interface is supposed to be running but is not, start
1661 * the hardware and software data path, regular activity for the port
1662 * (MAC statistics, link polling, etc.) and schedule the port to be
1663 * reconfigured. Interrupts must already be enabled. This function
1664 * is safe to call multiple times, so long as the NIC is not disabled.
1665 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001666 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001667static void efx_start_all(struct efx_nic *efx)
1668{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001669 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001670 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001671
1672 /* Check that it is appropriate to restart the interface. All
1673 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001674 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001675 return;
1676
Ben Hutchings8ceee662008-04-27 12:55:59 +01001677 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001678 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001679
Alexandre Rames626950d2013-01-14 17:20:22 +00001680 /* Start the hardware monitor if there is one */
1681 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001682 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1683 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001684
1685 /* If link state detection is normally event-driven, we have
1686 * to poll now because we could have missed a change
1687 */
1688 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001689 mutex_lock(&efx->mac_lock);
1690 if (efx->phy_op->poll(efx))
1691 efx_link_status_changed(efx);
1692 mutex_unlock(&efx->mac_lock);
1693 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001694
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001695 efx->type->start_stats(efx);
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001696 efx->type->pull_stats(efx);
1697 spin_lock_bh(&efx->stats_lock);
1698 efx->type->update_stats(efx, NULL, NULL);
1699 spin_unlock_bh(&efx->stats_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001700}
1701
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001702/* Quiesce the hardware and software data path, and regular activity
1703 * for the port without bringing the link down. Safe to call multiple
1704 * times with the NIC in almost any state, but interrupts should be
1705 * enabled. Requires the RTNL lock.
1706 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001707static void efx_stop_all(struct efx_nic *efx)
1708{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001709 EFX_ASSERT_RESET_SERIALISED(efx);
1710
1711 /* port_enabled can be read safely under the rtnl lock */
1712 if (!efx->port_enabled)
1713 return;
1714
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001715 /* update stats before we go down so we can accurately count
1716 * rx_nodesc_drops
1717 */
1718 efx->type->pull_stats(efx);
1719 spin_lock_bh(&efx->stats_lock);
1720 efx->type->update_stats(efx, NULL, NULL);
1721 spin_unlock_bh(&efx->stats_lock);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001722 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001723 efx_stop_port(efx);
1724
Ben Hutchings29c69a42013-01-28 19:01:06 +00001725 /* Stop the kernel transmit interface. This is only valid if
1726 * the device is stopped or detached; otherwise the watchdog
1727 * may fire immediately.
1728 */
1729 WARN_ON(netif_running(efx->net_dev) &&
1730 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001731 netif_tx_disable(efx->net_dev);
1732
1733 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001734}
1735
1736static void efx_remove_all(struct efx_nic *efx)
1737{
Ben Hutchings46426102010-09-10 06:42:33 +00001738 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001739 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001740 efx_remove_port(efx);
1741 efx_remove_nic(efx);
1742}
1743
Ben Hutchings8ceee662008-04-27 12:55:59 +01001744/**************************************************************************
1745 *
1746 * Interrupt moderation
1747 *
1748 **************************************************************************/
1749
Ben Hutchingscc180b62011-12-08 19:51:47 +00001750static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001751{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001752 if (usecs == 0)
1753 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001754 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001755 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001756 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001757}
1758
Ben Hutchings8ceee662008-04-27 12:55:59 +01001759/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001760int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1761 unsigned int rx_usecs, bool rx_adaptive,
1762 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001763{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001764 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001765 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1766 efx->timer_quantum_ns,
1767 1000);
1768 unsigned int tx_ticks;
1769 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001770
1771 EFX_ASSERT_RESET_SERIALISED(efx);
1772
Ben Hutchingscc180b62011-12-08 19:51:47 +00001773 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001774 return -EINVAL;
1775
Ben Hutchingscc180b62011-12-08 19:51:47 +00001776 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1777 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1778
Ben Hutchings9e393b32011-09-05 07:43:04 +00001779 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1780 !rx_may_override_tx) {
1781 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1782 "RX and TX IRQ moderation must be equal\n");
1783 return -EINVAL;
1784 }
1785
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001786 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001787 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001788 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001789 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001790 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001791 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001792 channel->irq_moderation = tx_ticks;
1793 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001794
1795 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001796}
1797
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001798void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1799 unsigned int *rx_usecs, bool *rx_adaptive)
1800{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001801 /* We must round up when converting ticks to microseconds
1802 * because we round down when converting the other way.
1803 */
1804
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001805 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001806 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1807 efx->timer_quantum_ns,
1808 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001809
1810 /* If channels are shared between RX and TX, so is IRQ
1811 * moderation. Otherwise, IRQ moderation is the same for all
1812 * TX channels and is not adaptive.
1813 */
1814 if (efx->tx_channel_offset == 0)
1815 *tx_usecs = *rx_usecs;
1816 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001817 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001818 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001819 efx->timer_quantum_ns,
1820 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001821}
1822
Ben Hutchings8ceee662008-04-27 12:55:59 +01001823/**************************************************************************
1824 *
1825 * Hardware monitor
1826 *
1827 **************************************************************************/
1828
Ben Hutchingse254c272010-09-20 08:44:10 +00001829/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001830static void efx_monitor(struct work_struct *data)
1831{
1832 struct efx_nic *efx = container_of(data, struct efx_nic,
1833 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001834
Ben Hutchings62776d02010-06-23 11:30:07 +00001835 netif_vdbg(efx, timer, efx->net_dev,
1836 "hardware monitor executing on CPU %d\n",
1837 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001838 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001839
Ben Hutchings8ceee662008-04-27 12:55:59 +01001840 /* If the mac_lock is already held then it is likely a port
1841 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001842 * most of the work of monitor() anyway. */
1843 if (mutex_trylock(&efx->mac_lock)) {
1844 if (efx->port_enabled)
1845 efx->type->monitor(efx);
1846 mutex_unlock(&efx->mac_lock);
1847 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001848
Ben Hutchings8ceee662008-04-27 12:55:59 +01001849 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1850 efx_monitor_interval);
1851}
1852
1853/**************************************************************************
1854 *
1855 * ioctls
1856 *
1857 *************************************************************************/
1858
1859/* Net device ioctl
1860 * Context: process, rtnl_lock() held.
1861 */
1862static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1863{
Ben Hutchings767e4682008-09-01 12:43:14 +01001864 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001865 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001866
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001867 if (cmd == SIOCSHWTSTAMP)
Ben Hutchings433dc9b2013-11-14 01:26:21 +00001868 return efx_ptp_set_ts_config(efx, ifr);
1869 if (cmd == SIOCGHWTSTAMP)
1870 return efx_ptp_get_ts_config(efx, ifr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001871
Ben Hutchings68e7f452009-04-29 08:05:08 +00001872 /* Convert phy_id from older PRTAD/DEVAD format */
1873 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1874 (data->phy_id & 0xfc00) == 0x0400)
1875 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1876
1877 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001878}
1879
1880/**************************************************************************
1881 *
1882 * NAPI interface
1883 *
1884 **************************************************************************/
1885
Ben Hutchings7f967c02012-02-13 23:45:02 +00001886static void efx_init_napi_channel(struct efx_channel *channel)
1887{
1888 struct efx_nic *efx = channel->efx;
1889
1890 channel->napi_dev = efx->net_dev;
1891 netif_napi_add(channel->napi_dev, &channel->napi_str,
1892 efx_poll, napi_weight);
1893}
1894
Ben Hutchingse8f14992010-12-07 19:47:34 +00001895static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001896{
1897 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001898
Ben Hutchings7f967c02012-02-13 23:45:02 +00001899 efx_for_each_channel(channel, efx)
1900 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001901}
1902
1903static void efx_fini_napi_channel(struct efx_channel *channel)
1904{
1905 if (channel->napi_dev)
1906 netif_napi_del(&channel->napi_str);
1907 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001908}
1909
1910static void efx_fini_napi(struct efx_nic *efx)
1911{
1912 struct efx_channel *channel;
1913
Ben Hutchingse8f14992010-12-07 19:47:34 +00001914 efx_for_each_channel(channel, efx)
1915 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001916}
1917
1918/**************************************************************************
1919 *
1920 * Kernel netpoll interface
1921 *
1922 *************************************************************************/
1923
1924#ifdef CONFIG_NET_POLL_CONTROLLER
1925
1926/* Although in the common case interrupts will be disabled, this is not
1927 * guaranteed. However, all our work happens inside the NAPI callback,
1928 * so no locking is required.
1929 */
1930static void efx_netpoll(struct net_device *net_dev)
1931{
Ben Hutchings767e4682008-09-01 12:43:14 +01001932 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001933 struct efx_channel *channel;
1934
Ben Hutchings64ee3122008-09-01 12:47:38 +01001935 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001936 efx_schedule_channel(channel);
1937}
1938
1939#endif
1940
1941/**************************************************************************
1942 *
1943 * Kernel net device interface
1944 *
1945 *************************************************************************/
1946
1947/* Context: process, rtnl_lock() held. */
1948static int efx_net_open(struct net_device *net_dev)
1949{
Ben Hutchings767e4682008-09-01 12:43:14 +01001950 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001951 int rc;
1952
Ben Hutchings62776d02010-06-23 11:30:07 +00001953 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1954 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001955
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001956 rc = efx_check_disabled(efx);
1957 if (rc)
1958 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001959 if (efx->phy_mode & PHY_MODE_SPECIAL)
1960 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001961 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1962 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001963
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001964 /* Notify the kernel of the link state polled during driver load,
1965 * before the monitor starts running */
1966 efx_link_status_changed(efx);
1967
Ben Hutchings8ceee662008-04-27 12:55:59 +01001968 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001969 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001970 return 0;
1971}
1972
1973/* Context: process, rtnl_lock() held.
1974 * Note that the kernel will ignore our return code; this method
1975 * should really be a void.
1976 */
1977static int efx_net_stop(struct net_device *net_dev)
1978{
Ben Hutchings767e4682008-09-01 12:43:14 +01001979 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001980
Ben Hutchings62776d02010-06-23 11:30:07 +00001981 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1982 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001983
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001984 /* Stop the device and flush all the channels */
1985 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001986
1987 return 0;
1988}
1989
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001990/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001991static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1992 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001993{
Ben Hutchings767e4682008-09-01 12:43:14 +01001994 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001995
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001996 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00001997 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001998 spin_unlock_bh(&efx->stats_lock);
1999
Ben Hutchings8ceee662008-04-27 12:55:59 +01002000 return stats;
2001}
2002
2003/* Context: netif_tx_lock held, BHs disabled. */
2004static void efx_watchdog(struct net_device *net_dev)
2005{
Ben Hutchings767e4682008-09-01 12:43:14 +01002006 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002007
Ben Hutchings62776d02010-06-23 11:30:07 +00002008 netif_err(efx, tx_err, efx->net_dev,
2009 "TX stuck with port_enabled=%d: resetting channels\n",
2010 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002011
Ben Hutchings739bb23d2008-11-04 20:35:36 +00002012 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002013}
2014
2015
2016/* Context: process, rtnl_lock() held. */
2017static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2018{
Ben Hutchings767e4682008-09-01 12:43:14 +01002019 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002020 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002021
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002022 rc = efx_check_disabled(efx);
2023 if (rc)
2024 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002025 if (new_mtu > EFX_MAX_MTU)
2026 return -EINVAL;
2027
Ben Hutchings62776d02010-06-23 11:30:07 +00002028 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002029
Ben Hutchings29c69a42013-01-28 19:01:06 +00002030 efx_device_detach_sync(efx);
2031 efx_stop_all(efx);
2032
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002033 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002034 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01002035 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002036 mutex_unlock(&efx->mac_lock);
2037
Ben Hutchings8ceee662008-04-27 12:55:59 +01002038 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00002039 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002040 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002041}
2042
2043static int efx_set_mac_address(struct net_device *net_dev, void *data)
2044{
Ben Hutchings767e4682008-09-01 12:43:14 +01002045 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002046 struct sockaddr *addr = data;
2047 char *new_addr = addr->sa_data;
2048
Ben Hutchings8ceee662008-04-27 12:55:59 +01002049 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002050 netif_err(efx, drv, efx->net_dev,
2051 "invalid ethernet MAC address requested: %pM\n",
2052 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002053 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002054 }
2055
2056 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002057 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002058
2059 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002060 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002061 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002062 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002063
2064 return 0;
2065}
2066
Ben Hutchingsa816f752008-09-01 12:49:12 +01002067/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002068static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002069{
Ben Hutchings767e4682008-09-01 12:43:14 +01002070 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002071
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002072 if (efx->port_enabled)
2073 queue_work(efx->workqueue, &efx->mac_work);
2074 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002075}
2076
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002077static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002078{
2079 struct efx_nic *efx = netdev_priv(net_dev);
2080
2081 /* If disabling RX n-tuple filtering, clear existing filters */
2082 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2083 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2084
2085 return 0;
2086}
2087
Ben Hutchings8127d662013-08-29 19:19:29 +01002088static const struct net_device_ops efx_farch_netdev_ops = {
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002089 .ndo_open = efx_net_open,
2090 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002091 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002092 .ndo_tx_timeout = efx_watchdog,
2093 .ndo_start_xmit = efx_hard_start_xmit,
2094 .ndo_validate_addr = eth_validate_addr,
2095 .ndo_do_ioctl = efx_ioctl,
2096 .ndo_change_mtu = efx_change_mtu,
2097 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002098 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002099 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002100#ifdef CONFIG_SFC_SRIOV
2101 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2102 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2103 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2104 .ndo_get_vf_config = efx_sriov_get_vf_config,
2105#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002106#ifdef CONFIG_NET_POLL_CONTROLLER
2107 .ndo_poll_controller = efx_netpoll,
2108#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002109 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002110#ifdef CONFIG_RFS_ACCEL
2111 .ndo_rx_flow_steer = efx_filter_rfs,
2112#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002113};
2114
Ben Hutchings8127d662013-08-29 19:19:29 +01002115static const struct net_device_ops efx_ef10_netdev_ops = {
2116 .ndo_open = efx_net_open,
2117 .ndo_stop = efx_net_stop,
2118 .ndo_get_stats64 = efx_net_stats,
2119 .ndo_tx_timeout = efx_watchdog,
2120 .ndo_start_xmit = efx_hard_start_xmit,
2121 .ndo_validate_addr = eth_validate_addr,
2122 .ndo_do_ioctl = efx_ioctl,
2123 .ndo_change_mtu = efx_change_mtu,
2124 .ndo_set_mac_address = efx_set_mac_address,
2125 .ndo_set_rx_mode = efx_set_rx_mode,
2126 .ndo_set_features = efx_set_features,
2127#ifdef CONFIG_NET_POLL_CONTROLLER
2128 .ndo_poll_controller = efx_netpoll,
2129#endif
2130#ifdef CONFIG_RFS_ACCEL
2131 .ndo_rx_flow_steer = efx_filter_rfs,
2132#endif
2133};
2134
Ben Hutchings7dde5962008-12-12 22:09:38 -08002135static void efx_update_name(struct efx_nic *efx)
2136{
2137 strcpy(efx->name, efx->net_dev->name);
2138 efx_mtd_rename(efx);
2139 efx_set_channel_names(efx);
2140}
2141
Ben Hutchings8ceee662008-04-27 12:55:59 +01002142static int efx_netdev_event(struct notifier_block *this,
2143 unsigned long event, void *ptr)
2144{
Jiri Pirko351638e2013-05-28 01:30:21 +00002145 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002146
Ben Hutchings8127d662013-08-29 19:19:29 +01002147 if ((net_dev->netdev_ops == &efx_farch_netdev_ops ||
2148 net_dev->netdev_ops == &efx_ef10_netdev_ops) &&
Ben Hutchings7dde5962008-12-12 22:09:38 -08002149 event == NETDEV_CHANGENAME)
2150 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002151
2152 return NOTIFY_DONE;
2153}
2154
2155static struct notifier_block efx_netdev_notifier = {
2156 .notifier_call = efx_netdev_event,
2157};
2158
Ben Hutchings06d5e192008-12-12 21:47:23 -08002159static ssize_t
2160show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2161{
2162 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2163 return sprintf(buf, "%d\n", efx->phy_type);
2164}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002165static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002166
Ben Hutchings8ceee662008-04-27 12:55:59 +01002167static int efx_register_netdev(struct efx_nic *efx)
2168{
2169 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002170 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002171 int rc;
2172
2173 net_dev->watchdog_timeo = 5 * HZ;
2174 net_dev->irq = efx->pci_dev->irq;
Ben Hutchings8127d662013-08-29 19:19:29 +01002175 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
2176 net_dev->netdev_ops = &efx_ef10_netdev_ops;
2177 net_dev->priv_flags |= IFF_UNICAST_FLT;
2178 } else {
2179 net_dev->netdev_ops = &efx_farch_netdev_ops;
2180 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002181 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002182 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002183
Ben Hutchings7dde5962008-12-12 22:09:38 -08002184 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002185
Ben Hutchings7153f622012-07-27 20:50:52 +01002186 /* Enable resets to be scheduled and check whether any were
2187 * already requested. If so, the NIC is probably hosed so we
2188 * abort.
2189 */
2190 efx->state = STATE_READY;
2191 smp_mb(); /* ensure we change state before checking reset_pending */
2192 if (efx->reset_pending) {
2193 netif_err(efx, probe, efx->net_dev,
2194 "aborting probe due to scheduled reset\n");
2195 rc = -EIO;
2196 goto fail_locked;
2197 }
2198
Ben Hutchingsaed06282009-08-26 08:16:27 +00002199 rc = dev_alloc_name(net_dev, net_dev->name);
2200 if (rc < 0)
2201 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002202 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002203
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002204 /* Always start with carrier off; PHY events will detect the link */
2205 netif_carrier_off(net_dev);
2206
Ben Hutchingsaed06282009-08-26 08:16:27 +00002207 rc = register_netdevice(net_dev);
2208 if (rc)
2209 goto fail_locked;
2210
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002211 efx_for_each_channel(channel, efx) {
2212 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002213 efx_for_each_channel_tx_queue(tx_queue, channel)
2214 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002215 }
2216
Ben Hutchings7dde5962008-12-12 22:09:38 -08002217 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002218
Ben Hutchings06d5e192008-12-12 21:47:23 -08002219 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2220 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002221 netif_err(efx, drv, efx->net_dev,
2222 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002223 goto fail_registered;
2224 }
2225
Ben Hutchings8ceee662008-04-27 12:55:59 +01002226 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002227
Ben Hutchings7153f622012-07-27 20:50:52 +01002228fail_registered:
2229 rtnl_lock();
2230 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002231fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002232 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002233 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002234 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002235 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002236}
2237
2238static void efx_unregister_netdev(struct efx_nic *efx)
2239{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002240 if (!efx->net_dev)
2241 return;
2242
Ben Hutchings767e4682008-09-01 12:43:14 +01002243 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002244
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002245 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2246 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002247
2248 rtnl_lock();
2249 unregister_netdevice(efx->net_dev);
2250 efx->state = STATE_UNINIT;
2251 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002252}
2253
2254/**************************************************************************
2255 *
2256 * Device reset and suspend
2257 *
2258 **************************************************************************/
2259
Ben Hutchings2467ca42008-09-01 12:48:50 +01002260/* Tears down the entire software state and most of the hardware state
2261 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002262void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002263{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002264 EFX_ASSERT_RESET_SERIALISED(efx);
2265
Ben Hutchings2467ca42008-09-01 12:48:50 +01002266 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002267 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002268
2269 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002270 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2271 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002272 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002273}
2274
Ben Hutchings2467ca42008-09-01 12:48:50 +01002275/* This function will always ensure that the locks acquired in
2276 * efx_reset_down() are released. A failure return code indicates
2277 * that we were unable to reinitialise the hardware, and the
2278 * driver should be disabled. If ok is false, then the rx and tx
2279 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002280int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002281{
2282 int rc;
2283
Ben Hutchings2467ca42008-09-01 12:48:50 +01002284 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002285
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002286 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002287 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002288 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002289 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002290 }
2291
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002292 if (!ok)
2293 goto fail;
2294
Steve Hodgson4b988282009-01-29 17:50:51 +00002295 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002296 rc = efx->phy_op->init(efx);
2297 if (rc)
2298 goto fail;
2299 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002300 netif_err(efx, drv, efx->net_dev,
2301 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002302 }
2303
Jon Cooper261e4d92013-04-15 18:51:54 +01002304 rc = efx_enable_interrupts(efx);
2305 if (rc)
2306 goto fail;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002307 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002308 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002309
2310 mutex_unlock(&efx->mac_lock);
2311
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002312 efx_start_all(efx);
2313
2314 return 0;
2315
2316fail:
2317 efx->port_initialized = false;
2318
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002319 mutex_unlock(&efx->mac_lock);
2320
Ben Hutchings8ceee662008-04-27 12:55:59 +01002321 return rc;
2322}
2323
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002324/* Reset the NIC using the specified method. Note that the reset may
2325 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002326 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002327 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002328 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002329int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002330{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002331 int rc, rc2;
2332 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002333
Ben Hutchings62776d02010-06-23 11:30:07 +00002334 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2335 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002336
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002337 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002338 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002339
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002340 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002341 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002342 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002343 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002344 }
2345
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002346 /* Clear flags for the scopes we covered. We assume the NIC and
2347 * driver are now quiescent so that there is no race here.
2348 */
2349 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002350
2351 /* Reinitialise bus-mastering, which may have been turned off before
2352 * the reset was scheduled. This is still appropriate, even in the
2353 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2354 * can respond to requests. */
2355 pci_set_master(efx->pci_dev);
2356
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002357out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002358 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002359 disabled = rc ||
2360 method == RESET_TYPE_DISABLE ||
2361 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002362 rc2 = efx_reset_up(efx, method, !disabled);
2363 if (rc2) {
2364 disabled = true;
2365 if (!rc)
2366 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002367 }
2368
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002369 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002370 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002371 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002372 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002373 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002374 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002375 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002376 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002377 return rc;
2378}
2379
Alexandre Rames626950d2013-01-14 17:20:22 +00002380/* Try recovery mechanisms.
2381 * For now only EEH is supported.
2382 * Returns 0 if the recovery mechanisms are unsuccessful.
2383 * Returns a non-zero value otherwise.
2384 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002385int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002386{
2387#ifdef CONFIG_EEH
2388 /* A PCI error can occur and not be seen by EEH because nothing
2389 * happens on the PCI bus. In this case the driver may fail and
2390 * schedule a 'recover or reset', leading to this recovery handler.
2391 * Manually call the eeh failure check function.
2392 */
2393 struct eeh_dev *eehdev =
2394 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2395
2396 if (eeh_dev_check_failure(eehdev)) {
2397 /* The EEH mechanisms will handle the error and reset the
2398 * device if necessary.
2399 */
2400 return 1;
2401 }
2402#endif
2403 return 0;
2404}
2405
Jon Cooper74cd60a2013-09-16 14:18:51 +01002406static void efx_wait_for_bist_end(struct efx_nic *efx)
2407{
2408 int i;
2409
2410 for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
2411 if (efx_mcdi_poll_reboot(efx))
2412 goto out;
2413 msleep(BIST_WAIT_DELAY_MS);
2414 }
2415
2416 netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
2417out:
2418 /* Either way unset the BIST flag. If we found no reboot we probably
2419 * won't recover, but we should try.
2420 */
2421 efx->mc_bist_for_other_fn = false;
2422}
2423
Ben Hutchings8ceee662008-04-27 12:55:59 +01002424/* The worker thread exists so that code that cannot sleep can
2425 * schedule a reset for later.
2426 */
2427static void efx_reset_work(struct work_struct *data)
2428{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002429 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002430 unsigned long pending;
2431 enum reset_type method;
2432
2433 pending = ACCESS_ONCE(efx->reset_pending);
2434 method = fls(pending) - 1;
2435
Jon Cooper74cd60a2013-09-16 14:18:51 +01002436 if (method == RESET_TYPE_MC_BIST)
2437 efx_wait_for_bist_end(efx);
2438
Alexandre Rames626950d2013-01-14 17:20:22 +00002439 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2440 method == RESET_TYPE_RECOVER_OR_ALL) &&
2441 efx_try_recovery(efx))
2442 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002443
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002444 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002445 return;
2446
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002447 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002448
2449 /* We checked the state in efx_schedule_reset() but it may
2450 * have changed by now. Now that we have the RTNL lock,
2451 * it cannot change again.
2452 */
2453 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002454 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002455
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002456 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002457}
2458
2459void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2460{
2461 enum reset_type method;
2462
Alexandre Rames626950d2013-01-14 17:20:22 +00002463 if (efx->state == STATE_RECOVERY) {
2464 netif_dbg(efx, drv, efx->net_dev,
2465 "recovering: skip scheduling %s reset\n",
2466 RESET_TYPE(type));
2467 return;
2468 }
2469
Ben Hutchings8ceee662008-04-27 12:55:59 +01002470 switch (type) {
2471 case RESET_TYPE_INVISIBLE:
2472 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002473 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002474 case RESET_TYPE_WORLD:
2475 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002476 case RESET_TYPE_RECOVER_OR_DISABLE:
Jon Cooper74cd60a2013-09-16 14:18:51 +01002477 case RESET_TYPE_MC_BIST:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002478 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002479 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2480 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002481 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002482 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002483 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002484 netif_dbg(efx, drv, efx->net_dev,
2485 "scheduling %s reset for %s\n",
2486 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002487 break;
2488 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002489
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002490 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002491 smp_mb(); /* ensure we change reset_pending before checking state */
2492
2493 /* If we're not READY then just leave the flags set as the cue
2494 * to abort probing or reschedule the reset later.
2495 */
2496 if (ACCESS_ONCE(efx->state) != STATE_READY)
2497 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002498
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002499 /* efx_process_channel() will no longer read events once a
2500 * reset is scheduled. So switch back to poll'd MCDI completions. */
2501 efx_mcdi_mode_poll(efx);
2502
Steve Hodgson1ab00622008-12-12 21:33:02 -08002503 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002504}
2505
2506/**************************************************************************
2507 *
2508 * List of NICs we support
2509 *
2510 **************************************************************************/
2511
2512/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002513static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002514 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2515 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002516 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002517 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2518 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002519 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002520 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002521 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002522 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002523 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8127d662013-08-29 19:19:29 +01002524 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
2525 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002526 {0} /* end of list */
2527};
2528
2529/**************************************************************************
2530 *
Ben Hutchings37594332009-11-23 16:05:45 +00002531 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002532 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002533 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002534 * Needed so all function pointers are valid and do not have to be tested
2535 * before use
2536 *
2537 **************************************************************************/
2538int efx_port_dummy_op_int(struct efx_nic *efx)
2539{
2540 return 0;
2541}
2542void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002543
2544static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002545{
2546 return false;
2547}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002548
stephen hemminger6c8c2512011-04-14 05:50:12 +00002549static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002550 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002551 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002552 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002553 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002554};
2555
Ben Hutchings8ceee662008-04-27 12:55:59 +01002556/**************************************************************************
2557 *
2558 * Data housekeeping
2559 *
2560 **************************************************************************/
2561
2562/* This zeroes out and then fills in the invariants in a struct
2563 * efx_nic (including all sub-structures).
2564 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002565static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002566 struct pci_dev *pci_dev, struct net_device *net_dev)
2567{
Ben Hutchings46426102010-09-10 06:42:33 +00002568 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002569
2570 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002571 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002572#ifdef CONFIG_SFC_MTD
2573 INIT_LIST_HEAD(&efx->mtd_list);
2574#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002575 INIT_WORK(&efx->reset_work, efx_reset_work);
2576 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002577 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002578 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002579 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002580 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002581 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002582
2583 efx->net_dev = net_dev;
Jon Cooper43a37392012-10-18 15:49:54 +01002584 efx->rx_prefix_size = efx->type->rx_prefix_size;
Andrew Rybchenko2ec03012013-11-16 11:02:27 +04002585 efx->rx_ip_align =
2586 NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
Jon Cooper43a37392012-10-18 15:49:54 +01002587 efx->rx_packet_hash_offset =
2588 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
Jon Cooperbd9a2652013-11-18 12:54:41 +00002589 efx->rx_packet_ts_offset =
2590 efx->type->rx_ts_offset - efx->type->rx_prefix_size;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002591 spin_lock_init(&efx->stats_lock);
2592 mutex_init(&efx->mac_lock);
2593 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002594 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002595 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002596 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002597
2598 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002599 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2600 if (!efx->channel[i])
2601 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002602 efx->msi_context[i].efx = efx;
2603 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002604 }
2605
Ben Hutchings8ceee662008-04-27 12:55:59 +01002606 /* Higher numbered interrupt modes are less capable! */
2607 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2608 interrupt_mode);
2609
Ben Hutchings6977dc62008-12-26 13:44:39 -08002610 /* Would be good to use the net_dev name, but we're too early */
2611 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2612 pci_name(pci_dev));
2613 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002614 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002615 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002616
Ben Hutchings8ceee662008-04-27 12:55:59 +01002617 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002618
2619fail:
2620 efx_fini_struct(efx);
2621 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002622}
2623
2624static void efx_fini_struct(struct efx_nic *efx)
2625{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002626 int i;
2627
2628 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2629 kfree(efx->channel[i]);
2630
Ben Hutchingsef215e62013-12-05 20:13:22 +00002631 kfree(efx->vpd_sn);
2632
Ben Hutchings8ceee662008-04-27 12:55:59 +01002633 if (efx->workqueue) {
2634 destroy_workqueue(efx->workqueue);
2635 efx->workqueue = NULL;
2636 }
2637}
2638
2639/**************************************************************************
2640 *
2641 * PCI interface
2642 *
2643 **************************************************************************/
2644
2645/* Main body of final NIC shutdown code
2646 * This is called only at module unload (or hotplug removal).
2647 */
2648static void efx_pci_remove_main(struct efx_nic *efx)
2649{
Ben Hutchings7153f622012-07-27 20:50:52 +01002650 /* Flush reset_work. It can no longer be scheduled since we
2651 * are not READY.
2652 */
2653 BUG_ON(efx->state == STATE_READY);
2654 cancel_work_sync(&efx->reset_work);
2655
Ben Hutchingsd8291182012-10-05 23:35:41 +01002656 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002657 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002658 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002659 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002660 efx_fini_napi(efx);
2661 efx_remove_all(efx);
2662}
2663
2664/* Final NIC shutdown
2665 * This is called only at module unload (or hotplug removal).
2666 */
2667static void efx_pci_remove(struct pci_dev *pci_dev)
2668{
2669 struct efx_nic *efx;
2670
2671 efx = pci_get_drvdata(pci_dev);
2672 if (!efx)
2673 return;
2674
2675 /* Mark the NIC as fini, then stop the interface */
2676 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002677 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002678 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002679 rtnl_unlock();
2680
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002681 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002682 efx_unregister_netdev(efx);
2683
Ben Hutchings7dde5962008-12-12 22:09:38 -08002684 efx_mtd_remove(efx);
2685
Ben Hutchings8ceee662008-04-27 12:55:59 +01002686 efx_pci_remove_main(efx);
2687
Ben Hutchings8ceee662008-04-27 12:55:59 +01002688 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002689 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002690
Ben Hutchings8ceee662008-04-27 12:55:59 +01002691 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002692 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002693 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002694
2695 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002696};
2697
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002698/* NIC VPD information
2699 * Called during probe to display the part number of the
2700 * installed NIC. VPD is potentially very large but this should
2701 * always appear within the first 512 bytes.
2702 */
2703#define SFC_VPD_LEN 512
Ben Hutchingsef215e62013-12-05 20:13:22 +00002704static void efx_probe_vpd_strings(struct efx_nic *efx)
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002705{
2706 struct pci_dev *dev = efx->pci_dev;
2707 char vpd_data[SFC_VPD_LEN];
2708 ssize_t vpd_size;
Ben Hutchingsef215e62013-12-05 20:13:22 +00002709 int ro_start, ro_size, i, j;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002710
2711 /* Get the vpd data from the device */
2712 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2713 if (vpd_size <= 0) {
2714 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2715 return;
2716 }
2717
2718 /* Get the Read only section */
Ben Hutchingsef215e62013-12-05 20:13:22 +00002719 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2720 if (ro_start < 0) {
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002721 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2722 return;
2723 }
2724
Ben Hutchingsef215e62013-12-05 20:13:22 +00002725 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
2726 j = ro_size;
2727 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002728 if (i + j > vpd_size)
2729 j = vpd_size - i;
2730
2731 /* Get the Part number */
2732 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2733 if (i < 0) {
2734 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2735 return;
2736 }
2737
2738 j = pci_vpd_info_field_size(&vpd_data[i]);
2739 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2740 if (i + j > vpd_size) {
2741 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2742 return;
2743 }
2744
2745 netif_info(efx, drv, efx->net_dev,
2746 "Part Number : %.*s\n", j, &vpd_data[i]);
Ben Hutchingsef215e62013-12-05 20:13:22 +00002747
2748 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
2749 j = ro_size;
2750 i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
2751 if (i < 0) {
2752 netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
2753 return;
2754 }
2755
2756 j = pci_vpd_info_field_size(&vpd_data[i]);
2757 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2758 if (i + j > vpd_size) {
2759 netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
2760 return;
2761 }
2762
2763 efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
2764 if (!efx->vpd_sn)
2765 return;
2766
2767 snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002768}
2769
2770
Ben Hutchings8ceee662008-04-27 12:55:59 +01002771/* Main body of NIC initialisation
2772 * This is called at module load (or hotplug insertion, theoretically).
2773 */
2774static int efx_pci_probe_main(struct efx_nic *efx)
2775{
2776 int rc;
2777
2778 /* Do start-of-day initialisation */
2779 rc = efx_probe_all(efx);
2780 if (rc)
2781 goto fail1;
2782
Ben Hutchingse8f14992010-12-07 19:47:34 +00002783 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002784
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002785 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002786 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002787 netif_err(efx, probe, efx->net_dev,
2788 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002789 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002790 }
2791
2792 rc = efx_init_port(efx);
2793 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002794 netif_err(efx, probe, efx->net_dev,
2795 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002796 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002797 }
2798
Ben Hutchings152b6a62009-11-29 03:43:56 +00002799 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002800 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002801 goto fail5;
Jon Cooper261e4d92013-04-15 18:51:54 +01002802 rc = efx_enable_interrupts(efx);
2803 if (rc)
2804 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002805
2806 return 0;
2807
Jon Cooper261e4d92013-04-15 18:51:54 +01002808 fail6:
2809 efx_nic_fini_interrupt(efx);
Ben Hutchings278c0622009-11-23 16:05:12 +00002810 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002811 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002812 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002813 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002814 fail3:
2815 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002816 efx_remove_all(efx);
2817 fail1:
2818 return rc;
2819}
2820
2821/* NIC initialisation
2822 *
2823 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002824 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002825 * sets up and registers the network devices with the kernel and hooks
2826 * the interrupt service routine. It does not prepare the device for
2827 * transmission; this is left to the first time one of the network
2828 * interfaces is brought up (i.e. efx_net_open).
2829 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002830static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002831 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002832{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002833 struct net_device *net_dev;
2834 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002835 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002836
2837 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002838 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2839 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002840 if (!net_dev)
2841 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002842 efx = netdev_priv(net_dev);
2843 efx->type = (const struct efx_nic_type *) entry->driver_data;
2844 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002845 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002846 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002847 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002848 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002849 /* Mask for features that also apply to VLAN devices */
2850 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002851 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2852 NETIF_F_RXCSUM);
2853 /* All offloads can be toggled */
2854 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002855 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002856 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002857 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002858 if (rc)
2859 goto fail1;
2860
Ben Hutchings62776d02010-06-23 11:30:07 +00002861 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002862 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002863
Ben Hutchingsef215e62013-12-05 20:13:22 +00002864 efx_probe_vpd_strings(efx);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002865
Ben Hutchings8ceee662008-04-27 12:55:59 +01002866 /* Set up basic I/O (BAR mappings etc) */
2867 rc = efx_init_io(efx);
2868 if (rc)
2869 goto fail2;
2870
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002871 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002872 if (rc)
2873 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002874
Ben Hutchings8ceee662008-04-27 12:55:59 +01002875 rc = efx_register_netdev(efx);
2876 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002877 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002878
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002879 rc = efx_sriov_init(efx);
2880 if (rc)
2881 netif_err(efx, probe, efx->net_dev,
2882 "SR-IOV can't be enabled rc %d\n", rc);
2883
Ben Hutchings62776d02010-06-23 11:30:07 +00002884 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002885
Ben Hutchings7c431612012-01-27 17:23:58 +00002886 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002887 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002888 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002889 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002890 if (rc)
2891 netif_warn(efx, probe, efx->net_dev,
2892 "failed to create MTDs (%d)\n", rc);
2893
Alexandre Rames626950d2013-01-14 17:20:22 +00002894 rc = pci_enable_pcie_error_reporting(pci_dev);
2895 if (rc && rc != -EINVAL)
2896 netif_warn(efx, probe, efx->net_dev,
2897 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2898
Ben Hutchings8ceee662008-04-27 12:55:59 +01002899 return 0;
2900
Ben Hutchings8ceee662008-04-27 12:55:59 +01002901 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002902 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002903 fail3:
2904 efx_fini_io(efx);
2905 fail2:
2906 efx_fini_struct(efx);
2907 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002908 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002909 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002910 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002911 free_netdev(net_dev);
2912 return rc;
2913}
2914
Ben Hutchings89c758f2009-11-29 03:43:07 +00002915static int efx_pm_freeze(struct device *dev)
2916{
2917 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2918
Ben Hutchings61da0262012-07-27 19:35:39 +01002919 rtnl_lock();
2920
Ben Hutchings6032fb52012-07-27 19:35:47 +01002921 if (efx->state != STATE_DISABLED) {
2922 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002923
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002924 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002925
Ben Hutchings6032fb52012-07-27 19:35:47 +01002926 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002927 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01002928 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002929
Ben Hutchings61da0262012-07-27 19:35:39 +01002930 rtnl_unlock();
2931
Ben Hutchings89c758f2009-11-29 03:43:07 +00002932 return 0;
2933}
2934
2935static int efx_pm_thaw(struct device *dev)
2936{
Jon Cooper261e4d92013-04-15 18:51:54 +01002937 int rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002938 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2939
Ben Hutchings61da0262012-07-27 19:35:39 +01002940 rtnl_lock();
2941
Ben Hutchings6032fb52012-07-27 19:35:47 +01002942 if (efx->state != STATE_DISABLED) {
Jon Cooper261e4d92013-04-15 18:51:54 +01002943 rc = efx_enable_interrupts(efx);
2944 if (rc)
2945 goto fail;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002946
Ben Hutchings6032fb52012-07-27 19:35:47 +01002947 mutex_lock(&efx->mac_lock);
2948 efx->phy_op->reconfigure(efx);
2949 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002950
Ben Hutchings6032fb52012-07-27 19:35:47 +01002951 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002952
Ben Hutchings6032fb52012-07-27 19:35:47 +01002953 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002954
Ben Hutchings6032fb52012-07-27 19:35:47 +01002955 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002956
Ben Hutchings6032fb52012-07-27 19:35:47 +01002957 efx->type->resume_wol(efx);
2958 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002959
Ben Hutchings61da0262012-07-27 19:35:39 +01002960 rtnl_unlock();
2961
Steve Hodgson319ba642010-06-01 11:17:24 +00002962 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2963 queue_work(reset_workqueue, &efx->reset_work);
2964
Ben Hutchings89c758f2009-11-29 03:43:07 +00002965 return 0;
Jon Cooper261e4d92013-04-15 18:51:54 +01002966
2967fail:
2968 rtnl_unlock();
2969
2970 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002971}
2972
2973static int efx_pm_poweroff(struct device *dev)
2974{
2975 struct pci_dev *pci_dev = to_pci_dev(dev);
2976 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2977
2978 efx->type->fini(efx);
2979
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002980 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002981
2982 pci_save_state(pci_dev);
2983 return pci_set_power_state(pci_dev, PCI_D3hot);
2984}
2985
2986/* Used for both resume and restore */
2987static int efx_pm_resume(struct device *dev)
2988{
2989 struct pci_dev *pci_dev = to_pci_dev(dev);
2990 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2991 int rc;
2992
2993 rc = pci_set_power_state(pci_dev, PCI_D0);
2994 if (rc)
2995 return rc;
2996 pci_restore_state(pci_dev);
2997 rc = pci_enable_device(pci_dev);
2998 if (rc)
2999 return rc;
3000 pci_set_master(efx->pci_dev);
3001 rc = efx->type->reset(efx, RESET_TYPE_ALL);
3002 if (rc)
3003 return rc;
3004 rc = efx->type->init(efx);
3005 if (rc)
3006 return rc;
Jon Cooper261e4d92013-04-15 18:51:54 +01003007 rc = efx_pm_thaw(dev);
3008 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003009}
3010
3011static int efx_pm_suspend(struct device *dev)
3012{
3013 int rc;
3014
3015 efx_pm_freeze(dev);
3016 rc = efx_pm_poweroff(dev);
3017 if (rc)
3018 efx_pm_resume(dev);
3019 return rc;
3020}
3021
Ben Hutchings18e83e42012-01-05 19:05:20 +00003022static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00003023 .suspend = efx_pm_suspend,
3024 .resume = efx_pm_resume,
3025 .freeze = efx_pm_freeze,
3026 .thaw = efx_pm_thaw,
3027 .poweroff = efx_pm_poweroff,
3028 .restore = efx_pm_resume,
3029};
3030
Alexandre Rames626950d2013-01-14 17:20:22 +00003031/* A PCI error affecting this device was detected.
3032 * At this point MMIO and DMA may be disabled.
3033 * Stop the software path and request a slot reset.
3034 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003035static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
3036 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00003037{
3038 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3039 struct efx_nic *efx = pci_get_drvdata(pdev);
3040
3041 if (state == pci_channel_io_perm_failure)
3042 return PCI_ERS_RESULT_DISCONNECT;
3043
3044 rtnl_lock();
3045
3046 if (efx->state != STATE_DISABLED) {
3047 efx->state = STATE_RECOVERY;
3048 efx->reset_pending = 0;
3049
3050 efx_device_detach_sync(efx);
3051
3052 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003053 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00003054
3055 status = PCI_ERS_RESULT_NEED_RESET;
3056 } else {
3057 /* If the interface is disabled we don't want to do anything
3058 * with it.
3059 */
3060 status = PCI_ERS_RESULT_RECOVERED;
3061 }
3062
3063 rtnl_unlock();
3064
3065 pci_disable_device(pdev);
3066
3067 return status;
3068}
3069
3070/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003071static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00003072{
3073 struct efx_nic *efx = pci_get_drvdata(pdev);
3074 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3075 int rc;
3076
3077 if (pci_enable_device(pdev)) {
3078 netif_err(efx, hw, efx->net_dev,
3079 "Cannot re-enable PCI device after reset.\n");
3080 status = PCI_ERS_RESULT_DISCONNECT;
3081 }
3082
3083 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
3084 if (rc) {
3085 netif_err(efx, hw, efx->net_dev,
3086 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3087 /* Non-fatal error. Continue. */
3088 }
3089
3090 return status;
3091}
3092
3093/* Perform the actual reset and resume I/O operations. */
3094static void efx_io_resume(struct pci_dev *pdev)
3095{
3096 struct efx_nic *efx = pci_get_drvdata(pdev);
3097 int rc;
3098
3099 rtnl_lock();
3100
3101 if (efx->state == STATE_DISABLED)
3102 goto out;
3103
3104 rc = efx_reset(efx, RESET_TYPE_ALL);
3105 if (rc) {
3106 netif_err(efx, hw, efx->net_dev,
3107 "efx_reset failed after PCI error (%d)\n", rc);
3108 } else {
3109 efx->state = STATE_READY;
3110 netif_dbg(efx, hw, efx->net_dev,
3111 "Done resetting and resuming IO after PCI error.\n");
3112 }
3113
3114out:
3115 rtnl_unlock();
3116}
3117
3118/* For simplicity and reliability, we always require a slot reset and try to
3119 * reset the hardware when a pci error affecting the device is detected.
3120 * We leave both the link_reset and mmio_enabled callback unimplemented:
3121 * with our request for slot reset the mmio_enabled callback will never be
3122 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3123 */
3124static struct pci_error_handlers efx_err_handlers = {
3125 .error_detected = efx_io_error_detected,
3126 .slot_reset = efx_io_slot_reset,
3127 .resume = efx_io_resume,
3128};
3129
Ben Hutchings8ceee662008-04-27 12:55:59 +01003130static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003131 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003132 .id_table = efx_pci_table,
3133 .probe = efx_pci_probe,
3134 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003135 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003136 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003137};
3138
3139/**************************************************************************
3140 *
3141 * Kernel module interface
3142 *
3143 *************************************************************************/
3144
3145module_param(interrupt_mode, uint, 0444);
3146MODULE_PARM_DESC(interrupt_mode,
3147 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3148
3149static int __init efx_init_module(void)
3150{
3151 int rc;
3152
3153 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3154
3155 rc = register_netdevice_notifier(&efx_netdev_notifier);
3156 if (rc)
3157 goto err_notifier;
3158
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003159 rc = efx_init_sriov();
3160 if (rc)
3161 goto err_sriov;
3162
Steve Hodgson1ab00622008-12-12 21:33:02 -08003163 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3164 if (!reset_workqueue) {
3165 rc = -ENOMEM;
3166 goto err_reset;
3167 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003168
3169 rc = pci_register_driver(&efx_pci_driver);
3170 if (rc < 0)
3171 goto err_pci;
3172
3173 return 0;
3174
3175 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003176 destroy_workqueue(reset_workqueue);
3177 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003178 efx_fini_sriov();
3179 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003180 unregister_netdevice_notifier(&efx_netdev_notifier);
3181 err_notifier:
3182 return rc;
3183}
3184
3185static void __exit efx_exit_module(void)
3186{
3187 printk(KERN_INFO "Solarflare NET driver unloading\n");
3188
3189 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003190 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003191 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003192 unregister_netdevice_notifier(&efx_netdev_notifier);
3193
3194}
3195
3196module_init(efx_init_module);
3197module_exit(efx_exit_module);
3198
Ben Hutchings906bb262009-11-29 15:16:19 +00003199MODULE_AUTHOR("Solarflare Communications and "
3200 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003201MODULE_DESCRIPTION("Solarflare Communications network driver");
3202MODULE_LICENSE("GPL");
3203MODULE_DEVICE_TABLE(pci, efx_pci_table);