blob: f050248e9fba54f62d8b6de4e81f607bd1594970 [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;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000646 } else if (efx->type->can_rx_scatter) {
647 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
648 EFX_PAGE_IP_ALIGN + EFX_RX_USR_BUF_SIZE >
649 PAGE_SIZE / 2);
650 efx->rx_scatter = true;
651 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
652 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000653 } else {
654 efx->rx_scatter = false;
655 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000656 }
657
Daniel Pieczko1648a232013-02-13 10:54:41 +0000658 efx_rx_config_page_split(efx);
659 if (efx->rx_buffer_order)
660 netif_dbg(efx, drv, efx->net_dev,
661 "RX buf len=%u; page order=%u batch=%u\n",
662 efx->rx_dma_len, efx->rx_buffer_order,
663 efx->rx_pages_per_batch);
664 else
665 netif_dbg(efx, drv, efx->net_dev,
666 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
667 efx->rx_dma_len, efx->rx_page_buf_step,
668 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000669
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000670 /* RX filters also have scatter-enabled flags */
671 if (efx->rx_scatter != old_rx_scatter)
672 efx_filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100673
Ben Hutchings14bf7182012-05-22 01:27:58 +0100674 /* We must keep at least one descriptor in a TX ring empty.
675 * We could avoid this when the queue size does not exactly
676 * match the hardware ring size, but it's not that important.
677 * Therefore we stop the queue when one more skb might fill
678 * the ring completely. We wake it when half way back to
679 * empty.
680 */
681 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
682 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
683
Ben Hutchings8ceee662008-04-27 12:55:59 +0100684 /* Initialise the channels */
685 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100686 efx_for_each_channel_tx_queue(tx_queue, channel)
687 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100688
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000689 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100690 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000691 efx_nic_generate_fill_event(rx_queue);
692 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100693
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000694 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100695 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000696
697 if (netif_device_present(efx->net_dev))
698 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100699}
700
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000701static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100702{
703 struct efx_channel *channel;
704 struct efx_tx_queue *tx_queue;
705 struct efx_rx_queue *rx_queue;
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100706 struct pci_dev *dev = efx->pci_dev;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100707 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100708
709 EFX_ASSERT_RESET_SERIALISED(efx);
710 BUG_ON(efx->port_enabled);
711
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100712 /* Only perform flush if dma is enabled */
Alexandre Rames626950d2013-01-14 17:20:22 +0000713 if (dev->is_busmaster && efx->state != STATE_RECOVERY) {
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100714 rc = efx_nic_flush_queues(efx);
715
716 if (rc && EFX_WORKAROUND_7803(efx)) {
717 /* Schedule a reset to recover from the flush failure. The
718 * descriptor caches reference memory we're about to free,
719 * but falcon_reconfigure_mac_wrapper() won't reconnect
720 * the MACs because of the pending reset. */
721 netif_err(efx, drv, efx->net_dev,
722 "Resetting to recover from flush failure\n");
723 efx_schedule_reset(efx, RESET_TYPE_ALL);
724 } else if (rc) {
725 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
726 } else {
727 netif_dbg(efx, drv, efx->net_dev,
728 "successfully flushed all queues\n");
729 }
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000730 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100731
Ben Hutchings8ceee662008-04-27 12:55:59 +0100732 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000733 /* RX packet processing is pipelined, so wait for the
734 * NAPI handler to complete. At least event queue 0
735 * might be kept active by non-data events, so don't
736 * use napi_synchronize() but actually disable NAPI
737 * temporarily.
738 */
739 if (efx_channel_has_rx_queue(channel)) {
740 efx_stop_eventq(channel);
741 efx_start_eventq(channel);
742 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100743
744 efx_for_each_channel_rx_queue(rx_queue, channel)
745 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000746 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100747 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100748 }
749}
750
751static void efx_remove_channel(struct efx_channel *channel)
752{
753 struct efx_tx_queue *tx_queue;
754 struct efx_rx_queue *rx_queue;
755
Ben Hutchings62776d02010-06-23 11:30:07 +0000756 netif_dbg(channel->efx, drv, channel->efx->net_dev,
757 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100758
759 efx_for_each_channel_rx_queue(rx_queue, channel)
760 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000761 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100762 efx_remove_tx_queue(tx_queue);
763 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100764 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100765}
766
Ben Hutchings46426102010-09-10 06:42:33 +0000767static void efx_remove_channels(struct efx_nic *efx)
768{
769 struct efx_channel *channel;
770
771 efx_for_each_channel(channel, efx)
772 efx_remove_channel(channel);
773}
774
775int
776efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
777{
778 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
779 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000780 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100781 int rc;
782
783 rc = efx_check_disabled(efx);
784 if (rc)
785 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000786
787 /* Not all channels should be reallocated. We must avoid
788 * reallocating their buffer table entries.
789 */
790 efx_for_each_channel(channel, efx) {
791 struct efx_rx_queue *rx_queue;
792 struct efx_tx_queue *tx_queue;
793
794 if (channel->type->copy)
795 continue;
796 next_buffer_table = max(next_buffer_table,
797 channel->eventq.index +
798 channel->eventq.entries);
799 efx_for_each_channel_rx_queue(rx_queue, channel)
800 next_buffer_table = max(next_buffer_table,
801 rx_queue->rxd.index +
802 rx_queue->rxd.entries);
803 efx_for_each_channel_tx_queue(tx_queue, channel)
804 next_buffer_table = max(next_buffer_table,
805 tx_queue->txd.index +
806 tx_queue->txd.entries);
807 }
Ben Hutchings46426102010-09-10 06:42:33 +0000808
Ben Hutchings29c69a42013-01-28 19:01:06 +0000809 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000810 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000811 efx_stop_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000812
Ben Hutchings7f967c02012-02-13 23:45:02 +0000813 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000814 memset(other_channel, 0, sizeof(other_channel));
815 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000816 channel = efx->channel[i];
817 if (channel->type->copy)
818 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000819 if (!channel) {
820 rc = -ENOMEM;
821 goto out;
822 }
823 other_channel[i] = channel;
824 }
825
826 /* Swap entry counts and channel pointers */
827 old_rxq_entries = efx->rxq_entries;
828 old_txq_entries = efx->txq_entries;
829 efx->rxq_entries = rxq_entries;
830 efx->txq_entries = txq_entries;
831 for (i = 0; i < efx->n_channels; i++) {
832 channel = efx->channel[i];
833 efx->channel[i] = other_channel[i];
834 other_channel[i] = channel;
835 }
836
Ben Hutchings7f967c02012-02-13 23:45:02 +0000837 /* Restart buffer table allocation */
838 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000839
Ben Hutchingse8f14992010-12-07 19:47:34 +0000840 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000841 channel = efx->channel[i];
842 if (!channel->type->copy)
843 continue;
844 rc = efx_probe_channel(channel);
845 if (rc)
846 goto rollback;
847 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000848 }
Ben Hutchings46426102010-09-10 06:42:33 +0000849
Ben Hutchings7f967c02012-02-13 23:45:02 +0000850out:
851 /* Destroy unused channel structures */
852 for (i = 0; i < efx->n_channels; i++) {
853 channel = other_channel[i];
854 if (channel && channel->type->copy) {
855 efx_fini_napi_channel(channel);
856 efx_remove_channel(channel);
857 kfree(channel);
858 }
859 }
860
861 efx_start_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000862 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000863 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000864 return rc;
865
866rollback:
867 /* Swap back */
868 efx->rxq_entries = old_rxq_entries;
869 efx->txq_entries = old_txq_entries;
870 for (i = 0; i < efx->n_channels; i++) {
871 channel = efx->channel[i];
872 efx->channel[i] = other_channel[i];
873 other_channel[i] = channel;
874 }
875 goto out;
876}
877
Steve Hodgson90d683a2010-06-01 11:19:39 +0000878void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100879{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000880 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100881}
882
Ben Hutchings7f967c02012-02-13 23:45:02 +0000883static const struct efx_channel_type efx_default_channel_type = {
884 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100885 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000886 .get_name = efx_get_channel_name,
887 .copy = efx_copy_channel,
888 .keep_eventq = false,
889};
890
891int efx_channel_dummy_op_int(struct efx_channel *channel)
892{
893 return 0;
894}
895
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100896void efx_channel_dummy_op_void(struct efx_channel *channel)
897{
898}
899
Ben Hutchings8ceee662008-04-27 12:55:59 +0100900/**************************************************************************
901 *
902 * Port handling
903 *
904 **************************************************************************/
905
906/* This ensures that the kernel is kept informed (via
907 * netif_carrier_on/off) of the link status, and also maintains the
908 * link status's stop on the port's TX queue.
909 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000910void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100911{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000912 struct efx_link_state *link_state = &efx->link_state;
913
Ben Hutchings8ceee662008-04-27 12:55:59 +0100914 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
915 * that no events are triggered between unregister_netdev() and the
916 * driver unloading. A more general condition is that NETDEV_CHANGE
917 * can only be generated between NETDEV_UP and NETDEV_DOWN */
918 if (!netif_running(efx->net_dev))
919 return;
920
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000921 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100922 efx->n_link_state_changes++;
923
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000924 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100925 netif_carrier_on(efx->net_dev);
926 else
927 netif_carrier_off(efx->net_dev);
928 }
929
930 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000931 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000932 netif_info(efx, link, efx->net_dev,
933 "link up at %uMbps %s-duplex (MTU %d)%s\n",
934 link_state->speed, link_state->fd ? "full" : "half",
935 efx->net_dev->mtu,
936 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000937 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000938 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100939}
940
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000941void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
942{
943 efx->link_advertising = advertising;
944 if (advertising) {
945 if (advertising & ADVERTISED_Pause)
946 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
947 else
948 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
949 if (advertising & ADVERTISED_Asym_Pause)
950 efx->wanted_fc ^= EFX_FC_TX;
951 }
952}
953
David S. Millerb56269462011-05-17 17:53:22 -0400954void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000955{
956 efx->wanted_fc = wanted_fc;
957 if (efx->link_advertising) {
958 if (wanted_fc & EFX_FC_RX)
959 efx->link_advertising |= (ADVERTISED_Pause |
960 ADVERTISED_Asym_Pause);
961 else
962 efx->link_advertising &= ~(ADVERTISED_Pause |
963 ADVERTISED_Asym_Pause);
964 if (wanted_fc & EFX_FC_TX)
965 efx->link_advertising ^= ADVERTISED_Asym_Pause;
966 }
967}
968
Ben Hutchings115122a2009-03-04 09:52:52 +0000969static void efx_fini_port(struct efx_nic *efx);
970
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000971/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
972 * the MAC appropriately. All other PHY configuration changes are pushed
973 * through phy_op->set_settings(), and pushed asynchronously to the MAC
974 * through efx_monitor().
975 *
976 * Callers must hold the mac_lock
977 */
978int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100979{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000980 enum efx_phy_mode phy_mode;
981 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100982
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000983 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100984
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000985 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000986 netif_addr_lock_bh(efx->net_dev);
987 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100988
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000989 /* Disable PHY transmit in mac level loopbacks */
990 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800991 if (LOOPBACK_INTERNAL(efx))
992 efx->phy_mode |= PHY_MODE_TX_DISABLED;
993 else
994 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800995
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000996 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800997
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000998 if (rc)
999 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001000
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001001 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001002}
1003
1004/* Reinitialise the MAC to pick up new PHY settings, even if the port is
1005 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001006int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001007{
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001008 int rc;
1009
Ben Hutchings8ceee662008-04-27 12:55:59 +01001010 EFX_ASSERT_RESET_SERIALISED(efx);
1011
1012 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001013 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001014 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001015
1016 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001017}
1018
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001019/* Asynchronous work item for changing MAC promiscuity and multicast
1020 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
1021 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001022static void efx_mac_work(struct work_struct *data)
1023{
1024 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
1025
1026 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +01001027 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +01001028 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001029 mutex_unlock(&efx->mac_lock);
1030}
1031
Ben Hutchings8ceee662008-04-27 12:55:59 +01001032static int efx_probe_port(struct efx_nic *efx)
1033{
1034 int rc;
1035
Ben Hutchings62776d02010-06-23 11:30:07 +00001036 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001037
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001038 if (phy_flash_cfg)
1039 efx->phy_mode = PHY_MODE_SPECIAL;
1040
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001041 /* Connect up MAC/PHY operations table */
1042 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001043 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001044 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001045
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001046 /* Initialise MAC address to permanent address */
1047 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001048
1049 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001050}
1051
1052static int efx_init_port(struct efx_nic *efx)
1053{
1054 int rc;
1055
Ben Hutchings62776d02010-06-23 11:30:07 +00001056 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001057
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001058 mutex_lock(&efx->mac_lock);
1059
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001060 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001061 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001062 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001063
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001064 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001065
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001066 /* Reconfigure the MAC before creating dma queues (required for
1067 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001068 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001069
1070 /* Ensure the PHY advertises the correct flow control settings */
1071 rc = efx->phy_op->reconfigure(efx);
1072 if (rc)
1073 goto fail2;
1074
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001075 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001076 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001077
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001078fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001079 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001080fail1:
1081 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001082 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001083}
1084
Ben Hutchings8ceee662008-04-27 12:55:59 +01001085static void efx_start_port(struct efx_nic *efx)
1086{
Ben Hutchings62776d02010-06-23 11:30:07 +00001087 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001088 BUG_ON(efx->port_enabled);
1089
1090 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001091 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001092
1093 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1094 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001095 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001096
Ben Hutchings8ceee662008-04-27 12:55:59 +01001097 mutex_unlock(&efx->mac_lock);
1098}
1099
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001100/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001101static void efx_stop_port(struct efx_nic *efx)
1102{
Ben Hutchings62776d02010-06-23 11:30:07 +00001103 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001104
1105 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001106 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001107 mutex_unlock(&efx->mac_lock);
1108
1109 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001110 netif_addr_lock_bh(efx->net_dev);
1111 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001112}
1113
1114static void efx_fini_port(struct efx_nic *efx)
1115{
Ben Hutchings62776d02010-06-23 11:30:07 +00001116 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001117
1118 if (!efx->port_initialized)
1119 return;
1120
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001121 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001122 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001123
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001124 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001125 efx_link_status_changed(efx);
1126}
1127
1128static void efx_remove_port(struct efx_nic *efx)
1129{
Ben Hutchings62776d02010-06-23 11:30:07 +00001130 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001131
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001132 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001133}
1134
1135/**************************************************************************
1136 *
1137 * NIC handling
1138 *
1139 **************************************************************************/
1140
1141/* This configures the PCI device to enable I/O and DMA. */
1142static int efx_init_io(struct efx_nic *efx)
1143{
1144 struct pci_dev *pci_dev = efx->pci_dev;
1145 dma_addr_t dma_mask = efx->type->max_dma_mask;
1146 int rc;
1147
Ben Hutchings62776d02010-06-23 11:30:07 +00001148 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001149
1150 rc = pci_enable_device(pci_dev);
1151 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001152 netif_err(efx, probe, efx->net_dev,
1153 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001154 goto fail1;
1155 }
1156
1157 pci_set_master(pci_dev);
1158
1159 /* Set the PCI DMA mask. Try all possibilities from our
1160 * genuine mask down to 32 bits, because some architectures
1161 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1162 * masks event though they reject 46 bit masks.
1163 */
1164 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001165 if (dma_supported(&pci_dev->dev, dma_mask)) {
1166 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001167 if (rc == 0)
1168 break;
1169 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001170 dma_mask >>= 1;
1171 }
1172 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001173 netif_err(efx, probe, efx->net_dev,
1174 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001175 goto fail2;
1176 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001177 netif_dbg(efx, probe, efx->net_dev,
1178 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001179 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001180 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001181 /* dma_set_coherent_mask() is not *allowed* to
1182 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001183 * but just in case...
1184 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001185 netif_err(efx, probe, efx->net_dev,
1186 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001187 goto fail2;
1188 }
1189
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001190 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1191 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001192 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001193 netif_err(efx, probe, efx->net_dev,
1194 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001195 rc = -EIO;
1196 goto fail3;
1197 }
David S. Miller8decf862011-09-22 03:23:13 -04001198 efx->membase = ioremap_nocache(efx->membase_phys,
1199 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001200 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001201 netif_err(efx, probe, efx->net_dev,
1202 "could not map memory BAR at %llx+%x\n",
1203 (unsigned long long)efx->membase_phys,
1204 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001205 rc = -ENOMEM;
1206 goto fail4;
1207 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001208 netif_dbg(efx, probe, efx->net_dev,
1209 "memory BAR at %llx+%x (virtual %p)\n",
1210 (unsigned long long)efx->membase_phys,
1211 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001212
1213 return 0;
1214
1215 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001216 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001217 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001218 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001219 fail2:
1220 pci_disable_device(efx->pci_dev);
1221 fail1:
1222 return rc;
1223}
1224
1225static void efx_fini_io(struct efx_nic *efx)
1226{
Ben Hutchings62776d02010-06-23 11:30:07 +00001227 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001228
1229 if (efx->membase) {
1230 iounmap(efx->membase);
1231 efx->membase = NULL;
1232 }
1233
1234 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001235 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001236 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001237 }
1238
1239 pci_disable_device(efx->pci_dev);
1240}
1241
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001242static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001243{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001244 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001245 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001246 int cpu;
1247
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001248 if (rss_cpus) {
1249 count = rss_cpus;
1250 } else {
1251 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1252 netif_warn(efx, probe, efx->net_dev,
1253 "RSS disabled due to allocation failure\n");
1254 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001255 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001256
1257 count = 0;
1258 for_each_online_cpu(cpu) {
1259 if (!cpumask_test_cpu(cpu, thread_mask)) {
1260 ++count;
1261 cpumask_or(thread_mask, thread_mask,
1262 topology_thread_cpumask(cpu));
1263 }
1264 }
1265
1266 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001267 }
1268
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001269 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1270 * table entries that are inaccessible to VFs
1271 */
1272 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1273 count > efx_vf_size(efx)) {
1274 netif_warn(efx, probe, efx->net_dev,
1275 "Reducing number of RSS channels from %u to %u for "
1276 "VF support. Increase vf-msix-limit to use more "
1277 "channels on the PF.\n",
1278 count, efx_vf_size(efx));
1279 count = efx_vf_size(efx);
1280 }
1281
Ben Hutchings46123d02008-09-01 12:47:33 +01001282 return count;
1283}
1284
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001285static int
1286efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1287{
1288#ifdef CONFIG_RFS_ACCEL
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001289 unsigned int i;
1290 int rc;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001291
1292 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1293 if (!efx->net_dev->rx_cpu_rmap)
1294 return -ENOMEM;
1295 for (i = 0; i < efx->n_rx_channels; i++) {
1296 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1297 xentries[i].vector);
1298 if (rc) {
1299 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1300 efx->net_dev->rx_cpu_rmap = NULL;
1301 return rc;
1302 }
1303 }
1304#endif
1305 return 0;
1306}
1307
Ben Hutchings46123d02008-09-01 12:47:33 +01001308/* Probe the number and type of interrupts we are able to obtain, and
1309 * the resulting numbers of channels and RX queues.
1310 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001311static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001312{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001313 unsigned int max_channels =
1314 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001315 unsigned int extra_channels = 0;
1316 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001317 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001318
Ben Hutchings7f967c02012-02-13 23:45:02 +00001319 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1320 if (efx->extra_channel_type[i])
1321 ++extra_channels;
1322
Ben Hutchings8ceee662008-04-27 12:55:59 +01001323 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001324 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001325 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001326
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001327 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001328 if (separate_tx_channels)
1329 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001330 n_channels += extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001331 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001332
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001333 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001334 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001335 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001336 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001337 netif_err(efx, drv, efx->net_dev,
1338 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001339 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001340 netif_err(efx, drv, efx->net_dev,
1341 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001342 EFX_BUG_ON_PARANOID(rc >= n_channels);
1343 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001344 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001345 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001346 }
1347
1348 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001349 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001350 if (n_channels > extra_channels)
1351 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001352 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001353 efx->n_tx_channels = max(n_channels / 2, 1U);
1354 efx->n_rx_channels = max(n_channels -
1355 efx->n_tx_channels,
1356 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001357 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001358 efx->n_tx_channels = n_channels;
1359 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001360 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001361 rc = efx_init_rx_cpu_rmap(efx, xentries);
1362 if (rc) {
1363 pci_disable_msix(efx->pci_dev);
1364 return rc;
1365 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001366 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001367 efx_get_channel(efx, i)->irq =
1368 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001369 } else {
1370 /* Fall back to single channel MSI */
1371 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001372 netif_err(efx, drv, efx->net_dev,
1373 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001374 }
1375 }
1376
1377 /* Try single interrupt MSI */
1378 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001379 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001380 efx->n_rx_channels = 1;
1381 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001382 rc = pci_enable_msi(efx->pci_dev);
1383 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001384 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001385 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001386 netif_err(efx, drv, efx->net_dev,
1387 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001388 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1389 }
1390 }
1391
1392 /* Assume legacy interrupts */
1393 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001394 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001395 efx->n_rx_channels = 1;
1396 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001397 efx->legacy_irq = efx->pci_dev->irq;
1398 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001399
Ben Hutchings7f967c02012-02-13 23:45:02 +00001400 /* Assign extra channels if possible */
1401 j = efx->n_channels;
1402 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1403 if (!efx->extra_channel_type[i])
1404 continue;
1405 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1406 efx->n_channels <= extra_channels) {
1407 efx->extra_channel_type[i]->handle_no_channel(efx);
1408 } else {
1409 --j;
1410 efx_get_channel(efx, j)->type =
1411 efx->extra_channel_type[i];
1412 }
1413 }
1414
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001415 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001416 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001417 efx->n_rx_channels : efx_vf_size(efx));
1418
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001419 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001420}
1421
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001422/* Enable interrupts, then probe and start the event queues */
Ben Hutchings7f967c02012-02-13 23:45:02 +00001423static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001424{
1425 struct efx_channel *channel;
1426
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001427 BUG_ON(efx->state == STATE_DISABLED);
1428
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001429 if (efx->legacy_irq)
1430 efx->legacy_irq_enabled = true;
1431 efx_nic_enable_interrupts(efx);
1432
1433 efx_for_each_channel(channel, efx) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001434 if (!channel->type->keep_eventq || !may_keep_eventq)
1435 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001436 efx_start_eventq(channel);
1437 }
1438
1439 efx_mcdi_mode_event(efx);
1440}
1441
Ben Hutchings7f967c02012-02-13 23:45:02 +00001442static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001443{
1444 struct efx_channel *channel;
1445
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001446 if (efx->state == STATE_DISABLED)
1447 return;
1448
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001449 efx_mcdi_mode_poll(efx);
1450
1451 efx_nic_disable_interrupts(efx);
1452 if (efx->legacy_irq) {
1453 synchronize_irq(efx->legacy_irq);
1454 efx->legacy_irq_enabled = false;
1455 }
1456
1457 efx_for_each_channel(channel, efx) {
1458 if (channel->irq)
1459 synchronize_irq(channel->irq);
1460
1461 efx_stop_eventq(channel);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001462 if (!channel->type->keep_eventq || !may_keep_eventq)
1463 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001464 }
1465}
1466
Ben Hutchings8ceee662008-04-27 12:55:59 +01001467static void efx_remove_interrupts(struct efx_nic *efx)
1468{
1469 struct efx_channel *channel;
1470
1471 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001472 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001473 channel->irq = 0;
1474 pci_disable_msi(efx->pci_dev);
1475 pci_disable_msix(efx->pci_dev);
1476
1477 /* Remove legacy interrupt */
1478 efx->legacy_irq = 0;
1479}
1480
Ben Hutchings8831da72008-09-01 12:47:48 +01001481static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001482{
Ben Hutchings602a5322011-05-16 17:32:39 +01001483 struct efx_channel *channel;
1484 struct efx_tx_queue *tx_queue;
1485
Ben Hutchings97653432011-01-12 18:26:56 +00001486 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001487 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001488
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001489 /* We need to mark which channels really have RX and TX
1490 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001491 * RX-only and TX-only channels.
1492 */
1493 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001494 if (channel->channel < efx->n_rx_channels)
1495 channel->rx_queue.core_index = channel->channel;
1496 else
1497 channel->rx_queue.core_index = -1;
1498
Ben Hutchings602a5322011-05-16 17:32:39 +01001499 efx_for_each_channel_tx_queue(tx_queue, channel)
1500 tx_queue->queue -= (efx->tx_channel_offset *
1501 EFX_TXQ_TYPES);
1502 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001503}
1504
1505static int efx_probe_nic(struct efx_nic *efx)
1506{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001507 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001508 int rc;
1509
Ben Hutchings62776d02010-06-23 11:30:07 +00001510 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001511
1512 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001513 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001514 if (rc)
1515 return rc;
1516
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001517 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001518 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001519 rc = efx_probe_interrupts(efx);
1520 if (rc)
1521 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001522
Ben Hutchings28e47c42012-02-15 01:58:49 +00001523 efx->type->dimension_resources(efx);
1524
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001525 if (efx->n_channels > 1)
1526 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001527 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001528 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001529 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001530
Ben Hutchings8831da72008-09-01 12:47:48 +01001531 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001532 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1533 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001534
1535 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001536 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1537 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001538
1539 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001540
1541fail:
1542 efx->type->remove(efx);
1543 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001544}
1545
1546static void efx_remove_nic(struct efx_nic *efx)
1547{
Ben Hutchings62776d02010-06-23 11:30:07 +00001548 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001549
1550 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001551 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001552}
1553
1554/**************************************************************************
1555 *
1556 * NIC startup/shutdown
1557 *
1558 *************************************************************************/
1559
1560static int efx_probe_all(struct efx_nic *efx)
1561{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001562 int rc;
1563
Ben Hutchings8ceee662008-04-27 12:55:59 +01001564 rc = efx_probe_nic(efx);
1565 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001566 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001567 goto fail1;
1568 }
1569
Ben Hutchings8ceee662008-04-27 12:55:59 +01001570 rc = efx_probe_port(efx);
1571 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001572 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001573 goto fail2;
1574 }
1575
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001576 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1577 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1578 rc = -EINVAL;
1579 goto fail3;
1580 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001581 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001582
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001583 rc = efx_probe_filters(efx);
1584 if (rc) {
1585 netif_err(efx, probe, efx->net_dev,
1586 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001587 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001588 }
1589
Ben Hutchings7f967c02012-02-13 23:45:02 +00001590 rc = efx_probe_channels(efx);
1591 if (rc)
1592 goto fail4;
1593
Ben Hutchings8ceee662008-04-27 12:55:59 +01001594 return 0;
1595
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001596 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001597 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001598 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001599 efx_remove_port(efx);
1600 fail2:
1601 efx_remove_nic(efx);
1602 fail1:
1603 return rc;
1604}
1605
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001606/* If the interface is supposed to be running but is not, start
1607 * the hardware and software data path, regular activity for the port
1608 * (MAC statistics, link polling, etc.) and schedule the port to be
1609 * reconfigured. Interrupts must already be enabled. This function
1610 * is safe to call multiple times, so long as the NIC is not disabled.
1611 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001612 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001613static void efx_start_all(struct efx_nic *efx)
1614{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001615 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001616 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001617
1618 /* Check that it is appropriate to restart the interface. All
1619 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001620 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001621 return;
1622
Ben Hutchings8ceee662008-04-27 12:55:59 +01001623 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001624 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001625
Alexandre Rames626950d2013-01-14 17:20:22 +00001626 /* Start the hardware monitor if there is one */
1627 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001628 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1629 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001630
1631 /* If link state detection is normally event-driven, we have
1632 * to poll now because we could have missed a change
1633 */
1634 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001635 mutex_lock(&efx->mac_lock);
1636 if (efx->phy_op->poll(efx))
1637 efx_link_status_changed(efx);
1638 mutex_unlock(&efx->mac_lock);
1639 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001640
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001641 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001642}
1643
1644/* Flush all delayed work. Should only be called when no more delayed work
1645 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1646 * since we're holding the rtnl_lock at this point. */
1647static void efx_flush_all(struct efx_nic *efx)
1648{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001649 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001650 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001651 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001652 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001653 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001654}
1655
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001656/* Quiesce the hardware and software data path, and regular activity
1657 * for the port without bringing the link down. Safe to call multiple
1658 * times with the NIC in almost any state, but interrupts should be
1659 * enabled. Requires the RTNL lock.
1660 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001661static void efx_stop_all(struct efx_nic *efx)
1662{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001663 EFX_ASSERT_RESET_SERIALISED(efx);
1664
1665 /* port_enabled can be read safely under the rtnl lock */
1666 if (!efx->port_enabled)
1667 return;
1668
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001669 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001670 efx_stop_port(efx);
1671
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001672 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001673 efx_flush_all(efx);
1674
Ben Hutchings29c69a42013-01-28 19:01:06 +00001675 /* Stop the kernel transmit interface. This is only valid if
1676 * the device is stopped or detached; otherwise the watchdog
1677 * may fire immediately.
1678 */
1679 WARN_ON(netif_running(efx->net_dev) &&
1680 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001681 netif_tx_disable(efx->net_dev);
1682
1683 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001684}
1685
1686static void efx_remove_all(struct efx_nic *efx)
1687{
Ben Hutchings46426102010-09-10 06:42:33 +00001688 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001689 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001690 efx_remove_port(efx);
1691 efx_remove_nic(efx);
1692}
1693
Ben Hutchings8ceee662008-04-27 12:55:59 +01001694/**************************************************************************
1695 *
1696 * Interrupt moderation
1697 *
1698 **************************************************************************/
1699
Ben Hutchingscc180b62011-12-08 19:51:47 +00001700static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001701{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001702 if (usecs == 0)
1703 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001704 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001705 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001706 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001707}
1708
Ben Hutchings8ceee662008-04-27 12:55:59 +01001709/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001710int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1711 unsigned int rx_usecs, bool rx_adaptive,
1712 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001713{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001714 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001715 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1716 efx->timer_quantum_ns,
1717 1000);
1718 unsigned int tx_ticks;
1719 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001720
1721 EFX_ASSERT_RESET_SERIALISED(efx);
1722
Ben Hutchingscc180b62011-12-08 19:51:47 +00001723 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001724 return -EINVAL;
1725
Ben Hutchingscc180b62011-12-08 19:51:47 +00001726 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1727 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1728
Ben Hutchings9e393b32011-09-05 07:43:04 +00001729 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1730 !rx_may_override_tx) {
1731 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1732 "RX and TX IRQ moderation must be equal\n");
1733 return -EINVAL;
1734 }
1735
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001736 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001737 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001738 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001739 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001740 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001741 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001742 channel->irq_moderation = tx_ticks;
1743 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001744
1745 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001746}
1747
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001748void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1749 unsigned int *rx_usecs, bool *rx_adaptive)
1750{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001751 /* We must round up when converting ticks to microseconds
1752 * because we round down when converting the other way.
1753 */
1754
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001755 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001756 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1757 efx->timer_quantum_ns,
1758 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001759
1760 /* If channels are shared between RX and TX, so is IRQ
1761 * moderation. Otherwise, IRQ moderation is the same for all
1762 * TX channels and is not adaptive.
1763 */
1764 if (efx->tx_channel_offset == 0)
1765 *tx_usecs = *rx_usecs;
1766 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001767 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001768 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001769 efx->timer_quantum_ns,
1770 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001771}
1772
Ben Hutchings8ceee662008-04-27 12:55:59 +01001773/**************************************************************************
1774 *
1775 * Hardware monitor
1776 *
1777 **************************************************************************/
1778
Ben Hutchingse254c272010-09-20 08:44:10 +00001779/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001780static void efx_monitor(struct work_struct *data)
1781{
1782 struct efx_nic *efx = container_of(data, struct efx_nic,
1783 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001784
Ben Hutchings62776d02010-06-23 11:30:07 +00001785 netif_vdbg(efx, timer, efx->net_dev,
1786 "hardware monitor executing on CPU %d\n",
1787 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001788 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001789
Ben Hutchings8ceee662008-04-27 12:55:59 +01001790 /* If the mac_lock is already held then it is likely a port
1791 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001792 * most of the work of monitor() anyway. */
1793 if (mutex_trylock(&efx->mac_lock)) {
1794 if (efx->port_enabled)
1795 efx->type->monitor(efx);
1796 mutex_unlock(&efx->mac_lock);
1797 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001798
Ben Hutchings8ceee662008-04-27 12:55:59 +01001799 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1800 efx_monitor_interval);
1801}
1802
1803/**************************************************************************
1804 *
1805 * ioctls
1806 *
1807 *************************************************************************/
1808
1809/* Net device ioctl
1810 * Context: process, rtnl_lock() held.
1811 */
1812static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1813{
Ben Hutchings767e4682008-09-01 12:43:14 +01001814 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001815 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001816
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001817 if (cmd == SIOCSHWTSTAMP)
1818 return efx_ptp_ioctl(efx, ifr, cmd);
1819
Ben Hutchings68e7f452009-04-29 08:05:08 +00001820 /* Convert phy_id from older PRTAD/DEVAD format */
1821 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1822 (data->phy_id & 0xfc00) == 0x0400)
1823 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1824
1825 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001826}
1827
1828/**************************************************************************
1829 *
1830 * NAPI interface
1831 *
1832 **************************************************************************/
1833
Ben Hutchings7f967c02012-02-13 23:45:02 +00001834static void efx_init_napi_channel(struct efx_channel *channel)
1835{
1836 struct efx_nic *efx = channel->efx;
1837
1838 channel->napi_dev = efx->net_dev;
1839 netif_napi_add(channel->napi_dev, &channel->napi_str,
1840 efx_poll, napi_weight);
1841}
1842
Ben Hutchingse8f14992010-12-07 19:47:34 +00001843static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001844{
1845 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001846
Ben Hutchings7f967c02012-02-13 23:45:02 +00001847 efx_for_each_channel(channel, efx)
1848 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001849}
1850
1851static void efx_fini_napi_channel(struct efx_channel *channel)
1852{
1853 if (channel->napi_dev)
1854 netif_napi_del(&channel->napi_str);
1855 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001856}
1857
1858static void efx_fini_napi(struct efx_nic *efx)
1859{
1860 struct efx_channel *channel;
1861
Ben Hutchingse8f14992010-12-07 19:47:34 +00001862 efx_for_each_channel(channel, efx)
1863 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001864}
1865
1866/**************************************************************************
1867 *
1868 * Kernel netpoll interface
1869 *
1870 *************************************************************************/
1871
1872#ifdef CONFIG_NET_POLL_CONTROLLER
1873
1874/* Although in the common case interrupts will be disabled, this is not
1875 * guaranteed. However, all our work happens inside the NAPI callback,
1876 * so no locking is required.
1877 */
1878static void efx_netpoll(struct net_device *net_dev)
1879{
Ben Hutchings767e4682008-09-01 12:43:14 +01001880 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001881 struct efx_channel *channel;
1882
Ben Hutchings64ee3122008-09-01 12:47:38 +01001883 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001884 efx_schedule_channel(channel);
1885}
1886
1887#endif
1888
1889/**************************************************************************
1890 *
1891 * Kernel net device interface
1892 *
1893 *************************************************************************/
1894
1895/* Context: process, rtnl_lock() held. */
1896static int efx_net_open(struct net_device *net_dev)
1897{
Ben Hutchings767e4682008-09-01 12:43:14 +01001898 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001899 int rc;
1900
Ben Hutchings62776d02010-06-23 11:30:07 +00001901 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1902 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001903
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001904 rc = efx_check_disabled(efx);
1905 if (rc)
1906 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001907 if (efx->phy_mode & PHY_MODE_SPECIAL)
1908 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001909 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1910 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001911
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001912 /* Notify the kernel of the link state polled during driver load,
1913 * before the monitor starts running */
1914 efx_link_status_changed(efx);
1915
Ben Hutchings8ceee662008-04-27 12:55:59 +01001916 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001917 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001918 return 0;
1919}
1920
1921/* Context: process, rtnl_lock() held.
1922 * Note that the kernel will ignore our return code; this method
1923 * should really be a void.
1924 */
1925static int efx_net_stop(struct net_device *net_dev)
1926{
Ben Hutchings767e4682008-09-01 12:43:14 +01001927 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001928
Ben Hutchings62776d02010-06-23 11:30:07 +00001929 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1930 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001931
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001932 /* Stop the device and flush all the channels */
1933 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001934
1935 return 0;
1936}
1937
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001938/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001939static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1940 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001941{
Ben Hutchings767e4682008-09-01 12:43:14 +01001942 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001943 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001944
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001945 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001946
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001947 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001948
1949 stats->rx_packets = mac_stats->rx_packets;
1950 stats->tx_packets = mac_stats->tx_packets;
1951 stats->rx_bytes = mac_stats->rx_bytes;
1952 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001953 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001954 stats->multicast = mac_stats->rx_multicast;
1955 stats->collisions = mac_stats->tx_collision;
1956 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1957 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001958 stats->rx_crc_errors = mac_stats->rx_bad;
1959 stats->rx_frame_errors = mac_stats->rx_align_error;
1960 stats->rx_fifo_errors = mac_stats->rx_overflow;
1961 stats->rx_missed_errors = mac_stats->rx_missed;
1962 stats->tx_window_errors = mac_stats->tx_late_collision;
1963
1964 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001965 stats->rx_crc_errors +
1966 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001967 mac_stats->rx_symbol_error);
1968 stats->tx_errors = (stats->tx_window_errors +
1969 mac_stats->tx_bad);
1970
Ben Hutchings1cb34522011-09-02 23:23:00 +01001971 spin_unlock_bh(&efx->stats_lock);
1972
Ben Hutchings8ceee662008-04-27 12:55:59 +01001973 return stats;
1974}
1975
1976/* Context: netif_tx_lock held, BHs disabled. */
1977static void efx_watchdog(struct net_device *net_dev)
1978{
Ben Hutchings767e4682008-09-01 12:43:14 +01001979 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001980
Ben Hutchings62776d02010-06-23 11:30:07 +00001981 netif_err(efx, tx_err, efx->net_dev,
1982 "TX stuck with port_enabled=%d: resetting channels\n",
1983 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001984
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001985 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001986}
1987
1988
1989/* Context: process, rtnl_lock() held. */
1990static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1991{
Ben Hutchings767e4682008-09-01 12:43:14 +01001992 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001993 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001994
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001995 rc = efx_check_disabled(efx);
1996 if (rc)
1997 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001998 if (new_mtu > EFX_MAX_MTU)
1999 return -EINVAL;
2000
Ben Hutchings62776d02010-06-23 11:30:07 +00002001 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002002
Ben Hutchings29c69a42013-01-28 19:01:06 +00002003 efx_device_detach_sync(efx);
2004 efx_stop_all(efx);
2005
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002006 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002007 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01002008 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002009 mutex_unlock(&efx->mac_lock);
2010
Ben Hutchings8ceee662008-04-27 12:55:59 +01002011 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00002012 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002013 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002014}
2015
2016static int efx_set_mac_address(struct net_device *net_dev, void *data)
2017{
Ben Hutchings767e4682008-09-01 12:43:14 +01002018 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002019 struct sockaddr *addr = data;
2020 char *new_addr = addr->sa_data;
2021
Ben Hutchings8ceee662008-04-27 12:55:59 +01002022 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002023 netif_err(efx, drv, efx->net_dev,
2024 "invalid ethernet MAC address requested: %pM\n",
2025 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002026 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002027 }
2028
2029 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002030 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002031
2032 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002033 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002034 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002035 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002036
2037 return 0;
2038}
2039
Ben Hutchingsa816f752008-09-01 12:49:12 +01002040/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002041static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002042{
Ben Hutchings767e4682008-09-01 12:43:14 +01002043 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002044 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002045 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002046 u32 crc;
2047 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002048
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002049 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002050
2051 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002052 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002053 memset(mc_hash, 0xff, sizeof(*mc_hash));
2054 } else {
2055 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00002056 netdev_for_each_mc_addr(ha, net_dev) {
2057 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002058 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
Ben Hutchings32766ec2012-10-04 17:13:03 -07002059 __set_bit_le(bit, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002060 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002061
2062 /* Broadcast packets go through the multicast hash filter.
2063 * ether_crc_le() of the broadcast address is 0xbe2612ff
2064 * so we always add bit 0xff to the mask.
2065 */
Ben Hutchings32766ec2012-10-04 17:13:03 -07002066 __set_bit_le(0xff, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002067 }
2068
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002069 if (efx->port_enabled)
2070 queue_work(efx->workqueue, &efx->mac_work);
2071 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002072}
2073
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002074static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002075{
2076 struct efx_nic *efx = netdev_priv(net_dev);
2077
2078 /* If disabling RX n-tuple filtering, clear existing filters */
2079 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2080 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2081
2082 return 0;
2083}
2084
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002085static const struct net_device_ops efx_netdev_ops = {
2086 .ndo_open = efx_net_open,
2087 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002088 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002089 .ndo_tx_timeout = efx_watchdog,
2090 .ndo_start_xmit = efx_hard_start_xmit,
2091 .ndo_validate_addr = eth_validate_addr,
2092 .ndo_do_ioctl = efx_ioctl,
2093 .ndo_change_mtu = efx_change_mtu,
2094 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002095 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002096 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002097#ifdef CONFIG_SFC_SRIOV
2098 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2099 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2100 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2101 .ndo_get_vf_config = efx_sriov_get_vf_config,
2102#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002103#ifdef CONFIG_NET_POLL_CONTROLLER
2104 .ndo_poll_controller = efx_netpoll,
2105#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002106 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002107#ifdef CONFIG_RFS_ACCEL
2108 .ndo_rx_flow_steer = efx_filter_rfs,
2109#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002110};
2111
Ben Hutchings7dde5962008-12-12 22:09:38 -08002112static void efx_update_name(struct efx_nic *efx)
2113{
2114 strcpy(efx->name, efx->net_dev->name);
2115 efx_mtd_rename(efx);
2116 efx_set_channel_names(efx);
2117}
2118
Ben Hutchings8ceee662008-04-27 12:55:59 +01002119static int efx_netdev_event(struct notifier_block *this,
2120 unsigned long event, void *ptr)
2121{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01002122 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002123
Ben Hutchings7dde5962008-12-12 22:09:38 -08002124 if (net_dev->netdev_ops == &efx_netdev_ops &&
2125 event == NETDEV_CHANGENAME)
2126 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002127
2128 return NOTIFY_DONE;
2129}
2130
2131static struct notifier_block efx_netdev_notifier = {
2132 .notifier_call = efx_netdev_event,
2133};
2134
Ben Hutchings06d5e192008-12-12 21:47:23 -08002135static ssize_t
2136show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2137{
2138 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2139 return sprintf(buf, "%d\n", efx->phy_type);
2140}
2141static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
2142
Ben Hutchings8ceee662008-04-27 12:55:59 +01002143static int efx_register_netdev(struct efx_nic *efx)
2144{
2145 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002146 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002147 int rc;
2148
2149 net_dev->watchdog_timeo = 5 * HZ;
2150 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002151 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002152 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002153 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002154
Ben Hutchings7dde5962008-12-12 22:09:38 -08002155 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002156
Ben Hutchings7153f622012-07-27 20:50:52 +01002157 /* Enable resets to be scheduled and check whether any were
2158 * already requested. If so, the NIC is probably hosed so we
2159 * abort.
2160 */
2161 efx->state = STATE_READY;
2162 smp_mb(); /* ensure we change state before checking reset_pending */
2163 if (efx->reset_pending) {
2164 netif_err(efx, probe, efx->net_dev,
2165 "aborting probe due to scheduled reset\n");
2166 rc = -EIO;
2167 goto fail_locked;
2168 }
2169
Ben Hutchingsaed06282009-08-26 08:16:27 +00002170 rc = dev_alloc_name(net_dev, net_dev->name);
2171 if (rc < 0)
2172 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002173 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002174
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002175 /* Always start with carrier off; PHY events will detect the link */
2176 netif_carrier_off(net_dev);
2177
Ben Hutchingsaed06282009-08-26 08:16:27 +00002178 rc = register_netdevice(net_dev);
2179 if (rc)
2180 goto fail_locked;
2181
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002182 efx_for_each_channel(channel, efx) {
2183 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002184 efx_for_each_channel_tx_queue(tx_queue, channel)
2185 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002186 }
2187
Ben Hutchings7dde5962008-12-12 22:09:38 -08002188 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002189
Ben Hutchings06d5e192008-12-12 21:47:23 -08002190 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2191 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002192 netif_err(efx, drv, efx->net_dev,
2193 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002194 goto fail_registered;
2195 }
2196
Ben Hutchings8ceee662008-04-27 12:55:59 +01002197 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002198
Ben Hutchings7153f622012-07-27 20:50:52 +01002199fail_registered:
2200 rtnl_lock();
2201 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002202fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002203 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002204 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002205 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002206 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002207}
2208
2209static void efx_unregister_netdev(struct efx_nic *efx)
2210{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002211 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002212 struct efx_tx_queue *tx_queue;
2213
2214 if (!efx->net_dev)
2215 return;
2216
Ben Hutchings767e4682008-09-01 12:43:14 +01002217 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002218
2219 /* Free up any skbs still remaining. This has to happen before
2220 * we try to unregister the netdev as running their destructors
2221 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002222 efx_for_each_channel(channel, efx) {
2223 efx_for_each_channel_tx_queue(tx_queue, channel)
2224 efx_release_tx_buffers(tx_queue);
2225 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002226
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002227 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2228 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002229
2230 rtnl_lock();
2231 unregister_netdevice(efx->net_dev);
2232 efx->state = STATE_UNINIT;
2233 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002234}
2235
2236/**************************************************************************
2237 *
2238 * Device reset and suspend
2239 *
2240 **************************************************************************/
2241
Ben Hutchings2467ca42008-09-01 12:48:50 +01002242/* Tears down the entire software state and most of the hardware state
2243 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002244void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002245{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002246 EFX_ASSERT_RESET_SERIALISED(efx);
2247
Ben Hutchings2467ca42008-09-01 12:48:50 +01002248 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00002249 efx_stop_interrupts(efx, false);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002250
2251 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002252 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2253 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002254 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002255}
2256
Ben Hutchings2467ca42008-09-01 12:48:50 +01002257/* This function will always ensure that the locks acquired in
2258 * efx_reset_down() are released. A failure return code indicates
2259 * that we were unable to reinitialise the hardware, and the
2260 * driver should be disabled. If ok is false, then the rx and tx
2261 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002262int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002263{
2264 int rc;
2265
Ben Hutchings2467ca42008-09-01 12:48:50 +01002266 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002267
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002268 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002269 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002270 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002271 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002272 }
2273
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002274 if (!ok)
2275 goto fail;
2276
Steve Hodgson4b988282009-01-29 17:50:51 +00002277 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002278 rc = efx->phy_op->init(efx);
2279 if (rc)
2280 goto fail;
2281 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002282 netif_err(efx, drv, efx->net_dev,
2283 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002284 }
2285
Ben Hutchings710b2082011-09-03 00:15:00 +01002286 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002287
Ben Hutchings7f967c02012-02-13 23:45:02 +00002288 efx_start_interrupts(efx, false);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002289 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002290 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002291
2292 mutex_unlock(&efx->mac_lock);
2293
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002294 efx_start_all(efx);
2295
2296 return 0;
2297
2298fail:
2299 efx->port_initialized = false;
2300
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002301 mutex_unlock(&efx->mac_lock);
2302
Ben Hutchings8ceee662008-04-27 12:55:59 +01002303 return rc;
2304}
2305
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002306/* Reset the NIC using the specified method. Note that the reset may
2307 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002308 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002309 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002310 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002311int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002312{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002313 int rc, rc2;
2314 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002315
Ben Hutchings62776d02010-06-23 11:30:07 +00002316 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2317 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002318
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002319 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002320 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002321
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002322 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002323 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002324 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002325 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002326 }
2327
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002328 /* Clear flags for the scopes we covered. We assume the NIC and
2329 * driver are now quiescent so that there is no race here.
2330 */
2331 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002332
2333 /* Reinitialise bus-mastering, which may have been turned off before
2334 * the reset was scheduled. This is still appropriate, even in the
2335 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2336 * can respond to requests. */
2337 pci_set_master(efx->pci_dev);
2338
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002339out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002340 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002341 disabled = rc ||
2342 method == RESET_TYPE_DISABLE ||
2343 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002344 rc2 = efx_reset_up(efx, method, !disabled);
2345 if (rc2) {
2346 disabled = true;
2347 if (!rc)
2348 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002349 }
2350
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002351 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002352 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002353 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002354 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002355 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002356 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002357 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002358 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002359 return rc;
2360}
2361
Alexandre Rames626950d2013-01-14 17:20:22 +00002362/* Try recovery mechanisms.
2363 * For now only EEH is supported.
2364 * Returns 0 if the recovery mechanisms are unsuccessful.
2365 * Returns a non-zero value otherwise.
2366 */
2367static int efx_try_recovery(struct efx_nic *efx)
2368{
2369#ifdef CONFIG_EEH
2370 /* A PCI error can occur and not be seen by EEH because nothing
2371 * happens on the PCI bus. In this case the driver may fail and
2372 * schedule a 'recover or reset', leading to this recovery handler.
2373 * Manually call the eeh failure check function.
2374 */
2375 struct eeh_dev *eehdev =
2376 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2377
2378 if (eeh_dev_check_failure(eehdev)) {
2379 /* The EEH mechanisms will handle the error and reset the
2380 * device if necessary.
2381 */
2382 return 1;
2383 }
2384#endif
2385 return 0;
2386}
2387
Ben Hutchings8ceee662008-04-27 12:55:59 +01002388/* The worker thread exists so that code that cannot sleep can
2389 * schedule a reset for later.
2390 */
2391static void efx_reset_work(struct work_struct *data)
2392{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002393 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002394 unsigned long pending;
2395 enum reset_type method;
2396
2397 pending = ACCESS_ONCE(efx->reset_pending);
2398 method = fls(pending) - 1;
2399
2400 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2401 method == RESET_TYPE_RECOVER_OR_ALL) &&
2402 efx_try_recovery(efx))
2403 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002404
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002405 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002406 return;
2407
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002408 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002409
2410 /* We checked the state in efx_schedule_reset() but it may
2411 * have changed by now. Now that we have the RTNL lock,
2412 * it cannot change again.
2413 */
2414 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002415 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002416
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002417 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002418}
2419
2420void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2421{
2422 enum reset_type method;
2423
Alexandre Rames626950d2013-01-14 17:20:22 +00002424 if (efx->state == STATE_RECOVERY) {
2425 netif_dbg(efx, drv, efx->net_dev,
2426 "recovering: skip scheduling %s reset\n",
2427 RESET_TYPE(type));
2428 return;
2429 }
2430
Ben Hutchings8ceee662008-04-27 12:55:59 +01002431 switch (type) {
2432 case RESET_TYPE_INVISIBLE:
2433 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002434 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002435 case RESET_TYPE_WORLD:
2436 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002437 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002438 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002439 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2440 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002441 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002442 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002443 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002444 netif_dbg(efx, drv, efx->net_dev,
2445 "scheduling %s reset for %s\n",
2446 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002447 break;
2448 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002449
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002450 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002451 smp_mb(); /* ensure we change reset_pending before checking state */
2452
2453 /* If we're not READY then just leave the flags set as the cue
2454 * to abort probing or reschedule the reset later.
2455 */
2456 if (ACCESS_ONCE(efx->state) != STATE_READY)
2457 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002458
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002459 /* efx_process_channel() will no longer read events once a
2460 * reset is scheduled. So switch back to poll'd MCDI completions. */
2461 efx_mcdi_mode_poll(efx);
2462
Steve Hodgson1ab00622008-12-12 21:33:02 -08002463 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002464}
2465
2466/**************************************************************************
2467 *
2468 * List of NICs we support
2469 *
2470 **************************************************************************/
2471
2472/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002473static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002474 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2475 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002476 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002477 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2478 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002479 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002480 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002481 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002482 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002483 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002484 {0} /* end of list */
2485};
2486
2487/**************************************************************************
2488 *
Ben Hutchings37594332009-11-23 16:05:45 +00002489 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002490 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002491 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002492 * Needed so all function pointers are valid and do not have to be tested
2493 * before use
2494 *
2495 **************************************************************************/
2496int efx_port_dummy_op_int(struct efx_nic *efx)
2497{
2498 return 0;
2499}
2500void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002501
2502static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002503{
2504 return false;
2505}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002506
stephen hemminger6c8c2512011-04-14 05:50:12 +00002507static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002508 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002509 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002510 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002511 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002512};
2513
Ben Hutchings8ceee662008-04-27 12:55:59 +01002514/**************************************************************************
2515 *
2516 * Data housekeeping
2517 *
2518 **************************************************************************/
2519
2520/* This zeroes out and then fills in the invariants in a struct
2521 * efx_nic (including all sub-structures).
2522 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002523static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002524 struct pci_dev *pci_dev, struct net_device *net_dev)
2525{
Ben Hutchings46426102010-09-10 06:42:33 +00002526 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002527
2528 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002529 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002530#ifdef CONFIG_SFC_MTD
2531 INIT_LIST_HEAD(&efx->mtd_list);
2532#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002533 INIT_WORK(&efx->reset_work, efx_reset_work);
2534 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002535 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002536 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002537 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002538 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002539 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002540
2541 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002542 spin_lock_init(&efx->stats_lock);
2543 mutex_init(&efx->mac_lock);
2544 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002545 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002546 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002547 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002548
2549 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002550 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2551 if (!efx->channel[i])
2552 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002553 }
2554
Ben Hutchings8ceee662008-04-27 12:55:59 +01002555 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2556
2557 /* Higher numbered interrupt modes are less capable! */
2558 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2559 interrupt_mode);
2560
Ben Hutchings6977dc62008-12-26 13:44:39 -08002561 /* Would be good to use the net_dev name, but we're too early */
2562 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2563 pci_name(pci_dev));
2564 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002565 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002566 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002567
Ben Hutchings8ceee662008-04-27 12:55:59 +01002568 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002569
2570fail:
2571 efx_fini_struct(efx);
2572 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002573}
2574
2575static void efx_fini_struct(struct efx_nic *efx)
2576{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002577 int i;
2578
2579 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2580 kfree(efx->channel[i]);
2581
Ben Hutchings8ceee662008-04-27 12:55:59 +01002582 if (efx->workqueue) {
2583 destroy_workqueue(efx->workqueue);
2584 efx->workqueue = NULL;
2585 }
2586}
2587
2588/**************************************************************************
2589 *
2590 * PCI interface
2591 *
2592 **************************************************************************/
2593
2594/* Main body of final NIC shutdown code
2595 * This is called only at module unload (or hotplug removal).
2596 */
2597static void efx_pci_remove_main(struct efx_nic *efx)
2598{
Ben Hutchings7153f622012-07-27 20:50:52 +01002599 /* Flush reset_work. It can no longer be scheduled since we
2600 * are not READY.
2601 */
2602 BUG_ON(efx->state == STATE_READY);
2603 cancel_work_sync(&efx->reset_work);
2604
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002605#ifdef CONFIG_RFS_ACCEL
2606 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2607 efx->net_dev->rx_cpu_rmap = NULL;
2608#endif
Ben Hutchings7f967c02012-02-13 23:45:02 +00002609 efx_stop_interrupts(efx, false);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002610 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002611 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002612 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002613 efx_fini_napi(efx);
2614 efx_remove_all(efx);
2615}
2616
2617/* Final NIC shutdown
2618 * This is called only at module unload (or hotplug removal).
2619 */
2620static void efx_pci_remove(struct pci_dev *pci_dev)
2621{
2622 struct efx_nic *efx;
2623
2624 efx = pci_get_drvdata(pci_dev);
2625 if (!efx)
2626 return;
2627
2628 /* Mark the NIC as fini, then stop the interface */
2629 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002630 dev_close(efx->net_dev);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002631 efx_stop_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002632 rtnl_unlock();
2633
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002634 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002635 efx_unregister_netdev(efx);
2636
Ben Hutchings7dde5962008-12-12 22:09:38 -08002637 efx_mtd_remove(efx);
2638
Ben Hutchings8ceee662008-04-27 12:55:59 +01002639 efx_pci_remove_main(efx);
2640
Ben Hutchings8ceee662008-04-27 12:55:59 +01002641 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002642 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002643
Ben Hutchings8ceee662008-04-27 12:55:59 +01002644 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002645 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002646 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002647
2648 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002649};
2650
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002651/* NIC VPD information
2652 * Called during probe to display the part number of the
2653 * installed NIC. VPD is potentially very large but this should
2654 * always appear within the first 512 bytes.
2655 */
2656#define SFC_VPD_LEN 512
2657static void efx_print_product_vpd(struct efx_nic *efx)
2658{
2659 struct pci_dev *dev = efx->pci_dev;
2660 char vpd_data[SFC_VPD_LEN];
2661 ssize_t vpd_size;
2662 int i, j;
2663
2664 /* Get the vpd data from the device */
2665 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2666 if (vpd_size <= 0) {
2667 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2668 return;
2669 }
2670
2671 /* Get the Read only section */
2672 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2673 if (i < 0) {
2674 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2675 return;
2676 }
2677
2678 j = pci_vpd_lrdt_size(&vpd_data[i]);
2679 i += PCI_VPD_LRDT_TAG_SIZE;
2680 if (i + j > vpd_size)
2681 j = vpd_size - i;
2682
2683 /* Get the Part number */
2684 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2685 if (i < 0) {
2686 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2687 return;
2688 }
2689
2690 j = pci_vpd_info_field_size(&vpd_data[i]);
2691 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2692 if (i + j > vpd_size) {
2693 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2694 return;
2695 }
2696
2697 netif_info(efx, drv, efx->net_dev,
2698 "Part Number : %.*s\n", j, &vpd_data[i]);
2699}
2700
2701
Ben Hutchings8ceee662008-04-27 12:55:59 +01002702/* Main body of NIC initialisation
2703 * This is called at module load (or hotplug insertion, theoretically).
2704 */
2705static int efx_pci_probe_main(struct efx_nic *efx)
2706{
2707 int rc;
2708
2709 /* Do start-of-day initialisation */
2710 rc = efx_probe_all(efx);
2711 if (rc)
2712 goto fail1;
2713
Ben Hutchingse8f14992010-12-07 19:47:34 +00002714 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002715
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002716 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002717 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002718 netif_err(efx, probe, efx->net_dev,
2719 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002720 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002721 }
2722
2723 rc = efx_init_port(efx);
2724 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002725 netif_err(efx, probe, efx->net_dev,
2726 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002727 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002728 }
2729
Ben Hutchings152b6a62009-11-29 03:43:56 +00002730 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002731 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002732 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00002733 efx_start_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002734
2735 return 0;
2736
Ben Hutchings278c0622009-11-23 16:05:12 +00002737 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002738 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002739 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002740 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002741 fail3:
2742 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002743 efx_remove_all(efx);
2744 fail1:
2745 return rc;
2746}
2747
2748/* NIC initialisation
2749 *
2750 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002751 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002752 * sets up and registers the network devices with the kernel and hooks
2753 * the interrupt service routine. It does not prepare the device for
2754 * transmission; this is left to the first time one of the network
2755 * interfaces is brought up (i.e. efx_net_open).
2756 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002757static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002758 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002759{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002760 struct net_device *net_dev;
2761 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002762 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002763
2764 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002765 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2766 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002767 if (!net_dev)
2768 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002769 efx = netdev_priv(net_dev);
2770 efx->type = (const struct efx_nic_type *) entry->driver_data;
2771 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002772 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002773 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002774 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002775 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002776 /* Mask for features that also apply to VLAN devices */
2777 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002778 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2779 NETIF_F_RXCSUM);
2780 /* All offloads can be toggled */
2781 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002782 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002783 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002784 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002785 if (rc)
2786 goto fail1;
2787
Ben Hutchings62776d02010-06-23 11:30:07 +00002788 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002789 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002790
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002791 efx_print_product_vpd(efx);
2792
Ben Hutchings8ceee662008-04-27 12:55:59 +01002793 /* Set up basic I/O (BAR mappings etc) */
2794 rc = efx_init_io(efx);
2795 if (rc)
2796 goto fail2;
2797
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002798 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002799 if (rc)
2800 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002801
Ben Hutchings8ceee662008-04-27 12:55:59 +01002802 rc = efx_register_netdev(efx);
2803 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002804 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002805
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002806 rc = efx_sriov_init(efx);
2807 if (rc)
2808 netif_err(efx, probe, efx->net_dev,
2809 "SR-IOV can't be enabled rc %d\n", rc);
2810
Ben Hutchings62776d02010-06-23 11:30:07 +00002811 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002812
Ben Hutchings7c431612012-01-27 17:23:58 +00002813 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002814 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002815 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002816 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002817 if (rc)
2818 netif_warn(efx, probe, efx->net_dev,
2819 "failed to create MTDs (%d)\n", rc);
2820
Alexandre Rames626950d2013-01-14 17:20:22 +00002821 rc = pci_enable_pcie_error_reporting(pci_dev);
2822 if (rc && rc != -EINVAL)
2823 netif_warn(efx, probe, efx->net_dev,
2824 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2825
Ben Hutchings8ceee662008-04-27 12:55:59 +01002826 return 0;
2827
Ben Hutchings8ceee662008-04-27 12:55:59 +01002828 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002829 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002830 fail3:
2831 efx_fini_io(efx);
2832 fail2:
2833 efx_fini_struct(efx);
2834 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002835 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002836 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002837 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002838 free_netdev(net_dev);
2839 return rc;
2840}
2841
Ben Hutchings89c758f2009-11-29 03:43:07 +00002842static int efx_pm_freeze(struct device *dev)
2843{
2844 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2845
Ben Hutchings61da0262012-07-27 19:35:39 +01002846 rtnl_lock();
2847
Ben Hutchings6032fb52012-07-27 19:35:47 +01002848 if (efx->state != STATE_DISABLED) {
2849 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002850
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002851 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002852
Ben Hutchings6032fb52012-07-27 19:35:47 +01002853 efx_stop_all(efx);
2854 efx_stop_interrupts(efx, false);
2855 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002856
Ben Hutchings61da0262012-07-27 19:35:39 +01002857 rtnl_unlock();
2858
Ben Hutchings89c758f2009-11-29 03:43:07 +00002859 return 0;
2860}
2861
2862static int efx_pm_thaw(struct device *dev)
2863{
2864 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2865
Ben Hutchings61da0262012-07-27 19:35:39 +01002866 rtnl_lock();
2867
Ben Hutchings6032fb52012-07-27 19:35:47 +01002868 if (efx->state != STATE_DISABLED) {
2869 efx_start_interrupts(efx, false);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002870
Ben Hutchings6032fb52012-07-27 19:35:47 +01002871 mutex_lock(&efx->mac_lock);
2872 efx->phy_op->reconfigure(efx);
2873 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002874
Ben Hutchings6032fb52012-07-27 19:35:47 +01002875 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002876
Ben Hutchings6032fb52012-07-27 19:35:47 +01002877 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002878
Ben Hutchings6032fb52012-07-27 19:35:47 +01002879 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002880
Ben Hutchings6032fb52012-07-27 19:35:47 +01002881 efx->type->resume_wol(efx);
2882 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002883
Ben Hutchings61da0262012-07-27 19:35:39 +01002884 rtnl_unlock();
2885
Steve Hodgson319ba642010-06-01 11:17:24 +00002886 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2887 queue_work(reset_workqueue, &efx->reset_work);
2888
Ben Hutchings89c758f2009-11-29 03:43:07 +00002889 return 0;
2890}
2891
2892static int efx_pm_poweroff(struct device *dev)
2893{
2894 struct pci_dev *pci_dev = to_pci_dev(dev);
2895 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2896
2897 efx->type->fini(efx);
2898
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002899 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002900
2901 pci_save_state(pci_dev);
2902 return pci_set_power_state(pci_dev, PCI_D3hot);
2903}
2904
2905/* Used for both resume and restore */
2906static int efx_pm_resume(struct device *dev)
2907{
2908 struct pci_dev *pci_dev = to_pci_dev(dev);
2909 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2910 int rc;
2911
2912 rc = pci_set_power_state(pci_dev, PCI_D0);
2913 if (rc)
2914 return rc;
2915 pci_restore_state(pci_dev);
2916 rc = pci_enable_device(pci_dev);
2917 if (rc)
2918 return rc;
2919 pci_set_master(efx->pci_dev);
2920 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2921 if (rc)
2922 return rc;
2923 rc = efx->type->init(efx);
2924 if (rc)
2925 return rc;
2926 efx_pm_thaw(dev);
2927 return 0;
2928}
2929
2930static int efx_pm_suspend(struct device *dev)
2931{
2932 int rc;
2933
2934 efx_pm_freeze(dev);
2935 rc = efx_pm_poweroff(dev);
2936 if (rc)
2937 efx_pm_resume(dev);
2938 return rc;
2939}
2940
Ben Hutchings18e83e42012-01-05 19:05:20 +00002941static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002942 .suspend = efx_pm_suspend,
2943 .resume = efx_pm_resume,
2944 .freeze = efx_pm_freeze,
2945 .thaw = efx_pm_thaw,
2946 .poweroff = efx_pm_poweroff,
2947 .restore = efx_pm_resume,
2948};
2949
Alexandre Rames626950d2013-01-14 17:20:22 +00002950/* A PCI error affecting this device was detected.
2951 * At this point MMIO and DMA may be disabled.
2952 * Stop the software path and request a slot reset.
2953 */
2954pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2955 enum pci_channel_state state)
2956{
2957 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2958 struct efx_nic *efx = pci_get_drvdata(pdev);
2959
2960 if (state == pci_channel_io_perm_failure)
2961 return PCI_ERS_RESULT_DISCONNECT;
2962
2963 rtnl_lock();
2964
2965 if (efx->state != STATE_DISABLED) {
2966 efx->state = STATE_RECOVERY;
2967 efx->reset_pending = 0;
2968
2969 efx_device_detach_sync(efx);
2970
2971 efx_stop_all(efx);
2972 efx_stop_interrupts(efx, false);
2973
2974 status = PCI_ERS_RESULT_NEED_RESET;
2975 } else {
2976 /* If the interface is disabled we don't want to do anything
2977 * with it.
2978 */
2979 status = PCI_ERS_RESULT_RECOVERED;
2980 }
2981
2982 rtnl_unlock();
2983
2984 pci_disable_device(pdev);
2985
2986 return status;
2987}
2988
2989/* Fake a successfull reset, which will be performed later in efx_io_resume. */
2990pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
2991{
2992 struct efx_nic *efx = pci_get_drvdata(pdev);
2993 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2994 int rc;
2995
2996 if (pci_enable_device(pdev)) {
2997 netif_err(efx, hw, efx->net_dev,
2998 "Cannot re-enable PCI device after reset.\n");
2999 status = PCI_ERS_RESULT_DISCONNECT;
3000 }
3001
3002 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
3003 if (rc) {
3004 netif_err(efx, hw, efx->net_dev,
3005 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3006 /* Non-fatal error. Continue. */
3007 }
3008
3009 return status;
3010}
3011
3012/* Perform the actual reset and resume I/O operations. */
3013static void efx_io_resume(struct pci_dev *pdev)
3014{
3015 struct efx_nic *efx = pci_get_drvdata(pdev);
3016 int rc;
3017
3018 rtnl_lock();
3019
3020 if (efx->state == STATE_DISABLED)
3021 goto out;
3022
3023 rc = efx_reset(efx, RESET_TYPE_ALL);
3024 if (rc) {
3025 netif_err(efx, hw, efx->net_dev,
3026 "efx_reset failed after PCI error (%d)\n", rc);
3027 } else {
3028 efx->state = STATE_READY;
3029 netif_dbg(efx, hw, efx->net_dev,
3030 "Done resetting and resuming IO after PCI error.\n");
3031 }
3032
3033out:
3034 rtnl_unlock();
3035}
3036
3037/* For simplicity and reliability, we always require a slot reset and try to
3038 * reset the hardware when a pci error affecting the device is detected.
3039 * We leave both the link_reset and mmio_enabled callback unimplemented:
3040 * with our request for slot reset the mmio_enabled callback will never be
3041 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3042 */
3043static struct pci_error_handlers efx_err_handlers = {
3044 .error_detected = efx_io_error_detected,
3045 .slot_reset = efx_io_slot_reset,
3046 .resume = efx_io_resume,
3047};
3048
Ben Hutchings8ceee662008-04-27 12:55:59 +01003049static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003050 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003051 .id_table = efx_pci_table,
3052 .probe = efx_pci_probe,
3053 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003054 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003055 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003056};
3057
3058/**************************************************************************
3059 *
3060 * Kernel module interface
3061 *
3062 *************************************************************************/
3063
3064module_param(interrupt_mode, uint, 0444);
3065MODULE_PARM_DESC(interrupt_mode,
3066 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3067
3068static int __init efx_init_module(void)
3069{
3070 int rc;
3071
3072 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3073
3074 rc = register_netdevice_notifier(&efx_netdev_notifier);
3075 if (rc)
3076 goto err_notifier;
3077
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003078 rc = efx_init_sriov();
3079 if (rc)
3080 goto err_sriov;
3081
Steve Hodgson1ab00622008-12-12 21:33:02 -08003082 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3083 if (!reset_workqueue) {
3084 rc = -ENOMEM;
3085 goto err_reset;
3086 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003087
3088 rc = pci_register_driver(&efx_pci_driver);
3089 if (rc < 0)
3090 goto err_pci;
3091
3092 return 0;
3093
3094 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003095 destroy_workqueue(reset_workqueue);
3096 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003097 efx_fini_sriov();
3098 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003099 unregister_netdevice_notifier(&efx_netdev_notifier);
3100 err_notifier:
3101 return rc;
3102}
3103
3104static void __exit efx_exit_module(void)
3105{
3106 printk(KERN_INFO "Solarflare NET driver unloading\n");
3107
3108 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003109 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003110 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003111 unregister_netdevice_notifier(&efx_netdev_notifier);
3112
3113}
3114
3115module_init(efx_init_module);
3116module_exit(efx_exit_module);
3117
Ben Hutchings906bb262009-11-29 15:16:19 +00003118MODULE_AUTHOR("Solarflare Communications and "
3119 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003120MODULE_DESCRIPTION("Solarflare Communications network driver");
3121MODULE_LICENSE("GPL");
3122MODULE_DEVICE_TABLE(pci, efx_pci_table);