blob: e943ffbe5e2acd221b2f2caa6ce225eab1bb21f9 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchings0a6f40c2011-02-25 00:01:34 +00004 * Copyright 2005-2011 Solarflare Communications Inc.
Ben Hutchings8ceee662008-04-27 12:55:59 +01005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/delay.h>
16#include <linux/notifier.h>
17#include <linux/ip.h>
18#include <linux/tcp.h>
19#include <linux/in.h>
20#include <linux/crc32.h>
21#include <linux/ethtool.h>
Ben Hutchingsaa6ef272008-07-18 19:03:10 +010022#include <linux/topology.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/gfp.h>
Ben Hutchings64d8ad62011-01-05 00:50:41 +000024#include <linux/cpu_rmap.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010025#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000027#include "nic.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010028
Ben Hutchings8880f4e2009-11-29 15:15:41 +000029#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000030#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000031
Ben Hutchingsc4593022009-11-23 16:08:17 +000032/**************************************************************************
33 *
34 * Type name strings
35 *
36 **************************************************************************
37 */
38
39/* Loopback mode names (see LOOPBACK_MODE()) */
40const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000041const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000042 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000043 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000044 [LOOPBACK_GMAC] = "GMAC",
45 [LOOPBACK_XGMII] = "XGMII",
46 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000047 [LOOPBACK_XAUI] = "XAUI",
48 [LOOPBACK_GMII] = "GMII",
49 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000050 [LOOPBACK_XGBR] = "XGBR",
51 [LOOPBACK_XFI] = "XFI",
52 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
53 [LOOPBACK_GMII_FAR] = "GMII_FAR",
54 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
55 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000056 [LOOPBACK_GPHY] = "GPHY",
57 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000058 [LOOPBACK_PCS] = "PCS",
59 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000060 [LOOPBACK_XPORT] = "XPORT",
61 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000062 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000063 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
64 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000065 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000066 [LOOPBACK_XFI_WS] = "XFI_WS",
67 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000068 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000069};
70
Ben Hutchingsc4593022009-11-23 16:08:17 +000071const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000072const char *const efx_reset_type_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000073 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
74 [RESET_TYPE_ALL] = "ALL",
75 [RESET_TYPE_WORLD] = "WORLD",
76 [RESET_TYPE_DISABLE] = "DISABLE",
77 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
78 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
79 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
80 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
81 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
82 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
Ben Hutchings8880f4e2009-11-29 15:15:41 +000083 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Ben Hutchingsc4593022009-11-23 16:08:17 +000084};
85
Ben Hutchings8ceee662008-04-27 12:55:59 +010086#define EFX_MAX_MTU (9 * 1024)
87
Steve Hodgson1ab00622008-12-12 21:33:02 -080088/* Reset workqueue. If any NIC has a hardware failure then a reset will be
89 * queued onto this work queue. This is not a per-nic work queue, because
90 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
91 */
92static struct workqueue_struct *reset_workqueue;
93
Ben Hutchings8ceee662008-04-27 12:55:59 +010094/**************************************************************************
95 *
96 * Configurable values
97 *
98 *************************************************************************/
99
100/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100101 * Use separate channels for TX and RX events
102 *
Neil Turton28b581a2008-12-12 21:41:06 -0800103 * Set this to 1 to use separate channels for TX and RX. It allows us
104 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100105 *
Neil Turton28b581a2008-12-12 21:41:06 -0800106 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100107 */
Neil Turton28b581a2008-12-12 21:41:06 -0800108static unsigned int separate_tx_channels;
Ben Hutchings8313aca2010-09-10 06:41:57 +0000109module_param(separate_tx_channels, uint, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800110MODULE_PARM_DESC(separate_tx_channels,
111 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100112
113/* This is the weight assigned to each of the (per-channel) virtual
114 * NAPI devices.
115 */
116static int napi_weight = 64;
117
118/* This is the time (in jiffies) between invocations of the hardware
Ben Hutchingse254c272010-09-20 08:44:10 +0000119 * monitor. On Falcon-based NICs, this will:
120 * - Check the on-board hardware monitor;
121 * - Poll the link state and reconfigure the hardware as necessary.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100122 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000123static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100124
Ben Hutchings8ceee662008-04-27 12:55:59 +0100125/* Initial interrupt moderation settings. They can be modified after
126 * module load with ethtool.
127 *
128 * The default for RX should strike a balance between increasing the
129 * round-trip latency and reducing overhead.
130 */
131static unsigned int rx_irq_mod_usec = 60;
132
133/* Initial interrupt moderation settings. They can be modified after
134 * module load with ethtool.
135 *
136 * This default is chosen to ensure that a 10G link does not go idle
137 * while a TX queue is stopped after it has become full. A queue is
138 * restarted when it drops below half full. The time this takes (assuming
139 * worst case 3 descriptors per packet and 1024 descriptors) is
140 * 512 / 3 * 1.2 = 205 usec.
141 */
142static unsigned int tx_irq_mod_usec = 150;
143
144/* This is the first interrupt mode to try out of:
145 * 0 => MSI-X
146 * 1 => MSI
147 * 2 => legacy
148 */
149static unsigned int interrupt_mode;
150
151/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
152 * i.e. the number of CPUs among which we may distribute simultaneous
153 * interrupt handling.
154 *
155 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000156 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100157 */
158static unsigned int rss_cpus;
159module_param(rss_cpus, uint, 0444);
160MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
161
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800162static int phy_flash_cfg;
163module_param(phy_flash_cfg, int, 0644);
164MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
165
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000166static unsigned irq_adapt_low_thresh = 10000;
167module_param(irq_adapt_low_thresh, uint, 0644);
168MODULE_PARM_DESC(irq_adapt_low_thresh,
169 "Threshold score for reducing IRQ moderation");
170
171static unsigned irq_adapt_high_thresh = 20000;
172module_param(irq_adapt_high_thresh, uint, 0644);
173MODULE_PARM_DESC(irq_adapt_high_thresh,
174 "Threshold score for increasing IRQ moderation");
175
Ben Hutchings62776d02010-06-23 11:30:07 +0000176static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
177 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
178 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
179 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
180module_param(debug, uint, 0);
181MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
182
Ben Hutchings8ceee662008-04-27 12:55:59 +0100183/**************************************************************************
184 *
185 * Utility functions and prototypes
186 *
187 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000188
Ben Hutchings7f967c02012-02-13 23:45:02 +0000189static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq);
190static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq);
191static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000192static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000193static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100194static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000195static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100196static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000197static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000198static void efx_fini_struct(struct efx_nic *efx);
199static void efx_start_all(struct efx_nic *efx);
200static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100201
202#define EFX_ASSERT_RESET_SERIALISED(efx) \
203 do { \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000204 if ((efx->state == STATE_RUNNING) || \
205 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100206 ASSERT_RTNL(); \
207 } while (0)
208
209/**************************************************************************
210 *
211 * Event queue processing
212 *
213 *************************************************************************/
214
215/* Process channel's event queue
216 *
217 * This function is responsible for processing the event queue of a
218 * single channel. The caller must guarantee that this function will
219 * never be concurrently called more than once on the same channel,
220 * though different channels may be being processed concurrently.
221 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000222static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100223{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000224 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100225
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000226 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100227 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100228
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000229 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000230 if (spent && efx_channel_has_rx_queue(channel)) {
231 struct efx_rx_queue *rx_queue =
232 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100233
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000234 /* Deliver last RX packet. */
235 if (channel->rx_pkt) {
236 __efx_rx_packet(channel, channel->rx_pkt);
237 channel->rx_pkt = NULL;
238 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000239 if (rx_queue->enabled) {
240 efx_rx_strategy(channel);
241 efx_fast_push_rx_descriptors(rx_queue);
242 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100243 }
244
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000245 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100246}
247
248/* Mark channel as finished processing
249 *
250 * Note that since we will not receive further interrupts for this
251 * channel before we finish processing and call the eventq_read_ack()
252 * method, there is no need to use the interrupt hold-off timers.
253 */
254static inline void efx_channel_processed(struct efx_channel *channel)
255{
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100256 /* The interrupt handler for this channel may set work_pending
257 * as soon as we acknowledge the events we've seen. Make sure
258 * it's cleared before then. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100259 channel->work_pending = false;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100260 smp_wmb();
261
Ben Hutchings152b6a62009-11-29 03:43:56 +0000262 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100263}
264
265/* NAPI poll handler
266 *
267 * NAPI guarantees serialisation of polls of the same device, which
268 * provides the guarantee required by efx_process_channel().
269 */
270static int efx_poll(struct napi_struct *napi, int budget)
271{
272 struct efx_channel *channel =
273 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000274 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000275 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100276
Ben Hutchings62776d02010-06-23 11:30:07 +0000277 netif_vdbg(efx, intr, efx->net_dev,
278 "channel %d NAPI poll executing on CPU %d\n",
279 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100280
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000281 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100282
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000283 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000284 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000285 efx->irq_rx_adaptive &&
286 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000287 if (unlikely(channel->irq_mod_score <
288 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000289 if (channel->irq_moderation > 1) {
290 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000291 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000292 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000293 } else if (unlikely(channel->irq_mod_score >
294 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000295 if (channel->irq_moderation <
296 efx->irq_rx_moderation) {
297 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000298 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000299 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000300 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000301 channel->irq_count = 0;
302 channel->irq_mod_score = 0;
303 }
304
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000305 efx_filter_rfs_expire(channel);
306
Ben Hutchings8ceee662008-04-27 12:55:59 +0100307 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800308 * only wait for napi_complete(), this isn't a problem
Ben Hutchings8ceee662008-04-27 12:55:59 +0100309 * since efx_channel_processed() will have no effect if
310 * interrupts have already been disabled.
311 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800312 napi_complete(napi);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100313 efx_channel_processed(channel);
314 }
315
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000316 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100317}
318
319/* Process the eventq of the specified channel immediately on this CPU
320 *
321 * Disable hardware generated interrupts, wait for any existing
322 * processing to finish, then directly poll (and ack ) the eventq.
323 * Finally reenable NAPI and interrupts.
324 *
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100325 * This is for use only during a loopback self-test. It must not
326 * deliver any packets up the stack as this can result in deadlock.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100327 */
328void efx_process_channel_now(struct efx_channel *channel)
329{
330 struct efx_nic *efx = channel->efx;
331
Ben Hutchings8313aca2010-09-10 06:41:57 +0000332 BUG_ON(channel->channel >= efx->n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100333 BUG_ON(!channel->enabled);
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100334 BUG_ON(!efx->loopback_selftest);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100335
336 /* Disable interrupts and wait for ISRs to complete */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000337 efx_nic_disable_interrupts(efx);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000338 if (efx->legacy_irq) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100339 synchronize_irq(efx->legacy_irq);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000340 efx->legacy_irq_enabled = false;
341 }
Ben Hutchings64ee3122008-09-01 12:47:38 +0100342 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100343 synchronize_irq(channel->irq);
344
345 /* Wait for any NAPI processing to complete */
346 napi_disable(&channel->napi_str);
347
348 /* Poll the channel */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000349 efx_process_channel(channel, channel->eventq_mask + 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100350
351 /* Ack the eventq. This may cause an interrupt to be generated
352 * when they are reenabled */
353 efx_channel_processed(channel);
354
355 napi_enable(&channel->napi_str);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000356 if (efx->legacy_irq)
357 efx->legacy_irq_enabled = true;
Ben Hutchings152b6a62009-11-29 03:43:56 +0000358 efx_nic_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100359}
360
361/* Create event queue
362 * Event queue memory allocations are done only once. If the channel
363 * is reset, the memory buffer will be reused; this guards against
364 * errors during channel reset and also simplifies interrupt handling.
365 */
366static int efx_probe_eventq(struct efx_channel *channel)
367{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000368 struct efx_nic *efx = channel->efx;
369 unsigned long entries;
370
Ben Hutchings86ee5302012-01-09 19:51:22 +0000371 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000372 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100373
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000374 /* Build an event queue with room for one event per tx and rx buffer,
375 * plus some extra for link state events and MCDI completions. */
376 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
377 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
378 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
379
Ben Hutchings152b6a62009-11-29 03:43:56 +0000380 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100381}
382
383/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100384static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100385{
Ben Hutchings62776d02010-06-23 11:30:07 +0000386 netif_dbg(channel->efx, drv, channel->efx->net_dev,
387 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100388
389 channel->eventq_read_ptr = 0;
390
Ben Hutchings152b6a62009-11-29 03:43:56 +0000391 efx_nic_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100392}
393
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000394/* Enable event queue processing and NAPI */
395static void efx_start_eventq(struct efx_channel *channel)
396{
397 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
398 "chan %d start event queue\n", channel->channel);
399
400 /* The interrupt handler for this channel may set work_pending
401 * as soon as we enable it. Make sure it's cleared before
402 * then. Similarly, make sure it sees the enabled flag set.
403 */
404 channel->work_pending = false;
405 channel->enabled = true;
406 smp_wmb();
407
408 napi_enable(&channel->napi_str);
409 efx_nic_eventq_read_ack(channel);
410}
411
412/* Disable event queue processing and NAPI */
413static void efx_stop_eventq(struct efx_channel *channel)
414{
415 if (!channel->enabled)
416 return;
417
418 napi_disable(&channel->napi_str);
419 channel->enabled = false;
420}
421
Ben Hutchings8ceee662008-04-27 12:55:59 +0100422static void efx_fini_eventq(struct efx_channel *channel)
423{
Ben Hutchings62776d02010-06-23 11:30:07 +0000424 netif_dbg(channel->efx, drv, channel->efx->net_dev,
425 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100426
Ben Hutchings152b6a62009-11-29 03:43:56 +0000427 efx_nic_fini_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100428}
429
430static void efx_remove_eventq(struct efx_channel *channel)
431{
Ben Hutchings62776d02010-06-23 11:30:07 +0000432 netif_dbg(channel->efx, drv, channel->efx->net_dev,
433 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100434
Ben Hutchings152b6a62009-11-29 03:43:56 +0000435 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100436}
437
438/**************************************************************************
439 *
440 * Channel handling
441 *
442 *************************************************************************/
443
Ben Hutchings7f967c02012-02-13 23:45:02 +0000444/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000445static struct efx_channel *
446efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
447{
448 struct efx_channel *channel;
449 struct efx_rx_queue *rx_queue;
450 struct efx_tx_queue *tx_queue;
451 int j;
452
Ben Hutchings7f967c02012-02-13 23:45:02 +0000453 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
454 if (!channel)
455 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000456
Ben Hutchings7f967c02012-02-13 23:45:02 +0000457 channel->efx = efx;
458 channel->channel = i;
459 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000460
Ben Hutchings7f967c02012-02-13 23:45:02 +0000461 for (j = 0; j < EFX_TXQ_TYPES; j++) {
462 tx_queue = &channel->tx_queue[j];
463 tx_queue->efx = efx;
464 tx_queue->queue = i * EFX_TXQ_TYPES + j;
465 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000466 }
467
Ben Hutchings46426102010-09-10 06:42:33 +0000468 rx_queue = &channel->rx_queue;
469 rx_queue->efx = efx;
470 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
471 (unsigned long)rx_queue);
472
473 return channel;
474}
475
Ben Hutchings7f967c02012-02-13 23:45:02 +0000476/* Allocate and initialise a channel structure, copying parameters
477 * (but not resources) from an old channel structure.
478 */
479static struct efx_channel *
480efx_copy_channel(const struct efx_channel *old_channel)
481{
482 struct efx_channel *channel;
483 struct efx_rx_queue *rx_queue;
484 struct efx_tx_queue *tx_queue;
485 int j;
486
487 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
488 if (!channel)
489 return NULL;
490
491 *channel = *old_channel;
492
493 channel->napi_dev = NULL;
494 memset(&channel->eventq, 0, sizeof(channel->eventq));
495
496 for (j = 0; j < EFX_TXQ_TYPES; j++) {
497 tx_queue = &channel->tx_queue[j];
498 if (tx_queue->channel)
499 tx_queue->channel = channel;
500 tx_queue->buffer = NULL;
501 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
502 }
503
504 rx_queue = &channel->rx_queue;
505 rx_queue->buffer = NULL;
506 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
507 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
508 (unsigned long)rx_queue);
509
510 return channel;
511}
512
Ben Hutchings8ceee662008-04-27 12:55:59 +0100513static int efx_probe_channel(struct efx_channel *channel)
514{
515 struct efx_tx_queue *tx_queue;
516 struct efx_rx_queue *rx_queue;
517 int rc;
518
Ben Hutchings62776d02010-06-23 11:30:07 +0000519 netif_dbg(channel->efx, probe, channel->efx->net_dev,
520 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100521
Ben Hutchings7f967c02012-02-13 23:45:02 +0000522 rc = channel->type->pre_probe(channel);
523 if (rc)
524 goto fail;
525
Ben Hutchings8ceee662008-04-27 12:55:59 +0100526 rc = efx_probe_eventq(channel);
527 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000528 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100529
530 efx_for_each_channel_tx_queue(tx_queue, channel) {
531 rc = efx_probe_tx_queue(tx_queue);
532 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000533 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100534 }
535
536 efx_for_each_channel_rx_queue(rx_queue, channel) {
537 rc = efx_probe_rx_queue(rx_queue);
538 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000539 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100540 }
541
542 channel->n_rx_frm_trunc = 0;
543
544 return 0;
545
Ben Hutchings7f967c02012-02-13 23:45:02 +0000546fail:
547 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100548 return rc;
549}
550
Ben Hutchings7f967c02012-02-13 23:45:02 +0000551static void
552efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
553{
554 struct efx_nic *efx = channel->efx;
555 const char *type;
556 int number;
557
558 number = channel->channel;
559 if (efx->tx_channel_offset == 0) {
560 type = "";
561 } else if (channel->channel < efx->tx_channel_offset) {
562 type = "-rx";
563 } else {
564 type = "-tx";
565 number -= efx->tx_channel_offset;
566 }
567 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
568}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100569
Ben Hutchings56536e92008-12-12 21:37:02 -0800570static void efx_set_channel_names(struct efx_nic *efx)
571{
572 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800573
Ben Hutchings7f967c02012-02-13 23:45:02 +0000574 efx_for_each_channel(channel, efx)
575 channel->type->get_name(channel,
576 efx->channel_name[channel->channel],
577 sizeof(efx->channel_name[0]));
Ben Hutchings56536e92008-12-12 21:37:02 -0800578}
579
Ben Hutchings46426102010-09-10 06:42:33 +0000580static int efx_probe_channels(struct efx_nic *efx)
581{
582 struct efx_channel *channel;
583 int rc;
584
585 /* Restart special buffer allocation */
586 efx->next_buffer_table = 0;
587
588 efx_for_each_channel(channel, efx) {
589 rc = efx_probe_channel(channel);
590 if (rc) {
591 netif_err(efx, probe, efx->net_dev,
592 "failed to create channel %d\n",
593 channel->channel);
594 goto fail;
595 }
596 }
597 efx_set_channel_names(efx);
598
599 return 0;
600
601fail:
602 efx_remove_channels(efx);
603 return rc;
604}
605
Ben Hutchings8ceee662008-04-27 12:55:59 +0100606/* Channels are shutdown and reinitialised whilst the NIC is running
607 * to propagate configuration changes (mtu, checksum offload), or
608 * to clear hardware error conditions
609 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000610static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100611{
612 struct efx_tx_queue *tx_queue;
613 struct efx_rx_queue *rx_queue;
614 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100615
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100616 /* Calculate the rx buffer allocation parameters required to
617 * support the current MTU, including padding for header
618 * alignment and overruns.
619 */
620 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
621 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
Ben Hutchings39c9cf02010-06-23 11:31:28 +0000622 efx->type->rx_buffer_hash_size +
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100623 efx->type->rx_buffer_padding);
Steve Hodgson62b330b2010-06-01 11:20:53 +0000624 efx->rx_buffer_order = get_order(efx->rx_buffer_len +
625 sizeof(struct efx_rx_page_state));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100626
627 /* Initialise the channels */
628 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100629 efx_for_each_channel_tx_queue(tx_queue, channel)
630 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100631
632 /* The rx buffer allocation strategy is MTU dependent */
633 efx_rx_strategy(channel);
634
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000635 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100636 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000637 efx_nic_generate_fill_event(rx_queue);
638 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100639
640 WARN_ON(channel->rx_pkt != NULL);
641 efx_rx_strategy(channel);
642 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000643
644 if (netif_device_present(efx->net_dev))
645 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100646}
647
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000648static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100649{
650 struct efx_channel *channel;
651 struct efx_tx_queue *tx_queue;
652 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100653 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100654
655 EFX_ASSERT_RESET_SERIALISED(efx);
656 BUG_ON(efx->port_enabled);
657
Ben Hutchings152b6a62009-11-29 03:43:56 +0000658 rc = efx_nic_flush_queues(efx);
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000659 if (rc && EFX_WORKAROUND_7803(efx)) {
660 /* Schedule a reset to recover from the flush failure. The
661 * descriptor caches reference memory we're about to free,
662 * but falcon_reconfigure_mac_wrapper() won't reconnect
663 * the MACs because of the pending reset. */
Ben Hutchings62776d02010-06-23 11:30:07 +0000664 netif_err(efx, drv, efx->net_dev,
665 "Resetting to recover from flush failure\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000666 efx_schedule_reset(efx, RESET_TYPE_ALL);
667 } else if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000668 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000669 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +0000670 netif_dbg(efx, drv, efx->net_dev,
671 "successfully flushed all queues\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000672 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100673
Ben Hutchings8ceee662008-04-27 12:55:59 +0100674 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000675 /* RX packet processing is pipelined, so wait for the
676 * NAPI handler to complete. At least event queue 0
677 * might be kept active by non-data events, so don't
678 * use napi_synchronize() but actually disable NAPI
679 * temporarily.
680 */
681 if (efx_channel_has_rx_queue(channel)) {
682 efx_stop_eventq(channel);
683 efx_start_eventq(channel);
684 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100685
686 efx_for_each_channel_rx_queue(rx_queue, channel)
687 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000688 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100689 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100690 }
691}
692
693static void efx_remove_channel(struct efx_channel *channel)
694{
695 struct efx_tx_queue *tx_queue;
696 struct efx_rx_queue *rx_queue;
697
Ben Hutchings62776d02010-06-23 11:30:07 +0000698 netif_dbg(channel->efx, drv, channel->efx->net_dev,
699 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100700
701 efx_for_each_channel_rx_queue(rx_queue, channel)
702 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000703 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100704 efx_remove_tx_queue(tx_queue);
705 efx_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100706}
707
Ben Hutchings46426102010-09-10 06:42:33 +0000708static void efx_remove_channels(struct efx_nic *efx)
709{
710 struct efx_channel *channel;
711
712 efx_for_each_channel(channel, efx)
713 efx_remove_channel(channel);
714}
715
716int
717efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
718{
719 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
720 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000721 unsigned i, next_buffer_table = 0;
722 int rc = 0;
723
724 /* Not all channels should be reallocated. We must avoid
725 * reallocating their buffer table entries.
726 */
727 efx_for_each_channel(channel, efx) {
728 struct efx_rx_queue *rx_queue;
729 struct efx_tx_queue *tx_queue;
730
731 if (channel->type->copy)
732 continue;
733 next_buffer_table = max(next_buffer_table,
734 channel->eventq.index +
735 channel->eventq.entries);
736 efx_for_each_channel_rx_queue(rx_queue, channel)
737 next_buffer_table = max(next_buffer_table,
738 rx_queue->rxd.index +
739 rx_queue->rxd.entries);
740 efx_for_each_channel_tx_queue(tx_queue, channel)
741 next_buffer_table = max(next_buffer_table,
742 tx_queue->txd.index +
743 tx_queue->txd.entries);
744 }
Ben Hutchings46426102010-09-10 06:42:33 +0000745
746 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000747 efx_stop_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000748
Ben Hutchings7f967c02012-02-13 23:45:02 +0000749 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000750 memset(other_channel, 0, sizeof(other_channel));
751 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000752 channel = efx->channel[i];
753 if (channel->type->copy)
754 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000755 if (!channel) {
756 rc = -ENOMEM;
757 goto out;
758 }
759 other_channel[i] = channel;
760 }
761
762 /* Swap entry counts and channel pointers */
763 old_rxq_entries = efx->rxq_entries;
764 old_txq_entries = efx->txq_entries;
765 efx->rxq_entries = rxq_entries;
766 efx->txq_entries = txq_entries;
767 for (i = 0; i < efx->n_channels; i++) {
768 channel = efx->channel[i];
769 efx->channel[i] = other_channel[i];
770 other_channel[i] = channel;
771 }
772
Ben Hutchings7f967c02012-02-13 23:45:02 +0000773 /* Restart buffer table allocation */
774 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000775
Ben Hutchingse8f14992010-12-07 19:47:34 +0000776 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000777 channel = efx->channel[i];
778 if (!channel->type->copy)
779 continue;
780 rc = efx_probe_channel(channel);
781 if (rc)
782 goto rollback;
783 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000784 }
Ben Hutchings46426102010-09-10 06:42:33 +0000785
Ben Hutchings7f967c02012-02-13 23:45:02 +0000786out:
787 /* Destroy unused channel structures */
788 for (i = 0; i < efx->n_channels; i++) {
789 channel = other_channel[i];
790 if (channel && channel->type->copy) {
791 efx_fini_napi_channel(channel);
792 efx_remove_channel(channel);
793 kfree(channel);
794 }
795 }
796
797 efx_start_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000798 efx_start_all(efx);
799 return rc;
800
801rollback:
802 /* Swap back */
803 efx->rxq_entries = old_rxq_entries;
804 efx->txq_entries = old_txq_entries;
805 for (i = 0; i < efx->n_channels; i++) {
806 channel = efx->channel[i];
807 efx->channel[i] = other_channel[i];
808 other_channel[i] = channel;
809 }
810 goto out;
811}
812
Steve Hodgson90d683a2010-06-01 11:19:39 +0000813void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100814{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000815 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100816}
817
Ben Hutchings7f967c02012-02-13 23:45:02 +0000818static const struct efx_channel_type efx_default_channel_type = {
819 .pre_probe = efx_channel_dummy_op_int,
820 .get_name = efx_get_channel_name,
821 .copy = efx_copy_channel,
822 .keep_eventq = false,
823};
824
825int efx_channel_dummy_op_int(struct efx_channel *channel)
826{
827 return 0;
828}
829
Ben Hutchings8ceee662008-04-27 12:55:59 +0100830/**************************************************************************
831 *
832 * Port handling
833 *
834 **************************************************************************/
835
836/* This ensures that the kernel is kept informed (via
837 * netif_carrier_on/off) of the link status, and also maintains the
838 * link status's stop on the port's TX queue.
839 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000840void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100841{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000842 struct efx_link_state *link_state = &efx->link_state;
843
Ben Hutchings8ceee662008-04-27 12:55:59 +0100844 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
845 * that no events are triggered between unregister_netdev() and the
846 * driver unloading. A more general condition is that NETDEV_CHANGE
847 * can only be generated between NETDEV_UP and NETDEV_DOWN */
848 if (!netif_running(efx->net_dev))
849 return;
850
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000851 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100852 efx->n_link_state_changes++;
853
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000854 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100855 netif_carrier_on(efx->net_dev);
856 else
857 netif_carrier_off(efx->net_dev);
858 }
859
860 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000861 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000862 netif_info(efx, link, efx->net_dev,
863 "link up at %uMbps %s-duplex (MTU %d)%s\n",
864 link_state->speed, link_state->fd ? "full" : "half",
865 efx->net_dev->mtu,
866 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000867 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000868 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100869}
870
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000871void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
872{
873 efx->link_advertising = advertising;
874 if (advertising) {
875 if (advertising & ADVERTISED_Pause)
876 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
877 else
878 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
879 if (advertising & ADVERTISED_Asym_Pause)
880 efx->wanted_fc ^= EFX_FC_TX;
881 }
882}
883
David S. Millerb56269462011-05-17 17:53:22 -0400884void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000885{
886 efx->wanted_fc = wanted_fc;
887 if (efx->link_advertising) {
888 if (wanted_fc & EFX_FC_RX)
889 efx->link_advertising |= (ADVERTISED_Pause |
890 ADVERTISED_Asym_Pause);
891 else
892 efx->link_advertising &= ~(ADVERTISED_Pause |
893 ADVERTISED_Asym_Pause);
894 if (wanted_fc & EFX_FC_TX)
895 efx->link_advertising ^= ADVERTISED_Asym_Pause;
896 }
897}
898
Ben Hutchings115122a2009-03-04 09:52:52 +0000899static void efx_fini_port(struct efx_nic *efx);
900
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000901/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
902 * the MAC appropriately. All other PHY configuration changes are pushed
903 * through phy_op->set_settings(), and pushed asynchronously to the MAC
904 * through efx_monitor().
905 *
906 * Callers must hold the mac_lock
907 */
908int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100909{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000910 enum efx_phy_mode phy_mode;
911 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100912
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000913 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100914
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000915 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000916 netif_addr_lock_bh(efx->net_dev);
917 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100918
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000919 /* Disable PHY transmit in mac level loopbacks */
920 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800921 if (LOOPBACK_INTERNAL(efx))
922 efx->phy_mode |= PHY_MODE_TX_DISABLED;
923 else
924 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800925
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000926 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800927
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000928 if (rc)
929 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100930
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000931 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100932}
933
934/* Reinitialise the MAC to pick up new PHY settings, even if the port is
935 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000936int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100937{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000938 int rc;
939
Ben Hutchings8ceee662008-04-27 12:55:59 +0100940 EFX_ASSERT_RESET_SERIALISED(efx);
941
942 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000943 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100944 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000945
946 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100947}
948
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000949/* Asynchronous work item for changing MAC promiscuity and multicast
950 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
951 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800952static void efx_mac_work(struct work_struct *data)
953{
954 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
955
956 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100957 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100958 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800959 mutex_unlock(&efx->mac_lock);
960}
961
Ben Hutchings8ceee662008-04-27 12:55:59 +0100962static int efx_probe_port(struct efx_nic *efx)
963{
964 int rc;
965
Ben Hutchings62776d02010-06-23 11:30:07 +0000966 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100967
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000968 if (phy_flash_cfg)
969 efx->phy_mode = PHY_MODE_SPECIAL;
970
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000971 /* Connect up MAC/PHY operations table */
972 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100973 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000974 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100975
Ben Hutchingse332bcb2011-12-20 01:22:51 +0000976 /* Initialise MAC address to permanent address */
977 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100978
979 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100980}
981
982static int efx_init_port(struct efx_nic *efx)
983{
984 int rc;
985
Ben Hutchings62776d02010-06-23 11:30:07 +0000986 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100987
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000988 mutex_lock(&efx->mac_lock);
989
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800990 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100991 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000992 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100993
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100994 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000995
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000996 /* Reconfigure the MAC before creating dma queues (required for
997 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +0100998 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000999
1000 /* Ensure the PHY advertises the correct flow control settings */
1001 rc = efx->phy_op->reconfigure(efx);
1002 if (rc)
1003 goto fail2;
1004
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001005 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001006 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001007
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001008fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001009 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001010fail1:
1011 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001012 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001013}
1014
Ben Hutchings8ceee662008-04-27 12:55:59 +01001015static void efx_start_port(struct efx_nic *efx)
1016{
Ben Hutchings62776d02010-06-23 11:30:07 +00001017 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001018 BUG_ON(efx->port_enabled);
1019
1020 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001021 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001022
1023 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1024 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001025 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001026
Ben Hutchings8ceee662008-04-27 12:55:59 +01001027 mutex_unlock(&efx->mac_lock);
1028}
1029
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001030/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001031static void efx_stop_port(struct efx_nic *efx)
1032{
Ben Hutchings62776d02010-06-23 11:30:07 +00001033 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001034
1035 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001036 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001037 mutex_unlock(&efx->mac_lock);
1038
1039 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001040 netif_addr_lock_bh(efx->net_dev);
1041 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001042}
1043
1044static void efx_fini_port(struct efx_nic *efx)
1045{
Ben Hutchings62776d02010-06-23 11:30:07 +00001046 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001047
1048 if (!efx->port_initialized)
1049 return;
1050
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001051 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001052 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001053
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001054 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001055 efx_link_status_changed(efx);
1056}
1057
1058static void efx_remove_port(struct efx_nic *efx)
1059{
Ben Hutchings62776d02010-06-23 11:30:07 +00001060 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001061
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001062 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001063}
1064
1065/**************************************************************************
1066 *
1067 * NIC handling
1068 *
1069 **************************************************************************/
1070
1071/* This configures the PCI device to enable I/O and DMA. */
1072static int efx_init_io(struct efx_nic *efx)
1073{
1074 struct pci_dev *pci_dev = efx->pci_dev;
1075 dma_addr_t dma_mask = efx->type->max_dma_mask;
1076 int rc;
1077
Ben Hutchings62776d02010-06-23 11:30:07 +00001078 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001079
1080 rc = pci_enable_device(pci_dev);
1081 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001082 netif_err(efx, probe, efx->net_dev,
1083 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001084 goto fail1;
1085 }
1086
1087 pci_set_master(pci_dev);
1088
1089 /* Set the PCI DMA mask. Try all possibilities from our
1090 * genuine mask down to 32 bits, because some architectures
1091 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1092 * masks event though they reject 46 bit masks.
1093 */
1094 while (dma_mask > 0x7fffffffUL) {
Ben Hutchingse9e01842012-01-05 18:50:29 +00001095 if (pci_dma_supported(pci_dev, dma_mask)) {
1096 rc = pci_set_dma_mask(pci_dev, dma_mask);
1097 if (rc == 0)
1098 break;
1099 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001100 dma_mask >>= 1;
1101 }
1102 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001103 netif_err(efx, probe, efx->net_dev,
1104 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001105 goto fail2;
1106 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001107 netif_dbg(efx, probe, efx->net_dev,
1108 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001109 rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
1110 if (rc) {
1111 /* pci_set_consistent_dma_mask() is not *allowed* to
1112 * fail with a mask that pci_set_dma_mask() accepted,
1113 * but just in case...
1114 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001115 netif_err(efx, probe, efx->net_dev,
1116 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001117 goto fail2;
1118 }
1119
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001120 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1121 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001122 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001123 netif_err(efx, probe, efx->net_dev,
1124 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001125 rc = -EIO;
1126 goto fail3;
1127 }
David S. Miller8decf862011-09-22 03:23:13 -04001128 efx->membase = ioremap_nocache(efx->membase_phys,
1129 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001130 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001131 netif_err(efx, probe, efx->net_dev,
1132 "could not map memory BAR at %llx+%x\n",
1133 (unsigned long long)efx->membase_phys,
1134 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001135 rc = -ENOMEM;
1136 goto fail4;
1137 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001138 netif_dbg(efx, probe, efx->net_dev,
1139 "memory BAR at %llx+%x (virtual %p)\n",
1140 (unsigned long long)efx->membase_phys,
1141 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001142
1143 return 0;
1144
1145 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001146 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001147 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001148 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001149 fail2:
1150 pci_disable_device(efx->pci_dev);
1151 fail1:
1152 return rc;
1153}
1154
1155static void efx_fini_io(struct efx_nic *efx)
1156{
Ben Hutchings62776d02010-06-23 11:30:07 +00001157 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001158
1159 if (efx->membase) {
1160 iounmap(efx->membase);
1161 efx->membase = NULL;
1162 }
1163
1164 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001165 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001166 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001167 }
1168
1169 pci_disable_device(efx->pci_dev);
1170}
1171
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001172static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001173{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001174 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001175 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001176 int cpu;
1177
Ben Hutchings5b874e22011-01-12 19:11:05 +00001178 if (rss_cpus)
1179 return rss_cpus;
1180
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001181 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001182 netif_warn(efx, probe, efx->net_dev,
1183 "RSS disabled due to allocation failure\n");
Rusty Russell2f8975f2009-01-10 21:58:09 -08001184 return 1;
1185 }
1186
Ben Hutchings46123d02008-09-01 12:47:33 +01001187 count = 0;
1188 for_each_online_cpu(cpu) {
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001189 if (!cpumask_test_cpu(cpu, thread_mask)) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001190 ++count;
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001191 cpumask_or(thread_mask, thread_mask,
1192 topology_thread_cpumask(cpu));
Ben Hutchings46123d02008-09-01 12:47:33 +01001193 }
1194 }
1195
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001196 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001197 return count;
1198}
1199
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001200static int
1201efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1202{
1203#ifdef CONFIG_RFS_ACCEL
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001204 unsigned int i;
1205 int rc;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001206
1207 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1208 if (!efx->net_dev->rx_cpu_rmap)
1209 return -ENOMEM;
1210 for (i = 0; i < efx->n_rx_channels; i++) {
1211 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1212 xentries[i].vector);
1213 if (rc) {
1214 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1215 efx->net_dev->rx_cpu_rmap = NULL;
1216 return rc;
1217 }
1218 }
1219#endif
1220 return 0;
1221}
1222
Ben Hutchings46123d02008-09-01 12:47:33 +01001223/* Probe the number and type of interrupts we are able to obtain, and
1224 * the resulting numbers of channels and RX queues.
1225 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001226static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001227{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001228 unsigned int max_channels =
1229 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001230 unsigned int extra_channels = 0;
1231 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001232 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001233
Ben Hutchings7f967c02012-02-13 23:45:02 +00001234 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1235 if (efx->extra_channel_type[i])
1236 ++extra_channels;
1237
Ben Hutchings8ceee662008-04-27 12:55:59 +01001238 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001239 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001240 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001241
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001242 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001243 if (separate_tx_channels)
1244 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001245 n_channels += extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001246 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001247
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001248 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001249 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001250 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001251 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001252 netif_err(efx, drv, efx->net_dev,
1253 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001254 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001255 netif_err(efx, drv, efx->net_dev,
1256 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001257 EFX_BUG_ON_PARANOID(rc >= n_channels);
1258 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001259 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001260 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001261 }
1262
1263 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001264 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001265 if (n_channels > extra_channels)
1266 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001267 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001268 efx->n_tx_channels = max(n_channels / 2, 1U);
1269 efx->n_rx_channels = max(n_channels -
1270 efx->n_tx_channels,
1271 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001272 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001273 efx->n_tx_channels = n_channels;
1274 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001275 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001276 rc = efx_init_rx_cpu_rmap(efx, xentries);
1277 if (rc) {
1278 pci_disable_msix(efx->pci_dev);
1279 return rc;
1280 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001281 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001282 efx_get_channel(efx, i)->irq =
1283 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001284 } else {
1285 /* Fall back to single channel MSI */
1286 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001287 netif_err(efx, drv, efx->net_dev,
1288 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001289 }
1290 }
1291
1292 /* Try single interrupt MSI */
1293 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001294 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001295 efx->n_rx_channels = 1;
1296 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001297 rc = pci_enable_msi(efx->pci_dev);
1298 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001299 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001300 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001301 netif_err(efx, drv, efx->net_dev,
1302 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001303 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1304 }
1305 }
1306
1307 /* Assume legacy interrupts */
1308 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001309 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001310 efx->n_rx_channels = 1;
1311 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001312 efx->legacy_irq = efx->pci_dev->irq;
1313 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001314
Ben Hutchings7f967c02012-02-13 23:45:02 +00001315 /* Assign extra channels if possible */
1316 j = efx->n_channels;
1317 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1318 if (!efx->extra_channel_type[i])
1319 continue;
1320 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1321 efx->n_channels <= extra_channels) {
1322 efx->extra_channel_type[i]->handle_no_channel(efx);
1323 } else {
1324 --j;
1325 efx_get_channel(efx, j)->type =
1326 efx->extra_channel_type[i];
1327 }
1328 }
1329
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001330 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001331}
1332
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001333/* Enable interrupts, then probe and start the event queues */
Ben Hutchings7f967c02012-02-13 23:45:02 +00001334static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001335{
1336 struct efx_channel *channel;
1337
1338 if (efx->legacy_irq)
1339 efx->legacy_irq_enabled = true;
1340 efx_nic_enable_interrupts(efx);
1341
1342 efx_for_each_channel(channel, efx) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001343 if (!channel->type->keep_eventq || !may_keep_eventq)
1344 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001345 efx_start_eventq(channel);
1346 }
1347
1348 efx_mcdi_mode_event(efx);
1349}
1350
Ben Hutchings7f967c02012-02-13 23:45:02 +00001351static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001352{
1353 struct efx_channel *channel;
1354
1355 efx_mcdi_mode_poll(efx);
1356
1357 efx_nic_disable_interrupts(efx);
1358 if (efx->legacy_irq) {
1359 synchronize_irq(efx->legacy_irq);
1360 efx->legacy_irq_enabled = false;
1361 }
1362
1363 efx_for_each_channel(channel, efx) {
1364 if (channel->irq)
1365 synchronize_irq(channel->irq);
1366
1367 efx_stop_eventq(channel);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001368 if (!channel->type->keep_eventq || !may_keep_eventq)
1369 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001370 }
1371}
1372
Ben Hutchings8ceee662008-04-27 12:55:59 +01001373static void efx_remove_interrupts(struct efx_nic *efx)
1374{
1375 struct efx_channel *channel;
1376
1377 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001378 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001379 channel->irq = 0;
1380 pci_disable_msi(efx->pci_dev);
1381 pci_disable_msix(efx->pci_dev);
1382
1383 /* Remove legacy interrupt */
1384 efx->legacy_irq = 0;
1385}
1386
Ben Hutchings8831da72008-09-01 12:47:48 +01001387static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001388{
Ben Hutchings602a5322011-05-16 17:32:39 +01001389 struct efx_channel *channel;
1390 struct efx_tx_queue *tx_queue;
1391
Ben Hutchings97653432011-01-12 18:26:56 +00001392 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001393 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001394
1395 /* We need to adjust the TX queue numbers if we have separate
1396 * RX-only and TX-only channels.
1397 */
1398 efx_for_each_channel(channel, efx) {
1399 efx_for_each_channel_tx_queue(tx_queue, channel)
1400 tx_queue->queue -= (efx->tx_channel_offset *
1401 EFX_TXQ_TYPES);
1402 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001403}
1404
1405static int efx_probe_nic(struct efx_nic *efx)
1406{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001407 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001408 int rc;
1409
Ben Hutchings62776d02010-06-23 11:30:07 +00001410 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001411
1412 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001413 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001414 if (rc)
1415 return rc;
1416
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001417 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001418 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001419 rc = efx_probe_interrupts(efx);
1420 if (rc)
1421 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001422
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001423 if (efx->n_channels > 1)
1424 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001425 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001426 efx->rx_indir_table[i] =
1427 ethtool_rxfh_indir_default(i, efx->n_rx_channels);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001428
Ben Hutchings8831da72008-09-01 12:47:48 +01001429 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001430 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1431 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001432
1433 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001434 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1435 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001436
1437 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001438
1439fail:
1440 efx->type->remove(efx);
1441 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001442}
1443
1444static void efx_remove_nic(struct efx_nic *efx)
1445{
Ben Hutchings62776d02010-06-23 11:30:07 +00001446 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001447
1448 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001449 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001450}
1451
1452/**************************************************************************
1453 *
1454 * NIC startup/shutdown
1455 *
1456 *************************************************************************/
1457
1458static int efx_probe_all(struct efx_nic *efx)
1459{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001460 int rc;
1461
Ben Hutchings8ceee662008-04-27 12:55:59 +01001462 rc = efx_probe_nic(efx);
1463 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001464 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001465 goto fail1;
1466 }
1467
Ben Hutchings8ceee662008-04-27 12:55:59 +01001468 rc = efx_probe_port(efx);
1469 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001470 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001471 goto fail2;
1472 }
1473
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001474 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001475
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001476 rc = efx_probe_filters(efx);
1477 if (rc) {
1478 netif_err(efx, probe, efx->net_dev,
1479 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001480 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001481 }
1482
Ben Hutchings7f967c02012-02-13 23:45:02 +00001483 rc = efx_probe_channels(efx);
1484 if (rc)
1485 goto fail4;
1486
Ben Hutchings8ceee662008-04-27 12:55:59 +01001487 return 0;
1488
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001489 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001490 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001491 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001492 efx_remove_port(efx);
1493 fail2:
1494 efx_remove_nic(efx);
1495 fail1:
1496 return rc;
1497}
1498
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001499/* Called after previous invocation(s) of efx_stop_all, restarts the port,
1500 * kernel transmit queues and NAPI processing, and ensures that the port is
1501 * scheduled to be reconfigured. This function is safe to call multiple
1502 * times when the NIC is in any state.
1503 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001504static void efx_start_all(struct efx_nic *efx)
1505{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001506 EFX_ASSERT_RESET_SERIALISED(efx);
1507
1508 /* Check that it is appropriate to restart the interface. All
1509 * of these flags are safe to read under just the rtnl lock */
1510 if (efx->port_enabled)
1511 return;
1512 if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1513 return;
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001514 if (!netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001515 return;
1516
Ben Hutchings8ceee662008-04-27 12:55:59 +01001517 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001518 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001519
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001520 /* Start the hardware monitor if there is one. Otherwise (we're link
1521 * event driven), we have to poll the PHY because after an event queue
1522 * flush, we could have a missed a link state change */
1523 if (efx->type->monitor != NULL) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001524 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1525 efx_monitor_interval);
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001526 } else {
1527 mutex_lock(&efx->mac_lock);
1528 if (efx->phy_op->poll(efx))
1529 efx_link_status_changed(efx);
1530 mutex_unlock(&efx->mac_lock);
1531 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001532
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001533 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001534}
1535
1536/* Flush all delayed work. Should only be called when no more delayed work
1537 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1538 * since we're holding the rtnl_lock at this point. */
1539static void efx_flush_all(struct efx_nic *efx)
1540{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001541 /* Make sure the hardware monitor is stopped */
1542 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001543 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001544 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001545}
1546
1547/* Quiesce hardware and software without bringing the link down.
1548 * Safe to call multiple times, when the nic and interface is in any
1549 * state. The caller is guaranteed to subsequently be in a position
1550 * to modify any hardware and software state they see fit without
1551 * taking locks. */
1552static void efx_stop_all(struct efx_nic *efx)
1553{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001554 EFX_ASSERT_RESET_SERIALISED(efx);
1555
1556 /* port_enabled can be read safely under the rtnl lock */
1557 if (!efx->port_enabled)
1558 return;
1559
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001560 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001561 efx_stop_port(efx);
1562
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001563 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001564 efx_flush_all(efx);
1565
Ben Hutchings8ceee662008-04-27 12:55:59 +01001566 /* Stop the kernel transmit interface late, so the watchdog
1567 * timer isn't ticking over the flush */
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001568 netif_tx_disable(efx->net_dev);
1569
1570 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001571}
1572
1573static void efx_remove_all(struct efx_nic *efx)
1574{
Ben Hutchings46426102010-09-10 06:42:33 +00001575 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001576 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001577 efx_remove_port(efx);
1578 efx_remove_nic(efx);
1579}
1580
Ben Hutchings8ceee662008-04-27 12:55:59 +01001581/**************************************************************************
1582 *
1583 * Interrupt moderation
1584 *
1585 **************************************************************************/
1586
Ben Hutchingscc180b62011-12-08 19:51:47 +00001587static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001588{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001589 if (usecs == 0)
1590 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001591 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001592 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001593 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001594}
1595
Ben Hutchings8ceee662008-04-27 12:55:59 +01001596/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001597int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1598 unsigned int rx_usecs, bool rx_adaptive,
1599 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001600{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001601 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001602 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1603 efx->timer_quantum_ns,
1604 1000);
1605 unsigned int tx_ticks;
1606 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001607
1608 EFX_ASSERT_RESET_SERIALISED(efx);
1609
Ben Hutchingscc180b62011-12-08 19:51:47 +00001610 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001611 return -EINVAL;
1612
Ben Hutchingscc180b62011-12-08 19:51:47 +00001613 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1614 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1615
Ben Hutchings9e393b32011-09-05 07:43:04 +00001616 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1617 !rx_may_override_tx) {
1618 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1619 "RX and TX IRQ moderation must be equal\n");
1620 return -EINVAL;
1621 }
1622
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001623 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001624 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001625 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001626 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001627 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001628 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001629 channel->irq_moderation = tx_ticks;
1630 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001631
1632 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001633}
1634
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001635void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1636 unsigned int *rx_usecs, bool *rx_adaptive)
1637{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001638 /* We must round up when converting ticks to microseconds
1639 * because we round down when converting the other way.
1640 */
1641
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001642 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001643 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1644 efx->timer_quantum_ns,
1645 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001646
1647 /* If channels are shared between RX and TX, so is IRQ
1648 * moderation. Otherwise, IRQ moderation is the same for all
1649 * TX channels and is not adaptive.
1650 */
1651 if (efx->tx_channel_offset == 0)
1652 *tx_usecs = *rx_usecs;
1653 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001654 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001655 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001656 efx->timer_quantum_ns,
1657 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001658}
1659
Ben Hutchings8ceee662008-04-27 12:55:59 +01001660/**************************************************************************
1661 *
1662 * Hardware monitor
1663 *
1664 **************************************************************************/
1665
Ben Hutchingse254c272010-09-20 08:44:10 +00001666/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001667static void efx_monitor(struct work_struct *data)
1668{
1669 struct efx_nic *efx = container_of(data, struct efx_nic,
1670 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001671
Ben Hutchings62776d02010-06-23 11:30:07 +00001672 netif_vdbg(efx, timer, efx->net_dev,
1673 "hardware monitor executing on CPU %d\n",
1674 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001675 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001676
Ben Hutchings8ceee662008-04-27 12:55:59 +01001677 /* If the mac_lock is already held then it is likely a port
1678 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001679 * most of the work of monitor() anyway. */
1680 if (mutex_trylock(&efx->mac_lock)) {
1681 if (efx->port_enabled)
1682 efx->type->monitor(efx);
1683 mutex_unlock(&efx->mac_lock);
1684 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001685
Ben Hutchings8ceee662008-04-27 12:55:59 +01001686 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1687 efx_monitor_interval);
1688}
1689
1690/**************************************************************************
1691 *
1692 * ioctls
1693 *
1694 *************************************************************************/
1695
1696/* Net device ioctl
1697 * Context: process, rtnl_lock() held.
1698 */
1699static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1700{
Ben Hutchings767e4682008-09-01 12:43:14 +01001701 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001702 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001703
1704 EFX_ASSERT_RESET_SERIALISED(efx);
1705
Ben Hutchings68e7f452009-04-29 08:05:08 +00001706 /* Convert phy_id from older PRTAD/DEVAD format */
1707 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1708 (data->phy_id & 0xfc00) == 0x0400)
1709 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1710
1711 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001712}
1713
1714/**************************************************************************
1715 *
1716 * NAPI interface
1717 *
1718 **************************************************************************/
1719
Ben Hutchings7f967c02012-02-13 23:45:02 +00001720static void efx_init_napi_channel(struct efx_channel *channel)
1721{
1722 struct efx_nic *efx = channel->efx;
1723
1724 channel->napi_dev = efx->net_dev;
1725 netif_napi_add(channel->napi_dev, &channel->napi_str,
1726 efx_poll, napi_weight);
1727}
1728
Ben Hutchingse8f14992010-12-07 19:47:34 +00001729static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001730{
1731 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001732
Ben Hutchings7f967c02012-02-13 23:45:02 +00001733 efx_for_each_channel(channel, efx)
1734 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001735}
1736
1737static void efx_fini_napi_channel(struct efx_channel *channel)
1738{
1739 if (channel->napi_dev)
1740 netif_napi_del(&channel->napi_str);
1741 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001742}
1743
1744static void efx_fini_napi(struct efx_nic *efx)
1745{
1746 struct efx_channel *channel;
1747
Ben Hutchingse8f14992010-12-07 19:47:34 +00001748 efx_for_each_channel(channel, efx)
1749 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001750}
1751
1752/**************************************************************************
1753 *
1754 * Kernel netpoll interface
1755 *
1756 *************************************************************************/
1757
1758#ifdef CONFIG_NET_POLL_CONTROLLER
1759
1760/* Although in the common case interrupts will be disabled, this is not
1761 * guaranteed. However, all our work happens inside the NAPI callback,
1762 * so no locking is required.
1763 */
1764static void efx_netpoll(struct net_device *net_dev)
1765{
Ben Hutchings767e4682008-09-01 12:43:14 +01001766 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001767 struct efx_channel *channel;
1768
Ben Hutchings64ee3122008-09-01 12:47:38 +01001769 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001770 efx_schedule_channel(channel);
1771}
1772
1773#endif
1774
1775/**************************************************************************
1776 *
1777 * Kernel net device interface
1778 *
1779 *************************************************************************/
1780
1781/* Context: process, rtnl_lock() held. */
1782static int efx_net_open(struct net_device *net_dev)
1783{
Ben Hutchings767e4682008-09-01 12:43:14 +01001784 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001785 EFX_ASSERT_RESET_SERIALISED(efx);
1786
Ben Hutchings62776d02010-06-23 11:30:07 +00001787 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1788 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001789
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001790 if (efx->state == STATE_DISABLED)
1791 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001792 if (efx->phy_mode & PHY_MODE_SPECIAL)
1793 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001794 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1795 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001796
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001797 /* Notify the kernel of the link state polled during driver load,
1798 * before the monitor starts running */
1799 efx_link_status_changed(efx);
1800
Ben Hutchings8ceee662008-04-27 12:55:59 +01001801 efx_start_all(efx);
1802 return 0;
1803}
1804
1805/* Context: process, rtnl_lock() held.
1806 * Note that the kernel will ignore our return code; this method
1807 * should really be a void.
1808 */
1809static int efx_net_stop(struct net_device *net_dev)
1810{
Ben Hutchings767e4682008-09-01 12:43:14 +01001811 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001812
Ben Hutchings62776d02010-06-23 11:30:07 +00001813 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1814 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001815
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001816 if (efx->state != STATE_DISABLED) {
1817 /* Stop the device and flush all the channels */
1818 efx_stop_all(efx);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001819 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001820
1821 return 0;
1822}
1823
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001824/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001825static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1826 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001827{
Ben Hutchings767e4682008-09-01 12:43:14 +01001828 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001829 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001830
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001831 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001832
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001833 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001834
1835 stats->rx_packets = mac_stats->rx_packets;
1836 stats->tx_packets = mac_stats->tx_packets;
1837 stats->rx_bytes = mac_stats->rx_bytes;
1838 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001839 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001840 stats->multicast = mac_stats->rx_multicast;
1841 stats->collisions = mac_stats->tx_collision;
1842 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1843 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001844 stats->rx_crc_errors = mac_stats->rx_bad;
1845 stats->rx_frame_errors = mac_stats->rx_align_error;
1846 stats->rx_fifo_errors = mac_stats->rx_overflow;
1847 stats->rx_missed_errors = mac_stats->rx_missed;
1848 stats->tx_window_errors = mac_stats->tx_late_collision;
1849
1850 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001851 stats->rx_crc_errors +
1852 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001853 mac_stats->rx_symbol_error);
1854 stats->tx_errors = (stats->tx_window_errors +
1855 mac_stats->tx_bad);
1856
Ben Hutchings1cb34522011-09-02 23:23:00 +01001857 spin_unlock_bh(&efx->stats_lock);
1858
Ben Hutchings8ceee662008-04-27 12:55:59 +01001859 return stats;
1860}
1861
1862/* Context: netif_tx_lock held, BHs disabled. */
1863static void efx_watchdog(struct net_device *net_dev)
1864{
Ben Hutchings767e4682008-09-01 12:43:14 +01001865 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001866
Ben Hutchings62776d02010-06-23 11:30:07 +00001867 netif_err(efx, tx_err, efx->net_dev,
1868 "TX stuck with port_enabled=%d: resetting channels\n",
1869 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001870
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001871 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001872}
1873
1874
1875/* Context: process, rtnl_lock() held. */
1876static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1877{
Ben Hutchings767e4682008-09-01 12:43:14 +01001878 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001879
1880 EFX_ASSERT_RESET_SERIALISED(efx);
1881
1882 if (new_mtu > EFX_MAX_MTU)
1883 return -EINVAL;
1884
1885 efx_stop_all(efx);
1886
Ben Hutchings62776d02010-06-23 11:30:07 +00001887 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001888
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001889 mutex_lock(&efx->mac_lock);
1890 /* Reconfigure the MAC before enabling the dma queues so that
1891 * the RX buffers don't overflow */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001892 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001893 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001894 mutex_unlock(&efx->mac_lock);
1895
Ben Hutchings8ceee662008-04-27 12:55:59 +01001896 efx_start_all(efx);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001897 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001898}
1899
1900static int efx_set_mac_address(struct net_device *net_dev, void *data)
1901{
Ben Hutchings767e4682008-09-01 12:43:14 +01001902 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001903 struct sockaddr *addr = data;
1904 char *new_addr = addr->sa_data;
1905
1906 EFX_ASSERT_RESET_SERIALISED(efx);
1907
1908 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001909 netif_err(efx, drv, efx->net_dev,
1910 "invalid ethernet MAC address requested: %pM\n",
1911 new_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001912 return -EINVAL;
1913 }
1914
1915 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1916
1917 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001918 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01001919 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001920 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001921
1922 return 0;
1923}
1924
Ben Hutchingsa816f752008-09-01 12:49:12 +01001925/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00001926static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001927{
Ben Hutchings767e4682008-09-01 12:43:14 +01001928 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001929 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001930 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001931 u32 crc;
1932 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001933
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001934 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001935
1936 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001937 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001938 memset(mc_hash, 0xff, sizeof(*mc_hash));
1939 } else {
1940 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00001941 netdev_for_each_mc_addr(ha, net_dev) {
1942 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001943 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1944 set_bit_le(bit, mc_hash->byte);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001945 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001946
1947 /* Broadcast packets go through the multicast hash filter.
1948 * ether_crc_le() of the broadcast address is 0xbe2612ff
1949 * so we always add bit 0xff to the mask.
1950 */
1951 set_bit_le(0xff, mc_hash->byte);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001952 }
1953
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001954 if (efx->port_enabled)
1955 queue_work(efx->workqueue, &efx->mac_work);
1956 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001957}
1958
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001959static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01001960{
1961 struct efx_nic *efx = netdev_priv(net_dev);
1962
1963 /* If disabling RX n-tuple filtering, clear existing filters */
1964 if (net_dev->features & ~data & NETIF_F_NTUPLE)
1965 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
1966
1967 return 0;
1968}
1969
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001970static const struct net_device_ops efx_netdev_ops = {
1971 .ndo_open = efx_net_open,
1972 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00001973 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001974 .ndo_tx_timeout = efx_watchdog,
1975 .ndo_start_xmit = efx_hard_start_xmit,
1976 .ndo_validate_addr = eth_validate_addr,
1977 .ndo_do_ioctl = efx_ioctl,
1978 .ndo_change_mtu = efx_change_mtu,
1979 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00001980 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01001981 .ndo_set_features = efx_set_features,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001982#ifdef CONFIG_NET_POLL_CONTROLLER
1983 .ndo_poll_controller = efx_netpoll,
1984#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00001985 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001986#ifdef CONFIG_RFS_ACCEL
1987 .ndo_rx_flow_steer = efx_filter_rfs,
1988#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001989};
1990
Ben Hutchings7dde5962008-12-12 22:09:38 -08001991static void efx_update_name(struct efx_nic *efx)
1992{
1993 strcpy(efx->name, efx->net_dev->name);
1994 efx_mtd_rename(efx);
1995 efx_set_channel_names(efx);
1996}
1997
Ben Hutchings8ceee662008-04-27 12:55:59 +01001998static int efx_netdev_event(struct notifier_block *this,
1999 unsigned long event, void *ptr)
2000{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01002001 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002002
Ben Hutchings7dde5962008-12-12 22:09:38 -08002003 if (net_dev->netdev_ops == &efx_netdev_ops &&
2004 event == NETDEV_CHANGENAME)
2005 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002006
2007 return NOTIFY_DONE;
2008}
2009
2010static struct notifier_block efx_netdev_notifier = {
2011 .notifier_call = efx_netdev_event,
2012};
2013
Ben Hutchings06d5e192008-12-12 21:47:23 -08002014static ssize_t
2015show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2016{
2017 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2018 return sprintf(buf, "%d\n", efx->phy_type);
2019}
2020static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
2021
Ben Hutchings8ceee662008-04-27 12:55:59 +01002022static int efx_register_netdev(struct efx_nic *efx)
2023{
2024 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002025 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002026 int rc;
2027
2028 net_dev->watchdog_timeo = 5 * HZ;
2029 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002030 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002031 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
2032
Ben Hutchings7dde5962008-12-12 22:09:38 -08002033 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002034
2035 rc = dev_alloc_name(net_dev, net_dev->name);
2036 if (rc < 0)
2037 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002038 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002039
2040 rc = register_netdevice(net_dev);
2041 if (rc)
2042 goto fail_locked;
2043
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002044 efx_for_each_channel(channel, efx) {
2045 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002046 efx_for_each_channel_tx_queue(tx_queue, channel)
2047 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002048 }
2049
Ben Hutchingsaed06282009-08-26 08:16:27 +00002050 /* Always start with carrier off; PHY events will detect the link */
Ben Hutchings86ee5302012-01-09 19:51:22 +00002051 netif_carrier_off(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002052
Ben Hutchings7dde5962008-12-12 22:09:38 -08002053 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002054
Ben Hutchings06d5e192008-12-12 21:47:23 -08002055 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2056 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002057 netif_err(efx, drv, efx->net_dev,
2058 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002059 goto fail_registered;
2060 }
2061
Ben Hutchings8ceee662008-04-27 12:55:59 +01002062 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002063
Ben Hutchingsaed06282009-08-26 08:16:27 +00002064fail_locked:
2065 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002066 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002067 return rc;
2068
Ben Hutchings06d5e192008-12-12 21:47:23 -08002069fail_registered:
2070 unregister_netdev(net_dev);
2071 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002072}
2073
2074static void efx_unregister_netdev(struct efx_nic *efx)
2075{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002076 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002077 struct efx_tx_queue *tx_queue;
2078
2079 if (!efx->net_dev)
2080 return;
2081
Ben Hutchings767e4682008-09-01 12:43:14 +01002082 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002083
2084 /* Free up any skbs still remaining. This has to happen before
2085 * we try to unregister the netdev as running their destructors
2086 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002087 efx_for_each_channel(channel, efx) {
2088 efx_for_each_channel_tx_queue(tx_queue, channel)
2089 efx_release_tx_buffers(tx_queue);
2090 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002091
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002092 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2093 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2094 unregister_netdev(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002095}
2096
2097/**************************************************************************
2098 *
2099 * Device reset and suspend
2100 *
2101 **************************************************************************/
2102
Ben Hutchings2467ca42008-09-01 12:48:50 +01002103/* Tears down the entire software state and most of the hardware state
2104 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002105void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002106{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002107 EFX_ASSERT_RESET_SERIALISED(efx);
2108
Ben Hutchings2467ca42008-09-01 12:48:50 +01002109 efx_stop_all(efx);
2110 mutex_lock(&efx->mac_lock);
2111
Ben Hutchings7f967c02012-02-13 23:45:02 +00002112 efx_stop_interrupts(efx, false);
Steve Hodgson4b988282009-01-29 17:50:51 +00002113 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2114 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002115 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002116}
2117
Ben Hutchings2467ca42008-09-01 12:48:50 +01002118/* This function will always ensure that the locks acquired in
2119 * efx_reset_down() are released. A failure return code indicates
2120 * that we were unable to reinitialise the hardware, and the
2121 * driver should be disabled. If ok is false, then the rx and tx
2122 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002123int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002124{
2125 int rc;
2126
Ben Hutchings2467ca42008-09-01 12:48:50 +01002127 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002128
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002129 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002130 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002131 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002132 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002133 }
2134
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002135 if (!ok)
2136 goto fail;
2137
Steve Hodgson4b988282009-01-29 17:50:51 +00002138 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002139 rc = efx->phy_op->init(efx);
2140 if (rc)
2141 goto fail;
2142 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002143 netif_err(efx, drv, efx->net_dev,
2144 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002145 }
2146
Ben Hutchings710b2082011-09-03 00:15:00 +01002147 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002148
Ben Hutchings7f967c02012-02-13 23:45:02 +00002149 efx_start_interrupts(efx, false);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002150 efx_restore_filters(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002151
2152 mutex_unlock(&efx->mac_lock);
2153
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002154 efx_start_all(efx);
2155
2156 return 0;
2157
2158fail:
2159 efx->port_initialized = false;
2160
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002161 mutex_unlock(&efx->mac_lock);
2162
Ben Hutchings8ceee662008-04-27 12:55:59 +01002163 return rc;
2164}
2165
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002166/* Reset the NIC using the specified method. Note that the reset may
2167 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002168 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002169 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002170 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002171int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002172{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002173 int rc, rc2;
2174 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002175
Ben Hutchings62776d02010-06-23 11:30:07 +00002176 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2177 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002178
Ben Hutchingse4abce82011-05-16 18:51:24 +01002179 netif_device_detach(efx->net_dev);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002180 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002181
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002182 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002183 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002184 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002185 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002186 }
2187
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002188 /* Clear flags for the scopes we covered. We assume the NIC and
2189 * driver are now quiescent so that there is no race here.
2190 */
2191 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002192
2193 /* Reinitialise bus-mastering, which may have been turned off before
2194 * the reset was scheduled. This is still appropriate, even in the
2195 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2196 * can respond to requests. */
2197 pci_set_master(efx->pci_dev);
2198
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002199out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002200 /* Leave device stopped if necessary */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002201 disabled = rc || method == RESET_TYPE_DISABLE;
2202 rc2 = efx_reset_up(efx, method, !disabled);
2203 if (rc2) {
2204 disabled = true;
2205 if (!rc)
2206 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002207 }
2208
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002209 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002210 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002211 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002212 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002213 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002214 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002215 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002216 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002217 return rc;
2218}
2219
2220/* The worker thread exists so that code that cannot sleep can
2221 * schedule a reset for later.
2222 */
2223static void efx_reset_work(struct work_struct *data)
2224{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002225 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002226 unsigned long pending = ACCESS_ONCE(efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002227
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002228 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002229 return;
2230
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002231 /* If we're not RUNNING then don't reset. Leave the reset_pending
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002232 * flags set so that efx_pci_probe_main will be retried */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002233 if (efx->state != STATE_RUNNING) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002234 netif_info(efx, drv, efx->net_dev,
2235 "scheduled reset quenched. NIC not RUNNING\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002236 return;
2237 }
2238
2239 rtnl_lock();
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002240 (void)efx_reset(efx, fls(pending) - 1);
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002241 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002242}
2243
2244void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2245{
2246 enum reset_type method;
2247
Ben Hutchings8ceee662008-04-27 12:55:59 +01002248 switch (type) {
2249 case RESET_TYPE_INVISIBLE:
2250 case RESET_TYPE_ALL:
2251 case RESET_TYPE_WORLD:
2252 case RESET_TYPE_DISABLE:
2253 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002254 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2255 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002256 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002257 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002258 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002259 netif_dbg(efx, drv, efx->net_dev,
2260 "scheduling %s reset for %s\n",
2261 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002262 break;
2263 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002264
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002265 set_bit(method, &efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002266
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002267 /* efx_process_channel() will no longer read events once a
2268 * reset is scheduled. So switch back to poll'd MCDI completions. */
2269 efx_mcdi_mode_poll(efx);
2270
Steve Hodgson1ab00622008-12-12 21:33:02 -08002271 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002272}
2273
2274/**************************************************************************
2275 *
2276 * List of NICs we support
2277 *
2278 **************************************************************************/
2279
2280/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002281static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002282 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2283 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002284 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002285 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2286 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002287 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002288 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002289 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002290 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002291 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002292 {0} /* end of list */
2293};
2294
2295/**************************************************************************
2296 *
Ben Hutchings37594332009-11-23 16:05:45 +00002297 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002298 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002299 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002300 * Needed so all function pointers are valid and do not have to be tested
2301 * before use
2302 *
2303 **************************************************************************/
2304int efx_port_dummy_op_int(struct efx_nic *efx)
2305{
2306 return 0;
2307}
2308void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002309
2310static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002311{
2312 return false;
2313}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002314
stephen hemminger6c8c2512011-04-14 05:50:12 +00002315static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002316 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002317 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002318 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002319 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002320};
2321
Ben Hutchings8ceee662008-04-27 12:55:59 +01002322/**************************************************************************
2323 *
2324 * Data housekeeping
2325 *
2326 **************************************************************************/
2327
2328/* This zeroes out and then fills in the invariants in a struct
2329 * efx_nic (including all sub-structures).
2330 */
stephen hemminger6c8c2512011-04-14 05:50:12 +00002331static int efx_init_struct(struct efx_nic *efx, const struct efx_nic_type *type,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002332 struct pci_dev *pci_dev, struct net_device *net_dev)
2333{
Ben Hutchings46426102010-09-10 06:42:33 +00002334 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002335
2336 /* Initialise common structures */
2337 memset(efx, 0, sizeof(*efx));
2338 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002339#ifdef CONFIG_SFC_MTD
2340 INIT_LIST_HEAD(&efx->mtd_list);
2341#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002342 INIT_WORK(&efx->reset_work, efx_reset_work);
2343 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
2344 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002345 efx->msg_enable = debug;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002346 efx->state = STATE_INIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002347 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002348
2349 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002350 spin_lock_init(&efx->stats_lock);
2351 mutex_init(&efx->mac_lock);
2352 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002353 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002354 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002355 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002356
2357 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002358 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2359 if (!efx->channel[i])
2360 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002361 }
2362
2363 efx->type = type;
2364
Ben Hutchings8ceee662008-04-27 12:55:59 +01002365 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2366
2367 /* Higher numbered interrupt modes are less capable! */
2368 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2369 interrupt_mode);
2370
Ben Hutchings6977dc62008-12-26 13:44:39 -08002371 /* Would be good to use the net_dev name, but we're too early */
2372 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2373 pci_name(pci_dev));
2374 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002375 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002376 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002377
Ben Hutchings8ceee662008-04-27 12:55:59 +01002378 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002379
2380fail:
2381 efx_fini_struct(efx);
2382 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002383}
2384
2385static void efx_fini_struct(struct efx_nic *efx)
2386{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002387 int i;
2388
2389 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2390 kfree(efx->channel[i]);
2391
Ben Hutchings8ceee662008-04-27 12:55:59 +01002392 if (efx->workqueue) {
2393 destroy_workqueue(efx->workqueue);
2394 efx->workqueue = NULL;
2395 }
2396}
2397
2398/**************************************************************************
2399 *
2400 * PCI interface
2401 *
2402 **************************************************************************/
2403
2404/* Main body of final NIC shutdown code
2405 * This is called only at module unload (or hotplug removal).
2406 */
2407static void efx_pci_remove_main(struct efx_nic *efx)
2408{
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002409#ifdef CONFIG_RFS_ACCEL
2410 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2411 efx->net_dev->rx_cpu_rmap = NULL;
2412#endif
Ben Hutchings7f967c02012-02-13 23:45:02 +00002413 efx_stop_interrupts(efx, false);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002414 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002415 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002416 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002417 efx_fini_napi(efx);
2418 efx_remove_all(efx);
2419}
2420
2421/* Final NIC shutdown
2422 * This is called only at module unload (or hotplug removal).
2423 */
2424static void efx_pci_remove(struct pci_dev *pci_dev)
2425{
2426 struct efx_nic *efx;
2427
2428 efx = pci_get_drvdata(pci_dev);
2429 if (!efx)
2430 return;
2431
2432 /* Mark the NIC as fini, then stop the interface */
2433 rtnl_lock();
2434 efx->state = STATE_FINI;
2435 dev_close(efx->net_dev);
2436
2437 /* Allow any queued efx_resets() to complete */
2438 rtnl_unlock();
2439
Ben Hutchings7f967c02012-02-13 23:45:02 +00002440 efx_stop_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002441 efx_unregister_netdev(efx);
2442
Ben Hutchings7dde5962008-12-12 22:09:38 -08002443 efx_mtd_remove(efx);
2444
Ben Hutchings8ceee662008-04-27 12:55:59 +01002445 /* Wait for any scheduled resets to complete. No more will be
2446 * scheduled from this point because efx_stop_all() has been
2447 * called, we are no longer registered with driverlink, and
2448 * the net_device's have been removed. */
Steve Hodgson1ab00622008-12-12 21:33:02 -08002449 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002450
2451 efx_pci_remove_main(efx);
2452
Ben Hutchings8ceee662008-04-27 12:55:59 +01002453 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002454 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002455
2456 pci_set_drvdata(pci_dev, NULL);
2457 efx_fini_struct(efx);
2458 free_netdev(efx->net_dev);
2459};
2460
2461/* Main body of NIC initialisation
2462 * This is called at module load (or hotplug insertion, theoretically).
2463 */
2464static int efx_pci_probe_main(struct efx_nic *efx)
2465{
2466 int rc;
2467
2468 /* Do start-of-day initialisation */
2469 rc = efx_probe_all(efx);
2470 if (rc)
2471 goto fail1;
2472
Ben Hutchingse8f14992010-12-07 19:47:34 +00002473 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002474
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002475 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002476 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002477 netif_err(efx, probe, efx->net_dev,
2478 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002479 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002480 }
2481
2482 rc = efx_init_port(efx);
2483 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002484 netif_err(efx, probe, efx->net_dev,
2485 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002486 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002487 }
2488
Ben Hutchings152b6a62009-11-29 03:43:56 +00002489 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002490 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002491 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00002492 efx_start_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002493
2494 return 0;
2495
Ben Hutchings278c0622009-11-23 16:05:12 +00002496 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002497 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002498 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002499 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002500 fail3:
2501 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002502 efx_remove_all(efx);
2503 fail1:
2504 return rc;
2505}
2506
2507/* NIC initialisation
2508 *
2509 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002510 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002511 * sets up and registers the network devices with the kernel and hooks
2512 * the interrupt service routine. It does not prepare the device for
2513 * transmission; this is left to the first time one of the network
2514 * interfaces is brought up (i.e. efx_net_open).
2515 */
2516static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2517 const struct pci_device_id *entry)
2518{
stephen hemminger6c8c2512011-04-14 05:50:12 +00002519 const struct efx_nic_type *type = (const struct efx_nic_type *) entry->driver_data;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002520 struct net_device *net_dev;
2521 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002522 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002523
2524 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002525 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2526 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002527 if (!net_dev)
2528 return -ENOMEM;
Ben Hutchingsc383b532009-11-29 15:11:02 +00002529 net_dev->features |= (type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002530 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002531 NETIF_F_RXCSUM);
Ben Hutchings738a8f42009-11-29 15:16:05 +00002532 if (type->offload_features & NETIF_F_V6_CSUM)
2533 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002534 /* Mask for features that also apply to VLAN devices */
2535 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002536 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2537 NETIF_F_RXCSUM);
2538 /* All offloads can be toggled */
2539 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings767e4682008-09-01 12:43:14 +01002540 efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002541 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002542 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002543 rc = efx_init_struct(efx, type, pci_dev, net_dev);
2544 if (rc)
2545 goto fail1;
2546
Ben Hutchings62776d02010-06-23 11:30:07 +00002547 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002548 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002549
2550 /* Set up basic I/O (BAR mappings etc) */
2551 rc = efx_init_io(efx);
2552 if (rc)
2553 goto fail2;
2554
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002555 rc = efx_pci_probe_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002556
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002557 /* Serialise against efx_reset(). No more resets will be
2558 * scheduled since efx_stop_all() has been called, and we have
2559 * not and never have been registered.
2560 */
2561 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002562
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002563 if (rc)
2564 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002565
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002566 /* If there was a scheduled reset during probe, the NIC is
2567 * probably hosed anyway.
2568 */
2569 if (efx->reset_pending) {
2570 rc = -EIO;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002571 goto fail4;
2572 }
2573
Ben Hutchings55edc6e2009-11-25 16:11:35 +00002574 /* Switch to the running state before we expose the device to the OS,
2575 * so that dev_open()|efx_start_all() will actually start the device */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002576 efx->state = STATE_RUNNING;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002577
Ben Hutchings8ceee662008-04-27 12:55:59 +01002578 rc = efx_register_netdev(efx);
2579 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002580 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002581
Ben Hutchings62776d02010-06-23 11:30:07 +00002582 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002583
Ben Hutchings7c431612012-01-27 17:23:58 +00002584 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002585 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002586 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002587 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002588 if (rc)
2589 netif_warn(efx, probe, efx->net_dev,
2590 "failed to create MTDs (%d)\n", rc);
2591
Ben Hutchings8ceee662008-04-27 12:55:59 +01002592 return 0;
2593
Ben Hutchings8ceee662008-04-27 12:55:59 +01002594 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002595 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002596 fail3:
2597 efx_fini_io(efx);
2598 fail2:
2599 efx_fini_struct(efx);
2600 fail1:
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002601 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002602 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002603 free_netdev(net_dev);
2604 return rc;
2605}
2606
Ben Hutchings89c758f2009-11-29 03:43:07 +00002607static int efx_pm_freeze(struct device *dev)
2608{
2609 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2610
2611 efx->state = STATE_FINI;
2612
2613 netif_device_detach(efx->net_dev);
2614
2615 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00002616 efx_stop_interrupts(efx, false);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002617
2618 return 0;
2619}
2620
2621static int efx_pm_thaw(struct device *dev)
2622{
2623 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2624
2625 efx->state = STATE_INIT;
2626
Ben Hutchings7f967c02012-02-13 23:45:02 +00002627 efx_start_interrupts(efx, false);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002628
2629 mutex_lock(&efx->mac_lock);
2630 efx->phy_op->reconfigure(efx);
2631 mutex_unlock(&efx->mac_lock);
2632
2633 efx_start_all(efx);
2634
2635 netif_device_attach(efx->net_dev);
2636
2637 efx->state = STATE_RUNNING;
2638
2639 efx->type->resume_wol(efx);
2640
Steve Hodgson319ba642010-06-01 11:17:24 +00002641 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2642 queue_work(reset_workqueue, &efx->reset_work);
2643
Ben Hutchings89c758f2009-11-29 03:43:07 +00002644 return 0;
2645}
2646
2647static int efx_pm_poweroff(struct device *dev)
2648{
2649 struct pci_dev *pci_dev = to_pci_dev(dev);
2650 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2651
2652 efx->type->fini(efx);
2653
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002654 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002655
2656 pci_save_state(pci_dev);
2657 return pci_set_power_state(pci_dev, PCI_D3hot);
2658}
2659
2660/* Used for both resume and restore */
2661static int efx_pm_resume(struct device *dev)
2662{
2663 struct pci_dev *pci_dev = to_pci_dev(dev);
2664 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2665 int rc;
2666
2667 rc = pci_set_power_state(pci_dev, PCI_D0);
2668 if (rc)
2669 return rc;
2670 pci_restore_state(pci_dev);
2671 rc = pci_enable_device(pci_dev);
2672 if (rc)
2673 return rc;
2674 pci_set_master(efx->pci_dev);
2675 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2676 if (rc)
2677 return rc;
2678 rc = efx->type->init(efx);
2679 if (rc)
2680 return rc;
2681 efx_pm_thaw(dev);
2682 return 0;
2683}
2684
2685static int efx_pm_suspend(struct device *dev)
2686{
2687 int rc;
2688
2689 efx_pm_freeze(dev);
2690 rc = efx_pm_poweroff(dev);
2691 if (rc)
2692 efx_pm_resume(dev);
2693 return rc;
2694}
2695
Ben Hutchings18e83e42012-01-05 19:05:20 +00002696static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002697 .suspend = efx_pm_suspend,
2698 .resume = efx_pm_resume,
2699 .freeze = efx_pm_freeze,
2700 .thaw = efx_pm_thaw,
2701 .poweroff = efx_pm_poweroff,
2702 .restore = efx_pm_resume,
2703};
2704
Ben Hutchings8ceee662008-04-27 12:55:59 +01002705static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00002706 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002707 .id_table = efx_pci_table,
2708 .probe = efx_pci_probe,
2709 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00002710 .driver.pm = &efx_pm_ops,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002711};
2712
2713/**************************************************************************
2714 *
2715 * Kernel module interface
2716 *
2717 *************************************************************************/
2718
2719module_param(interrupt_mode, uint, 0444);
2720MODULE_PARM_DESC(interrupt_mode,
2721 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2722
2723static int __init efx_init_module(void)
2724{
2725 int rc;
2726
2727 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2728
2729 rc = register_netdevice_notifier(&efx_netdev_notifier);
2730 if (rc)
2731 goto err_notifier;
2732
Steve Hodgson1ab00622008-12-12 21:33:02 -08002733 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2734 if (!reset_workqueue) {
2735 rc = -ENOMEM;
2736 goto err_reset;
2737 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002738
2739 rc = pci_register_driver(&efx_pci_driver);
2740 if (rc < 0)
2741 goto err_pci;
2742
2743 return 0;
2744
2745 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08002746 destroy_workqueue(reset_workqueue);
2747 err_reset:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002748 unregister_netdevice_notifier(&efx_netdev_notifier);
2749 err_notifier:
2750 return rc;
2751}
2752
2753static void __exit efx_exit_module(void)
2754{
2755 printk(KERN_INFO "Solarflare NET driver unloading\n");
2756
2757 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002758 destroy_workqueue(reset_workqueue);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002759 unregister_netdevice_notifier(&efx_netdev_notifier);
2760
2761}
2762
2763module_init(efx_init_module);
2764module_exit(efx_exit_module);
2765
Ben Hutchings906bb262009-11-29 15:16:19 +00002766MODULE_AUTHOR("Solarflare Communications and "
2767 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002768MODULE_DESCRIPTION("Solarflare Communications network driver");
2769MODULE_LICENSE("GPL");
2770MODULE_DEVICE_TABLE(pci, efx_pci_table);