blob: 952d0bf7695a4c89b8e8ca0401dbe964f70d97f5 [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
189static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100190static void efx_remove_port(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000191static void efx_init_napi(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100192static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000193static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000194static void efx_fini_struct(struct efx_nic *efx);
195static void efx_start_all(struct efx_nic *efx);
196static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100197
198#define EFX_ASSERT_RESET_SERIALISED(efx) \
199 do { \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000200 if ((efx->state == STATE_RUNNING) || \
201 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100202 ASSERT_RTNL(); \
203 } while (0)
204
205/**************************************************************************
206 *
207 * Event queue processing
208 *
209 *************************************************************************/
210
211/* Process channel's event queue
212 *
213 * This function is responsible for processing the event queue of a
214 * single channel. The caller must guarantee that this function will
215 * never be concurrently called more than once on the same channel,
216 * though different channels may be being processed concurrently.
217 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000218static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100219{
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100220 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000221 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100222
Ben Hutchingsa7d529a2011-06-24 20:46:31 +0100223 if (unlikely(efx->reset_pending || !channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100224 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100225
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000226 spent = efx_nic_process_eventq(channel, budget);
227 if (spent == 0)
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100228 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100229
230 /* Deliver last RX packet. */
231 if (channel->rx_pkt) {
Ben Hutchingsdb339562011-08-26 18:05:11 +0100232 __efx_rx_packet(channel, channel->rx_pkt);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100233 channel->rx_pkt = NULL;
234 }
235
Ben Hutchings8ceee662008-04-27 12:55:59 +0100236 efx_rx_strategy(channel);
237
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +0000238 efx_fast_push_rx_descriptors(efx_channel_get_rx_queue(channel));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100239
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000240 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100241}
242
243/* Mark channel as finished processing
244 *
245 * Note that since we will not receive further interrupts for this
246 * channel before we finish processing and call the eventq_read_ack()
247 * method, there is no need to use the interrupt hold-off timers.
248 */
249static inline void efx_channel_processed(struct efx_channel *channel)
250{
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100251 /* The interrupt handler for this channel may set work_pending
252 * as soon as we acknowledge the events we've seen. Make sure
253 * it's cleared before then. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100254 channel->work_pending = false;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100255 smp_wmb();
256
Ben Hutchings152b6a62009-11-29 03:43:56 +0000257 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100258}
259
260/* NAPI poll handler
261 *
262 * NAPI guarantees serialisation of polls of the same device, which
263 * provides the guarantee required by efx_process_channel().
264 */
265static int efx_poll(struct napi_struct *napi, int budget)
266{
267 struct efx_channel *channel =
268 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000269 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000270 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100271
Ben Hutchings62776d02010-06-23 11:30:07 +0000272 netif_vdbg(efx, intr, efx->net_dev,
273 "channel %d NAPI poll executing on CPU %d\n",
274 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100275
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000276 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100277
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000278 if (spent < budget) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +0000279 if (channel->channel < efx->n_rx_channels &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000280 efx->irq_rx_adaptive &&
281 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000282 if (unlikely(channel->irq_mod_score <
283 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000284 if (channel->irq_moderation > 1) {
285 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000286 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000287 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000288 } else if (unlikely(channel->irq_mod_score >
289 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000290 if (channel->irq_moderation <
291 efx->irq_rx_moderation) {
292 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000293 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000294 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000295 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000296 channel->irq_count = 0;
297 channel->irq_mod_score = 0;
298 }
299
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000300 efx_filter_rfs_expire(channel);
301
Ben Hutchings8ceee662008-04-27 12:55:59 +0100302 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800303 * only wait for napi_complete(), this isn't a problem
Ben Hutchings8ceee662008-04-27 12:55:59 +0100304 * since efx_channel_processed() will have no effect if
305 * interrupts have already been disabled.
306 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800307 napi_complete(napi);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100308 efx_channel_processed(channel);
309 }
310
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000311 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100312}
313
314/* Process the eventq of the specified channel immediately on this CPU
315 *
316 * Disable hardware generated interrupts, wait for any existing
317 * processing to finish, then directly poll (and ack ) the eventq.
318 * Finally reenable NAPI and interrupts.
319 *
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100320 * This is for use only during a loopback self-test. It must not
321 * deliver any packets up the stack as this can result in deadlock.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100322 */
323void efx_process_channel_now(struct efx_channel *channel)
324{
325 struct efx_nic *efx = channel->efx;
326
Ben Hutchings8313aca2010-09-10 06:41:57 +0000327 BUG_ON(channel->channel >= efx->n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100328 BUG_ON(!channel->enabled);
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100329 BUG_ON(!efx->loopback_selftest);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100330
331 /* Disable interrupts and wait for ISRs to complete */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000332 efx_nic_disable_interrupts(efx);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000333 if (efx->legacy_irq) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100334 synchronize_irq(efx->legacy_irq);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000335 efx->legacy_irq_enabled = false;
336 }
Ben Hutchings64ee3122008-09-01 12:47:38 +0100337 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100338 synchronize_irq(channel->irq);
339
340 /* Wait for any NAPI processing to complete */
341 napi_disable(&channel->napi_str);
342
343 /* Poll the channel */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000344 efx_process_channel(channel, channel->eventq_mask + 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100345
346 /* Ack the eventq. This may cause an interrupt to be generated
347 * when they are reenabled */
348 efx_channel_processed(channel);
349
350 napi_enable(&channel->napi_str);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000351 if (efx->legacy_irq)
352 efx->legacy_irq_enabled = true;
Ben Hutchings152b6a62009-11-29 03:43:56 +0000353 efx_nic_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100354}
355
356/* Create event queue
357 * Event queue memory allocations are done only once. If the channel
358 * is reset, the memory buffer will be reused; this guards against
359 * errors during channel reset and also simplifies interrupt handling.
360 */
361static int efx_probe_eventq(struct efx_channel *channel)
362{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000363 struct efx_nic *efx = channel->efx;
364 unsigned long entries;
365
Ben Hutchings86ee5302012-01-09 19:51:22 +0000366 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000367 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100368
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000369 /* Build an event queue with room for one event per tx and rx buffer,
370 * plus some extra for link state events and MCDI completions. */
371 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
372 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
373 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
374
Ben Hutchings152b6a62009-11-29 03:43:56 +0000375 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100376}
377
378/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100379static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100380{
Ben Hutchings62776d02010-06-23 11:30:07 +0000381 netif_dbg(channel->efx, drv, channel->efx->net_dev,
382 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100383
384 channel->eventq_read_ptr = 0;
385
Ben Hutchings152b6a62009-11-29 03:43:56 +0000386 efx_nic_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100387}
388
389static void efx_fini_eventq(struct efx_channel *channel)
390{
Ben Hutchings62776d02010-06-23 11:30:07 +0000391 netif_dbg(channel->efx, drv, channel->efx->net_dev,
392 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100393
Ben Hutchings152b6a62009-11-29 03:43:56 +0000394 efx_nic_fini_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100395}
396
397static void efx_remove_eventq(struct efx_channel *channel)
398{
Ben Hutchings62776d02010-06-23 11:30:07 +0000399 netif_dbg(channel->efx, drv, channel->efx->net_dev,
400 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100401
Ben Hutchings152b6a62009-11-29 03:43:56 +0000402 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100403}
404
405/**************************************************************************
406 *
407 * Channel handling
408 *
409 *************************************************************************/
410
Ben Hutchings46426102010-09-10 06:42:33 +0000411/* Allocate and initialise a channel structure, optionally copying
412 * parameters (but not resources) from an old channel structure. */
413static struct efx_channel *
414efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
415{
416 struct efx_channel *channel;
417 struct efx_rx_queue *rx_queue;
418 struct efx_tx_queue *tx_queue;
419 int j;
420
421 if (old_channel) {
422 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
423 if (!channel)
424 return NULL;
425
426 *channel = *old_channel;
427
Ben Hutchingse8f14992010-12-07 19:47:34 +0000428 channel->napi_dev = NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000429 memset(&channel->eventq, 0, sizeof(channel->eventq));
430
431 rx_queue = &channel->rx_queue;
432 rx_queue->buffer = NULL;
433 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
434
435 for (j = 0; j < EFX_TXQ_TYPES; j++) {
436 tx_queue = &channel->tx_queue[j];
437 if (tx_queue->channel)
438 tx_queue->channel = channel;
439 tx_queue->buffer = NULL;
440 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
441 }
442 } else {
443 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
444 if (!channel)
445 return NULL;
446
447 channel->efx = efx;
448 channel->channel = i;
449
450 for (j = 0; j < EFX_TXQ_TYPES; j++) {
451 tx_queue = &channel->tx_queue[j];
452 tx_queue->efx = efx;
453 tx_queue->queue = i * EFX_TXQ_TYPES + j;
454 tx_queue->channel = channel;
455 }
456 }
457
Ben Hutchings46426102010-09-10 06:42:33 +0000458 rx_queue = &channel->rx_queue;
459 rx_queue->efx = efx;
460 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
461 (unsigned long)rx_queue);
462
463 return channel;
464}
465
Ben Hutchings8ceee662008-04-27 12:55:59 +0100466static int efx_probe_channel(struct efx_channel *channel)
467{
468 struct efx_tx_queue *tx_queue;
469 struct efx_rx_queue *rx_queue;
470 int rc;
471
Ben Hutchings62776d02010-06-23 11:30:07 +0000472 netif_dbg(channel->efx, probe, channel->efx->net_dev,
473 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100474
475 rc = efx_probe_eventq(channel);
476 if (rc)
477 goto fail1;
478
479 efx_for_each_channel_tx_queue(tx_queue, channel) {
480 rc = efx_probe_tx_queue(tx_queue);
481 if (rc)
482 goto fail2;
483 }
484
485 efx_for_each_channel_rx_queue(rx_queue, channel) {
486 rc = efx_probe_rx_queue(rx_queue);
487 if (rc)
488 goto fail3;
489 }
490
491 channel->n_rx_frm_trunc = 0;
492
493 return 0;
494
495 fail3:
496 efx_for_each_channel_rx_queue(rx_queue, channel)
497 efx_remove_rx_queue(rx_queue);
498 fail2:
499 efx_for_each_channel_tx_queue(tx_queue, channel)
500 efx_remove_tx_queue(tx_queue);
501 fail1:
502 return rc;
503}
504
505
Ben Hutchings56536e92008-12-12 21:37:02 -0800506static void efx_set_channel_names(struct efx_nic *efx)
507{
508 struct efx_channel *channel;
509 const char *type = "";
510 int number;
511
512 efx_for_each_channel(channel, efx) {
513 number = channel->channel;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +0000514 if (efx->n_channels > efx->n_rx_channels) {
515 if (channel->channel < efx->n_rx_channels) {
Ben Hutchings56536e92008-12-12 21:37:02 -0800516 type = "-rx";
517 } else {
518 type = "-tx";
Ben Hutchingsa4900ac2010-04-28 09:30:43 +0000519 number -= efx->n_rx_channels;
Ben Hutchings56536e92008-12-12 21:37:02 -0800520 }
521 }
Ben Hutchings46426102010-09-10 06:42:33 +0000522 snprintf(efx->channel_name[channel->channel],
523 sizeof(efx->channel_name[0]),
Ben Hutchings56536e92008-12-12 21:37:02 -0800524 "%s%s-%d", efx->name, type, number);
525 }
526}
527
Ben Hutchings46426102010-09-10 06:42:33 +0000528static int efx_probe_channels(struct efx_nic *efx)
529{
530 struct efx_channel *channel;
531 int rc;
532
533 /* Restart special buffer allocation */
534 efx->next_buffer_table = 0;
535
536 efx_for_each_channel(channel, efx) {
537 rc = efx_probe_channel(channel);
538 if (rc) {
539 netif_err(efx, probe, efx->net_dev,
540 "failed to create channel %d\n",
541 channel->channel);
542 goto fail;
543 }
544 }
545 efx_set_channel_names(efx);
546
547 return 0;
548
549fail:
550 efx_remove_channels(efx);
551 return rc;
552}
553
Ben Hutchings8ceee662008-04-27 12:55:59 +0100554/* Channels are shutdown and reinitialised whilst the NIC is running
555 * to propagate configuration changes (mtu, checksum offload), or
556 * to clear hardware error conditions
557 */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100558static void efx_init_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100559{
560 struct efx_tx_queue *tx_queue;
561 struct efx_rx_queue *rx_queue;
562 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100563
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100564 /* Calculate the rx buffer allocation parameters required to
565 * support the current MTU, including padding for header
566 * alignment and overruns.
567 */
568 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
569 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
Ben Hutchings39c9cf02010-06-23 11:31:28 +0000570 efx->type->rx_buffer_hash_size +
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100571 efx->type->rx_buffer_padding);
Steve Hodgson62b330b2010-06-01 11:20:53 +0000572 efx->rx_buffer_order = get_order(efx->rx_buffer_len +
573 sizeof(struct efx_rx_page_state));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100574
575 /* Initialise the channels */
576 efx_for_each_channel(channel, efx) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000577 netif_dbg(channel->efx, drv, channel->efx->net_dev,
578 "init chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100579
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100580 efx_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100581
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100582 efx_for_each_channel_tx_queue(tx_queue, channel)
583 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100584
585 /* The rx buffer allocation strategy is MTU dependent */
586 efx_rx_strategy(channel);
587
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100588 efx_for_each_channel_rx_queue(rx_queue, channel)
589 efx_init_rx_queue(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100590
591 WARN_ON(channel->rx_pkt != NULL);
592 efx_rx_strategy(channel);
593 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100594}
595
596/* This enables event queue processing and packet transmission.
597 *
598 * Note that this function is not allowed to fail, since that would
599 * introduce too much complexity into the suspend/resume path.
600 */
601static void efx_start_channel(struct efx_channel *channel)
602{
603 struct efx_rx_queue *rx_queue;
604
Ben Hutchings62776d02010-06-23 11:30:07 +0000605 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
606 "starting chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100607
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100608 /* The interrupt handler for this channel may set work_pending
609 * as soon as we enable it. Make sure it's cleared before
610 * then. Similarly, make sure it sees the enabled flag set. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100611 channel->work_pending = false;
612 channel->enabled = true;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100613 smp_wmb();
Ben Hutchings8ceee662008-04-27 12:55:59 +0100614
Steve Hodgson90d683a2010-06-01 11:19:39 +0000615 /* Fill the queues before enabling NAPI */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100616 efx_for_each_channel_rx_queue(rx_queue, channel)
617 efx_fast_push_rx_descriptors(rx_queue);
Steve Hodgson90d683a2010-06-01 11:19:39 +0000618
619 napi_enable(&channel->napi_str);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100620}
621
622/* This disables event queue processing and packet transmission.
623 * This function does not guarantee that all queue processing
624 * (e.g. RX refill) is complete.
625 */
626static void efx_stop_channel(struct efx_channel *channel)
627{
Ben Hutchings8ceee662008-04-27 12:55:59 +0100628 if (!channel->enabled)
629 return;
630
Ben Hutchings62776d02010-06-23 11:30:07 +0000631 netif_dbg(channel->efx, ifdown, channel->efx->net_dev,
632 "stop chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100633
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100634 channel->enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100635 napi_disable(&channel->napi_str);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100636}
637
638static void efx_fini_channels(struct efx_nic *efx)
639{
640 struct efx_channel *channel;
641 struct efx_tx_queue *tx_queue;
642 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100643 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100644
645 EFX_ASSERT_RESET_SERIALISED(efx);
646 BUG_ON(efx->port_enabled);
647
Ben Hutchings152b6a62009-11-29 03:43:56 +0000648 rc = efx_nic_flush_queues(efx);
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000649 if (rc && EFX_WORKAROUND_7803(efx)) {
650 /* Schedule a reset to recover from the flush failure. The
651 * descriptor caches reference memory we're about to free,
652 * but falcon_reconfigure_mac_wrapper() won't reconnect
653 * the MACs because of the pending reset. */
Ben Hutchings62776d02010-06-23 11:30:07 +0000654 netif_err(efx, drv, efx->net_dev,
655 "Resetting to recover from flush failure\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000656 efx_schedule_reset(efx, RESET_TYPE_ALL);
657 } else if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000658 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000659 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +0000660 netif_dbg(efx, drv, efx->net_dev,
661 "successfully flushed all queues\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000662 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100663
Ben Hutchings8ceee662008-04-27 12:55:59 +0100664 efx_for_each_channel(channel, efx) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000665 netif_dbg(channel->efx, drv, channel->efx->net_dev,
666 "shut down chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100667
668 efx_for_each_channel_rx_queue(rx_queue, channel)
669 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000670 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100671 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100672 efx_fini_eventq(channel);
673 }
674}
675
676static void efx_remove_channel(struct efx_channel *channel)
677{
678 struct efx_tx_queue *tx_queue;
679 struct efx_rx_queue *rx_queue;
680
Ben Hutchings62776d02010-06-23 11:30:07 +0000681 netif_dbg(channel->efx, drv, channel->efx->net_dev,
682 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100683
684 efx_for_each_channel_rx_queue(rx_queue, channel)
685 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000686 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100687 efx_remove_tx_queue(tx_queue);
688 efx_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100689}
690
Ben Hutchings46426102010-09-10 06:42:33 +0000691static void efx_remove_channels(struct efx_nic *efx)
692{
693 struct efx_channel *channel;
694
695 efx_for_each_channel(channel, efx)
696 efx_remove_channel(channel);
697}
698
699int
700efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
701{
702 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
703 u32 old_rxq_entries, old_txq_entries;
704 unsigned i;
705 int rc;
706
707 efx_stop_all(efx);
708 efx_fini_channels(efx);
709
710 /* Clone channels */
711 memset(other_channel, 0, sizeof(other_channel));
712 for (i = 0; i < efx->n_channels; i++) {
713 channel = efx_alloc_channel(efx, i, efx->channel[i]);
714 if (!channel) {
715 rc = -ENOMEM;
716 goto out;
717 }
718 other_channel[i] = channel;
719 }
720
721 /* Swap entry counts and channel pointers */
722 old_rxq_entries = efx->rxq_entries;
723 old_txq_entries = efx->txq_entries;
724 efx->rxq_entries = rxq_entries;
725 efx->txq_entries = txq_entries;
726 for (i = 0; i < efx->n_channels; i++) {
727 channel = efx->channel[i];
728 efx->channel[i] = other_channel[i];
729 other_channel[i] = channel;
730 }
731
732 rc = efx_probe_channels(efx);
733 if (rc)
734 goto rollback;
735
Ben Hutchingse8f14992010-12-07 19:47:34 +0000736 efx_init_napi(efx);
737
Ben Hutchings46426102010-09-10 06:42:33 +0000738 /* Destroy old channels */
Ben Hutchingse8f14992010-12-07 19:47:34 +0000739 for (i = 0; i < efx->n_channels; i++) {
740 efx_fini_napi_channel(other_channel[i]);
Ben Hutchings46426102010-09-10 06:42:33 +0000741 efx_remove_channel(other_channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000742 }
Ben Hutchings46426102010-09-10 06:42:33 +0000743out:
744 /* Free unused channel structures */
745 for (i = 0; i < efx->n_channels; i++)
746 kfree(other_channel[i]);
747
748 efx_init_channels(efx);
749 efx_start_all(efx);
750 return rc;
751
752rollback:
753 /* Swap back */
754 efx->rxq_entries = old_rxq_entries;
755 efx->txq_entries = old_txq_entries;
756 for (i = 0; i < efx->n_channels; i++) {
757 channel = efx->channel[i];
758 efx->channel[i] = other_channel[i];
759 other_channel[i] = channel;
760 }
761 goto out;
762}
763
Steve Hodgson90d683a2010-06-01 11:19:39 +0000764void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100765{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000766 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100767}
768
769/**************************************************************************
770 *
771 * Port handling
772 *
773 **************************************************************************/
774
775/* This ensures that the kernel is kept informed (via
776 * netif_carrier_on/off) of the link status, and also maintains the
777 * link status's stop on the port's TX queue.
778 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000779void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100780{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000781 struct efx_link_state *link_state = &efx->link_state;
782
Ben Hutchings8ceee662008-04-27 12:55:59 +0100783 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
784 * that no events are triggered between unregister_netdev() and the
785 * driver unloading. A more general condition is that NETDEV_CHANGE
786 * can only be generated between NETDEV_UP and NETDEV_DOWN */
787 if (!netif_running(efx->net_dev))
788 return;
789
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000790 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100791 efx->n_link_state_changes++;
792
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000793 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100794 netif_carrier_on(efx->net_dev);
795 else
796 netif_carrier_off(efx->net_dev);
797 }
798
799 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000800 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000801 netif_info(efx, link, efx->net_dev,
802 "link up at %uMbps %s-duplex (MTU %d)%s\n",
803 link_state->speed, link_state->fd ? "full" : "half",
804 efx->net_dev->mtu,
805 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000806 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000807 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100808}
809
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000810void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
811{
812 efx->link_advertising = advertising;
813 if (advertising) {
814 if (advertising & ADVERTISED_Pause)
815 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
816 else
817 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
818 if (advertising & ADVERTISED_Asym_Pause)
819 efx->wanted_fc ^= EFX_FC_TX;
820 }
821}
822
David S. Millerb56269462011-05-17 17:53:22 -0400823void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000824{
825 efx->wanted_fc = wanted_fc;
826 if (efx->link_advertising) {
827 if (wanted_fc & EFX_FC_RX)
828 efx->link_advertising |= (ADVERTISED_Pause |
829 ADVERTISED_Asym_Pause);
830 else
831 efx->link_advertising &= ~(ADVERTISED_Pause |
832 ADVERTISED_Asym_Pause);
833 if (wanted_fc & EFX_FC_TX)
834 efx->link_advertising ^= ADVERTISED_Asym_Pause;
835 }
836}
837
Ben Hutchings115122a2009-03-04 09:52:52 +0000838static void efx_fini_port(struct efx_nic *efx);
839
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000840/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
841 * the MAC appropriately. All other PHY configuration changes are pushed
842 * through phy_op->set_settings(), and pushed asynchronously to the MAC
843 * through efx_monitor().
844 *
845 * Callers must hold the mac_lock
846 */
847int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100848{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000849 enum efx_phy_mode phy_mode;
850 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100851
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000852 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100853
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000854 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000855 netif_addr_lock_bh(efx->net_dev);
856 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100857
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000858 /* Disable PHY transmit in mac level loopbacks */
859 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800860 if (LOOPBACK_INTERNAL(efx))
861 efx->phy_mode |= PHY_MODE_TX_DISABLED;
862 else
863 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800864
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000865 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800866
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000867 if (rc)
868 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100869
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000870 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100871}
872
873/* Reinitialise the MAC to pick up new PHY settings, even if the port is
874 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000875int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100876{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000877 int rc;
878
Ben Hutchings8ceee662008-04-27 12:55:59 +0100879 EFX_ASSERT_RESET_SERIALISED(efx);
880
881 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000882 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100883 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000884
885 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100886}
887
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000888/* Asynchronous work item for changing MAC promiscuity and multicast
889 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
890 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800891static void efx_mac_work(struct work_struct *data)
892{
893 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
894
895 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100896 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100897 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800898 mutex_unlock(&efx->mac_lock);
899}
900
Ben Hutchings8ceee662008-04-27 12:55:59 +0100901static int efx_probe_port(struct efx_nic *efx)
902{
903 int rc;
904
Ben Hutchings62776d02010-06-23 11:30:07 +0000905 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100906
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000907 if (phy_flash_cfg)
908 efx->phy_mode = PHY_MODE_SPECIAL;
909
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000910 /* Connect up MAC/PHY operations table */
911 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100912 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000913 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100914
Ben Hutchingse332bcb2011-12-20 01:22:51 +0000915 /* Initialise MAC address to permanent address */
916 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100917
918 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100919}
920
921static int efx_init_port(struct efx_nic *efx)
922{
923 int rc;
924
Ben Hutchings62776d02010-06-23 11:30:07 +0000925 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100926
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000927 mutex_lock(&efx->mac_lock);
928
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800929 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100930 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000931 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100932
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100933 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000934
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000935 /* Reconfigure the MAC before creating dma queues (required for
936 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +0100937 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000938
939 /* Ensure the PHY advertises the correct flow control settings */
940 rc = efx->phy_op->reconfigure(efx);
941 if (rc)
942 goto fail2;
943
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000944 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100945 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800946
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000947fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800948 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000949fail1:
950 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800951 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100952}
953
Ben Hutchings8ceee662008-04-27 12:55:59 +0100954static void efx_start_port(struct efx_nic *efx)
955{
Ben Hutchings62776d02010-06-23 11:30:07 +0000956 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100957 BUG_ON(efx->port_enabled);
958
959 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100960 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000961
962 /* efx_mac_work() might have been scheduled after efx_stop_port(),
963 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +0100964 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000965
Ben Hutchings8ceee662008-04-27 12:55:59 +0100966 mutex_unlock(&efx->mac_lock);
967}
968
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000969/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100970static void efx_stop_port(struct efx_nic *efx)
971{
Ben Hutchings62776d02010-06-23 11:30:07 +0000972 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100973
974 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100975 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100976 mutex_unlock(&efx->mac_lock);
977
978 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000979 netif_addr_lock_bh(efx->net_dev);
980 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100981}
982
983static void efx_fini_port(struct efx_nic *efx)
984{
Ben Hutchings62776d02010-06-23 11:30:07 +0000985 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100986
987 if (!efx->port_initialized)
988 return;
989
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800990 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100991 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100992
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000993 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100994 efx_link_status_changed(efx);
995}
996
997static void efx_remove_port(struct efx_nic *efx)
998{
Ben Hutchings62776d02010-06-23 11:30:07 +0000999 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001000
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001001 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001002}
1003
1004/**************************************************************************
1005 *
1006 * NIC handling
1007 *
1008 **************************************************************************/
1009
1010/* This configures the PCI device to enable I/O and DMA. */
1011static int efx_init_io(struct efx_nic *efx)
1012{
1013 struct pci_dev *pci_dev = efx->pci_dev;
1014 dma_addr_t dma_mask = efx->type->max_dma_mask;
1015 int rc;
1016
Ben Hutchings62776d02010-06-23 11:30:07 +00001017 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001018
1019 rc = pci_enable_device(pci_dev);
1020 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001021 netif_err(efx, probe, efx->net_dev,
1022 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001023 goto fail1;
1024 }
1025
1026 pci_set_master(pci_dev);
1027
1028 /* Set the PCI DMA mask. Try all possibilities from our
1029 * genuine mask down to 32 bits, because some architectures
1030 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1031 * masks event though they reject 46 bit masks.
1032 */
1033 while (dma_mask > 0x7fffffffUL) {
Ben Hutchingse9e01842012-01-05 18:50:29 +00001034 if (pci_dma_supported(pci_dev, dma_mask)) {
1035 rc = pci_set_dma_mask(pci_dev, dma_mask);
1036 if (rc == 0)
1037 break;
1038 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001039 dma_mask >>= 1;
1040 }
1041 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001042 netif_err(efx, probe, efx->net_dev,
1043 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001044 goto fail2;
1045 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001046 netif_dbg(efx, probe, efx->net_dev,
1047 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001048 rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
1049 if (rc) {
1050 /* pci_set_consistent_dma_mask() is not *allowed* to
1051 * fail with a mask that pci_set_dma_mask() accepted,
1052 * but just in case...
1053 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001054 netif_err(efx, probe, efx->net_dev,
1055 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001056 goto fail2;
1057 }
1058
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001059 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1060 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001061 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001062 netif_err(efx, probe, efx->net_dev,
1063 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001064 rc = -EIO;
1065 goto fail3;
1066 }
David S. Miller8decf862011-09-22 03:23:13 -04001067 efx->membase = ioremap_nocache(efx->membase_phys,
1068 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001069 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001070 netif_err(efx, probe, efx->net_dev,
1071 "could not map memory BAR at %llx+%x\n",
1072 (unsigned long long)efx->membase_phys,
1073 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001074 rc = -ENOMEM;
1075 goto fail4;
1076 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001077 netif_dbg(efx, probe, efx->net_dev,
1078 "memory BAR at %llx+%x (virtual %p)\n",
1079 (unsigned long long)efx->membase_phys,
1080 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001081
1082 return 0;
1083
1084 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001085 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001086 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001087 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001088 fail2:
1089 pci_disable_device(efx->pci_dev);
1090 fail1:
1091 return rc;
1092}
1093
1094static void efx_fini_io(struct efx_nic *efx)
1095{
Ben Hutchings62776d02010-06-23 11:30:07 +00001096 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001097
1098 if (efx->membase) {
1099 iounmap(efx->membase);
1100 efx->membase = NULL;
1101 }
1102
1103 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001104 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001105 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001106 }
1107
1108 pci_disable_device(efx->pci_dev);
1109}
1110
Ben Hutchingsfa142b92011-12-20 01:15:30 +00001111static int efx_wanted_parallelism(void)
Ben Hutchings46123d02008-09-01 12:47:33 +01001112{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001113 cpumask_var_t thread_mask;
Ben Hutchings46123d02008-09-01 12:47:33 +01001114 int count;
1115 int cpu;
1116
Ben Hutchings5b874e22011-01-12 19:11:05 +00001117 if (rss_cpus)
1118 return rss_cpus;
1119
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001120 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
Rusty Russell2f8975f2009-01-10 21:58:09 -08001121 printk(KERN_WARNING
Mike Travis3977d032009-05-12 10:48:36 +00001122 "sfc: RSS disabled due to allocation failure\n");
Rusty Russell2f8975f2009-01-10 21:58:09 -08001123 return 1;
1124 }
1125
Ben Hutchings46123d02008-09-01 12:47:33 +01001126 count = 0;
1127 for_each_online_cpu(cpu) {
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001128 if (!cpumask_test_cpu(cpu, thread_mask)) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001129 ++count;
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001130 cpumask_or(thread_mask, thread_mask,
1131 topology_thread_cpumask(cpu));
Ben Hutchings46123d02008-09-01 12:47:33 +01001132 }
1133 }
1134
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001135 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001136 return count;
1137}
1138
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001139static int
1140efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1141{
1142#ifdef CONFIG_RFS_ACCEL
1143 int i, rc;
1144
1145 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1146 if (!efx->net_dev->rx_cpu_rmap)
1147 return -ENOMEM;
1148 for (i = 0; i < efx->n_rx_channels; i++) {
1149 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1150 xentries[i].vector);
1151 if (rc) {
1152 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1153 efx->net_dev->rx_cpu_rmap = NULL;
1154 return rc;
1155 }
1156 }
1157#endif
1158 return 0;
1159}
1160
Ben Hutchings46123d02008-09-01 12:47:33 +01001161/* Probe the number and type of interrupts we are able to obtain, and
1162 * the resulting numbers of channels and RX queues.
1163 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001164static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001165{
Ben Hutchings46123d02008-09-01 12:47:33 +01001166 int max_channels =
1167 min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001168 int rc, i;
1169
1170 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001171 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001172 int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001173
Ben Hutchingsfa142b92011-12-20 01:15:30 +00001174 n_channels = efx_wanted_parallelism();
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001175 if (separate_tx_channels)
1176 n_channels *= 2;
1177 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001178
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001179 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001180 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001181 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001182 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001183 netif_err(efx, drv, efx->net_dev,
1184 "WARNING: Insufficient MSI-X vectors"
1185 " available (%d < %d).\n", rc, n_channels);
1186 netif_err(efx, drv, efx->net_dev,
1187 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001188 EFX_BUG_ON_PARANOID(rc >= n_channels);
1189 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001190 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001191 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001192 }
1193
1194 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001195 efx->n_channels = n_channels;
1196 if (separate_tx_channels) {
1197 efx->n_tx_channels =
1198 max(efx->n_channels / 2, 1U);
1199 efx->n_rx_channels =
1200 max(efx->n_channels -
1201 efx->n_tx_channels, 1U);
1202 } else {
1203 efx->n_tx_channels = efx->n_channels;
1204 efx->n_rx_channels = efx->n_channels;
1205 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001206 rc = efx_init_rx_cpu_rmap(efx, xentries);
1207 if (rc) {
1208 pci_disable_msix(efx->pci_dev);
1209 return rc;
1210 }
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001211 for (i = 0; i < n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001212 efx_get_channel(efx, i)->irq =
1213 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001214 } else {
1215 /* Fall back to single channel MSI */
1216 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001217 netif_err(efx, drv, efx->net_dev,
1218 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001219 }
1220 }
1221
1222 /* Try single interrupt MSI */
1223 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001224 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001225 efx->n_rx_channels = 1;
1226 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001227 rc = pci_enable_msi(efx->pci_dev);
1228 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001229 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001230 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001231 netif_err(efx, drv, efx->net_dev,
1232 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001233 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1234 }
1235 }
1236
1237 /* Assume legacy interrupts */
1238 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001239 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001240 efx->n_rx_channels = 1;
1241 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001242 efx->legacy_irq = efx->pci_dev->irq;
1243 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001244
1245 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001246}
1247
1248static void efx_remove_interrupts(struct efx_nic *efx)
1249{
1250 struct efx_channel *channel;
1251
1252 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001253 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001254 channel->irq = 0;
1255 pci_disable_msi(efx->pci_dev);
1256 pci_disable_msix(efx->pci_dev);
1257
1258 /* Remove legacy interrupt */
1259 efx->legacy_irq = 0;
1260}
1261
Ben Hutchings8831da72008-09-01 12:47:48 +01001262static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001263{
Ben Hutchings602a5322011-05-16 17:32:39 +01001264 struct efx_channel *channel;
1265 struct efx_tx_queue *tx_queue;
1266
Ben Hutchings97653432011-01-12 18:26:56 +00001267 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001268 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001269
1270 /* We need to adjust the TX queue numbers if we have separate
1271 * RX-only and TX-only channels.
1272 */
1273 efx_for_each_channel(channel, efx) {
1274 efx_for_each_channel_tx_queue(tx_queue, channel)
1275 tx_queue->queue -= (efx->tx_channel_offset *
1276 EFX_TXQ_TYPES);
1277 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001278}
1279
1280static int efx_probe_nic(struct efx_nic *efx)
1281{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001282 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001283 int rc;
1284
Ben Hutchings62776d02010-06-23 11:30:07 +00001285 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001286
1287 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001288 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001289 if (rc)
1290 return rc;
1291
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001292 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001293 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001294 rc = efx_probe_interrupts(efx);
1295 if (rc)
1296 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001297
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001298 if (efx->n_channels > 1)
1299 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001300 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001301 efx->rx_indir_table[i] =
1302 ethtool_rxfh_indir_default(i, efx->n_rx_channels);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001303
Ben Hutchings8831da72008-09-01 12:47:48 +01001304 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001305 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1306 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001307
1308 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001309 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1310 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001311
1312 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001313
1314fail:
1315 efx->type->remove(efx);
1316 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001317}
1318
1319static void efx_remove_nic(struct efx_nic *efx)
1320{
Ben Hutchings62776d02010-06-23 11:30:07 +00001321 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001322
1323 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001324 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001325}
1326
1327/**************************************************************************
1328 *
1329 * NIC startup/shutdown
1330 *
1331 *************************************************************************/
1332
1333static int efx_probe_all(struct efx_nic *efx)
1334{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001335 int rc;
1336
Ben Hutchings8ceee662008-04-27 12:55:59 +01001337 rc = efx_probe_nic(efx);
1338 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001339 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001340 goto fail1;
1341 }
1342
Ben Hutchings8ceee662008-04-27 12:55:59 +01001343 rc = efx_probe_port(efx);
1344 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001345 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001346 goto fail2;
1347 }
1348
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001349 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings46426102010-09-10 06:42:33 +00001350 rc = efx_probe_channels(efx);
1351 if (rc)
1352 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001353
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001354 rc = efx_probe_filters(efx);
1355 if (rc) {
1356 netif_err(efx, probe, efx->net_dev,
1357 "failed to create filter tables\n");
1358 goto fail4;
1359 }
1360
Ben Hutchings8ceee662008-04-27 12:55:59 +01001361 return 0;
1362
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001363 fail4:
1364 efx_remove_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001365 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001366 efx_remove_port(efx);
1367 fail2:
1368 efx_remove_nic(efx);
1369 fail1:
1370 return rc;
1371}
1372
1373/* Called after previous invocation(s) of efx_stop_all, restarts the
1374 * port, kernel transmit queue, NAPI processing and hardware interrupts,
1375 * and ensures that the port is scheduled to be reconfigured.
1376 * This function is safe to call multiple times when the NIC is in any
1377 * state. */
1378static void efx_start_all(struct efx_nic *efx)
1379{
1380 struct efx_channel *channel;
1381
1382 EFX_ASSERT_RESET_SERIALISED(efx);
1383
1384 /* Check that it is appropriate to restart the interface. All
1385 * of these flags are safe to read under just the rtnl lock */
1386 if (efx->port_enabled)
1387 return;
1388 if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1389 return;
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001390 if (!netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001391 return;
1392
1393 /* Mark the port as enabled so port reconfigurations can start, then
1394 * restart the transmit interface early so the watchdog timer stops */
1395 efx_start_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001396
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001397 if (netif_device_present(efx->net_dev))
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00001398 netif_tx_wake_all_queues(efx->net_dev);
1399
1400 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001401 efx_start_channel(channel);
1402
Ben Hutchings94dec6a2010-12-07 19:24:45 +00001403 if (efx->legacy_irq)
1404 efx->legacy_irq_enabled = true;
Ben Hutchings152b6a62009-11-29 03:43:56 +00001405 efx_nic_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001406
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001407 /* Switch to event based MCDI completions after enabling interrupts.
1408 * If a reset has been scheduled, then we need to stay in polled mode.
1409 * Rather than serialising efx_mcdi_mode_event() [which sleeps] and
1410 * reset_pending [modified from an atomic context], we instead guarantee
1411 * that efx_mcdi_mode_poll() isn't reverted erroneously */
1412 efx_mcdi_mode_event(efx);
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01001413 if (efx->reset_pending)
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001414 efx_mcdi_mode_poll(efx);
1415
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001416 /* Start the hardware monitor if there is one. Otherwise (we're link
1417 * event driven), we have to poll the PHY because after an event queue
1418 * flush, we could have a missed a link state change */
1419 if (efx->type->monitor != NULL) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001420 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1421 efx_monitor_interval);
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001422 } else {
1423 mutex_lock(&efx->mac_lock);
1424 if (efx->phy_op->poll(efx))
1425 efx_link_status_changed(efx);
1426 mutex_unlock(&efx->mac_lock);
1427 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001428
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001429 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001430}
1431
1432/* Flush all delayed work. Should only be called when no more delayed work
1433 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1434 * since we're holding the rtnl_lock at this point. */
1435static void efx_flush_all(struct efx_nic *efx)
1436{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001437 /* Make sure the hardware monitor is stopped */
1438 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001439 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001440 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001441}
1442
1443/* Quiesce hardware and software without bringing the link down.
1444 * Safe to call multiple times, when the nic and interface is in any
1445 * state. The caller is guaranteed to subsequently be in a position
1446 * to modify any hardware and software state they see fit without
1447 * taking locks. */
1448static void efx_stop_all(struct efx_nic *efx)
1449{
1450 struct efx_channel *channel;
1451
1452 EFX_ASSERT_RESET_SERIALISED(efx);
1453
1454 /* port_enabled can be read safely under the rtnl lock */
1455 if (!efx->port_enabled)
1456 return;
1457
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001458 efx->type->stop_stats(efx);
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001459
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001460 /* Switch to MCDI polling on Siena before disabling interrupts */
1461 efx_mcdi_mode_poll(efx);
1462
Ben Hutchings8ceee662008-04-27 12:55:59 +01001463 /* Disable interrupts and wait for ISR to complete */
Ben Hutchings152b6a62009-11-29 03:43:56 +00001464 efx_nic_disable_interrupts(efx);
Ben Hutchings94dec6a2010-12-07 19:24:45 +00001465 if (efx->legacy_irq) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001466 synchronize_irq(efx->legacy_irq);
Ben Hutchings94dec6a2010-12-07 19:24:45 +00001467 efx->legacy_irq_enabled = false;
1468 }
Ben Hutchings64ee3122008-09-01 12:47:38 +01001469 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001470 if (channel->irq)
1471 synchronize_irq(channel->irq);
Ben Hutchingsb3475642008-05-16 21:15:49 +01001472 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001473
1474 /* Stop all NAPI processing and synchronous rx refills */
1475 efx_for_each_channel(channel, efx)
1476 efx_stop_channel(channel);
1477
1478 /* Stop all asynchronous port reconfigurations. Since all
1479 * event processing has already been stopped, there is no
1480 * window to loose phy events */
1481 efx_stop_port(efx);
1482
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001483 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001484 efx_flush_all(efx);
1485
Ben Hutchings8ceee662008-04-27 12:55:59 +01001486 /* Stop the kernel transmit interface late, so the watchdog
1487 * timer isn't ticking over the flush */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001488 netif_tx_stop_all_queues(efx->net_dev);
1489 netif_tx_lock_bh(efx->net_dev);
1490 netif_tx_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001491}
1492
1493static void efx_remove_all(struct efx_nic *efx)
1494{
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001495 efx_remove_filters(efx);
Ben Hutchings46426102010-09-10 06:42:33 +00001496 efx_remove_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001497 efx_remove_port(efx);
1498 efx_remove_nic(efx);
1499}
1500
Ben Hutchings8ceee662008-04-27 12:55:59 +01001501/**************************************************************************
1502 *
1503 * Interrupt moderation
1504 *
1505 **************************************************************************/
1506
Ben Hutchingscc180b62011-12-08 19:51:47 +00001507static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001508{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001509 if (usecs == 0)
1510 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001511 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001512 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001513 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001514}
1515
Ben Hutchings8ceee662008-04-27 12:55:59 +01001516/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001517int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1518 unsigned int rx_usecs, bool rx_adaptive,
1519 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001520{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001521 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001522 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1523 efx->timer_quantum_ns,
1524 1000);
1525 unsigned int tx_ticks;
1526 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001527
1528 EFX_ASSERT_RESET_SERIALISED(efx);
1529
Ben Hutchingscc180b62011-12-08 19:51:47 +00001530 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001531 return -EINVAL;
1532
Ben Hutchingscc180b62011-12-08 19:51:47 +00001533 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1534 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1535
Ben Hutchings9e393b32011-09-05 07:43:04 +00001536 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1537 !rx_may_override_tx) {
1538 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1539 "RX and TX IRQ moderation must be equal\n");
1540 return -EINVAL;
1541 }
1542
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001543 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001544 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001545 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001546 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001547 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001548 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001549 channel->irq_moderation = tx_ticks;
1550 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001551
1552 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001553}
1554
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001555void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1556 unsigned int *rx_usecs, bool *rx_adaptive)
1557{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001558 /* We must round up when converting ticks to microseconds
1559 * because we round down when converting the other way.
1560 */
1561
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001562 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001563 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1564 efx->timer_quantum_ns,
1565 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001566
1567 /* If channels are shared between RX and TX, so is IRQ
1568 * moderation. Otherwise, IRQ moderation is the same for all
1569 * TX channels and is not adaptive.
1570 */
1571 if (efx->tx_channel_offset == 0)
1572 *tx_usecs = *rx_usecs;
1573 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001574 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001575 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001576 efx->timer_quantum_ns,
1577 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001578}
1579
Ben Hutchings8ceee662008-04-27 12:55:59 +01001580/**************************************************************************
1581 *
1582 * Hardware monitor
1583 *
1584 **************************************************************************/
1585
Ben Hutchingse254c272010-09-20 08:44:10 +00001586/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001587static void efx_monitor(struct work_struct *data)
1588{
1589 struct efx_nic *efx = container_of(data, struct efx_nic,
1590 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001591
Ben Hutchings62776d02010-06-23 11:30:07 +00001592 netif_vdbg(efx, timer, efx->net_dev,
1593 "hardware monitor executing on CPU %d\n",
1594 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001595 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001596
Ben Hutchings8ceee662008-04-27 12:55:59 +01001597 /* If the mac_lock is already held then it is likely a port
1598 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001599 * most of the work of monitor() anyway. */
1600 if (mutex_trylock(&efx->mac_lock)) {
1601 if (efx->port_enabled)
1602 efx->type->monitor(efx);
1603 mutex_unlock(&efx->mac_lock);
1604 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001605
Ben Hutchings8ceee662008-04-27 12:55:59 +01001606 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1607 efx_monitor_interval);
1608}
1609
1610/**************************************************************************
1611 *
1612 * ioctls
1613 *
1614 *************************************************************************/
1615
1616/* Net device ioctl
1617 * Context: process, rtnl_lock() held.
1618 */
1619static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1620{
Ben Hutchings767e4682008-09-01 12:43:14 +01001621 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001622 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001623
1624 EFX_ASSERT_RESET_SERIALISED(efx);
1625
Ben Hutchings68e7f452009-04-29 08:05:08 +00001626 /* Convert phy_id from older PRTAD/DEVAD format */
1627 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1628 (data->phy_id & 0xfc00) == 0x0400)
1629 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1630
1631 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001632}
1633
1634/**************************************************************************
1635 *
1636 * NAPI interface
1637 *
1638 **************************************************************************/
1639
Ben Hutchingse8f14992010-12-07 19:47:34 +00001640static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001641{
1642 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001643
1644 efx_for_each_channel(channel, efx) {
1645 channel->napi_dev = efx->net_dev;
Ben Hutchings718cff12009-04-14 19:47:46 -07001646 netif_napi_add(channel->napi_dev, &channel->napi_str,
1647 efx_poll, napi_weight);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001648 }
Ben Hutchingse8f14992010-12-07 19:47:34 +00001649}
1650
1651static void efx_fini_napi_channel(struct efx_channel *channel)
1652{
1653 if (channel->napi_dev)
1654 netif_napi_del(&channel->napi_str);
1655 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001656}
1657
1658static void efx_fini_napi(struct efx_nic *efx)
1659{
1660 struct efx_channel *channel;
1661
Ben Hutchingse8f14992010-12-07 19:47:34 +00001662 efx_for_each_channel(channel, efx)
1663 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001664}
1665
1666/**************************************************************************
1667 *
1668 * Kernel netpoll interface
1669 *
1670 *************************************************************************/
1671
1672#ifdef CONFIG_NET_POLL_CONTROLLER
1673
1674/* Although in the common case interrupts will be disabled, this is not
1675 * guaranteed. However, all our work happens inside the NAPI callback,
1676 * so no locking is required.
1677 */
1678static void efx_netpoll(struct net_device *net_dev)
1679{
Ben Hutchings767e4682008-09-01 12:43:14 +01001680 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001681 struct efx_channel *channel;
1682
Ben Hutchings64ee3122008-09-01 12:47:38 +01001683 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001684 efx_schedule_channel(channel);
1685}
1686
1687#endif
1688
1689/**************************************************************************
1690 *
1691 * Kernel net device interface
1692 *
1693 *************************************************************************/
1694
1695/* Context: process, rtnl_lock() held. */
1696static int efx_net_open(struct net_device *net_dev)
1697{
Ben Hutchings767e4682008-09-01 12:43:14 +01001698 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001699 EFX_ASSERT_RESET_SERIALISED(efx);
1700
Ben Hutchings62776d02010-06-23 11:30:07 +00001701 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1702 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001703
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001704 if (efx->state == STATE_DISABLED)
1705 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001706 if (efx->phy_mode & PHY_MODE_SPECIAL)
1707 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001708 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1709 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001710
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001711 /* Notify the kernel of the link state polled during driver load,
1712 * before the monitor starts running */
1713 efx_link_status_changed(efx);
1714
Ben Hutchings8ceee662008-04-27 12:55:59 +01001715 efx_start_all(efx);
1716 return 0;
1717}
1718
1719/* Context: process, rtnl_lock() held.
1720 * Note that the kernel will ignore our return code; this method
1721 * should really be a void.
1722 */
1723static int efx_net_stop(struct net_device *net_dev)
1724{
Ben Hutchings767e4682008-09-01 12:43:14 +01001725 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001726
Ben Hutchings62776d02010-06-23 11:30:07 +00001727 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1728 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001729
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001730 if (efx->state != STATE_DISABLED) {
1731 /* Stop the device and flush all the channels */
1732 efx_stop_all(efx);
1733 efx_fini_channels(efx);
1734 efx_init_channels(efx);
1735 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001736
1737 return 0;
1738}
1739
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001740/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001741static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1742 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001743{
Ben Hutchings767e4682008-09-01 12:43:14 +01001744 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001745 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001746
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001747 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001748
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001749 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001750
1751 stats->rx_packets = mac_stats->rx_packets;
1752 stats->tx_packets = mac_stats->tx_packets;
1753 stats->rx_bytes = mac_stats->rx_bytes;
1754 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001755 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001756 stats->multicast = mac_stats->rx_multicast;
1757 stats->collisions = mac_stats->tx_collision;
1758 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1759 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001760 stats->rx_crc_errors = mac_stats->rx_bad;
1761 stats->rx_frame_errors = mac_stats->rx_align_error;
1762 stats->rx_fifo_errors = mac_stats->rx_overflow;
1763 stats->rx_missed_errors = mac_stats->rx_missed;
1764 stats->tx_window_errors = mac_stats->tx_late_collision;
1765
1766 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001767 stats->rx_crc_errors +
1768 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001769 mac_stats->rx_symbol_error);
1770 stats->tx_errors = (stats->tx_window_errors +
1771 mac_stats->tx_bad);
1772
Ben Hutchings1cb34522011-09-02 23:23:00 +01001773 spin_unlock_bh(&efx->stats_lock);
1774
Ben Hutchings8ceee662008-04-27 12:55:59 +01001775 return stats;
1776}
1777
1778/* Context: netif_tx_lock held, BHs disabled. */
1779static void efx_watchdog(struct net_device *net_dev)
1780{
Ben Hutchings767e4682008-09-01 12:43:14 +01001781 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001782
Ben Hutchings62776d02010-06-23 11:30:07 +00001783 netif_err(efx, tx_err, efx->net_dev,
1784 "TX stuck with port_enabled=%d: resetting channels\n",
1785 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001786
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001787 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001788}
1789
1790
1791/* Context: process, rtnl_lock() held. */
1792static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1793{
Ben Hutchings767e4682008-09-01 12:43:14 +01001794 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001795
1796 EFX_ASSERT_RESET_SERIALISED(efx);
1797
1798 if (new_mtu > EFX_MAX_MTU)
1799 return -EINVAL;
1800
1801 efx_stop_all(efx);
1802
Ben Hutchings62776d02010-06-23 11:30:07 +00001803 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001804
1805 efx_fini_channels(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001806
1807 mutex_lock(&efx->mac_lock);
1808 /* Reconfigure the MAC before enabling the dma queues so that
1809 * the RX buffers don't overflow */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001810 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001811 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001812 mutex_unlock(&efx->mac_lock);
1813
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001814 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001815
1816 efx_start_all(efx);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001817 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001818}
1819
1820static int efx_set_mac_address(struct net_device *net_dev, void *data)
1821{
Ben Hutchings767e4682008-09-01 12:43:14 +01001822 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001823 struct sockaddr *addr = data;
1824 char *new_addr = addr->sa_data;
1825
1826 EFX_ASSERT_RESET_SERIALISED(efx);
1827
1828 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001829 netif_err(efx, drv, efx->net_dev,
1830 "invalid ethernet MAC address requested: %pM\n",
1831 new_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001832 return -EINVAL;
1833 }
1834
1835 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1836
1837 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001838 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01001839 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001840 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001841
1842 return 0;
1843}
1844
Ben Hutchingsa816f752008-09-01 12:49:12 +01001845/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00001846static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001847{
Ben Hutchings767e4682008-09-01 12:43:14 +01001848 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001849 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001850 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001851 u32 crc;
1852 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001853
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001854 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001855
1856 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001857 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001858 memset(mc_hash, 0xff, sizeof(*mc_hash));
1859 } else {
1860 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00001861 netdev_for_each_mc_addr(ha, net_dev) {
1862 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001863 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1864 set_bit_le(bit, mc_hash->byte);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001865 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001866
1867 /* Broadcast packets go through the multicast hash filter.
1868 * ether_crc_le() of the broadcast address is 0xbe2612ff
1869 * so we always add bit 0xff to the mask.
1870 */
1871 set_bit_le(0xff, mc_hash->byte);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001872 }
1873
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001874 if (efx->port_enabled)
1875 queue_work(efx->workqueue, &efx->mac_work);
1876 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001877}
1878
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001879static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01001880{
1881 struct efx_nic *efx = netdev_priv(net_dev);
1882
1883 /* If disabling RX n-tuple filtering, clear existing filters */
1884 if (net_dev->features & ~data & NETIF_F_NTUPLE)
1885 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
1886
1887 return 0;
1888}
1889
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001890static const struct net_device_ops efx_netdev_ops = {
1891 .ndo_open = efx_net_open,
1892 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00001893 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001894 .ndo_tx_timeout = efx_watchdog,
1895 .ndo_start_xmit = efx_hard_start_xmit,
1896 .ndo_validate_addr = eth_validate_addr,
1897 .ndo_do_ioctl = efx_ioctl,
1898 .ndo_change_mtu = efx_change_mtu,
1899 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00001900 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01001901 .ndo_set_features = efx_set_features,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001902#ifdef CONFIG_NET_POLL_CONTROLLER
1903 .ndo_poll_controller = efx_netpoll,
1904#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00001905 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001906#ifdef CONFIG_RFS_ACCEL
1907 .ndo_rx_flow_steer = efx_filter_rfs,
1908#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001909};
1910
Ben Hutchings7dde5962008-12-12 22:09:38 -08001911static void efx_update_name(struct efx_nic *efx)
1912{
1913 strcpy(efx->name, efx->net_dev->name);
1914 efx_mtd_rename(efx);
1915 efx_set_channel_names(efx);
1916}
1917
Ben Hutchings8ceee662008-04-27 12:55:59 +01001918static int efx_netdev_event(struct notifier_block *this,
1919 unsigned long event, void *ptr)
1920{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001921 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001922
Ben Hutchings7dde5962008-12-12 22:09:38 -08001923 if (net_dev->netdev_ops == &efx_netdev_ops &&
1924 event == NETDEV_CHANGENAME)
1925 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001926
1927 return NOTIFY_DONE;
1928}
1929
1930static struct notifier_block efx_netdev_notifier = {
1931 .notifier_call = efx_netdev_event,
1932};
1933
Ben Hutchings06d5e192008-12-12 21:47:23 -08001934static ssize_t
1935show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
1936{
1937 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
1938 return sprintf(buf, "%d\n", efx->phy_type);
1939}
1940static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
1941
Ben Hutchings8ceee662008-04-27 12:55:59 +01001942static int efx_register_netdev(struct efx_nic *efx)
1943{
1944 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00001945 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001946 int rc;
1947
1948 net_dev->watchdog_timeo = 5 * HZ;
1949 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001950 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001951 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
1952
Ben Hutchings7dde5962008-12-12 22:09:38 -08001953 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00001954
1955 rc = dev_alloc_name(net_dev, net_dev->name);
1956 if (rc < 0)
1957 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08001958 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00001959
1960 rc = register_netdevice(net_dev);
1961 if (rc)
1962 goto fail_locked;
1963
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00001964 efx_for_each_channel(channel, efx) {
1965 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00001966 efx_for_each_channel_tx_queue(tx_queue, channel)
1967 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00001968 }
1969
Ben Hutchingsaed06282009-08-26 08:16:27 +00001970 /* Always start with carrier off; PHY events will detect the link */
Ben Hutchings86ee5302012-01-09 19:51:22 +00001971 netif_carrier_off(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00001972
Ben Hutchings7dde5962008-12-12 22:09:38 -08001973 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01001974
Ben Hutchings06d5e192008-12-12 21:47:23 -08001975 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
1976 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001977 netif_err(efx, drv, efx->net_dev,
1978 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08001979 goto fail_registered;
1980 }
1981
Ben Hutchings8ceee662008-04-27 12:55:59 +01001982 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08001983
Ben Hutchingsaed06282009-08-26 08:16:27 +00001984fail_locked:
1985 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00001986 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00001987 return rc;
1988
Ben Hutchings06d5e192008-12-12 21:47:23 -08001989fail_registered:
1990 unregister_netdev(net_dev);
1991 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001992}
1993
1994static void efx_unregister_netdev(struct efx_nic *efx)
1995{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001996 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001997 struct efx_tx_queue *tx_queue;
1998
1999 if (!efx->net_dev)
2000 return;
2001
Ben Hutchings767e4682008-09-01 12:43:14 +01002002 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002003
2004 /* Free up any skbs still remaining. This has to happen before
2005 * we try to unregister the netdev as running their destructors
2006 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002007 efx_for_each_channel(channel, efx) {
2008 efx_for_each_channel_tx_queue(tx_queue, channel)
2009 efx_release_tx_buffers(tx_queue);
2010 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002011
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002012 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2013 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2014 unregister_netdev(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002015}
2016
2017/**************************************************************************
2018 *
2019 * Device reset and suspend
2020 *
2021 **************************************************************************/
2022
Ben Hutchings2467ca42008-09-01 12:48:50 +01002023/* Tears down the entire software state and most of the hardware state
2024 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002025void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002026{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002027 EFX_ASSERT_RESET_SERIALISED(efx);
2028
Ben Hutchings2467ca42008-09-01 12:48:50 +01002029 efx_stop_all(efx);
2030 mutex_lock(&efx->mac_lock);
2031
Ben Hutchings8ceee662008-04-27 12:55:59 +01002032 efx_fini_channels(efx);
Steve Hodgson4b988282009-01-29 17:50:51 +00002033 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2034 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002035 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002036}
2037
Ben Hutchings2467ca42008-09-01 12:48:50 +01002038/* This function will always ensure that the locks acquired in
2039 * efx_reset_down() are released. A failure return code indicates
2040 * that we were unable to reinitialise the hardware, and the
2041 * driver should be disabled. If ok is false, then the rx and tx
2042 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002043int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002044{
2045 int rc;
2046
Ben Hutchings2467ca42008-09-01 12:48:50 +01002047 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002048
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002049 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002050 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002051 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002052 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002053 }
2054
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002055 if (!ok)
2056 goto fail;
2057
Steve Hodgson4b988282009-01-29 17:50:51 +00002058 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002059 rc = efx->phy_op->init(efx);
2060 if (rc)
2061 goto fail;
2062 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002063 netif_err(efx, drv, efx->net_dev,
2064 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002065 }
2066
Ben Hutchings710b2082011-09-03 00:15:00 +01002067 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002068
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002069 efx_init_channels(efx);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002070 efx_restore_filters(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002071
2072 mutex_unlock(&efx->mac_lock);
2073
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002074 efx_start_all(efx);
2075
2076 return 0;
2077
2078fail:
2079 efx->port_initialized = false;
2080
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002081 mutex_unlock(&efx->mac_lock);
2082
Ben Hutchings8ceee662008-04-27 12:55:59 +01002083 return rc;
2084}
2085
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002086/* Reset the NIC using the specified method. Note that the reset may
2087 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002088 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002089 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002090 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002091int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002092{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002093 int rc, rc2;
2094 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002095
Ben Hutchings62776d02010-06-23 11:30:07 +00002096 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2097 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002098
Ben Hutchingse4abce82011-05-16 18:51:24 +01002099 netif_device_detach(efx->net_dev);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002100 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002101
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002102 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002103 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002104 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002105 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002106 }
2107
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002108 /* Clear flags for the scopes we covered. We assume the NIC and
2109 * driver are now quiescent so that there is no race here.
2110 */
2111 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002112
2113 /* Reinitialise bus-mastering, which may have been turned off before
2114 * the reset was scheduled. This is still appropriate, even in the
2115 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2116 * can respond to requests. */
2117 pci_set_master(efx->pci_dev);
2118
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002119out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002120 /* Leave device stopped if necessary */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002121 disabled = rc || method == RESET_TYPE_DISABLE;
2122 rc2 = efx_reset_up(efx, method, !disabled);
2123 if (rc2) {
2124 disabled = true;
2125 if (!rc)
2126 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002127 }
2128
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002129 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002130 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002131 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002132 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002133 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002134 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002135 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002136 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002137 return rc;
2138}
2139
2140/* The worker thread exists so that code that cannot sleep can
2141 * schedule a reset for later.
2142 */
2143static void efx_reset_work(struct work_struct *data)
2144{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002145 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002146 unsigned long pending = ACCESS_ONCE(efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002147
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002148 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002149 return;
2150
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002151 /* If we're not RUNNING then don't reset. Leave the reset_pending
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002152 * flags set so that efx_pci_probe_main will be retried */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002153 if (efx->state != STATE_RUNNING) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002154 netif_info(efx, drv, efx->net_dev,
2155 "scheduled reset quenched. NIC not RUNNING\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002156 return;
2157 }
2158
2159 rtnl_lock();
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002160 (void)efx_reset(efx, fls(pending) - 1);
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002161 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002162}
2163
2164void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2165{
2166 enum reset_type method;
2167
Ben Hutchings8ceee662008-04-27 12:55:59 +01002168 switch (type) {
2169 case RESET_TYPE_INVISIBLE:
2170 case RESET_TYPE_ALL:
2171 case RESET_TYPE_WORLD:
2172 case RESET_TYPE_DISABLE:
2173 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002174 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2175 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002176 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002177 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002178 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002179 netif_dbg(efx, drv, efx->net_dev,
2180 "scheduling %s reset for %s\n",
2181 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002182 break;
2183 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002184
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002185 set_bit(method, &efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002186
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002187 /* efx_process_channel() will no longer read events once a
2188 * reset is scheduled. So switch back to poll'd MCDI completions. */
2189 efx_mcdi_mode_poll(efx);
2190
Steve Hodgson1ab00622008-12-12 21:33:02 -08002191 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002192}
2193
2194/**************************************************************************
2195 *
2196 * List of NICs we support
2197 *
2198 **************************************************************************/
2199
2200/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002201static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002202 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2203 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002204 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002205 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2206 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002207 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002208 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002209 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002210 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002211 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002212 {0} /* end of list */
2213};
2214
2215/**************************************************************************
2216 *
Ben Hutchings37594332009-11-23 16:05:45 +00002217 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002218 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002219 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002220 * Needed so all function pointers are valid and do not have to be tested
2221 * before use
2222 *
2223 **************************************************************************/
2224int efx_port_dummy_op_int(struct efx_nic *efx)
2225{
2226 return 0;
2227}
2228void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002229
2230static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002231{
2232 return false;
2233}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002234
stephen hemminger6c8c2512011-04-14 05:50:12 +00002235static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002236 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002237 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002238 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002239 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002240};
2241
Ben Hutchings8ceee662008-04-27 12:55:59 +01002242/**************************************************************************
2243 *
2244 * Data housekeeping
2245 *
2246 **************************************************************************/
2247
2248/* This zeroes out and then fills in the invariants in a struct
2249 * efx_nic (including all sub-structures).
2250 */
stephen hemminger6c8c2512011-04-14 05:50:12 +00002251static int efx_init_struct(struct efx_nic *efx, const struct efx_nic_type *type,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002252 struct pci_dev *pci_dev, struct net_device *net_dev)
2253{
Ben Hutchings46426102010-09-10 06:42:33 +00002254 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002255
2256 /* Initialise common structures */
2257 memset(efx, 0, sizeof(*efx));
2258 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002259#ifdef CONFIG_SFC_MTD
2260 INIT_LIST_HEAD(&efx->mtd_list);
2261#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002262 INIT_WORK(&efx->reset_work, efx_reset_work);
2263 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
2264 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002265 efx->msg_enable = debug;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002266 efx->state = STATE_INIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002267 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002268
2269 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002270 spin_lock_init(&efx->stats_lock);
2271 mutex_init(&efx->mac_lock);
2272 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002273 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002274 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002275
2276 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002277 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2278 if (!efx->channel[i])
2279 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002280 }
2281
2282 efx->type = type;
2283
Ben Hutchings8ceee662008-04-27 12:55:59 +01002284 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2285
2286 /* Higher numbered interrupt modes are less capable! */
2287 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2288 interrupt_mode);
2289
Ben Hutchings6977dc62008-12-26 13:44:39 -08002290 /* Would be good to use the net_dev name, but we're too early */
2291 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2292 pci_name(pci_dev));
2293 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002294 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002295 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002296
Ben Hutchings8ceee662008-04-27 12:55:59 +01002297 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002298
2299fail:
2300 efx_fini_struct(efx);
2301 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002302}
2303
2304static void efx_fini_struct(struct efx_nic *efx)
2305{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002306 int i;
2307
2308 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2309 kfree(efx->channel[i]);
2310
Ben Hutchings8ceee662008-04-27 12:55:59 +01002311 if (efx->workqueue) {
2312 destroy_workqueue(efx->workqueue);
2313 efx->workqueue = NULL;
2314 }
2315}
2316
2317/**************************************************************************
2318 *
2319 * PCI interface
2320 *
2321 **************************************************************************/
2322
2323/* Main body of final NIC shutdown code
2324 * This is called only at module unload (or hotplug removal).
2325 */
2326static void efx_pci_remove_main(struct efx_nic *efx)
2327{
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002328#ifdef CONFIG_RFS_ACCEL
2329 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2330 efx->net_dev->rx_cpu_rmap = NULL;
2331#endif
Ben Hutchings152b6a62009-11-29 03:43:56 +00002332 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002333 efx_fini_channels(efx);
2334 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002335 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002336 efx_fini_napi(efx);
2337 efx_remove_all(efx);
2338}
2339
2340/* Final NIC shutdown
2341 * This is called only at module unload (or hotplug removal).
2342 */
2343static void efx_pci_remove(struct pci_dev *pci_dev)
2344{
2345 struct efx_nic *efx;
2346
2347 efx = pci_get_drvdata(pci_dev);
2348 if (!efx)
2349 return;
2350
2351 /* Mark the NIC as fini, then stop the interface */
2352 rtnl_lock();
2353 efx->state = STATE_FINI;
2354 dev_close(efx->net_dev);
2355
2356 /* Allow any queued efx_resets() to complete */
2357 rtnl_unlock();
2358
Ben Hutchings8ceee662008-04-27 12:55:59 +01002359 efx_unregister_netdev(efx);
2360
Ben Hutchings7dde5962008-12-12 22:09:38 -08002361 efx_mtd_remove(efx);
2362
Ben Hutchings8ceee662008-04-27 12:55:59 +01002363 /* Wait for any scheduled resets to complete. No more will be
2364 * scheduled from this point because efx_stop_all() has been
2365 * called, we are no longer registered with driverlink, and
2366 * the net_device's have been removed. */
Steve Hodgson1ab00622008-12-12 21:33:02 -08002367 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002368
2369 efx_pci_remove_main(efx);
2370
Ben Hutchings8ceee662008-04-27 12:55:59 +01002371 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002372 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002373
2374 pci_set_drvdata(pci_dev, NULL);
2375 efx_fini_struct(efx);
2376 free_netdev(efx->net_dev);
2377};
2378
2379/* Main body of NIC initialisation
2380 * This is called at module load (or hotplug insertion, theoretically).
2381 */
2382static int efx_pci_probe_main(struct efx_nic *efx)
2383{
2384 int rc;
2385
2386 /* Do start-of-day initialisation */
2387 rc = efx_probe_all(efx);
2388 if (rc)
2389 goto fail1;
2390
Ben Hutchingse8f14992010-12-07 19:47:34 +00002391 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002392
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002393 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002394 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002395 netif_err(efx, probe, efx->net_dev,
2396 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002397 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002398 }
2399
2400 rc = efx_init_port(efx);
2401 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002402 netif_err(efx, probe, efx->net_dev,
2403 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002404 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002405 }
2406
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01002407 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002408
Ben Hutchings152b6a62009-11-29 03:43:56 +00002409 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002410 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002411 goto fail5;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002412
2413 return 0;
2414
Ben Hutchings278c0622009-11-23 16:05:12 +00002415 fail5:
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01002416 efx_fini_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002417 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002418 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002419 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002420 fail3:
2421 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002422 efx_remove_all(efx);
2423 fail1:
2424 return rc;
2425}
2426
2427/* NIC initialisation
2428 *
2429 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002430 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002431 * sets up and registers the network devices with the kernel and hooks
2432 * the interrupt service routine. It does not prepare the device for
2433 * transmission; this is left to the first time one of the network
2434 * interfaces is brought up (i.e. efx_net_open).
2435 */
2436static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2437 const struct pci_device_id *entry)
2438{
stephen hemminger6c8c2512011-04-14 05:50:12 +00002439 const struct efx_nic_type *type = (const struct efx_nic_type *) entry->driver_data;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002440 struct net_device *net_dev;
2441 struct efx_nic *efx;
2442 int i, rc;
2443
2444 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002445 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2446 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002447 if (!net_dev)
2448 return -ENOMEM;
Ben Hutchingsc383b532009-11-29 15:11:02 +00002449 net_dev->features |= (type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002450 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002451 NETIF_F_RXCSUM);
Ben Hutchings738a8f42009-11-29 15:16:05 +00002452 if (type->offload_features & NETIF_F_V6_CSUM)
2453 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002454 /* Mask for features that also apply to VLAN devices */
2455 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002456 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2457 NETIF_F_RXCSUM);
2458 /* All offloads can be toggled */
2459 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings767e4682008-09-01 12:43:14 +01002460 efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002461 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002462 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002463 rc = efx_init_struct(efx, type, pci_dev, net_dev);
2464 if (rc)
2465 goto fail1;
2466
Ben Hutchings62776d02010-06-23 11:30:07 +00002467 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002468 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002469
2470 /* Set up basic I/O (BAR mappings etc) */
2471 rc = efx_init_io(efx);
2472 if (rc)
2473 goto fail2;
2474
2475 /* No serialisation is required with the reset path because
2476 * we're in STATE_INIT. */
2477 for (i = 0; i < 5; i++) {
2478 rc = efx_pci_probe_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002479
2480 /* Serialise against efx_reset(). No more resets will be
2481 * scheduled since efx_stop_all() has been called, and we
2482 * have not and never have been registered with either
2483 * the rtnetlink or driverlink layers. */
Steve Hodgson1ab00622008-12-12 21:33:02 -08002484 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002485
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002486 if (rc == 0) {
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002487 if (efx->reset_pending) {
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002488 /* If there was a scheduled reset during
2489 * probe, the NIC is probably hosed anyway */
2490 efx_pci_remove_main(efx);
2491 rc = -EIO;
2492 } else {
2493 break;
2494 }
2495 }
2496
Ben Hutchings8ceee662008-04-27 12:55:59 +01002497 /* Retry if a recoverably reset event has been scheduled */
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002498 if (efx->reset_pending &
2499 ~(1 << RESET_TYPE_INVISIBLE | 1 << RESET_TYPE_ALL) ||
2500 !efx->reset_pending)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002501 goto fail3;
2502
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002503 efx->reset_pending = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002504 }
2505
2506 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002507 netif_err(efx, probe, efx->net_dev, "Could not reset NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002508 goto fail4;
2509 }
2510
Ben Hutchings55edc6e2009-11-25 16:11:35 +00002511 /* Switch to the running state before we expose the device to the OS,
2512 * so that dev_open()|efx_start_all() will actually start the device */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002513 efx->state = STATE_RUNNING;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002514
Ben Hutchings8ceee662008-04-27 12:55:59 +01002515 rc = efx_register_netdev(efx);
2516 if (rc)
2517 goto fail5;
2518
Ben Hutchings62776d02010-06-23 11:30:07 +00002519 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002520
2521 rtnl_lock();
2522 efx_mtd_probe(efx); /* allowed to fail */
2523 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002524 return 0;
2525
2526 fail5:
2527 efx_pci_remove_main(efx);
2528 fail4:
2529 fail3:
2530 efx_fini_io(efx);
2531 fail2:
2532 efx_fini_struct(efx);
2533 fail1:
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002534 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002535 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002536 free_netdev(net_dev);
2537 return rc;
2538}
2539
Ben Hutchings89c758f2009-11-29 03:43:07 +00002540static int efx_pm_freeze(struct device *dev)
2541{
2542 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2543
2544 efx->state = STATE_FINI;
2545
2546 netif_device_detach(efx->net_dev);
2547
2548 efx_stop_all(efx);
2549 efx_fini_channels(efx);
2550
2551 return 0;
2552}
2553
2554static int efx_pm_thaw(struct device *dev)
2555{
2556 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2557
2558 efx->state = STATE_INIT;
2559
2560 efx_init_channels(efx);
2561
2562 mutex_lock(&efx->mac_lock);
2563 efx->phy_op->reconfigure(efx);
2564 mutex_unlock(&efx->mac_lock);
2565
2566 efx_start_all(efx);
2567
2568 netif_device_attach(efx->net_dev);
2569
2570 efx->state = STATE_RUNNING;
2571
2572 efx->type->resume_wol(efx);
2573
Steve Hodgson319ba642010-06-01 11:17:24 +00002574 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2575 queue_work(reset_workqueue, &efx->reset_work);
2576
Ben Hutchings89c758f2009-11-29 03:43:07 +00002577 return 0;
2578}
2579
2580static int efx_pm_poweroff(struct device *dev)
2581{
2582 struct pci_dev *pci_dev = to_pci_dev(dev);
2583 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2584
2585 efx->type->fini(efx);
2586
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002587 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002588
2589 pci_save_state(pci_dev);
2590 return pci_set_power_state(pci_dev, PCI_D3hot);
2591}
2592
2593/* Used for both resume and restore */
2594static int efx_pm_resume(struct device *dev)
2595{
2596 struct pci_dev *pci_dev = to_pci_dev(dev);
2597 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2598 int rc;
2599
2600 rc = pci_set_power_state(pci_dev, PCI_D0);
2601 if (rc)
2602 return rc;
2603 pci_restore_state(pci_dev);
2604 rc = pci_enable_device(pci_dev);
2605 if (rc)
2606 return rc;
2607 pci_set_master(efx->pci_dev);
2608 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2609 if (rc)
2610 return rc;
2611 rc = efx->type->init(efx);
2612 if (rc)
2613 return rc;
2614 efx_pm_thaw(dev);
2615 return 0;
2616}
2617
2618static int efx_pm_suspend(struct device *dev)
2619{
2620 int rc;
2621
2622 efx_pm_freeze(dev);
2623 rc = efx_pm_poweroff(dev);
2624 if (rc)
2625 efx_pm_resume(dev);
2626 return rc;
2627}
2628
Ben Hutchings18e83e42012-01-05 19:05:20 +00002629static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002630 .suspend = efx_pm_suspend,
2631 .resume = efx_pm_resume,
2632 .freeze = efx_pm_freeze,
2633 .thaw = efx_pm_thaw,
2634 .poweroff = efx_pm_poweroff,
2635 .restore = efx_pm_resume,
2636};
2637
Ben Hutchings8ceee662008-04-27 12:55:59 +01002638static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00002639 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002640 .id_table = efx_pci_table,
2641 .probe = efx_pci_probe,
2642 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00002643 .driver.pm = &efx_pm_ops,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002644};
2645
2646/**************************************************************************
2647 *
2648 * Kernel module interface
2649 *
2650 *************************************************************************/
2651
2652module_param(interrupt_mode, uint, 0444);
2653MODULE_PARM_DESC(interrupt_mode,
2654 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2655
2656static int __init efx_init_module(void)
2657{
2658 int rc;
2659
2660 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2661
2662 rc = register_netdevice_notifier(&efx_netdev_notifier);
2663 if (rc)
2664 goto err_notifier;
2665
Steve Hodgson1ab00622008-12-12 21:33:02 -08002666 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2667 if (!reset_workqueue) {
2668 rc = -ENOMEM;
2669 goto err_reset;
2670 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002671
2672 rc = pci_register_driver(&efx_pci_driver);
2673 if (rc < 0)
2674 goto err_pci;
2675
2676 return 0;
2677
2678 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08002679 destroy_workqueue(reset_workqueue);
2680 err_reset:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002681 unregister_netdevice_notifier(&efx_netdev_notifier);
2682 err_notifier:
2683 return rc;
2684}
2685
2686static void __exit efx_exit_module(void)
2687{
2688 printk(KERN_INFO "Solarflare NET driver unloading\n");
2689
2690 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002691 destroy_workqueue(reset_workqueue);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002692 unregister_netdevice_notifier(&efx_netdev_notifier);
2693
2694}
2695
2696module_init(efx_init_module);
2697module_exit(efx_exit_module);
2698
Ben Hutchings906bb262009-11-29 15:16:19 +00002699MODULE_AUTHOR("Solarflare Communications and "
2700 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002701MODULE_DESCRIPTION("Solarflare Communications network driver");
2702MODULE_LICENSE("GPL");
2703MODULE_DEVICE_TABLE(pci, efx_pci_table);