blob: 57b971e5e6b2429bce3b8cb319df97e3e44912dd [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
Ben Hutchings8ceee662008-04-27 12:55:59 +0100506 return 0;
507
Ben Hutchings7f967c02012-02-13 23:45:02 +0000508fail:
509 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100510 return rc;
511}
512
Ben Hutchings7f967c02012-02-13 23:45:02 +0000513static void
514efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
515{
516 struct efx_nic *efx = channel->efx;
517 const char *type;
518 int number;
519
520 number = channel->channel;
521 if (efx->tx_channel_offset == 0) {
522 type = "";
523 } else if (channel->channel < efx->tx_channel_offset) {
524 type = "-rx";
525 } else {
526 type = "-tx";
527 number -= efx->tx_channel_offset;
528 }
529 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
530}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100531
Ben Hutchings56536e92008-12-12 21:37:02 -0800532static void efx_set_channel_names(struct efx_nic *efx)
533{
534 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800535
Ben Hutchings7f967c02012-02-13 23:45:02 +0000536 efx_for_each_channel(channel, efx)
537 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100538 efx->msi_context[channel->channel].name,
539 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800540}
541
Ben Hutchings46426102010-09-10 06:42:33 +0000542static int efx_probe_channels(struct efx_nic *efx)
543{
544 struct efx_channel *channel;
545 int rc;
546
547 /* Restart special buffer allocation */
548 efx->next_buffer_table = 0;
549
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000550 /* Probe channels in reverse, so that any 'extra' channels
551 * use the start of the buffer table. This allows the traffic
552 * channels to be resized without moving them or wasting the
553 * entries before them.
554 */
555 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000556 rc = efx_probe_channel(channel);
557 if (rc) {
558 netif_err(efx, probe, efx->net_dev,
559 "failed to create channel %d\n",
560 channel->channel);
561 goto fail;
562 }
563 }
564 efx_set_channel_names(efx);
565
566 return 0;
567
568fail:
569 efx_remove_channels(efx);
570 return rc;
571}
572
Ben Hutchings8ceee662008-04-27 12:55:59 +0100573/* Channels are shutdown and reinitialised whilst the NIC is running
574 * to propagate configuration changes (mtu, checksum offload), or
575 * to clear hardware error conditions
576 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000577static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100578{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000579 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100580 struct efx_tx_queue *tx_queue;
581 struct efx_rx_queue *rx_queue;
582 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000583 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100584
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100585 /* Calculate the rx buffer allocation parameters required to
586 * support the current MTU, including padding for header
587 * alignment and overruns.
588 */
Jon Cooper43a37392012-10-18 15:49:54 +0100589 efx->rx_dma_len = (efx->rx_prefix_size +
Ben Hutchings272baee2013-01-29 23:33:14 +0000590 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
591 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000592 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Andrew Rybchenko2ec03012013-11-16 11:02:27 +0400593 efx->rx_ip_align + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000594 if (rx_buf_len <= PAGE_SIZE) {
Jon Coopere8c68c02013-03-08 10:18:28 +0000595 efx->rx_scatter = efx->type->always_rx_scatter;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000596 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000597 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000598 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000599 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000600 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
601 EFX_RX_BUF_ALIGNMENT) >
602 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000603 efx->rx_scatter = true;
604 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
605 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000606 } else {
607 efx->rx_scatter = false;
608 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000609 }
610
Daniel Pieczko1648a232013-02-13 10:54:41 +0000611 efx_rx_config_page_split(efx);
612 if (efx->rx_buffer_order)
613 netif_dbg(efx, drv, efx->net_dev,
614 "RX buf len=%u; page order=%u batch=%u\n",
615 efx->rx_dma_len, efx->rx_buffer_order,
616 efx->rx_pages_per_batch);
617 else
618 netif_dbg(efx, drv, efx->net_dev,
619 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
620 efx->rx_dma_len, efx->rx_page_buf_step,
621 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000622
Jon Coopere8c68c02013-03-08 10:18:28 +0000623 /* RX filters may also have scatter-enabled flags */
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000624 if (efx->rx_scatter != old_rx_scatter)
Ben Hutchingsadd72472012-11-08 01:46:53 +0000625 efx->type->filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100626
Ben Hutchings14bf7182012-05-22 01:27:58 +0100627 /* We must keep at least one descriptor in a TX ring empty.
628 * We could avoid this when the queue size does not exactly
629 * match the hardware ring size, but it's not that important.
630 * Therefore we stop the queue when one more skb might fill
631 * the ring completely. We wake it when half way back to
632 * empty.
633 */
634 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
635 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
636
Ben Hutchings8ceee662008-04-27 12:55:59 +0100637 /* Initialise the channels */
638 efx_for_each_channel(channel, efx) {
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100639 efx_for_each_channel_tx_queue(tx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100640 efx_init_tx_queue(tx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100641 atomic_inc(&efx->active_queues);
642 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100643
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000644 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100645 efx_init_rx_queue(rx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100646 atomic_inc(&efx->active_queues);
Jon Coopercce28792013-10-02 11:04:14 +0100647 efx_stop_eventq(channel);
648 efx_fast_push_rx_descriptors(rx_queue, false);
649 efx_start_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000650 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100651
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000652 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100653 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000654
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000655 efx_ptp_start_datapath(efx);
656
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000657 if (netif_device_present(efx->net_dev))
658 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100659}
660
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000661static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100662{
663 struct efx_channel *channel;
664 struct efx_tx_queue *tx_queue;
665 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100666 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100667
668 EFX_ASSERT_RESET_SERIALISED(efx);
669 BUG_ON(efx->port_enabled);
670
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000671 efx_ptp_stop_datapath(efx);
672
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100673 /* Stop RX refill */
674 efx_for_each_channel(channel, efx) {
675 efx_for_each_channel_rx_queue(rx_queue, channel)
676 rx_queue->refill_enabled = false;
677 }
678
Ben Hutchings8ceee662008-04-27 12:55:59 +0100679 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000680 /* RX packet processing is pipelined, so wait for the
681 * NAPI handler to complete. At least event queue 0
682 * might be kept active by non-data events, so don't
683 * use napi_synchronize() but actually disable NAPI
684 * temporarily.
685 */
686 if (efx_channel_has_rx_queue(channel)) {
687 efx_stop_eventq(channel);
688 efx_start_eventq(channel);
689 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100690 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100691
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100692 rc = efx->type->fini_dmaq(efx);
693 if (rc && EFX_WORKAROUND_7803(efx)) {
694 /* Schedule a reset to recover from the flush failure. The
695 * descriptor caches reference memory we're about to free,
696 * but falcon_reconfigure_mac_wrapper() won't reconnect
697 * the MACs because of the pending reset.
698 */
699 netif_err(efx, drv, efx->net_dev,
700 "Resetting to recover from flush failure\n");
701 efx_schedule_reset(efx, RESET_TYPE_ALL);
702 } else if (rc) {
703 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
704 } else {
705 netif_dbg(efx, drv, efx->net_dev,
706 "successfully flushed all queues\n");
707 }
708
709 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100710 efx_for_each_channel_rx_queue(rx_queue, channel)
711 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000712 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100713 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100714 }
715}
716
717static void efx_remove_channel(struct efx_channel *channel)
718{
719 struct efx_tx_queue *tx_queue;
720 struct efx_rx_queue *rx_queue;
721
Ben Hutchings62776d02010-06-23 11:30:07 +0000722 netif_dbg(channel->efx, drv, channel->efx->net_dev,
723 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100724
725 efx_for_each_channel_rx_queue(rx_queue, channel)
726 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000727 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100728 efx_remove_tx_queue(tx_queue);
729 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100730 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100731}
732
Ben Hutchings46426102010-09-10 06:42:33 +0000733static void efx_remove_channels(struct efx_nic *efx)
734{
735 struct efx_channel *channel;
736
737 efx_for_each_channel(channel, efx)
738 efx_remove_channel(channel);
739}
740
741int
742efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
743{
744 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
745 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000746 unsigned i, next_buffer_table = 0;
Jon Cooper261e4d92013-04-15 18:51:54 +0100747 int rc, rc2;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100748
749 rc = efx_check_disabled(efx);
750 if (rc)
751 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000752
753 /* Not all channels should be reallocated. We must avoid
754 * reallocating their buffer table entries.
755 */
756 efx_for_each_channel(channel, efx) {
757 struct efx_rx_queue *rx_queue;
758 struct efx_tx_queue *tx_queue;
759
760 if (channel->type->copy)
761 continue;
762 next_buffer_table = max(next_buffer_table,
763 channel->eventq.index +
764 channel->eventq.entries);
765 efx_for_each_channel_rx_queue(rx_queue, channel)
766 next_buffer_table = max(next_buffer_table,
767 rx_queue->rxd.index +
768 rx_queue->rxd.entries);
769 efx_for_each_channel_tx_queue(tx_queue, channel)
770 next_buffer_table = max(next_buffer_table,
771 tx_queue->txd.index +
772 tx_queue->txd.entries);
773 }
Ben Hutchings46426102010-09-10 06:42:33 +0000774
Ben Hutchings29c69a42013-01-28 19:01:06 +0000775 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000776 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100777 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000778
Ben Hutchings7f967c02012-02-13 23:45:02 +0000779 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000780 memset(other_channel, 0, sizeof(other_channel));
781 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000782 channel = efx->channel[i];
783 if (channel->type->copy)
784 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000785 if (!channel) {
786 rc = -ENOMEM;
787 goto out;
788 }
789 other_channel[i] = channel;
790 }
791
792 /* Swap entry counts and channel pointers */
793 old_rxq_entries = efx->rxq_entries;
794 old_txq_entries = efx->txq_entries;
795 efx->rxq_entries = rxq_entries;
796 efx->txq_entries = txq_entries;
797 for (i = 0; i < efx->n_channels; i++) {
798 channel = efx->channel[i];
799 efx->channel[i] = other_channel[i];
800 other_channel[i] = channel;
801 }
802
Ben Hutchings7f967c02012-02-13 23:45:02 +0000803 /* Restart buffer table allocation */
804 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000805
Ben Hutchingse8f14992010-12-07 19:47:34 +0000806 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000807 channel = efx->channel[i];
808 if (!channel->type->copy)
809 continue;
810 rc = efx_probe_channel(channel);
811 if (rc)
812 goto rollback;
813 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000814 }
Ben Hutchings46426102010-09-10 06:42:33 +0000815
Ben Hutchings7f967c02012-02-13 23:45:02 +0000816out:
817 /* Destroy unused channel structures */
818 for (i = 0; i < efx->n_channels; i++) {
819 channel = other_channel[i];
820 if (channel && channel->type->copy) {
821 efx_fini_napi_channel(channel);
822 efx_remove_channel(channel);
823 kfree(channel);
824 }
825 }
826
Jon Cooper261e4d92013-04-15 18:51:54 +0100827 rc2 = efx_soft_enable_interrupts(efx);
828 if (rc2) {
829 rc = rc ? rc : rc2;
830 netif_err(efx, drv, efx->net_dev,
831 "unable to restart interrupts on channel reallocation\n");
832 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
833 } else {
834 efx_start_all(efx);
835 netif_device_attach(efx->net_dev);
836 }
Ben Hutchings46426102010-09-10 06:42:33 +0000837 return rc;
838
839rollback:
840 /* Swap back */
841 efx->rxq_entries = old_rxq_entries;
842 efx->txq_entries = old_txq_entries;
843 for (i = 0; i < efx->n_channels; i++) {
844 channel = efx->channel[i];
845 efx->channel[i] = other_channel[i];
846 other_channel[i] = channel;
847 }
848 goto out;
849}
850
Steve Hodgson90d683a2010-06-01 11:19:39 +0000851void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100852{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000853 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100854}
855
Ben Hutchings7f967c02012-02-13 23:45:02 +0000856static const struct efx_channel_type efx_default_channel_type = {
857 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100858 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000859 .get_name = efx_get_channel_name,
860 .copy = efx_copy_channel,
861 .keep_eventq = false,
862};
863
864int efx_channel_dummy_op_int(struct efx_channel *channel)
865{
866 return 0;
867}
868
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100869void efx_channel_dummy_op_void(struct efx_channel *channel)
870{
871}
872
Ben Hutchings8ceee662008-04-27 12:55:59 +0100873/**************************************************************************
874 *
875 * Port handling
876 *
877 **************************************************************************/
878
879/* This ensures that the kernel is kept informed (via
880 * netif_carrier_on/off) of the link status, and also maintains the
881 * link status's stop on the port's TX queue.
882 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000883void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100884{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000885 struct efx_link_state *link_state = &efx->link_state;
886
Ben Hutchings8ceee662008-04-27 12:55:59 +0100887 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
888 * that no events are triggered between unregister_netdev() and the
889 * driver unloading. A more general condition is that NETDEV_CHANGE
890 * can only be generated between NETDEV_UP and NETDEV_DOWN */
891 if (!netif_running(efx->net_dev))
892 return;
893
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000894 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100895 efx->n_link_state_changes++;
896
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000897 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100898 netif_carrier_on(efx->net_dev);
899 else
900 netif_carrier_off(efx->net_dev);
901 }
902
903 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000904 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000905 netif_info(efx, link, efx->net_dev,
Ben Hutchings964e6132012-11-19 23:08:22 +0000906 "link up at %uMbps %s-duplex (MTU %d)\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000907 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings964e6132012-11-19 23:08:22 +0000908 efx->net_dev->mtu);
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000909 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000910 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100911}
912
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000913void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
914{
915 efx->link_advertising = advertising;
916 if (advertising) {
917 if (advertising & ADVERTISED_Pause)
918 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
919 else
920 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
921 if (advertising & ADVERTISED_Asym_Pause)
922 efx->wanted_fc ^= EFX_FC_TX;
923 }
924}
925
David S. Millerb56269462011-05-17 17:53:22 -0400926void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000927{
928 efx->wanted_fc = wanted_fc;
929 if (efx->link_advertising) {
930 if (wanted_fc & EFX_FC_RX)
931 efx->link_advertising |= (ADVERTISED_Pause |
932 ADVERTISED_Asym_Pause);
933 else
934 efx->link_advertising &= ~(ADVERTISED_Pause |
935 ADVERTISED_Asym_Pause);
936 if (wanted_fc & EFX_FC_TX)
937 efx->link_advertising ^= ADVERTISED_Asym_Pause;
938 }
939}
940
Ben Hutchings115122a2009-03-04 09:52:52 +0000941static void efx_fini_port(struct efx_nic *efx);
942
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000943/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
944 * the MAC appropriately. All other PHY configuration changes are pushed
945 * through phy_op->set_settings(), and pushed asynchronously to the MAC
946 * through efx_monitor().
947 *
948 * Callers must hold the mac_lock
949 */
950int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100951{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000952 enum efx_phy_mode phy_mode;
953 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100954
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000955 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100956
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000957 /* Disable PHY transmit in mac level loopbacks */
958 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800959 if (LOOPBACK_INTERNAL(efx))
960 efx->phy_mode |= PHY_MODE_TX_DISABLED;
961 else
962 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800963
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000964 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800965
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000966 if (rc)
967 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100968
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000969 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100970}
971
972/* Reinitialise the MAC to pick up new PHY settings, even if the port is
973 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000974int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100975{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000976 int rc;
977
Ben Hutchings8ceee662008-04-27 12:55:59 +0100978 EFX_ASSERT_RESET_SERIALISED(efx);
979
980 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000981 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100982 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000983
984 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100985}
986
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000987/* Asynchronous work item for changing MAC promiscuity and multicast
988 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
989 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800990static void efx_mac_work(struct work_struct *data)
991{
992 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
993
994 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100995 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100996 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800997 mutex_unlock(&efx->mac_lock);
998}
999
Ben Hutchings8ceee662008-04-27 12:55:59 +01001000static int efx_probe_port(struct efx_nic *efx)
1001{
1002 int rc;
1003
Ben Hutchings62776d02010-06-23 11:30:07 +00001004 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001005
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001006 if (phy_flash_cfg)
1007 efx->phy_mode = PHY_MODE_SPECIAL;
1008
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001009 /* Connect up MAC/PHY operations table */
1010 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001011 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001012 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001013
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001014 /* Initialise MAC address to permanent address */
Edward Creecd84ff42014-03-07 18:27:41 +00001015 ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001016
1017 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001018}
1019
1020static int efx_init_port(struct efx_nic *efx)
1021{
1022 int rc;
1023
Ben Hutchings62776d02010-06-23 11:30:07 +00001024 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001025
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001026 mutex_lock(&efx->mac_lock);
1027
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001028 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001029 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001030 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001031
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001032 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001033
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001034 /* Reconfigure the MAC before creating dma queues (required for
1035 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001036 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001037
1038 /* Ensure the PHY advertises the correct flow control settings */
1039 rc = efx->phy_op->reconfigure(efx);
1040 if (rc)
1041 goto fail2;
1042
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001043 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001044 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001045
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001046fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001047 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001048fail1:
1049 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001050 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001051}
1052
Ben Hutchings8ceee662008-04-27 12:55:59 +01001053static void efx_start_port(struct efx_nic *efx)
1054{
Ben Hutchings62776d02010-06-23 11:30:07 +00001055 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001056 BUG_ON(efx->port_enabled);
1057
1058 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001059 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001060
Ben Hutchingsd615c032013-10-08 17:33:20 +01001061 /* Ensure MAC ingress/egress is enabled */
Ben Hutchings710b2082011-09-03 00:15:00 +01001062 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001063
Ben Hutchings8ceee662008-04-27 12:55:59 +01001064 mutex_unlock(&efx->mac_lock);
1065}
1066
Ben Hutchingsd615c032013-10-08 17:33:20 +01001067/* Cancel work for MAC reconfiguration, periodic hardware monitoring
1068 * and the async self-test, wait for them to finish and prevent them
1069 * being scheduled again. This doesn't cover online resets, which
1070 * should only be cancelled when removing the device.
1071 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001072static void efx_stop_port(struct efx_nic *efx)
1073{
Ben Hutchings62776d02010-06-23 11:30:07 +00001074 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001075
Ben Hutchingsd615c032013-10-08 17:33:20 +01001076 EFX_ASSERT_RESET_SERIALISED(efx);
1077
Ben Hutchings8ceee662008-04-27 12:55:59 +01001078 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001079 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001080 mutex_unlock(&efx->mac_lock);
1081
1082 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001083 netif_addr_lock_bh(efx->net_dev);
1084 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsd615c032013-10-08 17:33:20 +01001085
1086 cancel_delayed_work_sync(&efx->monitor_work);
1087 efx_selftest_async_cancel(efx);
1088 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001089}
1090
1091static void efx_fini_port(struct efx_nic *efx)
1092{
Ben Hutchings62776d02010-06-23 11:30:07 +00001093 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001094
1095 if (!efx->port_initialized)
1096 return;
1097
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001098 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001099 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001100
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001101 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001102 efx_link_status_changed(efx);
1103}
1104
1105static void efx_remove_port(struct efx_nic *efx)
1106{
Ben Hutchings62776d02010-06-23 11:30:07 +00001107 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001108
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001109 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001110}
1111
1112/**************************************************************************
1113 *
1114 * NIC handling
1115 *
1116 **************************************************************************/
1117
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01001118static LIST_HEAD(efx_primary_list);
1119static LIST_HEAD(efx_unassociated_list);
1120
1121static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
1122{
1123 return left->type == right->type &&
1124 left->vpd_sn && right->vpd_sn &&
1125 !strcmp(left->vpd_sn, right->vpd_sn);
1126}
1127
1128static void efx_associate(struct efx_nic *efx)
1129{
1130 struct efx_nic *other, *next;
1131
1132 if (efx->primary == efx) {
1133 /* Adding primary function; look for secondaries */
1134
1135 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
1136 list_add_tail(&efx->node, &efx_primary_list);
1137
1138 list_for_each_entry_safe(other, next, &efx_unassociated_list,
1139 node) {
1140 if (efx_same_controller(efx, other)) {
1141 list_del(&other->node);
1142 netif_dbg(other, probe, other->net_dev,
1143 "moving to secondary list of %s %s\n",
1144 pci_name(efx->pci_dev),
1145 efx->net_dev->name);
1146 list_add_tail(&other->node,
1147 &efx->secondary_list);
1148 other->primary = efx;
1149 }
1150 }
1151 } else {
1152 /* Adding secondary function; look for primary */
1153
1154 list_for_each_entry(other, &efx_primary_list, node) {
1155 if (efx_same_controller(efx, other)) {
1156 netif_dbg(efx, probe, efx->net_dev,
1157 "adding to secondary list of %s %s\n",
1158 pci_name(other->pci_dev),
1159 other->net_dev->name);
1160 list_add_tail(&efx->node,
1161 &other->secondary_list);
1162 efx->primary = other;
1163 return;
1164 }
1165 }
1166
1167 netif_dbg(efx, probe, efx->net_dev,
1168 "adding to unassociated list\n");
1169 list_add_tail(&efx->node, &efx_unassociated_list);
1170 }
1171}
1172
1173static void efx_dissociate(struct efx_nic *efx)
1174{
1175 struct efx_nic *other, *next;
1176
1177 list_del(&efx->node);
1178 efx->primary = NULL;
1179
1180 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
1181 list_del(&other->node);
1182 netif_dbg(other, probe, other->net_dev,
1183 "moving to unassociated list\n");
1184 list_add_tail(&other->node, &efx_unassociated_list);
1185 other->primary = NULL;
1186 }
1187}
1188
Ben Hutchings8ceee662008-04-27 12:55:59 +01001189/* This configures the PCI device to enable I/O and DMA. */
1190static int efx_init_io(struct efx_nic *efx)
1191{
1192 struct pci_dev *pci_dev = efx->pci_dev;
1193 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001194 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001195 int rc;
1196
Ben Hutchings62776d02010-06-23 11:30:07 +00001197 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001198
1199 rc = pci_enable_device(pci_dev);
1200 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001201 netif_err(efx, probe, efx->net_dev,
1202 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001203 goto fail1;
1204 }
1205
1206 pci_set_master(pci_dev);
1207
1208 /* Set the PCI DMA mask. Try all possibilities from our
1209 * genuine mask down to 32 bits, because some architectures
1210 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1211 * masks event though they reject 46 bit masks.
1212 */
1213 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001214 if (dma_supported(&pci_dev->dev, dma_mask)) {
Russell King9663ded2013-06-26 23:49:11 +01001215 rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001216 if (rc == 0)
1217 break;
1218 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001219 dma_mask >>= 1;
1220 }
1221 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001222 netif_err(efx, probe, efx->net_dev,
1223 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001224 goto fail2;
1225 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001226 netif_dbg(efx, probe, efx->net_dev,
1227 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001228
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001229 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1230 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001231 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001232 netif_err(efx, probe, efx->net_dev,
1233 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001234 rc = -EIO;
1235 goto fail3;
1236 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001237 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001238 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001239 netif_err(efx, probe, efx->net_dev,
1240 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001241 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001242 rc = -ENOMEM;
1243 goto fail4;
1244 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001245 netif_dbg(efx, probe, efx->net_dev,
1246 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001247 (unsigned long long)efx->membase_phys, mem_map_size,
1248 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001249
1250 return 0;
1251
1252 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001253 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001254 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001255 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001256 fail2:
1257 pci_disable_device(efx->pci_dev);
1258 fail1:
1259 return rc;
1260}
1261
1262static void efx_fini_io(struct efx_nic *efx)
1263{
Ben Hutchings62776d02010-06-23 11:30:07 +00001264 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001265
1266 if (efx->membase) {
1267 iounmap(efx->membase);
1268 efx->membase = NULL;
1269 }
1270
1271 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001272 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001273 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001274 }
1275
1276 pci_disable_device(efx->pci_dev);
1277}
1278
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001279static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001280{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001281 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001282 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001283 int cpu;
1284
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001285 if (rss_cpus) {
1286 count = rss_cpus;
1287 } else {
1288 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1289 netif_warn(efx, probe, efx->net_dev,
1290 "RSS disabled due to allocation failure\n");
1291 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001292 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001293
1294 count = 0;
1295 for_each_online_cpu(cpu) {
1296 if (!cpumask_test_cpu(cpu, thread_mask)) {
1297 ++count;
1298 cpumask_or(thread_mask, thread_mask,
1299 topology_thread_cpumask(cpu));
1300 }
1301 }
1302
1303 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001304 }
1305
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001306 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1307 * table entries that are inaccessible to VFs
1308 */
1309 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1310 count > efx_vf_size(efx)) {
1311 netif_warn(efx, probe, efx->net_dev,
1312 "Reducing number of RSS channels from %u to %u for "
1313 "VF support. Increase vf-msix-limit to use more "
1314 "channels on the PF.\n",
1315 count, efx_vf_size(efx));
1316 count = efx_vf_size(efx);
1317 }
1318
Ben Hutchings46123d02008-09-01 12:47:33 +01001319 return count;
1320}
1321
1322/* Probe the number and type of interrupts we are able to obtain, and
1323 * the resulting numbers of channels and RX queues.
1324 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001325static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001326{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001327 unsigned int extra_channels = 0;
1328 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001329 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001330
Ben Hutchings7f967c02012-02-13 23:45:02 +00001331 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1332 if (efx->extra_channel_type[i])
1333 ++extra_channels;
1334
Ben Hutchings8ceee662008-04-27 12:55:59 +01001335 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001336 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001337 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001338
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001339 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001340 if (separate_tx_channels)
1341 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001342 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001343 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001344
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001345 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001346 xentries[i].entry = i;
Alexander Gordeev184603d2014-02-18 11:12:00 +01001347 rc = pci_enable_msix_range(efx->pci_dev,
1348 xentries, 1, n_channels);
1349 if (rc < 0) {
1350 /* Fall back to single channel MSI */
1351 efx->interrupt_mode = EFX_INT_MODE_MSI;
1352 netif_err(efx, drv, efx->net_dev,
1353 "could not enable MSI-X\n");
1354 } else if (rc < n_channels) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001355 netif_err(efx, drv, efx->net_dev,
1356 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001357 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001358 netif_err(efx, drv, efx->net_dev,
1359 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001360 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001361 }
1362
Alexander Gordeev184603d2014-02-18 11:12:00 +01001363 if (rc > 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001364 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001365 if (n_channels > extra_channels)
1366 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001367 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001368 efx->n_tx_channels = max(n_channels / 2, 1U);
1369 efx->n_rx_channels = max(n_channels -
1370 efx->n_tx_channels,
1371 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001372 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001373 efx->n_tx_channels = n_channels;
1374 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001375 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001376 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001377 efx_get_channel(efx, i)->irq =
1378 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001379 }
1380 }
1381
1382 /* Try single interrupt MSI */
1383 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001384 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001385 efx->n_rx_channels = 1;
1386 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001387 rc = pci_enable_msi(efx->pci_dev);
1388 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001389 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001390 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001391 netif_err(efx, drv, efx->net_dev,
1392 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001393 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1394 }
1395 }
1396
1397 /* Assume legacy interrupts */
1398 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001399 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001400 efx->n_rx_channels = 1;
1401 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001402 efx->legacy_irq = efx->pci_dev->irq;
1403 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001404
Ben Hutchings7f967c02012-02-13 23:45:02 +00001405 /* Assign extra channels if possible */
1406 j = efx->n_channels;
1407 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1408 if (!efx->extra_channel_type[i])
1409 continue;
1410 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1411 efx->n_channels <= extra_channels) {
1412 efx->extra_channel_type[i]->handle_no_channel(efx);
1413 } else {
1414 --j;
1415 efx_get_channel(efx, j)->type =
1416 efx->extra_channel_type[i];
1417 }
1418 }
1419
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001420 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001421 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001422 efx->n_rx_channels : efx_vf_size(efx));
1423
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001424 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001425}
1426
Jon Cooper261e4d92013-04-15 18:51:54 +01001427static int efx_soft_enable_interrupts(struct efx_nic *efx)
Ben Hutchingsd8291182012-10-05 23:35:41 +01001428{
Jon Cooper261e4d92013-04-15 18:51:54 +01001429 struct efx_channel *channel, *end_channel;
1430 int rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001431
1432 BUG_ON(efx->state == STATE_DISABLED);
1433
1434 efx->irq_soft_enabled = true;
1435 smp_wmb();
1436
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 Hutchingsd8291182012-10-05 23:35:41 +01001443 efx_start_eventq(channel);
1444 }
1445
1446 efx_mcdi_mode_event(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +01001447
1448 return 0;
1449fail:
1450 end_channel = channel;
1451 efx_for_each_channel(channel, efx) {
1452 if (channel == end_channel)
1453 break;
1454 efx_stop_eventq(channel);
1455 if (!channel->type->keep_eventq)
1456 efx_fini_eventq(channel);
1457 }
1458
1459 return rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001460}
1461
1462static void efx_soft_disable_interrupts(struct efx_nic *efx)
1463{
1464 struct efx_channel *channel;
1465
1466 if (efx->state == STATE_DISABLED)
1467 return;
1468
1469 efx_mcdi_mode_poll(efx);
1470
1471 efx->irq_soft_enabled = false;
1472 smp_wmb();
1473
1474 if (efx->legacy_irq)
1475 synchronize_irq(efx->legacy_irq);
1476
1477 efx_for_each_channel(channel, efx) {
1478 if (channel->irq)
1479 synchronize_irq(channel->irq);
1480
1481 efx_stop_eventq(channel);
1482 if (!channel->type->keep_eventq)
1483 efx_fini_eventq(channel);
1484 }
Ben Hutchingscade7152013-08-27 23:12:31 +01001485
1486 /* Flush the asynchronous MCDI request queue */
1487 efx_mcdi_flush_async(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01001488}
1489
Jon Cooper261e4d92013-04-15 18:51:54 +01001490static int efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001491{
Jon Cooper261e4d92013-04-15 18:51:54 +01001492 struct efx_channel *channel, *end_channel;
1493 int rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001494
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001495 BUG_ON(efx->state == STATE_DISABLED);
1496
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001497 if (efx->eeh_disabled_legacy_irq) {
1498 enable_irq(efx->legacy_irq);
1499 efx->eeh_disabled_legacy_irq = false;
1500 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001501
Ben Hutchings86094f72013-08-21 19:51:04 +01001502 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001503
1504 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001505 if (channel->type->keep_eventq) {
1506 rc = efx_init_eventq(channel);
1507 if (rc)
1508 goto fail;
1509 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001510 }
1511
Jon Cooper261e4d92013-04-15 18:51:54 +01001512 rc = efx_soft_enable_interrupts(efx);
1513 if (rc)
1514 goto fail;
1515
1516 return 0;
1517
1518fail:
1519 end_channel = channel;
1520 efx_for_each_channel(channel, efx) {
1521 if (channel == end_channel)
1522 break;
1523 if (channel->type->keep_eventq)
1524 efx_fini_eventq(channel);
1525 }
1526
1527 efx->type->irq_disable_non_ev(efx);
1528
1529 return rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001530}
1531
Ben Hutchingsd8291182012-10-05 23:35:41 +01001532static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001533{
1534 struct efx_channel *channel;
1535
Ben Hutchingsd8291182012-10-05 23:35:41 +01001536 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001537
1538 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001539 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001540 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001541 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001542
Ben Hutchings86094f72013-08-21 19:51:04 +01001543 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001544}
1545
Ben Hutchings8ceee662008-04-27 12:55:59 +01001546static void efx_remove_interrupts(struct efx_nic *efx)
1547{
1548 struct efx_channel *channel;
1549
1550 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001551 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001552 channel->irq = 0;
1553 pci_disable_msi(efx->pci_dev);
1554 pci_disable_msix(efx->pci_dev);
1555
1556 /* Remove legacy interrupt */
1557 efx->legacy_irq = 0;
1558}
1559
Ben Hutchings8831da72008-09-01 12:47:48 +01001560static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001561{
Ben Hutchings602a5322011-05-16 17:32:39 +01001562 struct efx_channel *channel;
1563 struct efx_tx_queue *tx_queue;
1564
Ben Hutchings97653432011-01-12 18:26:56 +00001565 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001566 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001567
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001568 /* We need to mark which channels really have RX and TX
1569 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001570 * RX-only and TX-only channels.
1571 */
1572 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001573 if (channel->channel < efx->n_rx_channels)
1574 channel->rx_queue.core_index = channel->channel;
1575 else
1576 channel->rx_queue.core_index = -1;
1577
Ben Hutchings602a5322011-05-16 17:32:39 +01001578 efx_for_each_channel_tx_queue(tx_queue, channel)
1579 tx_queue->queue -= (efx->tx_channel_offset *
1580 EFX_TXQ_TYPES);
1581 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001582}
1583
1584static int efx_probe_nic(struct efx_nic *efx)
1585{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001586 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001587 int rc;
1588
Ben Hutchings62776d02010-06-23 11:30:07 +00001589 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001590
1591 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001592 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001593 if (rc)
1594 return rc;
1595
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001596 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001597 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001598 rc = efx_probe_interrupts(efx);
1599 if (rc)
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001600 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001601
Daniel Pieczko52ad7622014-04-01 13:10:34 +01001602 efx_set_channels(efx);
1603
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001604 rc = efx->type->dimension_resources(efx);
1605 if (rc)
1606 goto fail2;
Ben Hutchings28e47c42012-02-15 01:58:49 +00001607
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001608 if (efx->n_channels > 1)
1609 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001610 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001611 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001612 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001613
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001614 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1615 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001616
1617 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001618 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1619 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001620
1621 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001622
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001623fail2:
1624 efx_remove_interrupts(efx);
1625fail1:
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001626 efx->type->remove(efx);
1627 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001628}
1629
1630static void efx_remove_nic(struct efx_nic *efx)
1631{
Ben Hutchings62776d02010-06-23 11:30:07 +00001632 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001633
1634 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001635 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001636}
1637
Ben Hutchingsadd72472012-11-08 01:46:53 +00001638static int efx_probe_filters(struct efx_nic *efx)
1639{
1640 int rc;
1641
1642 spin_lock_init(&efx->filter_lock);
1643
1644 rc = efx->type->filter_table_probe(efx);
1645 if (rc)
1646 return rc;
1647
1648#ifdef CONFIG_RFS_ACCEL
1649 if (efx->type->offload_features & NETIF_F_NTUPLE) {
1650 efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
1651 sizeof(*efx->rps_flow_id),
1652 GFP_KERNEL);
1653 if (!efx->rps_flow_id) {
1654 efx->type->filter_table_remove(efx);
1655 return -ENOMEM;
1656 }
1657 }
1658#endif
1659
1660 return 0;
1661}
1662
1663static void efx_remove_filters(struct efx_nic *efx)
1664{
1665#ifdef CONFIG_RFS_ACCEL
1666 kfree(efx->rps_flow_id);
1667#endif
1668 efx->type->filter_table_remove(efx);
1669}
1670
1671static void efx_restore_filters(struct efx_nic *efx)
1672{
1673 efx->type->filter_table_restore(efx);
1674}
1675
Ben Hutchings8ceee662008-04-27 12:55:59 +01001676/**************************************************************************
1677 *
1678 * NIC startup/shutdown
1679 *
1680 *************************************************************************/
1681
1682static int efx_probe_all(struct efx_nic *efx)
1683{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001684 int rc;
1685
Ben Hutchings8ceee662008-04-27 12:55:59 +01001686 rc = efx_probe_nic(efx);
1687 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001688 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001689 goto fail1;
1690 }
1691
Ben Hutchings8ceee662008-04-27 12:55:59 +01001692 rc = efx_probe_port(efx);
1693 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001694 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001695 goto fail2;
1696 }
1697
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001698 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1699 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1700 rc = -EINVAL;
1701 goto fail3;
1702 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001703 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001704
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001705 rc = efx_probe_filters(efx);
1706 if (rc) {
1707 netif_err(efx, probe, efx->net_dev,
1708 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001709 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001710 }
1711
Ben Hutchings7f967c02012-02-13 23:45:02 +00001712 rc = efx_probe_channels(efx);
1713 if (rc)
1714 goto fail4;
1715
Ben Hutchings8ceee662008-04-27 12:55:59 +01001716 return 0;
1717
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001718 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001719 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001720 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001721 efx_remove_port(efx);
1722 fail2:
1723 efx_remove_nic(efx);
1724 fail1:
1725 return rc;
1726}
1727
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001728/* If the interface is supposed to be running but is not, start
1729 * the hardware and software data path, regular activity for the port
1730 * (MAC statistics, link polling, etc.) and schedule the port to be
1731 * reconfigured. Interrupts must already be enabled. This function
1732 * is safe to call multiple times, so long as the NIC is not disabled.
1733 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001734 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001735static void efx_start_all(struct efx_nic *efx)
1736{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001737 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001738 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001739
1740 /* Check that it is appropriate to restart the interface. All
1741 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001742 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001743 return;
1744
Ben Hutchings8ceee662008-04-27 12:55:59 +01001745 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001746 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001747
Alexandre Rames626950d2013-01-14 17:20:22 +00001748 /* Start the hardware monitor if there is one */
1749 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001750 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1751 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001752
1753 /* If link state detection is normally event-driven, we have
1754 * to poll now because we could have missed a change
1755 */
1756 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001757 mutex_lock(&efx->mac_lock);
1758 if (efx->phy_op->poll(efx))
1759 efx_link_status_changed(efx);
1760 mutex_unlock(&efx->mac_lock);
1761 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001762
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001763 efx->type->start_stats(efx);
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001764 efx->type->pull_stats(efx);
1765 spin_lock_bh(&efx->stats_lock);
1766 efx->type->update_stats(efx, NULL, NULL);
1767 spin_unlock_bh(&efx->stats_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001768}
1769
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001770/* Quiesce the hardware and software data path, and regular activity
1771 * for the port without bringing the link down. Safe to call multiple
1772 * times with the NIC in almost any state, but interrupts should be
1773 * enabled. Requires the RTNL lock.
1774 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001775static void efx_stop_all(struct efx_nic *efx)
1776{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001777 EFX_ASSERT_RESET_SERIALISED(efx);
1778
1779 /* port_enabled can be read safely under the rtnl lock */
1780 if (!efx->port_enabled)
1781 return;
1782
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001783 /* update stats before we go down so we can accurately count
1784 * rx_nodesc_drops
1785 */
1786 efx->type->pull_stats(efx);
1787 spin_lock_bh(&efx->stats_lock);
1788 efx->type->update_stats(efx, NULL, NULL);
1789 spin_unlock_bh(&efx->stats_lock);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001790 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001791 efx_stop_port(efx);
1792
Ben Hutchings29c69a42013-01-28 19:01:06 +00001793 /* Stop the kernel transmit interface. This is only valid if
1794 * the device is stopped or detached; otherwise the watchdog
1795 * may fire immediately.
1796 */
1797 WARN_ON(netif_running(efx->net_dev) &&
1798 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001799 netif_tx_disable(efx->net_dev);
1800
1801 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001802}
1803
1804static void efx_remove_all(struct efx_nic *efx)
1805{
Ben Hutchings46426102010-09-10 06:42:33 +00001806 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001807 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001808 efx_remove_port(efx);
1809 efx_remove_nic(efx);
1810}
1811
Ben Hutchings8ceee662008-04-27 12:55:59 +01001812/**************************************************************************
1813 *
1814 * Interrupt moderation
1815 *
1816 **************************************************************************/
1817
Ben Hutchingscc180b62011-12-08 19:51:47 +00001818static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001819{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001820 if (usecs == 0)
1821 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001822 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001823 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001824 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001825}
1826
Ben Hutchings8ceee662008-04-27 12:55:59 +01001827/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001828int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1829 unsigned int rx_usecs, bool rx_adaptive,
1830 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001831{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001832 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001833 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1834 efx->timer_quantum_ns,
1835 1000);
1836 unsigned int tx_ticks;
1837 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001838
1839 EFX_ASSERT_RESET_SERIALISED(efx);
1840
Ben Hutchingscc180b62011-12-08 19:51:47 +00001841 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001842 return -EINVAL;
1843
Ben Hutchingscc180b62011-12-08 19:51:47 +00001844 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1845 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1846
Ben Hutchings9e393b32011-09-05 07:43:04 +00001847 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1848 !rx_may_override_tx) {
1849 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1850 "RX and TX IRQ moderation must be equal\n");
1851 return -EINVAL;
1852 }
1853
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001854 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001855 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001856 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001857 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001858 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001859 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001860 channel->irq_moderation = tx_ticks;
1861 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001862
1863 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001864}
1865
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001866void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1867 unsigned int *rx_usecs, bool *rx_adaptive)
1868{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001869 /* We must round up when converting ticks to microseconds
1870 * because we round down when converting the other way.
1871 */
1872
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001873 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001874 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1875 efx->timer_quantum_ns,
1876 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001877
1878 /* If channels are shared between RX and TX, so is IRQ
1879 * moderation. Otherwise, IRQ moderation is the same for all
1880 * TX channels and is not adaptive.
1881 */
1882 if (efx->tx_channel_offset == 0)
1883 *tx_usecs = *rx_usecs;
1884 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001885 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001886 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001887 efx->timer_quantum_ns,
1888 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001889}
1890
Ben Hutchings8ceee662008-04-27 12:55:59 +01001891/**************************************************************************
1892 *
1893 * Hardware monitor
1894 *
1895 **************************************************************************/
1896
Ben Hutchingse254c272010-09-20 08:44:10 +00001897/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001898static void efx_monitor(struct work_struct *data)
1899{
1900 struct efx_nic *efx = container_of(data, struct efx_nic,
1901 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001902
Ben Hutchings62776d02010-06-23 11:30:07 +00001903 netif_vdbg(efx, timer, efx->net_dev,
1904 "hardware monitor executing on CPU %d\n",
1905 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001906 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001907
Ben Hutchings8ceee662008-04-27 12:55:59 +01001908 /* If the mac_lock is already held then it is likely a port
1909 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001910 * most of the work of monitor() anyway. */
1911 if (mutex_trylock(&efx->mac_lock)) {
1912 if (efx->port_enabled)
1913 efx->type->monitor(efx);
1914 mutex_unlock(&efx->mac_lock);
1915 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001916
Ben Hutchings8ceee662008-04-27 12:55:59 +01001917 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1918 efx_monitor_interval);
1919}
1920
1921/**************************************************************************
1922 *
1923 * ioctls
1924 *
1925 *************************************************************************/
1926
1927/* Net device ioctl
1928 * Context: process, rtnl_lock() held.
1929 */
1930static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1931{
Ben Hutchings767e4682008-09-01 12:43:14 +01001932 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001933 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001934
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001935 if (cmd == SIOCSHWTSTAMP)
Ben Hutchings433dc9b2013-11-14 01:26:21 +00001936 return efx_ptp_set_ts_config(efx, ifr);
1937 if (cmd == SIOCGHWTSTAMP)
1938 return efx_ptp_get_ts_config(efx, ifr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001939
Ben Hutchings68e7f452009-04-29 08:05:08 +00001940 /* Convert phy_id from older PRTAD/DEVAD format */
1941 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1942 (data->phy_id & 0xfc00) == 0x0400)
1943 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1944
1945 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001946}
1947
1948/**************************************************************************
1949 *
1950 * NAPI interface
1951 *
1952 **************************************************************************/
1953
Ben Hutchings7f967c02012-02-13 23:45:02 +00001954static void efx_init_napi_channel(struct efx_channel *channel)
1955{
1956 struct efx_nic *efx = channel->efx;
1957
1958 channel->napi_dev = efx->net_dev;
1959 netif_napi_add(channel->napi_dev, &channel->napi_str,
1960 efx_poll, napi_weight);
1961}
1962
Ben Hutchingse8f14992010-12-07 19:47:34 +00001963static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001964{
1965 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001966
Ben Hutchings7f967c02012-02-13 23:45:02 +00001967 efx_for_each_channel(channel, efx)
1968 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001969}
1970
1971static void efx_fini_napi_channel(struct efx_channel *channel)
1972{
1973 if (channel->napi_dev)
1974 netif_napi_del(&channel->napi_str);
1975 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001976}
1977
1978static void efx_fini_napi(struct efx_nic *efx)
1979{
1980 struct efx_channel *channel;
1981
Ben Hutchingse8f14992010-12-07 19:47:34 +00001982 efx_for_each_channel(channel, efx)
1983 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001984}
1985
1986/**************************************************************************
1987 *
1988 * Kernel netpoll interface
1989 *
1990 *************************************************************************/
1991
1992#ifdef CONFIG_NET_POLL_CONTROLLER
1993
1994/* Although in the common case interrupts will be disabled, this is not
1995 * guaranteed. However, all our work happens inside the NAPI callback,
1996 * so no locking is required.
1997 */
1998static void efx_netpoll(struct net_device *net_dev)
1999{
Ben Hutchings767e4682008-09-01 12:43:14 +01002000 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002001 struct efx_channel *channel;
2002
Ben Hutchings64ee3122008-09-01 12:47:38 +01002003 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002004 efx_schedule_channel(channel);
2005}
2006
2007#endif
2008
2009/**************************************************************************
2010 *
2011 * Kernel net device interface
2012 *
2013 *************************************************************************/
2014
2015/* Context: process, rtnl_lock() held. */
2016static int efx_net_open(struct net_device *net_dev)
2017{
Ben Hutchings767e4682008-09-01 12:43:14 +01002018 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002019 int rc;
2020
Ben Hutchings62776d02010-06-23 11:30:07 +00002021 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
2022 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01002023
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002024 rc = efx_check_disabled(efx);
2025 if (rc)
2026 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01002027 if (efx->phy_mode & PHY_MODE_SPECIAL)
2028 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002029 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
2030 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01002031
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00002032 /* Notify the kernel of the link state polled during driver load,
2033 * before the monitor starts running */
2034 efx_link_status_changed(efx);
2035
Ben Hutchings8ceee662008-04-27 12:55:59 +01002036 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002037 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002038 return 0;
2039}
2040
2041/* Context: process, rtnl_lock() held.
2042 * Note that the kernel will ignore our return code; this method
2043 * should really be a void.
2044 */
2045static int efx_net_stop(struct net_device *net_dev)
2046{
Ben Hutchings767e4682008-09-01 12:43:14 +01002047 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002048
Ben Hutchings62776d02010-06-23 11:30:07 +00002049 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
2050 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01002051
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002052 /* Stop the device and flush all the channels */
2053 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002054
2055 return 0;
2056}
2057
Ben Hutchings5b9e2072008-05-16 21:18:14 +01002058/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00002059static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
2060 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002061{
Ben Hutchings767e4682008-09-01 12:43:14 +01002062 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002063
Ben Hutchings55edc6e2009-11-25 16:11:35 +00002064 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00002065 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01002066 spin_unlock_bh(&efx->stats_lock);
2067
Ben Hutchings8ceee662008-04-27 12:55:59 +01002068 return stats;
2069}
2070
2071/* Context: netif_tx_lock held, BHs disabled. */
2072static void efx_watchdog(struct net_device *net_dev)
2073{
Ben Hutchings767e4682008-09-01 12:43:14 +01002074 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002075
Ben Hutchings62776d02010-06-23 11:30:07 +00002076 netif_err(efx, tx_err, efx->net_dev,
2077 "TX stuck with port_enabled=%d: resetting channels\n",
2078 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002079
Ben Hutchings739bb23d2008-11-04 20:35:36 +00002080 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002081}
2082
2083
2084/* Context: process, rtnl_lock() held. */
2085static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2086{
Ben Hutchings767e4682008-09-01 12:43:14 +01002087 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002088 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002089
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002090 rc = efx_check_disabled(efx);
2091 if (rc)
2092 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002093 if (new_mtu > EFX_MAX_MTU)
2094 return -EINVAL;
2095
Ben Hutchings62776d02010-06-23 11:30:07 +00002096 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002097
Ben Hutchings29c69a42013-01-28 19:01:06 +00002098 efx_device_detach_sync(efx);
2099 efx_stop_all(efx);
2100
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002101 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002102 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01002103 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002104 mutex_unlock(&efx->mac_lock);
2105
Ben Hutchings8ceee662008-04-27 12:55:59 +01002106 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00002107 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002108 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002109}
2110
2111static int efx_set_mac_address(struct net_device *net_dev, void *data)
2112{
Ben Hutchings767e4682008-09-01 12:43:14 +01002113 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002114 struct sockaddr *addr = data;
Ben Hutchingse0b3ae32014-02-12 18:59:54 +00002115 u8 *new_addr = addr->sa_data;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002116
Ben Hutchings8ceee662008-04-27 12:55:59 +01002117 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002118 netif_err(efx, drv, efx->net_dev,
2119 "invalid ethernet MAC address requested: %pM\n",
2120 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002121 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002122 }
2123
Edward Creecd84ff42014-03-07 18:27:41 +00002124 ether_addr_copy(net_dev->dev_addr, new_addr);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002125 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002126
2127 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002128 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002129 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002130 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002131
2132 return 0;
2133}
2134
Ben Hutchingsa816f752008-09-01 12:49:12 +01002135/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002136static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002137{
Ben Hutchings767e4682008-09-01 12:43:14 +01002138 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002139
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002140 if (efx->port_enabled)
2141 queue_work(efx->workqueue, &efx->mac_work);
2142 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002143}
2144
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002145static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002146{
2147 struct efx_nic *efx = netdev_priv(net_dev);
2148
2149 /* If disabling RX n-tuple filtering, clear existing filters */
2150 if (net_dev->features & ~data & NETIF_F_NTUPLE)
Ben Hutchingsfbd79122013-11-21 19:15:03 +00002151 return efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002152
2153 return 0;
2154}
2155
Ben Hutchings8127d662013-08-29 19:19:29 +01002156static const struct net_device_ops efx_farch_netdev_ops = {
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002157 .ndo_open = efx_net_open,
2158 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002159 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002160 .ndo_tx_timeout = efx_watchdog,
2161 .ndo_start_xmit = efx_hard_start_xmit,
2162 .ndo_validate_addr = eth_validate_addr,
2163 .ndo_do_ioctl = efx_ioctl,
2164 .ndo_change_mtu = efx_change_mtu,
2165 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002166 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002167 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002168#ifdef CONFIG_SFC_SRIOV
2169 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2170 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2171 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2172 .ndo_get_vf_config = efx_sriov_get_vf_config,
2173#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002174#ifdef CONFIG_NET_POLL_CONTROLLER
2175 .ndo_poll_controller = efx_netpoll,
2176#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002177 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002178#ifdef CONFIG_RFS_ACCEL
2179 .ndo_rx_flow_steer = efx_filter_rfs,
2180#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002181};
2182
Ben Hutchings8127d662013-08-29 19:19:29 +01002183static const struct net_device_ops efx_ef10_netdev_ops = {
2184 .ndo_open = efx_net_open,
2185 .ndo_stop = efx_net_stop,
2186 .ndo_get_stats64 = efx_net_stats,
2187 .ndo_tx_timeout = efx_watchdog,
2188 .ndo_start_xmit = efx_hard_start_xmit,
2189 .ndo_validate_addr = eth_validate_addr,
2190 .ndo_do_ioctl = efx_ioctl,
2191 .ndo_change_mtu = efx_change_mtu,
2192 .ndo_set_mac_address = efx_set_mac_address,
2193 .ndo_set_rx_mode = efx_set_rx_mode,
2194 .ndo_set_features = efx_set_features,
2195#ifdef CONFIG_NET_POLL_CONTROLLER
2196 .ndo_poll_controller = efx_netpoll,
2197#endif
2198#ifdef CONFIG_RFS_ACCEL
2199 .ndo_rx_flow_steer = efx_filter_rfs,
2200#endif
2201};
2202
Ben Hutchings7dde5962008-12-12 22:09:38 -08002203static void efx_update_name(struct efx_nic *efx)
2204{
2205 strcpy(efx->name, efx->net_dev->name);
2206 efx_mtd_rename(efx);
2207 efx_set_channel_names(efx);
2208}
2209
Ben Hutchings8ceee662008-04-27 12:55:59 +01002210static int efx_netdev_event(struct notifier_block *this,
2211 unsigned long event, void *ptr)
2212{
Jiri Pirko351638e2013-05-28 01:30:21 +00002213 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002214
Ben Hutchings8127d662013-08-29 19:19:29 +01002215 if ((net_dev->netdev_ops == &efx_farch_netdev_ops ||
2216 net_dev->netdev_ops == &efx_ef10_netdev_ops) &&
Ben Hutchings7dde5962008-12-12 22:09:38 -08002217 event == NETDEV_CHANGENAME)
2218 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002219
2220 return NOTIFY_DONE;
2221}
2222
2223static struct notifier_block efx_netdev_notifier = {
2224 .notifier_call = efx_netdev_event,
2225};
2226
Ben Hutchings06d5e192008-12-12 21:47:23 -08002227static ssize_t
2228show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2229{
2230 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2231 return sprintf(buf, "%d\n", efx->phy_type);
2232}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002233static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002234
Ben Hutchings8ceee662008-04-27 12:55:59 +01002235static int efx_register_netdev(struct efx_nic *efx)
2236{
2237 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002238 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002239 int rc;
2240
2241 net_dev->watchdog_timeo = 5 * HZ;
2242 net_dev->irq = efx->pci_dev->irq;
Ben Hutchings8127d662013-08-29 19:19:29 +01002243 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0) {
2244 net_dev->netdev_ops = &efx_ef10_netdev_ops;
2245 net_dev->priv_flags |= IFF_UNICAST_FLT;
2246 } else {
2247 net_dev->netdev_ops = &efx_farch_netdev_ops;
2248 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002249 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002250 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002251
Ben Hutchings7dde5962008-12-12 22:09:38 -08002252 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002253
Ben Hutchings7153f622012-07-27 20:50:52 +01002254 /* Enable resets to be scheduled and check whether any were
2255 * already requested. If so, the NIC is probably hosed so we
2256 * abort.
2257 */
2258 efx->state = STATE_READY;
2259 smp_mb(); /* ensure we change state before checking reset_pending */
2260 if (efx->reset_pending) {
2261 netif_err(efx, probe, efx->net_dev,
2262 "aborting probe due to scheduled reset\n");
2263 rc = -EIO;
2264 goto fail_locked;
2265 }
2266
Ben Hutchingsaed06282009-08-26 08:16:27 +00002267 rc = dev_alloc_name(net_dev, net_dev->name);
2268 if (rc < 0)
2269 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002270 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002271
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002272 /* Always start with carrier off; PHY events will detect the link */
2273 netif_carrier_off(net_dev);
2274
Ben Hutchingsaed06282009-08-26 08:16:27 +00002275 rc = register_netdevice(net_dev);
2276 if (rc)
2277 goto fail_locked;
2278
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002279 efx_for_each_channel(channel, efx) {
2280 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002281 efx_for_each_channel_tx_queue(tx_queue, channel)
2282 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002283 }
2284
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002285 efx_associate(efx);
2286
Ben Hutchings7dde5962008-12-12 22:09:38 -08002287 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002288
Ben Hutchings06d5e192008-12-12 21:47:23 -08002289 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2290 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002291 netif_err(efx, drv, efx->net_dev,
2292 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002293 goto fail_registered;
2294 }
2295
Ben Hutchings8ceee662008-04-27 12:55:59 +01002296 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002297
Ben Hutchings7153f622012-07-27 20:50:52 +01002298fail_registered:
2299 rtnl_lock();
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002300 efx_dissociate(efx);
Ben Hutchings7153f622012-07-27 20:50:52 +01002301 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002302fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002303 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002304 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002305 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002306 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002307}
2308
2309static void efx_unregister_netdev(struct efx_nic *efx)
2310{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002311 if (!efx->net_dev)
2312 return;
2313
Ben Hutchings767e4682008-09-01 12:43:14 +01002314 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002315
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002316 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2317 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002318
2319 rtnl_lock();
2320 unregister_netdevice(efx->net_dev);
2321 efx->state = STATE_UNINIT;
2322 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002323}
2324
2325/**************************************************************************
2326 *
2327 * Device reset and suspend
2328 *
2329 **************************************************************************/
2330
Ben Hutchings2467ca42008-09-01 12:48:50 +01002331/* Tears down the entire software state and most of the hardware state
2332 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002333void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002334{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002335 EFX_ASSERT_RESET_SERIALISED(efx);
2336
Ben Hutchings2467ca42008-09-01 12:48:50 +01002337 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002338 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002339
2340 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002341 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2342 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002343 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002344}
2345
Ben Hutchings2467ca42008-09-01 12:48:50 +01002346/* This function will always ensure that the locks acquired in
2347 * efx_reset_down() are released. A failure return code indicates
2348 * that we were unable to reinitialise the hardware, and the
2349 * driver should be disabled. If ok is false, then the rx and tx
2350 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002351int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002352{
2353 int rc;
2354
Ben Hutchings2467ca42008-09-01 12:48:50 +01002355 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002356
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002357 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002358 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002359 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002360 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002361 }
2362
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002363 if (!ok)
2364 goto fail;
2365
Steve Hodgson4b988282009-01-29 17:50:51 +00002366 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002367 rc = efx->phy_op->init(efx);
2368 if (rc)
2369 goto fail;
2370 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002371 netif_err(efx, drv, efx->net_dev,
2372 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002373 }
2374
Jon Cooper261e4d92013-04-15 18:51:54 +01002375 rc = efx_enable_interrupts(efx);
2376 if (rc)
2377 goto fail;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002378 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002379 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002380
2381 mutex_unlock(&efx->mac_lock);
2382
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002383 efx_start_all(efx);
2384
2385 return 0;
2386
2387fail:
2388 efx->port_initialized = false;
2389
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002390 mutex_unlock(&efx->mac_lock);
2391
Ben Hutchings8ceee662008-04-27 12:55:59 +01002392 return rc;
2393}
2394
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002395/* Reset the NIC using the specified method. Note that the reset may
2396 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002397 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002398 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002399 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002400int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002401{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002402 int rc, rc2;
2403 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002404
Ben Hutchings62776d02010-06-23 11:30:07 +00002405 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2406 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002407
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002408 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002409 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002410
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002411 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002412 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002413 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002414 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002415 }
2416
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002417 /* Clear flags for the scopes we covered. We assume the NIC and
2418 * driver are now quiescent so that there is no race here.
2419 */
2420 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002421
2422 /* Reinitialise bus-mastering, which may have been turned off before
2423 * the reset was scheduled. This is still appropriate, even in the
2424 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2425 * can respond to requests. */
2426 pci_set_master(efx->pci_dev);
2427
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002428out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002429 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002430 disabled = rc ||
2431 method == RESET_TYPE_DISABLE ||
2432 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002433 rc2 = efx_reset_up(efx, method, !disabled);
2434 if (rc2) {
2435 disabled = true;
2436 if (!rc)
2437 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002438 }
2439
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002440 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002441 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002442 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002443 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002444 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002445 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002446 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002447 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002448 return rc;
2449}
2450
Alexandre Rames626950d2013-01-14 17:20:22 +00002451/* Try recovery mechanisms.
2452 * For now only EEH is supported.
2453 * Returns 0 if the recovery mechanisms are unsuccessful.
2454 * Returns a non-zero value otherwise.
2455 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002456int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002457{
2458#ifdef CONFIG_EEH
2459 /* A PCI error can occur and not be seen by EEH because nothing
2460 * happens on the PCI bus. In this case the driver may fail and
2461 * schedule a 'recover or reset', leading to this recovery handler.
2462 * Manually call the eeh failure check function.
2463 */
2464 struct eeh_dev *eehdev =
2465 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2466
2467 if (eeh_dev_check_failure(eehdev)) {
2468 /* The EEH mechanisms will handle the error and reset the
2469 * device if necessary.
2470 */
2471 return 1;
2472 }
2473#endif
2474 return 0;
2475}
2476
Jon Cooper74cd60a2013-09-16 14:18:51 +01002477static void efx_wait_for_bist_end(struct efx_nic *efx)
2478{
2479 int i;
2480
2481 for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
2482 if (efx_mcdi_poll_reboot(efx))
2483 goto out;
2484 msleep(BIST_WAIT_DELAY_MS);
2485 }
2486
2487 netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
2488out:
2489 /* Either way unset the BIST flag. If we found no reboot we probably
2490 * won't recover, but we should try.
2491 */
2492 efx->mc_bist_for_other_fn = false;
2493}
2494
Ben Hutchings8ceee662008-04-27 12:55:59 +01002495/* The worker thread exists so that code that cannot sleep can
2496 * schedule a reset for later.
2497 */
2498static void efx_reset_work(struct work_struct *data)
2499{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002500 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002501 unsigned long pending;
2502 enum reset_type method;
2503
2504 pending = ACCESS_ONCE(efx->reset_pending);
2505 method = fls(pending) - 1;
2506
Jon Cooper74cd60a2013-09-16 14:18:51 +01002507 if (method == RESET_TYPE_MC_BIST)
2508 efx_wait_for_bist_end(efx);
2509
Alexandre Rames626950d2013-01-14 17:20:22 +00002510 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2511 method == RESET_TYPE_RECOVER_OR_ALL) &&
2512 efx_try_recovery(efx))
2513 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002514
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002515 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002516 return;
2517
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002518 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002519
2520 /* We checked the state in efx_schedule_reset() but it may
2521 * have changed by now. Now that we have the RTNL lock,
2522 * it cannot change again.
2523 */
2524 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002525 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002526
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002527 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002528}
2529
2530void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2531{
2532 enum reset_type method;
2533
Alexandre Rames626950d2013-01-14 17:20:22 +00002534 if (efx->state == STATE_RECOVERY) {
2535 netif_dbg(efx, drv, efx->net_dev,
2536 "recovering: skip scheduling %s reset\n",
2537 RESET_TYPE(type));
2538 return;
2539 }
2540
Ben Hutchings8ceee662008-04-27 12:55:59 +01002541 switch (type) {
2542 case RESET_TYPE_INVISIBLE:
2543 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002544 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002545 case RESET_TYPE_WORLD:
2546 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002547 case RESET_TYPE_RECOVER_OR_DISABLE:
Jon Cooper74cd60a2013-09-16 14:18:51 +01002548 case RESET_TYPE_MC_BIST:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002549 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002550 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2551 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002552 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002553 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002554 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002555 netif_dbg(efx, drv, efx->net_dev,
2556 "scheduling %s reset for %s\n",
2557 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002558 break;
2559 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002560
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002561 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002562 smp_mb(); /* ensure we change reset_pending before checking state */
2563
2564 /* If we're not READY then just leave the flags set as the cue
2565 * to abort probing or reschedule the reset later.
2566 */
2567 if (ACCESS_ONCE(efx->state) != STATE_READY)
2568 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002569
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002570 /* efx_process_channel() will no longer read events once a
2571 * reset is scheduled. So switch back to poll'd MCDI completions. */
2572 efx_mcdi_mode_poll(efx);
2573
Steve Hodgson1ab00622008-12-12 21:33:02 -08002574 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002575}
2576
2577/**************************************************************************
2578 *
2579 * List of NICs we support
2580 *
2581 **************************************************************************/
2582
2583/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002584static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002585 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2586 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002587 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002588 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2589 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002590 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002591 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002592 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002593 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002594 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8127d662013-08-29 19:19:29 +01002595 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
2596 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002597 {0} /* end of list */
2598};
2599
2600/**************************************************************************
2601 *
Ben Hutchings37594332009-11-23 16:05:45 +00002602 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002603 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002604 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002605 * Needed so all function pointers are valid and do not have to be tested
2606 * before use
2607 *
2608 **************************************************************************/
2609int efx_port_dummy_op_int(struct efx_nic *efx)
2610{
2611 return 0;
2612}
2613void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002614
2615static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002616{
2617 return false;
2618}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002619
stephen hemminger6c8c2512011-04-14 05:50:12 +00002620static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002621 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002622 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002623 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002624 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002625};
2626
Ben Hutchings8ceee662008-04-27 12:55:59 +01002627/**************************************************************************
2628 *
2629 * Data housekeeping
2630 *
2631 **************************************************************************/
2632
2633/* This zeroes out and then fills in the invariants in a struct
2634 * efx_nic (including all sub-structures).
2635 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002636static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002637 struct pci_dev *pci_dev, struct net_device *net_dev)
2638{
Ben Hutchings46426102010-09-10 06:42:33 +00002639 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002640
2641 /* Initialise common structures */
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002642 INIT_LIST_HEAD(&efx->node);
2643 INIT_LIST_HEAD(&efx->secondary_list);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002644 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002645#ifdef CONFIG_SFC_MTD
2646 INIT_LIST_HEAD(&efx->mtd_list);
2647#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002648 INIT_WORK(&efx->reset_work, efx_reset_work);
2649 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002650 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002651 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002652 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002653 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002654 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002655
2656 efx->net_dev = net_dev;
Jon Cooper43a37392012-10-18 15:49:54 +01002657 efx->rx_prefix_size = efx->type->rx_prefix_size;
Andrew Rybchenko2ec03012013-11-16 11:02:27 +04002658 efx->rx_ip_align =
2659 NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
Jon Cooper43a37392012-10-18 15:49:54 +01002660 efx->rx_packet_hash_offset =
2661 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
Jon Cooperbd9a2652013-11-18 12:54:41 +00002662 efx->rx_packet_ts_offset =
2663 efx->type->rx_ts_offset - efx->type->rx_prefix_size;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002664 spin_lock_init(&efx->stats_lock);
2665 mutex_init(&efx->mac_lock);
2666 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002667 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002668 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002669 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002670
2671 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002672 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2673 if (!efx->channel[i])
2674 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002675 efx->msi_context[i].efx = efx;
2676 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002677 }
2678
Ben Hutchings8ceee662008-04-27 12:55:59 +01002679 /* Higher numbered interrupt modes are less capable! */
2680 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2681 interrupt_mode);
2682
Ben Hutchings6977dc62008-12-26 13:44:39 -08002683 /* Would be good to use the net_dev name, but we're too early */
2684 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2685 pci_name(pci_dev));
2686 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002687 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002688 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002689
Ben Hutchings8ceee662008-04-27 12:55:59 +01002690 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002691
2692fail:
2693 efx_fini_struct(efx);
2694 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002695}
2696
2697static void efx_fini_struct(struct efx_nic *efx)
2698{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002699 int i;
2700
2701 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2702 kfree(efx->channel[i]);
2703
Ben Hutchingsef215e62013-12-05 20:13:22 +00002704 kfree(efx->vpd_sn);
2705
Ben Hutchings8ceee662008-04-27 12:55:59 +01002706 if (efx->workqueue) {
2707 destroy_workqueue(efx->workqueue);
2708 efx->workqueue = NULL;
2709 }
2710}
2711
2712/**************************************************************************
2713 *
2714 * PCI interface
2715 *
2716 **************************************************************************/
2717
2718/* Main body of final NIC shutdown code
2719 * This is called only at module unload (or hotplug removal).
2720 */
2721static void efx_pci_remove_main(struct efx_nic *efx)
2722{
Ben Hutchings7153f622012-07-27 20:50:52 +01002723 /* Flush reset_work. It can no longer be scheduled since we
2724 * are not READY.
2725 */
2726 BUG_ON(efx->state == STATE_READY);
2727 cancel_work_sync(&efx->reset_work);
2728
Ben Hutchingsd8291182012-10-05 23:35:41 +01002729 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002730 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002731 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002732 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002733 efx_fini_napi(efx);
2734 efx_remove_all(efx);
2735}
2736
2737/* Final NIC shutdown
2738 * This is called only at module unload (or hotplug removal).
2739 */
2740static void efx_pci_remove(struct pci_dev *pci_dev)
2741{
2742 struct efx_nic *efx;
2743
2744 efx = pci_get_drvdata(pci_dev);
2745 if (!efx)
2746 return;
2747
2748 /* Mark the NIC as fini, then stop the interface */
2749 rtnl_lock();
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002750 efx_dissociate(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002751 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002752 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002753 rtnl_unlock();
2754
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002755 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002756 efx_unregister_netdev(efx);
2757
Ben Hutchings7dde5962008-12-12 22:09:38 -08002758 efx_mtd_remove(efx);
2759
Ben Hutchings8ceee662008-04-27 12:55:59 +01002760 efx_pci_remove_main(efx);
2761
Ben Hutchings8ceee662008-04-27 12:55:59 +01002762 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002763 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002764
Ben Hutchings8ceee662008-04-27 12:55:59 +01002765 efx_fini_struct(efx);
2766 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002767
2768 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002769};
2770
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002771/* NIC VPD information
2772 * Called during probe to display the part number of the
2773 * installed NIC. VPD is potentially very large but this should
2774 * always appear within the first 512 bytes.
2775 */
2776#define SFC_VPD_LEN 512
Ben Hutchingsef215e62013-12-05 20:13:22 +00002777static void efx_probe_vpd_strings(struct efx_nic *efx)
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002778{
2779 struct pci_dev *dev = efx->pci_dev;
2780 char vpd_data[SFC_VPD_LEN];
2781 ssize_t vpd_size;
Ben Hutchingsef215e62013-12-05 20:13:22 +00002782 int ro_start, ro_size, i, j;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002783
2784 /* Get the vpd data from the device */
2785 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2786 if (vpd_size <= 0) {
2787 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2788 return;
2789 }
2790
2791 /* Get the Read only section */
Ben Hutchingsef215e62013-12-05 20:13:22 +00002792 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2793 if (ro_start < 0) {
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002794 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2795 return;
2796 }
2797
Ben Hutchingsef215e62013-12-05 20:13:22 +00002798 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
2799 j = ro_size;
2800 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002801 if (i + j > vpd_size)
2802 j = vpd_size - i;
2803
2804 /* Get the Part number */
2805 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2806 if (i < 0) {
2807 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2808 return;
2809 }
2810
2811 j = pci_vpd_info_field_size(&vpd_data[i]);
2812 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2813 if (i + j > vpd_size) {
2814 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2815 return;
2816 }
2817
2818 netif_info(efx, drv, efx->net_dev,
2819 "Part Number : %.*s\n", j, &vpd_data[i]);
Ben Hutchingsef215e62013-12-05 20:13:22 +00002820
2821 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
2822 j = ro_size;
2823 i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
2824 if (i < 0) {
2825 netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
2826 return;
2827 }
2828
2829 j = pci_vpd_info_field_size(&vpd_data[i]);
2830 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2831 if (i + j > vpd_size) {
2832 netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
2833 return;
2834 }
2835
2836 efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
2837 if (!efx->vpd_sn)
2838 return;
2839
2840 snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002841}
2842
2843
Ben Hutchings8ceee662008-04-27 12:55:59 +01002844/* Main body of NIC initialisation
2845 * This is called at module load (or hotplug insertion, theoretically).
2846 */
2847static int efx_pci_probe_main(struct efx_nic *efx)
2848{
2849 int rc;
2850
2851 /* Do start-of-day initialisation */
2852 rc = efx_probe_all(efx);
2853 if (rc)
2854 goto fail1;
2855
Ben Hutchingse8f14992010-12-07 19:47:34 +00002856 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002857
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002858 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002859 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002860 netif_err(efx, probe, efx->net_dev,
2861 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002862 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002863 }
2864
2865 rc = efx_init_port(efx);
2866 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002867 netif_err(efx, probe, efx->net_dev,
2868 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002869 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002870 }
2871
Ben Hutchings152b6a62009-11-29 03:43:56 +00002872 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002873 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002874 goto fail5;
Jon Cooper261e4d92013-04-15 18:51:54 +01002875 rc = efx_enable_interrupts(efx);
2876 if (rc)
2877 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002878
2879 return 0;
2880
Jon Cooper261e4d92013-04-15 18:51:54 +01002881 fail6:
2882 efx_nic_fini_interrupt(efx);
Ben Hutchings278c0622009-11-23 16:05:12 +00002883 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002884 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002885 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002886 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002887 fail3:
2888 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002889 efx_remove_all(efx);
2890 fail1:
2891 return rc;
2892}
2893
2894/* NIC initialisation
2895 *
2896 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002897 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002898 * sets up and registers the network devices with the kernel and hooks
2899 * the interrupt service routine. It does not prepare the device for
2900 * transmission; this is left to the first time one of the network
2901 * interfaces is brought up (i.e. efx_net_open).
2902 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002903static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002904 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002905{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002906 struct net_device *net_dev;
2907 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002908 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002909
2910 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002911 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2912 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002913 if (!net_dev)
2914 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002915 efx = netdev_priv(net_dev);
2916 efx->type = (const struct efx_nic_type *) entry->driver_data;
2917 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002918 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002919 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002920 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002921 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002922 /* Mask for features that also apply to VLAN devices */
2923 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002924 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2925 NETIF_F_RXCSUM);
2926 /* All offloads can be toggled */
2927 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002928 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002929 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002930 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002931 if (rc)
2932 goto fail1;
2933
Ben Hutchings62776d02010-06-23 11:30:07 +00002934 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002935 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002936
Ben Hutchingsef215e62013-12-05 20:13:22 +00002937 efx_probe_vpd_strings(efx);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002938
Ben Hutchings8ceee662008-04-27 12:55:59 +01002939 /* Set up basic I/O (BAR mappings etc) */
2940 rc = efx_init_io(efx);
2941 if (rc)
2942 goto fail2;
2943
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002944 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002945 if (rc)
2946 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002947
Ben Hutchings8ceee662008-04-27 12:55:59 +01002948 rc = efx_register_netdev(efx);
2949 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002950 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002951
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002952 rc = efx_sriov_init(efx);
2953 if (rc)
2954 netif_err(efx, probe, efx->net_dev,
2955 "SR-IOV can't be enabled rc %d\n", rc);
2956
Ben Hutchings62776d02010-06-23 11:30:07 +00002957 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002958
Ben Hutchings7c431612012-01-27 17:23:58 +00002959 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002960 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002961 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002962 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002963 if (rc)
2964 netif_warn(efx, probe, efx->net_dev,
2965 "failed to create MTDs (%d)\n", rc);
2966
Alexandre Rames626950d2013-01-14 17:20:22 +00002967 rc = pci_enable_pcie_error_reporting(pci_dev);
2968 if (rc && rc != -EINVAL)
2969 netif_warn(efx, probe, efx->net_dev,
2970 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2971
Ben Hutchings8ceee662008-04-27 12:55:59 +01002972 return 0;
2973
Ben Hutchings8ceee662008-04-27 12:55:59 +01002974 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002975 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002976 fail3:
2977 efx_fini_io(efx);
2978 fail2:
2979 efx_fini_struct(efx);
2980 fail1:
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002981 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002982 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002983 free_netdev(net_dev);
2984 return rc;
2985}
2986
Ben Hutchings89c758f2009-11-29 03:43:07 +00002987static int efx_pm_freeze(struct device *dev)
2988{
2989 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2990
Ben Hutchings61da0262012-07-27 19:35:39 +01002991 rtnl_lock();
2992
Ben Hutchings6032fb52012-07-27 19:35:47 +01002993 if (efx->state != STATE_DISABLED) {
2994 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002995
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002996 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002997
Ben Hutchings6032fb52012-07-27 19:35:47 +01002998 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002999 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01003000 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00003001
Ben Hutchings61da0262012-07-27 19:35:39 +01003002 rtnl_unlock();
3003
Ben Hutchings89c758f2009-11-29 03:43:07 +00003004 return 0;
3005}
3006
3007static int efx_pm_thaw(struct device *dev)
3008{
Jon Cooper261e4d92013-04-15 18:51:54 +01003009 int rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003010 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3011
Ben Hutchings61da0262012-07-27 19:35:39 +01003012 rtnl_lock();
3013
Ben Hutchings6032fb52012-07-27 19:35:47 +01003014 if (efx->state != STATE_DISABLED) {
Jon Cooper261e4d92013-04-15 18:51:54 +01003015 rc = efx_enable_interrupts(efx);
3016 if (rc)
3017 goto fail;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003018
Ben Hutchings6032fb52012-07-27 19:35:47 +01003019 mutex_lock(&efx->mac_lock);
3020 efx->phy_op->reconfigure(efx);
3021 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003022
Ben Hutchings6032fb52012-07-27 19:35:47 +01003023 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003024
Ben Hutchings6032fb52012-07-27 19:35:47 +01003025 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003026
Ben Hutchings6032fb52012-07-27 19:35:47 +01003027 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003028
Ben Hutchings6032fb52012-07-27 19:35:47 +01003029 efx->type->resume_wol(efx);
3030 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00003031
Ben Hutchings61da0262012-07-27 19:35:39 +01003032 rtnl_unlock();
3033
Steve Hodgson319ba642010-06-01 11:17:24 +00003034 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
3035 queue_work(reset_workqueue, &efx->reset_work);
3036
Ben Hutchings89c758f2009-11-29 03:43:07 +00003037 return 0;
Jon Cooper261e4d92013-04-15 18:51:54 +01003038
3039fail:
3040 rtnl_unlock();
3041
3042 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003043}
3044
3045static int efx_pm_poweroff(struct device *dev)
3046{
3047 struct pci_dev *pci_dev = to_pci_dev(dev);
3048 struct efx_nic *efx = pci_get_drvdata(pci_dev);
3049
3050 efx->type->fini(efx);
3051
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01003052 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003053
3054 pci_save_state(pci_dev);
3055 return pci_set_power_state(pci_dev, PCI_D3hot);
3056}
3057
3058/* Used for both resume and restore */
3059static int efx_pm_resume(struct device *dev)
3060{
3061 struct pci_dev *pci_dev = to_pci_dev(dev);
3062 struct efx_nic *efx = pci_get_drvdata(pci_dev);
3063 int rc;
3064
3065 rc = pci_set_power_state(pci_dev, PCI_D0);
3066 if (rc)
3067 return rc;
3068 pci_restore_state(pci_dev);
3069 rc = pci_enable_device(pci_dev);
3070 if (rc)
3071 return rc;
3072 pci_set_master(efx->pci_dev);
3073 rc = efx->type->reset(efx, RESET_TYPE_ALL);
3074 if (rc)
3075 return rc;
3076 rc = efx->type->init(efx);
3077 if (rc)
3078 return rc;
Jon Cooper261e4d92013-04-15 18:51:54 +01003079 rc = efx_pm_thaw(dev);
3080 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003081}
3082
3083static int efx_pm_suspend(struct device *dev)
3084{
3085 int rc;
3086
3087 efx_pm_freeze(dev);
3088 rc = efx_pm_poweroff(dev);
3089 if (rc)
3090 efx_pm_resume(dev);
3091 return rc;
3092}
3093
Ben Hutchings18e83e42012-01-05 19:05:20 +00003094static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00003095 .suspend = efx_pm_suspend,
3096 .resume = efx_pm_resume,
3097 .freeze = efx_pm_freeze,
3098 .thaw = efx_pm_thaw,
3099 .poweroff = efx_pm_poweroff,
3100 .restore = efx_pm_resume,
3101};
3102
Alexandre Rames626950d2013-01-14 17:20:22 +00003103/* A PCI error affecting this device was detected.
3104 * At this point MMIO and DMA may be disabled.
3105 * Stop the software path and request a slot reset.
3106 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003107static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
3108 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00003109{
3110 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3111 struct efx_nic *efx = pci_get_drvdata(pdev);
3112
3113 if (state == pci_channel_io_perm_failure)
3114 return PCI_ERS_RESULT_DISCONNECT;
3115
3116 rtnl_lock();
3117
3118 if (efx->state != STATE_DISABLED) {
3119 efx->state = STATE_RECOVERY;
3120 efx->reset_pending = 0;
3121
3122 efx_device_detach_sync(efx);
3123
3124 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003125 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00003126
3127 status = PCI_ERS_RESULT_NEED_RESET;
3128 } else {
3129 /* If the interface is disabled we don't want to do anything
3130 * with it.
3131 */
3132 status = PCI_ERS_RESULT_RECOVERED;
3133 }
3134
3135 rtnl_unlock();
3136
3137 pci_disable_device(pdev);
3138
3139 return status;
3140}
3141
3142/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003143static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00003144{
3145 struct efx_nic *efx = pci_get_drvdata(pdev);
3146 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3147 int rc;
3148
3149 if (pci_enable_device(pdev)) {
3150 netif_err(efx, hw, efx->net_dev,
3151 "Cannot re-enable PCI device after reset.\n");
3152 status = PCI_ERS_RESULT_DISCONNECT;
3153 }
3154
3155 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
3156 if (rc) {
3157 netif_err(efx, hw, efx->net_dev,
3158 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3159 /* Non-fatal error. Continue. */
3160 }
3161
3162 return status;
3163}
3164
3165/* Perform the actual reset and resume I/O operations. */
3166static void efx_io_resume(struct pci_dev *pdev)
3167{
3168 struct efx_nic *efx = pci_get_drvdata(pdev);
3169 int rc;
3170
3171 rtnl_lock();
3172
3173 if (efx->state == STATE_DISABLED)
3174 goto out;
3175
3176 rc = efx_reset(efx, RESET_TYPE_ALL);
3177 if (rc) {
3178 netif_err(efx, hw, efx->net_dev,
3179 "efx_reset failed after PCI error (%d)\n", rc);
3180 } else {
3181 efx->state = STATE_READY;
3182 netif_dbg(efx, hw, efx->net_dev,
3183 "Done resetting and resuming IO after PCI error.\n");
3184 }
3185
3186out:
3187 rtnl_unlock();
3188}
3189
3190/* For simplicity and reliability, we always require a slot reset and try to
3191 * reset the hardware when a pci error affecting the device is detected.
3192 * We leave both the link_reset and mmio_enabled callback unimplemented:
3193 * with our request for slot reset the mmio_enabled callback will never be
3194 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3195 */
3196static struct pci_error_handlers efx_err_handlers = {
3197 .error_detected = efx_io_error_detected,
3198 .slot_reset = efx_io_slot_reset,
3199 .resume = efx_io_resume,
3200};
3201
Ben Hutchings8ceee662008-04-27 12:55:59 +01003202static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003203 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003204 .id_table = efx_pci_table,
3205 .probe = efx_pci_probe,
3206 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003207 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003208 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003209};
3210
3211/**************************************************************************
3212 *
3213 * Kernel module interface
3214 *
3215 *************************************************************************/
3216
3217module_param(interrupt_mode, uint, 0444);
3218MODULE_PARM_DESC(interrupt_mode,
3219 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3220
3221static int __init efx_init_module(void)
3222{
3223 int rc;
3224
3225 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3226
3227 rc = register_netdevice_notifier(&efx_netdev_notifier);
3228 if (rc)
3229 goto err_notifier;
3230
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003231 rc = efx_init_sriov();
3232 if (rc)
3233 goto err_sriov;
3234
Steve Hodgson1ab00622008-12-12 21:33:02 -08003235 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3236 if (!reset_workqueue) {
3237 rc = -ENOMEM;
3238 goto err_reset;
3239 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003240
3241 rc = pci_register_driver(&efx_pci_driver);
3242 if (rc < 0)
3243 goto err_pci;
3244
3245 return 0;
3246
3247 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003248 destroy_workqueue(reset_workqueue);
3249 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003250 efx_fini_sriov();
3251 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003252 unregister_netdevice_notifier(&efx_netdev_notifier);
3253 err_notifier:
3254 return rc;
3255}
3256
3257static void __exit efx_exit_module(void)
3258{
3259 printk(KERN_INFO "Solarflare NET driver unloading\n");
3260
3261 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003262 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003263 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003264 unregister_netdevice_notifier(&efx_netdev_notifier);
3265
3266}
3267
3268module_init(efx_init_module);
3269module_exit(efx_exit_module);
3270
Ben Hutchings906bb262009-11-29 15:16:19 +00003271MODULE_AUTHOR("Solarflare Communications and "
3272 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings6a350fd2014-02-12 19:00:07 +00003273MODULE_DESCRIPTION("Solarflare network driver");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003274MODULE_LICENSE("GPL");
3275MODULE_DEVICE_TABLE(pci, efx_pci_table);