blob: a2daaae266d72b368e97ce823d67e58832d99961 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchings0a6f40c2011-02-25 00:01:34 +00004 * Copyright 2005-2011 Solarflare Communications Inc.
Ben Hutchings8ceee662008-04-27 12:55:59 +01005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/delay.h>
16#include <linux/notifier.h>
17#include <linux/ip.h>
18#include <linux/tcp.h>
19#include <linux/in.h>
20#include <linux/crc32.h>
21#include <linux/ethtool.h>
Ben Hutchingsaa6ef272008-07-18 19:03:10 +010022#include <linux/topology.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/gfp.h>
Alexandre Rames626950d2013-01-14 17:20:22 +000024#include <linux/aer.h>
Alexandre Ramesb28405b2013-03-21 16:41:43 +000025#include <linux/interrupt.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010027#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000028#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000029#include "selftest.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010030
Ben Hutchings8880f4e2009-11-29 15:15:41 +000031#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000032#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000033
Ben Hutchingsc4593022009-11-23 16:08:17 +000034/**************************************************************************
35 *
36 * Type name strings
37 *
38 **************************************************************************
39 */
40
41/* Loopback mode names (see LOOPBACK_MODE()) */
42const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000043const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000044 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000045 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000046 [LOOPBACK_GMAC] = "GMAC",
47 [LOOPBACK_XGMII] = "XGMII",
48 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000049 [LOOPBACK_XAUI] = "XAUI",
50 [LOOPBACK_GMII] = "GMII",
51 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000052 [LOOPBACK_XGBR] = "XGBR",
53 [LOOPBACK_XFI] = "XFI",
54 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
55 [LOOPBACK_GMII_FAR] = "GMII_FAR",
56 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
57 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000058 [LOOPBACK_GPHY] = "GPHY",
59 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000060 [LOOPBACK_PCS] = "PCS",
61 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000062 [LOOPBACK_XPORT] = "XPORT",
63 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000064 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000065 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
66 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000067 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000068 [LOOPBACK_XFI_WS] = "XFI_WS",
69 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000070 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000071};
72
Ben Hutchingsc4593022009-11-23 16:08:17 +000073const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000074const char *const efx_reset_type_names[] = {
Alexandre Rames626950d2013-01-14 17:20:22 +000075 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
76 [RESET_TYPE_ALL] = "ALL",
77 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
78 [RESET_TYPE_WORLD] = "WORLD",
79 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
80 [RESET_TYPE_DISABLE] = "DISABLE",
81 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
82 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
83 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
84 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
85 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
86 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
87 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Ben Hutchingsc4593022009-11-23 16:08:17 +000088};
89
Steve Hodgson1ab00622008-12-12 21:33:02 -080090/* Reset workqueue. If any NIC has a hardware failure then a reset will be
91 * queued onto this work queue. This is not a per-nic work queue, because
92 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
93 */
94static struct workqueue_struct *reset_workqueue;
95
Ben Hutchings8ceee662008-04-27 12:55:59 +010096/**************************************************************************
97 *
98 * Configurable values
99 *
100 *************************************************************************/
101
102/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100103 * Use separate channels for TX and RX events
104 *
Neil Turton28b581a2008-12-12 21:41:06 -0800105 * Set this to 1 to use separate channels for TX and RX. It allows us
106 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100107 *
Neil Turton28b581a2008-12-12 21:41:06 -0800108 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100109 */
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000110static bool separate_tx_channels;
111module_param(separate_tx_channels, bool, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800112MODULE_PARM_DESC(separate_tx_channels,
113 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100114
115/* This is the weight assigned to each of the (per-channel) virtual
116 * NAPI devices.
117 */
118static int napi_weight = 64;
119
120/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000121 * monitor.
122 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000123 * - Check the on-board hardware monitor;
124 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000125 * On Siena-based NICs for power systems with EEH support, this will give EEH a
126 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100127 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000128static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100129
Ben Hutchings8ceee662008-04-27 12:55:59 +0100130/* Initial interrupt moderation settings. They can be modified after
131 * module load with ethtool.
132 *
133 * The default for RX should strike a balance between increasing the
134 * round-trip latency and reducing overhead.
135 */
136static unsigned int rx_irq_mod_usec = 60;
137
138/* Initial interrupt moderation settings. They can be modified after
139 * module load with ethtool.
140 *
141 * This default is chosen to ensure that a 10G link does not go idle
142 * while a TX queue is stopped after it has become full. A queue is
143 * restarted when it drops below half full. The time this takes (assuming
144 * worst case 3 descriptors per packet and 1024 descriptors) is
145 * 512 / 3 * 1.2 = 205 usec.
146 */
147static unsigned int tx_irq_mod_usec = 150;
148
149/* This is the first interrupt mode to try out of:
150 * 0 => MSI-X
151 * 1 => MSI
152 * 2 => legacy
153 */
154static unsigned int interrupt_mode;
155
156/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
157 * i.e. the number of CPUs among which we may distribute simultaneous
158 * interrupt handling.
159 *
160 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000161 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100162 */
163static unsigned int rss_cpus;
164module_param(rss_cpus, uint, 0444);
165MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
166
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000167static bool phy_flash_cfg;
168module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800169MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
170
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000171static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000172module_param(irq_adapt_low_thresh, uint, 0644);
173MODULE_PARM_DESC(irq_adapt_low_thresh,
174 "Threshold score for reducing IRQ moderation");
175
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000176static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000177module_param(irq_adapt_high_thresh, uint, 0644);
178MODULE_PARM_DESC(irq_adapt_high_thresh,
179 "Threshold score for increasing IRQ moderation");
180
Ben Hutchings62776d02010-06-23 11:30:07 +0000181static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
182 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
183 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
184 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
185module_param(debug, uint, 0);
186MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
187
Ben Hutchings8ceee662008-04-27 12:55:59 +0100188/**************************************************************************
189 *
190 * Utility functions and prototypes
191 *
192 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000193
Ben Hutchingsd8291182012-10-05 23:35:41 +0100194static void efx_soft_enable_interrupts(struct efx_nic *efx);
195static void efx_soft_disable_interrupts(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000196static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000197static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000198static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100199static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000200static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100201static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000202static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000203static void efx_fini_struct(struct efx_nic *efx);
204static void efx_start_all(struct efx_nic *efx);
205static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100206
207#define EFX_ASSERT_RESET_SERIALISED(efx) \
208 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100209 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000210 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000211 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100212 ASSERT_RTNL(); \
213 } while (0)
214
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100215static int efx_check_disabled(struct efx_nic *efx)
216{
Alexandre Rames626950d2013-01-14 17:20:22 +0000217 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100218 netif_err(efx, drv, efx->net_dev,
219 "device is disabled due to earlier errors\n");
220 return -EIO;
221 }
222 return 0;
223}
224
Ben Hutchings8ceee662008-04-27 12:55:59 +0100225/**************************************************************************
226 *
227 * Event queue processing
228 *
229 *************************************************************************/
230
231/* Process channel's event queue
232 *
233 * This function is responsible for processing the event queue of a
234 * single channel. The caller must guarantee that this function will
235 * never be concurrently called more than once on the same channel,
236 * though different channels may be being processed concurrently.
237 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000238static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100239{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000240 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100241
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000242 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100243 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100244
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000245 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000246 if (spent && efx_channel_has_rx_queue(channel)) {
247 struct efx_rx_queue *rx_queue =
248 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100249
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000250 efx_rx_flush_packet(channel);
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100251 efx_fast_push_rx_descriptors(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100252 }
253
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000254 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100255}
256
Ben Hutchings8ceee662008-04-27 12:55:59 +0100257/* NAPI poll handler
258 *
259 * NAPI guarantees serialisation of polls of the same device, which
260 * provides the guarantee required by efx_process_channel().
261 */
262static int efx_poll(struct napi_struct *napi, int budget)
263{
264 struct efx_channel *channel =
265 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000266 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000267 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100268
Ben Hutchings62776d02010-06-23 11:30:07 +0000269 netif_vdbg(efx, intr, efx->net_dev,
270 "channel %d NAPI poll executing on CPU %d\n",
271 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100272
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000273 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100274
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000275 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000276 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000277 efx->irq_rx_adaptive &&
278 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000279 if (unlikely(channel->irq_mod_score <
280 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000281 if (channel->irq_moderation > 1) {
282 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000283 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000284 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000285 } else if (unlikely(channel->irq_mod_score >
286 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000287 if (channel->irq_moderation <
288 efx->irq_rx_moderation) {
289 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000290 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000291 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000292 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000293 channel->irq_count = 0;
294 channel->irq_mod_score = 0;
295 }
296
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000297 efx_filter_rfs_expire(channel);
298
Ben Hutchings8ceee662008-04-27 12:55:59 +0100299 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800300 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100301 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100302 * interrupts have already been disabled.
303 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800304 napi_complete(napi);
Ben Hutchings514bedb2012-10-05 19:30:16 +0100305 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100306 }
307
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000308 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100309}
310
Ben Hutchings8ceee662008-04-27 12:55:59 +0100311/* Create event queue
312 * Event queue memory allocations are done only once. If the channel
313 * is reset, the memory buffer will be reused; this guards against
314 * errors during channel reset and also simplifies interrupt handling.
315 */
316static int efx_probe_eventq(struct efx_channel *channel)
317{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000318 struct efx_nic *efx = channel->efx;
319 unsigned long entries;
320
Ben Hutchings86ee5302012-01-09 19:51:22 +0000321 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000322 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100323
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000324 /* Build an event queue with room for one event per tx and rx buffer,
325 * plus some extra for link state events and MCDI completions. */
326 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
327 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
328 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
329
Ben Hutchings152b6a62009-11-29 03:43:56 +0000330 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100331}
332
333/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100334static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100335{
Ben Hutchings62776d02010-06-23 11:30:07 +0000336 netif_dbg(channel->efx, drv, channel->efx->net_dev,
337 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100338
339 channel->eventq_read_ptr = 0;
340
Ben Hutchings152b6a62009-11-29 03:43:56 +0000341 efx_nic_init_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100342 channel->eventq_init = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100343}
344
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000345/* Enable event queue processing and NAPI */
346static void efx_start_eventq(struct efx_channel *channel)
347{
348 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
349 "chan %d start event queue\n", channel->channel);
350
Ben Hutchings514bedb2012-10-05 19:30:16 +0100351 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000352 channel->enabled = true;
353 smp_wmb();
354
355 napi_enable(&channel->napi_str);
356 efx_nic_eventq_read_ack(channel);
357}
358
359/* Disable event queue processing and NAPI */
360static void efx_stop_eventq(struct efx_channel *channel)
361{
362 if (!channel->enabled)
363 return;
364
365 napi_disable(&channel->napi_str);
366 channel->enabled = false;
367}
368
Ben Hutchings8ceee662008-04-27 12:55:59 +0100369static void efx_fini_eventq(struct efx_channel *channel)
370{
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100371 if (!channel->eventq_init)
372 return;
373
Ben Hutchings62776d02010-06-23 11:30:07 +0000374 netif_dbg(channel->efx, drv, channel->efx->net_dev,
375 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100376
Ben Hutchings152b6a62009-11-29 03:43:56 +0000377 efx_nic_fini_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100378 channel->eventq_init = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100379}
380
381static void efx_remove_eventq(struct efx_channel *channel)
382{
Ben Hutchings62776d02010-06-23 11:30:07 +0000383 netif_dbg(channel->efx, drv, channel->efx->net_dev,
384 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100385
Ben Hutchings152b6a62009-11-29 03:43:56 +0000386 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100387}
388
389/**************************************************************************
390 *
391 * Channel handling
392 *
393 *************************************************************************/
394
Ben Hutchings7f967c02012-02-13 23:45:02 +0000395/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000396static struct efx_channel *
397efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
398{
399 struct efx_channel *channel;
400 struct efx_rx_queue *rx_queue;
401 struct efx_tx_queue *tx_queue;
402 int j;
403
Ben Hutchings7f967c02012-02-13 23:45:02 +0000404 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
405 if (!channel)
406 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000407
Ben Hutchings7f967c02012-02-13 23:45:02 +0000408 channel->efx = efx;
409 channel->channel = i;
410 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000411
Ben Hutchings7f967c02012-02-13 23:45:02 +0000412 for (j = 0; j < EFX_TXQ_TYPES; j++) {
413 tx_queue = &channel->tx_queue[j];
414 tx_queue->efx = efx;
415 tx_queue->queue = i * EFX_TXQ_TYPES + j;
416 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000417 }
418
Ben Hutchings46426102010-09-10 06:42:33 +0000419 rx_queue = &channel->rx_queue;
420 rx_queue->efx = efx;
421 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
422 (unsigned long)rx_queue);
423
424 return channel;
425}
426
Ben Hutchings7f967c02012-02-13 23:45:02 +0000427/* Allocate and initialise a channel structure, copying parameters
428 * (but not resources) from an old channel structure.
429 */
430static struct efx_channel *
431efx_copy_channel(const struct efx_channel *old_channel)
432{
433 struct efx_channel *channel;
434 struct efx_rx_queue *rx_queue;
435 struct efx_tx_queue *tx_queue;
436 int j;
437
438 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
439 if (!channel)
440 return NULL;
441
442 *channel = *old_channel;
443
444 channel->napi_dev = NULL;
445 memset(&channel->eventq, 0, sizeof(channel->eventq));
446
447 for (j = 0; j < EFX_TXQ_TYPES; j++) {
448 tx_queue = &channel->tx_queue[j];
449 if (tx_queue->channel)
450 tx_queue->channel = channel;
451 tx_queue->buffer = NULL;
452 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
453 }
454
455 rx_queue = &channel->rx_queue;
456 rx_queue->buffer = NULL;
457 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
458 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
459 (unsigned long)rx_queue);
460
461 return channel;
462}
463
Ben Hutchings8ceee662008-04-27 12:55:59 +0100464static int efx_probe_channel(struct efx_channel *channel)
465{
466 struct efx_tx_queue *tx_queue;
467 struct efx_rx_queue *rx_queue;
468 int rc;
469
Ben Hutchings62776d02010-06-23 11:30:07 +0000470 netif_dbg(channel->efx, probe, channel->efx->net_dev,
471 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100472
Ben Hutchings7f967c02012-02-13 23:45:02 +0000473 rc = channel->type->pre_probe(channel);
474 if (rc)
475 goto fail;
476
Ben Hutchings8ceee662008-04-27 12:55:59 +0100477 rc = efx_probe_eventq(channel);
478 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000479 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100480
481 efx_for_each_channel_tx_queue(tx_queue, channel) {
482 rc = efx_probe_tx_queue(tx_queue);
483 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000484 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100485 }
486
487 efx_for_each_channel_rx_queue(rx_queue, channel) {
488 rc = efx_probe_rx_queue(rx_queue);
489 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000490 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100491 }
492
493 channel->n_rx_frm_trunc = 0;
494
495 return 0;
496
Ben Hutchings7f967c02012-02-13 23:45:02 +0000497fail:
498 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100499 return rc;
500}
501
Ben Hutchings7f967c02012-02-13 23:45:02 +0000502static void
503efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
504{
505 struct efx_nic *efx = channel->efx;
506 const char *type;
507 int number;
508
509 number = channel->channel;
510 if (efx->tx_channel_offset == 0) {
511 type = "";
512 } else if (channel->channel < efx->tx_channel_offset) {
513 type = "-rx";
514 } else {
515 type = "-tx";
516 number -= efx->tx_channel_offset;
517 }
518 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
519}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100520
Ben Hutchings56536e92008-12-12 21:37:02 -0800521static void efx_set_channel_names(struct efx_nic *efx)
522{
523 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800524
Ben Hutchings7f967c02012-02-13 23:45:02 +0000525 efx_for_each_channel(channel, efx)
526 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100527 efx->msi_context[channel->channel].name,
528 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800529}
530
Ben Hutchings46426102010-09-10 06:42:33 +0000531static int efx_probe_channels(struct efx_nic *efx)
532{
533 struct efx_channel *channel;
534 int rc;
535
536 /* Restart special buffer allocation */
537 efx->next_buffer_table = 0;
538
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000539 /* Probe channels in reverse, so that any 'extra' channels
540 * use the start of the buffer table. This allows the traffic
541 * channels to be resized without moving them or wasting the
542 * entries before them.
543 */
544 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000545 rc = efx_probe_channel(channel);
546 if (rc) {
547 netif_err(efx, probe, efx->net_dev,
548 "failed to create channel %d\n",
549 channel->channel);
550 goto fail;
551 }
552 }
553 efx_set_channel_names(efx);
554
555 return 0;
556
557fail:
558 efx_remove_channels(efx);
559 return rc;
560}
561
Ben Hutchings8ceee662008-04-27 12:55:59 +0100562/* Channels are shutdown and reinitialised whilst the NIC is running
563 * to propagate configuration changes (mtu, checksum offload), or
564 * to clear hardware error conditions
565 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000566static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100567{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000568 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100569 struct efx_tx_queue *tx_queue;
570 struct efx_rx_queue *rx_queue;
571 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000572 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100573
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100574 /* Calculate the rx buffer allocation parameters required to
575 * support the current MTU, including padding for header
576 * alignment and overruns.
577 */
Ben Hutchings272baee2013-01-29 23:33:14 +0000578 efx->rx_dma_len = (efx->type->rx_buffer_hash_size +
579 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
580 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000581 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Ben Hutchingsc14ff2e2013-05-13 11:58:31 +0000582 NET_IP_ALIGN + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000583 if (rx_buf_len <= PAGE_SIZE) {
584 efx->rx_scatter = false;
585 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000586 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000587 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000588 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000589 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
590 EFX_RX_BUF_ALIGNMENT) >
591 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000592 efx->rx_scatter = true;
593 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
594 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000595 } else {
596 efx->rx_scatter = false;
597 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000598 }
599
Daniel Pieczko1648a232013-02-13 10:54:41 +0000600 efx_rx_config_page_split(efx);
601 if (efx->rx_buffer_order)
602 netif_dbg(efx, drv, efx->net_dev,
603 "RX buf len=%u; page order=%u batch=%u\n",
604 efx->rx_dma_len, efx->rx_buffer_order,
605 efx->rx_pages_per_batch);
606 else
607 netif_dbg(efx, drv, efx->net_dev,
608 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
609 efx->rx_dma_len, efx->rx_page_buf_step,
610 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000611
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000612 /* RX filters also have scatter-enabled flags */
613 if (efx->rx_scatter != old_rx_scatter)
Ben Hutchingsadd72472012-11-08 01:46:53 +0000614 efx->type->filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100615
Ben Hutchings14bf7182012-05-22 01:27:58 +0100616 /* We must keep at least one descriptor in a TX ring empty.
617 * We could avoid this when the queue size does not exactly
618 * match the hardware ring size, but it's not that important.
619 * Therefore we stop the queue when one more skb might fill
620 * the ring completely. We wake it when half way back to
621 * empty.
622 */
623 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
624 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
625
Ben Hutchings8ceee662008-04-27 12:55:59 +0100626 /* Initialise the channels */
627 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100628 efx_for_each_channel_tx_queue(tx_queue, channel)
629 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100630
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000631 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100632 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000633 efx_nic_generate_fill_event(rx_queue);
634 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100635
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000636 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100637 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000638
639 if (netif_device_present(efx->net_dev))
640 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100641}
642
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000643static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100644{
645 struct efx_channel *channel;
646 struct efx_tx_queue *tx_queue;
647 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100648 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100649
650 EFX_ASSERT_RESET_SERIALISED(efx);
651 BUG_ON(efx->port_enabled);
652
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100653 /* Stop RX refill */
654 efx_for_each_channel(channel, efx) {
655 efx_for_each_channel_rx_queue(rx_queue, channel)
656 rx_queue->refill_enabled = false;
657 }
658
Ben Hutchings8ceee662008-04-27 12:55:59 +0100659 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000660 /* RX packet processing is pipelined, so wait for the
661 * NAPI handler to complete. At least event queue 0
662 * might be kept active by non-data events, so don't
663 * use napi_synchronize() but actually disable NAPI
664 * temporarily.
665 */
666 if (efx_channel_has_rx_queue(channel)) {
667 efx_stop_eventq(channel);
668 efx_start_eventq(channel);
669 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100670 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100671
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100672 rc = efx->type->fini_dmaq(efx);
673 if (rc && EFX_WORKAROUND_7803(efx)) {
674 /* Schedule a reset to recover from the flush failure. The
675 * descriptor caches reference memory we're about to free,
676 * but falcon_reconfigure_mac_wrapper() won't reconnect
677 * the MACs because of the pending reset.
678 */
679 netif_err(efx, drv, efx->net_dev,
680 "Resetting to recover from flush failure\n");
681 efx_schedule_reset(efx, RESET_TYPE_ALL);
682 } else if (rc) {
683 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
684 } else {
685 netif_dbg(efx, drv, efx->net_dev,
686 "successfully flushed all queues\n");
687 }
688
689 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100690 efx_for_each_channel_rx_queue(rx_queue, channel)
691 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000692 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100693 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100694 }
695}
696
697static void efx_remove_channel(struct efx_channel *channel)
698{
699 struct efx_tx_queue *tx_queue;
700 struct efx_rx_queue *rx_queue;
701
Ben Hutchings62776d02010-06-23 11:30:07 +0000702 netif_dbg(channel->efx, drv, channel->efx->net_dev,
703 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100704
705 efx_for_each_channel_rx_queue(rx_queue, channel)
706 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000707 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100708 efx_remove_tx_queue(tx_queue);
709 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100710 channel->type->post_remove(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;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100727 int rc;
728
729 rc = efx_check_disabled(efx);
730 if (rc)
731 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000732
733 /* Not all channels should be reallocated. We must avoid
734 * reallocating their buffer table entries.
735 */
736 efx_for_each_channel(channel, efx) {
737 struct efx_rx_queue *rx_queue;
738 struct efx_tx_queue *tx_queue;
739
740 if (channel->type->copy)
741 continue;
742 next_buffer_table = max(next_buffer_table,
743 channel->eventq.index +
744 channel->eventq.entries);
745 efx_for_each_channel_rx_queue(rx_queue, channel)
746 next_buffer_table = max(next_buffer_table,
747 rx_queue->rxd.index +
748 rx_queue->rxd.entries);
749 efx_for_each_channel_tx_queue(tx_queue, channel)
750 next_buffer_table = max(next_buffer_table,
751 tx_queue->txd.index +
752 tx_queue->txd.entries);
753 }
Ben Hutchings46426102010-09-10 06:42:33 +0000754
Ben Hutchings29c69a42013-01-28 19:01:06 +0000755 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000756 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100757 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000758
Ben Hutchings7f967c02012-02-13 23:45:02 +0000759 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000760 memset(other_channel, 0, sizeof(other_channel));
761 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000762 channel = efx->channel[i];
763 if (channel->type->copy)
764 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000765 if (!channel) {
766 rc = -ENOMEM;
767 goto out;
768 }
769 other_channel[i] = channel;
770 }
771
772 /* Swap entry counts and channel pointers */
773 old_rxq_entries = efx->rxq_entries;
774 old_txq_entries = efx->txq_entries;
775 efx->rxq_entries = rxq_entries;
776 efx->txq_entries = txq_entries;
777 for (i = 0; i < efx->n_channels; i++) {
778 channel = efx->channel[i];
779 efx->channel[i] = other_channel[i];
780 other_channel[i] = channel;
781 }
782
Ben Hutchings7f967c02012-02-13 23:45:02 +0000783 /* Restart buffer table allocation */
784 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000785
Ben Hutchingse8f14992010-12-07 19:47:34 +0000786 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000787 channel = efx->channel[i];
788 if (!channel->type->copy)
789 continue;
790 rc = efx_probe_channel(channel);
791 if (rc)
792 goto rollback;
793 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000794 }
Ben Hutchings46426102010-09-10 06:42:33 +0000795
Ben Hutchings7f967c02012-02-13 23:45:02 +0000796out:
797 /* Destroy unused channel structures */
798 for (i = 0; i < efx->n_channels; i++) {
799 channel = other_channel[i];
800 if (channel && channel->type->copy) {
801 efx_fini_napi_channel(channel);
802 efx_remove_channel(channel);
803 kfree(channel);
804 }
805 }
806
Ben Hutchingsd8291182012-10-05 23:35:41 +0100807 efx_soft_enable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000808 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000809 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000810 return rc;
811
812rollback:
813 /* Swap back */
814 efx->rxq_entries = old_rxq_entries;
815 efx->txq_entries = old_txq_entries;
816 for (i = 0; i < efx->n_channels; i++) {
817 channel = efx->channel[i];
818 efx->channel[i] = other_channel[i];
819 other_channel[i] = channel;
820 }
821 goto out;
822}
823
Steve Hodgson90d683a2010-06-01 11:19:39 +0000824void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100825{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000826 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100827}
828
Ben Hutchings7f967c02012-02-13 23:45:02 +0000829static const struct efx_channel_type efx_default_channel_type = {
830 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100831 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000832 .get_name = efx_get_channel_name,
833 .copy = efx_copy_channel,
834 .keep_eventq = false,
835};
836
837int efx_channel_dummy_op_int(struct efx_channel *channel)
838{
839 return 0;
840}
841
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100842void efx_channel_dummy_op_void(struct efx_channel *channel)
843{
844}
845
Ben Hutchings8ceee662008-04-27 12:55:59 +0100846/**************************************************************************
847 *
848 * Port handling
849 *
850 **************************************************************************/
851
852/* This ensures that the kernel is kept informed (via
853 * netif_carrier_on/off) of the link status, and also maintains the
854 * link status's stop on the port's TX queue.
855 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000856void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100857{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000858 struct efx_link_state *link_state = &efx->link_state;
859
Ben Hutchings8ceee662008-04-27 12:55:59 +0100860 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
861 * that no events are triggered between unregister_netdev() and the
862 * driver unloading. A more general condition is that NETDEV_CHANGE
863 * can only be generated between NETDEV_UP and NETDEV_DOWN */
864 if (!netif_running(efx->net_dev))
865 return;
866
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000867 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100868 efx->n_link_state_changes++;
869
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000870 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100871 netif_carrier_on(efx->net_dev);
872 else
873 netif_carrier_off(efx->net_dev);
874 }
875
876 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000877 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000878 netif_info(efx, link, efx->net_dev,
879 "link up at %uMbps %s-duplex (MTU %d)%s\n",
880 link_state->speed, link_state->fd ? "full" : "half",
881 efx->net_dev->mtu,
882 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000883 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000884 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100885}
886
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000887void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
888{
889 efx->link_advertising = advertising;
890 if (advertising) {
891 if (advertising & ADVERTISED_Pause)
892 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
893 else
894 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
895 if (advertising & ADVERTISED_Asym_Pause)
896 efx->wanted_fc ^= EFX_FC_TX;
897 }
898}
899
David S. Millerb56269462011-05-17 17:53:22 -0400900void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000901{
902 efx->wanted_fc = wanted_fc;
903 if (efx->link_advertising) {
904 if (wanted_fc & EFX_FC_RX)
905 efx->link_advertising |= (ADVERTISED_Pause |
906 ADVERTISED_Asym_Pause);
907 else
908 efx->link_advertising &= ~(ADVERTISED_Pause |
909 ADVERTISED_Asym_Pause);
910 if (wanted_fc & EFX_FC_TX)
911 efx->link_advertising ^= ADVERTISED_Asym_Pause;
912 }
913}
914
Ben Hutchings115122a2009-03-04 09:52:52 +0000915static void efx_fini_port(struct efx_nic *efx);
916
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000917/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
918 * the MAC appropriately. All other PHY configuration changes are pushed
919 * through phy_op->set_settings(), and pushed asynchronously to the MAC
920 * through efx_monitor().
921 *
922 * Callers must hold the mac_lock
923 */
924int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100925{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000926 enum efx_phy_mode phy_mode;
927 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100928
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000929 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100930
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000931 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000932 netif_addr_lock_bh(efx->net_dev);
933 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100934
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000935 /* Disable PHY transmit in mac level loopbacks */
936 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800937 if (LOOPBACK_INTERNAL(efx))
938 efx->phy_mode |= PHY_MODE_TX_DISABLED;
939 else
940 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800941
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000942 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800943
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000944 if (rc)
945 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100946
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000947 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100948}
949
950/* Reinitialise the MAC to pick up new PHY settings, even if the port is
951 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000952int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100953{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000954 int rc;
955
Ben Hutchings8ceee662008-04-27 12:55:59 +0100956 EFX_ASSERT_RESET_SERIALISED(efx);
957
958 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000959 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100960 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000961
962 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100963}
964
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000965/* Asynchronous work item for changing MAC promiscuity and multicast
966 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
967 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800968static void efx_mac_work(struct work_struct *data)
969{
970 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
971
972 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100973 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100974 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800975 mutex_unlock(&efx->mac_lock);
976}
977
Ben Hutchings8ceee662008-04-27 12:55:59 +0100978static int efx_probe_port(struct efx_nic *efx)
979{
980 int rc;
981
Ben Hutchings62776d02010-06-23 11:30:07 +0000982 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100983
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000984 if (phy_flash_cfg)
985 efx->phy_mode = PHY_MODE_SPECIAL;
986
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000987 /* Connect up MAC/PHY operations table */
988 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100989 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000990 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100991
Ben Hutchingse332bcb2011-12-20 01:22:51 +0000992 /* Initialise MAC address to permanent address */
993 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100994
995 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100996}
997
998static int efx_init_port(struct efx_nic *efx)
999{
1000 int rc;
1001
Ben Hutchings62776d02010-06-23 11:30:07 +00001002 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001003
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001004 mutex_lock(&efx->mac_lock);
1005
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001006 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001007 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001008 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001009
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001010 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001011
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001012 /* Reconfigure the MAC before creating dma queues (required for
1013 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001014 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001015
1016 /* Ensure the PHY advertises the correct flow control settings */
1017 rc = efx->phy_op->reconfigure(efx);
1018 if (rc)
1019 goto fail2;
1020
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001021 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001022 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001023
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001024fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001025 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001026fail1:
1027 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001028 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001029}
1030
Ben Hutchings8ceee662008-04-27 12:55:59 +01001031static void efx_start_port(struct efx_nic *efx)
1032{
Ben Hutchings62776d02010-06-23 11:30:07 +00001033 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001034 BUG_ON(efx->port_enabled);
1035
1036 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001037 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001038
1039 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1040 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001041 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001042
Ben Hutchings8ceee662008-04-27 12:55:59 +01001043 mutex_unlock(&efx->mac_lock);
1044}
1045
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001046/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001047static void efx_stop_port(struct efx_nic *efx)
1048{
Ben Hutchings62776d02010-06-23 11:30:07 +00001049 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001050
1051 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001052 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001053 mutex_unlock(&efx->mac_lock);
1054
1055 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001056 netif_addr_lock_bh(efx->net_dev);
1057 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001058}
1059
1060static void efx_fini_port(struct efx_nic *efx)
1061{
Ben Hutchings62776d02010-06-23 11:30:07 +00001062 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001063
1064 if (!efx->port_initialized)
1065 return;
1066
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001067 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001068 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001069
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001070 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001071 efx_link_status_changed(efx);
1072}
1073
1074static void efx_remove_port(struct efx_nic *efx)
1075{
Ben Hutchings62776d02010-06-23 11:30:07 +00001076 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001077
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001078 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001079}
1080
1081/**************************************************************************
1082 *
1083 * NIC handling
1084 *
1085 **************************************************************************/
1086
1087/* This configures the PCI device to enable I/O and DMA. */
1088static int efx_init_io(struct efx_nic *efx)
1089{
1090 struct pci_dev *pci_dev = efx->pci_dev;
1091 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001092 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001093 int rc;
1094
Ben Hutchings62776d02010-06-23 11:30:07 +00001095 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001096
1097 rc = pci_enable_device(pci_dev);
1098 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001099 netif_err(efx, probe, efx->net_dev,
1100 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001101 goto fail1;
1102 }
1103
1104 pci_set_master(pci_dev);
1105
1106 /* Set the PCI DMA mask. Try all possibilities from our
1107 * genuine mask down to 32 bits, because some architectures
1108 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1109 * masks event though they reject 46 bit masks.
1110 */
1111 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001112 if (dma_supported(&pci_dev->dev, dma_mask)) {
1113 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001114 if (rc == 0)
1115 break;
1116 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001117 dma_mask >>= 1;
1118 }
1119 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001120 netif_err(efx, probe, efx->net_dev,
1121 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001122 goto fail2;
1123 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001124 netif_dbg(efx, probe, efx->net_dev,
1125 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001126 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001127 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001128 /* dma_set_coherent_mask() is not *allowed* to
1129 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001130 * but just in case...
1131 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001132 netif_err(efx, probe, efx->net_dev,
1133 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001134 goto fail2;
1135 }
1136
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001137 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1138 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001139 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001140 netif_err(efx, probe, efx->net_dev,
1141 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001142 rc = -EIO;
1143 goto fail3;
1144 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001145 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001146 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001147 netif_err(efx, probe, efx->net_dev,
1148 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001149 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001150 rc = -ENOMEM;
1151 goto fail4;
1152 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001153 netif_dbg(efx, probe, efx->net_dev,
1154 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001155 (unsigned long long)efx->membase_phys, mem_map_size,
1156 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001157
1158 return 0;
1159
1160 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001161 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001162 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001163 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001164 fail2:
1165 pci_disable_device(efx->pci_dev);
1166 fail1:
1167 return rc;
1168}
1169
1170static void efx_fini_io(struct efx_nic *efx)
1171{
Ben Hutchings62776d02010-06-23 11:30:07 +00001172 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001173
1174 if (efx->membase) {
1175 iounmap(efx->membase);
1176 efx->membase = NULL;
1177 }
1178
1179 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001180 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001181 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001182 }
1183
1184 pci_disable_device(efx->pci_dev);
1185}
1186
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001187static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001188{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001189 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001190 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001191 int cpu;
1192
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001193 if (rss_cpus) {
1194 count = rss_cpus;
1195 } else {
1196 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1197 netif_warn(efx, probe, efx->net_dev,
1198 "RSS disabled due to allocation failure\n");
1199 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001200 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001201
1202 count = 0;
1203 for_each_online_cpu(cpu) {
1204 if (!cpumask_test_cpu(cpu, thread_mask)) {
1205 ++count;
1206 cpumask_or(thread_mask, thread_mask,
1207 topology_thread_cpumask(cpu));
1208 }
1209 }
1210
1211 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001212 }
1213
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001214 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1215 * table entries that are inaccessible to VFs
1216 */
1217 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1218 count > efx_vf_size(efx)) {
1219 netif_warn(efx, probe, efx->net_dev,
1220 "Reducing number of RSS channels from %u to %u for "
1221 "VF support. Increase vf-msix-limit to use more "
1222 "channels on the PF.\n",
1223 count, efx_vf_size(efx));
1224 count = efx_vf_size(efx);
1225 }
1226
Ben Hutchings46123d02008-09-01 12:47:33 +01001227 return count;
1228}
1229
1230/* Probe the number and type of interrupts we are able to obtain, and
1231 * the resulting numbers of channels and RX queues.
1232 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001233static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001234{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001235 unsigned int extra_channels = 0;
1236 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001237 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001238
Ben Hutchings7f967c02012-02-13 23:45:02 +00001239 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1240 if (efx->extra_channel_type[i])
1241 ++extra_channels;
1242
Ben Hutchings8ceee662008-04-27 12:55:59 +01001243 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001244 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001245 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001246
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001247 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001248 if (separate_tx_channels)
1249 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001250 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001251 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001252
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001253 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001254 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001255 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001256 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001257 netif_err(efx, drv, efx->net_dev,
1258 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001259 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001260 netif_err(efx, drv, efx->net_dev,
1261 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001262 EFX_BUG_ON_PARANOID(rc >= n_channels);
1263 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001264 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001265 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001266 }
1267
1268 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001269 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001270 if (n_channels > extra_channels)
1271 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001272 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001273 efx->n_tx_channels = max(n_channels / 2, 1U);
1274 efx->n_rx_channels = max(n_channels -
1275 efx->n_tx_channels,
1276 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001277 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001278 efx->n_tx_channels = n_channels;
1279 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001280 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001281 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001282 efx_get_channel(efx, i)->irq =
1283 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001284 } else {
1285 /* Fall back to single channel MSI */
1286 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001287 netif_err(efx, drv, efx->net_dev,
1288 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001289 }
1290 }
1291
1292 /* Try single interrupt MSI */
1293 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001294 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001295 efx->n_rx_channels = 1;
1296 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001297 rc = pci_enable_msi(efx->pci_dev);
1298 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001299 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001300 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001301 netif_err(efx, drv, efx->net_dev,
1302 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001303 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1304 }
1305 }
1306
1307 /* Assume legacy interrupts */
1308 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001309 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001310 efx->n_rx_channels = 1;
1311 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001312 efx->legacy_irq = efx->pci_dev->irq;
1313 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001314
Ben Hutchings7f967c02012-02-13 23:45:02 +00001315 /* Assign extra channels if possible */
1316 j = efx->n_channels;
1317 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1318 if (!efx->extra_channel_type[i])
1319 continue;
1320 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1321 efx->n_channels <= extra_channels) {
1322 efx->extra_channel_type[i]->handle_no_channel(efx);
1323 } else {
1324 --j;
1325 efx_get_channel(efx, j)->type =
1326 efx->extra_channel_type[i];
1327 }
1328 }
1329
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001330 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001331 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001332 efx->n_rx_channels : efx_vf_size(efx));
1333
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001334 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001335}
1336
Ben Hutchingsd8291182012-10-05 23:35:41 +01001337static void efx_soft_enable_interrupts(struct efx_nic *efx)
1338{
1339 struct efx_channel *channel;
1340
1341 BUG_ON(efx->state == STATE_DISABLED);
1342
1343 efx->irq_soft_enabled = true;
1344 smp_wmb();
1345
1346 efx_for_each_channel(channel, efx) {
1347 if (!channel->type->keep_eventq)
1348 efx_init_eventq(channel);
1349 efx_start_eventq(channel);
1350 }
1351
1352 efx_mcdi_mode_event(efx);
1353}
1354
1355static void efx_soft_disable_interrupts(struct efx_nic *efx)
1356{
1357 struct efx_channel *channel;
1358
1359 if (efx->state == STATE_DISABLED)
1360 return;
1361
1362 efx_mcdi_mode_poll(efx);
1363
1364 efx->irq_soft_enabled = false;
1365 smp_wmb();
1366
1367 if (efx->legacy_irq)
1368 synchronize_irq(efx->legacy_irq);
1369
1370 efx_for_each_channel(channel, efx) {
1371 if (channel->irq)
1372 synchronize_irq(channel->irq);
1373
1374 efx_stop_eventq(channel);
1375 if (!channel->type->keep_eventq)
1376 efx_fini_eventq(channel);
1377 }
1378}
1379
1380static void efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001381{
1382 struct efx_channel *channel;
1383
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001384 BUG_ON(efx->state == STATE_DISABLED);
1385
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001386 if (efx->eeh_disabled_legacy_irq) {
1387 enable_irq(efx->legacy_irq);
1388 efx->eeh_disabled_legacy_irq = false;
1389 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001390
Ben Hutchings86094f72013-08-21 19:51:04 +01001391 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001392
1393 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001394 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001395 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001396 }
1397
Ben Hutchingsd8291182012-10-05 23:35:41 +01001398 efx_soft_enable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001399}
1400
Ben Hutchingsd8291182012-10-05 23:35:41 +01001401static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001402{
1403 struct efx_channel *channel;
1404
Ben Hutchingsd8291182012-10-05 23:35:41 +01001405 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001406
1407 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001408 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001409 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001410 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001411
Ben Hutchings86094f72013-08-21 19:51:04 +01001412 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001413}
1414
Ben Hutchings8ceee662008-04-27 12:55:59 +01001415static void efx_remove_interrupts(struct efx_nic *efx)
1416{
1417 struct efx_channel *channel;
1418
1419 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001420 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001421 channel->irq = 0;
1422 pci_disable_msi(efx->pci_dev);
1423 pci_disable_msix(efx->pci_dev);
1424
1425 /* Remove legacy interrupt */
1426 efx->legacy_irq = 0;
1427}
1428
Ben Hutchings8831da72008-09-01 12:47:48 +01001429static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001430{
Ben Hutchings602a5322011-05-16 17:32:39 +01001431 struct efx_channel *channel;
1432 struct efx_tx_queue *tx_queue;
1433
Ben Hutchings97653432011-01-12 18:26:56 +00001434 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001435 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001436
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001437 /* We need to mark which channels really have RX and TX
1438 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001439 * RX-only and TX-only channels.
1440 */
1441 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001442 if (channel->channel < efx->n_rx_channels)
1443 channel->rx_queue.core_index = channel->channel;
1444 else
1445 channel->rx_queue.core_index = -1;
1446
Ben Hutchings602a5322011-05-16 17:32:39 +01001447 efx_for_each_channel_tx_queue(tx_queue, channel)
1448 tx_queue->queue -= (efx->tx_channel_offset *
1449 EFX_TXQ_TYPES);
1450 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001451}
1452
1453static int efx_probe_nic(struct efx_nic *efx)
1454{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001455 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001456 int rc;
1457
Ben Hutchings62776d02010-06-23 11:30:07 +00001458 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001459
1460 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001461 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001462 if (rc)
1463 return rc;
1464
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001465 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001466 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001467 rc = efx_probe_interrupts(efx);
1468 if (rc)
1469 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001470
Ben Hutchings28e47c42012-02-15 01:58:49 +00001471 efx->type->dimension_resources(efx);
1472
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001473 if (efx->n_channels > 1)
1474 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001475 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001476 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001477 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001478
Ben Hutchings8831da72008-09-01 12:47:48 +01001479 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001480 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1481 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001482
1483 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001484 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1485 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001486
1487 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001488
1489fail:
1490 efx->type->remove(efx);
1491 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001492}
1493
1494static void efx_remove_nic(struct efx_nic *efx)
1495{
Ben Hutchings62776d02010-06-23 11:30:07 +00001496 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001497
1498 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001499 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001500}
1501
Ben Hutchingsadd72472012-11-08 01:46:53 +00001502static int efx_probe_filters(struct efx_nic *efx)
1503{
1504 int rc;
1505
1506 spin_lock_init(&efx->filter_lock);
1507
1508 rc = efx->type->filter_table_probe(efx);
1509 if (rc)
1510 return rc;
1511
1512#ifdef CONFIG_RFS_ACCEL
1513 if (efx->type->offload_features & NETIF_F_NTUPLE) {
1514 efx->rps_flow_id = kcalloc(efx->type->max_rx_ip_filters,
1515 sizeof(*efx->rps_flow_id),
1516 GFP_KERNEL);
1517 if (!efx->rps_flow_id) {
1518 efx->type->filter_table_remove(efx);
1519 return -ENOMEM;
1520 }
1521 }
1522#endif
1523
1524 return 0;
1525}
1526
1527static void efx_remove_filters(struct efx_nic *efx)
1528{
1529#ifdef CONFIG_RFS_ACCEL
1530 kfree(efx->rps_flow_id);
1531#endif
1532 efx->type->filter_table_remove(efx);
1533}
1534
1535static void efx_restore_filters(struct efx_nic *efx)
1536{
1537 efx->type->filter_table_restore(efx);
1538}
1539
Ben Hutchings8ceee662008-04-27 12:55:59 +01001540/**************************************************************************
1541 *
1542 * NIC startup/shutdown
1543 *
1544 *************************************************************************/
1545
1546static int efx_probe_all(struct efx_nic *efx)
1547{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001548 int rc;
1549
Ben Hutchings8ceee662008-04-27 12:55:59 +01001550 rc = efx_probe_nic(efx);
1551 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001552 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001553 goto fail1;
1554 }
1555
Ben Hutchings8ceee662008-04-27 12:55:59 +01001556 rc = efx_probe_port(efx);
1557 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001558 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001559 goto fail2;
1560 }
1561
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001562 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1563 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1564 rc = -EINVAL;
1565 goto fail3;
1566 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001567 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001568
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001569 rc = efx_probe_filters(efx);
1570 if (rc) {
1571 netif_err(efx, probe, efx->net_dev,
1572 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001573 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001574 }
1575
Ben Hutchings7f967c02012-02-13 23:45:02 +00001576 rc = efx_probe_channels(efx);
1577 if (rc)
1578 goto fail4;
1579
Ben Hutchings8ceee662008-04-27 12:55:59 +01001580 return 0;
1581
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001582 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001583 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001584 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001585 efx_remove_port(efx);
1586 fail2:
1587 efx_remove_nic(efx);
1588 fail1:
1589 return rc;
1590}
1591
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001592/* If the interface is supposed to be running but is not, start
1593 * the hardware and software data path, regular activity for the port
1594 * (MAC statistics, link polling, etc.) and schedule the port to be
1595 * reconfigured. Interrupts must already be enabled. This function
1596 * is safe to call multiple times, so long as the NIC is not disabled.
1597 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001598 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001599static void efx_start_all(struct efx_nic *efx)
1600{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001601 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001602 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001603
1604 /* Check that it is appropriate to restart the interface. All
1605 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001606 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001607 return;
1608
Ben Hutchings8ceee662008-04-27 12:55:59 +01001609 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001610 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001611
Alexandre Rames626950d2013-01-14 17:20:22 +00001612 /* Start the hardware monitor if there is one */
1613 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001614 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1615 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001616
1617 /* If link state detection is normally event-driven, we have
1618 * to poll now because we could have missed a change
1619 */
1620 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001621 mutex_lock(&efx->mac_lock);
1622 if (efx->phy_op->poll(efx))
1623 efx_link_status_changed(efx);
1624 mutex_unlock(&efx->mac_lock);
1625 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001626
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001627 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001628}
1629
1630/* Flush all delayed work. Should only be called when no more delayed work
1631 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1632 * since we're holding the rtnl_lock at this point. */
1633static void efx_flush_all(struct efx_nic *efx)
1634{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001635 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001636 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001637 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001638 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001639 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001640}
1641
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001642/* Quiesce the hardware and software data path, and regular activity
1643 * for the port without bringing the link down. Safe to call multiple
1644 * times with the NIC in almost any state, but interrupts should be
1645 * enabled. Requires the RTNL lock.
1646 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001647static void efx_stop_all(struct efx_nic *efx)
1648{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001649 EFX_ASSERT_RESET_SERIALISED(efx);
1650
1651 /* port_enabled can be read safely under the rtnl lock */
1652 if (!efx->port_enabled)
1653 return;
1654
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001655 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001656 efx_stop_port(efx);
1657
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001658 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001659 efx_flush_all(efx);
1660
Ben Hutchings29c69a42013-01-28 19:01:06 +00001661 /* Stop the kernel transmit interface. This is only valid if
1662 * the device is stopped or detached; otherwise the watchdog
1663 * may fire immediately.
1664 */
1665 WARN_ON(netif_running(efx->net_dev) &&
1666 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001667 netif_tx_disable(efx->net_dev);
1668
1669 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001670}
1671
1672static void efx_remove_all(struct efx_nic *efx)
1673{
Ben Hutchings46426102010-09-10 06:42:33 +00001674 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001675 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001676 efx_remove_port(efx);
1677 efx_remove_nic(efx);
1678}
1679
Ben Hutchings8ceee662008-04-27 12:55:59 +01001680/**************************************************************************
1681 *
1682 * Interrupt moderation
1683 *
1684 **************************************************************************/
1685
Ben Hutchingscc180b62011-12-08 19:51:47 +00001686static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001687{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001688 if (usecs == 0)
1689 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001690 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001691 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001692 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001693}
1694
Ben Hutchings8ceee662008-04-27 12:55:59 +01001695/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001696int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1697 unsigned int rx_usecs, bool rx_adaptive,
1698 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001699{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001700 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001701 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1702 efx->timer_quantum_ns,
1703 1000);
1704 unsigned int tx_ticks;
1705 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001706
1707 EFX_ASSERT_RESET_SERIALISED(efx);
1708
Ben Hutchingscc180b62011-12-08 19:51:47 +00001709 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001710 return -EINVAL;
1711
Ben Hutchingscc180b62011-12-08 19:51:47 +00001712 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1713 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1714
Ben Hutchings9e393b32011-09-05 07:43:04 +00001715 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1716 !rx_may_override_tx) {
1717 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1718 "RX and TX IRQ moderation must be equal\n");
1719 return -EINVAL;
1720 }
1721
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001722 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001723 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001724 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001725 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001726 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001727 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001728 channel->irq_moderation = tx_ticks;
1729 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001730
1731 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001732}
1733
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001734void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1735 unsigned int *rx_usecs, bool *rx_adaptive)
1736{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001737 /* We must round up when converting ticks to microseconds
1738 * because we round down when converting the other way.
1739 */
1740
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001741 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001742 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1743 efx->timer_quantum_ns,
1744 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001745
1746 /* If channels are shared between RX and TX, so is IRQ
1747 * moderation. Otherwise, IRQ moderation is the same for all
1748 * TX channels and is not adaptive.
1749 */
1750 if (efx->tx_channel_offset == 0)
1751 *tx_usecs = *rx_usecs;
1752 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001753 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001754 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001755 efx->timer_quantum_ns,
1756 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001757}
1758
Ben Hutchings8ceee662008-04-27 12:55:59 +01001759/**************************************************************************
1760 *
1761 * Hardware monitor
1762 *
1763 **************************************************************************/
1764
Ben Hutchingse254c272010-09-20 08:44:10 +00001765/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001766static void efx_monitor(struct work_struct *data)
1767{
1768 struct efx_nic *efx = container_of(data, struct efx_nic,
1769 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001770
Ben Hutchings62776d02010-06-23 11:30:07 +00001771 netif_vdbg(efx, timer, efx->net_dev,
1772 "hardware monitor executing on CPU %d\n",
1773 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001774 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001775
Ben Hutchings8ceee662008-04-27 12:55:59 +01001776 /* If the mac_lock is already held then it is likely a port
1777 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001778 * most of the work of monitor() anyway. */
1779 if (mutex_trylock(&efx->mac_lock)) {
1780 if (efx->port_enabled)
1781 efx->type->monitor(efx);
1782 mutex_unlock(&efx->mac_lock);
1783 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001784
Ben Hutchings8ceee662008-04-27 12:55:59 +01001785 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1786 efx_monitor_interval);
1787}
1788
1789/**************************************************************************
1790 *
1791 * ioctls
1792 *
1793 *************************************************************************/
1794
1795/* Net device ioctl
1796 * Context: process, rtnl_lock() held.
1797 */
1798static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1799{
Ben Hutchings767e4682008-09-01 12:43:14 +01001800 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001801 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001802
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001803 if (cmd == SIOCSHWTSTAMP)
1804 return efx_ptp_ioctl(efx, ifr, cmd);
1805
Ben Hutchings68e7f452009-04-29 08:05:08 +00001806 /* Convert phy_id from older PRTAD/DEVAD format */
1807 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1808 (data->phy_id & 0xfc00) == 0x0400)
1809 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1810
1811 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001812}
1813
1814/**************************************************************************
1815 *
1816 * NAPI interface
1817 *
1818 **************************************************************************/
1819
Ben Hutchings7f967c02012-02-13 23:45:02 +00001820static void efx_init_napi_channel(struct efx_channel *channel)
1821{
1822 struct efx_nic *efx = channel->efx;
1823
1824 channel->napi_dev = efx->net_dev;
1825 netif_napi_add(channel->napi_dev, &channel->napi_str,
1826 efx_poll, napi_weight);
1827}
1828
Ben Hutchingse8f14992010-12-07 19:47:34 +00001829static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001830{
1831 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001832
Ben Hutchings7f967c02012-02-13 23:45:02 +00001833 efx_for_each_channel(channel, efx)
1834 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001835}
1836
1837static void efx_fini_napi_channel(struct efx_channel *channel)
1838{
1839 if (channel->napi_dev)
1840 netif_napi_del(&channel->napi_str);
1841 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001842}
1843
1844static void efx_fini_napi(struct efx_nic *efx)
1845{
1846 struct efx_channel *channel;
1847
Ben Hutchingse8f14992010-12-07 19:47:34 +00001848 efx_for_each_channel(channel, efx)
1849 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001850}
1851
1852/**************************************************************************
1853 *
1854 * Kernel netpoll interface
1855 *
1856 *************************************************************************/
1857
1858#ifdef CONFIG_NET_POLL_CONTROLLER
1859
1860/* Although in the common case interrupts will be disabled, this is not
1861 * guaranteed. However, all our work happens inside the NAPI callback,
1862 * so no locking is required.
1863 */
1864static void efx_netpoll(struct net_device *net_dev)
1865{
Ben Hutchings767e4682008-09-01 12:43:14 +01001866 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001867 struct efx_channel *channel;
1868
Ben Hutchings64ee3122008-09-01 12:47:38 +01001869 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001870 efx_schedule_channel(channel);
1871}
1872
1873#endif
1874
1875/**************************************************************************
1876 *
1877 * Kernel net device interface
1878 *
1879 *************************************************************************/
1880
1881/* Context: process, rtnl_lock() held. */
1882static int efx_net_open(struct net_device *net_dev)
1883{
Ben Hutchings767e4682008-09-01 12:43:14 +01001884 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001885 int rc;
1886
Ben Hutchings62776d02010-06-23 11:30:07 +00001887 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1888 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001889
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001890 rc = efx_check_disabled(efx);
1891 if (rc)
1892 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001893 if (efx->phy_mode & PHY_MODE_SPECIAL)
1894 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001895 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1896 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001897
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001898 /* Notify the kernel of the link state polled during driver load,
1899 * before the monitor starts running */
1900 efx_link_status_changed(efx);
1901
Ben Hutchings8ceee662008-04-27 12:55:59 +01001902 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001903 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001904 return 0;
1905}
1906
1907/* Context: process, rtnl_lock() held.
1908 * Note that the kernel will ignore our return code; this method
1909 * should really be a void.
1910 */
1911static int efx_net_stop(struct net_device *net_dev)
1912{
Ben Hutchings767e4682008-09-01 12:43:14 +01001913 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001914
Ben Hutchings62776d02010-06-23 11:30:07 +00001915 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1916 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001917
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001918 /* Stop the device and flush all the channels */
1919 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001920
1921 return 0;
1922}
1923
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001924/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001925static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1926 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001927{
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 efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001930
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001931 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001932
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001933 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001934
1935 stats->rx_packets = mac_stats->rx_packets;
1936 stats->tx_packets = mac_stats->tx_packets;
1937 stats->rx_bytes = mac_stats->rx_bytes;
1938 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001939 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001940 stats->multicast = mac_stats->rx_multicast;
1941 stats->collisions = mac_stats->tx_collision;
1942 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1943 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001944 stats->rx_crc_errors = mac_stats->rx_bad;
1945 stats->rx_frame_errors = mac_stats->rx_align_error;
1946 stats->rx_fifo_errors = mac_stats->rx_overflow;
1947 stats->rx_missed_errors = mac_stats->rx_missed;
1948 stats->tx_window_errors = mac_stats->tx_late_collision;
1949
1950 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001951 stats->rx_crc_errors +
1952 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001953 mac_stats->rx_symbol_error);
1954 stats->tx_errors = (stats->tx_window_errors +
1955 mac_stats->tx_bad);
1956
Ben Hutchings1cb34522011-09-02 23:23:00 +01001957 spin_unlock_bh(&efx->stats_lock);
1958
Ben Hutchings8ceee662008-04-27 12:55:59 +01001959 return stats;
1960}
1961
1962/* Context: netif_tx_lock held, BHs disabled. */
1963static void efx_watchdog(struct net_device *net_dev)
1964{
Ben Hutchings767e4682008-09-01 12:43:14 +01001965 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001966
Ben Hutchings62776d02010-06-23 11:30:07 +00001967 netif_err(efx, tx_err, efx->net_dev,
1968 "TX stuck with port_enabled=%d: resetting channels\n",
1969 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001970
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001971 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001972}
1973
1974
1975/* Context: process, rtnl_lock() held. */
1976static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1977{
Ben Hutchings767e4682008-09-01 12:43:14 +01001978 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001979 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001980
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001981 rc = efx_check_disabled(efx);
1982 if (rc)
1983 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001984 if (new_mtu > EFX_MAX_MTU)
1985 return -EINVAL;
1986
Ben Hutchings62776d02010-06-23 11:30:07 +00001987 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001988
Ben Hutchings29c69a42013-01-28 19:01:06 +00001989 efx_device_detach_sync(efx);
1990 efx_stop_all(efx);
1991
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001992 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001993 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001994 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001995 mutex_unlock(&efx->mac_lock);
1996
Ben Hutchings8ceee662008-04-27 12:55:59 +01001997 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00001998 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001999 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002000}
2001
2002static int efx_set_mac_address(struct net_device *net_dev, void *data)
2003{
Ben Hutchings767e4682008-09-01 12:43:14 +01002004 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002005 struct sockaddr *addr = data;
2006 char *new_addr = addr->sa_data;
2007
Ben Hutchings8ceee662008-04-27 12:55:59 +01002008 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002009 netif_err(efx, drv, efx->net_dev,
2010 "invalid ethernet MAC address requested: %pM\n",
2011 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002012 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002013 }
2014
2015 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002016 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002017
2018 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002019 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002020 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002021 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002022
2023 return 0;
2024}
2025
Ben Hutchingsa816f752008-09-01 12:49:12 +01002026/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002027static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002028{
Ben Hutchings767e4682008-09-01 12:43:14 +01002029 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002030 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002031 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002032 u32 crc;
2033 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002034
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002035 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002036
2037 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002038 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002039 memset(mc_hash, 0xff, sizeof(*mc_hash));
2040 } else {
2041 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00002042 netdev_for_each_mc_addr(ha, net_dev) {
2043 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002044 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
Ben Hutchings32766ec2012-10-04 17:13:03 -07002045 __set_bit_le(bit, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002046 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002047
2048 /* Broadcast packets go through the multicast hash filter.
2049 * ether_crc_le() of the broadcast address is 0xbe2612ff
2050 * so we always add bit 0xff to the mask.
2051 */
Ben Hutchings32766ec2012-10-04 17:13:03 -07002052 __set_bit_le(0xff, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002053 }
2054
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002055 if (efx->port_enabled)
2056 queue_work(efx->workqueue, &efx->mac_work);
2057 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002058}
2059
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002060static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002061{
2062 struct efx_nic *efx = netdev_priv(net_dev);
2063
2064 /* If disabling RX n-tuple filtering, clear existing filters */
2065 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2066 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2067
2068 return 0;
2069}
2070
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002071static const struct net_device_ops efx_netdev_ops = {
2072 .ndo_open = efx_net_open,
2073 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002074 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002075 .ndo_tx_timeout = efx_watchdog,
2076 .ndo_start_xmit = efx_hard_start_xmit,
2077 .ndo_validate_addr = eth_validate_addr,
2078 .ndo_do_ioctl = efx_ioctl,
2079 .ndo_change_mtu = efx_change_mtu,
2080 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002081 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002082 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002083#ifdef CONFIG_SFC_SRIOV
2084 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2085 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2086 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2087 .ndo_get_vf_config = efx_sriov_get_vf_config,
2088#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002089#ifdef CONFIG_NET_POLL_CONTROLLER
2090 .ndo_poll_controller = efx_netpoll,
2091#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002092 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002093#ifdef CONFIG_RFS_ACCEL
2094 .ndo_rx_flow_steer = efx_filter_rfs,
2095#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002096};
2097
Ben Hutchings7dde5962008-12-12 22:09:38 -08002098static void efx_update_name(struct efx_nic *efx)
2099{
2100 strcpy(efx->name, efx->net_dev->name);
2101 efx_mtd_rename(efx);
2102 efx_set_channel_names(efx);
2103}
2104
Ben Hutchings8ceee662008-04-27 12:55:59 +01002105static int efx_netdev_event(struct notifier_block *this,
2106 unsigned long event, void *ptr)
2107{
Jiri Pirko351638e2013-05-28 01:30:21 +00002108 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002109
Ben Hutchings7dde5962008-12-12 22:09:38 -08002110 if (net_dev->netdev_ops == &efx_netdev_ops &&
2111 event == NETDEV_CHANGENAME)
2112 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002113
2114 return NOTIFY_DONE;
2115}
2116
2117static struct notifier_block efx_netdev_notifier = {
2118 .notifier_call = efx_netdev_event,
2119};
2120
Ben Hutchings06d5e192008-12-12 21:47:23 -08002121static ssize_t
2122show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2123{
2124 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2125 return sprintf(buf, "%d\n", efx->phy_type);
2126}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002127static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002128
Ben Hutchings8ceee662008-04-27 12:55:59 +01002129static int efx_register_netdev(struct efx_nic *efx)
2130{
2131 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002132 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002133 int rc;
2134
2135 net_dev->watchdog_timeo = 5 * HZ;
2136 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002137 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002138 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002139 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002140
Ben Hutchings7dde5962008-12-12 22:09:38 -08002141 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002142
Ben Hutchings7153f622012-07-27 20:50:52 +01002143 /* Enable resets to be scheduled and check whether any were
2144 * already requested. If so, the NIC is probably hosed so we
2145 * abort.
2146 */
2147 efx->state = STATE_READY;
2148 smp_mb(); /* ensure we change state before checking reset_pending */
2149 if (efx->reset_pending) {
2150 netif_err(efx, probe, efx->net_dev,
2151 "aborting probe due to scheduled reset\n");
2152 rc = -EIO;
2153 goto fail_locked;
2154 }
2155
Ben Hutchingsaed06282009-08-26 08:16:27 +00002156 rc = dev_alloc_name(net_dev, net_dev->name);
2157 if (rc < 0)
2158 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002159 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002160
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002161 /* Always start with carrier off; PHY events will detect the link */
2162 netif_carrier_off(net_dev);
2163
Ben Hutchingsaed06282009-08-26 08:16:27 +00002164 rc = register_netdevice(net_dev);
2165 if (rc)
2166 goto fail_locked;
2167
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002168 efx_for_each_channel(channel, efx) {
2169 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002170 efx_for_each_channel_tx_queue(tx_queue, channel)
2171 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002172 }
2173
Ben Hutchings7dde5962008-12-12 22:09:38 -08002174 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002175
Ben Hutchings06d5e192008-12-12 21:47:23 -08002176 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2177 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002178 netif_err(efx, drv, efx->net_dev,
2179 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002180 goto fail_registered;
2181 }
2182
Ben Hutchings8ceee662008-04-27 12:55:59 +01002183 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002184
Ben Hutchings7153f622012-07-27 20:50:52 +01002185fail_registered:
2186 rtnl_lock();
2187 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002188fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002189 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002190 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002191 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002192 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002193}
2194
2195static void efx_unregister_netdev(struct efx_nic *efx)
2196{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002197 if (!efx->net_dev)
2198 return;
2199
Ben Hutchings767e4682008-09-01 12:43:14 +01002200 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002201
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002202 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2203 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002204
2205 rtnl_lock();
2206 unregister_netdevice(efx->net_dev);
2207 efx->state = STATE_UNINIT;
2208 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002209}
2210
2211/**************************************************************************
2212 *
2213 * Device reset and suspend
2214 *
2215 **************************************************************************/
2216
Ben Hutchings2467ca42008-09-01 12:48:50 +01002217/* Tears down the entire software state and most of the hardware state
2218 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002219void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002220{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002221 EFX_ASSERT_RESET_SERIALISED(efx);
2222
Ben Hutchings2467ca42008-09-01 12:48:50 +01002223 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002224 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002225
2226 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002227 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2228 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002229 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002230}
2231
Ben Hutchings2467ca42008-09-01 12:48:50 +01002232/* This function will always ensure that the locks acquired in
2233 * efx_reset_down() are released. A failure return code indicates
2234 * that we were unable to reinitialise the hardware, and the
2235 * driver should be disabled. If ok is false, then the rx and tx
2236 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002237int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002238{
2239 int rc;
2240
Ben Hutchings2467ca42008-09-01 12:48:50 +01002241 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002242
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002243 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002244 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002245 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002246 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002247 }
2248
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002249 if (!ok)
2250 goto fail;
2251
Steve Hodgson4b988282009-01-29 17:50:51 +00002252 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002253 rc = efx->phy_op->init(efx);
2254 if (rc)
2255 goto fail;
2256 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002257 netif_err(efx, drv, efx->net_dev,
2258 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002259 }
2260
Ben Hutchings710b2082011-09-03 00:15:00 +01002261 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002262
Ben Hutchingsd8291182012-10-05 23:35:41 +01002263 efx_enable_interrupts(efx);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002264 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002265 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002266
2267 mutex_unlock(&efx->mac_lock);
2268
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002269 efx_start_all(efx);
2270
2271 return 0;
2272
2273fail:
2274 efx->port_initialized = false;
2275
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002276 mutex_unlock(&efx->mac_lock);
2277
Ben Hutchings8ceee662008-04-27 12:55:59 +01002278 return rc;
2279}
2280
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002281/* Reset the NIC using the specified method. Note that the reset may
2282 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002283 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002284 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002285 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002286int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002287{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002288 int rc, rc2;
2289 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002290
Ben Hutchings62776d02010-06-23 11:30:07 +00002291 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2292 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002293
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002294 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002295 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002296
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002297 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002298 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002299 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002300 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002301 }
2302
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002303 /* Clear flags for the scopes we covered. We assume the NIC and
2304 * driver are now quiescent so that there is no race here.
2305 */
2306 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002307
2308 /* Reinitialise bus-mastering, which may have been turned off before
2309 * the reset was scheduled. This is still appropriate, even in the
2310 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2311 * can respond to requests. */
2312 pci_set_master(efx->pci_dev);
2313
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002314out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002315 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002316 disabled = rc ||
2317 method == RESET_TYPE_DISABLE ||
2318 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002319 rc2 = efx_reset_up(efx, method, !disabled);
2320 if (rc2) {
2321 disabled = true;
2322 if (!rc)
2323 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002324 }
2325
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002326 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002327 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002328 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002329 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002330 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002331 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002332 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002333 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002334 return rc;
2335}
2336
Alexandre Rames626950d2013-01-14 17:20:22 +00002337/* Try recovery mechanisms.
2338 * For now only EEH is supported.
2339 * Returns 0 if the recovery mechanisms are unsuccessful.
2340 * Returns a non-zero value otherwise.
2341 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002342int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002343{
2344#ifdef CONFIG_EEH
2345 /* A PCI error can occur and not be seen by EEH because nothing
2346 * happens on the PCI bus. In this case the driver may fail and
2347 * schedule a 'recover or reset', leading to this recovery handler.
2348 * Manually call the eeh failure check function.
2349 */
2350 struct eeh_dev *eehdev =
2351 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2352
2353 if (eeh_dev_check_failure(eehdev)) {
2354 /* The EEH mechanisms will handle the error and reset the
2355 * device if necessary.
2356 */
2357 return 1;
2358 }
2359#endif
2360 return 0;
2361}
2362
Ben Hutchings8ceee662008-04-27 12:55:59 +01002363/* The worker thread exists so that code that cannot sleep can
2364 * schedule a reset for later.
2365 */
2366static void efx_reset_work(struct work_struct *data)
2367{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002368 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002369 unsigned long pending;
2370 enum reset_type method;
2371
2372 pending = ACCESS_ONCE(efx->reset_pending);
2373 method = fls(pending) - 1;
2374
2375 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2376 method == RESET_TYPE_RECOVER_OR_ALL) &&
2377 efx_try_recovery(efx))
2378 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002379
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002380 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002381 return;
2382
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002383 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002384
2385 /* We checked the state in efx_schedule_reset() but it may
2386 * have changed by now. Now that we have the RTNL lock,
2387 * it cannot change again.
2388 */
2389 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002390 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002391
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002392 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002393}
2394
2395void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2396{
2397 enum reset_type method;
2398
Alexandre Rames626950d2013-01-14 17:20:22 +00002399 if (efx->state == STATE_RECOVERY) {
2400 netif_dbg(efx, drv, efx->net_dev,
2401 "recovering: skip scheduling %s reset\n",
2402 RESET_TYPE(type));
2403 return;
2404 }
2405
Ben Hutchings8ceee662008-04-27 12:55:59 +01002406 switch (type) {
2407 case RESET_TYPE_INVISIBLE:
2408 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002409 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002410 case RESET_TYPE_WORLD:
2411 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002412 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002413 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002414 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2415 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002416 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002417 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002418 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002419 netif_dbg(efx, drv, efx->net_dev,
2420 "scheduling %s reset for %s\n",
2421 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002422 break;
2423 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002424
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002425 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002426 smp_mb(); /* ensure we change reset_pending before checking state */
2427
2428 /* If we're not READY then just leave the flags set as the cue
2429 * to abort probing or reschedule the reset later.
2430 */
2431 if (ACCESS_ONCE(efx->state) != STATE_READY)
2432 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002433
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002434 /* efx_process_channel() will no longer read events once a
2435 * reset is scheduled. So switch back to poll'd MCDI completions. */
2436 efx_mcdi_mode_poll(efx);
2437
Steve Hodgson1ab00622008-12-12 21:33:02 -08002438 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002439}
2440
2441/**************************************************************************
2442 *
2443 * List of NICs we support
2444 *
2445 **************************************************************************/
2446
2447/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002448static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002449 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2450 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002451 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002452 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2453 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002454 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002455 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002456 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002457 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002458 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002459 {0} /* end of list */
2460};
2461
2462/**************************************************************************
2463 *
Ben Hutchings37594332009-11-23 16:05:45 +00002464 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002465 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002466 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002467 * Needed so all function pointers are valid and do not have to be tested
2468 * before use
2469 *
2470 **************************************************************************/
2471int efx_port_dummy_op_int(struct efx_nic *efx)
2472{
2473 return 0;
2474}
2475void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002476
2477static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002478{
2479 return false;
2480}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002481
stephen hemminger6c8c2512011-04-14 05:50:12 +00002482static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002483 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002484 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002485 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002486 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002487};
2488
Ben Hutchings8ceee662008-04-27 12:55:59 +01002489/**************************************************************************
2490 *
2491 * Data housekeeping
2492 *
2493 **************************************************************************/
2494
2495/* This zeroes out and then fills in the invariants in a struct
2496 * efx_nic (including all sub-structures).
2497 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002498static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002499 struct pci_dev *pci_dev, struct net_device *net_dev)
2500{
Ben Hutchings46426102010-09-10 06:42:33 +00002501 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002502
2503 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002504 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002505#ifdef CONFIG_SFC_MTD
2506 INIT_LIST_HEAD(&efx->mtd_list);
2507#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002508 INIT_WORK(&efx->reset_work, efx_reset_work);
2509 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002510 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002511 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002512 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002513 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002514 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002515
2516 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002517 spin_lock_init(&efx->stats_lock);
2518 mutex_init(&efx->mac_lock);
2519 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002520 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002521 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002522 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002523
2524 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002525 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2526 if (!efx->channel[i])
2527 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002528 efx->msi_context[i].efx = efx;
2529 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002530 }
2531
Ben Hutchings8ceee662008-04-27 12:55:59 +01002532 /* Higher numbered interrupt modes are less capable! */
2533 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2534 interrupt_mode);
2535
Ben Hutchings6977dc62008-12-26 13:44:39 -08002536 /* Would be good to use the net_dev name, but we're too early */
2537 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2538 pci_name(pci_dev));
2539 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002540 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002541 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002542
Ben Hutchings8ceee662008-04-27 12:55:59 +01002543 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002544
2545fail:
2546 efx_fini_struct(efx);
2547 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002548}
2549
2550static void efx_fini_struct(struct efx_nic *efx)
2551{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002552 int i;
2553
2554 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2555 kfree(efx->channel[i]);
2556
Ben Hutchings8ceee662008-04-27 12:55:59 +01002557 if (efx->workqueue) {
2558 destroy_workqueue(efx->workqueue);
2559 efx->workqueue = NULL;
2560 }
2561}
2562
2563/**************************************************************************
2564 *
2565 * PCI interface
2566 *
2567 **************************************************************************/
2568
2569/* Main body of final NIC shutdown code
2570 * This is called only at module unload (or hotplug removal).
2571 */
2572static void efx_pci_remove_main(struct efx_nic *efx)
2573{
Ben Hutchings7153f622012-07-27 20:50:52 +01002574 /* Flush reset_work. It can no longer be scheduled since we
2575 * are not READY.
2576 */
2577 BUG_ON(efx->state == STATE_READY);
2578 cancel_work_sync(&efx->reset_work);
2579
Ben Hutchingsd8291182012-10-05 23:35:41 +01002580 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002581 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002582 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002583 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002584 efx_fini_napi(efx);
2585 efx_remove_all(efx);
2586}
2587
2588/* Final NIC shutdown
2589 * This is called only at module unload (or hotplug removal).
2590 */
2591static void efx_pci_remove(struct pci_dev *pci_dev)
2592{
2593 struct efx_nic *efx;
2594
2595 efx = pci_get_drvdata(pci_dev);
2596 if (!efx)
2597 return;
2598
2599 /* Mark the NIC as fini, then stop the interface */
2600 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002601 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002602 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002603 rtnl_unlock();
2604
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002605 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002606 efx_unregister_netdev(efx);
2607
Ben Hutchings7dde5962008-12-12 22:09:38 -08002608 efx_mtd_remove(efx);
2609
Ben Hutchings8ceee662008-04-27 12:55:59 +01002610 efx_pci_remove_main(efx);
2611
Ben Hutchings8ceee662008-04-27 12:55:59 +01002612 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002613 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002614
Ben Hutchings8ceee662008-04-27 12:55:59 +01002615 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002616 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002617 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002618
2619 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002620};
2621
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002622/* NIC VPD information
2623 * Called during probe to display the part number of the
2624 * installed NIC. VPD is potentially very large but this should
2625 * always appear within the first 512 bytes.
2626 */
2627#define SFC_VPD_LEN 512
2628static void efx_print_product_vpd(struct efx_nic *efx)
2629{
2630 struct pci_dev *dev = efx->pci_dev;
2631 char vpd_data[SFC_VPD_LEN];
2632 ssize_t vpd_size;
2633 int i, j;
2634
2635 /* Get the vpd data from the device */
2636 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2637 if (vpd_size <= 0) {
2638 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2639 return;
2640 }
2641
2642 /* Get the Read only section */
2643 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2644 if (i < 0) {
2645 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2646 return;
2647 }
2648
2649 j = pci_vpd_lrdt_size(&vpd_data[i]);
2650 i += PCI_VPD_LRDT_TAG_SIZE;
2651 if (i + j > vpd_size)
2652 j = vpd_size - i;
2653
2654 /* Get the Part number */
2655 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2656 if (i < 0) {
2657 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2658 return;
2659 }
2660
2661 j = pci_vpd_info_field_size(&vpd_data[i]);
2662 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2663 if (i + j > vpd_size) {
2664 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2665 return;
2666 }
2667
2668 netif_info(efx, drv, efx->net_dev,
2669 "Part Number : %.*s\n", j, &vpd_data[i]);
2670}
2671
2672
Ben Hutchings8ceee662008-04-27 12:55:59 +01002673/* Main body of NIC initialisation
2674 * This is called at module load (or hotplug insertion, theoretically).
2675 */
2676static int efx_pci_probe_main(struct efx_nic *efx)
2677{
2678 int rc;
2679
2680 /* Do start-of-day initialisation */
2681 rc = efx_probe_all(efx);
2682 if (rc)
2683 goto fail1;
2684
Ben Hutchingse8f14992010-12-07 19:47:34 +00002685 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002686
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002687 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002688 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002689 netif_err(efx, probe, efx->net_dev,
2690 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002691 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002692 }
2693
2694 rc = efx_init_port(efx);
2695 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002696 netif_err(efx, probe, efx->net_dev,
2697 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002698 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002699 }
2700
Ben Hutchings152b6a62009-11-29 03:43:56 +00002701 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002702 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002703 goto fail5;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002704 efx_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002705
2706 return 0;
2707
Ben Hutchings278c0622009-11-23 16:05:12 +00002708 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002709 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002710 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002711 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002712 fail3:
2713 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002714 efx_remove_all(efx);
2715 fail1:
2716 return rc;
2717}
2718
2719/* NIC initialisation
2720 *
2721 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002722 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002723 * sets up and registers the network devices with the kernel and hooks
2724 * the interrupt service routine. It does not prepare the device for
2725 * transmission; this is left to the first time one of the network
2726 * interfaces is brought up (i.e. efx_net_open).
2727 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002728static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002729 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002730{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002731 struct net_device *net_dev;
2732 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002733 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002734
2735 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002736 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2737 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002738 if (!net_dev)
2739 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002740 efx = netdev_priv(net_dev);
2741 efx->type = (const struct efx_nic_type *) entry->driver_data;
2742 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002743 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002744 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002745 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002746 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002747 /* Mask for features that also apply to VLAN devices */
2748 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002749 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2750 NETIF_F_RXCSUM);
2751 /* All offloads can be toggled */
2752 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002753 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002754 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002755 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002756 if (rc)
2757 goto fail1;
2758
Ben Hutchings62776d02010-06-23 11:30:07 +00002759 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002760 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002761
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002762 efx_print_product_vpd(efx);
2763
Ben Hutchings8ceee662008-04-27 12:55:59 +01002764 /* Set up basic I/O (BAR mappings etc) */
2765 rc = efx_init_io(efx);
2766 if (rc)
2767 goto fail2;
2768
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002769 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002770 if (rc)
2771 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002772
Ben Hutchings8ceee662008-04-27 12:55:59 +01002773 rc = efx_register_netdev(efx);
2774 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002775 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002776
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002777 rc = efx_sriov_init(efx);
2778 if (rc)
2779 netif_err(efx, probe, efx->net_dev,
2780 "SR-IOV can't be enabled rc %d\n", rc);
2781
Ben Hutchings62776d02010-06-23 11:30:07 +00002782 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002783
Ben Hutchings7c431612012-01-27 17:23:58 +00002784 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002785 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002786 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002787 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002788 if (rc)
2789 netif_warn(efx, probe, efx->net_dev,
2790 "failed to create MTDs (%d)\n", rc);
2791
Alexandre Rames626950d2013-01-14 17:20:22 +00002792 rc = pci_enable_pcie_error_reporting(pci_dev);
2793 if (rc && rc != -EINVAL)
2794 netif_warn(efx, probe, efx->net_dev,
2795 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2796
Ben Hutchings8ceee662008-04-27 12:55:59 +01002797 return 0;
2798
Ben Hutchings8ceee662008-04-27 12:55:59 +01002799 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002800 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002801 fail3:
2802 efx_fini_io(efx);
2803 fail2:
2804 efx_fini_struct(efx);
2805 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002806 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002807 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002808 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002809 free_netdev(net_dev);
2810 return rc;
2811}
2812
Ben Hutchings89c758f2009-11-29 03:43:07 +00002813static int efx_pm_freeze(struct device *dev)
2814{
2815 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2816
Ben Hutchings61da0262012-07-27 19:35:39 +01002817 rtnl_lock();
2818
Ben Hutchings6032fb52012-07-27 19:35:47 +01002819 if (efx->state != STATE_DISABLED) {
2820 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002821
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002822 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002823
Ben Hutchings6032fb52012-07-27 19:35:47 +01002824 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002825 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01002826 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002827
Ben Hutchings61da0262012-07-27 19:35:39 +01002828 rtnl_unlock();
2829
Ben Hutchings89c758f2009-11-29 03:43:07 +00002830 return 0;
2831}
2832
2833static int efx_pm_thaw(struct device *dev)
2834{
2835 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2836
Ben Hutchings61da0262012-07-27 19:35:39 +01002837 rtnl_lock();
2838
Ben Hutchings6032fb52012-07-27 19:35:47 +01002839 if (efx->state != STATE_DISABLED) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01002840 efx_enable_interrupts(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002841
Ben Hutchings6032fb52012-07-27 19:35:47 +01002842 mutex_lock(&efx->mac_lock);
2843 efx->phy_op->reconfigure(efx);
2844 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002845
Ben Hutchings6032fb52012-07-27 19:35:47 +01002846 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002847
Ben Hutchings6032fb52012-07-27 19:35:47 +01002848 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002849
Ben Hutchings6032fb52012-07-27 19:35:47 +01002850 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002851
Ben Hutchings6032fb52012-07-27 19:35:47 +01002852 efx->type->resume_wol(efx);
2853 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002854
Ben Hutchings61da0262012-07-27 19:35:39 +01002855 rtnl_unlock();
2856
Steve Hodgson319ba642010-06-01 11:17:24 +00002857 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2858 queue_work(reset_workqueue, &efx->reset_work);
2859
Ben Hutchings89c758f2009-11-29 03:43:07 +00002860 return 0;
2861}
2862
2863static int efx_pm_poweroff(struct device *dev)
2864{
2865 struct pci_dev *pci_dev = to_pci_dev(dev);
2866 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2867
2868 efx->type->fini(efx);
2869
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002870 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002871
2872 pci_save_state(pci_dev);
2873 return pci_set_power_state(pci_dev, PCI_D3hot);
2874}
2875
2876/* Used for both resume and restore */
2877static int efx_pm_resume(struct device *dev)
2878{
2879 struct pci_dev *pci_dev = to_pci_dev(dev);
2880 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2881 int rc;
2882
2883 rc = pci_set_power_state(pci_dev, PCI_D0);
2884 if (rc)
2885 return rc;
2886 pci_restore_state(pci_dev);
2887 rc = pci_enable_device(pci_dev);
2888 if (rc)
2889 return rc;
2890 pci_set_master(efx->pci_dev);
2891 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2892 if (rc)
2893 return rc;
2894 rc = efx->type->init(efx);
2895 if (rc)
2896 return rc;
2897 efx_pm_thaw(dev);
2898 return 0;
2899}
2900
2901static int efx_pm_suspend(struct device *dev)
2902{
2903 int rc;
2904
2905 efx_pm_freeze(dev);
2906 rc = efx_pm_poweroff(dev);
2907 if (rc)
2908 efx_pm_resume(dev);
2909 return rc;
2910}
2911
Ben Hutchings18e83e42012-01-05 19:05:20 +00002912static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002913 .suspend = efx_pm_suspend,
2914 .resume = efx_pm_resume,
2915 .freeze = efx_pm_freeze,
2916 .thaw = efx_pm_thaw,
2917 .poweroff = efx_pm_poweroff,
2918 .restore = efx_pm_resume,
2919};
2920
Alexandre Rames626950d2013-01-14 17:20:22 +00002921/* A PCI error affecting this device was detected.
2922 * At this point MMIO and DMA may be disabled.
2923 * Stop the software path and request a slot reset.
2924 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002925static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2926 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00002927{
2928 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2929 struct efx_nic *efx = pci_get_drvdata(pdev);
2930
2931 if (state == pci_channel_io_perm_failure)
2932 return PCI_ERS_RESULT_DISCONNECT;
2933
2934 rtnl_lock();
2935
2936 if (efx->state != STATE_DISABLED) {
2937 efx->state = STATE_RECOVERY;
2938 efx->reset_pending = 0;
2939
2940 efx_device_detach_sync(efx);
2941
2942 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002943 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00002944
2945 status = PCI_ERS_RESULT_NEED_RESET;
2946 } else {
2947 /* If the interface is disabled we don't want to do anything
2948 * with it.
2949 */
2950 status = PCI_ERS_RESULT_RECOVERED;
2951 }
2952
2953 rtnl_unlock();
2954
2955 pci_disable_device(pdev);
2956
2957 return status;
2958}
2959
2960/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002961static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00002962{
2963 struct efx_nic *efx = pci_get_drvdata(pdev);
2964 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2965 int rc;
2966
2967 if (pci_enable_device(pdev)) {
2968 netif_err(efx, hw, efx->net_dev,
2969 "Cannot re-enable PCI device after reset.\n");
2970 status = PCI_ERS_RESULT_DISCONNECT;
2971 }
2972
2973 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2974 if (rc) {
2975 netif_err(efx, hw, efx->net_dev,
2976 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2977 /* Non-fatal error. Continue. */
2978 }
2979
2980 return status;
2981}
2982
2983/* Perform the actual reset and resume I/O operations. */
2984static void efx_io_resume(struct pci_dev *pdev)
2985{
2986 struct efx_nic *efx = pci_get_drvdata(pdev);
2987 int rc;
2988
2989 rtnl_lock();
2990
2991 if (efx->state == STATE_DISABLED)
2992 goto out;
2993
2994 rc = efx_reset(efx, RESET_TYPE_ALL);
2995 if (rc) {
2996 netif_err(efx, hw, efx->net_dev,
2997 "efx_reset failed after PCI error (%d)\n", rc);
2998 } else {
2999 efx->state = STATE_READY;
3000 netif_dbg(efx, hw, efx->net_dev,
3001 "Done resetting and resuming IO after PCI error.\n");
3002 }
3003
3004out:
3005 rtnl_unlock();
3006}
3007
3008/* For simplicity and reliability, we always require a slot reset and try to
3009 * reset the hardware when a pci error affecting the device is detected.
3010 * We leave both the link_reset and mmio_enabled callback unimplemented:
3011 * with our request for slot reset the mmio_enabled callback will never be
3012 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3013 */
3014static struct pci_error_handlers efx_err_handlers = {
3015 .error_detected = efx_io_error_detected,
3016 .slot_reset = efx_io_slot_reset,
3017 .resume = efx_io_resume,
3018};
3019
Ben Hutchings8ceee662008-04-27 12:55:59 +01003020static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003021 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003022 .id_table = efx_pci_table,
3023 .probe = efx_pci_probe,
3024 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003025 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003026 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003027};
3028
3029/**************************************************************************
3030 *
3031 * Kernel module interface
3032 *
3033 *************************************************************************/
3034
3035module_param(interrupt_mode, uint, 0444);
3036MODULE_PARM_DESC(interrupt_mode,
3037 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3038
3039static int __init efx_init_module(void)
3040{
3041 int rc;
3042
3043 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3044
3045 rc = register_netdevice_notifier(&efx_netdev_notifier);
3046 if (rc)
3047 goto err_notifier;
3048
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003049 rc = efx_init_sriov();
3050 if (rc)
3051 goto err_sriov;
3052
Steve Hodgson1ab00622008-12-12 21:33:02 -08003053 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3054 if (!reset_workqueue) {
3055 rc = -ENOMEM;
3056 goto err_reset;
3057 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003058
3059 rc = pci_register_driver(&efx_pci_driver);
3060 if (rc < 0)
3061 goto err_pci;
3062
3063 return 0;
3064
3065 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003066 destroy_workqueue(reset_workqueue);
3067 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003068 efx_fini_sriov();
3069 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003070 unregister_netdevice_notifier(&efx_netdev_notifier);
3071 err_notifier:
3072 return rc;
3073}
3074
3075static void __exit efx_exit_module(void)
3076{
3077 printk(KERN_INFO "Solarflare NET driver unloading\n");
3078
3079 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003080 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003081 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003082 unregister_netdevice_notifier(&efx_netdev_notifier);
3083
3084}
3085
3086module_init(efx_init_module);
3087module_exit(efx_exit_module);
3088
Ben Hutchings906bb262009-11-29 15:16:19 +00003089MODULE_AUTHOR("Solarflare Communications and "
3090 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003091MODULE_DESCRIPTION("Solarflare Communications network driver");
3092MODULE_LICENSE("GPL");
3093MODULE_DEVICE_TABLE(pci, efx_pci_table);