blob: 5e1ddc559b4f0c64d5c07de25e840a17a9fecdf9 [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 */
642 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
643 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
Ben Hutchings39c9cf02010-06-23 11:31:28 +0000644 efx->type->rx_buffer_hash_size +
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100645 efx->type->rx_buffer_padding);
Steve Hodgson62b330b2010-06-01 11:20:53 +0000646 efx->rx_buffer_order = get_order(efx->rx_buffer_len +
647 sizeof(struct efx_rx_page_state));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100648
Ben Hutchings14bf7182012-05-22 01:27:58 +0100649 /* We must keep at least one descriptor in a TX ring empty.
650 * We could avoid this when the queue size does not exactly
651 * match the hardware ring size, but it's not that important.
652 * Therefore we stop the queue when one more skb might fill
653 * the ring completely. We wake it when half way back to
654 * empty.
655 */
656 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
657 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
658
Ben Hutchings8ceee662008-04-27 12:55:59 +0100659 /* Initialise the channels */
660 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100661 efx_for_each_channel_tx_queue(tx_queue, channel)
662 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100663
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000664 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100665 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000666 efx_nic_generate_fill_event(rx_queue);
667 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100668
669 WARN_ON(channel->rx_pkt != NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100670 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000671
672 if (netif_device_present(efx->net_dev))
673 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100674}
675
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000676static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100677{
678 struct efx_channel *channel;
679 struct efx_tx_queue *tx_queue;
680 struct efx_rx_queue *rx_queue;
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100681 struct pci_dev *dev = efx->pci_dev;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100682 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100683
684 EFX_ASSERT_RESET_SERIALISED(efx);
685 BUG_ON(efx->port_enabled);
686
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100687 /* Only perform flush if dma is enabled */
Alexandre Rames626950d2013-01-14 17:20:22 +0000688 if (dev->is_busmaster && efx->state != STATE_RECOVERY) {
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100689 rc = efx_nic_flush_queues(efx);
690
691 if (rc && EFX_WORKAROUND_7803(efx)) {
692 /* Schedule a reset to recover from the flush failure. The
693 * descriptor caches reference memory we're about to free,
694 * but falcon_reconfigure_mac_wrapper() won't reconnect
695 * the MACs because of the pending reset. */
696 netif_err(efx, drv, efx->net_dev,
697 "Resetting to recover from flush failure\n");
698 efx_schedule_reset(efx, RESET_TYPE_ALL);
699 } else if (rc) {
700 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
701 } else {
702 netif_dbg(efx, drv, efx->net_dev,
703 "successfully flushed all queues\n");
704 }
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000705 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100706
Ben Hutchings8ceee662008-04-27 12:55:59 +0100707 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000708 /* RX packet processing is pipelined, so wait for the
709 * NAPI handler to complete. At least event queue 0
710 * might be kept active by non-data events, so don't
711 * use napi_synchronize() but actually disable NAPI
712 * temporarily.
713 */
714 if (efx_channel_has_rx_queue(channel)) {
715 efx_stop_eventq(channel);
716 efx_start_eventq(channel);
717 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100718
719 efx_for_each_channel_rx_queue(rx_queue, channel)
720 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000721 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100722 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100723 }
724}
725
726static void efx_remove_channel(struct efx_channel *channel)
727{
728 struct efx_tx_queue *tx_queue;
729 struct efx_rx_queue *rx_queue;
730
Ben Hutchings62776d02010-06-23 11:30:07 +0000731 netif_dbg(channel->efx, drv, channel->efx->net_dev,
732 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100733
734 efx_for_each_channel_rx_queue(rx_queue, channel)
735 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000736 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100737 efx_remove_tx_queue(tx_queue);
738 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100739 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100740}
741
Ben Hutchings46426102010-09-10 06:42:33 +0000742static void efx_remove_channels(struct efx_nic *efx)
743{
744 struct efx_channel *channel;
745
746 efx_for_each_channel(channel, efx)
747 efx_remove_channel(channel);
748}
749
750int
751efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
752{
753 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
754 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000755 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100756 int rc;
757
758 rc = efx_check_disabled(efx);
759 if (rc)
760 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000761
762 /* Not all channels should be reallocated. We must avoid
763 * reallocating their buffer table entries.
764 */
765 efx_for_each_channel(channel, efx) {
766 struct efx_rx_queue *rx_queue;
767 struct efx_tx_queue *tx_queue;
768
769 if (channel->type->copy)
770 continue;
771 next_buffer_table = max(next_buffer_table,
772 channel->eventq.index +
773 channel->eventq.entries);
774 efx_for_each_channel_rx_queue(rx_queue, channel)
775 next_buffer_table = max(next_buffer_table,
776 rx_queue->rxd.index +
777 rx_queue->rxd.entries);
778 efx_for_each_channel_tx_queue(tx_queue, channel)
779 next_buffer_table = max(next_buffer_table,
780 tx_queue->txd.index +
781 tx_queue->txd.entries);
782 }
Ben Hutchings46426102010-09-10 06:42:33 +0000783
Ben Hutchings29c69a42013-01-28 19:01:06 +0000784 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000785 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000786 efx_stop_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000787
Ben Hutchings7f967c02012-02-13 23:45:02 +0000788 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000789 memset(other_channel, 0, sizeof(other_channel));
790 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000791 channel = efx->channel[i];
792 if (channel->type->copy)
793 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000794 if (!channel) {
795 rc = -ENOMEM;
796 goto out;
797 }
798 other_channel[i] = channel;
799 }
800
801 /* Swap entry counts and channel pointers */
802 old_rxq_entries = efx->rxq_entries;
803 old_txq_entries = efx->txq_entries;
804 efx->rxq_entries = rxq_entries;
805 efx->txq_entries = txq_entries;
806 for (i = 0; i < efx->n_channels; i++) {
807 channel = efx->channel[i];
808 efx->channel[i] = other_channel[i];
809 other_channel[i] = channel;
810 }
811
Ben Hutchings7f967c02012-02-13 23:45:02 +0000812 /* Restart buffer table allocation */
813 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000814
Ben Hutchingse8f14992010-12-07 19:47:34 +0000815 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000816 channel = efx->channel[i];
817 if (!channel->type->copy)
818 continue;
819 rc = efx_probe_channel(channel);
820 if (rc)
821 goto rollback;
822 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000823 }
Ben Hutchings46426102010-09-10 06:42:33 +0000824
Ben Hutchings7f967c02012-02-13 23:45:02 +0000825out:
826 /* Destroy unused channel structures */
827 for (i = 0; i < efx->n_channels; i++) {
828 channel = other_channel[i];
829 if (channel && channel->type->copy) {
830 efx_fini_napi_channel(channel);
831 efx_remove_channel(channel);
832 kfree(channel);
833 }
834 }
835
836 efx_start_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000837 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000838 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000839 return rc;
840
841rollback:
842 /* Swap back */
843 efx->rxq_entries = old_rxq_entries;
844 efx->txq_entries = old_txq_entries;
845 for (i = 0; i < efx->n_channels; i++) {
846 channel = efx->channel[i];
847 efx->channel[i] = other_channel[i];
848 other_channel[i] = channel;
849 }
850 goto out;
851}
852
Steve Hodgson90d683a2010-06-01 11:19:39 +0000853void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100854{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000855 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100856}
857
Ben Hutchings7f967c02012-02-13 23:45:02 +0000858static const struct efx_channel_type efx_default_channel_type = {
859 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100860 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000861 .get_name = efx_get_channel_name,
862 .copy = efx_copy_channel,
863 .keep_eventq = false,
864};
865
866int efx_channel_dummy_op_int(struct efx_channel *channel)
867{
868 return 0;
869}
870
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100871void efx_channel_dummy_op_void(struct efx_channel *channel)
872{
873}
874
Ben Hutchings8ceee662008-04-27 12:55:59 +0100875/**************************************************************************
876 *
877 * Port handling
878 *
879 **************************************************************************/
880
881/* This ensures that the kernel is kept informed (via
882 * netif_carrier_on/off) of the link status, and also maintains the
883 * link status's stop on the port's TX queue.
884 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000885void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100886{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000887 struct efx_link_state *link_state = &efx->link_state;
888
Ben Hutchings8ceee662008-04-27 12:55:59 +0100889 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
890 * that no events are triggered between unregister_netdev() and the
891 * driver unloading. A more general condition is that NETDEV_CHANGE
892 * can only be generated between NETDEV_UP and NETDEV_DOWN */
893 if (!netif_running(efx->net_dev))
894 return;
895
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000896 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100897 efx->n_link_state_changes++;
898
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000899 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100900 netif_carrier_on(efx->net_dev);
901 else
902 netif_carrier_off(efx->net_dev);
903 }
904
905 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000906 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000907 netif_info(efx, link, efx->net_dev,
908 "link up at %uMbps %s-duplex (MTU %d)%s\n",
909 link_state->speed, link_state->fd ? "full" : "half",
910 efx->net_dev->mtu,
911 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000912 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000913 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100914}
915
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000916void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
917{
918 efx->link_advertising = advertising;
919 if (advertising) {
920 if (advertising & ADVERTISED_Pause)
921 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
922 else
923 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
924 if (advertising & ADVERTISED_Asym_Pause)
925 efx->wanted_fc ^= EFX_FC_TX;
926 }
927}
928
David S. Millerb56269462011-05-17 17:53:22 -0400929void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000930{
931 efx->wanted_fc = wanted_fc;
932 if (efx->link_advertising) {
933 if (wanted_fc & EFX_FC_RX)
934 efx->link_advertising |= (ADVERTISED_Pause |
935 ADVERTISED_Asym_Pause);
936 else
937 efx->link_advertising &= ~(ADVERTISED_Pause |
938 ADVERTISED_Asym_Pause);
939 if (wanted_fc & EFX_FC_TX)
940 efx->link_advertising ^= ADVERTISED_Asym_Pause;
941 }
942}
943
Ben Hutchings115122a2009-03-04 09:52:52 +0000944static void efx_fini_port(struct efx_nic *efx);
945
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000946/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
947 * the MAC appropriately. All other PHY configuration changes are pushed
948 * through phy_op->set_settings(), and pushed asynchronously to the MAC
949 * through efx_monitor().
950 *
951 * Callers must hold the mac_lock
952 */
953int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100954{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000955 enum efx_phy_mode phy_mode;
956 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100957
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000958 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100959
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000960 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000961 netif_addr_lock_bh(efx->net_dev);
962 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100963
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000964 /* Disable PHY transmit in mac level loopbacks */
965 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800966 if (LOOPBACK_INTERNAL(efx))
967 efx->phy_mode |= PHY_MODE_TX_DISABLED;
968 else
969 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800970
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000971 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800972
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000973 if (rc)
974 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100975
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000976 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100977}
978
979/* Reinitialise the MAC to pick up new PHY settings, even if the port is
980 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000981int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100982{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000983 int rc;
984
Ben Hutchings8ceee662008-04-27 12:55:59 +0100985 EFX_ASSERT_RESET_SERIALISED(efx);
986
987 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000988 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100989 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000990
991 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100992}
993
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000994/* Asynchronous work item for changing MAC promiscuity and multicast
995 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
996 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800997static void efx_mac_work(struct work_struct *data)
998{
999 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
1000
1001 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +01001002 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +01001003 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001004 mutex_unlock(&efx->mac_lock);
1005}
1006
Ben Hutchings8ceee662008-04-27 12:55:59 +01001007static int efx_probe_port(struct efx_nic *efx)
1008{
1009 int rc;
1010
Ben Hutchings62776d02010-06-23 11:30:07 +00001011 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001012
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001013 if (phy_flash_cfg)
1014 efx->phy_mode = PHY_MODE_SPECIAL;
1015
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001016 /* Connect up MAC/PHY operations table */
1017 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001018 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001019 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001020
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001021 /* Initialise MAC address to permanent address */
1022 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001023
1024 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001025}
1026
1027static int efx_init_port(struct efx_nic *efx)
1028{
1029 int rc;
1030
Ben Hutchings62776d02010-06-23 11:30:07 +00001031 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001032
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001033 mutex_lock(&efx->mac_lock);
1034
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001035 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001036 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001037 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001038
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001039 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001040
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001041 /* Reconfigure the MAC before creating dma queues (required for
1042 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001043 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001044
1045 /* Ensure the PHY advertises the correct flow control settings */
1046 rc = efx->phy_op->reconfigure(efx);
1047 if (rc)
1048 goto fail2;
1049
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001050 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001051 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001052
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001053fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001054 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001055fail1:
1056 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001057 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001058}
1059
Ben Hutchings8ceee662008-04-27 12:55:59 +01001060static void efx_start_port(struct efx_nic *efx)
1061{
Ben Hutchings62776d02010-06-23 11:30:07 +00001062 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001063 BUG_ON(efx->port_enabled);
1064
1065 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001066 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001067
1068 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1069 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001070 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001071
Ben Hutchings8ceee662008-04-27 12:55:59 +01001072 mutex_unlock(&efx->mac_lock);
1073}
1074
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001075/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001076static void efx_stop_port(struct efx_nic *efx)
1077{
Ben Hutchings62776d02010-06-23 11:30:07 +00001078 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001079
1080 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001081 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001082 mutex_unlock(&efx->mac_lock);
1083
1084 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001085 netif_addr_lock_bh(efx->net_dev);
1086 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001087}
1088
1089static void efx_fini_port(struct efx_nic *efx)
1090{
Ben Hutchings62776d02010-06-23 11:30:07 +00001091 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001092
1093 if (!efx->port_initialized)
1094 return;
1095
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001096 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001097 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001098
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001099 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001100 efx_link_status_changed(efx);
1101}
1102
1103static void efx_remove_port(struct efx_nic *efx)
1104{
Ben Hutchings62776d02010-06-23 11:30:07 +00001105 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001106
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001107 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001108}
1109
1110/**************************************************************************
1111 *
1112 * NIC handling
1113 *
1114 **************************************************************************/
1115
1116/* This configures the PCI device to enable I/O and DMA. */
1117static int efx_init_io(struct efx_nic *efx)
1118{
1119 struct pci_dev *pci_dev = efx->pci_dev;
1120 dma_addr_t dma_mask = efx->type->max_dma_mask;
1121 int rc;
1122
Ben Hutchings62776d02010-06-23 11:30:07 +00001123 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001124
1125 rc = pci_enable_device(pci_dev);
1126 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001127 netif_err(efx, probe, efx->net_dev,
1128 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001129 goto fail1;
1130 }
1131
1132 pci_set_master(pci_dev);
1133
1134 /* Set the PCI DMA mask. Try all possibilities from our
1135 * genuine mask down to 32 bits, because some architectures
1136 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1137 * masks event though they reject 46 bit masks.
1138 */
1139 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001140 if (dma_supported(&pci_dev->dev, dma_mask)) {
1141 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001142 if (rc == 0)
1143 break;
1144 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001145 dma_mask >>= 1;
1146 }
1147 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001148 netif_err(efx, probe, efx->net_dev,
1149 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001150 goto fail2;
1151 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001152 netif_dbg(efx, probe, efx->net_dev,
1153 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001154 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001155 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001156 /* dma_set_coherent_mask() is not *allowed* to
1157 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001158 * but just in case...
1159 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001160 netif_err(efx, probe, efx->net_dev,
1161 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001162 goto fail2;
1163 }
1164
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001165 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1166 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001167 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001168 netif_err(efx, probe, efx->net_dev,
1169 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001170 rc = -EIO;
1171 goto fail3;
1172 }
David S. Miller8decf862011-09-22 03:23:13 -04001173 efx->membase = ioremap_nocache(efx->membase_phys,
1174 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001175 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001176 netif_err(efx, probe, efx->net_dev,
1177 "could not map memory BAR at %llx+%x\n",
1178 (unsigned long long)efx->membase_phys,
1179 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001180 rc = -ENOMEM;
1181 goto fail4;
1182 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001183 netif_dbg(efx, probe, efx->net_dev,
1184 "memory BAR at %llx+%x (virtual %p)\n",
1185 (unsigned long long)efx->membase_phys,
1186 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001187
1188 return 0;
1189
1190 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001191 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001192 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001193 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001194 fail2:
1195 pci_disable_device(efx->pci_dev);
1196 fail1:
1197 return rc;
1198}
1199
1200static void efx_fini_io(struct efx_nic *efx)
1201{
Ben Hutchings62776d02010-06-23 11:30:07 +00001202 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001203
1204 if (efx->membase) {
1205 iounmap(efx->membase);
1206 efx->membase = NULL;
1207 }
1208
1209 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001210 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001211 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001212 }
1213
1214 pci_disable_device(efx->pci_dev);
1215}
1216
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001217static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001218{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001219 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001220 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001221 int cpu;
1222
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001223 if (rss_cpus) {
1224 count = rss_cpus;
1225 } else {
1226 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1227 netif_warn(efx, probe, efx->net_dev,
1228 "RSS disabled due to allocation failure\n");
1229 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001230 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001231
1232 count = 0;
1233 for_each_online_cpu(cpu) {
1234 if (!cpumask_test_cpu(cpu, thread_mask)) {
1235 ++count;
1236 cpumask_or(thread_mask, thread_mask,
1237 topology_thread_cpumask(cpu));
1238 }
1239 }
1240
1241 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001242 }
1243
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001244 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1245 * table entries that are inaccessible to VFs
1246 */
1247 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1248 count > efx_vf_size(efx)) {
1249 netif_warn(efx, probe, efx->net_dev,
1250 "Reducing number of RSS channels from %u to %u for "
1251 "VF support. Increase vf-msix-limit to use more "
1252 "channels on the PF.\n",
1253 count, efx_vf_size(efx));
1254 count = efx_vf_size(efx);
1255 }
1256
Ben Hutchings46123d02008-09-01 12:47:33 +01001257 return count;
1258}
1259
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001260static int
1261efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1262{
1263#ifdef CONFIG_RFS_ACCEL
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001264 unsigned int i;
1265 int rc;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001266
1267 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1268 if (!efx->net_dev->rx_cpu_rmap)
1269 return -ENOMEM;
1270 for (i = 0; i < efx->n_rx_channels; i++) {
1271 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1272 xentries[i].vector);
1273 if (rc) {
1274 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1275 efx->net_dev->rx_cpu_rmap = NULL;
1276 return rc;
1277 }
1278 }
1279#endif
1280 return 0;
1281}
1282
Ben Hutchings46123d02008-09-01 12:47:33 +01001283/* Probe the number and type of interrupts we are able to obtain, and
1284 * the resulting numbers of channels and RX queues.
1285 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001286static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001287{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001288 unsigned int max_channels =
1289 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001290 unsigned int extra_channels = 0;
1291 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001292 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001293
Ben Hutchings7f967c02012-02-13 23:45:02 +00001294 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1295 if (efx->extra_channel_type[i])
1296 ++extra_channels;
1297
Ben Hutchings8ceee662008-04-27 12:55:59 +01001298 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001299 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001300 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001301
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001302 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001303 if (separate_tx_channels)
1304 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001305 n_channels += extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001306 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001307
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001308 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001309 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001310 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001311 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001312 netif_err(efx, drv, efx->net_dev,
1313 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001314 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001315 netif_err(efx, drv, efx->net_dev,
1316 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001317 EFX_BUG_ON_PARANOID(rc >= n_channels);
1318 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001319 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001320 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001321 }
1322
1323 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001324 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001325 if (n_channels > extra_channels)
1326 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001327 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001328 efx->n_tx_channels = max(n_channels / 2, 1U);
1329 efx->n_rx_channels = max(n_channels -
1330 efx->n_tx_channels,
1331 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001332 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001333 efx->n_tx_channels = n_channels;
1334 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001335 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001336 rc = efx_init_rx_cpu_rmap(efx, xentries);
1337 if (rc) {
1338 pci_disable_msix(efx->pci_dev);
1339 return rc;
1340 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001341 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001342 efx_get_channel(efx, i)->irq =
1343 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001344 } else {
1345 /* Fall back to single channel MSI */
1346 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001347 netif_err(efx, drv, efx->net_dev,
1348 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001349 }
1350 }
1351
1352 /* Try single interrupt MSI */
1353 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001354 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001355 efx->n_rx_channels = 1;
1356 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001357 rc = pci_enable_msi(efx->pci_dev);
1358 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001359 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001360 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001361 netif_err(efx, drv, efx->net_dev,
1362 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001363 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1364 }
1365 }
1366
1367 /* Assume legacy interrupts */
1368 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001369 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001370 efx->n_rx_channels = 1;
1371 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001372 efx->legacy_irq = efx->pci_dev->irq;
1373 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001374
Ben Hutchings7f967c02012-02-13 23:45:02 +00001375 /* Assign extra channels if possible */
1376 j = efx->n_channels;
1377 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1378 if (!efx->extra_channel_type[i])
1379 continue;
1380 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1381 efx->n_channels <= extra_channels) {
1382 efx->extra_channel_type[i]->handle_no_channel(efx);
1383 } else {
1384 --j;
1385 efx_get_channel(efx, j)->type =
1386 efx->extra_channel_type[i];
1387 }
1388 }
1389
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001390 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001391 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001392 efx->n_rx_channels : efx_vf_size(efx));
1393
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001394 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001395}
1396
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001397/* Enable interrupts, then probe and start the event queues */
Ben Hutchings7f967c02012-02-13 23:45:02 +00001398static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001399{
1400 struct efx_channel *channel;
1401
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001402 BUG_ON(efx->state == STATE_DISABLED);
1403
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001404 if (efx->legacy_irq)
1405 efx->legacy_irq_enabled = true;
1406 efx_nic_enable_interrupts(efx);
1407
1408 efx_for_each_channel(channel, efx) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001409 if (!channel->type->keep_eventq || !may_keep_eventq)
1410 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001411 efx_start_eventq(channel);
1412 }
1413
1414 efx_mcdi_mode_event(efx);
1415}
1416
Ben Hutchings7f967c02012-02-13 23:45:02 +00001417static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001418{
1419 struct efx_channel *channel;
1420
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001421 if (efx->state == STATE_DISABLED)
1422 return;
1423
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001424 efx_mcdi_mode_poll(efx);
1425
1426 efx_nic_disable_interrupts(efx);
1427 if (efx->legacy_irq) {
1428 synchronize_irq(efx->legacy_irq);
1429 efx->legacy_irq_enabled = false;
1430 }
1431
1432 efx_for_each_channel(channel, efx) {
1433 if (channel->irq)
1434 synchronize_irq(channel->irq);
1435
1436 efx_stop_eventq(channel);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001437 if (!channel->type->keep_eventq || !may_keep_eventq)
1438 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001439 }
1440}
1441
Ben Hutchings8ceee662008-04-27 12:55:59 +01001442static void efx_remove_interrupts(struct efx_nic *efx)
1443{
1444 struct efx_channel *channel;
1445
1446 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001447 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001448 channel->irq = 0;
1449 pci_disable_msi(efx->pci_dev);
1450 pci_disable_msix(efx->pci_dev);
1451
1452 /* Remove legacy interrupt */
1453 efx->legacy_irq = 0;
1454}
1455
Ben Hutchings8831da72008-09-01 12:47:48 +01001456static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001457{
Ben Hutchings602a5322011-05-16 17:32:39 +01001458 struct efx_channel *channel;
1459 struct efx_tx_queue *tx_queue;
1460
Ben Hutchings97653432011-01-12 18:26:56 +00001461 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001462 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001463
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001464 /* We need to mark which channels really have RX and TX
1465 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001466 * RX-only and TX-only channels.
1467 */
1468 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001469 if (channel->channel < efx->n_rx_channels)
1470 channel->rx_queue.core_index = channel->channel;
1471 else
1472 channel->rx_queue.core_index = -1;
1473
Ben Hutchings602a5322011-05-16 17:32:39 +01001474 efx_for_each_channel_tx_queue(tx_queue, channel)
1475 tx_queue->queue -= (efx->tx_channel_offset *
1476 EFX_TXQ_TYPES);
1477 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001478}
1479
1480static int efx_probe_nic(struct efx_nic *efx)
1481{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001482 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001483 int rc;
1484
Ben Hutchings62776d02010-06-23 11:30:07 +00001485 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001486
1487 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001488 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001489 if (rc)
1490 return rc;
1491
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001492 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001493 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001494 rc = efx_probe_interrupts(efx);
1495 if (rc)
1496 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001497
Ben Hutchings28e47c42012-02-15 01:58:49 +00001498 efx->type->dimension_resources(efx);
1499
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001500 if (efx->n_channels > 1)
1501 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001502 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001503 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001504 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001505
Ben Hutchings8831da72008-09-01 12:47:48 +01001506 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001507 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1508 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001509
1510 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001511 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1512 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001513
1514 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001515
1516fail:
1517 efx->type->remove(efx);
1518 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001519}
1520
1521static void efx_remove_nic(struct efx_nic *efx)
1522{
Ben Hutchings62776d02010-06-23 11:30:07 +00001523 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001524
1525 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001526 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001527}
1528
1529/**************************************************************************
1530 *
1531 * NIC startup/shutdown
1532 *
1533 *************************************************************************/
1534
1535static int efx_probe_all(struct efx_nic *efx)
1536{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001537 int rc;
1538
Ben Hutchings8ceee662008-04-27 12:55:59 +01001539 rc = efx_probe_nic(efx);
1540 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001541 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001542 goto fail1;
1543 }
1544
Ben Hutchings8ceee662008-04-27 12:55:59 +01001545 rc = efx_probe_port(efx);
1546 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001547 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001548 goto fail2;
1549 }
1550
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001551 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1552 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1553 rc = -EINVAL;
1554 goto fail3;
1555 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001556 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001557
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001558 rc = efx_probe_filters(efx);
1559 if (rc) {
1560 netif_err(efx, probe, efx->net_dev,
1561 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001562 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001563 }
1564
Ben Hutchings7f967c02012-02-13 23:45:02 +00001565 rc = efx_probe_channels(efx);
1566 if (rc)
1567 goto fail4;
1568
Ben Hutchings8ceee662008-04-27 12:55:59 +01001569 return 0;
1570
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001571 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001572 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001573 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001574 efx_remove_port(efx);
1575 fail2:
1576 efx_remove_nic(efx);
1577 fail1:
1578 return rc;
1579}
1580
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001581/* If the interface is supposed to be running but is not, start
1582 * the hardware and software data path, regular activity for the port
1583 * (MAC statistics, link polling, etc.) and schedule the port to be
1584 * reconfigured. Interrupts must already be enabled. This function
1585 * is safe to call multiple times, so long as the NIC is not disabled.
1586 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001587 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001588static void efx_start_all(struct efx_nic *efx)
1589{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001590 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001591 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001592
1593 /* Check that it is appropriate to restart the interface. All
1594 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001595 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001596 return;
1597
Ben Hutchings8ceee662008-04-27 12:55:59 +01001598 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001599 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001600
Alexandre Rames626950d2013-01-14 17:20:22 +00001601 /* Start the hardware monitor if there is one */
1602 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001603 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1604 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001605
1606 /* If link state detection is normally event-driven, we have
1607 * to poll now because we could have missed a change
1608 */
1609 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001610 mutex_lock(&efx->mac_lock);
1611 if (efx->phy_op->poll(efx))
1612 efx_link_status_changed(efx);
1613 mutex_unlock(&efx->mac_lock);
1614 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001615
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001616 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001617}
1618
1619/* Flush all delayed work. Should only be called when no more delayed work
1620 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1621 * since we're holding the rtnl_lock at this point. */
1622static void efx_flush_all(struct efx_nic *efx)
1623{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001624 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001625 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001626 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001627 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001628 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001629}
1630
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001631/* Quiesce the hardware and software data path, and regular activity
1632 * for the port without bringing the link down. Safe to call multiple
1633 * times with the NIC in almost any state, but interrupts should be
1634 * enabled. Requires the RTNL lock.
1635 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001636static void efx_stop_all(struct efx_nic *efx)
1637{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001638 EFX_ASSERT_RESET_SERIALISED(efx);
1639
1640 /* port_enabled can be read safely under the rtnl lock */
1641 if (!efx->port_enabled)
1642 return;
1643
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001644 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001645 efx_stop_port(efx);
1646
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001647 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001648 efx_flush_all(efx);
1649
Ben Hutchings29c69a42013-01-28 19:01:06 +00001650 /* Stop the kernel transmit interface. This is only valid if
1651 * the device is stopped or detached; otherwise the watchdog
1652 * may fire immediately.
1653 */
1654 WARN_ON(netif_running(efx->net_dev) &&
1655 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001656 netif_tx_disable(efx->net_dev);
1657
1658 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001659}
1660
1661static void efx_remove_all(struct efx_nic *efx)
1662{
Ben Hutchings46426102010-09-10 06:42:33 +00001663 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001664 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001665 efx_remove_port(efx);
1666 efx_remove_nic(efx);
1667}
1668
Ben Hutchings8ceee662008-04-27 12:55:59 +01001669/**************************************************************************
1670 *
1671 * Interrupt moderation
1672 *
1673 **************************************************************************/
1674
Ben Hutchingscc180b62011-12-08 19:51:47 +00001675static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001676{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001677 if (usecs == 0)
1678 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001679 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001680 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001681 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001682}
1683
Ben Hutchings8ceee662008-04-27 12:55:59 +01001684/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001685int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1686 unsigned int rx_usecs, bool rx_adaptive,
1687 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001688{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001689 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001690 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1691 efx->timer_quantum_ns,
1692 1000);
1693 unsigned int tx_ticks;
1694 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001695
1696 EFX_ASSERT_RESET_SERIALISED(efx);
1697
Ben Hutchingscc180b62011-12-08 19:51:47 +00001698 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001699 return -EINVAL;
1700
Ben Hutchingscc180b62011-12-08 19:51:47 +00001701 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1702 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1703
Ben Hutchings9e393b32011-09-05 07:43:04 +00001704 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1705 !rx_may_override_tx) {
1706 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1707 "RX and TX IRQ moderation must be equal\n");
1708 return -EINVAL;
1709 }
1710
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001711 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001712 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001713 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001714 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001715 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001716 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001717 channel->irq_moderation = tx_ticks;
1718 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001719
1720 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001721}
1722
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001723void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1724 unsigned int *rx_usecs, bool *rx_adaptive)
1725{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001726 /* We must round up when converting ticks to microseconds
1727 * because we round down when converting the other way.
1728 */
1729
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001730 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001731 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1732 efx->timer_quantum_ns,
1733 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001734
1735 /* If channels are shared between RX and TX, so is IRQ
1736 * moderation. Otherwise, IRQ moderation is the same for all
1737 * TX channels and is not adaptive.
1738 */
1739 if (efx->tx_channel_offset == 0)
1740 *tx_usecs = *rx_usecs;
1741 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001742 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001743 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001744 efx->timer_quantum_ns,
1745 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001746}
1747
Ben Hutchings8ceee662008-04-27 12:55:59 +01001748/**************************************************************************
1749 *
1750 * Hardware monitor
1751 *
1752 **************************************************************************/
1753
Ben Hutchingse254c272010-09-20 08:44:10 +00001754/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001755static void efx_monitor(struct work_struct *data)
1756{
1757 struct efx_nic *efx = container_of(data, struct efx_nic,
1758 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001759
Ben Hutchings62776d02010-06-23 11:30:07 +00001760 netif_vdbg(efx, timer, efx->net_dev,
1761 "hardware monitor executing on CPU %d\n",
1762 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001763 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001764
Ben Hutchings8ceee662008-04-27 12:55:59 +01001765 /* If the mac_lock is already held then it is likely a port
1766 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001767 * most of the work of monitor() anyway. */
1768 if (mutex_trylock(&efx->mac_lock)) {
1769 if (efx->port_enabled)
1770 efx->type->monitor(efx);
1771 mutex_unlock(&efx->mac_lock);
1772 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001773
Ben Hutchings8ceee662008-04-27 12:55:59 +01001774 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1775 efx_monitor_interval);
1776}
1777
1778/**************************************************************************
1779 *
1780 * ioctls
1781 *
1782 *************************************************************************/
1783
1784/* Net device ioctl
1785 * Context: process, rtnl_lock() held.
1786 */
1787static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1788{
Ben Hutchings767e4682008-09-01 12:43:14 +01001789 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001790 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001791
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001792 if (cmd == SIOCSHWTSTAMP)
1793 return efx_ptp_ioctl(efx, ifr, cmd);
1794
Ben Hutchings68e7f452009-04-29 08:05:08 +00001795 /* Convert phy_id from older PRTAD/DEVAD format */
1796 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1797 (data->phy_id & 0xfc00) == 0x0400)
1798 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1799
1800 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001801}
1802
1803/**************************************************************************
1804 *
1805 * NAPI interface
1806 *
1807 **************************************************************************/
1808
Ben Hutchings7f967c02012-02-13 23:45:02 +00001809static void efx_init_napi_channel(struct efx_channel *channel)
1810{
1811 struct efx_nic *efx = channel->efx;
1812
1813 channel->napi_dev = efx->net_dev;
1814 netif_napi_add(channel->napi_dev, &channel->napi_str,
1815 efx_poll, napi_weight);
1816}
1817
Ben Hutchingse8f14992010-12-07 19:47:34 +00001818static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001819{
1820 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001821
Ben Hutchings7f967c02012-02-13 23:45:02 +00001822 efx_for_each_channel(channel, efx)
1823 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001824}
1825
1826static void efx_fini_napi_channel(struct efx_channel *channel)
1827{
1828 if (channel->napi_dev)
1829 netif_napi_del(&channel->napi_str);
1830 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001831}
1832
1833static void efx_fini_napi(struct efx_nic *efx)
1834{
1835 struct efx_channel *channel;
1836
Ben Hutchingse8f14992010-12-07 19:47:34 +00001837 efx_for_each_channel(channel, efx)
1838 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001839}
1840
1841/**************************************************************************
1842 *
1843 * Kernel netpoll interface
1844 *
1845 *************************************************************************/
1846
1847#ifdef CONFIG_NET_POLL_CONTROLLER
1848
1849/* Although in the common case interrupts will be disabled, this is not
1850 * guaranteed. However, all our work happens inside the NAPI callback,
1851 * so no locking is required.
1852 */
1853static void efx_netpoll(struct net_device *net_dev)
1854{
Ben Hutchings767e4682008-09-01 12:43:14 +01001855 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001856 struct efx_channel *channel;
1857
Ben Hutchings64ee3122008-09-01 12:47:38 +01001858 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001859 efx_schedule_channel(channel);
1860}
1861
1862#endif
1863
1864/**************************************************************************
1865 *
1866 * Kernel net device interface
1867 *
1868 *************************************************************************/
1869
1870/* Context: process, rtnl_lock() held. */
1871static int efx_net_open(struct net_device *net_dev)
1872{
Ben Hutchings767e4682008-09-01 12:43:14 +01001873 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001874 int rc;
1875
Ben Hutchings62776d02010-06-23 11:30:07 +00001876 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1877 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001878
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001879 rc = efx_check_disabled(efx);
1880 if (rc)
1881 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001882 if (efx->phy_mode & PHY_MODE_SPECIAL)
1883 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001884 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1885 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001886
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001887 /* Notify the kernel of the link state polled during driver load,
1888 * before the monitor starts running */
1889 efx_link_status_changed(efx);
1890
Ben Hutchings8ceee662008-04-27 12:55:59 +01001891 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001892 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001893 return 0;
1894}
1895
1896/* Context: process, rtnl_lock() held.
1897 * Note that the kernel will ignore our return code; this method
1898 * should really be a void.
1899 */
1900static int efx_net_stop(struct net_device *net_dev)
1901{
Ben Hutchings767e4682008-09-01 12:43:14 +01001902 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001903
Ben Hutchings62776d02010-06-23 11:30:07 +00001904 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1905 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001906
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001907 /* Stop the device and flush all the channels */
1908 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001909
1910 return 0;
1911}
1912
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001913/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001914static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1915 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001916{
Ben Hutchings767e4682008-09-01 12:43:14 +01001917 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001918 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001919
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001920 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001921
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001922 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001923
1924 stats->rx_packets = mac_stats->rx_packets;
1925 stats->tx_packets = mac_stats->tx_packets;
1926 stats->rx_bytes = mac_stats->rx_bytes;
1927 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001928 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001929 stats->multicast = mac_stats->rx_multicast;
1930 stats->collisions = mac_stats->tx_collision;
1931 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1932 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001933 stats->rx_crc_errors = mac_stats->rx_bad;
1934 stats->rx_frame_errors = mac_stats->rx_align_error;
1935 stats->rx_fifo_errors = mac_stats->rx_overflow;
1936 stats->rx_missed_errors = mac_stats->rx_missed;
1937 stats->tx_window_errors = mac_stats->tx_late_collision;
1938
1939 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001940 stats->rx_crc_errors +
1941 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001942 mac_stats->rx_symbol_error);
1943 stats->tx_errors = (stats->tx_window_errors +
1944 mac_stats->tx_bad);
1945
Ben Hutchings1cb34522011-09-02 23:23:00 +01001946 spin_unlock_bh(&efx->stats_lock);
1947
Ben Hutchings8ceee662008-04-27 12:55:59 +01001948 return stats;
1949}
1950
1951/* Context: netif_tx_lock held, BHs disabled. */
1952static void efx_watchdog(struct net_device *net_dev)
1953{
Ben Hutchings767e4682008-09-01 12:43:14 +01001954 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001955
Ben Hutchings62776d02010-06-23 11:30:07 +00001956 netif_err(efx, tx_err, efx->net_dev,
1957 "TX stuck with port_enabled=%d: resetting channels\n",
1958 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001959
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001960 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001961}
1962
1963
1964/* Context: process, rtnl_lock() held. */
1965static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1966{
Ben Hutchings767e4682008-09-01 12:43:14 +01001967 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001968 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001969
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001970 rc = efx_check_disabled(efx);
1971 if (rc)
1972 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001973 if (new_mtu > EFX_MAX_MTU)
1974 return -EINVAL;
1975
Ben Hutchings62776d02010-06-23 11:30:07 +00001976 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001977
Ben Hutchings29c69a42013-01-28 19:01:06 +00001978 efx_device_detach_sync(efx);
1979 efx_stop_all(efx);
1980
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001981 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001982 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001983 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001984 mutex_unlock(&efx->mac_lock);
1985
Ben Hutchings8ceee662008-04-27 12:55:59 +01001986 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00001987 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001988 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001989}
1990
1991static int efx_set_mac_address(struct net_device *net_dev, void *data)
1992{
Ben Hutchings767e4682008-09-01 12:43:14 +01001993 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001994 struct sockaddr *addr = data;
1995 char *new_addr = addr->sa_data;
1996
Ben Hutchings8ceee662008-04-27 12:55:59 +01001997 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001998 netif_err(efx, drv, efx->net_dev,
1999 "invalid ethernet MAC address requested: %pM\n",
2000 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002001 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002002 }
2003
2004 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002005 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002006
2007 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002008 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002009 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002010 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002011
2012 return 0;
2013}
2014
Ben Hutchingsa816f752008-09-01 12:49:12 +01002015/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002016static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002017{
Ben Hutchings767e4682008-09-01 12:43:14 +01002018 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002019 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002020 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002021 u32 crc;
2022 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002023
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002024 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002025
2026 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002027 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002028 memset(mc_hash, 0xff, sizeof(*mc_hash));
2029 } else {
2030 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00002031 netdev_for_each_mc_addr(ha, net_dev) {
2032 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002033 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
Ben Hutchings32766ec2012-10-04 17:13:03 -07002034 __set_bit_le(bit, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002035 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002036
2037 /* Broadcast packets go through the multicast hash filter.
2038 * ether_crc_le() of the broadcast address is 0xbe2612ff
2039 * so we always add bit 0xff to the mask.
2040 */
Ben Hutchings32766ec2012-10-04 17:13:03 -07002041 __set_bit_le(0xff, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002042 }
2043
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002044 if (efx->port_enabled)
2045 queue_work(efx->workqueue, &efx->mac_work);
2046 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002047}
2048
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002049static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002050{
2051 struct efx_nic *efx = netdev_priv(net_dev);
2052
2053 /* If disabling RX n-tuple filtering, clear existing filters */
2054 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2055 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2056
2057 return 0;
2058}
2059
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002060static const struct net_device_ops efx_netdev_ops = {
2061 .ndo_open = efx_net_open,
2062 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002063 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002064 .ndo_tx_timeout = efx_watchdog,
2065 .ndo_start_xmit = efx_hard_start_xmit,
2066 .ndo_validate_addr = eth_validate_addr,
2067 .ndo_do_ioctl = efx_ioctl,
2068 .ndo_change_mtu = efx_change_mtu,
2069 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002070 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002071 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002072#ifdef CONFIG_SFC_SRIOV
2073 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2074 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2075 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2076 .ndo_get_vf_config = efx_sriov_get_vf_config,
2077#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002078#ifdef CONFIG_NET_POLL_CONTROLLER
2079 .ndo_poll_controller = efx_netpoll,
2080#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002081 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002082#ifdef CONFIG_RFS_ACCEL
2083 .ndo_rx_flow_steer = efx_filter_rfs,
2084#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002085};
2086
Ben Hutchings7dde5962008-12-12 22:09:38 -08002087static void efx_update_name(struct efx_nic *efx)
2088{
2089 strcpy(efx->name, efx->net_dev->name);
2090 efx_mtd_rename(efx);
2091 efx_set_channel_names(efx);
2092}
2093
Ben Hutchings8ceee662008-04-27 12:55:59 +01002094static int efx_netdev_event(struct notifier_block *this,
2095 unsigned long event, void *ptr)
2096{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01002097 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002098
Ben Hutchings7dde5962008-12-12 22:09:38 -08002099 if (net_dev->netdev_ops == &efx_netdev_ops &&
2100 event == NETDEV_CHANGENAME)
2101 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002102
2103 return NOTIFY_DONE;
2104}
2105
2106static struct notifier_block efx_netdev_notifier = {
2107 .notifier_call = efx_netdev_event,
2108};
2109
Ben Hutchings06d5e192008-12-12 21:47:23 -08002110static ssize_t
2111show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2112{
2113 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2114 return sprintf(buf, "%d\n", efx->phy_type);
2115}
2116static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
2117
Ben Hutchings8ceee662008-04-27 12:55:59 +01002118static int efx_register_netdev(struct efx_nic *efx)
2119{
2120 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002121 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002122 int rc;
2123
2124 net_dev->watchdog_timeo = 5 * HZ;
2125 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002126 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002127 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002128 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002129
Ben Hutchings7dde5962008-12-12 22:09:38 -08002130 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002131
Ben Hutchings7153f622012-07-27 20:50:52 +01002132 /* Enable resets to be scheduled and check whether any were
2133 * already requested. If so, the NIC is probably hosed so we
2134 * abort.
2135 */
2136 efx->state = STATE_READY;
2137 smp_mb(); /* ensure we change state before checking reset_pending */
2138 if (efx->reset_pending) {
2139 netif_err(efx, probe, efx->net_dev,
2140 "aborting probe due to scheduled reset\n");
2141 rc = -EIO;
2142 goto fail_locked;
2143 }
2144
Ben Hutchingsaed06282009-08-26 08:16:27 +00002145 rc = dev_alloc_name(net_dev, net_dev->name);
2146 if (rc < 0)
2147 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002148 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002149
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002150 /* Always start with carrier off; PHY events will detect the link */
2151 netif_carrier_off(net_dev);
2152
Ben Hutchingsaed06282009-08-26 08:16:27 +00002153 rc = register_netdevice(net_dev);
2154 if (rc)
2155 goto fail_locked;
2156
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002157 efx_for_each_channel(channel, efx) {
2158 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002159 efx_for_each_channel_tx_queue(tx_queue, channel)
2160 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002161 }
2162
Ben Hutchings7dde5962008-12-12 22:09:38 -08002163 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002164
Ben Hutchings06d5e192008-12-12 21:47:23 -08002165 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2166 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002167 netif_err(efx, drv, efx->net_dev,
2168 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002169 goto fail_registered;
2170 }
2171
Ben Hutchings8ceee662008-04-27 12:55:59 +01002172 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002173
Ben Hutchings7153f622012-07-27 20:50:52 +01002174fail_registered:
2175 rtnl_lock();
2176 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002177fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002178 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002179 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002180 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002181 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002182}
2183
2184static void efx_unregister_netdev(struct efx_nic *efx)
2185{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002186 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002187 struct efx_tx_queue *tx_queue;
2188
2189 if (!efx->net_dev)
2190 return;
2191
Ben Hutchings767e4682008-09-01 12:43:14 +01002192 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002193
2194 /* Free up any skbs still remaining. This has to happen before
2195 * we try to unregister the netdev as running their destructors
2196 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002197 efx_for_each_channel(channel, efx) {
2198 efx_for_each_channel_tx_queue(tx_queue, channel)
2199 efx_release_tx_buffers(tx_queue);
2200 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002201
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002202 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2203 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002204
2205 rtnl_lock();
2206 unregister_netdevice(efx->net_dev);
2207 efx->state = STATE_UNINIT;
2208 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002209}
2210
2211/**************************************************************************
2212 *
2213 * Device reset and suspend
2214 *
2215 **************************************************************************/
2216
Ben Hutchings2467ca42008-09-01 12:48:50 +01002217/* Tears down the entire software state and most of the hardware state
2218 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002219void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002220{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002221 EFX_ASSERT_RESET_SERIALISED(efx);
2222
Ben Hutchings2467ca42008-09-01 12:48:50 +01002223 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00002224 efx_stop_interrupts(efx, false);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002225
2226 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002227 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2228 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002229 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002230}
2231
Ben Hutchings2467ca42008-09-01 12:48:50 +01002232/* This function will always ensure that the locks acquired in
2233 * efx_reset_down() are released. A failure return code indicates
2234 * that we were unable to reinitialise the hardware, and the
2235 * driver should be disabled. If ok is false, then the rx and tx
2236 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002237int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002238{
2239 int rc;
2240
Ben Hutchings2467ca42008-09-01 12:48:50 +01002241 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002242
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002243 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002244 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002245 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002246 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002247 }
2248
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002249 if (!ok)
2250 goto fail;
2251
Steve Hodgson4b988282009-01-29 17:50:51 +00002252 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002253 rc = efx->phy_op->init(efx);
2254 if (rc)
2255 goto fail;
2256 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002257 netif_err(efx, drv, efx->net_dev,
2258 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002259 }
2260
Ben Hutchings710b2082011-09-03 00:15:00 +01002261 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002262
Ben Hutchings7f967c02012-02-13 23:45:02 +00002263 efx_start_interrupts(efx, false);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002264 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002265 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002266
2267 mutex_unlock(&efx->mac_lock);
2268
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002269 efx_start_all(efx);
2270
2271 return 0;
2272
2273fail:
2274 efx->port_initialized = false;
2275
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002276 mutex_unlock(&efx->mac_lock);
2277
Ben Hutchings8ceee662008-04-27 12:55:59 +01002278 return rc;
2279}
2280
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002281/* Reset the NIC using the specified method. Note that the reset may
2282 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002283 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002284 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002285 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002286int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002287{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002288 int rc, rc2;
2289 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002290
Ben Hutchings62776d02010-06-23 11:30:07 +00002291 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2292 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002293
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002294 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002295 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002296
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002297 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002298 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002299 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002300 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002301 }
2302
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002303 /* Clear flags for the scopes we covered. We assume the NIC and
2304 * driver are now quiescent so that there is no race here.
2305 */
2306 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002307
2308 /* Reinitialise bus-mastering, which may have been turned off before
2309 * the reset was scheduled. This is still appropriate, even in the
2310 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2311 * can respond to requests. */
2312 pci_set_master(efx->pci_dev);
2313
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002314out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002315 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002316 disabled = rc ||
2317 method == RESET_TYPE_DISABLE ||
2318 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002319 rc2 = efx_reset_up(efx, method, !disabled);
2320 if (rc2) {
2321 disabled = true;
2322 if (!rc)
2323 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002324 }
2325
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002326 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002327 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002328 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002329 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002330 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002331 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002332 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002333 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002334 return rc;
2335}
2336
Alexandre Rames626950d2013-01-14 17:20:22 +00002337/* Try recovery mechanisms.
2338 * For now only EEH is supported.
2339 * Returns 0 if the recovery mechanisms are unsuccessful.
2340 * Returns a non-zero value otherwise.
2341 */
2342static int efx_try_recovery(struct efx_nic *efx)
2343{
2344#ifdef CONFIG_EEH
2345 /* A PCI error can occur and not be seen by EEH because nothing
2346 * happens on the PCI bus. In this case the driver may fail and
2347 * schedule a 'recover or reset', leading to this recovery handler.
2348 * Manually call the eeh failure check function.
2349 */
2350 struct eeh_dev *eehdev =
2351 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2352
2353 if (eeh_dev_check_failure(eehdev)) {
2354 /* The EEH mechanisms will handle the error and reset the
2355 * device if necessary.
2356 */
2357 return 1;
2358 }
2359#endif
2360 return 0;
2361}
2362
Ben Hutchings8ceee662008-04-27 12:55:59 +01002363/* The worker thread exists so that code that cannot sleep can
2364 * schedule a reset for later.
2365 */
2366static void efx_reset_work(struct work_struct *data)
2367{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002368 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002369 unsigned long pending;
2370 enum reset_type method;
2371
2372 pending = ACCESS_ONCE(efx->reset_pending);
2373 method = fls(pending) - 1;
2374
2375 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2376 method == RESET_TYPE_RECOVER_OR_ALL) &&
2377 efx_try_recovery(efx))
2378 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002379
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002380 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002381 return;
2382
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002383 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002384
2385 /* We checked the state in efx_schedule_reset() but it may
2386 * have changed by now. Now that we have the RTNL lock,
2387 * it cannot change again.
2388 */
2389 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002390 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002391
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002392 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002393}
2394
2395void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2396{
2397 enum reset_type method;
2398
Alexandre Rames626950d2013-01-14 17:20:22 +00002399 if (efx->state == STATE_RECOVERY) {
2400 netif_dbg(efx, drv, efx->net_dev,
2401 "recovering: skip scheduling %s reset\n",
2402 RESET_TYPE(type));
2403 return;
2404 }
2405
Ben Hutchings8ceee662008-04-27 12:55:59 +01002406 switch (type) {
2407 case RESET_TYPE_INVISIBLE:
2408 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002409 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002410 case RESET_TYPE_WORLD:
2411 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002412 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002413 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002414 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2415 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002416 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002417 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002418 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002419 netif_dbg(efx, drv, efx->net_dev,
2420 "scheduling %s reset for %s\n",
2421 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002422 break;
2423 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002424
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002425 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002426 smp_mb(); /* ensure we change reset_pending before checking state */
2427
2428 /* If we're not READY then just leave the flags set as the cue
2429 * to abort probing or reschedule the reset later.
2430 */
2431 if (ACCESS_ONCE(efx->state) != STATE_READY)
2432 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002433
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002434 /* efx_process_channel() will no longer read events once a
2435 * reset is scheduled. So switch back to poll'd MCDI completions. */
2436 efx_mcdi_mode_poll(efx);
2437
Steve Hodgson1ab00622008-12-12 21:33:02 -08002438 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002439}
2440
2441/**************************************************************************
2442 *
2443 * List of NICs we support
2444 *
2445 **************************************************************************/
2446
2447/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002448static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002449 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2450 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002451 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002452 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2453 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002454 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002455 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002456 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002457 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002458 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002459 {0} /* end of list */
2460};
2461
2462/**************************************************************************
2463 *
Ben Hutchings37594332009-11-23 16:05:45 +00002464 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002465 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002466 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002467 * Needed so all function pointers are valid and do not have to be tested
2468 * before use
2469 *
2470 **************************************************************************/
2471int efx_port_dummy_op_int(struct efx_nic *efx)
2472{
2473 return 0;
2474}
2475void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002476
2477static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002478{
2479 return false;
2480}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002481
stephen hemminger6c8c2512011-04-14 05:50:12 +00002482static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002483 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002484 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002485 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002486 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002487};
2488
Ben Hutchings8ceee662008-04-27 12:55:59 +01002489/**************************************************************************
2490 *
2491 * Data housekeeping
2492 *
2493 **************************************************************************/
2494
2495/* This zeroes out and then fills in the invariants in a struct
2496 * efx_nic (including all sub-structures).
2497 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002498static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002499 struct pci_dev *pci_dev, struct net_device *net_dev)
2500{
Ben Hutchings46426102010-09-10 06:42:33 +00002501 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002502
2503 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002504 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002505#ifdef CONFIG_SFC_MTD
2506 INIT_LIST_HEAD(&efx->mtd_list);
2507#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002508 INIT_WORK(&efx->reset_work, efx_reset_work);
2509 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002510 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002511 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002512 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002513 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002514 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002515
2516 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002517 spin_lock_init(&efx->stats_lock);
2518 mutex_init(&efx->mac_lock);
2519 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002520 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002521 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002522 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002523
2524 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002525 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2526 if (!efx->channel[i])
2527 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002528 }
2529
Ben Hutchings8ceee662008-04-27 12:55:59 +01002530 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2531
2532 /* Higher numbered interrupt modes are less capable! */
2533 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2534 interrupt_mode);
2535
Ben Hutchings6977dc62008-12-26 13:44:39 -08002536 /* Would be good to use the net_dev name, but we're too early */
2537 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2538 pci_name(pci_dev));
2539 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002540 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002541 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002542
Ben Hutchings8ceee662008-04-27 12:55:59 +01002543 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002544
2545fail:
2546 efx_fini_struct(efx);
2547 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002548}
2549
2550static void efx_fini_struct(struct efx_nic *efx)
2551{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002552 int i;
2553
2554 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2555 kfree(efx->channel[i]);
2556
Ben Hutchings8ceee662008-04-27 12:55:59 +01002557 if (efx->workqueue) {
2558 destroy_workqueue(efx->workqueue);
2559 efx->workqueue = NULL;
2560 }
2561}
2562
2563/**************************************************************************
2564 *
2565 * PCI interface
2566 *
2567 **************************************************************************/
2568
2569/* Main body of final NIC shutdown code
2570 * This is called only at module unload (or hotplug removal).
2571 */
2572static void efx_pci_remove_main(struct efx_nic *efx)
2573{
Ben Hutchings7153f622012-07-27 20:50:52 +01002574 /* Flush reset_work. It can no longer be scheduled since we
2575 * are not READY.
2576 */
2577 BUG_ON(efx->state == STATE_READY);
2578 cancel_work_sync(&efx->reset_work);
2579
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002580#ifdef CONFIG_RFS_ACCEL
2581 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2582 efx->net_dev->rx_cpu_rmap = NULL;
2583#endif
Ben Hutchings7f967c02012-02-13 23:45:02 +00002584 efx_stop_interrupts(efx, false);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002585 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002586 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002587 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002588 efx_fini_napi(efx);
2589 efx_remove_all(efx);
2590}
2591
2592/* Final NIC shutdown
2593 * This is called only at module unload (or hotplug removal).
2594 */
2595static void efx_pci_remove(struct pci_dev *pci_dev)
2596{
2597 struct efx_nic *efx;
2598
2599 efx = pci_get_drvdata(pci_dev);
2600 if (!efx)
2601 return;
2602
2603 /* Mark the NIC as fini, then stop the interface */
2604 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002605 dev_close(efx->net_dev);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002606 efx_stop_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002607 rtnl_unlock();
2608
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002609 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002610 efx_unregister_netdev(efx);
2611
Ben Hutchings7dde5962008-12-12 22:09:38 -08002612 efx_mtd_remove(efx);
2613
Ben Hutchings8ceee662008-04-27 12:55:59 +01002614 efx_pci_remove_main(efx);
2615
Ben Hutchings8ceee662008-04-27 12:55:59 +01002616 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002617 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002618
Ben Hutchings8ceee662008-04-27 12:55:59 +01002619 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002620 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002621 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002622
2623 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002624};
2625
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002626/* NIC VPD information
2627 * Called during probe to display the part number of the
2628 * installed NIC. VPD is potentially very large but this should
2629 * always appear within the first 512 bytes.
2630 */
2631#define SFC_VPD_LEN 512
2632static void efx_print_product_vpd(struct efx_nic *efx)
2633{
2634 struct pci_dev *dev = efx->pci_dev;
2635 char vpd_data[SFC_VPD_LEN];
2636 ssize_t vpd_size;
2637 int i, j;
2638
2639 /* Get the vpd data from the device */
2640 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2641 if (vpd_size <= 0) {
2642 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2643 return;
2644 }
2645
2646 /* Get the Read only section */
2647 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2648 if (i < 0) {
2649 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2650 return;
2651 }
2652
2653 j = pci_vpd_lrdt_size(&vpd_data[i]);
2654 i += PCI_VPD_LRDT_TAG_SIZE;
2655 if (i + j > vpd_size)
2656 j = vpd_size - i;
2657
2658 /* Get the Part number */
2659 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2660 if (i < 0) {
2661 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2662 return;
2663 }
2664
2665 j = pci_vpd_info_field_size(&vpd_data[i]);
2666 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2667 if (i + j > vpd_size) {
2668 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2669 return;
2670 }
2671
2672 netif_info(efx, drv, efx->net_dev,
2673 "Part Number : %.*s\n", j, &vpd_data[i]);
2674}
2675
2676
Ben Hutchings8ceee662008-04-27 12:55:59 +01002677/* Main body of NIC initialisation
2678 * This is called at module load (or hotplug insertion, theoretically).
2679 */
2680static int efx_pci_probe_main(struct efx_nic *efx)
2681{
2682 int rc;
2683
2684 /* Do start-of-day initialisation */
2685 rc = efx_probe_all(efx);
2686 if (rc)
2687 goto fail1;
2688
Ben Hutchingse8f14992010-12-07 19:47:34 +00002689 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002690
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002691 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002692 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002693 netif_err(efx, probe, efx->net_dev,
2694 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002695 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002696 }
2697
2698 rc = efx_init_port(efx);
2699 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002700 netif_err(efx, probe, efx->net_dev,
2701 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002702 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002703 }
2704
Ben Hutchings152b6a62009-11-29 03:43:56 +00002705 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002706 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002707 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00002708 efx_start_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002709
2710 return 0;
2711
Ben Hutchings278c0622009-11-23 16:05:12 +00002712 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002713 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002714 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002715 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002716 fail3:
2717 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002718 efx_remove_all(efx);
2719 fail1:
2720 return rc;
2721}
2722
2723/* NIC initialisation
2724 *
2725 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002726 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002727 * sets up and registers the network devices with the kernel and hooks
2728 * the interrupt service routine. It does not prepare the device for
2729 * transmission; this is left to the first time one of the network
2730 * interfaces is brought up (i.e. efx_net_open).
2731 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002732static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002733 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002734{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002735 struct net_device *net_dev;
2736 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002737 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002738
2739 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002740 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2741 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002742 if (!net_dev)
2743 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002744 efx = netdev_priv(net_dev);
2745 efx->type = (const struct efx_nic_type *) entry->driver_data;
2746 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002747 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002748 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002749 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002750 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002751 /* Mask for features that also apply to VLAN devices */
2752 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002753 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2754 NETIF_F_RXCSUM);
2755 /* All offloads can be toggled */
2756 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002757 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002758 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002759 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002760 if (rc)
2761 goto fail1;
2762
Ben Hutchings62776d02010-06-23 11:30:07 +00002763 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002764 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002765
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002766 efx_print_product_vpd(efx);
2767
Ben Hutchings8ceee662008-04-27 12:55:59 +01002768 /* Set up basic I/O (BAR mappings etc) */
2769 rc = efx_init_io(efx);
2770 if (rc)
2771 goto fail2;
2772
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002773 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002774 if (rc)
2775 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002776
Ben Hutchings8ceee662008-04-27 12:55:59 +01002777 rc = efx_register_netdev(efx);
2778 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002779 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002780
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002781 rc = efx_sriov_init(efx);
2782 if (rc)
2783 netif_err(efx, probe, efx->net_dev,
2784 "SR-IOV can't be enabled rc %d\n", rc);
2785
Ben Hutchings62776d02010-06-23 11:30:07 +00002786 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002787
Ben Hutchings7c431612012-01-27 17:23:58 +00002788 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002789 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002790 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002791 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002792 if (rc)
2793 netif_warn(efx, probe, efx->net_dev,
2794 "failed to create MTDs (%d)\n", rc);
2795
Alexandre Rames626950d2013-01-14 17:20:22 +00002796 rc = pci_enable_pcie_error_reporting(pci_dev);
2797 if (rc && rc != -EINVAL)
2798 netif_warn(efx, probe, efx->net_dev,
2799 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2800
Ben Hutchings8ceee662008-04-27 12:55:59 +01002801 return 0;
2802
Ben Hutchings8ceee662008-04-27 12:55:59 +01002803 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002804 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002805 fail3:
2806 efx_fini_io(efx);
2807 fail2:
2808 efx_fini_struct(efx);
2809 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002810 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002811 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002812 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002813 free_netdev(net_dev);
2814 return rc;
2815}
2816
Ben Hutchings89c758f2009-11-29 03:43:07 +00002817static int efx_pm_freeze(struct device *dev)
2818{
2819 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2820
Ben Hutchings61da0262012-07-27 19:35:39 +01002821 rtnl_lock();
2822
Ben Hutchings6032fb52012-07-27 19:35:47 +01002823 if (efx->state != STATE_DISABLED) {
2824 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002825
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002826 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002827
Ben Hutchings6032fb52012-07-27 19:35:47 +01002828 efx_stop_all(efx);
2829 efx_stop_interrupts(efx, false);
2830 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002831
Ben Hutchings61da0262012-07-27 19:35:39 +01002832 rtnl_unlock();
2833
Ben Hutchings89c758f2009-11-29 03:43:07 +00002834 return 0;
2835}
2836
2837static int efx_pm_thaw(struct device *dev)
2838{
2839 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2840
Ben Hutchings61da0262012-07-27 19:35:39 +01002841 rtnl_lock();
2842
Ben Hutchings6032fb52012-07-27 19:35:47 +01002843 if (efx->state != STATE_DISABLED) {
2844 efx_start_interrupts(efx, false);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002845
Ben Hutchings6032fb52012-07-27 19:35:47 +01002846 mutex_lock(&efx->mac_lock);
2847 efx->phy_op->reconfigure(efx);
2848 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002849
Ben Hutchings6032fb52012-07-27 19:35:47 +01002850 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002851
Ben Hutchings6032fb52012-07-27 19:35:47 +01002852 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002853
Ben Hutchings6032fb52012-07-27 19:35:47 +01002854 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002855
Ben Hutchings6032fb52012-07-27 19:35:47 +01002856 efx->type->resume_wol(efx);
2857 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002858
Ben Hutchings61da0262012-07-27 19:35:39 +01002859 rtnl_unlock();
2860
Steve Hodgson319ba642010-06-01 11:17:24 +00002861 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2862 queue_work(reset_workqueue, &efx->reset_work);
2863
Ben Hutchings89c758f2009-11-29 03:43:07 +00002864 return 0;
2865}
2866
2867static int efx_pm_poweroff(struct device *dev)
2868{
2869 struct pci_dev *pci_dev = to_pci_dev(dev);
2870 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2871
2872 efx->type->fini(efx);
2873
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002874 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002875
2876 pci_save_state(pci_dev);
2877 return pci_set_power_state(pci_dev, PCI_D3hot);
2878}
2879
2880/* Used for both resume and restore */
2881static int efx_pm_resume(struct device *dev)
2882{
2883 struct pci_dev *pci_dev = to_pci_dev(dev);
2884 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2885 int rc;
2886
2887 rc = pci_set_power_state(pci_dev, PCI_D0);
2888 if (rc)
2889 return rc;
2890 pci_restore_state(pci_dev);
2891 rc = pci_enable_device(pci_dev);
2892 if (rc)
2893 return rc;
2894 pci_set_master(efx->pci_dev);
2895 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2896 if (rc)
2897 return rc;
2898 rc = efx->type->init(efx);
2899 if (rc)
2900 return rc;
2901 efx_pm_thaw(dev);
2902 return 0;
2903}
2904
2905static int efx_pm_suspend(struct device *dev)
2906{
2907 int rc;
2908
2909 efx_pm_freeze(dev);
2910 rc = efx_pm_poweroff(dev);
2911 if (rc)
2912 efx_pm_resume(dev);
2913 return rc;
2914}
2915
Ben Hutchings18e83e42012-01-05 19:05:20 +00002916static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002917 .suspend = efx_pm_suspend,
2918 .resume = efx_pm_resume,
2919 .freeze = efx_pm_freeze,
2920 .thaw = efx_pm_thaw,
2921 .poweroff = efx_pm_poweroff,
2922 .restore = efx_pm_resume,
2923};
2924
Alexandre Rames626950d2013-01-14 17:20:22 +00002925/* A PCI error affecting this device was detected.
2926 * At this point MMIO and DMA may be disabled.
2927 * Stop the software path and request a slot reset.
2928 */
2929pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2930 enum pci_channel_state state)
2931{
2932 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2933 struct efx_nic *efx = pci_get_drvdata(pdev);
2934
2935 if (state == pci_channel_io_perm_failure)
2936 return PCI_ERS_RESULT_DISCONNECT;
2937
2938 rtnl_lock();
2939
2940 if (efx->state != STATE_DISABLED) {
2941 efx->state = STATE_RECOVERY;
2942 efx->reset_pending = 0;
2943
2944 efx_device_detach_sync(efx);
2945
2946 efx_stop_all(efx);
2947 efx_stop_interrupts(efx, false);
2948
2949 status = PCI_ERS_RESULT_NEED_RESET;
2950 } else {
2951 /* If the interface is disabled we don't want to do anything
2952 * with it.
2953 */
2954 status = PCI_ERS_RESULT_RECOVERED;
2955 }
2956
2957 rtnl_unlock();
2958
2959 pci_disable_device(pdev);
2960
2961 return status;
2962}
2963
2964/* Fake a successfull reset, which will be performed later in efx_io_resume. */
2965pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
2966{
2967 struct efx_nic *efx = pci_get_drvdata(pdev);
2968 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2969 int rc;
2970
2971 if (pci_enable_device(pdev)) {
2972 netif_err(efx, hw, efx->net_dev,
2973 "Cannot re-enable PCI device after reset.\n");
2974 status = PCI_ERS_RESULT_DISCONNECT;
2975 }
2976
2977 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2978 if (rc) {
2979 netif_err(efx, hw, efx->net_dev,
2980 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2981 /* Non-fatal error. Continue. */
2982 }
2983
2984 return status;
2985}
2986
2987/* Perform the actual reset and resume I/O operations. */
2988static void efx_io_resume(struct pci_dev *pdev)
2989{
2990 struct efx_nic *efx = pci_get_drvdata(pdev);
2991 int rc;
2992
2993 rtnl_lock();
2994
2995 if (efx->state == STATE_DISABLED)
2996 goto out;
2997
2998 rc = efx_reset(efx, RESET_TYPE_ALL);
2999 if (rc) {
3000 netif_err(efx, hw, efx->net_dev,
3001 "efx_reset failed after PCI error (%d)\n", rc);
3002 } else {
3003 efx->state = STATE_READY;
3004 netif_dbg(efx, hw, efx->net_dev,
3005 "Done resetting and resuming IO after PCI error.\n");
3006 }
3007
3008out:
3009 rtnl_unlock();
3010}
3011
3012/* For simplicity and reliability, we always require a slot reset and try to
3013 * reset the hardware when a pci error affecting the device is detected.
3014 * We leave both the link_reset and mmio_enabled callback unimplemented:
3015 * with our request for slot reset the mmio_enabled callback will never be
3016 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3017 */
3018static struct pci_error_handlers efx_err_handlers = {
3019 .error_detected = efx_io_error_detected,
3020 .slot_reset = efx_io_slot_reset,
3021 .resume = efx_io_resume,
3022};
3023
Ben Hutchings8ceee662008-04-27 12:55:59 +01003024static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003025 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003026 .id_table = efx_pci_table,
3027 .probe = efx_pci_probe,
3028 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003029 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003030 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003031};
3032
3033/**************************************************************************
3034 *
3035 * Kernel module interface
3036 *
3037 *************************************************************************/
3038
3039module_param(interrupt_mode, uint, 0444);
3040MODULE_PARM_DESC(interrupt_mode,
3041 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3042
3043static int __init efx_init_module(void)
3044{
3045 int rc;
3046
3047 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3048
3049 rc = register_netdevice_notifier(&efx_netdev_notifier);
3050 if (rc)
3051 goto err_notifier;
3052
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003053 rc = efx_init_sriov();
3054 if (rc)
3055 goto err_sriov;
3056
Steve Hodgson1ab00622008-12-12 21:33:02 -08003057 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3058 if (!reset_workqueue) {
3059 rc = -ENOMEM;
3060 goto err_reset;
3061 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003062
3063 rc = pci_register_driver(&efx_pci_driver);
3064 if (rc < 0)
3065 goto err_pci;
3066
3067 return 0;
3068
3069 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003070 destroy_workqueue(reset_workqueue);
3071 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003072 efx_fini_sriov();
3073 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003074 unregister_netdevice_notifier(&efx_netdev_notifier);
3075 err_notifier:
3076 return rc;
3077}
3078
3079static void __exit efx_exit_module(void)
3080{
3081 printk(KERN_INFO "Solarflare NET driver unloading\n");
3082
3083 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003084 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003085 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003086 unregister_netdevice_notifier(&efx_netdev_notifier);
3087
3088}
3089
3090module_init(efx_init_module);
3091module_exit(efx_exit_module);
3092
Ben Hutchings906bb262009-11-29 15:16:19 +00003093MODULE_AUTHOR("Solarflare Communications and "
3094 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003095MODULE_DESCRIPTION("Solarflare Communications network driver");
3096MODULE_LICENSE("GPL");
3097MODULE_DEVICE_TABLE(pci, efx_pci_table);