blob: f8013c3ea37cd45fefc3e70cba9fcc3f1f7ca91b [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 Hutchingsff734ef2013-01-29 23:33:14 +0000253 efx_rx_flush_packet(channel);
Alexandre Rames97d48a12013-01-11 12:26:21 +0000254 if (rx_queue->enabled)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000255 efx_fast_push_rx_descriptors(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100256 }
257
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000258 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100259}
260
261/* Mark channel as finished processing
262 *
263 * Note that since we will not receive further interrupts for this
264 * channel before we finish processing and call the eventq_read_ack()
265 * method, there is no need to use the interrupt hold-off timers.
266 */
267static inline void efx_channel_processed(struct efx_channel *channel)
268{
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100269 /* The interrupt handler for this channel may set work_pending
270 * as soon as we acknowledge the events we've seen. Make sure
271 * it's cleared before then. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100272 channel->work_pending = false;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100273 smp_wmb();
274
Ben Hutchings152b6a62009-11-29 03:43:56 +0000275 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100276}
277
278/* NAPI poll handler
279 *
280 * NAPI guarantees serialisation of polls of the same device, which
281 * provides the guarantee required by efx_process_channel().
282 */
283static int efx_poll(struct napi_struct *napi, int budget)
284{
285 struct efx_channel *channel =
286 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000287 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000288 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100289
Ben Hutchings62776d02010-06-23 11:30:07 +0000290 netif_vdbg(efx, intr, efx->net_dev,
291 "channel %d NAPI poll executing on CPU %d\n",
292 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100293
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000294 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100295
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000296 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000297 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000298 efx->irq_rx_adaptive &&
299 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000300 if (unlikely(channel->irq_mod_score <
301 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000302 if (channel->irq_moderation > 1) {
303 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000304 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000305 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000306 } else if (unlikely(channel->irq_mod_score >
307 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000308 if (channel->irq_moderation <
309 efx->irq_rx_moderation) {
310 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000311 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000312 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000313 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000314 channel->irq_count = 0;
315 channel->irq_mod_score = 0;
316 }
317
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000318 efx_filter_rfs_expire(channel);
319
Ben Hutchings8ceee662008-04-27 12:55:59 +0100320 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800321 * only wait for napi_complete(), this isn't a problem
Ben Hutchings8ceee662008-04-27 12:55:59 +0100322 * since efx_channel_processed() will have no effect if
323 * interrupts have already been disabled.
324 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800325 napi_complete(napi);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100326 efx_channel_processed(channel);
327 }
328
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000329 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100330}
331
332/* Process the eventq of the specified channel immediately on this CPU
333 *
334 * Disable hardware generated interrupts, wait for any existing
335 * processing to finish, then directly poll (and ack ) the eventq.
336 * Finally reenable NAPI and interrupts.
337 *
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100338 * This is for use only during a loopback self-test. It must not
339 * deliver any packets up the stack as this can result in deadlock.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100340 */
341void efx_process_channel_now(struct efx_channel *channel)
342{
343 struct efx_nic *efx = channel->efx;
344
Ben Hutchings8313aca2010-09-10 06:41:57 +0000345 BUG_ON(channel->channel >= efx->n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100346 BUG_ON(!channel->enabled);
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100347 BUG_ON(!efx->loopback_selftest);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100348
349 /* Disable interrupts and wait for ISRs to complete */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000350 efx_nic_disable_interrupts(efx);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000351 if (efx->legacy_irq) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100352 synchronize_irq(efx->legacy_irq);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000353 efx->legacy_irq_enabled = false;
354 }
Ben Hutchings64ee3122008-09-01 12:47:38 +0100355 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100356 synchronize_irq(channel->irq);
357
358 /* Wait for any NAPI processing to complete */
359 napi_disable(&channel->napi_str);
360
361 /* Poll the channel */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000362 efx_process_channel(channel, channel->eventq_mask + 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100363
364 /* Ack the eventq. This may cause an interrupt to be generated
365 * when they are reenabled */
366 efx_channel_processed(channel);
367
368 napi_enable(&channel->napi_str);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000369 if (efx->legacy_irq)
370 efx->legacy_irq_enabled = true;
Ben Hutchings152b6a62009-11-29 03:43:56 +0000371 efx_nic_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100372}
373
374/* Create event queue
375 * Event queue memory allocations are done only once. If the channel
376 * is reset, the memory buffer will be reused; this guards against
377 * errors during channel reset and also simplifies interrupt handling.
378 */
379static int efx_probe_eventq(struct efx_channel *channel)
380{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000381 struct efx_nic *efx = channel->efx;
382 unsigned long entries;
383
Ben Hutchings86ee5302012-01-09 19:51:22 +0000384 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000385 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100386
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000387 /* Build an event queue with room for one event per tx and rx buffer,
388 * plus some extra for link state events and MCDI completions. */
389 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
390 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
391 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
392
Ben Hutchings152b6a62009-11-29 03:43:56 +0000393 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100394}
395
396/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100397static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100398{
Ben Hutchings62776d02010-06-23 11:30:07 +0000399 netif_dbg(channel->efx, drv, channel->efx->net_dev,
400 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100401
402 channel->eventq_read_ptr = 0;
403
Ben Hutchings152b6a62009-11-29 03:43:56 +0000404 efx_nic_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100405}
406
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000407/* Enable event queue processing and NAPI */
408static void efx_start_eventq(struct efx_channel *channel)
409{
410 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
411 "chan %d start event queue\n", channel->channel);
412
413 /* The interrupt handler for this channel may set work_pending
414 * as soon as we enable it. Make sure it's cleared before
415 * then. Similarly, make sure it sees the enabled flag set.
416 */
417 channel->work_pending = false;
418 channel->enabled = true;
419 smp_wmb();
420
421 napi_enable(&channel->napi_str);
422 efx_nic_eventq_read_ack(channel);
423}
424
425/* Disable event queue processing and NAPI */
426static void efx_stop_eventq(struct efx_channel *channel)
427{
428 if (!channel->enabled)
429 return;
430
431 napi_disable(&channel->napi_str);
432 channel->enabled = false;
433}
434
Ben Hutchings8ceee662008-04-27 12:55:59 +0100435static void efx_fini_eventq(struct efx_channel *channel)
436{
Ben Hutchings62776d02010-06-23 11:30:07 +0000437 netif_dbg(channel->efx, drv, channel->efx->net_dev,
438 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100439
Ben Hutchings152b6a62009-11-29 03:43:56 +0000440 efx_nic_fini_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100441}
442
443static void efx_remove_eventq(struct efx_channel *channel)
444{
Ben Hutchings62776d02010-06-23 11:30:07 +0000445 netif_dbg(channel->efx, drv, channel->efx->net_dev,
446 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100447
Ben Hutchings152b6a62009-11-29 03:43:56 +0000448 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100449}
450
451/**************************************************************************
452 *
453 * Channel handling
454 *
455 *************************************************************************/
456
Ben Hutchings7f967c02012-02-13 23:45:02 +0000457/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000458static struct efx_channel *
459efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
460{
461 struct efx_channel *channel;
462 struct efx_rx_queue *rx_queue;
463 struct efx_tx_queue *tx_queue;
464 int j;
465
Ben Hutchings7f967c02012-02-13 23:45:02 +0000466 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
467 if (!channel)
468 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000469
Ben Hutchings7f967c02012-02-13 23:45:02 +0000470 channel->efx = efx;
471 channel->channel = i;
472 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000473
Ben Hutchings7f967c02012-02-13 23:45:02 +0000474 for (j = 0; j < EFX_TXQ_TYPES; j++) {
475 tx_queue = &channel->tx_queue[j];
476 tx_queue->efx = efx;
477 tx_queue->queue = i * EFX_TXQ_TYPES + j;
478 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000479 }
480
Ben Hutchings46426102010-09-10 06:42:33 +0000481 rx_queue = &channel->rx_queue;
482 rx_queue->efx = efx;
483 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
484 (unsigned long)rx_queue);
485
486 return channel;
487}
488
Ben Hutchings7f967c02012-02-13 23:45:02 +0000489/* Allocate and initialise a channel structure, copying parameters
490 * (but not resources) from an old channel structure.
491 */
492static struct efx_channel *
493efx_copy_channel(const struct efx_channel *old_channel)
494{
495 struct efx_channel *channel;
496 struct efx_rx_queue *rx_queue;
497 struct efx_tx_queue *tx_queue;
498 int j;
499
500 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
501 if (!channel)
502 return NULL;
503
504 *channel = *old_channel;
505
506 channel->napi_dev = NULL;
507 memset(&channel->eventq, 0, sizeof(channel->eventq));
508
509 for (j = 0; j < EFX_TXQ_TYPES; j++) {
510 tx_queue = &channel->tx_queue[j];
511 if (tx_queue->channel)
512 tx_queue->channel = channel;
513 tx_queue->buffer = NULL;
514 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
515 }
516
517 rx_queue = &channel->rx_queue;
518 rx_queue->buffer = NULL;
519 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
520 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
521 (unsigned long)rx_queue);
522
523 return channel;
524}
525
Ben Hutchings8ceee662008-04-27 12:55:59 +0100526static int efx_probe_channel(struct efx_channel *channel)
527{
528 struct efx_tx_queue *tx_queue;
529 struct efx_rx_queue *rx_queue;
530 int rc;
531
Ben Hutchings62776d02010-06-23 11:30:07 +0000532 netif_dbg(channel->efx, probe, channel->efx->net_dev,
533 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100534
Ben Hutchings7f967c02012-02-13 23:45:02 +0000535 rc = channel->type->pre_probe(channel);
536 if (rc)
537 goto fail;
538
Ben Hutchings8ceee662008-04-27 12:55:59 +0100539 rc = efx_probe_eventq(channel);
540 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000541 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100542
543 efx_for_each_channel_tx_queue(tx_queue, channel) {
544 rc = efx_probe_tx_queue(tx_queue);
545 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000546 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100547 }
548
549 efx_for_each_channel_rx_queue(rx_queue, channel) {
550 rc = efx_probe_rx_queue(rx_queue);
551 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000552 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100553 }
554
555 channel->n_rx_frm_trunc = 0;
556
557 return 0;
558
Ben Hutchings7f967c02012-02-13 23:45:02 +0000559fail:
560 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100561 return rc;
562}
563
Ben Hutchings7f967c02012-02-13 23:45:02 +0000564static void
565efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
566{
567 struct efx_nic *efx = channel->efx;
568 const char *type;
569 int number;
570
571 number = channel->channel;
572 if (efx->tx_channel_offset == 0) {
573 type = "";
574 } else if (channel->channel < efx->tx_channel_offset) {
575 type = "-rx";
576 } else {
577 type = "-tx";
578 number -= efx->tx_channel_offset;
579 }
580 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
581}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100582
Ben Hutchings56536e92008-12-12 21:37:02 -0800583static void efx_set_channel_names(struct efx_nic *efx)
584{
585 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800586
Ben Hutchings7f967c02012-02-13 23:45:02 +0000587 efx_for_each_channel(channel, efx)
588 channel->type->get_name(channel,
589 efx->channel_name[channel->channel],
590 sizeof(efx->channel_name[0]));
Ben Hutchings56536e92008-12-12 21:37:02 -0800591}
592
Ben Hutchings46426102010-09-10 06:42:33 +0000593static int efx_probe_channels(struct efx_nic *efx)
594{
595 struct efx_channel *channel;
596 int rc;
597
598 /* Restart special buffer allocation */
599 efx->next_buffer_table = 0;
600
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000601 /* Probe channels in reverse, so that any 'extra' channels
602 * use the start of the buffer table. This allows the traffic
603 * channels to be resized without moving them or wasting the
604 * entries before them.
605 */
606 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000607 rc = efx_probe_channel(channel);
608 if (rc) {
609 netif_err(efx, probe, efx->net_dev,
610 "failed to create channel %d\n",
611 channel->channel);
612 goto fail;
613 }
614 }
615 efx_set_channel_names(efx);
616
617 return 0;
618
619fail:
620 efx_remove_channels(efx);
621 return rc;
622}
623
Ben Hutchings8ceee662008-04-27 12:55:59 +0100624/* Channels are shutdown and reinitialised whilst the NIC is running
625 * to propagate configuration changes (mtu, checksum offload), or
626 * to clear hardware error conditions
627 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000628static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100629{
630 struct efx_tx_queue *tx_queue;
631 struct efx_rx_queue *rx_queue;
632 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100633
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100634 /* Calculate the rx buffer allocation parameters required to
635 * support the current MTU, including padding for header
636 * alignment and overruns.
637 */
Ben Hutchings272baee2013-01-29 23:33:14 +0000638 efx->rx_dma_len = (efx->type->rx_buffer_hash_size +
639 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
640 efx->type->rx_buffer_padding);
641 efx->rx_buffer_order = get_order(sizeof(struct efx_rx_page_state) +
642 EFX_PAGE_IP_ALIGN + efx->rx_dma_len);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100643
Ben Hutchings14bf7182012-05-22 01:27:58 +0100644 /* We must keep at least one descriptor in a TX ring empty.
645 * We could avoid this when the queue size does not exactly
646 * match the hardware ring size, but it's not that important.
647 * Therefore we stop the queue when one more skb might fill
648 * the ring completely. We wake it when half way back to
649 * empty.
650 */
651 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
652 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
653
Ben Hutchings8ceee662008-04-27 12:55:59 +0100654 /* Initialise the channels */
655 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100656 efx_for_each_channel_tx_queue(tx_queue, channel)
657 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100658
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000659 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100660 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000661 efx_nic_generate_fill_event(rx_queue);
662 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100663
664 WARN_ON(channel->rx_pkt != NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100665 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000666
667 if (netif_device_present(efx->net_dev))
668 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100669}
670
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000671static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100672{
673 struct efx_channel *channel;
674 struct efx_tx_queue *tx_queue;
675 struct efx_rx_queue *rx_queue;
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100676 struct pci_dev *dev = efx->pci_dev;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100677 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100678
679 EFX_ASSERT_RESET_SERIALISED(efx);
680 BUG_ON(efx->port_enabled);
681
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100682 /* Only perform flush if dma is enabled */
Alexandre Rames626950d2013-01-14 17:20:22 +0000683 if (dev->is_busmaster && efx->state != STATE_RECOVERY) {
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100684 rc = efx_nic_flush_queues(efx);
685
686 if (rc && EFX_WORKAROUND_7803(efx)) {
687 /* Schedule a reset to recover from the flush failure. The
688 * descriptor caches reference memory we're about to free,
689 * but falcon_reconfigure_mac_wrapper() won't reconnect
690 * the MACs because of the pending reset. */
691 netif_err(efx, drv, efx->net_dev,
692 "Resetting to recover from flush failure\n");
693 efx_schedule_reset(efx, RESET_TYPE_ALL);
694 } else if (rc) {
695 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
696 } else {
697 netif_dbg(efx, drv, efx->net_dev,
698 "successfully flushed all queues\n");
699 }
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000700 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100701
Ben Hutchings8ceee662008-04-27 12:55:59 +0100702 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000703 /* RX packet processing is pipelined, so wait for the
704 * NAPI handler to complete. At least event queue 0
705 * might be kept active by non-data events, so don't
706 * use napi_synchronize() but actually disable NAPI
707 * temporarily.
708 */
709 if (efx_channel_has_rx_queue(channel)) {
710 efx_stop_eventq(channel);
711 efx_start_eventq(channel);
712 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100713
714 efx_for_each_channel_rx_queue(rx_queue, channel)
715 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000716 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100717 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100718 }
719}
720
721static void efx_remove_channel(struct efx_channel *channel)
722{
723 struct efx_tx_queue *tx_queue;
724 struct efx_rx_queue *rx_queue;
725
Ben Hutchings62776d02010-06-23 11:30:07 +0000726 netif_dbg(channel->efx, drv, channel->efx->net_dev,
727 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100728
729 efx_for_each_channel_rx_queue(rx_queue, channel)
730 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000731 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100732 efx_remove_tx_queue(tx_queue);
733 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100734 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100735}
736
Ben Hutchings46426102010-09-10 06:42:33 +0000737static void efx_remove_channels(struct efx_nic *efx)
738{
739 struct efx_channel *channel;
740
741 efx_for_each_channel(channel, efx)
742 efx_remove_channel(channel);
743}
744
745int
746efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
747{
748 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
749 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000750 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100751 int rc;
752
753 rc = efx_check_disabled(efx);
754 if (rc)
755 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000756
757 /* Not all channels should be reallocated. We must avoid
758 * reallocating their buffer table entries.
759 */
760 efx_for_each_channel(channel, efx) {
761 struct efx_rx_queue *rx_queue;
762 struct efx_tx_queue *tx_queue;
763
764 if (channel->type->copy)
765 continue;
766 next_buffer_table = max(next_buffer_table,
767 channel->eventq.index +
768 channel->eventq.entries);
769 efx_for_each_channel_rx_queue(rx_queue, channel)
770 next_buffer_table = max(next_buffer_table,
771 rx_queue->rxd.index +
772 rx_queue->rxd.entries);
773 efx_for_each_channel_tx_queue(tx_queue, channel)
774 next_buffer_table = max(next_buffer_table,
775 tx_queue->txd.index +
776 tx_queue->txd.entries);
777 }
Ben Hutchings46426102010-09-10 06:42:33 +0000778
Ben Hutchings29c69a42013-01-28 19:01:06 +0000779 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000780 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000781 efx_stop_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000782
Ben Hutchings7f967c02012-02-13 23:45:02 +0000783 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000784 memset(other_channel, 0, sizeof(other_channel));
785 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000786 channel = efx->channel[i];
787 if (channel->type->copy)
788 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000789 if (!channel) {
790 rc = -ENOMEM;
791 goto out;
792 }
793 other_channel[i] = channel;
794 }
795
796 /* Swap entry counts and channel pointers */
797 old_rxq_entries = efx->rxq_entries;
798 old_txq_entries = efx->txq_entries;
799 efx->rxq_entries = rxq_entries;
800 efx->txq_entries = txq_entries;
801 for (i = 0; i < efx->n_channels; i++) {
802 channel = efx->channel[i];
803 efx->channel[i] = other_channel[i];
804 other_channel[i] = channel;
805 }
806
Ben Hutchings7f967c02012-02-13 23:45:02 +0000807 /* Restart buffer table allocation */
808 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000809
Ben Hutchingse8f14992010-12-07 19:47:34 +0000810 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000811 channel = efx->channel[i];
812 if (!channel->type->copy)
813 continue;
814 rc = efx_probe_channel(channel);
815 if (rc)
816 goto rollback;
817 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000818 }
Ben Hutchings46426102010-09-10 06:42:33 +0000819
Ben Hutchings7f967c02012-02-13 23:45:02 +0000820out:
821 /* Destroy unused channel structures */
822 for (i = 0; i < efx->n_channels; i++) {
823 channel = other_channel[i];
824 if (channel && channel->type->copy) {
825 efx_fini_napi_channel(channel);
826 efx_remove_channel(channel);
827 kfree(channel);
828 }
829 }
830
831 efx_start_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000832 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000833 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000834 return rc;
835
836rollback:
837 /* Swap back */
838 efx->rxq_entries = old_rxq_entries;
839 efx->txq_entries = old_txq_entries;
840 for (i = 0; i < efx->n_channels; i++) {
841 channel = efx->channel[i];
842 efx->channel[i] = other_channel[i];
843 other_channel[i] = channel;
844 }
845 goto out;
846}
847
Steve Hodgson90d683a2010-06-01 11:19:39 +0000848void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100849{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000850 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100851}
852
Ben Hutchings7f967c02012-02-13 23:45:02 +0000853static const struct efx_channel_type efx_default_channel_type = {
854 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100855 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000856 .get_name = efx_get_channel_name,
857 .copy = efx_copy_channel,
858 .keep_eventq = false,
859};
860
861int efx_channel_dummy_op_int(struct efx_channel *channel)
862{
863 return 0;
864}
865
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100866void efx_channel_dummy_op_void(struct efx_channel *channel)
867{
868}
869
Ben Hutchings8ceee662008-04-27 12:55:59 +0100870/**************************************************************************
871 *
872 * Port handling
873 *
874 **************************************************************************/
875
876/* This ensures that the kernel is kept informed (via
877 * netif_carrier_on/off) of the link status, and also maintains the
878 * link status's stop on the port's TX queue.
879 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000880void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100881{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000882 struct efx_link_state *link_state = &efx->link_state;
883
Ben Hutchings8ceee662008-04-27 12:55:59 +0100884 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
885 * that no events are triggered between unregister_netdev() and the
886 * driver unloading. A more general condition is that NETDEV_CHANGE
887 * can only be generated between NETDEV_UP and NETDEV_DOWN */
888 if (!netif_running(efx->net_dev))
889 return;
890
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000891 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100892 efx->n_link_state_changes++;
893
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000894 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100895 netif_carrier_on(efx->net_dev);
896 else
897 netif_carrier_off(efx->net_dev);
898 }
899
900 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000901 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000902 netif_info(efx, link, efx->net_dev,
903 "link up at %uMbps %s-duplex (MTU %d)%s\n",
904 link_state->speed, link_state->fd ? "full" : "half",
905 efx->net_dev->mtu,
906 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000907 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000908 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100909}
910
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000911void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
912{
913 efx->link_advertising = advertising;
914 if (advertising) {
915 if (advertising & ADVERTISED_Pause)
916 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
917 else
918 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
919 if (advertising & ADVERTISED_Asym_Pause)
920 efx->wanted_fc ^= EFX_FC_TX;
921 }
922}
923
David S. Millerb56269462011-05-17 17:53:22 -0400924void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000925{
926 efx->wanted_fc = wanted_fc;
927 if (efx->link_advertising) {
928 if (wanted_fc & EFX_FC_RX)
929 efx->link_advertising |= (ADVERTISED_Pause |
930 ADVERTISED_Asym_Pause);
931 else
932 efx->link_advertising &= ~(ADVERTISED_Pause |
933 ADVERTISED_Asym_Pause);
934 if (wanted_fc & EFX_FC_TX)
935 efx->link_advertising ^= ADVERTISED_Asym_Pause;
936 }
937}
938
Ben Hutchings115122a2009-03-04 09:52:52 +0000939static void efx_fini_port(struct efx_nic *efx);
940
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000941/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
942 * the MAC appropriately. All other PHY configuration changes are pushed
943 * through phy_op->set_settings(), and pushed asynchronously to the MAC
944 * through efx_monitor().
945 *
946 * Callers must hold the mac_lock
947 */
948int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100949{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000950 enum efx_phy_mode phy_mode;
951 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100952
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000953 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100954
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000955 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000956 netif_addr_lock_bh(efx->net_dev);
957 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100958
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000959 /* Disable PHY transmit in mac level loopbacks */
960 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800961 if (LOOPBACK_INTERNAL(efx))
962 efx->phy_mode |= PHY_MODE_TX_DISABLED;
963 else
964 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800965
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000966 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800967
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000968 if (rc)
969 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100970
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000971 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100972}
973
974/* Reinitialise the MAC to pick up new PHY settings, even if the port is
975 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000976int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100977{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000978 int rc;
979
Ben Hutchings8ceee662008-04-27 12:55:59 +0100980 EFX_ASSERT_RESET_SERIALISED(efx);
981
982 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000983 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100984 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000985
986 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100987}
988
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000989/* Asynchronous work item for changing MAC promiscuity and multicast
990 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
991 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800992static void efx_mac_work(struct work_struct *data)
993{
994 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
995
996 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100997 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100998 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800999 mutex_unlock(&efx->mac_lock);
1000}
1001
Ben Hutchings8ceee662008-04-27 12:55:59 +01001002static int efx_probe_port(struct efx_nic *efx)
1003{
1004 int rc;
1005
Ben Hutchings62776d02010-06-23 11:30:07 +00001006 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001007
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001008 if (phy_flash_cfg)
1009 efx->phy_mode = PHY_MODE_SPECIAL;
1010
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001011 /* Connect up MAC/PHY operations table */
1012 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001013 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001014 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001015
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001016 /* Initialise MAC address to permanent address */
1017 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001018
1019 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001020}
1021
1022static int efx_init_port(struct efx_nic *efx)
1023{
1024 int rc;
1025
Ben Hutchings62776d02010-06-23 11:30:07 +00001026 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001027
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001028 mutex_lock(&efx->mac_lock);
1029
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001030 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001031 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001032 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001033
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001034 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001035
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001036 /* Reconfigure the MAC before creating dma queues (required for
1037 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001038 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001039
1040 /* Ensure the PHY advertises the correct flow control settings */
1041 rc = efx->phy_op->reconfigure(efx);
1042 if (rc)
1043 goto fail2;
1044
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001045 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001046 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001047
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001048fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001049 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001050fail1:
1051 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001052 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001053}
1054
Ben Hutchings8ceee662008-04-27 12:55:59 +01001055static void efx_start_port(struct efx_nic *efx)
1056{
Ben Hutchings62776d02010-06-23 11:30:07 +00001057 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001058 BUG_ON(efx->port_enabled);
1059
1060 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001061 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001062
1063 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1064 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001065 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001066
Ben Hutchings8ceee662008-04-27 12:55:59 +01001067 mutex_unlock(&efx->mac_lock);
1068}
1069
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001070/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001071static void efx_stop_port(struct efx_nic *efx)
1072{
Ben Hutchings62776d02010-06-23 11:30:07 +00001073 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001074
1075 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001076 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001077 mutex_unlock(&efx->mac_lock);
1078
1079 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001080 netif_addr_lock_bh(efx->net_dev);
1081 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001082}
1083
1084static void efx_fini_port(struct efx_nic *efx)
1085{
Ben Hutchings62776d02010-06-23 11:30:07 +00001086 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001087
1088 if (!efx->port_initialized)
1089 return;
1090
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001091 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001092 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001093
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001094 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001095 efx_link_status_changed(efx);
1096}
1097
1098static void efx_remove_port(struct efx_nic *efx)
1099{
Ben Hutchings62776d02010-06-23 11:30:07 +00001100 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001101
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001102 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001103}
1104
1105/**************************************************************************
1106 *
1107 * NIC handling
1108 *
1109 **************************************************************************/
1110
1111/* This configures the PCI device to enable I/O and DMA. */
1112static int efx_init_io(struct efx_nic *efx)
1113{
1114 struct pci_dev *pci_dev = efx->pci_dev;
1115 dma_addr_t dma_mask = efx->type->max_dma_mask;
1116 int rc;
1117
Ben Hutchings62776d02010-06-23 11:30:07 +00001118 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001119
1120 rc = pci_enable_device(pci_dev);
1121 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001122 netif_err(efx, probe, efx->net_dev,
1123 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001124 goto fail1;
1125 }
1126
1127 pci_set_master(pci_dev);
1128
1129 /* Set the PCI DMA mask. Try all possibilities from our
1130 * genuine mask down to 32 bits, because some architectures
1131 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1132 * masks event though they reject 46 bit masks.
1133 */
1134 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001135 if (dma_supported(&pci_dev->dev, dma_mask)) {
1136 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001137 if (rc == 0)
1138 break;
1139 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001140 dma_mask >>= 1;
1141 }
1142 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001143 netif_err(efx, probe, efx->net_dev,
1144 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001145 goto fail2;
1146 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001147 netif_dbg(efx, probe, efx->net_dev,
1148 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001149 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001150 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001151 /* dma_set_coherent_mask() is not *allowed* to
1152 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001153 * but just in case...
1154 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001155 netif_err(efx, probe, efx->net_dev,
1156 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001157 goto fail2;
1158 }
1159
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001160 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1161 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001162 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001163 netif_err(efx, probe, efx->net_dev,
1164 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001165 rc = -EIO;
1166 goto fail3;
1167 }
David S. Miller8decf862011-09-22 03:23:13 -04001168 efx->membase = ioremap_nocache(efx->membase_phys,
1169 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001170 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001171 netif_err(efx, probe, efx->net_dev,
1172 "could not map memory BAR at %llx+%x\n",
1173 (unsigned long long)efx->membase_phys,
1174 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001175 rc = -ENOMEM;
1176 goto fail4;
1177 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001178 netif_dbg(efx, probe, efx->net_dev,
1179 "memory BAR at %llx+%x (virtual %p)\n",
1180 (unsigned long long)efx->membase_phys,
1181 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001182
1183 return 0;
1184
1185 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001186 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001187 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001188 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001189 fail2:
1190 pci_disable_device(efx->pci_dev);
1191 fail1:
1192 return rc;
1193}
1194
1195static void efx_fini_io(struct efx_nic *efx)
1196{
Ben Hutchings62776d02010-06-23 11:30:07 +00001197 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001198
1199 if (efx->membase) {
1200 iounmap(efx->membase);
1201 efx->membase = NULL;
1202 }
1203
1204 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001205 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001206 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001207 }
1208
1209 pci_disable_device(efx->pci_dev);
1210}
1211
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001212static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001213{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001214 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001215 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001216 int cpu;
1217
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001218 if (rss_cpus) {
1219 count = rss_cpus;
1220 } else {
1221 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1222 netif_warn(efx, probe, efx->net_dev,
1223 "RSS disabled due to allocation failure\n");
1224 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001225 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001226
1227 count = 0;
1228 for_each_online_cpu(cpu) {
1229 if (!cpumask_test_cpu(cpu, thread_mask)) {
1230 ++count;
1231 cpumask_or(thread_mask, thread_mask,
1232 topology_thread_cpumask(cpu));
1233 }
1234 }
1235
1236 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001237 }
1238
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001239 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1240 * table entries that are inaccessible to VFs
1241 */
1242 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1243 count > efx_vf_size(efx)) {
1244 netif_warn(efx, probe, efx->net_dev,
1245 "Reducing number of RSS channels from %u to %u for "
1246 "VF support. Increase vf-msix-limit to use more "
1247 "channels on the PF.\n",
1248 count, efx_vf_size(efx));
1249 count = efx_vf_size(efx);
1250 }
1251
Ben Hutchings46123d02008-09-01 12:47:33 +01001252 return count;
1253}
1254
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001255static int
1256efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1257{
1258#ifdef CONFIG_RFS_ACCEL
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001259 unsigned int i;
1260 int rc;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001261
1262 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1263 if (!efx->net_dev->rx_cpu_rmap)
1264 return -ENOMEM;
1265 for (i = 0; i < efx->n_rx_channels; i++) {
1266 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1267 xentries[i].vector);
1268 if (rc) {
1269 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1270 efx->net_dev->rx_cpu_rmap = NULL;
1271 return rc;
1272 }
1273 }
1274#endif
1275 return 0;
1276}
1277
Ben Hutchings46123d02008-09-01 12:47:33 +01001278/* Probe the number and type of interrupts we are able to obtain, and
1279 * the resulting numbers of channels and RX queues.
1280 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001281static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001282{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001283 unsigned int max_channels =
1284 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001285 unsigned int extra_channels = 0;
1286 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001287 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001288
Ben Hutchings7f967c02012-02-13 23:45:02 +00001289 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1290 if (efx->extra_channel_type[i])
1291 ++extra_channels;
1292
Ben Hutchings8ceee662008-04-27 12:55:59 +01001293 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001294 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001295 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001296
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001297 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001298 if (separate_tx_channels)
1299 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001300 n_channels += extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001301 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001302
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001303 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001304 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001305 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001306 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001307 netif_err(efx, drv, efx->net_dev,
1308 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001309 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001310 netif_err(efx, drv, efx->net_dev,
1311 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001312 EFX_BUG_ON_PARANOID(rc >= n_channels);
1313 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001314 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001315 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001316 }
1317
1318 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001319 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001320 if (n_channels > extra_channels)
1321 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001322 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001323 efx->n_tx_channels = max(n_channels / 2, 1U);
1324 efx->n_rx_channels = max(n_channels -
1325 efx->n_tx_channels,
1326 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001327 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001328 efx->n_tx_channels = n_channels;
1329 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001330 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001331 rc = efx_init_rx_cpu_rmap(efx, xentries);
1332 if (rc) {
1333 pci_disable_msix(efx->pci_dev);
1334 return rc;
1335 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001336 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001337 efx_get_channel(efx, i)->irq =
1338 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001339 } else {
1340 /* Fall back to single channel MSI */
1341 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001342 netif_err(efx, drv, efx->net_dev,
1343 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001344 }
1345 }
1346
1347 /* Try single interrupt MSI */
1348 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001349 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001350 efx->n_rx_channels = 1;
1351 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001352 rc = pci_enable_msi(efx->pci_dev);
1353 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001354 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001355 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001356 netif_err(efx, drv, efx->net_dev,
1357 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001358 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1359 }
1360 }
1361
1362 /* Assume legacy interrupts */
1363 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001364 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001365 efx->n_rx_channels = 1;
1366 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001367 efx->legacy_irq = efx->pci_dev->irq;
1368 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001369
Ben Hutchings7f967c02012-02-13 23:45:02 +00001370 /* Assign extra channels if possible */
1371 j = efx->n_channels;
1372 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1373 if (!efx->extra_channel_type[i])
1374 continue;
1375 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1376 efx->n_channels <= extra_channels) {
1377 efx->extra_channel_type[i]->handle_no_channel(efx);
1378 } else {
1379 --j;
1380 efx_get_channel(efx, j)->type =
1381 efx->extra_channel_type[i];
1382 }
1383 }
1384
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001385 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001386 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001387 efx->n_rx_channels : efx_vf_size(efx));
1388
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001389 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001390}
1391
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001392/* Enable interrupts, then probe and start the event queues */
Ben Hutchings7f967c02012-02-13 23:45:02 +00001393static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001394{
1395 struct efx_channel *channel;
1396
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001397 BUG_ON(efx->state == STATE_DISABLED);
1398
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001399 if (efx->legacy_irq)
1400 efx->legacy_irq_enabled = true;
1401 efx_nic_enable_interrupts(efx);
1402
1403 efx_for_each_channel(channel, efx) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001404 if (!channel->type->keep_eventq || !may_keep_eventq)
1405 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001406 efx_start_eventq(channel);
1407 }
1408
1409 efx_mcdi_mode_event(efx);
1410}
1411
Ben Hutchings7f967c02012-02-13 23:45:02 +00001412static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001413{
1414 struct efx_channel *channel;
1415
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001416 if (efx->state == STATE_DISABLED)
1417 return;
1418
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001419 efx_mcdi_mode_poll(efx);
1420
1421 efx_nic_disable_interrupts(efx);
1422 if (efx->legacy_irq) {
1423 synchronize_irq(efx->legacy_irq);
1424 efx->legacy_irq_enabled = false;
1425 }
1426
1427 efx_for_each_channel(channel, efx) {
1428 if (channel->irq)
1429 synchronize_irq(channel->irq);
1430
1431 efx_stop_eventq(channel);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001432 if (!channel->type->keep_eventq || !may_keep_eventq)
1433 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001434 }
1435}
1436
Ben Hutchings8ceee662008-04-27 12:55:59 +01001437static void efx_remove_interrupts(struct efx_nic *efx)
1438{
1439 struct efx_channel *channel;
1440
1441 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001442 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001443 channel->irq = 0;
1444 pci_disable_msi(efx->pci_dev);
1445 pci_disable_msix(efx->pci_dev);
1446
1447 /* Remove legacy interrupt */
1448 efx->legacy_irq = 0;
1449}
1450
Ben Hutchings8831da72008-09-01 12:47:48 +01001451static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001452{
Ben Hutchings602a5322011-05-16 17:32:39 +01001453 struct efx_channel *channel;
1454 struct efx_tx_queue *tx_queue;
1455
Ben Hutchings97653432011-01-12 18:26:56 +00001456 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001457 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001458
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001459 /* We need to mark which channels really have RX and TX
1460 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001461 * RX-only and TX-only channels.
1462 */
1463 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001464 if (channel->channel < efx->n_rx_channels)
1465 channel->rx_queue.core_index = channel->channel;
1466 else
1467 channel->rx_queue.core_index = -1;
1468
Ben Hutchings602a5322011-05-16 17:32:39 +01001469 efx_for_each_channel_tx_queue(tx_queue, channel)
1470 tx_queue->queue -= (efx->tx_channel_offset *
1471 EFX_TXQ_TYPES);
1472 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001473}
1474
1475static int efx_probe_nic(struct efx_nic *efx)
1476{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001477 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001478 int rc;
1479
Ben Hutchings62776d02010-06-23 11:30:07 +00001480 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001481
1482 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001483 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001484 if (rc)
1485 return rc;
1486
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001487 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001488 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001489 rc = efx_probe_interrupts(efx);
1490 if (rc)
1491 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001492
Ben Hutchings28e47c42012-02-15 01:58:49 +00001493 efx->type->dimension_resources(efx);
1494
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001495 if (efx->n_channels > 1)
1496 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001497 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001498 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001499 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001500
Ben Hutchings8831da72008-09-01 12:47:48 +01001501 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001502 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1503 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001504
1505 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001506 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1507 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001508
1509 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001510
1511fail:
1512 efx->type->remove(efx);
1513 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001514}
1515
1516static void efx_remove_nic(struct efx_nic *efx)
1517{
Ben Hutchings62776d02010-06-23 11:30:07 +00001518 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001519
1520 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001521 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001522}
1523
1524/**************************************************************************
1525 *
1526 * NIC startup/shutdown
1527 *
1528 *************************************************************************/
1529
1530static int efx_probe_all(struct efx_nic *efx)
1531{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001532 int rc;
1533
Ben Hutchings8ceee662008-04-27 12:55:59 +01001534 rc = efx_probe_nic(efx);
1535 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001536 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001537 goto fail1;
1538 }
1539
Ben Hutchings8ceee662008-04-27 12:55:59 +01001540 rc = efx_probe_port(efx);
1541 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001542 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001543 goto fail2;
1544 }
1545
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001546 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1547 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1548 rc = -EINVAL;
1549 goto fail3;
1550 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001551 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001552
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001553 rc = efx_probe_filters(efx);
1554 if (rc) {
1555 netif_err(efx, probe, efx->net_dev,
1556 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001557 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001558 }
1559
Ben Hutchings7f967c02012-02-13 23:45:02 +00001560 rc = efx_probe_channels(efx);
1561 if (rc)
1562 goto fail4;
1563
Ben Hutchings8ceee662008-04-27 12:55:59 +01001564 return 0;
1565
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001566 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001567 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001568 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001569 efx_remove_port(efx);
1570 fail2:
1571 efx_remove_nic(efx);
1572 fail1:
1573 return rc;
1574}
1575
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001576/* If the interface is supposed to be running but is not, start
1577 * the hardware and software data path, regular activity for the port
1578 * (MAC statistics, link polling, etc.) and schedule the port to be
1579 * reconfigured. Interrupts must already be enabled. This function
1580 * is safe to call multiple times, so long as the NIC is not disabled.
1581 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001582 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001583static void efx_start_all(struct efx_nic *efx)
1584{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001585 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001586 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001587
1588 /* Check that it is appropriate to restart the interface. All
1589 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001590 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001591 return;
1592
Ben Hutchings8ceee662008-04-27 12:55:59 +01001593 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001594 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001595
Alexandre Rames626950d2013-01-14 17:20:22 +00001596 /* Start the hardware monitor if there is one */
1597 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001598 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1599 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001600
1601 /* If link state detection is normally event-driven, we have
1602 * to poll now because we could have missed a change
1603 */
1604 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001605 mutex_lock(&efx->mac_lock);
1606 if (efx->phy_op->poll(efx))
1607 efx_link_status_changed(efx);
1608 mutex_unlock(&efx->mac_lock);
1609 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001610
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001611 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001612}
1613
1614/* Flush all delayed work. Should only be called when no more delayed work
1615 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1616 * since we're holding the rtnl_lock at this point. */
1617static void efx_flush_all(struct efx_nic *efx)
1618{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001619 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001620 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001621 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001622 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001623 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001624}
1625
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001626/* Quiesce the hardware and software data path, and regular activity
1627 * for the port without bringing the link down. Safe to call multiple
1628 * times with the NIC in almost any state, but interrupts should be
1629 * enabled. Requires the RTNL lock.
1630 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001631static void efx_stop_all(struct efx_nic *efx)
1632{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001633 EFX_ASSERT_RESET_SERIALISED(efx);
1634
1635 /* port_enabled can be read safely under the rtnl lock */
1636 if (!efx->port_enabled)
1637 return;
1638
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001639 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001640 efx_stop_port(efx);
1641
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001642 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001643 efx_flush_all(efx);
1644
Ben Hutchings29c69a42013-01-28 19:01:06 +00001645 /* Stop the kernel transmit interface. This is only valid if
1646 * the device is stopped or detached; otherwise the watchdog
1647 * may fire immediately.
1648 */
1649 WARN_ON(netif_running(efx->net_dev) &&
1650 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001651 netif_tx_disable(efx->net_dev);
1652
1653 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001654}
1655
1656static void efx_remove_all(struct efx_nic *efx)
1657{
Ben Hutchings46426102010-09-10 06:42:33 +00001658 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001659 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001660 efx_remove_port(efx);
1661 efx_remove_nic(efx);
1662}
1663
Ben Hutchings8ceee662008-04-27 12:55:59 +01001664/**************************************************************************
1665 *
1666 * Interrupt moderation
1667 *
1668 **************************************************************************/
1669
Ben Hutchingscc180b62011-12-08 19:51:47 +00001670static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001671{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001672 if (usecs == 0)
1673 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001674 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001675 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001676 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001677}
1678
Ben Hutchings8ceee662008-04-27 12:55:59 +01001679/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001680int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1681 unsigned int rx_usecs, bool rx_adaptive,
1682 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001683{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001684 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001685 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1686 efx->timer_quantum_ns,
1687 1000);
1688 unsigned int tx_ticks;
1689 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001690
1691 EFX_ASSERT_RESET_SERIALISED(efx);
1692
Ben Hutchingscc180b62011-12-08 19:51:47 +00001693 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001694 return -EINVAL;
1695
Ben Hutchingscc180b62011-12-08 19:51:47 +00001696 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1697 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1698
Ben Hutchings9e393b32011-09-05 07:43:04 +00001699 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1700 !rx_may_override_tx) {
1701 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1702 "RX and TX IRQ moderation must be equal\n");
1703 return -EINVAL;
1704 }
1705
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001706 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001707 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001708 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001709 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001710 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001711 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001712 channel->irq_moderation = tx_ticks;
1713 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001714
1715 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001716}
1717
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001718void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1719 unsigned int *rx_usecs, bool *rx_adaptive)
1720{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001721 /* We must round up when converting ticks to microseconds
1722 * because we round down when converting the other way.
1723 */
1724
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001725 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001726 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1727 efx->timer_quantum_ns,
1728 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001729
1730 /* If channels are shared between RX and TX, so is IRQ
1731 * moderation. Otherwise, IRQ moderation is the same for all
1732 * TX channels and is not adaptive.
1733 */
1734 if (efx->tx_channel_offset == 0)
1735 *tx_usecs = *rx_usecs;
1736 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001737 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001738 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001739 efx->timer_quantum_ns,
1740 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001741}
1742
Ben Hutchings8ceee662008-04-27 12:55:59 +01001743/**************************************************************************
1744 *
1745 * Hardware monitor
1746 *
1747 **************************************************************************/
1748
Ben Hutchingse254c272010-09-20 08:44:10 +00001749/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001750static void efx_monitor(struct work_struct *data)
1751{
1752 struct efx_nic *efx = container_of(data, struct efx_nic,
1753 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001754
Ben Hutchings62776d02010-06-23 11:30:07 +00001755 netif_vdbg(efx, timer, efx->net_dev,
1756 "hardware monitor executing on CPU %d\n",
1757 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001758 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001759
Ben Hutchings8ceee662008-04-27 12:55:59 +01001760 /* If the mac_lock is already held then it is likely a port
1761 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001762 * most of the work of monitor() anyway. */
1763 if (mutex_trylock(&efx->mac_lock)) {
1764 if (efx->port_enabled)
1765 efx->type->monitor(efx);
1766 mutex_unlock(&efx->mac_lock);
1767 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001768
Ben Hutchings8ceee662008-04-27 12:55:59 +01001769 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1770 efx_monitor_interval);
1771}
1772
1773/**************************************************************************
1774 *
1775 * ioctls
1776 *
1777 *************************************************************************/
1778
1779/* Net device ioctl
1780 * Context: process, rtnl_lock() held.
1781 */
1782static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1783{
Ben Hutchings767e4682008-09-01 12:43:14 +01001784 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001785 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001786
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001787 if (cmd == SIOCSHWTSTAMP)
1788 return efx_ptp_ioctl(efx, ifr, cmd);
1789
Ben Hutchings68e7f452009-04-29 08:05:08 +00001790 /* Convert phy_id from older PRTAD/DEVAD format */
1791 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1792 (data->phy_id & 0xfc00) == 0x0400)
1793 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1794
1795 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001796}
1797
1798/**************************************************************************
1799 *
1800 * NAPI interface
1801 *
1802 **************************************************************************/
1803
Ben Hutchings7f967c02012-02-13 23:45:02 +00001804static void efx_init_napi_channel(struct efx_channel *channel)
1805{
1806 struct efx_nic *efx = channel->efx;
1807
1808 channel->napi_dev = efx->net_dev;
1809 netif_napi_add(channel->napi_dev, &channel->napi_str,
1810 efx_poll, napi_weight);
1811}
1812
Ben Hutchingse8f14992010-12-07 19:47:34 +00001813static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001814{
1815 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001816
Ben Hutchings7f967c02012-02-13 23:45:02 +00001817 efx_for_each_channel(channel, efx)
1818 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001819}
1820
1821static void efx_fini_napi_channel(struct efx_channel *channel)
1822{
1823 if (channel->napi_dev)
1824 netif_napi_del(&channel->napi_str);
1825 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001826}
1827
1828static void efx_fini_napi(struct efx_nic *efx)
1829{
1830 struct efx_channel *channel;
1831
Ben Hutchingse8f14992010-12-07 19:47:34 +00001832 efx_for_each_channel(channel, efx)
1833 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001834}
1835
1836/**************************************************************************
1837 *
1838 * Kernel netpoll interface
1839 *
1840 *************************************************************************/
1841
1842#ifdef CONFIG_NET_POLL_CONTROLLER
1843
1844/* Although in the common case interrupts will be disabled, this is not
1845 * guaranteed. However, all our work happens inside the NAPI callback,
1846 * so no locking is required.
1847 */
1848static void efx_netpoll(struct net_device *net_dev)
1849{
Ben Hutchings767e4682008-09-01 12:43:14 +01001850 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001851 struct efx_channel *channel;
1852
Ben Hutchings64ee3122008-09-01 12:47:38 +01001853 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001854 efx_schedule_channel(channel);
1855}
1856
1857#endif
1858
1859/**************************************************************************
1860 *
1861 * Kernel net device interface
1862 *
1863 *************************************************************************/
1864
1865/* Context: process, rtnl_lock() held. */
1866static int efx_net_open(struct net_device *net_dev)
1867{
Ben Hutchings767e4682008-09-01 12:43:14 +01001868 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001869 int rc;
1870
Ben Hutchings62776d02010-06-23 11:30:07 +00001871 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1872 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001873
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001874 rc = efx_check_disabled(efx);
1875 if (rc)
1876 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001877 if (efx->phy_mode & PHY_MODE_SPECIAL)
1878 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001879 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1880 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001881
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001882 /* Notify the kernel of the link state polled during driver load,
1883 * before the monitor starts running */
1884 efx_link_status_changed(efx);
1885
Ben Hutchings8ceee662008-04-27 12:55:59 +01001886 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001887 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001888 return 0;
1889}
1890
1891/* Context: process, rtnl_lock() held.
1892 * Note that the kernel will ignore our return code; this method
1893 * should really be a void.
1894 */
1895static int efx_net_stop(struct net_device *net_dev)
1896{
Ben Hutchings767e4682008-09-01 12:43:14 +01001897 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001898
Ben Hutchings62776d02010-06-23 11:30:07 +00001899 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1900 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001901
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001902 /* Stop the device and flush all the channels */
1903 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001904
1905 return 0;
1906}
1907
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001908/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001909static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1910 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001911{
Ben Hutchings767e4682008-09-01 12:43:14 +01001912 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001913 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001914
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001915 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001916
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001917 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001918
1919 stats->rx_packets = mac_stats->rx_packets;
1920 stats->tx_packets = mac_stats->tx_packets;
1921 stats->rx_bytes = mac_stats->rx_bytes;
1922 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001923 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001924 stats->multicast = mac_stats->rx_multicast;
1925 stats->collisions = mac_stats->tx_collision;
1926 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1927 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001928 stats->rx_crc_errors = mac_stats->rx_bad;
1929 stats->rx_frame_errors = mac_stats->rx_align_error;
1930 stats->rx_fifo_errors = mac_stats->rx_overflow;
1931 stats->rx_missed_errors = mac_stats->rx_missed;
1932 stats->tx_window_errors = mac_stats->tx_late_collision;
1933
1934 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001935 stats->rx_crc_errors +
1936 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001937 mac_stats->rx_symbol_error);
1938 stats->tx_errors = (stats->tx_window_errors +
1939 mac_stats->tx_bad);
1940
Ben Hutchings1cb34522011-09-02 23:23:00 +01001941 spin_unlock_bh(&efx->stats_lock);
1942
Ben Hutchings8ceee662008-04-27 12:55:59 +01001943 return stats;
1944}
1945
1946/* Context: netif_tx_lock held, BHs disabled. */
1947static void efx_watchdog(struct net_device *net_dev)
1948{
Ben Hutchings767e4682008-09-01 12:43:14 +01001949 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001950
Ben Hutchings62776d02010-06-23 11:30:07 +00001951 netif_err(efx, tx_err, efx->net_dev,
1952 "TX stuck with port_enabled=%d: resetting channels\n",
1953 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001954
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001955 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001956}
1957
1958
1959/* Context: process, rtnl_lock() held. */
1960static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1961{
Ben Hutchings767e4682008-09-01 12:43:14 +01001962 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001963 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001964
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001965 rc = efx_check_disabled(efx);
1966 if (rc)
1967 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001968 if (new_mtu > EFX_MAX_MTU)
1969 return -EINVAL;
1970
Ben Hutchings62776d02010-06-23 11:30:07 +00001971 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001972
Ben Hutchings29c69a42013-01-28 19:01:06 +00001973 efx_device_detach_sync(efx);
1974 efx_stop_all(efx);
1975
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001976 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001977 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001978 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001979 mutex_unlock(&efx->mac_lock);
1980
Ben Hutchings8ceee662008-04-27 12:55:59 +01001981 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00001982 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001983 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001984}
1985
1986static int efx_set_mac_address(struct net_device *net_dev, void *data)
1987{
Ben Hutchings767e4682008-09-01 12:43:14 +01001988 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001989 struct sockaddr *addr = data;
1990 char *new_addr = addr->sa_data;
1991
Ben Hutchings8ceee662008-04-27 12:55:59 +01001992 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001993 netif_err(efx, drv, efx->net_dev,
1994 "invalid ethernet MAC address requested: %pM\n",
1995 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00001996 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001997 }
1998
1999 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002000 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002001
2002 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002003 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002004 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002005 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002006
2007 return 0;
2008}
2009
Ben Hutchingsa816f752008-09-01 12:49:12 +01002010/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002011static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002012{
Ben Hutchings767e4682008-09-01 12:43:14 +01002013 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002014 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002015 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002016 u32 crc;
2017 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002018
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002019 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002020
2021 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002022 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002023 memset(mc_hash, 0xff, sizeof(*mc_hash));
2024 } else {
2025 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00002026 netdev_for_each_mc_addr(ha, net_dev) {
2027 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002028 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
Ben Hutchings32766ec2012-10-04 17:13:03 -07002029 __set_bit_le(bit, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002030 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002031
2032 /* Broadcast packets go through the multicast hash filter.
2033 * ether_crc_le() of the broadcast address is 0xbe2612ff
2034 * so we always add bit 0xff to the mask.
2035 */
Ben Hutchings32766ec2012-10-04 17:13:03 -07002036 __set_bit_le(0xff, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002037 }
2038
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002039 if (efx->port_enabled)
2040 queue_work(efx->workqueue, &efx->mac_work);
2041 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002042}
2043
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002044static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002045{
2046 struct efx_nic *efx = netdev_priv(net_dev);
2047
2048 /* If disabling RX n-tuple filtering, clear existing filters */
2049 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2050 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2051
2052 return 0;
2053}
2054
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002055static const struct net_device_ops efx_netdev_ops = {
2056 .ndo_open = efx_net_open,
2057 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002058 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002059 .ndo_tx_timeout = efx_watchdog,
2060 .ndo_start_xmit = efx_hard_start_xmit,
2061 .ndo_validate_addr = eth_validate_addr,
2062 .ndo_do_ioctl = efx_ioctl,
2063 .ndo_change_mtu = efx_change_mtu,
2064 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002065 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002066 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002067#ifdef CONFIG_SFC_SRIOV
2068 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2069 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2070 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2071 .ndo_get_vf_config = efx_sriov_get_vf_config,
2072#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002073#ifdef CONFIG_NET_POLL_CONTROLLER
2074 .ndo_poll_controller = efx_netpoll,
2075#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002076 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002077#ifdef CONFIG_RFS_ACCEL
2078 .ndo_rx_flow_steer = efx_filter_rfs,
2079#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002080};
2081
Ben Hutchings7dde5962008-12-12 22:09:38 -08002082static void efx_update_name(struct efx_nic *efx)
2083{
2084 strcpy(efx->name, efx->net_dev->name);
2085 efx_mtd_rename(efx);
2086 efx_set_channel_names(efx);
2087}
2088
Ben Hutchings8ceee662008-04-27 12:55:59 +01002089static int efx_netdev_event(struct notifier_block *this,
2090 unsigned long event, void *ptr)
2091{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01002092 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002093
Ben Hutchings7dde5962008-12-12 22:09:38 -08002094 if (net_dev->netdev_ops == &efx_netdev_ops &&
2095 event == NETDEV_CHANGENAME)
2096 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002097
2098 return NOTIFY_DONE;
2099}
2100
2101static struct notifier_block efx_netdev_notifier = {
2102 .notifier_call = efx_netdev_event,
2103};
2104
Ben Hutchings06d5e192008-12-12 21:47:23 -08002105static ssize_t
2106show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2107{
2108 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2109 return sprintf(buf, "%d\n", efx->phy_type);
2110}
2111static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
2112
Ben Hutchings8ceee662008-04-27 12:55:59 +01002113static int efx_register_netdev(struct efx_nic *efx)
2114{
2115 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002116 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002117 int rc;
2118
2119 net_dev->watchdog_timeo = 5 * HZ;
2120 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002121 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002122 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002123 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002124
Ben Hutchings7dde5962008-12-12 22:09:38 -08002125 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002126
Ben Hutchings7153f622012-07-27 20:50:52 +01002127 /* Enable resets to be scheduled and check whether any were
2128 * already requested. If so, the NIC is probably hosed so we
2129 * abort.
2130 */
2131 efx->state = STATE_READY;
2132 smp_mb(); /* ensure we change state before checking reset_pending */
2133 if (efx->reset_pending) {
2134 netif_err(efx, probe, efx->net_dev,
2135 "aborting probe due to scheduled reset\n");
2136 rc = -EIO;
2137 goto fail_locked;
2138 }
2139
Ben Hutchingsaed06282009-08-26 08:16:27 +00002140 rc = dev_alloc_name(net_dev, net_dev->name);
2141 if (rc < 0)
2142 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002143 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002144
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002145 /* Always start with carrier off; PHY events will detect the link */
2146 netif_carrier_off(net_dev);
2147
Ben Hutchingsaed06282009-08-26 08:16:27 +00002148 rc = register_netdevice(net_dev);
2149 if (rc)
2150 goto fail_locked;
2151
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002152 efx_for_each_channel(channel, efx) {
2153 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002154 efx_for_each_channel_tx_queue(tx_queue, channel)
2155 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002156 }
2157
Ben Hutchings7dde5962008-12-12 22:09:38 -08002158 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002159
Ben Hutchings06d5e192008-12-12 21:47:23 -08002160 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2161 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002162 netif_err(efx, drv, efx->net_dev,
2163 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002164 goto fail_registered;
2165 }
2166
Ben Hutchings8ceee662008-04-27 12:55:59 +01002167 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002168
Ben Hutchings7153f622012-07-27 20:50:52 +01002169fail_registered:
2170 rtnl_lock();
2171 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002172fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002173 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002174 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002175 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002176 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002177}
2178
2179static void efx_unregister_netdev(struct efx_nic *efx)
2180{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002181 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002182 struct efx_tx_queue *tx_queue;
2183
2184 if (!efx->net_dev)
2185 return;
2186
Ben Hutchings767e4682008-09-01 12:43:14 +01002187 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002188
2189 /* Free up any skbs still remaining. This has to happen before
2190 * we try to unregister the netdev as running their destructors
2191 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002192 efx_for_each_channel(channel, efx) {
2193 efx_for_each_channel_tx_queue(tx_queue, channel)
2194 efx_release_tx_buffers(tx_queue);
2195 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002196
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002197 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2198 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002199
2200 rtnl_lock();
2201 unregister_netdevice(efx->net_dev);
2202 efx->state = STATE_UNINIT;
2203 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002204}
2205
2206/**************************************************************************
2207 *
2208 * Device reset and suspend
2209 *
2210 **************************************************************************/
2211
Ben Hutchings2467ca42008-09-01 12:48:50 +01002212/* Tears down the entire software state and most of the hardware state
2213 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002214void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002215{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002216 EFX_ASSERT_RESET_SERIALISED(efx);
2217
Ben Hutchings2467ca42008-09-01 12:48:50 +01002218 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00002219 efx_stop_interrupts(efx, false);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002220
2221 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002222 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2223 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002224 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002225}
2226
Ben Hutchings2467ca42008-09-01 12:48:50 +01002227/* This function will always ensure that the locks acquired in
2228 * efx_reset_down() are released. A failure return code indicates
2229 * that we were unable to reinitialise the hardware, and the
2230 * driver should be disabled. If ok is false, then the rx and tx
2231 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002232int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002233{
2234 int rc;
2235
Ben Hutchings2467ca42008-09-01 12:48:50 +01002236 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002237
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002238 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002239 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002240 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002241 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002242 }
2243
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002244 if (!ok)
2245 goto fail;
2246
Steve Hodgson4b988282009-01-29 17:50:51 +00002247 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002248 rc = efx->phy_op->init(efx);
2249 if (rc)
2250 goto fail;
2251 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002252 netif_err(efx, drv, efx->net_dev,
2253 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002254 }
2255
Ben Hutchings710b2082011-09-03 00:15:00 +01002256 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002257
Ben Hutchings7f967c02012-02-13 23:45:02 +00002258 efx_start_interrupts(efx, false);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002259 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002260 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002261
2262 mutex_unlock(&efx->mac_lock);
2263
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002264 efx_start_all(efx);
2265
2266 return 0;
2267
2268fail:
2269 efx->port_initialized = false;
2270
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002271 mutex_unlock(&efx->mac_lock);
2272
Ben Hutchings8ceee662008-04-27 12:55:59 +01002273 return rc;
2274}
2275
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002276/* Reset the NIC using the specified method. Note that the reset may
2277 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002278 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002279 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002280 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002281int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002282{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002283 int rc, rc2;
2284 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002285
Ben Hutchings62776d02010-06-23 11:30:07 +00002286 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2287 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002288
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002289 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002290 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002291
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002292 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002293 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002294 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002295 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002296 }
2297
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002298 /* Clear flags for the scopes we covered. We assume the NIC and
2299 * driver are now quiescent so that there is no race here.
2300 */
2301 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002302
2303 /* Reinitialise bus-mastering, which may have been turned off before
2304 * the reset was scheduled. This is still appropriate, even in the
2305 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2306 * can respond to requests. */
2307 pci_set_master(efx->pci_dev);
2308
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002309out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002310 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002311 disabled = rc ||
2312 method == RESET_TYPE_DISABLE ||
2313 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002314 rc2 = efx_reset_up(efx, method, !disabled);
2315 if (rc2) {
2316 disabled = true;
2317 if (!rc)
2318 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002319 }
2320
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002321 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002322 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002323 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002324 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002325 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002326 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002327 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002328 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002329 return rc;
2330}
2331
Alexandre Rames626950d2013-01-14 17:20:22 +00002332/* Try recovery mechanisms.
2333 * For now only EEH is supported.
2334 * Returns 0 if the recovery mechanisms are unsuccessful.
2335 * Returns a non-zero value otherwise.
2336 */
2337static int efx_try_recovery(struct efx_nic *efx)
2338{
2339#ifdef CONFIG_EEH
2340 /* A PCI error can occur and not be seen by EEH because nothing
2341 * happens on the PCI bus. In this case the driver may fail and
2342 * schedule a 'recover or reset', leading to this recovery handler.
2343 * Manually call the eeh failure check function.
2344 */
2345 struct eeh_dev *eehdev =
2346 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2347
2348 if (eeh_dev_check_failure(eehdev)) {
2349 /* The EEH mechanisms will handle the error and reset the
2350 * device if necessary.
2351 */
2352 return 1;
2353 }
2354#endif
2355 return 0;
2356}
2357
Ben Hutchings8ceee662008-04-27 12:55:59 +01002358/* The worker thread exists so that code that cannot sleep can
2359 * schedule a reset for later.
2360 */
2361static void efx_reset_work(struct work_struct *data)
2362{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002363 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002364 unsigned long pending;
2365 enum reset_type method;
2366
2367 pending = ACCESS_ONCE(efx->reset_pending);
2368 method = fls(pending) - 1;
2369
2370 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2371 method == RESET_TYPE_RECOVER_OR_ALL) &&
2372 efx_try_recovery(efx))
2373 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002374
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002375 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002376 return;
2377
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002378 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002379
2380 /* We checked the state in efx_schedule_reset() but it may
2381 * have changed by now. Now that we have the RTNL lock,
2382 * it cannot change again.
2383 */
2384 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002385 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002386
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002387 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002388}
2389
2390void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2391{
2392 enum reset_type method;
2393
Alexandre Rames626950d2013-01-14 17:20:22 +00002394 if (efx->state == STATE_RECOVERY) {
2395 netif_dbg(efx, drv, efx->net_dev,
2396 "recovering: skip scheduling %s reset\n",
2397 RESET_TYPE(type));
2398 return;
2399 }
2400
Ben Hutchings8ceee662008-04-27 12:55:59 +01002401 switch (type) {
2402 case RESET_TYPE_INVISIBLE:
2403 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002404 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002405 case RESET_TYPE_WORLD:
2406 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002407 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002408 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002409 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2410 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002411 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002412 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002413 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002414 netif_dbg(efx, drv, efx->net_dev,
2415 "scheduling %s reset for %s\n",
2416 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002417 break;
2418 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002419
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002420 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002421 smp_mb(); /* ensure we change reset_pending before checking state */
2422
2423 /* If we're not READY then just leave the flags set as the cue
2424 * to abort probing or reschedule the reset later.
2425 */
2426 if (ACCESS_ONCE(efx->state) != STATE_READY)
2427 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002428
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002429 /* efx_process_channel() will no longer read events once a
2430 * reset is scheduled. So switch back to poll'd MCDI completions. */
2431 efx_mcdi_mode_poll(efx);
2432
Steve Hodgson1ab00622008-12-12 21:33:02 -08002433 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002434}
2435
2436/**************************************************************************
2437 *
2438 * List of NICs we support
2439 *
2440 **************************************************************************/
2441
2442/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002443static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002444 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2445 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002446 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002447 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2448 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002449 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002450 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002451 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002452 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002453 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002454 {0} /* end of list */
2455};
2456
2457/**************************************************************************
2458 *
Ben Hutchings37594332009-11-23 16:05:45 +00002459 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002460 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002461 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002462 * Needed so all function pointers are valid and do not have to be tested
2463 * before use
2464 *
2465 **************************************************************************/
2466int efx_port_dummy_op_int(struct efx_nic *efx)
2467{
2468 return 0;
2469}
2470void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002471
2472static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002473{
2474 return false;
2475}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002476
stephen hemminger6c8c2512011-04-14 05:50:12 +00002477static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002478 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002479 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002480 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002481 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002482};
2483
Ben Hutchings8ceee662008-04-27 12:55:59 +01002484/**************************************************************************
2485 *
2486 * Data housekeeping
2487 *
2488 **************************************************************************/
2489
2490/* This zeroes out and then fills in the invariants in a struct
2491 * efx_nic (including all sub-structures).
2492 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002493static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002494 struct pci_dev *pci_dev, struct net_device *net_dev)
2495{
Ben Hutchings46426102010-09-10 06:42:33 +00002496 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002497
2498 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002499 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002500#ifdef CONFIG_SFC_MTD
2501 INIT_LIST_HEAD(&efx->mtd_list);
2502#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002503 INIT_WORK(&efx->reset_work, efx_reset_work);
2504 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002505 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002506 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002507 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002508 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002509 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002510
2511 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002512 spin_lock_init(&efx->stats_lock);
2513 mutex_init(&efx->mac_lock);
2514 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002515 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002516 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002517 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002518
2519 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002520 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2521 if (!efx->channel[i])
2522 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002523 }
2524
Ben Hutchings8ceee662008-04-27 12:55:59 +01002525 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2526
2527 /* Higher numbered interrupt modes are less capable! */
2528 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2529 interrupt_mode);
2530
Ben Hutchings6977dc62008-12-26 13:44:39 -08002531 /* Would be good to use the net_dev name, but we're too early */
2532 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2533 pci_name(pci_dev));
2534 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002535 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002536 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002537
Ben Hutchings8ceee662008-04-27 12:55:59 +01002538 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002539
2540fail:
2541 efx_fini_struct(efx);
2542 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002543}
2544
2545static void efx_fini_struct(struct efx_nic *efx)
2546{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002547 int i;
2548
2549 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2550 kfree(efx->channel[i]);
2551
Ben Hutchings8ceee662008-04-27 12:55:59 +01002552 if (efx->workqueue) {
2553 destroy_workqueue(efx->workqueue);
2554 efx->workqueue = NULL;
2555 }
2556}
2557
2558/**************************************************************************
2559 *
2560 * PCI interface
2561 *
2562 **************************************************************************/
2563
2564/* Main body of final NIC shutdown code
2565 * This is called only at module unload (or hotplug removal).
2566 */
2567static void efx_pci_remove_main(struct efx_nic *efx)
2568{
Ben Hutchings7153f622012-07-27 20:50:52 +01002569 /* Flush reset_work. It can no longer be scheduled since we
2570 * are not READY.
2571 */
2572 BUG_ON(efx->state == STATE_READY);
2573 cancel_work_sync(&efx->reset_work);
2574
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002575#ifdef CONFIG_RFS_ACCEL
2576 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2577 efx->net_dev->rx_cpu_rmap = NULL;
2578#endif
Ben Hutchings7f967c02012-02-13 23:45:02 +00002579 efx_stop_interrupts(efx, false);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002580 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002581 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002582 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002583 efx_fini_napi(efx);
2584 efx_remove_all(efx);
2585}
2586
2587/* Final NIC shutdown
2588 * This is called only at module unload (or hotplug removal).
2589 */
2590static void efx_pci_remove(struct pci_dev *pci_dev)
2591{
2592 struct efx_nic *efx;
2593
2594 efx = pci_get_drvdata(pci_dev);
2595 if (!efx)
2596 return;
2597
2598 /* Mark the NIC as fini, then stop the interface */
2599 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002600 dev_close(efx->net_dev);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002601 efx_stop_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002602 rtnl_unlock();
2603
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002604 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002605 efx_unregister_netdev(efx);
2606
Ben Hutchings7dde5962008-12-12 22:09:38 -08002607 efx_mtd_remove(efx);
2608
Ben Hutchings8ceee662008-04-27 12:55:59 +01002609 efx_pci_remove_main(efx);
2610
Ben Hutchings8ceee662008-04-27 12:55:59 +01002611 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002612 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002613
Ben Hutchings8ceee662008-04-27 12:55:59 +01002614 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002615 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002616 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002617
2618 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002619};
2620
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002621/* NIC VPD information
2622 * Called during probe to display the part number of the
2623 * installed NIC. VPD is potentially very large but this should
2624 * always appear within the first 512 bytes.
2625 */
2626#define SFC_VPD_LEN 512
2627static void efx_print_product_vpd(struct efx_nic *efx)
2628{
2629 struct pci_dev *dev = efx->pci_dev;
2630 char vpd_data[SFC_VPD_LEN];
2631 ssize_t vpd_size;
2632 int i, j;
2633
2634 /* Get the vpd data from the device */
2635 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2636 if (vpd_size <= 0) {
2637 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2638 return;
2639 }
2640
2641 /* Get the Read only section */
2642 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2643 if (i < 0) {
2644 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2645 return;
2646 }
2647
2648 j = pci_vpd_lrdt_size(&vpd_data[i]);
2649 i += PCI_VPD_LRDT_TAG_SIZE;
2650 if (i + j > vpd_size)
2651 j = vpd_size - i;
2652
2653 /* Get the Part number */
2654 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2655 if (i < 0) {
2656 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2657 return;
2658 }
2659
2660 j = pci_vpd_info_field_size(&vpd_data[i]);
2661 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2662 if (i + j > vpd_size) {
2663 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2664 return;
2665 }
2666
2667 netif_info(efx, drv, efx->net_dev,
2668 "Part Number : %.*s\n", j, &vpd_data[i]);
2669}
2670
2671
Ben Hutchings8ceee662008-04-27 12:55:59 +01002672/* Main body of NIC initialisation
2673 * This is called at module load (or hotplug insertion, theoretically).
2674 */
2675static int efx_pci_probe_main(struct efx_nic *efx)
2676{
2677 int rc;
2678
2679 /* Do start-of-day initialisation */
2680 rc = efx_probe_all(efx);
2681 if (rc)
2682 goto fail1;
2683
Ben Hutchingse8f14992010-12-07 19:47:34 +00002684 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002685
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002686 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002687 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002688 netif_err(efx, probe, efx->net_dev,
2689 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002690 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002691 }
2692
2693 rc = efx_init_port(efx);
2694 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002695 netif_err(efx, probe, efx->net_dev,
2696 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002697 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002698 }
2699
Ben Hutchings152b6a62009-11-29 03:43:56 +00002700 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002701 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002702 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00002703 efx_start_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002704
2705 return 0;
2706
Ben Hutchings278c0622009-11-23 16:05:12 +00002707 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002708 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002709 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002710 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002711 fail3:
2712 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002713 efx_remove_all(efx);
2714 fail1:
2715 return rc;
2716}
2717
2718/* NIC initialisation
2719 *
2720 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002721 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002722 * sets up and registers the network devices with the kernel and hooks
2723 * the interrupt service routine. It does not prepare the device for
2724 * transmission; this is left to the first time one of the network
2725 * interfaces is brought up (i.e. efx_net_open).
2726 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002727static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002728 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002729{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002730 struct net_device *net_dev;
2731 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002732 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002733
2734 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002735 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2736 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002737 if (!net_dev)
2738 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002739 efx = netdev_priv(net_dev);
2740 efx->type = (const struct efx_nic_type *) entry->driver_data;
2741 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002742 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002743 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002744 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002745 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002746 /* Mask for features that also apply to VLAN devices */
2747 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002748 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2749 NETIF_F_RXCSUM);
2750 /* All offloads can be toggled */
2751 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002752 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002753 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002754 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002755 if (rc)
2756 goto fail1;
2757
Ben Hutchings62776d02010-06-23 11:30:07 +00002758 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002759 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002760
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002761 efx_print_product_vpd(efx);
2762
Ben Hutchings8ceee662008-04-27 12:55:59 +01002763 /* Set up basic I/O (BAR mappings etc) */
2764 rc = efx_init_io(efx);
2765 if (rc)
2766 goto fail2;
2767
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002768 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002769 if (rc)
2770 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002771
Ben Hutchings8ceee662008-04-27 12:55:59 +01002772 rc = efx_register_netdev(efx);
2773 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002774 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002775
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002776 rc = efx_sriov_init(efx);
2777 if (rc)
2778 netif_err(efx, probe, efx->net_dev,
2779 "SR-IOV can't be enabled rc %d\n", rc);
2780
Ben Hutchings62776d02010-06-23 11:30:07 +00002781 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002782
Ben Hutchings7c431612012-01-27 17:23:58 +00002783 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002784 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002785 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002786 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002787 if (rc)
2788 netif_warn(efx, probe, efx->net_dev,
2789 "failed to create MTDs (%d)\n", rc);
2790
Alexandre Rames626950d2013-01-14 17:20:22 +00002791 rc = pci_enable_pcie_error_reporting(pci_dev);
2792 if (rc && rc != -EINVAL)
2793 netif_warn(efx, probe, efx->net_dev,
2794 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2795
Ben Hutchings8ceee662008-04-27 12:55:59 +01002796 return 0;
2797
Ben Hutchings8ceee662008-04-27 12:55:59 +01002798 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002799 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002800 fail3:
2801 efx_fini_io(efx);
2802 fail2:
2803 efx_fini_struct(efx);
2804 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002805 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002806 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002807 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002808 free_netdev(net_dev);
2809 return rc;
2810}
2811
Ben Hutchings89c758f2009-11-29 03:43:07 +00002812static int efx_pm_freeze(struct device *dev)
2813{
2814 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2815
Ben Hutchings61da0262012-07-27 19:35:39 +01002816 rtnl_lock();
2817
Ben Hutchings6032fb52012-07-27 19:35:47 +01002818 if (efx->state != STATE_DISABLED) {
2819 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002820
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002821 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002822
Ben Hutchings6032fb52012-07-27 19:35:47 +01002823 efx_stop_all(efx);
2824 efx_stop_interrupts(efx, false);
2825 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002826
Ben Hutchings61da0262012-07-27 19:35:39 +01002827 rtnl_unlock();
2828
Ben Hutchings89c758f2009-11-29 03:43:07 +00002829 return 0;
2830}
2831
2832static int efx_pm_thaw(struct device *dev)
2833{
2834 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2835
Ben Hutchings61da0262012-07-27 19:35:39 +01002836 rtnl_lock();
2837
Ben Hutchings6032fb52012-07-27 19:35:47 +01002838 if (efx->state != STATE_DISABLED) {
2839 efx_start_interrupts(efx, false);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002840
Ben Hutchings6032fb52012-07-27 19:35:47 +01002841 mutex_lock(&efx->mac_lock);
2842 efx->phy_op->reconfigure(efx);
2843 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002844
Ben Hutchings6032fb52012-07-27 19:35:47 +01002845 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002846
Ben Hutchings6032fb52012-07-27 19:35:47 +01002847 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002848
Ben Hutchings6032fb52012-07-27 19:35:47 +01002849 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002850
Ben Hutchings6032fb52012-07-27 19:35:47 +01002851 efx->type->resume_wol(efx);
2852 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002853
Ben Hutchings61da0262012-07-27 19:35:39 +01002854 rtnl_unlock();
2855
Steve Hodgson319ba642010-06-01 11:17:24 +00002856 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2857 queue_work(reset_workqueue, &efx->reset_work);
2858
Ben Hutchings89c758f2009-11-29 03:43:07 +00002859 return 0;
2860}
2861
2862static int efx_pm_poweroff(struct device *dev)
2863{
2864 struct pci_dev *pci_dev = to_pci_dev(dev);
2865 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2866
2867 efx->type->fini(efx);
2868
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002869 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002870
2871 pci_save_state(pci_dev);
2872 return pci_set_power_state(pci_dev, PCI_D3hot);
2873}
2874
2875/* Used for both resume and restore */
2876static int efx_pm_resume(struct device *dev)
2877{
2878 struct pci_dev *pci_dev = to_pci_dev(dev);
2879 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2880 int rc;
2881
2882 rc = pci_set_power_state(pci_dev, PCI_D0);
2883 if (rc)
2884 return rc;
2885 pci_restore_state(pci_dev);
2886 rc = pci_enable_device(pci_dev);
2887 if (rc)
2888 return rc;
2889 pci_set_master(efx->pci_dev);
2890 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2891 if (rc)
2892 return rc;
2893 rc = efx->type->init(efx);
2894 if (rc)
2895 return rc;
2896 efx_pm_thaw(dev);
2897 return 0;
2898}
2899
2900static int efx_pm_suspend(struct device *dev)
2901{
2902 int rc;
2903
2904 efx_pm_freeze(dev);
2905 rc = efx_pm_poweroff(dev);
2906 if (rc)
2907 efx_pm_resume(dev);
2908 return rc;
2909}
2910
Ben Hutchings18e83e42012-01-05 19:05:20 +00002911static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002912 .suspend = efx_pm_suspend,
2913 .resume = efx_pm_resume,
2914 .freeze = efx_pm_freeze,
2915 .thaw = efx_pm_thaw,
2916 .poweroff = efx_pm_poweroff,
2917 .restore = efx_pm_resume,
2918};
2919
Alexandre Rames626950d2013-01-14 17:20:22 +00002920/* A PCI error affecting this device was detected.
2921 * At this point MMIO and DMA may be disabled.
2922 * Stop the software path and request a slot reset.
2923 */
2924pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2925 enum pci_channel_state state)
2926{
2927 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2928 struct efx_nic *efx = pci_get_drvdata(pdev);
2929
2930 if (state == pci_channel_io_perm_failure)
2931 return PCI_ERS_RESULT_DISCONNECT;
2932
2933 rtnl_lock();
2934
2935 if (efx->state != STATE_DISABLED) {
2936 efx->state = STATE_RECOVERY;
2937 efx->reset_pending = 0;
2938
2939 efx_device_detach_sync(efx);
2940
2941 efx_stop_all(efx);
2942 efx_stop_interrupts(efx, false);
2943
2944 status = PCI_ERS_RESULT_NEED_RESET;
2945 } else {
2946 /* If the interface is disabled we don't want to do anything
2947 * with it.
2948 */
2949 status = PCI_ERS_RESULT_RECOVERED;
2950 }
2951
2952 rtnl_unlock();
2953
2954 pci_disable_device(pdev);
2955
2956 return status;
2957}
2958
2959/* Fake a successfull reset, which will be performed later in efx_io_resume. */
2960pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
2961{
2962 struct efx_nic *efx = pci_get_drvdata(pdev);
2963 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2964 int rc;
2965
2966 if (pci_enable_device(pdev)) {
2967 netif_err(efx, hw, efx->net_dev,
2968 "Cannot re-enable PCI device after reset.\n");
2969 status = PCI_ERS_RESULT_DISCONNECT;
2970 }
2971
2972 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
2973 if (rc) {
2974 netif_err(efx, hw, efx->net_dev,
2975 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
2976 /* Non-fatal error. Continue. */
2977 }
2978
2979 return status;
2980}
2981
2982/* Perform the actual reset and resume I/O operations. */
2983static void efx_io_resume(struct pci_dev *pdev)
2984{
2985 struct efx_nic *efx = pci_get_drvdata(pdev);
2986 int rc;
2987
2988 rtnl_lock();
2989
2990 if (efx->state == STATE_DISABLED)
2991 goto out;
2992
2993 rc = efx_reset(efx, RESET_TYPE_ALL);
2994 if (rc) {
2995 netif_err(efx, hw, efx->net_dev,
2996 "efx_reset failed after PCI error (%d)\n", rc);
2997 } else {
2998 efx->state = STATE_READY;
2999 netif_dbg(efx, hw, efx->net_dev,
3000 "Done resetting and resuming IO after PCI error.\n");
3001 }
3002
3003out:
3004 rtnl_unlock();
3005}
3006
3007/* For simplicity and reliability, we always require a slot reset and try to
3008 * reset the hardware when a pci error affecting the device is detected.
3009 * We leave both the link_reset and mmio_enabled callback unimplemented:
3010 * with our request for slot reset the mmio_enabled callback will never be
3011 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3012 */
3013static struct pci_error_handlers efx_err_handlers = {
3014 .error_detected = efx_io_error_detected,
3015 .slot_reset = efx_io_slot_reset,
3016 .resume = efx_io_resume,
3017};
3018
Ben Hutchings8ceee662008-04-27 12:55:59 +01003019static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003020 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003021 .id_table = efx_pci_table,
3022 .probe = efx_pci_probe,
3023 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003024 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003025 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003026};
3027
3028/**************************************************************************
3029 *
3030 * Kernel module interface
3031 *
3032 *************************************************************************/
3033
3034module_param(interrupt_mode, uint, 0444);
3035MODULE_PARM_DESC(interrupt_mode,
3036 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3037
3038static int __init efx_init_module(void)
3039{
3040 int rc;
3041
3042 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3043
3044 rc = register_netdevice_notifier(&efx_netdev_notifier);
3045 if (rc)
3046 goto err_notifier;
3047
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003048 rc = efx_init_sriov();
3049 if (rc)
3050 goto err_sriov;
3051
Steve Hodgson1ab00622008-12-12 21:33:02 -08003052 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3053 if (!reset_workqueue) {
3054 rc = -ENOMEM;
3055 goto err_reset;
3056 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003057
3058 rc = pci_register_driver(&efx_pci_driver);
3059 if (rc < 0)
3060 goto err_pci;
3061
3062 return 0;
3063
3064 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003065 destroy_workqueue(reset_workqueue);
3066 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003067 efx_fini_sriov();
3068 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003069 unregister_netdevice_notifier(&efx_netdev_notifier);
3070 err_notifier:
3071 return rc;
3072}
3073
3074static void __exit efx_exit_module(void)
3075{
3076 printk(KERN_INFO "Solarflare NET driver unloading\n");
3077
3078 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003079 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003080 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003081 unregister_netdevice_notifier(&efx_netdev_notifier);
3082
3083}
3084
3085module_init(efx_init_module);
3086module_exit(efx_exit_module);
3087
Ben Hutchings906bb262009-11-29 15:16:19 +00003088MODULE_AUTHOR("Solarflare Communications and "
3089 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003090MODULE_DESCRIPTION("Solarflare Communications network driver");
3091MODULE_LICENSE("GPL");
3092MODULE_DEVICE_TABLE(pci, efx_pci_table);