blob: 3a103f8972e7ed54555f434b45dd3c09fc45ba28 [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>
Ben Hutchings64d8ad62011-01-05 00:50:41 +000024#include <linux/cpu_rmap.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010025#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000027#include "nic.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010028
Ben Hutchings8880f4e2009-11-29 15:15:41 +000029#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000030#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000031
Ben Hutchingsc4593022009-11-23 16:08:17 +000032/**************************************************************************
33 *
34 * Type name strings
35 *
36 **************************************************************************
37 */
38
39/* Loopback mode names (see LOOPBACK_MODE()) */
40const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000041const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000042 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000043 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000044 [LOOPBACK_GMAC] = "GMAC",
45 [LOOPBACK_XGMII] = "XGMII",
46 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000047 [LOOPBACK_XAUI] = "XAUI",
48 [LOOPBACK_GMII] = "GMII",
49 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000050 [LOOPBACK_XGBR] = "XGBR",
51 [LOOPBACK_XFI] = "XFI",
52 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
53 [LOOPBACK_GMII_FAR] = "GMII_FAR",
54 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
55 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000056 [LOOPBACK_GPHY] = "GPHY",
57 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000058 [LOOPBACK_PCS] = "PCS",
59 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000060 [LOOPBACK_XPORT] = "XPORT",
61 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000062 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000063 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
64 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000065 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000066 [LOOPBACK_XFI_WS] = "XFI_WS",
67 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000068 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000069};
70
Ben Hutchingsc4593022009-11-23 16:08:17 +000071const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000072const char *const efx_reset_type_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000073 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
74 [RESET_TYPE_ALL] = "ALL",
75 [RESET_TYPE_WORLD] = "WORLD",
76 [RESET_TYPE_DISABLE] = "DISABLE",
77 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
78 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
79 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
80 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
81 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
82 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
Ben Hutchings8880f4e2009-11-29 15:15:41 +000083 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Ben Hutchingsc4593022009-11-23 16:08:17 +000084};
85
Ben Hutchings8ceee662008-04-27 12:55:59 +010086#define EFX_MAX_MTU (9 * 1024)
87
Steve Hodgson1ab00622008-12-12 21:33:02 -080088/* Reset workqueue. If any NIC has a hardware failure then a reset will be
89 * queued onto this work queue. This is not a per-nic work queue, because
90 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
91 */
92static struct workqueue_struct *reset_workqueue;
93
Ben Hutchings8ceee662008-04-27 12:55:59 +010094/**************************************************************************
95 *
96 * Configurable values
97 *
98 *************************************************************************/
99
100/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100101 * Use separate channels for TX and RX events
102 *
Neil Turton28b581a2008-12-12 21:41:06 -0800103 * Set this to 1 to use separate channels for TX and RX. It allows us
104 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100105 *
Neil Turton28b581a2008-12-12 21:41:06 -0800106 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100107 */
Neil Turton28b581a2008-12-12 21:41:06 -0800108static unsigned int separate_tx_channels;
Ben Hutchings8313aca2010-09-10 06:41:57 +0000109module_param(separate_tx_channels, uint, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800110MODULE_PARM_DESC(separate_tx_channels,
111 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100112
113/* This is the weight assigned to each of the (per-channel) virtual
114 * NAPI devices.
115 */
116static int napi_weight = 64;
117
118/* This is the time (in jiffies) between invocations of the hardware
Ben Hutchingse254c272010-09-20 08:44:10 +0000119 * monitor. On Falcon-based NICs, this will:
120 * - Check the on-board hardware monitor;
121 * - Poll the link state and reconfigure the hardware as necessary.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100122 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000123static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100124
Ben Hutchings8ceee662008-04-27 12:55:59 +0100125/* Initial interrupt moderation settings. They can be modified after
126 * module load with ethtool.
127 *
128 * The default for RX should strike a balance between increasing the
129 * round-trip latency and reducing overhead.
130 */
131static unsigned int rx_irq_mod_usec = 60;
132
133/* Initial interrupt moderation settings. They can be modified after
134 * module load with ethtool.
135 *
136 * This default is chosen to ensure that a 10G link does not go idle
137 * while a TX queue is stopped after it has become full. A queue is
138 * restarted when it drops below half full. The time this takes (assuming
139 * worst case 3 descriptors per packet and 1024 descriptors) is
140 * 512 / 3 * 1.2 = 205 usec.
141 */
142static unsigned int tx_irq_mod_usec = 150;
143
144/* This is the first interrupt mode to try out of:
145 * 0 => MSI-X
146 * 1 => MSI
147 * 2 => legacy
148 */
149static unsigned int interrupt_mode;
150
151/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
152 * i.e. the number of CPUs among which we may distribute simultaneous
153 * interrupt handling.
154 *
155 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000156 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100157 */
158static unsigned int rss_cpus;
159module_param(rss_cpus, uint, 0444);
160MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
161
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800162static int phy_flash_cfg;
163module_param(phy_flash_cfg, int, 0644);
164MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
165
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000166static unsigned irq_adapt_low_thresh = 10000;
167module_param(irq_adapt_low_thresh, uint, 0644);
168MODULE_PARM_DESC(irq_adapt_low_thresh,
169 "Threshold score for reducing IRQ moderation");
170
171static unsigned irq_adapt_high_thresh = 20000;
172module_param(irq_adapt_high_thresh, uint, 0644);
173MODULE_PARM_DESC(irq_adapt_high_thresh,
174 "Threshold score for increasing IRQ moderation");
175
Ben Hutchings62776d02010-06-23 11:30:07 +0000176static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
177 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
178 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
179 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
180module_param(debug, uint, 0);
181MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
182
Ben Hutchings8ceee662008-04-27 12:55:59 +0100183/**************************************************************************
184 *
185 * Utility functions and prototypes
186 *
187 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000188
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000189static void efx_start_interrupts(struct efx_nic *efx);
190static void efx_stop_interrupts(struct efx_nic *efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000191static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100192static void efx_remove_port(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000193static void efx_init_napi(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100194static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000195static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000196static void efx_fini_struct(struct efx_nic *efx);
197static void efx_start_all(struct efx_nic *efx);
198static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100199
200#define EFX_ASSERT_RESET_SERIALISED(efx) \
201 do { \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000202 if ((efx->state == STATE_RUNNING) || \
203 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100204 ASSERT_RTNL(); \
205 } while (0)
206
207/**************************************************************************
208 *
209 * Event queue processing
210 *
211 *************************************************************************/
212
213/* Process channel's event queue
214 *
215 * This function is responsible for processing the event queue of a
216 * single channel. The caller must guarantee that this function will
217 * never be concurrently called more than once on the same channel,
218 * though different channels may be being processed concurrently.
219 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000220static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100221{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000222 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100223
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000224 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100225 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100226
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000227 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000228 if (spent && efx_channel_has_rx_queue(channel)) {
229 struct efx_rx_queue *rx_queue =
230 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100231
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000232 /* Deliver last RX packet. */
233 if (channel->rx_pkt) {
234 __efx_rx_packet(channel, channel->rx_pkt);
235 channel->rx_pkt = NULL;
236 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000237 if (rx_queue->enabled) {
238 efx_rx_strategy(channel);
239 efx_fast_push_rx_descriptors(rx_queue);
240 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100241 }
242
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000243 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100244}
245
246/* Mark channel as finished processing
247 *
248 * Note that since we will not receive further interrupts for this
249 * channel before we finish processing and call the eventq_read_ack()
250 * method, there is no need to use the interrupt hold-off timers.
251 */
252static inline void efx_channel_processed(struct efx_channel *channel)
253{
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100254 /* The interrupt handler for this channel may set work_pending
255 * as soon as we acknowledge the events we've seen. Make sure
256 * it's cleared before then. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100257 channel->work_pending = false;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100258 smp_wmb();
259
Ben Hutchings152b6a62009-11-29 03:43:56 +0000260 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100261}
262
263/* NAPI poll handler
264 *
265 * NAPI guarantees serialisation of polls of the same device, which
266 * provides the guarantee required by efx_process_channel().
267 */
268static int efx_poll(struct napi_struct *napi, int budget)
269{
270 struct efx_channel *channel =
271 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000272 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000273 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100274
Ben Hutchings62776d02010-06-23 11:30:07 +0000275 netif_vdbg(efx, intr, efx->net_dev,
276 "channel %d NAPI poll executing on CPU %d\n",
277 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100278
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000279 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100280
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000281 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000282 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000283 efx->irq_rx_adaptive &&
284 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000285 if (unlikely(channel->irq_mod_score <
286 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000287 if (channel->irq_moderation > 1) {
288 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000289 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000290 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000291 } else if (unlikely(channel->irq_mod_score >
292 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000293 if (channel->irq_moderation <
294 efx->irq_rx_moderation) {
295 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000296 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000297 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000298 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000299 channel->irq_count = 0;
300 channel->irq_mod_score = 0;
301 }
302
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000303 efx_filter_rfs_expire(channel);
304
Ben Hutchings8ceee662008-04-27 12:55:59 +0100305 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800306 * only wait for napi_complete(), this isn't a problem
Ben Hutchings8ceee662008-04-27 12:55:59 +0100307 * since efx_channel_processed() will have no effect if
308 * interrupts have already been disabled.
309 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800310 napi_complete(napi);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100311 efx_channel_processed(channel);
312 }
313
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000314 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100315}
316
317/* Process the eventq of the specified channel immediately on this CPU
318 *
319 * Disable hardware generated interrupts, wait for any existing
320 * processing to finish, then directly poll (and ack ) the eventq.
321 * Finally reenable NAPI and interrupts.
322 *
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100323 * This is for use only during a loopback self-test. It must not
324 * deliver any packets up the stack as this can result in deadlock.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100325 */
326void efx_process_channel_now(struct efx_channel *channel)
327{
328 struct efx_nic *efx = channel->efx;
329
Ben Hutchings8313aca2010-09-10 06:41:57 +0000330 BUG_ON(channel->channel >= efx->n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100331 BUG_ON(!channel->enabled);
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100332 BUG_ON(!efx->loopback_selftest);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100333
334 /* Disable interrupts and wait for ISRs to complete */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000335 efx_nic_disable_interrupts(efx);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000336 if (efx->legacy_irq) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100337 synchronize_irq(efx->legacy_irq);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000338 efx->legacy_irq_enabled = false;
339 }
Ben Hutchings64ee3122008-09-01 12:47:38 +0100340 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100341 synchronize_irq(channel->irq);
342
343 /* Wait for any NAPI processing to complete */
344 napi_disable(&channel->napi_str);
345
346 /* Poll the channel */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000347 efx_process_channel(channel, channel->eventq_mask + 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100348
349 /* Ack the eventq. This may cause an interrupt to be generated
350 * when they are reenabled */
351 efx_channel_processed(channel);
352
353 napi_enable(&channel->napi_str);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000354 if (efx->legacy_irq)
355 efx->legacy_irq_enabled = true;
Ben Hutchings152b6a62009-11-29 03:43:56 +0000356 efx_nic_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100357}
358
359/* Create event queue
360 * Event queue memory allocations are done only once. If the channel
361 * is reset, the memory buffer will be reused; this guards against
362 * errors during channel reset and also simplifies interrupt handling.
363 */
364static int efx_probe_eventq(struct efx_channel *channel)
365{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000366 struct efx_nic *efx = channel->efx;
367 unsigned long entries;
368
Ben Hutchings86ee5302012-01-09 19:51:22 +0000369 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000370 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100371
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000372 /* Build an event queue with room for one event per tx and rx buffer,
373 * plus some extra for link state events and MCDI completions. */
374 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
375 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
376 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
377
Ben Hutchings152b6a62009-11-29 03:43:56 +0000378 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100379}
380
381/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100382static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100383{
Ben Hutchings62776d02010-06-23 11:30:07 +0000384 netif_dbg(channel->efx, drv, channel->efx->net_dev,
385 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100386
387 channel->eventq_read_ptr = 0;
388
Ben Hutchings152b6a62009-11-29 03:43:56 +0000389 efx_nic_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100390}
391
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000392/* Enable event queue processing and NAPI */
393static void efx_start_eventq(struct efx_channel *channel)
394{
395 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
396 "chan %d start event queue\n", channel->channel);
397
398 /* The interrupt handler for this channel may set work_pending
399 * as soon as we enable it. Make sure it's cleared before
400 * then. Similarly, make sure it sees the enabled flag set.
401 */
402 channel->work_pending = false;
403 channel->enabled = true;
404 smp_wmb();
405
406 napi_enable(&channel->napi_str);
407 efx_nic_eventq_read_ack(channel);
408}
409
410/* Disable event queue processing and NAPI */
411static void efx_stop_eventq(struct efx_channel *channel)
412{
413 if (!channel->enabled)
414 return;
415
416 napi_disable(&channel->napi_str);
417 channel->enabled = false;
418}
419
Ben Hutchings8ceee662008-04-27 12:55:59 +0100420static void efx_fini_eventq(struct efx_channel *channel)
421{
Ben Hutchings62776d02010-06-23 11:30:07 +0000422 netif_dbg(channel->efx, drv, channel->efx->net_dev,
423 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100424
Ben Hutchings152b6a62009-11-29 03:43:56 +0000425 efx_nic_fini_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100426}
427
428static void efx_remove_eventq(struct efx_channel *channel)
429{
Ben Hutchings62776d02010-06-23 11:30:07 +0000430 netif_dbg(channel->efx, drv, channel->efx->net_dev,
431 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100432
Ben Hutchings152b6a62009-11-29 03:43:56 +0000433 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100434}
435
436/**************************************************************************
437 *
438 * Channel handling
439 *
440 *************************************************************************/
441
Ben Hutchings46426102010-09-10 06:42:33 +0000442/* Allocate and initialise a channel structure, optionally copying
443 * parameters (but not resources) from an old channel structure. */
444static struct efx_channel *
445efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
446{
447 struct efx_channel *channel;
448 struct efx_rx_queue *rx_queue;
449 struct efx_tx_queue *tx_queue;
450 int j;
451
452 if (old_channel) {
453 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
454 if (!channel)
455 return NULL;
456
457 *channel = *old_channel;
458
Ben Hutchingse8f14992010-12-07 19:47:34 +0000459 channel->napi_dev = NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000460 memset(&channel->eventq, 0, sizeof(channel->eventq));
461
462 rx_queue = &channel->rx_queue;
463 rx_queue->buffer = NULL;
464 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
465
466 for (j = 0; j < EFX_TXQ_TYPES; j++) {
467 tx_queue = &channel->tx_queue[j];
468 if (tx_queue->channel)
469 tx_queue->channel = channel;
470 tx_queue->buffer = NULL;
471 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
472 }
473 } else {
474 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
475 if (!channel)
476 return NULL;
477
478 channel->efx = efx;
479 channel->channel = i;
480
481 for (j = 0; j < EFX_TXQ_TYPES; j++) {
482 tx_queue = &channel->tx_queue[j];
483 tx_queue->efx = efx;
484 tx_queue->queue = i * EFX_TXQ_TYPES + j;
485 tx_queue->channel = channel;
486 }
487 }
488
Ben Hutchings46426102010-09-10 06:42:33 +0000489 rx_queue = &channel->rx_queue;
490 rx_queue->efx = efx;
491 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
492 (unsigned long)rx_queue);
493
494 return channel;
495}
496
Ben Hutchings8ceee662008-04-27 12:55:59 +0100497static int efx_probe_channel(struct efx_channel *channel)
498{
499 struct efx_tx_queue *tx_queue;
500 struct efx_rx_queue *rx_queue;
501 int rc;
502
Ben Hutchings62776d02010-06-23 11:30:07 +0000503 netif_dbg(channel->efx, probe, channel->efx->net_dev,
504 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100505
506 rc = efx_probe_eventq(channel);
507 if (rc)
508 goto fail1;
509
510 efx_for_each_channel_tx_queue(tx_queue, channel) {
511 rc = efx_probe_tx_queue(tx_queue);
512 if (rc)
513 goto fail2;
514 }
515
516 efx_for_each_channel_rx_queue(rx_queue, channel) {
517 rc = efx_probe_rx_queue(rx_queue);
518 if (rc)
519 goto fail3;
520 }
521
522 channel->n_rx_frm_trunc = 0;
523
524 return 0;
525
526 fail3:
527 efx_for_each_channel_rx_queue(rx_queue, channel)
528 efx_remove_rx_queue(rx_queue);
529 fail2:
530 efx_for_each_channel_tx_queue(tx_queue, channel)
531 efx_remove_tx_queue(tx_queue);
532 fail1:
533 return rc;
534}
535
536
Ben Hutchings56536e92008-12-12 21:37:02 -0800537static void efx_set_channel_names(struct efx_nic *efx)
538{
539 struct efx_channel *channel;
540 const char *type = "";
541 int number;
542
543 efx_for_each_channel(channel, efx) {
544 number = channel->channel;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +0000545 if (efx->n_channels > efx->n_rx_channels) {
546 if (channel->channel < efx->n_rx_channels) {
Ben Hutchings56536e92008-12-12 21:37:02 -0800547 type = "-rx";
548 } else {
549 type = "-tx";
Ben Hutchingsa4900ac2010-04-28 09:30:43 +0000550 number -= efx->n_rx_channels;
Ben Hutchings56536e92008-12-12 21:37:02 -0800551 }
552 }
Ben Hutchings46426102010-09-10 06:42:33 +0000553 snprintf(efx->channel_name[channel->channel],
554 sizeof(efx->channel_name[0]),
Ben Hutchings56536e92008-12-12 21:37:02 -0800555 "%s%s-%d", efx->name, type, number);
556 }
557}
558
Ben Hutchings46426102010-09-10 06:42:33 +0000559static int efx_probe_channels(struct efx_nic *efx)
560{
561 struct efx_channel *channel;
562 int rc;
563
564 /* Restart special buffer allocation */
565 efx->next_buffer_table = 0;
566
567 efx_for_each_channel(channel, efx) {
568 rc = efx_probe_channel(channel);
569 if (rc) {
570 netif_err(efx, probe, efx->net_dev,
571 "failed to create channel %d\n",
572 channel->channel);
573 goto fail;
574 }
575 }
576 efx_set_channel_names(efx);
577
578 return 0;
579
580fail:
581 efx_remove_channels(efx);
582 return rc;
583}
584
Ben Hutchings8ceee662008-04-27 12:55:59 +0100585/* Channels are shutdown and reinitialised whilst the NIC is running
586 * to propagate configuration changes (mtu, checksum offload), or
587 * to clear hardware error conditions
588 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000589static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100590{
591 struct efx_tx_queue *tx_queue;
592 struct efx_rx_queue *rx_queue;
593 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100594
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100595 /* Calculate the rx buffer allocation parameters required to
596 * support the current MTU, including padding for header
597 * alignment and overruns.
598 */
599 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
600 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
Ben Hutchings39c9cf02010-06-23 11:31:28 +0000601 efx->type->rx_buffer_hash_size +
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100602 efx->type->rx_buffer_padding);
Steve Hodgson62b330b2010-06-01 11:20:53 +0000603 efx->rx_buffer_order = get_order(efx->rx_buffer_len +
604 sizeof(struct efx_rx_page_state));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100605
606 /* Initialise the channels */
607 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100608 efx_for_each_channel_tx_queue(tx_queue, channel)
609 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100610
611 /* The rx buffer allocation strategy is MTU dependent */
612 efx_rx_strategy(channel);
613
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000614 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100615 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000616 efx_nic_generate_fill_event(rx_queue);
617 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100618
619 WARN_ON(channel->rx_pkt != NULL);
620 efx_rx_strategy(channel);
621 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000622
623 if (netif_device_present(efx->net_dev))
624 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100625}
626
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000627static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100628{
629 struct efx_channel *channel;
630 struct efx_tx_queue *tx_queue;
631 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100632 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100633
634 EFX_ASSERT_RESET_SERIALISED(efx);
635 BUG_ON(efx->port_enabled);
636
Ben Hutchings152b6a62009-11-29 03:43:56 +0000637 rc = efx_nic_flush_queues(efx);
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000638 if (rc && EFX_WORKAROUND_7803(efx)) {
639 /* Schedule a reset to recover from the flush failure. The
640 * descriptor caches reference memory we're about to free,
641 * but falcon_reconfigure_mac_wrapper() won't reconnect
642 * the MACs because of the pending reset. */
Ben Hutchings62776d02010-06-23 11:30:07 +0000643 netif_err(efx, drv, efx->net_dev,
644 "Resetting to recover from flush failure\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000645 efx_schedule_reset(efx, RESET_TYPE_ALL);
646 } else if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000647 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000648 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +0000649 netif_dbg(efx, drv, efx->net_dev,
650 "successfully flushed all queues\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000651 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100652
Ben Hutchings8ceee662008-04-27 12:55:59 +0100653 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000654 /* RX packet processing is pipelined, so wait for the
655 * NAPI handler to complete. At least event queue 0
656 * might be kept active by non-data events, so don't
657 * use napi_synchronize() but actually disable NAPI
658 * temporarily.
659 */
660 if (efx_channel_has_rx_queue(channel)) {
661 efx_stop_eventq(channel);
662 efx_start_eventq(channel);
663 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100664
665 efx_for_each_channel_rx_queue(rx_queue, channel)
666 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000667 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100668 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100669 }
670}
671
672static void efx_remove_channel(struct efx_channel *channel)
673{
674 struct efx_tx_queue *tx_queue;
675 struct efx_rx_queue *rx_queue;
676
Ben Hutchings62776d02010-06-23 11:30:07 +0000677 netif_dbg(channel->efx, drv, channel->efx->net_dev,
678 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100679
680 efx_for_each_channel_rx_queue(rx_queue, channel)
681 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000682 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100683 efx_remove_tx_queue(tx_queue);
684 efx_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100685}
686
Ben Hutchings46426102010-09-10 06:42:33 +0000687static void efx_remove_channels(struct efx_nic *efx)
688{
689 struct efx_channel *channel;
690
691 efx_for_each_channel(channel, efx)
692 efx_remove_channel(channel);
693}
694
695int
696efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
697{
698 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
699 u32 old_rxq_entries, old_txq_entries;
700 unsigned i;
701 int rc;
702
703 efx_stop_all(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000704 efx_stop_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000705
706 /* Clone channels */
707 memset(other_channel, 0, sizeof(other_channel));
708 for (i = 0; i < efx->n_channels; i++) {
709 channel = efx_alloc_channel(efx, i, efx->channel[i]);
710 if (!channel) {
711 rc = -ENOMEM;
712 goto out;
713 }
714 other_channel[i] = channel;
715 }
716
717 /* Swap entry counts and channel pointers */
718 old_rxq_entries = efx->rxq_entries;
719 old_txq_entries = efx->txq_entries;
720 efx->rxq_entries = rxq_entries;
721 efx->txq_entries = txq_entries;
722 for (i = 0; i < efx->n_channels; i++) {
723 channel = efx->channel[i];
724 efx->channel[i] = other_channel[i];
725 other_channel[i] = channel;
726 }
727
728 rc = efx_probe_channels(efx);
729 if (rc)
730 goto rollback;
731
Ben Hutchingse8f14992010-12-07 19:47:34 +0000732 efx_init_napi(efx);
733
Ben Hutchings46426102010-09-10 06:42:33 +0000734 /* Destroy old channels */
Ben Hutchingse8f14992010-12-07 19:47:34 +0000735 for (i = 0; i < efx->n_channels; i++) {
736 efx_fini_napi_channel(other_channel[i]);
Ben Hutchings46426102010-09-10 06:42:33 +0000737 efx_remove_channel(other_channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000738 }
Ben Hutchings46426102010-09-10 06:42:33 +0000739out:
740 /* Free unused channel structures */
741 for (i = 0; i < efx->n_channels; i++)
742 kfree(other_channel[i]);
743
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000744 efx_start_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000745 efx_start_all(efx);
746 return rc;
747
748rollback:
749 /* Swap back */
750 efx->rxq_entries = old_rxq_entries;
751 efx->txq_entries = old_txq_entries;
752 for (i = 0; i < efx->n_channels; i++) {
753 channel = efx->channel[i];
754 efx->channel[i] = other_channel[i];
755 other_channel[i] = channel;
756 }
757 goto out;
758}
759
Steve Hodgson90d683a2010-06-01 11:19:39 +0000760void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100761{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000762 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100763}
764
765/**************************************************************************
766 *
767 * Port handling
768 *
769 **************************************************************************/
770
771/* This ensures that the kernel is kept informed (via
772 * netif_carrier_on/off) of the link status, and also maintains the
773 * link status's stop on the port's TX queue.
774 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000775void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100776{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000777 struct efx_link_state *link_state = &efx->link_state;
778
Ben Hutchings8ceee662008-04-27 12:55:59 +0100779 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
780 * that no events are triggered between unregister_netdev() and the
781 * driver unloading. A more general condition is that NETDEV_CHANGE
782 * can only be generated between NETDEV_UP and NETDEV_DOWN */
783 if (!netif_running(efx->net_dev))
784 return;
785
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000786 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100787 efx->n_link_state_changes++;
788
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000789 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100790 netif_carrier_on(efx->net_dev);
791 else
792 netif_carrier_off(efx->net_dev);
793 }
794
795 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000796 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000797 netif_info(efx, link, efx->net_dev,
798 "link up at %uMbps %s-duplex (MTU %d)%s\n",
799 link_state->speed, link_state->fd ? "full" : "half",
800 efx->net_dev->mtu,
801 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000802 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000803 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100804}
805
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000806void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
807{
808 efx->link_advertising = advertising;
809 if (advertising) {
810 if (advertising & ADVERTISED_Pause)
811 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
812 else
813 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
814 if (advertising & ADVERTISED_Asym_Pause)
815 efx->wanted_fc ^= EFX_FC_TX;
816 }
817}
818
David S. Millerb56269462011-05-17 17:53:22 -0400819void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000820{
821 efx->wanted_fc = wanted_fc;
822 if (efx->link_advertising) {
823 if (wanted_fc & EFX_FC_RX)
824 efx->link_advertising |= (ADVERTISED_Pause |
825 ADVERTISED_Asym_Pause);
826 else
827 efx->link_advertising &= ~(ADVERTISED_Pause |
828 ADVERTISED_Asym_Pause);
829 if (wanted_fc & EFX_FC_TX)
830 efx->link_advertising ^= ADVERTISED_Asym_Pause;
831 }
832}
833
Ben Hutchings115122a2009-03-04 09:52:52 +0000834static void efx_fini_port(struct efx_nic *efx);
835
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000836/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
837 * the MAC appropriately. All other PHY configuration changes are pushed
838 * through phy_op->set_settings(), and pushed asynchronously to the MAC
839 * through efx_monitor().
840 *
841 * Callers must hold the mac_lock
842 */
843int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100844{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000845 enum efx_phy_mode phy_mode;
846 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100847
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000848 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100849
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000850 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000851 netif_addr_lock_bh(efx->net_dev);
852 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100853
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000854 /* Disable PHY transmit in mac level loopbacks */
855 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800856 if (LOOPBACK_INTERNAL(efx))
857 efx->phy_mode |= PHY_MODE_TX_DISABLED;
858 else
859 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800860
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000861 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800862
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000863 if (rc)
864 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100865
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000866 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100867}
868
869/* Reinitialise the MAC to pick up new PHY settings, even if the port is
870 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000871int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100872{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000873 int rc;
874
Ben Hutchings8ceee662008-04-27 12:55:59 +0100875 EFX_ASSERT_RESET_SERIALISED(efx);
876
877 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000878 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100879 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000880
881 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100882}
883
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000884/* Asynchronous work item for changing MAC promiscuity and multicast
885 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
886 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800887static void efx_mac_work(struct work_struct *data)
888{
889 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
890
891 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100892 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100893 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800894 mutex_unlock(&efx->mac_lock);
895}
896
Ben Hutchings8ceee662008-04-27 12:55:59 +0100897static int efx_probe_port(struct efx_nic *efx)
898{
899 int rc;
900
Ben Hutchings62776d02010-06-23 11:30:07 +0000901 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100902
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000903 if (phy_flash_cfg)
904 efx->phy_mode = PHY_MODE_SPECIAL;
905
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000906 /* Connect up MAC/PHY operations table */
907 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100908 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000909 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100910
Ben Hutchingse332bcb2011-12-20 01:22:51 +0000911 /* Initialise MAC address to permanent address */
912 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100913
914 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100915}
916
917static int efx_init_port(struct efx_nic *efx)
918{
919 int rc;
920
Ben Hutchings62776d02010-06-23 11:30:07 +0000921 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100922
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000923 mutex_lock(&efx->mac_lock);
924
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800925 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100926 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000927 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100928
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100929 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000930
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000931 /* Reconfigure the MAC before creating dma queues (required for
932 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +0100933 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000934
935 /* Ensure the PHY advertises the correct flow control settings */
936 rc = efx->phy_op->reconfigure(efx);
937 if (rc)
938 goto fail2;
939
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000940 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100941 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800942
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000943fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800944 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000945fail1:
946 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800947 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100948}
949
Ben Hutchings8ceee662008-04-27 12:55:59 +0100950static void efx_start_port(struct efx_nic *efx)
951{
Ben Hutchings62776d02010-06-23 11:30:07 +0000952 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100953 BUG_ON(efx->port_enabled);
954
955 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100956 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000957
958 /* efx_mac_work() might have been scheduled after efx_stop_port(),
959 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +0100960 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000961
Ben Hutchings8ceee662008-04-27 12:55:59 +0100962 mutex_unlock(&efx->mac_lock);
963}
964
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000965/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100966static void efx_stop_port(struct efx_nic *efx)
967{
Ben Hutchings62776d02010-06-23 11:30:07 +0000968 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100969
970 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100971 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100972 mutex_unlock(&efx->mac_lock);
973
974 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000975 netif_addr_lock_bh(efx->net_dev);
976 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100977}
978
979static void efx_fini_port(struct efx_nic *efx)
980{
Ben Hutchings62776d02010-06-23 11:30:07 +0000981 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100982
983 if (!efx->port_initialized)
984 return;
985
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800986 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100987 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100988
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000989 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100990 efx_link_status_changed(efx);
991}
992
993static void efx_remove_port(struct efx_nic *efx)
994{
Ben Hutchings62776d02010-06-23 11:30:07 +0000995 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100996
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000997 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100998}
999
1000/**************************************************************************
1001 *
1002 * NIC handling
1003 *
1004 **************************************************************************/
1005
1006/* This configures the PCI device to enable I/O and DMA. */
1007static int efx_init_io(struct efx_nic *efx)
1008{
1009 struct pci_dev *pci_dev = efx->pci_dev;
1010 dma_addr_t dma_mask = efx->type->max_dma_mask;
1011 int rc;
1012
Ben Hutchings62776d02010-06-23 11:30:07 +00001013 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001014
1015 rc = pci_enable_device(pci_dev);
1016 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001017 netif_err(efx, probe, efx->net_dev,
1018 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001019 goto fail1;
1020 }
1021
1022 pci_set_master(pci_dev);
1023
1024 /* Set the PCI DMA mask. Try all possibilities from our
1025 * genuine mask down to 32 bits, because some architectures
1026 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1027 * masks event though they reject 46 bit masks.
1028 */
1029 while (dma_mask > 0x7fffffffUL) {
Ben Hutchingse9e01842012-01-05 18:50:29 +00001030 if (pci_dma_supported(pci_dev, dma_mask)) {
1031 rc = pci_set_dma_mask(pci_dev, dma_mask);
1032 if (rc == 0)
1033 break;
1034 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001035 dma_mask >>= 1;
1036 }
1037 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001038 netif_err(efx, probe, efx->net_dev,
1039 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001040 goto fail2;
1041 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001042 netif_dbg(efx, probe, efx->net_dev,
1043 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001044 rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
1045 if (rc) {
1046 /* pci_set_consistent_dma_mask() is not *allowed* to
1047 * fail with a mask that pci_set_dma_mask() accepted,
1048 * but just in case...
1049 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001050 netif_err(efx, probe, efx->net_dev,
1051 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001052 goto fail2;
1053 }
1054
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001055 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1056 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001057 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001058 netif_err(efx, probe, efx->net_dev,
1059 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001060 rc = -EIO;
1061 goto fail3;
1062 }
David S. Miller8decf862011-09-22 03:23:13 -04001063 efx->membase = ioremap_nocache(efx->membase_phys,
1064 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001065 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001066 netif_err(efx, probe, efx->net_dev,
1067 "could not map memory BAR at %llx+%x\n",
1068 (unsigned long long)efx->membase_phys,
1069 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001070 rc = -ENOMEM;
1071 goto fail4;
1072 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001073 netif_dbg(efx, probe, efx->net_dev,
1074 "memory BAR at %llx+%x (virtual %p)\n",
1075 (unsigned long long)efx->membase_phys,
1076 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001077
1078 return 0;
1079
1080 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001081 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001082 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001083 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001084 fail2:
1085 pci_disable_device(efx->pci_dev);
1086 fail1:
1087 return rc;
1088}
1089
1090static void efx_fini_io(struct efx_nic *efx)
1091{
Ben Hutchings62776d02010-06-23 11:30:07 +00001092 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001093
1094 if (efx->membase) {
1095 iounmap(efx->membase);
1096 efx->membase = NULL;
1097 }
1098
1099 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001100 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001101 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001102 }
1103
1104 pci_disable_device(efx->pci_dev);
1105}
1106
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001107static unsigned int efx_wanted_parallelism(void)
Ben Hutchings46123d02008-09-01 12:47:33 +01001108{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001109 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001110 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001111 int cpu;
1112
Ben Hutchings5b874e22011-01-12 19:11:05 +00001113 if (rss_cpus)
1114 return rss_cpus;
1115
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001116 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
Rusty Russell2f8975f2009-01-10 21:58:09 -08001117 printk(KERN_WARNING
Mike Travis3977d032009-05-12 10:48:36 +00001118 "sfc: RSS disabled due to allocation failure\n");
Rusty Russell2f8975f2009-01-10 21:58:09 -08001119 return 1;
1120 }
1121
Ben Hutchings46123d02008-09-01 12:47:33 +01001122 count = 0;
1123 for_each_online_cpu(cpu) {
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001124 if (!cpumask_test_cpu(cpu, thread_mask)) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001125 ++count;
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001126 cpumask_or(thread_mask, thread_mask,
1127 topology_thread_cpumask(cpu));
Ben Hutchings46123d02008-09-01 12:47:33 +01001128 }
1129 }
1130
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001131 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001132 return count;
1133}
1134
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001135static int
1136efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1137{
1138#ifdef CONFIG_RFS_ACCEL
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001139 unsigned int i;
1140 int rc;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001141
1142 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1143 if (!efx->net_dev->rx_cpu_rmap)
1144 return -ENOMEM;
1145 for (i = 0; i < efx->n_rx_channels; i++) {
1146 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1147 xentries[i].vector);
1148 if (rc) {
1149 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1150 efx->net_dev->rx_cpu_rmap = NULL;
1151 return rc;
1152 }
1153 }
1154#endif
1155 return 0;
1156}
1157
Ben Hutchings46123d02008-09-01 12:47:33 +01001158/* Probe the number and type of interrupts we are able to obtain, and
1159 * the resulting numbers of channels and RX queues.
1160 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001161static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001162{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001163 unsigned int max_channels =
1164 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
1165 unsigned int i;
1166 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001167
1168 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001169 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001170 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001171
Ben Hutchingsfa142b92011-12-20 01:15:30 +00001172 n_channels = efx_wanted_parallelism();
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001173 if (separate_tx_channels)
1174 n_channels *= 2;
1175 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001176
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001177 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001178 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001179 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001180 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001181 netif_err(efx, drv, efx->net_dev,
1182 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001183 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001184 netif_err(efx, drv, efx->net_dev,
1185 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001186 EFX_BUG_ON_PARANOID(rc >= n_channels);
1187 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001188 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001189 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001190 }
1191
1192 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001193 efx->n_channels = n_channels;
1194 if (separate_tx_channels) {
1195 efx->n_tx_channels =
1196 max(efx->n_channels / 2, 1U);
1197 efx->n_rx_channels =
1198 max(efx->n_channels -
1199 efx->n_tx_channels, 1U);
1200 } else {
1201 efx->n_tx_channels = efx->n_channels;
1202 efx->n_rx_channels = efx->n_channels;
1203 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001204 rc = efx_init_rx_cpu_rmap(efx, xentries);
1205 if (rc) {
1206 pci_disable_msix(efx->pci_dev);
1207 return rc;
1208 }
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001209 for (i = 0; i < n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001210 efx_get_channel(efx, i)->irq =
1211 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001212 } else {
1213 /* Fall back to single channel MSI */
1214 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001215 netif_err(efx, drv, efx->net_dev,
1216 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001217 }
1218 }
1219
1220 /* Try single interrupt MSI */
1221 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001222 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001223 efx->n_rx_channels = 1;
1224 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001225 rc = pci_enable_msi(efx->pci_dev);
1226 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001227 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001228 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001229 netif_err(efx, drv, efx->net_dev,
1230 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001231 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1232 }
1233 }
1234
1235 /* Assume legacy interrupts */
1236 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001237 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001238 efx->n_rx_channels = 1;
1239 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001240 efx->legacy_irq = efx->pci_dev->irq;
1241 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001242
1243 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001244}
1245
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001246/* Enable interrupts, then probe and start the event queues */
1247static void efx_start_interrupts(struct efx_nic *efx)
1248{
1249 struct efx_channel *channel;
1250
1251 if (efx->legacy_irq)
1252 efx->legacy_irq_enabled = true;
1253 efx_nic_enable_interrupts(efx);
1254
1255 efx_for_each_channel(channel, efx) {
1256 efx_init_eventq(channel);
1257 efx_start_eventq(channel);
1258 }
1259
1260 efx_mcdi_mode_event(efx);
1261}
1262
1263static void efx_stop_interrupts(struct efx_nic *efx)
1264{
1265 struct efx_channel *channel;
1266
1267 efx_mcdi_mode_poll(efx);
1268
1269 efx_nic_disable_interrupts(efx);
1270 if (efx->legacy_irq) {
1271 synchronize_irq(efx->legacy_irq);
1272 efx->legacy_irq_enabled = false;
1273 }
1274
1275 efx_for_each_channel(channel, efx) {
1276 if (channel->irq)
1277 synchronize_irq(channel->irq);
1278
1279 efx_stop_eventq(channel);
1280 efx_fini_eventq(channel);
1281 }
1282}
1283
Ben Hutchings8ceee662008-04-27 12:55:59 +01001284static void efx_remove_interrupts(struct efx_nic *efx)
1285{
1286 struct efx_channel *channel;
1287
1288 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001289 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001290 channel->irq = 0;
1291 pci_disable_msi(efx->pci_dev);
1292 pci_disable_msix(efx->pci_dev);
1293
1294 /* Remove legacy interrupt */
1295 efx->legacy_irq = 0;
1296}
1297
Ben Hutchings8831da72008-09-01 12:47:48 +01001298static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001299{
Ben Hutchings602a5322011-05-16 17:32:39 +01001300 struct efx_channel *channel;
1301 struct efx_tx_queue *tx_queue;
1302
Ben Hutchings97653432011-01-12 18:26:56 +00001303 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001304 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001305
1306 /* We need to adjust the TX queue numbers if we have separate
1307 * RX-only and TX-only channels.
1308 */
1309 efx_for_each_channel(channel, efx) {
1310 efx_for_each_channel_tx_queue(tx_queue, channel)
1311 tx_queue->queue -= (efx->tx_channel_offset *
1312 EFX_TXQ_TYPES);
1313 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001314}
1315
1316static int efx_probe_nic(struct efx_nic *efx)
1317{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001318 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001319 int rc;
1320
Ben Hutchings62776d02010-06-23 11:30:07 +00001321 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001322
1323 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001324 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001325 if (rc)
1326 return rc;
1327
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001328 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001329 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001330 rc = efx_probe_interrupts(efx);
1331 if (rc)
1332 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001333
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001334 if (efx->n_channels > 1)
1335 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001336 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001337 efx->rx_indir_table[i] =
1338 ethtool_rxfh_indir_default(i, efx->n_rx_channels);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001339
Ben Hutchings8831da72008-09-01 12:47:48 +01001340 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001341 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1342 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001343
1344 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001345 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1346 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001347
1348 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001349
1350fail:
1351 efx->type->remove(efx);
1352 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001353}
1354
1355static void efx_remove_nic(struct efx_nic *efx)
1356{
Ben Hutchings62776d02010-06-23 11:30:07 +00001357 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001358
1359 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001360 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001361}
1362
1363/**************************************************************************
1364 *
1365 * NIC startup/shutdown
1366 *
1367 *************************************************************************/
1368
1369static int efx_probe_all(struct efx_nic *efx)
1370{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001371 int rc;
1372
Ben Hutchings8ceee662008-04-27 12:55:59 +01001373 rc = efx_probe_nic(efx);
1374 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001375 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001376 goto fail1;
1377 }
1378
Ben Hutchings8ceee662008-04-27 12:55:59 +01001379 rc = efx_probe_port(efx);
1380 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001381 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001382 goto fail2;
1383 }
1384
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001385 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings46426102010-09-10 06:42:33 +00001386 rc = efx_probe_channels(efx);
1387 if (rc)
1388 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001389
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001390 rc = efx_probe_filters(efx);
1391 if (rc) {
1392 netif_err(efx, probe, efx->net_dev,
1393 "failed to create filter tables\n");
1394 goto fail4;
1395 }
1396
Ben Hutchings8ceee662008-04-27 12:55:59 +01001397 return 0;
1398
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001399 fail4:
1400 efx_remove_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001401 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001402 efx_remove_port(efx);
1403 fail2:
1404 efx_remove_nic(efx);
1405 fail1:
1406 return rc;
1407}
1408
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001409/* Called after previous invocation(s) of efx_stop_all, restarts the port,
1410 * kernel transmit queues and NAPI processing, and ensures that the port is
1411 * scheduled to be reconfigured. This function is safe to call multiple
1412 * times when the NIC is in any state.
1413 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001414static void efx_start_all(struct efx_nic *efx)
1415{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001416 EFX_ASSERT_RESET_SERIALISED(efx);
1417
1418 /* Check that it is appropriate to restart the interface. All
1419 * of these flags are safe to read under just the rtnl lock */
1420 if (efx->port_enabled)
1421 return;
1422 if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1423 return;
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001424 if (!netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001425 return;
1426
Ben Hutchings8ceee662008-04-27 12:55:59 +01001427 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001428 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001429
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001430 /* Start the hardware monitor if there is one. Otherwise (we're link
1431 * event driven), we have to poll the PHY because after an event queue
1432 * flush, we could have a missed a link state change */
1433 if (efx->type->monitor != NULL) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001434 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1435 efx_monitor_interval);
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001436 } else {
1437 mutex_lock(&efx->mac_lock);
1438 if (efx->phy_op->poll(efx))
1439 efx_link_status_changed(efx);
1440 mutex_unlock(&efx->mac_lock);
1441 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001442
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001443 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001444}
1445
1446/* Flush all delayed work. Should only be called when no more delayed work
1447 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1448 * since we're holding the rtnl_lock at this point. */
1449static void efx_flush_all(struct efx_nic *efx)
1450{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001451 /* Make sure the hardware monitor is stopped */
1452 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001453 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001454 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001455}
1456
1457/* Quiesce hardware and software without bringing the link down.
1458 * Safe to call multiple times, when the nic and interface is in any
1459 * state. The caller is guaranteed to subsequently be in a position
1460 * to modify any hardware and software state they see fit without
1461 * taking locks. */
1462static void efx_stop_all(struct efx_nic *efx)
1463{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001464 EFX_ASSERT_RESET_SERIALISED(efx);
1465
1466 /* port_enabled can be read safely under the rtnl lock */
1467 if (!efx->port_enabled)
1468 return;
1469
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001470 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001471 efx_stop_port(efx);
1472
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001473 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001474 efx_flush_all(efx);
1475
Ben Hutchings8ceee662008-04-27 12:55:59 +01001476 /* Stop the kernel transmit interface late, so the watchdog
1477 * timer isn't ticking over the flush */
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001478 netif_tx_disable(efx->net_dev);
1479
1480 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001481}
1482
1483static void efx_remove_all(struct efx_nic *efx)
1484{
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001485 efx_remove_filters(efx);
Ben Hutchings46426102010-09-10 06:42:33 +00001486 efx_remove_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001487 efx_remove_port(efx);
1488 efx_remove_nic(efx);
1489}
1490
Ben Hutchings8ceee662008-04-27 12:55:59 +01001491/**************************************************************************
1492 *
1493 * Interrupt moderation
1494 *
1495 **************************************************************************/
1496
Ben Hutchingscc180b62011-12-08 19:51:47 +00001497static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001498{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001499 if (usecs == 0)
1500 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001501 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001502 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001503 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001504}
1505
Ben Hutchings8ceee662008-04-27 12:55:59 +01001506/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001507int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1508 unsigned int rx_usecs, bool rx_adaptive,
1509 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001510{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001511 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001512 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1513 efx->timer_quantum_ns,
1514 1000);
1515 unsigned int tx_ticks;
1516 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001517
1518 EFX_ASSERT_RESET_SERIALISED(efx);
1519
Ben Hutchingscc180b62011-12-08 19:51:47 +00001520 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001521 return -EINVAL;
1522
Ben Hutchingscc180b62011-12-08 19:51:47 +00001523 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1524 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1525
Ben Hutchings9e393b32011-09-05 07:43:04 +00001526 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1527 !rx_may_override_tx) {
1528 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1529 "RX and TX IRQ moderation must be equal\n");
1530 return -EINVAL;
1531 }
1532
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001533 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001534 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001535 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001536 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001537 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001538 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001539 channel->irq_moderation = tx_ticks;
1540 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001541
1542 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001543}
1544
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001545void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1546 unsigned int *rx_usecs, bool *rx_adaptive)
1547{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001548 /* We must round up when converting ticks to microseconds
1549 * because we round down when converting the other way.
1550 */
1551
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001552 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001553 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1554 efx->timer_quantum_ns,
1555 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001556
1557 /* If channels are shared between RX and TX, so is IRQ
1558 * moderation. Otherwise, IRQ moderation is the same for all
1559 * TX channels and is not adaptive.
1560 */
1561 if (efx->tx_channel_offset == 0)
1562 *tx_usecs = *rx_usecs;
1563 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001564 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001565 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001566 efx->timer_quantum_ns,
1567 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001568}
1569
Ben Hutchings8ceee662008-04-27 12:55:59 +01001570/**************************************************************************
1571 *
1572 * Hardware monitor
1573 *
1574 **************************************************************************/
1575
Ben Hutchingse254c272010-09-20 08:44:10 +00001576/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001577static void efx_monitor(struct work_struct *data)
1578{
1579 struct efx_nic *efx = container_of(data, struct efx_nic,
1580 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001581
Ben Hutchings62776d02010-06-23 11:30:07 +00001582 netif_vdbg(efx, timer, efx->net_dev,
1583 "hardware monitor executing on CPU %d\n",
1584 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001585 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001586
Ben Hutchings8ceee662008-04-27 12:55:59 +01001587 /* If the mac_lock is already held then it is likely a port
1588 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001589 * most of the work of monitor() anyway. */
1590 if (mutex_trylock(&efx->mac_lock)) {
1591 if (efx->port_enabled)
1592 efx->type->monitor(efx);
1593 mutex_unlock(&efx->mac_lock);
1594 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001595
Ben Hutchings8ceee662008-04-27 12:55:59 +01001596 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1597 efx_monitor_interval);
1598}
1599
1600/**************************************************************************
1601 *
1602 * ioctls
1603 *
1604 *************************************************************************/
1605
1606/* Net device ioctl
1607 * Context: process, rtnl_lock() held.
1608 */
1609static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1610{
Ben Hutchings767e4682008-09-01 12:43:14 +01001611 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001612 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001613
1614 EFX_ASSERT_RESET_SERIALISED(efx);
1615
Ben Hutchings68e7f452009-04-29 08:05:08 +00001616 /* Convert phy_id from older PRTAD/DEVAD format */
1617 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1618 (data->phy_id & 0xfc00) == 0x0400)
1619 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1620
1621 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001622}
1623
1624/**************************************************************************
1625 *
1626 * NAPI interface
1627 *
1628 **************************************************************************/
1629
Ben Hutchingse8f14992010-12-07 19:47:34 +00001630static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001631{
1632 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001633
1634 efx_for_each_channel(channel, efx) {
1635 channel->napi_dev = efx->net_dev;
Ben Hutchings718cff12009-04-14 19:47:46 -07001636 netif_napi_add(channel->napi_dev, &channel->napi_str,
1637 efx_poll, napi_weight);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001638 }
Ben Hutchingse8f14992010-12-07 19:47:34 +00001639}
1640
1641static void efx_fini_napi_channel(struct efx_channel *channel)
1642{
1643 if (channel->napi_dev)
1644 netif_napi_del(&channel->napi_str);
1645 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001646}
1647
1648static void efx_fini_napi(struct efx_nic *efx)
1649{
1650 struct efx_channel *channel;
1651
Ben Hutchingse8f14992010-12-07 19:47:34 +00001652 efx_for_each_channel(channel, efx)
1653 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001654}
1655
1656/**************************************************************************
1657 *
1658 * Kernel netpoll interface
1659 *
1660 *************************************************************************/
1661
1662#ifdef CONFIG_NET_POLL_CONTROLLER
1663
1664/* Although in the common case interrupts will be disabled, this is not
1665 * guaranteed. However, all our work happens inside the NAPI callback,
1666 * so no locking is required.
1667 */
1668static void efx_netpoll(struct net_device *net_dev)
1669{
Ben Hutchings767e4682008-09-01 12:43:14 +01001670 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001671 struct efx_channel *channel;
1672
Ben Hutchings64ee3122008-09-01 12:47:38 +01001673 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001674 efx_schedule_channel(channel);
1675}
1676
1677#endif
1678
1679/**************************************************************************
1680 *
1681 * Kernel net device interface
1682 *
1683 *************************************************************************/
1684
1685/* Context: process, rtnl_lock() held. */
1686static int efx_net_open(struct net_device *net_dev)
1687{
Ben Hutchings767e4682008-09-01 12:43:14 +01001688 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001689 EFX_ASSERT_RESET_SERIALISED(efx);
1690
Ben Hutchings62776d02010-06-23 11:30:07 +00001691 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1692 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001693
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001694 if (efx->state == STATE_DISABLED)
1695 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001696 if (efx->phy_mode & PHY_MODE_SPECIAL)
1697 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001698 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1699 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001700
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001701 /* Notify the kernel of the link state polled during driver load,
1702 * before the monitor starts running */
1703 efx_link_status_changed(efx);
1704
Ben Hutchings8ceee662008-04-27 12:55:59 +01001705 efx_start_all(efx);
1706 return 0;
1707}
1708
1709/* Context: process, rtnl_lock() held.
1710 * Note that the kernel will ignore our return code; this method
1711 * should really be a void.
1712 */
1713static int efx_net_stop(struct net_device *net_dev)
1714{
Ben Hutchings767e4682008-09-01 12:43:14 +01001715 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001716
Ben Hutchings62776d02010-06-23 11:30:07 +00001717 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1718 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001719
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001720 if (efx->state != STATE_DISABLED) {
1721 /* Stop the device and flush all the channels */
1722 efx_stop_all(efx);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001723 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001724
1725 return 0;
1726}
1727
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001728/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001729static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1730 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001731{
Ben Hutchings767e4682008-09-01 12:43:14 +01001732 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001733 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001734
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001735 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001736
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001737 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001738
1739 stats->rx_packets = mac_stats->rx_packets;
1740 stats->tx_packets = mac_stats->tx_packets;
1741 stats->rx_bytes = mac_stats->rx_bytes;
1742 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001743 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001744 stats->multicast = mac_stats->rx_multicast;
1745 stats->collisions = mac_stats->tx_collision;
1746 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1747 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001748 stats->rx_crc_errors = mac_stats->rx_bad;
1749 stats->rx_frame_errors = mac_stats->rx_align_error;
1750 stats->rx_fifo_errors = mac_stats->rx_overflow;
1751 stats->rx_missed_errors = mac_stats->rx_missed;
1752 stats->tx_window_errors = mac_stats->tx_late_collision;
1753
1754 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001755 stats->rx_crc_errors +
1756 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001757 mac_stats->rx_symbol_error);
1758 stats->tx_errors = (stats->tx_window_errors +
1759 mac_stats->tx_bad);
1760
Ben Hutchings1cb34522011-09-02 23:23:00 +01001761 spin_unlock_bh(&efx->stats_lock);
1762
Ben Hutchings8ceee662008-04-27 12:55:59 +01001763 return stats;
1764}
1765
1766/* Context: netif_tx_lock held, BHs disabled. */
1767static void efx_watchdog(struct net_device *net_dev)
1768{
Ben Hutchings767e4682008-09-01 12:43:14 +01001769 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001770
Ben Hutchings62776d02010-06-23 11:30:07 +00001771 netif_err(efx, tx_err, efx->net_dev,
1772 "TX stuck with port_enabled=%d: resetting channels\n",
1773 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001774
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001775 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001776}
1777
1778
1779/* Context: process, rtnl_lock() held. */
1780static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1781{
Ben Hutchings767e4682008-09-01 12:43:14 +01001782 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001783
1784 EFX_ASSERT_RESET_SERIALISED(efx);
1785
1786 if (new_mtu > EFX_MAX_MTU)
1787 return -EINVAL;
1788
1789 efx_stop_all(efx);
1790
Ben Hutchings62776d02010-06-23 11:30:07 +00001791 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001792
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001793 mutex_lock(&efx->mac_lock);
1794 /* Reconfigure the MAC before enabling the dma queues so that
1795 * the RX buffers don't overflow */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001796 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001797 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001798 mutex_unlock(&efx->mac_lock);
1799
Ben Hutchings8ceee662008-04-27 12:55:59 +01001800 efx_start_all(efx);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001801 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001802}
1803
1804static int efx_set_mac_address(struct net_device *net_dev, void *data)
1805{
Ben Hutchings767e4682008-09-01 12:43:14 +01001806 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001807 struct sockaddr *addr = data;
1808 char *new_addr = addr->sa_data;
1809
1810 EFX_ASSERT_RESET_SERIALISED(efx);
1811
1812 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001813 netif_err(efx, drv, efx->net_dev,
1814 "invalid ethernet MAC address requested: %pM\n",
1815 new_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001816 return -EINVAL;
1817 }
1818
1819 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1820
1821 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001822 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01001823 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001824 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001825
1826 return 0;
1827}
1828
Ben Hutchingsa816f752008-09-01 12:49:12 +01001829/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00001830static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001831{
Ben Hutchings767e4682008-09-01 12:43:14 +01001832 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001833 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001834 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001835 u32 crc;
1836 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001837
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001838 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001839
1840 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001841 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001842 memset(mc_hash, 0xff, sizeof(*mc_hash));
1843 } else {
1844 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00001845 netdev_for_each_mc_addr(ha, net_dev) {
1846 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001847 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1848 set_bit_le(bit, mc_hash->byte);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001849 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001850
1851 /* Broadcast packets go through the multicast hash filter.
1852 * ether_crc_le() of the broadcast address is 0xbe2612ff
1853 * so we always add bit 0xff to the mask.
1854 */
1855 set_bit_le(0xff, mc_hash->byte);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001856 }
1857
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001858 if (efx->port_enabled)
1859 queue_work(efx->workqueue, &efx->mac_work);
1860 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001861}
1862
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001863static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01001864{
1865 struct efx_nic *efx = netdev_priv(net_dev);
1866
1867 /* If disabling RX n-tuple filtering, clear existing filters */
1868 if (net_dev->features & ~data & NETIF_F_NTUPLE)
1869 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
1870
1871 return 0;
1872}
1873
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001874static const struct net_device_ops efx_netdev_ops = {
1875 .ndo_open = efx_net_open,
1876 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00001877 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001878 .ndo_tx_timeout = efx_watchdog,
1879 .ndo_start_xmit = efx_hard_start_xmit,
1880 .ndo_validate_addr = eth_validate_addr,
1881 .ndo_do_ioctl = efx_ioctl,
1882 .ndo_change_mtu = efx_change_mtu,
1883 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00001884 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01001885 .ndo_set_features = efx_set_features,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001886#ifdef CONFIG_NET_POLL_CONTROLLER
1887 .ndo_poll_controller = efx_netpoll,
1888#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00001889 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001890#ifdef CONFIG_RFS_ACCEL
1891 .ndo_rx_flow_steer = efx_filter_rfs,
1892#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001893};
1894
Ben Hutchings7dde5962008-12-12 22:09:38 -08001895static void efx_update_name(struct efx_nic *efx)
1896{
1897 strcpy(efx->name, efx->net_dev->name);
1898 efx_mtd_rename(efx);
1899 efx_set_channel_names(efx);
1900}
1901
Ben Hutchings8ceee662008-04-27 12:55:59 +01001902static int efx_netdev_event(struct notifier_block *this,
1903 unsigned long event, void *ptr)
1904{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001905 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001906
Ben Hutchings7dde5962008-12-12 22:09:38 -08001907 if (net_dev->netdev_ops == &efx_netdev_ops &&
1908 event == NETDEV_CHANGENAME)
1909 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001910
1911 return NOTIFY_DONE;
1912}
1913
1914static struct notifier_block efx_netdev_notifier = {
1915 .notifier_call = efx_netdev_event,
1916};
1917
Ben Hutchings06d5e192008-12-12 21:47:23 -08001918static ssize_t
1919show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
1920{
1921 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
1922 return sprintf(buf, "%d\n", efx->phy_type);
1923}
1924static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
1925
Ben Hutchings8ceee662008-04-27 12:55:59 +01001926static int efx_register_netdev(struct efx_nic *efx)
1927{
1928 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00001929 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001930 int rc;
1931
1932 net_dev->watchdog_timeo = 5 * HZ;
1933 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001934 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001935 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
1936
Ben Hutchings7dde5962008-12-12 22:09:38 -08001937 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00001938
1939 rc = dev_alloc_name(net_dev, net_dev->name);
1940 if (rc < 0)
1941 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08001942 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00001943
1944 rc = register_netdevice(net_dev);
1945 if (rc)
1946 goto fail_locked;
1947
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00001948 efx_for_each_channel(channel, efx) {
1949 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00001950 efx_for_each_channel_tx_queue(tx_queue, channel)
1951 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00001952 }
1953
Ben Hutchingsaed06282009-08-26 08:16:27 +00001954 /* Always start with carrier off; PHY events will detect the link */
Ben Hutchings86ee5302012-01-09 19:51:22 +00001955 netif_carrier_off(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00001956
Ben Hutchings7dde5962008-12-12 22:09:38 -08001957 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01001958
Ben Hutchings06d5e192008-12-12 21:47:23 -08001959 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
1960 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001961 netif_err(efx, drv, efx->net_dev,
1962 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08001963 goto fail_registered;
1964 }
1965
Ben Hutchings8ceee662008-04-27 12:55:59 +01001966 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08001967
Ben Hutchingsaed06282009-08-26 08:16:27 +00001968fail_locked:
1969 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00001970 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00001971 return rc;
1972
Ben Hutchings06d5e192008-12-12 21:47:23 -08001973fail_registered:
1974 unregister_netdev(net_dev);
1975 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001976}
1977
1978static void efx_unregister_netdev(struct efx_nic *efx)
1979{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001980 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001981 struct efx_tx_queue *tx_queue;
1982
1983 if (!efx->net_dev)
1984 return;
1985
Ben Hutchings767e4682008-09-01 12:43:14 +01001986 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001987
1988 /* Free up any skbs still remaining. This has to happen before
1989 * we try to unregister the netdev as running their destructors
1990 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001991 efx_for_each_channel(channel, efx) {
1992 efx_for_each_channel_tx_queue(tx_queue, channel)
1993 efx_release_tx_buffers(tx_queue);
1994 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001995
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001996 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
1997 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
1998 unregister_netdev(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001999}
2000
2001/**************************************************************************
2002 *
2003 * Device reset and suspend
2004 *
2005 **************************************************************************/
2006
Ben Hutchings2467ca42008-09-01 12:48:50 +01002007/* Tears down the entire software state and most of the hardware state
2008 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002009void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002010{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002011 EFX_ASSERT_RESET_SERIALISED(efx);
2012
Ben Hutchings2467ca42008-09-01 12:48:50 +01002013 efx_stop_all(efx);
2014 mutex_lock(&efx->mac_lock);
2015
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002016 efx_stop_interrupts(efx);
Steve Hodgson4b988282009-01-29 17:50:51 +00002017 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2018 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002019 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002020}
2021
Ben Hutchings2467ca42008-09-01 12:48:50 +01002022/* This function will always ensure that the locks acquired in
2023 * efx_reset_down() are released. A failure return code indicates
2024 * that we were unable to reinitialise the hardware, and the
2025 * driver should be disabled. If ok is false, then the rx and tx
2026 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002027int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002028{
2029 int rc;
2030
Ben Hutchings2467ca42008-09-01 12:48:50 +01002031 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002032
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002033 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002034 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002035 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002036 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002037 }
2038
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002039 if (!ok)
2040 goto fail;
2041
Steve Hodgson4b988282009-01-29 17:50:51 +00002042 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002043 rc = efx->phy_op->init(efx);
2044 if (rc)
2045 goto fail;
2046 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002047 netif_err(efx, drv, efx->net_dev,
2048 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002049 }
2050
Ben Hutchings710b2082011-09-03 00:15:00 +01002051 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002052
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002053 efx_start_interrupts(efx);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002054 efx_restore_filters(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002055
2056 mutex_unlock(&efx->mac_lock);
2057
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002058 efx_start_all(efx);
2059
2060 return 0;
2061
2062fail:
2063 efx->port_initialized = false;
2064
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002065 mutex_unlock(&efx->mac_lock);
2066
Ben Hutchings8ceee662008-04-27 12:55:59 +01002067 return rc;
2068}
2069
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002070/* Reset the NIC using the specified method. Note that the reset may
2071 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002072 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002073 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002074 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002075int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002076{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002077 int rc, rc2;
2078 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002079
Ben Hutchings62776d02010-06-23 11:30:07 +00002080 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2081 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002082
Ben Hutchingse4abce82011-05-16 18:51:24 +01002083 netif_device_detach(efx->net_dev);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002084 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002085
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002086 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002087 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002088 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002089 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002090 }
2091
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002092 /* Clear flags for the scopes we covered. We assume the NIC and
2093 * driver are now quiescent so that there is no race here.
2094 */
2095 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002096
2097 /* Reinitialise bus-mastering, which may have been turned off before
2098 * the reset was scheduled. This is still appropriate, even in the
2099 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2100 * can respond to requests. */
2101 pci_set_master(efx->pci_dev);
2102
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002103out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002104 /* Leave device stopped if necessary */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002105 disabled = rc || method == RESET_TYPE_DISABLE;
2106 rc2 = efx_reset_up(efx, method, !disabled);
2107 if (rc2) {
2108 disabled = true;
2109 if (!rc)
2110 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002111 }
2112
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002113 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002114 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002115 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002116 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002117 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002118 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002119 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002120 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002121 return rc;
2122}
2123
2124/* The worker thread exists so that code that cannot sleep can
2125 * schedule a reset for later.
2126 */
2127static void efx_reset_work(struct work_struct *data)
2128{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002129 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002130 unsigned long pending = ACCESS_ONCE(efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002131
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002132 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002133 return;
2134
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002135 /* If we're not RUNNING then don't reset. Leave the reset_pending
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002136 * flags set so that efx_pci_probe_main will be retried */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002137 if (efx->state != STATE_RUNNING) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002138 netif_info(efx, drv, efx->net_dev,
2139 "scheduled reset quenched. NIC not RUNNING\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002140 return;
2141 }
2142
2143 rtnl_lock();
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002144 (void)efx_reset(efx, fls(pending) - 1);
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002145 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002146}
2147
2148void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2149{
2150 enum reset_type method;
2151
Ben Hutchings8ceee662008-04-27 12:55:59 +01002152 switch (type) {
2153 case RESET_TYPE_INVISIBLE:
2154 case RESET_TYPE_ALL:
2155 case RESET_TYPE_WORLD:
2156 case RESET_TYPE_DISABLE:
2157 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002158 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2159 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002160 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002161 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002162 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002163 netif_dbg(efx, drv, efx->net_dev,
2164 "scheduling %s reset for %s\n",
2165 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002166 break;
2167 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002168
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002169 set_bit(method, &efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002170
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002171 /* efx_process_channel() will no longer read events once a
2172 * reset is scheduled. So switch back to poll'd MCDI completions. */
2173 efx_mcdi_mode_poll(efx);
2174
Steve Hodgson1ab00622008-12-12 21:33:02 -08002175 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002176}
2177
2178/**************************************************************************
2179 *
2180 * List of NICs we support
2181 *
2182 **************************************************************************/
2183
2184/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002185static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002186 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2187 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002188 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002189 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2190 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002191 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002192 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002193 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002194 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002195 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002196 {0} /* end of list */
2197};
2198
2199/**************************************************************************
2200 *
Ben Hutchings37594332009-11-23 16:05:45 +00002201 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002202 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002203 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002204 * Needed so all function pointers are valid and do not have to be tested
2205 * before use
2206 *
2207 **************************************************************************/
2208int efx_port_dummy_op_int(struct efx_nic *efx)
2209{
2210 return 0;
2211}
2212void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002213
2214static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002215{
2216 return false;
2217}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002218
stephen hemminger6c8c2512011-04-14 05:50:12 +00002219static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002220 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002221 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002222 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002223 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002224};
2225
Ben Hutchings8ceee662008-04-27 12:55:59 +01002226/**************************************************************************
2227 *
2228 * Data housekeeping
2229 *
2230 **************************************************************************/
2231
2232/* This zeroes out and then fills in the invariants in a struct
2233 * efx_nic (including all sub-structures).
2234 */
stephen hemminger6c8c2512011-04-14 05:50:12 +00002235static int efx_init_struct(struct efx_nic *efx, const struct efx_nic_type *type,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002236 struct pci_dev *pci_dev, struct net_device *net_dev)
2237{
Ben Hutchings46426102010-09-10 06:42:33 +00002238 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002239
2240 /* Initialise common structures */
2241 memset(efx, 0, sizeof(*efx));
2242 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002243#ifdef CONFIG_SFC_MTD
2244 INIT_LIST_HEAD(&efx->mtd_list);
2245#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002246 INIT_WORK(&efx->reset_work, efx_reset_work);
2247 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
2248 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002249 efx->msg_enable = debug;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002250 efx->state = STATE_INIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002251 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002252
2253 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002254 spin_lock_init(&efx->stats_lock);
2255 mutex_init(&efx->mac_lock);
2256 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002257 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002258 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002259 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002260
2261 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002262 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2263 if (!efx->channel[i])
2264 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002265 }
2266
2267 efx->type = type;
2268
Ben Hutchings8ceee662008-04-27 12:55:59 +01002269 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2270
2271 /* Higher numbered interrupt modes are less capable! */
2272 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2273 interrupt_mode);
2274
Ben Hutchings6977dc62008-12-26 13:44:39 -08002275 /* Would be good to use the net_dev name, but we're too early */
2276 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2277 pci_name(pci_dev));
2278 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002279 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002280 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002281
Ben Hutchings8ceee662008-04-27 12:55:59 +01002282 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002283
2284fail:
2285 efx_fini_struct(efx);
2286 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002287}
2288
2289static void efx_fini_struct(struct efx_nic *efx)
2290{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002291 int i;
2292
2293 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2294 kfree(efx->channel[i]);
2295
Ben Hutchings8ceee662008-04-27 12:55:59 +01002296 if (efx->workqueue) {
2297 destroy_workqueue(efx->workqueue);
2298 efx->workqueue = NULL;
2299 }
2300}
2301
2302/**************************************************************************
2303 *
2304 * PCI interface
2305 *
2306 **************************************************************************/
2307
2308/* Main body of final NIC shutdown code
2309 * This is called only at module unload (or hotplug removal).
2310 */
2311static void efx_pci_remove_main(struct efx_nic *efx)
2312{
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002313#ifdef CONFIG_RFS_ACCEL
2314 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2315 efx->net_dev->rx_cpu_rmap = NULL;
2316#endif
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002317 efx_stop_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002318 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002319 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002320 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002321 efx_fini_napi(efx);
2322 efx_remove_all(efx);
2323}
2324
2325/* Final NIC shutdown
2326 * This is called only at module unload (or hotplug removal).
2327 */
2328static void efx_pci_remove(struct pci_dev *pci_dev)
2329{
2330 struct efx_nic *efx;
2331
2332 efx = pci_get_drvdata(pci_dev);
2333 if (!efx)
2334 return;
2335
2336 /* Mark the NIC as fini, then stop the interface */
2337 rtnl_lock();
2338 efx->state = STATE_FINI;
2339 dev_close(efx->net_dev);
2340
2341 /* Allow any queued efx_resets() to complete */
2342 rtnl_unlock();
2343
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002344 efx_stop_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002345 efx_unregister_netdev(efx);
2346
Ben Hutchings7dde5962008-12-12 22:09:38 -08002347 efx_mtd_remove(efx);
2348
Ben Hutchings8ceee662008-04-27 12:55:59 +01002349 /* Wait for any scheduled resets to complete. No more will be
2350 * scheduled from this point because efx_stop_all() has been
2351 * called, we are no longer registered with driverlink, and
2352 * the net_device's have been removed. */
Steve Hodgson1ab00622008-12-12 21:33:02 -08002353 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002354
2355 efx_pci_remove_main(efx);
2356
Ben Hutchings8ceee662008-04-27 12:55:59 +01002357 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002358 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002359
2360 pci_set_drvdata(pci_dev, NULL);
2361 efx_fini_struct(efx);
2362 free_netdev(efx->net_dev);
2363};
2364
2365/* Main body of NIC initialisation
2366 * This is called at module load (or hotplug insertion, theoretically).
2367 */
2368static int efx_pci_probe_main(struct efx_nic *efx)
2369{
2370 int rc;
2371
2372 /* Do start-of-day initialisation */
2373 rc = efx_probe_all(efx);
2374 if (rc)
2375 goto fail1;
2376
Ben Hutchingse8f14992010-12-07 19:47:34 +00002377 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002378
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002379 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002380 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002381 netif_err(efx, probe, efx->net_dev,
2382 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002383 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002384 }
2385
2386 rc = efx_init_port(efx);
2387 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002388 netif_err(efx, probe, efx->net_dev,
2389 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002390 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002391 }
2392
Ben Hutchings152b6a62009-11-29 03:43:56 +00002393 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002394 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002395 goto fail5;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002396 efx_start_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002397
2398 return 0;
2399
Ben Hutchings278c0622009-11-23 16:05:12 +00002400 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002401 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002402 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002403 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002404 fail3:
2405 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002406 efx_remove_all(efx);
2407 fail1:
2408 return rc;
2409}
2410
2411/* NIC initialisation
2412 *
2413 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002414 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002415 * sets up and registers the network devices with the kernel and hooks
2416 * the interrupt service routine. It does not prepare the device for
2417 * transmission; this is left to the first time one of the network
2418 * interfaces is brought up (i.e. efx_net_open).
2419 */
2420static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2421 const struct pci_device_id *entry)
2422{
stephen hemminger6c8c2512011-04-14 05:50:12 +00002423 const struct efx_nic_type *type = (const struct efx_nic_type *) entry->driver_data;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002424 struct net_device *net_dev;
2425 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002426 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002427
2428 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002429 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2430 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002431 if (!net_dev)
2432 return -ENOMEM;
Ben Hutchingsc383b532009-11-29 15:11:02 +00002433 net_dev->features |= (type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002434 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002435 NETIF_F_RXCSUM);
Ben Hutchings738a8f42009-11-29 15:16:05 +00002436 if (type->offload_features & NETIF_F_V6_CSUM)
2437 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002438 /* Mask for features that also apply to VLAN devices */
2439 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002440 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2441 NETIF_F_RXCSUM);
2442 /* All offloads can be toggled */
2443 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings767e4682008-09-01 12:43:14 +01002444 efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002445 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002446 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002447 rc = efx_init_struct(efx, type, pci_dev, net_dev);
2448 if (rc)
2449 goto fail1;
2450
Ben Hutchings62776d02010-06-23 11:30:07 +00002451 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002452 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002453
2454 /* Set up basic I/O (BAR mappings etc) */
2455 rc = efx_init_io(efx);
2456 if (rc)
2457 goto fail2;
2458
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002459 rc = efx_pci_probe_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002460
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002461 /* Serialise against efx_reset(). No more resets will be
2462 * scheduled since efx_stop_all() has been called, and we have
2463 * not and never have been registered.
2464 */
2465 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002466
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002467 if (rc)
2468 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002469
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002470 /* If there was a scheduled reset during probe, the NIC is
2471 * probably hosed anyway.
2472 */
2473 if (efx->reset_pending) {
2474 rc = -EIO;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002475 goto fail4;
2476 }
2477
Ben Hutchings55edc6e2009-11-25 16:11:35 +00002478 /* Switch to the running state before we expose the device to the OS,
2479 * so that dev_open()|efx_start_all() will actually start the device */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002480 efx->state = STATE_RUNNING;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002481
Ben Hutchings8ceee662008-04-27 12:55:59 +01002482 rc = efx_register_netdev(efx);
2483 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002484 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002485
Ben Hutchings62776d02010-06-23 11:30:07 +00002486 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002487
Ben Hutchings7c431612012-01-27 17:23:58 +00002488 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002489 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002490 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002491 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002492 if (rc)
2493 netif_warn(efx, probe, efx->net_dev,
2494 "failed to create MTDs (%d)\n", rc);
2495
Ben Hutchings8ceee662008-04-27 12:55:59 +01002496 return 0;
2497
Ben Hutchings8ceee662008-04-27 12:55:59 +01002498 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002499 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002500 fail3:
2501 efx_fini_io(efx);
2502 fail2:
2503 efx_fini_struct(efx);
2504 fail1:
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002505 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002506 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002507 free_netdev(net_dev);
2508 return rc;
2509}
2510
Ben Hutchings89c758f2009-11-29 03:43:07 +00002511static int efx_pm_freeze(struct device *dev)
2512{
2513 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2514
2515 efx->state = STATE_FINI;
2516
2517 netif_device_detach(efx->net_dev);
2518
2519 efx_stop_all(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002520 efx_stop_interrupts(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002521
2522 return 0;
2523}
2524
2525static int efx_pm_thaw(struct device *dev)
2526{
2527 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2528
2529 efx->state = STATE_INIT;
2530
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002531 efx_start_interrupts(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002532
2533 mutex_lock(&efx->mac_lock);
2534 efx->phy_op->reconfigure(efx);
2535 mutex_unlock(&efx->mac_lock);
2536
2537 efx_start_all(efx);
2538
2539 netif_device_attach(efx->net_dev);
2540
2541 efx->state = STATE_RUNNING;
2542
2543 efx->type->resume_wol(efx);
2544
Steve Hodgson319ba642010-06-01 11:17:24 +00002545 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2546 queue_work(reset_workqueue, &efx->reset_work);
2547
Ben Hutchings89c758f2009-11-29 03:43:07 +00002548 return 0;
2549}
2550
2551static int efx_pm_poweroff(struct device *dev)
2552{
2553 struct pci_dev *pci_dev = to_pci_dev(dev);
2554 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2555
2556 efx->type->fini(efx);
2557
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002558 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002559
2560 pci_save_state(pci_dev);
2561 return pci_set_power_state(pci_dev, PCI_D3hot);
2562}
2563
2564/* Used for both resume and restore */
2565static int efx_pm_resume(struct device *dev)
2566{
2567 struct pci_dev *pci_dev = to_pci_dev(dev);
2568 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2569 int rc;
2570
2571 rc = pci_set_power_state(pci_dev, PCI_D0);
2572 if (rc)
2573 return rc;
2574 pci_restore_state(pci_dev);
2575 rc = pci_enable_device(pci_dev);
2576 if (rc)
2577 return rc;
2578 pci_set_master(efx->pci_dev);
2579 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2580 if (rc)
2581 return rc;
2582 rc = efx->type->init(efx);
2583 if (rc)
2584 return rc;
2585 efx_pm_thaw(dev);
2586 return 0;
2587}
2588
2589static int efx_pm_suspend(struct device *dev)
2590{
2591 int rc;
2592
2593 efx_pm_freeze(dev);
2594 rc = efx_pm_poweroff(dev);
2595 if (rc)
2596 efx_pm_resume(dev);
2597 return rc;
2598}
2599
Ben Hutchings18e83e42012-01-05 19:05:20 +00002600static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002601 .suspend = efx_pm_suspend,
2602 .resume = efx_pm_resume,
2603 .freeze = efx_pm_freeze,
2604 .thaw = efx_pm_thaw,
2605 .poweroff = efx_pm_poweroff,
2606 .restore = efx_pm_resume,
2607};
2608
Ben Hutchings8ceee662008-04-27 12:55:59 +01002609static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00002610 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002611 .id_table = efx_pci_table,
2612 .probe = efx_pci_probe,
2613 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00002614 .driver.pm = &efx_pm_ops,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002615};
2616
2617/**************************************************************************
2618 *
2619 * Kernel module interface
2620 *
2621 *************************************************************************/
2622
2623module_param(interrupt_mode, uint, 0444);
2624MODULE_PARM_DESC(interrupt_mode,
2625 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2626
2627static int __init efx_init_module(void)
2628{
2629 int rc;
2630
2631 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2632
2633 rc = register_netdevice_notifier(&efx_netdev_notifier);
2634 if (rc)
2635 goto err_notifier;
2636
Steve Hodgson1ab00622008-12-12 21:33:02 -08002637 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2638 if (!reset_workqueue) {
2639 rc = -ENOMEM;
2640 goto err_reset;
2641 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002642
2643 rc = pci_register_driver(&efx_pci_driver);
2644 if (rc < 0)
2645 goto err_pci;
2646
2647 return 0;
2648
2649 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08002650 destroy_workqueue(reset_workqueue);
2651 err_reset:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002652 unregister_netdevice_notifier(&efx_netdev_notifier);
2653 err_notifier:
2654 return rc;
2655}
2656
2657static void __exit efx_exit_module(void)
2658{
2659 printk(KERN_INFO "Solarflare NET driver unloading\n");
2660
2661 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002662 destroy_workqueue(reset_workqueue);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002663 unregister_netdevice_notifier(&efx_netdev_notifier);
2664
2665}
2666
2667module_init(efx_init_module);
2668module_exit(efx_exit_module);
2669
Ben Hutchings906bb262009-11-29 15:16:19 +00002670MODULE_AUTHOR("Solarflare Communications and "
2671 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002672MODULE_DESCRIPTION("Solarflare Communications network driver");
2673MODULE_LICENSE("GPL");
2674MODULE_DEVICE_TABLE(pci, efx_pci_table);