blob: 4a234a8d38965fa78f22baf140f3c885c47a9954 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01002 * Driver for Solarflare network controllers and boards
Ben Hutchings8ceee662008-04-27 12:55:59 +01003 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01004 * Copyright 2005-2013 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>
Ben Hutchings8ceee662008-04-27 12:55:59 +010020#include <linux/ethtool.h>
Ben Hutchingsaa6ef272008-07-18 19:03:10 +010021#include <linux/topology.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Alexandre Rames626950d2013-01-14 17:20:22 +000023#include <linux/aer.h>
Alexandre Ramesb28405b2013-03-21 16:41:43 +000024#include <linux/interrupt.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010025#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000027#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000028#include "selftest.h"
Shradha Shah7fa8d542015-05-06 00:55:13 +010029#include "sriov.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",
Jon Cooper087e9022015-05-20 11:11:35 +010080 [RESET_TYPE_DATAPATH] = "DATAPATH",
Edward Creee2835462014-04-16 19:27:48 +010081 [RESET_TYPE_MC_BIST] = "MC_BIST",
Alexandre Rames626950d2013-01-14 17:20:22 +000082 [RESET_TYPE_DISABLE] = "DISABLE",
83 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
84 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
85 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
Alexandre Rames3de82b92013-06-13 11:36:15 +010086 [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
Alexandre Rames626950d2013-01-14 17:20:22 +000087 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
88 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Edward Creee2835462014-04-16 19:27:48 +010089 [RESET_TYPE_MCDI_TIMEOUT] = "MCDI_TIMEOUT (FLR)",
Ben Hutchingsc4593022009-11-23 16:08:17 +000090};
91
Steve Hodgson1ab00622008-12-12 21:33:02 -080092/* Reset workqueue. If any NIC has a hardware failure then a reset will be
93 * queued onto this work queue. This is not a per-nic work queue, because
94 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
95 */
96static struct workqueue_struct *reset_workqueue;
97
Jon Cooper74cd60a2013-09-16 14:18:51 +010098/* How often and how many times to poll for a reset while waiting for a
99 * BIST that another function started to complete.
100 */
101#define BIST_WAIT_DELAY_MS 100
102#define BIST_WAIT_DELAY_COUNT 100
103
Ben Hutchings8ceee662008-04-27 12:55:59 +0100104/**************************************************************************
105 *
106 * Configurable values
107 *
108 *************************************************************************/
109
110/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100111 * Use separate channels for TX and RX events
112 *
Neil Turton28b581a2008-12-12 21:41:06 -0800113 * Set this to 1 to use separate channels for TX and RX. It allows us
114 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100115 *
Neil Turton28b581a2008-12-12 21:41:06 -0800116 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100117 */
Shradha Shahb0fbdae2015-08-28 10:55:42 +0100118bool efx_separate_tx_channels;
119module_param(efx_separate_tx_channels, bool, 0444);
120MODULE_PARM_DESC(efx_separate_tx_channels,
Neil Turton28b581a2008-12-12 21:41:06 -0800121 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100122
123/* This is the weight assigned to each of the (per-channel) virtual
124 * NAPI devices.
125 */
126static int napi_weight = 64;
127
128/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000129 * monitor.
130 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000131 * - Check the on-board hardware monitor;
132 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000133 * On Siena-based NICs for power systems with EEH support, this will give EEH a
134 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100135 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000136static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100137
Ben Hutchings8ceee662008-04-27 12:55:59 +0100138/* Initial interrupt moderation settings. They can be modified after
139 * module load with ethtool.
140 *
141 * The default for RX should strike a balance between increasing the
142 * round-trip latency and reducing overhead.
143 */
144static unsigned int rx_irq_mod_usec = 60;
145
146/* Initial interrupt moderation settings. They can be modified after
147 * module load with ethtool.
148 *
149 * This default is chosen to ensure that a 10G link does not go idle
150 * while a TX queue is stopped after it has become full. A queue is
151 * restarted when it drops below half full. The time this takes (assuming
152 * worst case 3 descriptors per packet and 1024 descriptors) is
153 * 512 / 3 * 1.2 = 205 usec.
154 */
155static unsigned int tx_irq_mod_usec = 150;
156
157/* This is the first interrupt mode to try out of:
158 * 0 => MSI-X
159 * 1 => MSI
160 * 2 => legacy
161 */
162static unsigned int interrupt_mode;
163
164/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
165 * i.e. the number of CPUs among which we may distribute simultaneous
166 * interrupt handling.
167 *
168 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000169 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100170 */
171static unsigned int rss_cpus;
172module_param(rss_cpus, uint, 0444);
173MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
174
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000175static bool phy_flash_cfg;
176module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800177MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
178
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000179static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000180module_param(irq_adapt_low_thresh, uint, 0644);
181MODULE_PARM_DESC(irq_adapt_low_thresh,
182 "Threshold score for reducing IRQ moderation");
183
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000184static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000185module_param(irq_adapt_high_thresh, uint, 0644);
186MODULE_PARM_DESC(irq_adapt_high_thresh,
187 "Threshold score for increasing IRQ moderation");
188
Ben Hutchings62776d02010-06-23 11:30:07 +0000189static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
190 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
191 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
192 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
193module_param(debug, uint, 0);
194MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
195
Ben Hutchings8ceee662008-04-27 12:55:59 +0100196/**************************************************************************
197 *
198 * Utility functions and prototypes
199 *
200 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000201
Jon Cooper261e4d92013-04-15 18:51:54 +0100202static int efx_soft_enable_interrupts(struct efx_nic *efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100203static void efx_soft_disable_interrupts(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000204static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000205static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000206static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100207static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000208static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100209static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000210static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000211static void efx_fini_struct(struct efx_nic *efx);
212static void efx_start_all(struct efx_nic *efx);
213static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100214
215#define EFX_ASSERT_RESET_SERIALISED(efx) \
216 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100217 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000218 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000219 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100220 ASSERT_RTNL(); \
221 } while (0)
222
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100223static int efx_check_disabled(struct efx_nic *efx)
224{
Alexandre Rames626950d2013-01-14 17:20:22 +0000225 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100226 netif_err(efx, drv, efx->net_dev,
227 "device is disabled due to earlier errors\n");
228 return -EIO;
229 }
230 return 0;
231}
232
Ben Hutchings8ceee662008-04-27 12:55:59 +0100233/**************************************************************************
234 *
235 * Event queue processing
236 *
237 *************************************************************************/
238
239/* Process channel's event queue
240 *
241 * This function is responsible for processing the event queue of a
242 * single channel. The caller must guarantee that this function will
243 * never be concurrently called more than once on the same channel,
244 * though different channels may be being processed concurrently.
245 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000246static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100247{
Peter Dunningc9368352015-07-08 10:05:10 +0100248 struct efx_tx_queue *tx_queue;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000249 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100250
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000251 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100252 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100253
Peter Dunningc9368352015-07-08 10:05:10 +0100254 efx_for_each_channel_tx_queue(tx_queue, channel) {
255 tx_queue->pkts_compl = 0;
256 tx_queue->bytes_compl = 0;
257 }
258
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000259 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000260 if (spent && efx_channel_has_rx_queue(channel)) {
261 struct efx_rx_queue *rx_queue =
262 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100263
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000264 efx_rx_flush_packet(channel);
Jon Coopercce28792013-10-02 11:04:14 +0100265 efx_fast_push_rx_descriptors(rx_queue, true);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100266 }
267
Peter Dunningc9368352015-07-08 10:05:10 +0100268 /* Update BQL */
269 efx_for_each_channel_tx_queue(tx_queue, channel) {
270 if (tx_queue->bytes_compl) {
271 netdev_tx_completed_queue(tx_queue->core_txq,
272 tx_queue->pkts_compl, tx_queue->bytes_compl);
273 }
274 }
275
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000276 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100277}
278
Ben Hutchings8ceee662008-04-27 12:55:59 +0100279/* NAPI poll handler
280 *
281 * NAPI guarantees serialisation of polls of the same device, which
282 * provides the guarantee required by efx_process_channel().
283 */
Bert Kenward539de7c2016-08-11 13:02:09 +0100284static void efx_update_irq_mod(struct efx_nic *efx, struct efx_channel *channel)
285{
286 int step = efx->irq_mod_step_us;
287
288 if (channel->irq_mod_score < irq_adapt_low_thresh) {
289 if (channel->irq_moderation_us > step) {
290 channel->irq_moderation_us -= step;
291 efx->type->push_irq_moderation(channel);
292 }
293 } else if (channel->irq_mod_score > irq_adapt_high_thresh) {
294 if (channel->irq_moderation_us <
295 efx->irq_rx_moderation_us) {
296 channel->irq_moderation_us += step;
297 efx->type->push_irq_moderation(channel);
298 }
299 }
300
301 channel->irq_count = 0;
302 channel->irq_mod_score = 0;
303}
304
Ben Hutchings8ceee662008-04-27 12:55:59 +0100305static int efx_poll(struct napi_struct *napi, int budget)
306{
307 struct efx_channel *channel =
308 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000309 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000310 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100311
Alexandre Rames36763262014-07-22 14:03:25 +0100312 if (!efx_channel_lock_napi(channel))
313 return budget;
314
Ben Hutchings62776d02010-06-23 11:30:07 +0000315 netif_vdbg(efx, intr, efx->net_dev,
316 "channel %d NAPI poll executing on CPU %d\n",
317 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100318
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000319 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100320
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000321 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000322 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000323 efx->irq_rx_adaptive &&
324 unlikely(++channel->irq_count == 1000)) {
Bert Kenward539de7c2016-08-11 13:02:09 +0100325 efx_update_irq_mod(efx, channel);
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000326 }
327
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000328 efx_filter_rfs_expire(channel);
329
Ben Hutchings8ceee662008-04-27 12:55:59 +0100330 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800331 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100332 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100333 * interrupts have already been disabled.
334 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800335 napi_complete(napi);
Ben Hutchings514bedb2012-10-05 19:30:16 +0100336 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100337 }
338
Alexandre Rames36763262014-07-22 14:03:25 +0100339 efx_channel_unlock_napi(channel);
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000340 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100341}
342
Ben Hutchings8ceee662008-04-27 12:55:59 +0100343/* Create event queue
344 * Event queue memory allocations are done only once. If the channel
345 * is reset, the memory buffer will be reused; this guards against
346 * errors during channel reset and also simplifies interrupt handling.
347 */
348static int efx_probe_eventq(struct efx_channel *channel)
349{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000350 struct efx_nic *efx = channel->efx;
351 unsigned long entries;
352
Ben Hutchings86ee5302012-01-09 19:51:22 +0000353 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000354 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100355
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000356 /* Build an event queue with room for one event per tx and rx buffer,
357 * plus some extra for link state events and MCDI completions. */
358 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
359 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
360 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
361
Ben Hutchings152b6a62009-11-29 03:43:56 +0000362 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100363}
364
365/* Prepare channel's event queue */
Jon Cooper261e4d92013-04-15 18:51:54 +0100366static int efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100367{
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100368 struct efx_nic *efx = channel->efx;
Jon Cooper261e4d92013-04-15 18:51:54 +0100369 int rc;
370
371 EFX_WARN_ON_PARANOID(channel->eventq_init);
372
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100373 netif_dbg(efx, drv, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000374 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100375
Jon Cooper261e4d92013-04-15 18:51:54 +0100376 rc = efx_nic_init_eventq(channel);
377 if (rc == 0) {
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100378 efx->type->push_irq_moderation(channel);
Jon Cooper261e4d92013-04-15 18:51:54 +0100379 channel->eventq_read_ptr = 0;
380 channel->eventq_init = true;
381 }
382 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100383}
384
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000385/* Enable event queue processing and NAPI */
Alexandre Rames36763262014-07-22 14:03:25 +0100386void efx_start_eventq(struct efx_channel *channel)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000387{
388 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
389 "chan %d start event queue\n", channel->channel);
390
Ben Hutchings514bedb2012-10-05 19:30:16 +0100391 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000392 channel->enabled = true;
393 smp_wmb();
394
Alexandre Rames36763262014-07-22 14:03:25 +0100395 efx_channel_enable(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000396 napi_enable(&channel->napi_str);
397 efx_nic_eventq_read_ack(channel);
398}
399
400/* Disable event queue processing and NAPI */
Alexandre Rames36763262014-07-22 14:03:25 +0100401void efx_stop_eventq(struct efx_channel *channel)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000402{
403 if (!channel->enabled)
404 return;
405
406 napi_disable(&channel->napi_str);
Alexandre Rames36763262014-07-22 14:03:25 +0100407 while (!efx_channel_disable(channel))
408 usleep_range(1000, 20000);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000409 channel->enabled = false;
410}
411
Ben Hutchings8ceee662008-04-27 12:55:59 +0100412static void efx_fini_eventq(struct efx_channel *channel)
413{
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100414 if (!channel->eventq_init)
415 return;
416
Ben Hutchings62776d02010-06-23 11:30:07 +0000417 netif_dbg(channel->efx, drv, channel->efx->net_dev,
418 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100419
Ben Hutchings152b6a62009-11-29 03:43:56 +0000420 efx_nic_fini_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100421 channel->eventq_init = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100422}
423
424static void efx_remove_eventq(struct efx_channel *channel)
425{
Ben Hutchings62776d02010-06-23 11:30:07 +0000426 netif_dbg(channel->efx, drv, channel->efx->net_dev,
427 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100428
Ben Hutchings152b6a62009-11-29 03:43:56 +0000429 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100430}
431
432/**************************************************************************
433 *
434 * Channel handling
435 *
436 *************************************************************************/
437
Ben Hutchings7f967c02012-02-13 23:45:02 +0000438/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000439static struct efx_channel *
440efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
441{
442 struct efx_channel *channel;
443 struct efx_rx_queue *rx_queue;
444 struct efx_tx_queue *tx_queue;
445 int j;
446
Ben Hutchings7f967c02012-02-13 23:45:02 +0000447 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
448 if (!channel)
449 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000450
Ben Hutchings7f967c02012-02-13 23:45:02 +0000451 channel->efx = efx;
452 channel->channel = i;
453 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000454
Ben Hutchings7f967c02012-02-13 23:45:02 +0000455 for (j = 0; j < EFX_TXQ_TYPES; j++) {
456 tx_queue = &channel->tx_queue[j];
457 tx_queue->efx = efx;
458 tx_queue->queue = i * EFX_TXQ_TYPES + j;
459 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000460 }
461
Ben Hutchings46426102010-09-10 06:42:33 +0000462 rx_queue = &channel->rx_queue;
463 rx_queue->efx = efx;
464 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
465 (unsigned long)rx_queue);
466
467 return channel;
468}
469
Ben Hutchings7f967c02012-02-13 23:45:02 +0000470/* Allocate and initialise a channel structure, copying parameters
471 * (but not resources) from an old channel structure.
472 */
473static struct efx_channel *
474efx_copy_channel(const struct efx_channel *old_channel)
475{
476 struct efx_channel *channel;
477 struct efx_rx_queue *rx_queue;
478 struct efx_tx_queue *tx_queue;
479 int j;
480
481 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
482 if (!channel)
483 return NULL;
484
485 *channel = *old_channel;
486
487 channel->napi_dev = NULL;
Bert Kenward46d054f2016-11-11 15:56:51 +0000488 INIT_HLIST_NODE(&channel->napi_str.napi_hash_node);
489 channel->napi_str.napi_id = 0;
490 channel->napi_str.state = 0;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000491 memset(&channel->eventq, 0, sizeof(channel->eventq));
492
493 for (j = 0; j < EFX_TXQ_TYPES; j++) {
494 tx_queue = &channel->tx_queue[j];
495 if (tx_queue->channel)
496 tx_queue->channel = channel;
497 tx_queue->buffer = NULL;
498 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
499 }
500
501 rx_queue = &channel->rx_queue;
502 rx_queue->buffer = NULL;
503 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
504 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
505 (unsigned long)rx_queue);
506
507 return channel;
508}
509
Ben Hutchings8ceee662008-04-27 12:55:59 +0100510static int efx_probe_channel(struct efx_channel *channel)
511{
512 struct efx_tx_queue *tx_queue;
513 struct efx_rx_queue *rx_queue;
514 int rc;
515
Ben Hutchings62776d02010-06-23 11:30:07 +0000516 netif_dbg(channel->efx, probe, channel->efx->net_dev,
517 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100518
Ben Hutchings7f967c02012-02-13 23:45:02 +0000519 rc = channel->type->pre_probe(channel);
520 if (rc)
521 goto fail;
522
Ben Hutchings8ceee662008-04-27 12:55:59 +0100523 rc = efx_probe_eventq(channel);
524 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000525 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100526
527 efx_for_each_channel_tx_queue(tx_queue, channel) {
528 rc = efx_probe_tx_queue(tx_queue);
529 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000530 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100531 }
532
533 efx_for_each_channel_rx_queue(rx_queue, channel) {
534 rc = efx_probe_rx_queue(rx_queue);
535 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000536 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100537 }
538
Ben Hutchings8ceee662008-04-27 12:55:59 +0100539 return 0;
540
Ben Hutchings7f967c02012-02-13 23:45:02 +0000541fail:
542 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100543 return rc;
544}
545
Ben Hutchings7f967c02012-02-13 23:45:02 +0000546static void
547efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
548{
549 struct efx_nic *efx = channel->efx;
550 const char *type;
551 int number;
552
553 number = channel->channel;
554 if (efx->tx_channel_offset == 0) {
555 type = "";
556 } else if (channel->channel < efx->tx_channel_offset) {
557 type = "-rx";
558 } else {
559 type = "-tx";
560 number -= efx->tx_channel_offset;
561 }
562 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
563}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100564
Ben Hutchings56536e92008-12-12 21:37:02 -0800565static void efx_set_channel_names(struct efx_nic *efx)
566{
567 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800568
Ben Hutchings7f967c02012-02-13 23:45:02 +0000569 efx_for_each_channel(channel, efx)
570 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100571 efx->msi_context[channel->channel].name,
572 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800573}
574
Ben Hutchings46426102010-09-10 06:42:33 +0000575static int efx_probe_channels(struct efx_nic *efx)
576{
577 struct efx_channel *channel;
578 int rc;
579
580 /* Restart special buffer allocation */
581 efx->next_buffer_table = 0;
582
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000583 /* Probe channels in reverse, so that any 'extra' channels
584 * use the start of the buffer table. This allows the traffic
585 * channels to be resized without moving them or wasting the
586 * entries before them.
587 */
588 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000589 rc = efx_probe_channel(channel);
590 if (rc) {
591 netif_err(efx, probe, efx->net_dev,
592 "failed to create channel %d\n",
593 channel->channel);
594 goto fail;
595 }
596 }
597 efx_set_channel_names(efx);
598
599 return 0;
600
601fail:
602 efx_remove_channels(efx);
603 return rc;
604}
605
Ben Hutchings8ceee662008-04-27 12:55:59 +0100606/* Channels are shutdown and reinitialised whilst the NIC is running
607 * to propagate configuration changes (mtu, checksum offload), or
608 * to clear hardware error conditions
609 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000610static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100611{
Andrew Rybchenkoebfcd0f2016-06-15 17:43:20 +0100612 netdev_features_t old_features = efx->net_dev->features;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000613 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100614 struct efx_tx_queue *tx_queue;
615 struct efx_rx_queue *rx_queue;
616 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000617 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100618
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100619 /* Calculate the rx buffer allocation parameters required to
620 * support the current MTU, including padding for header
621 * alignment and overruns.
622 */
Jon Cooper43a37392012-10-18 15:49:54 +0100623 efx->rx_dma_len = (efx->rx_prefix_size +
Ben Hutchings272baee2013-01-29 23:33:14 +0000624 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
625 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000626 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Andrew Rybchenko2ec03012013-11-16 11:02:27 +0400627 efx->rx_ip_align + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000628 if (rx_buf_len <= PAGE_SIZE) {
Jon Coopere8c68c02013-03-08 10:18:28 +0000629 efx->rx_scatter = efx->type->always_rx_scatter;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000630 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000631 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000632 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000633 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000634 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
635 EFX_RX_BUF_ALIGNMENT) >
636 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000637 efx->rx_scatter = true;
638 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
639 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000640 } else {
641 efx->rx_scatter = false;
642 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000643 }
644
Daniel Pieczko1648a232013-02-13 10:54:41 +0000645 efx_rx_config_page_split(efx);
646 if (efx->rx_buffer_order)
647 netif_dbg(efx, drv, efx->net_dev,
648 "RX buf len=%u; page order=%u batch=%u\n",
649 efx->rx_dma_len, efx->rx_buffer_order,
650 efx->rx_pages_per_batch);
651 else
652 netif_dbg(efx, drv, efx->net_dev,
653 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
654 efx->rx_dma_len, efx->rx_page_buf_step,
655 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000656
Andrew Rybchenkoebfcd0f2016-06-15 17:43:20 +0100657 /* Restore previously fixed features in hw_features and remove
658 * features which are fixed now
659 */
660 efx->net_dev->hw_features |= efx->net_dev->features;
661 efx->net_dev->hw_features &= ~efx->fixed_features;
662 efx->net_dev->features |= efx->fixed_features;
663 if (efx->net_dev->features != old_features)
664 netdev_features_change(efx->net_dev);
665
Jon Coopere8c68c02013-03-08 10:18:28 +0000666 /* RX filters may also have scatter-enabled flags */
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000667 if (efx->rx_scatter != old_rx_scatter)
Ben Hutchingsadd72472012-11-08 01:46:53 +0000668 efx->type->filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100669
Ben Hutchings14bf7182012-05-22 01:27:58 +0100670 /* We must keep at least one descriptor in a TX ring empty.
671 * We could avoid this when the queue size does not exactly
672 * match the hardware ring size, but it's not that important.
673 * Therefore we stop the queue when one more skb might fill
674 * the ring completely. We wake it when half way back to
675 * empty.
676 */
677 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
678 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
679
Ben Hutchings8ceee662008-04-27 12:55:59 +0100680 /* Initialise the channels */
681 efx_for_each_channel(channel, efx) {
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100682 efx_for_each_channel_tx_queue(tx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100683 efx_init_tx_queue(tx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100684 atomic_inc(&efx->active_queues);
685 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100686
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000687 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100688 efx_init_rx_queue(rx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100689 atomic_inc(&efx->active_queues);
Jon Coopercce28792013-10-02 11:04:14 +0100690 efx_stop_eventq(channel);
691 efx_fast_push_rx_descriptors(rx_queue, false);
692 efx_start_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000693 }
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
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000698 efx_ptp_start_datapath(efx);
699
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000700 if (netif_device_present(efx->net_dev))
701 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100702}
703
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000704static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100705{
706 struct efx_channel *channel;
707 struct efx_tx_queue *tx_queue;
708 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100709 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100710
711 EFX_ASSERT_RESET_SERIALISED(efx);
712 BUG_ON(efx->port_enabled);
713
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000714 efx_ptp_stop_datapath(efx);
715
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100716 /* Stop RX refill */
717 efx_for_each_channel(channel, efx) {
718 efx_for_each_channel_rx_queue(rx_queue, channel)
719 rx_queue->refill_enabled = false;
720 }
721
Ben Hutchings8ceee662008-04-27 12:55:59 +0100722 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000723 /* RX packet processing is pipelined, so wait for the
724 * NAPI handler to complete. At least event queue 0
725 * might be kept active by non-data events, so don't
726 * use napi_synchronize() but actually disable NAPI
727 * temporarily.
728 */
729 if (efx_channel_has_rx_queue(channel)) {
730 efx_stop_eventq(channel);
731 efx_start_eventq(channel);
732 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100733 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100734
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100735 rc = efx->type->fini_dmaq(efx);
Edward Cree5a6681e2016-11-28 18:55:34 +0000736 if (rc) {
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100737 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
738 } else {
739 netif_dbg(efx, drv, efx->net_dev,
740 "successfully flushed all queues\n");
741 }
742
743 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100744 efx_for_each_channel_rx_queue(rx_queue, channel)
745 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000746 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100747 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100748 }
749}
750
751static void efx_remove_channel(struct efx_channel *channel)
752{
753 struct efx_tx_queue *tx_queue;
754 struct efx_rx_queue *rx_queue;
755
Ben Hutchings62776d02010-06-23 11:30:07 +0000756 netif_dbg(channel->efx, drv, channel->efx->net_dev,
757 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100758
759 efx_for_each_channel_rx_queue(rx_queue, channel)
760 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000761 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100762 efx_remove_tx_queue(tx_queue);
763 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100764 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100765}
766
Ben Hutchings46426102010-09-10 06:42:33 +0000767static void efx_remove_channels(struct efx_nic *efx)
768{
769 struct efx_channel *channel;
770
771 efx_for_each_channel(channel, efx)
772 efx_remove_channel(channel);
773}
774
775int
776efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
777{
778 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
779 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000780 unsigned i, next_buffer_table = 0;
Jon Cooper261e4d92013-04-15 18:51:54 +0100781 int rc, rc2;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100782
783 rc = efx_check_disabled(efx);
784 if (rc)
785 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000786
787 /* Not all channels should be reallocated. We must avoid
788 * reallocating their buffer table entries.
789 */
790 efx_for_each_channel(channel, efx) {
791 struct efx_rx_queue *rx_queue;
792 struct efx_tx_queue *tx_queue;
793
794 if (channel->type->copy)
795 continue;
796 next_buffer_table = max(next_buffer_table,
797 channel->eventq.index +
798 channel->eventq.entries);
799 efx_for_each_channel_rx_queue(rx_queue, channel)
800 next_buffer_table = max(next_buffer_table,
801 rx_queue->rxd.index +
802 rx_queue->rxd.entries);
803 efx_for_each_channel_tx_queue(tx_queue, channel)
804 next_buffer_table = max(next_buffer_table,
805 tx_queue->txd.index +
806 tx_queue->txd.entries);
807 }
Ben Hutchings46426102010-09-10 06:42:33 +0000808
Ben Hutchings29c69a42013-01-28 19:01:06 +0000809 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000810 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100811 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000812
Ben Hutchings7f967c02012-02-13 23:45:02 +0000813 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000814 memset(other_channel, 0, sizeof(other_channel));
815 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000816 channel = efx->channel[i];
817 if (channel->type->copy)
818 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000819 if (!channel) {
820 rc = -ENOMEM;
821 goto out;
822 }
823 other_channel[i] = channel;
824 }
825
826 /* Swap entry counts and channel pointers */
827 old_rxq_entries = efx->rxq_entries;
828 old_txq_entries = efx->txq_entries;
829 efx->rxq_entries = rxq_entries;
830 efx->txq_entries = txq_entries;
831 for (i = 0; i < efx->n_channels; i++) {
832 channel = efx->channel[i];
833 efx->channel[i] = other_channel[i];
834 other_channel[i] = channel;
835 }
836
Ben Hutchings7f967c02012-02-13 23:45:02 +0000837 /* Restart buffer table allocation */
838 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000839
Ben Hutchingse8f14992010-12-07 19:47:34 +0000840 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000841 channel = efx->channel[i];
842 if (!channel->type->copy)
843 continue;
844 rc = efx_probe_channel(channel);
845 if (rc)
846 goto rollback;
847 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000848 }
Ben Hutchings46426102010-09-10 06:42:33 +0000849
Ben Hutchings7f967c02012-02-13 23:45:02 +0000850out:
851 /* Destroy unused channel structures */
852 for (i = 0; i < efx->n_channels; i++) {
853 channel = other_channel[i];
854 if (channel && channel->type->copy) {
855 efx_fini_napi_channel(channel);
856 efx_remove_channel(channel);
857 kfree(channel);
858 }
859 }
860
Jon Cooper261e4d92013-04-15 18:51:54 +0100861 rc2 = efx_soft_enable_interrupts(efx);
862 if (rc2) {
863 rc = rc ? rc : rc2;
864 netif_err(efx, drv, efx->net_dev,
865 "unable to restart interrupts on channel reallocation\n");
866 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
867 } else {
868 efx_start_all(efx);
869 netif_device_attach(efx->net_dev);
870 }
Ben Hutchings46426102010-09-10 06:42:33 +0000871 return rc;
872
873rollback:
874 /* Swap back */
875 efx->rxq_entries = old_rxq_entries;
876 efx->txq_entries = old_txq_entries;
877 for (i = 0; i < efx->n_channels; i++) {
878 channel = efx->channel[i];
879 efx->channel[i] = other_channel[i];
880 other_channel[i] = channel;
881 }
882 goto out;
883}
884
Steve Hodgson90d683a2010-06-01 11:19:39 +0000885void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100886{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000887 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100888}
889
Ben Hutchings7f967c02012-02-13 23:45:02 +0000890static const struct efx_channel_type efx_default_channel_type = {
891 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100892 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000893 .get_name = efx_get_channel_name,
894 .copy = efx_copy_channel,
895 .keep_eventq = false,
896};
897
898int efx_channel_dummy_op_int(struct efx_channel *channel)
899{
900 return 0;
901}
902
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100903void efx_channel_dummy_op_void(struct efx_channel *channel)
904{
905}
906
Ben Hutchings8ceee662008-04-27 12:55:59 +0100907/**************************************************************************
908 *
909 * Port handling
910 *
911 **************************************************************************/
912
913/* This ensures that the kernel is kept informed (via
914 * netif_carrier_on/off) of the link status, and also maintains the
915 * link status's stop on the port's TX queue.
916 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000917void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100918{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000919 struct efx_link_state *link_state = &efx->link_state;
920
Ben Hutchings8ceee662008-04-27 12:55:59 +0100921 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
922 * that no events are triggered between unregister_netdev() and the
923 * driver unloading. A more general condition is that NETDEV_CHANGE
924 * can only be generated between NETDEV_UP and NETDEV_DOWN */
925 if (!netif_running(efx->net_dev))
926 return;
927
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000928 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100929 efx->n_link_state_changes++;
930
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000931 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100932 netif_carrier_on(efx->net_dev);
933 else
934 netif_carrier_off(efx->net_dev);
935 }
936
937 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000938 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000939 netif_info(efx, link, efx->net_dev,
Ben Hutchings964e6132012-11-19 23:08:22 +0000940 "link up at %uMbps %s-duplex (MTU %d)\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000941 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings964e6132012-11-19 23:08:22 +0000942 efx->net_dev->mtu);
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000943 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000944 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100945}
946
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000947void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
948{
949 efx->link_advertising = advertising;
950 if (advertising) {
951 if (advertising & ADVERTISED_Pause)
952 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
953 else
954 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
955 if (advertising & ADVERTISED_Asym_Pause)
956 efx->wanted_fc ^= EFX_FC_TX;
957 }
958}
959
David S. Millerb56269462011-05-17 17:53:22 -0400960void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000961{
962 efx->wanted_fc = wanted_fc;
963 if (efx->link_advertising) {
964 if (wanted_fc & EFX_FC_RX)
965 efx->link_advertising |= (ADVERTISED_Pause |
966 ADVERTISED_Asym_Pause);
967 else
968 efx->link_advertising &= ~(ADVERTISED_Pause |
969 ADVERTISED_Asym_Pause);
970 if (wanted_fc & EFX_FC_TX)
971 efx->link_advertising ^= ADVERTISED_Asym_Pause;
972 }
973}
974
Ben Hutchings115122a2009-03-04 09:52:52 +0000975static void efx_fini_port(struct efx_nic *efx);
976
Edward Cree0d322412015-05-20 11:10:03 +0100977/* We assume that efx->type->reconfigure_mac will always try to sync RX
978 * filters and therefore needs to read-lock the filter table against freeing
979 */
980void efx_mac_reconfigure(struct efx_nic *efx)
981{
982 down_read(&efx->filter_sem);
983 efx->type->reconfigure_mac(efx);
984 up_read(&efx->filter_sem);
985}
986
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000987/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
988 * the MAC appropriately. All other PHY configuration changes are pushed
989 * through phy_op->set_settings(), and pushed asynchronously to the MAC
990 * through efx_monitor().
991 *
992 * Callers must hold the mac_lock
993 */
994int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100995{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000996 enum efx_phy_mode phy_mode;
997 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100998
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000999 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001000
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001001 /* Disable PHY transmit in mac level loopbacks */
1002 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001003 if (LOOPBACK_INTERNAL(efx))
1004 efx->phy_mode |= PHY_MODE_TX_DISABLED;
1005 else
1006 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001007
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001008 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001009
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001010 if (rc)
1011 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001012
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001013 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001014}
1015
1016/* Reinitialise the MAC to pick up new PHY settings, even if the port is
1017 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001018int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001019{
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001020 int rc;
1021
Ben Hutchings8ceee662008-04-27 12:55:59 +01001022 EFX_ASSERT_RESET_SERIALISED(efx);
1023
1024 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001025 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001026 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001027
1028 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001029}
1030
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001031/* Asynchronous work item for changing MAC promiscuity and multicast
1032 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
1033 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001034static void efx_mac_work(struct work_struct *data)
1035{
1036 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
1037
1038 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +01001039 if (efx->port_enabled)
Edward Cree0d322412015-05-20 11:10:03 +01001040 efx_mac_reconfigure(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001041 mutex_unlock(&efx->mac_lock);
1042}
1043
Ben Hutchings8ceee662008-04-27 12:55:59 +01001044static int efx_probe_port(struct efx_nic *efx)
1045{
1046 int rc;
1047
Ben Hutchings62776d02010-06-23 11:30:07 +00001048 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001049
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001050 if (phy_flash_cfg)
1051 efx->phy_mode = PHY_MODE_SPECIAL;
1052
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001053 /* Connect up MAC/PHY operations table */
1054 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001055 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001056 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001057
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001058 /* Initialise MAC address to permanent address */
Edward Creecd84ff42014-03-07 18:27:41 +00001059 ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001060
1061 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001062}
1063
1064static int efx_init_port(struct efx_nic *efx)
1065{
1066 int rc;
1067
Ben Hutchings62776d02010-06-23 11:30:07 +00001068 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001069
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001070 mutex_lock(&efx->mac_lock);
1071
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001072 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001073 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001074 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001075
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001076 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001077
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001078 /* Reconfigure the MAC before creating dma queues (required for
1079 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Edward Cree0d322412015-05-20 11:10:03 +01001080 efx_mac_reconfigure(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001081
1082 /* Ensure the PHY advertises the correct flow control settings */
1083 rc = efx->phy_op->reconfigure(efx);
Edward Cree267d9d72015-05-06 00:59:18 +01001084 if (rc && rc != -EPERM)
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001085 goto fail2;
1086
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001087 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001088 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001089
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001090fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001091 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001092fail1:
1093 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001094 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001095}
1096
Ben Hutchings8ceee662008-04-27 12:55:59 +01001097static void efx_start_port(struct efx_nic *efx)
1098{
Ben Hutchings62776d02010-06-23 11:30:07 +00001099 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001100 BUG_ON(efx->port_enabled);
1101
1102 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001103 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001104
Ben Hutchingsd615c032013-10-08 17:33:20 +01001105 /* Ensure MAC ingress/egress is enabled */
Edward Cree0d322412015-05-20 11:10:03 +01001106 efx_mac_reconfigure(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001107
Ben Hutchings8ceee662008-04-27 12:55:59 +01001108 mutex_unlock(&efx->mac_lock);
1109}
1110
Ben Hutchingsd615c032013-10-08 17:33:20 +01001111/* Cancel work for MAC reconfiguration, periodic hardware monitoring
1112 * and the async self-test, wait for them to finish and prevent them
1113 * being scheduled again. This doesn't cover online resets, which
1114 * should only be cancelled when removing the device.
1115 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001116static void efx_stop_port(struct efx_nic *efx)
1117{
Ben Hutchings62776d02010-06-23 11:30:07 +00001118 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001119
Ben Hutchingsd615c032013-10-08 17:33:20 +01001120 EFX_ASSERT_RESET_SERIALISED(efx);
1121
Ben Hutchings8ceee662008-04-27 12:55:59 +01001122 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001123 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001124 mutex_unlock(&efx->mac_lock);
1125
1126 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001127 netif_addr_lock_bh(efx->net_dev);
1128 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsd615c032013-10-08 17:33:20 +01001129
1130 cancel_delayed_work_sync(&efx->monitor_work);
1131 efx_selftest_async_cancel(efx);
1132 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001133}
1134
1135static void efx_fini_port(struct efx_nic *efx)
1136{
Ben Hutchings62776d02010-06-23 11:30:07 +00001137 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001138
1139 if (!efx->port_initialized)
1140 return;
1141
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001142 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001143 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001144
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001145 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001146 efx_link_status_changed(efx);
1147}
1148
1149static void efx_remove_port(struct efx_nic *efx)
1150{
Ben Hutchings62776d02010-06-23 11:30:07 +00001151 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001152
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001153 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001154}
1155
1156/**************************************************************************
1157 *
1158 * NIC handling
1159 *
1160 **************************************************************************/
1161
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01001162static LIST_HEAD(efx_primary_list);
1163static LIST_HEAD(efx_unassociated_list);
1164
1165static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
1166{
1167 return left->type == right->type &&
1168 left->vpd_sn && right->vpd_sn &&
1169 !strcmp(left->vpd_sn, right->vpd_sn);
1170}
1171
1172static void efx_associate(struct efx_nic *efx)
1173{
1174 struct efx_nic *other, *next;
1175
1176 if (efx->primary == efx) {
1177 /* Adding primary function; look for secondaries */
1178
1179 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
1180 list_add_tail(&efx->node, &efx_primary_list);
1181
1182 list_for_each_entry_safe(other, next, &efx_unassociated_list,
1183 node) {
1184 if (efx_same_controller(efx, other)) {
1185 list_del(&other->node);
1186 netif_dbg(other, probe, other->net_dev,
1187 "moving to secondary list of %s %s\n",
1188 pci_name(efx->pci_dev),
1189 efx->net_dev->name);
1190 list_add_tail(&other->node,
1191 &efx->secondary_list);
1192 other->primary = efx;
1193 }
1194 }
1195 } else {
1196 /* Adding secondary function; look for primary */
1197
1198 list_for_each_entry(other, &efx_primary_list, node) {
1199 if (efx_same_controller(efx, other)) {
1200 netif_dbg(efx, probe, efx->net_dev,
1201 "adding to secondary list of %s %s\n",
1202 pci_name(other->pci_dev),
1203 other->net_dev->name);
1204 list_add_tail(&efx->node,
1205 &other->secondary_list);
1206 efx->primary = other;
1207 return;
1208 }
1209 }
1210
1211 netif_dbg(efx, probe, efx->net_dev,
1212 "adding to unassociated list\n");
1213 list_add_tail(&efx->node, &efx_unassociated_list);
1214 }
1215}
1216
1217static void efx_dissociate(struct efx_nic *efx)
1218{
1219 struct efx_nic *other, *next;
1220
1221 list_del(&efx->node);
1222 efx->primary = NULL;
1223
1224 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
1225 list_del(&other->node);
1226 netif_dbg(other, probe, other->net_dev,
1227 "moving to unassociated list\n");
1228 list_add_tail(&other->node, &efx_unassociated_list);
1229 other->primary = NULL;
1230 }
1231}
1232
Ben Hutchings8ceee662008-04-27 12:55:59 +01001233/* This configures the PCI device to enable I/O and DMA. */
1234static int efx_init_io(struct efx_nic *efx)
1235{
1236 struct pci_dev *pci_dev = efx->pci_dev;
1237 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001238 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Shradha Shah02246a72015-05-06 00:58:14 +01001239 int rc, bar;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001240
Ben Hutchings62776d02010-06-23 11:30:07 +00001241 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001242
Shradha Shah02246a72015-05-06 00:58:14 +01001243 bar = efx->type->mem_bar;
1244
Ben Hutchings8ceee662008-04-27 12:55:59 +01001245 rc = pci_enable_device(pci_dev);
1246 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001247 netif_err(efx, probe, efx->net_dev,
1248 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001249 goto fail1;
1250 }
1251
1252 pci_set_master(pci_dev);
1253
1254 /* Set the PCI DMA mask. Try all possibilities from our
1255 * genuine mask down to 32 bits, because some architectures
1256 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1257 * masks event though they reject 46 bit masks.
1258 */
1259 while (dma_mask > 0x7fffffffUL) {
Christoph Hellwig8722b8f2015-11-10 14:45:42 -08001260 rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
1261 if (rc == 0)
1262 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001263 dma_mask >>= 1;
1264 }
1265 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001266 netif_err(efx, probe, efx->net_dev,
1267 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001268 goto fail2;
1269 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001270 netif_dbg(efx, probe, efx->net_dev,
1271 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001272
Shradha Shah02246a72015-05-06 00:58:14 +01001273 efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
1274 rc = pci_request_region(pci_dev, bar, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001275 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001276 netif_err(efx, probe, efx->net_dev,
1277 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001278 rc = -EIO;
1279 goto fail3;
1280 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001281 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001282 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001283 netif_err(efx, probe, efx->net_dev,
1284 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001285 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001286 rc = -ENOMEM;
1287 goto fail4;
1288 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001289 netif_dbg(efx, probe, efx->net_dev,
1290 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001291 (unsigned long long)efx->membase_phys, mem_map_size,
1292 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001293
1294 return 0;
1295
1296 fail4:
Shradha Shah02246a72015-05-06 00:58:14 +01001297 pci_release_region(efx->pci_dev, bar);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001298 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001299 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001300 fail2:
1301 pci_disable_device(efx->pci_dev);
1302 fail1:
1303 return rc;
1304}
1305
1306static void efx_fini_io(struct efx_nic *efx)
1307{
Shradha Shah02246a72015-05-06 00:58:14 +01001308 int bar;
1309
Ben Hutchings62776d02010-06-23 11:30:07 +00001310 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001311
1312 if (efx->membase) {
1313 iounmap(efx->membase);
1314 efx->membase = NULL;
1315 }
1316
1317 if (efx->membase_phys) {
Shradha Shah02246a72015-05-06 00:58:14 +01001318 bar = efx->type->mem_bar;
1319 pci_release_region(efx->pci_dev, bar);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001320 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001321 }
1322
Daniel Pieczko6598dad2015-06-02 11:41:00 +01001323 /* Don't disable bus-mastering if VFs are assigned */
1324 if (!pci_vfs_assigned(efx->pci_dev))
1325 pci_disable_device(efx->pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001326}
1327
Jon Cooper267c0152015-05-06 00:59:38 +01001328void efx_set_default_rx_indir_table(struct efx_nic *efx)
1329{
1330 size_t i;
1331
1332 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
1333 efx->rx_indir_table[i] =
1334 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001335}
1336
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001337static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001338{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001339 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001340 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001341 int cpu;
1342
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001343 if (rss_cpus) {
1344 count = rss_cpus;
1345 } else {
1346 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1347 netif_warn(efx, probe, efx->net_dev,
1348 "RSS disabled due to allocation failure\n");
1349 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001350 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001351
1352 count = 0;
1353 for_each_online_cpu(cpu) {
1354 if (!cpumask_test_cpu(cpu, thread_mask)) {
1355 ++count;
1356 cpumask_or(thread_mask, thread_mask,
Bartosz Golaszewski06931e62015-05-26 15:11:28 +02001357 topology_sibling_cpumask(cpu));
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001358 }
1359 }
1360
1361 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001362 }
1363
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001364 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1365 * table entries that are inaccessible to VFs
1366 */
Shradha Shah7fa8d542015-05-06 00:55:13 +01001367#ifdef CONFIG_SFC_SRIOV
1368 if (efx->type->sriov_wanted) {
1369 if (efx->type->sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1370 count > efx_vf_size(efx)) {
1371 netif_warn(efx, probe, efx->net_dev,
1372 "Reducing number of RSS channels from %u to %u for "
1373 "VF support. Increase vf-msix-limit to use more "
1374 "channels on the PF.\n",
1375 count, efx_vf_size(efx));
1376 count = efx_vf_size(efx);
1377 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001378 }
Shradha Shah7fa8d542015-05-06 00:55:13 +01001379#endif
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001380
Ben Hutchings46123d02008-09-01 12:47:33 +01001381 return count;
1382}
1383
1384/* Probe the number and type of interrupts we are able to obtain, and
1385 * the resulting numbers of channels and RX queues.
1386 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001387static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001388{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001389 unsigned int extra_channels = 0;
1390 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001391 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001392
Ben Hutchings7f967c02012-02-13 23:45:02 +00001393 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1394 if (efx->extra_channel_type[i])
1395 ++extra_channels;
1396
Ben Hutchings8ceee662008-04-27 12:55:59 +01001397 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001398 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001399 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001400
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001401 n_channels = efx_wanted_parallelism(efx);
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001402 if (efx_separate_tx_channels)
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001403 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001404 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001405 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001406
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001407 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001408 xentries[i].entry = i;
Alexander Gordeev184603d2014-02-18 11:12:00 +01001409 rc = pci_enable_msix_range(efx->pci_dev,
1410 xentries, 1, n_channels);
1411 if (rc < 0) {
1412 /* Fall back to single channel MSI */
1413 efx->interrupt_mode = EFX_INT_MODE_MSI;
1414 netif_err(efx, drv, efx->net_dev,
1415 "could not enable MSI-X\n");
1416 } else if (rc < n_channels) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001417 netif_err(efx, drv, efx->net_dev,
1418 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001419 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001420 netif_err(efx, drv, efx->net_dev,
1421 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001422 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001423 }
1424
Alexander Gordeev184603d2014-02-18 11:12:00 +01001425 if (rc > 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001426 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001427 if (n_channels > extra_channels)
1428 n_channels -= extra_channels;
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001429 if (efx_separate_tx_channels) {
1430 efx->n_tx_channels = min(max(n_channels / 2,
1431 1U),
1432 efx->max_tx_channels);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001433 efx->n_rx_channels = max(n_channels -
1434 efx->n_tx_channels,
1435 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001436 } else {
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001437 efx->n_tx_channels = min(n_channels,
1438 efx->max_tx_channels);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001439 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001440 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001441 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001442 efx_get_channel(efx, i)->irq =
1443 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001444 }
1445 }
1446
1447 /* Try single interrupt MSI */
1448 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001449 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001450 efx->n_rx_channels = 1;
1451 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001452 rc = pci_enable_msi(efx->pci_dev);
1453 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001454 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001455 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001456 netif_err(efx, drv, efx->net_dev,
1457 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001458 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1459 }
1460 }
1461
1462 /* Assume legacy interrupts */
1463 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001464 efx->n_channels = 1 + (efx_separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001465 efx->n_rx_channels = 1;
1466 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001467 efx->legacy_irq = efx->pci_dev->irq;
1468 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001469
Ben Hutchings7f967c02012-02-13 23:45:02 +00001470 /* Assign extra channels if possible */
1471 j = efx->n_channels;
1472 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1473 if (!efx->extra_channel_type[i])
1474 continue;
1475 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1476 efx->n_channels <= extra_channels) {
1477 efx->extra_channel_type[i]->handle_no_channel(efx);
1478 } else {
1479 --j;
1480 efx_get_channel(efx, j)->type =
1481 efx->extra_channel_type[i];
1482 }
1483 }
1484
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001485 /* RSS might be usable on VFs even if it is disabled on the PF */
Shradha Shah7fa8d542015-05-06 00:55:13 +01001486#ifdef CONFIG_SFC_SRIOV
1487 if (efx->type->sriov_wanted) {
1488 efx->rss_spread = ((efx->n_rx_channels > 1 ||
1489 !efx->type->sriov_wanted(efx)) ?
1490 efx->n_rx_channels : efx_vf_size(efx));
1491 return 0;
1492 }
1493#endif
1494 efx->rss_spread = efx->n_rx_channels;
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001495
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001496 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001497}
1498
Jon Cooper261e4d92013-04-15 18:51:54 +01001499static int efx_soft_enable_interrupts(struct efx_nic *efx)
Ben Hutchingsd8291182012-10-05 23:35:41 +01001500{
Jon Cooper261e4d92013-04-15 18:51:54 +01001501 struct efx_channel *channel, *end_channel;
1502 int rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001503
1504 BUG_ON(efx->state == STATE_DISABLED);
1505
1506 efx->irq_soft_enabled = true;
1507 smp_wmb();
1508
1509 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001510 if (!channel->type->keep_eventq) {
1511 rc = efx_init_eventq(channel);
1512 if (rc)
1513 goto fail;
1514 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001515 efx_start_eventq(channel);
1516 }
1517
1518 efx_mcdi_mode_event(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +01001519
1520 return 0;
1521fail:
1522 end_channel = channel;
1523 efx_for_each_channel(channel, efx) {
1524 if (channel == end_channel)
1525 break;
1526 efx_stop_eventq(channel);
1527 if (!channel->type->keep_eventq)
1528 efx_fini_eventq(channel);
1529 }
1530
1531 return rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001532}
1533
1534static void efx_soft_disable_interrupts(struct efx_nic *efx)
1535{
1536 struct efx_channel *channel;
1537
1538 if (efx->state == STATE_DISABLED)
1539 return;
1540
1541 efx_mcdi_mode_poll(efx);
1542
1543 efx->irq_soft_enabled = false;
1544 smp_wmb();
1545
1546 if (efx->legacy_irq)
1547 synchronize_irq(efx->legacy_irq);
1548
1549 efx_for_each_channel(channel, efx) {
1550 if (channel->irq)
1551 synchronize_irq(channel->irq);
1552
1553 efx_stop_eventq(channel);
1554 if (!channel->type->keep_eventq)
1555 efx_fini_eventq(channel);
1556 }
Ben Hutchingscade7152013-08-27 23:12:31 +01001557
1558 /* Flush the asynchronous MCDI request queue */
1559 efx_mcdi_flush_async(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01001560}
1561
Jon Cooper261e4d92013-04-15 18:51:54 +01001562static int efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001563{
Jon Cooper261e4d92013-04-15 18:51:54 +01001564 struct efx_channel *channel, *end_channel;
1565 int rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001566
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001567 BUG_ON(efx->state == STATE_DISABLED);
1568
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001569 if (efx->eeh_disabled_legacy_irq) {
1570 enable_irq(efx->legacy_irq);
1571 efx->eeh_disabled_legacy_irq = false;
1572 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001573
Ben Hutchings86094f72013-08-21 19:51:04 +01001574 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001575
1576 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001577 if (channel->type->keep_eventq) {
1578 rc = efx_init_eventq(channel);
1579 if (rc)
1580 goto fail;
1581 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001582 }
1583
Jon Cooper261e4d92013-04-15 18:51:54 +01001584 rc = efx_soft_enable_interrupts(efx);
1585 if (rc)
1586 goto fail;
1587
1588 return 0;
1589
1590fail:
1591 end_channel = channel;
1592 efx_for_each_channel(channel, efx) {
1593 if (channel == end_channel)
1594 break;
1595 if (channel->type->keep_eventq)
1596 efx_fini_eventq(channel);
1597 }
1598
1599 efx->type->irq_disable_non_ev(efx);
1600
1601 return rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001602}
1603
Ben Hutchingsd8291182012-10-05 23:35:41 +01001604static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001605{
1606 struct efx_channel *channel;
1607
Ben Hutchingsd8291182012-10-05 23:35:41 +01001608 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001609
1610 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001611 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001612 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001613 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001614
Ben Hutchings86094f72013-08-21 19:51:04 +01001615 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001616}
1617
Ben Hutchings8ceee662008-04-27 12:55:59 +01001618static void efx_remove_interrupts(struct efx_nic *efx)
1619{
1620 struct efx_channel *channel;
1621
1622 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001623 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001624 channel->irq = 0;
1625 pci_disable_msi(efx->pci_dev);
1626 pci_disable_msix(efx->pci_dev);
1627
1628 /* Remove legacy interrupt */
1629 efx->legacy_irq = 0;
1630}
1631
Ben Hutchings8831da72008-09-01 12:47:48 +01001632static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001633{
Ben Hutchings602a5322011-05-16 17:32:39 +01001634 struct efx_channel *channel;
1635 struct efx_tx_queue *tx_queue;
1636
Ben Hutchings97653432011-01-12 18:26:56 +00001637 efx->tx_channel_offset =
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001638 efx_separate_tx_channels ?
1639 efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001640
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001641 /* We need to mark which channels really have RX and TX
1642 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001643 * RX-only and TX-only channels.
1644 */
1645 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001646 if (channel->channel < efx->n_rx_channels)
1647 channel->rx_queue.core_index = channel->channel;
1648 else
1649 channel->rx_queue.core_index = -1;
1650
Ben Hutchings602a5322011-05-16 17:32:39 +01001651 efx_for_each_channel_tx_queue(tx_queue, channel)
1652 tx_queue->queue -= (efx->tx_channel_offset *
1653 EFX_TXQ_TYPES);
1654 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001655}
1656
1657static int efx_probe_nic(struct efx_nic *efx)
1658{
1659 int rc;
1660
Ben Hutchings62776d02010-06-23 11:30:07 +00001661 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001662
1663 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001664 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001665 if (rc)
1666 return rc;
1667
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001668 do {
1669 if (!efx->max_channels || !efx->max_tx_channels) {
1670 netif_err(efx, drv, efx->net_dev,
1671 "Insufficient resources to allocate"
1672 " any channels\n");
1673 rc = -ENOSPC;
1674 goto fail1;
1675 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001676
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001677 /* Determine the number of channels and queues by trying
1678 * to hook in MSI-X interrupts.
1679 */
1680 rc = efx_probe_interrupts(efx);
1681 if (rc)
1682 goto fail1;
Daniel Pieczko52ad7622014-04-01 13:10:34 +01001683
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001684 efx_set_channels(efx);
1685
1686 /* dimension_resources can fail with EAGAIN */
1687 rc = efx->type->dimension_resources(efx);
1688 if (rc != 0 && rc != -EAGAIN)
1689 goto fail2;
1690
1691 if (rc == -EAGAIN)
1692 /* try again with new max_channels */
1693 efx_remove_interrupts(efx);
1694
1695 } while (rc == -EAGAIN);
Ben Hutchings28e47c42012-02-15 01:58:49 +00001696
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001697 if (efx->n_channels > 1)
Jon Cooper267c0152015-05-06 00:59:38 +01001698 netdev_rss_key_fill(&efx->rx_hash_key,
1699 sizeof(efx->rx_hash_key));
1700 efx_set_default_rx_indir_table(efx);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001701
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001702 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1703 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001704
1705 /* Initialise the interrupt moderation settings */
Bert Kenward539de7c2016-08-11 13:02:09 +01001706 efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
Ben Hutchings9e393b32011-09-05 07:43:04 +00001707 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1708 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001709
1710 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001711
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001712fail2:
1713 efx_remove_interrupts(efx);
1714fail1:
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001715 efx->type->remove(efx);
1716 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001717}
1718
1719static void efx_remove_nic(struct efx_nic *efx)
1720{
Ben Hutchings62776d02010-06-23 11:30:07 +00001721 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001722
1723 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001724 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001725}
1726
Ben Hutchingsadd72472012-11-08 01:46:53 +00001727static int efx_probe_filters(struct efx_nic *efx)
1728{
1729 int rc;
1730
1731 spin_lock_init(&efx->filter_lock);
Edward Cree0d322412015-05-20 11:10:03 +01001732 init_rwsem(&efx->filter_sem);
Martin Habetsd2489532016-06-15 17:48:49 +01001733 mutex_lock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01001734 down_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001735 rc = efx->type->filter_table_probe(efx);
1736 if (rc)
Edward Cree0d322412015-05-20 11:10:03 +01001737 goto out_unlock;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001738
1739#ifdef CONFIG_RFS_ACCEL
1740 if (efx->type->offload_features & NETIF_F_NTUPLE) {
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001741 struct efx_channel *channel;
1742 int i, success = 1;
1743
1744 efx_for_each_channel(channel, efx) {
1745 channel->rps_flow_id =
1746 kcalloc(efx->type->max_rx_ip_filters,
1747 sizeof(*channel->rps_flow_id),
1748 GFP_KERNEL);
1749 if (!channel->rps_flow_id)
1750 success = 0;
1751 else
1752 for (i = 0;
1753 i < efx->type->max_rx_ip_filters;
1754 ++i)
1755 channel->rps_flow_id[i] =
1756 RPS_FLOW_ID_INVALID;
1757 }
1758
1759 if (!success) {
1760 efx_for_each_channel(channel, efx)
1761 kfree(channel->rps_flow_id);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001762 efx->type->filter_table_remove(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001763 rc = -ENOMEM;
1764 goto out_unlock;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001765 }
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001766
1767 efx->rps_expire_index = efx->rps_expire_channel = 0;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001768 }
1769#endif
Edward Cree0d322412015-05-20 11:10:03 +01001770out_unlock:
1771 up_write(&efx->filter_sem);
Martin Habetsd2489532016-06-15 17:48:49 +01001772 mutex_unlock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01001773 return rc;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001774}
1775
1776static void efx_remove_filters(struct efx_nic *efx)
1777{
1778#ifdef CONFIG_RFS_ACCEL
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001779 struct efx_channel *channel;
1780
1781 efx_for_each_channel(channel, efx)
1782 kfree(channel->rps_flow_id);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001783#endif
Edward Cree0d322412015-05-20 11:10:03 +01001784 down_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001785 efx->type->filter_table_remove(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001786 up_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001787}
1788
1789static void efx_restore_filters(struct efx_nic *efx)
1790{
Edward Cree0d322412015-05-20 11:10:03 +01001791 down_read(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001792 efx->type->filter_table_restore(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001793 up_read(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001794}
1795
Ben Hutchings8ceee662008-04-27 12:55:59 +01001796/**************************************************************************
1797 *
1798 * NIC startup/shutdown
1799 *
1800 *************************************************************************/
1801
1802static int efx_probe_all(struct efx_nic *efx)
1803{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001804 int rc;
1805
Ben Hutchings8ceee662008-04-27 12:55:59 +01001806 rc = efx_probe_nic(efx);
1807 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001808 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001809 goto fail1;
1810 }
1811
Ben Hutchings8ceee662008-04-27 12:55:59 +01001812 rc = efx_probe_port(efx);
1813 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001814 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001815 goto fail2;
1816 }
1817
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001818 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1819 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1820 rc = -EINVAL;
1821 goto fail3;
1822 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001823 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001824
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001825#ifdef CONFIG_SFC_SRIOV
1826 rc = efx->type->vswitching_probe(efx);
1827 if (rc) /* not fatal; the PF will still work fine */
1828 netif_warn(efx, probe, efx->net_dev,
1829 "failed to setup vswitching rc=%d;"
1830 " VFs may not function\n", rc);
1831#endif
1832
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001833 rc = efx_probe_filters(efx);
1834 if (rc) {
1835 netif_err(efx, probe, efx->net_dev,
1836 "failed to create filter tables\n");
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001837 goto fail4;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001838 }
1839
Ben Hutchings7f967c02012-02-13 23:45:02 +00001840 rc = efx_probe_channels(efx);
1841 if (rc)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001842 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001843
Ben Hutchings8ceee662008-04-27 12:55:59 +01001844 return 0;
1845
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001846 fail5:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001847 efx_remove_filters(efx);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001848 fail4:
1849#ifdef CONFIG_SFC_SRIOV
1850 efx->type->vswitching_remove(efx);
1851#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01001852 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001853 efx_remove_port(efx);
1854 fail2:
1855 efx_remove_nic(efx);
1856 fail1:
1857 return rc;
1858}
1859
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001860/* If the interface is supposed to be running but is not, start
1861 * the hardware and software data path, regular activity for the port
1862 * (MAC statistics, link polling, etc.) and schedule the port to be
1863 * reconfigured. Interrupts must already be enabled. This function
1864 * is safe to call multiple times, so long as the NIC is not disabled.
1865 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001866 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001867static void efx_start_all(struct efx_nic *efx)
1868{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001869 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001870 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001871
1872 /* Check that it is appropriate to restart the interface. All
1873 * of these flags are safe to read under just the rtnl lock */
Edward Creee2835462014-04-16 19:27:48 +01001874 if (efx->port_enabled || !netif_running(efx->net_dev) ||
1875 efx->reset_pending)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001876 return;
1877
Ben Hutchings8ceee662008-04-27 12:55:59 +01001878 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001879 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001880
Alexandre Rames626950d2013-01-14 17:20:22 +00001881 /* Start the hardware monitor if there is one */
1882 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001883 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1884 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001885
Edward Cree5a6681e2016-11-28 18:55:34 +00001886 /* Link state detection is normally event-driven; we have
Alexandre Rames626950d2013-01-14 17:20:22 +00001887 * to poll now because we could have missed a change
1888 */
Edward Cree5a6681e2016-11-28 18:55:34 +00001889 mutex_lock(&efx->mac_lock);
1890 if (efx->phy_op->poll(efx))
1891 efx_link_status_changed(efx);
1892 mutex_unlock(&efx->mac_lock);
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001893
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001894 efx->type->start_stats(efx);
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001895 efx->type->pull_stats(efx);
1896 spin_lock_bh(&efx->stats_lock);
1897 efx->type->update_stats(efx, NULL, NULL);
1898 spin_unlock_bh(&efx->stats_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001899}
1900
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001901/* Quiesce the hardware and software data path, and regular activity
1902 * for the port without bringing the link down. Safe to call multiple
1903 * times with the NIC in almost any state, but interrupts should be
1904 * enabled. Requires the RTNL lock.
1905 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001906static void efx_stop_all(struct efx_nic *efx)
1907{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001908 EFX_ASSERT_RESET_SERIALISED(efx);
1909
1910 /* port_enabled can be read safely under the rtnl lock */
1911 if (!efx->port_enabled)
1912 return;
1913
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001914 /* update stats before we go down so we can accurately count
1915 * rx_nodesc_drops
1916 */
1917 efx->type->pull_stats(efx);
1918 spin_lock_bh(&efx->stats_lock);
1919 efx->type->update_stats(efx, NULL, NULL);
1920 spin_unlock_bh(&efx->stats_lock);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001921 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001922 efx_stop_port(efx);
1923
Ben Hutchings29c69a42013-01-28 19:01:06 +00001924 /* Stop the kernel transmit interface. This is only valid if
1925 * the device is stopped or detached; otherwise the watchdog
1926 * may fire immediately.
1927 */
1928 WARN_ON(netif_running(efx->net_dev) &&
1929 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001930 netif_tx_disable(efx->net_dev);
1931
1932 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001933}
1934
1935static void efx_remove_all(struct efx_nic *efx)
1936{
Ben Hutchings46426102010-09-10 06:42:33 +00001937 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001938 efx_remove_filters(efx);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001939#ifdef CONFIG_SFC_SRIOV
1940 efx->type->vswitching_remove(efx);
1941#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01001942 efx_remove_port(efx);
1943 efx_remove_nic(efx);
1944}
1945
Ben Hutchings8ceee662008-04-27 12:55:59 +01001946/**************************************************************************
1947 *
1948 * Interrupt moderation
1949 *
1950 **************************************************************************/
Bert Kenward539de7c2016-08-11 13:02:09 +01001951unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001952{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001953 if (usecs == 0)
1954 return 0;
Bert Kenward539de7c2016-08-11 13:02:09 +01001955 if (usecs * 1000 < efx->timer_quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001956 return 1; /* never round down to 0 */
Bert Kenward539de7c2016-08-11 13:02:09 +01001957 return usecs * 1000 / efx->timer_quantum_ns;
1958}
1959
1960unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
1961{
1962 /* We must round up when converting ticks to microseconds
1963 * because we round down when converting the other way.
1964 */
1965 return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001966}
1967
Ben Hutchings8ceee662008-04-27 12:55:59 +01001968/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001969int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1970 unsigned int rx_usecs, bool rx_adaptive,
1971 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001972{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001973 struct efx_channel *channel;
Bert Kenwardd95e3292016-08-11 13:02:36 +01001974 unsigned int timer_max_us;
1975
Ben Hutchings8ceee662008-04-27 12:55:59 +01001976 EFX_ASSERT_RESET_SERIALISED(efx);
1977
Bert Kenwardd95e3292016-08-11 13:02:36 +01001978 timer_max_us = efx->timer_max_ns / 1000;
1979
1980 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001981 return -EINVAL;
1982
Bert Kenward539de7c2016-08-11 13:02:09 +01001983 if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
Ben Hutchings9e393b32011-09-05 07:43:04 +00001984 !rx_may_override_tx) {
1985 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1986 "RX and TX IRQ moderation must be equal\n");
1987 return -EINVAL;
1988 }
1989
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001990 efx->irq_rx_adaptive = rx_adaptive;
Bert Kenward539de7c2016-08-11 13:02:09 +01001991 efx->irq_rx_moderation_us = rx_usecs;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001992 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001993 if (efx_channel_has_rx_queue(channel))
Bert Kenward539de7c2016-08-11 13:02:09 +01001994 channel->irq_moderation_us = rx_usecs;
Ben Hutchings525da902011-02-07 23:04:38 +00001995 else if (efx_channel_has_tx_queues(channel))
Bert Kenward539de7c2016-08-11 13:02:09 +01001996 channel->irq_moderation_us = tx_usecs;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001997 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001998
1999 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002000}
2001
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002002void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
2003 unsigned int *rx_usecs, bool *rx_adaptive)
2004{
2005 *rx_adaptive = efx->irq_rx_adaptive;
Bert Kenward539de7c2016-08-11 13:02:09 +01002006 *rx_usecs = efx->irq_rx_moderation_us;
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002007
2008 /* If channels are shared between RX and TX, so is IRQ
2009 * moderation. Otherwise, IRQ moderation is the same for all
2010 * TX channels and is not adaptive.
2011 */
Bert Kenward539de7c2016-08-11 13:02:09 +01002012 if (efx->tx_channel_offset == 0) {
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002013 *tx_usecs = *rx_usecs;
Bert Kenward539de7c2016-08-11 13:02:09 +01002014 } else {
2015 struct efx_channel *tx_channel;
2016
2017 tx_channel = efx->channel[efx->tx_channel_offset];
2018 *tx_usecs = tx_channel->irq_moderation_us;
2019 }
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002020}
2021
Ben Hutchings8ceee662008-04-27 12:55:59 +01002022/**************************************************************************
2023 *
2024 * Hardware monitor
2025 *
2026 **************************************************************************/
2027
Ben Hutchingse254c272010-09-20 08:44:10 +00002028/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002029static void efx_monitor(struct work_struct *data)
2030{
2031 struct efx_nic *efx = container_of(data, struct efx_nic,
2032 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002033
Ben Hutchings62776d02010-06-23 11:30:07 +00002034 netif_vdbg(efx, timer, efx->net_dev,
2035 "hardware monitor executing on CPU %d\n",
2036 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002037 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002038
Ben Hutchings8ceee662008-04-27 12:55:59 +01002039 /* If the mac_lock is already held then it is likely a port
2040 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00002041 * most of the work of monitor() anyway. */
2042 if (mutex_trylock(&efx->mac_lock)) {
2043 if (efx->port_enabled)
2044 efx->type->monitor(efx);
2045 mutex_unlock(&efx->mac_lock);
2046 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002047
Ben Hutchings8ceee662008-04-27 12:55:59 +01002048 queue_delayed_work(efx->workqueue, &efx->monitor_work,
2049 efx_monitor_interval);
2050}
2051
2052/**************************************************************************
2053 *
2054 * ioctls
2055 *
2056 *************************************************************************/
2057
2058/* Net device ioctl
2059 * Context: process, rtnl_lock() held.
2060 */
2061static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
2062{
Ben Hutchings767e4682008-09-01 12:43:14 +01002063 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00002064 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002065
Stuart Hodgson7c236c42012-09-03 11:09:36 +01002066 if (cmd == SIOCSHWTSTAMP)
Ben Hutchings433dc9b2013-11-14 01:26:21 +00002067 return efx_ptp_set_ts_config(efx, ifr);
2068 if (cmd == SIOCGHWTSTAMP)
2069 return efx_ptp_get_ts_config(efx, ifr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01002070
Ben Hutchings68e7f452009-04-29 08:05:08 +00002071 /* Convert phy_id from older PRTAD/DEVAD format */
2072 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
2073 (data->phy_id & 0xfc00) == 0x0400)
2074 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
2075
2076 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002077}
2078
2079/**************************************************************************
2080 *
2081 * NAPI interface
2082 *
2083 **************************************************************************/
2084
Ben Hutchings7f967c02012-02-13 23:45:02 +00002085static void efx_init_napi_channel(struct efx_channel *channel)
2086{
2087 struct efx_nic *efx = channel->efx;
2088
2089 channel->napi_dev = efx->net_dev;
2090 netif_napi_add(channel->napi_dev, &channel->napi_str,
2091 efx_poll, napi_weight);
Bert Kenwardc0f9c7e2015-10-26 14:23:42 +00002092 efx_channel_busy_poll_init(channel);
Ben Hutchings7f967c02012-02-13 23:45:02 +00002093}
2094
Ben Hutchingse8f14992010-12-07 19:47:34 +00002095static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002096{
2097 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002098
Ben Hutchings7f967c02012-02-13 23:45:02 +00002099 efx_for_each_channel(channel, efx)
2100 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00002101}
2102
2103static void efx_fini_napi_channel(struct efx_channel *channel)
2104{
Eric Dumazet973334a2016-11-16 06:01:47 -08002105 if (channel->napi_dev)
Ben Hutchingse8f14992010-12-07 19:47:34 +00002106 netif_napi_del(&channel->napi_str);
Eric Dumazet973334a2016-11-16 06:01:47 -08002107
Ben Hutchingse8f14992010-12-07 19:47:34 +00002108 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002109}
2110
2111static void efx_fini_napi(struct efx_nic *efx)
2112{
2113 struct efx_channel *channel;
2114
Ben Hutchingse8f14992010-12-07 19:47:34 +00002115 efx_for_each_channel(channel, efx)
2116 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002117}
2118
2119/**************************************************************************
2120 *
2121 * Kernel netpoll interface
2122 *
2123 *************************************************************************/
2124
2125#ifdef CONFIG_NET_POLL_CONTROLLER
2126
2127/* Although in the common case interrupts will be disabled, this is not
2128 * guaranteed. However, all our work happens inside the NAPI callback,
2129 * so no locking is required.
2130 */
2131static void efx_netpoll(struct net_device *net_dev)
2132{
Ben Hutchings767e4682008-09-01 12:43:14 +01002133 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002134 struct efx_channel *channel;
2135
Ben Hutchings64ee3122008-09-01 12:47:38 +01002136 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002137 efx_schedule_channel(channel);
2138}
2139
2140#endif
2141
Alexandre Rames36763262014-07-22 14:03:25 +01002142#ifdef CONFIG_NET_RX_BUSY_POLL
2143static int efx_busy_poll(struct napi_struct *napi)
2144{
2145 struct efx_channel *channel =
2146 container_of(napi, struct efx_channel, napi_str);
2147 struct efx_nic *efx = channel->efx;
2148 int budget = 4;
2149 int old_rx_packets, rx_packets;
2150
2151 if (!netif_running(efx->net_dev))
2152 return LL_FLUSH_FAILED;
2153
Bert Kenwardc0f9c7e2015-10-26 14:23:42 +00002154 if (!efx_channel_try_lock_poll(channel))
Alexandre Rames36763262014-07-22 14:03:25 +01002155 return LL_FLUSH_BUSY;
2156
2157 old_rx_packets = channel->rx_queue.rx_packets;
2158 efx_process_channel(channel, budget);
2159
2160 rx_packets = channel->rx_queue.rx_packets - old_rx_packets;
2161
2162 /* There is no race condition with NAPI here.
2163 * NAPI will automatically be rescheduled if it yielded during busy
2164 * polling, because it was not able to take the lock and thus returned
2165 * the full budget.
2166 */
2167 efx_channel_unlock_poll(channel);
2168
2169 return rx_packets;
2170}
2171#endif
2172
Ben Hutchings8ceee662008-04-27 12:55:59 +01002173/**************************************************************************
2174 *
2175 * Kernel net device interface
2176 *
2177 *************************************************************************/
2178
2179/* Context: process, rtnl_lock() held. */
Shradha Shahe340be92015-05-20 11:11:03 +01002180int efx_net_open(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002181{
Ben Hutchings767e4682008-09-01 12:43:14 +01002182 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002183 int rc;
2184
Ben Hutchings62776d02010-06-23 11:30:07 +00002185 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
2186 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01002187
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002188 rc = efx_check_disabled(efx);
2189 if (rc)
2190 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01002191 if (efx->phy_mode & PHY_MODE_SPECIAL)
2192 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002193 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
2194 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01002195
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00002196 /* Notify the kernel of the link state polled during driver load,
2197 * before the monitor starts running */
2198 efx_link_status_changed(efx);
2199
Ben Hutchings8ceee662008-04-27 12:55:59 +01002200 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002201 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002202 return 0;
2203}
2204
2205/* Context: process, rtnl_lock() held.
2206 * Note that the kernel will ignore our return code; this method
2207 * should really be a void.
2208 */
Shradha Shahe340be92015-05-20 11:11:03 +01002209int efx_net_stop(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002210{
Ben Hutchings767e4682008-09-01 12:43:14 +01002211 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002212
Ben Hutchings62776d02010-06-23 11:30:07 +00002213 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
2214 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01002215
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002216 /* Stop the device and flush all the channels */
2217 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002218
2219 return 0;
2220}
2221
Ben Hutchings5b9e2072008-05-16 21:18:14 +01002222/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +00002223static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
2224 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002225{
Ben Hutchings767e4682008-09-01 12:43:14 +01002226 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002227
Ben Hutchings55edc6e2009-11-25 16:11:35 +00002228 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00002229 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01002230 spin_unlock_bh(&efx->stats_lock);
2231
Ben Hutchings8ceee662008-04-27 12:55:59 +01002232 return stats;
2233}
2234
2235/* Context: netif_tx_lock held, BHs disabled. */
2236static void efx_watchdog(struct net_device *net_dev)
2237{
Ben Hutchings767e4682008-09-01 12:43:14 +01002238 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002239
Ben Hutchings62776d02010-06-23 11:30:07 +00002240 netif_err(efx, tx_err, efx->net_dev,
2241 "TX stuck with port_enabled=%d: resetting channels\n",
2242 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002243
Ben Hutchings739bb23d2008-11-04 20:35:36 +00002244 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002245}
2246
2247
2248/* Context: process, rtnl_lock() held. */
2249static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2250{
Ben Hutchings767e4682008-09-01 12:43:14 +01002251 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002252 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002253
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002254 rc = efx_check_disabled(efx);
2255 if (rc)
2256 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002257
Ben Hutchings62776d02010-06-23 11:30:07 +00002258 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002259
Ben Hutchings29c69a42013-01-28 19:01:06 +00002260 efx_device_detach_sync(efx);
2261 efx_stop_all(efx);
2262
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002263 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002264 net_dev->mtu = new_mtu;
Edward Cree0d322412015-05-20 11:10:03 +01002265 efx_mac_reconfigure(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002266 mutex_unlock(&efx->mac_lock);
2267
Ben Hutchings8ceee662008-04-27 12:55:59 +01002268 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00002269 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002270 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002271}
2272
2273static int efx_set_mac_address(struct net_device *net_dev, void *data)
2274{
Ben Hutchings767e4682008-09-01 12:43:14 +01002275 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002276 struct sockaddr *addr = data;
Ben Hutchingse0b3ae32014-02-12 18:59:54 +00002277 u8 *new_addr = addr->sa_data;
Shradha Shahcfc77c22015-05-20 11:09:30 +01002278 u8 old_addr[6];
2279 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002280
Ben Hutchings8ceee662008-04-27 12:55:59 +01002281 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002282 netif_err(efx, drv, efx->net_dev,
2283 "invalid ethernet MAC address requested: %pM\n",
2284 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002285 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002286 }
2287
Shradha Shahcfc77c22015-05-20 11:09:30 +01002288 /* save old address */
2289 ether_addr_copy(old_addr, net_dev->dev_addr);
Edward Creecd84ff42014-03-07 18:27:41 +00002290 ether_addr_copy(net_dev->dev_addr, new_addr);
Shradha Shah910c8782015-05-20 11:12:48 +01002291 if (efx->type->set_mac_address) {
2292 rc = efx->type->set_mac_address(efx);
Shradha Shahcfc77c22015-05-20 11:09:30 +01002293 if (rc) {
2294 ether_addr_copy(net_dev->dev_addr, old_addr);
2295 return rc;
2296 }
2297 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002298
2299 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002300 mutex_lock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01002301 efx_mac_reconfigure(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002302 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002303
2304 return 0;
2305}
2306
Ben Hutchingsa816f752008-09-01 12:49:12 +01002307/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002308static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002309{
Ben Hutchings767e4682008-09-01 12:43:14 +01002310 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002311
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002312 if (efx->port_enabled)
2313 queue_work(efx->workqueue, &efx->mac_work);
2314 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002315}
2316
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002317static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002318{
2319 struct efx_nic *efx = netdev_priv(net_dev);
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002320 int rc;
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002321
2322 /* If disabling RX n-tuple filtering, clear existing filters */
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002323 if (net_dev->features & ~data & NETIF_F_NTUPLE) {
2324 rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2325 if (rc)
2326 return rc;
2327 }
2328
2329 /* If Rx VLAN filter is changed, update filters via mac_reconfigure */
2330 if ((net_dev->features ^ data) & NETIF_F_HW_VLAN_CTAG_FILTER) {
2331 /* efx_set_rx_mode() will schedule MAC work to update filters
2332 * when a new features are finally set in net_dev.
2333 */
2334 efx_set_rx_mode(net_dev);
2335 }
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002336
2337 return 0;
2338}
2339
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002340static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
2341{
2342 struct efx_nic *efx = netdev_priv(net_dev);
2343
2344 if (efx->type->vlan_rx_add_vid)
2345 return efx->type->vlan_rx_add_vid(efx, proto, vid);
2346 else
2347 return -EOPNOTSUPP;
2348}
2349
2350static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
2351{
2352 struct efx_nic *efx = netdev_priv(net_dev);
2353
2354 if (efx->type->vlan_rx_kill_vid)
2355 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
2356 else
2357 return -EOPNOTSUPP;
2358}
2359
Shradha Shah7fa8d542015-05-06 00:55:13 +01002360static const struct net_device_ops efx_netdev_ops = {
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002361 .ndo_open = efx_net_open,
2362 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002363 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002364 .ndo_tx_timeout = efx_watchdog,
2365 .ndo_start_xmit = efx_hard_start_xmit,
2366 .ndo_validate_addr = eth_validate_addr,
2367 .ndo_do_ioctl = efx_ioctl,
2368 .ndo_change_mtu = efx_change_mtu,
2369 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002370 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002371 .ndo_set_features = efx_set_features,
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002372 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
2373 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002374#ifdef CONFIG_SFC_SRIOV
Shradha Shah7fa8d542015-05-06 00:55:13 +01002375 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2376 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2377 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2378 .ndo_get_vf_config = efx_sriov_get_vf_config,
Edward Cree4392dc62015-05-20 11:12:13 +01002379 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
Shradha Shah1d051e02015-06-02 11:38:16 +01002380 .ndo_get_phys_port_id = efx_sriov_get_phys_port_id,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002381#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002382#ifdef CONFIG_NET_POLL_CONTROLLER
2383 .ndo_poll_controller = efx_netpoll,
2384#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002385 .ndo_setup_tc = efx_setup_tc,
Alexandre Rames36763262014-07-22 14:03:25 +01002386#ifdef CONFIG_NET_RX_BUSY_POLL
2387 .ndo_busy_poll = efx_busy_poll,
2388#endif
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002389#ifdef CONFIG_RFS_ACCEL
2390 .ndo_rx_flow_steer = efx_filter_rfs,
2391#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002392};
2393
Ben Hutchings7dde5962008-12-12 22:09:38 -08002394static void efx_update_name(struct efx_nic *efx)
2395{
2396 strcpy(efx->name, efx->net_dev->name);
2397 efx_mtd_rename(efx);
2398 efx_set_channel_names(efx);
2399}
2400
Ben Hutchings8ceee662008-04-27 12:55:59 +01002401static int efx_netdev_event(struct notifier_block *this,
2402 unsigned long event, void *ptr)
2403{
Jiri Pirko351638e2013-05-28 01:30:21 +00002404 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002405
Shradha Shah7fa8d542015-05-06 00:55:13 +01002406 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
Ben Hutchings7dde5962008-12-12 22:09:38 -08002407 event == NETDEV_CHANGENAME)
2408 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002409
2410 return NOTIFY_DONE;
2411}
2412
2413static struct notifier_block efx_netdev_notifier = {
2414 .notifier_call = efx_netdev_event,
2415};
2416
Ben Hutchings06d5e192008-12-12 21:47:23 -08002417static ssize_t
2418show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2419{
2420 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2421 return sprintf(buf, "%d\n", efx->phy_type);
2422}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002423static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002424
Edward Creee7fef9b2015-05-27 13:14:01 +01002425#ifdef CONFIG_SFC_MCDI_LOGGING
2426static ssize_t show_mcdi_log(struct device *dev, struct device_attribute *attr,
2427 char *buf)
2428{
2429 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2430 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2431
2432 return scnprintf(buf, PAGE_SIZE, "%d\n", mcdi->logging_enabled);
2433}
2434static ssize_t set_mcdi_log(struct device *dev, struct device_attribute *attr,
2435 const char *buf, size_t count)
2436{
2437 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2438 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2439 bool enable = count > 0 && *buf != '0';
2440
2441 mcdi->logging_enabled = enable;
2442 return count;
2443}
2444static DEVICE_ATTR(mcdi_logging, 0644, show_mcdi_log, set_mcdi_log);
2445#endif
2446
Ben Hutchings8ceee662008-04-27 12:55:59 +01002447static int efx_register_netdev(struct efx_nic *efx)
2448{
2449 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002450 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002451 int rc;
2452
2453 net_dev->watchdog_timeo = 5 * HZ;
2454 net_dev->irq = efx->pci_dev->irq;
Shradha Shah7fa8d542015-05-06 00:55:13 +01002455 net_dev->netdev_ops = &efx_netdev_ops;
2456 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
Ben Hutchings8127d662013-08-29 19:19:29 +01002457 net_dev->priv_flags |= IFF_UNICAST_FLT;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002458 net_dev->ethtool_ops = &efx_ethtool_ops;
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002459 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Bert Kenwardcd94e512016-10-18 17:47:45 +01002460 net_dev->min_mtu = EFX_MIN_MTU;
2461 net_dev->max_mtu = EFX_MAX_MTU;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002462
Ben Hutchings7dde5962008-12-12 22:09:38 -08002463 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002464
Ben Hutchings7153f622012-07-27 20:50:52 +01002465 /* Enable resets to be scheduled and check whether any were
2466 * already requested. If so, the NIC is probably hosed so we
2467 * abort.
2468 */
2469 efx->state = STATE_READY;
2470 smp_mb(); /* ensure we change state before checking reset_pending */
2471 if (efx->reset_pending) {
2472 netif_err(efx, probe, efx->net_dev,
2473 "aborting probe due to scheduled reset\n");
2474 rc = -EIO;
2475 goto fail_locked;
2476 }
2477
Ben Hutchingsaed06282009-08-26 08:16:27 +00002478 rc = dev_alloc_name(net_dev, net_dev->name);
2479 if (rc < 0)
2480 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002481 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002482
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002483 /* Always start with carrier off; PHY events will detect the link */
2484 netif_carrier_off(net_dev);
2485
Ben Hutchingsaed06282009-08-26 08:16:27 +00002486 rc = register_netdevice(net_dev);
2487 if (rc)
2488 goto fail_locked;
2489
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002490 efx_for_each_channel(channel, efx) {
2491 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002492 efx_for_each_channel_tx_queue(tx_queue, channel)
2493 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002494 }
2495
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002496 efx_associate(efx);
2497
Ben Hutchings7dde5962008-12-12 22:09:38 -08002498 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002499
Ben Hutchings06d5e192008-12-12 21:47:23 -08002500 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2501 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002502 netif_err(efx, drv, efx->net_dev,
2503 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002504 goto fail_registered;
2505 }
Edward Creee7fef9b2015-05-27 13:14:01 +01002506#ifdef CONFIG_SFC_MCDI_LOGGING
2507 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2508 if (rc) {
2509 netif_err(efx, drv, efx->net_dev,
2510 "failed to init net dev attributes\n");
2511 goto fail_attr_mcdi_logging;
2512 }
2513#endif
Ben Hutchings06d5e192008-12-12 21:47:23 -08002514
Ben Hutchings8ceee662008-04-27 12:55:59 +01002515 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002516
Edward Creee7fef9b2015-05-27 13:14:01 +01002517#ifdef CONFIG_SFC_MCDI_LOGGING
2518fail_attr_mcdi_logging:
2519 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2520#endif
Ben Hutchings7153f622012-07-27 20:50:52 +01002521fail_registered:
2522 rtnl_lock();
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002523 efx_dissociate(efx);
Ben Hutchings7153f622012-07-27 20:50:52 +01002524 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002525fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002526 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002527 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002528 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002529 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002530}
2531
2532static void efx_unregister_netdev(struct efx_nic *efx)
2533{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002534 if (!efx->net_dev)
2535 return;
2536
Ben Hutchings767e4682008-09-01 12:43:14 +01002537 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002538
Edward Creee7fef9b2015-05-27 13:14:01 +01002539 if (efx_dev_registered(efx)) {
2540 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2541#ifdef CONFIG_SFC_MCDI_LOGGING
2542 device_remove_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2543#endif
2544 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2545 unregister_netdev(efx->net_dev);
2546 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002547}
2548
2549/**************************************************************************
2550 *
2551 * Device reset and suspend
2552 *
2553 **************************************************************************/
2554
Ben Hutchings2467ca42008-09-01 12:48:50 +01002555/* Tears down the entire software state and most of the hardware state
2556 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002557void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002558{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002559 EFX_ASSERT_RESET_SERIALISED(efx);
2560
Edward Creee2835462014-04-16 19:27:48 +01002561 if (method == RESET_TYPE_MCDI_TIMEOUT)
2562 efx->type->prepare_flr(efx);
2563
Ben Hutchings2467ca42008-09-01 12:48:50 +01002564 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002565 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002566
2567 mutex_lock(&efx->mac_lock);
Jon Cooper087e9022015-05-20 11:11:35 +01002568 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2569 method != RESET_TYPE_DATAPATH)
Steve Hodgson4b988282009-01-29 17:50:51 +00002570 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002571 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002572}
2573
Ben Hutchings2467ca42008-09-01 12:48:50 +01002574/* This function will always ensure that the locks acquired in
2575 * efx_reset_down() are released. A failure return code indicates
2576 * that we were unable to reinitialise the hardware, and the
2577 * driver should be disabled. If ok is false, then the rx and tx
2578 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002579int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002580{
2581 int rc;
2582
Ben Hutchings2467ca42008-09-01 12:48:50 +01002583 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002584
Edward Creee2835462014-04-16 19:27:48 +01002585 if (method == RESET_TYPE_MCDI_TIMEOUT)
2586 efx->type->finish_flr(efx);
2587
2588 /* Ensure that SRAM is initialised even if we're disabling the device */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002589 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002590 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002591 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002592 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002593 }
2594
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002595 if (!ok)
2596 goto fail;
2597
Jon Cooper087e9022015-05-20 11:11:35 +01002598 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2599 method != RESET_TYPE_DATAPATH) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002600 rc = efx->phy_op->init(efx);
2601 if (rc)
2602 goto fail;
Edward Cree267d9d72015-05-06 00:59:18 +01002603 rc = efx->phy_op->reconfigure(efx);
2604 if (rc && rc != -EPERM)
Ben Hutchings62776d02010-06-23 11:30:07 +00002605 netif_err(efx, drv, efx->net_dev,
2606 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002607 }
2608
Jon Cooper261e4d92013-04-15 18:51:54 +01002609 rc = efx_enable_interrupts(efx);
2610 if (rc)
2611 goto fail;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01002612
2613#ifdef CONFIG_SFC_SRIOV
2614 rc = efx->type->vswitching_restore(efx);
2615 if (rc) /* not fatal; the PF will still work fine */
2616 netif_warn(efx, probe, efx->net_dev,
2617 "failed to restore vswitching rc=%d;"
2618 " VFs may not function\n", rc);
2619#endif
2620
Edward Cree0d322412015-05-20 11:10:03 +01002621 down_read(&efx->filter_sem);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002622 efx_restore_filters(efx);
Edward Cree0d322412015-05-20 11:10:03 +01002623 up_read(&efx->filter_sem);
Shradha Shah7fa8d542015-05-06 00:55:13 +01002624 if (efx->type->sriov_reset)
2625 efx->type->sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002626
2627 mutex_unlock(&efx->mac_lock);
2628
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002629 efx_start_all(efx);
2630
2631 return 0;
2632
2633fail:
2634 efx->port_initialized = false;
2635
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002636 mutex_unlock(&efx->mac_lock);
2637
Ben Hutchings8ceee662008-04-27 12:55:59 +01002638 return rc;
2639}
2640
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002641/* Reset the NIC using the specified method. Note that the reset may
2642 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002643 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002644 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002645 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002646int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002647{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002648 int rc, rc2;
2649 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002650
Ben Hutchings62776d02010-06-23 11:30:07 +00002651 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2652 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002653
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002654 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002655 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002656
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002657 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002658 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002659 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002660 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002661 }
2662
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002663 /* Clear flags for the scopes we covered. We assume the NIC and
2664 * driver are now quiescent so that there is no race here.
2665 */
Edward Creee2835462014-04-16 19:27:48 +01002666 if (method < RESET_TYPE_MAX_METHOD)
2667 efx->reset_pending &= -(1 << (method + 1));
2668 else /* it doesn't fit into the well-ordered scope hierarchy */
2669 __clear_bit(method, &efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002670
2671 /* Reinitialise bus-mastering, which may have been turned off before
2672 * the reset was scheduled. This is still appropriate, even in the
2673 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2674 * can respond to requests. */
2675 pci_set_master(efx->pci_dev);
2676
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002677out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002678 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002679 disabled = rc ||
2680 method == RESET_TYPE_DISABLE ||
2681 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002682 rc2 = efx_reset_up(efx, method, !disabled);
2683 if (rc2) {
2684 disabled = true;
2685 if (!rc)
2686 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002687 }
2688
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002689 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002690 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002691 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002692 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002693 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002694 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002695 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002696 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002697 return rc;
2698}
2699
Alexandre Rames626950d2013-01-14 17:20:22 +00002700/* Try recovery mechanisms.
2701 * For now only EEH is supported.
2702 * Returns 0 if the recovery mechanisms are unsuccessful.
2703 * Returns a non-zero value otherwise.
2704 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002705int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002706{
2707#ifdef CONFIG_EEH
2708 /* A PCI error can occur and not be seen by EEH because nothing
2709 * happens on the PCI bus. In this case the driver may fail and
2710 * schedule a 'recover or reset', leading to this recovery handler.
2711 * Manually call the eeh failure check function.
2712 */
Benjamin Herrenschmidt12a89db2015-03-23 14:00:47 +11002713 struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002714 if (eeh_dev_check_failure(eehdev)) {
2715 /* The EEH mechanisms will handle the error and reset the
2716 * device if necessary.
2717 */
2718 return 1;
2719 }
2720#endif
2721 return 0;
2722}
2723
Jon Cooper74cd60a2013-09-16 14:18:51 +01002724static void efx_wait_for_bist_end(struct efx_nic *efx)
2725{
2726 int i;
2727
2728 for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
2729 if (efx_mcdi_poll_reboot(efx))
2730 goto out;
2731 msleep(BIST_WAIT_DELAY_MS);
2732 }
2733
2734 netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
2735out:
2736 /* Either way unset the BIST flag. If we found no reboot we probably
2737 * won't recover, but we should try.
2738 */
2739 efx->mc_bist_for_other_fn = false;
2740}
2741
Ben Hutchings8ceee662008-04-27 12:55:59 +01002742/* The worker thread exists so that code that cannot sleep can
2743 * schedule a reset for later.
2744 */
2745static void efx_reset_work(struct work_struct *data)
2746{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002747 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002748 unsigned long pending;
2749 enum reset_type method;
2750
2751 pending = ACCESS_ONCE(efx->reset_pending);
2752 method = fls(pending) - 1;
2753
Jon Cooper74cd60a2013-09-16 14:18:51 +01002754 if (method == RESET_TYPE_MC_BIST)
2755 efx_wait_for_bist_end(efx);
2756
Alexandre Rames626950d2013-01-14 17:20:22 +00002757 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2758 method == RESET_TYPE_RECOVER_OR_ALL) &&
2759 efx_try_recovery(efx))
2760 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002761
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002762 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002763 return;
2764
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002765 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002766
2767 /* We checked the state in efx_schedule_reset() but it may
2768 * have changed by now. Now that we have the RTNL lock,
2769 * it cannot change again.
2770 */
2771 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002772 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002773
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002774 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002775}
2776
2777void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2778{
2779 enum reset_type method;
2780
Alexandre Rames626950d2013-01-14 17:20:22 +00002781 if (efx->state == STATE_RECOVERY) {
2782 netif_dbg(efx, drv, efx->net_dev,
2783 "recovering: skip scheduling %s reset\n",
2784 RESET_TYPE(type));
2785 return;
2786 }
2787
Ben Hutchings8ceee662008-04-27 12:55:59 +01002788 switch (type) {
2789 case RESET_TYPE_INVISIBLE:
2790 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002791 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002792 case RESET_TYPE_WORLD:
2793 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002794 case RESET_TYPE_RECOVER_OR_DISABLE:
Jon Cooper087e9022015-05-20 11:11:35 +01002795 case RESET_TYPE_DATAPATH:
Jon Cooper74cd60a2013-09-16 14:18:51 +01002796 case RESET_TYPE_MC_BIST:
Edward Creee2835462014-04-16 19:27:48 +01002797 case RESET_TYPE_MCDI_TIMEOUT:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002798 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002799 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2800 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002801 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002802 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002803 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002804 netif_dbg(efx, drv, efx->net_dev,
2805 "scheduling %s reset for %s\n",
2806 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002807 break;
2808 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002809
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002810 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002811 smp_mb(); /* ensure we change reset_pending before checking state */
2812
2813 /* If we're not READY then just leave the flags set as the cue
2814 * to abort probing or reschedule the reset later.
2815 */
2816 if (ACCESS_ONCE(efx->state) != STATE_READY)
2817 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002818
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002819 /* efx_process_channel() will no longer read events once a
2820 * reset is scheduled. So switch back to poll'd MCDI completions. */
2821 efx_mcdi_mode_poll(efx);
2822
Steve Hodgson1ab00622008-12-12 21:33:02 -08002823 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002824}
2825
2826/**************************************************************************
2827 *
2828 * List of NICs we support
2829 *
2830 **************************************************************************/
2831
2832/* PCI device ID table */
Benoit Taine9baa3c32014-08-08 15:56:03 +02002833static const struct pci_device_id efx_pci_table[] = {
Ben Hutchings547c4742011-12-02 18:23:56 +00002834 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002835 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002836 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002837 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8127d662013-08-29 19:19:29 +01002838 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
2839 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Shradha Shah6f7f8aa2015-05-06 01:00:07 +01002840 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
2841 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
Mateusz Wrzesinski3b06a002014-07-14 08:38:49 +01002842 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
2843 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Bert Kenwarddd248f12015-11-30 09:05:47 +00002844 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
2845 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
2846 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
2847 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2848 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
2849 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002850 {0} /* end of list */
2851};
2852
2853/**************************************************************************
2854 *
Ben Hutchings37594332009-11-23 16:05:45 +00002855 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002856 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002857 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002858 * Needed so all function pointers are valid and do not have to be tested
2859 * before use
2860 *
2861 **************************************************************************/
2862int efx_port_dummy_op_int(struct efx_nic *efx)
2863{
2864 return 0;
2865}
2866void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002867
2868static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002869{
2870 return false;
2871}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002872
stephen hemminger6c8c2512011-04-14 05:50:12 +00002873static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002874 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002875 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002876 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002877 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002878};
2879
Ben Hutchings8ceee662008-04-27 12:55:59 +01002880/**************************************************************************
2881 *
2882 * Data housekeeping
2883 *
2884 **************************************************************************/
2885
2886/* This zeroes out and then fills in the invariants in a struct
2887 * efx_nic (including all sub-structures).
2888 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002889static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002890 struct pci_dev *pci_dev, struct net_device *net_dev)
2891{
Ben Hutchings46426102010-09-10 06:42:33 +00002892 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002893
2894 /* Initialise common structures */
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002895 INIT_LIST_HEAD(&efx->node);
2896 INIT_LIST_HEAD(&efx->secondary_list);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002897 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002898#ifdef CONFIG_SFC_MTD
2899 INIT_LIST_HEAD(&efx->mtd_list);
2900#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002901 INIT_WORK(&efx->reset_work, efx_reset_work);
2902 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002903 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002904 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002905 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002906 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002907 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002908
2909 efx->net_dev = net_dev;
Jon Cooper43a37392012-10-18 15:49:54 +01002910 efx->rx_prefix_size = efx->type->rx_prefix_size;
Andrew Rybchenko2ec03012013-11-16 11:02:27 +04002911 efx->rx_ip_align =
2912 NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
Jon Cooper43a37392012-10-18 15:49:54 +01002913 efx->rx_packet_hash_offset =
2914 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
Jon Cooperbd9a2652013-11-18 12:54:41 +00002915 efx->rx_packet_ts_offset =
2916 efx->type->rx_ts_offset - efx->type->rx_prefix_size;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002917 spin_lock_init(&efx->stats_lock);
2918 mutex_init(&efx->mac_lock);
2919 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002920 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002921 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002922 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002923
2924 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002925 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2926 if (!efx->channel[i])
2927 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002928 efx->msi_context[i].efx = efx;
2929 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002930 }
2931
Ben Hutchings8ceee662008-04-27 12:55:59 +01002932 /* Higher numbered interrupt modes are less capable! */
2933 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2934 interrupt_mode);
2935
Ben Hutchings6977dc62008-12-26 13:44:39 -08002936 /* Would be good to use the net_dev name, but we're too early */
2937 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2938 pci_name(pci_dev));
2939 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002940 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002941 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002942
Ben Hutchings8ceee662008-04-27 12:55:59 +01002943 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002944
2945fail:
2946 efx_fini_struct(efx);
2947 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002948}
2949
2950static void efx_fini_struct(struct efx_nic *efx)
2951{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002952 int i;
2953
2954 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2955 kfree(efx->channel[i]);
2956
Ben Hutchingsef215e62013-12-05 20:13:22 +00002957 kfree(efx->vpd_sn);
2958
Ben Hutchings8ceee662008-04-27 12:55:59 +01002959 if (efx->workqueue) {
2960 destroy_workqueue(efx->workqueue);
2961 efx->workqueue = NULL;
2962 }
2963}
2964
Edward Creee4d112e2014-07-15 11:58:12 +01002965void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
2966{
2967 u64 n_rx_nodesc_trunc = 0;
2968 struct efx_channel *channel;
2969
2970 efx_for_each_channel(channel, efx)
2971 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
2972 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
2973 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
2974}
2975
Ben Hutchings8ceee662008-04-27 12:55:59 +01002976/**************************************************************************
2977 *
2978 * PCI interface
2979 *
2980 **************************************************************************/
2981
2982/* Main body of final NIC shutdown code
2983 * This is called only at module unload (or hotplug removal).
2984 */
2985static void efx_pci_remove_main(struct efx_nic *efx)
2986{
Ben Hutchings7153f622012-07-27 20:50:52 +01002987 /* Flush reset_work. It can no longer be scheduled since we
2988 * are not READY.
2989 */
2990 BUG_ON(efx->state == STATE_READY);
2991 cancel_work_sync(&efx->reset_work);
2992
Ben Hutchingsd8291182012-10-05 23:35:41 +01002993 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002994 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002995 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002996 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002997 efx_fini_napi(efx);
2998 efx_remove_all(efx);
2999}
3000
3001/* Final NIC shutdown
Daniel Pieczko2a3fc312015-06-02 11:40:46 +01003002 * This is called only at module unload (or hotplug removal). A PF can call
3003 * this on its VFs to ensure they are unbound first.
Ben Hutchings8ceee662008-04-27 12:55:59 +01003004 */
3005static void efx_pci_remove(struct pci_dev *pci_dev)
3006{
3007 struct efx_nic *efx;
3008
3009 efx = pci_get_drvdata(pci_dev);
3010 if (!efx)
3011 return;
3012
3013 /* Mark the NIC as fini, then stop the interface */
3014 rtnl_lock();
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01003015 efx_dissociate(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003016 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003017 efx_disable_interrupts(efx);
Edward Creeea6bb992015-06-15 18:27:54 +01003018 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003019 rtnl_unlock();
3020
Shradha Shah7fa8d542015-05-06 00:55:13 +01003021 if (efx->type->sriov_fini)
3022 efx->type->sriov_fini(efx);
3023
Ben Hutchings8ceee662008-04-27 12:55:59 +01003024 efx_unregister_netdev(efx);
3025
Ben Hutchings7dde5962008-12-12 22:09:38 -08003026 efx_mtd_remove(efx);
3027
Ben Hutchings8ceee662008-04-27 12:55:59 +01003028 efx_pci_remove_main(efx);
3029
Ben Hutchings8ceee662008-04-27 12:55:59 +01003030 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00003031 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003032
Ben Hutchings8ceee662008-04-27 12:55:59 +01003033 efx_fini_struct(efx);
3034 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00003035
3036 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003037};
3038
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003039/* NIC VPD information
3040 * Called during probe to display the part number of the
3041 * installed NIC. VPD is potentially very large but this should
3042 * always appear within the first 512 bytes.
3043 */
3044#define SFC_VPD_LEN 512
Ben Hutchingsef215e62013-12-05 20:13:22 +00003045static void efx_probe_vpd_strings(struct efx_nic *efx)
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003046{
3047 struct pci_dev *dev = efx->pci_dev;
3048 char vpd_data[SFC_VPD_LEN];
3049 ssize_t vpd_size;
Ben Hutchingsef215e62013-12-05 20:13:22 +00003050 int ro_start, ro_size, i, j;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003051
3052 /* Get the vpd data from the device */
3053 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
3054 if (vpd_size <= 0) {
3055 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
3056 return;
3057 }
3058
3059 /* Get the Read only section */
Ben Hutchingsef215e62013-12-05 20:13:22 +00003060 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
3061 if (ro_start < 0) {
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003062 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
3063 return;
3064 }
3065
Ben Hutchingsef215e62013-12-05 20:13:22 +00003066 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
3067 j = ro_size;
3068 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003069 if (i + j > vpd_size)
3070 j = vpd_size - i;
3071
3072 /* Get the Part number */
3073 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
3074 if (i < 0) {
3075 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
3076 return;
3077 }
3078
3079 j = pci_vpd_info_field_size(&vpd_data[i]);
3080 i += PCI_VPD_INFO_FLD_HDR_SIZE;
3081 if (i + j > vpd_size) {
3082 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
3083 return;
3084 }
3085
3086 netif_info(efx, drv, efx->net_dev,
3087 "Part Number : %.*s\n", j, &vpd_data[i]);
Ben Hutchingsef215e62013-12-05 20:13:22 +00003088
3089 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
3090 j = ro_size;
3091 i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
3092 if (i < 0) {
3093 netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
3094 return;
3095 }
3096
3097 j = pci_vpd_info_field_size(&vpd_data[i]);
3098 i += PCI_VPD_INFO_FLD_HDR_SIZE;
3099 if (i + j > vpd_size) {
3100 netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
3101 return;
3102 }
3103
3104 efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
3105 if (!efx->vpd_sn)
3106 return;
3107
3108 snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003109}
3110
3111
Ben Hutchings8ceee662008-04-27 12:55:59 +01003112/* Main body of NIC initialisation
3113 * This is called at module load (or hotplug insertion, theoretically).
3114 */
3115static int efx_pci_probe_main(struct efx_nic *efx)
3116{
3117 int rc;
3118
3119 /* Do start-of-day initialisation */
3120 rc = efx_probe_all(efx);
3121 if (rc)
3122 goto fail1;
3123
Ben Hutchingse8f14992010-12-07 19:47:34 +00003124 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003125
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00003126 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003127 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00003128 netif_err(efx, probe, efx->net_dev,
3129 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00003130 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003131 }
3132
3133 rc = efx_init_port(efx);
3134 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00003135 netif_err(efx, probe, efx->net_dev,
3136 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00003137 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003138 }
3139
Ben Hutchings152b6a62009-11-29 03:43:56 +00003140 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003141 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00003142 goto fail5;
Jon Cooper261e4d92013-04-15 18:51:54 +01003143 rc = efx_enable_interrupts(efx);
3144 if (rc)
3145 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003146
3147 return 0;
3148
Jon Cooper261e4d92013-04-15 18:51:54 +01003149 fail6:
3150 efx_nic_fini_interrupt(efx);
Ben Hutchings278c0622009-11-23 16:05:12 +00003151 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003152 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003153 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00003154 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003155 fail3:
3156 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003157 efx_remove_all(efx);
3158 fail1:
3159 return rc;
3160}
3161
3162/* NIC initialisation
3163 *
3164 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00003165 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003166 * sets up and registers the network devices with the kernel and hooks
3167 * the interrupt service routine. It does not prepare the device for
3168 * transmission; this is left to the first time one of the network
3169 * interfaces is brought up (i.e. efx_net_open).
3170 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05003171static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00003172 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01003173{
Ben Hutchings8ceee662008-04-27 12:55:59 +01003174 struct net_device *net_dev;
3175 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003176 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003177
3178 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00003179 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
3180 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003181 if (!net_dev)
3182 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01003183 efx = netdev_priv(net_dev);
3184 efx->type = (const struct efx_nic_type *) entry->driver_data;
Andrew Rybchenkoebfcd0f2016-06-15 17:43:20 +01003185 efx->fixed_features |= NETIF_F_HIGHDMA;
Andrew Rybchenkoeb7cfd82016-06-15 17:51:36 +01003186
Ben Hutchings8ceee662008-04-27 12:55:59 +01003187 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00003188 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01003189 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003190 if (rc)
3191 goto fail1;
3192
Ben Hutchings62776d02010-06-23 11:30:07 +00003193 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01003194 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003195
Shradha Shah6f7f8aa2015-05-06 01:00:07 +01003196 if (!efx->type->is_vf)
3197 efx_probe_vpd_strings(efx);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003198
Ben Hutchings8ceee662008-04-27 12:55:59 +01003199 /* Set up basic I/O (BAR mappings etc) */
3200 rc = efx_init_io(efx);
3201 if (rc)
3202 goto fail2;
3203
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003204 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003205 if (rc)
3206 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08003207
Edward Cree46d1efd2016-11-17 10:52:36 +00003208 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
3209 NETIF_F_TSO | NETIF_F_RXCSUM);
3210 if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
3211 net_dev->features |= NETIF_F_TSO6;
3212 /* Check whether device supports TSO */
3213 if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
3214 net_dev->features &= ~NETIF_F_ALL_TSO;
3215 /* Mask for features that also apply to VLAN devices */
3216 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
3217 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
3218 NETIF_F_RXCSUM);
3219
3220 net_dev->hw_features = net_dev->features & ~efx->fixed_features;
3221
3222 /* Disable VLAN filtering by default. It may be enforced if
3223 * the feature is fixed (i.e. VLAN filters are required to
3224 * receive VLAN tagged packets due to vPort restrictions).
3225 */
3226 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
3227 net_dev->features |= efx->fixed_features;
3228
Ben Hutchings8ceee662008-04-27 12:55:59 +01003229 rc = efx_register_netdev(efx);
3230 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003231 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003232
Shradha Shah7fa8d542015-05-06 00:55:13 +01003233 if (efx->type->sriov_init) {
3234 rc = efx->type->sriov_init(efx);
3235 if (rc)
3236 netif_err(efx, probe, efx->net_dev,
3237 "SR-IOV can't be enabled rc %d\n", rc);
3238 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003239
Ben Hutchings62776d02010-06-23 11:30:07 +00003240 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003241
Ben Hutchings7c431612012-01-27 17:23:58 +00003242 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003243 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00003244 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003245 rtnl_unlock();
Bert Kenward09a04202015-12-23 08:58:15 +00003246 if (rc && rc != -EPERM)
Ben Hutchings7c431612012-01-27 17:23:58 +00003247 netif_warn(efx, probe, efx->net_dev,
3248 "failed to create MTDs (%d)\n", rc);
3249
Alexandre Rames626950d2013-01-14 17:20:22 +00003250 rc = pci_enable_pcie_error_reporting(pci_dev);
3251 if (rc && rc != -EINVAL)
Bert Kenward09a04202015-12-23 08:58:15 +00003252 netif_notice(efx, probe, efx->net_dev,
3253 "PCIE error reporting unavailable (%d).\n",
3254 rc);
Alexandre Rames626950d2013-01-14 17:20:22 +00003255
Ben Hutchings8ceee662008-04-27 12:55:59 +01003256 return 0;
3257
Ben Hutchings8ceee662008-04-27 12:55:59 +01003258 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003259 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003260 fail3:
3261 efx_fini_io(efx);
3262 fail2:
3263 efx_fini_struct(efx);
3264 fail1:
Steve Hodgson5e2a9112010-02-12 12:32:27 -08003265 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00003266 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003267 free_netdev(net_dev);
3268 return rc;
3269}
3270
Shradha Shah834e23d2015-05-06 00:55:58 +01003271/* efx_pci_sriov_configure returns the actual number of Virtual Functions
3272 * enabled on success
3273 */
3274#ifdef CONFIG_SFC_SRIOV
3275static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
3276{
3277 int rc;
3278 struct efx_nic *efx = pci_get_drvdata(dev);
3279
3280 if (efx->type->sriov_configure) {
3281 rc = efx->type->sriov_configure(efx, num_vfs);
3282 if (rc)
3283 return rc;
3284 else
3285 return num_vfs;
3286 } else
3287 return -EOPNOTSUPP;
3288}
3289#endif
3290
Ben Hutchings89c758f2009-11-29 03:43:07 +00003291static int efx_pm_freeze(struct device *dev)
3292{
3293 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3294
Ben Hutchings61da0262012-07-27 19:35:39 +01003295 rtnl_lock();
3296
Ben Hutchings6032fb52012-07-27 19:35:47 +01003297 if (efx->state != STATE_DISABLED) {
3298 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003299
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01003300 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003301
Ben Hutchings6032fb52012-07-27 19:35:47 +01003302 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003303 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01003304 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00003305
Ben Hutchings61da0262012-07-27 19:35:39 +01003306 rtnl_unlock();
3307
Ben Hutchings89c758f2009-11-29 03:43:07 +00003308 return 0;
3309}
3310
3311static int efx_pm_thaw(struct device *dev)
3312{
Jon Cooper261e4d92013-04-15 18:51:54 +01003313 int rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003314 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3315
Ben Hutchings61da0262012-07-27 19:35:39 +01003316 rtnl_lock();
3317
Ben Hutchings6032fb52012-07-27 19:35:47 +01003318 if (efx->state != STATE_DISABLED) {
Jon Cooper261e4d92013-04-15 18:51:54 +01003319 rc = efx_enable_interrupts(efx);
3320 if (rc)
3321 goto fail;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003322
Ben Hutchings6032fb52012-07-27 19:35:47 +01003323 mutex_lock(&efx->mac_lock);
3324 efx->phy_op->reconfigure(efx);
3325 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003326
Ben Hutchings6032fb52012-07-27 19:35:47 +01003327 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003328
Ben Hutchings6032fb52012-07-27 19:35:47 +01003329 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003330
Ben Hutchings6032fb52012-07-27 19:35:47 +01003331 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003332
Ben Hutchings6032fb52012-07-27 19:35:47 +01003333 efx->type->resume_wol(efx);
3334 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00003335
Ben Hutchings61da0262012-07-27 19:35:39 +01003336 rtnl_unlock();
3337
Steve Hodgson319ba642010-06-01 11:17:24 +00003338 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
3339 queue_work(reset_workqueue, &efx->reset_work);
3340
Ben Hutchings89c758f2009-11-29 03:43:07 +00003341 return 0;
Jon Cooper261e4d92013-04-15 18:51:54 +01003342
3343fail:
3344 rtnl_unlock();
3345
3346 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003347}
3348
3349static int efx_pm_poweroff(struct device *dev)
3350{
3351 struct pci_dev *pci_dev = to_pci_dev(dev);
3352 struct efx_nic *efx = pci_get_drvdata(pci_dev);
3353
3354 efx->type->fini(efx);
3355
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01003356 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003357
3358 pci_save_state(pci_dev);
3359 return pci_set_power_state(pci_dev, PCI_D3hot);
3360}
3361
3362/* Used for both resume and restore */
3363static int efx_pm_resume(struct device *dev)
3364{
3365 struct pci_dev *pci_dev = to_pci_dev(dev);
3366 struct efx_nic *efx = pci_get_drvdata(pci_dev);
3367 int rc;
3368
3369 rc = pci_set_power_state(pci_dev, PCI_D0);
3370 if (rc)
3371 return rc;
3372 pci_restore_state(pci_dev);
3373 rc = pci_enable_device(pci_dev);
3374 if (rc)
3375 return rc;
3376 pci_set_master(efx->pci_dev);
3377 rc = efx->type->reset(efx, RESET_TYPE_ALL);
3378 if (rc)
3379 return rc;
3380 rc = efx->type->init(efx);
3381 if (rc)
3382 return rc;
Jon Cooper261e4d92013-04-15 18:51:54 +01003383 rc = efx_pm_thaw(dev);
3384 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003385}
3386
3387static int efx_pm_suspend(struct device *dev)
3388{
3389 int rc;
3390
3391 efx_pm_freeze(dev);
3392 rc = efx_pm_poweroff(dev);
3393 if (rc)
3394 efx_pm_resume(dev);
3395 return rc;
3396}
3397
Ben Hutchings18e83e42012-01-05 19:05:20 +00003398static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00003399 .suspend = efx_pm_suspend,
3400 .resume = efx_pm_resume,
3401 .freeze = efx_pm_freeze,
3402 .thaw = efx_pm_thaw,
3403 .poweroff = efx_pm_poweroff,
3404 .restore = efx_pm_resume,
3405};
3406
Alexandre Rames626950d2013-01-14 17:20:22 +00003407/* A PCI error affecting this device was detected.
3408 * At this point MMIO and DMA may be disabled.
3409 * Stop the software path and request a slot reset.
3410 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003411static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
3412 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00003413{
3414 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3415 struct efx_nic *efx = pci_get_drvdata(pdev);
3416
3417 if (state == pci_channel_io_perm_failure)
3418 return PCI_ERS_RESULT_DISCONNECT;
3419
3420 rtnl_lock();
3421
3422 if (efx->state != STATE_DISABLED) {
3423 efx->state = STATE_RECOVERY;
3424 efx->reset_pending = 0;
3425
3426 efx_device_detach_sync(efx);
3427
3428 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003429 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00003430
3431 status = PCI_ERS_RESULT_NEED_RESET;
3432 } else {
3433 /* If the interface is disabled we don't want to do anything
3434 * with it.
3435 */
3436 status = PCI_ERS_RESULT_RECOVERED;
3437 }
3438
3439 rtnl_unlock();
3440
3441 pci_disable_device(pdev);
3442
3443 return status;
3444}
3445
Joe Perchesdbedd442015-03-06 20:49:12 -08003446/* Fake a successful reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003447static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00003448{
3449 struct efx_nic *efx = pci_get_drvdata(pdev);
3450 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3451 int rc;
3452
3453 if (pci_enable_device(pdev)) {
3454 netif_err(efx, hw, efx->net_dev,
3455 "Cannot re-enable PCI device after reset.\n");
3456 status = PCI_ERS_RESULT_DISCONNECT;
3457 }
3458
3459 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
3460 if (rc) {
3461 netif_err(efx, hw, efx->net_dev,
3462 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3463 /* Non-fatal error. Continue. */
3464 }
3465
3466 return status;
3467}
3468
3469/* Perform the actual reset and resume I/O operations. */
3470static void efx_io_resume(struct pci_dev *pdev)
3471{
3472 struct efx_nic *efx = pci_get_drvdata(pdev);
3473 int rc;
3474
3475 rtnl_lock();
3476
3477 if (efx->state == STATE_DISABLED)
3478 goto out;
3479
3480 rc = efx_reset(efx, RESET_TYPE_ALL);
3481 if (rc) {
3482 netif_err(efx, hw, efx->net_dev,
3483 "efx_reset failed after PCI error (%d)\n", rc);
3484 } else {
3485 efx->state = STATE_READY;
3486 netif_dbg(efx, hw, efx->net_dev,
3487 "Done resetting and resuming IO after PCI error.\n");
3488 }
3489
3490out:
3491 rtnl_unlock();
3492}
3493
3494/* For simplicity and reliability, we always require a slot reset and try to
3495 * reset the hardware when a pci error affecting the device is detected.
3496 * We leave both the link_reset and mmio_enabled callback unimplemented:
3497 * with our request for slot reset the mmio_enabled callback will never be
3498 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3499 */
Julia Lawallc300366b2015-11-14 11:06:57 +01003500static const struct pci_error_handlers efx_err_handlers = {
Alexandre Rames626950d2013-01-14 17:20:22 +00003501 .error_detected = efx_io_error_detected,
3502 .slot_reset = efx_io_slot_reset,
3503 .resume = efx_io_resume,
3504};
3505
Ben Hutchings8ceee662008-04-27 12:55:59 +01003506static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003507 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003508 .id_table = efx_pci_table,
3509 .probe = efx_pci_probe,
3510 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003511 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003512 .err_handler = &efx_err_handlers,
Shradha Shah834e23d2015-05-06 00:55:58 +01003513#ifdef CONFIG_SFC_SRIOV
3514 .sriov_configure = efx_pci_sriov_configure,
3515#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003516};
3517
3518/**************************************************************************
3519 *
3520 * Kernel module interface
3521 *
3522 *************************************************************************/
3523
3524module_param(interrupt_mode, uint, 0444);
3525MODULE_PARM_DESC(interrupt_mode,
3526 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3527
3528static int __init efx_init_module(void)
3529{
3530 int rc;
3531
3532 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3533
3534 rc = register_netdevice_notifier(&efx_netdev_notifier);
3535 if (rc)
3536 goto err_notifier;
3537
Shradha Shah7fa8d542015-05-06 00:55:13 +01003538#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003539 rc = efx_init_sriov();
3540 if (rc)
3541 goto err_sriov;
Shradha Shah7fa8d542015-05-06 00:55:13 +01003542#endif
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003543
Steve Hodgson1ab00622008-12-12 21:33:02 -08003544 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3545 if (!reset_workqueue) {
3546 rc = -ENOMEM;
3547 goto err_reset;
3548 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003549
3550 rc = pci_register_driver(&efx_pci_driver);
3551 if (rc < 0)
3552 goto err_pci;
3553
3554 return 0;
3555
3556 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003557 destroy_workqueue(reset_workqueue);
3558 err_reset:
Shradha Shah7fa8d542015-05-06 00:55:13 +01003559#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003560 efx_fini_sriov();
3561 err_sriov:
Shradha Shah7fa8d542015-05-06 00:55:13 +01003562#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003563 unregister_netdevice_notifier(&efx_netdev_notifier);
3564 err_notifier:
3565 return rc;
3566}
3567
3568static void __exit efx_exit_module(void)
3569{
3570 printk(KERN_INFO "Solarflare NET driver unloading\n");
3571
3572 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003573 destroy_workqueue(reset_workqueue);
Shradha Shah7fa8d542015-05-06 00:55:13 +01003574#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003575 efx_fini_sriov();
Shradha Shah7fa8d542015-05-06 00:55:13 +01003576#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003577 unregister_netdevice_notifier(&efx_netdev_notifier);
3578
3579}
3580
3581module_init(efx_init_module);
3582module_exit(efx_exit_module);
3583
Ben Hutchings906bb262009-11-29 15:16:19 +00003584MODULE_AUTHOR("Solarflare Communications and "
3585 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings6a350fd2014-02-12 19:00:07 +00003586MODULE_DESCRIPTION("Solarflare network driver");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003587MODULE_LICENSE("GPL");
3588MODULE_DEVICE_TABLE(pci, efx_pci_table);