blob: 11a81084bec4f78e406f1096af23c3957c3f1974 [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>
Ben Hutchings64d8ad62011-01-05 00:50:41 +000024#include <linux/cpu_rmap.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010025#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000027#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000028#include "selftest.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010029
Ben Hutchings8880f4e2009-11-29 15:15:41 +000030#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000031#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000032
Ben Hutchingsc4593022009-11-23 16:08:17 +000033/**************************************************************************
34 *
35 * Type name strings
36 *
37 **************************************************************************
38 */
39
40/* Loopback mode names (see LOOPBACK_MODE()) */
41const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000042const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000043 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000044 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000045 [LOOPBACK_GMAC] = "GMAC",
46 [LOOPBACK_XGMII] = "XGMII",
47 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000048 [LOOPBACK_XAUI] = "XAUI",
49 [LOOPBACK_GMII] = "GMII",
50 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000051 [LOOPBACK_XGBR] = "XGBR",
52 [LOOPBACK_XFI] = "XFI",
53 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
54 [LOOPBACK_GMII_FAR] = "GMII_FAR",
55 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
56 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000057 [LOOPBACK_GPHY] = "GPHY",
58 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000059 [LOOPBACK_PCS] = "PCS",
60 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000061 [LOOPBACK_XPORT] = "XPORT",
62 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000063 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000064 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
65 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000066 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000067 [LOOPBACK_XFI_WS] = "XFI_WS",
68 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000069 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000070};
71
Ben Hutchingsc4593022009-11-23 16:08:17 +000072const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000073const char *const efx_reset_type_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000074 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
75 [RESET_TYPE_ALL] = "ALL",
76 [RESET_TYPE_WORLD] = "WORLD",
77 [RESET_TYPE_DISABLE] = "DISABLE",
78 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
79 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
80 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
81 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
82 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
83 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
Ben Hutchings8880f4e2009-11-29 15:15:41 +000084 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Ben Hutchingsc4593022009-11-23 16:08:17 +000085};
86
Ben Hutchings8ceee662008-04-27 12:55:59 +010087#define EFX_MAX_MTU (9 * 1024)
88
Steve Hodgson1ab00622008-12-12 21:33:02 -080089/* Reset workqueue. If any NIC has a hardware failure then a reset will be
90 * queued onto this work queue. This is not a per-nic work queue, because
91 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
92 */
93static struct workqueue_struct *reset_workqueue;
94
Ben Hutchings8ceee662008-04-27 12:55:59 +010095/**************************************************************************
96 *
97 * Configurable values
98 *
99 *************************************************************************/
100
101/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100102 * Use separate channels for TX and RX events
103 *
Neil Turton28b581a2008-12-12 21:41:06 -0800104 * Set this to 1 to use separate channels for TX and RX. It allows us
105 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100106 *
Neil Turton28b581a2008-12-12 21:41:06 -0800107 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100108 */
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000109static bool separate_tx_channels;
110module_param(separate_tx_channels, bool, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800111MODULE_PARM_DESC(separate_tx_channels,
112 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100113
114/* This is the weight assigned to each of the (per-channel) virtual
115 * NAPI devices.
116 */
117static int napi_weight = 64;
118
119/* This is the time (in jiffies) between invocations of the hardware
Ben Hutchingse254c272010-09-20 08:44:10 +0000120 * monitor. On Falcon-based NICs, this will:
121 * - Check the on-board hardware monitor;
122 * - Poll the link state and reconfigure the hardware as necessary.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100123 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000124static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100125
Ben Hutchings8ceee662008-04-27 12:55:59 +0100126/* Initial interrupt moderation settings. They can be modified after
127 * module load with ethtool.
128 *
129 * The default for RX should strike a balance between increasing the
130 * round-trip latency and reducing overhead.
131 */
132static unsigned int rx_irq_mod_usec = 60;
133
134/* Initial interrupt moderation settings. They can be modified after
135 * module load with ethtool.
136 *
137 * This default is chosen to ensure that a 10G link does not go idle
138 * while a TX queue is stopped after it has become full. A queue is
139 * restarted when it drops below half full. The time this takes (assuming
140 * worst case 3 descriptors per packet and 1024 descriptors) is
141 * 512 / 3 * 1.2 = 205 usec.
142 */
143static unsigned int tx_irq_mod_usec = 150;
144
145/* This is the first interrupt mode to try out of:
146 * 0 => MSI-X
147 * 1 => MSI
148 * 2 => legacy
149 */
150static unsigned int interrupt_mode;
151
152/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
153 * i.e. the number of CPUs among which we may distribute simultaneous
154 * interrupt handling.
155 *
156 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000157 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100158 */
159static unsigned int rss_cpus;
160module_param(rss_cpus, uint, 0444);
161MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
162
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000163static bool phy_flash_cfg;
164module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800165MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
166
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000167static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000168module_param(irq_adapt_low_thresh, uint, 0644);
169MODULE_PARM_DESC(irq_adapt_low_thresh,
170 "Threshold score for reducing IRQ moderation");
171
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000172static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000173module_param(irq_adapt_high_thresh, uint, 0644);
174MODULE_PARM_DESC(irq_adapt_high_thresh,
175 "Threshold score for increasing IRQ moderation");
176
Ben Hutchings62776d02010-06-23 11:30:07 +0000177static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
178 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
179 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
180 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
181module_param(debug, uint, 0);
182MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
183
Ben Hutchings8ceee662008-04-27 12:55:59 +0100184/**************************************************************************
185 *
186 * Utility functions and prototypes
187 *
188 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000189
Ben Hutchings7f967c02012-02-13 23:45:02 +0000190static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq);
191static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq);
192static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000193static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000194static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100195static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000196static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100197static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000198static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000199static void efx_fini_struct(struct efx_nic *efx);
200static void efx_start_all(struct efx_nic *efx);
201static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100202
203#define EFX_ASSERT_RESET_SERIALISED(efx) \
204 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100205 if ((efx->state == STATE_READY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000206 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100207 ASSERT_RTNL(); \
208 } while (0)
209
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100210static int efx_check_disabled(struct efx_nic *efx)
211{
212 if (efx->state == STATE_DISABLED) {
213 netif_err(efx, drv, efx->net_dev,
214 "device is disabled due to earlier errors\n");
215 return -EIO;
216 }
217 return 0;
218}
219
Ben Hutchings8ceee662008-04-27 12:55:59 +0100220/**************************************************************************
221 *
222 * Event queue processing
223 *
224 *************************************************************************/
225
226/* Process channel's event queue
227 *
228 * This function is responsible for processing the event queue of a
229 * single channel. The caller must guarantee that this function will
230 * never be concurrently called more than once on the same channel,
231 * though different channels may be being processed concurrently.
232 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000233static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100234{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000235 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100236
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000237 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100238 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100239
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000240 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000241 if (spent && efx_channel_has_rx_queue(channel)) {
242 struct efx_rx_queue *rx_queue =
243 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100244
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000245 /* Deliver last RX packet. */
246 if (channel->rx_pkt) {
247 __efx_rx_packet(channel, channel->rx_pkt);
248 channel->rx_pkt = NULL;
249 }
Alexandre Rames97d48a12013-01-11 12:26:21 +0000250 if (rx_queue->enabled)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000251 efx_fast_push_rx_descriptors(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100252 }
253
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000254 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100255}
256
257/* Mark channel as finished processing
258 *
259 * Note that since we will not receive further interrupts for this
260 * channel before we finish processing and call the eventq_read_ack()
261 * method, there is no need to use the interrupt hold-off timers.
262 */
263static inline void efx_channel_processed(struct efx_channel *channel)
264{
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100265 /* The interrupt handler for this channel may set work_pending
266 * as soon as we acknowledge the events we've seen. Make sure
267 * it's cleared before then. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100268 channel->work_pending = false;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100269 smp_wmb();
270
Ben Hutchings152b6a62009-11-29 03:43:56 +0000271 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100272}
273
274/* NAPI poll handler
275 *
276 * NAPI guarantees serialisation of polls of the same device, which
277 * provides the guarantee required by efx_process_channel().
278 */
279static int efx_poll(struct napi_struct *napi, int budget)
280{
281 struct efx_channel *channel =
282 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000283 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000284 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100285
Ben Hutchings62776d02010-06-23 11:30:07 +0000286 netif_vdbg(efx, intr, efx->net_dev,
287 "channel %d NAPI poll executing on CPU %d\n",
288 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100289
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000290 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100291
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000292 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000293 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000294 efx->irq_rx_adaptive &&
295 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000296 if (unlikely(channel->irq_mod_score <
297 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000298 if (channel->irq_moderation > 1) {
299 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000300 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000301 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000302 } else if (unlikely(channel->irq_mod_score >
303 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000304 if (channel->irq_moderation <
305 efx->irq_rx_moderation) {
306 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000307 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000308 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000309 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000310 channel->irq_count = 0;
311 channel->irq_mod_score = 0;
312 }
313
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000314 efx_filter_rfs_expire(channel);
315
Ben Hutchings8ceee662008-04-27 12:55:59 +0100316 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800317 * only wait for napi_complete(), this isn't a problem
Ben Hutchings8ceee662008-04-27 12:55:59 +0100318 * since efx_channel_processed() will have no effect if
319 * interrupts have already been disabled.
320 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800321 napi_complete(napi);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100322 efx_channel_processed(channel);
323 }
324
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000325 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100326}
327
328/* Process the eventq of the specified channel immediately on this CPU
329 *
330 * Disable hardware generated interrupts, wait for any existing
331 * processing to finish, then directly poll (and ack ) the eventq.
332 * Finally reenable NAPI and interrupts.
333 *
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100334 * This is for use only during a loopback self-test. It must not
335 * deliver any packets up the stack as this can result in deadlock.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100336 */
337void efx_process_channel_now(struct efx_channel *channel)
338{
339 struct efx_nic *efx = channel->efx;
340
Ben Hutchings8313aca2010-09-10 06:41:57 +0000341 BUG_ON(channel->channel >= efx->n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100342 BUG_ON(!channel->enabled);
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100343 BUG_ON(!efx->loopback_selftest);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100344
345 /* Disable interrupts and wait for ISRs to complete */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000346 efx_nic_disable_interrupts(efx);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000347 if (efx->legacy_irq) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100348 synchronize_irq(efx->legacy_irq);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000349 efx->legacy_irq_enabled = false;
350 }
Ben Hutchings64ee3122008-09-01 12:47:38 +0100351 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100352 synchronize_irq(channel->irq);
353
354 /* Wait for any NAPI processing to complete */
355 napi_disable(&channel->napi_str);
356
357 /* Poll the channel */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000358 efx_process_channel(channel, channel->eventq_mask + 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100359
360 /* Ack the eventq. This may cause an interrupt to be generated
361 * when they are reenabled */
362 efx_channel_processed(channel);
363
364 napi_enable(&channel->napi_str);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000365 if (efx->legacy_irq)
366 efx->legacy_irq_enabled = true;
Ben Hutchings152b6a62009-11-29 03:43:56 +0000367 efx_nic_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100368}
369
370/* Create event queue
371 * Event queue memory allocations are done only once. If the channel
372 * is reset, the memory buffer will be reused; this guards against
373 * errors during channel reset and also simplifies interrupt handling.
374 */
375static int efx_probe_eventq(struct efx_channel *channel)
376{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000377 struct efx_nic *efx = channel->efx;
378 unsigned long entries;
379
Ben Hutchings86ee5302012-01-09 19:51:22 +0000380 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000381 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100382
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000383 /* Build an event queue with room for one event per tx and rx buffer,
384 * plus some extra for link state events and MCDI completions. */
385 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
386 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
387 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
388
Ben Hutchings152b6a62009-11-29 03:43:56 +0000389 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100390}
391
392/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100393static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100394{
Ben Hutchings62776d02010-06-23 11:30:07 +0000395 netif_dbg(channel->efx, drv, channel->efx->net_dev,
396 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100397
398 channel->eventq_read_ptr = 0;
399
Ben Hutchings152b6a62009-11-29 03:43:56 +0000400 efx_nic_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100401}
402
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000403/* Enable event queue processing and NAPI */
404static void efx_start_eventq(struct efx_channel *channel)
405{
406 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
407 "chan %d start event queue\n", channel->channel);
408
409 /* The interrupt handler for this channel may set work_pending
410 * as soon as we enable it. Make sure it's cleared before
411 * then. Similarly, make sure it sees the enabled flag set.
412 */
413 channel->work_pending = false;
414 channel->enabled = true;
415 smp_wmb();
416
417 napi_enable(&channel->napi_str);
418 efx_nic_eventq_read_ack(channel);
419}
420
421/* Disable event queue processing and NAPI */
422static void efx_stop_eventq(struct efx_channel *channel)
423{
424 if (!channel->enabled)
425 return;
426
427 napi_disable(&channel->napi_str);
428 channel->enabled = false;
429}
430
Ben Hutchings8ceee662008-04-27 12:55:59 +0100431static void efx_fini_eventq(struct efx_channel *channel)
432{
Ben Hutchings62776d02010-06-23 11:30:07 +0000433 netif_dbg(channel->efx, drv, channel->efx->net_dev,
434 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100435
Ben Hutchings152b6a62009-11-29 03:43:56 +0000436 efx_nic_fini_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100437}
438
439static void efx_remove_eventq(struct efx_channel *channel)
440{
Ben Hutchings62776d02010-06-23 11:30:07 +0000441 netif_dbg(channel->efx, drv, channel->efx->net_dev,
442 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100443
Ben Hutchings152b6a62009-11-29 03:43:56 +0000444 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100445}
446
447/**************************************************************************
448 *
449 * Channel handling
450 *
451 *************************************************************************/
452
Ben Hutchings7f967c02012-02-13 23:45:02 +0000453/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000454static struct efx_channel *
455efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
456{
457 struct efx_channel *channel;
458 struct efx_rx_queue *rx_queue;
459 struct efx_tx_queue *tx_queue;
460 int j;
461
Ben Hutchings7f967c02012-02-13 23:45:02 +0000462 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
463 if (!channel)
464 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000465
Ben Hutchings7f967c02012-02-13 23:45:02 +0000466 channel->efx = efx;
467 channel->channel = i;
468 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000469
Ben Hutchings7f967c02012-02-13 23:45:02 +0000470 for (j = 0; j < EFX_TXQ_TYPES; j++) {
471 tx_queue = &channel->tx_queue[j];
472 tx_queue->efx = efx;
473 tx_queue->queue = i * EFX_TXQ_TYPES + j;
474 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000475 }
476
Ben Hutchings46426102010-09-10 06:42:33 +0000477 rx_queue = &channel->rx_queue;
478 rx_queue->efx = efx;
479 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
480 (unsigned long)rx_queue);
481
482 return channel;
483}
484
Ben Hutchings7f967c02012-02-13 23:45:02 +0000485/* Allocate and initialise a channel structure, copying parameters
486 * (but not resources) from an old channel structure.
487 */
488static struct efx_channel *
489efx_copy_channel(const struct efx_channel *old_channel)
490{
491 struct efx_channel *channel;
492 struct efx_rx_queue *rx_queue;
493 struct efx_tx_queue *tx_queue;
494 int j;
495
496 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
497 if (!channel)
498 return NULL;
499
500 *channel = *old_channel;
501
502 channel->napi_dev = NULL;
503 memset(&channel->eventq, 0, sizeof(channel->eventq));
504
505 for (j = 0; j < EFX_TXQ_TYPES; j++) {
506 tx_queue = &channel->tx_queue[j];
507 if (tx_queue->channel)
508 tx_queue->channel = channel;
509 tx_queue->buffer = NULL;
510 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
511 }
512
513 rx_queue = &channel->rx_queue;
514 rx_queue->buffer = NULL;
515 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
516 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
517 (unsigned long)rx_queue);
518
519 return channel;
520}
521
Ben Hutchings8ceee662008-04-27 12:55:59 +0100522static int efx_probe_channel(struct efx_channel *channel)
523{
524 struct efx_tx_queue *tx_queue;
525 struct efx_rx_queue *rx_queue;
526 int rc;
527
Ben Hutchings62776d02010-06-23 11:30:07 +0000528 netif_dbg(channel->efx, probe, channel->efx->net_dev,
529 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100530
Ben Hutchings7f967c02012-02-13 23:45:02 +0000531 rc = channel->type->pre_probe(channel);
532 if (rc)
533 goto fail;
534
Ben Hutchings8ceee662008-04-27 12:55:59 +0100535 rc = efx_probe_eventq(channel);
536 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000537 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100538
539 efx_for_each_channel_tx_queue(tx_queue, channel) {
540 rc = efx_probe_tx_queue(tx_queue);
541 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000542 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100543 }
544
545 efx_for_each_channel_rx_queue(rx_queue, channel) {
546 rc = efx_probe_rx_queue(rx_queue);
547 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000548 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100549 }
550
551 channel->n_rx_frm_trunc = 0;
552
553 return 0;
554
Ben Hutchings7f967c02012-02-13 23:45:02 +0000555fail:
556 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100557 return rc;
558}
559
Ben Hutchings7f967c02012-02-13 23:45:02 +0000560static void
561efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
562{
563 struct efx_nic *efx = channel->efx;
564 const char *type;
565 int number;
566
567 number = channel->channel;
568 if (efx->tx_channel_offset == 0) {
569 type = "";
570 } else if (channel->channel < efx->tx_channel_offset) {
571 type = "-rx";
572 } else {
573 type = "-tx";
574 number -= efx->tx_channel_offset;
575 }
576 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
577}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100578
Ben Hutchings56536e92008-12-12 21:37:02 -0800579static void efx_set_channel_names(struct efx_nic *efx)
580{
581 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800582
Ben Hutchings7f967c02012-02-13 23:45:02 +0000583 efx_for_each_channel(channel, efx)
584 channel->type->get_name(channel,
585 efx->channel_name[channel->channel],
586 sizeof(efx->channel_name[0]));
Ben Hutchings56536e92008-12-12 21:37:02 -0800587}
588
Ben Hutchings46426102010-09-10 06:42:33 +0000589static int efx_probe_channels(struct efx_nic *efx)
590{
591 struct efx_channel *channel;
592 int rc;
593
594 /* Restart special buffer allocation */
595 efx->next_buffer_table = 0;
596
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000597 /* Probe channels in reverse, so that any 'extra' channels
598 * use the start of the buffer table. This allows the traffic
599 * channels to be resized without moving them or wasting the
600 * entries before them.
601 */
602 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000603 rc = efx_probe_channel(channel);
604 if (rc) {
605 netif_err(efx, probe, efx->net_dev,
606 "failed to create channel %d\n",
607 channel->channel);
608 goto fail;
609 }
610 }
611 efx_set_channel_names(efx);
612
613 return 0;
614
615fail:
616 efx_remove_channels(efx);
617 return rc;
618}
619
Ben Hutchings8ceee662008-04-27 12:55:59 +0100620/* Channels are shutdown and reinitialised whilst the NIC is running
621 * to propagate configuration changes (mtu, checksum offload), or
622 * to clear hardware error conditions
623 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000624static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100625{
626 struct efx_tx_queue *tx_queue;
627 struct efx_rx_queue *rx_queue;
628 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100629
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100630 /* Calculate the rx buffer allocation parameters required to
631 * support the current MTU, including padding for header
632 * alignment and overruns.
633 */
634 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
635 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
Ben Hutchings39c9cf02010-06-23 11:31:28 +0000636 efx->type->rx_buffer_hash_size +
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100637 efx->type->rx_buffer_padding);
Steve Hodgson62b330b2010-06-01 11:20:53 +0000638 efx->rx_buffer_order = get_order(efx->rx_buffer_len +
639 sizeof(struct efx_rx_page_state));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100640
Ben Hutchings14bf7182012-05-22 01:27:58 +0100641 /* We must keep at least one descriptor in a TX ring empty.
642 * We could avoid this when the queue size does not exactly
643 * match the hardware ring size, but it's not that important.
644 * Therefore we stop the queue when one more skb might fill
645 * the ring completely. We wake it when half way back to
646 * empty.
647 */
648 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
649 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
650
Ben Hutchings8ceee662008-04-27 12:55:59 +0100651 /* Initialise the channels */
652 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100653 efx_for_each_channel_tx_queue(tx_queue, channel)
654 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100655
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000656 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100657 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000658 efx_nic_generate_fill_event(rx_queue);
659 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100660
661 WARN_ON(channel->rx_pkt != NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100662 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000663
664 if (netif_device_present(efx->net_dev))
665 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100666}
667
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000668static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100669{
670 struct efx_channel *channel;
671 struct efx_tx_queue *tx_queue;
672 struct efx_rx_queue *rx_queue;
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100673 struct pci_dev *dev = efx->pci_dev;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100674 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100675
676 EFX_ASSERT_RESET_SERIALISED(efx);
677 BUG_ON(efx->port_enabled);
678
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100679 /* Only perform flush if dma is enabled */
680 if (dev->is_busmaster) {
681 rc = efx_nic_flush_queues(efx);
682
683 if (rc && EFX_WORKAROUND_7803(efx)) {
684 /* Schedule a reset to recover from the flush failure. The
685 * descriptor caches reference memory we're about to free,
686 * but falcon_reconfigure_mac_wrapper() won't reconnect
687 * the MACs because of the pending reset. */
688 netif_err(efx, drv, efx->net_dev,
689 "Resetting to recover from flush failure\n");
690 efx_schedule_reset(efx, RESET_TYPE_ALL);
691 } else if (rc) {
692 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
693 } else {
694 netif_dbg(efx, drv, efx->net_dev,
695 "successfully flushed all queues\n");
696 }
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000697 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100698
Ben Hutchings8ceee662008-04-27 12:55:59 +0100699 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000700 /* RX packet processing is pipelined, so wait for the
701 * NAPI handler to complete. At least event queue 0
702 * might be kept active by non-data events, so don't
703 * use napi_synchronize() but actually disable NAPI
704 * temporarily.
705 */
706 if (efx_channel_has_rx_queue(channel)) {
707 efx_stop_eventq(channel);
708 efx_start_eventq(channel);
709 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100710
711 efx_for_each_channel_rx_queue(rx_queue, channel)
712 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000713 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100714 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100715 }
716}
717
718static void efx_remove_channel(struct efx_channel *channel)
719{
720 struct efx_tx_queue *tx_queue;
721 struct efx_rx_queue *rx_queue;
722
Ben Hutchings62776d02010-06-23 11:30:07 +0000723 netif_dbg(channel->efx, drv, channel->efx->net_dev,
724 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100725
726 efx_for_each_channel_rx_queue(rx_queue, channel)
727 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000728 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100729 efx_remove_tx_queue(tx_queue);
730 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100731 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100732}
733
Ben Hutchings46426102010-09-10 06:42:33 +0000734static void efx_remove_channels(struct efx_nic *efx)
735{
736 struct efx_channel *channel;
737
738 efx_for_each_channel(channel, efx)
739 efx_remove_channel(channel);
740}
741
742int
743efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
744{
745 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
746 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000747 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100748 int rc;
749
750 rc = efx_check_disabled(efx);
751 if (rc)
752 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000753
754 /* Not all channels should be reallocated. We must avoid
755 * reallocating their buffer table entries.
756 */
757 efx_for_each_channel(channel, efx) {
758 struct efx_rx_queue *rx_queue;
759 struct efx_tx_queue *tx_queue;
760
761 if (channel->type->copy)
762 continue;
763 next_buffer_table = max(next_buffer_table,
764 channel->eventq.index +
765 channel->eventq.entries);
766 efx_for_each_channel_rx_queue(rx_queue, channel)
767 next_buffer_table = max(next_buffer_table,
768 rx_queue->rxd.index +
769 rx_queue->rxd.entries);
770 efx_for_each_channel_tx_queue(tx_queue, channel)
771 next_buffer_table = max(next_buffer_table,
772 tx_queue->txd.index +
773 tx_queue->txd.entries);
774 }
Ben Hutchings46426102010-09-10 06:42:33 +0000775
Ben Hutchings29c69a42013-01-28 19:01:06 +0000776 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000777 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000778 efx_stop_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000779
Ben Hutchings7f967c02012-02-13 23:45:02 +0000780 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000781 memset(other_channel, 0, sizeof(other_channel));
782 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000783 channel = efx->channel[i];
784 if (channel->type->copy)
785 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000786 if (!channel) {
787 rc = -ENOMEM;
788 goto out;
789 }
790 other_channel[i] = channel;
791 }
792
793 /* Swap entry counts and channel pointers */
794 old_rxq_entries = efx->rxq_entries;
795 old_txq_entries = efx->txq_entries;
796 efx->rxq_entries = rxq_entries;
797 efx->txq_entries = txq_entries;
798 for (i = 0; i < efx->n_channels; i++) {
799 channel = efx->channel[i];
800 efx->channel[i] = other_channel[i];
801 other_channel[i] = channel;
802 }
803
Ben Hutchings7f967c02012-02-13 23:45:02 +0000804 /* Restart buffer table allocation */
805 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000806
Ben Hutchingse8f14992010-12-07 19:47:34 +0000807 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000808 channel = efx->channel[i];
809 if (!channel->type->copy)
810 continue;
811 rc = efx_probe_channel(channel);
812 if (rc)
813 goto rollback;
814 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000815 }
Ben Hutchings46426102010-09-10 06:42:33 +0000816
Ben Hutchings7f967c02012-02-13 23:45:02 +0000817out:
818 /* Destroy unused channel structures */
819 for (i = 0; i < efx->n_channels; i++) {
820 channel = other_channel[i];
821 if (channel && channel->type->copy) {
822 efx_fini_napi_channel(channel);
823 efx_remove_channel(channel);
824 kfree(channel);
825 }
826 }
827
828 efx_start_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000829 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000830 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000831 return rc;
832
833rollback:
834 /* Swap back */
835 efx->rxq_entries = old_rxq_entries;
836 efx->txq_entries = old_txq_entries;
837 for (i = 0; i < efx->n_channels; i++) {
838 channel = efx->channel[i];
839 efx->channel[i] = other_channel[i];
840 other_channel[i] = channel;
841 }
842 goto out;
843}
844
Steve Hodgson90d683a2010-06-01 11:19:39 +0000845void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100846{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000847 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100848}
849
Ben Hutchings7f967c02012-02-13 23:45:02 +0000850static const struct efx_channel_type efx_default_channel_type = {
851 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100852 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000853 .get_name = efx_get_channel_name,
854 .copy = efx_copy_channel,
855 .keep_eventq = false,
856};
857
858int efx_channel_dummy_op_int(struct efx_channel *channel)
859{
860 return 0;
861}
862
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100863void efx_channel_dummy_op_void(struct efx_channel *channel)
864{
865}
866
Ben Hutchings8ceee662008-04-27 12:55:59 +0100867/**************************************************************************
868 *
869 * Port handling
870 *
871 **************************************************************************/
872
873/* This ensures that the kernel is kept informed (via
874 * netif_carrier_on/off) of the link status, and also maintains the
875 * link status's stop on the port's TX queue.
876 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000877void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100878{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000879 struct efx_link_state *link_state = &efx->link_state;
880
Ben Hutchings8ceee662008-04-27 12:55:59 +0100881 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
882 * that no events are triggered between unregister_netdev() and the
883 * driver unloading. A more general condition is that NETDEV_CHANGE
884 * can only be generated between NETDEV_UP and NETDEV_DOWN */
885 if (!netif_running(efx->net_dev))
886 return;
887
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000888 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100889 efx->n_link_state_changes++;
890
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000891 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100892 netif_carrier_on(efx->net_dev);
893 else
894 netif_carrier_off(efx->net_dev);
895 }
896
897 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000898 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000899 netif_info(efx, link, efx->net_dev,
900 "link up at %uMbps %s-duplex (MTU %d)%s\n",
901 link_state->speed, link_state->fd ? "full" : "half",
902 efx->net_dev->mtu,
903 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000904 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000905 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100906}
907
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000908void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
909{
910 efx->link_advertising = advertising;
911 if (advertising) {
912 if (advertising & ADVERTISED_Pause)
913 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
914 else
915 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
916 if (advertising & ADVERTISED_Asym_Pause)
917 efx->wanted_fc ^= EFX_FC_TX;
918 }
919}
920
David S. Millerb56269462011-05-17 17:53:22 -0400921void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000922{
923 efx->wanted_fc = wanted_fc;
924 if (efx->link_advertising) {
925 if (wanted_fc & EFX_FC_RX)
926 efx->link_advertising |= (ADVERTISED_Pause |
927 ADVERTISED_Asym_Pause);
928 else
929 efx->link_advertising &= ~(ADVERTISED_Pause |
930 ADVERTISED_Asym_Pause);
931 if (wanted_fc & EFX_FC_TX)
932 efx->link_advertising ^= ADVERTISED_Asym_Pause;
933 }
934}
935
Ben Hutchings115122a2009-03-04 09:52:52 +0000936static void efx_fini_port(struct efx_nic *efx);
937
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000938/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
939 * the MAC appropriately. All other PHY configuration changes are pushed
940 * through phy_op->set_settings(), and pushed asynchronously to the MAC
941 * through efx_monitor().
942 *
943 * Callers must hold the mac_lock
944 */
945int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100946{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000947 enum efx_phy_mode phy_mode;
948 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100949
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000950 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100951
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000952 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000953 netif_addr_lock_bh(efx->net_dev);
954 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100955
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000956 /* Disable PHY transmit in mac level loopbacks */
957 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800958 if (LOOPBACK_INTERNAL(efx))
959 efx->phy_mode |= PHY_MODE_TX_DISABLED;
960 else
961 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800962
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000963 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800964
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000965 if (rc)
966 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100967
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000968 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100969}
970
971/* Reinitialise the MAC to pick up new PHY settings, even if the port is
972 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000973int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100974{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000975 int rc;
976
Ben Hutchings8ceee662008-04-27 12:55:59 +0100977 EFX_ASSERT_RESET_SERIALISED(efx);
978
979 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000980 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100981 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000982
983 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100984}
985
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000986/* Asynchronous work item for changing MAC promiscuity and multicast
987 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
988 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800989static void efx_mac_work(struct work_struct *data)
990{
991 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
992
993 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100994 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100995 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800996 mutex_unlock(&efx->mac_lock);
997}
998
Ben Hutchings8ceee662008-04-27 12:55:59 +0100999static int efx_probe_port(struct efx_nic *efx)
1000{
1001 int rc;
1002
Ben Hutchings62776d02010-06-23 11:30:07 +00001003 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001004
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001005 if (phy_flash_cfg)
1006 efx->phy_mode = PHY_MODE_SPECIAL;
1007
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001008 /* Connect up MAC/PHY operations table */
1009 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001010 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001011 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001012
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001013 /* Initialise MAC address to permanent address */
1014 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001015
1016 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001017}
1018
1019static int efx_init_port(struct efx_nic *efx)
1020{
1021 int rc;
1022
Ben Hutchings62776d02010-06-23 11:30:07 +00001023 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001024
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001025 mutex_lock(&efx->mac_lock);
1026
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001027 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001028 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001029 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001030
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001031 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001032
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001033 /* Reconfigure the MAC before creating dma queues (required for
1034 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001035 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001036
1037 /* Ensure the PHY advertises the correct flow control settings */
1038 rc = efx->phy_op->reconfigure(efx);
1039 if (rc)
1040 goto fail2;
1041
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001042 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001043 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001044
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001045fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001046 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001047fail1:
1048 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001049 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001050}
1051
Ben Hutchings8ceee662008-04-27 12:55:59 +01001052static void efx_start_port(struct efx_nic *efx)
1053{
Ben Hutchings62776d02010-06-23 11:30:07 +00001054 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001055 BUG_ON(efx->port_enabled);
1056
1057 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001058 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001059
1060 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1061 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001062 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001063
Ben Hutchings8ceee662008-04-27 12:55:59 +01001064 mutex_unlock(&efx->mac_lock);
1065}
1066
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001067/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001068static void efx_stop_port(struct efx_nic *efx)
1069{
Ben Hutchings62776d02010-06-23 11:30:07 +00001070 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001071
1072 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001073 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001074 mutex_unlock(&efx->mac_lock);
1075
1076 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001077 netif_addr_lock_bh(efx->net_dev);
1078 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001079}
1080
1081static void efx_fini_port(struct efx_nic *efx)
1082{
Ben Hutchings62776d02010-06-23 11:30:07 +00001083 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001084
1085 if (!efx->port_initialized)
1086 return;
1087
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001088 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001089 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001090
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001091 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001092 efx_link_status_changed(efx);
1093}
1094
1095static void efx_remove_port(struct efx_nic *efx)
1096{
Ben Hutchings62776d02010-06-23 11:30:07 +00001097 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001098
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001099 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001100}
1101
1102/**************************************************************************
1103 *
1104 * NIC handling
1105 *
1106 **************************************************************************/
1107
1108/* This configures the PCI device to enable I/O and DMA. */
1109static int efx_init_io(struct efx_nic *efx)
1110{
1111 struct pci_dev *pci_dev = efx->pci_dev;
1112 dma_addr_t dma_mask = efx->type->max_dma_mask;
1113 int rc;
1114
Ben Hutchings62776d02010-06-23 11:30:07 +00001115 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001116
1117 rc = pci_enable_device(pci_dev);
1118 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001119 netif_err(efx, probe, efx->net_dev,
1120 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001121 goto fail1;
1122 }
1123
1124 pci_set_master(pci_dev);
1125
1126 /* Set the PCI DMA mask. Try all possibilities from our
1127 * genuine mask down to 32 bits, because some architectures
1128 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1129 * masks event though they reject 46 bit masks.
1130 */
1131 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001132 if (dma_supported(&pci_dev->dev, dma_mask)) {
1133 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001134 if (rc == 0)
1135 break;
1136 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001137 dma_mask >>= 1;
1138 }
1139 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001140 netif_err(efx, probe, efx->net_dev,
1141 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001142 goto fail2;
1143 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001144 netif_dbg(efx, probe, efx->net_dev,
1145 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001146 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001147 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001148 /* dma_set_coherent_mask() is not *allowed* to
1149 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001150 * but just in case...
1151 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001152 netif_err(efx, probe, efx->net_dev,
1153 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001154 goto fail2;
1155 }
1156
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001157 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1158 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001159 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001160 netif_err(efx, probe, efx->net_dev,
1161 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001162 rc = -EIO;
1163 goto fail3;
1164 }
David S. Miller8decf862011-09-22 03:23:13 -04001165 efx->membase = ioremap_nocache(efx->membase_phys,
1166 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001167 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001168 netif_err(efx, probe, efx->net_dev,
1169 "could not map memory BAR at %llx+%x\n",
1170 (unsigned long long)efx->membase_phys,
1171 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001172 rc = -ENOMEM;
1173 goto fail4;
1174 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001175 netif_dbg(efx, probe, efx->net_dev,
1176 "memory BAR at %llx+%x (virtual %p)\n",
1177 (unsigned long long)efx->membase_phys,
1178 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001179
1180 return 0;
1181
1182 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001183 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001184 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001185 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001186 fail2:
1187 pci_disable_device(efx->pci_dev);
1188 fail1:
1189 return rc;
1190}
1191
1192static void efx_fini_io(struct efx_nic *efx)
1193{
Ben Hutchings62776d02010-06-23 11:30:07 +00001194 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001195
1196 if (efx->membase) {
1197 iounmap(efx->membase);
1198 efx->membase = NULL;
1199 }
1200
1201 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001202 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001203 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001204 }
1205
1206 pci_disable_device(efx->pci_dev);
1207}
1208
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001209static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001210{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001211 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001212 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001213 int cpu;
1214
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001215 if (rss_cpus) {
1216 count = rss_cpus;
1217 } else {
1218 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1219 netif_warn(efx, probe, efx->net_dev,
1220 "RSS disabled due to allocation failure\n");
1221 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001222 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001223
1224 count = 0;
1225 for_each_online_cpu(cpu) {
1226 if (!cpumask_test_cpu(cpu, thread_mask)) {
1227 ++count;
1228 cpumask_or(thread_mask, thread_mask,
1229 topology_thread_cpumask(cpu));
1230 }
1231 }
1232
1233 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001234 }
1235
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001236 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1237 * table entries that are inaccessible to VFs
1238 */
1239 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1240 count > efx_vf_size(efx)) {
1241 netif_warn(efx, probe, efx->net_dev,
1242 "Reducing number of RSS channels from %u to %u for "
1243 "VF support. Increase vf-msix-limit to use more "
1244 "channels on the PF.\n",
1245 count, efx_vf_size(efx));
1246 count = efx_vf_size(efx);
1247 }
1248
Ben Hutchings46123d02008-09-01 12:47:33 +01001249 return count;
1250}
1251
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001252static int
1253efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1254{
1255#ifdef CONFIG_RFS_ACCEL
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001256 unsigned int i;
1257 int rc;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001258
1259 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1260 if (!efx->net_dev->rx_cpu_rmap)
1261 return -ENOMEM;
1262 for (i = 0; i < efx->n_rx_channels; i++) {
1263 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1264 xentries[i].vector);
1265 if (rc) {
1266 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1267 efx->net_dev->rx_cpu_rmap = NULL;
1268 return rc;
1269 }
1270 }
1271#endif
1272 return 0;
1273}
1274
Ben Hutchings46123d02008-09-01 12:47:33 +01001275/* Probe the number and type of interrupts we are able to obtain, and
1276 * the resulting numbers of channels and RX queues.
1277 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001278static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001279{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001280 unsigned int max_channels =
1281 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001282 unsigned int extra_channels = 0;
1283 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001284 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001285
Ben Hutchings7f967c02012-02-13 23:45:02 +00001286 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1287 if (efx->extra_channel_type[i])
1288 ++extra_channels;
1289
Ben Hutchings8ceee662008-04-27 12:55:59 +01001290 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001291 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001292 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001293
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001294 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001295 if (separate_tx_channels)
1296 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001297 n_channels += extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001298 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001299
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001300 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001301 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001302 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001303 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001304 netif_err(efx, drv, efx->net_dev,
1305 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001306 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001307 netif_err(efx, drv, efx->net_dev,
1308 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001309 EFX_BUG_ON_PARANOID(rc >= n_channels);
1310 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001311 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001312 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001313 }
1314
1315 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001316 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001317 if (n_channels > extra_channels)
1318 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001319 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001320 efx->n_tx_channels = max(n_channels / 2, 1U);
1321 efx->n_rx_channels = max(n_channels -
1322 efx->n_tx_channels,
1323 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001324 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001325 efx->n_tx_channels = n_channels;
1326 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001327 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001328 rc = efx_init_rx_cpu_rmap(efx, xentries);
1329 if (rc) {
1330 pci_disable_msix(efx->pci_dev);
1331 return rc;
1332 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001333 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001334 efx_get_channel(efx, i)->irq =
1335 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001336 } else {
1337 /* Fall back to single channel MSI */
1338 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001339 netif_err(efx, drv, efx->net_dev,
1340 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001341 }
1342 }
1343
1344 /* Try single interrupt MSI */
1345 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001346 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001347 efx->n_rx_channels = 1;
1348 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001349 rc = pci_enable_msi(efx->pci_dev);
1350 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001351 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001352 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001353 netif_err(efx, drv, efx->net_dev,
1354 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001355 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1356 }
1357 }
1358
1359 /* Assume legacy interrupts */
1360 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001361 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001362 efx->n_rx_channels = 1;
1363 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001364 efx->legacy_irq = efx->pci_dev->irq;
1365 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001366
Ben Hutchings7f967c02012-02-13 23:45:02 +00001367 /* Assign extra channels if possible */
1368 j = efx->n_channels;
1369 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1370 if (!efx->extra_channel_type[i])
1371 continue;
1372 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1373 efx->n_channels <= extra_channels) {
1374 efx->extra_channel_type[i]->handle_no_channel(efx);
1375 } else {
1376 --j;
1377 efx_get_channel(efx, j)->type =
1378 efx->extra_channel_type[i];
1379 }
1380 }
1381
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001382 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001383 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001384 efx->n_rx_channels : efx_vf_size(efx));
1385
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001386 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001387}
1388
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001389/* Enable interrupts, then probe and start the event queues */
Ben Hutchings7f967c02012-02-13 23:45:02 +00001390static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001391{
1392 struct efx_channel *channel;
1393
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001394 BUG_ON(efx->state == STATE_DISABLED);
1395
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001396 if (efx->legacy_irq)
1397 efx->legacy_irq_enabled = true;
1398 efx_nic_enable_interrupts(efx);
1399
1400 efx_for_each_channel(channel, efx) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001401 if (!channel->type->keep_eventq || !may_keep_eventq)
1402 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001403 efx_start_eventq(channel);
1404 }
1405
1406 efx_mcdi_mode_event(efx);
1407}
1408
Ben Hutchings7f967c02012-02-13 23:45:02 +00001409static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001410{
1411 struct efx_channel *channel;
1412
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001413 if (efx->state == STATE_DISABLED)
1414 return;
1415
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001416 efx_mcdi_mode_poll(efx);
1417
1418 efx_nic_disable_interrupts(efx);
1419 if (efx->legacy_irq) {
1420 synchronize_irq(efx->legacy_irq);
1421 efx->legacy_irq_enabled = false;
1422 }
1423
1424 efx_for_each_channel(channel, efx) {
1425 if (channel->irq)
1426 synchronize_irq(channel->irq);
1427
1428 efx_stop_eventq(channel);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001429 if (!channel->type->keep_eventq || !may_keep_eventq)
1430 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001431 }
1432}
1433
Ben Hutchings8ceee662008-04-27 12:55:59 +01001434static void efx_remove_interrupts(struct efx_nic *efx)
1435{
1436 struct efx_channel *channel;
1437
1438 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001439 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001440 channel->irq = 0;
1441 pci_disable_msi(efx->pci_dev);
1442 pci_disable_msix(efx->pci_dev);
1443
1444 /* Remove legacy interrupt */
1445 efx->legacy_irq = 0;
1446}
1447
Ben Hutchings8831da72008-09-01 12:47:48 +01001448static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001449{
Ben Hutchings602a5322011-05-16 17:32:39 +01001450 struct efx_channel *channel;
1451 struct efx_tx_queue *tx_queue;
1452
Ben Hutchings97653432011-01-12 18:26:56 +00001453 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001454 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001455
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001456 /* We need to mark which channels really have RX and TX
1457 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001458 * RX-only and TX-only channels.
1459 */
1460 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001461 if (channel->channel < efx->n_rx_channels)
1462 channel->rx_queue.core_index = channel->channel;
1463 else
1464 channel->rx_queue.core_index = -1;
1465
Ben Hutchings602a5322011-05-16 17:32:39 +01001466 efx_for_each_channel_tx_queue(tx_queue, channel)
1467 tx_queue->queue -= (efx->tx_channel_offset *
1468 EFX_TXQ_TYPES);
1469 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001470}
1471
1472static int efx_probe_nic(struct efx_nic *efx)
1473{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001474 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001475 int rc;
1476
Ben Hutchings62776d02010-06-23 11:30:07 +00001477 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001478
1479 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001480 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001481 if (rc)
1482 return rc;
1483
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001484 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001485 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001486 rc = efx_probe_interrupts(efx);
1487 if (rc)
1488 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001489
Ben Hutchings28e47c42012-02-15 01:58:49 +00001490 efx->type->dimension_resources(efx);
1491
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001492 if (efx->n_channels > 1)
1493 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001494 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001495 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001496 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001497
Ben Hutchings8831da72008-09-01 12:47:48 +01001498 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001499 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1500 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001501
1502 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001503 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1504 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001505
1506 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001507
1508fail:
1509 efx->type->remove(efx);
1510 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001511}
1512
1513static void efx_remove_nic(struct efx_nic *efx)
1514{
Ben Hutchings62776d02010-06-23 11:30:07 +00001515 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001516
1517 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001518 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001519}
1520
1521/**************************************************************************
1522 *
1523 * NIC startup/shutdown
1524 *
1525 *************************************************************************/
1526
1527static int efx_probe_all(struct efx_nic *efx)
1528{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001529 int rc;
1530
Ben Hutchings8ceee662008-04-27 12:55:59 +01001531 rc = efx_probe_nic(efx);
1532 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001533 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001534 goto fail1;
1535 }
1536
Ben Hutchings8ceee662008-04-27 12:55:59 +01001537 rc = efx_probe_port(efx);
1538 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001539 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001540 goto fail2;
1541 }
1542
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001543 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1544 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1545 rc = -EINVAL;
1546 goto fail3;
1547 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001548 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001549
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001550 rc = efx_probe_filters(efx);
1551 if (rc) {
1552 netif_err(efx, probe, efx->net_dev,
1553 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001554 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001555 }
1556
Ben Hutchings7f967c02012-02-13 23:45:02 +00001557 rc = efx_probe_channels(efx);
1558 if (rc)
1559 goto fail4;
1560
Ben Hutchings8ceee662008-04-27 12:55:59 +01001561 return 0;
1562
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001563 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001564 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001565 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001566 efx_remove_port(efx);
1567 fail2:
1568 efx_remove_nic(efx);
1569 fail1:
1570 return rc;
1571}
1572
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001573/* If the interface is supposed to be running but is not, start
1574 * the hardware and software data path, regular activity for the port
1575 * (MAC statistics, link polling, etc.) and schedule the port to be
1576 * reconfigured. Interrupts must already be enabled. This function
1577 * is safe to call multiple times, so long as the NIC is not disabled.
1578 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001579 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001580static void efx_start_all(struct efx_nic *efx)
1581{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001582 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001583 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001584
1585 /* Check that it is appropriate to restart the interface. All
1586 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001587 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001588 return;
1589
Ben Hutchings8ceee662008-04-27 12:55:59 +01001590 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001591 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001592
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001593 /* Start the hardware monitor if there is one. Otherwise (we're link
1594 * event driven), we have to poll the PHY because after an event queue
1595 * flush, we could have a missed a link state change */
1596 if (efx->type->monitor != NULL) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001597 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1598 efx_monitor_interval);
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001599 } else {
1600 mutex_lock(&efx->mac_lock);
1601 if (efx->phy_op->poll(efx))
1602 efx_link_status_changed(efx);
1603 mutex_unlock(&efx->mac_lock);
1604 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001605
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001606 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001607}
1608
1609/* Flush all delayed work. Should only be called when no more delayed work
1610 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1611 * since we're holding the rtnl_lock at this point. */
1612static void efx_flush_all(struct efx_nic *efx)
1613{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001614 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001615 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001616 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001617 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001618 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001619}
1620
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001621/* Quiesce the hardware and software data path, and regular activity
1622 * for the port without bringing the link down. Safe to call multiple
1623 * times with the NIC in almost any state, but interrupts should be
1624 * enabled. Requires the RTNL lock.
1625 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001626static void efx_stop_all(struct efx_nic *efx)
1627{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001628 EFX_ASSERT_RESET_SERIALISED(efx);
1629
1630 /* port_enabled can be read safely under the rtnl lock */
1631 if (!efx->port_enabled)
1632 return;
1633
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001634 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001635 efx_stop_port(efx);
1636
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001637 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001638 efx_flush_all(efx);
1639
Ben Hutchings29c69a42013-01-28 19:01:06 +00001640 /* Stop the kernel transmit interface. This is only valid if
1641 * the device is stopped or detached; otherwise the watchdog
1642 * may fire immediately.
1643 */
1644 WARN_ON(netif_running(efx->net_dev) &&
1645 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001646 netif_tx_disable(efx->net_dev);
1647
1648 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001649}
1650
1651static void efx_remove_all(struct efx_nic *efx)
1652{
Ben Hutchings46426102010-09-10 06:42:33 +00001653 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001654 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001655 efx_remove_port(efx);
1656 efx_remove_nic(efx);
1657}
1658
Ben Hutchings8ceee662008-04-27 12:55:59 +01001659/**************************************************************************
1660 *
1661 * Interrupt moderation
1662 *
1663 **************************************************************************/
1664
Ben Hutchingscc180b62011-12-08 19:51:47 +00001665static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001666{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001667 if (usecs == 0)
1668 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001669 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001670 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001671 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001672}
1673
Ben Hutchings8ceee662008-04-27 12:55:59 +01001674/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001675int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1676 unsigned int rx_usecs, bool rx_adaptive,
1677 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001678{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001679 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001680 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1681 efx->timer_quantum_ns,
1682 1000);
1683 unsigned int tx_ticks;
1684 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001685
1686 EFX_ASSERT_RESET_SERIALISED(efx);
1687
Ben Hutchingscc180b62011-12-08 19:51:47 +00001688 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001689 return -EINVAL;
1690
Ben Hutchingscc180b62011-12-08 19:51:47 +00001691 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1692 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1693
Ben Hutchings9e393b32011-09-05 07:43:04 +00001694 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1695 !rx_may_override_tx) {
1696 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1697 "RX and TX IRQ moderation must be equal\n");
1698 return -EINVAL;
1699 }
1700
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001701 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001702 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001703 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001704 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001705 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001706 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001707 channel->irq_moderation = tx_ticks;
1708 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001709
1710 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001711}
1712
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001713void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1714 unsigned int *rx_usecs, bool *rx_adaptive)
1715{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001716 /* We must round up when converting ticks to microseconds
1717 * because we round down when converting the other way.
1718 */
1719
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001720 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001721 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1722 efx->timer_quantum_ns,
1723 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001724
1725 /* If channels are shared between RX and TX, so is IRQ
1726 * moderation. Otherwise, IRQ moderation is the same for all
1727 * TX channels and is not adaptive.
1728 */
1729 if (efx->tx_channel_offset == 0)
1730 *tx_usecs = *rx_usecs;
1731 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001732 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001733 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001734 efx->timer_quantum_ns,
1735 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001736}
1737
Ben Hutchings8ceee662008-04-27 12:55:59 +01001738/**************************************************************************
1739 *
1740 * Hardware monitor
1741 *
1742 **************************************************************************/
1743
Ben Hutchingse254c272010-09-20 08:44:10 +00001744/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001745static void efx_monitor(struct work_struct *data)
1746{
1747 struct efx_nic *efx = container_of(data, struct efx_nic,
1748 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001749
Ben Hutchings62776d02010-06-23 11:30:07 +00001750 netif_vdbg(efx, timer, efx->net_dev,
1751 "hardware monitor executing on CPU %d\n",
1752 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001753 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001754
Ben Hutchings8ceee662008-04-27 12:55:59 +01001755 /* If the mac_lock is already held then it is likely a port
1756 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001757 * most of the work of monitor() anyway. */
1758 if (mutex_trylock(&efx->mac_lock)) {
1759 if (efx->port_enabled)
1760 efx->type->monitor(efx);
1761 mutex_unlock(&efx->mac_lock);
1762 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001763
Ben Hutchings8ceee662008-04-27 12:55:59 +01001764 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1765 efx_monitor_interval);
1766}
1767
1768/**************************************************************************
1769 *
1770 * ioctls
1771 *
1772 *************************************************************************/
1773
1774/* Net device ioctl
1775 * Context: process, rtnl_lock() held.
1776 */
1777static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1778{
Ben Hutchings767e4682008-09-01 12:43:14 +01001779 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001780 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001781
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001782 if (cmd == SIOCSHWTSTAMP)
1783 return efx_ptp_ioctl(efx, ifr, cmd);
1784
Ben Hutchings68e7f452009-04-29 08:05:08 +00001785 /* Convert phy_id from older PRTAD/DEVAD format */
1786 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1787 (data->phy_id & 0xfc00) == 0x0400)
1788 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1789
1790 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001791}
1792
1793/**************************************************************************
1794 *
1795 * NAPI interface
1796 *
1797 **************************************************************************/
1798
Ben Hutchings7f967c02012-02-13 23:45:02 +00001799static void efx_init_napi_channel(struct efx_channel *channel)
1800{
1801 struct efx_nic *efx = channel->efx;
1802
1803 channel->napi_dev = efx->net_dev;
1804 netif_napi_add(channel->napi_dev, &channel->napi_str,
1805 efx_poll, napi_weight);
1806}
1807
Ben Hutchingse8f14992010-12-07 19:47:34 +00001808static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001809{
1810 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001811
Ben Hutchings7f967c02012-02-13 23:45:02 +00001812 efx_for_each_channel(channel, efx)
1813 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001814}
1815
1816static void efx_fini_napi_channel(struct efx_channel *channel)
1817{
1818 if (channel->napi_dev)
1819 netif_napi_del(&channel->napi_str);
1820 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001821}
1822
1823static void efx_fini_napi(struct efx_nic *efx)
1824{
1825 struct efx_channel *channel;
1826
Ben Hutchingse8f14992010-12-07 19:47:34 +00001827 efx_for_each_channel(channel, efx)
1828 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001829}
1830
1831/**************************************************************************
1832 *
1833 * Kernel netpoll interface
1834 *
1835 *************************************************************************/
1836
1837#ifdef CONFIG_NET_POLL_CONTROLLER
1838
1839/* Although in the common case interrupts will be disabled, this is not
1840 * guaranteed. However, all our work happens inside the NAPI callback,
1841 * so no locking is required.
1842 */
1843static void efx_netpoll(struct net_device *net_dev)
1844{
Ben Hutchings767e4682008-09-01 12:43:14 +01001845 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001846 struct efx_channel *channel;
1847
Ben Hutchings64ee3122008-09-01 12:47:38 +01001848 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001849 efx_schedule_channel(channel);
1850}
1851
1852#endif
1853
1854/**************************************************************************
1855 *
1856 * Kernel net device interface
1857 *
1858 *************************************************************************/
1859
1860/* Context: process, rtnl_lock() held. */
1861static int efx_net_open(struct net_device *net_dev)
1862{
Ben Hutchings767e4682008-09-01 12:43:14 +01001863 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001864 int rc;
1865
Ben Hutchings62776d02010-06-23 11:30:07 +00001866 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1867 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001868
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001869 rc = efx_check_disabled(efx);
1870 if (rc)
1871 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001872 if (efx->phy_mode & PHY_MODE_SPECIAL)
1873 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001874 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1875 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001876
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001877 /* Notify the kernel of the link state polled during driver load,
1878 * before the monitor starts running */
1879 efx_link_status_changed(efx);
1880
Ben Hutchings8ceee662008-04-27 12:55:59 +01001881 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001882 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001883 return 0;
1884}
1885
1886/* Context: process, rtnl_lock() held.
1887 * Note that the kernel will ignore our return code; this method
1888 * should really be a void.
1889 */
1890static int efx_net_stop(struct net_device *net_dev)
1891{
Ben Hutchings767e4682008-09-01 12:43:14 +01001892 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001893
Ben Hutchings62776d02010-06-23 11:30:07 +00001894 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1895 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001896
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001897 /* Stop the device and flush all the channels */
1898 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001899
1900 return 0;
1901}
1902
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001903/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001904static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1905 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001906{
Ben Hutchings767e4682008-09-01 12:43:14 +01001907 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001908 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001909
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001910 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001911
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001912 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001913
1914 stats->rx_packets = mac_stats->rx_packets;
1915 stats->tx_packets = mac_stats->tx_packets;
1916 stats->rx_bytes = mac_stats->rx_bytes;
1917 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001918 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001919 stats->multicast = mac_stats->rx_multicast;
1920 stats->collisions = mac_stats->tx_collision;
1921 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1922 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001923 stats->rx_crc_errors = mac_stats->rx_bad;
1924 stats->rx_frame_errors = mac_stats->rx_align_error;
1925 stats->rx_fifo_errors = mac_stats->rx_overflow;
1926 stats->rx_missed_errors = mac_stats->rx_missed;
1927 stats->tx_window_errors = mac_stats->tx_late_collision;
1928
1929 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001930 stats->rx_crc_errors +
1931 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001932 mac_stats->rx_symbol_error);
1933 stats->tx_errors = (stats->tx_window_errors +
1934 mac_stats->tx_bad);
1935
Ben Hutchings1cb34522011-09-02 23:23:00 +01001936 spin_unlock_bh(&efx->stats_lock);
1937
Ben Hutchings8ceee662008-04-27 12:55:59 +01001938 return stats;
1939}
1940
1941/* Context: netif_tx_lock held, BHs disabled. */
1942static void efx_watchdog(struct net_device *net_dev)
1943{
Ben Hutchings767e4682008-09-01 12:43:14 +01001944 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001945
Ben Hutchings62776d02010-06-23 11:30:07 +00001946 netif_err(efx, tx_err, efx->net_dev,
1947 "TX stuck with port_enabled=%d: resetting channels\n",
1948 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001949
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001950 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001951}
1952
1953
1954/* Context: process, rtnl_lock() held. */
1955static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1956{
Ben Hutchings767e4682008-09-01 12:43:14 +01001957 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001958 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001959
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001960 rc = efx_check_disabled(efx);
1961 if (rc)
1962 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001963 if (new_mtu > EFX_MAX_MTU)
1964 return -EINVAL;
1965
Ben Hutchings62776d02010-06-23 11:30:07 +00001966 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001967
Ben Hutchings29c69a42013-01-28 19:01:06 +00001968 efx_device_detach_sync(efx);
1969 efx_stop_all(efx);
1970
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001971 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001972 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001973 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001974 mutex_unlock(&efx->mac_lock);
1975
Ben Hutchings8ceee662008-04-27 12:55:59 +01001976 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00001977 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001978 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001979}
1980
1981static int efx_set_mac_address(struct net_device *net_dev, void *data)
1982{
Ben Hutchings767e4682008-09-01 12:43:14 +01001983 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001984 struct sockaddr *addr = data;
1985 char *new_addr = addr->sa_data;
1986
Ben Hutchings8ceee662008-04-27 12:55:59 +01001987 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001988 netif_err(efx, drv, efx->net_dev,
1989 "invalid ethernet MAC address requested: %pM\n",
1990 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00001991 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001992 }
1993
1994 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001995 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001996
1997 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001998 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01001999 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002000 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002001
2002 return 0;
2003}
2004
Ben Hutchingsa816f752008-09-01 12:49:12 +01002005/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002006static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002007{
Ben Hutchings767e4682008-09-01 12:43:14 +01002008 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002009 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002010 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002011 u32 crc;
2012 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002013
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002014 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002015
2016 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002017 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002018 memset(mc_hash, 0xff, sizeof(*mc_hash));
2019 } else {
2020 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00002021 netdev_for_each_mc_addr(ha, net_dev) {
2022 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002023 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
Ben Hutchings32766ec2012-10-04 17:13:03 -07002024 __set_bit_le(bit, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002025 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002026
2027 /* Broadcast packets go through the multicast hash filter.
2028 * ether_crc_le() of the broadcast address is 0xbe2612ff
2029 * so we always add bit 0xff to the mask.
2030 */
Ben Hutchings32766ec2012-10-04 17:13:03 -07002031 __set_bit_le(0xff, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002032 }
2033
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002034 if (efx->port_enabled)
2035 queue_work(efx->workqueue, &efx->mac_work);
2036 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002037}
2038
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002039static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002040{
2041 struct efx_nic *efx = netdev_priv(net_dev);
2042
2043 /* If disabling RX n-tuple filtering, clear existing filters */
2044 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2045 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2046
2047 return 0;
2048}
2049
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002050static const struct net_device_ops efx_netdev_ops = {
2051 .ndo_open = efx_net_open,
2052 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002053 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002054 .ndo_tx_timeout = efx_watchdog,
2055 .ndo_start_xmit = efx_hard_start_xmit,
2056 .ndo_validate_addr = eth_validate_addr,
2057 .ndo_do_ioctl = efx_ioctl,
2058 .ndo_change_mtu = efx_change_mtu,
2059 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002060 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002061 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002062#ifdef CONFIG_SFC_SRIOV
2063 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2064 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2065 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2066 .ndo_get_vf_config = efx_sriov_get_vf_config,
2067#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002068#ifdef CONFIG_NET_POLL_CONTROLLER
2069 .ndo_poll_controller = efx_netpoll,
2070#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002071 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002072#ifdef CONFIG_RFS_ACCEL
2073 .ndo_rx_flow_steer = efx_filter_rfs,
2074#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002075};
2076
Ben Hutchings7dde5962008-12-12 22:09:38 -08002077static void efx_update_name(struct efx_nic *efx)
2078{
2079 strcpy(efx->name, efx->net_dev->name);
2080 efx_mtd_rename(efx);
2081 efx_set_channel_names(efx);
2082}
2083
Ben Hutchings8ceee662008-04-27 12:55:59 +01002084static int efx_netdev_event(struct notifier_block *this,
2085 unsigned long event, void *ptr)
2086{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01002087 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002088
Ben Hutchings7dde5962008-12-12 22:09:38 -08002089 if (net_dev->netdev_ops == &efx_netdev_ops &&
2090 event == NETDEV_CHANGENAME)
2091 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002092
2093 return NOTIFY_DONE;
2094}
2095
2096static struct notifier_block efx_netdev_notifier = {
2097 .notifier_call = efx_netdev_event,
2098};
2099
Ben Hutchings06d5e192008-12-12 21:47:23 -08002100static ssize_t
2101show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2102{
2103 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2104 return sprintf(buf, "%d\n", efx->phy_type);
2105}
2106static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
2107
Ben Hutchings8ceee662008-04-27 12:55:59 +01002108static int efx_register_netdev(struct efx_nic *efx)
2109{
2110 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002111 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002112 int rc;
2113
2114 net_dev->watchdog_timeo = 5 * HZ;
2115 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002116 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002117 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002118 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002119
Ben Hutchings7dde5962008-12-12 22:09:38 -08002120 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002121
Ben Hutchings7153f622012-07-27 20:50:52 +01002122 /* Enable resets to be scheduled and check whether any were
2123 * already requested. If so, the NIC is probably hosed so we
2124 * abort.
2125 */
2126 efx->state = STATE_READY;
2127 smp_mb(); /* ensure we change state before checking reset_pending */
2128 if (efx->reset_pending) {
2129 netif_err(efx, probe, efx->net_dev,
2130 "aborting probe due to scheduled reset\n");
2131 rc = -EIO;
2132 goto fail_locked;
2133 }
2134
Ben Hutchingsaed06282009-08-26 08:16:27 +00002135 rc = dev_alloc_name(net_dev, net_dev->name);
2136 if (rc < 0)
2137 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002138 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002139
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002140 /* Always start with carrier off; PHY events will detect the link */
2141 netif_carrier_off(net_dev);
2142
Ben Hutchingsaed06282009-08-26 08:16:27 +00002143 rc = register_netdevice(net_dev);
2144 if (rc)
2145 goto fail_locked;
2146
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002147 efx_for_each_channel(channel, efx) {
2148 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002149 efx_for_each_channel_tx_queue(tx_queue, channel)
2150 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002151 }
2152
Ben Hutchings7dde5962008-12-12 22:09:38 -08002153 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002154
Ben Hutchings06d5e192008-12-12 21:47:23 -08002155 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2156 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002157 netif_err(efx, drv, efx->net_dev,
2158 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002159 goto fail_registered;
2160 }
2161
Ben Hutchings8ceee662008-04-27 12:55:59 +01002162 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002163
Ben Hutchings7153f622012-07-27 20:50:52 +01002164fail_registered:
2165 rtnl_lock();
2166 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002167fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002168 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002169 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002170 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002171 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002172}
2173
2174static void efx_unregister_netdev(struct efx_nic *efx)
2175{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002176 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002177 struct efx_tx_queue *tx_queue;
2178
2179 if (!efx->net_dev)
2180 return;
2181
Ben Hutchings767e4682008-09-01 12:43:14 +01002182 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002183
2184 /* Free up any skbs still remaining. This has to happen before
2185 * we try to unregister the netdev as running their destructors
2186 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002187 efx_for_each_channel(channel, efx) {
2188 efx_for_each_channel_tx_queue(tx_queue, channel)
2189 efx_release_tx_buffers(tx_queue);
2190 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002191
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002192 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2193 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002194
2195 rtnl_lock();
2196 unregister_netdevice(efx->net_dev);
2197 efx->state = STATE_UNINIT;
2198 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002199}
2200
2201/**************************************************************************
2202 *
2203 * Device reset and suspend
2204 *
2205 **************************************************************************/
2206
Ben Hutchings2467ca42008-09-01 12:48:50 +01002207/* Tears down the entire software state and most of the hardware state
2208 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002209void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002210{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002211 EFX_ASSERT_RESET_SERIALISED(efx);
2212
Ben Hutchings2467ca42008-09-01 12:48:50 +01002213 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00002214 efx_stop_interrupts(efx, false);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002215
2216 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002217 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2218 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002219 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002220}
2221
Ben Hutchings2467ca42008-09-01 12:48:50 +01002222/* This function will always ensure that the locks acquired in
2223 * efx_reset_down() are released. A failure return code indicates
2224 * that we were unable to reinitialise the hardware, and the
2225 * driver should be disabled. If ok is false, then the rx and tx
2226 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002227int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002228{
2229 int rc;
2230
Ben Hutchings2467ca42008-09-01 12:48:50 +01002231 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002232
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002233 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002234 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002235 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002236 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002237 }
2238
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002239 if (!ok)
2240 goto fail;
2241
Steve Hodgson4b988282009-01-29 17:50:51 +00002242 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002243 rc = efx->phy_op->init(efx);
2244 if (rc)
2245 goto fail;
2246 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002247 netif_err(efx, drv, efx->net_dev,
2248 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002249 }
2250
Ben Hutchings710b2082011-09-03 00:15:00 +01002251 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002252
Ben Hutchings7f967c02012-02-13 23:45:02 +00002253 efx_start_interrupts(efx, false);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002254 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002255 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002256
2257 mutex_unlock(&efx->mac_lock);
2258
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002259 efx_start_all(efx);
2260
2261 return 0;
2262
2263fail:
2264 efx->port_initialized = false;
2265
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002266 mutex_unlock(&efx->mac_lock);
2267
Ben Hutchings8ceee662008-04-27 12:55:59 +01002268 return rc;
2269}
2270
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002271/* Reset the NIC using the specified method. Note that the reset may
2272 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002273 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002274 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002275 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002276int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002277{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002278 int rc, rc2;
2279 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002280
Ben Hutchings62776d02010-06-23 11:30:07 +00002281 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2282 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002283
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002284 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002285 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002286
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002287 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002288 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002289 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002290 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002291 }
2292
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002293 /* Clear flags for the scopes we covered. We assume the NIC and
2294 * driver are now quiescent so that there is no race here.
2295 */
2296 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002297
2298 /* Reinitialise bus-mastering, which may have been turned off before
2299 * the reset was scheduled. This is still appropriate, even in the
2300 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2301 * can respond to requests. */
2302 pci_set_master(efx->pci_dev);
2303
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002304out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002305 /* Leave device stopped if necessary */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002306 disabled = rc || method == RESET_TYPE_DISABLE;
2307 rc2 = efx_reset_up(efx, method, !disabled);
2308 if (rc2) {
2309 disabled = true;
2310 if (!rc)
2311 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002312 }
2313
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002314 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002315 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002316 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002317 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002318 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002319 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002320 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002321 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002322 return rc;
2323}
2324
2325/* The worker thread exists so that code that cannot sleep can
2326 * schedule a reset for later.
2327 */
2328static void efx_reset_work(struct work_struct *data)
2329{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002330 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002331 unsigned long pending = ACCESS_ONCE(efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002332
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002333 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002334 return;
2335
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002336 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002337
2338 /* We checked the state in efx_schedule_reset() but it may
2339 * have changed by now. Now that we have the RTNL lock,
2340 * it cannot change again.
2341 */
2342 if (efx->state == STATE_READY)
2343 (void)efx_reset(efx, fls(pending) - 1);
2344
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002345 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002346}
2347
2348void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2349{
2350 enum reset_type method;
2351
Ben Hutchings8ceee662008-04-27 12:55:59 +01002352 switch (type) {
2353 case RESET_TYPE_INVISIBLE:
2354 case RESET_TYPE_ALL:
2355 case RESET_TYPE_WORLD:
2356 case RESET_TYPE_DISABLE:
2357 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002358 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2359 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002360 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002361 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002362 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002363 netif_dbg(efx, drv, efx->net_dev,
2364 "scheduling %s reset for %s\n",
2365 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002366 break;
2367 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002368
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002369 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002370 smp_mb(); /* ensure we change reset_pending before checking state */
2371
2372 /* If we're not READY then just leave the flags set as the cue
2373 * to abort probing or reschedule the reset later.
2374 */
2375 if (ACCESS_ONCE(efx->state) != STATE_READY)
2376 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002377
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002378 /* efx_process_channel() will no longer read events once a
2379 * reset is scheduled. So switch back to poll'd MCDI completions. */
2380 efx_mcdi_mode_poll(efx);
2381
Steve Hodgson1ab00622008-12-12 21:33:02 -08002382 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002383}
2384
2385/**************************************************************************
2386 *
2387 * List of NICs we support
2388 *
2389 **************************************************************************/
2390
2391/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002392static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002393 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2394 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002395 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002396 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2397 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002398 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002399 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002400 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002401 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002402 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002403 {0} /* end of list */
2404};
2405
2406/**************************************************************************
2407 *
Ben Hutchings37594332009-11-23 16:05:45 +00002408 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002409 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002410 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002411 * Needed so all function pointers are valid and do not have to be tested
2412 * before use
2413 *
2414 **************************************************************************/
2415int efx_port_dummy_op_int(struct efx_nic *efx)
2416{
2417 return 0;
2418}
2419void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002420
2421static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002422{
2423 return false;
2424}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002425
stephen hemminger6c8c2512011-04-14 05:50:12 +00002426static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002427 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002428 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002429 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002430 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002431};
2432
Ben Hutchings8ceee662008-04-27 12:55:59 +01002433/**************************************************************************
2434 *
2435 * Data housekeeping
2436 *
2437 **************************************************************************/
2438
2439/* This zeroes out and then fills in the invariants in a struct
2440 * efx_nic (including all sub-structures).
2441 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002442static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002443 struct pci_dev *pci_dev, struct net_device *net_dev)
2444{
Ben Hutchings46426102010-09-10 06:42:33 +00002445 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002446
2447 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002448 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002449#ifdef CONFIG_SFC_MTD
2450 INIT_LIST_HEAD(&efx->mtd_list);
2451#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002452 INIT_WORK(&efx->reset_work, efx_reset_work);
2453 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002454 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002455 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002456 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002457 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002458 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002459
2460 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002461 spin_lock_init(&efx->stats_lock);
2462 mutex_init(&efx->mac_lock);
2463 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002464 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002465 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002466 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002467
2468 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002469 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2470 if (!efx->channel[i])
2471 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002472 }
2473
Ben Hutchings8ceee662008-04-27 12:55:59 +01002474 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2475
2476 /* Higher numbered interrupt modes are less capable! */
2477 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2478 interrupt_mode);
2479
Ben Hutchings6977dc62008-12-26 13:44:39 -08002480 /* Would be good to use the net_dev name, but we're too early */
2481 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2482 pci_name(pci_dev));
2483 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002484 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002485 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002486
Ben Hutchings8ceee662008-04-27 12:55:59 +01002487 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002488
2489fail:
2490 efx_fini_struct(efx);
2491 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002492}
2493
2494static void efx_fini_struct(struct efx_nic *efx)
2495{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002496 int i;
2497
2498 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2499 kfree(efx->channel[i]);
2500
Ben Hutchings8ceee662008-04-27 12:55:59 +01002501 if (efx->workqueue) {
2502 destroy_workqueue(efx->workqueue);
2503 efx->workqueue = NULL;
2504 }
2505}
2506
2507/**************************************************************************
2508 *
2509 * PCI interface
2510 *
2511 **************************************************************************/
2512
2513/* Main body of final NIC shutdown code
2514 * This is called only at module unload (or hotplug removal).
2515 */
2516static void efx_pci_remove_main(struct efx_nic *efx)
2517{
Ben Hutchings7153f622012-07-27 20:50:52 +01002518 /* Flush reset_work. It can no longer be scheduled since we
2519 * are not READY.
2520 */
2521 BUG_ON(efx->state == STATE_READY);
2522 cancel_work_sync(&efx->reset_work);
2523
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002524#ifdef CONFIG_RFS_ACCEL
2525 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2526 efx->net_dev->rx_cpu_rmap = NULL;
2527#endif
Ben Hutchings7f967c02012-02-13 23:45:02 +00002528 efx_stop_interrupts(efx, false);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002529 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002530 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002531 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002532 efx_fini_napi(efx);
2533 efx_remove_all(efx);
2534}
2535
2536/* Final NIC shutdown
2537 * This is called only at module unload (or hotplug removal).
2538 */
2539static void efx_pci_remove(struct pci_dev *pci_dev)
2540{
2541 struct efx_nic *efx;
2542
2543 efx = pci_get_drvdata(pci_dev);
2544 if (!efx)
2545 return;
2546
2547 /* Mark the NIC as fini, then stop the interface */
2548 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002549 dev_close(efx->net_dev);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002550 efx_stop_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002551 rtnl_unlock();
2552
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002553 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002554 efx_unregister_netdev(efx);
2555
Ben Hutchings7dde5962008-12-12 22:09:38 -08002556 efx_mtd_remove(efx);
2557
Ben Hutchings8ceee662008-04-27 12:55:59 +01002558 efx_pci_remove_main(efx);
2559
Ben Hutchings8ceee662008-04-27 12:55:59 +01002560 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002561 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002562
Ben Hutchings8ceee662008-04-27 12:55:59 +01002563 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002564 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002565 free_netdev(efx->net_dev);
2566};
2567
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002568/* NIC VPD information
2569 * Called during probe to display the part number of the
2570 * installed NIC. VPD is potentially very large but this should
2571 * always appear within the first 512 bytes.
2572 */
2573#define SFC_VPD_LEN 512
2574static void efx_print_product_vpd(struct efx_nic *efx)
2575{
2576 struct pci_dev *dev = efx->pci_dev;
2577 char vpd_data[SFC_VPD_LEN];
2578 ssize_t vpd_size;
2579 int i, j;
2580
2581 /* Get the vpd data from the device */
2582 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2583 if (vpd_size <= 0) {
2584 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2585 return;
2586 }
2587
2588 /* Get the Read only section */
2589 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2590 if (i < 0) {
2591 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2592 return;
2593 }
2594
2595 j = pci_vpd_lrdt_size(&vpd_data[i]);
2596 i += PCI_VPD_LRDT_TAG_SIZE;
2597 if (i + j > vpd_size)
2598 j = vpd_size - i;
2599
2600 /* Get the Part number */
2601 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2602 if (i < 0) {
2603 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2604 return;
2605 }
2606
2607 j = pci_vpd_info_field_size(&vpd_data[i]);
2608 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2609 if (i + j > vpd_size) {
2610 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2611 return;
2612 }
2613
2614 netif_info(efx, drv, efx->net_dev,
2615 "Part Number : %.*s\n", j, &vpd_data[i]);
2616}
2617
2618
Ben Hutchings8ceee662008-04-27 12:55:59 +01002619/* Main body of NIC initialisation
2620 * This is called at module load (or hotplug insertion, theoretically).
2621 */
2622static int efx_pci_probe_main(struct efx_nic *efx)
2623{
2624 int rc;
2625
2626 /* Do start-of-day initialisation */
2627 rc = efx_probe_all(efx);
2628 if (rc)
2629 goto fail1;
2630
Ben Hutchingse8f14992010-12-07 19:47:34 +00002631 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002632
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002633 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002634 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002635 netif_err(efx, probe, efx->net_dev,
2636 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002637 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002638 }
2639
2640 rc = efx_init_port(efx);
2641 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002642 netif_err(efx, probe, efx->net_dev,
2643 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002644 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002645 }
2646
Ben Hutchings152b6a62009-11-29 03:43:56 +00002647 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002648 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002649 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00002650 efx_start_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002651
2652 return 0;
2653
Ben Hutchings278c0622009-11-23 16:05:12 +00002654 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002655 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002656 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002657 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002658 fail3:
2659 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002660 efx_remove_all(efx);
2661 fail1:
2662 return rc;
2663}
2664
2665/* NIC initialisation
2666 *
2667 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002668 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002669 * sets up and registers the network devices with the kernel and hooks
2670 * the interrupt service routine. It does not prepare the device for
2671 * transmission; this is left to the first time one of the network
2672 * interfaces is brought up (i.e. efx_net_open).
2673 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002674static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002675 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002676{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002677 struct net_device *net_dev;
2678 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002679 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002680
2681 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002682 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2683 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002684 if (!net_dev)
2685 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002686 efx = netdev_priv(net_dev);
2687 efx->type = (const struct efx_nic_type *) entry->driver_data;
2688 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002689 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002690 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002691 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002692 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002693 /* Mask for features that also apply to VLAN devices */
2694 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002695 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2696 NETIF_F_RXCSUM);
2697 /* All offloads can be toggled */
2698 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002699 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002700 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002701 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002702 if (rc)
2703 goto fail1;
2704
Ben Hutchings62776d02010-06-23 11:30:07 +00002705 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002706 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002707
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002708 efx_print_product_vpd(efx);
2709
Ben Hutchings8ceee662008-04-27 12:55:59 +01002710 /* Set up basic I/O (BAR mappings etc) */
2711 rc = efx_init_io(efx);
2712 if (rc)
2713 goto fail2;
2714
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002715 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002716 if (rc)
2717 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002718
Ben Hutchings8ceee662008-04-27 12:55:59 +01002719 rc = efx_register_netdev(efx);
2720 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002721 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002722
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002723 rc = efx_sriov_init(efx);
2724 if (rc)
2725 netif_err(efx, probe, efx->net_dev,
2726 "SR-IOV can't be enabled rc %d\n", rc);
2727
Ben Hutchings62776d02010-06-23 11:30:07 +00002728 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002729
Ben Hutchings7c431612012-01-27 17:23:58 +00002730 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002731 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002732 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002733 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002734 if (rc)
2735 netif_warn(efx, probe, efx->net_dev,
2736 "failed to create MTDs (%d)\n", rc);
2737
Ben Hutchings8ceee662008-04-27 12:55:59 +01002738 return 0;
2739
Ben Hutchings8ceee662008-04-27 12:55:59 +01002740 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002741 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002742 fail3:
2743 efx_fini_io(efx);
2744 fail2:
2745 efx_fini_struct(efx);
2746 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002747 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002748 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002749 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002750 free_netdev(net_dev);
2751 return rc;
2752}
2753
Ben Hutchings89c758f2009-11-29 03:43:07 +00002754static int efx_pm_freeze(struct device *dev)
2755{
2756 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2757
Ben Hutchings61da0262012-07-27 19:35:39 +01002758 rtnl_lock();
2759
Ben Hutchings6032fb52012-07-27 19:35:47 +01002760 if (efx->state != STATE_DISABLED) {
2761 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002762
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002763 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002764
Ben Hutchings6032fb52012-07-27 19:35:47 +01002765 efx_stop_all(efx);
2766 efx_stop_interrupts(efx, false);
2767 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002768
Ben Hutchings61da0262012-07-27 19:35:39 +01002769 rtnl_unlock();
2770
Ben Hutchings89c758f2009-11-29 03:43:07 +00002771 return 0;
2772}
2773
2774static int efx_pm_thaw(struct device *dev)
2775{
2776 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2777
Ben Hutchings61da0262012-07-27 19:35:39 +01002778 rtnl_lock();
2779
Ben Hutchings6032fb52012-07-27 19:35:47 +01002780 if (efx->state != STATE_DISABLED) {
2781 efx_start_interrupts(efx, false);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002782
Ben Hutchings6032fb52012-07-27 19:35:47 +01002783 mutex_lock(&efx->mac_lock);
2784 efx->phy_op->reconfigure(efx);
2785 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002786
Ben Hutchings6032fb52012-07-27 19:35:47 +01002787 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002788
Ben Hutchings6032fb52012-07-27 19:35:47 +01002789 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002790
Ben Hutchings6032fb52012-07-27 19:35:47 +01002791 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002792
Ben Hutchings6032fb52012-07-27 19:35:47 +01002793 efx->type->resume_wol(efx);
2794 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002795
Ben Hutchings61da0262012-07-27 19:35:39 +01002796 rtnl_unlock();
2797
Steve Hodgson319ba642010-06-01 11:17:24 +00002798 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2799 queue_work(reset_workqueue, &efx->reset_work);
2800
Ben Hutchings89c758f2009-11-29 03:43:07 +00002801 return 0;
2802}
2803
2804static int efx_pm_poweroff(struct device *dev)
2805{
2806 struct pci_dev *pci_dev = to_pci_dev(dev);
2807 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2808
2809 efx->type->fini(efx);
2810
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002811 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002812
2813 pci_save_state(pci_dev);
2814 return pci_set_power_state(pci_dev, PCI_D3hot);
2815}
2816
2817/* Used for both resume and restore */
2818static int efx_pm_resume(struct device *dev)
2819{
2820 struct pci_dev *pci_dev = to_pci_dev(dev);
2821 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2822 int rc;
2823
2824 rc = pci_set_power_state(pci_dev, PCI_D0);
2825 if (rc)
2826 return rc;
2827 pci_restore_state(pci_dev);
2828 rc = pci_enable_device(pci_dev);
2829 if (rc)
2830 return rc;
2831 pci_set_master(efx->pci_dev);
2832 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2833 if (rc)
2834 return rc;
2835 rc = efx->type->init(efx);
2836 if (rc)
2837 return rc;
2838 efx_pm_thaw(dev);
2839 return 0;
2840}
2841
2842static int efx_pm_suspend(struct device *dev)
2843{
2844 int rc;
2845
2846 efx_pm_freeze(dev);
2847 rc = efx_pm_poweroff(dev);
2848 if (rc)
2849 efx_pm_resume(dev);
2850 return rc;
2851}
2852
Ben Hutchings18e83e42012-01-05 19:05:20 +00002853static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002854 .suspend = efx_pm_suspend,
2855 .resume = efx_pm_resume,
2856 .freeze = efx_pm_freeze,
2857 .thaw = efx_pm_thaw,
2858 .poweroff = efx_pm_poweroff,
2859 .restore = efx_pm_resume,
2860};
2861
Ben Hutchings8ceee662008-04-27 12:55:59 +01002862static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00002863 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002864 .id_table = efx_pci_table,
2865 .probe = efx_pci_probe,
2866 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00002867 .driver.pm = &efx_pm_ops,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002868};
2869
2870/**************************************************************************
2871 *
2872 * Kernel module interface
2873 *
2874 *************************************************************************/
2875
2876module_param(interrupt_mode, uint, 0444);
2877MODULE_PARM_DESC(interrupt_mode,
2878 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2879
2880static int __init efx_init_module(void)
2881{
2882 int rc;
2883
2884 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2885
2886 rc = register_netdevice_notifier(&efx_netdev_notifier);
2887 if (rc)
2888 goto err_notifier;
2889
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002890 rc = efx_init_sriov();
2891 if (rc)
2892 goto err_sriov;
2893
Steve Hodgson1ab00622008-12-12 21:33:02 -08002894 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2895 if (!reset_workqueue) {
2896 rc = -ENOMEM;
2897 goto err_reset;
2898 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002899
2900 rc = pci_register_driver(&efx_pci_driver);
2901 if (rc < 0)
2902 goto err_pci;
2903
2904 return 0;
2905
2906 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08002907 destroy_workqueue(reset_workqueue);
2908 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002909 efx_fini_sriov();
2910 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002911 unregister_netdevice_notifier(&efx_netdev_notifier);
2912 err_notifier:
2913 return rc;
2914}
2915
2916static void __exit efx_exit_module(void)
2917{
2918 printk(KERN_INFO "Solarflare NET driver unloading\n");
2919
2920 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002921 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002922 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002923 unregister_netdevice_notifier(&efx_netdev_notifier);
2924
2925}
2926
2927module_init(efx_init_module);
2928module_exit(efx_exit_module);
2929
Ben Hutchings906bb262009-11-29 15:16:19 +00002930MODULE_AUTHOR("Solarflare Communications and "
2931 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002932MODULE_DESCRIPTION("Solarflare Communications network driver");
2933MODULE_LICENSE("GPL");
2934MODULE_DEVICE_TABLE(pci, efx_pci_table);