blob: 9c6555c12acf40cf51de820c8b0bc81b16c32d95 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchings0a6f40c2011-02-25 00:01:34 +00004 * Copyright 2005-2011 Solarflare Communications Inc.
Ben Hutchings8ceee662008-04-27 12:55:59 +01005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/delay.h>
16#include <linux/notifier.h>
17#include <linux/ip.h>
18#include <linux/tcp.h>
19#include <linux/in.h>
20#include <linux/crc32.h>
21#include <linux/ethtool.h>
Ben Hutchingsaa6ef272008-07-18 19:03:10 +010022#include <linux/topology.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/gfp.h>
Alexandre Rames626950d2013-01-14 17:20:22 +000024#include <linux/aer.h>
Alexandre Ramesb28405b2013-03-21 16:41:43 +000025#include <linux/interrupt.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010027#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000028#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000029#include "selftest.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010030
Ben Hutchings8880f4e2009-11-29 15:15:41 +000031#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000032#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000033
Ben Hutchingsc4593022009-11-23 16:08:17 +000034/**************************************************************************
35 *
36 * Type name strings
37 *
38 **************************************************************************
39 */
40
41/* Loopback mode names (see LOOPBACK_MODE()) */
42const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000043const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000044 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000045 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000046 [LOOPBACK_GMAC] = "GMAC",
47 [LOOPBACK_XGMII] = "XGMII",
48 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000049 [LOOPBACK_XAUI] = "XAUI",
50 [LOOPBACK_GMII] = "GMII",
51 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000052 [LOOPBACK_XGBR] = "XGBR",
53 [LOOPBACK_XFI] = "XFI",
54 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
55 [LOOPBACK_GMII_FAR] = "GMII_FAR",
56 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
57 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000058 [LOOPBACK_GPHY] = "GPHY",
59 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000060 [LOOPBACK_PCS] = "PCS",
61 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000062 [LOOPBACK_XPORT] = "XPORT",
63 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000064 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000065 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
66 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000067 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000068 [LOOPBACK_XFI_WS] = "XFI_WS",
69 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000070 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000071};
72
Ben Hutchingsc4593022009-11-23 16:08:17 +000073const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000074const char *const efx_reset_type_names[] = {
Alexandre Rames626950d2013-01-14 17:20:22 +000075 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
76 [RESET_TYPE_ALL] = "ALL",
77 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
78 [RESET_TYPE_WORLD] = "WORLD",
79 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
80 [RESET_TYPE_DISABLE] = "DISABLE",
81 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
82 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
83 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
84 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
85 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
86 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
87 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Ben Hutchingsc4593022009-11-23 16:08:17 +000088};
89
Steve Hodgson1ab00622008-12-12 21:33:02 -080090/* Reset workqueue. If any NIC has a hardware failure then a reset will be
91 * queued onto this work queue. This is not a per-nic work queue, because
92 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
93 */
94static struct workqueue_struct *reset_workqueue;
95
Ben Hutchings8ceee662008-04-27 12:55:59 +010096/**************************************************************************
97 *
98 * Configurable values
99 *
100 *************************************************************************/
101
102/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100103 * Use separate channels for TX and RX events
104 *
Neil Turton28b581a2008-12-12 21:41:06 -0800105 * Set this to 1 to use separate channels for TX and RX. It allows us
106 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100107 *
Neil Turton28b581a2008-12-12 21:41:06 -0800108 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100109 */
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000110static bool separate_tx_channels;
111module_param(separate_tx_channels, bool, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800112MODULE_PARM_DESC(separate_tx_channels,
113 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100114
115/* This is the weight assigned to each of the (per-channel) virtual
116 * NAPI devices.
117 */
118static int napi_weight = 64;
119
120/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000121 * monitor.
122 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000123 * - Check the on-board hardware monitor;
124 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000125 * On Siena-based NICs for power systems with EEH support, this will give EEH a
126 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100127 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000128static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100129
Ben Hutchings8ceee662008-04-27 12:55:59 +0100130/* Initial interrupt moderation settings. They can be modified after
131 * module load with ethtool.
132 *
133 * The default for RX should strike a balance between increasing the
134 * round-trip latency and reducing overhead.
135 */
136static unsigned int rx_irq_mod_usec = 60;
137
138/* Initial interrupt moderation settings. They can be modified after
139 * module load with ethtool.
140 *
141 * This default is chosen to ensure that a 10G link does not go idle
142 * while a TX queue is stopped after it has become full. A queue is
143 * restarted when it drops below half full. The time this takes (assuming
144 * worst case 3 descriptors per packet and 1024 descriptors) is
145 * 512 / 3 * 1.2 = 205 usec.
146 */
147static unsigned int tx_irq_mod_usec = 150;
148
149/* This is the first interrupt mode to try out of:
150 * 0 => MSI-X
151 * 1 => MSI
152 * 2 => legacy
153 */
154static unsigned int interrupt_mode;
155
156/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
157 * i.e. the number of CPUs among which we may distribute simultaneous
158 * interrupt handling.
159 *
160 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000161 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100162 */
163static unsigned int rss_cpus;
164module_param(rss_cpus, uint, 0444);
165MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
166
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000167static bool phy_flash_cfg;
168module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800169MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
170
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000171static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000172module_param(irq_adapt_low_thresh, uint, 0644);
173MODULE_PARM_DESC(irq_adapt_low_thresh,
174 "Threshold score for reducing IRQ moderation");
175
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000176static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000177module_param(irq_adapt_high_thresh, uint, 0644);
178MODULE_PARM_DESC(irq_adapt_high_thresh,
179 "Threshold score for increasing IRQ moderation");
180
Ben Hutchings62776d02010-06-23 11:30:07 +0000181static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
182 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
183 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
184 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
185module_param(debug, uint, 0);
186MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
187
Ben Hutchings8ceee662008-04-27 12:55:59 +0100188/**************************************************************************
189 *
190 * Utility functions and prototypes
191 *
192 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000193
Ben Hutchingsd8291182012-10-05 23:35:41 +0100194static void efx_soft_enable_interrupts(struct efx_nic *efx);
195static void efx_soft_disable_interrupts(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000196static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000197static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000198static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100199static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000200static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100201static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000202static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000203static void efx_fini_struct(struct efx_nic *efx);
204static void efx_start_all(struct efx_nic *efx);
205static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100206
207#define EFX_ASSERT_RESET_SERIALISED(efx) \
208 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100209 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000210 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000211 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100212 ASSERT_RTNL(); \
213 } while (0)
214
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100215static int efx_check_disabled(struct efx_nic *efx)
216{
Alexandre Rames626950d2013-01-14 17:20:22 +0000217 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100218 netif_err(efx, drv, efx->net_dev,
219 "device is disabled due to earlier errors\n");
220 return -EIO;
221 }
222 return 0;
223}
224
Ben Hutchings8ceee662008-04-27 12:55:59 +0100225/**************************************************************************
226 *
227 * Event queue processing
228 *
229 *************************************************************************/
230
231/* Process channel's event queue
232 *
233 * This function is responsible for processing the event queue of a
234 * single channel. The caller must guarantee that this function will
235 * never be concurrently called more than once on the same channel,
236 * though different channels may be being processed concurrently.
237 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000238static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100239{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000240 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100241
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000242 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100243 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100244
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000245 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000246 if (spent && efx_channel_has_rx_queue(channel)) {
247 struct efx_rx_queue *rx_queue =
248 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100249
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000250 efx_rx_flush_packet(channel);
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100251 efx_fast_push_rx_descriptors(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100252 }
253
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000254 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100255}
256
Ben Hutchings8ceee662008-04-27 12:55:59 +0100257/* NAPI poll handler
258 *
259 * NAPI guarantees serialisation of polls of the same device, which
260 * provides the guarantee required by efx_process_channel().
261 */
262static int efx_poll(struct napi_struct *napi, int budget)
263{
264 struct efx_channel *channel =
265 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000266 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000267 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100268
Ben Hutchings62776d02010-06-23 11:30:07 +0000269 netif_vdbg(efx, intr, efx->net_dev,
270 "channel %d NAPI poll executing on CPU %d\n",
271 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100272
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000273 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100274
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000275 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000276 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000277 efx->irq_rx_adaptive &&
278 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000279 if (unlikely(channel->irq_mod_score <
280 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000281 if (channel->irq_moderation > 1) {
282 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000283 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000284 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000285 } else if (unlikely(channel->irq_mod_score >
286 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000287 if (channel->irq_moderation <
288 efx->irq_rx_moderation) {
289 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000290 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000291 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000292 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000293 channel->irq_count = 0;
294 channel->irq_mod_score = 0;
295 }
296
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000297 efx_filter_rfs_expire(channel);
298
Ben Hutchings8ceee662008-04-27 12:55:59 +0100299 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800300 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100301 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100302 * interrupts have already been disabled.
303 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800304 napi_complete(napi);
Ben Hutchings514bedb2012-10-05 19:30:16 +0100305 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100306 }
307
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000308 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100309}
310
Ben Hutchings8ceee662008-04-27 12:55:59 +0100311/* Create event queue
312 * Event queue memory allocations are done only once. If the channel
313 * is reset, the memory buffer will be reused; this guards against
314 * errors during channel reset and also simplifies interrupt handling.
315 */
316static int efx_probe_eventq(struct efx_channel *channel)
317{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000318 struct efx_nic *efx = channel->efx;
319 unsigned long entries;
320
Ben Hutchings86ee5302012-01-09 19:51:22 +0000321 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000322 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100323
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000324 /* Build an event queue with room for one event per tx and rx buffer,
325 * plus some extra for link state events and MCDI completions. */
326 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
327 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
328 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
329
Ben Hutchings152b6a62009-11-29 03:43:56 +0000330 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100331}
332
333/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100334static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100335{
Ben Hutchings62776d02010-06-23 11:30:07 +0000336 netif_dbg(channel->efx, drv, channel->efx->net_dev,
337 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100338
339 channel->eventq_read_ptr = 0;
340
Ben Hutchings152b6a62009-11-29 03:43:56 +0000341 efx_nic_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100342}
343
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000344/* Enable event queue processing and NAPI */
345static void efx_start_eventq(struct efx_channel *channel)
346{
347 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
348 "chan %d start event queue\n", channel->channel);
349
Ben Hutchings514bedb2012-10-05 19:30:16 +0100350 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000351 channel->enabled = true;
352 smp_wmb();
353
354 napi_enable(&channel->napi_str);
355 efx_nic_eventq_read_ack(channel);
356}
357
358/* Disable event queue processing and NAPI */
359static void efx_stop_eventq(struct efx_channel *channel)
360{
361 if (!channel->enabled)
362 return;
363
364 napi_disable(&channel->napi_str);
365 channel->enabled = false;
366}
367
Ben Hutchings8ceee662008-04-27 12:55:59 +0100368static void efx_fini_eventq(struct efx_channel *channel)
369{
Ben Hutchings62776d02010-06-23 11:30:07 +0000370 netif_dbg(channel->efx, drv, channel->efx->net_dev,
371 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100372
Ben Hutchings152b6a62009-11-29 03:43:56 +0000373 efx_nic_fini_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100374}
375
376static void efx_remove_eventq(struct efx_channel *channel)
377{
Ben Hutchings62776d02010-06-23 11:30:07 +0000378 netif_dbg(channel->efx, drv, channel->efx->net_dev,
379 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100380
Ben Hutchings152b6a62009-11-29 03:43:56 +0000381 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100382}
383
384/**************************************************************************
385 *
386 * Channel handling
387 *
388 *************************************************************************/
389
Ben Hutchings7f967c02012-02-13 23:45:02 +0000390/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000391static struct efx_channel *
392efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
393{
394 struct efx_channel *channel;
395 struct efx_rx_queue *rx_queue;
396 struct efx_tx_queue *tx_queue;
397 int j;
398
Ben Hutchings7f967c02012-02-13 23:45:02 +0000399 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
400 if (!channel)
401 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000402
Ben Hutchings7f967c02012-02-13 23:45:02 +0000403 channel->efx = efx;
404 channel->channel = i;
405 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000406
Ben Hutchings7f967c02012-02-13 23:45:02 +0000407 for (j = 0; j < EFX_TXQ_TYPES; j++) {
408 tx_queue = &channel->tx_queue[j];
409 tx_queue->efx = efx;
410 tx_queue->queue = i * EFX_TXQ_TYPES + j;
411 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000412 }
413
Ben Hutchings46426102010-09-10 06:42:33 +0000414 rx_queue = &channel->rx_queue;
415 rx_queue->efx = efx;
416 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
417 (unsigned long)rx_queue);
418
419 return channel;
420}
421
Ben Hutchings7f967c02012-02-13 23:45:02 +0000422/* Allocate and initialise a channel structure, copying parameters
423 * (but not resources) from an old channel structure.
424 */
425static struct efx_channel *
426efx_copy_channel(const struct efx_channel *old_channel)
427{
428 struct efx_channel *channel;
429 struct efx_rx_queue *rx_queue;
430 struct efx_tx_queue *tx_queue;
431 int j;
432
433 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
434 if (!channel)
435 return NULL;
436
437 *channel = *old_channel;
438
439 channel->napi_dev = NULL;
440 memset(&channel->eventq, 0, sizeof(channel->eventq));
441
442 for (j = 0; j < EFX_TXQ_TYPES; j++) {
443 tx_queue = &channel->tx_queue[j];
444 if (tx_queue->channel)
445 tx_queue->channel = channel;
446 tx_queue->buffer = NULL;
447 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
448 }
449
450 rx_queue = &channel->rx_queue;
451 rx_queue->buffer = NULL;
452 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
453 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
454 (unsigned long)rx_queue);
455
456 return channel;
457}
458
Ben Hutchings8ceee662008-04-27 12:55:59 +0100459static int efx_probe_channel(struct efx_channel *channel)
460{
461 struct efx_tx_queue *tx_queue;
462 struct efx_rx_queue *rx_queue;
463 int rc;
464
Ben Hutchings62776d02010-06-23 11:30:07 +0000465 netif_dbg(channel->efx, probe, channel->efx->net_dev,
466 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100467
Ben Hutchings7f967c02012-02-13 23:45:02 +0000468 rc = channel->type->pre_probe(channel);
469 if (rc)
470 goto fail;
471
Ben Hutchings8ceee662008-04-27 12:55:59 +0100472 rc = efx_probe_eventq(channel);
473 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000474 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100475
476 efx_for_each_channel_tx_queue(tx_queue, channel) {
477 rc = efx_probe_tx_queue(tx_queue);
478 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000479 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100480 }
481
482 efx_for_each_channel_rx_queue(rx_queue, channel) {
483 rc = efx_probe_rx_queue(rx_queue);
484 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000485 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100486 }
487
488 channel->n_rx_frm_trunc = 0;
489
490 return 0;
491
Ben Hutchings7f967c02012-02-13 23:45:02 +0000492fail:
493 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100494 return rc;
495}
496
Ben Hutchings7f967c02012-02-13 23:45:02 +0000497static void
498efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
499{
500 struct efx_nic *efx = channel->efx;
501 const char *type;
502 int number;
503
504 number = channel->channel;
505 if (efx->tx_channel_offset == 0) {
506 type = "";
507 } else if (channel->channel < efx->tx_channel_offset) {
508 type = "-rx";
509 } else {
510 type = "-tx";
511 number -= efx->tx_channel_offset;
512 }
513 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
514}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100515
Ben Hutchings56536e92008-12-12 21:37:02 -0800516static void efx_set_channel_names(struct efx_nic *efx)
517{
518 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800519
Ben Hutchings7f967c02012-02-13 23:45:02 +0000520 efx_for_each_channel(channel, efx)
521 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100522 efx->msi_context[channel->channel].name,
523 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800524}
525
Ben Hutchings46426102010-09-10 06:42:33 +0000526static int efx_probe_channels(struct efx_nic *efx)
527{
528 struct efx_channel *channel;
529 int rc;
530
531 /* Restart special buffer allocation */
532 efx->next_buffer_table = 0;
533
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000534 /* Probe channels in reverse, so that any 'extra' channels
535 * use the start of the buffer table. This allows the traffic
536 * channels to be resized without moving them or wasting the
537 * entries before them.
538 */
539 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000540 rc = efx_probe_channel(channel);
541 if (rc) {
542 netif_err(efx, probe, efx->net_dev,
543 "failed to create channel %d\n",
544 channel->channel);
545 goto fail;
546 }
547 }
548 efx_set_channel_names(efx);
549
550 return 0;
551
552fail:
553 efx_remove_channels(efx);
554 return rc;
555}
556
Ben Hutchings8ceee662008-04-27 12:55:59 +0100557/* Channels are shutdown and reinitialised whilst the NIC is running
558 * to propagate configuration changes (mtu, checksum offload), or
559 * to clear hardware error conditions
560 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000561static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100562{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000563 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100564 struct efx_tx_queue *tx_queue;
565 struct efx_rx_queue *rx_queue;
566 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000567 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100568
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100569 /* Calculate the rx buffer allocation parameters required to
570 * support the current MTU, including padding for header
571 * alignment and overruns.
572 */
Ben Hutchings272baee2013-01-29 23:33:14 +0000573 efx->rx_dma_len = (efx->type->rx_buffer_hash_size +
574 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
575 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000576 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Ben Hutchingsc14ff2e2013-05-13 11:58:31 +0000577 NET_IP_ALIGN + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000578 if (rx_buf_len <= PAGE_SIZE) {
579 efx->rx_scatter = false;
580 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000581 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000582 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000583 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000584 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
585 EFX_RX_BUF_ALIGNMENT) >
586 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000587 efx->rx_scatter = true;
588 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
589 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000590 } else {
591 efx->rx_scatter = false;
592 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000593 }
594
Daniel Pieczko1648a232013-02-13 10:54:41 +0000595 efx_rx_config_page_split(efx);
596 if (efx->rx_buffer_order)
597 netif_dbg(efx, drv, efx->net_dev,
598 "RX buf len=%u; page order=%u batch=%u\n",
599 efx->rx_dma_len, efx->rx_buffer_order,
600 efx->rx_pages_per_batch);
601 else
602 netif_dbg(efx, drv, efx->net_dev,
603 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
604 efx->rx_dma_len, efx->rx_page_buf_step,
605 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000606
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000607 /* RX filters also have scatter-enabled flags */
608 if (efx->rx_scatter != old_rx_scatter)
609 efx_filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100610
Ben Hutchings14bf7182012-05-22 01:27:58 +0100611 /* We must keep at least one descriptor in a TX ring empty.
612 * We could avoid this when the queue size does not exactly
613 * match the hardware ring size, but it's not that important.
614 * Therefore we stop the queue when one more skb might fill
615 * the ring completely. We wake it when half way back to
616 * empty.
617 */
618 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
619 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
620
Ben Hutchings8ceee662008-04-27 12:55:59 +0100621 /* Initialise the channels */
622 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100623 efx_for_each_channel_tx_queue(tx_queue, channel)
624 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100625
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000626 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100627 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000628 efx_nic_generate_fill_event(rx_queue);
629 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100630
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000631 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100632 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000633
634 if (netif_device_present(efx->net_dev))
635 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100636}
637
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000638static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100639{
640 struct efx_channel *channel;
641 struct efx_tx_queue *tx_queue;
642 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100643 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100644
645 EFX_ASSERT_RESET_SERIALISED(efx);
646 BUG_ON(efx->port_enabled);
647
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100648 /* Stop RX refill */
649 efx_for_each_channel(channel, efx) {
650 efx_for_each_channel_rx_queue(rx_queue, channel)
651 rx_queue->refill_enabled = false;
652 }
653
Ben Hutchings8ceee662008-04-27 12:55:59 +0100654 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000655 /* RX packet processing is pipelined, so wait for the
656 * NAPI handler to complete. At least event queue 0
657 * might be kept active by non-data events, so don't
658 * use napi_synchronize() but actually disable NAPI
659 * temporarily.
660 */
661 if (efx_channel_has_rx_queue(channel)) {
662 efx_stop_eventq(channel);
663 efx_start_eventq(channel);
664 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100665 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100666
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100667 rc = efx->type->fini_dmaq(efx);
668 if (rc && EFX_WORKAROUND_7803(efx)) {
669 /* Schedule a reset to recover from the flush failure. The
670 * descriptor caches reference memory we're about to free,
671 * but falcon_reconfigure_mac_wrapper() won't reconnect
672 * the MACs because of the pending reset.
673 */
674 netif_err(efx, drv, efx->net_dev,
675 "Resetting to recover from flush failure\n");
676 efx_schedule_reset(efx, RESET_TYPE_ALL);
677 } else if (rc) {
678 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
679 } else {
680 netif_dbg(efx, drv, efx->net_dev,
681 "successfully flushed all queues\n");
682 }
683
684 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100685 efx_for_each_channel_rx_queue(rx_queue, channel)
686 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000687 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100688 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100689 }
690}
691
692static void efx_remove_channel(struct efx_channel *channel)
693{
694 struct efx_tx_queue *tx_queue;
695 struct efx_rx_queue *rx_queue;
696
Ben Hutchings62776d02010-06-23 11:30:07 +0000697 netif_dbg(channel->efx, drv, channel->efx->net_dev,
698 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100699
700 efx_for_each_channel_rx_queue(rx_queue, channel)
701 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000702 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100703 efx_remove_tx_queue(tx_queue);
704 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100705 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100706}
707
Ben Hutchings46426102010-09-10 06:42:33 +0000708static void efx_remove_channels(struct efx_nic *efx)
709{
710 struct efx_channel *channel;
711
712 efx_for_each_channel(channel, efx)
713 efx_remove_channel(channel);
714}
715
716int
717efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
718{
719 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
720 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000721 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100722 int rc;
723
724 rc = efx_check_disabled(efx);
725 if (rc)
726 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000727
728 /* Not all channels should be reallocated. We must avoid
729 * reallocating their buffer table entries.
730 */
731 efx_for_each_channel(channel, efx) {
732 struct efx_rx_queue *rx_queue;
733 struct efx_tx_queue *tx_queue;
734
735 if (channel->type->copy)
736 continue;
737 next_buffer_table = max(next_buffer_table,
738 channel->eventq.index +
739 channel->eventq.entries);
740 efx_for_each_channel_rx_queue(rx_queue, channel)
741 next_buffer_table = max(next_buffer_table,
742 rx_queue->rxd.index +
743 rx_queue->rxd.entries);
744 efx_for_each_channel_tx_queue(tx_queue, channel)
745 next_buffer_table = max(next_buffer_table,
746 tx_queue->txd.index +
747 tx_queue->txd.entries);
748 }
Ben Hutchings46426102010-09-10 06:42:33 +0000749
Ben Hutchings29c69a42013-01-28 19:01:06 +0000750 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000751 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100752 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000753
Ben Hutchings7f967c02012-02-13 23:45:02 +0000754 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000755 memset(other_channel, 0, sizeof(other_channel));
756 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000757 channel = efx->channel[i];
758 if (channel->type->copy)
759 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000760 if (!channel) {
761 rc = -ENOMEM;
762 goto out;
763 }
764 other_channel[i] = channel;
765 }
766
767 /* Swap entry counts and channel pointers */
768 old_rxq_entries = efx->rxq_entries;
769 old_txq_entries = efx->txq_entries;
770 efx->rxq_entries = rxq_entries;
771 efx->txq_entries = txq_entries;
772 for (i = 0; i < efx->n_channels; i++) {
773 channel = efx->channel[i];
774 efx->channel[i] = other_channel[i];
775 other_channel[i] = channel;
776 }
777
Ben Hutchings7f967c02012-02-13 23:45:02 +0000778 /* Restart buffer table allocation */
779 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000780
Ben Hutchingse8f14992010-12-07 19:47:34 +0000781 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000782 channel = efx->channel[i];
783 if (!channel->type->copy)
784 continue;
785 rc = efx_probe_channel(channel);
786 if (rc)
787 goto rollback;
788 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000789 }
Ben Hutchings46426102010-09-10 06:42:33 +0000790
Ben Hutchings7f967c02012-02-13 23:45:02 +0000791out:
792 /* Destroy unused channel structures */
793 for (i = 0; i < efx->n_channels; i++) {
794 channel = other_channel[i];
795 if (channel && channel->type->copy) {
796 efx_fini_napi_channel(channel);
797 efx_remove_channel(channel);
798 kfree(channel);
799 }
800 }
801
Ben Hutchingsd8291182012-10-05 23:35:41 +0100802 efx_soft_enable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000803 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000804 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000805 return rc;
806
807rollback:
808 /* Swap back */
809 efx->rxq_entries = old_rxq_entries;
810 efx->txq_entries = old_txq_entries;
811 for (i = 0; i < efx->n_channels; i++) {
812 channel = efx->channel[i];
813 efx->channel[i] = other_channel[i];
814 other_channel[i] = channel;
815 }
816 goto out;
817}
818
Steve Hodgson90d683a2010-06-01 11:19:39 +0000819void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100820{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000821 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100822}
823
Ben Hutchings7f967c02012-02-13 23:45:02 +0000824static const struct efx_channel_type efx_default_channel_type = {
825 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100826 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000827 .get_name = efx_get_channel_name,
828 .copy = efx_copy_channel,
829 .keep_eventq = false,
830};
831
832int efx_channel_dummy_op_int(struct efx_channel *channel)
833{
834 return 0;
835}
836
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100837void efx_channel_dummy_op_void(struct efx_channel *channel)
838{
839}
840
Ben Hutchings8ceee662008-04-27 12:55:59 +0100841/**************************************************************************
842 *
843 * Port handling
844 *
845 **************************************************************************/
846
847/* This ensures that the kernel is kept informed (via
848 * netif_carrier_on/off) of the link status, and also maintains the
849 * link status's stop on the port's TX queue.
850 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000851void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100852{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000853 struct efx_link_state *link_state = &efx->link_state;
854
Ben Hutchings8ceee662008-04-27 12:55:59 +0100855 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
856 * that no events are triggered between unregister_netdev() and the
857 * driver unloading. A more general condition is that NETDEV_CHANGE
858 * can only be generated between NETDEV_UP and NETDEV_DOWN */
859 if (!netif_running(efx->net_dev))
860 return;
861
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000862 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100863 efx->n_link_state_changes++;
864
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000865 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100866 netif_carrier_on(efx->net_dev);
867 else
868 netif_carrier_off(efx->net_dev);
869 }
870
871 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000872 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000873 netif_info(efx, link, efx->net_dev,
874 "link up at %uMbps %s-duplex (MTU %d)%s\n",
875 link_state->speed, link_state->fd ? "full" : "half",
876 efx->net_dev->mtu,
877 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000878 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000879 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100880}
881
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000882void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
883{
884 efx->link_advertising = advertising;
885 if (advertising) {
886 if (advertising & ADVERTISED_Pause)
887 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
888 else
889 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
890 if (advertising & ADVERTISED_Asym_Pause)
891 efx->wanted_fc ^= EFX_FC_TX;
892 }
893}
894
David S. Millerb56269462011-05-17 17:53:22 -0400895void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000896{
897 efx->wanted_fc = wanted_fc;
898 if (efx->link_advertising) {
899 if (wanted_fc & EFX_FC_RX)
900 efx->link_advertising |= (ADVERTISED_Pause |
901 ADVERTISED_Asym_Pause);
902 else
903 efx->link_advertising &= ~(ADVERTISED_Pause |
904 ADVERTISED_Asym_Pause);
905 if (wanted_fc & EFX_FC_TX)
906 efx->link_advertising ^= ADVERTISED_Asym_Pause;
907 }
908}
909
Ben Hutchings115122a2009-03-04 09:52:52 +0000910static void efx_fini_port(struct efx_nic *efx);
911
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000912/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
913 * the MAC appropriately. All other PHY configuration changes are pushed
914 * through phy_op->set_settings(), and pushed asynchronously to the MAC
915 * through efx_monitor().
916 *
917 * Callers must hold the mac_lock
918 */
919int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100920{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000921 enum efx_phy_mode phy_mode;
922 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100923
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000924 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100925
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000926 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000927 netif_addr_lock_bh(efx->net_dev);
928 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100929
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000930 /* Disable PHY transmit in mac level loopbacks */
931 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800932 if (LOOPBACK_INTERNAL(efx))
933 efx->phy_mode |= PHY_MODE_TX_DISABLED;
934 else
935 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800936
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000937 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800938
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000939 if (rc)
940 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100941
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000942 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100943}
944
945/* Reinitialise the MAC to pick up new PHY settings, even if the port is
946 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000947int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100948{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000949 int rc;
950
Ben Hutchings8ceee662008-04-27 12:55:59 +0100951 EFX_ASSERT_RESET_SERIALISED(efx);
952
953 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000954 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100955 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000956
957 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100958}
959
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000960/* Asynchronous work item for changing MAC promiscuity and multicast
961 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
962 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800963static void efx_mac_work(struct work_struct *data)
964{
965 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
966
967 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100968 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100969 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800970 mutex_unlock(&efx->mac_lock);
971}
972
Ben Hutchings8ceee662008-04-27 12:55:59 +0100973static int efx_probe_port(struct efx_nic *efx)
974{
975 int rc;
976
Ben Hutchings62776d02010-06-23 11:30:07 +0000977 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100978
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000979 if (phy_flash_cfg)
980 efx->phy_mode = PHY_MODE_SPECIAL;
981
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000982 /* Connect up MAC/PHY operations table */
983 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100984 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000985 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100986
Ben Hutchingse332bcb2011-12-20 01:22:51 +0000987 /* Initialise MAC address to permanent address */
988 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100989
990 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100991}
992
993static int efx_init_port(struct efx_nic *efx)
994{
995 int rc;
996
Ben Hutchings62776d02010-06-23 11:30:07 +0000997 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100998
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000999 mutex_lock(&efx->mac_lock);
1000
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001001 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001002 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001003 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001004
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001005 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001006
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001007 /* Reconfigure the MAC before creating dma queues (required for
1008 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001009 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001010
1011 /* Ensure the PHY advertises the correct flow control settings */
1012 rc = efx->phy_op->reconfigure(efx);
1013 if (rc)
1014 goto fail2;
1015
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001016 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001017 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001018
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001019fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001020 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001021fail1:
1022 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001023 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001024}
1025
Ben Hutchings8ceee662008-04-27 12:55:59 +01001026static void efx_start_port(struct efx_nic *efx)
1027{
Ben Hutchings62776d02010-06-23 11:30:07 +00001028 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001029 BUG_ON(efx->port_enabled);
1030
1031 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001032 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001033
1034 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1035 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001036 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001037
Ben Hutchings8ceee662008-04-27 12:55:59 +01001038 mutex_unlock(&efx->mac_lock);
1039}
1040
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001041/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001042static void efx_stop_port(struct efx_nic *efx)
1043{
Ben Hutchings62776d02010-06-23 11:30:07 +00001044 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001045
1046 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001047 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001048 mutex_unlock(&efx->mac_lock);
1049
1050 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001051 netif_addr_lock_bh(efx->net_dev);
1052 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001053}
1054
1055static void efx_fini_port(struct efx_nic *efx)
1056{
Ben Hutchings62776d02010-06-23 11:30:07 +00001057 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001058
1059 if (!efx->port_initialized)
1060 return;
1061
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001062 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001063 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001064
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001065 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001066 efx_link_status_changed(efx);
1067}
1068
1069static void efx_remove_port(struct efx_nic *efx)
1070{
Ben Hutchings62776d02010-06-23 11:30:07 +00001071 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001072
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001073 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001074}
1075
1076/**************************************************************************
1077 *
1078 * NIC handling
1079 *
1080 **************************************************************************/
1081
1082/* This configures the PCI device to enable I/O and DMA. */
1083static int efx_init_io(struct efx_nic *efx)
1084{
1085 struct pci_dev *pci_dev = efx->pci_dev;
1086 dma_addr_t dma_mask = efx->type->max_dma_mask;
1087 int rc;
1088
Ben Hutchings62776d02010-06-23 11:30:07 +00001089 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001090
1091 rc = pci_enable_device(pci_dev);
1092 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001093 netif_err(efx, probe, efx->net_dev,
1094 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001095 goto fail1;
1096 }
1097
1098 pci_set_master(pci_dev);
1099
1100 /* Set the PCI DMA mask. Try all possibilities from our
1101 * genuine mask down to 32 bits, because some architectures
1102 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1103 * masks event though they reject 46 bit masks.
1104 */
1105 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001106 if (dma_supported(&pci_dev->dev, dma_mask)) {
1107 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001108 if (rc == 0)
1109 break;
1110 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001111 dma_mask >>= 1;
1112 }
1113 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001114 netif_err(efx, probe, efx->net_dev,
1115 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001116 goto fail2;
1117 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001118 netif_dbg(efx, probe, efx->net_dev,
1119 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001120 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001121 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001122 /* dma_set_coherent_mask() is not *allowed* to
1123 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001124 * but just in case...
1125 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001126 netif_err(efx, probe, efx->net_dev,
1127 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001128 goto fail2;
1129 }
1130
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001131 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1132 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001133 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001134 netif_err(efx, probe, efx->net_dev,
1135 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001136 rc = -EIO;
1137 goto fail3;
1138 }
David S. Miller8decf862011-09-22 03:23:13 -04001139 efx->membase = ioremap_nocache(efx->membase_phys,
1140 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001141 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001142 netif_err(efx, probe, efx->net_dev,
1143 "could not map memory BAR at %llx+%x\n",
1144 (unsigned long long)efx->membase_phys,
1145 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001146 rc = -ENOMEM;
1147 goto fail4;
1148 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001149 netif_dbg(efx, probe, efx->net_dev,
1150 "memory BAR at %llx+%x (virtual %p)\n",
1151 (unsigned long long)efx->membase_phys,
1152 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001153
1154 return 0;
1155
1156 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001157 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001158 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001159 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001160 fail2:
1161 pci_disable_device(efx->pci_dev);
1162 fail1:
1163 return rc;
1164}
1165
1166static void efx_fini_io(struct efx_nic *efx)
1167{
Ben Hutchings62776d02010-06-23 11:30:07 +00001168 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001169
1170 if (efx->membase) {
1171 iounmap(efx->membase);
1172 efx->membase = NULL;
1173 }
1174
1175 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001176 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001177 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001178 }
1179
1180 pci_disable_device(efx->pci_dev);
1181}
1182
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001183static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001184{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001185 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001186 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001187 int cpu;
1188
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001189 if (rss_cpus) {
1190 count = rss_cpus;
1191 } else {
1192 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1193 netif_warn(efx, probe, efx->net_dev,
1194 "RSS disabled due to allocation failure\n");
1195 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001196 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001197
1198 count = 0;
1199 for_each_online_cpu(cpu) {
1200 if (!cpumask_test_cpu(cpu, thread_mask)) {
1201 ++count;
1202 cpumask_or(thread_mask, thread_mask,
1203 topology_thread_cpumask(cpu));
1204 }
1205 }
1206
1207 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001208 }
1209
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001210 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1211 * table entries that are inaccessible to VFs
1212 */
1213 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1214 count > efx_vf_size(efx)) {
1215 netif_warn(efx, probe, efx->net_dev,
1216 "Reducing number of RSS channels from %u to %u for "
1217 "VF support. Increase vf-msix-limit to use more "
1218 "channels on the PF.\n",
1219 count, efx_vf_size(efx));
1220 count = efx_vf_size(efx);
1221 }
1222
Ben Hutchings46123d02008-09-01 12:47:33 +01001223 return count;
1224}
1225
1226/* Probe the number and type of interrupts we are able to obtain, and
1227 * the resulting numbers of channels and RX queues.
1228 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001229static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001230{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001231 unsigned int max_channels =
1232 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001233 unsigned int extra_channels = 0;
1234 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001235 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001236
Ben Hutchings7f967c02012-02-13 23:45:02 +00001237 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1238 if (efx->extra_channel_type[i])
1239 ++extra_channels;
1240
Ben Hutchings8ceee662008-04-27 12:55:59 +01001241 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001242 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001243 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001244
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001245 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001246 if (separate_tx_channels)
1247 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001248 n_channels += extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001249 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001250
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001251 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001252 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001253 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001254 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001255 netif_err(efx, drv, efx->net_dev,
1256 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001257 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001258 netif_err(efx, drv, efx->net_dev,
1259 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001260 EFX_BUG_ON_PARANOID(rc >= n_channels);
1261 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001262 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001263 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001264 }
1265
1266 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001267 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001268 if (n_channels > extra_channels)
1269 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001270 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001271 efx->n_tx_channels = max(n_channels / 2, 1U);
1272 efx->n_rx_channels = max(n_channels -
1273 efx->n_tx_channels,
1274 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001275 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001276 efx->n_tx_channels = n_channels;
1277 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001278 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001279 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001280 efx_get_channel(efx, i)->irq =
1281 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001282 } else {
1283 /* Fall back to single channel MSI */
1284 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001285 netif_err(efx, drv, efx->net_dev,
1286 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001287 }
1288 }
1289
1290 /* Try single interrupt MSI */
1291 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001292 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001293 efx->n_rx_channels = 1;
1294 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001295 rc = pci_enable_msi(efx->pci_dev);
1296 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001297 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001298 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001299 netif_err(efx, drv, efx->net_dev,
1300 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001301 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1302 }
1303 }
1304
1305 /* Assume legacy interrupts */
1306 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001307 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001308 efx->n_rx_channels = 1;
1309 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001310 efx->legacy_irq = efx->pci_dev->irq;
1311 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001312
Ben Hutchings7f967c02012-02-13 23:45:02 +00001313 /* Assign extra channels if possible */
1314 j = efx->n_channels;
1315 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1316 if (!efx->extra_channel_type[i])
1317 continue;
1318 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1319 efx->n_channels <= extra_channels) {
1320 efx->extra_channel_type[i]->handle_no_channel(efx);
1321 } else {
1322 --j;
1323 efx_get_channel(efx, j)->type =
1324 efx->extra_channel_type[i];
1325 }
1326 }
1327
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001328 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001329 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001330 efx->n_rx_channels : efx_vf_size(efx));
1331
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001332 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001333}
1334
Ben Hutchingsd8291182012-10-05 23:35:41 +01001335static void efx_soft_enable_interrupts(struct efx_nic *efx)
1336{
1337 struct efx_channel *channel;
1338
1339 BUG_ON(efx->state == STATE_DISABLED);
1340
1341 efx->irq_soft_enabled = true;
1342 smp_wmb();
1343
1344 efx_for_each_channel(channel, efx) {
1345 if (!channel->type->keep_eventq)
1346 efx_init_eventq(channel);
1347 efx_start_eventq(channel);
1348 }
1349
1350 efx_mcdi_mode_event(efx);
1351}
1352
1353static void efx_soft_disable_interrupts(struct efx_nic *efx)
1354{
1355 struct efx_channel *channel;
1356
1357 if (efx->state == STATE_DISABLED)
1358 return;
1359
1360 efx_mcdi_mode_poll(efx);
1361
1362 efx->irq_soft_enabled = false;
1363 smp_wmb();
1364
1365 if (efx->legacy_irq)
1366 synchronize_irq(efx->legacy_irq);
1367
1368 efx_for_each_channel(channel, efx) {
1369 if (channel->irq)
1370 synchronize_irq(channel->irq);
1371
1372 efx_stop_eventq(channel);
1373 if (!channel->type->keep_eventq)
1374 efx_fini_eventq(channel);
1375 }
1376}
1377
1378static void efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001379{
1380 struct efx_channel *channel;
1381
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001382 BUG_ON(efx->state == STATE_DISABLED);
1383
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001384 if (efx->eeh_disabled_legacy_irq) {
1385 enable_irq(efx->legacy_irq);
1386 efx->eeh_disabled_legacy_irq = false;
1387 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001388
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001389 efx_nic_enable_interrupts(efx);
1390
1391 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001392 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001393 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001394 }
1395
Ben Hutchingsd8291182012-10-05 23:35:41 +01001396 efx_soft_enable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001397}
1398
Ben Hutchingsd8291182012-10-05 23:35:41 +01001399static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001400{
1401 struct efx_channel *channel;
1402
Ben Hutchingsd8291182012-10-05 23:35:41 +01001403 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001404
1405 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001406 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001407 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001408 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001409
1410 efx_nic_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001411}
1412
Ben Hutchings8ceee662008-04-27 12:55:59 +01001413static void efx_remove_interrupts(struct efx_nic *efx)
1414{
1415 struct efx_channel *channel;
1416
1417 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001418 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001419 channel->irq = 0;
1420 pci_disable_msi(efx->pci_dev);
1421 pci_disable_msix(efx->pci_dev);
1422
1423 /* Remove legacy interrupt */
1424 efx->legacy_irq = 0;
1425}
1426
Ben Hutchings8831da72008-09-01 12:47:48 +01001427static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001428{
Ben Hutchings602a5322011-05-16 17:32:39 +01001429 struct efx_channel *channel;
1430 struct efx_tx_queue *tx_queue;
1431
Ben Hutchings97653432011-01-12 18:26:56 +00001432 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001433 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001434
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001435 /* We need to mark which channels really have RX and TX
1436 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001437 * RX-only and TX-only channels.
1438 */
1439 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001440 if (channel->channel < efx->n_rx_channels)
1441 channel->rx_queue.core_index = channel->channel;
1442 else
1443 channel->rx_queue.core_index = -1;
1444
Ben Hutchings602a5322011-05-16 17:32:39 +01001445 efx_for_each_channel_tx_queue(tx_queue, channel)
1446 tx_queue->queue -= (efx->tx_channel_offset *
1447 EFX_TXQ_TYPES);
1448 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001449}
1450
1451static int efx_probe_nic(struct efx_nic *efx)
1452{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001453 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001454 int rc;
1455
Ben Hutchings62776d02010-06-23 11:30:07 +00001456 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001457
1458 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001459 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001460 if (rc)
1461 return rc;
1462
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001463 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001464 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001465 rc = efx_probe_interrupts(efx);
1466 if (rc)
1467 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001468
Ben Hutchings28e47c42012-02-15 01:58:49 +00001469 efx->type->dimension_resources(efx);
1470
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001471 if (efx->n_channels > 1)
1472 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001473 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001474 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001475 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001476
Ben Hutchings8831da72008-09-01 12:47:48 +01001477 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001478 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1479 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001480
1481 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001482 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1483 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001484
1485 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001486
1487fail:
1488 efx->type->remove(efx);
1489 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001490}
1491
1492static void efx_remove_nic(struct efx_nic *efx)
1493{
Ben Hutchings62776d02010-06-23 11:30:07 +00001494 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001495
1496 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001497 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001498}
1499
1500/**************************************************************************
1501 *
1502 * NIC startup/shutdown
1503 *
1504 *************************************************************************/
1505
1506static int efx_probe_all(struct efx_nic *efx)
1507{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001508 int rc;
1509
Ben Hutchings8ceee662008-04-27 12:55:59 +01001510 rc = efx_probe_nic(efx);
1511 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001512 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001513 goto fail1;
1514 }
1515
Ben Hutchings8ceee662008-04-27 12:55:59 +01001516 rc = efx_probe_port(efx);
1517 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001518 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001519 goto fail2;
1520 }
1521
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001522 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1523 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1524 rc = -EINVAL;
1525 goto fail3;
1526 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001527 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001528
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001529 rc = efx_probe_filters(efx);
1530 if (rc) {
1531 netif_err(efx, probe, efx->net_dev,
1532 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001533 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001534 }
1535
Ben Hutchings7f967c02012-02-13 23:45:02 +00001536 rc = efx_probe_channels(efx);
1537 if (rc)
1538 goto fail4;
1539
Ben Hutchings8ceee662008-04-27 12:55:59 +01001540 return 0;
1541
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001542 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001543 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001544 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001545 efx_remove_port(efx);
1546 fail2:
1547 efx_remove_nic(efx);
1548 fail1:
1549 return rc;
1550}
1551
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001552/* If the interface is supposed to be running but is not, start
1553 * the hardware and software data path, regular activity for the port
1554 * (MAC statistics, link polling, etc.) and schedule the port to be
1555 * reconfigured. Interrupts must already be enabled. This function
1556 * is safe to call multiple times, so long as the NIC is not disabled.
1557 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001558 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001559static void efx_start_all(struct efx_nic *efx)
1560{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001561 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001562 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001563
1564 /* Check that it is appropriate to restart the interface. All
1565 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001566 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001567 return;
1568
Ben Hutchings8ceee662008-04-27 12:55:59 +01001569 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001570 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001571
Alexandre Rames626950d2013-01-14 17:20:22 +00001572 /* Start the hardware monitor if there is one */
1573 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001574 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1575 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001576
1577 /* If link state detection is normally event-driven, we have
1578 * to poll now because we could have missed a change
1579 */
1580 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001581 mutex_lock(&efx->mac_lock);
1582 if (efx->phy_op->poll(efx))
1583 efx_link_status_changed(efx);
1584 mutex_unlock(&efx->mac_lock);
1585 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001586
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001587 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001588}
1589
1590/* Flush all delayed work. Should only be called when no more delayed work
1591 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1592 * since we're holding the rtnl_lock at this point. */
1593static void efx_flush_all(struct efx_nic *efx)
1594{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001595 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001596 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001597 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001598 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001599 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001600}
1601
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001602/* Quiesce the hardware and software data path, and regular activity
1603 * for the port without bringing the link down. Safe to call multiple
1604 * times with the NIC in almost any state, but interrupts should be
1605 * enabled. Requires the RTNL lock.
1606 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001607static void efx_stop_all(struct efx_nic *efx)
1608{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001609 EFX_ASSERT_RESET_SERIALISED(efx);
1610
1611 /* port_enabled can be read safely under the rtnl lock */
1612 if (!efx->port_enabled)
1613 return;
1614
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001615 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001616 efx_stop_port(efx);
1617
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001618 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001619 efx_flush_all(efx);
1620
Ben Hutchings29c69a42013-01-28 19:01:06 +00001621 /* Stop the kernel transmit interface. This is only valid if
1622 * the device is stopped or detached; otherwise the watchdog
1623 * may fire immediately.
1624 */
1625 WARN_ON(netif_running(efx->net_dev) &&
1626 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001627 netif_tx_disable(efx->net_dev);
1628
1629 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001630}
1631
1632static void efx_remove_all(struct efx_nic *efx)
1633{
Ben Hutchings46426102010-09-10 06:42:33 +00001634 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001635 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001636 efx_remove_port(efx);
1637 efx_remove_nic(efx);
1638}
1639
Ben Hutchings8ceee662008-04-27 12:55:59 +01001640/**************************************************************************
1641 *
1642 * Interrupt moderation
1643 *
1644 **************************************************************************/
1645
Ben Hutchingscc180b62011-12-08 19:51:47 +00001646static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001647{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001648 if (usecs == 0)
1649 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001650 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001651 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001652 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001653}
1654
Ben Hutchings8ceee662008-04-27 12:55:59 +01001655/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001656int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1657 unsigned int rx_usecs, bool rx_adaptive,
1658 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001659{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001660 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001661 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1662 efx->timer_quantum_ns,
1663 1000);
1664 unsigned int tx_ticks;
1665 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001666
1667 EFX_ASSERT_RESET_SERIALISED(efx);
1668
Ben Hutchingscc180b62011-12-08 19:51:47 +00001669 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001670 return -EINVAL;
1671
Ben Hutchingscc180b62011-12-08 19:51:47 +00001672 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1673 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1674
Ben Hutchings9e393b32011-09-05 07:43:04 +00001675 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1676 !rx_may_override_tx) {
1677 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1678 "RX and TX IRQ moderation must be equal\n");
1679 return -EINVAL;
1680 }
1681
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001682 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001683 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001684 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001685 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001686 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001687 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001688 channel->irq_moderation = tx_ticks;
1689 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001690
1691 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001692}
1693
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001694void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1695 unsigned int *rx_usecs, bool *rx_adaptive)
1696{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001697 /* We must round up when converting ticks to microseconds
1698 * because we round down when converting the other way.
1699 */
1700
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001701 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001702 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1703 efx->timer_quantum_ns,
1704 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001705
1706 /* If channels are shared between RX and TX, so is IRQ
1707 * moderation. Otherwise, IRQ moderation is the same for all
1708 * TX channels and is not adaptive.
1709 */
1710 if (efx->tx_channel_offset == 0)
1711 *tx_usecs = *rx_usecs;
1712 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001713 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001714 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001715 efx->timer_quantum_ns,
1716 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001717}
1718
Ben Hutchings8ceee662008-04-27 12:55:59 +01001719/**************************************************************************
1720 *
1721 * Hardware monitor
1722 *
1723 **************************************************************************/
1724
Ben Hutchingse254c272010-09-20 08:44:10 +00001725/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001726static void efx_monitor(struct work_struct *data)
1727{
1728 struct efx_nic *efx = container_of(data, struct efx_nic,
1729 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001730
Ben Hutchings62776d02010-06-23 11:30:07 +00001731 netif_vdbg(efx, timer, efx->net_dev,
1732 "hardware monitor executing on CPU %d\n",
1733 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001734 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001735
Ben Hutchings8ceee662008-04-27 12:55:59 +01001736 /* If the mac_lock is already held then it is likely a port
1737 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001738 * most of the work of monitor() anyway. */
1739 if (mutex_trylock(&efx->mac_lock)) {
1740 if (efx->port_enabled)
1741 efx->type->monitor(efx);
1742 mutex_unlock(&efx->mac_lock);
1743 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001744
Ben Hutchings8ceee662008-04-27 12:55:59 +01001745 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1746 efx_monitor_interval);
1747}
1748
1749/**************************************************************************
1750 *
1751 * ioctls
1752 *
1753 *************************************************************************/
1754
1755/* Net device ioctl
1756 * Context: process, rtnl_lock() held.
1757 */
1758static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1759{
Ben Hutchings767e4682008-09-01 12:43:14 +01001760 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001761 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001762
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001763 if (cmd == SIOCSHWTSTAMP)
1764 return efx_ptp_ioctl(efx, ifr, cmd);
1765
Ben Hutchings68e7f452009-04-29 08:05:08 +00001766 /* Convert phy_id from older PRTAD/DEVAD format */
1767 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1768 (data->phy_id & 0xfc00) == 0x0400)
1769 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1770
1771 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001772}
1773
1774/**************************************************************************
1775 *
1776 * NAPI interface
1777 *
1778 **************************************************************************/
1779
Ben Hutchings7f967c02012-02-13 23:45:02 +00001780static void efx_init_napi_channel(struct efx_channel *channel)
1781{
1782 struct efx_nic *efx = channel->efx;
1783
1784 channel->napi_dev = efx->net_dev;
1785 netif_napi_add(channel->napi_dev, &channel->napi_str,
1786 efx_poll, napi_weight);
1787}
1788
Ben Hutchingse8f14992010-12-07 19:47:34 +00001789static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001790{
1791 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001792
Ben Hutchings7f967c02012-02-13 23:45:02 +00001793 efx_for_each_channel(channel, efx)
1794 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001795}
1796
1797static void efx_fini_napi_channel(struct efx_channel *channel)
1798{
1799 if (channel->napi_dev)
1800 netif_napi_del(&channel->napi_str);
1801 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001802}
1803
1804static void efx_fini_napi(struct efx_nic *efx)
1805{
1806 struct efx_channel *channel;
1807
Ben Hutchingse8f14992010-12-07 19:47:34 +00001808 efx_for_each_channel(channel, efx)
1809 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001810}
1811
1812/**************************************************************************
1813 *
1814 * Kernel netpoll interface
1815 *
1816 *************************************************************************/
1817
1818#ifdef CONFIG_NET_POLL_CONTROLLER
1819
1820/* Although in the common case interrupts will be disabled, this is not
1821 * guaranteed. However, all our work happens inside the NAPI callback,
1822 * so no locking is required.
1823 */
1824static void efx_netpoll(struct net_device *net_dev)
1825{
Ben Hutchings767e4682008-09-01 12:43:14 +01001826 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001827 struct efx_channel *channel;
1828
Ben Hutchings64ee3122008-09-01 12:47:38 +01001829 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001830 efx_schedule_channel(channel);
1831}
1832
1833#endif
1834
1835/**************************************************************************
1836 *
1837 * Kernel net device interface
1838 *
1839 *************************************************************************/
1840
1841/* Context: process, rtnl_lock() held. */
1842static int efx_net_open(struct net_device *net_dev)
1843{
Ben Hutchings767e4682008-09-01 12:43:14 +01001844 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001845 int rc;
1846
Ben Hutchings62776d02010-06-23 11:30:07 +00001847 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1848 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001849
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001850 rc = efx_check_disabled(efx);
1851 if (rc)
1852 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001853 if (efx->phy_mode & PHY_MODE_SPECIAL)
1854 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001855 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1856 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001857
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001858 /* Notify the kernel of the link state polled during driver load,
1859 * before the monitor starts running */
1860 efx_link_status_changed(efx);
1861
Ben Hutchings8ceee662008-04-27 12:55:59 +01001862 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001863 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001864 return 0;
1865}
1866
1867/* Context: process, rtnl_lock() held.
1868 * Note that the kernel will ignore our return code; this method
1869 * should really be a void.
1870 */
1871static int efx_net_stop(struct net_device *net_dev)
1872{
Ben Hutchings767e4682008-09-01 12:43:14 +01001873 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001874
Ben Hutchings62776d02010-06-23 11:30:07 +00001875 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1876 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001877
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001878 /* Stop the device and flush all the channels */
1879 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001880
1881 return 0;
1882}
1883
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001884/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001885static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1886 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001887{
Ben Hutchings767e4682008-09-01 12:43:14 +01001888 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001889 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001890
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001891 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001892
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001893 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001894
1895 stats->rx_packets = mac_stats->rx_packets;
1896 stats->tx_packets = mac_stats->tx_packets;
1897 stats->rx_bytes = mac_stats->rx_bytes;
1898 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001899 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001900 stats->multicast = mac_stats->rx_multicast;
1901 stats->collisions = mac_stats->tx_collision;
1902 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1903 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001904 stats->rx_crc_errors = mac_stats->rx_bad;
1905 stats->rx_frame_errors = mac_stats->rx_align_error;
1906 stats->rx_fifo_errors = mac_stats->rx_overflow;
1907 stats->rx_missed_errors = mac_stats->rx_missed;
1908 stats->tx_window_errors = mac_stats->tx_late_collision;
1909
1910 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001911 stats->rx_crc_errors +
1912 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001913 mac_stats->rx_symbol_error);
1914 stats->tx_errors = (stats->tx_window_errors +
1915 mac_stats->tx_bad);
1916
Ben Hutchings1cb34522011-09-02 23:23:00 +01001917 spin_unlock_bh(&efx->stats_lock);
1918
Ben Hutchings8ceee662008-04-27 12:55:59 +01001919 return stats;
1920}
1921
1922/* Context: netif_tx_lock held, BHs disabled. */
1923static void efx_watchdog(struct net_device *net_dev)
1924{
Ben Hutchings767e4682008-09-01 12:43:14 +01001925 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001926
Ben Hutchings62776d02010-06-23 11:30:07 +00001927 netif_err(efx, tx_err, efx->net_dev,
1928 "TX stuck with port_enabled=%d: resetting channels\n",
1929 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001930
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001931 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001932}
1933
1934
1935/* Context: process, rtnl_lock() held. */
1936static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1937{
Ben Hutchings767e4682008-09-01 12:43:14 +01001938 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001939 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001940
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001941 rc = efx_check_disabled(efx);
1942 if (rc)
1943 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001944 if (new_mtu > EFX_MAX_MTU)
1945 return -EINVAL;
1946
Ben Hutchings62776d02010-06-23 11:30:07 +00001947 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001948
Ben Hutchings29c69a42013-01-28 19:01:06 +00001949 efx_device_detach_sync(efx);
1950 efx_stop_all(efx);
1951
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001952 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001953 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001954 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001955 mutex_unlock(&efx->mac_lock);
1956
Ben Hutchings8ceee662008-04-27 12:55:59 +01001957 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00001958 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001959 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001960}
1961
1962static int efx_set_mac_address(struct net_device *net_dev, void *data)
1963{
Ben Hutchings767e4682008-09-01 12:43:14 +01001964 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001965 struct sockaddr *addr = data;
1966 char *new_addr = addr->sa_data;
1967
Ben Hutchings8ceee662008-04-27 12:55:59 +01001968 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001969 netif_err(efx, drv, efx->net_dev,
1970 "invalid ethernet MAC address requested: %pM\n",
1971 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00001972 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001973 }
1974
1975 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001976 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001977
1978 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001979 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01001980 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001981 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001982
1983 return 0;
1984}
1985
Ben Hutchingsa816f752008-09-01 12:49:12 +01001986/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00001987static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001988{
Ben Hutchings767e4682008-09-01 12:43:14 +01001989 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001990 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001991 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001992 u32 crc;
1993 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001994
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001995 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001996
1997 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001998 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001999 memset(mc_hash, 0xff, sizeof(*mc_hash));
2000 } else {
2001 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00002002 netdev_for_each_mc_addr(ha, net_dev) {
2003 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002004 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
Ben Hutchings32766ec2012-10-04 17:13:03 -07002005 __set_bit_le(bit, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002006 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002007
2008 /* Broadcast packets go through the multicast hash filter.
2009 * ether_crc_le() of the broadcast address is 0xbe2612ff
2010 * so we always add bit 0xff to the mask.
2011 */
Ben Hutchings32766ec2012-10-04 17:13:03 -07002012 __set_bit_le(0xff, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002013 }
2014
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002015 if (efx->port_enabled)
2016 queue_work(efx->workqueue, &efx->mac_work);
2017 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002018}
2019
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002020static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002021{
2022 struct efx_nic *efx = netdev_priv(net_dev);
2023
2024 /* If disabling RX n-tuple filtering, clear existing filters */
2025 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2026 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2027
2028 return 0;
2029}
2030
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002031static const struct net_device_ops efx_netdev_ops = {
2032 .ndo_open = efx_net_open,
2033 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002034 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002035 .ndo_tx_timeout = efx_watchdog,
2036 .ndo_start_xmit = efx_hard_start_xmit,
2037 .ndo_validate_addr = eth_validate_addr,
2038 .ndo_do_ioctl = efx_ioctl,
2039 .ndo_change_mtu = efx_change_mtu,
2040 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002041 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002042 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002043#ifdef CONFIG_SFC_SRIOV
2044 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2045 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2046 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2047 .ndo_get_vf_config = efx_sriov_get_vf_config,
2048#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002049#ifdef CONFIG_NET_POLL_CONTROLLER
2050 .ndo_poll_controller = efx_netpoll,
2051#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002052 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002053#ifdef CONFIG_RFS_ACCEL
2054 .ndo_rx_flow_steer = efx_filter_rfs,
2055#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002056};
2057
Ben Hutchings7dde5962008-12-12 22:09:38 -08002058static void efx_update_name(struct efx_nic *efx)
2059{
2060 strcpy(efx->name, efx->net_dev->name);
2061 efx_mtd_rename(efx);
2062 efx_set_channel_names(efx);
2063}
2064
Ben Hutchings8ceee662008-04-27 12:55:59 +01002065static int efx_netdev_event(struct notifier_block *this,
2066 unsigned long event, void *ptr)
2067{
Jiri Pirko351638e2013-05-28 01:30:21 +00002068 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002069
Ben Hutchings7dde5962008-12-12 22:09:38 -08002070 if (net_dev->netdev_ops == &efx_netdev_ops &&
2071 event == NETDEV_CHANGENAME)
2072 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002073
2074 return NOTIFY_DONE;
2075}
2076
2077static struct notifier_block efx_netdev_notifier = {
2078 .notifier_call = efx_netdev_event,
2079};
2080
Ben Hutchings06d5e192008-12-12 21:47:23 -08002081static ssize_t
2082show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2083{
2084 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2085 return sprintf(buf, "%d\n", efx->phy_type);
2086}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002087static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002088
Ben Hutchings8ceee662008-04-27 12:55:59 +01002089static int efx_register_netdev(struct efx_nic *efx)
2090{
2091 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002092 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002093 int rc;
2094
2095 net_dev->watchdog_timeo = 5 * HZ;
2096 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002097 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002098 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002099 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002100
Ben Hutchings7dde5962008-12-12 22:09:38 -08002101 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002102
Ben Hutchings7153f622012-07-27 20:50:52 +01002103 /* Enable resets to be scheduled and check whether any were
2104 * already requested. If so, the NIC is probably hosed so we
2105 * abort.
2106 */
2107 efx->state = STATE_READY;
2108 smp_mb(); /* ensure we change state before checking reset_pending */
2109 if (efx->reset_pending) {
2110 netif_err(efx, probe, efx->net_dev,
2111 "aborting probe due to scheduled reset\n");
2112 rc = -EIO;
2113 goto fail_locked;
2114 }
2115
Ben Hutchingsaed06282009-08-26 08:16:27 +00002116 rc = dev_alloc_name(net_dev, net_dev->name);
2117 if (rc < 0)
2118 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002119 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002120
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002121 /* Always start with carrier off; PHY events will detect the link */
2122 netif_carrier_off(net_dev);
2123
Ben Hutchingsaed06282009-08-26 08:16:27 +00002124 rc = register_netdevice(net_dev);
2125 if (rc)
2126 goto fail_locked;
2127
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002128 efx_for_each_channel(channel, efx) {
2129 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002130 efx_for_each_channel_tx_queue(tx_queue, channel)
2131 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002132 }
2133
Ben Hutchings7dde5962008-12-12 22:09:38 -08002134 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002135
Ben Hutchings06d5e192008-12-12 21:47:23 -08002136 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2137 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002138 netif_err(efx, drv, efx->net_dev,
2139 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002140 goto fail_registered;
2141 }
2142
Ben Hutchings8ceee662008-04-27 12:55:59 +01002143 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002144
Ben Hutchings7153f622012-07-27 20:50:52 +01002145fail_registered:
2146 rtnl_lock();
2147 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002148fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002149 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002150 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002151 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002152 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002153}
2154
2155static void efx_unregister_netdev(struct efx_nic *efx)
2156{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002157 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
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002162 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2163 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002164
2165 rtnl_lock();
2166 unregister_netdevice(efx->net_dev);
2167 efx->state = STATE_UNINIT;
2168 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002169}
2170
2171/**************************************************************************
2172 *
2173 * Device reset and suspend
2174 *
2175 **************************************************************************/
2176
Ben Hutchings2467ca42008-09-01 12:48:50 +01002177/* Tears down the entire software state and most of the hardware state
2178 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002179void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002180{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002181 EFX_ASSERT_RESET_SERIALISED(efx);
2182
Ben Hutchings2467ca42008-09-01 12:48:50 +01002183 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002184 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002185
2186 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002187 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2188 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002189 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002190}
2191
Ben Hutchings2467ca42008-09-01 12:48:50 +01002192/* This function will always ensure that the locks acquired in
2193 * efx_reset_down() are released. A failure return code indicates
2194 * that we were unable to reinitialise the hardware, and the
2195 * driver should be disabled. If ok is false, then the rx and tx
2196 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002197int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002198{
2199 int rc;
2200
Ben Hutchings2467ca42008-09-01 12:48:50 +01002201 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002202
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002203 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002204 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002205 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002206 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002207 }
2208
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002209 if (!ok)
2210 goto fail;
2211
Steve Hodgson4b988282009-01-29 17:50:51 +00002212 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002213 rc = efx->phy_op->init(efx);
2214 if (rc)
2215 goto fail;
2216 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002217 netif_err(efx, drv, efx->net_dev,
2218 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002219 }
2220
Ben Hutchings710b2082011-09-03 00:15:00 +01002221 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002222
Ben Hutchingsd8291182012-10-05 23:35:41 +01002223 efx_enable_interrupts(efx);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002224 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002225 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002226
2227 mutex_unlock(&efx->mac_lock);
2228
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002229 efx_start_all(efx);
2230
2231 return 0;
2232
2233fail:
2234 efx->port_initialized = false;
2235
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002236 mutex_unlock(&efx->mac_lock);
2237
Ben Hutchings8ceee662008-04-27 12:55:59 +01002238 return rc;
2239}
2240
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002241/* Reset the NIC using the specified method. Note that the reset may
2242 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002243 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002244 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002245 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002246int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002247{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002248 int rc, rc2;
2249 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002250
Ben Hutchings62776d02010-06-23 11:30:07 +00002251 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2252 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002253
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002254 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002255 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002256
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002257 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002258 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002259 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002260 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002261 }
2262
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002263 /* Clear flags for the scopes we covered. We assume the NIC and
2264 * driver are now quiescent so that there is no race here.
2265 */
2266 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002267
2268 /* Reinitialise bus-mastering, which may have been turned off before
2269 * the reset was scheduled. This is still appropriate, even in the
2270 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2271 * can respond to requests. */
2272 pci_set_master(efx->pci_dev);
2273
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002274out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002275 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002276 disabled = rc ||
2277 method == RESET_TYPE_DISABLE ||
2278 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002279 rc2 = efx_reset_up(efx, method, !disabled);
2280 if (rc2) {
2281 disabled = true;
2282 if (!rc)
2283 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002284 }
2285
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002286 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002287 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002288 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002289 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002290 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002291 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002292 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002293 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002294 return rc;
2295}
2296
Alexandre Rames626950d2013-01-14 17:20:22 +00002297/* Try recovery mechanisms.
2298 * For now only EEH is supported.
2299 * Returns 0 if the recovery mechanisms are unsuccessful.
2300 * Returns a non-zero value otherwise.
2301 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002302int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002303{
2304#ifdef CONFIG_EEH
2305 /* A PCI error can occur and not be seen by EEH because nothing
2306 * happens on the PCI bus. In this case the driver may fail and
2307 * schedule a 'recover or reset', leading to this recovery handler.
2308 * Manually call the eeh failure check function.
2309 */
2310 struct eeh_dev *eehdev =
2311 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2312
2313 if (eeh_dev_check_failure(eehdev)) {
2314 /* The EEH mechanisms will handle the error and reset the
2315 * device if necessary.
2316 */
2317 return 1;
2318 }
2319#endif
2320 return 0;
2321}
2322
Ben Hutchings8ceee662008-04-27 12:55:59 +01002323/* The worker thread exists so that code that cannot sleep can
2324 * schedule a reset for later.
2325 */
2326static void efx_reset_work(struct work_struct *data)
2327{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002328 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002329 unsigned long pending;
2330 enum reset_type method;
2331
2332 pending = ACCESS_ONCE(efx->reset_pending);
2333 method = fls(pending) - 1;
2334
2335 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2336 method == RESET_TYPE_RECOVER_OR_ALL) &&
2337 efx_try_recovery(efx))
2338 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002339
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002340 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002341 return;
2342
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002343 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002344
2345 /* We checked the state in efx_schedule_reset() but it may
2346 * have changed by now. Now that we have the RTNL lock,
2347 * it cannot change again.
2348 */
2349 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002350 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002351
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002352 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002353}
2354
2355void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2356{
2357 enum reset_type method;
2358
Alexandre Rames626950d2013-01-14 17:20:22 +00002359 if (efx->state == STATE_RECOVERY) {
2360 netif_dbg(efx, drv, efx->net_dev,
2361 "recovering: skip scheduling %s reset\n",
2362 RESET_TYPE(type));
2363 return;
2364 }
2365
Ben Hutchings8ceee662008-04-27 12:55:59 +01002366 switch (type) {
2367 case RESET_TYPE_INVISIBLE:
2368 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002369 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002370 case RESET_TYPE_WORLD:
2371 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002372 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002373 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002374 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2375 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002376 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002377 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002378 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002379 netif_dbg(efx, drv, efx->net_dev,
2380 "scheduling %s reset for %s\n",
2381 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002382 break;
2383 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002384
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002385 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002386 smp_mb(); /* ensure we change reset_pending before checking state */
2387
2388 /* If we're not READY then just leave the flags set as the cue
2389 * to abort probing or reschedule the reset later.
2390 */
2391 if (ACCESS_ONCE(efx->state) != STATE_READY)
2392 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002393
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002394 /* efx_process_channel() will no longer read events once a
2395 * reset is scheduled. So switch back to poll'd MCDI completions. */
2396 efx_mcdi_mode_poll(efx);
2397
Steve Hodgson1ab00622008-12-12 21:33:02 -08002398 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002399}
2400
2401/**************************************************************************
2402 *
2403 * List of NICs we support
2404 *
2405 **************************************************************************/
2406
2407/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002408static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002409 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2410 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002411 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002412 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2413 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002414 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002415 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002416 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002417 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002418 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002419 {0} /* end of list */
2420};
2421
2422/**************************************************************************
2423 *
Ben Hutchings37594332009-11-23 16:05:45 +00002424 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002425 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002426 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002427 * Needed so all function pointers are valid and do not have to be tested
2428 * before use
2429 *
2430 **************************************************************************/
2431int efx_port_dummy_op_int(struct efx_nic *efx)
2432{
2433 return 0;
2434}
2435void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002436
2437static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002438{
2439 return false;
2440}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002441
stephen hemminger6c8c2512011-04-14 05:50:12 +00002442static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002443 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002444 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002445 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002446 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002447};
2448
Ben Hutchings8ceee662008-04-27 12:55:59 +01002449/**************************************************************************
2450 *
2451 * Data housekeeping
2452 *
2453 **************************************************************************/
2454
2455/* This zeroes out and then fills in the invariants in a struct
2456 * efx_nic (including all sub-structures).
2457 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002458static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002459 struct pci_dev *pci_dev, struct net_device *net_dev)
2460{
Ben Hutchings46426102010-09-10 06:42:33 +00002461 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002462
2463 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002464 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002465#ifdef CONFIG_SFC_MTD
2466 INIT_LIST_HEAD(&efx->mtd_list);
2467#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002468 INIT_WORK(&efx->reset_work, efx_reset_work);
2469 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002470 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002471 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002472 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002473 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002474 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002475
2476 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002477 spin_lock_init(&efx->stats_lock);
2478 mutex_init(&efx->mac_lock);
2479 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002480 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002481 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002482 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002483
2484 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002485 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2486 if (!efx->channel[i])
2487 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002488 efx->msi_context[i].efx = efx;
2489 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002490 }
2491
Ben Hutchings8ceee662008-04-27 12:55:59 +01002492 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2493
2494 /* Higher numbered interrupt modes are less capable! */
2495 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2496 interrupt_mode);
2497
Ben Hutchings6977dc62008-12-26 13:44:39 -08002498 /* Would be good to use the net_dev name, but we're too early */
2499 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2500 pci_name(pci_dev));
2501 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002502 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002503 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002504
Ben Hutchings8ceee662008-04-27 12:55:59 +01002505 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002506
2507fail:
2508 efx_fini_struct(efx);
2509 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002510}
2511
2512static void efx_fini_struct(struct efx_nic *efx)
2513{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002514 int i;
2515
2516 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2517 kfree(efx->channel[i]);
2518
Ben Hutchings8ceee662008-04-27 12:55:59 +01002519 if (efx->workqueue) {
2520 destroy_workqueue(efx->workqueue);
2521 efx->workqueue = NULL;
2522 }
2523}
2524
2525/**************************************************************************
2526 *
2527 * PCI interface
2528 *
2529 **************************************************************************/
2530
2531/* Main body of final NIC shutdown code
2532 * This is called only at module unload (or hotplug removal).
2533 */
2534static void efx_pci_remove_main(struct efx_nic *efx)
2535{
Ben Hutchings7153f622012-07-27 20:50:52 +01002536 /* Flush reset_work. It can no longer be scheduled since we
2537 * are not READY.
2538 */
2539 BUG_ON(efx->state == STATE_READY);
2540 cancel_work_sync(&efx->reset_work);
2541
Ben Hutchingsd8291182012-10-05 23:35:41 +01002542 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002543 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002544 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002545 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002546 efx_fini_napi(efx);
2547 efx_remove_all(efx);
2548}
2549
2550/* Final NIC shutdown
2551 * This is called only at module unload (or hotplug removal).
2552 */
2553static void efx_pci_remove(struct pci_dev *pci_dev)
2554{
2555 struct efx_nic *efx;
2556
2557 efx = pci_get_drvdata(pci_dev);
2558 if (!efx)
2559 return;
2560
2561 /* Mark the NIC as fini, then stop the interface */
2562 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002563 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002564 efx_disable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002565 rtnl_unlock();
2566
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002567 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002568 efx_unregister_netdev(efx);
2569
Ben Hutchings7dde5962008-12-12 22:09:38 -08002570 efx_mtd_remove(efx);
2571
Ben Hutchings8ceee662008-04-27 12:55:59 +01002572 efx_pci_remove_main(efx);
2573
Ben Hutchings8ceee662008-04-27 12:55:59 +01002574 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002575 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002576
Ben Hutchings8ceee662008-04-27 12:55:59 +01002577 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002578 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002579 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002580
2581 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002582};
2583
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002584/* NIC VPD information
2585 * Called during probe to display the part number of the
2586 * installed NIC. VPD is potentially very large but this should
2587 * always appear within the first 512 bytes.
2588 */
2589#define SFC_VPD_LEN 512
2590static void efx_print_product_vpd(struct efx_nic *efx)
2591{
2592 struct pci_dev *dev = efx->pci_dev;
2593 char vpd_data[SFC_VPD_LEN];
2594 ssize_t vpd_size;
2595 int i, j;
2596
2597 /* Get the vpd data from the device */
2598 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2599 if (vpd_size <= 0) {
2600 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2601 return;
2602 }
2603
2604 /* Get the Read only section */
2605 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2606 if (i < 0) {
2607 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2608 return;
2609 }
2610
2611 j = pci_vpd_lrdt_size(&vpd_data[i]);
2612 i += PCI_VPD_LRDT_TAG_SIZE;
2613 if (i + j > vpd_size)
2614 j = vpd_size - i;
2615
2616 /* Get the Part number */
2617 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2618 if (i < 0) {
2619 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2620 return;
2621 }
2622
2623 j = pci_vpd_info_field_size(&vpd_data[i]);
2624 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2625 if (i + j > vpd_size) {
2626 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2627 return;
2628 }
2629
2630 netif_info(efx, drv, efx->net_dev,
2631 "Part Number : %.*s\n", j, &vpd_data[i]);
2632}
2633
2634
Ben Hutchings8ceee662008-04-27 12:55:59 +01002635/* Main body of NIC initialisation
2636 * This is called at module load (or hotplug insertion, theoretically).
2637 */
2638static int efx_pci_probe_main(struct efx_nic *efx)
2639{
2640 int rc;
2641
2642 /* Do start-of-day initialisation */
2643 rc = efx_probe_all(efx);
2644 if (rc)
2645 goto fail1;
2646
Ben Hutchingse8f14992010-12-07 19:47:34 +00002647 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002648
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002649 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002650 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002651 netif_err(efx, probe, efx->net_dev,
2652 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002653 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002654 }
2655
2656 rc = efx_init_port(efx);
2657 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002658 netif_err(efx, probe, efx->net_dev,
2659 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002660 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002661 }
2662
Ben Hutchings152b6a62009-11-29 03:43:56 +00002663 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002664 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002665 goto fail5;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002666 efx_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002667
2668 return 0;
2669
Ben Hutchings278c0622009-11-23 16:05:12 +00002670 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002671 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002672 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002673 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002674 fail3:
2675 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002676 efx_remove_all(efx);
2677 fail1:
2678 return rc;
2679}
2680
2681/* NIC initialisation
2682 *
2683 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002684 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002685 * sets up and registers the network devices with the kernel and hooks
2686 * the interrupt service routine. It does not prepare the device for
2687 * transmission; this is left to the first time one of the network
2688 * interfaces is brought up (i.e. efx_net_open).
2689 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002690static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002691 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002692{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002693 struct net_device *net_dev;
2694 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002695 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002696
2697 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002698 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2699 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002700 if (!net_dev)
2701 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002702 efx = netdev_priv(net_dev);
2703 efx->type = (const struct efx_nic_type *) entry->driver_data;
2704 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002705 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002706 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002707 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002708 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002709 /* Mask for features that also apply to VLAN devices */
2710 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002711 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2712 NETIF_F_RXCSUM);
2713 /* All offloads can be toggled */
2714 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002715 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002716 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002717 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002718 if (rc)
2719 goto fail1;
2720
Ben Hutchings62776d02010-06-23 11:30:07 +00002721 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002722 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002723
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002724 efx_print_product_vpd(efx);
2725
Ben Hutchings8ceee662008-04-27 12:55:59 +01002726 /* Set up basic I/O (BAR mappings etc) */
2727 rc = efx_init_io(efx);
2728 if (rc)
2729 goto fail2;
2730
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002731 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002732 if (rc)
2733 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002734
Ben Hutchings8ceee662008-04-27 12:55:59 +01002735 rc = efx_register_netdev(efx);
2736 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002737 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002738
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002739 rc = efx_sriov_init(efx);
2740 if (rc)
2741 netif_err(efx, probe, efx->net_dev,
2742 "SR-IOV can't be enabled rc %d\n", rc);
2743
Ben Hutchings62776d02010-06-23 11:30:07 +00002744 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002745
Ben Hutchings7c431612012-01-27 17:23:58 +00002746 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002747 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002748 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002749 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002750 if (rc)
2751 netif_warn(efx, probe, efx->net_dev,
2752 "failed to create MTDs (%d)\n", rc);
2753
Alexandre Rames626950d2013-01-14 17:20:22 +00002754 rc = pci_enable_pcie_error_reporting(pci_dev);
2755 if (rc && rc != -EINVAL)
2756 netif_warn(efx, probe, efx->net_dev,
2757 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2758
Ben Hutchings8ceee662008-04-27 12:55:59 +01002759 return 0;
2760
Ben Hutchings8ceee662008-04-27 12:55:59 +01002761 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002762 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002763 fail3:
2764 efx_fini_io(efx);
2765 fail2:
2766 efx_fini_struct(efx);
2767 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002768 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002769 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002770 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002771 free_netdev(net_dev);
2772 return rc;
2773}
2774
Ben Hutchings89c758f2009-11-29 03:43:07 +00002775static int efx_pm_freeze(struct device *dev)
2776{
2777 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2778
Ben Hutchings61da0262012-07-27 19:35:39 +01002779 rtnl_lock();
2780
Ben Hutchings6032fb52012-07-27 19:35:47 +01002781 if (efx->state != STATE_DISABLED) {
2782 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002783
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002784 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002785
Ben Hutchings6032fb52012-07-27 19:35:47 +01002786 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002787 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01002788 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002789
Ben Hutchings61da0262012-07-27 19:35:39 +01002790 rtnl_unlock();
2791
Ben Hutchings89c758f2009-11-29 03:43:07 +00002792 return 0;
2793}
2794
2795static int efx_pm_thaw(struct device *dev)
2796{
2797 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2798
Ben Hutchings61da0262012-07-27 19:35:39 +01002799 rtnl_lock();
2800
Ben Hutchings6032fb52012-07-27 19:35:47 +01002801 if (efx->state != STATE_DISABLED) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01002802 efx_enable_interrupts(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002803
Ben Hutchings6032fb52012-07-27 19:35:47 +01002804 mutex_lock(&efx->mac_lock);
2805 efx->phy_op->reconfigure(efx);
2806 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002807
Ben Hutchings6032fb52012-07-27 19:35:47 +01002808 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002809
Ben Hutchings6032fb52012-07-27 19:35:47 +01002810 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002811
Ben Hutchings6032fb52012-07-27 19:35:47 +01002812 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002813
Ben Hutchings6032fb52012-07-27 19:35:47 +01002814 efx->type->resume_wol(efx);
2815 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002816
Ben Hutchings61da0262012-07-27 19:35:39 +01002817 rtnl_unlock();
2818
Steve Hodgson319ba642010-06-01 11:17:24 +00002819 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2820 queue_work(reset_workqueue, &efx->reset_work);
2821
Ben Hutchings89c758f2009-11-29 03:43:07 +00002822 return 0;
2823}
2824
2825static int efx_pm_poweroff(struct device *dev)
2826{
2827 struct pci_dev *pci_dev = to_pci_dev(dev);
2828 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2829
2830 efx->type->fini(efx);
2831
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002832 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002833
2834 pci_save_state(pci_dev);
2835 return pci_set_power_state(pci_dev, PCI_D3hot);
2836}
2837
2838/* Used for both resume and restore */
2839static int efx_pm_resume(struct device *dev)
2840{
2841 struct pci_dev *pci_dev = to_pci_dev(dev);
2842 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2843 int rc;
2844
2845 rc = pci_set_power_state(pci_dev, PCI_D0);
2846 if (rc)
2847 return rc;
2848 pci_restore_state(pci_dev);
2849 rc = pci_enable_device(pci_dev);
2850 if (rc)
2851 return rc;
2852 pci_set_master(efx->pci_dev);
2853 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2854 if (rc)
2855 return rc;
2856 rc = efx->type->init(efx);
2857 if (rc)
2858 return rc;
2859 efx_pm_thaw(dev);
2860 return 0;
2861}
2862
2863static int efx_pm_suspend(struct device *dev)
2864{
2865 int rc;
2866
2867 efx_pm_freeze(dev);
2868 rc = efx_pm_poweroff(dev);
2869 if (rc)
2870 efx_pm_resume(dev);
2871 return rc;
2872}
2873
Ben Hutchings18e83e42012-01-05 19:05:20 +00002874static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002875 .suspend = efx_pm_suspend,
2876 .resume = efx_pm_resume,
2877 .freeze = efx_pm_freeze,
2878 .thaw = efx_pm_thaw,
2879 .poweroff = efx_pm_poweroff,
2880 .restore = efx_pm_resume,
2881};
2882
Alexandre Rames626950d2013-01-14 17:20:22 +00002883/* A PCI error affecting this device was detected.
2884 * At this point MMIO and DMA may be disabled.
2885 * Stop the software path and request a slot reset.
2886 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002887static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2888 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00002889{
2890 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2891 struct efx_nic *efx = pci_get_drvdata(pdev);
2892
2893 if (state == pci_channel_io_perm_failure)
2894 return PCI_ERS_RESULT_DISCONNECT;
2895
2896 rtnl_lock();
2897
2898 if (efx->state != STATE_DISABLED) {
2899 efx->state = STATE_RECOVERY;
2900 efx->reset_pending = 0;
2901
2902 efx_device_detach_sync(efx);
2903
2904 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002905 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00002906
2907 status = PCI_ERS_RESULT_NEED_RESET;
2908 } else {
2909 /* If the interface is disabled we don't want to do anything
2910 * with it.
2911 */
2912 status = PCI_ERS_RESULT_RECOVERED;
2913 }
2914
2915 rtnl_unlock();
2916
2917 pci_disable_device(pdev);
2918
2919 return status;
2920}
2921
2922/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002923static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00002924{
2925 struct efx_nic *efx = pci_get_drvdata(pdev);
2926 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2927 int rc;
2928
2929 if (pci_enable_device(pdev)) {
2930 netif_err(efx, hw, efx->net_dev,
2931 "Cannot re-enable PCI device after reset.\n");
2932 status = PCI_ERS_RESULT_DISCONNECT;
2933 }
2934
2935 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2936 if (rc) {
2937 netif_err(efx, hw, efx->net_dev,
2938 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2939 /* Non-fatal error. Continue. */
2940 }
2941
2942 return status;
2943}
2944
2945/* Perform the actual reset and resume I/O operations. */
2946static void efx_io_resume(struct pci_dev *pdev)
2947{
2948 struct efx_nic *efx = pci_get_drvdata(pdev);
2949 int rc;
2950
2951 rtnl_lock();
2952
2953 if (efx->state == STATE_DISABLED)
2954 goto out;
2955
2956 rc = efx_reset(efx, RESET_TYPE_ALL);
2957 if (rc) {
2958 netif_err(efx, hw, efx->net_dev,
2959 "efx_reset failed after PCI error (%d)\n", rc);
2960 } else {
2961 efx->state = STATE_READY;
2962 netif_dbg(efx, hw, efx->net_dev,
2963 "Done resetting and resuming IO after PCI error.\n");
2964 }
2965
2966out:
2967 rtnl_unlock();
2968}
2969
2970/* For simplicity and reliability, we always require a slot reset and try to
2971 * reset the hardware when a pci error affecting the device is detected.
2972 * We leave both the link_reset and mmio_enabled callback unimplemented:
2973 * with our request for slot reset the mmio_enabled callback will never be
2974 * called, and the link_reset callback is not used by AER or EEH mechanisms.
2975 */
2976static struct pci_error_handlers efx_err_handlers = {
2977 .error_detected = efx_io_error_detected,
2978 .slot_reset = efx_io_slot_reset,
2979 .resume = efx_io_resume,
2980};
2981
Ben Hutchings8ceee662008-04-27 12:55:59 +01002982static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00002983 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002984 .id_table = efx_pci_table,
2985 .probe = efx_pci_probe,
2986 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00002987 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00002988 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002989};
2990
2991/**************************************************************************
2992 *
2993 * Kernel module interface
2994 *
2995 *************************************************************************/
2996
2997module_param(interrupt_mode, uint, 0444);
2998MODULE_PARM_DESC(interrupt_mode,
2999 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3000
3001static int __init efx_init_module(void)
3002{
3003 int rc;
3004
3005 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3006
3007 rc = register_netdevice_notifier(&efx_netdev_notifier);
3008 if (rc)
3009 goto err_notifier;
3010
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003011 rc = efx_init_sriov();
3012 if (rc)
3013 goto err_sriov;
3014
Steve Hodgson1ab00622008-12-12 21:33:02 -08003015 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3016 if (!reset_workqueue) {
3017 rc = -ENOMEM;
3018 goto err_reset;
3019 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003020
3021 rc = pci_register_driver(&efx_pci_driver);
3022 if (rc < 0)
3023 goto err_pci;
3024
3025 return 0;
3026
3027 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003028 destroy_workqueue(reset_workqueue);
3029 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003030 efx_fini_sriov();
3031 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003032 unregister_netdevice_notifier(&efx_netdev_notifier);
3033 err_notifier:
3034 return rc;
3035}
3036
3037static void __exit efx_exit_module(void)
3038{
3039 printk(KERN_INFO "Solarflare NET driver unloading\n");
3040
3041 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003042 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003043 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003044 unregister_netdevice_notifier(&efx_netdev_notifier);
3045
3046}
3047
3048module_init(efx_init_module);
3049module_exit(efx_exit_module);
3050
Ben Hutchings906bb262009-11-29 15:16:19 +00003051MODULE_AUTHOR("Solarflare Communications and "
3052 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003053MODULE_DESCRIPTION("Solarflare Communications network driver");
3054MODULE_LICENSE("GPL");
3055MODULE_DEVICE_TABLE(pci, efx_pci_table);