blob: 4a0005342e65f2c117b3c14a5fbf23b14feda87e [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 */
Neil Turton28b581a2008-12-12 21:41:06 -0800109static unsigned int separate_tx_channels;
Ben Hutchings8313aca2010-09-10 06:41:57 +0000110module_param(separate_tx_channels, uint, 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 Hutchings84ae48f2008-12-12 21:34:54 -0800163static int phy_flash_cfg;
164module_param(phy_flash_cfg, int, 0644);
165MODULE_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 Hutchings332c1ce2009-11-25 16:08:52 +0000205 if ((efx->state == STATE_RUNNING) || \
206 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100207 ASSERT_RTNL(); \
208 } while (0)
209
210/**************************************************************************
211 *
212 * Event queue processing
213 *
214 *************************************************************************/
215
216/* Process channel's event queue
217 *
218 * This function is responsible for processing the event queue of a
219 * single channel. The caller must guarantee that this function will
220 * never be concurrently called more than once on the same channel,
221 * though different channels may be being processed concurrently.
222 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000223static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100224{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000225 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100226
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000227 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100228 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100229
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000230 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000231 if (spent && efx_channel_has_rx_queue(channel)) {
232 struct efx_rx_queue *rx_queue =
233 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100234
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000235 /* Deliver last RX packet. */
236 if (channel->rx_pkt) {
237 __efx_rx_packet(channel, channel->rx_pkt);
238 channel->rx_pkt = NULL;
239 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000240 if (rx_queue->enabled) {
241 efx_rx_strategy(channel);
242 efx_fast_push_rx_descriptors(rx_queue);
243 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100244 }
245
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000246 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100247}
248
249/* Mark channel as finished processing
250 *
251 * Note that since we will not receive further interrupts for this
252 * channel before we finish processing and call the eventq_read_ack()
253 * method, there is no need to use the interrupt hold-off timers.
254 */
255static inline void efx_channel_processed(struct efx_channel *channel)
256{
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100257 /* The interrupt handler for this channel may set work_pending
258 * as soon as we acknowledge the events we've seen. Make sure
259 * it's cleared before then. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100260 channel->work_pending = false;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100261 smp_wmb();
262
Ben Hutchings152b6a62009-11-29 03:43:56 +0000263 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100264}
265
266/* NAPI poll handler
267 *
268 * NAPI guarantees serialisation of polls of the same device, which
269 * provides the guarantee required by efx_process_channel().
270 */
271static int efx_poll(struct napi_struct *napi, int budget)
272{
273 struct efx_channel *channel =
274 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000275 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000276 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100277
Ben Hutchings62776d02010-06-23 11:30:07 +0000278 netif_vdbg(efx, intr, efx->net_dev,
279 "channel %d NAPI poll executing on CPU %d\n",
280 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100281
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000282 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100283
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000284 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000285 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000286 efx->irq_rx_adaptive &&
287 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000288 if (unlikely(channel->irq_mod_score <
289 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000290 if (channel->irq_moderation > 1) {
291 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000292 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000293 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000294 } else if (unlikely(channel->irq_mod_score >
295 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000296 if (channel->irq_moderation <
297 efx->irq_rx_moderation) {
298 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000299 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000300 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000301 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000302 channel->irq_count = 0;
303 channel->irq_mod_score = 0;
304 }
305
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000306 efx_filter_rfs_expire(channel);
307
Ben Hutchings8ceee662008-04-27 12:55:59 +0100308 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800309 * only wait for napi_complete(), this isn't a problem
Ben Hutchings8ceee662008-04-27 12:55:59 +0100310 * since efx_channel_processed() will have no effect if
311 * interrupts have already been disabled.
312 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800313 napi_complete(napi);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100314 efx_channel_processed(channel);
315 }
316
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000317 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100318}
319
320/* Process the eventq of the specified channel immediately on this CPU
321 *
322 * Disable hardware generated interrupts, wait for any existing
323 * processing to finish, then directly poll (and ack ) the eventq.
324 * Finally reenable NAPI and interrupts.
325 *
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100326 * This is for use only during a loopback self-test. It must not
327 * deliver any packets up the stack as this can result in deadlock.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100328 */
329void efx_process_channel_now(struct efx_channel *channel)
330{
331 struct efx_nic *efx = channel->efx;
332
Ben Hutchings8313aca2010-09-10 06:41:57 +0000333 BUG_ON(channel->channel >= efx->n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100334 BUG_ON(!channel->enabled);
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100335 BUG_ON(!efx->loopback_selftest);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100336
337 /* Disable interrupts and wait for ISRs to complete */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000338 efx_nic_disable_interrupts(efx);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000339 if (efx->legacy_irq) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100340 synchronize_irq(efx->legacy_irq);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000341 efx->legacy_irq_enabled = false;
342 }
Ben Hutchings64ee3122008-09-01 12:47:38 +0100343 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100344 synchronize_irq(channel->irq);
345
346 /* Wait for any NAPI processing to complete */
347 napi_disable(&channel->napi_str);
348
349 /* Poll the channel */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000350 efx_process_channel(channel, channel->eventq_mask + 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100351
352 /* Ack the eventq. This may cause an interrupt to be generated
353 * when they are reenabled */
354 efx_channel_processed(channel);
355
356 napi_enable(&channel->napi_str);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000357 if (efx->legacy_irq)
358 efx->legacy_irq_enabled = true;
Ben Hutchings152b6a62009-11-29 03:43:56 +0000359 efx_nic_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100360}
361
362/* Create event queue
363 * Event queue memory allocations are done only once. If the channel
364 * is reset, the memory buffer will be reused; this guards against
365 * errors during channel reset and also simplifies interrupt handling.
366 */
367static int efx_probe_eventq(struct efx_channel *channel)
368{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000369 struct efx_nic *efx = channel->efx;
370 unsigned long entries;
371
Ben Hutchings86ee5302012-01-09 19:51:22 +0000372 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000373 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100374
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000375 /* Build an event queue with room for one event per tx and rx buffer,
376 * plus some extra for link state events and MCDI completions. */
377 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
378 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
379 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
380
Ben Hutchings152b6a62009-11-29 03:43:56 +0000381 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100382}
383
384/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100385static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100386{
Ben Hutchings62776d02010-06-23 11:30:07 +0000387 netif_dbg(channel->efx, drv, channel->efx->net_dev,
388 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100389
390 channel->eventq_read_ptr = 0;
391
Ben Hutchings152b6a62009-11-29 03:43:56 +0000392 efx_nic_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100393}
394
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000395/* Enable event queue processing and NAPI */
396static void efx_start_eventq(struct efx_channel *channel)
397{
398 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
399 "chan %d start event queue\n", channel->channel);
400
401 /* The interrupt handler for this channel may set work_pending
402 * as soon as we enable it. Make sure it's cleared before
403 * then. Similarly, make sure it sees the enabled flag set.
404 */
405 channel->work_pending = false;
406 channel->enabled = true;
407 smp_wmb();
408
409 napi_enable(&channel->napi_str);
410 efx_nic_eventq_read_ack(channel);
411}
412
413/* Disable event queue processing and NAPI */
414static void efx_stop_eventq(struct efx_channel *channel)
415{
416 if (!channel->enabled)
417 return;
418
419 napi_disable(&channel->napi_str);
420 channel->enabled = false;
421}
422
Ben Hutchings8ceee662008-04-27 12:55:59 +0100423static void efx_fini_eventq(struct efx_channel *channel)
424{
Ben Hutchings62776d02010-06-23 11:30:07 +0000425 netif_dbg(channel->efx, drv, channel->efx->net_dev,
426 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100427
Ben Hutchings152b6a62009-11-29 03:43:56 +0000428 efx_nic_fini_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100429}
430
431static void efx_remove_eventq(struct efx_channel *channel)
432{
Ben Hutchings62776d02010-06-23 11:30:07 +0000433 netif_dbg(channel->efx, drv, channel->efx->net_dev,
434 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100435
Ben Hutchings152b6a62009-11-29 03:43:56 +0000436 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100437}
438
439/**************************************************************************
440 *
441 * Channel handling
442 *
443 *************************************************************************/
444
Ben Hutchings7f967c02012-02-13 23:45:02 +0000445/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000446static struct efx_channel *
447efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
448{
449 struct efx_channel *channel;
450 struct efx_rx_queue *rx_queue;
451 struct efx_tx_queue *tx_queue;
452 int j;
453
Ben Hutchings7f967c02012-02-13 23:45:02 +0000454 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
455 if (!channel)
456 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000457
Ben Hutchings7f967c02012-02-13 23:45:02 +0000458 channel->efx = efx;
459 channel->channel = i;
460 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000461
Ben Hutchings7f967c02012-02-13 23:45:02 +0000462 for (j = 0; j < EFX_TXQ_TYPES; j++) {
463 tx_queue = &channel->tx_queue[j];
464 tx_queue->efx = efx;
465 tx_queue->queue = i * EFX_TXQ_TYPES + j;
466 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000467 }
468
Ben Hutchings46426102010-09-10 06:42:33 +0000469 rx_queue = &channel->rx_queue;
470 rx_queue->efx = efx;
471 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
472 (unsigned long)rx_queue);
473
474 return channel;
475}
476
Ben Hutchings7f967c02012-02-13 23:45:02 +0000477/* Allocate and initialise a channel structure, copying parameters
478 * (but not resources) from an old channel structure.
479 */
480static struct efx_channel *
481efx_copy_channel(const struct efx_channel *old_channel)
482{
483 struct efx_channel *channel;
484 struct efx_rx_queue *rx_queue;
485 struct efx_tx_queue *tx_queue;
486 int j;
487
488 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
489 if (!channel)
490 return NULL;
491
492 *channel = *old_channel;
493
494 channel->napi_dev = NULL;
495 memset(&channel->eventq, 0, sizeof(channel->eventq));
496
497 for (j = 0; j < EFX_TXQ_TYPES; j++) {
498 tx_queue = &channel->tx_queue[j];
499 if (tx_queue->channel)
500 tx_queue->channel = channel;
501 tx_queue->buffer = NULL;
502 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
503 }
504
505 rx_queue = &channel->rx_queue;
506 rx_queue->buffer = NULL;
507 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
508 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
509 (unsigned long)rx_queue);
510
511 return channel;
512}
513
Ben Hutchings8ceee662008-04-27 12:55:59 +0100514static int efx_probe_channel(struct efx_channel *channel)
515{
516 struct efx_tx_queue *tx_queue;
517 struct efx_rx_queue *rx_queue;
518 int rc;
519
Ben Hutchings62776d02010-06-23 11:30:07 +0000520 netif_dbg(channel->efx, probe, channel->efx->net_dev,
521 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100522
Ben Hutchings7f967c02012-02-13 23:45:02 +0000523 rc = channel->type->pre_probe(channel);
524 if (rc)
525 goto fail;
526
Ben Hutchings8ceee662008-04-27 12:55:59 +0100527 rc = efx_probe_eventq(channel);
528 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000529 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100530
531 efx_for_each_channel_tx_queue(tx_queue, channel) {
532 rc = efx_probe_tx_queue(tx_queue);
533 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000534 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100535 }
536
537 efx_for_each_channel_rx_queue(rx_queue, channel) {
538 rc = efx_probe_rx_queue(rx_queue);
539 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000540 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100541 }
542
543 channel->n_rx_frm_trunc = 0;
544
545 return 0;
546
Ben Hutchings7f967c02012-02-13 23:45:02 +0000547fail:
548 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100549 return rc;
550}
551
Ben Hutchings7f967c02012-02-13 23:45:02 +0000552static void
553efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
554{
555 struct efx_nic *efx = channel->efx;
556 const char *type;
557 int number;
558
559 number = channel->channel;
560 if (efx->tx_channel_offset == 0) {
561 type = "";
562 } else if (channel->channel < efx->tx_channel_offset) {
563 type = "-rx";
564 } else {
565 type = "-tx";
566 number -= efx->tx_channel_offset;
567 }
568 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
569}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100570
Ben Hutchings56536e92008-12-12 21:37:02 -0800571static void efx_set_channel_names(struct efx_nic *efx)
572{
573 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800574
Ben Hutchings7f967c02012-02-13 23:45:02 +0000575 efx_for_each_channel(channel, efx)
576 channel->type->get_name(channel,
577 efx->channel_name[channel->channel],
578 sizeof(efx->channel_name[0]));
Ben Hutchings56536e92008-12-12 21:37:02 -0800579}
580
Ben Hutchings46426102010-09-10 06:42:33 +0000581static int efx_probe_channels(struct efx_nic *efx)
582{
583 struct efx_channel *channel;
584 int rc;
585
586 /* Restart special buffer allocation */
587 efx->next_buffer_table = 0;
588
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000589 /* Probe channels in reverse, so that any 'extra' channels
590 * use the start of the buffer table. This allows the traffic
591 * channels to be resized without moving them or wasting the
592 * entries before them.
593 */
594 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000595 rc = efx_probe_channel(channel);
596 if (rc) {
597 netif_err(efx, probe, efx->net_dev,
598 "failed to create channel %d\n",
599 channel->channel);
600 goto fail;
601 }
602 }
603 efx_set_channel_names(efx);
604
605 return 0;
606
607fail:
608 efx_remove_channels(efx);
609 return rc;
610}
611
Ben Hutchings8ceee662008-04-27 12:55:59 +0100612/* Channels are shutdown and reinitialised whilst the NIC is running
613 * to propagate configuration changes (mtu, checksum offload), or
614 * to clear hardware error conditions
615 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000616static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100617{
618 struct efx_tx_queue *tx_queue;
619 struct efx_rx_queue *rx_queue;
620 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100621
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100622 /* Calculate the rx buffer allocation parameters required to
623 * support the current MTU, including padding for header
624 * alignment and overruns.
625 */
626 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
627 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
Ben Hutchings39c9cf02010-06-23 11:31:28 +0000628 efx->type->rx_buffer_hash_size +
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100629 efx->type->rx_buffer_padding);
Steve Hodgson62b330b2010-06-01 11:20:53 +0000630 efx->rx_buffer_order = get_order(efx->rx_buffer_len +
631 sizeof(struct efx_rx_page_state));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100632
633 /* Initialise the channels */
634 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100635 efx_for_each_channel_tx_queue(tx_queue, channel)
636 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100637
638 /* The rx buffer allocation strategy is MTU dependent */
639 efx_rx_strategy(channel);
640
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000641 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100642 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000643 efx_nic_generate_fill_event(rx_queue);
644 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100645
646 WARN_ON(channel->rx_pkt != NULL);
647 efx_rx_strategy(channel);
648 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000649
650 if (netif_device_present(efx->net_dev))
651 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100652}
653
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000654static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100655{
656 struct efx_channel *channel;
657 struct efx_tx_queue *tx_queue;
658 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100659 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100660
661 EFX_ASSERT_RESET_SERIALISED(efx);
662 BUG_ON(efx->port_enabled);
663
Ben Hutchings152b6a62009-11-29 03:43:56 +0000664 rc = efx_nic_flush_queues(efx);
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000665 if (rc && EFX_WORKAROUND_7803(efx)) {
666 /* Schedule a reset to recover from the flush failure. The
667 * descriptor caches reference memory we're about to free,
668 * but falcon_reconfigure_mac_wrapper() won't reconnect
669 * the MACs because of the pending reset. */
Ben Hutchings62776d02010-06-23 11:30:07 +0000670 netif_err(efx, drv, efx->net_dev,
671 "Resetting to recover from flush failure\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000672 efx_schedule_reset(efx, RESET_TYPE_ALL);
673 } else if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +0000674 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000675 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +0000676 netif_dbg(efx, drv, efx->net_dev,
677 "successfully flushed all queues\n");
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000678 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100679
Ben Hutchings8ceee662008-04-27 12:55:59 +0100680 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000681 /* RX packet processing is pipelined, so wait for the
682 * NAPI handler to complete. At least event queue 0
683 * might be kept active by non-data events, so don't
684 * use napi_synchronize() but actually disable NAPI
685 * temporarily.
686 */
687 if (efx_channel_has_rx_queue(channel)) {
688 efx_stop_eventq(channel);
689 efx_start_eventq(channel);
690 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100691
692 efx_for_each_channel_rx_queue(rx_queue, channel)
693 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000694 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100695 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100696 }
697}
698
699static void efx_remove_channel(struct efx_channel *channel)
700{
701 struct efx_tx_queue *tx_queue;
702 struct efx_rx_queue *rx_queue;
703
Ben Hutchings62776d02010-06-23 11:30:07 +0000704 netif_dbg(channel->efx, drv, channel->efx->net_dev,
705 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100706
707 efx_for_each_channel_rx_queue(rx_queue, channel)
708 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000709 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100710 efx_remove_tx_queue(tx_queue);
711 efx_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100712}
713
Ben Hutchings46426102010-09-10 06:42:33 +0000714static void efx_remove_channels(struct efx_nic *efx)
715{
716 struct efx_channel *channel;
717
718 efx_for_each_channel(channel, efx)
719 efx_remove_channel(channel);
720}
721
722int
723efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
724{
725 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
726 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000727 unsigned i, next_buffer_table = 0;
728 int rc = 0;
729
730 /* Not all channels should be reallocated. We must avoid
731 * reallocating their buffer table entries.
732 */
733 efx_for_each_channel(channel, efx) {
734 struct efx_rx_queue *rx_queue;
735 struct efx_tx_queue *tx_queue;
736
737 if (channel->type->copy)
738 continue;
739 next_buffer_table = max(next_buffer_table,
740 channel->eventq.index +
741 channel->eventq.entries);
742 efx_for_each_channel_rx_queue(rx_queue, channel)
743 next_buffer_table = max(next_buffer_table,
744 rx_queue->rxd.index +
745 rx_queue->rxd.entries);
746 efx_for_each_channel_tx_queue(tx_queue, channel)
747 next_buffer_table = max(next_buffer_table,
748 tx_queue->txd.index +
749 tx_queue->txd.entries);
750 }
Ben Hutchings46426102010-09-10 06:42:33 +0000751
752 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000753 efx_stop_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000754
Ben Hutchings7f967c02012-02-13 23:45:02 +0000755 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000756 memset(other_channel, 0, sizeof(other_channel));
757 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000758 channel = efx->channel[i];
759 if (channel->type->copy)
760 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000761 if (!channel) {
762 rc = -ENOMEM;
763 goto out;
764 }
765 other_channel[i] = channel;
766 }
767
768 /* Swap entry counts and channel pointers */
769 old_rxq_entries = efx->rxq_entries;
770 old_txq_entries = efx->txq_entries;
771 efx->rxq_entries = rxq_entries;
772 efx->txq_entries = txq_entries;
773 for (i = 0; i < efx->n_channels; i++) {
774 channel = efx->channel[i];
775 efx->channel[i] = other_channel[i];
776 other_channel[i] = channel;
777 }
778
Ben Hutchings7f967c02012-02-13 23:45:02 +0000779 /* Restart buffer table allocation */
780 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000781
Ben Hutchingse8f14992010-12-07 19:47:34 +0000782 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000783 channel = efx->channel[i];
784 if (!channel->type->copy)
785 continue;
786 rc = efx_probe_channel(channel);
787 if (rc)
788 goto rollback;
789 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000790 }
Ben Hutchings46426102010-09-10 06:42:33 +0000791
Ben Hutchings7f967c02012-02-13 23:45:02 +0000792out:
793 /* Destroy unused channel structures */
794 for (i = 0; i < efx->n_channels; i++) {
795 channel = other_channel[i];
796 if (channel && channel->type->copy) {
797 efx_fini_napi_channel(channel);
798 efx_remove_channel(channel);
799 kfree(channel);
800 }
801 }
802
803 efx_start_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000804 efx_start_all(efx);
805 return rc;
806
807rollback:
808 /* Swap back */
809 efx->rxq_entries = old_rxq_entries;
810 efx->txq_entries = old_txq_entries;
811 for (i = 0; i < efx->n_channels; i++) {
812 channel = efx->channel[i];
813 efx->channel[i] = other_channel[i];
814 other_channel[i] = channel;
815 }
816 goto out;
817}
818
Steve Hodgson90d683a2010-06-01 11:19:39 +0000819void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100820{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000821 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100822}
823
Ben Hutchings7f967c02012-02-13 23:45:02 +0000824static const struct efx_channel_type efx_default_channel_type = {
825 .pre_probe = efx_channel_dummy_op_int,
826 .get_name = efx_get_channel_name,
827 .copy = efx_copy_channel,
828 .keep_eventq = false,
829};
830
831int efx_channel_dummy_op_int(struct efx_channel *channel)
832{
833 return 0;
834}
835
Ben Hutchings8ceee662008-04-27 12:55:59 +0100836/**************************************************************************
837 *
838 * Port handling
839 *
840 **************************************************************************/
841
842/* This ensures that the kernel is kept informed (via
843 * netif_carrier_on/off) of the link status, and also maintains the
844 * link status's stop on the port's TX queue.
845 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000846void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100847{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000848 struct efx_link_state *link_state = &efx->link_state;
849
Ben Hutchings8ceee662008-04-27 12:55:59 +0100850 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
851 * that no events are triggered between unregister_netdev() and the
852 * driver unloading. A more general condition is that NETDEV_CHANGE
853 * can only be generated between NETDEV_UP and NETDEV_DOWN */
854 if (!netif_running(efx->net_dev))
855 return;
856
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000857 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100858 efx->n_link_state_changes++;
859
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000860 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100861 netif_carrier_on(efx->net_dev);
862 else
863 netif_carrier_off(efx->net_dev);
864 }
865
866 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000867 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000868 netif_info(efx, link, efx->net_dev,
869 "link up at %uMbps %s-duplex (MTU %d)%s\n",
870 link_state->speed, link_state->fd ? "full" : "half",
871 efx->net_dev->mtu,
872 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000873 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000874 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100875}
876
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000877void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
878{
879 efx->link_advertising = advertising;
880 if (advertising) {
881 if (advertising & ADVERTISED_Pause)
882 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
883 else
884 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
885 if (advertising & ADVERTISED_Asym_Pause)
886 efx->wanted_fc ^= EFX_FC_TX;
887 }
888}
889
David S. Millerb56269462011-05-17 17:53:22 -0400890void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000891{
892 efx->wanted_fc = wanted_fc;
893 if (efx->link_advertising) {
894 if (wanted_fc & EFX_FC_RX)
895 efx->link_advertising |= (ADVERTISED_Pause |
896 ADVERTISED_Asym_Pause);
897 else
898 efx->link_advertising &= ~(ADVERTISED_Pause |
899 ADVERTISED_Asym_Pause);
900 if (wanted_fc & EFX_FC_TX)
901 efx->link_advertising ^= ADVERTISED_Asym_Pause;
902 }
903}
904
Ben Hutchings115122a2009-03-04 09:52:52 +0000905static void efx_fini_port(struct efx_nic *efx);
906
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000907/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
908 * the MAC appropriately. All other PHY configuration changes are pushed
909 * through phy_op->set_settings(), and pushed asynchronously to the MAC
910 * through efx_monitor().
911 *
912 * Callers must hold the mac_lock
913 */
914int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100915{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000916 enum efx_phy_mode phy_mode;
917 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100918
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000919 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100920
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000921 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000922 netif_addr_lock_bh(efx->net_dev);
923 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100924
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000925 /* Disable PHY transmit in mac level loopbacks */
926 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800927 if (LOOPBACK_INTERNAL(efx))
928 efx->phy_mode |= PHY_MODE_TX_DISABLED;
929 else
930 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800931
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000932 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800933
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000934 if (rc)
935 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100936
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000937 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100938}
939
940/* Reinitialise the MAC to pick up new PHY settings, even if the port is
941 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000942int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100943{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000944 int rc;
945
Ben Hutchings8ceee662008-04-27 12:55:59 +0100946 EFX_ASSERT_RESET_SERIALISED(efx);
947
948 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000949 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100950 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000951
952 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100953}
954
Ben Hutchings8be4f3e2009-11-25 16:12:16 +0000955/* Asynchronous work item for changing MAC promiscuity and multicast
956 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
957 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800958static void efx_mac_work(struct work_struct *data)
959{
960 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
961
962 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +0100963 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +0100964 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800965 mutex_unlock(&efx->mac_lock);
966}
967
Ben Hutchings8ceee662008-04-27 12:55:59 +0100968static int efx_probe_port(struct efx_nic *efx)
969{
970 int rc;
971
Ben Hutchings62776d02010-06-23 11:30:07 +0000972 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100973
Steve Hodgsonff3b00a2009-12-23 13:46:36 +0000974 if (phy_flash_cfg)
975 efx->phy_mode = PHY_MODE_SPECIAL;
976
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000977 /* Connect up MAC/PHY operations table */
978 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100979 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +0000980 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100981
Ben Hutchingse332bcb2011-12-20 01:22:51 +0000982 /* Initialise MAC address to permanent address */
983 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100984
985 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100986}
987
988static int efx_init_port(struct efx_nic *efx)
989{
990 int rc;
991
Ben Hutchings62776d02010-06-23 11:30:07 +0000992 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100993
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000994 mutex_lock(&efx->mac_lock);
995
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800996 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100997 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +0000998 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100999
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001000 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001001
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001002 /* Reconfigure the MAC before creating dma queues (required for
1003 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001004 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001005
1006 /* Ensure the PHY advertises the correct flow control settings */
1007 rc = efx->phy_op->reconfigure(efx);
1008 if (rc)
1009 goto fail2;
1010
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001011 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001012 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001013
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001014fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001015 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001016fail1:
1017 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001018 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001019}
1020
Ben Hutchings8ceee662008-04-27 12:55:59 +01001021static void efx_start_port(struct efx_nic *efx)
1022{
Ben Hutchings62776d02010-06-23 11:30:07 +00001023 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001024 BUG_ON(efx->port_enabled);
1025
1026 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001027 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001028
1029 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1030 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001031 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001032
Ben Hutchings8ceee662008-04-27 12:55:59 +01001033 mutex_unlock(&efx->mac_lock);
1034}
1035
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001036/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001037static void efx_stop_port(struct efx_nic *efx)
1038{
Ben Hutchings62776d02010-06-23 11:30:07 +00001039 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001040
1041 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001042 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001043 mutex_unlock(&efx->mac_lock);
1044
1045 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001046 netif_addr_lock_bh(efx->net_dev);
1047 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001048}
1049
1050static void efx_fini_port(struct efx_nic *efx)
1051{
Ben Hutchings62776d02010-06-23 11:30:07 +00001052 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001053
1054 if (!efx->port_initialized)
1055 return;
1056
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001057 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001058 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001059
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001060 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001061 efx_link_status_changed(efx);
1062}
1063
1064static void efx_remove_port(struct efx_nic *efx)
1065{
Ben Hutchings62776d02010-06-23 11:30:07 +00001066 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001067
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001068 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001069}
1070
1071/**************************************************************************
1072 *
1073 * NIC handling
1074 *
1075 **************************************************************************/
1076
1077/* This configures the PCI device to enable I/O and DMA. */
1078static int efx_init_io(struct efx_nic *efx)
1079{
1080 struct pci_dev *pci_dev = efx->pci_dev;
1081 dma_addr_t dma_mask = efx->type->max_dma_mask;
1082 int rc;
1083
Ben Hutchings62776d02010-06-23 11:30:07 +00001084 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001085
1086 rc = pci_enable_device(pci_dev);
1087 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001088 netif_err(efx, probe, efx->net_dev,
1089 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001090 goto fail1;
1091 }
1092
1093 pci_set_master(pci_dev);
1094
1095 /* Set the PCI DMA mask. Try all possibilities from our
1096 * genuine mask down to 32 bits, because some architectures
1097 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1098 * masks event though they reject 46 bit masks.
1099 */
1100 while (dma_mask > 0x7fffffffUL) {
Ben Hutchingse9e01842012-01-05 18:50:29 +00001101 if (pci_dma_supported(pci_dev, dma_mask)) {
1102 rc = pci_set_dma_mask(pci_dev, dma_mask);
1103 if (rc == 0)
1104 break;
1105 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001106 dma_mask >>= 1;
1107 }
1108 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001109 netif_err(efx, probe, efx->net_dev,
1110 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001111 goto fail2;
1112 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001113 netif_dbg(efx, probe, efx->net_dev,
1114 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001115 rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
1116 if (rc) {
1117 /* pci_set_consistent_dma_mask() is not *allowed* to
1118 * fail with a mask that pci_set_dma_mask() accepted,
1119 * but just in case...
1120 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001121 netif_err(efx, probe, efx->net_dev,
1122 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001123 goto fail2;
1124 }
1125
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001126 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1127 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001128 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001129 netif_err(efx, probe, efx->net_dev,
1130 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001131 rc = -EIO;
1132 goto fail3;
1133 }
David S. Miller8decf862011-09-22 03:23:13 -04001134 efx->membase = ioremap_nocache(efx->membase_phys,
1135 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001136 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001137 netif_err(efx, probe, efx->net_dev,
1138 "could not map memory BAR at %llx+%x\n",
1139 (unsigned long long)efx->membase_phys,
1140 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001141 rc = -ENOMEM;
1142 goto fail4;
1143 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001144 netif_dbg(efx, probe, efx->net_dev,
1145 "memory BAR at %llx+%x (virtual %p)\n",
1146 (unsigned long long)efx->membase_phys,
1147 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001148
1149 return 0;
1150
1151 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001152 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001153 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001154 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001155 fail2:
1156 pci_disable_device(efx->pci_dev);
1157 fail1:
1158 return rc;
1159}
1160
1161static void efx_fini_io(struct efx_nic *efx)
1162{
Ben Hutchings62776d02010-06-23 11:30:07 +00001163 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001164
1165 if (efx->membase) {
1166 iounmap(efx->membase);
1167 efx->membase = NULL;
1168 }
1169
1170 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001171 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001172 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001173 }
1174
1175 pci_disable_device(efx->pci_dev);
1176}
1177
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001178static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001179{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001180 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001181 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001182 int cpu;
1183
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001184 if (rss_cpus) {
1185 count = rss_cpus;
1186 } else {
1187 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1188 netif_warn(efx, probe, efx->net_dev,
1189 "RSS disabled due to allocation failure\n");
1190 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001191 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001192
1193 count = 0;
1194 for_each_online_cpu(cpu) {
1195 if (!cpumask_test_cpu(cpu, thread_mask)) {
1196 ++count;
1197 cpumask_or(thread_mask, thread_mask,
1198 topology_thread_cpumask(cpu));
1199 }
1200 }
1201
1202 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001203 }
1204
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001205 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1206 * table entries that are inaccessible to VFs
1207 */
1208 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1209 count > efx_vf_size(efx)) {
1210 netif_warn(efx, probe, efx->net_dev,
1211 "Reducing number of RSS channels from %u to %u for "
1212 "VF support. Increase vf-msix-limit to use more "
1213 "channels on the PF.\n",
1214 count, efx_vf_size(efx));
1215 count = efx_vf_size(efx);
1216 }
1217
Ben Hutchings46123d02008-09-01 12:47:33 +01001218 return count;
1219}
1220
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001221static int
1222efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1223{
1224#ifdef CONFIG_RFS_ACCEL
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001225 unsigned int i;
1226 int rc;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001227
1228 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1229 if (!efx->net_dev->rx_cpu_rmap)
1230 return -ENOMEM;
1231 for (i = 0; i < efx->n_rx_channels; i++) {
1232 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1233 xentries[i].vector);
1234 if (rc) {
1235 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1236 efx->net_dev->rx_cpu_rmap = NULL;
1237 return rc;
1238 }
1239 }
1240#endif
1241 return 0;
1242}
1243
Ben Hutchings46123d02008-09-01 12:47:33 +01001244/* Probe the number and type of interrupts we are able to obtain, and
1245 * the resulting numbers of channels and RX queues.
1246 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001247static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001248{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001249 unsigned int max_channels =
1250 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001251 unsigned int extra_channels = 0;
1252 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001253 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001254
Ben Hutchings7f967c02012-02-13 23:45:02 +00001255 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1256 if (efx->extra_channel_type[i])
1257 ++extra_channels;
1258
Ben Hutchings8ceee662008-04-27 12:55:59 +01001259 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001260 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001261 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001262
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001263 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001264 if (separate_tx_channels)
1265 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001266 n_channels += extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001267 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001268
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001269 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001270 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001271 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001272 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001273 netif_err(efx, drv, efx->net_dev,
1274 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001275 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001276 netif_err(efx, drv, efx->net_dev,
1277 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001278 EFX_BUG_ON_PARANOID(rc >= n_channels);
1279 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001280 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001281 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001282 }
1283
1284 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001285 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001286 if (n_channels > extra_channels)
1287 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001288 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001289 efx->n_tx_channels = max(n_channels / 2, 1U);
1290 efx->n_rx_channels = max(n_channels -
1291 efx->n_tx_channels,
1292 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001293 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001294 efx->n_tx_channels = n_channels;
1295 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001296 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001297 rc = efx_init_rx_cpu_rmap(efx, xentries);
1298 if (rc) {
1299 pci_disable_msix(efx->pci_dev);
1300 return rc;
1301 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001302 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001303 efx_get_channel(efx, i)->irq =
1304 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001305 } else {
1306 /* Fall back to single channel MSI */
1307 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001308 netif_err(efx, drv, efx->net_dev,
1309 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001310 }
1311 }
1312
1313 /* Try single interrupt MSI */
1314 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001315 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001316 efx->n_rx_channels = 1;
1317 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001318 rc = pci_enable_msi(efx->pci_dev);
1319 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001320 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001321 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001322 netif_err(efx, drv, efx->net_dev,
1323 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001324 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1325 }
1326 }
1327
1328 /* Assume legacy interrupts */
1329 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001330 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001331 efx->n_rx_channels = 1;
1332 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001333 efx->legacy_irq = efx->pci_dev->irq;
1334 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001335
Ben Hutchings7f967c02012-02-13 23:45:02 +00001336 /* Assign extra channels if possible */
1337 j = efx->n_channels;
1338 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1339 if (!efx->extra_channel_type[i])
1340 continue;
1341 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1342 efx->n_channels <= extra_channels) {
1343 efx->extra_channel_type[i]->handle_no_channel(efx);
1344 } else {
1345 --j;
1346 efx_get_channel(efx, j)->type =
1347 efx->extra_channel_type[i];
1348 }
1349 }
1350
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001351 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001352 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001353 efx->n_rx_channels : efx_vf_size(efx));
1354
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001355 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001356}
1357
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001358/* Enable interrupts, then probe and start the event queues */
Ben Hutchings7f967c02012-02-13 23:45:02 +00001359static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001360{
1361 struct efx_channel *channel;
1362
1363 if (efx->legacy_irq)
1364 efx->legacy_irq_enabled = true;
1365 efx_nic_enable_interrupts(efx);
1366
1367 efx_for_each_channel(channel, efx) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001368 if (!channel->type->keep_eventq || !may_keep_eventq)
1369 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001370 efx_start_eventq(channel);
1371 }
1372
1373 efx_mcdi_mode_event(efx);
1374}
1375
Ben Hutchings7f967c02012-02-13 23:45:02 +00001376static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001377{
1378 struct efx_channel *channel;
1379
1380 efx_mcdi_mode_poll(efx);
1381
1382 efx_nic_disable_interrupts(efx);
1383 if (efx->legacy_irq) {
1384 synchronize_irq(efx->legacy_irq);
1385 efx->legacy_irq_enabled = false;
1386 }
1387
1388 efx_for_each_channel(channel, efx) {
1389 if (channel->irq)
1390 synchronize_irq(channel->irq);
1391
1392 efx_stop_eventq(channel);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001393 if (!channel->type->keep_eventq || !may_keep_eventq)
1394 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001395 }
1396}
1397
Ben Hutchings8ceee662008-04-27 12:55:59 +01001398static void efx_remove_interrupts(struct efx_nic *efx)
1399{
1400 struct efx_channel *channel;
1401
1402 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001403 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001404 channel->irq = 0;
1405 pci_disable_msi(efx->pci_dev);
1406 pci_disable_msix(efx->pci_dev);
1407
1408 /* Remove legacy interrupt */
1409 efx->legacy_irq = 0;
1410}
1411
Ben Hutchings8831da72008-09-01 12:47:48 +01001412static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001413{
Ben Hutchings602a5322011-05-16 17:32:39 +01001414 struct efx_channel *channel;
1415 struct efx_tx_queue *tx_queue;
1416
Ben Hutchings97653432011-01-12 18:26:56 +00001417 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001418 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001419
1420 /* We need to adjust the TX queue numbers if we have separate
1421 * RX-only and TX-only channels.
1422 */
1423 efx_for_each_channel(channel, efx) {
1424 efx_for_each_channel_tx_queue(tx_queue, channel)
1425 tx_queue->queue -= (efx->tx_channel_offset *
1426 EFX_TXQ_TYPES);
1427 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001428}
1429
1430static int efx_probe_nic(struct efx_nic *efx)
1431{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001432 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001433 int rc;
1434
Ben Hutchings62776d02010-06-23 11:30:07 +00001435 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001436
1437 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001438 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001439 if (rc)
1440 return rc;
1441
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001442 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001443 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001444 rc = efx_probe_interrupts(efx);
1445 if (rc)
1446 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001447
Ben Hutchings28e47c42012-02-15 01:58:49 +00001448 efx->type->dimension_resources(efx);
1449
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001450 if (efx->n_channels > 1)
1451 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001452 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001453 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001454 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001455
Ben Hutchings8831da72008-09-01 12:47:48 +01001456 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001457 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1458 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001459
1460 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001461 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1462 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001463
1464 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001465
1466fail:
1467 efx->type->remove(efx);
1468 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001469}
1470
1471static void efx_remove_nic(struct efx_nic *efx)
1472{
Ben Hutchings62776d02010-06-23 11:30:07 +00001473 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001474
1475 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001476 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001477}
1478
1479/**************************************************************************
1480 *
1481 * NIC startup/shutdown
1482 *
1483 *************************************************************************/
1484
1485static int efx_probe_all(struct efx_nic *efx)
1486{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001487 int rc;
1488
Ben Hutchings8ceee662008-04-27 12:55:59 +01001489 rc = efx_probe_nic(efx);
1490 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001491 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001492 goto fail1;
1493 }
1494
Ben Hutchings8ceee662008-04-27 12:55:59 +01001495 rc = efx_probe_port(efx);
1496 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001497 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001498 goto fail2;
1499 }
1500
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001501 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001502
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001503 rc = efx_probe_filters(efx);
1504 if (rc) {
1505 netif_err(efx, probe, efx->net_dev,
1506 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001507 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001508 }
1509
Ben Hutchings7f967c02012-02-13 23:45:02 +00001510 rc = efx_probe_channels(efx);
1511 if (rc)
1512 goto fail4;
1513
Ben Hutchings8ceee662008-04-27 12:55:59 +01001514 return 0;
1515
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001516 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001517 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001518 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001519 efx_remove_port(efx);
1520 fail2:
1521 efx_remove_nic(efx);
1522 fail1:
1523 return rc;
1524}
1525
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001526/* Called after previous invocation(s) of efx_stop_all, restarts the port,
1527 * kernel transmit queues and NAPI processing, and ensures that the port is
1528 * scheduled to be reconfigured. This function is safe to call multiple
1529 * times when the NIC is in any state.
1530 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001531static void efx_start_all(struct efx_nic *efx)
1532{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001533 EFX_ASSERT_RESET_SERIALISED(efx);
1534
1535 /* Check that it is appropriate to restart the interface. All
1536 * of these flags are safe to read under just the rtnl lock */
1537 if (efx->port_enabled)
1538 return;
1539 if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1540 return;
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001541 if (!netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001542 return;
1543
Ben Hutchings8ceee662008-04-27 12:55:59 +01001544 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001545 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001546
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001547 /* Start the hardware monitor if there is one. Otherwise (we're link
1548 * event driven), we have to poll the PHY because after an event queue
1549 * flush, we could have a missed a link state change */
1550 if (efx->type->monitor != NULL) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001551 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1552 efx_monitor_interval);
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001553 } else {
1554 mutex_lock(&efx->mac_lock);
1555 if (efx->phy_op->poll(efx))
1556 efx_link_status_changed(efx);
1557 mutex_unlock(&efx->mac_lock);
1558 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001559
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001560 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001561}
1562
1563/* Flush all delayed work. Should only be called when no more delayed work
1564 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1565 * since we're holding the rtnl_lock at this point. */
1566static void efx_flush_all(struct efx_nic *efx)
1567{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001568 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001569 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001570 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001571 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001572 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001573}
1574
1575/* Quiesce hardware and software without bringing the link down.
1576 * Safe to call multiple times, when the nic and interface is in any
1577 * state. The caller is guaranteed to subsequently be in a position
1578 * to modify any hardware and software state they see fit without
1579 * taking locks. */
1580static void efx_stop_all(struct efx_nic *efx)
1581{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001582 EFX_ASSERT_RESET_SERIALISED(efx);
1583
1584 /* port_enabled can be read safely under the rtnl lock */
1585 if (!efx->port_enabled)
1586 return;
1587
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001588 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001589 efx_stop_port(efx);
1590
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001591 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001592 efx_flush_all(efx);
1593
Ben Hutchings8ceee662008-04-27 12:55:59 +01001594 /* Stop the kernel transmit interface late, so the watchdog
1595 * timer isn't ticking over the flush */
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001596 netif_tx_disable(efx->net_dev);
1597
1598 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001599}
1600
1601static void efx_remove_all(struct efx_nic *efx)
1602{
Ben Hutchings46426102010-09-10 06:42:33 +00001603 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001604 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001605 efx_remove_port(efx);
1606 efx_remove_nic(efx);
1607}
1608
Ben Hutchings8ceee662008-04-27 12:55:59 +01001609/**************************************************************************
1610 *
1611 * Interrupt moderation
1612 *
1613 **************************************************************************/
1614
Ben Hutchingscc180b62011-12-08 19:51:47 +00001615static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001616{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001617 if (usecs == 0)
1618 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001619 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001620 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001621 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001622}
1623
Ben Hutchings8ceee662008-04-27 12:55:59 +01001624/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001625int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1626 unsigned int rx_usecs, bool rx_adaptive,
1627 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001628{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001629 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001630 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1631 efx->timer_quantum_ns,
1632 1000);
1633 unsigned int tx_ticks;
1634 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001635
1636 EFX_ASSERT_RESET_SERIALISED(efx);
1637
Ben Hutchingscc180b62011-12-08 19:51:47 +00001638 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001639 return -EINVAL;
1640
Ben Hutchingscc180b62011-12-08 19:51:47 +00001641 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1642 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1643
Ben Hutchings9e393b32011-09-05 07:43:04 +00001644 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1645 !rx_may_override_tx) {
1646 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1647 "RX and TX IRQ moderation must be equal\n");
1648 return -EINVAL;
1649 }
1650
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001651 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001652 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001653 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001654 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001655 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001656 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001657 channel->irq_moderation = tx_ticks;
1658 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001659
1660 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001661}
1662
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001663void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1664 unsigned int *rx_usecs, bool *rx_adaptive)
1665{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001666 /* We must round up when converting ticks to microseconds
1667 * because we round down when converting the other way.
1668 */
1669
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001670 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001671 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1672 efx->timer_quantum_ns,
1673 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001674
1675 /* If channels are shared between RX and TX, so is IRQ
1676 * moderation. Otherwise, IRQ moderation is the same for all
1677 * TX channels and is not adaptive.
1678 */
1679 if (efx->tx_channel_offset == 0)
1680 *tx_usecs = *rx_usecs;
1681 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001682 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001683 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001684 efx->timer_quantum_ns,
1685 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001686}
1687
Ben Hutchings8ceee662008-04-27 12:55:59 +01001688/**************************************************************************
1689 *
1690 * Hardware monitor
1691 *
1692 **************************************************************************/
1693
Ben Hutchingse254c272010-09-20 08:44:10 +00001694/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001695static void efx_monitor(struct work_struct *data)
1696{
1697 struct efx_nic *efx = container_of(data, struct efx_nic,
1698 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001699
Ben Hutchings62776d02010-06-23 11:30:07 +00001700 netif_vdbg(efx, timer, efx->net_dev,
1701 "hardware monitor executing on CPU %d\n",
1702 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001703 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001704
Ben Hutchings8ceee662008-04-27 12:55:59 +01001705 /* If the mac_lock is already held then it is likely a port
1706 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001707 * most of the work of monitor() anyway. */
1708 if (mutex_trylock(&efx->mac_lock)) {
1709 if (efx->port_enabled)
1710 efx->type->monitor(efx);
1711 mutex_unlock(&efx->mac_lock);
1712 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001713
Ben Hutchings8ceee662008-04-27 12:55:59 +01001714 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1715 efx_monitor_interval);
1716}
1717
1718/**************************************************************************
1719 *
1720 * ioctls
1721 *
1722 *************************************************************************/
1723
1724/* Net device ioctl
1725 * Context: process, rtnl_lock() held.
1726 */
1727static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1728{
Ben Hutchings767e4682008-09-01 12:43:14 +01001729 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001730 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001731
1732 EFX_ASSERT_RESET_SERIALISED(efx);
1733
Ben Hutchings68e7f452009-04-29 08:05:08 +00001734 /* Convert phy_id from older PRTAD/DEVAD format */
1735 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1736 (data->phy_id & 0xfc00) == 0x0400)
1737 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1738
1739 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001740}
1741
1742/**************************************************************************
1743 *
1744 * NAPI interface
1745 *
1746 **************************************************************************/
1747
Ben Hutchings7f967c02012-02-13 23:45:02 +00001748static void efx_init_napi_channel(struct efx_channel *channel)
1749{
1750 struct efx_nic *efx = channel->efx;
1751
1752 channel->napi_dev = efx->net_dev;
1753 netif_napi_add(channel->napi_dev, &channel->napi_str,
1754 efx_poll, napi_weight);
1755}
1756
Ben Hutchingse8f14992010-12-07 19:47:34 +00001757static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001758{
1759 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001760
Ben Hutchings7f967c02012-02-13 23:45:02 +00001761 efx_for_each_channel(channel, efx)
1762 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001763}
1764
1765static void efx_fini_napi_channel(struct efx_channel *channel)
1766{
1767 if (channel->napi_dev)
1768 netif_napi_del(&channel->napi_str);
1769 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001770}
1771
1772static void efx_fini_napi(struct efx_nic *efx)
1773{
1774 struct efx_channel *channel;
1775
Ben Hutchingse8f14992010-12-07 19:47:34 +00001776 efx_for_each_channel(channel, efx)
1777 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001778}
1779
1780/**************************************************************************
1781 *
1782 * Kernel netpoll interface
1783 *
1784 *************************************************************************/
1785
1786#ifdef CONFIG_NET_POLL_CONTROLLER
1787
1788/* Although in the common case interrupts will be disabled, this is not
1789 * guaranteed. However, all our work happens inside the NAPI callback,
1790 * so no locking is required.
1791 */
1792static void efx_netpoll(struct net_device *net_dev)
1793{
Ben Hutchings767e4682008-09-01 12:43:14 +01001794 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001795 struct efx_channel *channel;
1796
Ben Hutchings64ee3122008-09-01 12:47:38 +01001797 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001798 efx_schedule_channel(channel);
1799}
1800
1801#endif
1802
1803/**************************************************************************
1804 *
1805 * Kernel net device interface
1806 *
1807 *************************************************************************/
1808
1809/* Context: process, rtnl_lock() held. */
1810static int efx_net_open(struct net_device *net_dev)
1811{
Ben Hutchings767e4682008-09-01 12:43:14 +01001812 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001813 EFX_ASSERT_RESET_SERIALISED(efx);
1814
Ben Hutchings62776d02010-06-23 11:30:07 +00001815 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1816 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001817
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001818 if (efx->state == STATE_DISABLED)
1819 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001820 if (efx->phy_mode & PHY_MODE_SPECIAL)
1821 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001822 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1823 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001824
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001825 /* Notify the kernel of the link state polled during driver load,
1826 * before the monitor starts running */
1827 efx_link_status_changed(efx);
1828
Ben Hutchings8ceee662008-04-27 12:55:59 +01001829 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001830 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001831 return 0;
1832}
1833
1834/* Context: process, rtnl_lock() held.
1835 * Note that the kernel will ignore our return code; this method
1836 * should really be a void.
1837 */
1838static int efx_net_stop(struct net_device *net_dev)
1839{
Ben Hutchings767e4682008-09-01 12:43:14 +01001840 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001841
Ben Hutchings62776d02010-06-23 11:30:07 +00001842 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1843 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001844
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001845 if (efx->state != STATE_DISABLED) {
1846 /* Stop the device and flush all the channels */
1847 efx_stop_all(efx);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001848 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001849
1850 return 0;
1851}
1852
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001853/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001854static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1855 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001856{
Ben Hutchings767e4682008-09-01 12:43:14 +01001857 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001858 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001859
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001860 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001861
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001862 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001863
1864 stats->rx_packets = mac_stats->rx_packets;
1865 stats->tx_packets = mac_stats->tx_packets;
1866 stats->rx_bytes = mac_stats->rx_bytes;
1867 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001868 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001869 stats->multicast = mac_stats->rx_multicast;
1870 stats->collisions = mac_stats->tx_collision;
1871 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1872 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001873 stats->rx_crc_errors = mac_stats->rx_bad;
1874 stats->rx_frame_errors = mac_stats->rx_align_error;
1875 stats->rx_fifo_errors = mac_stats->rx_overflow;
1876 stats->rx_missed_errors = mac_stats->rx_missed;
1877 stats->tx_window_errors = mac_stats->tx_late_collision;
1878
1879 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001880 stats->rx_crc_errors +
1881 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001882 mac_stats->rx_symbol_error);
1883 stats->tx_errors = (stats->tx_window_errors +
1884 mac_stats->tx_bad);
1885
Ben Hutchings1cb34522011-09-02 23:23:00 +01001886 spin_unlock_bh(&efx->stats_lock);
1887
Ben Hutchings8ceee662008-04-27 12:55:59 +01001888 return stats;
1889}
1890
1891/* Context: netif_tx_lock held, BHs disabled. */
1892static void efx_watchdog(struct net_device *net_dev)
1893{
Ben Hutchings767e4682008-09-01 12:43:14 +01001894 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001895
Ben Hutchings62776d02010-06-23 11:30:07 +00001896 netif_err(efx, tx_err, efx->net_dev,
1897 "TX stuck with port_enabled=%d: resetting channels\n",
1898 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001899
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001900 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001901}
1902
1903
1904/* Context: process, rtnl_lock() held. */
1905static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1906{
Ben Hutchings767e4682008-09-01 12:43:14 +01001907 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001908
1909 EFX_ASSERT_RESET_SERIALISED(efx);
1910
1911 if (new_mtu > EFX_MAX_MTU)
1912 return -EINVAL;
1913
1914 efx_stop_all(efx);
1915
Ben Hutchings62776d02010-06-23 11:30:07 +00001916 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001917
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001918 mutex_lock(&efx->mac_lock);
1919 /* Reconfigure the MAC before enabling the dma queues so that
1920 * the RX buffers don't overflow */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001921 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01001922 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001923 mutex_unlock(&efx->mac_lock);
1924
Ben Hutchings8ceee662008-04-27 12:55:59 +01001925 efx_start_all(efx);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00001926 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001927}
1928
1929static int efx_set_mac_address(struct net_device *net_dev, void *data)
1930{
Ben Hutchings767e4682008-09-01 12:43:14 +01001931 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001932 struct sockaddr *addr = data;
1933 char *new_addr = addr->sa_data;
1934
1935 EFX_ASSERT_RESET_SERIALISED(efx);
1936
1937 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001938 netif_err(efx, drv, efx->net_dev,
1939 "invalid ethernet MAC address requested: %pM\n",
1940 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00001941 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001942 }
1943
1944 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001945 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001946
1947 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001948 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01001949 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001950 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001951
1952 return 0;
1953}
1954
Ben Hutchingsa816f752008-09-01 12:49:12 +01001955/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00001956static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001957{
Ben Hutchings767e4682008-09-01 12:43:14 +01001958 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001959 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001960 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001961 u32 crc;
1962 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001963
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001964 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001965
1966 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001967 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001968 memset(mc_hash, 0xff, sizeof(*mc_hash));
1969 } else {
1970 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00001971 netdev_for_each_mc_addr(ha, net_dev) {
1972 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001973 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1974 set_bit_le(bit, mc_hash->byte);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001975 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001976
1977 /* Broadcast packets go through the multicast hash filter.
1978 * ether_crc_le() of the broadcast address is 0xbe2612ff
1979 * so we always add bit 0xff to the mask.
1980 */
1981 set_bit_le(0xff, mc_hash->byte);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001982 }
1983
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001984 if (efx->port_enabled)
1985 queue_work(efx->workqueue, &efx->mac_work);
1986 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001987}
1988
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001989static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01001990{
1991 struct efx_nic *efx = netdev_priv(net_dev);
1992
1993 /* If disabling RX n-tuple filtering, clear existing filters */
1994 if (net_dev->features & ~data & NETIF_F_NTUPLE)
1995 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
1996
1997 return 0;
1998}
1999
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002000static const struct net_device_ops efx_netdev_ops = {
2001 .ndo_open = efx_net_open,
2002 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002003 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002004 .ndo_tx_timeout = efx_watchdog,
2005 .ndo_start_xmit = efx_hard_start_xmit,
2006 .ndo_validate_addr = eth_validate_addr,
2007 .ndo_do_ioctl = efx_ioctl,
2008 .ndo_change_mtu = efx_change_mtu,
2009 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002010 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002011 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002012#ifdef CONFIG_SFC_SRIOV
2013 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2014 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2015 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2016 .ndo_get_vf_config = efx_sriov_get_vf_config,
2017#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002018#ifdef CONFIG_NET_POLL_CONTROLLER
2019 .ndo_poll_controller = efx_netpoll,
2020#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002021 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002022#ifdef CONFIG_RFS_ACCEL
2023 .ndo_rx_flow_steer = efx_filter_rfs,
2024#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002025};
2026
Ben Hutchings7dde5962008-12-12 22:09:38 -08002027static void efx_update_name(struct efx_nic *efx)
2028{
2029 strcpy(efx->name, efx->net_dev->name);
2030 efx_mtd_rename(efx);
2031 efx_set_channel_names(efx);
2032}
2033
Ben Hutchings8ceee662008-04-27 12:55:59 +01002034static int efx_netdev_event(struct notifier_block *this,
2035 unsigned long event, void *ptr)
2036{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01002037 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002038
Ben Hutchings7dde5962008-12-12 22:09:38 -08002039 if (net_dev->netdev_ops == &efx_netdev_ops &&
2040 event == NETDEV_CHANGENAME)
2041 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002042
2043 return NOTIFY_DONE;
2044}
2045
2046static struct notifier_block efx_netdev_notifier = {
2047 .notifier_call = efx_netdev_event,
2048};
2049
Ben Hutchings06d5e192008-12-12 21:47:23 -08002050static ssize_t
2051show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2052{
2053 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2054 return sprintf(buf, "%d\n", efx->phy_type);
2055}
2056static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
2057
Ben Hutchings8ceee662008-04-27 12:55:59 +01002058static int efx_register_netdev(struct efx_nic *efx)
2059{
2060 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002061 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002062 int rc;
2063
2064 net_dev->watchdog_timeo = 5 * HZ;
2065 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002066 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002067 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
2068
Ben Hutchings7dde5962008-12-12 22:09:38 -08002069 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002070
2071 rc = dev_alloc_name(net_dev, net_dev->name);
2072 if (rc < 0)
2073 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002074 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002075
2076 rc = register_netdevice(net_dev);
2077 if (rc)
2078 goto fail_locked;
2079
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002080 efx_for_each_channel(channel, efx) {
2081 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002082 efx_for_each_channel_tx_queue(tx_queue, channel)
2083 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002084 }
2085
Ben Hutchingsaed06282009-08-26 08:16:27 +00002086 /* Always start with carrier off; PHY events will detect the link */
Ben Hutchings86ee5302012-01-09 19:51:22 +00002087 netif_carrier_off(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002088
Ben Hutchings7dde5962008-12-12 22:09:38 -08002089 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002090
Ben Hutchings06d5e192008-12-12 21:47:23 -08002091 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2092 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002093 netif_err(efx, drv, efx->net_dev,
2094 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002095 goto fail_registered;
2096 }
2097
Ben Hutchings8ceee662008-04-27 12:55:59 +01002098 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002099
Ben Hutchingsaed06282009-08-26 08:16:27 +00002100fail_locked:
2101 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002102 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002103 return rc;
2104
Ben Hutchings06d5e192008-12-12 21:47:23 -08002105fail_registered:
2106 unregister_netdev(net_dev);
2107 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002108}
2109
2110static void efx_unregister_netdev(struct efx_nic *efx)
2111{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002112 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002113 struct efx_tx_queue *tx_queue;
2114
2115 if (!efx->net_dev)
2116 return;
2117
Ben Hutchings767e4682008-09-01 12:43:14 +01002118 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002119
2120 /* Free up any skbs still remaining. This has to happen before
2121 * we try to unregister the netdev as running their destructors
2122 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002123 efx_for_each_channel(channel, efx) {
2124 efx_for_each_channel_tx_queue(tx_queue, channel)
2125 efx_release_tx_buffers(tx_queue);
2126 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002127
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002128 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2129 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2130 unregister_netdev(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002131}
2132
2133/**************************************************************************
2134 *
2135 * Device reset and suspend
2136 *
2137 **************************************************************************/
2138
Ben Hutchings2467ca42008-09-01 12:48:50 +01002139/* Tears down the entire software state and most of the hardware state
2140 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002141void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002142{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002143 EFX_ASSERT_RESET_SERIALISED(efx);
2144
Ben Hutchings2467ca42008-09-01 12:48:50 +01002145 efx_stop_all(efx);
2146 mutex_lock(&efx->mac_lock);
2147
Ben Hutchings7f967c02012-02-13 23:45:02 +00002148 efx_stop_interrupts(efx, false);
Steve Hodgson4b988282009-01-29 17:50:51 +00002149 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2150 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002151 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002152}
2153
Ben Hutchings2467ca42008-09-01 12:48:50 +01002154/* This function will always ensure that the locks acquired in
2155 * efx_reset_down() are released. A failure return code indicates
2156 * that we were unable to reinitialise the hardware, and the
2157 * driver should be disabled. If ok is false, then the rx and tx
2158 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002159int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002160{
2161 int rc;
2162
Ben Hutchings2467ca42008-09-01 12:48:50 +01002163 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002164
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002165 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002166 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002167 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002168 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002169 }
2170
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002171 if (!ok)
2172 goto fail;
2173
Steve Hodgson4b988282009-01-29 17:50:51 +00002174 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002175 rc = efx->phy_op->init(efx);
2176 if (rc)
2177 goto fail;
2178 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002179 netif_err(efx, drv, efx->net_dev,
2180 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002181 }
2182
Ben Hutchings710b2082011-09-03 00:15:00 +01002183 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002184
Ben Hutchings7f967c02012-02-13 23:45:02 +00002185 efx_start_interrupts(efx, false);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002186 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002187 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002188
2189 mutex_unlock(&efx->mac_lock);
2190
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002191 efx_start_all(efx);
2192
2193 return 0;
2194
2195fail:
2196 efx->port_initialized = false;
2197
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002198 mutex_unlock(&efx->mac_lock);
2199
Ben Hutchings8ceee662008-04-27 12:55:59 +01002200 return rc;
2201}
2202
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002203/* Reset the NIC using the specified method. Note that the reset may
2204 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002205 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002206 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002207 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002208int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002209{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002210 int rc, rc2;
2211 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002212
Ben Hutchings62776d02010-06-23 11:30:07 +00002213 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2214 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002215
Ben Hutchingse4abce82011-05-16 18:51:24 +01002216 netif_device_detach(efx->net_dev);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002217 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002218
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002219 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002220 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002221 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002222 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002223 }
2224
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002225 /* Clear flags for the scopes we covered. We assume the NIC and
2226 * driver are now quiescent so that there is no race here.
2227 */
2228 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002229
2230 /* Reinitialise bus-mastering, which may have been turned off before
2231 * the reset was scheduled. This is still appropriate, even in the
2232 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2233 * can respond to requests. */
2234 pci_set_master(efx->pci_dev);
2235
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002236out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002237 /* Leave device stopped if necessary */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002238 disabled = rc || method == RESET_TYPE_DISABLE;
2239 rc2 = efx_reset_up(efx, method, !disabled);
2240 if (rc2) {
2241 disabled = true;
2242 if (!rc)
2243 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002244 }
2245
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002246 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002247 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002248 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002249 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002250 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002251 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002252 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002253 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002254 return rc;
2255}
2256
2257/* The worker thread exists so that code that cannot sleep can
2258 * schedule a reset for later.
2259 */
2260static void efx_reset_work(struct work_struct *data)
2261{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002262 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002263 unsigned long pending = ACCESS_ONCE(efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002264
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002265 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002266 return;
2267
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002268 /* If we're not RUNNING then don't reset. Leave the reset_pending
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002269 * flags set so that efx_pci_probe_main will be retried */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002270 if (efx->state != STATE_RUNNING) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002271 netif_info(efx, drv, efx->net_dev,
2272 "scheduled reset quenched. NIC not RUNNING\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002273 return;
2274 }
2275
2276 rtnl_lock();
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002277 (void)efx_reset(efx, fls(pending) - 1);
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002278 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002279}
2280
2281void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2282{
2283 enum reset_type method;
2284
Ben Hutchings8ceee662008-04-27 12:55:59 +01002285 switch (type) {
2286 case RESET_TYPE_INVISIBLE:
2287 case RESET_TYPE_ALL:
2288 case RESET_TYPE_WORLD:
2289 case RESET_TYPE_DISABLE:
2290 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002291 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2292 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002293 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002294 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002295 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002296 netif_dbg(efx, drv, efx->net_dev,
2297 "scheduling %s reset for %s\n",
2298 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002299 break;
2300 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002301
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002302 set_bit(method, &efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002303
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002304 /* efx_process_channel() will no longer read events once a
2305 * reset is scheduled. So switch back to poll'd MCDI completions. */
2306 efx_mcdi_mode_poll(efx);
2307
Steve Hodgson1ab00622008-12-12 21:33:02 -08002308 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002309}
2310
2311/**************************************************************************
2312 *
2313 * List of NICs we support
2314 *
2315 **************************************************************************/
2316
2317/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002318static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002319 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2320 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002321 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002322 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2323 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002324 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002325 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002326 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002327 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002328 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002329 {0} /* end of list */
2330};
2331
2332/**************************************************************************
2333 *
Ben Hutchings37594332009-11-23 16:05:45 +00002334 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002335 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002336 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002337 * Needed so all function pointers are valid and do not have to be tested
2338 * before use
2339 *
2340 **************************************************************************/
2341int efx_port_dummy_op_int(struct efx_nic *efx)
2342{
2343 return 0;
2344}
2345void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002346
2347static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002348{
2349 return false;
2350}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002351
stephen hemminger6c8c2512011-04-14 05:50:12 +00002352static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002353 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002354 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002355 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002356 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002357};
2358
Ben Hutchings8ceee662008-04-27 12:55:59 +01002359/**************************************************************************
2360 *
2361 * Data housekeeping
2362 *
2363 **************************************************************************/
2364
2365/* This zeroes out and then fills in the invariants in a struct
2366 * efx_nic (including all sub-structures).
2367 */
stephen hemminger6c8c2512011-04-14 05:50:12 +00002368static int efx_init_struct(struct efx_nic *efx, const struct efx_nic_type *type,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002369 struct pci_dev *pci_dev, struct net_device *net_dev)
2370{
Ben Hutchings46426102010-09-10 06:42:33 +00002371 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002372
2373 /* Initialise common structures */
2374 memset(efx, 0, sizeof(*efx));
2375 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002376#ifdef CONFIG_SFC_MTD
2377 INIT_LIST_HEAD(&efx->mtd_list);
2378#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002379 INIT_WORK(&efx->reset_work, efx_reset_work);
2380 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002381 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002382 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002383 efx->msg_enable = debug;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002384 efx->state = STATE_INIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002385 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002386
2387 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002388 spin_lock_init(&efx->stats_lock);
2389 mutex_init(&efx->mac_lock);
2390 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002391 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002392 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002393 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002394
2395 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002396 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2397 if (!efx->channel[i])
2398 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002399 }
2400
2401 efx->type = type;
2402
Ben Hutchings8ceee662008-04-27 12:55:59 +01002403 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2404
2405 /* Higher numbered interrupt modes are less capable! */
2406 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2407 interrupt_mode);
2408
Ben Hutchings6977dc62008-12-26 13:44:39 -08002409 /* Would be good to use the net_dev name, but we're too early */
2410 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2411 pci_name(pci_dev));
2412 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002413 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002414 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002415
Ben Hutchings8ceee662008-04-27 12:55:59 +01002416 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002417
2418fail:
2419 efx_fini_struct(efx);
2420 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002421}
2422
2423static void efx_fini_struct(struct efx_nic *efx)
2424{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002425 int i;
2426
2427 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2428 kfree(efx->channel[i]);
2429
Ben Hutchings8ceee662008-04-27 12:55:59 +01002430 if (efx->workqueue) {
2431 destroy_workqueue(efx->workqueue);
2432 efx->workqueue = NULL;
2433 }
2434}
2435
2436/**************************************************************************
2437 *
2438 * PCI interface
2439 *
2440 **************************************************************************/
2441
2442/* Main body of final NIC shutdown code
2443 * This is called only at module unload (or hotplug removal).
2444 */
2445static void efx_pci_remove_main(struct efx_nic *efx)
2446{
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002447#ifdef CONFIG_RFS_ACCEL
2448 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2449 efx->net_dev->rx_cpu_rmap = NULL;
2450#endif
Ben Hutchings7f967c02012-02-13 23:45:02 +00002451 efx_stop_interrupts(efx, false);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002452 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002453 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002454 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002455 efx_fini_napi(efx);
2456 efx_remove_all(efx);
2457}
2458
2459/* Final NIC shutdown
2460 * This is called only at module unload (or hotplug removal).
2461 */
2462static void efx_pci_remove(struct pci_dev *pci_dev)
2463{
2464 struct efx_nic *efx;
2465
2466 efx = pci_get_drvdata(pci_dev);
2467 if (!efx)
2468 return;
2469
2470 /* Mark the NIC as fini, then stop the interface */
2471 rtnl_lock();
2472 efx->state = STATE_FINI;
2473 dev_close(efx->net_dev);
2474
2475 /* Allow any queued efx_resets() to complete */
2476 rtnl_unlock();
2477
Ben Hutchings7f967c02012-02-13 23:45:02 +00002478 efx_stop_interrupts(efx, false);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002479 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002480 efx_unregister_netdev(efx);
2481
Ben Hutchings7dde5962008-12-12 22:09:38 -08002482 efx_mtd_remove(efx);
2483
Ben Hutchings8ceee662008-04-27 12:55:59 +01002484 /* Wait for any scheduled resets to complete. No more will be
2485 * scheduled from this point because efx_stop_all() has been
2486 * called, we are no longer registered with driverlink, and
2487 * the net_device's have been removed. */
Steve Hodgson1ab00622008-12-12 21:33:02 -08002488 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002489
2490 efx_pci_remove_main(efx);
2491
Ben Hutchings8ceee662008-04-27 12:55:59 +01002492 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002493 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002494
2495 pci_set_drvdata(pci_dev, NULL);
2496 efx_fini_struct(efx);
2497 free_netdev(efx->net_dev);
2498};
2499
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002500/* NIC VPD information
2501 * Called during probe to display the part number of the
2502 * installed NIC. VPD is potentially very large but this should
2503 * always appear within the first 512 bytes.
2504 */
2505#define SFC_VPD_LEN 512
2506static void efx_print_product_vpd(struct efx_nic *efx)
2507{
2508 struct pci_dev *dev = efx->pci_dev;
2509 char vpd_data[SFC_VPD_LEN];
2510 ssize_t vpd_size;
2511 int i, j;
2512
2513 /* Get the vpd data from the device */
2514 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2515 if (vpd_size <= 0) {
2516 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2517 return;
2518 }
2519
2520 /* Get the Read only section */
2521 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2522 if (i < 0) {
2523 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2524 return;
2525 }
2526
2527 j = pci_vpd_lrdt_size(&vpd_data[i]);
2528 i += PCI_VPD_LRDT_TAG_SIZE;
2529 if (i + j > vpd_size)
2530 j = vpd_size - i;
2531
2532 /* Get the Part number */
2533 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2534 if (i < 0) {
2535 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2536 return;
2537 }
2538
2539 j = pci_vpd_info_field_size(&vpd_data[i]);
2540 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2541 if (i + j > vpd_size) {
2542 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2543 return;
2544 }
2545
2546 netif_info(efx, drv, efx->net_dev,
2547 "Part Number : %.*s\n", j, &vpd_data[i]);
2548}
2549
2550
Ben Hutchings8ceee662008-04-27 12:55:59 +01002551/* Main body of NIC initialisation
2552 * This is called at module load (or hotplug insertion, theoretically).
2553 */
2554static int efx_pci_probe_main(struct efx_nic *efx)
2555{
2556 int rc;
2557
2558 /* Do start-of-day initialisation */
2559 rc = efx_probe_all(efx);
2560 if (rc)
2561 goto fail1;
2562
Ben Hutchingse8f14992010-12-07 19:47:34 +00002563 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002564
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002565 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002566 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002567 netif_err(efx, probe, efx->net_dev,
2568 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002569 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002570 }
2571
2572 rc = efx_init_port(efx);
2573 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002574 netif_err(efx, probe, efx->net_dev,
2575 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002576 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002577 }
2578
Ben Hutchings152b6a62009-11-29 03:43:56 +00002579 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002580 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002581 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00002582 efx_start_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002583
2584 return 0;
2585
Ben Hutchings278c0622009-11-23 16:05:12 +00002586 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002587 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002588 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002589 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002590 fail3:
2591 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002592 efx_remove_all(efx);
2593 fail1:
2594 return rc;
2595}
2596
2597/* NIC initialisation
2598 *
2599 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002600 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002601 * sets up and registers the network devices with the kernel and hooks
2602 * the interrupt service routine. It does not prepare the device for
2603 * transmission; this is left to the first time one of the network
2604 * interfaces is brought up (i.e. efx_net_open).
2605 */
2606static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2607 const struct pci_device_id *entry)
2608{
stephen hemminger6c8c2512011-04-14 05:50:12 +00002609 const struct efx_nic_type *type = (const struct efx_nic_type *) entry->driver_data;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002610 struct net_device *net_dev;
2611 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002612 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002613
2614 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002615 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2616 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002617 if (!net_dev)
2618 return -ENOMEM;
Ben Hutchingsc383b532009-11-29 15:11:02 +00002619 net_dev->features |= (type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002620 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002621 NETIF_F_RXCSUM);
Ben Hutchings738a8f42009-11-29 15:16:05 +00002622 if (type->offload_features & NETIF_F_V6_CSUM)
2623 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002624 /* Mask for features that also apply to VLAN devices */
2625 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002626 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2627 NETIF_F_RXCSUM);
2628 /* All offloads can be toggled */
2629 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings767e4682008-09-01 12:43:14 +01002630 efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002631 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002632 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002633 rc = efx_init_struct(efx, type, pci_dev, net_dev);
2634 if (rc)
2635 goto fail1;
2636
Ben Hutchings62776d02010-06-23 11:30:07 +00002637 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002638 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002639
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002640 efx_print_product_vpd(efx);
2641
Ben Hutchings8ceee662008-04-27 12:55:59 +01002642 /* Set up basic I/O (BAR mappings etc) */
2643 rc = efx_init_io(efx);
2644 if (rc)
2645 goto fail2;
2646
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002647 rc = efx_pci_probe_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002648
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002649 /* Serialise against efx_reset(). No more resets will be
2650 * scheduled since efx_stop_all() has been called, and we have
2651 * not and never have been registered.
2652 */
2653 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002654
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002655 if (rc)
2656 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002657
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002658 /* If there was a scheduled reset during probe, the NIC is
2659 * probably hosed anyway.
2660 */
2661 if (efx->reset_pending) {
2662 rc = -EIO;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002663 goto fail4;
2664 }
2665
Ben Hutchings55edc6e2009-11-25 16:11:35 +00002666 /* Switch to the running state before we expose the device to the OS,
2667 * so that dev_open()|efx_start_all() will actually start the device */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002668 efx->state = STATE_RUNNING;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002669
Ben Hutchings8ceee662008-04-27 12:55:59 +01002670 rc = efx_register_netdev(efx);
2671 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002672 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002673
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002674 rc = efx_sriov_init(efx);
2675 if (rc)
2676 netif_err(efx, probe, efx->net_dev,
2677 "SR-IOV can't be enabled rc %d\n", rc);
2678
Ben Hutchings62776d02010-06-23 11:30:07 +00002679 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002680
Ben Hutchings7c431612012-01-27 17:23:58 +00002681 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002682 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002683 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002684 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002685 if (rc)
2686 netif_warn(efx, probe, efx->net_dev,
2687 "failed to create MTDs (%d)\n", rc);
2688
Ben Hutchings8ceee662008-04-27 12:55:59 +01002689 return 0;
2690
Ben Hutchings8ceee662008-04-27 12:55:59 +01002691 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002692 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002693 fail3:
2694 efx_fini_io(efx);
2695 fail2:
2696 efx_fini_struct(efx);
2697 fail1:
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002698 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002699 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002700 free_netdev(net_dev);
2701 return rc;
2702}
2703
Ben Hutchings89c758f2009-11-29 03:43:07 +00002704static int efx_pm_freeze(struct device *dev)
2705{
2706 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2707
2708 efx->state = STATE_FINI;
2709
2710 netif_device_detach(efx->net_dev);
2711
2712 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00002713 efx_stop_interrupts(efx, false);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002714
2715 return 0;
2716}
2717
2718static int efx_pm_thaw(struct device *dev)
2719{
2720 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2721
2722 efx->state = STATE_INIT;
2723
Ben Hutchings7f967c02012-02-13 23:45:02 +00002724 efx_start_interrupts(efx, false);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002725
2726 mutex_lock(&efx->mac_lock);
2727 efx->phy_op->reconfigure(efx);
2728 mutex_unlock(&efx->mac_lock);
2729
2730 efx_start_all(efx);
2731
2732 netif_device_attach(efx->net_dev);
2733
2734 efx->state = STATE_RUNNING;
2735
2736 efx->type->resume_wol(efx);
2737
Steve Hodgson319ba642010-06-01 11:17:24 +00002738 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2739 queue_work(reset_workqueue, &efx->reset_work);
2740
Ben Hutchings89c758f2009-11-29 03:43:07 +00002741 return 0;
2742}
2743
2744static int efx_pm_poweroff(struct device *dev)
2745{
2746 struct pci_dev *pci_dev = to_pci_dev(dev);
2747 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2748
2749 efx->type->fini(efx);
2750
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002751 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002752
2753 pci_save_state(pci_dev);
2754 return pci_set_power_state(pci_dev, PCI_D3hot);
2755}
2756
2757/* Used for both resume and restore */
2758static int efx_pm_resume(struct device *dev)
2759{
2760 struct pci_dev *pci_dev = to_pci_dev(dev);
2761 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2762 int rc;
2763
2764 rc = pci_set_power_state(pci_dev, PCI_D0);
2765 if (rc)
2766 return rc;
2767 pci_restore_state(pci_dev);
2768 rc = pci_enable_device(pci_dev);
2769 if (rc)
2770 return rc;
2771 pci_set_master(efx->pci_dev);
2772 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2773 if (rc)
2774 return rc;
2775 rc = efx->type->init(efx);
2776 if (rc)
2777 return rc;
2778 efx_pm_thaw(dev);
2779 return 0;
2780}
2781
2782static int efx_pm_suspend(struct device *dev)
2783{
2784 int rc;
2785
2786 efx_pm_freeze(dev);
2787 rc = efx_pm_poweroff(dev);
2788 if (rc)
2789 efx_pm_resume(dev);
2790 return rc;
2791}
2792
Ben Hutchings18e83e42012-01-05 19:05:20 +00002793static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002794 .suspend = efx_pm_suspend,
2795 .resume = efx_pm_resume,
2796 .freeze = efx_pm_freeze,
2797 .thaw = efx_pm_thaw,
2798 .poweroff = efx_pm_poweroff,
2799 .restore = efx_pm_resume,
2800};
2801
Ben Hutchings8ceee662008-04-27 12:55:59 +01002802static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00002803 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002804 .id_table = efx_pci_table,
2805 .probe = efx_pci_probe,
2806 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00002807 .driver.pm = &efx_pm_ops,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002808};
2809
2810/**************************************************************************
2811 *
2812 * Kernel module interface
2813 *
2814 *************************************************************************/
2815
2816module_param(interrupt_mode, uint, 0444);
2817MODULE_PARM_DESC(interrupt_mode,
2818 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2819
2820static int __init efx_init_module(void)
2821{
2822 int rc;
2823
2824 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2825
2826 rc = register_netdevice_notifier(&efx_netdev_notifier);
2827 if (rc)
2828 goto err_notifier;
2829
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002830 rc = efx_init_sriov();
2831 if (rc)
2832 goto err_sriov;
2833
Steve Hodgson1ab00622008-12-12 21:33:02 -08002834 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2835 if (!reset_workqueue) {
2836 rc = -ENOMEM;
2837 goto err_reset;
2838 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002839
2840 rc = pci_register_driver(&efx_pci_driver);
2841 if (rc < 0)
2842 goto err_pci;
2843
2844 return 0;
2845
2846 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08002847 destroy_workqueue(reset_workqueue);
2848 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002849 efx_fini_sriov();
2850 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002851 unregister_netdevice_notifier(&efx_netdev_notifier);
2852 err_notifier:
2853 return rc;
2854}
2855
2856static void __exit efx_exit_module(void)
2857{
2858 printk(KERN_INFO "Solarflare NET driver unloading\n");
2859
2860 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002861 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002862 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002863 unregister_netdevice_notifier(&efx_netdev_notifier);
2864
2865}
2866
2867module_init(efx_init_module);
2868module_exit(efx_exit_module);
2869
Ben Hutchings906bb262009-11-29 15:16:19 +00002870MODULE_AUTHOR("Solarflare Communications and "
2871 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002872MODULE_DESCRIPTION("Solarflare Communications network driver");
2873MODULE_LICENSE("GPL");
2874MODULE_DEVICE_TABLE(pci, efx_pci_table);