blob: 34b56ec87fba57179e9498b59aeffcf2c0160e4d [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/pci.h>
Ben Hutchings64d8ad62011-01-05 00:50:41 +000025#include <linux/cpu_rmap.h>
Alexandre Rames626950d2013-01-14 17:20:22 +000026#include <linux/aer.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010027#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010028#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000029#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000030#include "selftest.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010031
Ben Hutchings8880f4e2009-11-29 15:15:41 +000032#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000033#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000034
Ben Hutchingsc4593022009-11-23 16:08:17 +000035/**************************************************************************
36 *
37 * Type name strings
38 *
39 **************************************************************************
40 */
41
42/* Loopback mode names (see LOOPBACK_MODE()) */
43const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000044const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000045 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000046 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000047 [LOOPBACK_GMAC] = "GMAC",
48 [LOOPBACK_XGMII] = "XGMII",
49 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000050 [LOOPBACK_XAUI] = "XAUI",
51 [LOOPBACK_GMII] = "GMII",
52 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000053 [LOOPBACK_XGBR] = "XGBR",
54 [LOOPBACK_XFI] = "XFI",
55 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
56 [LOOPBACK_GMII_FAR] = "GMII_FAR",
57 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
58 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000059 [LOOPBACK_GPHY] = "GPHY",
60 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000061 [LOOPBACK_PCS] = "PCS",
62 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000063 [LOOPBACK_XPORT] = "XPORT",
64 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000065 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000066 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
67 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000068 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000069 [LOOPBACK_XFI_WS] = "XFI_WS",
70 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000071 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000072};
73
Ben Hutchingsc4593022009-11-23 16:08:17 +000074const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000075const char *const efx_reset_type_names[] = {
Alexandre Rames626950d2013-01-14 17:20:22 +000076 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
77 [RESET_TYPE_ALL] = "ALL",
78 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
79 [RESET_TYPE_WORLD] = "WORLD",
80 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
81 [RESET_TYPE_DISABLE] = "DISABLE",
82 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
83 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
84 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
85 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
86 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
87 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
88 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Ben Hutchingsc4593022009-11-23 16:08:17 +000089};
90
Ben Hutchings8ceee662008-04-27 12:55:59 +010091#define EFX_MAX_MTU (9 * 1024)
92
Steve Hodgson1ab00622008-12-12 21:33:02 -080093/* Reset workqueue. If any NIC has a hardware failure then a reset will be
94 * queued onto this work queue. This is not a per-nic work queue, because
95 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
96 */
97static struct workqueue_struct *reset_workqueue;
98
Ben Hutchings8ceee662008-04-27 12:55:59 +010099/**************************************************************************
100 *
101 * Configurable values
102 *
103 *************************************************************************/
104
105/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100106 * Use separate channels for TX and RX events
107 *
Neil Turton28b581a2008-12-12 21:41:06 -0800108 * Set this to 1 to use separate channels for TX and RX. It allows us
109 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100110 *
Neil Turton28b581a2008-12-12 21:41:06 -0800111 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100112 */
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000113static bool separate_tx_channels;
114module_param(separate_tx_channels, bool, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800115MODULE_PARM_DESC(separate_tx_channels,
116 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100117
118/* This is the weight assigned to each of the (per-channel) virtual
119 * NAPI devices.
120 */
121static int napi_weight = 64;
122
123/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000124 * monitor.
125 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000126 * - Check the on-board hardware monitor;
127 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000128 * On Siena-based NICs for power systems with EEH support, this will give EEH a
129 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100130 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000131static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100132
Ben Hutchings8ceee662008-04-27 12:55:59 +0100133/* Initial interrupt moderation settings. They can be modified after
134 * module load with ethtool.
135 *
136 * The default for RX should strike a balance between increasing the
137 * round-trip latency and reducing overhead.
138 */
139static unsigned int rx_irq_mod_usec = 60;
140
141/* Initial interrupt moderation settings. They can be modified after
142 * module load with ethtool.
143 *
144 * This default is chosen to ensure that a 10G link does not go idle
145 * while a TX queue is stopped after it has become full. A queue is
146 * restarted when it drops below half full. The time this takes (assuming
147 * worst case 3 descriptors per packet and 1024 descriptors) is
148 * 512 / 3 * 1.2 = 205 usec.
149 */
150static unsigned int tx_irq_mod_usec = 150;
151
152/* This is the first interrupt mode to try out of:
153 * 0 => MSI-X
154 * 1 => MSI
155 * 2 => legacy
156 */
157static unsigned int interrupt_mode;
158
159/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
160 * i.e. the number of CPUs among which we may distribute simultaneous
161 * interrupt handling.
162 *
163 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000164 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100165 */
166static unsigned int rss_cpus;
167module_param(rss_cpus, uint, 0444);
168MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
169
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000170static bool phy_flash_cfg;
171module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800172MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
173
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000174static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000175module_param(irq_adapt_low_thresh, uint, 0644);
176MODULE_PARM_DESC(irq_adapt_low_thresh,
177 "Threshold score for reducing IRQ moderation");
178
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000179static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000180module_param(irq_adapt_high_thresh, uint, 0644);
181MODULE_PARM_DESC(irq_adapt_high_thresh,
182 "Threshold score for increasing IRQ moderation");
183
Ben Hutchings62776d02010-06-23 11:30:07 +0000184static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
185 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
186 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
187 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
188module_param(debug, uint, 0);
189MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
190
Ben Hutchings8ceee662008-04-27 12:55:59 +0100191/**************************************************************************
192 *
193 * Utility functions and prototypes
194 *
195 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000196
Ben Hutchings7f967c02012-02-13 23:45:02 +0000197static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq);
198static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq);
199static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000200static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000201static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100202static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000203static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100204static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000205static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000206static void efx_fini_struct(struct efx_nic *efx);
207static void efx_start_all(struct efx_nic *efx);
208static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100209
210#define EFX_ASSERT_RESET_SERIALISED(efx) \
211 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100212 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000213 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000214 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100215 ASSERT_RTNL(); \
216 } while (0)
217
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100218static int efx_check_disabled(struct efx_nic *efx)
219{
Alexandre Rames626950d2013-01-14 17:20:22 +0000220 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100221 netif_err(efx, drv, efx->net_dev,
222 "device is disabled due to earlier errors\n");
223 return -EIO;
224 }
225 return 0;
226}
227
Ben Hutchings8ceee662008-04-27 12:55:59 +0100228/**************************************************************************
229 *
230 * Event queue processing
231 *
232 *************************************************************************/
233
234/* Process channel's event queue
235 *
236 * This function is responsible for processing the event queue of a
237 * single channel. The caller must guarantee that this function will
238 * never be concurrently called more than once on the same channel,
239 * though different channels may be being processed concurrently.
240 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000241static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100242{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000243 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100244
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000245 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100246 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100247
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000248 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000249 if (spent && efx_channel_has_rx_queue(channel)) {
250 struct efx_rx_queue *rx_queue =
251 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100252
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000253 /* Deliver last RX packet. */
254 if (channel->rx_pkt) {
255 __efx_rx_packet(channel, channel->rx_pkt);
256 channel->rx_pkt = NULL;
257 }
Alexandre Rames97d48a12013-01-11 12:26:21 +0000258 if (rx_queue->enabled)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000259 efx_fast_push_rx_descriptors(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100260 }
261
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000262 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100263}
264
265/* Mark channel as finished processing
266 *
267 * Note that since we will not receive further interrupts for this
268 * channel before we finish processing and call the eventq_read_ack()
269 * method, there is no need to use the interrupt hold-off timers.
270 */
271static inline void efx_channel_processed(struct efx_channel *channel)
272{
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100273 /* The interrupt handler for this channel may set work_pending
274 * as soon as we acknowledge the events we've seen. Make sure
275 * it's cleared before then. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100276 channel->work_pending = false;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100277 smp_wmb();
278
Ben Hutchings152b6a62009-11-29 03:43:56 +0000279 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100280}
281
282/* NAPI poll handler
283 *
284 * NAPI guarantees serialisation of polls of the same device, which
285 * provides the guarantee required by efx_process_channel().
286 */
287static int efx_poll(struct napi_struct *napi, int budget)
288{
289 struct efx_channel *channel =
290 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000291 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000292 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100293
Ben Hutchings62776d02010-06-23 11:30:07 +0000294 netif_vdbg(efx, intr, efx->net_dev,
295 "channel %d NAPI poll executing on CPU %d\n",
296 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100297
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000298 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100299
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000300 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000301 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000302 efx->irq_rx_adaptive &&
303 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000304 if (unlikely(channel->irq_mod_score <
305 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000306 if (channel->irq_moderation > 1) {
307 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000308 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000309 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000310 } else if (unlikely(channel->irq_mod_score >
311 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000312 if (channel->irq_moderation <
313 efx->irq_rx_moderation) {
314 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000315 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000316 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000317 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000318 channel->irq_count = 0;
319 channel->irq_mod_score = 0;
320 }
321
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000322 efx_filter_rfs_expire(channel);
323
Ben Hutchings8ceee662008-04-27 12:55:59 +0100324 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800325 * only wait for napi_complete(), this isn't a problem
Ben Hutchings8ceee662008-04-27 12:55:59 +0100326 * since efx_channel_processed() will have no effect if
327 * interrupts have already been disabled.
328 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800329 napi_complete(napi);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100330 efx_channel_processed(channel);
331 }
332
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000333 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100334}
335
336/* Process the eventq of the specified channel immediately on this CPU
337 *
338 * Disable hardware generated interrupts, wait for any existing
339 * processing to finish, then directly poll (and ack ) the eventq.
340 * Finally reenable NAPI and interrupts.
341 *
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100342 * This is for use only during a loopback self-test. It must not
343 * deliver any packets up the stack as this can result in deadlock.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100344 */
345void efx_process_channel_now(struct efx_channel *channel)
346{
347 struct efx_nic *efx = channel->efx;
348
Ben Hutchings8313aca2010-09-10 06:41:57 +0000349 BUG_ON(channel->channel >= efx->n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100350 BUG_ON(!channel->enabled);
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100351 BUG_ON(!efx->loopback_selftest);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100352
353 /* Disable interrupts and wait for ISRs to complete */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000354 efx_nic_disable_interrupts(efx);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000355 if (efx->legacy_irq) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100356 synchronize_irq(efx->legacy_irq);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000357 efx->legacy_irq_enabled = false;
358 }
Ben Hutchings64ee3122008-09-01 12:47:38 +0100359 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100360 synchronize_irq(channel->irq);
361
362 /* Wait for any NAPI processing to complete */
363 napi_disable(&channel->napi_str);
364
365 /* Poll the channel */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000366 efx_process_channel(channel, channel->eventq_mask + 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100367
368 /* Ack the eventq. This may cause an interrupt to be generated
369 * when they are reenabled */
370 efx_channel_processed(channel);
371
372 napi_enable(&channel->napi_str);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000373 if (efx->legacy_irq)
374 efx->legacy_irq_enabled = true;
Ben Hutchings152b6a62009-11-29 03:43:56 +0000375 efx_nic_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100376}
377
378/* Create event queue
379 * Event queue memory allocations are done only once. If the channel
380 * is reset, the memory buffer will be reused; this guards against
381 * errors during channel reset and also simplifies interrupt handling.
382 */
383static int efx_probe_eventq(struct efx_channel *channel)
384{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000385 struct efx_nic *efx = channel->efx;
386 unsigned long entries;
387
Ben Hutchings86ee5302012-01-09 19:51:22 +0000388 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000389 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100390
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000391 /* Build an event queue with room for one event per tx and rx buffer,
392 * plus some extra for link state events and MCDI completions. */
393 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
394 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
395 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
396
Ben Hutchings152b6a62009-11-29 03:43:56 +0000397 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100398}
399
400/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100401static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100402{
Ben Hutchings62776d02010-06-23 11:30:07 +0000403 netif_dbg(channel->efx, drv, channel->efx->net_dev,
404 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100405
406 channel->eventq_read_ptr = 0;
407
Ben Hutchings152b6a62009-11-29 03:43:56 +0000408 efx_nic_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100409}
410
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000411/* Enable event queue processing and NAPI */
412static void efx_start_eventq(struct efx_channel *channel)
413{
414 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
415 "chan %d start event queue\n", channel->channel);
416
417 /* The interrupt handler for this channel may set work_pending
418 * as soon as we enable it. Make sure it's cleared before
419 * then. Similarly, make sure it sees the enabled flag set.
420 */
421 channel->work_pending = false;
422 channel->enabled = true;
423 smp_wmb();
424
425 napi_enable(&channel->napi_str);
426 efx_nic_eventq_read_ack(channel);
427}
428
429/* Disable event queue processing and NAPI */
430static void efx_stop_eventq(struct efx_channel *channel)
431{
432 if (!channel->enabled)
433 return;
434
435 napi_disable(&channel->napi_str);
436 channel->enabled = false;
437}
438
Ben Hutchings8ceee662008-04-27 12:55:59 +0100439static void efx_fini_eventq(struct efx_channel *channel)
440{
Ben Hutchings62776d02010-06-23 11:30:07 +0000441 netif_dbg(channel->efx, drv, channel->efx->net_dev,
442 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100443
Ben Hutchings152b6a62009-11-29 03:43:56 +0000444 efx_nic_fini_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100445}
446
447static void efx_remove_eventq(struct efx_channel *channel)
448{
Ben Hutchings62776d02010-06-23 11:30:07 +0000449 netif_dbg(channel->efx, drv, channel->efx->net_dev,
450 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100451
Ben Hutchings152b6a62009-11-29 03:43:56 +0000452 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100453}
454
455/**************************************************************************
456 *
457 * Channel handling
458 *
459 *************************************************************************/
460
Ben Hutchings7f967c02012-02-13 23:45:02 +0000461/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000462static struct efx_channel *
463efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
464{
465 struct efx_channel *channel;
466 struct efx_rx_queue *rx_queue;
467 struct efx_tx_queue *tx_queue;
468 int j;
469
Ben Hutchings7f967c02012-02-13 23:45:02 +0000470 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
471 if (!channel)
472 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000473
Ben Hutchings7f967c02012-02-13 23:45:02 +0000474 channel->efx = efx;
475 channel->channel = i;
476 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000477
Ben Hutchings7f967c02012-02-13 23:45:02 +0000478 for (j = 0; j < EFX_TXQ_TYPES; j++) {
479 tx_queue = &channel->tx_queue[j];
480 tx_queue->efx = efx;
481 tx_queue->queue = i * EFX_TXQ_TYPES + j;
482 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000483 }
484
Ben Hutchings46426102010-09-10 06:42:33 +0000485 rx_queue = &channel->rx_queue;
486 rx_queue->efx = efx;
487 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
488 (unsigned long)rx_queue);
489
490 return channel;
491}
492
Ben Hutchings7f967c02012-02-13 23:45:02 +0000493/* Allocate and initialise a channel structure, copying parameters
494 * (but not resources) from an old channel structure.
495 */
496static struct efx_channel *
497efx_copy_channel(const struct efx_channel *old_channel)
498{
499 struct efx_channel *channel;
500 struct efx_rx_queue *rx_queue;
501 struct efx_tx_queue *tx_queue;
502 int j;
503
504 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
505 if (!channel)
506 return NULL;
507
508 *channel = *old_channel;
509
510 channel->napi_dev = NULL;
511 memset(&channel->eventq, 0, sizeof(channel->eventq));
512
513 for (j = 0; j < EFX_TXQ_TYPES; j++) {
514 tx_queue = &channel->tx_queue[j];
515 if (tx_queue->channel)
516 tx_queue->channel = channel;
517 tx_queue->buffer = NULL;
518 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
519 }
520
521 rx_queue = &channel->rx_queue;
522 rx_queue->buffer = NULL;
523 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
524 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
525 (unsigned long)rx_queue);
526
527 return channel;
528}
529
Ben Hutchings8ceee662008-04-27 12:55:59 +0100530static int efx_probe_channel(struct efx_channel *channel)
531{
532 struct efx_tx_queue *tx_queue;
533 struct efx_rx_queue *rx_queue;
534 int rc;
535
Ben Hutchings62776d02010-06-23 11:30:07 +0000536 netif_dbg(channel->efx, probe, channel->efx->net_dev,
537 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100538
Ben Hutchings7f967c02012-02-13 23:45:02 +0000539 rc = channel->type->pre_probe(channel);
540 if (rc)
541 goto fail;
542
Ben Hutchings8ceee662008-04-27 12:55:59 +0100543 rc = efx_probe_eventq(channel);
544 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000545 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100546
547 efx_for_each_channel_tx_queue(tx_queue, channel) {
548 rc = efx_probe_tx_queue(tx_queue);
549 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000550 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100551 }
552
553 efx_for_each_channel_rx_queue(rx_queue, channel) {
554 rc = efx_probe_rx_queue(rx_queue);
555 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000556 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100557 }
558
559 channel->n_rx_frm_trunc = 0;
560
561 return 0;
562
Ben Hutchings7f967c02012-02-13 23:45:02 +0000563fail:
564 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100565 return rc;
566}
567
Ben Hutchings7f967c02012-02-13 23:45:02 +0000568static void
569efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
570{
571 struct efx_nic *efx = channel->efx;
572 const char *type;
573 int number;
574
575 number = channel->channel;
576 if (efx->tx_channel_offset == 0) {
577 type = "";
578 } else if (channel->channel < efx->tx_channel_offset) {
579 type = "-rx";
580 } else {
581 type = "-tx";
582 number -= efx->tx_channel_offset;
583 }
584 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
585}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100586
Ben Hutchings56536e92008-12-12 21:37:02 -0800587static void efx_set_channel_names(struct efx_nic *efx)
588{
589 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800590
Ben Hutchings7f967c02012-02-13 23:45:02 +0000591 efx_for_each_channel(channel, efx)
592 channel->type->get_name(channel,
593 efx->channel_name[channel->channel],
594 sizeof(efx->channel_name[0]));
Ben Hutchings56536e92008-12-12 21:37:02 -0800595}
596
Ben Hutchings46426102010-09-10 06:42:33 +0000597static int efx_probe_channels(struct efx_nic *efx)
598{
599 struct efx_channel *channel;
600 int rc;
601
602 /* Restart special buffer allocation */
603 efx->next_buffer_table = 0;
604
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000605 /* Probe channels in reverse, so that any 'extra' channels
606 * use the start of the buffer table. This allows the traffic
607 * channels to be resized without moving them or wasting the
608 * entries before them.
609 */
610 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000611 rc = efx_probe_channel(channel);
612 if (rc) {
613 netif_err(efx, probe, efx->net_dev,
614 "failed to create channel %d\n",
615 channel->channel);
616 goto fail;
617 }
618 }
619 efx_set_channel_names(efx);
620
621 return 0;
622
623fail:
624 efx_remove_channels(efx);
625 return rc;
626}
627
Ben Hutchings8ceee662008-04-27 12:55:59 +0100628/* Channels are shutdown and reinitialised whilst the NIC is running
629 * to propagate configuration changes (mtu, checksum offload), or
630 * to clear hardware error conditions
631 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000632static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100633{
634 struct efx_tx_queue *tx_queue;
635 struct efx_rx_queue *rx_queue;
636 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100637
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100638 /* Calculate the rx buffer allocation parameters required to
639 * support the current MTU, including padding for header
640 * alignment and overruns.
641 */
Ben Hutchings272baee2013-01-29 23:33:14 +0000642 efx->rx_dma_len = (efx->type->rx_buffer_hash_size +
643 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
644 efx->type->rx_buffer_padding);
645 efx->rx_buffer_order = get_order(sizeof(struct efx_rx_page_state) +
646 EFX_PAGE_IP_ALIGN + efx->rx_dma_len);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100647
Ben Hutchings14bf7182012-05-22 01:27:58 +0100648 /* We must keep at least one descriptor in a TX ring empty.
649 * We could avoid this when the queue size does not exactly
650 * match the hardware ring size, but it's not that important.
651 * Therefore we stop the queue when one more skb might fill
652 * the ring completely. We wake it when half way back to
653 * empty.
654 */
655 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
656 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
657
Ben Hutchings8ceee662008-04-27 12:55:59 +0100658 /* Initialise the channels */
659 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100660 efx_for_each_channel_tx_queue(tx_queue, channel)
661 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100662
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000663 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100664 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000665 efx_nic_generate_fill_event(rx_queue);
666 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100667
668 WARN_ON(channel->rx_pkt != NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100669 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000670
671 if (netif_device_present(efx->net_dev))
672 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100673}
674
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000675static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100676{
677 struct efx_channel *channel;
678 struct efx_tx_queue *tx_queue;
679 struct efx_rx_queue *rx_queue;
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100680 struct pci_dev *dev = efx->pci_dev;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100681 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100682
683 EFX_ASSERT_RESET_SERIALISED(efx);
684 BUG_ON(efx->port_enabled);
685
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100686 /* Only perform flush if dma is enabled */
Alexandre Rames626950d2013-01-14 17:20:22 +0000687 if (dev->is_busmaster && efx->state != STATE_RECOVERY) {
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100688 rc = efx_nic_flush_queues(efx);
689
690 if (rc && EFX_WORKAROUND_7803(efx)) {
691 /* Schedule a reset to recover from the flush failure. The
692 * descriptor caches reference memory we're about to free,
693 * but falcon_reconfigure_mac_wrapper() won't reconnect
694 * the MACs because of the pending reset. */
695 netif_err(efx, drv, efx->net_dev,
696 "Resetting to recover from flush failure\n");
697 efx_schedule_reset(efx, RESET_TYPE_ALL);
698 } else if (rc) {
699 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
700 } else {
701 netif_dbg(efx, drv, efx->net_dev,
702 "successfully flushed all queues\n");
703 }
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000704 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100705
Ben Hutchings8ceee662008-04-27 12:55:59 +0100706 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000707 /* RX packet processing is pipelined, so wait for the
708 * NAPI handler to complete. At least event queue 0
709 * might be kept active by non-data events, so don't
710 * use napi_synchronize() but actually disable NAPI
711 * temporarily.
712 */
713 if (efx_channel_has_rx_queue(channel)) {
714 efx_stop_eventq(channel);
715 efx_start_eventq(channel);
716 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100717
718 efx_for_each_channel_rx_queue(rx_queue, channel)
719 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000720 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100721 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100722 }
723}
724
725static void efx_remove_channel(struct efx_channel *channel)
726{
727 struct efx_tx_queue *tx_queue;
728 struct efx_rx_queue *rx_queue;
729
Ben Hutchings62776d02010-06-23 11:30:07 +0000730 netif_dbg(channel->efx, drv, channel->efx->net_dev,
731 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100732
733 efx_for_each_channel_rx_queue(rx_queue, channel)
734 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000735 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100736 efx_remove_tx_queue(tx_queue);
737 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100738 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100739}
740
Ben Hutchings46426102010-09-10 06:42:33 +0000741static void efx_remove_channels(struct efx_nic *efx)
742{
743 struct efx_channel *channel;
744
745 efx_for_each_channel(channel, efx)
746 efx_remove_channel(channel);
747}
748
749int
750efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
751{
752 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
753 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000754 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100755 int rc;
756
757 rc = efx_check_disabled(efx);
758 if (rc)
759 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000760
761 /* Not all channels should be reallocated. We must avoid
762 * reallocating their buffer table entries.
763 */
764 efx_for_each_channel(channel, efx) {
765 struct efx_rx_queue *rx_queue;
766 struct efx_tx_queue *tx_queue;
767
768 if (channel->type->copy)
769 continue;
770 next_buffer_table = max(next_buffer_table,
771 channel->eventq.index +
772 channel->eventq.entries);
773 efx_for_each_channel_rx_queue(rx_queue, channel)
774 next_buffer_table = max(next_buffer_table,
775 rx_queue->rxd.index +
776 rx_queue->rxd.entries);
777 efx_for_each_channel_tx_queue(tx_queue, channel)
778 next_buffer_table = max(next_buffer_table,
779 tx_queue->txd.index +
780 tx_queue->txd.entries);
781 }
Ben Hutchings46426102010-09-10 06:42:33 +0000782
Ben Hutchings29c69a42013-01-28 19:01:06 +0000783 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000784 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000785 efx_stop_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000786
Ben Hutchings7f967c02012-02-13 23:45:02 +0000787 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000788 memset(other_channel, 0, sizeof(other_channel));
789 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000790 channel = efx->channel[i];
791 if (channel->type->copy)
792 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000793 if (!channel) {
794 rc = -ENOMEM;
795 goto out;
796 }
797 other_channel[i] = channel;
798 }
799
800 /* Swap entry counts and channel pointers */
801 old_rxq_entries = efx->rxq_entries;
802 old_txq_entries = efx->txq_entries;
803 efx->rxq_entries = rxq_entries;
804 efx->txq_entries = txq_entries;
805 for (i = 0; i < efx->n_channels; i++) {
806 channel = efx->channel[i];
807 efx->channel[i] = other_channel[i];
808 other_channel[i] = channel;
809 }
810
Ben Hutchings7f967c02012-02-13 23:45:02 +0000811 /* Restart buffer table allocation */
812 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000813
Ben Hutchingse8f14992010-12-07 19:47:34 +0000814 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000815 channel = efx->channel[i];
816 if (!channel->type->copy)
817 continue;
818 rc = efx_probe_channel(channel);
819 if (rc)
820 goto rollback;
821 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000822 }
Ben Hutchings46426102010-09-10 06:42:33 +0000823
Ben Hutchings7f967c02012-02-13 23:45:02 +0000824out:
825 /* Destroy unused channel structures */
826 for (i = 0; i < efx->n_channels; i++) {
827 channel = other_channel[i];
828 if (channel && channel->type->copy) {
829 efx_fini_napi_channel(channel);
830 efx_remove_channel(channel);
831 kfree(channel);
832 }
833 }
834
835 efx_start_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000836 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000837 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000838 return rc;
839
840rollback:
841 /* Swap back */
842 efx->rxq_entries = old_rxq_entries;
843 efx->txq_entries = old_txq_entries;
844 for (i = 0; i < efx->n_channels; i++) {
845 channel = efx->channel[i];
846 efx->channel[i] = other_channel[i];
847 other_channel[i] = channel;
848 }
849 goto out;
850}
851
Steve Hodgson90d683a2010-06-01 11:19:39 +0000852void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100853{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000854 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100855}
856
Ben Hutchings7f967c02012-02-13 23:45:02 +0000857static const struct efx_channel_type efx_default_channel_type = {
858 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100859 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000860 .get_name = efx_get_channel_name,
861 .copy = efx_copy_channel,
862 .keep_eventq = false,
863};
864
865int efx_channel_dummy_op_int(struct efx_channel *channel)
866{
867 return 0;
868}
869
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100870void efx_channel_dummy_op_void(struct efx_channel *channel)
871{
872}
873
Ben Hutchings8ceee662008-04-27 12:55:59 +0100874/**************************************************************************
875 *
876 * Port handling
877 *
878 **************************************************************************/
879
880/* This ensures that the kernel is kept informed (via
881 * netif_carrier_on/off) of the link status, and also maintains the
882 * link status's stop on the port's TX queue.
883 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000884void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100885{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000886 struct efx_link_state *link_state = &efx->link_state;
887
Ben Hutchings8ceee662008-04-27 12:55:59 +0100888 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
889 * that no events are triggered between unregister_netdev() and the
890 * driver unloading. A more general condition is that NETDEV_CHANGE
891 * can only be generated between NETDEV_UP and NETDEV_DOWN */
892 if (!netif_running(efx->net_dev))
893 return;
894
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000895 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100896 efx->n_link_state_changes++;
897
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000898 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100899 netif_carrier_on(efx->net_dev);
900 else
901 netif_carrier_off(efx->net_dev);
902 }
903
904 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000905 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000906 netif_info(efx, link, efx->net_dev,
907 "link up at %uMbps %s-duplex (MTU %d)%s\n",
908 link_state->speed, link_state->fd ? "full" : "half",
909 efx->net_dev->mtu,
910 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000911 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000912 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100913}
914
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000915void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
916{
917 efx->link_advertising = advertising;
918 if (advertising) {
919 if (advertising & ADVERTISED_Pause)
920 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
921 else
922 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
923 if (advertising & ADVERTISED_Asym_Pause)
924 efx->wanted_fc ^= EFX_FC_TX;
925 }
926}
927
David S. Millerb56269462011-05-17 17:53:22 -0400928void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000929{
930 efx->wanted_fc = wanted_fc;
931 if (efx->link_advertising) {
932 if (wanted_fc & EFX_FC_RX)
933 efx->link_advertising |= (ADVERTISED_Pause |
934 ADVERTISED_Asym_Pause);
935 else
936 efx->link_advertising &= ~(ADVERTISED_Pause |
937 ADVERTISED_Asym_Pause);
938 if (wanted_fc & EFX_FC_TX)
939 efx->link_advertising ^= ADVERTISED_Asym_Pause;
940 }
941}
942
Ben Hutchings115122a2009-03-04 09:52:52 +0000943static void efx_fini_port(struct efx_nic *efx);
944
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000945/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
946 * the MAC appropriately. All other PHY configuration changes are pushed
947 * through phy_op->set_settings(), and pushed asynchronously to the MAC
948 * through efx_monitor().
949 *
950 * Callers must hold the mac_lock
951 */
952int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100953{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000954 enum efx_phy_mode phy_mode;
955 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100956
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000957 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100958
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000959 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000960 netif_addr_lock_bh(efx->net_dev);
961 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100962
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000963 /* Disable PHY transmit in mac level loopbacks */
964 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800965 if (LOOPBACK_INTERNAL(efx))
966 efx->phy_mode |= PHY_MODE_TX_DISABLED;
967 else
968 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800969
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000970 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800971
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000972 if (rc)
973 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100974
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000975 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100976}
977
978/* Reinitialise the MAC to pick up new PHY settings, even if the port is
979 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000980int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100981{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000982 int rc;
983
Ben Hutchings8ceee662008-04-27 12:55:59 +0100984 EFX_ASSERT_RESET_SERIALISED(efx);
985
986 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000987 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100988 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000989
990 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100991}
992
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000993/* Asynchronous work item for changing MAC promiscuity and multicast
994 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
995 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800996static void efx_mac_work(struct work_struct *data)
997{
998 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
999
1000 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +01001001 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +01001002 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001003 mutex_unlock(&efx->mac_lock);
1004}
1005
Ben Hutchings8ceee662008-04-27 12:55:59 +01001006static int efx_probe_port(struct efx_nic *efx)
1007{
1008 int rc;
1009
Ben Hutchings62776d02010-06-23 11:30:07 +00001010 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001011
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001012 if (phy_flash_cfg)
1013 efx->phy_mode = PHY_MODE_SPECIAL;
1014
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001015 /* Connect up MAC/PHY operations table */
1016 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001017 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001018 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001019
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001020 /* Initialise MAC address to permanent address */
1021 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001022
1023 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001024}
1025
1026static int efx_init_port(struct efx_nic *efx)
1027{
1028 int rc;
1029
Ben Hutchings62776d02010-06-23 11:30:07 +00001030 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001031
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001032 mutex_lock(&efx->mac_lock);
1033
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001034 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001035 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001036 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001037
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001038 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001039
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001040 /* Reconfigure the MAC before creating dma queues (required for
1041 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001042 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001043
1044 /* Ensure the PHY advertises the correct flow control settings */
1045 rc = efx->phy_op->reconfigure(efx);
1046 if (rc)
1047 goto fail2;
1048
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001049 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001050 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001051
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001052fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001053 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001054fail1:
1055 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001056 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001057}
1058
Ben Hutchings8ceee662008-04-27 12:55:59 +01001059static void efx_start_port(struct efx_nic *efx)
1060{
Ben Hutchings62776d02010-06-23 11:30:07 +00001061 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001062 BUG_ON(efx->port_enabled);
1063
1064 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001065 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001066
1067 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1068 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001069 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001070
Ben Hutchings8ceee662008-04-27 12:55:59 +01001071 mutex_unlock(&efx->mac_lock);
1072}
1073
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001074/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001075static void efx_stop_port(struct efx_nic *efx)
1076{
Ben Hutchings62776d02010-06-23 11:30:07 +00001077 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001078
1079 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001080 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001081 mutex_unlock(&efx->mac_lock);
1082
1083 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001084 netif_addr_lock_bh(efx->net_dev);
1085 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001086}
1087
1088static void efx_fini_port(struct efx_nic *efx)
1089{
Ben Hutchings62776d02010-06-23 11:30:07 +00001090 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001091
1092 if (!efx->port_initialized)
1093 return;
1094
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001095 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001096 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001097
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001098 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001099 efx_link_status_changed(efx);
1100}
1101
1102static void efx_remove_port(struct efx_nic *efx)
1103{
Ben Hutchings62776d02010-06-23 11:30:07 +00001104 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001105
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001106 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001107}
1108
1109/**************************************************************************
1110 *
1111 * NIC handling
1112 *
1113 **************************************************************************/
1114
1115/* This configures the PCI device to enable I/O and DMA. */
1116static int efx_init_io(struct efx_nic *efx)
1117{
1118 struct pci_dev *pci_dev = efx->pci_dev;
1119 dma_addr_t dma_mask = efx->type->max_dma_mask;
1120 int rc;
1121
Ben Hutchings62776d02010-06-23 11:30:07 +00001122 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001123
1124 rc = pci_enable_device(pci_dev);
1125 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001126 netif_err(efx, probe, efx->net_dev,
1127 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001128 goto fail1;
1129 }
1130
1131 pci_set_master(pci_dev);
1132
1133 /* Set the PCI DMA mask. Try all possibilities from our
1134 * genuine mask down to 32 bits, because some architectures
1135 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1136 * masks event though they reject 46 bit masks.
1137 */
1138 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001139 if (dma_supported(&pci_dev->dev, dma_mask)) {
1140 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001141 if (rc == 0)
1142 break;
1143 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001144 dma_mask >>= 1;
1145 }
1146 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001147 netif_err(efx, probe, efx->net_dev,
1148 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001149 goto fail2;
1150 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001151 netif_dbg(efx, probe, efx->net_dev,
1152 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001153 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001154 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001155 /* dma_set_coherent_mask() is not *allowed* to
1156 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001157 * but just in case...
1158 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001159 netif_err(efx, probe, efx->net_dev,
1160 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001161 goto fail2;
1162 }
1163
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001164 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1165 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001166 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001167 netif_err(efx, probe, efx->net_dev,
1168 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001169 rc = -EIO;
1170 goto fail3;
1171 }
David S. Miller8decf862011-09-22 03:23:13 -04001172 efx->membase = ioremap_nocache(efx->membase_phys,
1173 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001174 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001175 netif_err(efx, probe, efx->net_dev,
1176 "could not map memory BAR at %llx+%x\n",
1177 (unsigned long long)efx->membase_phys,
1178 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001179 rc = -ENOMEM;
1180 goto fail4;
1181 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001182 netif_dbg(efx, probe, efx->net_dev,
1183 "memory BAR at %llx+%x (virtual %p)\n",
1184 (unsigned long long)efx->membase_phys,
1185 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001186
1187 return 0;
1188
1189 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001190 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001191 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001192 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001193 fail2:
1194 pci_disable_device(efx->pci_dev);
1195 fail1:
1196 return rc;
1197}
1198
1199static void efx_fini_io(struct efx_nic *efx)
1200{
Ben Hutchings62776d02010-06-23 11:30:07 +00001201 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001202
1203 if (efx->membase) {
1204 iounmap(efx->membase);
1205 efx->membase = NULL;
1206 }
1207
1208 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001209 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001210 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001211 }
1212
1213 pci_disable_device(efx->pci_dev);
1214}
1215
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001216static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001217{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001218 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001219 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001220 int cpu;
1221
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001222 if (rss_cpus) {
1223 count = rss_cpus;
1224 } else {
1225 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1226 netif_warn(efx, probe, efx->net_dev,
1227 "RSS disabled due to allocation failure\n");
1228 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001229 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001230
1231 count = 0;
1232 for_each_online_cpu(cpu) {
1233 if (!cpumask_test_cpu(cpu, thread_mask)) {
1234 ++count;
1235 cpumask_or(thread_mask, thread_mask,
1236 topology_thread_cpumask(cpu));
1237 }
1238 }
1239
1240 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001241 }
1242
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001243 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1244 * table entries that are inaccessible to VFs
1245 */
1246 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1247 count > efx_vf_size(efx)) {
1248 netif_warn(efx, probe, efx->net_dev,
1249 "Reducing number of RSS channels from %u to %u for "
1250 "VF support. Increase vf-msix-limit to use more "
1251 "channels on the PF.\n",
1252 count, efx_vf_size(efx));
1253 count = efx_vf_size(efx);
1254 }
1255
Ben Hutchings46123d02008-09-01 12:47:33 +01001256 return count;
1257}
1258
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001259static int
1260efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1261{
1262#ifdef CONFIG_RFS_ACCEL
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001263 unsigned int i;
1264 int rc;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001265
1266 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1267 if (!efx->net_dev->rx_cpu_rmap)
1268 return -ENOMEM;
1269 for (i = 0; i < efx->n_rx_channels; i++) {
1270 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1271 xentries[i].vector);
1272 if (rc) {
1273 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1274 efx->net_dev->rx_cpu_rmap = NULL;
1275 return rc;
1276 }
1277 }
1278#endif
1279 return 0;
1280}
1281
Ben Hutchings46123d02008-09-01 12:47:33 +01001282/* Probe the number and type of interrupts we are able to obtain, and
1283 * the resulting numbers of channels and RX queues.
1284 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001285static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001286{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001287 unsigned int max_channels =
1288 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001289 unsigned int extra_channels = 0;
1290 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001291 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001292
Ben Hutchings7f967c02012-02-13 23:45:02 +00001293 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1294 if (efx->extra_channel_type[i])
1295 ++extra_channels;
1296
Ben Hutchings8ceee662008-04-27 12:55:59 +01001297 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001298 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001299 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001300
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001301 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001302 if (separate_tx_channels)
1303 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001304 n_channels += extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001305 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001306
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001307 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001308 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001309 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001310 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001311 netif_err(efx, drv, efx->net_dev,
1312 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001313 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001314 netif_err(efx, drv, efx->net_dev,
1315 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001316 EFX_BUG_ON_PARANOID(rc >= n_channels);
1317 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001318 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001319 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001320 }
1321
1322 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001323 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001324 if (n_channels > extra_channels)
1325 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001326 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001327 efx->n_tx_channels = max(n_channels / 2, 1U);
1328 efx->n_rx_channels = max(n_channels -
1329 efx->n_tx_channels,
1330 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001331 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001332 efx->n_tx_channels = n_channels;
1333 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001334 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001335 rc = efx_init_rx_cpu_rmap(efx, xentries);
1336 if (rc) {
1337 pci_disable_msix(efx->pci_dev);
1338 return rc;
1339 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001340 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001341 efx_get_channel(efx, i)->irq =
1342 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001343 } else {
1344 /* Fall back to single channel MSI */
1345 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001346 netif_err(efx, drv, efx->net_dev,
1347 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001348 }
1349 }
1350
1351 /* Try single interrupt MSI */
1352 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001353 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001354 efx->n_rx_channels = 1;
1355 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001356 rc = pci_enable_msi(efx->pci_dev);
1357 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001358 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001359 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001360 netif_err(efx, drv, efx->net_dev,
1361 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001362 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1363 }
1364 }
1365
1366 /* Assume legacy interrupts */
1367 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001368 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001369 efx->n_rx_channels = 1;
1370 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001371 efx->legacy_irq = efx->pci_dev->irq;
1372 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001373
Ben Hutchings7f967c02012-02-13 23:45:02 +00001374 /* Assign extra channels if possible */
1375 j = efx->n_channels;
1376 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1377 if (!efx->extra_channel_type[i])
1378 continue;
1379 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1380 efx->n_channels <= extra_channels) {
1381 efx->extra_channel_type[i]->handle_no_channel(efx);
1382 } else {
1383 --j;
1384 efx_get_channel(efx, j)->type =
1385 efx->extra_channel_type[i];
1386 }
1387 }
1388
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001389 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001390 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001391 efx->n_rx_channels : efx_vf_size(efx));
1392
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001393 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001394}
1395
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001396/* Enable interrupts, then probe and start the event queues */
Ben Hutchings7f967c02012-02-13 23:45:02 +00001397static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001398{
1399 struct efx_channel *channel;
1400
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001401 BUG_ON(efx->state == STATE_DISABLED);
1402
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001403 if (efx->legacy_irq)
1404 efx->legacy_irq_enabled = true;
1405 efx_nic_enable_interrupts(efx);
1406
1407 efx_for_each_channel(channel, efx) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001408 if (!channel->type->keep_eventq || !may_keep_eventq)
1409 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001410 efx_start_eventq(channel);
1411 }
1412
1413 efx_mcdi_mode_event(efx);
1414}
1415
Ben Hutchings7f967c02012-02-13 23:45:02 +00001416static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001417{
1418 struct efx_channel *channel;
1419
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001420 if (efx->state == STATE_DISABLED)
1421 return;
1422
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001423 efx_mcdi_mode_poll(efx);
1424
1425 efx_nic_disable_interrupts(efx);
1426 if (efx->legacy_irq) {
1427 synchronize_irq(efx->legacy_irq);
1428 efx->legacy_irq_enabled = false;
1429 }
1430
1431 efx_for_each_channel(channel, efx) {
1432 if (channel->irq)
1433 synchronize_irq(channel->irq);
1434
1435 efx_stop_eventq(channel);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001436 if (!channel->type->keep_eventq || !may_keep_eventq)
1437 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001438 }
1439}
1440
Ben Hutchings8ceee662008-04-27 12:55:59 +01001441static void efx_remove_interrupts(struct efx_nic *efx)
1442{
1443 struct efx_channel *channel;
1444
1445 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001446 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001447 channel->irq = 0;
1448 pci_disable_msi(efx->pci_dev);
1449 pci_disable_msix(efx->pci_dev);
1450
1451 /* Remove legacy interrupt */
1452 efx->legacy_irq = 0;
1453}
1454
Ben Hutchings8831da72008-09-01 12:47:48 +01001455static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001456{
Ben Hutchings602a5322011-05-16 17:32:39 +01001457 struct efx_channel *channel;
1458 struct efx_tx_queue *tx_queue;
1459
Ben Hutchings97653432011-01-12 18:26:56 +00001460 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001461 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001462
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001463 /* We need to mark which channels really have RX and TX
1464 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001465 * RX-only and TX-only channels.
1466 */
1467 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001468 if (channel->channel < efx->n_rx_channels)
1469 channel->rx_queue.core_index = channel->channel;
1470 else
1471 channel->rx_queue.core_index = -1;
1472
Ben Hutchings602a5322011-05-16 17:32:39 +01001473 efx_for_each_channel_tx_queue(tx_queue, channel)
1474 tx_queue->queue -= (efx->tx_channel_offset *
1475 EFX_TXQ_TYPES);
1476 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001477}
1478
1479static int efx_probe_nic(struct efx_nic *efx)
1480{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001481 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001482 int rc;
1483
Ben Hutchings62776d02010-06-23 11:30:07 +00001484 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001485
1486 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001487 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001488 if (rc)
1489 return rc;
1490
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001491 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001492 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001493 rc = efx_probe_interrupts(efx);
1494 if (rc)
1495 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001496
Ben Hutchings28e47c42012-02-15 01:58:49 +00001497 efx->type->dimension_resources(efx);
1498
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001499 if (efx->n_channels > 1)
1500 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001501 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001502 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001503 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001504
Ben Hutchings8831da72008-09-01 12:47:48 +01001505 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001506 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1507 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001508
1509 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001510 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1511 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001512
1513 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001514
1515fail:
1516 efx->type->remove(efx);
1517 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001518}
1519
1520static void efx_remove_nic(struct efx_nic *efx)
1521{
Ben Hutchings62776d02010-06-23 11:30:07 +00001522 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001523
1524 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001525 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001526}
1527
1528/**************************************************************************
1529 *
1530 * NIC startup/shutdown
1531 *
1532 *************************************************************************/
1533
1534static int efx_probe_all(struct efx_nic *efx)
1535{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001536 int rc;
1537
Ben Hutchings8ceee662008-04-27 12:55:59 +01001538 rc = efx_probe_nic(efx);
1539 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001540 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001541 goto fail1;
1542 }
1543
Ben Hutchings8ceee662008-04-27 12:55:59 +01001544 rc = efx_probe_port(efx);
1545 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001546 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001547 goto fail2;
1548 }
1549
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001550 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1551 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1552 rc = -EINVAL;
1553 goto fail3;
1554 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001555 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001556
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001557 rc = efx_probe_filters(efx);
1558 if (rc) {
1559 netif_err(efx, probe, efx->net_dev,
1560 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001561 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001562 }
1563
Ben Hutchings7f967c02012-02-13 23:45:02 +00001564 rc = efx_probe_channels(efx);
1565 if (rc)
1566 goto fail4;
1567
Ben Hutchings8ceee662008-04-27 12:55:59 +01001568 return 0;
1569
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001570 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001571 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001572 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001573 efx_remove_port(efx);
1574 fail2:
1575 efx_remove_nic(efx);
1576 fail1:
1577 return rc;
1578}
1579
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001580/* If the interface is supposed to be running but is not, start
1581 * the hardware and software data path, regular activity for the port
1582 * (MAC statistics, link polling, etc.) and schedule the port to be
1583 * reconfigured. Interrupts must already be enabled. This function
1584 * is safe to call multiple times, so long as the NIC is not disabled.
1585 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001586 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001587static void efx_start_all(struct efx_nic *efx)
1588{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001589 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001590 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001591
1592 /* Check that it is appropriate to restart the interface. All
1593 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001594 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001595 return;
1596
Ben Hutchings8ceee662008-04-27 12:55:59 +01001597 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001598 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001599
Alexandre Rames626950d2013-01-14 17:20:22 +00001600 /* Start the hardware monitor if there is one */
1601 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001602 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1603 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001604
1605 /* If link state detection is normally event-driven, we have
1606 * to poll now because we could have missed a change
1607 */
1608 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001609 mutex_lock(&efx->mac_lock);
1610 if (efx->phy_op->poll(efx))
1611 efx_link_status_changed(efx);
1612 mutex_unlock(&efx->mac_lock);
1613 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001614
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001615 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001616}
1617
1618/* Flush all delayed work. Should only be called when no more delayed work
1619 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1620 * since we're holding the rtnl_lock at this point. */
1621static void efx_flush_all(struct efx_nic *efx)
1622{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001623 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001624 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001625 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001626 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001627 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001628}
1629
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001630/* Quiesce the hardware and software data path, and regular activity
1631 * for the port without bringing the link down. Safe to call multiple
1632 * times with the NIC in almost any state, but interrupts should be
1633 * enabled. Requires the RTNL lock.
1634 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001635static void efx_stop_all(struct efx_nic *efx)
1636{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001637 EFX_ASSERT_RESET_SERIALISED(efx);
1638
1639 /* port_enabled can be read safely under the rtnl lock */
1640 if (!efx->port_enabled)
1641 return;
1642
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001643 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001644 efx_stop_port(efx);
1645
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001646 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001647 efx_flush_all(efx);
1648
Ben Hutchings29c69a42013-01-28 19:01:06 +00001649 /* Stop the kernel transmit interface. This is only valid if
1650 * the device is stopped or detached; otherwise the watchdog
1651 * may fire immediately.
1652 */
1653 WARN_ON(netif_running(efx->net_dev) &&
1654 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001655 netif_tx_disable(efx->net_dev);
1656
1657 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001658}
1659
1660static void efx_remove_all(struct efx_nic *efx)
1661{
Ben Hutchings46426102010-09-10 06:42:33 +00001662 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001663 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001664 efx_remove_port(efx);
1665 efx_remove_nic(efx);
1666}
1667
Ben Hutchings8ceee662008-04-27 12:55:59 +01001668/**************************************************************************
1669 *
1670 * Interrupt moderation
1671 *
1672 **************************************************************************/
1673
Ben Hutchingscc180b62011-12-08 19:51:47 +00001674static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001675{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001676 if (usecs == 0)
1677 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001678 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001679 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001680 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001681}
1682
Ben Hutchings8ceee662008-04-27 12:55:59 +01001683/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001684int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1685 unsigned int rx_usecs, bool rx_adaptive,
1686 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001687{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001688 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001689 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1690 efx->timer_quantum_ns,
1691 1000);
1692 unsigned int tx_ticks;
1693 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001694
1695 EFX_ASSERT_RESET_SERIALISED(efx);
1696
Ben Hutchingscc180b62011-12-08 19:51:47 +00001697 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001698 return -EINVAL;
1699
Ben Hutchingscc180b62011-12-08 19:51:47 +00001700 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1701 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1702
Ben Hutchings9e393b32011-09-05 07:43:04 +00001703 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1704 !rx_may_override_tx) {
1705 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1706 "RX and TX IRQ moderation must be equal\n");
1707 return -EINVAL;
1708 }
1709
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001710 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001711 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001712 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001713 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001714 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001715 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001716 channel->irq_moderation = tx_ticks;
1717 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001718
1719 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001720}
1721
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001722void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1723 unsigned int *rx_usecs, bool *rx_adaptive)
1724{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001725 /* We must round up when converting ticks to microseconds
1726 * because we round down when converting the other way.
1727 */
1728
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001729 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001730 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1731 efx->timer_quantum_ns,
1732 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001733
1734 /* If channels are shared between RX and TX, so is IRQ
1735 * moderation. Otherwise, IRQ moderation is the same for all
1736 * TX channels and is not adaptive.
1737 */
1738 if (efx->tx_channel_offset == 0)
1739 *tx_usecs = *rx_usecs;
1740 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001741 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001742 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001743 efx->timer_quantum_ns,
1744 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001745}
1746
Ben Hutchings8ceee662008-04-27 12:55:59 +01001747/**************************************************************************
1748 *
1749 * Hardware monitor
1750 *
1751 **************************************************************************/
1752
Ben Hutchingse254c272010-09-20 08:44:10 +00001753/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001754static void efx_monitor(struct work_struct *data)
1755{
1756 struct efx_nic *efx = container_of(data, struct efx_nic,
1757 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001758
Ben Hutchings62776d02010-06-23 11:30:07 +00001759 netif_vdbg(efx, timer, efx->net_dev,
1760 "hardware monitor executing on CPU %d\n",
1761 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001762 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001763
Ben Hutchings8ceee662008-04-27 12:55:59 +01001764 /* If the mac_lock is already held then it is likely a port
1765 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001766 * most of the work of monitor() anyway. */
1767 if (mutex_trylock(&efx->mac_lock)) {
1768 if (efx->port_enabled)
1769 efx->type->monitor(efx);
1770 mutex_unlock(&efx->mac_lock);
1771 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001772
Ben Hutchings8ceee662008-04-27 12:55:59 +01001773 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1774 efx_monitor_interval);
1775}
1776
1777/**************************************************************************
1778 *
1779 * ioctls
1780 *
1781 *************************************************************************/
1782
1783/* Net device ioctl
1784 * Context: process, rtnl_lock() held.
1785 */
1786static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1787{
Ben Hutchings767e4682008-09-01 12:43:14 +01001788 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001789 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001790
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001791 if (cmd == SIOCSHWTSTAMP)
1792 return efx_ptp_ioctl(efx, ifr, cmd);
1793
Ben Hutchings68e7f452009-04-29 08:05:08 +00001794 /* Convert phy_id from older PRTAD/DEVAD format */
1795 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1796 (data->phy_id & 0xfc00) == 0x0400)
1797 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1798
1799 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001800}
1801
1802/**************************************************************************
1803 *
1804 * NAPI interface
1805 *
1806 **************************************************************************/
1807
Ben Hutchings7f967c02012-02-13 23:45:02 +00001808static void efx_init_napi_channel(struct efx_channel *channel)
1809{
1810 struct efx_nic *efx = channel->efx;
1811
1812 channel->napi_dev = efx->net_dev;
1813 netif_napi_add(channel->napi_dev, &channel->napi_str,
1814 efx_poll, napi_weight);
1815}
1816
Ben Hutchingse8f14992010-12-07 19:47:34 +00001817static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001818{
1819 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001820
Ben Hutchings7f967c02012-02-13 23:45:02 +00001821 efx_for_each_channel(channel, efx)
1822 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001823}
1824
1825static void efx_fini_napi_channel(struct efx_channel *channel)
1826{
1827 if (channel->napi_dev)
1828 netif_napi_del(&channel->napi_str);
1829 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001830}
1831
1832static void efx_fini_napi(struct efx_nic *efx)
1833{
1834 struct efx_channel *channel;
1835
Ben Hutchingse8f14992010-12-07 19:47:34 +00001836 efx_for_each_channel(channel, efx)
1837 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001838}
1839
1840/**************************************************************************
1841 *
1842 * Kernel netpoll interface
1843 *
1844 *************************************************************************/
1845
1846#ifdef CONFIG_NET_POLL_CONTROLLER
1847
1848/* Although in the common case interrupts will be disabled, this is not
1849 * guaranteed. However, all our work happens inside the NAPI callback,
1850 * so no locking is required.
1851 */
1852static void efx_netpoll(struct net_device *net_dev)
1853{
Ben Hutchings767e4682008-09-01 12:43:14 +01001854 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001855 struct efx_channel *channel;
1856
Ben Hutchings64ee3122008-09-01 12:47:38 +01001857 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001858 efx_schedule_channel(channel);
1859}
1860
1861#endif
1862
1863/**************************************************************************
1864 *
1865 * Kernel net device interface
1866 *
1867 *************************************************************************/
1868
1869/* Context: process, rtnl_lock() held. */
1870static int efx_net_open(struct net_device *net_dev)
1871{
Ben Hutchings767e4682008-09-01 12:43:14 +01001872 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001873 int rc;
1874
Ben Hutchings62776d02010-06-23 11:30:07 +00001875 netif_dbg(efx, ifup, efx->net_dev, "opening device 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 rc = efx_check_disabled(efx);
1879 if (rc)
1880 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001881 if (efx->phy_mode & PHY_MODE_SPECIAL)
1882 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001883 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1884 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001885
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001886 /* Notify the kernel of the link state polled during driver load,
1887 * before the monitor starts running */
1888 efx_link_status_changed(efx);
1889
Ben Hutchings8ceee662008-04-27 12:55:59 +01001890 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001891 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001892 return 0;
1893}
1894
1895/* Context: process, rtnl_lock() held.
1896 * Note that the kernel will ignore our return code; this method
1897 * should really be a void.
1898 */
1899static int efx_net_stop(struct net_device *net_dev)
1900{
Ben Hutchings767e4682008-09-01 12:43:14 +01001901 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001902
Ben Hutchings62776d02010-06-23 11:30:07 +00001903 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1904 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001905
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001906 /* Stop the device and flush all the channels */
1907 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001908
1909 return 0;
1910}
1911
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001912/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001913static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1914 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001915{
Ben Hutchings767e4682008-09-01 12:43:14 +01001916 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001917 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001918
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001919 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001920
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001921 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001922
1923 stats->rx_packets = mac_stats->rx_packets;
1924 stats->tx_packets = mac_stats->tx_packets;
1925 stats->rx_bytes = mac_stats->rx_bytes;
1926 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001927 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001928 stats->multicast = mac_stats->rx_multicast;
1929 stats->collisions = mac_stats->tx_collision;
1930 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1931 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001932 stats->rx_crc_errors = mac_stats->rx_bad;
1933 stats->rx_frame_errors = mac_stats->rx_align_error;
1934 stats->rx_fifo_errors = mac_stats->rx_overflow;
1935 stats->rx_missed_errors = mac_stats->rx_missed;
1936 stats->tx_window_errors = mac_stats->tx_late_collision;
1937
1938 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001939 stats->rx_crc_errors +
1940 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001941 mac_stats->rx_symbol_error);
1942 stats->tx_errors = (stats->tx_window_errors +
1943 mac_stats->tx_bad);
1944
Ben Hutchings1cb34522011-09-02 23:23:00 +01001945 spin_unlock_bh(&efx->stats_lock);
1946
Ben Hutchings8ceee662008-04-27 12:55:59 +01001947 return stats;
1948}
1949
1950/* Context: netif_tx_lock held, BHs disabled. */
1951static void efx_watchdog(struct net_device *net_dev)
1952{
Ben Hutchings767e4682008-09-01 12:43:14 +01001953 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001954
Ben Hutchings62776d02010-06-23 11:30:07 +00001955 netif_err(efx, tx_err, efx->net_dev,
1956 "TX stuck with port_enabled=%d: resetting channels\n",
1957 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001958
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001959 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001960}
1961
1962
1963/* Context: process, rtnl_lock() held. */
1964static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1965{
Ben Hutchings767e4682008-09-01 12:43:14 +01001966 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001967 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001968
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001969 rc = efx_check_disabled(efx);
1970 if (rc)
1971 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001972 if (new_mtu > EFX_MAX_MTU)
1973 return -EINVAL;
1974
Ben Hutchings62776d02010-06-23 11:30:07 +00001975 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001976
Ben Hutchings29c69a42013-01-28 19:01:06 +00001977 efx_device_detach_sync(efx);
1978 efx_stop_all(efx);
1979
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001980 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001981 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001982 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001983 mutex_unlock(&efx->mac_lock);
1984
Ben Hutchings8ceee662008-04-27 12:55:59 +01001985 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00001986 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001987 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001988}
1989
1990static int efx_set_mac_address(struct net_device *net_dev, void *data)
1991{
Ben Hutchings767e4682008-09-01 12:43:14 +01001992 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001993 struct sockaddr *addr = data;
1994 char *new_addr = addr->sa_data;
1995
Ben Hutchings8ceee662008-04-27 12:55:59 +01001996 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001997 netif_err(efx, drv, efx->net_dev,
1998 "invalid ethernet MAC address requested: %pM\n",
1999 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002000 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002001 }
2002
2003 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002004 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002005
2006 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002007 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002008 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002009 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002010
2011 return 0;
2012}
2013
Ben Hutchingsa816f752008-09-01 12:49:12 +01002014/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002015static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002016{
Ben Hutchings767e4682008-09-01 12:43:14 +01002017 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002018 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002019 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002020 u32 crc;
2021 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002022
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002023 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002024
2025 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002026 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002027 memset(mc_hash, 0xff, sizeof(*mc_hash));
2028 } else {
2029 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00002030 netdev_for_each_mc_addr(ha, net_dev) {
2031 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002032 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
Ben Hutchings32766ec2012-10-04 17:13:03 -07002033 __set_bit_le(bit, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002034 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002035
2036 /* Broadcast packets go through the multicast hash filter.
2037 * ether_crc_le() of the broadcast address is 0xbe2612ff
2038 * so we always add bit 0xff to the mask.
2039 */
Ben Hutchings32766ec2012-10-04 17:13:03 -07002040 __set_bit_le(0xff, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002041 }
2042
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002043 if (efx->port_enabled)
2044 queue_work(efx->workqueue, &efx->mac_work);
2045 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002046}
2047
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002048static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002049{
2050 struct efx_nic *efx = netdev_priv(net_dev);
2051
2052 /* If disabling RX n-tuple filtering, clear existing filters */
2053 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2054 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2055
2056 return 0;
2057}
2058
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002059static const struct net_device_ops efx_netdev_ops = {
2060 .ndo_open = efx_net_open,
2061 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002062 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002063 .ndo_tx_timeout = efx_watchdog,
2064 .ndo_start_xmit = efx_hard_start_xmit,
2065 .ndo_validate_addr = eth_validate_addr,
2066 .ndo_do_ioctl = efx_ioctl,
2067 .ndo_change_mtu = efx_change_mtu,
2068 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002069 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002070 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002071#ifdef CONFIG_SFC_SRIOV
2072 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2073 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2074 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2075 .ndo_get_vf_config = efx_sriov_get_vf_config,
2076#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002077#ifdef CONFIG_NET_POLL_CONTROLLER
2078 .ndo_poll_controller = efx_netpoll,
2079#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002080 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002081#ifdef CONFIG_RFS_ACCEL
2082 .ndo_rx_flow_steer = efx_filter_rfs,
2083#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002084};
2085
Ben Hutchings7dde5962008-12-12 22:09:38 -08002086static void efx_update_name(struct efx_nic *efx)
2087{
2088 strcpy(efx->name, efx->net_dev->name);
2089 efx_mtd_rename(efx);
2090 efx_set_channel_names(efx);
2091}
2092
Ben Hutchings8ceee662008-04-27 12:55:59 +01002093static int efx_netdev_event(struct notifier_block *this,
2094 unsigned long event, void *ptr)
2095{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01002096 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002097
Ben Hutchings7dde5962008-12-12 22:09:38 -08002098 if (net_dev->netdev_ops == &efx_netdev_ops &&
2099 event == NETDEV_CHANGENAME)
2100 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002101
2102 return NOTIFY_DONE;
2103}
2104
2105static struct notifier_block efx_netdev_notifier = {
2106 .notifier_call = efx_netdev_event,
2107};
2108
Ben Hutchings06d5e192008-12-12 21:47:23 -08002109static ssize_t
2110show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2111{
2112 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2113 return sprintf(buf, "%d\n", efx->phy_type);
2114}
2115static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
2116
Ben Hutchings8ceee662008-04-27 12:55:59 +01002117static int efx_register_netdev(struct efx_nic *efx)
2118{
2119 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002120 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002121 int rc;
2122
2123 net_dev->watchdog_timeo = 5 * HZ;
2124 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002125 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002126 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002127 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002128
Ben Hutchings7dde5962008-12-12 22:09:38 -08002129 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002130
Ben Hutchings7153f622012-07-27 20:50:52 +01002131 /* Enable resets to be scheduled and check whether any were
2132 * already requested. If so, the NIC is probably hosed so we
2133 * abort.
2134 */
2135 efx->state = STATE_READY;
2136 smp_mb(); /* ensure we change state before checking reset_pending */
2137 if (efx->reset_pending) {
2138 netif_err(efx, probe, efx->net_dev,
2139 "aborting probe due to scheduled reset\n");
2140 rc = -EIO;
2141 goto fail_locked;
2142 }
2143
Ben Hutchingsaed06282009-08-26 08:16:27 +00002144 rc = dev_alloc_name(net_dev, net_dev->name);
2145 if (rc < 0)
2146 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002147 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002148
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002149 /* Always start with carrier off; PHY events will detect the link */
2150 netif_carrier_off(net_dev);
2151
Ben Hutchingsaed06282009-08-26 08:16:27 +00002152 rc = register_netdevice(net_dev);
2153 if (rc)
2154 goto fail_locked;
2155
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002156 efx_for_each_channel(channel, efx) {
2157 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002158 efx_for_each_channel_tx_queue(tx_queue, channel)
2159 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002160 }
2161
Ben Hutchings7dde5962008-12-12 22:09:38 -08002162 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002163
Ben Hutchings06d5e192008-12-12 21:47:23 -08002164 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2165 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002166 netif_err(efx, drv, efx->net_dev,
2167 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002168 goto fail_registered;
2169 }
2170
Ben Hutchings8ceee662008-04-27 12:55:59 +01002171 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002172
Ben Hutchings7153f622012-07-27 20:50:52 +01002173fail_registered:
2174 rtnl_lock();
2175 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002176fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002177 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002178 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002179 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002180 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002181}
2182
2183static void efx_unregister_netdev(struct efx_nic *efx)
2184{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002185 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002186 struct efx_tx_queue *tx_queue;
2187
2188 if (!efx->net_dev)
2189 return;
2190
Ben Hutchings767e4682008-09-01 12:43:14 +01002191 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002192
2193 /* Free up any skbs still remaining. This has to happen before
2194 * we try to unregister the netdev as running their destructors
2195 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002196 efx_for_each_channel(channel, efx) {
2197 efx_for_each_channel_tx_queue(tx_queue, channel)
2198 efx_release_tx_buffers(tx_queue);
2199 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002200
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002201 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2202 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002203
2204 rtnl_lock();
2205 unregister_netdevice(efx->net_dev);
2206 efx->state = STATE_UNINIT;
2207 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002208}
2209
2210/**************************************************************************
2211 *
2212 * Device reset and suspend
2213 *
2214 **************************************************************************/
2215
Ben Hutchings2467ca42008-09-01 12:48:50 +01002216/* Tears down the entire software state and most of the hardware state
2217 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002218void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002219{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002220 EFX_ASSERT_RESET_SERIALISED(efx);
2221
Ben Hutchings2467ca42008-09-01 12:48:50 +01002222 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00002223 efx_stop_interrupts(efx, false);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002224
2225 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002226 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2227 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002228 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002229}
2230
Ben Hutchings2467ca42008-09-01 12:48:50 +01002231/* This function will always ensure that the locks acquired in
2232 * efx_reset_down() are released. A failure return code indicates
2233 * that we were unable to reinitialise the hardware, and the
2234 * driver should be disabled. If ok is false, then the rx and tx
2235 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002236int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002237{
2238 int rc;
2239
Ben Hutchings2467ca42008-09-01 12:48:50 +01002240 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002241
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002242 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002243 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002244 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002245 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002246 }
2247
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002248 if (!ok)
2249 goto fail;
2250
Steve Hodgson4b988282009-01-29 17:50:51 +00002251 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002252 rc = efx->phy_op->init(efx);
2253 if (rc)
2254 goto fail;
2255 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002256 netif_err(efx, drv, efx->net_dev,
2257 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002258 }
2259
Ben Hutchings710b2082011-09-03 00:15:00 +01002260 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002261
Ben Hutchings7f967c02012-02-13 23:45:02 +00002262 efx_start_interrupts(efx, false);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002263 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002264 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002265
2266 mutex_unlock(&efx->mac_lock);
2267
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002268 efx_start_all(efx);
2269
2270 return 0;
2271
2272fail:
2273 efx->port_initialized = false;
2274
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002275 mutex_unlock(&efx->mac_lock);
2276
Ben Hutchings8ceee662008-04-27 12:55:59 +01002277 return rc;
2278}
2279
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002280/* Reset the NIC using the specified method. Note that the reset may
2281 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002282 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002283 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002284 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002285int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002286{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002287 int rc, rc2;
2288 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002289
Ben Hutchings62776d02010-06-23 11:30:07 +00002290 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2291 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002292
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002293 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002294 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002295
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002296 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002297 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002298 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002299 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002300 }
2301
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002302 /* Clear flags for the scopes we covered. We assume the NIC and
2303 * driver are now quiescent so that there is no race here.
2304 */
2305 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002306
2307 /* Reinitialise bus-mastering, which may have been turned off before
2308 * the reset was scheduled. This is still appropriate, even in the
2309 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2310 * can respond to requests. */
2311 pci_set_master(efx->pci_dev);
2312
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002313out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002314 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002315 disabled = rc ||
2316 method == RESET_TYPE_DISABLE ||
2317 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002318 rc2 = efx_reset_up(efx, method, !disabled);
2319 if (rc2) {
2320 disabled = true;
2321 if (!rc)
2322 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002323 }
2324
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002325 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002326 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002327 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002328 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002329 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002330 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002331 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002332 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002333 return rc;
2334}
2335
Alexandre Rames626950d2013-01-14 17:20:22 +00002336/* Try recovery mechanisms.
2337 * For now only EEH is supported.
2338 * Returns 0 if the recovery mechanisms are unsuccessful.
2339 * Returns a non-zero value otherwise.
2340 */
2341static int efx_try_recovery(struct efx_nic *efx)
2342{
2343#ifdef CONFIG_EEH
2344 /* A PCI error can occur and not be seen by EEH because nothing
2345 * happens on the PCI bus. In this case the driver may fail and
2346 * schedule a 'recover or reset', leading to this recovery handler.
2347 * Manually call the eeh failure check function.
2348 */
2349 struct eeh_dev *eehdev =
2350 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2351
2352 if (eeh_dev_check_failure(eehdev)) {
2353 /* The EEH mechanisms will handle the error and reset the
2354 * device if necessary.
2355 */
2356 return 1;
2357 }
2358#endif
2359 return 0;
2360}
2361
Ben Hutchings8ceee662008-04-27 12:55:59 +01002362/* The worker thread exists so that code that cannot sleep can
2363 * schedule a reset for later.
2364 */
2365static void efx_reset_work(struct work_struct *data)
2366{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002367 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002368 unsigned long pending;
2369 enum reset_type method;
2370
2371 pending = ACCESS_ONCE(efx->reset_pending);
2372 method = fls(pending) - 1;
2373
2374 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2375 method == RESET_TYPE_RECOVER_OR_ALL) &&
2376 efx_try_recovery(efx))
2377 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002378
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002379 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002380 return;
2381
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002382 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002383
2384 /* We checked the state in efx_schedule_reset() but it may
2385 * have changed by now. Now that we have the RTNL lock,
2386 * it cannot change again.
2387 */
2388 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002389 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002390
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002391 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002392}
2393
2394void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2395{
2396 enum reset_type method;
2397
Alexandre Rames626950d2013-01-14 17:20:22 +00002398 if (efx->state == STATE_RECOVERY) {
2399 netif_dbg(efx, drv, efx->net_dev,
2400 "recovering: skip scheduling %s reset\n",
2401 RESET_TYPE(type));
2402 return;
2403 }
2404
Ben Hutchings8ceee662008-04-27 12:55:59 +01002405 switch (type) {
2406 case RESET_TYPE_INVISIBLE:
2407 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002408 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002409 case RESET_TYPE_WORLD:
2410 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002411 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002412 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002413 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2414 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002415 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002416 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002417 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002418 netif_dbg(efx, drv, efx->net_dev,
2419 "scheduling %s reset for %s\n",
2420 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002421 break;
2422 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002423
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002424 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002425 smp_mb(); /* ensure we change reset_pending before checking state */
2426
2427 /* If we're not READY then just leave the flags set as the cue
2428 * to abort probing or reschedule the reset later.
2429 */
2430 if (ACCESS_ONCE(efx->state) != STATE_READY)
2431 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002432
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002433 /* efx_process_channel() will no longer read events once a
2434 * reset is scheduled. So switch back to poll'd MCDI completions. */
2435 efx_mcdi_mode_poll(efx);
2436
Steve Hodgson1ab00622008-12-12 21:33:02 -08002437 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002438}
2439
2440/**************************************************************************
2441 *
2442 * List of NICs we support
2443 *
2444 **************************************************************************/
2445
2446/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002447static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002448 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2449 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002450 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002451 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2452 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002453 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002454 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002455 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002456 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002457 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002458 {0} /* end of list */
2459};
2460
2461/**************************************************************************
2462 *
Ben Hutchings37594332009-11-23 16:05:45 +00002463 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002464 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002465 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002466 * Needed so all function pointers are valid and do not have to be tested
2467 * before use
2468 *
2469 **************************************************************************/
2470int efx_port_dummy_op_int(struct efx_nic *efx)
2471{
2472 return 0;
2473}
2474void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002475
2476static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002477{
2478 return false;
2479}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002480
stephen hemminger6c8c2512011-04-14 05:50:12 +00002481static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002482 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002483 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002484 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002485 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002486};
2487
Ben Hutchings8ceee662008-04-27 12:55:59 +01002488/**************************************************************************
2489 *
2490 * Data housekeeping
2491 *
2492 **************************************************************************/
2493
2494/* This zeroes out and then fills in the invariants in a struct
2495 * efx_nic (including all sub-structures).
2496 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002497static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002498 struct pci_dev *pci_dev, struct net_device *net_dev)
2499{
Ben Hutchings46426102010-09-10 06:42:33 +00002500 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002501
2502 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002503 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002504#ifdef CONFIG_SFC_MTD
2505 INIT_LIST_HEAD(&efx->mtd_list);
2506#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002507 INIT_WORK(&efx->reset_work, efx_reset_work);
2508 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002509 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002510 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002511 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002512 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002513 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002514
2515 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002516 spin_lock_init(&efx->stats_lock);
2517 mutex_init(&efx->mac_lock);
2518 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002519 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002520 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002521 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002522
2523 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002524 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2525 if (!efx->channel[i])
2526 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002527 }
2528
Ben Hutchings8ceee662008-04-27 12:55:59 +01002529 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2530
2531 /* Higher numbered interrupt modes are less capable! */
2532 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2533 interrupt_mode);
2534
Ben Hutchings6977dc62008-12-26 13:44:39 -08002535 /* Would be good to use the net_dev name, but we're too early */
2536 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2537 pci_name(pci_dev));
2538 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002539 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002540 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002541
Ben Hutchings8ceee662008-04-27 12:55:59 +01002542 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002543
2544fail:
2545 efx_fini_struct(efx);
2546 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002547}
2548
2549static void efx_fini_struct(struct efx_nic *efx)
2550{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002551 int i;
2552
2553 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2554 kfree(efx->channel[i]);
2555
Ben Hutchings8ceee662008-04-27 12:55:59 +01002556 if (efx->workqueue) {
2557 destroy_workqueue(efx->workqueue);
2558 efx->workqueue = NULL;
2559 }
2560}
2561
2562/**************************************************************************
2563 *
2564 * PCI interface
2565 *
2566 **************************************************************************/
2567
2568/* Main body of final NIC shutdown code
2569 * This is called only at module unload (or hotplug removal).
2570 */
2571static void efx_pci_remove_main(struct efx_nic *efx)
2572{
Ben Hutchings7153f622012-07-27 20:50:52 +01002573 /* Flush reset_work. It can no longer be scheduled since we
2574 * are not READY.
2575 */
2576 BUG_ON(efx->state == STATE_READY);
2577 cancel_work_sync(&efx->reset_work);
2578
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002579#ifdef CONFIG_RFS_ACCEL
2580 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2581 efx->net_dev->rx_cpu_rmap = NULL;
2582#endif
Ben Hutchings7f967c02012-02-13 23:45:02 +00002583 efx_stop_interrupts(efx, false);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002584 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002585 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002586 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002587 efx_fini_napi(efx);
2588 efx_remove_all(efx);
2589}
2590
2591/* Final NIC shutdown
2592 * This is called only at module unload (or hotplug removal).
2593 */
2594static void efx_pci_remove(struct pci_dev *pci_dev)
2595{
2596 struct efx_nic *efx;
2597
2598 efx = pci_get_drvdata(pci_dev);
2599 if (!efx)
2600 return;
2601
2602 /* Mark the NIC as fini, then stop the interface */
2603 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002604 dev_close(efx->net_dev);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002605 efx_stop_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002606 rtnl_unlock();
2607
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002608 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002609 efx_unregister_netdev(efx);
2610
Ben Hutchings7dde5962008-12-12 22:09:38 -08002611 efx_mtd_remove(efx);
2612
Ben Hutchings8ceee662008-04-27 12:55:59 +01002613 efx_pci_remove_main(efx);
2614
Ben Hutchings8ceee662008-04-27 12:55:59 +01002615 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002616 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002617
Ben Hutchings8ceee662008-04-27 12:55:59 +01002618 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002619 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002620 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002621
2622 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002623};
2624
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002625/* NIC VPD information
2626 * Called during probe to display the part number of the
2627 * installed NIC. VPD is potentially very large but this should
2628 * always appear within the first 512 bytes.
2629 */
2630#define SFC_VPD_LEN 512
2631static void efx_print_product_vpd(struct efx_nic *efx)
2632{
2633 struct pci_dev *dev = efx->pci_dev;
2634 char vpd_data[SFC_VPD_LEN];
2635 ssize_t vpd_size;
2636 int i, j;
2637
2638 /* Get the vpd data from the device */
2639 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2640 if (vpd_size <= 0) {
2641 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2642 return;
2643 }
2644
2645 /* Get the Read only section */
2646 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2647 if (i < 0) {
2648 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2649 return;
2650 }
2651
2652 j = pci_vpd_lrdt_size(&vpd_data[i]);
2653 i += PCI_VPD_LRDT_TAG_SIZE;
2654 if (i + j > vpd_size)
2655 j = vpd_size - i;
2656
2657 /* Get the Part number */
2658 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2659 if (i < 0) {
2660 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2661 return;
2662 }
2663
2664 j = pci_vpd_info_field_size(&vpd_data[i]);
2665 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2666 if (i + j > vpd_size) {
2667 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2668 return;
2669 }
2670
2671 netif_info(efx, drv, efx->net_dev,
2672 "Part Number : %.*s\n", j, &vpd_data[i]);
2673}
2674
2675
Ben Hutchings8ceee662008-04-27 12:55:59 +01002676/* Main body of NIC initialisation
2677 * This is called at module load (or hotplug insertion, theoretically).
2678 */
2679static int efx_pci_probe_main(struct efx_nic *efx)
2680{
2681 int rc;
2682
2683 /* Do start-of-day initialisation */
2684 rc = efx_probe_all(efx);
2685 if (rc)
2686 goto fail1;
2687
Ben Hutchingse8f14992010-12-07 19:47:34 +00002688 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002689
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002690 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002691 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002692 netif_err(efx, probe, efx->net_dev,
2693 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002694 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002695 }
2696
2697 rc = efx_init_port(efx);
2698 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002699 netif_err(efx, probe, efx->net_dev,
2700 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002701 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002702 }
2703
Ben Hutchings152b6a62009-11-29 03:43:56 +00002704 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002705 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002706 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00002707 efx_start_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002708
2709 return 0;
2710
Ben Hutchings278c0622009-11-23 16:05:12 +00002711 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002712 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002713 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002714 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002715 fail3:
2716 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002717 efx_remove_all(efx);
2718 fail1:
2719 return rc;
2720}
2721
2722/* NIC initialisation
2723 *
2724 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002725 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002726 * sets up and registers the network devices with the kernel and hooks
2727 * the interrupt service routine. It does not prepare the device for
2728 * transmission; this is left to the first time one of the network
2729 * interfaces is brought up (i.e. efx_net_open).
2730 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002731static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002732 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002733{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002734 struct net_device *net_dev;
2735 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002736 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002737
2738 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002739 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2740 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002741 if (!net_dev)
2742 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002743 efx = netdev_priv(net_dev);
2744 efx->type = (const struct efx_nic_type *) entry->driver_data;
2745 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002746 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002747 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002748 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002749 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002750 /* Mask for features that also apply to VLAN devices */
2751 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002752 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2753 NETIF_F_RXCSUM);
2754 /* All offloads can be toggled */
2755 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002756 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002757 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002758 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002759 if (rc)
2760 goto fail1;
2761
Ben Hutchings62776d02010-06-23 11:30:07 +00002762 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002763 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002764
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002765 efx_print_product_vpd(efx);
2766
Ben Hutchings8ceee662008-04-27 12:55:59 +01002767 /* Set up basic I/O (BAR mappings etc) */
2768 rc = efx_init_io(efx);
2769 if (rc)
2770 goto fail2;
2771
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002772 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002773 if (rc)
2774 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002775
Ben Hutchings8ceee662008-04-27 12:55:59 +01002776 rc = efx_register_netdev(efx);
2777 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002778 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002779
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002780 rc = efx_sriov_init(efx);
2781 if (rc)
2782 netif_err(efx, probe, efx->net_dev,
2783 "SR-IOV can't be enabled rc %d\n", rc);
2784
Ben Hutchings62776d02010-06-23 11:30:07 +00002785 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002786
Ben Hutchings7c431612012-01-27 17:23:58 +00002787 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002788 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002789 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002790 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002791 if (rc)
2792 netif_warn(efx, probe, efx->net_dev,
2793 "failed to create MTDs (%d)\n", rc);
2794
Alexandre Rames626950d2013-01-14 17:20:22 +00002795 rc = pci_enable_pcie_error_reporting(pci_dev);
2796 if (rc && rc != -EINVAL)
2797 netif_warn(efx, probe, efx->net_dev,
2798 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2799
Ben Hutchings8ceee662008-04-27 12:55:59 +01002800 return 0;
2801
Ben Hutchings8ceee662008-04-27 12:55:59 +01002802 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002803 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002804 fail3:
2805 efx_fini_io(efx);
2806 fail2:
2807 efx_fini_struct(efx);
2808 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002809 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002810 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002811 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002812 free_netdev(net_dev);
2813 return rc;
2814}
2815
Ben Hutchings89c758f2009-11-29 03:43:07 +00002816static int efx_pm_freeze(struct device *dev)
2817{
2818 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2819
Ben Hutchings61da0262012-07-27 19:35:39 +01002820 rtnl_lock();
2821
Ben Hutchings6032fb52012-07-27 19:35:47 +01002822 if (efx->state != STATE_DISABLED) {
2823 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002824
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002825 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002826
Ben Hutchings6032fb52012-07-27 19:35:47 +01002827 efx_stop_all(efx);
2828 efx_stop_interrupts(efx, false);
2829 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002830
Ben Hutchings61da0262012-07-27 19:35:39 +01002831 rtnl_unlock();
2832
Ben Hutchings89c758f2009-11-29 03:43:07 +00002833 return 0;
2834}
2835
2836static int efx_pm_thaw(struct device *dev)
2837{
2838 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2839
Ben Hutchings61da0262012-07-27 19:35:39 +01002840 rtnl_lock();
2841
Ben Hutchings6032fb52012-07-27 19:35:47 +01002842 if (efx->state != STATE_DISABLED) {
2843 efx_start_interrupts(efx, false);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002844
Ben Hutchings6032fb52012-07-27 19:35:47 +01002845 mutex_lock(&efx->mac_lock);
2846 efx->phy_op->reconfigure(efx);
2847 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002848
Ben Hutchings6032fb52012-07-27 19:35:47 +01002849 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002850
Ben Hutchings6032fb52012-07-27 19:35:47 +01002851 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002852
Ben Hutchings6032fb52012-07-27 19:35:47 +01002853 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002854
Ben Hutchings6032fb52012-07-27 19:35:47 +01002855 efx->type->resume_wol(efx);
2856 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002857
Ben Hutchings61da0262012-07-27 19:35:39 +01002858 rtnl_unlock();
2859
Steve Hodgson319ba642010-06-01 11:17:24 +00002860 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2861 queue_work(reset_workqueue, &efx->reset_work);
2862
Ben Hutchings89c758f2009-11-29 03:43:07 +00002863 return 0;
2864}
2865
2866static int efx_pm_poweroff(struct device *dev)
2867{
2868 struct pci_dev *pci_dev = to_pci_dev(dev);
2869 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2870
2871 efx->type->fini(efx);
2872
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002873 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002874
2875 pci_save_state(pci_dev);
2876 return pci_set_power_state(pci_dev, PCI_D3hot);
2877}
2878
2879/* Used for both resume and restore */
2880static int efx_pm_resume(struct device *dev)
2881{
2882 struct pci_dev *pci_dev = to_pci_dev(dev);
2883 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2884 int rc;
2885
2886 rc = pci_set_power_state(pci_dev, PCI_D0);
2887 if (rc)
2888 return rc;
2889 pci_restore_state(pci_dev);
2890 rc = pci_enable_device(pci_dev);
2891 if (rc)
2892 return rc;
2893 pci_set_master(efx->pci_dev);
2894 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2895 if (rc)
2896 return rc;
2897 rc = efx->type->init(efx);
2898 if (rc)
2899 return rc;
2900 efx_pm_thaw(dev);
2901 return 0;
2902}
2903
2904static int efx_pm_suspend(struct device *dev)
2905{
2906 int rc;
2907
2908 efx_pm_freeze(dev);
2909 rc = efx_pm_poweroff(dev);
2910 if (rc)
2911 efx_pm_resume(dev);
2912 return rc;
2913}
2914
Ben Hutchings18e83e42012-01-05 19:05:20 +00002915static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002916 .suspend = efx_pm_suspend,
2917 .resume = efx_pm_resume,
2918 .freeze = efx_pm_freeze,
2919 .thaw = efx_pm_thaw,
2920 .poweroff = efx_pm_poweroff,
2921 .restore = efx_pm_resume,
2922};
2923
Alexandre Rames626950d2013-01-14 17:20:22 +00002924/* A PCI error affecting this device was detected.
2925 * At this point MMIO and DMA may be disabled.
2926 * Stop the software path and request a slot reset.
2927 */
2928pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2929 enum pci_channel_state state)
2930{
2931 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2932 struct efx_nic *efx = pci_get_drvdata(pdev);
2933
2934 if (state == pci_channel_io_perm_failure)
2935 return PCI_ERS_RESULT_DISCONNECT;
2936
2937 rtnl_lock();
2938
2939 if (efx->state != STATE_DISABLED) {
2940 efx->state = STATE_RECOVERY;
2941 efx->reset_pending = 0;
2942
2943 efx_device_detach_sync(efx);
2944
2945 efx_stop_all(efx);
2946 efx_stop_interrupts(efx, false);
2947
2948 status = PCI_ERS_RESULT_NEED_RESET;
2949 } else {
2950 /* If the interface is disabled we don't want to do anything
2951 * with it.
2952 */
2953 status = PCI_ERS_RESULT_RECOVERED;
2954 }
2955
2956 rtnl_unlock();
2957
2958 pci_disable_device(pdev);
2959
2960 return status;
2961}
2962
2963/* Fake a successfull reset, which will be performed later in efx_io_resume. */
2964pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
2965{
2966 struct efx_nic *efx = pci_get_drvdata(pdev);
2967 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2968 int rc;
2969
2970 if (pci_enable_device(pdev)) {
2971 netif_err(efx, hw, efx->net_dev,
2972 "Cannot re-enable PCI device after reset.\n");
2973 status = PCI_ERS_RESULT_DISCONNECT;
2974 }
2975
2976 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2977 if (rc) {
2978 netif_err(efx, hw, efx->net_dev,
2979 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2980 /* Non-fatal error. Continue. */
2981 }
2982
2983 return status;
2984}
2985
2986/* Perform the actual reset and resume I/O operations. */
2987static void efx_io_resume(struct pci_dev *pdev)
2988{
2989 struct efx_nic *efx = pci_get_drvdata(pdev);
2990 int rc;
2991
2992 rtnl_lock();
2993
2994 if (efx->state == STATE_DISABLED)
2995 goto out;
2996
2997 rc = efx_reset(efx, RESET_TYPE_ALL);
2998 if (rc) {
2999 netif_err(efx, hw, efx->net_dev,
3000 "efx_reset failed after PCI error (%d)\n", rc);
3001 } else {
3002 efx->state = STATE_READY;
3003 netif_dbg(efx, hw, efx->net_dev,
3004 "Done resetting and resuming IO after PCI error.\n");
3005 }
3006
3007out:
3008 rtnl_unlock();
3009}
3010
3011/* For simplicity and reliability, we always require a slot reset and try to
3012 * reset the hardware when a pci error affecting the device is detected.
3013 * We leave both the link_reset and mmio_enabled callback unimplemented:
3014 * with our request for slot reset the mmio_enabled callback will never be
3015 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3016 */
3017static struct pci_error_handlers efx_err_handlers = {
3018 .error_detected = efx_io_error_detected,
3019 .slot_reset = efx_io_slot_reset,
3020 .resume = efx_io_resume,
3021};
3022
Ben Hutchings8ceee662008-04-27 12:55:59 +01003023static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003024 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003025 .id_table = efx_pci_table,
3026 .probe = efx_pci_probe,
3027 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003028 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003029 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003030};
3031
3032/**************************************************************************
3033 *
3034 * Kernel module interface
3035 *
3036 *************************************************************************/
3037
3038module_param(interrupt_mode, uint, 0444);
3039MODULE_PARM_DESC(interrupt_mode,
3040 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3041
3042static int __init efx_init_module(void)
3043{
3044 int rc;
3045
3046 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3047
3048 rc = register_netdevice_notifier(&efx_netdev_notifier);
3049 if (rc)
3050 goto err_notifier;
3051
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003052 rc = efx_init_sriov();
3053 if (rc)
3054 goto err_sriov;
3055
Steve Hodgson1ab00622008-12-12 21:33:02 -08003056 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3057 if (!reset_workqueue) {
3058 rc = -ENOMEM;
3059 goto err_reset;
3060 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003061
3062 rc = pci_register_driver(&efx_pci_driver);
3063 if (rc < 0)
3064 goto err_pci;
3065
3066 return 0;
3067
3068 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003069 destroy_workqueue(reset_workqueue);
3070 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003071 efx_fini_sriov();
3072 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003073 unregister_netdevice_notifier(&efx_netdev_notifier);
3074 err_notifier:
3075 return rc;
3076}
3077
3078static void __exit efx_exit_module(void)
3079{
3080 printk(KERN_INFO "Solarflare NET driver unloading\n");
3081
3082 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003083 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003084 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003085 unregister_netdevice_notifier(&efx_netdev_notifier);
3086
3087}
3088
3089module_init(efx_init_module);
3090module_exit(efx_exit_module);
3091
Ben Hutchings906bb262009-11-29 15:16:19 +00003092MODULE_AUTHOR("Solarflare Communications and "
3093 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003094MODULE_DESCRIPTION("Solarflare Communications network driver");
3095MODULE_LICENSE("GPL");
3096MODULE_DEVICE_TABLE(pci, efx_pci_table);