blob: 4a14a940c65e70f052a83bd7384722a4dbe4e768 [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>
Alexandre Rames626950d2013-01-14 17:20:22 +000025#include <linux/aer.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010027#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000028#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000029#include "selftest.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010030
Ben Hutchings8880f4e2009-11-29 15:15:41 +000031#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000032#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000033
Ben Hutchingsc4593022009-11-23 16:08:17 +000034/**************************************************************************
35 *
36 * Type name strings
37 *
38 **************************************************************************
39 */
40
41/* Loopback mode names (see LOOPBACK_MODE()) */
42const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000043const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000044 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000045 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000046 [LOOPBACK_GMAC] = "GMAC",
47 [LOOPBACK_XGMII] = "XGMII",
48 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000049 [LOOPBACK_XAUI] = "XAUI",
50 [LOOPBACK_GMII] = "GMII",
51 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000052 [LOOPBACK_XGBR] = "XGBR",
53 [LOOPBACK_XFI] = "XFI",
54 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
55 [LOOPBACK_GMII_FAR] = "GMII_FAR",
56 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
57 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000058 [LOOPBACK_GPHY] = "GPHY",
59 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000060 [LOOPBACK_PCS] = "PCS",
61 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000062 [LOOPBACK_XPORT] = "XPORT",
63 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000064 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000065 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
66 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000067 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000068 [LOOPBACK_XFI_WS] = "XFI_WS",
69 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000070 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000071};
72
Ben Hutchingsc4593022009-11-23 16:08:17 +000073const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000074const char *const efx_reset_type_names[] = {
Alexandre Rames626950d2013-01-14 17:20:22 +000075 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
76 [RESET_TYPE_ALL] = "ALL",
77 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
78 [RESET_TYPE_WORLD] = "WORLD",
79 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
80 [RESET_TYPE_DISABLE] = "DISABLE",
81 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
82 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
83 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
84 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
85 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
86 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
87 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Ben Hutchingsc4593022009-11-23 16:08:17 +000088};
89
Steve Hodgson1ab00622008-12-12 21:33:02 -080090/* Reset workqueue. If any NIC has a hardware failure then a reset will be
91 * queued onto this work queue. This is not a per-nic work queue, because
92 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
93 */
94static struct workqueue_struct *reset_workqueue;
95
Ben Hutchings8ceee662008-04-27 12:55:59 +010096/**************************************************************************
97 *
98 * Configurable values
99 *
100 *************************************************************************/
101
102/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100103 * Use separate channels for TX and RX events
104 *
Neil Turton28b581a2008-12-12 21:41:06 -0800105 * Set this to 1 to use separate channels for TX and RX. It allows us
106 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100107 *
Neil Turton28b581a2008-12-12 21:41:06 -0800108 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100109 */
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000110static bool separate_tx_channels;
111module_param(separate_tx_channels, bool, 0444);
Neil Turton28b581a2008-12-12 21:41:06 -0800112MODULE_PARM_DESC(separate_tx_channels,
113 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100114
115/* This is the weight assigned to each of the (per-channel) virtual
116 * NAPI devices.
117 */
118static int napi_weight = 64;
119
120/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000121 * monitor.
122 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000123 * - Check the on-board hardware monitor;
124 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000125 * On Siena-based NICs for power systems with EEH support, this will give EEH a
126 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100127 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000128static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100129
Ben Hutchings8ceee662008-04-27 12:55:59 +0100130/* Initial interrupt moderation settings. They can be modified after
131 * module load with ethtool.
132 *
133 * The default for RX should strike a balance between increasing the
134 * round-trip latency and reducing overhead.
135 */
136static unsigned int rx_irq_mod_usec = 60;
137
138/* Initial interrupt moderation settings. They can be modified after
139 * module load with ethtool.
140 *
141 * This default is chosen to ensure that a 10G link does not go idle
142 * while a TX queue is stopped after it has become full. A queue is
143 * restarted when it drops below half full. The time this takes (assuming
144 * worst case 3 descriptors per packet and 1024 descriptors) is
145 * 512 / 3 * 1.2 = 205 usec.
146 */
147static unsigned int tx_irq_mod_usec = 150;
148
149/* This is the first interrupt mode to try out of:
150 * 0 => MSI-X
151 * 1 => MSI
152 * 2 => legacy
153 */
154static unsigned int interrupt_mode;
155
156/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
157 * i.e. the number of CPUs among which we may distribute simultaneous
158 * interrupt handling.
159 *
160 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000161 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100162 */
163static unsigned int rss_cpus;
164module_param(rss_cpus, uint, 0444);
165MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
166
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000167static bool phy_flash_cfg;
168module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800169MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
170
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000171static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000172module_param(irq_adapt_low_thresh, uint, 0644);
173MODULE_PARM_DESC(irq_adapt_low_thresh,
174 "Threshold score for reducing IRQ moderation");
175
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000176static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000177module_param(irq_adapt_high_thresh, uint, 0644);
178MODULE_PARM_DESC(irq_adapt_high_thresh,
179 "Threshold score for increasing IRQ moderation");
180
Ben Hutchings62776d02010-06-23 11:30:07 +0000181static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
182 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
183 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
184 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
185module_param(debug, uint, 0);
186MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
187
Ben Hutchings8ceee662008-04-27 12:55:59 +0100188/**************************************************************************
189 *
190 * Utility functions and prototypes
191 *
192 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000193
Ben Hutchings7f967c02012-02-13 23:45:02 +0000194static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq);
195static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq);
196static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000197static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000198static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100199static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000200static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100201static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000202static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000203static void efx_fini_struct(struct efx_nic *efx);
204static void efx_start_all(struct efx_nic *efx);
205static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100206
207#define EFX_ASSERT_RESET_SERIALISED(efx) \
208 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100209 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000210 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000211 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100212 ASSERT_RTNL(); \
213 } while (0)
214
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100215static int efx_check_disabled(struct efx_nic *efx)
216{
Alexandre Rames626950d2013-01-14 17:20:22 +0000217 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100218 netif_err(efx, drv, efx->net_dev,
219 "device is disabled due to earlier errors\n");
220 return -EIO;
221 }
222 return 0;
223}
224
Ben Hutchings8ceee662008-04-27 12:55:59 +0100225/**************************************************************************
226 *
227 * Event queue processing
228 *
229 *************************************************************************/
230
231/* Process channel's event queue
232 *
233 * This function is responsible for processing the event queue of a
234 * single channel. The caller must guarantee that this function will
235 * never be concurrently called more than once on the same channel,
236 * though different channels may be being processed concurrently.
237 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000238static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100239{
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000240 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100241
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000242 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100243 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100244
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000245 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000246 if (spent && efx_channel_has_rx_queue(channel)) {
247 struct efx_rx_queue *rx_queue =
248 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100249
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000250 efx_rx_flush_packet(channel);
Alexandre Rames97d48a12013-01-11 12:26:21 +0000251 if (rx_queue->enabled)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000252 efx_fast_push_rx_descriptors(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100253 }
254
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000255 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100256}
257
258/* Mark channel as finished processing
259 *
260 * Note that since we will not receive further interrupts for this
261 * channel before we finish processing and call the eventq_read_ack()
262 * method, there is no need to use the interrupt hold-off timers.
263 */
264static inline void efx_channel_processed(struct efx_channel *channel)
265{
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100266 /* The interrupt handler for this channel may set work_pending
267 * as soon as we acknowledge the events we've seen. Make sure
268 * it's cleared before then. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100269 channel->work_pending = false;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100270 smp_wmb();
271
Ben Hutchings152b6a62009-11-29 03:43:56 +0000272 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100273}
274
275/* NAPI poll handler
276 *
277 * NAPI guarantees serialisation of polls of the same device, which
278 * provides the guarantee required by efx_process_channel().
279 */
280static int efx_poll(struct napi_struct *napi, int budget)
281{
282 struct efx_channel *channel =
283 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000284 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000285 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100286
Ben Hutchings62776d02010-06-23 11:30:07 +0000287 netif_vdbg(efx, intr, efx->net_dev,
288 "channel %d NAPI poll executing on CPU %d\n",
289 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100290
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000291 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100292
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000293 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000294 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000295 efx->irq_rx_adaptive &&
296 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000297 if (unlikely(channel->irq_mod_score <
298 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000299 if (channel->irq_moderation > 1) {
300 channel->irq_moderation -= 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000301 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000302 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000303 } else if (unlikely(channel->irq_mod_score >
304 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000305 if (channel->irq_moderation <
306 efx->irq_rx_moderation) {
307 channel->irq_moderation += 1;
Ben Hutchingsef2b90e2009-11-29 03:42:31 +0000308 efx->type->push_irq_moderation(channel);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000309 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000310 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000311 channel->irq_count = 0;
312 channel->irq_mod_score = 0;
313 }
314
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000315 efx_filter_rfs_expire(channel);
316
Ben Hutchings8ceee662008-04-27 12:55:59 +0100317 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800318 * only wait for napi_complete(), this isn't a problem
Ben Hutchings8ceee662008-04-27 12:55:59 +0100319 * since efx_channel_processed() will have no effect if
320 * interrupts have already been disabled.
321 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800322 napi_complete(napi);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100323 efx_channel_processed(channel);
324 }
325
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000326 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100327}
328
329/* Process the eventq of the specified channel immediately on this CPU
330 *
331 * Disable hardware generated interrupts, wait for any existing
332 * processing to finish, then directly poll (and ack ) the eventq.
333 * Finally reenable NAPI and interrupts.
334 *
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100335 * This is for use only during a loopback self-test. It must not
336 * deliver any packets up the stack as this can result in deadlock.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100337 */
338void efx_process_channel_now(struct efx_channel *channel)
339{
340 struct efx_nic *efx = channel->efx;
341
Ben Hutchings8313aca2010-09-10 06:41:57 +0000342 BUG_ON(channel->channel >= efx->n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100343 BUG_ON(!channel->enabled);
Ben Hutchingsd4fabcc2011-04-04 14:22:11 +0100344 BUG_ON(!efx->loopback_selftest);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100345
346 /* Disable interrupts and wait for ISRs to complete */
Ben Hutchings152b6a62009-11-29 03:43:56 +0000347 efx_nic_disable_interrupts(efx);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000348 if (efx->legacy_irq) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100349 synchronize_irq(efx->legacy_irq);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000350 efx->legacy_irq_enabled = false;
351 }
Ben Hutchings64ee3122008-09-01 12:47:38 +0100352 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100353 synchronize_irq(channel->irq);
354
355 /* Wait for any NAPI processing to complete */
356 napi_disable(&channel->napi_str);
357
358 /* Poll the channel */
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000359 efx_process_channel(channel, channel->eventq_mask + 1);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100360
361 /* Ack the eventq. This may cause an interrupt to be generated
362 * when they are reenabled */
363 efx_channel_processed(channel);
364
365 napi_enable(&channel->napi_str);
Ben Hutchings94dec6a2010-12-07 19:24:45 +0000366 if (efx->legacy_irq)
367 efx->legacy_irq_enabled = true;
Ben Hutchings152b6a62009-11-29 03:43:56 +0000368 efx_nic_enable_interrupts(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100369}
370
371/* Create event queue
372 * Event queue memory allocations are done only once. If the channel
373 * is reset, the memory buffer will be reused; this guards against
374 * errors during channel reset and also simplifies interrupt handling.
375 */
376static int efx_probe_eventq(struct efx_channel *channel)
377{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000378 struct efx_nic *efx = channel->efx;
379 unsigned long entries;
380
Ben Hutchings86ee5302012-01-09 19:51:22 +0000381 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000382 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100383
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000384 /* Build an event queue with room for one event per tx and rx buffer,
385 * plus some extra for link state events and MCDI completions. */
386 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
387 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
388 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
389
Ben Hutchings152b6a62009-11-29 03:43:56 +0000390 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100391}
392
393/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100394static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100395{
Ben Hutchings62776d02010-06-23 11:30:07 +0000396 netif_dbg(channel->efx, drv, channel->efx->net_dev,
397 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100398
399 channel->eventq_read_ptr = 0;
400
Ben Hutchings152b6a62009-11-29 03:43:56 +0000401 efx_nic_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100402}
403
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000404/* Enable event queue processing and NAPI */
405static void efx_start_eventq(struct efx_channel *channel)
406{
407 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
408 "chan %d start event queue\n", channel->channel);
409
410 /* The interrupt handler for this channel may set work_pending
411 * as soon as we enable it. Make sure it's cleared before
412 * then. Similarly, make sure it sees the enabled flag set.
413 */
414 channel->work_pending = false;
415 channel->enabled = true;
416 smp_wmb();
417
418 napi_enable(&channel->napi_str);
419 efx_nic_eventq_read_ack(channel);
420}
421
422/* Disable event queue processing and NAPI */
423static void efx_stop_eventq(struct efx_channel *channel)
424{
425 if (!channel->enabled)
426 return;
427
428 napi_disable(&channel->napi_str);
429 channel->enabled = false;
430}
431
Ben Hutchings8ceee662008-04-27 12:55:59 +0100432static void efx_fini_eventq(struct efx_channel *channel)
433{
Ben Hutchings62776d02010-06-23 11:30:07 +0000434 netif_dbg(channel->efx, drv, channel->efx->net_dev,
435 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100436
Ben Hutchings152b6a62009-11-29 03:43:56 +0000437 efx_nic_fini_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100438}
439
440static void efx_remove_eventq(struct efx_channel *channel)
441{
Ben Hutchings62776d02010-06-23 11:30:07 +0000442 netif_dbg(channel->efx, drv, channel->efx->net_dev,
443 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100444
Ben Hutchings152b6a62009-11-29 03:43:56 +0000445 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100446}
447
448/**************************************************************************
449 *
450 * Channel handling
451 *
452 *************************************************************************/
453
Ben Hutchings7f967c02012-02-13 23:45:02 +0000454/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000455static struct efx_channel *
456efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
457{
458 struct efx_channel *channel;
459 struct efx_rx_queue *rx_queue;
460 struct efx_tx_queue *tx_queue;
461 int j;
462
Ben Hutchings7f967c02012-02-13 23:45:02 +0000463 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
464 if (!channel)
465 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000466
Ben Hutchings7f967c02012-02-13 23:45:02 +0000467 channel->efx = efx;
468 channel->channel = i;
469 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000470
Ben Hutchings7f967c02012-02-13 23:45:02 +0000471 for (j = 0; j < EFX_TXQ_TYPES; j++) {
472 tx_queue = &channel->tx_queue[j];
473 tx_queue->efx = efx;
474 tx_queue->queue = i * EFX_TXQ_TYPES + j;
475 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000476 }
477
Ben Hutchings46426102010-09-10 06:42:33 +0000478 rx_queue = &channel->rx_queue;
479 rx_queue->efx = efx;
480 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
481 (unsigned long)rx_queue);
482
483 return channel;
484}
485
Ben Hutchings7f967c02012-02-13 23:45:02 +0000486/* Allocate and initialise a channel structure, copying parameters
487 * (but not resources) from an old channel structure.
488 */
489static struct efx_channel *
490efx_copy_channel(const struct efx_channel *old_channel)
491{
492 struct efx_channel *channel;
493 struct efx_rx_queue *rx_queue;
494 struct efx_tx_queue *tx_queue;
495 int j;
496
497 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
498 if (!channel)
499 return NULL;
500
501 *channel = *old_channel;
502
503 channel->napi_dev = NULL;
504 memset(&channel->eventq, 0, sizeof(channel->eventq));
505
506 for (j = 0; j < EFX_TXQ_TYPES; j++) {
507 tx_queue = &channel->tx_queue[j];
508 if (tx_queue->channel)
509 tx_queue->channel = channel;
510 tx_queue->buffer = NULL;
511 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
512 }
513
514 rx_queue = &channel->rx_queue;
515 rx_queue->buffer = NULL;
516 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
517 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
518 (unsigned long)rx_queue);
519
520 return channel;
521}
522
Ben Hutchings8ceee662008-04-27 12:55:59 +0100523static int efx_probe_channel(struct efx_channel *channel)
524{
525 struct efx_tx_queue *tx_queue;
526 struct efx_rx_queue *rx_queue;
527 int rc;
528
Ben Hutchings62776d02010-06-23 11:30:07 +0000529 netif_dbg(channel->efx, probe, channel->efx->net_dev,
530 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100531
Ben Hutchings7f967c02012-02-13 23:45:02 +0000532 rc = channel->type->pre_probe(channel);
533 if (rc)
534 goto fail;
535
Ben Hutchings8ceee662008-04-27 12:55:59 +0100536 rc = efx_probe_eventq(channel);
537 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000538 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100539
540 efx_for_each_channel_tx_queue(tx_queue, channel) {
541 rc = efx_probe_tx_queue(tx_queue);
542 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000543 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100544 }
545
546 efx_for_each_channel_rx_queue(rx_queue, channel) {
547 rc = efx_probe_rx_queue(rx_queue);
548 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000549 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100550 }
551
552 channel->n_rx_frm_trunc = 0;
553
554 return 0;
555
Ben Hutchings7f967c02012-02-13 23:45:02 +0000556fail:
557 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100558 return rc;
559}
560
Ben Hutchings7f967c02012-02-13 23:45:02 +0000561static void
562efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
563{
564 struct efx_nic *efx = channel->efx;
565 const char *type;
566 int number;
567
568 number = channel->channel;
569 if (efx->tx_channel_offset == 0) {
570 type = "";
571 } else if (channel->channel < efx->tx_channel_offset) {
572 type = "-rx";
573 } else {
574 type = "-tx";
575 number -= efx->tx_channel_offset;
576 }
577 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
578}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100579
Ben Hutchings56536e92008-12-12 21:37:02 -0800580static void efx_set_channel_names(struct efx_nic *efx)
581{
582 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800583
Ben Hutchings7f967c02012-02-13 23:45:02 +0000584 efx_for_each_channel(channel, efx)
585 channel->type->get_name(channel,
586 efx->channel_name[channel->channel],
587 sizeof(efx->channel_name[0]));
Ben Hutchings56536e92008-12-12 21:37:02 -0800588}
589
Ben Hutchings46426102010-09-10 06:42:33 +0000590static int efx_probe_channels(struct efx_nic *efx)
591{
592 struct efx_channel *channel;
593 int rc;
594
595 /* Restart special buffer allocation */
596 efx->next_buffer_table = 0;
597
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000598 /* Probe channels in reverse, so that any 'extra' channels
599 * use the start of the buffer table. This allows the traffic
600 * channels to be resized without moving them or wasting the
601 * entries before them.
602 */
603 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000604 rc = efx_probe_channel(channel);
605 if (rc) {
606 netif_err(efx, probe, efx->net_dev,
607 "failed to create channel %d\n",
608 channel->channel);
609 goto fail;
610 }
611 }
612 efx_set_channel_names(efx);
613
614 return 0;
615
616fail:
617 efx_remove_channels(efx);
618 return rc;
619}
620
Ben Hutchings8ceee662008-04-27 12:55:59 +0100621/* Channels are shutdown and reinitialised whilst the NIC is running
622 * to propagate configuration changes (mtu, checksum offload), or
623 * to clear hardware error conditions
624 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000625static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100626{
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000627 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100628 struct efx_tx_queue *tx_queue;
629 struct efx_rx_queue *rx_queue;
630 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000631 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100632
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100633 /* Calculate the rx buffer allocation parameters required to
634 * support the current MTU, including padding for header
635 * alignment and overruns.
636 */
Ben Hutchings272baee2013-01-29 23:33:14 +0000637 efx->rx_dma_len = (efx->type->rx_buffer_hash_size +
638 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
639 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000640 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Ben Hutchingsc14ff2e2013-05-13 11:58:31 +0000641 NET_IP_ALIGN + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000642 if (rx_buf_len <= PAGE_SIZE) {
643 efx->rx_scatter = false;
644 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000645 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000646 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000647 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000648 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
649 EFX_RX_BUF_ALIGNMENT) >
650 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000651 efx->rx_scatter = true;
652 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
653 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000654 } else {
655 efx->rx_scatter = false;
656 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000657 }
658
Daniel Pieczko1648a232013-02-13 10:54:41 +0000659 efx_rx_config_page_split(efx);
660 if (efx->rx_buffer_order)
661 netif_dbg(efx, drv, efx->net_dev,
662 "RX buf len=%u; page order=%u batch=%u\n",
663 efx->rx_dma_len, efx->rx_buffer_order,
664 efx->rx_pages_per_batch);
665 else
666 netif_dbg(efx, drv, efx->net_dev,
667 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
668 efx->rx_dma_len, efx->rx_page_buf_step,
669 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000670
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000671 /* RX filters also have scatter-enabled flags */
672 if (efx->rx_scatter != old_rx_scatter)
673 efx_filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100674
Ben Hutchings14bf7182012-05-22 01:27:58 +0100675 /* We must keep at least one descriptor in a TX ring empty.
676 * We could avoid this when the queue size does not exactly
677 * match the hardware ring size, but it's not that important.
678 * Therefore we stop the queue when one more skb might fill
679 * the ring completely. We wake it when half way back to
680 * empty.
681 */
682 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
683 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
684
Ben Hutchings8ceee662008-04-27 12:55:59 +0100685 /* Initialise the channels */
686 efx_for_each_channel(channel, efx) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100687 efx_for_each_channel_tx_queue(tx_queue, channel)
688 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100689
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000690 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100691 efx_init_rx_queue(rx_queue);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000692 efx_nic_generate_fill_event(rx_queue);
693 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100694
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000695 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100696 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000697
698 if (netif_device_present(efx->net_dev))
699 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100700}
701
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000702static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100703{
704 struct efx_channel *channel;
705 struct efx_tx_queue *tx_queue;
706 struct efx_rx_queue *rx_queue;
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100707 struct pci_dev *dev = efx->pci_dev;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100708 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100709
710 EFX_ASSERT_RESET_SERIALISED(efx);
711 BUG_ON(efx->port_enabled);
712
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100713 /* Only perform flush if dma is enabled */
Alexandre Rames626950d2013-01-14 17:20:22 +0000714 if (dev->is_busmaster && efx->state != STATE_RECOVERY) {
Stuart Hodgson3dca9d22012-03-30 13:04:51 +0100715 rc = efx_nic_flush_queues(efx);
716
717 if (rc && EFX_WORKAROUND_7803(efx)) {
718 /* Schedule a reset to recover from the flush failure. The
719 * descriptor caches reference memory we're about to free,
720 * but falcon_reconfigure_mac_wrapper() won't reconnect
721 * the MACs because of the pending reset. */
722 netif_err(efx, drv, efx->net_dev,
723 "Resetting to recover from flush failure\n");
724 efx_schedule_reset(efx, RESET_TYPE_ALL);
725 } else if (rc) {
726 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
727 } else {
728 netif_dbg(efx, drv, efx->net_dev,
729 "successfully flushed all queues\n");
730 }
Steve Hodgsonfd371e32010-06-01 11:17:51 +0000731 }
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100732
Ben Hutchings8ceee662008-04-27 12:55:59 +0100733 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000734 /* RX packet processing is pipelined, so wait for the
735 * NAPI handler to complete. At least event queue 0
736 * might be kept active by non-data events, so don't
737 * use napi_synchronize() but actually disable NAPI
738 * temporarily.
739 */
740 if (efx_channel_has_rx_queue(channel)) {
741 efx_stop_eventq(channel);
742 efx_start_eventq(channel);
743 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100744
745 efx_for_each_channel_rx_queue(rx_queue, channel)
746 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000747 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100748 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100749 }
750}
751
752static void efx_remove_channel(struct efx_channel *channel)
753{
754 struct efx_tx_queue *tx_queue;
755 struct efx_rx_queue *rx_queue;
756
Ben Hutchings62776d02010-06-23 11:30:07 +0000757 netif_dbg(channel->efx, drv, channel->efx->net_dev,
758 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100759
760 efx_for_each_channel_rx_queue(rx_queue, channel)
761 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000762 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100763 efx_remove_tx_queue(tx_queue);
764 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100765 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100766}
767
Ben Hutchings46426102010-09-10 06:42:33 +0000768static void efx_remove_channels(struct efx_nic *efx)
769{
770 struct efx_channel *channel;
771
772 efx_for_each_channel(channel, efx)
773 efx_remove_channel(channel);
774}
775
776int
777efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
778{
779 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
780 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000781 unsigned i, next_buffer_table = 0;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100782 int rc;
783
784 rc = efx_check_disabled(efx);
785 if (rc)
786 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000787
788 /* Not all channels should be reallocated. We must avoid
789 * reallocating their buffer table entries.
790 */
791 efx_for_each_channel(channel, efx) {
792 struct efx_rx_queue *rx_queue;
793 struct efx_tx_queue *tx_queue;
794
795 if (channel->type->copy)
796 continue;
797 next_buffer_table = max(next_buffer_table,
798 channel->eventq.index +
799 channel->eventq.entries);
800 efx_for_each_channel_rx_queue(rx_queue, channel)
801 next_buffer_table = max(next_buffer_table,
802 rx_queue->rxd.index +
803 rx_queue->rxd.entries);
804 efx_for_each_channel_tx_queue(tx_queue, channel)
805 next_buffer_table = max(next_buffer_table,
806 tx_queue->txd.index +
807 tx_queue->txd.entries);
808 }
Ben Hutchings46426102010-09-10 06:42:33 +0000809
Ben Hutchings29c69a42013-01-28 19:01:06 +0000810 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000811 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000812 efx_stop_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000813
Ben Hutchings7f967c02012-02-13 23:45:02 +0000814 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000815 memset(other_channel, 0, sizeof(other_channel));
816 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000817 channel = efx->channel[i];
818 if (channel->type->copy)
819 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000820 if (!channel) {
821 rc = -ENOMEM;
822 goto out;
823 }
824 other_channel[i] = channel;
825 }
826
827 /* Swap entry counts and channel pointers */
828 old_rxq_entries = efx->rxq_entries;
829 old_txq_entries = efx->txq_entries;
830 efx->rxq_entries = rxq_entries;
831 efx->txq_entries = txq_entries;
832 for (i = 0; i < efx->n_channels; i++) {
833 channel = efx->channel[i];
834 efx->channel[i] = other_channel[i];
835 other_channel[i] = channel;
836 }
837
Ben Hutchings7f967c02012-02-13 23:45:02 +0000838 /* Restart buffer table allocation */
839 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000840
Ben Hutchingse8f14992010-12-07 19:47:34 +0000841 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000842 channel = efx->channel[i];
843 if (!channel->type->copy)
844 continue;
845 rc = efx_probe_channel(channel);
846 if (rc)
847 goto rollback;
848 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000849 }
Ben Hutchings46426102010-09-10 06:42:33 +0000850
Ben Hutchings7f967c02012-02-13 23:45:02 +0000851out:
852 /* Destroy unused channel structures */
853 for (i = 0; i < efx->n_channels; i++) {
854 channel = other_channel[i];
855 if (channel && channel->type->copy) {
856 efx_fini_napi_channel(channel);
857 efx_remove_channel(channel);
858 kfree(channel);
859 }
860 }
861
862 efx_start_interrupts(efx, true);
Ben Hutchings46426102010-09-10 06:42:33 +0000863 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +0000864 netif_device_attach(efx->net_dev);
Ben Hutchings46426102010-09-10 06:42:33 +0000865 return rc;
866
867rollback:
868 /* Swap back */
869 efx->rxq_entries = old_rxq_entries;
870 efx->txq_entries = old_txq_entries;
871 for (i = 0; i < efx->n_channels; i++) {
872 channel = efx->channel[i];
873 efx->channel[i] = other_channel[i];
874 other_channel[i] = channel;
875 }
876 goto out;
877}
878
Steve Hodgson90d683a2010-06-01 11:19:39 +0000879void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100880{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000881 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100882}
883
Ben Hutchings7f967c02012-02-13 23:45:02 +0000884static const struct efx_channel_type efx_default_channel_type = {
885 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100886 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000887 .get_name = efx_get_channel_name,
888 .copy = efx_copy_channel,
889 .keep_eventq = false,
890};
891
892int efx_channel_dummy_op_int(struct efx_channel *channel)
893{
894 return 0;
895}
896
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100897void efx_channel_dummy_op_void(struct efx_channel *channel)
898{
899}
900
Ben Hutchings8ceee662008-04-27 12:55:59 +0100901/**************************************************************************
902 *
903 * Port handling
904 *
905 **************************************************************************/
906
907/* This ensures that the kernel is kept informed (via
908 * netif_carrier_on/off) of the link status, and also maintains the
909 * link status's stop on the port's TX queue.
910 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000911void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100912{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000913 struct efx_link_state *link_state = &efx->link_state;
914
Ben Hutchings8ceee662008-04-27 12:55:59 +0100915 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
916 * that no events are triggered between unregister_netdev() and the
917 * driver unloading. A more general condition is that NETDEV_CHANGE
918 * can only be generated between NETDEV_UP and NETDEV_DOWN */
919 if (!netif_running(efx->net_dev))
920 return;
921
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000922 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100923 efx->n_link_state_changes++;
924
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000925 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100926 netif_carrier_on(efx->net_dev);
927 else
928 netif_carrier_off(efx->net_dev);
929 }
930
931 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000932 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000933 netif_info(efx, link, efx->net_dev,
934 "link up at %uMbps %s-duplex (MTU %d)%s\n",
935 link_state->speed, link_state->fd ? "full" : "half",
936 efx->net_dev->mtu,
937 (efx->promiscuous ? " [PROMISC]" : ""));
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000938 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000939 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100940}
941
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000942void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
943{
944 efx->link_advertising = advertising;
945 if (advertising) {
946 if (advertising & ADVERTISED_Pause)
947 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
948 else
949 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
950 if (advertising & ADVERTISED_Asym_Pause)
951 efx->wanted_fc ^= EFX_FC_TX;
952 }
953}
954
David S. Millerb56269462011-05-17 17:53:22 -0400955void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000956{
957 efx->wanted_fc = wanted_fc;
958 if (efx->link_advertising) {
959 if (wanted_fc & EFX_FC_RX)
960 efx->link_advertising |= (ADVERTISED_Pause |
961 ADVERTISED_Asym_Pause);
962 else
963 efx->link_advertising &= ~(ADVERTISED_Pause |
964 ADVERTISED_Asym_Pause);
965 if (wanted_fc & EFX_FC_TX)
966 efx->link_advertising ^= ADVERTISED_Asym_Pause;
967 }
968}
969
Ben Hutchings115122a2009-03-04 09:52:52 +0000970static void efx_fini_port(struct efx_nic *efx);
971
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000972/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
973 * the MAC appropriately. All other PHY configuration changes are pushed
974 * through phy_op->set_settings(), and pushed asynchronously to the MAC
975 * through efx_monitor().
976 *
977 * Callers must hold the mac_lock
978 */
979int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100980{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000981 enum efx_phy_mode phy_mode;
982 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100983
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000984 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100985
Ben Hutchings0fca8c92012-01-09 19:54:44 +0000986 /* Serialise the promiscuous flag with efx_set_rx_mode. */
Ben Hutchings73ba7b62012-01-09 19:47:08 +0000987 netif_addr_lock_bh(efx->net_dev);
988 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsa816f752008-09-01 12:49:12 +0100989
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000990 /* Disable PHY transmit in mac level loopbacks */
991 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800992 if (LOOPBACK_INTERNAL(efx))
993 efx->phy_mode |= PHY_MODE_TX_DISABLED;
994 else
995 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800996
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000997 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800998
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000999 if (rc)
1000 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001001
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001002 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001003}
1004
1005/* Reinitialise the MAC to pick up new PHY settings, even if the port is
1006 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001007int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001008{
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001009 int rc;
1010
Ben Hutchings8ceee662008-04-27 12:55:59 +01001011 EFX_ASSERT_RESET_SERIALISED(efx);
1012
1013 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001014 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001015 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001016
1017 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001018}
1019
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001020/* Asynchronous work item for changing MAC promiscuity and multicast
1021 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
1022 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001023static void efx_mac_work(struct work_struct *data)
1024{
1025 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
1026
1027 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +01001028 if (efx->port_enabled)
Ben Hutchings710b2082011-09-03 00:15:00 +01001029 efx->type->reconfigure_mac(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001030 mutex_unlock(&efx->mac_lock);
1031}
1032
Ben Hutchings8ceee662008-04-27 12:55:59 +01001033static int efx_probe_port(struct efx_nic *efx)
1034{
1035 int rc;
1036
Ben Hutchings62776d02010-06-23 11:30:07 +00001037 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001038
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001039 if (phy_flash_cfg)
1040 efx->phy_mode = PHY_MODE_SPECIAL;
1041
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001042 /* Connect up MAC/PHY operations table */
1043 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001044 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001045 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001046
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001047 /* Initialise MAC address to permanent address */
1048 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001049
1050 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001051}
1052
1053static int efx_init_port(struct efx_nic *efx)
1054{
1055 int rc;
1056
Ben Hutchings62776d02010-06-23 11:30:07 +00001057 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001058
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001059 mutex_lock(&efx->mac_lock);
1060
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001061 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001062 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001063 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001064
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001065 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001066
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001067 /* Reconfigure the MAC before creating dma queues (required for
1068 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Ben Hutchings710b2082011-09-03 00:15:00 +01001069 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001070
1071 /* Ensure the PHY advertises the correct flow control settings */
1072 rc = efx->phy_op->reconfigure(efx);
1073 if (rc)
1074 goto fail2;
1075
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001076 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001077 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001078
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001079fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001080 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001081fail1:
1082 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001083 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001084}
1085
Ben Hutchings8ceee662008-04-27 12:55:59 +01001086static void efx_start_port(struct efx_nic *efx)
1087{
Ben Hutchings62776d02010-06-23 11:30:07 +00001088 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001089 BUG_ON(efx->port_enabled);
1090
1091 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001092 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001093
1094 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1095 * and then cancelled by efx_flush_all() */
Ben Hutchings710b2082011-09-03 00:15:00 +01001096 efx->type->reconfigure_mac(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001097
Ben Hutchings8ceee662008-04-27 12:55:59 +01001098 mutex_unlock(&efx->mac_lock);
1099}
1100
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001101/* Prevent efx_mac_work() and efx_monitor() from working */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001102static void efx_stop_port(struct efx_nic *efx)
1103{
Ben Hutchings62776d02010-06-23 11:30:07 +00001104 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001105
1106 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001107 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001108 mutex_unlock(&efx->mac_lock);
1109
1110 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001111 netif_addr_lock_bh(efx->net_dev);
1112 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001113}
1114
1115static void efx_fini_port(struct efx_nic *efx)
1116{
Ben Hutchings62776d02010-06-23 11:30:07 +00001117 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001118
1119 if (!efx->port_initialized)
1120 return;
1121
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001122 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001123 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001124
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001125 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001126 efx_link_status_changed(efx);
1127}
1128
1129static void efx_remove_port(struct efx_nic *efx)
1130{
Ben Hutchings62776d02010-06-23 11:30:07 +00001131 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001132
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001133 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001134}
1135
1136/**************************************************************************
1137 *
1138 * NIC handling
1139 *
1140 **************************************************************************/
1141
1142/* This configures the PCI device to enable I/O and DMA. */
1143static int efx_init_io(struct efx_nic *efx)
1144{
1145 struct pci_dev *pci_dev = efx->pci_dev;
1146 dma_addr_t dma_mask = efx->type->max_dma_mask;
1147 int rc;
1148
Ben Hutchings62776d02010-06-23 11:30:07 +00001149 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001150
1151 rc = pci_enable_device(pci_dev);
1152 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001153 netif_err(efx, probe, efx->net_dev,
1154 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001155 goto fail1;
1156 }
1157
1158 pci_set_master(pci_dev);
1159
1160 /* Set the PCI DMA mask. Try all possibilities from our
1161 * genuine mask down to 32 bits, because some architectures
1162 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1163 * masks event though they reject 46 bit masks.
1164 */
1165 while (dma_mask > 0x7fffffffUL) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001166 if (dma_supported(&pci_dev->dev, dma_mask)) {
1167 rc = dma_set_mask(&pci_dev->dev, dma_mask);
Ben Hutchingse9e01842012-01-05 18:50:29 +00001168 if (rc == 0)
1169 break;
1170 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001171 dma_mask >>= 1;
1172 }
1173 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001174 netif_err(efx, probe, efx->net_dev,
1175 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001176 goto fail2;
1177 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001178 netif_dbg(efx, probe, efx->net_dev,
1179 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings0e33d872012-05-17 17:46:55 +01001180 rc = dma_set_coherent_mask(&pci_dev->dev, dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001181 if (rc) {
Ben Hutchings0e33d872012-05-17 17:46:55 +01001182 /* dma_set_coherent_mask() is not *allowed* to
1183 * fail with a mask that dma_set_mask() accepted,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001184 * but just in case...
1185 */
Ben Hutchings62776d02010-06-23 11:30:07 +00001186 netif_err(efx, probe, efx->net_dev,
1187 "failed to set consistent DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001188 goto fail2;
1189 }
1190
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001191 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1192 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001193 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001194 netif_err(efx, probe, efx->net_dev,
1195 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001196 rc = -EIO;
1197 goto fail3;
1198 }
David S. Miller8decf862011-09-22 03:23:13 -04001199 efx->membase = ioremap_nocache(efx->membase_phys,
1200 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001201 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001202 netif_err(efx, probe, efx->net_dev,
1203 "could not map memory BAR at %llx+%x\n",
1204 (unsigned long long)efx->membase_phys,
1205 efx->type->mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001206 rc = -ENOMEM;
1207 goto fail4;
1208 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001209 netif_dbg(efx, probe, efx->net_dev,
1210 "memory BAR at %llx+%x (virtual %p)\n",
1211 (unsigned long long)efx->membase_phys,
1212 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001213
1214 return 0;
1215
1216 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001217 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001218 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001219 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001220 fail2:
1221 pci_disable_device(efx->pci_dev);
1222 fail1:
1223 return rc;
1224}
1225
1226static void efx_fini_io(struct efx_nic *efx)
1227{
Ben Hutchings62776d02010-06-23 11:30:07 +00001228 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001229
1230 if (efx->membase) {
1231 iounmap(efx->membase);
1232 efx->membase = NULL;
1233 }
1234
1235 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +00001236 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001237 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001238 }
1239
1240 pci_disable_device(efx->pci_dev);
1241}
1242
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001243static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001244{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001245 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001246 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001247 int cpu;
1248
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001249 if (rss_cpus) {
1250 count = rss_cpus;
1251 } else {
1252 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1253 netif_warn(efx, probe, efx->net_dev,
1254 "RSS disabled due to allocation failure\n");
1255 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001256 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001257
1258 count = 0;
1259 for_each_online_cpu(cpu) {
1260 if (!cpumask_test_cpu(cpu, thread_mask)) {
1261 ++count;
1262 cpumask_or(thread_mask, thread_mask,
1263 topology_thread_cpumask(cpu));
1264 }
1265 }
1266
1267 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001268 }
1269
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001270 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1271 * table entries that are inaccessible to VFs
1272 */
1273 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1274 count > efx_vf_size(efx)) {
1275 netif_warn(efx, probe, efx->net_dev,
1276 "Reducing number of RSS channels from %u to %u for "
1277 "VF support. Increase vf-msix-limit to use more "
1278 "channels on the PF.\n",
1279 count, efx_vf_size(efx));
1280 count = efx_vf_size(efx);
1281 }
1282
Ben Hutchings46123d02008-09-01 12:47:33 +01001283 return count;
1284}
1285
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001286static int
1287efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1288{
1289#ifdef CONFIG_RFS_ACCEL
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001290 unsigned int i;
1291 int rc;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001292
1293 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1294 if (!efx->net_dev->rx_cpu_rmap)
1295 return -ENOMEM;
1296 for (i = 0; i < efx->n_rx_channels; i++) {
1297 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1298 xentries[i].vector);
1299 if (rc) {
1300 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1301 efx->net_dev->rx_cpu_rmap = NULL;
1302 return rc;
1303 }
1304 }
1305#endif
1306 return 0;
1307}
1308
Ben Hutchings46123d02008-09-01 12:47:33 +01001309/* Probe the number and type of interrupts we are able to obtain, and
1310 * the resulting numbers of channels and RX queues.
1311 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001312static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001313{
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001314 unsigned int max_channels =
1315 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001316 unsigned int extra_channels = 0;
1317 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001318 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001319
Ben Hutchings7f967c02012-02-13 23:45:02 +00001320 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1321 if (efx->extra_channel_type[i])
1322 ++extra_channels;
1323
Ben Hutchings8ceee662008-04-27 12:55:59 +01001324 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001325 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001326 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001327
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001328 n_channels = efx_wanted_parallelism(efx);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001329 if (separate_tx_channels)
1330 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001331 n_channels += extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001332 n_channels = min(n_channels, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001333
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001334 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001335 xentries[i].entry = i;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001336 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001337 if (rc > 0) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001338 netif_err(efx, drv, efx->net_dev,
1339 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001340 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001341 netif_err(efx, drv, efx->net_dev,
1342 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001343 EFX_BUG_ON_PARANOID(rc >= n_channels);
1344 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001345 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001346 n_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001347 }
1348
1349 if (rc == 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001350 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001351 if (n_channels > extra_channels)
1352 n_channels -= extra_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001353 if (separate_tx_channels) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001354 efx->n_tx_channels = max(n_channels / 2, 1U);
1355 efx->n_rx_channels = max(n_channels -
1356 efx->n_tx_channels,
1357 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001358 } else {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001359 efx->n_tx_channels = n_channels;
1360 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001361 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001362 rc = efx_init_rx_cpu_rmap(efx, xentries);
1363 if (rc) {
1364 pci_disable_msix(efx->pci_dev);
1365 return rc;
1366 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001367 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001368 efx_get_channel(efx, i)->irq =
1369 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001370 } else {
1371 /* Fall back to single channel MSI */
1372 efx->interrupt_mode = EFX_INT_MODE_MSI;
Ben Hutchings62776d02010-06-23 11:30:07 +00001373 netif_err(efx, drv, efx->net_dev,
1374 "could not enable MSI-X\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001375 }
1376 }
1377
1378 /* Try single interrupt MSI */
1379 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001380 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001381 efx->n_rx_channels = 1;
1382 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001383 rc = pci_enable_msi(efx->pci_dev);
1384 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001385 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001386 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001387 netif_err(efx, drv, efx->net_dev,
1388 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001389 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1390 }
1391 }
1392
1393 /* Assume legacy interrupts */
1394 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Neil Turton28b581a2008-12-12 21:41:06 -08001395 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001396 efx->n_rx_channels = 1;
1397 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001398 efx->legacy_irq = efx->pci_dev->irq;
1399 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001400
Ben Hutchings7f967c02012-02-13 23:45:02 +00001401 /* Assign extra channels if possible */
1402 j = efx->n_channels;
1403 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1404 if (!efx->extra_channel_type[i])
1405 continue;
1406 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1407 efx->n_channels <= extra_channels) {
1408 efx->extra_channel_type[i]->handle_no_channel(efx);
1409 } else {
1410 --j;
1411 efx_get_channel(efx, j)->type =
1412 efx->extra_channel_type[i];
1413 }
1414 }
1415
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001416 /* RSS might be usable on VFs even if it is disabled on the PF */
Ben Hutchings3132d282012-05-05 02:31:23 +01001417 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001418 efx->n_rx_channels : efx_vf_size(efx));
1419
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001420 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001421}
1422
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001423/* Enable interrupts, then probe and start the event queues */
Ben Hutchings7f967c02012-02-13 23:45:02 +00001424static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001425{
1426 struct efx_channel *channel;
1427
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001428 BUG_ON(efx->state == STATE_DISABLED);
1429
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001430 if (efx->legacy_irq)
1431 efx->legacy_irq_enabled = true;
1432 efx_nic_enable_interrupts(efx);
1433
1434 efx_for_each_channel(channel, efx) {
Ben Hutchings7f967c02012-02-13 23:45:02 +00001435 if (!channel->type->keep_eventq || !may_keep_eventq)
1436 efx_init_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001437 efx_start_eventq(channel);
1438 }
1439
1440 efx_mcdi_mode_event(efx);
1441}
1442
Ben Hutchings7f967c02012-02-13 23:45:02 +00001443static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001444{
1445 struct efx_channel *channel;
1446
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001447 if (efx->state == STATE_DISABLED)
1448 return;
1449
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001450 efx_mcdi_mode_poll(efx);
1451
1452 efx_nic_disable_interrupts(efx);
1453 if (efx->legacy_irq) {
1454 synchronize_irq(efx->legacy_irq);
1455 efx->legacy_irq_enabled = false;
1456 }
1457
1458 efx_for_each_channel(channel, efx) {
1459 if (channel->irq)
1460 synchronize_irq(channel->irq);
1461
1462 efx_stop_eventq(channel);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001463 if (!channel->type->keep_eventq || !may_keep_eventq)
1464 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001465 }
1466}
1467
Ben Hutchings8ceee662008-04-27 12:55:59 +01001468static void efx_remove_interrupts(struct efx_nic *efx)
1469{
1470 struct efx_channel *channel;
1471
1472 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001473 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001474 channel->irq = 0;
1475 pci_disable_msi(efx->pci_dev);
1476 pci_disable_msix(efx->pci_dev);
1477
1478 /* Remove legacy interrupt */
1479 efx->legacy_irq = 0;
1480}
1481
Ben Hutchings8831da72008-09-01 12:47:48 +01001482static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001483{
Ben Hutchings602a5322011-05-16 17:32:39 +01001484 struct efx_channel *channel;
1485 struct efx_tx_queue *tx_queue;
1486
Ben Hutchings97653432011-01-12 18:26:56 +00001487 efx->tx_channel_offset =
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001488 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001489
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001490 /* We need to mark which channels really have RX and TX
1491 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001492 * RX-only and TX-only channels.
1493 */
1494 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001495 if (channel->channel < efx->n_rx_channels)
1496 channel->rx_queue.core_index = channel->channel;
1497 else
1498 channel->rx_queue.core_index = -1;
1499
Ben Hutchings602a5322011-05-16 17:32:39 +01001500 efx_for_each_channel_tx_queue(tx_queue, channel)
1501 tx_queue->queue -= (efx->tx_channel_offset *
1502 EFX_TXQ_TYPES);
1503 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001504}
1505
1506static int efx_probe_nic(struct efx_nic *efx)
1507{
Ben Hutchings765c9f42010-06-30 05:06:28 +00001508 size_t i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001509 int rc;
1510
Ben Hutchings62776d02010-06-23 11:30:07 +00001511 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001512
1513 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001514 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001515 if (rc)
1516 return rc;
1517
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001518 /* Determine the number of channels and queues by trying to hook
Ben Hutchings8ceee662008-04-27 12:55:59 +01001519 * in MSI-X interrupts. */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001520 rc = efx_probe_interrupts(efx);
1521 if (rc)
1522 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001523
Ben Hutchings28e47c42012-02-15 01:58:49 +00001524 efx->type->dimension_resources(efx);
1525
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001526 if (efx->n_channels > 1)
1527 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
Ben Hutchings765c9f42010-06-30 05:06:28 +00001528 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
Ben Hutchings278bc422011-12-15 13:56:49 +00001529 efx->rx_indir_table[i] =
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001530 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001531
Ben Hutchings8831da72008-09-01 12:47:48 +01001532 efx_set_channels(efx);
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001533 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1534 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001535
1536 /* Initialise the interrupt moderation settings */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001537 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1538 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001539
1540 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001541
1542fail:
1543 efx->type->remove(efx);
1544 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001545}
1546
1547static void efx_remove_nic(struct efx_nic *efx)
1548{
Ben Hutchings62776d02010-06-23 11:30:07 +00001549 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001550
1551 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001552 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001553}
1554
1555/**************************************************************************
1556 *
1557 * NIC startup/shutdown
1558 *
1559 *************************************************************************/
1560
1561static int efx_probe_all(struct efx_nic *efx)
1562{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001563 int rc;
1564
Ben Hutchings8ceee662008-04-27 12:55:59 +01001565 rc = efx_probe_nic(efx);
1566 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001567 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001568 goto fail1;
1569 }
1570
Ben Hutchings8ceee662008-04-27 12:55:59 +01001571 rc = efx_probe_port(efx);
1572 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001573 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001574 goto fail2;
1575 }
1576
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001577 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1578 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1579 rc = -EINVAL;
1580 goto fail3;
1581 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001582 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001583
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001584 rc = efx_probe_filters(efx);
1585 if (rc) {
1586 netif_err(efx, probe, efx->net_dev,
1587 "failed to create filter tables\n");
Ben Hutchings7f967c02012-02-13 23:45:02 +00001588 goto fail3;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001589 }
1590
Ben Hutchings7f967c02012-02-13 23:45:02 +00001591 rc = efx_probe_channels(efx);
1592 if (rc)
1593 goto fail4;
1594
Ben Hutchings8ceee662008-04-27 12:55:59 +01001595 return 0;
1596
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001597 fail4:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001598 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001599 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001600 efx_remove_port(efx);
1601 fail2:
1602 efx_remove_nic(efx);
1603 fail1:
1604 return rc;
1605}
1606
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001607/* If the interface is supposed to be running but is not, start
1608 * the hardware and software data path, regular activity for the port
1609 * (MAC statistics, link polling, etc.) and schedule the port to be
1610 * reconfigured. Interrupts must already be enabled. This function
1611 * is safe to call multiple times, so long as the NIC is not disabled.
1612 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001613 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001614static void efx_start_all(struct efx_nic *efx)
1615{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001616 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001617 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001618
1619 /* Check that it is appropriate to restart the interface. All
1620 * of these flags are safe to read under just the rtnl lock */
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001621 if (efx->port_enabled || !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001622 return;
1623
Ben Hutchings8ceee662008-04-27 12:55:59 +01001624 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001625 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001626
Alexandre Rames626950d2013-01-14 17:20:22 +00001627 /* Start the hardware monitor if there is one */
1628 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001629 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1630 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001631
1632 /* If link state detection is normally event-driven, we have
1633 * to poll now because we could have missed a change
1634 */
1635 if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0) {
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001636 mutex_lock(&efx->mac_lock);
1637 if (efx->phy_op->poll(efx))
1638 efx_link_status_changed(efx);
1639 mutex_unlock(&efx->mac_lock);
1640 }
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001641
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001642 efx->type->start_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001643}
1644
1645/* Flush all delayed work. Should only be called when no more delayed work
1646 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1647 * since we're holding the rtnl_lock at this point. */
1648static void efx_flush_all(struct efx_nic *efx)
1649{
Ben Hutchingsdd407812012-02-28 23:40:21 +00001650 /* Make sure the hardware monitor and event self-test are stopped */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001651 cancel_delayed_work_sync(&efx->monitor_work);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001652 efx_selftest_async_cancel(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001653 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001654 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001655}
1656
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001657/* Quiesce the hardware and software data path, and regular activity
1658 * for the port without bringing the link down. Safe to call multiple
1659 * times with the NIC in almost any state, but interrupts should be
1660 * enabled. Requires the RTNL lock.
1661 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001662static void efx_stop_all(struct efx_nic *efx)
1663{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001664 EFX_ASSERT_RESET_SERIALISED(efx);
1665
1666 /* port_enabled can be read safely under the rtnl lock */
1667 if (!efx->port_enabled)
1668 return;
1669
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001670 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001671 efx_stop_port(efx);
1672
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00001673 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001674 efx_flush_all(efx);
1675
Ben Hutchings29c69a42013-01-28 19:01:06 +00001676 /* Stop the kernel transmit interface. This is only valid if
1677 * the device is stopped or detached; otherwise the watchdog
1678 * may fire immediately.
1679 */
1680 WARN_ON(netif_running(efx->net_dev) &&
1681 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001682 netif_tx_disable(efx->net_dev);
1683
1684 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001685}
1686
1687static void efx_remove_all(struct efx_nic *efx)
1688{
Ben Hutchings46426102010-09-10 06:42:33 +00001689 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001690 efx_remove_filters(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001691 efx_remove_port(efx);
1692 efx_remove_nic(efx);
1693}
1694
Ben Hutchings8ceee662008-04-27 12:55:59 +01001695/**************************************************************************
1696 *
1697 * Interrupt moderation
1698 *
1699 **************************************************************************/
1700
Ben Hutchingscc180b62011-12-08 19:51:47 +00001701static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001702{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001703 if (usecs == 0)
1704 return 0;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001705 if (usecs * 1000 < quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001706 return 1; /* never round down to 0 */
Ben Hutchingscc180b62011-12-08 19:51:47 +00001707 return usecs * 1000 / quantum_ns;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001708}
1709
Ben Hutchings8ceee662008-04-27 12:55:59 +01001710/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001711int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1712 unsigned int rx_usecs, bool rx_adaptive,
1713 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001714{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001715 struct efx_channel *channel;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001716 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1717 efx->timer_quantum_ns,
1718 1000);
1719 unsigned int tx_ticks;
1720 unsigned int rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001721
1722 EFX_ASSERT_RESET_SERIALISED(efx);
1723
Ben Hutchingscc180b62011-12-08 19:51:47 +00001724 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001725 return -EINVAL;
1726
Ben Hutchingscc180b62011-12-08 19:51:47 +00001727 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1728 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1729
Ben Hutchings9e393b32011-09-05 07:43:04 +00001730 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1731 !rx_may_override_tx) {
1732 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1733 "RX and TX IRQ moderation must be equal\n");
1734 return -EINVAL;
1735 }
1736
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001737 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001738 efx->irq_rx_moderation = rx_ticks;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001739 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001740 if (efx_channel_has_rx_queue(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001741 channel->irq_moderation = rx_ticks;
Ben Hutchings525da902011-02-07 23:04:38 +00001742 else if (efx_channel_has_tx_queues(channel))
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001743 channel->irq_moderation = tx_ticks;
1744 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001745
1746 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001747}
1748
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001749void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1750 unsigned int *rx_usecs, bool *rx_adaptive)
1751{
Ben Hutchingscc180b62011-12-08 19:51:47 +00001752 /* We must round up when converting ticks to microseconds
1753 * because we round down when converting the other way.
1754 */
1755
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001756 *rx_adaptive = efx->irq_rx_adaptive;
Ben Hutchingscc180b62011-12-08 19:51:47 +00001757 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1758 efx->timer_quantum_ns,
1759 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001760
1761 /* If channels are shared between RX and TX, so is IRQ
1762 * moderation. Otherwise, IRQ moderation is the same for all
1763 * TX channels and is not adaptive.
1764 */
1765 if (efx->tx_channel_offset == 0)
1766 *tx_usecs = *rx_usecs;
1767 else
Ben Hutchingscc180b62011-12-08 19:51:47 +00001768 *tx_usecs = DIV_ROUND_UP(
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001769 efx->channel[efx->tx_channel_offset]->irq_moderation *
Ben Hutchingscc180b62011-12-08 19:51:47 +00001770 efx->timer_quantum_ns,
1771 1000);
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001772}
1773
Ben Hutchings8ceee662008-04-27 12:55:59 +01001774/**************************************************************************
1775 *
1776 * Hardware monitor
1777 *
1778 **************************************************************************/
1779
Ben Hutchingse254c272010-09-20 08:44:10 +00001780/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001781static void efx_monitor(struct work_struct *data)
1782{
1783 struct efx_nic *efx = container_of(data, struct efx_nic,
1784 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001785
Ben Hutchings62776d02010-06-23 11:30:07 +00001786 netif_vdbg(efx, timer, efx->net_dev,
1787 "hardware monitor executing on CPU %d\n",
1788 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001789 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001790
Ben Hutchings8ceee662008-04-27 12:55:59 +01001791 /* If the mac_lock is already held then it is likely a port
1792 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00001793 * most of the work of monitor() anyway. */
1794 if (mutex_trylock(&efx->mac_lock)) {
1795 if (efx->port_enabled)
1796 efx->type->monitor(efx);
1797 mutex_unlock(&efx->mac_lock);
1798 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001799
Ben Hutchings8ceee662008-04-27 12:55:59 +01001800 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1801 efx_monitor_interval);
1802}
1803
1804/**************************************************************************
1805 *
1806 * ioctls
1807 *
1808 *************************************************************************/
1809
1810/* Net device ioctl
1811 * Context: process, rtnl_lock() held.
1812 */
1813static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1814{
Ben Hutchings767e4682008-09-01 12:43:14 +01001815 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001816 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001817
Stuart Hodgson7c236c42012-09-03 11:09:36 +01001818 if (cmd == SIOCSHWTSTAMP)
1819 return efx_ptp_ioctl(efx, ifr, cmd);
1820
Ben Hutchings68e7f452009-04-29 08:05:08 +00001821 /* Convert phy_id from older PRTAD/DEVAD format */
1822 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1823 (data->phy_id & 0xfc00) == 0x0400)
1824 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1825
1826 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001827}
1828
1829/**************************************************************************
1830 *
1831 * NAPI interface
1832 *
1833 **************************************************************************/
1834
Ben Hutchings7f967c02012-02-13 23:45:02 +00001835static void efx_init_napi_channel(struct efx_channel *channel)
1836{
1837 struct efx_nic *efx = channel->efx;
1838
1839 channel->napi_dev = efx->net_dev;
1840 netif_napi_add(channel->napi_dev, &channel->napi_str,
1841 efx_poll, napi_weight);
1842}
1843
Ben Hutchingse8f14992010-12-07 19:47:34 +00001844static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001845{
1846 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001847
Ben Hutchings7f967c02012-02-13 23:45:02 +00001848 efx_for_each_channel(channel, efx)
1849 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00001850}
1851
1852static void efx_fini_napi_channel(struct efx_channel *channel)
1853{
1854 if (channel->napi_dev)
1855 netif_napi_del(&channel->napi_str);
1856 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001857}
1858
1859static void efx_fini_napi(struct efx_nic *efx)
1860{
1861 struct efx_channel *channel;
1862
Ben Hutchingse8f14992010-12-07 19:47:34 +00001863 efx_for_each_channel(channel, efx)
1864 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001865}
1866
1867/**************************************************************************
1868 *
1869 * Kernel netpoll interface
1870 *
1871 *************************************************************************/
1872
1873#ifdef CONFIG_NET_POLL_CONTROLLER
1874
1875/* Although in the common case interrupts will be disabled, this is not
1876 * guaranteed. However, all our work happens inside the NAPI callback,
1877 * so no locking is required.
1878 */
1879static void efx_netpoll(struct net_device *net_dev)
1880{
Ben Hutchings767e4682008-09-01 12:43:14 +01001881 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001882 struct efx_channel *channel;
1883
Ben Hutchings64ee3122008-09-01 12:47:38 +01001884 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001885 efx_schedule_channel(channel);
1886}
1887
1888#endif
1889
1890/**************************************************************************
1891 *
1892 * Kernel net device interface
1893 *
1894 *************************************************************************/
1895
1896/* Context: process, rtnl_lock() held. */
1897static int efx_net_open(struct net_device *net_dev)
1898{
Ben Hutchings767e4682008-09-01 12:43:14 +01001899 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001900 int rc;
1901
Ben Hutchings62776d02010-06-23 11:30:07 +00001902 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1903 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001904
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001905 rc = efx_check_disabled(efx);
1906 if (rc)
1907 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001908 if (efx->phy_mode & PHY_MODE_SPECIAL)
1909 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001910 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1911 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001912
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00001913 /* Notify the kernel of the link state polled during driver load,
1914 * before the monitor starts running */
1915 efx_link_status_changed(efx);
1916
Ben Hutchings8ceee662008-04-27 12:55:59 +01001917 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00001918 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001919 return 0;
1920}
1921
1922/* Context: process, rtnl_lock() held.
1923 * Note that the kernel will ignore our return code; this method
1924 * should really be a void.
1925 */
1926static int efx_net_stop(struct net_device *net_dev)
1927{
Ben Hutchings767e4682008-09-01 12:43:14 +01001928 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001929
Ben Hutchings62776d02010-06-23 11:30:07 +00001930 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1931 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01001932
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001933 /* Stop the device and flush all the channels */
1934 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001935
1936 return 0;
1937}
1938
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001939/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00001940static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1941 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001942{
Ben Hutchings767e4682008-09-01 12:43:14 +01001943 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001944 struct efx_mac_stats *mac_stats = &efx->mac_stats;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001945
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001946 spin_lock_bh(&efx->stats_lock);
Ben Hutchings1cb34522011-09-02 23:23:00 +01001947
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001948 efx->type->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001949
1950 stats->rx_packets = mac_stats->rx_packets;
1951 stats->tx_packets = mac_stats->tx_packets;
1952 stats->rx_bytes = mac_stats->rx_bytes;
1953 stats->tx_bytes = mac_stats->tx_bytes;
Ben Hutchings80485d32010-09-10 06:41:06 +00001954 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001955 stats->multicast = mac_stats->rx_multicast;
1956 stats->collisions = mac_stats->tx_collision;
1957 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1958 mac_stats->rx_length_error);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001959 stats->rx_crc_errors = mac_stats->rx_bad;
1960 stats->rx_frame_errors = mac_stats->rx_align_error;
1961 stats->rx_fifo_errors = mac_stats->rx_overflow;
1962 stats->rx_missed_errors = mac_stats->rx_missed;
1963 stats->tx_window_errors = mac_stats->tx_late_collision;
1964
1965 stats->rx_errors = (stats->rx_length_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001966 stats->rx_crc_errors +
1967 stats->rx_frame_errors +
Ben Hutchings8ceee662008-04-27 12:55:59 +01001968 mac_stats->rx_symbol_error);
1969 stats->tx_errors = (stats->tx_window_errors +
1970 mac_stats->tx_bad);
1971
Ben Hutchings1cb34522011-09-02 23:23:00 +01001972 spin_unlock_bh(&efx->stats_lock);
1973
Ben Hutchings8ceee662008-04-27 12:55:59 +01001974 return stats;
1975}
1976
1977/* Context: netif_tx_lock held, BHs disabled. */
1978static void efx_watchdog(struct net_device *net_dev)
1979{
Ben Hutchings767e4682008-09-01 12:43:14 +01001980 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001981
Ben Hutchings62776d02010-06-23 11:30:07 +00001982 netif_err(efx, tx_err, efx->net_dev,
1983 "TX stuck with port_enabled=%d: resetting channels\n",
1984 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001985
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001986 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001987}
1988
1989
1990/* Context: process, rtnl_lock() held. */
1991static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1992{
Ben Hutchings767e4682008-09-01 12:43:14 +01001993 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001994 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001995
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001996 rc = efx_check_disabled(efx);
1997 if (rc)
1998 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001999 if (new_mtu > EFX_MAX_MTU)
2000 return -EINVAL;
2001
Ben Hutchings62776d02010-06-23 11:30:07 +00002002 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002003
Ben Hutchings29c69a42013-01-28 19:01:06 +00002004 efx_device_detach_sync(efx);
2005 efx_stop_all(efx);
2006
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002007 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002008 net_dev->mtu = new_mtu;
Ben Hutchings710b2082011-09-03 00:15:00 +01002009 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002010 mutex_unlock(&efx->mac_lock);
2011
Ben Hutchings8ceee662008-04-27 12:55:59 +01002012 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00002013 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002014 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002015}
2016
2017static int efx_set_mac_address(struct net_device *net_dev, void *data)
2018{
Ben Hutchings767e4682008-09-01 12:43:14 +01002019 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002020 struct sockaddr *addr = data;
2021 char *new_addr = addr->sa_data;
2022
Ben Hutchings8ceee662008-04-27 12:55:59 +01002023 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002024 netif_err(efx, drv, efx->net_dev,
2025 "invalid ethernet MAC address requested: %pM\n",
2026 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002027 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002028 }
2029
2030 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002031 efx_sriov_mac_address_changed(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002032
2033 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002034 mutex_lock(&efx->mac_lock);
Ben Hutchings710b2082011-09-03 00:15:00 +01002035 efx->type->reconfigure_mac(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002036 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002037
2038 return 0;
2039}
2040
Ben Hutchingsa816f752008-09-01 12:49:12 +01002041/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002042static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002043{
Ben Hutchings767e4682008-09-01 12:43:14 +01002044 struct efx_nic *efx = netdev_priv(net_dev);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002045 struct netdev_hw_addr *ha;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002046 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002047 u32 crc;
2048 int bit;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002049
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002050 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002051
2052 /* Build multicast hash table */
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002053 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002054 memset(mc_hash, 0xff, sizeof(*mc_hash));
2055 } else {
2056 memset(mc_hash, 0x00, sizeof(*mc_hash));
Jiri Pirko22bedad32010-04-01 21:22:57 +00002057 netdev_for_each_mc_addr(ha, net_dev) {
2058 crc = ether_crc_le(ETH_ALEN, ha->addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002059 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
Ben Hutchings32766ec2012-10-04 17:13:03 -07002060 __set_bit_le(bit, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002061 }
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002062
2063 /* Broadcast packets go through the multicast hash filter.
2064 * ether_crc_le() of the broadcast address is 0xbe2612ff
2065 * so we always add bit 0xff to the mask.
2066 */
Ben Hutchings32766ec2012-10-04 17:13:03 -07002067 __set_bit_le(0xff, mc_hash);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002068 }
2069
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002070 if (efx->port_enabled)
2071 queue_work(efx->workqueue, &efx->mac_work);
2072 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002073}
2074
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002075static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002076{
2077 struct efx_nic *efx = netdev_priv(net_dev);
2078
2079 /* If disabling RX n-tuple filtering, clear existing filters */
2080 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2081 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2082
2083 return 0;
2084}
2085
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002086static const struct net_device_ops efx_netdev_ops = {
2087 .ndo_open = efx_net_open,
2088 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002089 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002090 .ndo_tx_timeout = efx_watchdog,
2091 .ndo_start_xmit = efx_hard_start_xmit,
2092 .ndo_validate_addr = eth_validate_addr,
2093 .ndo_do_ioctl = efx_ioctl,
2094 .ndo_change_mtu = efx_change_mtu,
2095 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002096 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002097 .ndo_set_features = efx_set_features,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002098#ifdef CONFIG_SFC_SRIOV
2099 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2100 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2101 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2102 .ndo_get_vf_config = efx_sriov_get_vf_config,
2103#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002104#ifdef CONFIG_NET_POLL_CONTROLLER
2105 .ndo_poll_controller = efx_netpoll,
2106#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002107 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002108#ifdef CONFIG_RFS_ACCEL
2109 .ndo_rx_flow_steer = efx_filter_rfs,
2110#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002111};
2112
Ben Hutchings7dde5962008-12-12 22:09:38 -08002113static void efx_update_name(struct efx_nic *efx)
2114{
2115 strcpy(efx->name, efx->net_dev->name);
2116 efx_mtd_rename(efx);
2117 efx_set_channel_names(efx);
2118}
2119
Ben Hutchings8ceee662008-04-27 12:55:59 +01002120static int efx_netdev_event(struct notifier_block *this,
2121 unsigned long event, void *ptr)
2122{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01002123 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002124
Ben Hutchings7dde5962008-12-12 22:09:38 -08002125 if (net_dev->netdev_ops == &efx_netdev_ops &&
2126 event == NETDEV_CHANGENAME)
2127 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002128
2129 return NOTIFY_DONE;
2130}
2131
2132static struct notifier_block efx_netdev_notifier = {
2133 .notifier_call = efx_netdev_event,
2134};
2135
Ben Hutchings06d5e192008-12-12 21:47:23 -08002136static ssize_t
2137show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2138{
2139 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2140 return sprintf(buf, "%d\n", efx->phy_type);
2141}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002142static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002143
Ben Hutchings8ceee662008-04-27 12:55:59 +01002144static int efx_register_netdev(struct efx_nic *efx)
2145{
2146 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002147 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002148 int rc;
2149
2150 net_dev->watchdog_timeo = 5 * HZ;
2151 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002152 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002153 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002154 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002155
Ben Hutchings7dde5962008-12-12 22:09:38 -08002156 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002157
Ben Hutchings7153f622012-07-27 20:50:52 +01002158 /* Enable resets to be scheduled and check whether any were
2159 * already requested. If so, the NIC is probably hosed so we
2160 * abort.
2161 */
2162 efx->state = STATE_READY;
2163 smp_mb(); /* ensure we change state before checking reset_pending */
2164 if (efx->reset_pending) {
2165 netif_err(efx, probe, efx->net_dev,
2166 "aborting probe due to scheduled reset\n");
2167 rc = -EIO;
2168 goto fail_locked;
2169 }
2170
Ben Hutchingsaed06282009-08-26 08:16:27 +00002171 rc = dev_alloc_name(net_dev, net_dev->name);
2172 if (rc < 0)
2173 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002174 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002175
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002176 /* Always start with carrier off; PHY events will detect the link */
2177 netif_carrier_off(net_dev);
2178
Ben Hutchingsaed06282009-08-26 08:16:27 +00002179 rc = register_netdevice(net_dev);
2180 if (rc)
2181 goto fail_locked;
2182
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002183 efx_for_each_channel(channel, efx) {
2184 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002185 efx_for_each_channel_tx_queue(tx_queue, channel)
2186 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002187 }
2188
Ben Hutchings7dde5962008-12-12 22:09:38 -08002189 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002190
Ben Hutchings06d5e192008-12-12 21:47:23 -08002191 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2192 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002193 netif_err(efx, drv, efx->net_dev,
2194 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002195 goto fail_registered;
2196 }
2197
Ben Hutchings8ceee662008-04-27 12:55:59 +01002198 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002199
Ben Hutchings7153f622012-07-27 20:50:52 +01002200fail_registered:
2201 rtnl_lock();
2202 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002203fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002204 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002205 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002206 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002207 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002208}
2209
2210static void efx_unregister_netdev(struct efx_nic *efx)
2211{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002212 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002213 struct efx_tx_queue *tx_queue;
2214
2215 if (!efx->net_dev)
2216 return;
2217
Ben Hutchings767e4682008-09-01 12:43:14 +01002218 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002219
2220 /* Free up any skbs still remaining. This has to happen before
2221 * we try to unregister the netdev as running their destructors
2222 * may be needed to get the device ref. count to 0. */
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002223 efx_for_each_channel(channel, efx) {
2224 efx_for_each_channel_tx_queue(tx_queue, channel)
2225 efx_release_tx_buffers(tx_queue);
2226 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002227
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002228 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2229 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings7153f622012-07-27 20:50:52 +01002230
2231 rtnl_lock();
2232 unregister_netdevice(efx->net_dev);
2233 efx->state = STATE_UNINIT;
2234 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002235}
2236
2237/**************************************************************************
2238 *
2239 * Device reset and suspend
2240 *
2241 **************************************************************************/
2242
Ben Hutchings2467ca42008-09-01 12:48:50 +01002243/* Tears down the entire software state and most of the hardware state
2244 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002245void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002246{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002247 EFX_ASSERT_RESET_SERIALISED(efx);
2248
Ben Hutchings2467ca42008-09-01 12:48:50 +01002249 efx_stop_all(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00002250 efx_stop_interrupts(efx, false);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002251
2252 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +00002253 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2254 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002255 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002256}
2257
Ben Hutchings2467ca42008-09-01 12:48:50 +01002258/* This function will always ensure that the locks acquired in
2259 * efx_reset_down() are released. A failure return code indicates
2260 * that we were unable to reinitialise the hardware, and the
2261 * driver should be disabled. If ok is false, then the rx and tx
2262 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002263int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002264{
2265 int rc;
2266
Ben Hutchings2467ca42008-09-01 12:48:50 +01002267 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002268
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002269 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002270 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002271 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002272 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002273 }
2274
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002275 if (!ok)
2276 goto fail;
2277
Steve Hodgson4b988282009-01-29 17:50:51 +00002278 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002279 rc = efx->phy_op->init(efx);
2280 if (rc)
2281 goto fail;
2282 if (efx->phy_op->reconfigure(efx))
Ben Hutchings62776d02010-06-23 11:30:07 +00002283 netif_err(efx, drv, efx->net_dev,
2284 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002285 }
2286
Ben Hutchings710b2082011-09-03 00:15:00 +01002287 efx->type->reconfigure_mac(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002288
Ben Hutchings7f967c02012-02-13 23:45:02 +00002289 efx_start_interrupts(efx, false);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002290 efx_restore_filters(efx);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002291 efx_sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002292
2293 mutex_unlock(&efx->mac_lock);
2294
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002295 efx_start_all(efx);
2296
2297 return 0;
2298
2299fail:
2300 efx->port_initialized = false;
2301
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002302 mutex_unlock(&efx->mac_lock);
2303
Ben Hutchings8ceee662008-04-27 12:55:59 +01002304 return rc;
2305}
2306
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002307/* Reset the NIC using the specified method. Note that the reset may
2308 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002309 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002310 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002311 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002312int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002313{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002314 int rc, rc2;
2315 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002316
Ben Hutchings62776d02010-06-23 11:30:07 +00002317 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2318 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002319
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002320 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002321 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002322
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002323 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002324 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002325 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002326 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002327 }
2328
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002329 /* Clear flags for the scopes we covered. We assume the NIC and
2330 * driver are now quiescent so that there is no race here.
2331 */
2332 efx->reset_pending &= -(1 << (method + 1));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002333
2334 /* Reinitialise bus-mastering, which may have been turned off before
2335 * the reset was scheduled. This is still appropriate, even in the
2336 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2337 * can respond to requests. */
2338 pci_set_master(efx->pci_dev);
2339
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002340out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002341 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002342 disabled = rc ||
2343 method == RESET_TYPE_DISABLE ||
2344 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002345 rc2 = efx_reset_up(efx, method, !disabled);
2346 if (rc2) {
2347 disabled = true;
2348 if (!rc)
2349 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002350 }
2351
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002352 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002353 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002354 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002355 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002356 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002357 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002358 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002359 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002360 return rc;
2361}
2362
Alexandre Rames626950d2013-01-14 17:20:22 +00002363/* Try recovery mechanisms.
2364 * For now only EEH is supported.
2365 * Returns 0 if the recovery mechanisms are unsuccessful.
2366 * Returns a non-zero value otherwise.
2367 */
2368static int efx_try_recovery(struct efx_nic *efx)
2369{
2370#ifdef CONFIG_EEH
2371 /* A PCI error can occur and not be seen by EEH because nothing
2372 * happens on the PCI bus. In this case the driver may fail and
2373 * schedule a 'recover or reset', leading to this recovery handler.
2374 * Manually call the eeh failure check function.
2375 */
2376 struct eeh_dev *eehdev =
2377 of_node_to_eeh_dev(pci_device_to_OF_node(efx->pci_dev));
2378
2379 if (eeh_dev_check_failure(eehdev)) {
2380 /* The EEH mechanisms will handle the error and reset the
2381 * device if necessary.
2382 */
2383 return 1;
2384 }
2385#endif
2386 return 0;
2387}
2388
Ben Hutchings8ceee662008-04-27 12:55:59 +01002389/* The worker thread exists so that code that cannot sleep can
2390 * schedule a reset for later.
2391 */
2392static void efx_reset_work(struct work_struct *data)
2393{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002394 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002395 unsigned long pending;
2396 enum reset_type method;
2397
2398 pending = ACCESS_ONCE(efx->reset_pending);
2399 method = fls(pending) - 1;
2400
2401 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2402 method == RESET_TYPE_RECOVER_OR_ALL) &&
2403 efx_try_recovery(efx))
2404 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002405
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002406 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002407 return;
2408
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002409 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002410
2411 /* We checked the state in efx_schedule_reset() but it may
2412 * have changed by now. Now that we have the RTNL lock,
2413 * it cannot change again.
2414 */
2415 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002416 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002417
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002418 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002419}
2420
2421void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2422{
2423 enum reset_type method;
2424
Alexandre Rames626950d2013-01-14 17:20:22 +00002425 if (efx->state == STATE_RECOVERY) {
2426 netif_dbg(efx, drv, efx->net_dev,
2427 "recovering: skip scheduling %s reset\n",
2428 RESET_TYPE(type));
2429 return;
2430 }
2431
Ben Hutchings8ceee662008-04-27 12:55:59 +01002432 switch (type) {
2433 case RESET_TYPE_INVISIBLE:
2434 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002435 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002436 case RESET_TYPE_WORLD:
2437 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002438 case RESET_TYPE_RECOVER_OR_DISABLE:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002439 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002440 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2441 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002442 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002443 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002444 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002445 netif_dbg(efx, drv, efx->net_dev,
2446 "scheduling %s reset for %s\n",
2447 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002448 break;
2449 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002450
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002451 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002452 smp_mb(); /* ensure we change reset_pending before checking state */
2453
2454 /* If we're not READY then just leave the flags set as the cue
2455 * to abort probing or reschedule the reset later.
2456 */
2457 if (ACCESS_ONCE(efx->state) != STATE_READY)
2458 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002459
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002460 /* efx_process_channel() will no longer read events once a
2461 * reset is scheduled. So switch back to poll'd MCDI completions. */
2462 efx_mcdi_mode_poll(efx);
2463
Steve Hodgson1ab00622008-12-12 21:33:02 -08002464 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002465}
2466
2467/**************************************************************************
2468 *
2469 * List of NICs we support
2470 *
2471 **************************************************************************/
2472
2473/* PCI device ID table */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00002474static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002475 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2476 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002477 .driver_data = (unsigned long) &falcon_a1_nic_type},
Linus Torvalds0e59e7e72011-10-28 14:20:44 -07002478 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2479 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
Ben Hutchingsdaeda632009-11-28 05:36:04 +00002480 .driver_data = (unsigned long) &falcon_b0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002481 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002482 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002483 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002484 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002485 {0} /* end of list */
2486};
2487
2488/**************************************************************************
2489 *
Ben Hutchings37594332009-11-23 16:05:45 +00002490 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002491 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002492 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002493 * Needed so all function pointers are valid and do not have to be tested
2494 * before use
2495 *
2496 **************************************************************************/
2497int efx_port_dummy_op_int(struct efx_nic *efx)
2498{
2499 return 0;
2500}
2501void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002502
2503static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002504{
2505 return false;
2506}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002507
stephen hemminger6c8c2512011-04-14 05:50:12 +00002508static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002509 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002510 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002511 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002512 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002513};
2514
Ben Hutchings8ceee662008-04-27 12:55:59 +01002515/**************************************************************************
2516 *
2517 * Data housekeeping
2518 *
2519 **************************************************************************/
2520
2521/* This zeroes out and then fills in the invariants in a struct
2522 * efx_nic (including all sub-structures).
2523 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002524static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002525 struct pci_dev *pci_dev, struct net_device *net_dev)
2526{
Ben Hutchings46426102010-09-10 06:42:33 +00002527 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002528
2529 /* Initialise common structures */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002530 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002531#ifdef CONFIG_SFC_MTD
2532 INIT_LIST_HEAD(&efx->mtd_list);
2533#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002534 INIT_WORK(&efx->reset_work, efx_reset_work);
2535 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002536 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002537 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002538 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002539 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002540 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002541
2542 efx->net_dev = net_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002543 spin_lock_init(&efx->stats_lock);
2544 mutex_init(&efx->mac_lock);
2545 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002546 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002547 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002548 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002549
2550 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002551 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2552 if (!efx->channel[i])
2553 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002554 }
2555
Ben Hutchings8ceee662008-04-27 12:55:59 +01002556 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2557
2558 /* Higher numbered interrupt modes are less capable! */
2559 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2560 interrupt_mode);
2561
Ben Hutchings6977dc62008-12-26 13:44:39 -08002562 /* Would be good to use the net_dev name, but we're too early */
2563 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2564 pci_name(pci_dev));
2565 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002566 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002567 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002568
Ben Hutchings8ceee662008-04-27 12:55:59 +01002569 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002570
2571fail:
2572 efx_fini_struct(efx);
2573 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002574}
2575
2576static void efx_fini_struct(struct efx_nic *efx)
2577{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002578 int i;
2579
2580 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2581 kfree(efx->channel[i]);
2582
Ben Hutchings8ceee662008-04-27 12:55:59 +01002583 if (efx->workqueue) {
2584 destroy_workqueue(efx->workqueue);
2585 efx->workqueue = NULL;
2586 }
2587}
2588
2589/**************************************************************************
2590 *
2591 * PCI interface
2592 *
2593 **************************************************************************/
2594
2595/* Main body of final NIC shutdown code
2596 * This is called only at module unload (or hotplug removal).
2597 */
2598static void efx_pci_remove_main(struct efx_nic *efx)
2599{
Ben Hutchings7153f622012-07-27 20:50:52 +01002600 /* Flush reset_work. It can no longer be scheduled since we
2601 * are not READY.
2602 */
2603 BUG_ON(efx->state == STATE_READY);
2604 cancel_work_sync(&efx->reset_work);
2605
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002606#ifdef CONFIG_RFS_ACCEL
2607 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2608 efx->net_dev->rx_cpu_rmap = NULL;
2609#endif
Ben Hutchings7f967c02012-02-13 23:45:02 +00002610 efx_stop_interrupts(efx, false);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002611 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002612 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002613 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002614 efx_fini_napi(efx);
2615 efx_remove_all(efx);
2616}
2617
2618/* Final NIC shutdown
2619 * This is called only at module unload (or hotplug removal).
2620 */
2621static void efx_pci_remove(struct pci_dev *pci_dev)
2622{
2623 struct efx_nic *efx;
2624
2625 efx = pci_get_drvdata(pci_dev);
2626 if (!efx)
2627 return;
2628
2629 /* Mark the NIC as fini, then stop the interface */
2630 rtnl_lock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002631 dev_close(efx->net_dev);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002632 efx_stop_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002633 rtnl_unlock();
2634
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002635 efx_sriov_fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002636 efx_unregister_netdev(efx);
2637
Ben Hutchings7dde5962008-12-12 22:09:38 -08002638 efx_mtd_remove(efx);
2639
Ben Hutchings8ceee662008-04-27 12:55:59 +01002640 efx_pci_remove_main(efx);
2641
Ben Hutchings8ceee662008-04-27 12:55:59 +01002642 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002643 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002644
Ben Hutchings8ceee662008-04-27 12:55:59 +01002645 efx_fini_struct(efx);
Ben Hutchings3de4e302012-04-05 00:22:19 +01002646 pci_set_drvdata(pci_dev, NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002647 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002648
2649 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002650};
2651
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002652/* NIC VPD information
2653 * Called during probe to display the part number of the
2654 * installed NIC. VPD is potentially very large but this should
2655 * always appear within the first 512 bytes.
2656 */
2657#define SFC_VPD_LEN 512
2658static void efx_print_product_vpd(struct efx_nic *efx)
2659{
2660 struct pci_dev *dev = efx->pci_dev;
2661 char vpd_data[SFC_VPD_LEN];
2662 ssize_t vpd_size;
2663 int i, j;
2664
2665 /* Get the vpd data from the device */
2666 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2667 if (vpd_size <= 0) {
2668 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2669 return;
2670 }
2671
2672 /* Get the Read only section */
2673 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2674 if (i < 0) {
2675 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2676 return;
2677 }
2678
2679 j = pci_vpd_lrdt_size(&vpd_data[i]);
2680 i += PCI_VPD_LRDT_TAG_SIZE;
2681 if (i + j > vpd_size)
2682 j = vpd_size - i;
2683
2684 /* Get the Part number */
2685 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2686 if (i < 0) {
2687 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2688 return;
2689 }
2690
2691 j = pci_vpd_info_field_size(&vpd_data[i]);
2692 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2693 if (i + j > vpd_size) {
2694 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2695 return;
2696 }
2697
2698 netif_info(efx, drv, efx->net_dev,
2699 "Part Number : %.*s\n", j, &vpd_data[i]);
2700}
2701
2702
Ben Hutchings8ceee662008-04-27 12:55:59 +01002703/* Main body of NIC initialisation
2704 * This is called at module load (or hotplug insertion, theoretically).
2705 */
2706static int efx_pci_probe_main(struct efx_nic *efx)
2707{
2708 int rc;
2709
2710 /* Do start-of-day initialisation */
2711 rc = efx_probe_all(efx);
2712 if (rc)
2713 goto fail1;
2714
Ben Hutchingse8f14992010-12-07 19:47:34 +00002715 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002716
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002717 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002718 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002719 netif_err(efx, probe, efx->net_dev,
2720 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002721 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002722 }
2723
2724 rc = efx_init_port(efx);
2725 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002726 netif_err(efx, probe, efx->net_dev,
2727 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002728 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002729 }
2730
Ben Hutchings152b6a62009-11-29 03:43:56 +00002731 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002732 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002733 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00002734 efx_start_interrupts(efx, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002735
2736 return 0;
2737
Ben Hutchings278c0622009-11-23 16:05:12 +00002738 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002739 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002740 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002741 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002742 fail3:
2743 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002744 efx_remove_all(efx);
2745 fail1:
2746 return rc;
2747}
2748
2749/* NIC initialisation
2750 *
2751 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00002752 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002753 * sets up and registers the network devices with the kernel and hooks
2754 * the interrupt service routine. It does not prepare the device for
2755 * transmission; this is left to the first time one of the network
2756 * interfaces is brought up (i.e. efx_net_open).
2757 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05002758static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00002759 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002760{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002761 struct net_device *net_dev;
2762 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002763 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002764
2765 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00002766 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2767 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002768 if (!net_dev)
2769 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002770 efx = netdev_priv(net_dev);
2771 efx->type = (const struct efx_nic_type *) entry->driver_data;
2772 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002773 NETIF_F_HIGHDMA | NETIF_F_TSO |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002774 NETIF_F_RXCSUM);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002775 if (efx->type->offload_features & NETIF_F_V6_CSUM)
Ben Hutchings738a8f42009-11-29 15:16:05 +00002776 net_dev->features |= NETIF_F_TSO6;
Ben Hutchings285065632008-09-01 12:46:54 +01002777 /* Mask for features that also apply to VLAN devices */
2778 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002779 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2780 NETIF_F_RXCSUM);
2781 /* All offloads can be toggled */
2782 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002783 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00002784 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002785 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002786 if (rc)
2787 goto fail1;
2788
Ben Hutchings62776d02010-06-23 11:30:07 +00002789 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01002790 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002791
Ben Hutchings460eeaa2012-03-05 15:35:39 +00002792 efx_print_product_vpd(efx);
2793
Ben Hutchings8ceee662008-04-27 12:55:59 +01002794 /* Set up basic I/O (BAR mappings etc) */
2795 rc = efx_init_io(efx);
2796 if (rc)
2797 goto fail2;
2798
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002799 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002800 if (rc)
2801 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002802
Ben Hutchings8ceee662008-04-27 12:55:59 +01002803 rc = efx_register_netdev(efx);
2804 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002805 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002806
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002807 rc = efx_sriov_init(efx);
2808 if (rc)
2809 netif_err(efx, probe, efx->net_dev,
2810 "SR-IOV can't be enabled rc %d\n", rc);
2811
Ben Hutchings62776d02010-06-23 11:30:07 +00002812 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002813
Ben Hutchings7c431612012-01-27 17:23:58 +00002814 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002815 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002816 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002817 rtnl_unlock();
Ben Hutchings7c431612012-01-27 17:23:58 +00002818 if (rc)
2819 netif_warn(efx, probe, efx->net_dev,
2820 "failed to create MTDs (%d)\n", rc);
2821
Alexandre Rames626950d2013-01-14 17:20:22 +00002822 rc = pci_enable_pcie_error_reporting(pci_dev);
2823 if (rc && rc != -EINVAL)
2824 netif_warn(efx, probe, efx->net_dev,
2825 "pci_enable_pcie_error_reporting failed (%d)\n", rc);
2826
Ben Hutchings8ceee662008-04-27 12:55:59 +01002827 return 0;
2828
Ben Hutchings8ceee662008-04-27 12:55:59 +01002829 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00002830 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002831 fail3:
2832 efx_fini_io(efx);
2833 fail2:
2834 efx_fini_struct(efx);
2835 fail1:
Ben Hutchings3de4e302012-04-05 00:22:19 +01002836 pci_set_drvdata(pci_dev, NULL);
Steve Hodgson5e2a9112010-02-12 12:32:27 -08002837 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00002838 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002839 free_netdev(net_dev);
2840 return rc;
2841}
2842
Ben Hutchings89c758f2009-11-29 03:43:07 +00002843static int efx_pm_freeze(struct device *dev)
2844{
2845 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2846
Ben Hutchings61da0262012-07-27 19:35:39 +01002847 rtnl_lock();
2848
Ben Hutchings6032fb52012-07-27 19:35:47 +01002849 if (efx->state != STATE_DISABLED) {
2850 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002851
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002852 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002853
Ben Hutchings6032fb52012-07-27 19:35:47 +01002854 efx_stop_all(efx);
2855 efx_stop_interrupts(efx, false);
2856 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002857
Ben Hutchings61da0262012-07-27 19:35:39 +01002858 rtnl_unlock();
2859
Ben Hutchings89c758f2009-11-29 03:43:07 +00002860 return 0;
2861}
2862
2863static int efx_pm_thaw(struct device *dev)
2864{
2865 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2866
Ben Hutchings61da0262012-07-27 19:35:39 +01002867 rtnl_lock();
2868
Ben Hutchings6032fb52012-07-27 19:35:47 +01002869 if (efx->state != STATE_DISABLED) {
2870 efx_start_interrupts(efx, false);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002871
Ben Hutchings6032fb52012-07-27 19:35:47 +01002872 mutex_lock(&efx->mac_lock);
2873 efx->phy_op->reconfigure(efx);
2874 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002875
Ben Hutchings6032fb52012-07-27 19:35:47 +01002876 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002877
Ben Hutchings6032fb52012-07-27 19:35:47 +01002878 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00002879
Ben Hutchings6032fb52012-07-27 19:35:47 +01002880 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002881
Ben Hutchings6032fb52012-07-27 19:35:47 +01002882 efx->type->resume_wol(efx);
2883 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00002884
Ben Hutchings61da0262012-07-27 19:35:39 +01002885 rtnl_unlock();
2886
Steve Hodgson319ba642010-06-01 11:17:24 +00002887 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2888 queue_work(reset_workqueue, &efx->reset_work);
2889
Ben Hutchings89c758f2009-11-29 03:43:07 +00002890 return 0;
2891}
2892
2893static int efx_pm_poweroff(struct device *dev)
2894{
2895 struct pci_dev *pci_dev = to_pci_dev(dev);
2896 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2897
2898 efx->type->fini(efx);
2899
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002900 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00002901
2902 pci_save_state(pci_dev);
2903 return pci_set_power_state(pci_dev, PCI_D3hot);
2904}
2905
2906/* Used for both resume and restore */
2907static int efx_pm_resume(struct device *dev)
2908{
2909 struct pci_dev *pci_dev = to_pci_dev(dev);
2910 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2911 int rc;
2912
2913 rc = pci_set_power_state(pci_dev, PCI_D0);
2914 if (rc)
2915 return rc;
2916 pci_restore_state(pci_dev);
2917 rc = pci_enable_device(pci_dev);
2918 if (rc)
2919 return rc;
2920 pci_set_master(efx->pci_dev);
2921 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2922 if (rc)
2923 return rc;
2924 rc = efx->type->init(efx);
2925 if (rc)
2926 return rc;
2927 efx_pm_thaw(dev);
2928 return 0;
2929}
2930
2931static int efx_pm_suspend(struct device *dev)
2932{
2933 int rc;
2934
2935 efx_pm_freeze(dev);
2936 rc = efx_pm_poweroff(dev);
2937 if (rc)
2938 efx_pm_resume(dev);
2939 return rc;
2940}
2941
Ben Hutchings18e83e42012-01-05 19:05:20 +00002942static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00002943 .suspend = efx_pm_suspend,
2944 .resume = efx_pm_resume,
2945 .freeze = efx_pm_freeze,
2946 .thaw = efx_pm_thaw,
2947 .poweroff = efx_pm_poweroff,
2948 .restore = efx_pm_resume,
2949};
2950
Alexandre Rames626950d2013-01-14 17:20:22 +00002951/* A PCI error affecting this device was detected.
2952 * At this point MMIO and DMA may be disabled.
2953 * Stop the software path and request a slot reset.
2954 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002955static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
2956 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00002957{
2958 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2959 struct efx_nic *efx = pci_get_drvdata(pdev);
2960
2961 if (state == pci_channel_io_perm_failure)
2962 return PCI_ERS_RESULT_DISCONNECT;
2963
2964 rtnl_lock();
2965
2966 if (efx->state != STATE_DISABLED) {
2967 efx->state = STATE_RECOVERY;
2968 efx->reset_pending = 0;
2969
2970 efx_device_detach_sync(efx);
2971
2972 efx_stop_all(efx);
2973 efx_stop_interrupts(efx, false);
2974
2975 status = PCI_ERS_RESULT_NEED_RESET;
2976 } else {
2977 /* If the interface is disabled we don't want to do anything
2978 * with it.
2979 */
2980 status = PCI_ERS_RESULT_RECOVERED;
2981 }
2982
2983 rtnl_unlock();
2984
2985 pci_disable_device(pdev);
2986
2987 return status;
2988}
2989
2990/* Fake a successfull reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00002991static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00002992{
2993 struct efx_nic *efx = pci_get_drvdata(pdev);
2994 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
2995 int rc;
2996
2997 if (pci_enable_device(pdev)) {
2998 netif_err(efx, hw, efx->net_dev,
2999 "Cannot re-enable PCI device after reset.\n");
3000 status = PCI_ERS_RESULT_DISCONNECT;
3001 }
3002
3003 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
3004 if (rc) {
3005 netif_err(efx, hw, efx->net_dev,
3006 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3007 /* Non-fatal error. Continue. */
3008 }
3009
3010 return status;
3011}
3012
3013/* Perform the actual reset and resume I/O operations. */
3014static void efx_io_resume(struct pci_dev *pdev)
3015{
3016 struct efx_nic *efx = pci_get_drvdata(pdev);
3017 int rc;
3018
3019 rtnl_lock();
3020
3021 if (efx->state == STATE_DISABLED)
3022 goto out;
3023
3024 rc = efx_reset(efx, RESET_TYPE_ALL);
3025 if (rc) {
3026 netif_err(efx, hw, efx->net_dev,
3027 "efx_reset failed after PCI error (%d)\n", rc);
3028 } else {
3029 efx->state = STATE_READY;
3030 netif_dbg(efx, hw, efx->net_dev,
3031 "Done resetting and resuming IO after PCI error.\n");
3032 }
3033
3034out:
3035 rtnl_unlock();
3036}
3037
3038/* For simplicity and reliability, we always require a slot reset and try to
3039 * reset the hardware when a pci error affecting the device is detected.
3040 * We leave both the link_reset and mmio_enabled callback unimplemented:
3041 * with our request for slot reset the mmio_enabled callback will never be
3042 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3043 */
3044static struct pci_error_handlers efx_err_handlers = {
3045 .error_detected = efx_io_error_detected,
3046 .slot_reset = efx_io_slot_reset,
3047 .resume = efx_io_resume,
3048};
3049
Ben Hutchings8ceee662008-04-27 12:55:59 +01003050static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003051 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003052 .id_table = efx_pci_table,
3053 .probe = efx_pci_probe,
3054 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003055 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003056 .err_handler = &efx_err_handlers,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003057};
3058
3059/**************************************************************************
3060 *
3061 * Kernel module interface
3062 *
3063 *************************************************************************/
3064
3065module_param(interrupt_mode, uint, 0444);
3066MODULE_PARM_DESC(interrupt_mode,
3067 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3068
3069static int __init efx_init_module(void)
3070{
3071 int rc;
3072
3073 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3074
3075 rc = register_netdevice_notifier(&efx_netdev_notifier);
3076 if (rc)
3077 goto err_notifier;
3078
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003079 rc = efx_init_sriov();
3080 if (rc)
3081 goto err_sriov;
3082
Steve Hodgson1ab00622008-12-12 21:33:02 -08003083 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3084 if (!reset_workqueue) {
3085 rc = -ENOMEM;
3086 goto err_reset;
3087 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003088
3089 rc = pci_register_driver(&efx_pci_driver);
3090 if (rc < 0)
3091 goto err_pci;
3092
3093 return 0;
3094
3095 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003096 destroy_workqueue(reset_workqueue);
3097 err_reset:
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003098 efx_fini_sriov();
3099 err_sriov:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003100 unregister_netdevice_notifier(&efx_netdev_notifier);
3101 err_notifier:
3102 return rc;
3103}
3104
3105static void __exit efx_exit_module(void)
3106{
3107 printk(KERN_INFO "Solarflare NET driver unloading\n");
3108
3109 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003110 destroy_workqueue(reset_workqueue);
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003111 efx_fini_sriov();
Ben Hutchings8ceee662008-04-27 12:55:59 +01003112 unregister_netdevice_notifier(&efx_netdev_notifier);
3113
3114}
3115
3116module_init(efx_init_module);
3117module_exit(efx_exit_module);
3118
Ben Hutchings906bb262009-11-29 15:16:19 +00003119MODULE_AUTHOR("Solarflare Communications and "
3120 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003121MODULE_DESCRIPTION("Solarflare Communications network driver");
3122MODULE_LICENSE("GPL");
3123MODULE_DEVICE_TABLE(pci, efx_pci_table);