blob: 0bc00991d31010cf1ec4efd95ec95afba8aa5d53 [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 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000250 if (rx_queue->enabled) {
251 efx_rx_strategy(channel);
252 efx_fast_push_rx_descriptors(rx_queue);
253 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100254 }
255
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000256 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100257}
258
259/* Mark channel as finished processing
260 *
261 * Note that since we will not receive further interrupts for this
262 * channel before we finish processing and call the eventq_read_ack()
263 * method, there is no need to use the interrupt hold-off timers.
264 */
265static inline void efx_channel_processed(struct efx_channel *channel)
266{
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100267 /* The interrupt handler for this channel may set work_pending
268 * as soon as we acknowledge the events we've seen. Make sure
269 * it's cleared before then. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100270 channel->work_pending = false;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100271 smp_wmb();
272
Ben Hutchings152b6a62009-11-29 03:43:56 +0000273 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100274}
275
276/* NAPI poll handler
277 *
278 * NAPI guarantees serialisation of polls of the same device, which
279 * provides the guarantee required by efx_process_channel().
280 */
281static int efx_poll(struct napi_struct *napi, int budget)
282{
283 struct efx_channel *channel =
284 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000285 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000286 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100287
Ben Hutchings62776d02010-06-23 11:30:07 +0000288 netif_vdbg(efx, intr, efx->net_dev,
289 "channel %d NAPI poll executing on CPU %d\n",
290 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100291
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000292 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100293
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000294 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000295 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000296 efx->irq_rx_adaptive &&
297 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000298 if (unlikely(channel->irq_mod_score <
299 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000300 if (channel->irq_moderation > 1) {
301 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000302 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000303 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000304 } else if (unlikely(channel->irq_mod_score >
305 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000306 if (channel->irq_moderation <
307 efx->irq_rx_moderation) {
308 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000309 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000310 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000311 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000312 channel->irq_count = 0;
313 channel->irq_mod_score = 0;
314 }
315
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000316 efx_filter_rfs_expire(channel);
317
Ben Hutchings8ceee662008-04-27 12:55:59 +0100318 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800319 * only wait for napi_complete(), this isn't a problem
Ben Hutchings8ceee662008-04-27 12:55:59 +0100320 * since efx_channel_processed() will have no effect if
321 * interrupts have already been disabled.
322 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800323 napi_complete(napi);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100324 efx_channel_processed(channel);
325 }
326
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000327 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100328}
329
330/* Process the eventq of the specified channel immediately on this CPU
331 *
332 * Disable hardware generated interrupts, wait for any existing
333 * processing to finish, then directly poll (and ack ) the eventq.
334 * Finally reenable NAPI and interrupts.
335 *
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100336 * This is for use only during a loopback self-test. It must not
337 * deliver any packets up the stack as this can result in deadlock.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100338 */
339void efx_process_channel_now(struct efx_channel *channel)
340{
341 struct efx_nic *efx = channel->efx;
342
Ben Hutchings8313aca2010-09-10 06:41:57 +0000343 BUG_ON(channel->channel >= efx->n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100344 BUG_ON(!channel->enabled);
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100345 BUG_ON(!efx->loopback_selftest);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100346
347 /* Disable interrupts and wait for ISRs to complete */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000348 efx_nic_disable_interrupts(efx);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000349 if (efx->legacy_irq) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100350 synchronize_irq(efx->legacy_irq);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000351 efx->legacy_irq_enabled = false;
352 }
Ben Hutchings64ee3122008-09-01 12:47:38 +0100353 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100354 synchronize_irq(channel->irq);
355
356 /* Wait for any NAPI processing to complete */
357 napi_disable(&channel->napi_str);
358
359 /* Poll the channel */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000360 efx_process_channel(channel, channel->eventq_mask + 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100361
362 /* Ack the eventq. This may cause an interrupt to be generated
363 * when they are reenabled */
364 efx_channel_processed(channel);
365
366 napi_enable(&channel->napi_str);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000367 if (efx->legacy_irq)
368 efx->legacy_irq_enabled = true;
Ben Hutchings152b6a62009-11-29 03:43:56 +0000369 efx_nic_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100370}
371
372/* Create event queue
373 * Event queue memory allocations are done only once. If the channel
374 * is reset, the memory buffer will be reused; this guards against
375 * errors during channel reset and also simplifies interrupt handling.
376 */
377static int efx_probe_eventq(struct efx_channel *channel)
378{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000379 struct efx_nic *efx = channel->efx;
380 unsigned long entries;
381
Ben Hutchings86ee5302012-01-09 19:51:22 +0000382 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000383 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100384
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000385 /* Build an event queue with room for one event per tx and rx buffer,
386 * plus some extra for link state events and MCDI completions. */
387 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
388 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
389 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
390
Ben Hutchings152b6a62009-11-29 03:43:56 +0000391 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100392}
393
394/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100395static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100396{
Ben Hutchings62776d02010-06-23 11:30:07 +0000397 netif_dbg(channel->efx, drv, channel->efx->net_dev,
398 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100399
400 channel->eventq_read_ptr = 0;
401
Ben Hutchings152b6a62009-11-29 03:43:56 +0000402 efx_nic_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100403}
404
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000405/* Enable event queue processing and NAPI */
406static void efx_start_eventq(struct efx_channel *channel)
407{
408 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
409 "chan %d start event queue\n", channel->channel);
410
411 /* The interrupt handler for this channel may set work_pending
412 * as soon as we enable it. Make sure it's cleared before
413 * then. Similarly, make sure it sees the enabled flag set.
414 */
415 channel->work_pending = false;
416 channel->enabled = true;
417 smp_wmb();
418
419 napi_enable(&channel->napi_str);
420 efx_nic_eventq_read_ack(channel);
421}
422
423/* Disable event queue processing and NAPI */
424static void efx_stop_eventq(struct efx_channel *channel)
425{
426 if (!channel->enabled)
427 return;
428
429 napi_disable(&channel->napi_str);
430 channel->enabled = false;
431}
432
Ben Hutchings8ceee662008-04-27 12:55:59 +0100433static void efx_fini_eventq(struct efx_channel *channel)
434{
Ben Hutchings62776d02010-06-23 11:30:07 +0000435 netif_dbg(channel->efx, drv, channel->efx->net_dev,
436 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100437
Ben Hutchings152b6a62009-11-29 03:43:56 +0000438 efx_nic_fini_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100439}
440
441static void efx_remove_eventq(struct efx_channel *channel)
442{
Ben Hutchings62776d02010-06-23 11:30:07 +0000443 netif_dbg(channel->efx, drv, channel->efx->net_dev,
444 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100445
Ben Hutchings152b6a62009-11-29 03:43:56 +0000446 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100447}
448
449/**************************************************************************
450 *
451 * Channel handling
452 *
453 *************************************************************************/
454
Ben Hutchings7f967c02012-02-13 23:45:02 +0000455/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000456static struct efx_channel *
457efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
458{
459 struct efx_channel *channel;
460 struct efx_rx_queue *rx_queue;
461 struct efx_tx_queue *tx_queue;
462 int j;
463
Ben Hutchings7f967c02012-02-13 23:45:02 +0000464 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
465 if (!channel)
466 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000467
Ben Hutchings7f967c02012-02-13 23:45:02 +0000468 channel->efx = efx;
469 channel->channel = i;
470 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000471
Ben Hutchings7f967c02012-02-13 23:45:02 +0000472 for (j = 0; j < EFX_TXQ_TYPES; j++) {
473 tx_queue = &channel->tx_queue[j];
474 tx_queue->efx = efx;
475 tx_queue->queue = i * EFX_TXQ_TYPES + j;
476 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000477 }
478
Ben Hutchings46426102010-09-10 06:42:33 +0000479 rx_queue = &channel->rx_queue;
480 rx_queue->efx = efx;
481 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
482 (unsigned long)rx_queue);
483
484 return channel;
485}
486
Ben Hutchings7f967c02012-02-13 23:45:02 +0000487/* Allocate and initialise a channel structure, copying parameters
488 * (but not resources) from an old channel structure.
489 */
490static struct efx_channel *
491efx_copy_channel(const struct efx_channel *old_channel)
492{
493 struct efx_channel *channel;
494 struct efx_rx_queue *rx_queue;
495 struct efx_tx_queue *tx_queue;
496 int j;
497
498 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
499 if (!channel)
500 return NULL;
501
502 *channel = *old_channel;
503
504 channel->napi_dev = NULL;
505 memset(&channel->eventq, 0, sizeof(channel->eventq));
506
507 for (j = 0; j < EFX_TXQ_TYPES; j++) {
508 tx_queue = &channel->tx_queue[j];
509 if (tx_queue->channel)
510 tx_queue->channel = channel;
511 tx_queue->buffer = NULL;
512 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
513 }
514
515 rx_queue = &channel->rx_queue;
516 rx_queue->buffer = NULL;
517 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
518 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
519 (unsigned long)rx_queue);
520
521 return channel;
522}
523
Ben Hutchings8ceee662008-04-27 12:55:59 +0100524static int efx_probe_channel(struct efx_channel *channel)
525{
526 struct efx_tx_queue *tx_queue;
527 struct efx_rx_queue *rx_queue;
528 int rc;
529
Ben Hutchings62776d02010-06-23 11:30:07 +0000530 netif_dbg(channel->efx, probe, channel->efx->net_dev,
531 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100532
Ben Hutchings7f967c02012-02-13 23:45:02 +0000533 rc = channel->type->pre_probe(channel);
534 if (rc)
535 goto fail;
536
Ben Hutchings8ceee662008-04-27 12:55:59 +0100537 rc = efx_probe_eventq(channel);
538 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000539 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100540
541 efx_for_each_channel_tx_queue(tx_queue, channel) {
542 rc = efx_probe_tx_queue(tx_queue);
543 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000544 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100545 }
546
547 efx_for_each_channel_rx_queue(rx_queue, channel) {
548 rc = efx_probe_rx_queue(rx_queue);
549 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000550 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100551 }
552
553 channel->n_rx_frm_trunc = 0;
554
555 return 0;
556
Ben Hutchings7f967c02012-02-13 23:45:02 +0000557fail:
558 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100559 return rc;
560}
561
Ben Hutchings7f967c02012-02-13 23:45:02 +0000562static void
563efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
564{
565 struct efx_nic *efx = channel->efx;
566 const char *type;
567 int number;
568
569 number = channel->channel;
570 if (efx->tx_channel_offset == 0) {
571 type = "";
572 } else if (channel->channel < efx->tx_channel_offset) {
573 type = "-rx";
574 } else {
575 type = "-tx";
576 number -= efx->tx_channel_offset;
577 }
578 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
579}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100580
Ben Hutchings56536e92008-12-12 21:37:02 -0800581static void efx_set_channel_names(struct efx_nic *efx)
582{
583 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800584
Ben Hutchings7f967c02012-02-13 23:45:02 +0000585 efx_for_each_channel(channel, efx)
586 channel->type->get_name(channel,
587 efx->channel_name[channel->channel],
588 sizeof(efx->channel_name[0]));
Ben Hutchings56536e92008-12-12 21:37:02 -0800589}
590
Ben Hutchings46426102010-09-10 06:42:33 +0000591static int efx_probe_channels(struct efx_nic *efx)
592{
593 struct efx_channel *channel;
594 int rc;
595
596 /* Restart special buffer allocation */
597 efx->next_buffer_table = 0;
598
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000599 /* Probe channels in reverse, so that any 'extra' channels
600 * use the start of the buffer table. This allows the traffic
601 * channels to be resized without moving them or wasting the
602 * entries before them.
603 */
604 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000605 rc = efx_probe_channel(channel);
606 if (rc) {
607 netif_err(efx, probe, efx->net_dev,
608 "failed to create channel %d\n",
609 channel->channel);
610 goto fail;
611 }
612 }
613 efx_set_channel_names(efx);
614
615 return 0;
616
617fail:
618 efx_remove_channels(efx);
619 return rc;
620}
621
Ben Hutchings8ceee662008-04-27 12:55:59 +0100622/* Channels are shutdown and reinitialised whilst the NIC is running
623 * to propagate configuration changes (mtu, checksum offload), or
624 * to clear hardware error conditions
625 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000626static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100627{
628 struct efx_tx_queue *tx_queue;
629 struct efx_rx_queue *rx_queue;
630 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100631
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100632 /* Calculate the rx buffer allocation parameters required to
633 * support the current MTU, including padding for header
634 * alignment and overruns.
635 */
636 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
637 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
Ben Hutchings39c9cf02010-06-23 11:31:28 +0000638 efx->type->rx_buffer_hash_size +
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100639 efx->type->rx_buffer_padding);
Steve Hodgson62b330b2010-06-01 11:20:53 +0000640 efx->rx_buffer_order = get_order(efx->rx_buffer_len +
641 sizeof(struct efx_rx_page_state));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100642
Ben Hutchings14bf7182012-05-22 01:27:58 +0100643 /* We must keep at least one descriptor in a TX ring empty.
644 * We could avoid this when the queue size does not exactly
645 * match the hardware ring size, but it's not that important.
646 * Therefore we stop the queue when one more skb might fill
647 * the ring completely. We wake it when half way back to
648 * empty.
649 */
650 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
651 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
652
Ben Hutchings8ceee662008-04-27 12:55:59 +0100653 /* Initialise the channels */
654 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100655 efx_for_each_channel_tx_queue(tx_queue, channel)
656 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100657
658 /* The rx buffer allocation strategy is MTU dependent */
659 efx_rx_strategy(channel);
660
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000661 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100662 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000663 efx_nic_generate_fill_event(rx_queue);
664 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100665
666 WARN_ON(channel->rx_pkt != NULL);
667 efx_rx_strategy(channel);
668 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000669
670 if (netif_device_present(efx->net_dev))
671 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100672}
673
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000674static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100675{
676 struct efx_channel *channel;
677 struct efx_tx_queue *tx_queue;
678 struct efx_rx_queue *rx_queue;
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100679 struct pci_dev *dev = efx->pci_dev;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100680 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100681
682 EFX_ASSERT_RESET_SERIALISED(efx);
683 BUG_ON(efx->port_enabled);
684
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100685 /* Only perform flush if dma is enabled */
686 if (dev->is_busmaster) {
687 rc = efx_nic_flush_queues(efx);
688
689 if (rc && EFX_WORKAROUND_7803(efx)) {
690 /* Schedule a reset to recover from the flush failure. The
691 * descriptor caches reference memory we're about to free,
692 * but falcon_reconfigure_mac_wrapper() won't reconnect
693 * the MACs because of the pending reset. */
694 netif_err(efx, drv, efx->net_dev,
695 "Resetting to recover from flush failure\n");
696 efx_schedule_reset(efx, RESET_TYPE_ALL);
697 } else if (rc) {
698 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
699 } else {
700 netif_dbg(efx, drv, efx->net_dev,
701 "successfully flushed all queues\n");
702 }
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000703 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100704
Ben Hutchings8ceee662008-04-27 12:55:59 +0100705 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000706 /* RX packet processing is pipelined, so wait for the
707 * NAPI handler to complete. At least event queue 0
708 * might be kept active by non-data events, so don't
709 * use napi_synchronize() but actually disable NAPI
710 * temporarily.
711 */
712 if (efx_channel_has_rx_queue(channel)) {
713 efx_stop_eventq(channel);
714 efx_start_eventq(channel);
715 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100716
717 efx_for_each_channel_rx_queue(rx_queue, channel)
718 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000719 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100720 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100721 }
722}
723
724static void efx_remove_channel(struct efx_channel *channel)
725{
726 struct efx_tx_queue *tx_queue;
727 struct efx_rx_queue *rx_queue;
728
Ben Hutchings62776d02010-06-23 11:30:07 +0000729 netif_dbg(channel->efx, drv, channel->efx->net_dev,
730 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100731
732 efx_for_each_channel_rx_queue(rx_queue, channel)
733 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000734 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100735 efx_remove_tx_queue(tx_queue);
736 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100737 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100738}
739
Ben Hutchings46426102010-09-10 06:42:33 +0000740static void efx_remove_channels(struct efx_nic *efx)
741{
742 struct efx_channel *channel;
743
744 efx_for_each_channel(channel, efx)
745 efx_remove_channel(channel);
746}
747
748int
749efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
750{
751 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
752 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000753 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100754 int rc;
755
756 rc = efx_check_disabled(efx);
757 if (rc)
758 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000759
760 /* Not all channels should be reallocated. We must avoid
761 * reallocating their buffer table entries.
762 */
763 efx_for_each_channel(channel, efx) {
764 struct efx_rx_queue *rx_queue;
765 struct efx_tx_queue *tx_queue;
766
767 if (channel->type->copy)
768 continue;
769 next_buffer_table = max(next_buffer_table,
770 channel->eventq.index +
771 channel->eventq.entries);
772 efx_for_each_channel_rx_queue(rx_queue, channel)
773 next_buffer_table = max(next_buffer_table,
774 rx_queue->rxd.index +
775 rx_queue->rxd.entries);
776 efx_for_each_channel_tx_queue(tx_queue, channel)
777 next_buffer_table = max(next_buffer_table,
778 tx_queue->txd.index +
779 tx_queue->txd.entries);
780 }
Ben Hutchings46426102010-09-10 06:42:33 +0000781
Ben Hutchings29c69a42013-01-28 19:01:06 +0000782 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000783 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000784 efx_stop_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000785
Ben Hutchings7f967c02012-02-13 23:45:02 +0000786 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000787 memset(other_channel, 0, sizeof(other_channel));
788 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000789 channel = efx->channel[i];
790 if (channel->type->copy)
791 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000792 if (!channel) {
793 rc = -ENOMEM;
794 goto out;
795 }
796 other_channel[i] = channel;
797 }
798
799 /* Swap entry counts and channel pointers */
800 old_rxq_entries = efx->rxq_entries;
801 old_txq_entries = efx->txq_entries;
802 efx->rxq_entries = rxq_entries;
803 efx->txq_entries = txq_entries;
804 for (i = 0; i < efx->n_channels; i++) {
805 channel = efx->channel[i];
806 efx->channel[i] = other_channel[i];
807 other_channel[i] = channel;
808 }
809
Ben Hutchings7f967c02012-02-13 23:45:02 +0000810 /* Restart buffer table allocation */
811 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000812
Ben Hutchingse8f14992010-12-07 19:47:34 +0000813 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000814 channel = efx->channel[i];
815 if (!channel->type->copy)
816 continue;
817 rc = efx_probe_channel(channel);
818 if (rc)
819 goto rollback;
820 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000821 }
Ben Hutchings46426102010-09-10 06:42:33 +0000822
Ben Hutchings7f967c02012-02-13 23:45:02 +0000823out:
824 /* Destroy unused channel structures */
825 for (i = 0; i < efx->n_channels; i++) {
826 channel = other_channel[i];
827 if (channel && channel->type->copy) {
828 efx_fini_napi_channel(channel);
829 efx_remove_channel(channel);
830 kfree(channel);
831 }
832 }
833
834 efx_start_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000835 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000836 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000837 return rc;
838
839rollback:
840 /* Swap back */
841 efx->rxq_entries = old_rxq_entries;
842 efx->txq_entries = old_txq_entries;
843 for (i = 0; i < efx->n_channels; i++) {
844 channel = efx->channel[i];
845 efx->channel[i] = other_channel[i];
846 other_channel[i] = channel;
847 }
848 goto out;
849}
850
Steve Hodgson90d683a2010-06-01 11:19:39 +0000851void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100852{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000853 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100854}
855
Ben Hutchings7f967c02012-02-13 23:45:02 +0000856static const struct efx_channel_type efx_default_channel_type = {
857 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100858 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000859 .get_name = efx_get_channel_name,
860 .copy = efx_copy_channel,
861 .keep_eventq = false,
862};
863
864int efx_channel_dummy_op_int(struct efx_channel *channel)
865{
866 return 0;
867}
868
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100869void efx_channel_dummy_op_void(struct efx_channel *channel)
870{
871}
872
Ben Hutchings8ceee662008-04-27 12:55:59 +0100873/**************************************************************************
874 *
875 * Port handling
876 *
877 **************************************************************************/
878
879/* This ensures that the kernel is kept informed (via
880 * netif_carrier_on/off) of the link status, and also maintains the
881 * link status's stop on the port's TX queue.
882 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000883void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100884{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000885 struct efx_link_state *link_state = &efx->link_state;
886
Ben Hutchings8ceee662008-04-27 12:55:59 +0100887 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
888 * that no events are triggered between unregister_netdev() and the
889 * driver unloading. A more general condition is that NETDEV_CHANGE
890 * can only be generated between NETDEV_UP and NETDEV_DOWN */
891 if (!netif_running(efx->net_dev))
892 return;
893
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000894 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100895 efx->n_link_state_changes++;
896
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000897 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100898 netif_carrier_on(efx->net_dev);
899 else
900 netif_carrier_off(efx->net_dev);
901 }
902
903 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000904 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000905 netif_info(efx, link, efx->net_dev,
906 "link up at %uMbps %s-duplex (MTU %d)%s\n",
907 link_state->speed, link_state->fd ? "full" : "half",
908 efx->net_dev->mtu,
909 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000910 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000911 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100912}
913
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000914void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
915{
916 efx->link_advertising = advertising;
917 if (advertising) {
918 if (advertising & ADVERTISED_Pause)
919 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
920 else
921 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
922 if (advertising & ADVERTISED_Asym_Pause)
923 efx->wanted_fc ^= EFX_FC_TX;
924 }
925}
926
David S. Millerb56269462011-05-17 17:53:22 -0400927void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000928{
929 efx->wanted_fc = wanted_fc;
930 if (efx->link_advertising) {
931 if (wanted_fc & EFX_FC_RX)
932 efx->link_advertising |= (ADVERTISED_Pause |
933 ADVERTISED_Asym_Pause);
934 else
935 efx->link_advertising &= ~(ADVERTISED_Pause |
936 ADVERTISED_Asym_Pause);
937 if (wanted_fc & EFX_FC_TX)
938 efx->link_advertising ^= ADVERTISED_Asym_Pause;
939 }
940}
941
Ben Hutchings115122a2009-03-04 09:52:52 +0000942static void efx_fini_port(struct efx_nic *efx);
943
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000944/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
945 * the MAC appropriately. All other PHY configuration changes are pushed
946 * through phy_op->set_settings(), and pushed asynchronously to the MAC
947 * through efx_monitor().
948 *
949 * Callers must hold the mac_lock
950 */
951int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100952{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000953 enum efx_phy_mode phy_mode;
954 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100955
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000956 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100957
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000958 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000959 netif_addr_lock_bh(efx->net_dev);
960 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100961
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000962 /* Disable PHY transmit in mac level loopbacks */
963 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800964 if (LOOPBACK_INTERNAL(efx))
965 efx->phy_mode |= PHY_MODE_TX_DISABLED;
966 else
967 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800968
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000969 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800970
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000971 if (rc)
972 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100973
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000974 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100975}
976
977/* Reinitialise the MAC to pick up new PHY settings, even if the port is
978 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000979int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100980{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000981 int rc;
982
Ben Hutchings8ceee662008-04-27 12:55:59 +0100983 EFX_ASSERT_RESET_SERIALISED(efx);
984
985 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000986 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100987 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000988
989 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100990}
991
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000992/* Asynchronous work item for changing MAC promiscuity and multicast
993 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
994 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800995static void efx_mac_work(struct work_struct *data)
996{
997 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
998
999 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +01001000 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +01001001 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001002 mutex_unlock(&efx->mac_lock);
1003}
1004
Ben Hutchings8ceee662008-04-27 12:55:59 +01001005static int efx_probe_port(struct efx_nic *efx)
1006{
1007 int rc;
1008
Ben Hutchings62776d02010-06-23 11:30:07 +00001009 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001010
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001011 if (phy_flash_cfg)
1012 efx->phy_mode = PHY_MODE_SPECIAL;
1013
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001014 /* Connect up MAC/PHY operations table */
1015 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001016 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001017 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001018
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001019 /* Initialise MAC address to permanent address */
1020 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001021
1022 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001023}
1024
1025static int efx_init_port(struct efx_nic *efx)
1026{
1027 int rc;
1028
Ben Hutchings62776d02010-06-23 11:30:07 +00001029 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001030
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001031 mutex_lock(&efx->mac_lock);
1032
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001033 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001034 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001035 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001036
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001037 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001038
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001039 /* Reconfigure the MAC before creating dma queues (required for
1040 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001041 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001042
1043 /* Ensure the PHY advertises the correct flow control settings */
1044 rc = efx->phy_op->reconfigure(efx);
1045 if (rc)
1046 goto fail2;
1047
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001048 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001049 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001050
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001051fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001052 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001053fail1:
1054 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001055 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001056}
1057
Ben Hutchings8ceee662008-04-27 12:55:59 +01001058static void efx_start_port(struct efx_nic *efx)
1059{
Ben Hutchings62776d02010-06-23 11:30:07 +00001060 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001061 BUG_ON(efx->port_enabled);
1062
1063 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001064 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001065
1066 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1067 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001068 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001069
Ben Hutchings8ceee662008-04-27 12:55:59 +01001070 mutex_unlock(&efx->mac_lock);
1071}
1072
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001073/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001074static void efx_stop_port(struct efx_nic *efx)
1075{
Ben Hutchings62776d02010-06-23 11:30:07 +00001076 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001077
1078 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001079 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001080 mutex_unlock(&efx->mac_lock);
1081
1082 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001083 netif_addr_lock_bh(efx->net_dev);
1084 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001085}
1086
1087static void efx_fini_port(struct efx_nic *efx)
1088{
Ben Hutchings62776d02010-06-23 11:30:07 +00001089 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001090
1091 if (!efx->port_initialized)
1092 return;
1093
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001094 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001095 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001096
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001097 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001098 efx_link_status_changed(efx);
1099}
1100
1101static void efx_remove_port(struct efx_nic *efx)
1102{
Ben Hutchings62776d02010-06-23 11:30:07 +00001103 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001104
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001105 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001106}
1107
1108/**************************************************************************
1109 *
1110 * NIC handling
1111 *
1112 **************************************************************************/
1113
1114/* This configures the PCI device to enable I/O and DMA. */
1115static int efx_init_io(struct efx_nic *efx)
1116{
1117 struct pci_dev *pci_dev = efx->pci_dev;
1118 dma_addr_t dma_mask = efx->type->max_dma_mask;
1119 int rc;
1120
Ben Hutchings62776d02010-06-23 11:30:07 +00001121 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001122
1123 rc = pci_enable_device(pci_dev);
1124 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001125 netif_err(efx, probe, efx->net_dev,
1126 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001127 goto fail1;
1128 }
1129
1130 pci_set_master(pci_dev);
1131
1132 /* Set the PCI DMA mask. Try all possibilities from our
1133 * genuine mask down to 32 bits, because some architectures
1134 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1135 * masks event though they reject 46 bit masks.
1136 */
1137 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001138 if (dma_supported(&pci_dev->dev, dma_mask)) {
1139 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001140 if (rc == 0)
1141 break;
1142 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001143 dma_mask >>= 1;
1144 }
1145 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001146 netif_err(efx, probe, efx->net_dev,
1147 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001148 goto fail2;
1149 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001150 netif_dbg(efx, probe, efx->net_dev,
1151 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001152 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001153 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001154 /* dma_set_coherent_mask() is not *allowed* to
1155 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001156 * but just in case...
1157 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001158 netif_err(efx, probe, efx->net_dev,
1159 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001160 goto fail2;
1161 }
1162
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001163 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1164 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001165 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001166 netif_err(efx, probe, efx->net_dev,
1167 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001168 rc = -EIO;
1169 goto fail3;
1170 }
David S. Miller8decf862011-09-22 03:23:13 -04001171 efx->membase = ioremap_nocache(efx->membase_phys,
1172 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001173 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001174 netif_err(efx, probe, efx->net_dev,
1175 "could not map memory BAR at %llx+%x\n",
1176 (unsigned long long)efx->membase_phys,
1177 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001178 rc = -ENOMEM;
1179 goto fail4;
1180 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001181 netif_dbg(efx, probe, efx->net_dev,
1182 "memory BAR at %llx+%x (virtual %p)\n",
1183 (unsigned long long)efx->membase_phys,
1184 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001185
1186 return 0;
1187
1188 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001189 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001190 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001191 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001192 fail2:
1193 pci_disable_device(efx->pci_dev);
1194 fail1:
1195 return rc;
1196}
1197
1198static void efx_fini_io(struct efx_nic *efx)
1199{
Ben Hutchings62776d02010-06-23 11:30:07 +00001200 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001201
1202 if (efx->membase) {
1203 iounmap(efx->membase);
1204 efx->membase = NULL;
1205 }
1206
1207 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001208 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001209 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001210 }
1211
1212 pci_disable_device(efx->pci_dev);
1213}
1214
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001215static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001216{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001217 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001218 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001219 int cpu;
1220
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001221 if (rss_cpus) {
1222 count = rss_cpus;
1223 } else {
1224 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1225 netif_warn(efx, probe, efx->net_dev,
1226 "RSS disabled due to allocation failure\n");
1227 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001228 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001229
1230 count = 0;
1231 for_each_online_cpu(cpu) {
1232 if (!cpumask_test_cpu(cpu, thread_mask)) {
1233 ++count;
1234 cpumask_or(thread_mask, thread_mask,
1235 topology_thread_cpumask(cpu));
1236 }
1237 }
1238
1239 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001240 }
1241
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001242 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1243 * table entries that are inaccessible to VFs
1244 */
1245 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1246 count > efx_vf_size(efx)) {
1247 netif_warn(efx, probe, efx->net_dev,
1248 "Reducing number of RSS channels from %u to %u for "
1249 "VF support. Increase vf-msix-limit to use more "
1250 "channels on the PF.\n",
1251 count, efx_vf_size(efx));
1252 count = efx_vf_size(efx);
1253 }
1254
Ben Hutchings46123d02008-09-01 12:47:33 +01001255 return count;
1256}
1257
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001258static int
1259efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1260{
1261#ifdef CONFIG_RFS_ACCEL
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001262 unsigned int i;
1263 int rc;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001264
1265 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1266 if (!efx->net_dev->rx_cpu_rmap)
1267 return -ENOMEM;
1268 for (i = 0; i < efx->n_rx_channels; i++) {
1269 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1270 xentries[i].vector);
1271 if (rc) {
1272 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1273 efx->net_dev->rx_cpu_rmap = NULL;
1274 return rc;
1275 }
1276 }
1277#endif
1278 return 0;
1279}
1280
Ben Hutchings46123d02008-09-01 12:47:33 +01001281/* Probe the number and type of interrupts we are able to obtain, and
1282 * the resulting numbers of channels and RX queues.
1283 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001284static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001285{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001286 unsigned int max_channels =
1287 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001288 unsigned int extra_channels = 0;
1289 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001290 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001291
Ben Hutchings7f967c02012-02-13 23:45:02 +00001292 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1293 if (efx->extra_channel_type[i])
1294 ++extra_channels;
1295
Ben Hutchings8ceee662008-04-27 12:55:59 +01001296 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001297 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001298 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001299
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001300 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001301 if (separate_tx_channels)
1302 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001303 n_channels += extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001304 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001305
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001306 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001307 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001308 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001309 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001310 netif_err(efx, drv, efx->net_dev,
1311 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001312 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001313 netif_err(efx, drv, efx->net_dev,
1314 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001315 EFX_BUG_ON_PARANOID(rc >= n_channels);
1316 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001317 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001318 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001319 }
1320
1321 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001322 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001323 if (n_channels > extra_channels)
1324 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001325 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001326 efx->n_tx_channels = max(n_channels / 2, 1U);
1327 efx->n_rx_channels = max(n_channels -
1328 efx->n_tx_channels,
1329 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001330 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001331 efx->n_tx_channels = n_channels;
1332 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001333 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001334 rc = efx_init_rx_cpu_rmap(efx, xentries);
1335 if (rc) {
1336 pci_disable_msix(efx->pci_dev);
1337 return rc;
1338 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001339 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001340 efx_get_channel(efx, i)->irq =
1341 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001342 } else {
1343 /* Fall back to single channel MSI */
1344 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001345 netif_err(efx, drv, efx->net_dev,
1346 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001347 }
1348 }
1349
1350 /* Try single interrupt MSI */
1351 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001352 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001353 efx->n_rx_channels = 1;
1354 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001355 rc = pci_enable_msi(efx->pci_dev);
1356 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001357 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001358 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001359 netif_err(efx, drv, efx->net_dev,
1360 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001361 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1362 }
1363 }
1364
1365 /* Assume legacy interrupts */
1366 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001367 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001368 efx->n_rx_channels = 1;
1369 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001370 efx->legacy_irq = efx->pci_dev->irq;
1371 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001372
Ben Hutchings7f967c02012-02-13 23:45:02 +00001373 /* Assign extra channels if possible */
1374 j = efx->n_channels;
1375 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1376 if (!efx->extra_channel_type[i])
1377 continue;
1378 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1379 efx->n_channels <= extra_channels) {
1380 efx->extra_channel_type[i]->handle_no_channel(efx);
1381 } else {
1382 --j;
1383 efx_get_channel(efx, j)->type =
1384 efx->extra_channel_type[i];
1385 }
1386 }
1387
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001388 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001389 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001390 efx->n_rx_channels : efx_vf_size(efx));
1391
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001392 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001393}
1394
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001395/* Enable interrupts, then probe and start the event queues */
Ben Hutchings7f967c02012-02-13 23:45:02 +00001396static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001397{
1398 struct efx_channel *channel;
1399
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001400 BUG_ON(efx->state == STATE_DISABLED);
1401
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001402 if (efx->legacy_irq)
1403 efx->legacy_irq_enabled = true;
1404 efx_nic_enable_interrupts(efx);
1405
1406 efx_for_each_channel(channel, efx) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001407 if (!channel->type->keep_eventq || !may_keep_eventq)
1408 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001409 efx_start_eventq(channel);
1410 }
1411
1412 efx_mcdi_mode_event(efx);
1413}
1414
Ben Hutchings7f967c02012-02-13 23:45:02 +00001415static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001416{
1417 struct efx_channel *channel;
1418
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001419 if (efx->state == STATE_DISABLED)
1420 return;
1421
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001422 efx_mcdi_mode_poll(efx);
1423
1424 efx_nic_disable_interrupts(efx);
1425 if (efx->legacy_irq) {
1426 synchronize_irq(efx->legacy_irq);
1427 efx->legacy_irq_enabled = false;
1428 }
1429
1430 efx_for_each_channel(channel, efx) {
1431 if (channel->irq)
1432 synchronize_irq(channel->irq);
1433
1434 efx_stop_eventq(channel);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001435 if (!channel->type->keep_eventq || !may_keep_eventq)
1436 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001437 }
1438}
1439
Ben Hutchings8ceee662008-04-27 12:55:59 +01001440static void efx_remove_interrupts(struct efx_nic *efx)
1441{
1442 struct efx_channel *channel;
1443
1444 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001445 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001446 channel->irq = 0;
1447 pci_disable_msi(efx->pci_dev);
1448 pci_disable_msix(efx->pci_dev);
1449
1450 /* Remove legacy interrupt */
1451 efx->legacy_irq = 0;
1452}
1453
Ben Hutchings8831da72008-09-01 12:47:48 +01001454static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001455{
Ben Hutchings602a5322011-05-16 17:32:39 +01001456 struct efx_channel *channel;
1457 struct efx_tx_queue *tx_queue;
1458
Ben Hutchings97653432011-01-12 18:26:56 +00001459 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001460 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001461
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001462 /* We need to mark which channels really have RX and TX
1463 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001464 * RX-only and TX-only channels.
1465 */
1466 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001467 if (channel->channel < efx->n_rx_channels)
1468 channel->rx_queue.core_index = channel->channel;
1469 else
1470 channel->rx_queue.core_index = -1;
1471
Ben Hutchings602a5322011-05-16 17:32:39 +01001472 efx_for_each_channel_tx_queue(tx_queue, channel)
1473 tx_queue->queue -= (efx->tx_channel_offset *
1474 EFX_TXQ_TYPES);
1475 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001476}
1477
1478static int efx_probe_nic(struct efx_nic *efx)
1479{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001480 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001481 int rc;
1482
Ben Hutchings62776d02010-06-23 11:30:07 +00001483 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001484
1485 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001486 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001487 if (rc)
1488 return rc;
1489
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001490 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001491 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001492 rc = efx_probe_interrupts(efx);
1493 if (rc)
1494 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001495
Ben Hutchings28e47c42012-02-15 01:58:49 +00001496 efx->type->dimension_resources(efx);
1497
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001498 if (efx->n_channels > 1)
1499 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001500 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001501 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001502 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001503
Ben Hutchings8831da72008-09-01 12:47:48 +01001504 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001505 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1506 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001507
1508 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001509 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1510 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001511
1512 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001513
1514fail:
1515 efx->type->remove(efx);
1516 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001517}
1518
1519static void efx_remove_nic(struct efx_nic *efx)
1520{
Ben Hutchings62776d02010-06-23 11:30:07 +00001521 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001522
1523 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001524 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001525}
1526
1527/**************************************************************************
1528 *
1529 * NIC startup/shutdown
1530 *
1531 *************************************************************************/
1532
1533static int efx_probe_all(struct efx_nic *efx)
1534{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001535 int rc;
1536
Ben Hutchings8ceee662008-04-27 12:55:59 +01001537 rc = efx_probe_nic(efx);
1538 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001539 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001540 goto fail1;
1541 }
1542
Ben Hutchings8ceee662008-04-27 12:55:59 +01001543 rc = efx_probe_port(efx);
1544 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001545 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001546 goto fail2;
1547 }
1548
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001549 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1550 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1551 rc = -EINVAL;
1552 goto fail3;
1553 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001554 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001555
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001556 rc = efx_probe_filters(efx);
1557 if (rc) {
1558 netif_err(efx, probe, efx->net_dev,
1559 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001560 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001561 }
1562
Ben Hutchings7f967c02012-02-13 23:45:02 +00001563 rc = efx_probe_channels(efx);
1564 if (rc)
1565 goto fail4;
1566
Ben Hutchings8ceee662008-04-27 12:55:59 +01001567 return 0;
1568
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001569 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001570 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001571 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001572 efx_remove_port(efx);
1573 fail2:
1574 efx_remove_nic(efx);
1575 fail1:
1576 return rc;
1577}
1578
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001579/* If the interface is supposed to be running but is not, start
1580 * the hardware and software data path, regular activity for the port
1581 * (MAC statistics, link polling, etc.) and schedule the port to be
1582 * reconfigured. Interrupts must already be enabled. This function
1583 * is safe to call multiple times, so long as the NIC is not disabled.
1584 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001585 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001586static void efx_start_all(struct efx_nic *efx)
1587{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001588 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001589 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001590
1591 /* Check that it is appropriate to restart the interface. All
1592 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001593 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001594 return;
1595
Ben Hutchings8ceee662008-04-27 12:55:59 +01001596 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001597 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001598
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001599 /* Start the hardware monitor if there is one. Otherwise (we're link
1600 * event driven), we have to poll the PHY because after an event queue
1601 * flush, we could have a missed a link state change */
1602 if (efx->type->monitor != NULL) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001603 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1604 efx_monitor_interval);
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001605 } else {
1606 mutex_lock(&efx->mac_lock);
1607 if (efx->phy_op->poll(efx))
1608 efx_link_status_changed(efx);
1609 mutex_unlock(&efx->mac_lock);
1610 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001611
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001612 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001613}
1614
1615/* Flush all delayed work. Should only be called when no more delayed work
1616 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1617 * since we're holding the rtnl_lock at this point. */
1618static void efx_flush_all(struct efx_nic *efx)
1619{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001620 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001621 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001622 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001623 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001624 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001625}
1626
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001627/* Quiesce the hardware and software data path, and regular activity
1628 * for the port without bringing the link down. Safe to call multiple
1629 * times with the NIC in almost any state, but interrupts should be
1630 * enabled. Requires the RTNL lock.
1631 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001632static void efx_stop_all(struct efx_nic *efx)
1633{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001634 EFX_ASSERT_RESET_SERIALISED(efx);
1635
1636 /* port_enabled can be read safely under the rtnl lock */
1637 if (!efx->port_enabled)
1638 return;
1639
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001640 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001641 efx_stop_port(efx);
1642
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001643 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001644 efx_flush_all(efx);
1645
Ben Hutchings29c69a42013-01-28 19:01:06 +00001646 /* Stop the kernel transmit interface. This is only valid if
1647 * the device is stopped or detached; otherwise the watchdog
1648 * may fire immediately.
1649 */
1650 WARN_ON(netif_running(efx->net_dev) &&
1651 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001652 netif_tx_disable(efx->net_dev);
1653
1654 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001655}
1656
1657static void efx_remove_all(struct efx_nic *efx)
1658{
Ben Hutchings46426102010-09-10 06:42:33 +00001659 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001660 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001661 efx_remove_port(efx);
1662 efx_remove_nic(efx);
1663}
1664
Ben Hutchings8ceee662008-04-27 12:55:59 +01001665/**************************************************************************
1666 *
1667 * Interrupt moderation
1668 *
1669 **************************************************************************/
1670
Ben Hutchingscc180b62011-12-08 19:51:47 +00001671static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001672{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001673 if (usecs == 0)
1674 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001675 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001676 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001677 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001678}
1679
Ben Hutchings8ceee662008-04-27 12:55:59 +01001680/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001681int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1682 unsigned int rx_usecs, bool rx_adaptive,
1683 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001684{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001685 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001686 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1687 efx->timer_quantum_ns,
1688 1000);
1689 unsigned int tx_ticks;
1690 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001691
1692 EFX_ASSERT_RESET_SERIALISED(efx);
1693
Ben Hutchingscc180b62011-12-08 19:51:47 +00001694 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001695 return -EINVAL;
1696
Ben Hutchingscc180b62011-12-08 19:51:47 +00001697 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1698 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1699
Ben Hutchings9e393b32011-09-05 07:43:04 +00001700 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1701 !rx_may_override_tx) {
1702 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1703 "RX and TX IRQ moderation must be equal\n");
1704 return -EINVAL;
1705 }
1706
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001707 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001708 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001709 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001710 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001711 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001712 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001713 channel->irq_moderation = tx_ticks;
1714 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001715
1716 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001717}
1718
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001719void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1720 unsigned int *rx_usecs, bool *rx_adaptive)
1721{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001722 /* We must round up when converting ticks to microseconds
1723 * because we round down when converting the other way.
1724 */
1725
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001726 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001727 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1728 efx->timer_quantum_ns,
1729 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001730
1731 /* If channels are shared between RX and TX, so is IRQ
1732 * moderation. Otherwise, IRQ moderation is the same for all
1733 * TX channels and is not adaptive.
1734 */
1735 if (efx->tx_channel_offset == 0)
1736 *tx_usecs = *rx_usecs;
1737 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001738 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001739 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001740 efx->timer_quantum_ns,
1741 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001742}
1743
Ben Hutchings8ceee662008-04-27 12:55:59 +01001744/**************************************************************************
1745 *
1746 * Hardware monitor
1747 *
1748 **************************************************************************/
1749
Ben Hutchingse254c272010-09-20 08:44:10 +00001750/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001751static void efx_monitor(struct work_struct *data)
1752{
1753 struct efx_nic *efx = container_of(data, struct efx_nic,
1754 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001755
Ben Hutchings62776d02010-06-23 11:30:07 +00001756 netif_vdbg(efx, timer, efx->net_dev,
1757 "hardware monitor executing on CPU %d\n",
1758 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001759 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001760
Ben Hutchings8ceee662008-04-27 12:55:59 +01001761 /* If the mac_lock is already held then it is likely a port
1762 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001763 * most of the work of monitor() anyway. */
1764 if (mutex_trylock(&efx->mac_lock)) {
1765 if (efx->port_enabled)
1766 efx->type->monitor(efx);
1767 mutex_unlock(&efx->mac_lock);
1768 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001769
Ben Hutchings8ceee662008-04-27 12:55:59 +01001770 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1771 efx_monitor_interval);
1772}
1773
1774/**************************************************************************
1775 *
1776 * ioctls
1777 *
1778 *************************************************************************/
1779
1780/* Net device ioctl
1781 * Context: process, rtnl_lock() held.
1782 */
1783static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1784{
Ben Hutchings767e4682008-09-01 12:43:14 +01001785 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001786 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001787
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001788 if (cmd == SIOCSHWTSTAMP)
1789 return efx_ptp_ioctl(efx, ifr, cmd);
1790
Ben Hutchings68e7f452009-04-29 08:05:08 +00001791 /* Convert phy_id from older PRTAD/DEVAD format */
1792 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1793 (data->phy_id & 0xfc00) == 0x0400)
1794 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1795
1796 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001797}
1798
1799/**************************************************************************
1800 *
1801 * NAPI interface
1802 *
1803 **************************************************************************/
1804
Ben Hutchings7f967c02012-02-13 23:45:02 +00001805static void efx_init_napi_channel(struct efx_channel *channel)
1806{
1807 struct efx_nic *efx = channel->efx;
1808
1809 channel->napi_dev = efx->net_dev;
1810 netif_napi_add(channel->napi_dev, &channel->napi_str,
1811 efx_poll, napi_weight);
1812}
1813
Ben Hutchingse8f14992010-12-07 19:47:34 +00001814static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001815{
1816 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001817
Ben Hutchings7f967c02012-02-13 23:45:02 +00001818 efx_for_each_channel(channel, efx)
1819 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001820}
1821
1822static void efx_fini_napi_channel(struct efx_channel *channel)
1823{
1824 if (channel->napi_dev)
1825 netif_napi_del(&channel->napi_str);
1826 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001827}
1828
1829static void efx_fini_napi(struct efx_nic *efx)
1830{
1831 struct efx_channel *channel;
1832
Ben Hutchingse8f14992010-12-07 19:47:34 +00001833 efx_for_each_channel(channel, efx)
1834 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001835}
1836
1837/**************************************************************************
1838 *
1839 * Kernel netpoll interface
1840 *
1841 *************************************************************************/
1842
1843#ifdef CONFIG_NET_POLL_CONTROLLER
1844
1845/* Although in the common case interrupts will be disabled, this is not
1846 * guaranteed. However, all our work happens inside the NAPI callback,
1847 * so no locking is required.
1848 */
1849static void efx_netpoll(struct net_device *net_dev)
1850{
Ben Hutchings767e4682008-09-01 12:43:14 +01001851 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001852 struct efx_channel *channel;
1853
Ben Hutchings64ee3122008-09-01 12:47:38 +01001854 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001855 efx_schedule_channel(channel);
1856}
1857
1858#endif
1859
1860/**************************************************************************
1861 *
1862 * Kernel net device interface
1863 *
1864 *************************************************************************/
1865
1866/* Context: process, rtnl_lock() held. */
1867static int efx_net_open(struct net_device *net_dev)
1868{
Ben Hutchings767e4682008-09-01 12:43:14 +01001869 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001870 int rc;
1871
Ben Hutchings62776d02010-06-23 11:30:07 +00001872 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1873 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001874
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001875 rc = efx_check_disabled(efx);
1876 if (rc)
1877 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001878 if (efx->phy_mode & PHY_MODE_SPECIAL)
1879 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001880 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1881 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001882
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001883 /* Notify the kernel of the link state polled during driver load,
1884 * before the monitor starts running */
1885 efx_link_status_changed(efx);
1886
Ben Hutchings8ceee662008-04-27 12:55:59 +01001887 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001888 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001889 return 0;
1890}
1891
1892/* Context: process, rtnl_lock() held.
1893 * Note that the kernel will ignore our return code; this method
1894 * should really be a void.
1895 */
1896static int efx_net_stop(struct net_device *net_dev)
1897{
Ben Hutchings767e4682008-09-01 12:43:14 +01001898 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001899
Ben Hutchings62776d02010-06-23 11:30:07 +00001900 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1901 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001902
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001903 /* Stop the device and flush all the channels */
1904 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001905
1906 return 0;
1907}
1908
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001909/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001910static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1911 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001912{
Ben Hutchings767e4682008-09-01 12:43:14 +01001913 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001914 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001915
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001916 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001917
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001918 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001919
1920 stats->rx_packets = mac_stats->rx_packets;
1921 stats->tx_packets = mac_stats->tx_packets;
1922 stats->rx_bytes = mac_stats->rx_bytes;
1923 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001924 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001925 stats->multicast = mac_stats->rx_multicast;
1926 stats->collisions = mac_stats->tx_collision;
1927 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1928 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001929 stats->rx_crc_errors = mac_stats->rx_bad;
1930 stats->rx_frame_errors = mac_stats->rx_align_error;
1931 stats->rx_fifo_errors = mac_stats->rx_overflow;
1932 stats->rx_missed_errors = mac_stats->rx_missed;
1933 stats->tx_window_errors = mac_stats->tx_late_collision;
1934
1935 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001936 stats->rx_crc_errors +
1937 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001938 mac_stats->rx_symbol_error);
1939 stats->tx_errors = (stats->tx_window_errors +
1940 mac_stats->tx_bad);
1941
Ben Hutchings1cb34522011-09-02 23:23:00 +01001942 spin_unlock_bh(&efx->stats_lock);
1943
Ben Hutchings8ceee662008-04-27 12:55:59 +01001944 return stats;
1945}
1946
1947/* Context: netif_tx_lock held, BHs disabled. */
1948static void efx_watchdog(struct net_device *net_dev)
1949{
Ben Hutchings767e4682008-09-01 12:43:14 +01001950 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001951
Ben Hutchings62776d02010-06-23 11:30:07 +00001952 netif_err(efx, tx_err, efx->net_dev,
1953 "TX stuck with port_enabled=%d: resetting channels\n",
1954 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001955
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001956 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001957}
1958
1959
1960/* Context: process, rtnl_lock() held. */
1961static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1962{
Ben Hutchings767e4682008-09-01 12:43:14 +01001963 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001964 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001965
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001966 rc = efx_check_disabled(efx);
1967 if (rc)
1968 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001969 if (new_mtu > EFX_MAX_MTU)
1970 return -EINVAL;
1971
Ben Hutchings62776d02010-06-23 11:30:07 +00001972 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001973
Ben Hutchings29c69a42013-01-28 19:01:06 +00001974 efx_device_detach_sync(efx);
1975 efx_stop_all(efx);
1976
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001977 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001978 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001979 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001980 mutex_unlock(&efx->mac_lock);
1981
Ben Hutchings8ceee662008-04-27 12:55:59 +01001982 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00001983 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001984 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001985}
1986
1987static int efx_set_mac_address(struct net_device *net_dev, void *data)
1988{
Ben Hutchings767e4682008-09-01 12:43:14 +01001989 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001990 struct sockaddr *addr = data;
1991 char *new_addr = addr->sa_data;
1992
Ben Hutchings8ceee662008-04-27 12:55:59 +01001993 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001994 netif_err(efx, drv, efx->net_dev,
1995 "invalid ethernet MAC address requested: %pM\n",
1996 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00001997 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001998 }
1999
2000 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002001 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002002
2003 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002004 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002005 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002006 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002007
2008 return 0;
2009}
2010
Ben Hutchingsa816f752008-09-01 12:49:12 +01002011/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002012static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002013{
Ben Hutchings767e4682008-09-01 12:43:14 +01002014 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002015 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002016 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002017 u32 crc;
2018 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002019
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002020 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002021
2022 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002023 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002024 memset(mc_hash, 0xff, sizeof(*mc_hash));
2025 } else {
2026 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00002027 netdev_for_each_mc_addr(ha, net_dev) {
2028 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002029 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
Ben Hutchings32766ec2012-10-04 17:13:03 -07002030 __set_bit_le(bit, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002031 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002032
2033 /* Broadcast packets go through the multicast hash filter.
2034 * ether_crc_le() of the broadcast address is 0xbe2612ff
2035 * so we always add bit 0xff to the mask.
2036 */
Ben Hutchings32766ec2012-10-04 17:13:03 -07002037 __set_bit_le(0xff, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002038 }
2039
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002040 if (efx->port_enabled)
2041 queue_work(efx->workqueue, &efx->mac_work);
2042 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002043}
2044
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002045static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002046{
2047 struct efx_nic *efx = netdev_priv(net_dev);
2048
2049 /* If disabling RX n-tuple filtering, clear existing filters */
2050 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2051 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2052
2053 return 0;
2054}
2055
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002056static const struct net_device_ops efx_netdev_ops = {
2057 .ndo_open = efx_net_open,
2058 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002059 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002060 .ndo_tx_timeout = efx_watchdog,
2061 .ndo_start_xmit = efx_hard_start_xmit,
2062 .ndo_validate_addr = eth_validate_addr,
2063 .ndo_do_ioctl = efx_ioctl,
2064 .ndo_change_mtu = efx_change_mtu,
2065 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002066 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002067 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002068#ifdef CONFIG_SFC_SRIOV
2069 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2070 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2071 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2072 .ndo_get_vf_config = efx_sriov_get_vf_config,
2073#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002074#ifdef CONFIG_NET_POLL_CONTROLLER
2075 .ndo_poll_controller = efx_netpoll,
2076#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002077 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002078#ifdef CONFIG_RFS_ACCEL
2079 .ndo_rx_flow_steer = efx_filter_rfs,
2080#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002081};
2082
Ben Hutchings7dde5962008-12-12 22:09:38 -08002083static void efx_update_name(struct efx_nic *efx)
2084{
2085 strcpy(efx->name, efx->net_dev->name);
2086 efx_mtd_rename(efx);
2087 efx_set_channel_names(efx);
2088}
2089
Ben Hutchings8ceee662008-04-27 12:55:59 +01002090static int efx_netdev_event(struct notifier_block *this,
2091 unsigned long event, void *ptr)
2092{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01002093 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002094
Ben Hutchings7dde5962008-12-12 22:09:38 -08002095 if (net_dev->netdev_ops == &efx_netdev_ops &&
2096 event == NETDEV_CHANGENAME)
2097 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002098
2099 return NOTIFY_DONE;
2100}
2101
2102static struct notifier_block efx_netdev_notifier = {
2103 .notifier_call = efx_netdev_event,
2104};
2105
Ben Hutchings06d5e192008-12-12 21:47:23 -08002106static ssize_t
2107show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2108{
2109 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2110 return sprintf(buf, "%d\n", efx->phy_type);
2111}
2112static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
2113
Ben Hutchings8ceee662008-04-27 12:55:59 +01002114static int efx_register_netdev(struct efx_nic *efx)
2115{
2116 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002117 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002118 int rc;
2119
2120 net_dev->watchdog_timeo = 5 * HZ;
2121 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002122 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002123 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002124 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002125
Ben Hutchings7dde5962008-12-12 22:09:38 -08002126 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002127
Ben Hutchings7153f622012-07-27 20:50:52 +01002128 /* Enable resets to be scheduled and check whether any were
2129 * already requested. If so, the NIC is probably hosed so we
2130 * abort.
2131 */
2132 efx->state = STATE_READY;
2133 smp_mb(); /* ensure we change state before checking reset_pending */
2134 if (efx->reset_pending) {
2135 netif_err(efx, probe, efx->net_dev,
2136 "aborting probe due to scheduled reset\n");
2137 rc = -EIO;
2138 goto fail_locked;
2139 }
2140
Ben Hutchingsaed06282009-08-26 08:16:27 +00002141 rc = dev_alloc_name(net_dev, net_dev->name);
2142 if (rc < 0)
2143 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002144 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002145
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002146 /* Always start with carrier off; PHY events will detect the link */
2147 netif_carrier_off(net_dev);
2148
Ben Hutchingsaed06282009-08-26 08:16:27 +00002149 rc = register_netdevice(net_dev);
2150 if (rc)
2151 goto fail_locked;
2152
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002153 efx_for_each_channel(channel, efx) {
2154 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002155 efx_for_each_channel_tx_queue(tx_queue, channel)
2156 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002157 }
2158
Ben Hutchings7dde5962008-12-12 22:09:38 -08002159 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002160
Ben Hutchings06d5e192008-12-12 21:47:23 -08002161 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2162 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002163 netif_err(efx, drv, efx->net_dev,
2164 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002165 goto fail_registered;
2166 }
2167
Ben Hutchings8ceee662008-04-27 12:55:59 +01002168 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002169
Ben Hutchings7153f622012-07-27 20:50:52 +01002170fail_registered:
2171 rtnl_lock();
2172 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002173fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002174 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002175 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002176 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002177 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002178}
2179
2180static void efx_unregister_netdev(struct efx_nic *efx)
2181{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002182 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002183 struct efx_tx_queue *tx_queue;
2184
2185 if (!efx->net_dev)
2186 return;
2187
Ben Hutchings767e4682008-09-01 12:43:14 +01002188 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002189
2190 /* Free up any skbs still remaining. This has to happen before
2191 * we try to unregister the netdev as running their destructors
2192 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002193 efx_for_each_channel(channel, efx) {
2194 efx_for_each_channel_tx_queue(tx_queue, channel)
2195 efx_release_tx_buffers(tx_queue);
2196 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002197
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002198 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2199 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002200
2201 rtnl_lock();
2202 unregister_netdevice(efx->net_dev);
2203 efx->state = STATE_UNINIT;
2204 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002205}
2206
2207/**************************************************************************
2208 *
2209 * Device reset and suspend
2210 *
2211 **************************************************************************/
2212
Ben Hutchings2467ca42008-09-01 12:48:50 +01002213/* Tears down the entire software state and most of the hardware state
2214 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002215void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002216{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002217 EFX_ASSERT_RESET_SERIALISED(efx);
2218
Ben Hutchings2467ca42008-09-01 12:48:50 +01002219 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00002220 efx_stop_interrupts(efx, false);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002221
2222 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002223 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2224 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002225 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002226}
2227
Ben Hutchings2467ca42008-09-01 12:48:50 +01002228/* This function will always ensure that the locks acquired in
2229 * efx_reset_down() are released. A failure return code indicates
2230 * that we were unable to reinitialise the hardware, and the
2231 * driver should be disabled. If ok is false, then the rx and tx
2232 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002233int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002234{
2235 int rc;
2236
Ben Hutchings2467ca42008-09-01 12:48:50 +01002237 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002238
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002239 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002240 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002241 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002242 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002243 }
2244
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002245 if (!ok)
2246 goto fail;
2247
Steve Hodgson4b988282009-01-29 17:50:51 +00002248 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002249 rc = efx->phy_op->init(efx);
2250 if (rc)
2251 goto fail;
2252 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002253 netif_err(efx, drv, efx->net_dev,
2254 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002255 }
2256
Ben Hutchings710b2082011-09-03 00:15:00 +01002257 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002258
Ben Hutchings7f967c02012-02-13 23:45:02 +00002259 efx_start_interrupts(efx, false);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002260 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002261 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002262
2263 mutex_unlock(&efx->mac_lock);
2264
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002265 efx_start_all(efx);
2266
2267 return 0;
2268
2269fail:
2270 efx->port_initialized = false;
2271
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002272 mutex_unlock(&efx->mac_lock);
2273
Ben Hutchings8ceee662008-04-27 12:55:59 +01002274 return rc;
2275}
2276
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002277/* Reset the NIC using the specified method. Note that the reset may
2278 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002279 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002280 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002281 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002282int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002283{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002284 int rc, rc2;
2285 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002286
Ben Hutchings62776d02010-06-23 11:30:07 +00002287 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2288 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002289
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002290 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002291 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002292
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002293 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002294 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002295 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002296 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002297 }
2298
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002299 /* Clear flags for the scopes we covered. We assume the NIC and
2300 * driver are now quiescent so that there is no race here.
2301 */
2302 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002303
2304 /* Reinitialise bus-mastering, which may have been turned off before
2305 * the reset was scheduled. This is still appropriate, even in the
2306 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2307 * can respond to requests. */
2308 pci_set_master(efx->pci_dev);
2309
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002310out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002311 /* Leave device stopped if necessary */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002312 disabled = rc || method == RESET_TYPE_DISABLE;
2313 rc2 = efx_reset_up(efx, method, !disabled);
2314 if (rc2) {
2315 disabled = true;
2316 if (!rc)
2317 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002318 }
2319
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002320 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002321 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002322 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002323 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002324 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002325 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002326 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002327 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002328 return rc;
2329}
2330
2331/* The worker thread exists so that code that cannot sleep can
2332 * schedule a reset for later.
2333 */
2334static void efx_reset_work(struct work_struct *data)
2335{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002336 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002337 unsigned long pending = ACCESS_ONCE(efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002338
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002339 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002340 return;
2341
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002342 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002343
2344 /* We checked the state in efx_schedule_reset() but it may
2345 * have changed by now. Now that we have the RTNL lock,
2346 * it cannot change again.
2347 */
2348 if (efx->state == STATE_READY)
2349 (void)efx_reset(efx, fls(pending) - 1);
2350
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002351 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002352}
2353
2354void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2355{
2356 enum reset_type method;
2357
Ben Hutchings8ceee662008-04-27 12:55:59 +01002358 switch (type) {
2359 case RESET_TYPE_INVISIBLE:
2360 case RESET_TYPE_ALL:
2361 case RESET_TYPE_WORLD:
2362 case RESET_TYPE_DISABLE:
2363 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002364 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2365 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002366 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002367 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002368 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002369 netif_dbg(efx, drv, efx->net_dev,
2370 "scheduling %s reset for %s\n",
2371 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002372 break;
2373 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002374
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002375 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002376 smp_mb(); /* ensure we change reset_pending before checking state */
2377
2378 /* If we're not READY then just leave the flags set as the cue
2379 * to abort probing or reschedule the reset later.
2380 */
2381 if (ACCESS_ONCE(efx->state) != STATE_READY)
2382 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002383
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002384 /* efx_process_channel() will no longer read events once a
2385 * reset is scheduled. So switch back to poll'd MCDI completions. */
2386 efx_mcdi_mode_poll(efx);
2387
Steve Hodgson1ab00622008-12-12 21:33:02 -08002388 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002389}
2390
2391/**************************************************************************
2392 *
2393 * List of NICs we support
2394 *
2395 **************************************************************************/
2396
2397/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002398static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002399 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2400 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002401 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002402 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2403 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002404 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002405 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002406 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002407 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002408 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002409 {0} /* end of list */
2410};
2411
2412/**************************************************************************
2413 *
Ben Hutchings37594332009-11-23 16:05:45 +00002414 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002415 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002416 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002417 * Needed so all function pointers are valid and do not have to be tested
2418 * before use
2419 *
2420 **************************************************************************/
2421int efx_port_dummy_op_int(struct efx_nic *efx)
2422{
2423 return 0;
2424}
2425void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002426
2427static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002428{
2429 return false;
2430}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002431
stephen hemminger6c8c2512011-04-14 05:50:12 +00002432static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002433 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002434 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002435 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002436 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002437};
2438
Ben Hutchings8ceee662008-04-27 12:55:59 +01002439/**************************************************************************
2440 *
2441 * Data housekeeping
2442 *
2443 **************************************************************************/
2444
2445/* This zeroes out and then fills in the invariants in a struct
2446 * efx_nic (including all sub-structures).
2447 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002448static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002449 struct pci_dev *pci_dev, struct net_device *net_dev)
2450{
Ben Hutchings46426102010-09-10 06:42:33 +00002451 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002452
2453 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002454 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002455#ifdef CONFIG_SFC_MTD
2456 INIT_LIST_HEAD(&efx->mtd_list);
2457#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002458 INIT_WORK(&efx->reset_work, efx_reset_work);
2459 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002460 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002461 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002462 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002463 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002464 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002465
2466 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002467 spin_lock_init(&efx->stats_lock);
2468 mutex_init(&efx->mac_lock);
2469 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002470 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002471 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002472 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002473
2474 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002475 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2476 if (!efx->channel[i])
2477 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002478 }
2479
Ben Hutchings8ceee662008-04-27 12:55:59 +01002480 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2481
2482 /* Higher numbered interrupt modes are less capable! */
2483 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2484 interrupt_mode);
2485
Ben Hutchings6977dc62008-12-26 13:44:39 -08002486 /* Would be good to use the net_dev name, but we're too early */
2487 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2488 pci_name(pci_dev));
2489 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002490 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002491 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002492
Ben Hutchings8ceee662008-04-27 12:55:59 +01002493 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002494
2495fail:
2496 efx_fini_struct(efx);
2497 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002498}
2499
2500static void efx_fini_struct(struct efx_nic *efx)
2501{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002502 int i;
2503
2504 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2505 kfree(efx->channel[i]);
2506
Ben Hutchings8ceee662008-04-27 12:55:59 +01002507 if (efx->workqueue) {
2508 destroy_workqueue(efx->workqueue);
2509 efx->workqueue = NULL;
2510 }
2511}
2512
2513/**************************************************************************
2514 *
2515 * PCI interface
2516 *
2517 **************************************************************************/
2518
2519/* Main body of final NIC shutdown code
2520 * This is called only at module unload (or hotplug removal).
2521 */
2522static void efx_pci_remove_main(struct efx_nic *efx)
2523{
Ben Hutchings7153f622012-07-27 20:50:52 +01002524 /* Flush reset_work. It can no longer be scheduled since we
2525 * are not READY.
2526 */
2527 BUG_ON(efx->state == STATE_READY);
2528 cancel_work_sync(&efx->reset_work);
2529
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002530#ifdef CONFIG_RFS_ACCEL
2531 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2532 efx->net_dev->rx_cpu_rmap = NULL;
2533#endif
Ben Hutchings7f967c02012-02-13 23:45:02 +00002534 efx_stop_interrupts(efx, false);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002535 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002536 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002537 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002538 efx_fini_napi(efx);
2539 efx_remove_all(efx);
2540}
2541
2542/* Final NIC shutdown
2543 * This is called only at module unload (or hotplug removal).
2544 */
2545static void efx_pci_remove(struct pci_dev *pci_dev)
2546{
2547 struct efx_nic *efx;
2548
2549 efx = pci_get_drvdata(pci_dev);
2550 if (!efx)
2551 return;
2552
2553 /* Mark the NIC as fini, then stop the interface */
2554 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002555 dev_close(efx->net_dev);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002556 efx_stop_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002557 rtnl_unlock();
2558
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002559 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002560 efx_unregister_netdev(efx);
2561
Ben Hutchings7dde5962008-12-12 22:09:38 -08002562 efx_mtd_remove(efx);
2563
Ben Hutchings8ceee662008-04-27 12:55:59 +01002564 efx_pci_remove_main(efx);
2565
Ben Hutchings8ceee662008-04-27 12:55:59 +01002566 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002567 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002568
Ben Hutchings8ceee662008-04-27 12:55:59 +01002569 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002570 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002571 free_netdev(efx->net_dev);
2572};
2573
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002574/* NIC VPD information
2575 * Called during probe to display the part number of the
2576 * installed NIC. VPD is potentially very large but this should
2577 * always appear within the first 512 bytes.
2578 */
2579#define SFC_VPD_LEN 512
2580static void efx_print_product_vpd(struct efx_nic *efx)
2581{
2582 struct pci_dev *dev = efx->pci_dev;
2583 char vpd_data[SFC_VPD_LEN];
2584 ssize_t vpd_size;
2585 int i, j;
2586
2587 /* Get the vpd data from the device */
2588 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2589 if (vpd_size <= 0) {
2590 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2591 return;
2592 }
2593
2594 /* Get the Read only section */
2595 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2596 if (i < 0) {
2597 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2598 return;
2599 }
2600
2601 j = pci_vpd_lrdt_size(&vpd_data[i]);
2602 i += PCI_VPD_LRDT_TAG_SIZE;
2603 if (i + j > vpd_size)
2604 j = vpd_size - i;
2605
2606 /* Get the Part number */
2607 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2608 if (i < 0) {
2609 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2610 return;
2611 }
2612
2613 j = pci_vpd_info_field_size(&vpd_data[i]);
2614 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2615 if (i + j > vpd_size) {
2616 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2617 return;
2618 }
2619
2620 netif_info(efx, drv, efx->net_dev,
2621 "Part Number : %.*s\n", j, &vpd_data[i]);
2622}
2623
2624
Ben Hutchings8ceee662008-04-27 12:55:59 +01002625/* Main body of NIC initialisation
2626 * This is called at module load (or hotplug insertion, theoretically).
2627 */
2628static int efx_pci_probe_main(struct efx_nic *efx)
2629{
2630 int rc;
2631
2632 /* Do start-of-day initialisation */
2633 rc = efx_probe_all(efx);
2634 if (rc)
2635 goto fail1;
2636
Ben Hutchingse8f14992010-12-07 19:47:34 +00002637 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002638
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002639 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002640 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002641 netif_err(efx, probe, efx->net_dev,
2642 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002643 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002644 }
2645
2646 rc = efx_init_port(efx);
2647 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002648 netif_err(efx, probe, efx->net_dev,
2649 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002650 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002651 }
2652
Ben Hutchings152b6a62009-11-29 03:43:56 +00002653 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002654 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002655 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00002656 efx_start_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002657
2658 return 0;
2659
Ben Hutchings278c0622009-11-23 16:05:12 +00002660 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002661 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002662 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002663 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002664 fail3:
2665 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002666 efx_remove_all(efx);
2667 fail1:
2668 return rc;
2669}
2670
2671/* NIC initialisation
2672 *
2673 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002674 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002675 * sets up and registers the network devices with the kernel and hooks
2676 * the interrupt service routine. It does not prepare the device for
2677 * transmission; this is left to the first time one of the network
2678 * interfaces is brought up (i.e. efx_net_open).
2679 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002680static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002681 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002682{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002683 struct net_device *net_dev;
2684 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002685 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002686
2687 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002688 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2689 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002690 if (!net_dev)
2691 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002692 efx = netdev_priv(net_dev);
2693 efx->type = (const struct efx_nic_type *) entry->driver_data;
2694 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002695 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002696 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002697 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002698 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002699 /* Mask for features that also apply to VLAN devices */
2700 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002701 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2702 NETIF_F_RXCSUM);
2703 /* All offloads can be toggled */
2704 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002705 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002706 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002707 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002708 if (rc)
2709 goto fail1;
2710
Ben Hutchings62776d02010-06-23 11:30:07 +00002711 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002712 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002713
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002714 efx_print_product_vpd(efx);
2715
Ben Hutchings8ceee662008-04-27 12:55:59 +01002716 /* Set up basic I/O (BAR mappings etc) */
2717 rc = efx_init_io(efx);
2718 if (rc)
2719 goto fail2;
2720
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002721 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002722 if (rc)
2723 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002724
Ben Hutchings8ceee662008-04-27 12:55:59 +01002725 rc = efx_register_netdev(efx);
2726 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002727 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002728
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002729 rc = efx_sriov_init(efx);
2730 if (rc)
2731 netif_err(efx, probe, efx->net_dev,
2732 "SR-IOV can't be enabled rc %d\n", rc);
2733
Ben Hutchings62776d02010-06-23 11:30:07 +00002734 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002735
Ben Hutchings7c431612012-01-27 17:23:58 +00002736 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002737 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002738 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002739 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002740 if (rc)
2741 netif_warn(efx, probe, efx->net_dev,
2742 "failed to create MTDs (%d)\n", rc);
2743
Ben Hutchings8ceee662008-04-27 12:55:59 +01002744 return 0;
2745
Ben Hutchings8ceee662008-04-27 12:55:59 +01002746 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002747 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002748 fail3:
2749 efx_fini_io(efx);
2750 fail2:
2751 efx_fini_struct(efx);
2752 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002753 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002754 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002755 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002756 free_netdev(net_dev);
2757 return rc;
2758}
2759
Ben Hutchings89c758f2009-11-29 03:43:07 +00002760static int efx_pm_freeze(struct device *dev)
2761{
2762 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2763
Ben Hutchings61da0262012-07-27 19:35:39 +01002764 rtnl_lock();
2765
Ben Hutchings6032fb52012-07-27 19:35:47 +01002766 if (efx->state != STATE_DISABLED) {
2767 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002768
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002769 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002770
Ben Hutchings6032fb52012-07-27 19:35:47 +01002771 efx_stop_all(efx);
2772 efx_stop_interrupts(efx, false);
2773 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002774
Ben Hutchings61da0262012-07-27 19:35:39 +01002775 rtnl_unlock();
2776
Ben Hutchings89c758f2009-11-29 03:43:07 +00002777 return 0;
2778}
2779
2780static int efx_pm_thaw(struct device *dev)
2781{
2782 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2783
Ben Hutchings61da0262012-07-27 19:35:39 +01002784 rtnl_lock();
2785
Ben Hutchings6032fb52012-07-27 19:35:47 +01002786 if (efx->state != STATE_DISABLED) {
2787 efx_start_interrupts(efx, false);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002788
Ben Hutchings6032fb52012-07-27 19:35:47 +01002789 mutex_lock(&efx->mac_lock);
2790 efx->phy_op->reconfigure(efx);
2791 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002792
Ben Hutchings6032fb52012-07-27 19:35:47 +01002793 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002794
Ben Hutchings6032fb52012-07-27 19:35:47 +01002795 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002796
Ben Hutchings6032fb52012-07-27 19:35:47 +01002797 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002798
Ben Hutchings6032fb52012-07-27 19:35:47 +01002799 efx->type->resume_wol(efx);
2800 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002801
Ben Hutchings61da0262012-07-27 19:35:39 +01002802 rtnl_unlock();
2803
Steve Hodgson319ba642010-06-01 11:17:24 +00002804 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2805 queue_work(reset_workqueue, &efx->reset_work);
2806
Ben Hutchings89c758f2009-11-29 03:43:07 +00002807 return 0;
2808}
2809
2810static int efx_pm_poweroff(struct device *dev)
2811{
2812 struct pci_dev *pci_dev = to_pci_dev(dev);
2813 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2814
2815 efx->type->fini(efx);
2816
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002817 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002818
2819 pci_save_state(pci_dev);
2820 return pci_set_power_state(pci_dev, PCI_D3hot);
2821}
2822
2823/* Used for both resume and restore */
2824static int efx_pm_resume(struct device *dev)
2825{
2826 struct pci_dev *pci_dev = to_pci_dev(dev);
2827 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2828 int rc;
2829
2830 rc = pci_set_power_state(pci_dev, PCI_D0);
2831 if (rc)
2832 return rc;
2833 pci_restore_state(pci_dev);
2834 rc = pci_enable_device(pci_dev);
2835 if (rc)
2836 return rc;
2837 pci_set_master(efx->pci_dev);
2838 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2839 if (rc)
2840 return rc;
2841 rc = efx->type->init(efx);
2842 if (rc)
2843 return rc;
2844 efx_pm_thaw(dev);
2845 return 0;
2846}
2847
2848static int efx_pm_suspend(struct device *dev)
2849{
2850 int rc;
2851
2852 efx_pm_freeze(dev);
2853 rc = efx_pm_poweroff(dev);
2854 if (rc)
2855 efx_pm_resume(dev);
2856 return rc;
2857}
2858
Ben Hutchings18e83e42012-01-05 19:05:20 +00002859static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002860 .suspend = efx_pm_suspend,
2861 .resume = efx_pm_resume,
2862 .freeze = efx_pm_freeze,
2863 .thaw = efx_pm_thaw,
2864 .poweroff = efx_pm_poweroff,
2865 .restore = efx_pm_resume,
2866};
2867
Ben Hutchings8ceee662008-04-27 12:55:59 +01002868static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00002869 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002870 .id_table = efx_pci_table,
2871 .probe = efx_pci_probe,
2872 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00002873 .driver.pm = &efx_pm_ops,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002874};
2875
2876/**************************************************************************
2877 *
2878 * Kernel module interface
2879 *
2880 *************************************************************************/
2881
2882module_param(interrupt_mode, uint, 0444);
2883MODULE_PARM_DESC(interrupt_mode,
2884 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2885
2886static int __init efx_init_module(void)
2887{
2888 int rc;
2889
2890 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2891
2892 rc = register_netdevice_notifier(&efx_netdev_notifier);
2893 if (rc)
2894 goto err_notifier;
2895
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002896 rc = efx_init_sriov();
2897 if (rc)
2898 goto err_sriov;
2899
Steve Hodgson1ab00622008-12-12 21:33:02 -08002900 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2901 if (!reset_workqueue) {
2902 rc = -ENOMEM;
2903 goto err_reset;
2904 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002905
2906 rc = pci_register_driver(&efx_pci_driver);
2907 if (rc < 0)
2908 goto err_pci;
2909
2910 return 0;
2911
2912 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08002913 destroy_workqueue(reset_workqueue);
2914 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002915 efx_fini_sriov();
2916 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002917 unregister_netdevice_notifier(&efx_netdev_notifier);
2918 err_notifier:
2919 return rc;
2920}
2921
2922static void __exit efx_exit_module(void)
2923{
2924 printk(KERN_INFO "Solarflare NET driver unloading\n");
2925
2926 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002927 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002928 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002929 unregister_netdevice_notifier(&efx_netdev_notifier);
2930
2931}
2932
2933module_init(efx_init_module);
2934module_exit(efx_exit_module);
2935
Ben Hutchings906bb262009-11-29 15:16:19 +00002936MODULE_AUTHOR("Solarflare Communications and "
2937 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002938MODULE_DESCRIPTION("Solarflare Communications network driver");
2939MODULE_LICENSE("GPL");
2940MODULE_DEVICE_TABLE(pci, efx_pci_table);