blob: 1213af5024d18ff6a92cb2c222460429ed94db45 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchings0a6f40c2011-02-25 00:01:34 +00004 * Copyright 2005-2011 Solarflare Communications Inc.
Ben Hutchings8ceee662008-04-27 12:55:59 +01005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/delay.h>
16#include <linux/notifier.h>
17#include <linux/ip.h>
18#include <linux/tcp.h>
19#include <linux/in.h>
20#include <linux/crc32.h>
21#include <linux/ethtool.h>
Ben Hutchingsaa6ef272008-07-18 19:03:10 +010022#include <linux/topology.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/gfp.h>
Alexandre Rames626950d2013-01-14 17:20:22 +000024#include <linux/pci.h>
Ben Hutchings64d8ad62011-01-05 00:50:41 +000025#include <linux/cpu_rmap.h>
Alexandre Rames626950d2013-01-14 17:20:22 +000026#include <linux/aer.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010027#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010028#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000029#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000030#include "selftest.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010031
Ben Hutchings8880f4e2009-11-29 15:15:41 +000032#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000033#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000034
Ben Hutchingsc4593022009-11-23 16:08:17 +000035/**************************************************************************
36 *
37 * Type name strings
38 *
39 **************************************************************************
40 */
41
42/* Loopback mode names (see LOOPBACK_MODE()) */
43const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000044const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000045 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000046 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000047 [LOOPBACK_GMAC] = "GMAC",
48 [LOOPBACK_XGMII] = "XGMII",
49 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000050 [LOOPBACK_XAUI] = "XAUI",
51 [LOOPBACK_GMII] = "GMII",
52 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000053 [LOOPBACK_XGBR] = "XGBR",
54 [LOOPBACK_XFI] = "XFI",
55 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
56 [LOOPBACK_GMII_FAR] = "GMII_FAR",
57 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
58 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000059 [LOOPBACK_GPHY] = "GPHY",
60 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000061 [LOOPBACK_PCS] = "PCS",
62 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000063 [LOOPBACK_XPORT] = "XPORT",
64 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000065 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000066 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
67 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000068 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000069 [LOOPBACK_XFI_WS] = "XFI_WS",
70 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000071 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000072};
73
Ben Hutchingsc4593022009-11-23 16:08:17 +000074const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000075const char *const efx_reset_type_names[] = {
Alexandre Rames626950d2013-01-14 17:20:22 +000076 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
77 [RESET_TYPE_ALL] = "ALL",
78 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
79 [RESET_TYPE_WORLD] = "WORLD",
80 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
81 [RESET_TYPE_DISABLE] = "DISABLE",
82 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
83 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
84 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
85 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
86 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
87 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
88 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Ben Hutchingsc4593022009-11-23 16:08:17 +000089};
90
Steve Hodgson1ab00622008-12-12 21:33:02 -080091/* Reset workqueue. If any NIC has a hardware failure then a reset will be
92 * queued onto this work queue. This is not a per-nic work queue, because
93 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
94 */
95static struct workqueue_struct *reset_workqueue;
96
Ben Hutchings8ceee662008-04-27 12:55:59 +010097/**************************************************************************
98 *
99 * Configurable values
100 *
101 *************************************************************************/
102
103/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100104 * Use separate channels for TX and RX events
105 *
Neil Turton28b581a2008-12-12 21:41:06 -0800106 * Set this to 1 to use separate channels for TX and RX. It allows us
107 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100108 *
Neil Turton28b581a2008-12-12 21:41:06 -0800109 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100110 */
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000111static bool separate_tx_channels;
112module_param(separate_tx_channels, bool, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800113MODULE_PARM_DESC(separate_tx_channels,
114 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100115
116/* This is the weight assigned to each of the (per-channel) virtual
117 * NAPI devices.
118 */
119static int napi_weight = 64;
120
121/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000122 * monitor.
123 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000124 * - Check the on-board hardware monitor;
125 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000126 * On Siena-based NICs for power systems with EEH support, this will give EEH a
127 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100128 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000129static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100130
Ben Hutchings8ceee662008-04-27 12:55:59 +0100131/* Initial interrupt moderation settings. They can be modified after
132 * module load with ethtool.
133 *
134 * The default for RX should strike a balance between increasing the
135 * round-trip latency and reducing overhead.
136 */
137static unsigned int rx_irq_mod_usec = 60;
138
139/* Initial interrupt moderation settings. They can be modified after
140 * module load with ethtool.
141 *
142 * This default is chosen to ensure that a 10G link does not go idle
143 * while a TX queue is stopped after it has become full. A queue is
144 * restarted when it drops below half full. The time this takes (assuming
145 * worst case 3 descriptors per packet and 1024 descriptors) is
146 * 512 / 3 * 1.2 = 205 usec.
147 */
148static unsigned int tx_irq_mod_usec = 150;
149
150/* This is the first interrupt mode to try out of:
151 * 0 => MSI-X
152 * 1 => MSI
153 * 2 => legacy
154 */
155static unsigned int interrupt_mode;
156
157/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
158 * i.e. the number of CPUs among which we may distribute simultaneous
159 * interrupt handling.
160 *
161 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000162 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100163 */
164static unsigned int rss_cpus;
165module_param(rss_cpus, uint, 0444);
166MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
167
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000168static bool phy_flash_cfg;
169module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800170MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
171
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000172static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000173module_param(irq_adapt_low_thresh, uint, 0644);
174MODULE_PARM_DESC(irq_adapt_low_thresh,
175 "Threshold score for reducing IRQ moderation");
176
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000177static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000178module_param(irq_adapt_high_thresh, uint, 0644);
179MODULE_PARM_DESC(irq_adapt_high_thresh,
180 "Threshold score for increasing IRQ moderation");
181
Ben Hutchings62776d02010-06-23 11:30:07 +0000182static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
183 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
184 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
185 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
186module_param(debug, uint, 0);
187MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
188
Ben Hutchings8ceee662008-04-27 12:55:59 +0100189/**************************************************************************
190 *
191 * Utility functions and prototypes
192 *
193 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000194
Ben Hutchings7f967c02012-02-13 23:45:02 +0000195static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq);
196static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq);
197static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000198static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000199static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100200static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000201static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100202static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000203static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000204static void efx_fini_struct(struct efx_nic *efx);
205static void efx_start_all(struct efx_nic *efx);
206static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100207
208#define EFX_ASSERT_RESET_SERIALISED(efx) \
209 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100210 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000211 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000212 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100213 ASSERT_RTNL(); \
214 } while (0)
215
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100216static int efx_check_disabled(struct efx_nic *efx)
217{
Alexandre Rames626950d2013-01-14 17:20:22 +0000218 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100219 netif_err(efx, drv, efx->net_dev,
220 "device is disabled due to earlier errors\n");
221 return -EIO;
222 }
223 return 0;
224}
225
Ben Hutchings8ceee662008-04-27 12:55:59 +0100226/**************************************************************************
227 *
228 * Event queue processing
229 *
230 *************************************************************************/
231
232/* Process channel's event queue
233 *
234 * This function is responsible for processing the event queue of a
235 * single channel. The caller must guarantee that this function will
236 * never be concurrently called more than once on the same channel,
237 * though different channels may be being processed concurrently.
238 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000239static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100240{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000241 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100242
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000243 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100244 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100245
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000246 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000247 if (spent && efx_channel_has_rx_queue(channel)) {
248 struct efx_rx_queue *rx_queue =
249 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100250
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000251 efx_rx_flush_packet(channel);
Alexandre Rames97d48a12013-01-11 12:26:21 +0000252 if (rx_queue->enabled)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000253 efx_fast_push_rx_descriptors(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100254 }
255
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000256 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100257}
258
259/* Mark channel as finished processing
260 *
261 * Note that since we will not receive further interrupts for this
262 * channel before we finish processing and call the eventq_read_ack()
263 * method, there is no need to use the interrupt hold-off timers.
264 */
265static inline void efx_channel_processed(struct efx_channel *channel)
266{
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100267 /* The interrupt handler for this channel may set work_pending
268 * as soon as we acknowledge the events we've seen. Make sure
269 * it's cleared before then. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100270 channel->work_pending = false;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100271 smp_wmb();
272
Ben Hutchings152b6a62009-11-29 03:43:56 +0000273 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100274}
275
276/* NAPI poll handler
277 *
278 * NAPI guarantees serialisation of polls of the same device, which
279 * provides the guarantee required by efx_process_channel().
280 */
281static int efx_poll(struct napi_struct *napi, int budget)
282{
283 struct efx_channel *channel =
284 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000285 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000286 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100287
Ben Hutchings62776d02010-06-23 11:30:07 +0000288 netif_vdbg(efx, intr, efx->net_dev,
289 "channel %d NAPI poll executing on CPU %d\n",
290 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100291
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000292 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100293
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000294 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000295 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000296 efx->irq_rx_adaptive &&
297 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000298 if (unlikely(channel->irq_mod_score <
299 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000300 if (channel->irq_moderation > 1) {
301 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000302 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000303 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000304 } else if (unlikely(channel->irq_mod_score >
305 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000306 if (channel->irq_moderation <
307 efx->irq_rx_moderation) {
308 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000309 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000310 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000311 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000312 channel->irq_count = 0;
313 channel->irq_mod_score = 0;
314 }
315
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000316 efx_filter_rfs_expire(channel);
317
Ben Hutchings8ceee662008-04-27 12:55:59 +0100318 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800319 * only wait for napi_complete(), this isn't a problem
Ben Hutchings8ceee662008-04-27 12:55:59 +0100320 * since efx_channel_processed() will have no effect if
321 * interrupts have already been disabled.
322 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800323 napi_complete(napi);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100324 efx_channel_processed(channel);
325 }
326
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000327 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100328}
329
330/* Process the eventq of the specified channel immediately on this CPU
331 *
332 * Disable hardware generated interrupts, wait for any existing
333 * processing to finish, then directly poll (and ack ) the eventq.
334 * Finally reenable NAPI and interrupts.
335 *
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100336 * This is for use only during a loopback self-test. It must not
337 * deliver any packets up the stack as this can result in deadlock.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100338 */
339void efx_process_channel_now(struct efx_channel *channel)
340{
341 struct efx_nic *efx = channel->efx;
342
Ben Hutchings8313aca2010-09-10 06:41:57 +0000343 BUG_ON(channel->channel >= efx->n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100344 BUG_ON(!channel->enabled);
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100345 BUG_ON(!efx->loopback_selftest);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100346
347 /* Disable interrupts and wait for ISRs to complete */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000348 efx_nic_disable_interrupts(efx);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000349 if (efx->legacy_irq) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100350 synchronize_irq(efx->legacy_irq);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000351 efx->legacy_irq_enabled = false;
352 }
Ben Hutchings64ee3122008-09-01 12:47:38 +0100353 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100354 synchronize_irq(channel->irq);
355
356 /* Wait for any NAPI processing to complete */
357 napi_disable(&channel->napi_str);
358
359 /* Poll the channel */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000360 efx_process_channel(channel, channel->eventq_mask + 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100361
362 /* Ack the eventq. This may cause an interrupt to be generated
363 * when they are reenabled */
364 efx_channel_processed(channel);
365
366 napi_enable(&channel->napi_str);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000367 if (efx->legacy_irq)
368 efx->legacy_irq_enabled = true;
Ben Hutchings152b6a62009-11-29 03:43:56 +0000369 efx_nic_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100370}
371
372/* Create event queue
373 * Event queue memory allocations are done only once. If the channel
374 * is reset, the memory buffer will be reused; this guards against
375 * errors during channel reset and also simplifies interrupt handling.
376 */
377static int efx_probe_eventq(struct efx_channel *channel)
378{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000379 struct efx_nic *efx = channel->efx;
380 unsigned long entries;
381
Ben Hutchings86ee5302012-01-09 19:51:22 +0000382 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000383 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100384
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000385 /* Build an event queue with room for one event per tx and rx buffer,
386 * plus some extra for link state events and MCDI completions. */
387 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
388 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
389 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
390
Ben Hutchings152b6a62009-11-29 03:43:56 +0000391 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100392}
393
394/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100395static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100396{
Ben Hutchings62776d02010-06-23 11:30:07 +0000397 netif_dbg(channel->efx, drv, channel->efx->net_dev,
398 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100399
400 channel->eventq_read_ptr = 0;
401
Ben Hutchings152b6a62009-11-29 03:43:56 +0000402 efx_nic_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100403}
404
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000405/* Enable event queue processing and NAPI */
406static void efx_start_eventq(struct efx_channel *channel)
407{
408 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
409 "chan %d start event queue\n", channel->channel);
410
411 /* The interrupt handler for this channel may set work_pending
412 * as soon as we enable it. Make sure it's cleared before
413 * then. Similarly, make sure it sees the enabled flag set.
414 */
415 channel->work_pending = false;
416 channel->enabled = true;
417 smp_wmb();
418
419 napi_enable(&channel->napi_str);
420 efx_nic_eventq_read_ack(channel);
421}
422
423/* Disable event queue processing and NAPI */
424static void efx_stop_eventq(struct efx_channel *channel)
425{
426 if (!channel->enabled)
427 return;
428
429 napi_disable(&channel->napi_str);
430 channel->enabled = false;
431}
432
Ben Hutchings8ceee662008-04-27 12:55:59 +0100433static void efx_fini_eventq(struct efx_channel *channel)
434{
Ben Hutchings62776d02010-06-23 11:30:07 +0000435 netif_dbg(channel->efx, drv, channel->efx->net_dev,
436 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100437
Ben Hutchings152b6a62009-11-29 03:43:56 +0000438 efx_nic_fini_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100439}
440
441static void efx_remove_eventq(struct efx_channel *channel)
442{
Ben Hutchings62776d02010-06-23 11:30:07 +0000443 netif_dbg(channel->efx, drv, channel->efx->net_dev,
444 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100445
Ben Hutchings152b6a62009-11-29 03:43:56 +0000446 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100447}
448
449/**************************************************************************
450 *
451 * Channel handling
452 *
453 *************************************************************************/
454
Ben Hutchings7f967c02012-02-13 23:45:02 +0000455/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000456static struct efx_channel *
457efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
458{
459 struct efx_channel *channel;
460 struct efx_rx_queue *rx_queue;
461 struct efx_tx_queue *tx_queue;
462 int j;
463
Ben Hutchings7f967c02012-02-13 23:45:02 +0000464 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
465 if (!channel)
466 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000467
Ben Hutchings7f967c02012-02-13 23:45:02 +0000468 channel->efx = efx;
469 channel->channel = i;
470 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000471
Ben Hutchings7f967c02012-02-13 23:45:02 +0000472 for (j = 0; j < EFX_TXQ_TYPES; j++) {
473 tx_queue = &channel->tx_queue[j];
474 tx_queue->efx = efx;
475 tx_queue->queue = i * EFX_TXQ_TYPES + j;
476 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000477 }
478
Ben Hutchings46426102010-09-10 06:42:33 +0000479 rx_queue = &channel->rx_queue;
480 rx_queue->efx = efx;
481 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
482 (unsigned long)rx_queue);
483
484 return channel;
485}
486
Ben Hutchings7f967c02012-02-13 23:45:02 +0000487/* Allocate and initialise a channel structure, copying parameters
488 * (but not resources) from an old channel structure.
489 */
490static struct efx_channel *
491efx_copy_channel(const struct efx_channel *old_channel)
492{
493 struct efx_channel *channel;
494 struct efx_rx_queue *rx_queue;
495 struct efx_tx_queue *tx_queue;
496 int j;
497
498 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
499 if (!channel)
500 return NULL;
501
502 *channel = *old_channel;
503
504 channel->napi_dev = NULL;
505 memset(&channel->eventq, 0, sizeof(channel->eventq));
506
507 for (j = 0; j < EFX_TXQ_TYPES; j++) {
508 tx_queue = &channel->tx_queue[j];
509 if (tx_queue->channel)
510 tx_queue->channel = channel;
511 tx_queue->buffer = NULL;
512 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
513 }
514
515 rx_queue = &channel->rx_queue;
516 rx_queue->buffer = NULL;
517 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
518 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
519 (unsigned long)rx_queue);
520
521 return channel;
522}
523
Ben Hutchings8ceee662008-04-27 12:55:59 +0100524static int efx_probe_channel(struct efx_channel *channel)
525{
526 struct efx_tx_queue *tx_queue;
527 struct efx_rx_queue *rx_queue;
528 int rc;
529
Ben Hutchings62776d02010-06-23 11:30:07 +0000530 netif_dbg(channel->efx, probe, channel->efx->net_dev,
531 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100532
Ben Hutchings7f967c02012-02-13 23:45:02 +0000533 rc = channel->type->pre_probe(channel);
534 if (rc)
535 goto fail;
536
Ben Hutchings8ceee662008-04-27 12:55:59 +0100537 rc = efx_probe_eventq(channel);
538 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000539 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100540
541 efx_for_each_channel_tx_queue(tx_queue, channel) {
542 rc = efx_probe_tx_queue(tx_queue);
543 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000544 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100545 }
546
547 efx_for_each_channel_rx_queue(rx_queue, channel) {
548 rc = efx_probe_rx_queue(rx_queue);
549 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000550 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100551 }
552
553 channel->n_rx_frm_trunc = 0;
554
555 return 0;
556
Ben Hutchings7f967c02012-02-13 23:45:02 +0000557fail:
558 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100559 return rc;
560}
561
Ben Hutchings7f967c02012-02-13 23:45:02 +0000562static void
563efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
564{
565 struct efx_nic *efx = channel->efx;
566 const char *type;
567 int number;
568
569 number = channel->channel;
570 if (efx->tx_channel_offset == 0) {
571 type = "";
572 } else if (channel->channel < efx->tx_channel_offset) {
573 type = "-rx";
574 } else {
575 type = "-tx";
576 number -= efx->tx_channel_offset;
577 }
578 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
579}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100580
Ben Hutchings56536e92008-12-12 21:37:02 -0800581static void efx_set_channel_names(struct efx_nic *efx)
582{
583 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800584
Ben Hutchings7f967c02012-02-13 23:45:02 +0000585 efx_for_each_channel(channel, efx)
586 channel->type->get_name(channel,
587 efx->channel_name[channel->channel],
588 sizeof(efx->channel_name[0]));
Ben Hutchings56536e92008-12-12 21:37:02 -0800589}
590
Ben Hutchings46426102010-09-10 06:42:33 +0000591static int efx_probe_channels(struct efx_nic *efx)
592{
593 struct efx_channel *channel;
594 int rc;
595
596 /* Restart special buffer allocation */
597 efx->next_buffer_table = 0;
598
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000599 /* Probe channels in reverse, so that any 'extra' channels
600 * use the start of the buffer table. This allows the traffic
601 * channels to be resized without moving them or wasting the
602 * entries before them.
603 */
604 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000605 rc = efx_probe_channel(channel);
606 if (rc) {
607 netif_err(efx, probe, efx->net_dev,
608 "failed to create channel %d\n",
609 channel->channel);
610 goto fail;
611 }
612 }
613 efx_set_channel_names(efx);
614
615 return 0;
616
617fail:
618 efx_remove_channels(efx);
619 return rc;
620}
621
Ben Hutchings8ceee662008-04-27 12:55:59 +0100622/* Channels are shutdown and reinitialised whilst the NIC is running
623 * to propagate configuration changes (mtu, checksum offload), or
624 * to clear hardware error conditions
625 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000626static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100627{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000628 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100629 struct efx_tx_queue *tx_queue;
630 struct efx_rx_queue *rx_queue;
631 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000632 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100633
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100634 /* Calculate the rx buffer allocation parameters required to
635 * support the current MTU, including padding for header
636 * alignment and overruns.
637 */
Ben Hutchings272baee2013-01-29 23:33:14 +0000638 efx->rx_dma_len = (efx->type->rx_buffer_hash_size +
639 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
640 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000641 rx_buf_len = (sizeof(struct efx_rx_page_state) +
642 EFX_PAGE_IP_ALIGN + efx->rx_dma_len);
643 if (rx_buf_len <= PAGE_SIZE) {
644 efx->rx_scatter = false;
645 efx->rx_buffer_order = 0;
646 if (rx_buf_len <= PAGE_SIZE / 2)
647 efx->rx_buffer_truesize = PAGE_SIZE / 2;
648 else
649 efx->rx_buffer_truesize = PAGE_SIZE;
650 } else if (efx->type->can_rx_scatter) {
651 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
652 EFX_PAGE_IP_ALIGN + EFX_RX_USR_BUF_SIZE >
653 PAGE_SIZE / 2);
654 efx->rx_scatter = true;
655 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
656 efx->rx_buffer_order = 0;
657 efx->rx_buffer_truesize = PAGE_SIZE / 2;
658 } else {
659 efx->rx_scatter = false;
660 efx->rx_buffer_order = get_order(rx_buf_len);
661 efx->rx_buffer_truesize = PAGE_SIZE << efx->rx_buffer_order;
662 }
663
664 /* RX filters also have scatter-enabled flags */
665 if (efx->rx_scatter != old_rx_scatter)
666 efx_filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100667
Ben Hutchings14bf7182012-05-22 01:27:58 +0100668 /* We must keep at least one descriptor in a TX ring empty.
669 * We could avoid this when the queue size does not exactly
670 * match the hardware ring size, but it's not that important.
671 * Therefore we stop the queue when one more skb might fill
672 * the ring completely. We wake it when half way back to
673 * empty.
674 */
675 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
676 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
677
Ben Hutchings8ceee662008-04-27 12:55:59 +0100678 /* Initialise the channels */
679 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100680 efx_for_each_channel_tx_queue(tx_queue, channel)
681 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100682
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000683 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100684 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000685 efx_nic_generate_fill_event(rx_queue);
686 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100687
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000688 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100689 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000690
691 if (netif_device_present(efx->net_dev))
692 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100693}
694
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000695static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100696{
697 struct efx_channel *channel;
698 struct efx_tx_queue *tx_queue;
699 struct efx_rx_queue *rx_queue;
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100700 struct pci_dev *dev = efx->pci_dev;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100701 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100702
703 EFX_ASSERT_RESET_SERIALISED(efx);
704 BUG_ON(efx->port_enabled);
705
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100706 /* Only perform flush if dma is enabled */
Alexandre Rames626950d2013-01-14 17:20:22 +0000707 if (dev->is_busmaster && efx->state != STATE_RECOVERY) {
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100708 rc = efx_nic_flush_queues(efx);
709
710 if (rc && EFX_WORKAROUND_7803(efx)) {
711 /* Schedule a reset to recover from the flush failure. The
712 * descriptor caches reference memory we're about to free,
713 * but falcon_reconfigure_mac_wrapper() won't reconnect
714 * the MACs because of the pending reset. */
715 netif_err(efx, drv, efx->net_dev,
716 "Resetting to recover from flush failure\n");
717 efx_schedule_reset(efx, RESET_TYPE_ALL);
718 } else if (rc) {
719 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
720 } else {
721 netif_dbg(efx, drv, efx->net_dev,
722 "successfully flushed all queues\n");
723 }
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000724 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100725
Ben Hutchings8ceee662008-04-27 12:55:59 +0100726 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000727 /* RX packet processing is pipelined, so wait for the
728 * NAPI handler to complete. At least event queue 0
729 * might be kept active by non-data events, so don't
730 * use napi_synchronize() but actually disable NAPI
731 * temporarily.
732 */
733 if (efx_channel_has_rx_queue(channel)) {
734 efx_stop_eventq(channel);
735 efx_start_eventq(channel);
736 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100737
738 efx_for_each_channel_rx_queue(rx_queue, channel)
739 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000740 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100741 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100742 }
743}
744
745static void efx_remove_channel(struct efx_channel *channel)
746{
747 struct efx_tx_queue *tx_queue;
748 struct efx_rx_queue *rx_queue;
749
Ben Hutchings62776d02010-06-23 11:30:07 +0000750 netif_dbg(channel->efx, drv, channel->efx->net_dev,
751 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100752
753 efx_for_each_channel_rx_queue(rx_queue, channel)
754 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000755 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100756 efx_remove_tx_queue(tx_queue);
757 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100758 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100759}
760
Ben Hutchings46426102010-09-10 06:42:33 +0000761static void efx_remove_channels(struct efx_nic *efx)
762{
763 struct efx_channel *channel;
764
765 efx_for_each_channel(channel, efx)
766 efx_remove_channel(channel);
767}
768
769int
770efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
771{
772 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
773 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000774 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100775 int rc;
776
777 rc = efx_check_disabled(efx);
778 if (rc)
779 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000780
781 /* Not all channels should be reallocated. We must avoid
782 * reallocating their buffer table entries.
783 */
784 efx_for_each_channel(channel, efx) {
785 struct efx_rx_queue *rx_queue;
786 struct efx_tx_queue *tx_queue;
787
788 if (channel->type->copy)
789 continue;
790 next_buffer_table = max(next_buffer_table,
791 channel->eventq.index +
792 channel->eventq.entries);
793 efx_for_each_channel_rx_queue(rx_queue, channel)
794 next_buffer_table = max(next_buffer_table,
795 rx_queue->rxd.index +
796 rx_queue->rxd.entries);
797 efx_for_each_channel_tx_queue(tx_queue, channel)
798 next_buffer_table = max(next_buffer_table,
799 tx_queue->txd.index +
800 tx_queue->txd.entries);
801 }
Ben Hutchings46426102010-09-10 06:42:33 +0000802
Ben Hutchings29c69a42013-01-28 19:01:06 +0000803 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000804 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000805 efx_stop_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000806
Ben Hutchings7f967c02012-02-13 23:45:02 +0000807 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000808 memset(other_channel, 0, sizeof(other_channel));
809 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000810 channel = efx->channel[i];
811 if (channel->type->copy)
812 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000813 if (!channel) {
814 rc = -ENOMEM;
815 goto out;
816 }
817 other_channel[i] = channel;
818 }
819
820 /* Swap entry counts and channel pointers */
821 old_rxq_entries = efx->rxq_entries;
822 old_txq_entries = efx->txq_entries;
823 efx->rxq_entries = rxq_entries;
824 efx->txq_entries = txq_entries;
825 for (i = 0; i < efx->n_channels; i++) {
826 channel = efx->channel[i];
827 efx->channel[i] = other_channel[i];
828 other_channel[i] = channel;
829 }
830
Ben Hutchings7f967c02012-02-13 23:45:02 +0000831 /* Restart buffer table allocation */
832 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000833
Ben Hutchingse8f14992010-12-07 19:47:34 +0000834 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000835 channel = efx->channel[i];
836 if (!channel->type->copy)
837 continue;
838 rc = efx_probe_channel(channel);
839 if (rc)
840 goto rollback;
841 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000842 }
Ben Hutchings46426102010-09-10 06:42:33 +0000843
Ben Hutchings7f967c02012-02-13 23:45:02 +0000844out:
845 /* Destroy unused channel structures */
846 for (i = 0; i < efx->n_channels; i++) {
847 channel = other_channel[i];
848 if (channel && channel->type->copy) {
849 efx_fini_napi_channel(channel);
850 efx_remove_channel(channel);
851 kfree(channel);
852 }
853 }
854
855 efx_start_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000856 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000857 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000858 return rc;
859
860rollback:
861 /* Swap back */
862 efx->rxq_entries = old_rxq_entries;
863 efx->txq_entries = old_txq_entries;
864 for (i = 0; i < efx->n_channels; i++) {
865 channel = efx->channel[i];
866 efx->channel[i] = other_channel[i];
867 other_channel[i] = channel;
868 }
869 goto out;
870}
871
Steve Hodgson90d683a2010-06-01 11:19:39 +0000872void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100873{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000874 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100875}
876
Ben Hutchings7f967c02012-02-13 23:45:02 +0000877static const struct efx_channel_type efx_default_channel_type = {
878 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100879 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000880 .get_name = efx_get_channel_name,
881 .copy = efx_copy_channel,
882 .keep_eventq = false,
883};
884
885int efx_channel_dummy_op_int(struct efx_channel *channel)
886{
887 return 0;
888}
889
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100890void efx_channel_dummy_op_void(struct efx_channel *channel)
891{
892}
893
Ben Hutchings8ceee662008-04-27 12:55:59 +0100894/**************************************************************************
895 *
896 * Port handling
897 *
898 **************************************************************************/
899
900/* This ensures that the kernel is kept informed (via
901 * netif_carrier_on/off) of the link status, and also maintains the
902 * link status's stop on the port's TX queue.
903 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000904void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100905{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000906 struct efx_link_state *link_state = &efx->link_state;
907
Ben Hutchings8ceee662008-04-27 12:55:59 +0100908 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
909 * that no events are triggered between unregister_netdev() and the
910 * driver unloading. A more general condition is that NETDEV_CHANGE
911 * can only be generated between NETDEV_UP and NETDEV_DOWN */
912 if (!netif_running(efx->net_dev))
913 return;
914
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000915 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100916 efx->n_link_state_changes++;
917
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000918 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100919 netif_carrier_on(efx->net_dev);
920 else
921 netif_carrier_off(efx->net_dev);
922 }
923
924 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000925 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000926 netif_info(efx, link, efx->net_dev,
927 "link up at %uMbps %s-duplex (MTU %d)%s\n",
928 link_state->speed, link_state->fd ? "full" : "half",
929 efx->net_dev->mtu,
930 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000931 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000932 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100933}
934
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000935void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
936{
937 efx->link_advertising = advertising;
938 if (advertising) {
939 if (advertising & ADVERTISED_Pause)
940 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
941 else
942 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
943 if (advertising & ADVERTISED_Asym_Pause)
944 efx->wanted_fc ^= EFX_FC_TX;
945 }
946}
947
David S. Millerb56269462011-05-17 17:53:22 -0400948void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000949{
950 efx->wanted_fc = wanted_fc;
951 if (efx->link_advertising) {
952 if (wanted_fc & EFX_FC_RX)
953 efx->link_advertising |= (ADVERTISED_Pause |
954 ADVERTISED_Asym_Pause);
955 else
956 efx->link_advertising &= ~(ADVERTISED_Pause |
957 ADVERTISED_Asym_Pause);
958 if (wanted_fc & EFX_FC_TX)
959 efx->link_advertising ^= ADVERTISED_Asym_Pause;
960 }
961}
962
Ben Hutchings115122a2009-03-04 09:52:52 +0000963static void efx_fini_port(struct efx_nic *efx);
964
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000965/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
966 * the MAC appropriately. All other PHY configuration changes are pushed
967 * through phy_op->set_settings(), and pushed asynchronously to the MAC
968 * through efx_monitor().
969 *
970 * Callers must hold the mac_lock
971 */
972int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100973{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000974 enum efx_phy_mode phy_mode;
975 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100976
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000977 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100978
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000979 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000980 netif_addr_lock_bh(efx->net_dev);
981 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100982
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000983 /* Disable PHY transmit in mac level loopbacks */
984 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800985 if (LOOPBACK_INTERNAL(efx))
986 efx->phy_mode |= PHY_MODE_TX_DISABLED;
987 else
988 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800989
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000990 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800991
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000992 if (rc)
993 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100994
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000995 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100996}
997
998/* Reinitialise the MAC to pick up new PHY settings, even if the port is
999 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001000int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001001{
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001002 int rc;
1003
Ben Hutchings8ceee662008-04-27 12:55:59 +01001004 EFX_ASSERT_RESET_SERIALISED(efx);
1005
1006 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001007 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001008 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001009
1010 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001011}
1012
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001013/* Asynchronous work item for changing MAC promiscuity and multicast
1014 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
1015 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001016static void efx_mac_work(struct work_struct *data)
1017{
1018 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
1019
1020 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +01001021 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +01001022 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001023 mutex_unlock(&efx->mac_lock);
1024}
1025
Ben Hutchings8ceee662008-04-27 12:55:59 +01001026static int efx_probe_port(struct efx_nic *efx)
1027{
1028 int rc;
1029
Ben Hutchings62776d02010-06-23 11:30:07 +00001030 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001031
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001032 if (phy_flash_cfg)
1033 efx->phy_mode = PHY_MODE_SPECIAL;
1034
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001035 /* Connect up MAC/PHY operations table */
1036 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001037 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001038 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001039
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001040 /* Initialise MAC address to permanent address */
1041 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001042
1043 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001044}
1045
1046static int efx_init_port(struct efx_nic *efx)
1047{
1048 int rc;
1049
Ben Hutchings62776d02010-06-23 11:30:07 +00001050 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001051
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001052 mutex_lock(&efx->mac_lock);
1053
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001054 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001055 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001056 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001057
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001058 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001059
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001060 /* Reconfigure the MAC before creating dma queues (required for
1061 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001062 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001063
1064 /* Ensure the PHY advertises the correct flow control settings */
1065 rc = efx->phy_op->reconfigure(efx);
1066 if (rc)
1067 goto fail2;
1068
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001069 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001070 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001071
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001072fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001073 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001074fail1:
1075 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001076 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001077}
1078
Ben Hutchings8ceee662008-04-27 12:55:59 +01001079static void efx_start_port(struct efx_nic *efx)
1080{
Ben Hutchings62776d02010-06-23 11:30:07 +00001081 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001082 BUG_ON(efx->port_enabled);
1083
1084 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001085 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001086
1087 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1088 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001089 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001090
Ben Hutchings8ceee662008-04-27 12:55:59 +01001091 mutex_unlock(&efx->mac_lock);
1092}
1093
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001094/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001095static void efx_stop_port(struct efx_nic *efx)
1096{
Ben Hutchings62776d02010-06-23 11:30:07 +00001097 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001098
1099 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001100 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001101 mutex_unlock(&efx->mac_lock);
1102
1103 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001104 netif_addr_lock_bh(efx->net_dev);
1105 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001106}
1107
1108static void efx_fini_port(struct efx_nic *efx)
1109{
Ben Hutchings62776d02010-06-23 11:30:07 +00001110 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001111
1112 if (!efx->port_initialized)
1113 return;
1114
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001115 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001116 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001117
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001118 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001119 efx_link_status_changed(efx);
1120}
1121
1122static void efx_remove_port(struct efx_nic *efx)
1123{
Ben Hutchings62776d02010-06-23 11:30:07 +00001124 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001125
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001126 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001127}
1128
1129/**************************************************************************
1130 *
1131 * NIC handling
1132 *
1133 **************************************************************************/
1134
1135/* This configures the PCI device to enable I/O and DMA. */
1136static int efx_init_io(struct efx_nic *efx)
1137{
1138 struct pci_dev *pci_dev = efx->pci_dev;
1139 dma_addr_t dma_mask = efx->type->max_dma_mask;
1140 int rc;
1141
Ben Hutchings62776d02010-06-23 11:30:07 +00001142 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001143
1144 rc = pci_enable_device(pci_dev);
1145 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001146 netif_err(efx, probe, efx->net_dev,
1147 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001148 goto fail1;
1149 }
1150
1151 pci_set_master(pci_dev);
1152
1153 /* Set the PCI DMA mask. Try all possibilities from our
1154 * genuine mask down to 32 bits, because some architectures
1155 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1156 * masks event though they reject 46 bit masks.
1157 */
1158 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001159 if (dma_supported(&pci_dev->dev, dma_mask)) {
1160 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001161 if (rc == 0)
1162 break;
1163 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001164 dma_mask >>= 1;
1165 }
1166 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001167 netif_err(efx, probe, efx->net_dev,
1168 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001169 goto fail2;
1170 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001171 netif_dbg(efx, probe, efx->net_dev,
1172 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001173 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001174 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001175 /* dma_set_coherent_mask() is not *allowed* to
1176 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001177 * but just in case...
1178 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001179 netif_err(efx, probe, efx->net_dev,
1180 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001181 goto fail2;
1182 }
1183
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001184 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1185 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001186 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001187 netif_err(efx, probe, efx->net_dev,
1188 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001189 rc = -EIO;
1190 goto fail3;
1191 }
David S. Miller8decf862011-09-22 03:23:13 -04001192 efx->membase = ioremap_nocache(efx->membase_phys,
1193 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001194 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001195 netif_err(efx, probe, efx->net_dev,
1196 "could not map memory BAR at %llx+%x\n",
1197 (unsigned long long)efx->membase_phys,
1198 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001199 rc = -ENOMEM;
1200 goto fail4;
1201 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001202 netif_dbg(efx, probe, efx->net_dev,
1203 "memory BAR at %llx+%x (virtual %p)\n",
1204 (unsigned long long)efx->membase_phys,
1205 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001206
1207 return 0;
1208
1209 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001210 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001211 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001212 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001213 fail2:
1214 pci_disable_device(efx->pci_dev);
1215 fail1:
1216 return rc;
1217}
1218
1219static void efx_fini_io(struct efx_nic *efx)
1220{
Ben Hutchings62776d02010-06-23 11:30:07 +00001221 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001222
1223 if (efx->membase) {
1224 iounmap(efx->membase);
1225 efx->membase = NULL;
1226 }
1227
1228 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001229 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001230 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001231 }
1232
1233 pci_disable_device(efx->pci_dev);
1234}
1235
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001236static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001237{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001238 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001239 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001240 int cpu;
1241
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001242 if (rss_cpus) {
1243 count = rss_cpus;
1244 } else {
1245 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1246 netif_warn(efx, probe, efx->net_dev,
1247 "RSS disabled due to allocation failure\n");
1248 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001249 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001250
1251 count = 0;
1252 for_each_online_cpu(cpu) {
1253 if (!cpumask_test_cpu(cpu, thread_mask)) {
1254 ++count;
1255 cpumask_or(thread_mask, thread_mask,
1256 topology_thread_cpumask(cpu));
1257 }
1258 }
1259
1260 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001261 }
1262
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001263 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1264 * table entries that are inaccessible to VFs
1265 */
1266 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1267 count > efx_vf_size(efx)) {
1268 netif_warn(efx, probe, efx->net_dev,
1269 "Reducing number of RSS channels from %u to %u for "
1270 "VF support. Increase vf-msix-limit to use more "
1271 "channels on the PF.\n",
1272 count, efx_vf_size(efx));
1273 count = efx_vf_size(efx);
1274 }
1275
Ben Hutchings46123d02008-09-01 12:47:33 +01001276 return count;
1277}
1278
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001279static int
1280efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1281{
1282#ifdef CONFIG_RFS_ACCEL
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001283 unsigned int i;
1284 int rc;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001285
1286 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1287 if (!efx->net_dev->rx_cpu_rmap)
1288 return -ENOMEM;
1289 for (i = 0; i < efx->n_rx_channels; i++) {
1290 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1291 xentries[i].vector);
1292 if (rc) {
1293 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1294 efx->net_dev->rx_cpu_rmap = NULL;
1295 return rc;
1296 }
1297 }
1298#endif
1299 return 0;
1300}
1301
Ben Hutchings46123d02008-09-01 12:47:33 +01001302/* Probe the number and type of interrupts we are able to obtain, and
1303 * the resulting numbers of channels and RX queues.
1304 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001305static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001306{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001307 unsigned int max_channels =
1308 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001309 unsigned int extra_channels = 0;
1310 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001311 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001312
Ben Hutchings7f967c02012-02-13 23:45:02 +00001313 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1314 if (efx->extra_channel_type[i])
1315 ++extra_channels;
1316
Ben Hutchings8ceee662008-04-27 12:55:59 +01001317 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001318 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001319 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001320
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001321 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001322 if (separate_tx_channels)
1323 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001324 n_channels += extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001325 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001326
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001327 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001328 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001329 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001330 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001331 netif_err(efx, drv, efx->net_dev,
1332 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001333 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001334 netif_err(efx, drv, efx->net_dev,
1335 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001336 EFX_BUG_ON_PARANOID(rc >= n_channels);
1337 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001338 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001339 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001340 }
1341
1342 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001343 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001344 if (n_channels > extra_channels)
1345 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001346 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001347 efx->n_tx_channels = max(n_channels / 2, 1U);
1348 efx->n_rx_channels = max(n_channels -
1349 efx->n_tx_channels,
1350 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001351 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001352 efx->n_tx_channels = n_channels;
1353 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001354 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001355 rc = efx_init_rx_cpu_rmap(efx, xentries);
1356 if (rc) {
1357 pci_disable_msix(efx->pci_dev);
1358 return rc;
1359 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001360 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001361 efx_get_channel(efx, i)->irq =
1362 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001363 } else {
1364 /* Fall back to single channel MSI */
1365 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001366 netif_err(efx, drv, efx->net_dev,
1367 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001368 }
1369 }
1370
1371 /* Try single interrupt MSI */
1372 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001373 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001374 efx->n_rx_channels = 1;
1375 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001376 rc = pci_enable_msi(efx->pci_dev);
1377 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001378 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001379 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001380 netif_err(efx, drv, efx->net_dev,
1381 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001382 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1383 }
1384 }
1385
1386 /* Assume legacy interrupts */
1387 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001388 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001389 efx->n_rx_channels = 1;
1390 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001391 efx->legacy_irq = efx->pci_dev->irq;
1392 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001393
Ben Hutchings7f967c02012-02-13 23:45:02 +00001394 /* Assign extra channels if possible */
1395 j = efx->n_channels;
1396 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1397 if (!efx->extra_channel_type[i])
1398 continue;
1399 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1400 efx->n_channels <= extra_channels) {
1401 efx->extra_channel_type[i]->handle_no_channel(efx);
1402 } else {
1403 --j;
1404 efx_get_channel(efx, j)->type =
1405 efx->extra_channel_type[i];
1406 }
1407 }
1408
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001409 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001410 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001411 efx->n_rx_channels : efx_vf_size(efx));
1412
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001413 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001414}
1415
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001416/* Enable interrupts, then probe and start the event queues */
Ben Hutchings7f967c02012-02-13 23:45:02 +00001417static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001418{
1419 struct efx_channel *channel;
1420
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001421 BUG_ON(efx->state == STATE_DISABLED);
1422
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001423 if (efx->legacy_irq)
1424 efx->legacy_irq_enabled = true;
1425 efx_nic_enable_interrupts(efx);
1426
1427 efx_for_each_channel(channel, efx) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001428 if (!channel->type->keep_eventq || !may_keep_eventq)
1429 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001430 efx_start_eventq(channel);
1431 }
1432
1433 efx_mcdi_mode_event(efx);
1434}
1435
Ben Hutchings7f967c02012-02-13 23:45:02 +00001436static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001437{
1438 struct efx_channel *channel;
1439
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001440 if (efx->state == STATE_DISABLED)
1441 return;
1442
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001443 efx_mcdi_mode_poll(efx);
1444
1445 efx_nic_disable_interrupts(efx);
1446 if (efx->legacy_irq) {
1447 synchronize_irq(efx->legacy_irq);
1448 efx->legacy_irq_enabled = false;
1449 }
1450
1451 efx_for_each_channel(channel, efx) {
1452 if (channel->irq)
1453 synchronize_irq(channel->irq);
1454
1455 efx_stop_eventq(channel);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001456 if (!channel->type->keep_eventq || !may_keep_eventq)
1457 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001458 }
1459}
1460
Ben Hutchings8ceee662008-04-27 12:55:59 +01001461static void efx_remove_interrupts(struct efx_nic *efx)
1462{
1463 struct efx_channel *channel;
1464
1465 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001466 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001467 channel->irq = 0;
1468 pci_disable_msi(efx->pci_dev);
1469 pci_disable_msix(efx->pci_dev);
1470
1471 /* Remove legacy interrupt */
1472 efx->legacy_irq = 0;
1473}
1474
Ben Hutchings8831da72008-09-01 12:47:48 +01001475static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001476{
Ben Hutchings602a5322011-05-16 17:32:39 +01001477 struct efx_channel *channel;
1478 struct efx_tx_queue *tx_queue;
1479
Ben Hutchings97653432011-01-12 18:26:56 +00001480 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001481 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001482
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001483 /* We need to mark which channels really have RX and TX
1484 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001485 * RX-only and TX-only channels.
1486 */
1487 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001488 if (channel->channel < efx->n_rx_channels)
1489 channel->rx_queue.core_index = channel->channel;
1490 else
1491 channel->rx_queue.core_index = -1;
1492
Ben Hutchings602a5322011-05-16 17:32:39 +01001493 efx_for_each_channel_tx_queue(tx_queue, channel)
1494 tx_queue->queue -= (efx->tx_channel_offset *
1495 EFX_TXQ_TYPES);
1496 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001497}
1498
1499static int efx_probe_nic(struct efx_nic *efx)
1500{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001501 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001502 int rc;
1503
Ben Hutchings62776d02010-06-23 11:30:07 +00001504 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001505
1506 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001507 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001508 if (rc)
1509 return rc;
1510
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001511 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001512 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001513 rc = efx_probe_interrupts(efx);
1514 if (rc)
1515 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001516
Ben Hutchings28e47c42012-02-15 01:58:49 +00001517 efx->type->dimension_resources(efx);
1518
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001519 if (efx->n_channels > 1)
1520 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001521 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001522 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001523 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001524
Ben Hutchings8831da72008-09-01 12:47:48 +01001525 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001526 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1527 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001528
1529 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001530 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1531 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001532
1533 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001534
1535fail:
1536 efx->type->remove(efx);
1537 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001538}
1539
1540static void efx_remove_nic(struct efx_nic *efx)
1541{
Ben Hutchings62776d02010-06-23 11:30:07 +00001542 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001543
1544 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001545 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001546}
1547
1548/**************************************************************************
1549 *
1550 * NIC startup/shutdown
1551 *
1552 *************************************************************************/
1553
1554static int efx_probe_all(struct efx_nic *efx)
1555{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001556 int rc;
1557
Ben Hutchings8ceee662008-04-27 12:55:59 +01001558 rc = efx_probe_nic(efx);
1559 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001560 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001561 goto fail1;
1562 }
1563
Ben Hutchings8ceee662008-04-27 12:55:59 +01001564 rc = efx_probe_port(efx);
1565 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001566 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001567 goto fail2;
1568 }
1569
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001570 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1571 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1572 rc = -EINVAL;
1573 goto fail3;
1574 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001575 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001576
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001577 rc = efx_probe_filters(efx);
1578 if (rc) {
1579 netif_err(efx, probe, efx->net_dev,
1580 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001581 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001582 }
1583
Ben Hutchings7f967c02012-02-13 23:45:02 +00001584 rc = efx_probe_channels(efx);
1585 if (rc)
1586 goto fail4;
1587
Ben Hutchings8ceee662008-04-27 12:55:59 +01001588 return 0;
1589
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001590 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001591 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001592 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001593 efx_remove_port(efx);
1594 fail2:
1595 efx_remove_nic(efx);
1596 fail1:
1597 return rc;
1598}
1599
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001600/* If the interface is supposed to be running but is not, start
1601 * the hardware and software data path, regular activity for the port
1602 * (MAC statistics, link polling, etc.) and schedule the port to be
1603 * reconfigured. Interrupts must already be enabled. This function
1604 * is safe to call multiple times, so long as the NIC is not disabled.
1605 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001606 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001607static void efx_start_all(struct efx_nic *efx)
1608{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001609 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001610 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001611
1612 /* Check that it is appropriate to restart the interface. All
1613 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001614 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001615 return;
1616
Ben Hutchings8ceee662008-04-27 12:55:59 +01001617 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001618 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001619
Alexandre Rames626950d2013-01-14 17:20:22 +00001620 /* Start the hardware monitor if there is one */
1621 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001622 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1623 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001624
1625 /* If link state detection is normally event-driven, we have
1626 * to poll now because we could have missed a change
1627 */
1628 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001629 mutex_lock(&efx->mac_lock);
1630 if (efx->phy_op->poll(efx))
1631 efx_link_status_changed(efx);
1632 mutex_unlock(&efx->mac_lock);
1633 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001634
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001635 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001636}
1637
1638/* Flush all delayed work. Should only be called when no more delayed work
1639 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1640 * since we're holding the rtnl_lock at this point. */
1641static void efx_flush_all(struct efx_nic *efx)
1642{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001643 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001644 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001645 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001646 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001647 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001648}
1649
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001650/* Quiesce the hardware and software data path, and regular activity
1651 * for the port without bringing the link down. Safe to call multiple
1652 * times with the NIC in almost any state, but interrupts should be
1653 * enabled. Requires the RTNL lock.
1654 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001655static void efx_stop_all(struct efx_nic *efx)
1656{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001657 EFX_ASSERT_RESET_SERIALISED(efx);
1658
1659 /* port_enabled can be read safely under the rtnl lock */
1660 if (!efx->port_enabled)
1661 return;
1662
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001663 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001664 efx_stop_port(efx);
1665
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001666 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001667 efx_flush_all(efx);
1668
Ben Hutchings29c69a42013-01-28 19:01:06 +00001669 /* Stop the kernel transmit interface. This is only valid if
1670 * the device is stopped or detached; otherwise the watchdog
1671 * may fire immediately.
1672 */
1673 WARN_ON(netif_running(efx->net_dev) &&
1674 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001675 netif_tx_disable(efx->net_dev);
1676
1677 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001678}
1679
1680static void efx_remove_all(struct efx_nic *efx)
1681{
Ben Hutchings46426102010-09-10 06:42:33 +00001682 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001683 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001684 efx_remove_port(efx);
1685 efx_remove_nic(efx);
1686}
1687
Ben Hutchings8ceee662008-04-27 12:55:59 +01001688/**************************************************************************
1689 *
1690 * Interrupt moderation
1691 *
1692 **************************************************************************/
1693
Ben Hutchingscc180b62011-12-08 19:51:47 +00001694static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001695{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001696 if (usecs == 0)
1697 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001698 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001699 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001700 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001701}
1702
Ben Hutchings8ceee662008-04-27 12:55:59 +01001703/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001704int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1705 unsigned int rx_usecs, bool rx_adaptive,
1706 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001707{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001708 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001709 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1710 efx->timer_quantum_ns,
1711 1000);
1712 unsigned int tx_ticks;
1713 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001714
1715 EFX_ASSERT_RESET_SERIALISED(efx);
1716
Ben Hutchingscc180b62011-12-08 19:51:47 +00001717 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001718 return -EINVAL;
1719
Ben Hutchingscc180b62011-12-08 19:51:47 +00001720 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1721 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1722
Ben Hutchings9e393b32011-09-05 07:43:04 +00001723 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1724 !rx_may_override_tx) {
1725 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1726 "RX and TX IRQ moderation must be equal\n");
1727 return -EINVAL;
1728 }
1729
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001730 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001731 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001732 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001733 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001734 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001735 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001736 channel->irq_moderation = tx_ticks;
1737 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001738
1739 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001740}
1741
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001742void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1743 unsigned int *rx_usecs, bool *rx_adaptive)
1744{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001745 /* We must round up when converting ticks to microseconds
1746 * because we round down when converting the other way.
1747 */
1748
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001749 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001750 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1751 efx->timer_quantum_ns,
1752 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001753
1754 /* If channels are shared between RX and TX, so is IRQ
1755 * moderation. Otherwise, IRQ moderation is the same for all
1756 * TX channels and is not adaptive.
1757 */
1758 if (efx->tx_channel_offset == 0)
1759 *tx_usecs = *rx_usecs;
1760 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001761 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001762 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001763 efx->timer_quantum_ns,
1764 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001765}
1766
Ben Hutchings8ceee662008-04-27 12:55:59 +01001767/**************************************************************************
1768 *
1769 * Hardware monitor
1770 *
1771 **************************************************************************/
1772
Ben Hutchingse254c272010-09-20 08:44:10 +00001773/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001774static void efx_monitor(struct work_struct *data)
1775{
1776 struct efx_nic *efx = container_of(data, struct efx_nic,
1777 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001778
Ben Hutchings62776d02010-06-23 11:30:07 +00001779 netif_vdbg(efx, timer, efx->net_dev,
1780 "hardware monitor executing on CPU %d\n",
1781 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001782 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001783
Ben Hutchings8ceee662008-04-27 12:55:59 +01001784 /* If the mac_lock is already held then it is likely a port
1785 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001786 * most of the work of monitor() anyway. */
1787 if (mutex_trylock(&efx->mac_lock)) {
1788 if (efx->port_enabled)
1789 efx->type->monitor(efx);
1790 mutex_unlock(&efx->mac_lock);
1791 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001792
Ben Hutchings8ceee662008-04-27 12:55:59 +01001793 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1794 efx_monitor_interval);
1795}
1796
1797/**************************************************************************
1798 *
1799 * ioctls
1800 *
1801 *************************************************************************/
1802
1803/* Net device ioctl
1804 * Context: process, rtnl_lock() held.
1805 */
1806static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1807{
Ben Hutchings767e4682008-09-01 12:43:14 +01001808 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001809 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001810
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001811 if (cmd == SIOCSHWTSTAMP)
1812 return efx_ptp_ioctl(efx, ifr, cmd);
1813
Ben Hutchings68e7f452009-04-29 08:05:08 +00001814 /* Convert phy_id from older PRTAD/DEVAD format */
1815 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1816 (data->phy_id & 0xfc00) == 0x0400)
1817 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1818
1819 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001820}
1821
1822/**************************************************************************
1823 *
1824 * NAPI interface
1825 *
1826 **************************************************************************/
1827
Ben Hutchings7f967c02012-02-13 23:45:02 +00001828static void efx_init_napi_channel(struct efx_channel *channel)
1829{
1830 struct efx_nic *efx = channel->efx;
1831
1832 channel->napi_dev = efx->net_dev;
1833 netif_napi_add(channel->napi_dev, &channel->napi_str,
1834 efx_poll, napi_weight);
1835}
1836
Ben Hutchingse8f14992010-12-07 19:47:34 +00001837static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001838{
1839 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001840
Ben Hutchings7f967c02012-02-13 23:45:02 +00001841 efx_for_each_channel(channel, efx)
1842 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001843}
1844
1845static void efx_fini_napi_channel(struct efx_channel *channel)
1846{
1847 if (channel->napi_dev)
1848 netif_napi_del(&channel->napi_str);
1849 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001850}
1851
1852static void efx_fini_napi(struct efx_nic *efx)
1853{
1854 struct efx_channel *channel;
1855
Ben Hutchingse8f14992010-12-07 19:47:34 +00001856 efx_for_each_channel(channel, efx)
1857 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001858}
1859
1860/**************************************************************************
1861 *
1862 * Kernel netpoll interface
1863 *
1864 *************************************************************************/
1865
1866#ifdef CONFIG_NET_POLL_CONTROLLER
1867
1868/* Although in the common case interrupts will be disabled, this is not
1869 * guaranteed. However, all our work happens inside the NAPI callback,
1870 * so no locking is required.
1871 */
1872static void efx_netpoll(struct net_device *net_dev)
1873{
Ben Hutchings767e4682008-09-01 12:43:14 +01001874 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001875 struct efx_channel *channel;
1876
Ben Hutchings64ee3122008-09-01 12:47:38 +01001877 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001878 efx_schedule_channel(channel);
1879}
1880
1881#endif
1882
1883/**************************************************************************
1884 *
1885 * Kernel net device interface
1886 *
1887 *************************************************************************/
1888
1889/* Context: process, rtnl_lock() held. */
1890static int efx_net_open(struct net_device *net_dev)
1891{
Ben Hutchings767e4682008-09-01 12:43:14 +01001892 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001893 int rc;
1894
Ben Hutchings62776d02010-06-23 11:30:07 +00001895 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1896 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001897
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001898 rc = efx_check_disabled(efx);
1899 if (rc)
1900 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001901 if (efx->phy_mode & PHY_MODE_SPECIAL)
1902 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001903 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1904 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001905
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001906 /* Notify the kernel of the link state polled during driver load,
1907 * before the monitor starts running */
1908 efx_link_status_changed(efx);
1909
Ben Hutchings8ceee662008-04-27 12:55:59 +01001910 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001911 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001912 return 0;
1913}
1914
1915/* Context: process, rtnl_lock() held.
1916 * Note that the kernel will ignore our return code; this method
1917 * should really be a void.
1918 */
1919static int efx_net_stop(struct net_device *net_dev)
1920{
Ben Hutchings767e4682008-09-01 12:43:14 +01001921 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001922
Ben Hutchings62776d02010-06-23 11:30:07 +00001923 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1924 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001925
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001926 /* Stop the device and flush all the channels */
1927 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001928
1929 return 0;
1930}
1931
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001932/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001933static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1934 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001935{
Ben Hutchings767e4682008-09-01 12:43:14 +01001936 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001937 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001938
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001939 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001940
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001941 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001942
1943 stats->rx_packets = mac_stats->rx_packets;
1944 stats->tx_packets = mac_stats->tx_packets;
1945 stats->rx_bytes = mac_stats->rx_bytes;
1946 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001947 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001948 stats->multicast = mac_stats->rx_multicast;
1949 stats->collisions = mac_stats->tx_collision;
1950 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1951 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001952 stats->rx_crc_errors = mac_stats->rx_bad;
1953 stats->rx_frame_errors = mac_stats->rx_align_error;
1954 stats->rx_fifo_errors = mac_stats->rx_overflow;
1955 stats->rx_missed_errors = mac_stats->rx_missed;
1956 stats->tx_window_errors = mac_stats->tx_late_collision;
1957
1958 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001959 stats->rx_crc_errors +
1960 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001961 mac_stats->rx_symbol_error);
1962 stats->tx_errors = (stats->tx_window_errors +
1963 mac_stats->tx_bad);
1964
Ben Hutchings1cb34522011-09-02 23:23:00 +01001965 spin_unlock_bh(&efx->stats_lock);
1966
Ben Hutchings8ceee662008-04-27 12:55:59 +01001967 return stats;
1968}
1969
1970/* Context: netif_tx_lock held, BHs disabled. */
1971static void efx_watchdog(struct net_device *net_dev)
1972{
Ben Hutchings767e4682008-09-01 12:43:14 +01001973 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001974
Ben Hutchings62776d02010-06-23 11:30:07 +00001975 netif_err(efx, tx_err, efx->net_dev,
1976 "TX stuck with port_enabled=%d: resetting channels\n",
1977 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001978
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001979 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001980}
1981
1982
1983/* Context: process, rtnl_lock() held. */
1984static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1985{
Ben Hutchings767e4682008-09-01 12:43:14 +01001986 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001987 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001988
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001989 rc = efx_check_disabled(efx);
1990 if (rc)
1991 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001992 if (new_mtu > EFX_MAX_MTU)
1993 return -EINVAL;
1994
Ben Hutchings62776d02010-06-23 11:30:07 +00001995 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001996
Ben Hutchings29c69a42013-01-28 19:01:06 +00001997 efx_device_detach_sync(efx);
1998 efx_stop_all(efx);
1999
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002000 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002001 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01002002 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002003 mutex_unlock(&efx->mac_lock);
2004
Ben Hutchings8ceee662008-04-27 12:55:59 +01002005 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00002006 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002007 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002008}
2009
2010static int efx_set_mac_address(struct net_device *net_dev, void *data)
2011{
Ben Hutchings767e4682008-09-01 12:43:14 +01002012 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002013 struct sockaddr *addr = data;
2014 char *new_addr = addr->sa_data;
2015
Ben Hutchings8ceee662008-04-27 12:55:59 +01002016 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002017 netif_err(efx, drv, efx->net_dev,
2018 "invalid ethernet MAC address requested: %pM\n",
2019 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002020 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002021 }
2022
2023 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002024 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002025
2026 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002027 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002028 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002029 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002030
2031 return 0;
2032}
2033
Ben Hutchingsa816f752008-09-01 12:49:12 +01002034/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002035static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002036{
Ben Hutchings767e4682008-09-01 12:43:14 +01002037 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002038 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002039 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002040 u32 crc;
2041 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002042
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002043 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002044
2045 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002046 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002047 memset(mc_hash, 0xff, sizeof(*mc_hash));
2048 } else {
2049 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00002050 netdev_for_each_mc_addr(ha, net_dev) {
2051 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002052 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
Ben Hutchings32766ec2012-10-04 17:13:03 -07002053 __set_bit_le(bit, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002054 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002055
2056 /* Broadcast packets go through the multicast hash filter.
2057 * ether_crc_le() of the broadcast address is 0xbe2612ff
2058 * so we always add bit 0xff to the mask.
2059 */
Ben Hutchings32766ec2012-10-04 17:13:03 -07002060 __set_bit_le(0xff, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002061 }
2062
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002063 if (efx->port_enabled)
2064 queue_work(efx->workqueue, &efx->mac_work);
2065 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002066}
2067
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002068static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002069{
2070 struct efx_nic *efx = netdev_priv(net_dev);
2071
2072 /* If disabling RX n-tuple filtering, clear existing filters */
2073 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2074 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2075
2076 return 0;
2077}
2078
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002079static const struct net_device_ops efx_netdev_ops = {
2080 .ndo_open = efx_net_open,
2081 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002082 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002083 .ndo_tx_timeout = efx_watchdog,
2084 .ndo_start_xmit = efx_hard_start_xmit,
2085 .ndo_validate_addr = eth_validate_addr,
2086 .ndo_do_ioctl = efx_ioctl,
2087 .ndo_change_mtu = efx_change_mtu,
2088 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002089 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002090 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002091#ifdef CONFIG_SFC_SRIOV
2092 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2093 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2094 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2095 .ndo_get_vf_config = efx_sriov_get_vf_config,
2096#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002097#ifdef CONFIG_NET_POLL_CONTROLLER
2098 .ndo_poll_controller = efx_netpoll,
2099#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002100 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002101#ifdef CONFIG_RFS_ACCEL
2102 .ndo_rx_flow_steer = efx_filter_rfs,
2103#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002104};
2105
Ben Hutchings7dde5962008-12-12 22:09:38 -08002106static void efx_update_name(struct efx_nic *efx)
2107{
2108 strcpy(efx->name, efx->net_dev->name);
2109 efx_mtd_rename(efx);
2110 efx_set_channel_names(efx);
2111}
2112
Ben Hutchings8ceee662008-04-27 12:55:59 +01002113static int efx_netdev_event(struct notifier_block *this,
2114 unsigned long event, void *ptr)
2115{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01002116 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002117
Ben Hutchings7dde5962008-12-12 22:09:38 -08002118 if (net_dev->netdev_ops == &efx_netdev_ops &&
2119 event == NETDEV_CHANGENAME)
2120 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002121
2122 return NOTIFY_DONE;
2123}
2124
2125static struct notifier_block efx_netdev_notifier = {
2126 .notifier_call = efx_netdev_event,
2127};
2128
Ben Hutchings06d5e192008-12-12 21:47:23 -08002129static ssize_t
2130show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2131{
2132 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2133 return sprintf(buf, "%d\n", efx->phy_type);
2134}
2135static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
2136
Ben Hutchings8ceee662008-04-27 12:55:59 +01002137static int efx_register_netdev(struct efx_nic *efx)
2138{
2139 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002140 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002141 int rc;
2142
2143 net_dev->watchdog_timeo = 5 * HZ;
2144 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002145 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002146 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002147 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002148
Ben Hutchings7dde5962008-12-12 22:09:38 -08002149 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002150
Ben Hutchings7153f622012-07-27 20:50:52 +01002151 /* Enable resets to be scheduled and check whether any were
2152 * already requested. If so, the NIC is probably hosed so we
2153 * abort.
2154 */
2155 efx->state = STATE_READY;
2156 smp_mb(); /* ensure we change state before checking reset_pending */
2157 if (efx->reset_pending) {
2158 netif_err(efx, probe, efx->net_dev,
2159 "aborting probe due to scheduled reset\n");
2160 rc = -EIO;
2161 goto fail_locked;
2162 }
2163
Ben Hutchingsaed06282009-08-26 08:16:27 +00002164 rc = dev_alloc_name(net_dev, net_dev->name);
2165 if (rc < 0)
2166 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002167 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002168
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002169 /* Always start with carrier off; PHY events will detect the link */
2170 netif_carrier_off(net_dev);
2171
Ben Hutchingsaed06282009-08-26 08:16:27 +00002172 rc = register_netdevice(net_dev);
2173 if (rc)
2174 goto fail_locked;
2175
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002176 efx_for_each_channel(channel, efx) {
2177 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002178 efx_for_each_channel_tx_queue(tx_queue, channel)
2179 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002180 }
2181
Ben Hutchings7dde5962008-12-12 22:09:38 -08002182 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002183
Ben Hutchings06d5e192008-12-12 21:47:23 -08002184 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2185 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002186 netif_err(efx, drv, efx->net_dev,
2187 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002188 goto fail_registered;
2189 }
2190
Ben Hutchings8ceee662008-04-27 12:55:59 +01002191 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002192
Ben Hutchings7153f622012-07-27 20:50:52 +01002193fail_registered:
2194 rtnl_lock();
2195 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002196fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002197 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002198 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002199 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002200 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002201}
2202
2203static void efx_unregister_netdev(struct efx_nic *efx)
2204{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002205 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002206 struct efx_tx_queue *tx_queue;
2207
2208 if (!efx->net_dev)
2209 return;
2210
Ben Hutchings767e4682008-09-01 12:43:14 +01002211 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002212
2213 /* Free up any skbs still remaining. This has to happen before
2214 * we try to unregister the netdev as running their destructors
2215 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002216 efx_for_each_channel(channel, efx) {
2217 efx_for_each_channel_tx_queue(tx_queue, channel)
2218 efx_release_tx_buffers(tx_queue);
2219 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002220
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002221 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2222 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002223
2224 rtnl_lock();
2225 unregister_netdevice(efx->net_dev);
2226 efx->state = STATE_UNINIT;
2227 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002228}
2229
2230/**************************************************************************
2231 *
2232 * Device reset and suspend
2233 *
2234 **************************************************************************/
2235
Ben Hutchings2467ca42008-09-01 12:48:50 +01002236/* Tears down the entire software state and most of the hardware state
2237 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002238void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002239{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002240 EFX_ASSERT_RESET_SERIALISED(efx);
2241
Ben Hutchings2467ca42008-09-01 12:48:50 +01002242 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00002243 efx_stop_interrupts(efx, false);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002244
2245 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002246 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2247 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002248 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002249}
2250
Ben Hutchings2467ca42008-09-01 12:48:50 +01002251/* This function will always ensure that the locks acquired in
2252 * efx_reset_down() are released. A failure return code indicates
2253 * that we were unable to reinitialise the hardware, and the
2254 * driver should be disabled. If ok is false, then the rx and tx
2255 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002256int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002257{
2258 int rc;
2259
Ben Hutchings2467ca42008-09-01 12:48:50 +01002260 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002261
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002262 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002263 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002264 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002265 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002266 }
2267
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002268 if (!ok)
2269 goto fail;
2270
Steve Hodgson4b988282009-01-29 17:50:51 +00002271 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002272 rc = efx->phy_op->init(efx);
2273 if (rc)
2274 goto fail;
2275 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002276 netif_err(efx, drv, efx->net_dev,
2277 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002278 }
2279
Ben Hutchings710b2082011-09-03 00:15:00 +01002280 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002281
Ben Hutchings7f967c02012-02-13 23:45:02 +00002282 efx_start_interrupts(efx, false);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002283 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002284 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002285
2286 mutex_unlock(&efx->mac_lock);
2287
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002288 efx_start_all(efx);
2289
2290 return 0;
2291
2292fail:
2293 efx->port_initialized = false;
2294
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002295 mutex_unlock(&efx->mac_lock);
2296
Ben Hutchings8ceee662008-04-27 12:55:59 +01002297 return rc;
2298}
2299
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002300/* Reset the NIC using the specified method. Note that the reset may
2301 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002302 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002303 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002304 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002305int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002306{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002307 int rc, rc2;
2308 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002309
Ben Hutchings62776d02010-06-23 11:30:07 +00002310 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2311 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002312
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002313 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002314 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002315
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002316 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002317 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002318 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002319 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002320 }
2321
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002322 /* Clear flags for the scopes we covered. We assume the NIC and
2323 * driver are now quiescent so that there is no race here.
2324 */
2325 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002326
2327 /* Reinitialise bus-mastering, which may have been turned off before
2328 * the reset was scheduled. This is still appropriate, even in the
2329 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2330 * can respond to requests. */
2331 pci_set_master(efx->pci_dev);
2332
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002333out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002334 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002335 disabled = rc ||
2336 method == RESET_TYPE_DISABLE ||
2337 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002338 rc2 = efx_reset_up(efx, method, !disabled);
2339 if (rc2) {
2340 disabled = true;
2341 if (!rc)
2342 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002343 }
2344
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002345 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002346 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002347 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002348 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002349 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002350 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002351 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002352 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002353 return rc;
2354}
2355
Alexandre Rames626950d2013-01-14 17:20:22 +00002356/* Try recovery mechanisms.
2357 * For now only EEH is supported.
2358 * Returns 0 if the recovery mechanisms are unsuccessful.
2359 * Returns a non-zero value otherwise.
2360 */
2361static int efx_try_recovery(struct efx_nic *efx)
2362{
2363#ifdef CONFIG_EEH
2364 /* A PCI error can occur and not be seen by EEH because nothing
2365 * happens on the PCI bus. In this case the driver may fail and
2366 * schedule a 'recover or reset', leading to this recovery handler.
2367 * Manually call the eeh failure check function.
2368 */
2369 struct eeh_dev *eehdev =
2370 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2371
2372 if (eeh_dev_check_failure(eehdev)) {
2373 /* The EEH mechanisms will handle the error and reset the
2374 * device if necessary.
2375 */
2376 return 1;
2377 }
2378#endif
2379 return 0;
2380}
2381
Ben Hutchings8ceee662008-04-27 12:55:59 +01002382/* The worker thread exists so that code that cannot sleep can
2383 * schedule a reset for later.
2384 */
2385static void efx_reset_work(struct work_struct *data)
2386{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002387 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002388 unsigned long pending;
2389 enum reset_type method;
2390
2391 pending = ACCESS_ONCE(efx->reset_pending);
2392 method = fls(pending) - 1;
2393
2394 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2395 method == RESET_TYPE_RECOVER_OR_ALL) &&
2396 efx_try_recovery(efx))
2397 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002398
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002399 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002400 return;
2401
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002402 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002403
2404 /* We checked the state in efx_schedule_reset() but it may
2405 * have changed by now. Now that we have the RTNL lock,
2406 * it cannot change again.
2407 */
2408 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002409 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002410
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002411 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002412}
2413
2414void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2415{
2416 enum reset_type method;
2417
Alexandre Rames626950d2013-01-14 17:20:22 +00002418 if (efx->state == STATE_RECOVERY) {
2419 netif_dbg(efx, drv, efx->net_dev,
2420 "recovering: skip scheduling %s reset\n",
2421 RESET_TYPE(type));
2422 return;
2423 }
2424
Ben Hutchings8ceee662008-04-27 12:55:59 +01002425 switch (type) {
2426 case RESET_TYPE_INVISIBLE:
2427 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002428 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002429 case RESET_TYPE_WORLD:
2430 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002431 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002432 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002433 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2434 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002435 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002436 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002437 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002438 netif_dbg(efx, drv, efx->net_dev,
2439 "scheduling %s reset for %s\n",
2440 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002441 break;
2442 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002443
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002444 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002445 smp_mb(); /* ensure we change reset_pending before checking state */
2446
2447 /* If we're not READY then just leave the flags set as the cue
2448 * to abort probing or reschedule the reset later.
2449 */
2450 if (ACCESS_ONCE(efx->state) != STATE_READY)
2451 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002452
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002453 /* efx_process_channel() will no longer read events once a
2454 * reset is scheduled. So switch back to poll'd MCDI completions. */
2455 efx_mcdi_mode_poll(efx);
2456
Steve Hodgson1ab00622008-12-12 21:33:02 -08002457 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002458}
2459
2460/**************************************************************************
2461 *
2462 * List of NICs we support
2463 *
2464 **************************************************************************/
2465
2466/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002467static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002468 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2469 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002470 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002471 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2472 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002473 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002474 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002475 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002476 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002477 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002478 {0} /* end of list */
2479};
2480
2481/**************************************************************************
2482 *
Ben Hutchings37594332009-11-23 16:05:45 +00002483 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002484 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002485 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002486 * Needed so all function pointers are valid and do not have to be tested
2487 * before use
2488 *
2489 **************************************************************************/
2490int efx_port_dummy_op_int(struct efx_nic *efx)
2491{
2492 return 0;
2493}
2494void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002495
2496static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002497{
2498 return false;
2499}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002500
stephen hemminger6c8c2512011-04-14 05:50:12 +00002501static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002502 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002503 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002504 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002505 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002506};
2507
Ben Hutchings8ceee662008-04-27 12:55:59 +01002508/**************************************************************************
2509 *
2510 * Data housekeeping
2511 *
2512 **************************************************************************/
2513
2514/* This zeroes out and then fills in the invariants in a struct
2515 * efx_nic (including all sub-structures).
2516 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002517static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002518 struct pci_dev *pci_dev, struct net_device *net_dev)
2519{
Ben Hutchings46426102010-09-10 06:42:33 +00002520 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002521
2522 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002523 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002524#ifdef CONFIG_SFC_MTD
2525 INIT_LIST_HEAD(&efx->mtd_list);
2526#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002527 INIT_WORK(&efx->reset_work, efx_reset_work);
2528 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002529 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002530 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002531 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002532 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002533 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002534
2535 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002536 spin_lock_init(&efx->stats_lock);
2537 mutex_init(&efx->mac_lock);
2538 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002539 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002540 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002541 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002542
2543 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002544 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2545 if (!efx->channel[i])
2546 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002547 }
2548
Ben Hutchings8ceee662008-04-27 12:55:59 +01002549 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2550
2551 /* Higher numbered interrupt modes are less capable! */
2552 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2553 interrupt_mode);
2554
Ben Hutchings6977dc62008-12-26 13:44:39 -08002555 /* Would be good to use the net_dev name, but we're too early */
2556 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2557 pci_name(pci_dev));
2558 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002559 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002560 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002561
Ben Hutchings8ceee662008-04-27 12:55:59 +01002562 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002563
2564fail:
2565 efx_fini_struct(efx);
2566 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002567}
2568
2569static void efx_fini_struct(struct efx_nic *efx)
2570{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002571 int i;
2572
2573 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2574 kfree(efx->channel[i]);
2575
Ben Hutchings8ceee662008-04-27 12:55:59 +01002576 if (efx->workqueue) {
2577 destroy_workqueue(efx->workqueue);
2578 efx->workqueue = NULL;
2579 }
2580}
2581
2582/**************************************************************************
2583 *
2584 * PCI interface
2585 *
2586 **************************************************************************/
2587
2588/* Main body of final NIC shutdown code
2589 * This is called only at module unload (or hotplug removal).
2590 */
2591static void efx_pci_remove_main(struct efx_nic *efx)
2592{
Ben Hutchings7153f622012-07-27 20:50:52 +01002593 /* Flush reset_work. It can no longer be scheduled since we
2594 * are not READY.
2595 */
2596 BUG_ON(efx->state == STATE_READY);
2597 cancel_work_sync(&efx->reset_work);
2598
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002599#ifdef CONFIG_RFS_ACCEL
2600 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2601 efx->net_dev->rx_cpu_rmap = NULL;
2602#endif
Ben Hutchings7f967c02012-02-13 23:45:02 +00002603 efx_stop_interrupts(efx, false);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002604 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002605 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002606 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002607 efx_fini_napi(efx);
2608 efx_remove_all(efx);
2609}
2610
2611/* Final NIC shutdown
2612 * This is called only at module unload (or hotplug removal).
2613 */
2614static void efx_pci_remove(struct pci_dev *pci_dev)
2615{
2616 struct efx_nic *efx;
2617
2618 efx = pci_get_drvdata(pci_dev);
2619 if (!efx)
2620 return;
2621
2622 /* Mark the NIC as fini, then stop the interface */
2623 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002624 dev_close(efx->net_dev);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002625 efx_stop_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002626 rtnl_unlock();
2627
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002628 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002629 efx_unregister_netdev(efx);
2630
Ben Hutchings7dde5962008-12-12 22:09:38 -08002631 efx_mtd_remove(efx);
2632
Ben Hutchings8ceee662008-04-27 12:55:59 +01002633 efx_pci_remove_main(efx);
2634
Ben Hutchings8ceee662008-04-27 12:55:59 +01002635 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002636 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002637
Ben Hutchings8ceee662008-04-27 12:55:59 +01002638 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002639 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002640 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002641
2642 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002643};
2644
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002645/* NIC VPD information
2646 * Called during probe to display the part number of the
2647 * installed NIC. VPD is potentially very large but this should
2648 * always appear within the first 512 bytes.
2649 */
2650#define SFC_VPD_LEN 512
2651static void efx_print_product_vpd(struct efx_nic *efx)
2652{
2653 struct pci_dev *dev = efx->pci_dev;
2654 char vpd_data[SFC_VPD_LEN];
2655 ssize_t vpd_size;
2656 int i, j;
2657
2658 /* Get the vpd data from the device */
2659 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2660 if (vpd_size <= 0) {
2661 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2662 return;
2663 }
2664
2665 /* Get the Read only section */
2666 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2667 if (i < 0) {
2668 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2669 return;
2670 }
2671
2672 j = pci_vpd_lrdt_size(&vpd_data[i]);
2673 i += PCI_VPD_LRDT_TAG_SIZE;
2674 if (i + j > vpd_size)
2675 j = vpd_size - i;
2676
2677 /* Get the Part number */
2678 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2679 if (i < 0) {
2680 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2681 return;
2682 }
2683
2684 j = pci_vpd_info_field_size(&vpd_data[i]);
2685 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2686 if (i + j > vpd_size) {
2687 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2688 return;
2689 }
2690
2691 netif_info(efx, drv, efx->net_dev,
2692 "Part Number : %.*s\n", j, &vpd_data[i]);
2693}
2694
2695
Ben Hutchings8ceee662008-04-27 12:55:59 +01002696/* Main body of NIC initialisation
2697 * This is called at module load (or hotplug insertion, theoretically).
2698 */
2699static int efx_pci_probe_main(struct efx_nic *efx)
2700{
2701 int rc;
2702
2703 /* Do start-of-day initialisation */
2704 rc = efx_probe_all(efx);
2705 if (rc)
2706 goto fail1;
2707
Ben Hutchingse8f14992010-12-07 19:47:34 +00002708 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002709
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002710 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002711 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002712 netif_err(efx, probe, efx->net_dev,
2713 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002714 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002715 }
2716
2717 rc = efx_init_port(efx);
2718 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002719 netif_err(efx, probe, efx->net_dev,
2720 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002721 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002722 }
2723
Ben Hutchings152b6a62009-11-29 03:43:56 +00002724 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002725 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002726 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00002727 efx_start_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002728
2729 return 0;
2730
Ben Hutchings278c0622009-11-23 16:05:12 +00002731 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002732 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002733 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002734 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002735 fail3:
2736 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002737 efx_remove_all(efx);
2738 fail1:
2739 return rc;
2740}
2741
2742/* NIC initialisation
2743 *
2744 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002745 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002746 * sets up and registers the network devices with the kernel and hooks
2747 * the interrupt service routine. It does not prepare the device for
2748 * transmission; this is left to the first time one of the network
2749 * interfaces is brought up (i.e. efx_net_open).
2750 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002751static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002752 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002753{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002754 struct net_device *net_dev;
2755 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002756 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002757
2758 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002759 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2760 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002761 if (!net_dev)
2762 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002763 efx = netdev_priv(net_dev);
2764 efx->type = (const struct efx_nic_type *) entry->driver_data;
2765 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002766 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002767 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002768 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002769 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002770 /* Mask for features that also apply to VLAN devices */
2771 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002772 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2773 NETIF_F_RXCSUM);
2774 /* All offloads can be toggled */
2775 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002776 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002777 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002778 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002779 if (rc)
2780 goto fail1;
2781
Ben Hutchings62776d02010-06-23 11:30:07 +00002782 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002783 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002784
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002785 efx_print_product_vpd(efx);
2786
Ben Hutchings8ceee662008-04-27 12:55:59 +01002787 /* Set up basic I/O (BAR mappings etc) */
2788 rc = efx_init_io(efx);
2789 if (rc)
2790 goto fail2;
2791
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002792 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002793 if (rc)
2794 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002795
Ben Hutchings8ceee662008-04-27 12:55:59 +01002796 rc = efx_register_netdev(efx);
2797 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002798 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002799
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002800 rc = efx_sriov_init(efx);
2801 if (rc)
2802 netif_err(efx, probe, efx->net_dev,
2803 "SR-IOV can't be enabled rc %d\n", rc);
2804
Ben Hutchings62776d02010-06-23 11:30:07 +00002805 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002806
Ben Hutchings7c431612012-01-27 17:23:58 +00002807 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002808 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002809 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002810 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002811 if (rc)
2812 netif_warn(efx, probe, efx->net_dev,
2813 "failed to create MTDs (%d)\n", rc);
2814
Alexandre Rames626950d2013-01-14 17:20:22 +00002815 rc = pci_enable_pcie_error_reporting(pci_dev);
2816 if (rc && rc != -EINVAL)
2817 netif_warn(efx, probe, efx->net_dev,
2818 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2819
Ben Hutchings8ceee662008-04-27 12:55:59 +01002820 return 0;
2821
Ben Hutchings8ceee662008-04-27 12:55:59 +01002822 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002823 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002824 fail3:
2825 efx_fini_io(efx);
2826 fail2:
2827 efx_fini_struct(efx);
2828 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002829 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002830 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002831 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002832 free_netdev(net_dev);
2833 return rc;
2834}
2835
Ben Hutchings89c758f2009-11-29 03:43:07 +00002836static int efx_pm_freeze(struct device *dev)
2837{
2838 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2839
Ben Hutchings61da0262012-07-27 19:35:39 +01002840 rtnl_lock();
2841
Ben Hutchings6032fb52012-07-27 19:35:47 +01002842 if (efx->state != STATE_DISABLED) {
2843 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002844
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002845 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002846
Ben Hutchings6032fb52012-07-27 19:35:47 +01002847 efx_stop_all(efx);
2848 efx_stop_interrupts(efx, false);
2849 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002850
Ben Hutchings61da0262012-07-27 19:35:39 +01002851 rtnl_unlock();
2852
Ben Hutchings89c758f2009-11-29 03:43:07 +00002853 return 0;
2854}
2855
2856static int efx_pm_thaw(struct device *dev)
2857{
2858 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2859
Ben Hutchings61da0262012-07-27 19:35:39 +01002860 rtnl_lock();
2861
Ben Hutchings6032fb52012-07-27 19:35:47 +01002862 if (efx->state != STATE_DISABLED) {
2863 efx_start_interrupts(efx, false);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002864
Ben Hutchings6032fb52012-07-27 19:35:47 +01002865 mutex_lock(&efx->mac_lock);
2866 efx->phy_op->reconfigure(efx);
2867 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002868
Ben Hutchings6032fb52012-07-27 19:35:47 +01002869 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002870
Ben Hutchings6032fb52012-07-27 19:35:47 +01002871 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002872
Ben Hutchings6032fb52012-07-27 19:35:47 +01002873 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002874
Ben Hutchings6032fb52012-07-27 19:35:47 +01002875 efx->type->resume_wol(efx);
2876 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002877
Ben Hutchings61da0262012-07-27 19:35:39 +01002878 rtnl_unlock();
2879
Steve Hodgson319ba642010-06-01 11:17:24 +00002880 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2881 queue_work(reset_workqueue, &efx->reset_work);
2882
Ben Hutchings89c758f2009-11-29 03:43:07 +00002883 return 0;
2884}
2885
2886static int efx_pm_poweroff(struct device *dev)
2887{
2888 struct pci_dev *pci_dev = to_pci_dev(dev);
2889 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2890
2891 efx->type->fini(efx);
2892
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002893 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002894
2895 pci_save_state(pci_dev);
2896 return pci_set_power_state(pci_dev, PCI_D3hot);
2897}
2898
2899/* Used for both resume and restore */
2900static int efx_pm_resume(struct device *dev)
2901{
2902 struct pci_dev *pci_dev = to_pci_dev(dev);
2903 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2904 int rc;
2905
2906 rc = pci_set_power_state(pci_dev, PCI_D0);
2907 if (rc)
2908 return rc;
2909 pci_restore_state(pci_dev);
2910 rc = pci_enable_device(pci_dev);
2911 if (rc)
2912 return rc;
2913 pci_set_master(efx->pci_dev);
2914 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2915 if (rc)
2916 return rc;
2917 rc = efx->type->init(efx);
2918 if (rc)
2919 return rc;
2920 efx_pm_thaw(dev);
2921 return 0;
2922}
2923
2924static int efx_pm_suspend(struct device *dev)
2925{
2926 int rc;
2927
2928 efx_pm_freeze(dev);
2929 rc = efx_pm_poweroff(dev);
2930 if (rc)
2931 efx_pm_resume(dev);
2932 return rc;
2933}
2934
Ben Hutchings18e83e42012-01-05 19:05:20 +00002935static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002936 .suspend = efx_pm_suspend,
2937 .resume = efx_pm_resume,
2938 .freeze = efx_pm_freeze,
2939 .thaw = efx_pm_thaw,
2940 .poweroff = efx_pm_poweroff,
2941 .restore = efx_pm_resume,
2942};
2943
Alexandre Rames626950d2013-01-14 17:20:22 +00002944/* A PCI error affecting this device was detected.
2945 * At this point MMIO and DMA may be disabled.
2946 * Stop the software path and request a slot reset.
2947 */
2948pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2949 enum pci_channel_state state)
2950{
2951 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2952 struct efx_nic *efx = pci_get_drvdata(pdev);
2953
2954 if (state == pci_channel_io_perm_failure)
2955 return PCI_ERS_RESULT_DISCONNECT;
2956
2957 rtnl_lock();
2958
2959 if (efx->state != STATE_DISABLED) {
2960 efx->state = STATE_RECOVERY;
2961 efx->reset_pending = 0;
2962
2963 efx_device_detach_sync(efx);
2964
2965 efx_stop_all(efx);
2966 efx_stop_interrupts(efx, false);
2967
2968 status = PCI_ERS_RESULT_NEED_RESET;
2969 } else {
2970 /* If the interface is disabled we don't want to do anything
2971 * with it.
2972 */
2973 status = PCI_ERS_RESULT_RECOVERED;
2974 }
2975
2976 rtnl_unlock();
2977
2978 pci_disable_device(pdev);
2979
2980 return status;
2981}
2982
2983/* Fake a successfull reset, which will be performed later in efx_io_resume. */
2984pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
2985{
2986 struct efx_nic *efx = pci_get_drvdata(pdev);
2987 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2988 int rc;
2989
2990 if (pci_enable_device(pdev)) {
2991 netif_err(efx, hw, efx->net_dev,
2992 "Cannot re-enable PCI device after reset.\n");
2993 status = PCI_ERS_RESULT_DISCONNECT;
2994 }
2995
2996 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2997 if (rc) {
2998 netif_err(efx, hw, efx->net_dev,
2999 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3000 /* Non-fatal error. Continue. */
3001 }
3002
3003 return status;
3004}
3005
3006/* Perform the actual reset and resume I/O operations. */
3007static void efx_io_resume(struct pci_dev *pdev)
3008{
3009 struct efx_nic *efx = pci_get_drvdata(pdev);
3010 int rc;
3011
3012 rtnl_lock();
3013
3014 if (efx->state == STATE_DISABLED)
3015 goto out;
3016
3017 rc = efx_reset(efx, RESET_TYPE_ALL);
3018 if (rc) {
3019 netif_err(efx, hw, efx->net_dev,
3020 "efx_reset failed after PCI error (%d)\n", rc);
3021 } else {
3022 efx->state = STATE_READY;
3023 netif_dbg(efx, hw, efx->net_dev,
3024 "Done resetting and resuming IO after PCI error.\n");
3025 }
3026
3027out:
3028 rtnl_unlock();
3029}
3030
3031/* For simplicity and reliability, we always require a slot reset and try to
3032 * reset the hardware when a pci error affecting the device is detected.
3033 * We leave both the link_reset and mmio_enabled callback unimplemented:
3034 * with our request for slot reset the mmio_enabled callback will never be
3035 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3036 */
3037static struct pci_error_handlers efx_err_handlers = {
3038 .error_detected = efx_io_error_detected,
3039 .slot_reset = efx_io_slot_reset,
3040 .resume = efx_io_resume,
3041};
3042
Ben Hutchings8ceee662008-04-27 12:55:59 +01003043static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003044 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003045 .id_table = efx_pci_table,
3046 .probe = efx_pci_probe,
3047 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003048 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003049 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003050};
3051
3052/**************************************************************************
3053 *
3054 * Kernel module interface
3055 *
3056 *************************************************************************/
3057
3058module_param(interrupt_mode, uint, 0444);
3059MODULE_PARM_DESC(interrupt_mode,
3060 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3061
3062static int __init efx_init_module(void)
3063{
3064 int rc;
3065
3066 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3067
3068 rc = register_netdevice_notifier(&efx_netdev_notifier);
3069 if (rc)
3070 goto err_notifier;
3071
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003072 rc = efx_init_sriov();
3073 if (rc)
3074 goto err_sriov;
3075
Steve Hodgson1ab00622008-12-12 21:33:02 -08003076 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3077 if (!reset_workqueue) {
3078 rc = -ENOMEM;
3079 goto err_reset;
3080 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003081
3082 rc = pci_register_driver(&efx_pci_driver);
3083 if (rc < 0)
3084 goto err_pci;
3085
3086 return 0;
3087
3088 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003089 destroy_workqueue(reset_workqueue);
3090 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003091 efx_fini_sriov();
3092 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003093 unregister_netdevice_notifier(&efx_netdev_notifier);
3094 err_notifier:
3095 return rc;
3096}
3097
3098static void __exit efx_exit_module(void)
3099{
3100 printk(KERN_INFO "Solarflare NET driver unloading\n");
3101
3102 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003103 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003104 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003105 unregister_netdevice_notifier(&efx_netdev_notifier);
3106
3107}
3108
3109module_init(efx_init_module);
3110module_exit(efx_exit_module);
3111
Ben Hutchings906bb262009-11-29 15:16:19 +00003112MODULE_AUTHOR("Solarflare Communications and "
3113 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003114MODULE_DESCRIPTION("Solarflare Communications network driver");
3115MODULE_LICENSE("GPL");
3116MODULE_DEVICE_TABLE(pci, efx_pci_table);