blob: a7818d1c2415782ad739807595821d3ad0e50466 [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);
Alexandre Rames97d48a12013-01-11 12:26:21 +0000251 if (rx_queue->enabled)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000252 efx_fast_push_rx_descriptors(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100253 }
254
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000255 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100256}
257
Ben Hutchings8ceee662008-04-27 12:55:59 +0100258/* NAPI poll handler
259 *
260 * NAPI guarantees serialisation of polls of the same device, which
261 * provides the guarantee required by efx_process_channel().
262 */
263static int efx_poll(struct napi_struct *napi, int budget)
264{
265 struct efx_channel *channel =
266 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000267 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000268 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100269
Ben Hutchings62776d02010-06-23 11:30:07 +0000270 netif_vdbg(efx, intr, efx->net_dev,
271 "channel %d NAPI poll executing on CPU %d\n",
272 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100273
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000274 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100275
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000276 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000277 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000278 efx->irq_rx_adaptive &&
279 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000280 if (unlikely(channel->irq_mod_score <
281 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000282 if (channel->irq_moderation > 1) {
283 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000284 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000285 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000286 } else if (unlikely(channel->irq_mod_score >
287 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000288 if (channel->irq_moderation <
289 efx->irq_rx_moderation) {
290 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000291 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000292 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000293 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000294 channel->irq_count = 0;
295 channel->irq_mod_score = 0;
296 }
297
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000298 efx_filter_rfs_expire(channel);
299
Ben Hutchings8ceee662008-04-27 12:55:59 +0100300 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800301 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100302 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100303 * interrupts have already been disabled.
304 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800305 napi_complete(napi);
Ben Hutchings514bedb2012-10-05 19:30:16 +0100306 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100307 }
308
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000309 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100310}
311
Ben Hutchings8ceee662008-04-27 12:55:59 +0100312/* Create event queue
313 * Event queue memory allocations are done only once. If the channel
314 * is reset, the memory buffer will be reused; this guards against
315 * errors during channel reset and also simplifies interrupt handling.
316 */
317static int efx_probe_eventq(struct efx_channel *channel)
318{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000319 struct efx_nic *efx = channel->efx;
320 unsigned long entries;
321
Ben Hutchings86ee5302012-01-09 19:51:22 +0000322 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000323 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100324
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000325 /* Build an event queue with room for one event per tx and rx buffer,
326 * plus some extra for link state events and MCDI completions. */
327 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
328 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
329 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
330
Ben Hutchings152b6a62009-11-29 03:43:56 +0000331 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100332}
333
334/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100335static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100336{
Ben Hutchings62776d02010-06-23 11:30:07 +0000337 netif_dbg(channel->efx, drv, channel->efx->net_dev,
338 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100339
340 channel->eventq_read_ptr = 0;
341
Ben Hutchings152b6a62009-11-29 03:43:56 +0000342 efx_nic_init_eventq(channel);
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 Hutchings62776d02010-06-23 11:30:07 +0000371 netif_dbg(channel->efx, drv, channel->efx->net_dev,
372 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100373
Ben Hutchings152b6a62009-11-29 03:43:56 +0000374 efx_nic_fini_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100375}
376
377static void efx_remove_eventq(struct efx_channel *channel)
378{
Ben Hutchings62776d02010-06-23 11:30:07 +0000379 netif_dbg(channel->efx, drv, channel->efx->net_dev,
380 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100381
Ben Hutchings152b6a62009-11-29 03:43:56 +0000382 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100383}
384
385/**************************************************************************
386 *
387 * Channel handling
388 *
389 *************************************************************************/
390
Ben Hutchings7f967c02012-02-13 23:45:02 +0000391/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000392static struct efx_channel *
393efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
394{
395 struct efx_channel *channel;
396 struct efx_rx_queue *rx_queue;
397 struct efx_tx_queue *tx_queue;
398 int j;
399
Ben Hutchings7f967c02012-02-13 23:45:02 +0000400 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
401 if (!channel)
402 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000403
Ben Hutchings7f967c02012-02-13 23:45:02 +0000404 channel->efx = efx;
405 channel->channel = i;
406 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000407
Ben Hutchings7f967c02012-02-13 23:45:02 +0000408 for (j = 0; j < EFX_TXQ_TYPES; j++) {
409 tx_queue = &channel->tx_queue[j];
410 tx_queue->efx = efx;
411 tx_queue->queue = i * EFX_TXQ_TYPES + j;
412 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000413 }
414
Ben Hutchings46426102010-09-10 06:42:33 +0000415 rx_queue = &channel->rx_queue;
416 rx_queue->efx = efx;
417 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
418 (unsigned long)rx_queue);
419
420 return channel;
421}
422
Ben Hutchings7f967c02012-02-13 23:45:02 +0000423/* Allocate and initialise a channel structure, copying parameters
424 * (but not resources) from an old channel structure.
425 */
426static struct efx_channel *
427efx_copy_channel(const struct efx_channel *old_channel)
428{
429 struct efx_channel *channel;
430 struct efx_rx_queue *rx_queue;
431 struct efx_tx_queue *tx_queue;
432 int j;
433
434 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
435 if (!channel)
436 return NULL;
437
438 *channel = *old_channel;
439
440 channel->napi_dev = NULL;
441 memset(&channel->eventq, 0, sizeof(channel->eventq));
442
443 for (j = 0; j < EFX_TXQ_TYPES; j++) {
444 tx_queue = &channel->tx_queue[j];
445 if (tx_queue->channel)
446 tx_queue->channel = channel;
447 tx_queue->buffer = NULL;
448 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
449 }
450
451 rx_queue = &channel->rx_queue;
452 rx_queue->buffer = NULL;
453 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
454 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
455 (unsigned long)rx_queue);
456
457 return channel;
458}
459
Ben Hutchings8ceee662008-04-27 12:55:59 +0100460static int efx_probe_channel(struct efx_channel *channel)
461{
462 struct efx_tx_queue *tx_queue;
463 struct efx_rx_queue *rx_queue;
464 int rc;
465
Ben Hutchings62776d02010-06-23 11:30:07 +0000466 netif_dbg(channel->efx, probe, channel->efx->net_dev,
467 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100468
Ben Hutchings7f967c02012-02-13 23:45:02 +0000469 rc = channel->type->pre_probe(channel);
470 if (rc)
471 goto fail;
472
Ben Hutchings8ceee662008-04-27 12:55:59 +0100473 rc = efx_probe_eventq(channel);
474 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000475 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100476
477 efx_for_each_channel_tx_queue(tx_queue, channel) {
478 rc = efx_probe_tx_queue(tx_queue);
479 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000480 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100481 }
482
483 efx_for_each_channel_rx_queue(rx_queue, channel) {
484 rc = efx_probe_rx_queue(rx_queue);
485 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000486 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100487 }
488
489 channel->n_rx_frm_trunc = 0;
490
491 return 0;
492
Ben Hutchings7f967c02012-02-13 23:45:02 +0000493fail:
494 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100495 return rc;
496}
497
Ben Hutchings7f967c02012-02-13 23:45:02 +0000498static void
499efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
500{
501 struct efx_nic *efx = channel->efx;
502 const char *type;
503 int number;
504
505 number = channel->channel;
506 if (efx->tx_channel_offset == 0) {
507 type = "";
508 } else if (channel->channel < efx->tx_channel_offset) {
509 type = "-rx";
510 } else {
511 type = "-tx";
512 number -= efx->tx_channel_offset;
513 }
514 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
515}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100516
Ben Hutchings56536e92008-12-12 21:37:02 -0800517static void efx_set_channel_names(struct efx_nic *efx)
518{
519 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800520
Ben Hutchings7f967c02012-02-13 23:45:02 +0000521 efx_for_each_channel(channel, efx)
522 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100523 efx->msi_context[channel->channel].name,
524 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800525}
526
Ben Hutchings46426102010-09-10 06:42:33 +0000527static int efx_probe_channels(struct efx_nic *efx)
528{
529 struct efx_channel *channel;
530 int rc;
531
532 /* Restart special buffer allocation */
533 efx->next_buffer_table = 0;
534
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000535 /* Probe channels in reverse, so that any 'extra' channels
536 * use the start of the buffer table. This allows the traffic
537 * channels to be resized without moving them or wasting the
538 * entries before them.
539 */
540 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000541 rc = efx_probe_channel(channel);
542 if (rc) {
543 netif_err(efx, probe, efx->net_dev,
544 "failed to create channel %d\n",
545 channel->channel);
546 goto fail;
547 }
548 }
549 efx_set_channel_names(efx);
550
551 return 0;
552
553fail:
554 efx_remove_channels(efx);
555 return rc;
556}
557
Ben Hutchings8ceee662008-04-27 12:55:59 +0100558/* Channels are shutdown and reinitialised whilst the NIC is running
559 * to propagate configuration changes (mtu, checksum offload), or
560 * to clear hardware error conditions
561 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000562static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100563{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000564 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100565 struct efx_tx_queue *tx_queue;
566 struct efx_rx_queue *rx_queue;
567 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000568 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100569
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100570 /* Calculate the rx buffer allocation parameters required to
571 * support the current MTU, including padding for header
572 * alignment and overruns.
573 */
Ben Hutchings272baee2013-01-29 23:33:14 +0000574 efx->rx_dma_len = (efx->type->rx_buffer_hash_size +
575 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
576 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000577 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Ben Hutchingsc14ff2e2013-05-13 11:58:31 +0000578 NET_IP_ALIGN + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000579 if (rx_buf_len <= PAGE_SIZE) {
580 efx->rx_scatter = false;
581 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000582 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000583 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000584 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000585 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
586 EFX_RX_BUF_ALIGNMENT) >
587 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000588 efx->rx_scatter = true;
589 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
590 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000591 } else {
592 efx->rx_scatter = false;
593 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000594 }
595
Daniel Pieczko1648a232013-02-13 10:54:41 +0000596 efx_rx_config_page_split(efx);
597 if (efx->rx_buffer_order)
598 netif_dbg(efx, drv, efx->net_dev,
599 "RX buf len=%u; page order=%u batch=%u\n",
600 efx->rx_dma_len, efx->rx_buffer_order,
601 efx->rx_pages_per_batch);
602 else
603 netif_dbg(efx, drv, efx->net_dev,
604 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
605 efx->rx_dma_len, efx->rx_page_buf_step,
606 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000607
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000608 /* RX filters also have scatter-enabled flags */
609 if (efx->rx_scatter != old_rx_scatter)
610 efx_filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100611
Ben Hutchings14bf7182012-05-22 01:27:58 +0100612 /* We must keep at least one descriptor in a TX ring empty.
613 * We could avoid this when the queue size does not exactly
614 * match the hardware ring size, but it's not that important.
615 * Therefore we stop the queue when one more skb might fill
616 * the ring completely. We wake it when half way back to
617 * empty.
618 */
619 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
620 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
621
Ben Hutchings8ceee662008-04-27 12:55:59 +0100622 /* Initialise the channels */
623 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100624 efx_for_each_channel_tx_queue(tx_queue, channel)
625 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100626
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000627 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100628 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000629 efx_nic_generate_fill_event(rx_queue);
630 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100631
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000632 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100633 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000634
635 if (netif_device_present(efx->net_dev))
636 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100637}
638
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000639static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100640{
641 struct efx_channel *channel;
642 struct efx_tx_queue *tx_queue;
643 struct efx_rx_queue *rx_queue;
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100644 struct pci_dev *dev = efx->pci_dev;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100645 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100646
647 EFX_ASSERT_RESET_SERIALISED(efx);
648 BUG_ON(efx->port_enabled);
649
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100650 /* Only perform flush if dma is enabled */
Alexandre Rames626950d2013-01-14 17:20:22 +0000651 if (dev->is_busmaster && efx->state != STATE_RECOVERY) {
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100652 rc = efx_nic_flush_queues(efx);
653
654 if (rc && EFX_WORKAROUND_7803(efx)) {
655 /* Schedule a reset to recover from the flush failure. The
656 * descriptor caches reference memory we're about to free,
657 * but falcon_reconfigure_mac_wrapper() won't reconnect
658 * the MACs because of the pending reset. */
659 netif_err(efx, drv, efx->net_dev,
660 "Resetting to recover from flush failure\n");
661 efx_schedule_reset(efx, RESET_TYPE_ALL);
662 } else if (rc) {
663 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
664 } else {
665 netif_dbg(efx, drv, efx->net_dev,
666 "successfully flushed all queues\n");
667 }
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000668 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100669
Ben Hutchings8ceee662008-04-27 12:55:59 +0100670 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000671 /* RX packet processing is pipelined, so wait for the
672 * NAPI handler to complete. At least event queue 0
673 * might be kept active by non-data events, so don't
674 * use napi_synchronize() but actually disable NAPI
675 * temporarily.
676 */
677 if (efx_channel_has_rx_queue(channel)) {
678 efx_stop_eventq(channel);
679 efx_start_eventq(channel);
680 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100681
682 efx_for_each_channel_rx_queue(rx_queue, channel)
683 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000684 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100685 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100686 }
687}
688
689static void efx_remove_channel(struct efx_channel *channel)
690{
691 struct efx_tx_queue *tx_queue;
692 struct efx_rx_queue *rx_queue;
693
Ben Hutchings62776d02010-06-23 11:30:07 +0000694 netif_dbg(channel->efx, drv, channel->efx->net_dev,
695 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100696
697 efx_for_each_channel_rx_queue(rx_queue, channel)
698 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000699 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100700 efx_remove_tx_queue(tx_queue);
701 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100702 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100703}
704
Ben Hutchings46426102010-09-10 06:42:33 +0000705static void efx_remove_channels(struct efx_nic *efx)
706{
707 struct efx_channel *channel;
708
709 efx_for_each_channel(channel, efx)
710 efx_remove_channel(channel);
711}
712
713int
714efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
715{
716 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
717 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000718 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100719 int rc;
720
721 rc = efx_check_disabled(efx);
722 if (rc)
723 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000724
725 /* Not all channels should be reallocated. We must avoid
726 * reallocating their buffer table entries.
727 */
728 efx_for_each_channel(channel, efx) {
729 struct efx_rx_queue *rx_queue;
730 struct efx_tx_queue *tx_queue;
731
732 if (channel->type->copy)
733 continue;
734 next_buffer_table = max(next_buffer_table,
735 channel->eventq.index +
736 channel->eventq.entries);
737 efx_for_each_channel_rx_queue(rx_queue, channel)
738 next_buffer_table = max(next_buffer_table,
739 rx_queue->rxd.index +
740 rx_queue->rxd.entries);
741 efx_for_each_channel_tx_queue(tx_queue, channel)
742 next_buffer_table = max(next_buffer_table,
743 tx_queue->txd.index +
744 tx_queue->txd.entries);
745 }
Ben Hutchings46426102010-09-10 06:42:33 +0000746
Ben Hutchings29c69a42013-01-28 19:01:06 +0000747 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000748 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100749 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000750
Ben Hutchings7f967c02012-02-13 23:45:02 +0000751 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000752 memset(other_channel, 0, sizeof(other_channel));
753 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000754 channel = efx->channel[i];
755 if (channel->type->copy)
756 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000757 if (!channel) {
758 rc = -ENOMEM;
759 goto out;
760 }
761 other_channel[i] = channel;
762 }
763
764 /* Swap entry counts and channel pointers */
765 old_rxq_entries = efx->rxq_entries;
766 old_txq_entries = efx->txq_entries;
767 efx->rxq_entries = rxq_entries;
768 efx->txq_entries = txq_entries;
769 for (i = 0; i < efx->n_channels; i++) {
770 channel = efx->channel[i];
771 efx->channel[i] = other_channel[i];
772 other_channel[i] = channel;
773 }
774
Ben Hutchings7f967c02012-02-13 23:45:02 +0000775 /* Restart buffer table allocation */
776 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000777
Ben Hutchingse8f14992010-12-07 19:47:34 +0000778 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000779 channel = efx->channel[i];
780 if (!channel->type->copy)
781 continue;
782 rc = efx_probe_channel(channel);
783 if (rc)
784 goto rollback;
785 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000786 }
Ben Hutchings46426102010-09-10 06:42:33 +0000787
Ben Hutchings7f967c02012-02-13 23:45:02 +0000788out:
789 /* Destroy unused channel structures */
790 for (i = 0; i < efx->n_channels; i++) {
791 channel = other_channel[i];
792 if (channel && channel->type->copy) {
793 efx_fini_napi_channel(channel);
794 efx_remove_channel(channel);
795 kfree(channel);
796 }
797 }
798
Ben Hutchingsd8291182012-10-05 23:35:41 +0100799 efx_soft_enable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000800 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000801 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000802 return rc;
803
804rollback:
805 /* Swap back */
806 efx->rxq_entries = old_rxq_entries;
807 efx->txq_entries = old_txq_entries;
808 for (i = 0; i < efx->n_channels; i++) {
809 channel = efx->channel[i];
810 efx->channel[i] = other_channel[i];
811 other_channel[i] = channel;
812 }
813 goto out;
814}
815
Steve Hodgson90d683a2010-06-01 11:19:39 +0000816void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100817{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000818 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100819}
820
Ben Hutchings7f967c02012-02-13 23:45:02 +0000821static const struct efx_channel_type efx_default_channel_type = {
822 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100823 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000824 .get_name = efx_get_channel_name,
825 .copy = efx_copy_channel,
826 .keep_eventq = false,
827};
828
829int efx_channel_dummy_op_int(struct efx_channel *channel)
830{
831 return 0;
832}
833
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100834void efx_channel_dummy_op_void(struct efx_channel *channel)
835{
836}
837
Ben Hutchings8ceee662008-04-27 12:55:59 +0100838/**************************************************************************
839 *
840 * Port handling
841 *
842 **************************************************************************/
843
844/* This ensures that the kernel is kept informed (via
845 * netif_carrier_on/off) of the link status, and also maintains the
846 * link status's stop on the port's TX queue.
847 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000848void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100849{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000850 struct efx_link_state *link_state = &efx->link_state;
851
Ben Hutchings8ceee662008-04-27 12:55:59 +0100852 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
853 * that no events are triggered between unregister_netdev() and the
854 * driver unloading. A more general condition is that NETDEV_CHANGE
855 * can only be generated between NETDEV_UP and NETDEV_DOWN */
856 if (!netif_running(efx->net_dev))
857 return;
858
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000859 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100860 efx->n_link_state_changes++;
861
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000862 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100863 netif_carrier_on(efx->net_dev);
864 else
865 netif_carrier_off(efx->net_dev);
866 }
867
868 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000869 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000870 netif_info(efx, link, efx->net_dev,
871 "link up at %uMbps %s-duplex (MTU %d)%s\n",
872 link_state->speed, link_state->fd ? "full" : "half",
873 efx->net_dev->mtu,
874 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000875 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000876 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100877}
878
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000879void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
880{
881 efx->link_advertising = advertising;
882 if (advertising) {
883 if (advertising & ADVERTISED_Pause)
884 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
885 else
886 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
887 if (advertising & ADVERTISED_Asym_Pause)
888 efx->wanted_fc ^= EFX_FC_TX;
889 }
890}
891
David S. Millerb56269462011-05-17 17:53:22 -0400892void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000893{
894 efx->wanted_fc = wanted_fc;
895 if (efx->link_advertising) {
896 if (wanted_fc & EFX_FC_RX)
897 efx->link_advertising |= (ADVERTISED_Pause |
898 ADVERTISED_Asym_Pause);
899 else
900 efx->link_advertising &= ~(ADVERTISED_Pause |
901 ADVERTISED_Asym_Pause);
902 if (wanted_fc & EFX_FC_TX)
903 efx->link_advertising ^= ADVERTISED_Asym_Pause;
904 }
905}
906
Ben Hutchings115122a2009-03-04 09:52:52 +0000907static void efx_fini_port(struct efx_nic *efx);
908
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000909/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
910 * the MAC appropriately. All other PHY configuration changes are pushed
911 * through phy_op->set_settings(), and pushed asynchronously to the MAC
912 * through efx_monitor().
913 *
914 * Callers must hold the mac_lock
915 */
916int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100917{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000918 enum efx_phy_mode phy_mode;
919 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100920
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000921 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100922
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000923 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000924 netif_addr_lock_bh(efx->net_dev);
925 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100926
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000927 /* Disable PHY transmit in mac level loopbacks */
928 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800929 if (LOOPBACK_INTERNAL(efx))
930 efx->phy_mode |= PHY_MODE_TX_DISABLED;
931 else
932 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800933
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000934 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800935
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000936 if (rc)
937 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100938
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000939 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100940}
941
942/* Reinitialise the MAC to pick up new PHY settings, even if the port is
943 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000944int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100945{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000946 int rc;
947
Ben Hutchings8ceee662008-04-27 12:55:59 +0100948 EFX_ASSERT_RESET_SERIALISED(efx);
949
950 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000951 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100952 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000953
954 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100955}
956
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000957/* Asynchronous work item for changing MAC promiscuity and multicast
958 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
959 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800960static void efx_mac_work(struct work_struct *data)
961{
962 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
963
964 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100965 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100966 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800967 mutex_unlock(&efx->mac_lock);
968}
969
Ben Hutchings8ceee662008-04-27 12:55:59 +0100970static int efx_probe_port(struct efx_nic *efx)
971{
972 int rc;
973
Ben Hutchings62776d02010-06-23 11:30:07 +0000974 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100975
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000976 if (phy_flash_cfg)
977 efx->phy_mode = PHY_MODE_SPECIAL;
978
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000979 /* Connect up MAC/PHY operations table */
980 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100981 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000982 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100983
Ben Hutchingse332bcb2011-12-20 01:22:51 +0000984 /* Initialise MAC address to permanent address */
985 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100986
987 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100988}
989
990static int efx_init_port(struct efx_nic *efx)
991{
992 int rc;
993
Ben Hutchings62776d02010-06-23 11:30:07 +0000994 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100995
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000996 mutex_lock(&efx->mac_lock);
997
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800998 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100999 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001000 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001001
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001002 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001003
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001004 /* Reconfigure the MAC before creating dma queues (required for
1005 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001006 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001007
1008 /* Ensure the PHY advertises the correct flow control settings */
1009 rc = efx->phy_op->reconfigure(efx);
1010 if (rc)
1011 goto fail2;
1012
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001013 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001014 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001015
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001016fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001017 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001018fail1:
1019 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001020 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001021}
1022
Ben Hutchings8ceee662008-04-27 12:55:59 +01001023static void efx_start_port(struct efx_nic *efx)
1024{
Ben Hutchings62776d02010-06-23 11:30:07 +00001025 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001026 BUG_ON(efx->port_enabled);
1027
1028 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001029 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001030
1031 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1032 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001033 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001034
Ben Hutchings8ceee662008-04-27 12:55:59 +01001035 mutex_unlock(&efx->mac_lock);
1036}
1037
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001038/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001039static void efx_stop_port(struct efx_nic *efx)
1040{
Ben Hutchings62776d02010-06-23 11:30:07 +00001041 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001042
1043 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001044 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001045 mutex_unlock(&efx->mac_lock);
1046
1047 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001048 netif_addr_lock_bh(efx->net_dev);
1049 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001050}
1051
1052static void efx_fini_port(struct efx_nic *efx)
1053{
Ben Hutchings62776d02010-06-23 11:30:07 +00001054 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001055
1056 if (!efx->port_initialized)
1057 return;
1058
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001059 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001060 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001061
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001062 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001063 efx_link_status_changed(efx);
1064}
1065
1066static void efx_remove_port(struct efx_nic *efx)
1067{
Ben Hutchings62776d02010-06-23 11:30:07 +00001068 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001069
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001070 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001071}
1072
1073/**************************************************************************
1074 *
1075 * NIC handling
1076 *
1077 **************************************************************************/
1078
1079/* This configures the PCI device to enable I/O and DMA. */
1080static int efx_init_io(struct efx_nic *efx)
1081{
1082 struct pci_dev *pci_dev = efx->pci_dev;
1083 dma_addr_t dma_mask = efx->type->max_dma_mask;
1084 int rc;
1085
Ben Hutchings62776d02010-06-23 11:30:07 +00001086 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001087
1088 rc = pci_enable_device(pci_dev);
1089 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001090 netif_err(efx, probe, efx->net_dev,
1091 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001092 goto fail1;
1093 }
1094
1095 pci_set_master(pci_dev);
1096
1097 /* Set the PCI DMA mask. Try all possibilities from our
1098 * genuine mask down to 32 bits, because some architectures
1099 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1100 * masks event though they reject 46 bit masks.
1101 */
1102 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001103 if (dma_supported(&pci_dev->dev, dma_mask)) {
1104 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001105 if (rc == 0)
1106 break;
1107 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001108 dma_mask >>= 1;
1109 }
1110 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001111 netif_err(efx, probe, efx->net_dev,
1112 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001113 goto fail2;
1114 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001115 netif_dbg(efx, probe, efx->net_dev,
1116 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001117 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001118 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001119 /* dma_set_coherent_mask() is not *allowed* to
1120 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001121 * but just in case...
1122 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001123 netif_err(efx, probe, efx->net_dev,
1124 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001125 goto fail2;
1126 }
1127
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001128 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1129 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001130 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001131 netif_err(efx, probe, efx->net_dev,
1132 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001133 rc = -EIO;
1134 goto fail3;
1135 }
David S. Miller8decf862011-09-22 03:23:13 -04001136 efx->membase = ioremap_nocache(efx->membase_phys,
1137 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001138 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001139 netif_err(efx, probe, efx->net_dev,
1140 "could not map memory BAR at %llx+%x\n",
1141 (unsigned long long)efx->membase_phys,
1142 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001143 rc = -ENOMEM;
1144 goto fail4;
1145 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001146 netif_dbg(efx, probe, efx->net_dev,
1147 "memory BAR at %llx+%x (virtual %p)\n",
1148 (unsigned long long)efx->membase_phys,
1149 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001150
1151 return 0;
1152
1153 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001154 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001155 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001156 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001157 fail2:
1158 pci_disable_device(efx->pci_dev);
1159 fail1:
1160 return rc;
1161}
1162
1163static void efx_fini_io(struct efx_nic *efx)
1164{
Ben Hutchings62776d02010-06-23 11:30:07 +00001165 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001166
1167 if (efx->membase) {
1168 iounmap(efx->membase);
1169 efx->membase = NULL;
1170 }
1171
1172 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001173 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001174 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001175 }
1176
1177 pci_disable_device(efx->pci_dev);
1178}
1179
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001180static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001181{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001182 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001183 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001184 int cpu;
1185
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001186 if (rss_cpus) {
1187 count = rss_cpus;
1188 } else {
1189 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1190 netif_warn(efx, probe, efx->net_dev,
1191 "RSS disabled due to allocation failure\n");
1192 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001193 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001194
1195 count = 0;
1196 for_each_online_cpu(cpu) {
1197 if (!cpumask_test_cpu(cpu, thread_mask)) {
1198 ++count;
1199 cpumask_or(thread_mask, thread_mask,
1200 topology_thread_cpumask(cpu));
1201 }
1202 }
1203
1204 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001205 }
1206
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001207 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1208 * table entries that are inaccessible to VFs
1209 */
1210 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1211 count > efx_vf_size(efx)) {
1212 netif_warn(efx, probe, efx->net_dev,
1213 "Reducing number of RSS channels from %u to %u for "
1214 "VF support. Increase vf-msix-limit to use more "
1215 "channels on the PF.\n",
1216 count, efx_vf_size(efx));
1217 count = efx_vf_size(efx);
1218 }
1219
Ben Hutchings46123d02008-09-01 12:47:33 +01001220 return count;
1221}
1222
1223/* Probe the number and type of interrupts we are able to obtain, and
1224 * the resulting numbers of channels and RX queues.
1225 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001226static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001227{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001228 unsigned int max_channels =
1229 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001230 unsigned int extra_channels = 0;
1231 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001232 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001233
Ben Hutchings7f967c02012-02-13 23:45:02 +00001234 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1235 if (efx->extra_channel_type[i])
1236 ++extra_channels;
1237
Ben Hutchings8ceee662008-04-27 12:55:59 +01001238 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001239 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001240 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001241
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001242 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001243 if (separate_tx_channels)
1244 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001245 n_channels += extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001246 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001247
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001248 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001249 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001250 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001251 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001252 netif_err(efx, drv, efx->net_dev,
1253 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001254 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001255 netif_err(efx, drv, efx->net_dev,
1256 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001257 EFX_BUG_ON_PARANOID(rc >= n_channels);
1258 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001259 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001260 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001261 }
1262
1263 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001264 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001265 if (n_channels > extra_channels)
1266 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001267 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001268 efx->n_tx_channels = max(n_channels / 2, 1U);
1269 efx->n_rx_channels = max(n_channels -
1270 efx->n_tx_channels,
1271 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001272 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001273 efx->n_tx_channels = n_channels;
1274 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001275 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001276 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001277 efx_get_channel(efx, i)->irq =
1278 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001279 } else {
1280 /* Fall back to single channel MSI */
1281 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001282 netif_err(efx, drv, efx->net_dev,
1283 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001284 }
1285 }
1286
1287 /* Try single interrupt MSI */
1288 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001289 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001290 efx->n_rx_channels = 1;
1291 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001292 rc = pci_enable_msi(efx->pci_dev);
1293 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001294 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001295 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001296 netif_err(efx, drv, efx->net_dev,
1297 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001298 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1299 }
1300 }
1301
1302 /* Assume legacy interrupts */
1303 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001304 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001305 efx->n_rx_channels = 1;
1306 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001307 efx->legacy_irq = efx->pci_dev->irq;
1308 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001309
Ben Hutchings7f967c02012-02-13 23:45:02 +00001310 /* Assign extra channels if possible */
1311 j = efx->n_channels;
1312 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1313 if (!efx->extra_channel_type[i])
1314 continue;
1315 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1316 efx->n_channels <= extra_channels) {
1317 efx->extra_channel_type[i]->handle_no_channel(efx);
1318 } else {
1319 --j;
1320 efx_get_channel(efx, j)->type =
1321 efx->extra_channel_type[i];
1322 }
1323 }
1324
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001325 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001326 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001327 efx->n_rx_channels : efx_vf_size(efx));
1328
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001329 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001330}
1331
Ben Hutchingsd8291182012-10-05 23:35:41 +01001332static void efx_soft_enable_interrupts(struct efx_nic *efx)
1333{
1334 struct efx_channel *channel;
1335
1336 BUG_ON(efx->state == STATE_DISABLED);
1337
1338 efx->irq_soft_enabled = true;
1339 smp_wmb();
1340
1341 efx_for_each_channel(channel, efx) {
1342 if (!channel->type->keep_eventq)
1343 efx_init_eventq(channel);
1344 efx_start_eventq(channel);
1345 }
1346
1347 efx_mcdi_mode_event(efx);
1348}
1349
1350static void efx_soft_disable_interrupts(struct efx_nic *efx)
1351{
1352 struct efx_channel *channel;
1353
1354 if (efx->state == STATE_DISABLED)
1355 return;
1356
1357 efx_mcdi_mode_poll(efx);
1358
1359 efx->irq_soft_enabled = false;
1360 smp_wmb();
1361
1362 if (efx->legacy_irq)
1363 synchronize_irq(efx->legacy_irq);
1364
1365 efx_for_each_channel(channel, efx) {
1366 if (channel->irq)
1367 synchronize_irq(channel->irq);
1368
1369 efx_stop_eventq(channel);
1370 if (!channel->type->keep_eventq)
1371 efx_fini_eventq(channel);
1372 }
1373}
1374
1375static void efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001376{
1377 struct efx_channel *channel;
1378
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001379 BUG_ON(efx->state == STATE_DISABLED);
1380
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001381 if (efx->eeh_disabled_legacy_irq) {
1382 enable_irq(efx->legacy_irq);
1383 efx->eeh_disabled_legacy_irq = false;
1384 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001385
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001386 efx_nic_enable_interrupts(efx);
1387
1388 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001389 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001390 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001391 }
1392
Ben Hutchingsd8291182012-10-05 23:35:41 +01001393 efx_soft_enable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001394}
1395
Ben Hutchingsd8291182012-10-05 23:35:41 +01001396static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001397{
1398 struct efx_channel *channel;
1399
Ben Hutchingsd8291182012-10-05 23:35:41 +01001400 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001401
1402 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001403 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001404 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001405 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001406
1407 efx_nic_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001408}
1409
Ben Hutchings8ceee662008-04-27 12:55:59 +01001410static void efx_remove_interrupts(struct efx_nic *efx)
1411{
1412 struct efx_channel *channel;
1413
1414 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001415 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001416 channel->irq = 0;
1417 pci_disable_msi(efx->pci_dev);
1418 pci_disable_msix(efx->pci_dev);
1419
1420 /* Remove legacy interrupt */
1421 efx->legacy_irq = 0;
1422}
1423
Ben Hutchings8831da72008-09-01 12:47:48 +01001424static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001425{
Ben Hutchings602a5322011-05-16 17:32:39 +01001426 struct efx_channel *channel;
1427 struct efx_tx_queue *tx_queue;
1428
Ben Hutchings97653432011-01-12 18:26:56 +00001429 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001430 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001431
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001432 /* We need to mark which channels really have RX and TX
1433 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001434 * RX-only and TX-only channels.
1435 */
1436 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001437 if (channel->channel < efx->n_rx_channels)
1438 channel->rx_queue.core_index = channel->channel;
1439 else
1440 channel->rx_queue.core_index = -1;
1441
Ben Hutchings602a5322011-05-16 17:32:39 +01001442 efx_for_each_channel_tx_queue(tx_queue, channel)
1443 tx_queue->queue -= (efx->tx_channel_offset *
1444 EFX_TXQ_TYPES);
1445 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001446}
1447
1448static int efx_probe_nic(struct efx_nic *efx)
1449{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001450 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001451 int rc;
1452
Ben Hutchings62776d02010-06-23 11:30:07 +00001453 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001454
1455 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001456 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001457 if (rc)
1458 return rc;
1459
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001460 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001461 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001462 rc = efx_probe_interrupts(efx);
1463 if (rc)
1464 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001465
Ben Hutchings28e47c42012-02-15 01:58:49 +00001466 efx->type->dimension_resources(efx);
1467
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001468 if (efx->n_channels > 1)
1469 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001470 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001471 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001472 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001473
Ben Hutchings8831da72008-09-01 12:47:48 +01001474 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001475 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1476 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001477
1478 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001479 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1480 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001481
1482 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001483
1484fail:
1485 efx->type->remove(efx);
1486 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001487}
1488
1489static void efx_remove_nic(struct efx_nic *efx)
1490{
Ben Hutchings62776d02010-06-23 11:30:07 +00001491 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001492
1493 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001494 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001495}
1496
1497/**************************************************************************
1498 *
1499 * NIC startup/shutdown
1500 *
1501 *************************************************************************/
1502
1503static int efx_probe_all(struct efx_nic *efx)
1504{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001505 int rc;
1506
Ben Hutchings8ceee662008-04-27 12:55:59 +01001507 rc = efx_probe_nic(efx);
1508 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001509 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001510 goto fail1;
1511 }
1512
Ben Hutchings8ceee662008-04-27 12:55:59 +01001513 rc = efx_probe_port(efx);
1514 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001515 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001516 goto fail2;
1517 }
1518
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001519 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1520 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1521 rc = -EINVAL;
1522 goto fail3;
1523 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001524 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001525
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001526 rc = efx_probe_filters(efx);
1527 if (rc) {
1528 netif_err(efx, probe, efx->net_dev,
1529 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001530 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001531 }
1532
Ben Hutchings7f967c02012-02-13 23:45:02 +00001533 rc = efx_probe_channels(efx);
1534 if (rc)
1535 goto fail4;
1536
Ben Hutchings8ceee662008-04-27 12:55:59 +01001537 return 0;
1538
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001539 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001540 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001541 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001542 efx_remove_port(efx);
1543 fail2:
1544 efx_remove_nic(efx);
1545 fail1:
1546 return rc;
1547}
1548
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001549/* If the interface is supposed to be running but is not, start
1550 * the hardware and software data path, regular activity for the port
1551 * (MAC statistics, link polling, etc.) and schedule the port to be
1552 * reconfigured. Interrupts must already be enabled. This function
1553 * is safe to call multiple times, so long as the NIC is not disabled.
1554 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001555 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001556static void efx_start_all(struct efx_nic *efx)
1557{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001558 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001559 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001560
1561 /* Check that it is appropriate to restart the interface. All
1562 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001563 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001564 return;
1565
Ben Hutchings8ceee662008-04-27 12:55:59 +01001566 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001567 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001568
Alexandre Rames626950d2013-01-14 17:20:22 +00001569 /* Start the hardware monitor if there is one */
1570 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001571 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1572 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001573
1574 /* If link state detection is normally event-driven, we have
1575 * to poll now because we could have missed a change
1576 */
1577 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001578 mutex_lock(&efx->mac_lock);
1579 if (efx->phy_op->poll(efx))
1580 efx_link_status_changed(efx);
1581 mutex_unlock(&efx->mac_lock);
1582 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001583
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001584 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001585}
1586
1587/* Flush all delayed work. Should only be called when no more delayed work
1588 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1589 * since we're holding the rtnl_lock at this point. */
1590static void efx_flush_all(struct efx_nic *efx)
1591{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001592 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001593 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001594 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001595 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001596 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001597}
1598
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001599/* Quiesce the hardware and software data path, and regular activity
1600 * for the port without bringing the link down. Safe to call multiple
1601 * times with the NIC in almost any state, but interrupts should be
1602 * enabled. Requires the RTNL lock.
1603 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001604static void efx_stop_all(struct efx_nic *efx)
1605{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001606 EFX_ASSERT_RESET_SERIALISED(efx);
1607
1608 /* port_enabled can be read safely under the rtnl lock */
1609 if (!efx->port_enabled)
1610 return;
1611
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001612 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001613 efx_stop_port(efx);
1614
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001615 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001616 efx_flush_all(efx);
1617
Ben Hutchings29c69a42013-01-28 19:01:06 +00001618 /* Stop the kernel transmit interface. This is only valid if
1619 * the device is stopped or detached; otherwise the watchdog
1620 * may fire immediately.
1621 */
1622 WARN_ON(netif_running(efx->net_dev) &&
1623 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001624 netif_tx_disable(efx->net_dev);
1625
1626 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001627}
1628
1629static void efx_remove_all(struct efx_nic *efx)
1630{
Ben Hutchings46426102010-09-10 06:42:33 +00001631 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001632 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001633 efx_remove_port(efx);
1634 efx_remove_nic(efx);
1635}
1636
Ben Hutchings8ceee662008-04-27 12:55:59 +01001637/**************************************************************************
1638 *
1639 * Interrupt moderation
1640 *
1641 **************************************************************************/
1642
Ben Hutchingscc180b62011-12-08 19:51:47 +00001643static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001644{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001645 if (usecs == 0)
1646 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001647 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001648 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001649 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001650}
1651
Ben Hutchings8ceee662008-04-27 12:55:59 +01001652/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001653int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1654 unsigned int rx_usecs, bool rx_adaptive,
1655 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001656{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001657 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001658 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1659 efx->timer_quantum_ns,
1660 1000);
1661 unsigned int tx_ticks;
1662 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001663
1664 EFX_ASSERT_RESET_SERIALISED(efx);
1665
Ben Hutchingscc180b62011-12-08 19:51:47 +00001666 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001667 return -EINVAL;
1668
Ben Hutchingscc180b62011-12-08 19:51:47 +00001669 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1670 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1671
Ben Hutchings9e393b32011-09-05 07:43:04 +00001672 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1673 !rx_may_override_tx) {
1674 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1675 "RX and TX IRQ moderation must be equal\n");
1676 return -EINVAL;
1677 }
1678
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001679 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001680 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001681 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001682 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001683 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001684 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001685 channel->irq_moderation = tx_ticks;
1686 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001687
1688 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001689}
1690
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001691void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1692 unsigned int *rx_usecs, bool *rx_adaptive)
1693{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001694 /* We must round up when converting ticks to microseconds
1695 * because we round down when converting the other way.
1696 */
1697
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001698 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001699 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1700 efx->timer_quantum_ns,
1701 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001702
1703 /* If channels are shared between RX and TX, so is IRQ
1704 * moderation. Otherwise, IRQ moderation is the same for all
1705 * TX channels and is not adaptive.
1706 */
1707 if (efx->tx_channel_offset == 0)
1708 *tx_usecs = *rx_usecs;
1709 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001710 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001711 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001712 efx->timer_quantum_ns,
1713 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001714}
1715
Ben Hutchings8ceee662008-04-27 12:55:59 +01001716/**************************************************************************
1717 *
1718 * Hardware monitor
1719 *
1720 **************************************************************************/
1721
Ben Hutchingse254c272010-09-20 08:44:10 +00001722/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001723static void efx_monitor(struct work_struct *data)
1724{
1725 struct efx_nic *efx = container_of(data, struct efx_nic,
1726 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001727
Ben Hutchings62776d02010-06-23 11:30:07 +00001728 netif_vdbg(efx, timer, efx->net_dev,
1729 "hardware monitor executing on CPU %d\n",
1730 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001731 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001732
Ben Hutchings8ceee662008-04-27 12:55:59 +01001733 /* If the mac_lock is already held then it is likely a port
1734 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001735 * most of the work of monitor() anyway. */
1736 if (mutex_trylock(&efx->mac_lock)) {
1737 if (efx->port_enabled)
1738 efx->type->monitor(efx);
1739 mutex_unlock(&efx->mac_lock);
1740 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001741
Ben Hutchings8ceee662008-04-27 12:55:59 +01001742 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1743 efx_monitor_interval);
1744}
1745
1746/**************************************************************************
1747 *
1748 * ioctls
1749 *
1750 *************************************************************************/
1751
1752/* Net device ioctl
1753 * Context: process, rtnl_lock() held.
1754 */
1755static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1756{
Ben Hutchings767e4682008-09-01 12:43:14 +01001757 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001758 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001759
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001760 if (cmd == SIOCSHWTSTAMP)
1761 return efx_ptp_ioctl(efx, ifr, cmd);
1762
Ben Hutchings68e7f452009-04-29 08:05:08 +00001763 /* Convert phy_id from older PRTAD/DEVAD format */
1764 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1765 (data->phy_id & 0xfc00) == 0x0400)
1766 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1767
1768 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001769}
1770
1771/**************************************************************************
1772 *
1773 * NAPI interface
1774 *
1775 **************************************************************************/
1776
Ben Hutchings7f967c02012-02-13 23:45:02 +00001777static void efx_init_napi_channel(struct efx_channel *channel)
1778{
1779 struct efx_nic *efx = channel->efx;
1780
1781 channel->napi_dev = efx->net_dev;
1782 netif_napi_add(channel->napi_dev, &channel->napi_str,
1783 efx_poll, napi_weight);
1784}
1785
Ben Hutchingse8f14992010-12-07 19:47:34 +00001786static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001787{
1788 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001789
Ben Hutchings7f967c02012-02-13 23:45:02 +00001790 efx_for_each_channel(channel, efx)
1791 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001792}
1793
1794static void efx_fini_napi_channel(struct efx_channel *channel)
1795{
1796 if (channel->napi_dev)
1797 netif_napi_del(&channel->napi_str);
1798 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001799}
1800
1801static void efx_fini_napi(struct efx_nic *efx)
1802{
1803 struct efx_channel *channel;
1804
Ben Hutchingse8f14992010-12-07 19:47:34 +00001805 efx_for_each_channel(channel, efx)
1806 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001807}
1808
1809/**************************************************************************
1810 *
1811 * Kernel netpoll interface
1812 *
1813 *************************************************************************/
1814
1815#ifdef CONFIG_NET_POLL_CONTROLLER
1816
1817/* Although in the common case interrupts will be disabled, this is not
1818 * guaranteed. However, all our work happens inside the NAPI callback,
1819 * so no locking is required.
1820 */
1821static void efx_netpoll(struct net_device *net_dev)
1822{
Ben Hutchings767e4682008-09-01 12:43:14 +01001823 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001824 struct efx_channel *channel;
1825
Ben Hutchings64ee3122008-09-01 12:47:38 +01001826 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001827 efx_schedule_channel(channel);
1828}
1829
1830#endif
1831
1832/**************************************************************************
1833 *
1834 * Kernel net device interface
1835 *
1836 *************************************************************************/
1837
1838/* Context: process, rtnl_lock() held. */
1839static int efx_net_open(struct net_device *net_dev)
1840{
Ben Hutchings767e4682008-09-01 12:43:14 +01001841 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001842 int rc;
1843
Ben Hutchings62776d02010-06-23 11:30:07 +00001844 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1845 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001846
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001847 rc = efx_check_disabled(efx);
1848 if (rc)
1849 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001850 if (efx->phy_mode & PHY_MODE_SPECIAL)
1851 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001852 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1853 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001854
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001855 /* Notify the kernel of the link state polled during driver load,
1856 * before the monitor starts running */
1857 efx_link_status_changed(efx);
1858
Ben Hutchings8ceee662008-04-27 12:55:59 +01001859 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001860 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001861 return 0;
1862}
1863
1864/* Context: process, rtnl_lock() held.
1865 * Note that the kernel will ignore our return code; this method
1866 * should really be a void.
1867 */
1868static int efx_net_stop(struct net_device *net_dev)
1869{
Ben Hutchings767e4682008-09-01 12:43:14 +01001870 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001871
Ben Hutchings62776d02010-06-23 11:30:07 +00001872 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1873 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001874
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001875 /* Stop the device and flush all the channels */
1876 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001877
1878 return 0;
1879}
1880
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001881/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001882static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1883 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001884{
Ben Hutchings767e4682008-09-01 12:43:14 +01001885 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001886 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001887
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001888 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001889
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001890 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001891
1892 stats->rx_packets = mac_stats->rx_packets;
1893 stats->tx_packets = mac_stats->tx_packets;
1894 stats->rx_bytes = mac_stats->rx_bytes;
1895 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001896 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001897 stats->multicast = mac_stats->rx_multicast;
1898 stats->collisions = mac_stats->tx_collision;
1899 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1900 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001901 stats->rx_crc_errors = mac_stats->rx_bad;
1902 stats->rx_frame_errors = mac_stats->rx_align_error;
1903 stats->rx_fifo_errors = mac_stats->rx_overflow;
1904 stats->rx_missed_errors = mac_stats->rx_missed;
1905 stats->tx_window_errors = mac_stats->tx_late_collision;
1906
1907 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001908 stats->rx_crc_errors +
1909 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001910 mac_stats->rx_symbol_error);
1911 stats->tx_errors = (stats->tx_window_errors +
1912 mac_stats->tx_bad);
1913
Ben Hutchings1cb34522011-09-02 23:23:00 +01001914 spin_unlock_bh(&efx->stats_lock);
1915
Ben Hutchings8ceee662008-04-27 12:55:59 +01001916 return stats;
1917}
1918
1919/* Context: netif_tx_lock held, BHs disabled. */
1920static void efx_watchdog(struct net_device *net_dev)
1921{
Ben Hutchings767e4682008-09-01 12:43:14 +01001922 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001923
Ben Hutchings62776d02010-06-23 11:30:07 +00001924 netif_err(efx, tx_err, efx->net_dev,
1925 "TX stuck with port_enabled=%d: resetting channels\n",
1926 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001927
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001928 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001929}
1930
1931
1932/* Context: process, rtnl_lock() held. */
1933static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1934{
Ben Hutchings767e4682008-09-01 12:43:14 +01001935 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001936 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001937
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001938 rc = efx_check_disabled(efx);
1939 if (rc)
1940 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001941 if (new_mtu > EFX_MAX_MTU)
1942 return -EINVAL;
1943
Ben Hutchings62776d02010-06-23 11:30:07 +00001944 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001945
Ben Hutchings29c69a42013-01-28 19:01:06 +00001946 efx_device_detach_sync(efx);
1947 efx_stop_all(efx);
1948
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001949 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001950 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001951 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001952 mutex_unlock(&efx->mac_lock);
1953
Ben Hutchings8ceee662008-04-27 12:55:59 +01001954 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00001955 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001956 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001957}
1958
1959static int efx_set_mac_address(struct net_device *net_dev, void *data)
1960{
Ben Hutchings767e4682008-09-01 12:43:14 +01001961 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001962 struct sockaddr *addr = data;
1963 char *new_addr = addr->sa_data;
1964
Ben Hutchings8ceee662008-04-27 12:55:59 +01001965 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001966 netif_err(efx, drv, efx->net_dev,
1967 "invalid ethernet MAC address requested: %pM\n",
1968 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00001969 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001970 }
1971
1972 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001973 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001974
1975 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001976 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01001977 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001978 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001979
1980 return 0;
1981}
1982
Ben Hutchingsa816f752008-09-01 12:49:12 +01001983/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00001984static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001985{
Ben Hutchings767e4682008-09-01 12:43:14 +01001986 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001987 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001988 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001989 u32 crc;
1990 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001991
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001992 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001993
1994 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001995 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001996 memset(mc_hash, 0xff, sizeof(*mc_hash));
1997 } else {
1998 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00001999 netdev_for_each_mc_addr(ha, net_dev) {
2000 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002001 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
Ben Hutchings32766ec2012-10-04 17:13:03 -07002002 __set_bit_le(bit, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002003 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002004
2005 /* Broadcast packets go through the multicast hash filter.
2006 * ether_crc_le() of the broadcast address is 0xbe2612ff
2007 * so we always add bit 0xff to the mask.
2008 */
Ben Hutchings32766ec2012-10-04 17:13:03 -07002009 __set_bit_le(0xff, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002010 }
2011
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002012 if (efx->port_enabled)
2013 queue_work(efx->workqueue, &efx->mac_work);
2014 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002015}
2016
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002017static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002018{
2019 struct efx_nic *efx = netdev_priv(net_dev);
2020
2021 /* If disabling RX n-tuple filtering, clear existing filters */
2022 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2023 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2024
2025 return 0;
2026}
2027
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002028static const struct net_device_ops efx_netdev_ops = {
2029 .ndo_open = efx_net_open,
2030 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002031 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002032 .ndo_tx_timeout = efx_watchdog,
2033 .ndo_start_xmit = efx_hard_start_xmit,
2034 .ndo_validate_addr = eth_validate_addr,
2035 .ndo_do_ioctl = efx_ioctl,
2036 .ndo_change_mtu = efx_change_mtu,
2037 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002038 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002039 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002040#ifdef CONFIG_SFC_SRIOV
2041 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2042 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2043 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2044 .ndo_get_vf_config = efx_sriov_get_vf_config,
2045#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002046#ifdef CONFIG_NET_POLL_CONTROLLER
2047 .ndo_poll_controller = efx_netpoll,
2048#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002049 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002050#ifdef CONFIG_RFS_ACCEL
2051 .ndo_rx_flow_steer = efx_filter_rfs,
2052#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002053};
2054
Ben Hutchings7dde5962008-12-12 22:09:38 -08002055static void efx_update_name(struct efx_nic *efx)
2056{
2057 strcpy(efx->name, efx->net_dev->name);
2058 efx_mtd_rename(efx);
2059 efx_set_channel_names(efx);
2060}
2061
Ben Hutchings8ceee662008-04-27 12:55:59 +01002062static int efx_netdev_event(struct notifier_block *this,
2063 unsigned long event, void *ptr)
2064{
Jiri Pirko351638e2013-05-28 01:30:21 +00002065 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002066
Ben Hutchings7dde5962008-12-12 22:09:38 -08002067 if (net_dev->netdev_ops == &efx_netdev_ops &&
2068 event == NETDEV_CHANGENAME)
2069 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002070
2071 return NOTIFY_DONE;
2072}
2073
2074static struct notifier_block efx_netdev_notifier = {
2075 .notifier_call = efx_netdev_event,
2076};
2077
Ben Hutchings06d5e192008-12-12 21:47:23 -08002078static ssize_t
2079show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2080{
2081 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2082 return sprintf(buf, "%d\n", efx->phy_type);
2083}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002084static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002085
Ben Hutchings8ceee662008-04-27 12:55:59 +01002086static int efx_register_netdev(struct efx_nic *efx)
2087{
2088 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002089 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002090 int rc;
2091
2092 net_dev->watchdog_timeo = 5 * HZ;
2093 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002094 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002095 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002096 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002097
Ben Hutchings7dde5962008-12-12 22:09:38 -08002098 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002099
Ben Hutchings7153f622012-07-27 20:50:52 +01002100 /* Enable resets to be scheduled and check whether any were
2101 * already requested. If so, the NIC is probably hosed so we
2102 * abort.
2103 */
2104 efx->state = STATE_READY;
2105 smp_mb(); /* ensure we change state before checking reset_pending */
2106 if (efx->reset_pending) {
2107 netif_err(efx, probe, efx->net_dev,
2108 "aborting probe due to scheduled reset\n");
2109 rc = -EIO;
2110 goto fail_locked;
2111 }
2112
Ben Hutchingsaed06282009-08-26 08:16:27 +00002113 rc = dev_alloc_name(net_dev, net_dev->name);
2114 if (rc < 0)
2115 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002116 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002117
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002118 /* Always start with carrier off; PHY events will detect the link */
2119 netif_carrier_off(net_dev);
2120
Ben Hutchingsaed06282009-08-26 08:16:27 +00002121 rc = register_netdevice(net_dev);
2122 if (rc)
2123 goto fail_locked;
2124
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002125 efx_for_each_channel(channel, efx) {
2126 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002127 efx_for_each_channel_tx_queue(tx_queue, channel)
2128 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002129 }
2130
Ben Hutchings7dde5962008-12-12 22:09:38 -08002131 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002132
Ben Hutchings06d5e192008-12-12 21:47:23 -08002133 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2134 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002135 netif_err(efx, drv, efx->net_dev,
2136 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002137 goto fail_registered;
2138 }
2139
Ben Hutchings8ceee662008-04-27 12:55:59 +01002140 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002141
Ben Hutchings7153f622012-07-27 20:50:52 +01002142fail_registered:
2143 rtnl_lock();
2144 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002145fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002146 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002147 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002148 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002149 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002150}
2151
2152static void efx_unregister_netdev(struct efx_nic *efx)
2153{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002154 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002155 struct efx_tx_queue *tx_queue;
2156
2157 if (!efx->net_dev)
2158 return;
2159
Ben Hutchings767e4682008-09-01 12:43:14 +01002160 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002161
2162 /* Free up any skbs still remaining. This has to happen before
2163 * we try to unregister the netdev as running their destructors
2164 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002165 efx_for_each_channel(channel, efx) {
2166 efx_for_each_channel_tx_queue(tx_queue, channel)
2167 efx_release_tx_buffers(tx_queue);
2168 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002169
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002170 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2171 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002172
2173 rtnl_lock();
2174 unregister_netdevice(efx->net_dev);
2175 efx->state = STATE_UNINIT;
2176 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002177}
2178
2179/**************************************************************************
2180 *
2181 * Device reset and suspend
2182 *
2183 **************************************************************************/
2184
Ben Hutchings2467ca42008-09-01 12:48:50 +01002185/* Tears down the entire software state and most of the hardware state
2186 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002187void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002188{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002189 EFX_ASSERT_RESET_SERIALISED(efx);
2190
Ben Hutchings2467ca42008-09-01 12:48:50 +01002191 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002192 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002193
2194 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002195 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2196 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002197 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002198}
2199
Ben Hutchings2467ca42008-09-01 12:48:50 +01002200/* This function will always ensure that the locks acquired in
2201 * efx_reset_down() are released. A failure return code indicates
2202 * that we were unable to reinitialise the hardware, and the
2203 * driver should be disabled. If ok is false, then the rx and tx
2204 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002205int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002206{
2207 int rc;
2208
Ben Hutchings2467ca42008-09-01 12:48:50 +01002209 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002210
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002211 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002212 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002213 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002214 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002215 }
2216
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002217 if (!ok)
2218 goto fail;
2219
Steve Hodgson4b988282009-01-29 17:50:51 +00002220 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002221 rc = efx->phy_op->init(efx);
2222 if (rc)
2223 goto fail;
2224 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002225 netif_err(efx, drv, efx->net_dev,
2226 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002227 }
2228
Ben Hutchings710b2082011-09-03 00:15:00 +01002229 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002230
Ben Hutchingsd8291182012-10-05 23:35:41 +01002231 efx_enable_interrupts(efx);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002232 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002233 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002234
2235 mutex_unlock(&efx->mac_lock);
2236
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002237 efx_start_all(efx);
2238
2239 return 0;
2240
2241fail:
2242 efx->port_initialized = false;
2243
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002244 mutex_unlock(&efx->mac_lock);
2245
Ben Hutchings8ceee662008-04-27 12:55:59 +01002246 return rc;
2247}
2248
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002249/* Reset the NIC using the specified method. Note that the reset may
2250 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002251 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002252 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002253 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002254int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002255{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002256 int rc, rc2;
2257 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002258
Ben Hutchings62776d02010-06-23 11:30:07 +00002259 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2260 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002261
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002262 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002263 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002264
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002265 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002266 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002267 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002268 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002269 }
2270
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002271 /* Clear flags for the scopes we covered. We assume the NIC and
2272 * driver are now quiescent so that there is no race here.
2273 */
2274 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002275
2276 /* Reinitialise bus-mastering, which may have been turned off before
2277 * the reset was scheduled. This is still appropriate, even in the
2278 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2279 * can respond to requests. */
2280 pci_set_master(efx->pci_dev);
2281
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002282out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002283 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002284 disabled = rc ||
2285 method == RESET_TYPE_DISABLE ||
2286 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002287 rc2 = efx_reset_up(efx, method, !disabled);
2288 if (rc2) {
2289 disabled = true;
2290 if (!rc)
2291 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002292 }
2293
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002294 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002295 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002296 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002297 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002298 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002299 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002300 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002301 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002302 return rc;
2303}
2304
Alexandre Rames626950d2013-01-14 17:20:22 +00002305/* Try recovery mechanisms.
2306 * For now only EEH is supported.
2307 * Returns 0 if the recovery mechanisms are unsuccessful.
2308 * Returns a non-zero value otherwise.
2309 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002310int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002311{
2312#ifdef CONFIG_EEH
2313 /* A PCI error can occur and not be seen by EEH because nothing
2314 * happens on the PCI bus. In this case the driver may fail and
2315 * schedule a 'recover or reset', leading to this recovery handler.
2316 * Manually call the eeh failure check function.
2317 */
2318 struct eeh_dev *eehdev =
2319 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2320
2321 if (eeh_dev_check_failure(eehdev)) {
2322 /* The EEH mechanisms will handle the error and reset the
2323 * device if necessary.
2324 */
2325 return 1;
2326 }
2327#endif
2328 return 0;
2329}
2330
Ben Hutchings8ceee662008-04-27 12:55:59 +01002331/* The worker thread exists so that code that cannot sleep can
2332 * schedule a reset for later.
2333 */
2334static void efx_reset_work(struct work_struct *data)
2335{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002336 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002337 unsigned long pending;
2338 enum reset_type method;
2339
2340 pending = ACCESS_ONCE(efx->reset_pending);
2341 method = fls(pending) - 1;
2342
2343 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2344 method == RESET_TYPE_RECOVER_OR_ALL) &&
2345 efx_try_recovery(efx))
2346 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002347
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002348 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002349 return;
2350
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002351 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002352
2353 /* We checked the state in efx_schedule_reset() but it may
2354 * have changed by now. Now that we have the RTNL lock,
2355 * it cannot change again.
2356 */
2357 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002358 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002359
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002360 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002361}
2362
2363void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2364{
2365 enum reset_type method;
2366
Alexandre Rames626950d2013-01-14 17:20:22 +00002367 if (efx->state == STATE_RECOVERY) {
2368 netif_dbg(efx, drv, efx->net_dev,
2369 "recovering: skip scheduling %s reset\n",
2370 RESET_TYPE(type));
2371 return;
2372 }
2373
Ben Hutchings8ceee662008-04-27 12:55:59 +01002374 switch (type) {
2375 case RESET_TYPE_INVISIBLE:
2376 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002377 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002378 case RESET_TYPE_WORLD:
2379 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002380 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002381 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002382 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2383 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002384 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002385 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002386 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002387 netif_dbg(efx, drv, efx->net_dev,
2388 "scheduling %s reset for %s\n",
2389 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002390 break;
2391 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002392
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002393 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002394 smp_mb(); /* ensure we change reset_pending before checking state */
2395
2396 /* If we're not READY then just leave the flags set as the cue
2397 * to abort probing or reschedule the reset later.
2398 */
2399 if (ACCESS_ONCE(efx->state) != STATE_READY)
2400 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002401
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002402 /* efx_process_channel() will no longer read events once a
2403 * reset is scheduled. So switch back to poll'd MCDI completions. */
2404 efx_mcdi_mode_poll(efx);
2405
Steve Hodgson1ab00622008-12-12 21:33:02 -08002406 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002407}
2408
2409/**************************************************************************
2410 *
2411 * List of NICs we support
2412 *
2413 **************************************************************************/
2414
2415/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002416static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002417 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2418 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002419 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002420 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2421 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002422 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002423 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002424 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002425 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002426 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002427 {0} /* end of list */
2428};
2429
2430/**************************************************************************
2431 *
Ben Hutchings37594332009-11-23 16:05:45 +00002432 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002433 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002434 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002435 * Needed so all function pointers are valid and do not have to be tested
2436 * before use
2437 *
2438 **************************************************************************/
2439int efx_port_dummy_op_int(struct efx_nic *efx)
2440{
2441 return 0;
2442}
2443void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002444
2445static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002446{
2447 return false;
2448}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002449
stephen hemminger6c8c2512011-04-14 05:50:12 +00002450static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002451 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002452 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002453 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002454 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002455};
2456
Ben Hutchings8ceee662008-04-27 12:55:59 +01002457/**************************************************************************
2458 *
2459 * Data housekeeping
2460 *
2461 **************************************************************************/
2462
2463/* This zeroes out and then fills in the invariants in a struct
2464 * efx_nic (including all sub-structures).
2465 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002466static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002467 struct pci_dev *pci_dev, struct net_device *net_dev)
2468{
Ben Hutchings46426102010-09-10 06:42:33 +00002469 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002470
2471 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002472 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002473#ifdef CONFIG_SFC_MTD
2474 INIT_LIST_HEAD(&efx->mtd_list);
2475#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002476 INIT_WORK(&efx->reset_work, efx_reset_work);
2477 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002478 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002479 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002480 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002481 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002482 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002483
2484 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002485 spin_lock_init(&efx->stats_lock);
2486 mutex_init(&efx->mac_lock);
2487 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002488 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002489 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002490 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002491
2492 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002493 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2494 if (!efx->channel[i])
2495 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002496 efx->msi_context[i].efx = efx;
2497 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002498 }
2499
Ben Hutchings8ceee662008-04-27 12:55:59 +01002500 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2501
2502 /* Higher numbered interrupt modes are less capable! */
2503 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2504 interrupt_mode);
2505
Ben Hutchings6977dc62008-12-26 13:44:39 -08002506 /* Would be good to use the net_dev name, but we're too early */
2507 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2508 pci_name(pci_dev));
2509 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002510 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002511 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002512
Ben Hutchings8ceee662008-04-27 12:55:59 +01002513 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002514
2515fail:
2516 efx_fini_struct(efx);
2517 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002518}
2519
2520static void efx_fini_struct(struct efx_nic *efx)
2521{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002522 int i;
2523
2524 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2525 kfree(efx->channel[i]);
2526
Ben Hutchings8ceee662008-04-27 12:55:59 +01002527 if (efx->workqueue) {
2528 destroy_workqueue(efx->workqueue);
2529 efx->workqueue = NULL;
2530 }
2531}
2532
2533/**************************************************************************
2534 *
2535 * PCI interface
2536 *
2537 **************************************************************************/
2538
2539/* Main body of final NIC shutdown code
2540 * This is called only at module unload (or hotplug removal).
2541 */
2542static void efx_pci_remove_main(struct efx_nic *efx)
2543{
Ben Hutchings7153f622012-07-27 20:50:52 +01002544 /* Flush reset_work. It can no longer be scheduled since we
2545 * are not READY.
2546 */
2547 BUG_ON(efx->state == STATE_READY);
2548 cancel_work_sync(&efx->reset_work);
2549
Ben Hutchingsd8291182012-10-05 23:35:41 +01002550 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002551 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002552 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002553 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002554 efx_fini_napi(efx);
2555 efx_remove_all(efx);
2556}
2557
2558/* Final NIC shutdown
2559 * This is called only at module unload (or hotplug removal).
2560 */
2561static void efx_pci_remove(struct pci_dev *pci_dev)
2562{
2563 struct efx_nic *efx;
2564
2565 efx = pci_get_drvdata(pci_dev);
2566 if (!efx)
2567 return;
2568
2569 /* Mark the NIC as fini, then stop the interface */
2570 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002571 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002572 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002573 rtnl_unlock();
2574
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002575 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002576 efx_unregister_netdev(efx);
2577
Ben Hutchings7dde5962008-12-12 22:09:38 -08002578 efx_mtd_remove(efx);
2579
Ben Hutchings8ceee662008-04-27 12:55:59 +01002580 efx_pci_remove_main(efx);
2581
Ben Hutchings8ceee662008-04-27 12:55:59 +01002582 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002583 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002584
Ben Hutchings8ceee662008-04-27 12:55:59 +01002585 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002586 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002587 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002588
2589 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002590};
2591
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002592/* NIC VPD information
2593 * Called during probe to display the part number of the
2594 * installed NIC. VPD is potentially very large but this should
2595 * always appear within the first 512 bytes.
2596 */
2597#define SFC_VPD_LEN 512
2598static void efx_print_product_vpd(struct efx_nic *efx)
2599{
2600 struct pci_dev *dev = efx->pci_dev;
2601 char vpd_data[SFC_VPD_LEN];
2602 ssize_t vpd_size;
2603 int i, j;
2604
2605 /* Get the vpd data from the device */
2606 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2607 if (vpd_size <= 0) {
2608 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2609 return;
2610 }
2611
2612 /* Get the Read only section */
2613 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2614 if (i < 0) {
2615 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2616 return;
2617 }
2618
2619 j = pci_vpd_lrdt_size(&vpd_data[i]);
2620 i += PCI_VPD_LRDT_TAG_SIZE;
2621 if (i + j > vpd_size)
2622 j = vpd_size - i;
2623
2624 /* Get the Part number */
2625 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2626 if (i < 0) {
2627 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2628 return;
2629 }
2630
2631 j = pci_vpd_info_field_size(&vpd_data[i]);
2632 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2633 if (i + j > vpd_size) {
2634 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2635 return;
2636 }
2637
2638 netif_info(efx, drv, efx->net_dev,
2639 "Part Number : %.*s\n", j, &vpd_data[i]);
2640}
2641
2642
Ben Hutchings8ceee662008-04-27 12:55:59 +01002643/* Main body of NIC initialisation
2644 * This is called at module load (or hotplug insertion, theoretically).
2645 */
2646static int efx_pci_probe_main(struct efx_nic *efx)
2647{
2648 int rc;
2649
2650 /* Do start-of-day initialisation */
2651 rc = efx_probe_all(efx);
2652 if (rc)
2653 goto fail1;
2654
Ben Hutchingse8f14992010-12-07 19:47:34 +00002655 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002656
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002657 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002658 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002659 netif_err(efx, probe, efx->net_dev,
2660 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002661 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002662 }
2663
2664 rc = efx_init_port(efx);
2665 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002666 netif_err(efx, probe, efx->net_dev,
2667 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002668 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002669 }
2670
Ben Hutchings152b6a62009-11-29 03:43:56 +00002671 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002672 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002673 goto fail5;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002674 efx_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002675
2676 return 0;
2677
Ben Hutchings278c0622009-11-23 16:05:12 +00002678 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002679 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002680 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002681 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002682 fail3:
2683 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002684 efx_remove_all(efx);
2685 fail1:
2686 return rc;
2687}
2688
2689/* NIC initialisation
2690 *
2691 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002692 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002693 * sets up and registers the network devices with the kernel and hooks
2694 * the interrupt service routine. It does not prepare the device for
2695 * transmission; this is left to the first time one of the network
2696 * interfaces is brought up (i.e. efx_net_open).
2697 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002698static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002699 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002700{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002701 struct net_device *net_dev;
2702 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002703 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002704
2705 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002706 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2707 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002708 if (!net_dev)
2709 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002710 efx = netdev_priv(net_dev);
2711 efx->type = (const struct efx_nic_type *) entry->driver_data;
2712 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002713 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002714 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002715 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002716 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002717 /* Mask for features that also apply to VLAN devices */
2718 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002719 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2720 NETIF_F_RXCSUM);
2721 /* All offloads can be toggled */
2722 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002723 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002724 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002725 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002726 if (rc)
2727 goto fail1;
2728
Ben Hutchings62776d02010-06-23 11:30:07 +00002729 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002730 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002731
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002732 efx_print_product_vpd(efx);
2733
Ben Hutchings8ceee662008-04-27 12:55:59 +01002734 /* Set up basic I/O (BAR mappings etc) */
2735 rc = efx_init_io(efx);
2736 if (rc)
2737 goto fail2;
2738
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002739 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002740 if (rc)
2741 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002742
Ben Hutchings8ceee662008-04-27 12:55:59 +01002743 rc = efx_register_netdev(efx);
2744 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002745 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002746
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002747 rc = efx_sriov_init(efx);
2748 if (rc)
2749 netif_err(efx, probe, efx->net_dev,
2750 "SR-IOV can't be enabled rc %d\n", rc);
2751
Ben Hutchings62776d02010-06-23 11:30:07 +00002752 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002753
Ben Hutchings7c431612012-01-27 17:23:58 +00002754 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002755 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002756 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002757 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002758 if (rc)
2759 netif_warn(efx, probe, efx->net_dev,
2760 "failed to create MTDs (%d)\n", rc);
2761
Alexandre Rames626950d2013-01-14 17:20:22 +00002762 rc = pci_enable_pcie_error_reporting(pci_dev);
2763 if (rc && rc != -EINVAL)
2764 netif_warn(efx, probe, efx->net_dev,
2765 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2766
Ben Hutchings8ceee662008-04-27 12:55:59 +01002767 return 0;
2768
Ben Hutchings8ceee662008-04-27 12:55:59 +01002769 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002770 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002771 fail3:
2772 efx_fini_io(efx);
2773 fail2:
2774 efx_fini_struct(efx);
2775 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002776 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002777 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002778 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002779 free_netdev(net_dev);
2780 return rc;
2781}
2782
Ben Hutchings89c758f2009-11-29 03:43:07 +00002783static int efx_pm_freeze(struct device *dev)
2784{
2785 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2786
Ben Hutchings61da0262012-07-27 19:35:39 +01002787 rtnl_lock();
2788
Ben Hutchings6032fb52012-07-27 19:35:47 +01002789 if (efx->state != STATE_DISABLED) {
2790 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002791
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002792 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002793
Ben Hutchings6032fb52012-07-27 19:35:47 +01002794 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002795 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01002796 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002797
Ben Hutchings61da0262012-07-27 19:35:39 +01002798 rtnl_unlock();
2799
Ben Hutchings89c758f2009-11-29 03:43:07 +00002800 return 0;
2801}
2802
2803static int efx_pm_thaw(struct device *dev)
2804{
2805 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2806
Ben Hutchings61da0262012-07-27 19:35:39 +01002807 rtnl_lock();
2808
Ben Hutchings6032fb52012-07-27 19:35:47 +01002809 if (efx->state != STATE_DISABLED) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01002810 efx_enable_interrupts(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002811
Ben Hutchings6032fb52012-07-27 19:35:47 +01002812 mutex_lock(&efx->mac_lock);
2813 efx->phy_op->reconfigure(efx);
2814 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002815
Ben Hutchings6032fb52012-07-27 19:35:47 +01002816 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002817
Ben Hutchings6032fb52012-07-27 19:35:47 +01002818 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002819
Ben Hutchings6032fb52012-07-27 19:35:47 +01002820 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002821
Ben Hutchings6032fb52012-07-27 19:35:47 +01002822 efx->type->resume_wol(efx);
2823 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002824
Ben Hutchings61da0262012-07-27 19:35:39 +01002825 rtnl_unlock();
2826
Steve Hodgson319ba642010-06-01 11:17:24 +00002827 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2828 queue_work(reset_workqueue, &efx->reset_work);
2829
Ben Hutchings89c758f2009-11-29 03:43:07 +00002830 return 0;
2831}
2832
2833static int efx_pm_poweroff(struct device *dev)
2834{
2835 struct pci_dev *pci_dev = to_pci_dev(dev);
2836 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2837
2838 efx->type->fini(efx);
2839
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002840 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002841
2842 pci_save_state(pci_dev);
2843 return pci_set_power_state(pci_dev, PCI_D3hot);
2844}
2845
2846/* Used for both resume and restore */
2847static int efx_pm_resume(struct device *dev)
2848{
2849 struct pci_dev *pci_dev = to_pci_dev(dev);
2850 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2851 int rc;
2852
2853 rc = pci_set_power_state(pci_dev, PCI_D0);
2854 if (rc)
2855 return rc;
2856 pci_restore_state(pci_dev);
2857 rc = pci_enable_device(pci_dev);
2858 if (rc)
2859 return rc;
2860 pci_set_master(efx->pci_dev);
2861 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2862 if (rc)
2863 return rc;
2864 rc = efx->type->init(efx);
2865 if (rc)
2866 return rc;
2867 efx_pm_thaw(dev);
2868 return 0;
2869}
2870
2871static int efx_pm_suspend(struct device *dev)
2872{
2873 int rc;
2874
2875 efx_pm_freeze(dev);
2876 rc = efx_pm_poweroff(dev);
2877 if (rc)
2878 efx_pm_resume(dev);
2879 return rc;
2880}
2881
Ben Hutchings18e83e42012-01-05 19:05:20 +00002882static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002883 .suspend = efx_pm_suspend,
2884 .resume = efx_pm_resume,
2885 .freeze = efx_pm_freeze,
2886 .thaw = efx_pm_thaw,
2887 .poweroff = efx_pm_poweroff,
2888 .restore = efx_pm_resume,
2889};
2890
Alexandre Rames626950d2013-01-14 17:20:22 +00002891/* A PCI error affecting this device was detected.
2892 * At this point MMIO and DMA may be disabled.
2893 * Stop the software path and request a slot reset.
2894 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002895static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2896 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00002897{
2898 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2899 struct efx_nic *efx = pci_get_drvdata(pdev);
2900
2901 if (state == pci_channel_io_perm_failure)
2902 return PCI_ERS_RESULT_DISCONNECT;
2903
2904 rtnl_lock();
2905
2906 if (efx->state != STATE_DISABLED) {
2907 efx->state = STATE_RECOVERY;
2908 efx->reset_pending = 0;
2909
2910 efx_device_detach_sync(efx);
2911
2912 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002913 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00002914
2915 status = PCI_ERS_RESULT_NEED_RESET;
2916 } else {
2917 /* If the interface is disabled we don't want to do anything
2918 * with it.
2919 */
2920 status = PCI_ERS_RESULT_RECOVERED;
2921 }
2922
2923 rtnl_unlock();
2924
2925 pci_disable_device(pdev);
2926
2927 return status;
2928}
2929
2930/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002931static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00002932{
2933 struct efx_nic *efx = pci_get_drvdata(pdev);
2934 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2935 int rc;
2936
2937 if (pci_enable_device(pdev)) {
2938 netif_err(efx, hw, efx->net_dev,
2939 "Cannot re-enable PCI device after reset.\n");
2940 status = PCI_ERS_RESULT_DISCONNECT;
2941 }
2942
2943 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2944 if (rc) {
2945 netif_err(efx, hw, efx->net_dev,
2946 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2947 /* Non-fatal error. Continue. */
2948 }
2949
2950 return status;
2951}
2952
2953/* Perform the actual reset and resume I/O operations. */
2954static void efx_io_resume(struct pci_dev *pdev)
2955{
2956 struct efx_nic *efx = pci_get_drvdata(pdev);
2957 int rc;
2958
2959 rtnl_lock();
2960
2961 if (efx->state == STATE_DISABLED)
2962 goto out;
2963
2964 rc = efx_reset(efx, RESET_TYPE_ALL);
2965 if (rc) {
2966 netif_err(efx, hw, efx->net_dev,
2967 "efx_reset failed after PCI error (%d)\n", rc);
2968 } else {
2969 efx->state = STATE_READY;
2970 netif_dbg(efx, hw, efx->net_dev,
2971 "Done resetting and resuming IO after PCI error.\n");
2972 }
2973
2974out:
2975 rtnl_unlock();
2976}
2977
2978/* For simplicity and reliability, we always require a slot reset and try to
2979 * reset the hardware when a pci error affecting the device is detected.
2980 * We leave both the link_reset and mmio_enabled callback unimplemented:
2981 * with our request for slot reset the mmio_enabled callback will never be
2982 * called, and the link_reset callback is not used by AER or EEH mechanisms.
2983 */
2984static struct pci_error_handlers efx_err_handlers = {
2985 .error_detected = efx_io_error_detected,
2986 .slot_reset = efx_io_slot_reset,
2987 .resume = efx_io_resume,
2988};
2989
Ben Hutchings8ceee662008-04-27 12:55:59 +01002990static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00002991 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002992 .id_table = efx_pci_table,
2993 .probe = efx_pci_probe,
2994 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00002995 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00002996 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002997};
2998
2999/**************************************************************************
3000 *
3001 * Kernel module interface
3002 *
3003 *************************************************************************/
3004
3005module_param(interrupt_mode, uint, 0444);
3006MODULE_PARM_DESC(interrupt_mode,
3007 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3008
3009static int __init efx_init_module(void)
3010{
3011 int rc;
3012
3013 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3014
3015 rc = register_netdevice_notifier(&efx_netdev_notifier);
3016 if (rc)
3017 goto err_notifier;
3018
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003019 rc = efx_init_sriov();
3020 if (rc)
3021 goto err_sriov;
3022
Steve Hodgson1ab00622008-12-12 21:33:02 -08003023 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3024 if (!reset_workqueue) {
3025 rc = -ENOMEM;
3026 goto err_reset;
3027 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003028
3029 rc = pci_register_driver(&efx_pci_driver);
3030 if (rc < 0)
3031 goto err_pci;
3032
3033 return 0;
3034
3035 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003036 destroy_workqueue(reset_workqueue);
3037 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003038 efx_fini_sriov();
3039 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003040 unregister_netdevice_notifier(&efx_netdev_notifier);
3041 err_notifier:
3042 return rc;
3043}
3044
3045static void __exit efx_exit_module(void)
3046{
3047 printk(KERN_INFO "Solarflare NET driver unloading\n");
3048
3049 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003050 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003051 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003052 unregister_netdevice_notifier(&efx_netdev_notifier);
3053
3054}
3055
3056module_init(efx_init_module);
3057module_exit(efx_exit_module);
3058
Ben Hutchings906bb262009-11-29 15:16:19 +00003059MODULE_AUTHOR("Solarflare Communications and "
3060 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003061MODULE_DESCRIPTION("Solarflare Communications network driver");
3062MODULE_LICENSE("GPL");
3063MODULE_DEVICE_TABLE(pci, efx_pci_table);