blob: 00e13ab080e699b7f800fa1cc7716d31a9a500a0 [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 Hutchingse7bed9c2012-02-28 18:44:13 +0000166static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000167module_param(irq_adapt_low_thresh, uint, 0644);
168MODULE_PARM_DESC(irq_adapt_low_thresh,
169 "Threshold score for reducing IRQ moderation");
170
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000171static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000172module_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
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000588 /* Probe channels in reverse, so that any 'extra' channels
589 * use the start of the buffer table. This allows the traffic
590 * channels to be resized without moving them or wasting the
591 * entries before them.
592 */
593 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000594 rc = efx_probe_channel(channel);
595 if (rc) {
596 netif_err(efx, probe, efx->net_dev,
597 "failed to create channel %d\n",
598 channel->channel);
599 goto fail;
600 }
601 }
602 efx_set_channel_names(efx);
603
604 return 0;
605
606fail:
607 efx_remove_channels(efx);
608 return rc;
609}
610
Ben Hutchings8ceee662008-04-27 12:55:59 +0100611/* Channels are shutdown and reinitialised whilst the NIC is running
612 * to propagate configuration changes (mtu, checksum offload), or
613 * to clear hardware error conditions
614 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000615static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100616{
617 struct efx_tx_queue *tx_queue;
618 struct efx_rx_queue *rx_queue;
619 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100620
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100621 /* Calculate the rx buffer allocation parameters required to
622 * support the current MTU, including padding for header
623 * alignment and overruns.
624 */
625 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
626 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
Ben Hutchings39c9cf02010-06-23 11:31:28 +0000627 efx->type->rx_buffer_hash_size +
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100628 efx->type->rx_buffer_padding);
Steve Hodgson62b330b2010-06-01 11:20:53 +0000629 efx->rx_buffer_order = get_order(efx->rx_buffer_len +
630 sizeof(struct efx_rx_page_state));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100631
632 /* Initialise the channels */
633 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100634 efx_for_each_channel_tx_queue(tx_queue, channel)
635 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100636
637 /* The rx buffer allocation strategy is MTU dependent */
638 efx_rx_strategy(channel);
639
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000640 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100641 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000642 efx_nic_generate_fill_event(rx_queue);
643 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100644
645 WARN_ON(channel->rx_pkt != NULL);
646 efx_rx_strategy(channel);
647 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000648
649 if (netif_device_present(efx->net_dev))
650 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100651}
652
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000653static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100654{
655 struct efx_channel *channel;
656 struct efx_tx_queue *tx_queue;
657 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100658 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100659
660 EFX_ASSERT_RESET_SERIALISED(efx);
661 BUG_ON(efx->port_enabled);
662
Ben Hutchings152b6a62009-11-29 03:43:56 +0000663 rc = efx_nic_flush_queues(efx);
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000664 if (rc && EFX_WORKAROUND_7803(efx)) {
665 /* Schedule a reset to recover from the flush failure. The
666 * descriptor caches reference memory we're about to free,
667 * but falcon_reconfigure_mac_wrapper() won't reconnect
668 * the MACs because of the pending reset. */
Ben Hutchings62776d02010-06-23 11:30:07 +0000669 netif_err(efx, drv, efx->net_dev,
670 "Resetting to recover from flush failure\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000671 efx_schedule_reset(efx, RESET_TYPE_ALL);
672 } else if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000673 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000674 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +0000675 netif_dbg(efx, drv, efx->net_dev,
676 "successfully flushed all queues\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000677 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100678
Ben Hutchings8ceee662008-04-27 12:55:59 +0100679 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000680 /* RX packet processing is pipelined, so wait for the
681 * NAPI handler to complete. At least event queue 0
682 * might be kept active by non-data events, so don't
683 * use napi_synchronize() but actually disable NAPI
684 * temporarily.
685 */
686 if (efx_channel_has_rx_queue(channel)) {
687 efx_stop_eventq(channel);
688 efx_start_eventq(channel);
689 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100690
691 efx_for_each_channel_rx_queue(rx_queue, channel)
692 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000693 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100694 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100695 }
696}
697
698static void efx_remove_channel(struct efx_channel *channel)
699{
700 struct efx_tx_queue *tx_queue;
701 struct efx_rx_queue *rx_queue;
702
Ben Hutchings62776d02010-06-23 11:30:07 +0000703 netif_dbg(channel->efx, drv, channel->efx->net_dev,
704 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100705
706 efx_for_each_channel_rx_queue(rx_queue, channel)
707 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000708 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100709 efx_remove_tx_queue(tx_queue);
710 efx_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100711}
712
Ben Hutchings46426102010-09-10 06:42:33 +0000713static void efx_remove_channels(struct efx_nic *efx)
714{
715 struct efx_channel *channel;
716
717 efx_for_each_channel(channel, efx)
718 efx_remove_channel(channel);
719}
720
721int
722efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
723{
724 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
725 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000726 unsigned i, next_buffer_table = 0;
727 int rc = 0;
728
729 /* Not all channels should be reallocated. We must avoid
730 * reallocating their buffer table entries.
731 */
732 efx_for_each_channel(channel, efx) {
733 struct efx_rx_queue *rx_queue;
734 struct efx_tx_queue *tx_queue;
735
736 if (channel->type->copy)
737 continue;
738 next_buffer_table = max(next_buffer_table,
739 channel->eventq.index +
740 channel->eventq.entries);
741 efx_for_each_channel_rx_queue(rx_queue, channel)
742 next_buffer_table = max(next_buffer_table,
743 rx_queue->rxd.index +
744 rx_queue->rxd.entries);
745 efx_for_each_channel_tx_queue(tx_queue, channel)
746 next_buffer_table = max(next_buffer_table,
747 tx_queue->txd.index +
748 tx_queue->txd.entries);
749 }
Ben Hutchings46426102010-09-10 06:42:33 +0000750
751 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000752 efx_stop_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000753
Ben Hutchings7f967c02012-02-13 23:45:02 +0000754 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000755 memset(other_channel, 0, sizeof(other_channel));
756 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000757 channel = efx->channel[i];
758 if (channel->type->copy)
759 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000760 if (!channel) {
761 rc = -ENOMEM;
762 goto out;
763 }
764 other_channel[i] = channel;
765 }
766
767 /* Swap entry counts and channel pointers */
768 old_rxq_entries = efx->rxq_entries;
769 old_txq_entries = efx->txq_entries;
770 efx->rxq_entries = rxq_entries;
771 efx->txq_entries = txq_entries;
772 for (i = 0; i < efx->n_channels; i++) {
773 channel = efx->channel[i];
774 efx->channel[i] = other_channel[i];
775 other_channel[i] = channel;
776 }
777
Ben Hutchings7f967c02012-02-13 23:45:02 +0000778 /* Restart buffer table allocation */
779 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000780
Ben Hutchingse8f14992010-12-07 19:47:34 +0000781 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000782 channel = efx->channel[i];
783 if (!channel->type->copy)
784 continue;
785 rc = efx_probe_channel(channel);
786 if (rc)
787 goto rollback;
788 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000789 }
Ben Hutchings46426102010-09-10 06:42:33 +0000790
Ben Hutchings7f967c02012-02-13 23:45:02 +0000791out:
792 /* Destroy unused channel structures */
793 for (i = 0; i < efx->n_channels; i++) {
794 channel = other_channel[i];
795 if (channel && channel->type->copy) {
796 efx_fini_napi_channel(channel);
797 efx_remove_channel(channel);
798 kfree(channel);
799 }
800 }
801
802 efx_start_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000803 efx_start_all(efx);
804 return rc;
805
806rollback:
807 /* Swap back */
808 efx->rxq_entries = old_rxq_entries;
809 efx->txq_entries = old_txq_entries;
810 for (i = 0; i < efx->n_channels; i++) {
811 channel = efx->channel[i];
812 efx->channel[i] = other_channel[i];
813 other_channel[i] = channel;
814 }
815 goto out;
816}
817
Steve Hodgson90d683a2010-06-01 11:19:39 +0000818void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100819{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000820 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100821}
822
Ben Hutchings7f967c02012-02-13 23:45:02 +0000823static const struct efx_channel_type efx_default_channel_type = {
824 .pre_probe = efx_channel_dummy_op_int,
825 .get_name = efx_get_channel_name,
826 .copy = efx_copy_channel,
827 .keep_eventq = false,
828};
829
830int efx_channel_dummy_op_int(struct efx_channel *channel)
831{
832 return 0;
833}
834
Ben Hutchings8ceee662008-04-27 12:55:59 +0100835/**************************************************************************
836 *
837 * Port handling
838 *
839 **************************************************************************/
840
841/* This ensures that the kernel is kept informed (via
842 * netif_carrier_on/off) of the link status, and also maintains the
843 * link status's stop on the port's TX queue.
844 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000845void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100846{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000847 struct efx_link_state *link_state = &efx->link_state;
848
Ben Hutchings8ceee662008-04-27 12:55:59 +0100849 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
850 * that no events are triggered between unregister_netdev() and the
851 * driver unloading. A more general condition is that NETDEV_CHANGE
852 * can only be generated between NETDEV_UP and NETDEV_DOWN */
853 if (!netif_running(efx->net_dev))
854 return;
855
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000856 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100857 efx->n_link_state_changes++;
858
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000859 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100860 netif_carrier_on(efx->net_dev);
861 else
862 netif_carrier_off(efx->net_dev);
863 }
864
865 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000866 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000867 netif_info(efx, link, efx->net_dev,
868 "link up at %uMbps %s-duplex (MTU %d)%s\n",
869 link_state->speed, link_state->fd ? "full" : "half",
870 efx->net_dev->mtu,
871 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000872 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000873 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100874}
875
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000876void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
877{
878 efx->link_advertising = advertising;
879 if (advertising) {
880 if (advertising & ADVERTISED_Pause)
881 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
882 else
883 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
884 if (advertising & ADVERTISED_Asym_Pause)
885 efx->wanted_fc ^= EFX_FC_TX;
886 }
887}
888
David S. Millerb56269462011-05-17 17:53:22 -0400889void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000890{
891 efx->wanted_fc = wanted_fc;
892 if (efx->link_advertising) {
893 if (wanted_fc & EFX_FC_RX)
894 efx->link_advertising |= (ADVERTISED_Pause |
895 ADVERTISED_Asym_Pause);
896 else
897 efx->link_advertising &= ~(ADVERTISED_Pause |
898 ADVERTISED_Asym_Pause);
899 if (wanted_fc & EFX_FC_TX)
900 efx->link_advertising ^= ADVERTISED_Asym_Pause;
901 }
902}
903
Ben Hutchings115122a2009-03-04 09:52:52 +0000904static void efx_fini_port(struct efx_nic *efx);
905
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000906/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
907 * the MAC appropriately. All other PHY configuration changes are pushed
908 * through phy_op->set_settings(), and pushed asynchronously to the MAC
909 * through efx_monitor().
910 *
911 * Callers must hold the mac_lock
912 */
913int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100914{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000915 enum efx_phy_mode phy_mode;
916 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100917
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000918 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100919
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000920 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000921 netif_addr_lock_bh(efx->net_dev);
922 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100923
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000924 /* Disable PHY transmit in mac level loopbacks */
925 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800926 if (LOOPBACK_INTERNAL(efx))
927 efx->phy_mode |= PHY_MODE_TX_DISABLED;
928 else
929 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800930
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000931 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800932
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000933 if (rc)
934 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100935
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000936 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100937}
938
939/* Reinitialise the MAC to pick up new PHY settings, even if the port is
940 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000941int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100942{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000943 int rc;
944
Ben Hutchings8ceee662008-04-27 12:55:59 +0100945 EFX_ASSERT_RESET_SERIALISED(efx);
946
947 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000948 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100949 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000950
951 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100952}
953
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000954/* Asynchronous work item for changing MAC promiscuity and multicast
955 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
956 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800957static void efx_mac_work(struct work_struct *data)
958{
959 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
960
961 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100962 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100963 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800964 mutex_unlock(&efx->mac_lock);
965}
966
Ben Hutchings8ceee662008-04-27 12:55:59 +0100967static int efx_probe_port(struct efx_nic *efx)
968{
969 int rc;
970
Ben Hutchings62776d02010-06-23 11:30:07 +0000971 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100972
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000973 if (phy_flash_cfg)
974 efx->phy_mode = PHY_MODE_SPECIAL;
975
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000976 /* Connect up MAC/PHY operations table */
977 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100978 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000979 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100980
Ben Hutchingse332bcb2011-12-20 01:22:51 +0000981 /* Initialise MAC address to permanent address */
982 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100983
984 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100985}
986
987static int efx_init_port(struct efx_nic *efx)
988{
989 int rc;
990
Ben Hutchings62776d02010-06-23 11:30:07 +0000991 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100992
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000993 mutex_lock(&efx->mac_lock);
994
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800995 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100996 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000997 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100998
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100999 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001000
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001001 /* Reconfigure the MAC before creating dma queues (required for
1002 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001003 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001004
1005 /* Ensure the PHY advertises the correct flow control settings */
1006 rc = efx->phy_op->reconfigure(efx);
1007 if (rc)
1008 goto fail2;
1009
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001010 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001011 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001012
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001013fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001014 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001015fail1:
1016 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001017 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001018}
1019
Ben Hutchings8ceee662008-04-27 12:55:59 +01001020static void efx_start_port(struct efx_nic *efx)
1021{
Ben Hutchings62776d02010-06-23 11:30:07 +00001022 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001023 BUG_ON(efx->port_enabled);
1024
1025 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001026 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001027
1028 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1029 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001030 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001031
Ben Hutchings8ceee662008-04-27 12:55:59 +01001032 mutex_unlock(&efx->mac_lock);
1033}
1034
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001035/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001036static void efx_stop_port(struct efx_nic *efx)
1037{
Ben Hutchings62776d02010-06-23 11:30:07 +00001038 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001039
1040 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001041 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001042 mutex_unlock(&efx->mac_lock);
1043
1044 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001045 netif_addr_lock_bh(efx->net_dev);
1046 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001047}
1048
1049static void efx_fini_port(struct efx_nic *efx)
1050{
Ben Hutchings62776d02010-06-23 11:30:07 +00001051 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001052
1053 if (!efx->port_initialized)
1054 return;
1055
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001056 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001057 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001058
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001059 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001060 efx_link_status_changed(efx);
1061}
1062
1063static void efx_remove_port(struct efx_nic *efx)
1064{
Ben Hutchings62776d02010-06-23 11:30:07 +00001065 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001066
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001067 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001068}
1069
1070/**************************************************************************
1071 *
1072 * NIC handling
1073 *
1074 **************************************************************************/
1075
1076/* This configures the PCI device to enable I/O and DMA. */
1077static int efx_init_io(struct efx_nic *efx)
1078{
1079 struct pci_dev *pci_dev = efx->pci_dev;
1080 dma_addr_t dma_mask = efx->type->max_dma_mask;
1081 int rc;
1082
Ben Hutchings62776d02010-06-23 11:30:07 +00001083 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001084
1085 rc = pci_enable_device(pci_dev);
1086 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001087 netif_err(efx, probe, efx->net_dev,
1088 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001089 goto fail1;
1090 }
1091
1092 pci_set_master(pci_dev);
1093
1094 /* Set the PCI DMA mask. Try all possibilities from our
1095 * genuine mask down to 32 bits, because some architectures
1096 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1097 * masks event though they reject 46 bit masks.
1098 */
1099 while (dma_mask > 0x7fffffffUL) {
Ben Hutchingse9e01842012-01-05 18:50:29 +00001100 if (pci_dma_supported(pci_dev, dma_mask)) {
1101 rc = pci_set_dma_mask(pci_dev, dma_mask);
1102 if (rc == 0)
1103 break;
1104 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001105 dma_mask >>= 1;
1106 }
1107 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001108 netif_err(efx, probe, efx->net_dev,
1109 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001110 goto fail2;
1111 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001112 netif_dbg(efx, probe, efx->net_dev,
1113 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001114 rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
1115 if (rc) {
1116 /* pci_set_consistent_dma_mask() is not *allowed* to
1117 * fail with a mask that pci_set_dma_mask() accepted,
1118 * but just in case...
1119 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001120 netif_err(efx, probe, efx->net_dev,
1121 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001122 goto fail2;
1123 }
1124
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001125 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1126 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001127 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001128 netif_err(efx, probe, efx->net_dev,
1129 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001130 rc = -EIO;
1131 goto fail3;
1132 }
David S. Miller8decf862011-09-22 03:23:13 -04001133 efx->membase = ioremap_nocache(efx->membase_phys,
1134 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001135 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001136 netif_err(efx, probe, efx->net_dev,
1137 "could not map memory BAR at %llx+%x\n",
1138 (unsigned long long)efx->membase_phys,
1139 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001140 rc = -ENOMEM;
1141 goto fail4;
1142 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001143 netif_dbg(efx, probe, efx->net_dev,
1144 "memory BAR at %llx+%x (virtual %p)\n",
1145 (unsigned long long)efx->membase_phys,
1146 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001147
1148 return 0;
1149
1150 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001151 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001152 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001153 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001154 fail2:
1155 pci_disable_device(efx->pci_dev);
1156 fail1:
1157 return rc;
1158}
1159
1160static void efx_fini_io(struct efx_nic *efx)
1161{
Ben Hutchings62776d02010-06-23 11:30:07 +00001162 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001163
1164 if (efx->membase) {
1165 iounmap(efx->membase);
1166 efx->membase = NULL;
1167 }
1168
1169 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001170 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001171 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001172 }
1173
1174 pci_disable_device(efx->pci_dev);
1175}
1176
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001177static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001178{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001179 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001180 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001181 int cpu;
1182
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001183 if (rss_cpus) {
1184 count = rss_cpus;
1185 } else {
1186 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1187 netif_warn(efx, probe, efx->net_dev,
1188 "RSS disabled due to allocation failure\n");
1189 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001190 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001191
1192 count = 0;
1193 for_each_online_cpu(cpu) {
1194 if (!cpumask_test_cpu(cpu, thread_mask)) {
1195 ++count;
1196 cpumask_or(thread_mask, thread_mask,
1197 topology_thread_cpumask(cpu));
1198 }
1199 }
1200
1201 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001202 }
1203
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001204 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1205 * table entries that are inaccessible to VFs
1206 */
1207 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1208 count > efx_vf_size(efx)) {
1209 netif_warn(efx, probe, efx->net_dev,
1210 "Reducing number of RSS channels from %u to %u for "
1211 "VF support. Increase vf-msix-limit to use more "
1212 "channels on the PF.\n",
1213 count, efx_vf_size(efx));
1214 count = efx_vf_size(efx);
1215 }
1216
Ben Hutchings46123d02008-09-01 12:47:33 +01001217 return count;
1218}
1219
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001220static int
1221efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1222{
1223#ifdef CONFIG_RFS_ACCEL
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001224 unsigned int i;
1225 int rc;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001226
1227 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1228 if (!efx->net_dev->rx_cpu_rmap)
1229 return -ENOMEM;
1230 for (i = 0; i < efx->n_rx_channels; i++) {
1231 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1232 xentries[i].vector);
1233 if (rc) {
1234 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1235 efx->net_dev->rx_cpu_rmap = NULL;
1236 return rc;
1237 }
1238 }
1239#endif
1240 return 0;
1241}
1242
Ben Hutchings46123d02008-09-01 12:47:33 +01001243/* Probe the number and type of interrupts we are able to obtain, and
1244 * the resulting numbers of channels and RX queues.
1245 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001246static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001247{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001248 unsigned int max_channels =
1249 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001250 unsigned int extra_channels = 0;
1251 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001252 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001253
Ben Hutchings7f967c02012-02-13 23:45:02 +00001254 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1255 if (efx->extra_channel_type[i])
1256 ++extra_channels;
1257
Ben Hutchings8ceee662008-04-27 12:55:59 +01001258 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001259 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001260 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001261
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001262 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001263 if (separate_tx_channels)
1264 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001265 n_channels += extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001266 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001267
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001268 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001269 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001270 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001271 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001272 netif_err(efx, drv, efx->net_dev,
1273 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001274 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001275 netif_err(efx, drv, efx->net_dev,
1276 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001277 EFX_BUG_ON_PARANOID(rc >= n_channels);
1278 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001279 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001280 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001281 }
1282
1283 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001284 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001285 if (n_channels > extra_channels)
1286 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001287 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001288 efx->n_tx_channels = max(n_channels / 2, 1U);
1289 efx->n_rx_channels = max(n_channels -
1290 efx->n_tx_channels,
1291 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001292 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001293 efx->n_tx_channels = n_channels;
1294 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001295 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001296 rc = efx_init_rx_cpu_rmap(efx, xentries);
1297 if (rc) {
1298 pci_disable_msix(efx->pci_dev);
1299 return rc;
1300 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001301 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001302 efx_get_channel(efx, i)->irq =
1303 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001304 } else {
1305 /* Fall back to single channel MSI */
1306 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001307 netif_err(efx, drv, efx->net_dev,
1308 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001309 }
1310 }
1311
1312 /* Try single interrupt MSI */
1313 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001314 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001315 efx->n_rx_channels = 1;
1316 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001317 rc = pci_enable_msi(efx->pci_dev);
1318 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001319 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001320 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001321 netif_err(efx, drv, efx->net_dev,
1322 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001323 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1324 }
1325 }
1326
1327 /* Assume legacy interrupts */
1328 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001329 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001330 efx->n_rx_channels = 1;
1331 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001332 efx->legacy_irq = efx->pci_dev->irq;
1333 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001334
Ben Hutchings7f967c02012-02-13 23:45:02 +00001335 /* Assign extra channels if possible */
1336 j = efx->n_channels;
1337 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1338 if (!efx->extra_channel_type[i])
1339 continue;
1340 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1341 efx->n_channels <= extra_channels) {
1342 efx->extra_channel_type[i]->handle_no_channel(efx);
1343 } else {
1344 --j;
1345 efx_get_channel(efx, j)->type =
1346 efx->extra_channel_type[i];
1347 }
1348 }
1349
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001350 /* RSS might be usable on VFs even if it is disabled on the PF */
1351 efx->rss_spread = (efx->n_rx_channels > 1 ?
1352 efx->n_rx_channels : efx_vf_size(efx));
1353
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001354 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001355}
1356
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001357/* Enable interrupts, then probe and start the event queues */
Ben Hutchings7f967c02012-02-13 23:45:02 +00001358static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001359{
1360 struct efx_channel *channel;
1361
1362 if (efx->legacy_irq)
1363 efx->legacy_irq_enabled = true;
1364 efx_nic_enable_interrupts(efx);
1365
1366 efx_for_each_channel(channel, efx) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001367 if (!channel->type->keep_eventq || !may_keep_eventq)
1368 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001369 efx_start_eventq(channel);
1370 }
1371
1372 efx_mcdi_mode_event(efx);
1373}
1374
Ben Hutchings7f967c02012-02-13 23:45:02 +00001375static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001376{
1377 struct efx_channel *channel;
1378
1379 efx_mcdi_mode_poll(efx);
1380
1381 efx_nic_disable_interrupts(efx);
1382 if (efx->legacy_irq) {
1383 synchronize_irq(efx->legacy_irq);
1384 efx->legacy_irq_enabled = false;
1385 }
1386
1387 efx_for_each_channel(channel, efx) {
1388 if (channel->irq)
1389 synchronize_irq(channel->irq);
1390
1391 efx_stop_eventq(channel);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001392 if (!channel->type->keep_eventq || !may_keep_eventq)
1393 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001394 }
1395}
1396
Ben Hutchings8ceee662008-04-27 12:55:59 +01001397static void efx_remove_interrupts(struct efx_nic *efx)
1398{
1399 struct efx_channel *channel;
1400
1401 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001402 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001403 channel->irq = 0;
1404 pci_disable_msi(efx->pci_dev);
1405 pci_disable_msix(efx->pci_dev);
1406
1407 /* Remove legacy interrupt */
1408 efx->legacy_irq = 0;
1409}
1410
Ben Hutchings8831da72008-09-01 12:47:48 +01001411static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001412{
Ben Hutchings602a5322011-05-16 17:32:39 +01001413 struct efx_channel *channel;
1414 struct efx_tx_queue *tx_queue;
1415
Ben Hutchings97653432011-01-12 18:26:56 +00001416 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001417 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001418
1419 /* We need to adjust the TX queue numbers if we have separate
1420 * RX-only and TX-only channels.
1421 */
1422 efx_for_each_channel(channel, efx) {
1423 efx_for_each_channel_tx_queue(tx_queue, channel)
1424 tx_queue->queue -= (efx->tx_channel_offset *
1425 EFX_TXQ_TYPES);
1426 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001427}
1428
1429static int efx_probe_nic(struct efx_nic *efx)
1430{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001431 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001432 int rc;
1433
Ben Hutchings62776d02010-06-23 11:30:07 +00001434 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001435
1436 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001437 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001438 if (rc)
1439 return rc;
1440
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001441 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001442 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001443 rc = efx_probe_interrupts(efx);
1444 if (rc)
1445 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001446
Ben Hutchings28e47c42012-02-15 01:58:49 +00001447 efx->type->dimension_resources(efx);
1448
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001449 if (efx->n_channels > 1)
1450 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001451 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001452 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001453 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001454
Ben Hutchings8831da72008-09-01 12:47:48 +01001455 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001456 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1457 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001458
1459 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001460 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1461 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001462
1463 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001464
1465fail:
1466 efx->type->remove(efx);
1467 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001468}
1469
1470static void efx_remove_nic(struct efx_nic *efx)
1471{
Ben Hutchings62776d02010-06-23 11:30:07 +00001472 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001473
1474 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001475 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001476}
1477
1478/**************************************************************************
1479 *
1480 * NIC startup/shutdown
1481 *
1482 *************************************************************************/
1483
1484static int efx_probe_all(struct efx_nic *efx)
1485{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001486 int rc;
1487
Ben Hutchings8ceee662008-04-27 12:55:59 +01001488 rc = efx_probe_nic(efx);
1489 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001490 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001491 goto fail1;
1492 }
1493
Ben Hutchings8ceee662008-04-27 12:55:59 +01001494 rc = efx_probe_port(efx);
1495 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001496 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001497 goto fail2;
1498 }
1499
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001500 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001501
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001502 rc = efx_probe_filters(efx);
1503 if (rc) {
1504 netif_err(efx, probe, efx->net_dev,
1505 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001506 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001507 }
1508
Ben Hutchings7f967c02012-02-13 23:45:02 +00001509 rc = efx_probe_channels(efx);
1510 if (rc)
1511 goto fail4;
1512
Ben Hutchings8ceee662008-04-27 12:55:59 +01001513 return 0;
1514
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001515 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001516 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001517 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001518 efx_remove_port(efx);
1519 fail2:
1520 efx_remove_nic(efx);
1521 fail1:
1522 return rc;
1523}
1524
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001525/* Called after previous invocation(s) of efx_stop_all, restarts the port,
1526 * kernel transmit queues and NAPI processing, and ensures that the port is
1527 * scheduled to be reconfigured. This function is safe to call multiple
1528 * times when the NIC is in any state.
1529 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001530static void efx_start_all(struct efx_nic *efx)
1531{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001532 EFX_ASSERT_RESET_SERIALISED(efx);
1533
1534 /* Check that it is appropriate to restart the interface. All
1535 * of these flags are safe to read under just the rtnl lock */
1536 if (efx->port_enabled)
1537 return;
1538 if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1539 return;
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001540 if (!netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001541 return;
1542
Ben Hutchings8ceee662008-04-27 12:55:59 +01001543 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001544 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001545
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001546 /* Start the hardware monitor if there is one. Otherwise (we're link
1547 * event driven), we have to poll the PHY because after an event queue
1548 * flush, we could have a missed a link state change */
1549 if (efx->type->monitor != NULL) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001550 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1551 efx_monitor_interval);
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001552 } else {
1553 mutex_lock(&efx->mac_lock);
1554 if (efx->phy_op->poll(efx))
1555 efx_link_status_changed(efx);
1556 mutex_unlock(&efx->mac_lock);
1557 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001558
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001559 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001560}
1561
1562/* Flush all delayed work. Should only be called when no more delayed work
1563 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1564 * since we're holding the rtnl_lock at this point. */
1565static void efx_flush_all(struct efx_nic *efx)
1566{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001567 /* Make sure the hardware monitor is stopped */
1568 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001569 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001570 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001571}
1572
1573/* Quiesce hardware and software without bringing the link down.
1574 * Safe to call multiple times, when the nic and interface is in any
1575 * state. The caller is guaranteed to subsequently be in a position
1576 * to modify any hardware and software state they see fit without
1577 * taking locks. */
1578static void efx_stop_all(struct efx_nic *efx)
1579{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001580 EFX_ASSERT_RESET_SERIALISED(efx);
1581
1582 /* port_enabled can be read safely under the rtnl lock */
1583 if (!efx->port_enabled)
1584 return;
1585
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001586 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001587 efx_stop_port(efx);
1588
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001589 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001590 efx_flush_all(efx);
1591
Ben Hutchings8ceee662008-04-27 12:55:59 +01001592 /* Stop the kernel transmit interface late, so the watchdog
1593 * timer isn't ticking over the flush */
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001594 netif_tx_disable(efx->net_dev);
1595
1596 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001597}
1598
1599static void efx_remove_all(struct efx_nic *efx)
1600{
Ben Hutchings46426102010-09-10 06:42:33 +00001601 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001602 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001603 efx_remove_port(efx);
1604 efx_remove_nic(efx);
1605}
1606
Ben Hutchings8ceee662008-04-27 12:55:59 +01001607/**************************************************************************
1608 *
1609 * Interrupt moderation
1610 *
1611 **************************************************************************/
1612
Ben Hutchingscc180b62011-12-08 19:51:47 +00001613static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001614{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001615 if (usecs == 0)
1616 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001617 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001618 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001619 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001620}
1621
Ben Hutchings8ceee662008-04-27 12:55:59 +01001622/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001623int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1624 unsigned int rx_usecs, bool rx_adaptive,
1625 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001626{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001627 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001628 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1629 efx->timer_quantum_ns,
1630 1000);
1631 unsigned int tx_ticks;
1632 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001633
1634 EFX_ASSERT_RESET_SERIALISED(efx);
1635
Ben Hutchingscc180b62011-12-08 19:51:47 +00001636 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001637 return -EINVAL;
1638
Ben Hutchingscc180b62011-12-08 19:51:47 +00001639 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1640 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1641
Ben Hutchings9e393b32011-09-05 07:43:04 +00001642 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1643 !rx_may_override_tx) {
1644 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1645 "RX and TX IRQ moderation must be equal\n");
1646 return -EINVAL;
1647 }
1648
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001649 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001650 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001651 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001652 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001653 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001654 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001655 channel->irq_moderation = tx_ticks;
1656 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001657
1658 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001659}
1660
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001661void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1662 unsigned int *rx_usecs, bool *rx_adaptive)
1663{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001664 /* We must round up when converting ticks to microseconds
1665 * because we round down when converting the other way.
1666 */
1667
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001668 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001669 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1670 efx->timer_quantum_ns,
1671 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001672
1673 /* If channels are shared between RX and TX, so is IRQ
1674 * moderation. Otherwise, IRQ moderation is the same for all
1675 * TX channels and is not adaptive.
1676 */
1677 if (efx->tx_channel_offset == 0)
1678 *tx_usecs = *rx_usecs;
1679 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001680 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001681 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001682 efx->timer_quantum_ns,
1683 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001684}
1685
Ben Hutchings8ceee662008-04-27 12:55:59 +01001686/**************************************************************************
1687 *
1688 * Hardware monitor
1689 *
1690 **************************************************************************/
1691
Ben Hutchingse254c272010-09-20 08:44:10 +00001692/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001693static void efx_monitor(struct work_struct *data)
1694{
1695 struct efx_nic *efx = container_of(data, struct efx_nic,
1696 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001697
Ben Hutchings62776d02010-06-23 11:30:07 +00001698 netif_vdbg(efx, timer, efx->net_dev,
1699 "hardware monitor executing on CPU %d\n",
1700 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001701 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001702
Ben Hutchings8ceee662008-04-27 12:55:59 +01001703 /* If the mac_lock is already held then it is likely a port
1704 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001705 * most of the work of monitor() anyway. */
1706 if (mutex_trylock(&efx->mac_lock)) {
1707 if (efx->port_enabled)
1708 efx->type->monitor(efx);
1709 mutex_unlock(&efx->mac_lock);
1710 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001711
Ben Hutchings8ceee662008-04-27 12:55:59 +01001712 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1713 efx_monitor_interval);
1714}
1715
1716/**************************************************************************
1717 *
1718 * ioctls
1719 *
1720 *************************************************************************/
1721
1722/* Net device ioctl
1723 * Context: process, rtnl_lock() held.
1724 */
1725static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1726{
Ben Hutchings767e4682008-09-01 12:43:14 +01001727 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001728 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001729
1730 EFX_ASSERT_RESET_SERIALISED(efx);
1731
Ben Hutchings68e7f452009-04-29 08:05:08 +00001732 /* Convert phy_id from older PRTAD/DEVAD format */
1733 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1734 (data->phy_id & 0xfc00) == 0x0400)
1735 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1736
1737 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001738}
1739
1740/**************************************************************************
1741 *
1742 * NAPI interface
1743 *
1744 **************************************************************************/
1745
Ben Hutchings7f967c02012-02-13 23:45:02 +00001746static void efx_init_napi_channel(struct efx_channel *channel)
1747{
1748 struct efx_nic *efx = channel->efx;
1749
1750 channel->napi_dev = efx->net_dev;
1751 netif_napi_add(channel->napi_dev, &channel->napi_str,
1752 efx_poll, napi_weight);
1753}
1754
Ben Hutchingse8f14992010-12-07 19:47:34 +00001755static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001756{
1757 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001758
Ben Hutchings7f967c02012-02-13 23:45:02 +00001759 efx_for_each_channel(channel, efx)
1760 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001761}
1762
1763static void efx_fini_napi_channel(struct efx_channel *channel)
1764{
1765 if (channel->napi_dev)
1766 netif_napi_del(&channel->napi_str);
1767 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001768}
1769
1770static void efx_fini_napi(struct efx_nic *efx)
1771{
1772 struct efx_channel *channel;
1773
Ben Hutchingse8f14992010-12-07 19:47:34 +00001774 efx_for_each_channel(channel, efx)
1775 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001776}
1777
1778/**************************************************************************
1779 *
1780 * Kernel netpoll interface
1781 *
1782 *************************************************************************/
1783
1784#ifdef CONFIG_NET_POLL_CONTROLLER
1785
1786/* Although in the common case interrupts will be disabled, this is not
1787 * guaranteed. However, all our work happens inside the NAPI callback,
1788 * so no locking is required.
1789 */
1790static void efx_netpoll(struct net_device *net_dev)
1791{
Ben Hutchings767e4682008-09-01 12:43:14 +01001792 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001793 struct efx_channel *channel;
1794
Ben Hutchings64ee3122008-09-01 12:47:38 +01001795 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001796 efx_schedule_channel(channel);
1797}
1798
1799#endif
1800
1801/**************************************************************************
1802 *
1803 * Kernel net device interface
1804 *
1805 *************************************************************************/
1806
1807/* Context: process, rtnl_lock() held. */
1808static int efx_net_open(struct net_device *net_dev)
1809{
Ben Hutchings767e4682008-09-01 12:43:14 +01001810 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001811 EFX_ASSERT_RESET_SERIALISED(efx);
1812
Ben Hutchings62776d02010-06-23 11:30:07 +00001813 netif_dbg(efx, ifup, efx->net_dev, "opening device 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 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001818 if (efx->phy_mode & PHY_MODE_SPECIAL)
1819 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001820 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1821 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001822
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001823 /* Notify the kernel of the link state polled during driver load,
1824 * before the monitor starts running */
1825 efx_link_status_changed(efx);
1826
Ben Hutchings8ceee662008-04-27 12:55:59 +01001827 efx_start_all(efx);
1828 return 0;
1829}
1830
1831/* Context: process, rtnl_lock() held.
1832 * Note that the kernel will ignore our return code; this method
1833 * should really be a void.
1834 */
1835static int efx_net_stop(struct net_device *net_dev)
1836{
Ben Hutchings767e4682008-09-01 12:43:14 +01001837 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001838
Ben Hutchings62776d02010-06-23 11:30:07 +00001839 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1840 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001841
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001842 if (efx->state != STATE_DISABLED) {
1843 /* Stop the device and flush all the channels */
1844 efx_stop_all(efx);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001845 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001846
1847 return 0;
1848}
1849
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001850/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001851static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1852 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001853{
Ben Hutchings767e4682008-09-01 12:43:14 +01001854 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001855 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001856
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001857 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001858
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001859 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001860
1861 stats->rx_packets = mac_stats->rx_packets;
1862 stats->tx_packets = mac_stats->tx_packets;
1863 stats->rx_bytes = mac_stats->rx_bytes;
1864 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001865 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001866 stats->multicast = mac_stats->rx_multicast;
1867 stats->collisions = mac_stats->tx_collision;
1868 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1869 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001870 stats->rx_crc_errors = mac_stats->rx_bad;
1871 stats->rx_frame_errors = mac_stats->rx_align_error;
1872 stats->rx_fifo_errors = mac_stats->rx_overflow;
1873 stats->rx_missed_errors = mac_stats->rx_missed;
1874 stats->tx_window_errors = mac_stats->tx_late_collision;
1875
1876 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001877 stats->rx_crc_errors +
1878 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001879 mac_stats->rx_symbol_error);
1880 stats->tx_errors = (stats->tx_window_errors +
1881 mac_stats->tx_bad);
1882
Ben Hutchings1cb34522011-09-02 23:23:00 +01001883 spin_unlock_bh(&efx->stats_lock);
1884
Ben Hutchings8ceee662008-04-27 12:55:59 +01001885 return stats;
1886}
1887
1888/* Context: netif_tx_lock held, BHs disabled. */
1889static void efx_watchdog(struct net_device *net_dev)
1890{
Ben Hutchings767e4682008-09-01 12:43:14 +01001891 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001892
Ben Hutchings62776d02010-06-23 11:30:07 +00001893 netif_err(efx, tx_err, efx->net_dev,
1894 "TX stuck with port_enabled=%d: resetting channels\n",
1895 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001896
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001897 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001898}
1899
1900
1901/* Context: process, rtnl_lock() held. */
1902static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1903{
Ben Hutchings767e4682008-09-01 12:43:14 +01001904 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001905
1906 EFX_ASSERT_RESET_SERIALISED(efx);
1907
1908 if (new_mtu > EFX_MAX_MTU)
1909 return -EINVAL;
1910
1911 efx_stop_all(efx);
1912
Ben Hutchings62776d02010-06-23 11:30:07 +00001913 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001914
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001915 mutex_lock(&efx->mac_lock);
1916 /* Reconfigure the MAC before enabling the dma queues so that
1917 * the RX buffers don't overflow */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001918 net_dev->mtu = new_mtu;
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);
1921
Ben Hutchings8ceee662008-04-27 12:55:59 +01001922 efx_start_all(efx);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001923 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001924}
1925
1926static int efx_set_mac_address(struct net_device *net_dev, void *data)
1927{
Ben Hutchings767e4682008-09-01 12:43:14 +01001928 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001929 struct sockaddr *addr = data;
1930 char *new_addr = addr->sa_data;
1931
1932 EFX_ASSERT_RESET_SERIALISED(efx);
1933
1934 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001935 netif_err(efx, drv, efx->net_dev,
1936 "invalid ethernet MAC address requested: %pM\n",
1937 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00001938 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001939 }
1940
1941 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001942 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001943
1944 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001945 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01001946 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001947 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001948
1949 return 0;
1950}
1951
Ben Hutchingsa816f752008-09-01 12:49:12 +01001952/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00001953static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001954{
Ben Hutchings767e4682008-09-01 12:43:14 +01001955 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001956 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001957 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001958 u32 crc;
1959 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001960
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001961 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001962
1963 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001964 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001965 memset(mc_hash, 0xff, sizeof(*mc_hash));
1966 } else {
1967 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00001968 netdev_for_each_mc_addr(ha, net_dev) {
1969 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001970 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1971 set_bit_le(bit, mc_hash->byte);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001972 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001973
1974 /* Broadcast packets go through the multicast hash filter.
1975 * ether_crc_le() of the broadcast address is 0xbe2612ff
1976 * so we always add bit 0xff to the mask.
1977 */
1978 set_bit_le(0xff, mc_hash->byte);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001979 }
1980
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001981 if (efx->port_enabled)
1982 queue_work(efx->workqueue, &efx->mac_work);
1983 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001984}
1985
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001986static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01001987{
1988 struct efx_nic *efx = netdev_priv(net_dev);
1989
1990 /* If disabling RX n-tuple filtering, clear existing filters */
1991 if (net_dev->features & ~data & NETIF_F_NTUPLE)
1992 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
1993
1994 return 0;
1995}
1996
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001997static const struct net_device_ops efx_netdev_ops = {
1998 .ndo_open = efx_net_open,
1999 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002000 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002001 .ndo_tx_timeout = efx_watchdog,
2002 .ndo_start_xmit = efx_hard_start_xmit,
2003 .ndo_validate_addr = eth_validate_addr,
2004 .ndo_do_ioctl = efx_ioctl,
2005 .ndo_change_mtu = efx_change_mtu,
2006 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002007 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002008 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002009#ifdef CONFIG_SFC_SRIOV
2010 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2011 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2012 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2013 .ndo_get_vf_config = efx_sriov_get_vf_config,
2014#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002015#ifdef CONFIG_NET_POLL_CONTROLLER
2016 .ndo_poll_controller = efx_netpoll,
2017#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002018 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002019#ifdef CONFIG_RFS_ACCEL
2020 .ndo_rx_flow_steer = efx_filter_rfs,
2021#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002022};
2023
Ben Hutchings7dde5962008-12-12 22:09:38 -08002024static void efx_update_name(struct efx_nic *efx)
2025{
2026 strcpy(efx->name, efx->net_dev->name);
2027 efx_mtd_rename(efx);
2028 efx_set_channel_names(efx);
2029}
2030
Ben Hutchings8ceee662008-04-27 12:55:59 +01002031static int efx_netdev_event(struct notifier_block *this,
2032 unsigned long event, void *ptr)
2033{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01002034 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002035
Ben Hutchings7dde5962008-12-12 22:09:38 -08002036 if (net_dev->netdev_ops == &efx_netdev_ops &&
2037 event == NETDEV_CHANGENAME)
2038 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002039
2040 return NOTIFY_DONE;
2041}
2042
2043static struct notifier_block efx_netdev_notifier = {
2044 .notifier_call = efx_netdev_event,
2045};
2046
Ben Hutchings06d5e192008-12-12 21:47:23 -08002047static ssize_t
2048show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2049{
2050 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2051 return sprintf(buf, "%d\n", efx->phy_type);
2052}
2053static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
2054
Ben Hutchings8ceee662008-04-27 12:55:59 +01002055static int efx_register_netdev(struct efx_nic *efx)
2056{
2057 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002058 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002059 int rc;
2060
2061 net_dev->watchdog_timeo = 5 * HZ;
2062 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002063 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002064 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
2065
Ben Hutchings7dde5962008-12-12 22:09:38 -08002066 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002067
2068 rc = dev_alloc_name(net_dev, net_dev->name);
2069 if (rc < 0)
2070 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002071 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002072
2073 rc = register_netdevice(net_dev);
2074 if (rc)
2075 goto fail_locked;
2076
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002077 efx_for_each_channel(channel, efx) {
2078 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002079 efx_for_each_channel_tx_queue(tx_queue, channel)
2080 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002081 }
2082
Ben Hutchingsaed06282009-08-26 08:16:27 +00002083 /* Always start with carrier off; PHY events will detect the link */
Ben Hutchings86ee5302012-01-09 19:51:22 +00002084 netif_carrier_off(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002085
Ben Hutchings7dde5962008-12-12 22:09:38 -08002086 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002087
Ben Hutchings06d5e192008-12-12 21:47:23 -08002088 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2089 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002090 netif_err(efx, drv, efx->net_dev,
2091 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002092 goto fail_registered;
2093 }
2094
Ben Hutchings8ceee662008-04-27 12:55:59 +01002095 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002096
Ben Hutchingsaed06282009-08-26 08:16:27 +00002097fail_locked:
2098 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002099 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002100 return rc;
2101
Ben Hutchings06d5e192008-12-12 21:47:23 -08002102fail_registered:
2103 unregister_netdev(net_dev);
2104 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002105}
2106
2107static void efx_unregister_netdev(struct efx_nic *efx)
2108{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002109 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002110 struct efx_tx_queue *tx_queue;
2111
2112 if (!efx->net_dev)
2113 return;
2114
Ben Hutchings767e4682008-09-01 12:43:14 +01002115 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002116
2117 /* Free up any skbs still remaining. This has to happen before
2118 * we try to unregister the netdev as running their destructors
2119 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002120 efx_for_each_channel(channel, efx) {
2121 efx_for_each_channel_tx_queue(tx_queue, channel)
2122 efx_release_tx_buffers(tx_queue);
2123 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002124
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002125 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2126 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2127 unregister_netdev(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002128}
2129
2130/**************************************************************************
2131 *
2132 * Device reset and suspend
2133 *
2134 **************************************************************************/
2135
Ben Hutchings2467ca42008-09-01 12:48:50 +01002136/* Tears down the entire software state and most of the hardware state
2137 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002138void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002139{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002140 EFX_ASSERT_RESET_SERIALISED(efx);
2141
Ben Hutchings2467ca42008-09-01 12:48:50 +01002142 efx_stop_all(efx);
2143 mutex_lock(&efx->mac_lock);
2144
Ben Hutchings7f967c02012-02-13 23:45:02 +00002145 efx_stop_interrupts(efx, false);
Steve Hodgson4b988282009-01-29 17:50:51 +00002146 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2147 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002148 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002149}
2150
Ben Hutchings2467ca42008-09-01 12:48:50 +01002151/* This function will always ensure that the locks acquired in
2152 * efx_reset_down() are released. A failure return code indicates
2153 * that we were unable to reinitialise the hardware, and the
2154 * driver should be disabled. If ok is false, then the rx and tx
2155 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002156int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002157{
2158 int rc;
2159
Ben Hutchings2467ca42008-09-01 12:48:50 +01002160 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002161
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002162 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002163 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002164 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002165 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002166 }
2167
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002168 if (!ok)
2169 goto fail;
2170
Steve Hodgson4b988282009-01-29 17:50:51 +00002171 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002172 rc = efx->phy_op->init(efx);
2173 if (rc)
2174 goto fail;
2175 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002176 netif_err(efx, drv, efx->net_dev,
2177 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002178 }
2179
Ben Hutchings710b2082011-09-03 00:15:00 +01002180 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002181
Ben Hutchings7f967c02012-02-13 23:45:02 +00002182 efx_start_interrupts(efx, false);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002183 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002184 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002185
2186 mutex_unlock(&efx->mac_lock);
2187
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002188 efx_start_all(efx);
2189
2190 return 0;
2191
2192fail:
2193 efx->port_initialized = false;
2194
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002195 mutex_unlock(&efx->mac_lock);
2196
Ben Hutchings8ceee662008-04-27 12:55:59 +01002197 return rc;
2198}
2199
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002200/* Reset the NIC using the specified method. Note that the reset may
2201 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002202 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002203 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002204 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002205int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002206{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002207 int rc, rc2;
2208 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002209
Ben Hutchings62776d02010-06-23 11:30:07 +00002210 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2211 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002212
Ben Hutchingse4abce82011-05-16 18:51:24 +01002213 netif_device_detach(efx->net_dev);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002214 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002215
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002216 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002217 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002218 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002219 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002220 }
2221
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002222 /* Clear flags for the scopes we covered. We assume the NIC and
2223 * driver are now quiescent so that there is no race here.
2224 */
2225 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002226
2227 /* Reinitialise bus-mastering, which may have been turned off before
2228 * the reset was scheduled. This is still appropriate, even in the
2229 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2230 * can respond to requests. */
2231 pci_set_master(efx->pci_dev);
2232
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002233out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002234 /* Leave device stopped if necessary */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002235 disabled = rc || method == RESET_TYPE_DISABLE;
2236 rc2 = efx_reset_up(efx, method, !disabled);
2237 if (rc2) {
2238 disabled = true;
2239 if (!rc)
2240 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002241 }
2242
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002243 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002244 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002245 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002246 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002247 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002248 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002249 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002250 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002251 return rc;
2252}
2253
2254/* The worker thread exists so that code that cannot sleep can
2255 * schedule a reset for later.
2256 */
2257static void efx_reset_work(struct work_struct *data)
2258{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002259 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002260 unsigned long pending = ACCESS_ONCE(efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002261
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002262 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002263 return;
2264
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002265 /* If we're not RUNNING then don't reset. Leave the reset_pending
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002266 * flags set so that efx_pci_probe_main will be retried */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002267 if (efx->state != STATE_RUNNING) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002268 netif_info(efx, drv, efx->net_dev,
2269 "scheduled reset quenched. NIC not RUNNING\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002270 return;
2271 }
2272
2273 rtnl_lock();
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002274 (void)efx_reset(efx, fls(pending) - 1);
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002275 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002276}
2277
2278void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2279{
2280 enum reset_type method;
2281
Ben Hutchings8ceee662008-04-27 12:55:59 +01002282 switch (type) {
2283 case RESET_TYPE_INVISIBLE:
2284 case RESET_TYPE_ALL:
2285 case RESET_TYPE_WORLD:
2286 case RESET_TYPE_DISABLE:
2287 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002288 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2289 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002290 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002291 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002292 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002293 netif_dbg(efx, drv, efx->net_dev,
2294 "scheduling %s reset for %s\n",
2295 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002296 break;
2297 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002298
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002299 set_bit(method, &efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002300
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002301 /* efx_process_channel() will no longer read events once a
2302 * reset is scheduled. So switch back to poll'd MCDI completions. */
2303 efx_mcdi_mode_poll(efx);
2304
Steve Hodgson1ab00622008-12-12 21:33:02 -08002305 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002306}
2307
2308/**************************************************************************
2309 *
2310 * List of NICs we support
2311 *
2312 **************************************************************************/
2313
2314/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002315static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002316 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2317 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002318 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002319 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2320 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002321 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002322 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002323 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002324 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002325 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002326 {0} /* end of list */
2327};
2328
2329/**************************************************************************
2330 *
Ben Hutchings37594332009-11-23 16:05:45 +00002331 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002332 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002333 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002334 * Needed so all function pointers are valid and do not have to be tested
2335 * before use
2336 *
2337 **************************************************************************/
2338int efx_port_dummy_op_int(struct efx_nic *efx)
2339{
2340 return 0;
2341}
2342void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002343
2344static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002345{
2346 return false;
2347}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002348
stephen hemminger6c8c2512011-04-14 05:50:12 +00002349static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002350 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002351 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002352 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002353 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002354};
2355
Ben Hutchings8ceee662008-04-27 12:55:59 +01002356/**************************************************************************
2357 *
2358 * Data housekeeping
2359 *
2360 **************************************************************************/
2361
2362/* This zeroes out and then fills in the invariants in a struct
2363 * efx_nic (including all sub-structures).
2364 */
stephen hemminger6c8c2512011-04-14 05:50:12 +00002365static int efx_init_struct(struct efx_nic *efx, const struct efx_nic_type *type,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002366 struct pci_dev *pci_dev, struct net_device *net_dev)
2367{
Ben Hutchings46426102010-09-10 06:42:33 +00002368 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002369
2370 /* Initialise common structures */
2371 memset(efx, 0, sizeof(*efx));
2372 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002373#ifdef CONFIG_SFC_MTD
2374 INIT_LIST_HEAD(&efx->mtd_list);
2375#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002376 INIT_WORK(&efx->reset_work, efx_reset_work);
2377 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
2378 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002379 efx->msg_enable = debug;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002380 efx->state = STATE_INIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002381 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002382
2383 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002384 spin_lock_init(&efx->stats_lock);
2385 mutex_init(&efx->mac_lock);
2386 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002387 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002388 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002389 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002390
2391 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002392 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2393 if (!efx->channel[i])
2394 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002395 }
2396
2397 efx->type = type;
2398
Ben Hutchings8ceee662008-04-27 12:55:59 +01002399 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2400
2401 /* Higher numbered interrupt modes are less capable! */
2402 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2403 interrupt_mode);
2404
Ben Hutchings6977dc62008-12-26 13:44:39 -08002405 /* Would be good to use the net_dev name, but we're too early */
2406 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2407 pci_name(pci_dev));
2408 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002409 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002410 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002411
Ben Hutchings8ceee662008-04-27 12:55:59 +01002412 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002413
2414fail:
2415 efx_fini_struct(efx);
2416 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002417}
2418
2419static void efx_fini_struct(struct efx_nic *efx)
2420{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002421 int i;
2422
2423 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2424 kfree(efx->channel[i]);
2425
Ben Hutchings8ceee662008-04-27 12:55:59 +01002426 if (efx->workqueue) {
2427 destroy_workqueue(efx->workqueue);
2428 efx->workqueue = NULL;
2429 }
2430}
2431
2432/**************************************************************************
2433 *
2434 * PCI interface
2435 *
2436 **************************************************************************/
2437
2438/* Main body of final NIC shutdown code
2439 * This is called only at module unload (or hotplug removal).
2440 */
2441static void efx_pci_remove_main(struct efx_nic *efx)
2442{
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002443#ifdef CONFIG_RFS_ACCEL
2444 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2445 efx->net_dev->rx_cpu_rmap = NULL;
2446#endif
Ben Hutchings7f967c02012-02-13 23:45:02 +00002447 efx_stop_interrupts(efx, false);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002448 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002449 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002450 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002451 efx_fini_napi(efx);
2452 efx_remove_all(efx);
2453}
2454
2455/* Final NIC shutdown
2456 * This is called only at module unload (or hotplug removal).
2457 */
2458static void efx_pci_remove(struct pci_dev *pci_dev)
2459{
2460 struct efx_nic *efx;
2461
2462 efx = pci_get_drvdata(pci_dev);
2463 if (!efx)
2464 return;
2465
2466 /* Mark the NIC as fini, then stop the interface */
2467 rtnl_lock();
2468 efx->state = STATE_FINI;
2469 dev_close(efx->net_dev);
2470
2471 /* Allow any queued efx_resets() to complete */
2472 rtnl_unlock();
2473
Ben Hutchings7f967c02012-02-13 23:45:02 +00002474 efx_stop_interrupts(efx, false);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002475 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002476 efx_unregister_netdev(efx);
2477
Ben Hutchings7dde5962008-12-12 22:09:38 -08002478 efx_mtd_remove(efx);
2479
Ben Hutchings8ceee662008-04-27 12:55:59 +01002480 /* Wait for any scheduled resets to complete. No more will be
2481 * scheduled from this point because efx_stop_all() has been
2482 * called, we are no longer registered with driverlink, and
2483 * the net_device's have been removed. */
Steve Hodgson1ab00622008-12-12 21:33:02 -08002484 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002485
2486 efx_pci_remove_main(efx);
2487
Ben Hutchings8ceee662008-04-27 12:55:59 +01002488 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002489 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002490
2491 pci_set_drvdata(pci_dev, NULL);
2492 efx_fini_struct(efx);
2493 free_netdev(efx->net_dev);
2494};
2495
2496/* Main body of NIC initialisation
2497 * This is called at module load (or hotplug insertion, theoretically).
2498 */
2499static int efx_pci_probe_main(struct efx_nic *efx)
2500{
2501 int rc;
2502
2503 /* Do start-of-day initialisation */
2504 rc = efx_probe_all(efx);
2505 if (rc)
2506 goto fail1;
2507
Ben Hutchingse8f14992010-12-07 19:47:34 +00002508 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002509
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002510 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002511 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002512 netif_err(efx, probe, efx->net_dev,
2513 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002514 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002515 }
2516
2517 rc = efx_init_port(efx);
2518 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002519 netif_err(efx, probe, efx->net_dev,
2520 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002521 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002522 }
2523
Ben Hutchings152b6a62009-11-29 03:43:56 +00002524 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002525 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002526 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00002527 efx_start_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002528
2529 return 0;
2530
Ben Hutchings278c0622009-11-23 16:05:12 +00002531 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002532 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002533 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002534 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002535 fail3:
2536 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002537 efx_remove_all(efx);
2538 fail1:
2539 return rc;
2540}
2541
2542/* NIC initialisation
2543 *
2544 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002545 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002546 * sets up and registers the network devices with the kernel and hooks
2547 * the interrupt service routine. It does not prepare the device for
2548 * transmission; this is left to the first time one of the network
2549 * interfaces is brought up (i.e. efx_net_open).
2550 */
2551static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2552 const struct pci_device_id *entry)
2553{
stephen hemminger6c8c2512011-04-14 05:50:12 +00002554 const struct efx_nic_type *type = (const struct efx_nic_type *) entry->driver_data;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002555 struct net_device *net_dev;
2556 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002557 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002558
2559 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002560 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2561 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002562 if (!net_dev)
2563 return -ENOMEM;
Ben Hutchingsc383b532009-11-29 15:11:02 +00002564 net_dev->features |= (type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002565 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002566 NETIF_F_RXCSUM);
Ben Hutchings738a8f42009-11-29 15:16:05 +00002567 if (type->offload_features & NETIF_F_V6_CSUM)
2568 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002569 /* Mask for features that also apply to VLAN devices */
2570 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002571 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2572 NETIF_F_RXCSUM);
2573 /* All offloads can be toggled */
2574 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings767e4682008-09-01 12:43:14 +01002575 efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002576 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002577 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002578 rc = efx_init_struct(efx, type, pci_dev, net_dev);
2579 if (rc)
2580 goto fail1;
2581
Ben Hutchings62776d02010-06-23 11:30:07 +00002582 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002583 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002584
2585 /* Set up basic I/O (BAR mappings etc) */
2586 rc = efx_init_io(efx);
2587 if (rc)
2588 goto fail2;
2589
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002590 rc = efx_pci_probe_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002591
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002592 /* Serialise against efx_reset(). No more resets will be
2593 * scheduled since efx_stop_all() has been called, and we have
2594 * not and never have been registered.
2595 */
2596 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002597
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002598 if (rc)
2599 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002600
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002601 /* If there was a scheduled reset during probe, the NIC is
2602 * probably hosed anyway.
2603 */
2604 if (efx->reset_pending) {
2605 rc = -EIO;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002606 goto fail4;
2607 }
2608
Ben Hutchings55edc6e2009-11-25 16:11:35 +00002609 /* Switch to the running state before we expose the device to the OS,
2610 * so that dev_open()|efx_start_all() will actually start the device */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002611 efx->state = STATE_RUNNING;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002612
Ben Hutchings8ceee662008-04-27 12:55:59 +01002613 rc = efx_register_netdev(efx);
2614 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002615 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002616
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002617 rc = efx_sriov_init(efx);
2618 if (rc)
2619 netif_err(efx, probe, efx->net_dev,
2620 "SR-IOV can't be enabled rc %d\n", rc);
2621
Ben Hutchings62776d02010-06-23 11:30:07 +00002622 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002623
Ben Hutchings7c431612012-01-27 17:23:58 +00002624 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002625 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002626 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002627 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002628 if (rc)
2629 netif_warn(efx, probe, efx->net_dev,
2630 "failed to create MTDs (%d)\n", rc);
2631
Ben Hutchings8ceee662008-04-27 12:55:59 +01002632 return 0;
2633
Ben Hutchings8ceee662008-04-27 12:55:59 +01002634 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002635 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002636 fail3:
2637 efx_fini_io(efx);
2638 fail2:
2639 efx_fini_struct(efx);
2640 fail1:
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002641 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002642 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002643 free_netdev(net_dev);
2644 return rc;
2645}
2646
Ben Hutchings89c758f2009-11-29 03:43:07 +00002647static int efx_pm_freeze(struct device *dev)
2648{
2649 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2650
2651 efx->state = STATE_FINI;
2652
2653 netif_device_detach(efx->net_dev);
2654
2655 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00002656 efx_stop_interrupts(efx, false);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002657
2658 return 0;
2659}
2660
2661static int efx_pm_thaw(struct device *dev)
2662{
2663 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2664
2665 efx->state = STATE_INIT;
2666
Ben Hutchings7f967c02012-02-13 23:45:02 +00002667 efx_start_interrupts(efx, false);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002668
2669 mutex_lock(&efx->mac_lock);
2670 efx->phy_op->reconfigure(efx);
2671 mutex_unlock(&efx->mac_lock);
2672
2673 efx_start_all(efx);
2674
2675 netif_device_attach(efx->net_dev);
2676
2677 efx->state = STATE_RUNNING;
2678
2679 efx->type->resume_wol(efx);
2680
Steve Hodgson319ba642010-06-01 11:17:24 +00002681 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2682 queue_work(reset_workqueue, &efx->reset_work);
2683
Ben Hutchings89c758f2009-11-29 03:43:07 +00002684 return 0;
2685}
2686
2687static int efx_pm_poweroff(struct device *dev)
2688{
2689 struct pci_dev *pci_dev = to_pci_dev(dev);
2690 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2691
2692 efx->type->fini(efx);
2693
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002694 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002695
2696 pci_save_state(pci_dev);
2697 return pci_set_power_state(pci_dev, PCI_D3hot);
2698}
2699
2700/* Used for both resume and restore */
2701static int efx_pm_resume(struct device *dev)
2702{
2703 struct pci_dev *pci_dev = to_pci_dev(dev);
2704 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2705 int rc;
2706
2707 rc = pci_set_power_state(pci_dev, PCI_D0);
2708 if (rc)
2709 return rc;
2710 pci_restore_state(pci_dev);
2711 rc = pci_enable_device(pci_dev);
2712 if (rc)
2713 return rc;
2714 pci_set_master(efx->pci_dev);
2715 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2716 if (rc)
2717 return rc;
2718 rc = efx->type->init(efx);
2719 if (rc)
2720 return rc;
2721 efx_pm_thaw(dev);
2722 return 0;
2723}
2724
2725static int efx_pm_suspend(struct device *dev)
2726{
2727 int rc;
2728
2729 efx_pm_freeze(dev);
2730 rc = efx_pm_poweroff(dev);
2731 if (rc)
2732 efx_pm_resume(dev);
2733 return rc;
2734}
2735
Ben Hutchings18e83e42012-01-05 19:05:20 +00002736static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002737 .suspend = efx_pm_suspend,
2738 .resume = efx_pm_resume,
2739 .freeze = efx_pm_freeze,
2740 .thaw = efx_pm_thaw,
2741 .poweroff = efx_pm_poweroff,
2742 .restore = efx_pm_resume,
2743};
2744
Ben Hutchings8ceee662008-04-27 12:55:59 +01002745static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00002746 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002747 .id_table = efx_pci_table,
2748 .probe = efx_pci_probe,
2749 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00002750 .driver.pm = &efx_pm_ops,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002751};
2752
2753/**************************************************************************
2754 *
2755 * Kernel module interface
2756 *
2757 *************************************************************************/
2758
2759module_param(interrupt_mode, uint, 0444);
2760MODULE_PARM_DESC(interrupt_mode,
2761 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2762
2763static int __init efx_init_module(void)
2764{
2765 int rc;
2766
2767 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2768
2769 rc = register_netdevice_notifier(&efx_netdev_notifier);
2770 if (rc)
2771 goto err_notifier;
2772
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002773 rc = efx_init_sriov();
2774 if (rc)
2775 goto err_sriov;
2776
Steve Hodgson1ab00622008-12-12 21:33:02 -08002777 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2778 if (!reset_workqueue) {
2779 rc = -ENOMEM;
2780 goto err_reset;
2781 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002782
2783 rc = pci_register_driver(&efx_pci_driver);
2784 if (rc < 0)
2785 goto err_pci;
2786
2787 return 0;
2788
2789 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08002790 destroy_workqueue(reset_workqueue);
2791 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002792 efx_fini_sriov();
2793 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002794 unregister_netdevice_notifier(&efx_netdev_notifier);
2795 err_notifier:
2796 return rc;
2797}
2798
2799static void __exit efx_exit_module(void)
2800{
2801 printk(KERN_INFO "Solarflare NET driver unloading\n");
2802
2803 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002804 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002805 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002806 unregister_netdevice_notifier(&efx_netdev_notifier);
2807
2808}
2809
2810module_init(efx_init_module);
2811module_exit(efx_exit_module);
2812
Ben Hutchings906bb262009-11-29 15:16:19 +00002813MODULE_AUTHOR("Solarflare Communications and "
2814 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002815MODULE_DESCRIPTION("Solarflare Communications network driver");
2816MODULE_LICENSE("GPL");
2817MODULE_DEVICE_TABLE(pci, efx_pci_table);