blob: 7321a4cf6f4dc9819132ef92441e211a558ce757 [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"
Jon Coopere5fbd972017-02-08 16:52:10 +000026#include <net/gre.h>
27#include <net/udp_tunnel.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010028#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000029#include "nic.h"
Edward Cree71827442017-12-18 16:56:19 +000030#include "io.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000031#include "selftest.h"
Shradha Shah7fa8d542015-05-06 00:55:13 +010032#include "sriov.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010033
Ben Hutchings8880f4e2009-11-29 15:15:41 +000034#include "mcdi.h"
Jon Coopere5fbd972017-02-08 16:52:10 +000035#include "mcdi_pcol.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000036#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000037
Ben Hutchingsc4593022009-11-23 16:08:17 +000038/**************************************************************************
39 *
40 * Type name strings
41 *
42 **************************************************************************
43 */
44
45/* Loopback mode names (see LOOPBACK_MODE()) */
46const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000047const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000048 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000049 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000050 [LOOPBACK_GMAC] = "GMAC",
51 [LOOPBACK_XGMII] = "XGMII",
52 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000053 [LOOPBACK_XAUI] = "XAUI",
54 [LOOPBACK_GMII] = "GMII",
55 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000056 [LOOPBACK_XGBR] = "XGBR",
57 [LOOPBACK_XFI] = "XFI",
58 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
59 [LOOPBACK_GMII_FAR] = "GMII_FAR",
60 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
61 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000062 [LOOPBACK_GPHY] = "GPHY",
63 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000064 [LOOPBACK_PCS] = "PCS",
65 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000066 [LOOPBACK_XPORT] = "XPORT",
67 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000068 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000069 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
70 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000071 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000072 [LOOPBACK_XFI_WS] = "XFI_WS",
73 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000074 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000075};
76
Ben Hutchingsc4593022009-11-23 16:08:17 +000077const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000078const char *const efx_reset_type_names[] = {
Alexandre Rames626950d2013-01-14 17:20:22 +000079 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
80 [RESET_TYPE_ALL] = "ALL",
81 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
82 [RESET_TYPE_WORLD] = "WORLD",
83 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
Jon Cooper087e9022015-05-20 11:11:35 +010084 [RESET_TYPE_DATAPATH] = "DATAPATH",
Edward Creee2835462014-04-16 19:27:48 +010085 [RESET_TYPE_MC_BIST] = "MC_BIST",
Alexandre Rames626950d2013-01-14 17:20:22 +000086 [RESET_TYPE_DISABLE] = "DISABLE",
87 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
88 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
Alexandre Rames3de82b92013-06-13 11:36:15 +010089 [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
Alexandre Rames626950d2013-01-14 17:20:22 +000090 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
91 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Edward Creee2835462014-04-16 19:27:48 +010092 [RESET_TYPE_MCDI_TIMEOUT] = "MCDI_TIMEOUT (FLR)",
Ben Hutchingsc4593022009-11-23 16:08:17 +000093};
94
Jon Coopere5fbd972017-02-08 16:52:10 +000095/* UDP tunnel type names */
96static const char *const efx_udp_tunnel_type_names[] = {
97 [TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN] = "vxlan",
98 [TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE] = "geneve",
99};
100
101void efx_get_udp_tunnel_type_name(u16 type, char *buf, size_t buflen)
102{
103 if (type < ARRAY_SIZE(efx_udp_tunnel_type_names) &&
104 efx_udp_tunnel_type_names[type] != NULL)
105 snprintf(buf, buflen, "%s", efx_udp_tunnel_type_names[type]);
106 else
107 snprintf(buf, buflen, "type %d", type);
108}
109
Steve Hodgson1ab00622008-12-12 21:33:02 -0800110/* Reset workqueue. If any NIC has a hardware failure then a reset will be
111 * queued onto this work queue. This is not a per-nic work queue, because
112 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
113 */
114static struct workqueue_struct *reset_workqueue;
115
Jon Cooper74cd60a2013-09-16 14:18:51 +0100116/* How often and how many times to poll for a reset while waiting for a
117 * BIST that another function started to complete.
118 */
119#define BIST_WAIT_DELAY_MS 100
120#define BIST_WAIT_DELAY_COUNT 100
121
Ben Hutchings8ceee662008-04-27 12:55:59 +0100122/**************************************************************************
123 *
124 * Configurable values
125 *
126 *************************************************************************/
127
128/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100129 * Use separate channels for TX and RX events
130 *
Neil Turton28b581a2008-12-12 21:41:06 -0800131 * Set this to 1 to use separate channels for TX and RX. It allows us
132 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100133 *
Neil Turton28b581a2008-12-12 21:41:06 -0800134 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100135 */
Shradha Shahb0fbdae2015-08-28 10:55:42 +0100136bool efx_separate_tx_channels;
137module_param(efx_separate_tx_channels, bool, 0444);
138MODULE_PARM_DESC(efx_separate_tx_channels,
Neil Turton28b581a2008-12-12 21:41:06 -0800139 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100140
141/* This is the weight assigned to each of the (per-channel) virtual
142 * NAPI devices.
143 */
144static int napi_weight = 64;
145
146/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000147 * monitor.
148 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000149 * - Check the on-board hardware monitor;
150 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000151 * On Siena-based NICs for power systems with EEH support, this will give EEH a
152 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100153 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000154static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100155
Ben Hutchings8ceee662008-04-27 12:55:59 +0100156/* Initial interrupt moderation settings. They can be modified after
157 * module load with ethtool.
158 *
159 * The default for RX should strike a balance between increasing the
160 * round-trip latency and reducing overhead.
161 */
162static unsigned int rx_irq_mod_usec = 60;
163
164/* Initial interrupt moderation settings. They can be modified after
165 * module load with ethtool.
166 *
167 * This default is chosen to ensure that a 10G link does not go idle
168 * while a TX queue is stopped after it has become full. A queue is
169 * restarted when it drops below half full. The time this takes (assuming
170 * worst case 3 descriptors per packet and 1024 descriptors) is
171 * 512 / 3 * 1.2 = 205 usec.
172 */
173static unsigned int tx_irq_mod_usec = 150;
174
175/* This is the first interrupt mode to try out of:
176 * 0 => MSI-X
177 * 1 => MSI
178 * 2 => legacy
179 */
180static unsigned int interrupt_mode;
181
182/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
183 * i.e. the number of CPUs among which we may distribute simultaneous
184 * interrupt handling.
185 *
186 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000187 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100188 */
189static unsigned int rss_cpus;
190module_param(rss_cpus, uint, 0444);
191MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
192
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000193static bool phy_flash_cfg;
194module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800195MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
196
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000197static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000198module_param(irq_adapt_low_thresh, uint, 0644);
199MODULE_PARM_DESC(irq_adapt_low_thresh,
200 "Threshold score for reducing IRQ moderation");
201
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000202static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000203module_param(irq_adapt_high_thresh, uint, 0644);
204MODULE_PARM_DESC(irq_adapt_high_thresh,
205 "Threshold score for increasing IRQ moderation");
206
Ben Hutchings62776d02010-06-23 11:30:07 +0000207static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
208 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
209 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
210 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
211module_param(debug, uint, 0);
212MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
213
Ben Hutchings8ceee662008-04-27 12:55:59 +0100214/**************************************************************************
215 *
216 * Utility functions and prototypes
217 *
218 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000219
Jon Cooper261e4d92013-04-15 18:51:54 +0100220static int efx_soft_enable_interrupts(struct efx_nic *efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100221static void efx_soft_disable_interrupts(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000222static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000223static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000224static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100225static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000226static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100227static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000228static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000229static void efx_fini_struct(struct efx_nic *efx);
230static void efx_start_all(struct efx_nic *efx);
231static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100232
233#define EFX_ASSERT_RESET_SERIALISED(efx) \
234 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100235 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000236 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000237 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100238 ASSERT_RTNL(); \
239 } while (0)
240
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100241static int efx_check_disabled(struct efx_nic *efx)
242{
Alexandre Rames626950d2013-01-14 17:20:22 +0000243 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100244 netif_err(efx, drv, efx->net_dev,
245 "device is disabled due to earlier errors\n");
246 return -EIO;
247 }
248 return 0;
249}
250
Ben Hutchings8ceee662008-04-27 12:55:59 +0100251/**************************************************************************
252 *
253 * Event queue processing
254 *
255 *************************************************************************/
256
257/* Process channel's event queue
258 *
259 * This function is responsible for processing the event queue of a
260 * single channel. The caller must guarantee that this function will
261 * never be concurrently called more than once on the same channel,
262 * though different channels may be being processed concurrently.
263 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000264static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100265{
Peter Dunningc9368352015-07-08 10:05:10 +0100266 struct efx_tx_queue *tx_queue;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000267 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100268
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000269 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100270 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100271
Peter Dunningc9368352015-07-08 10:05:10 +0100272 efx_for_each_channel_tx_queue(tx_queue, channel) {
273 tx_queue->pkts_compl = 0;
274 tx_queue->bytes_compl = 0;
275 }
276
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000277 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000278 if (spent && efx_channel_has_rx_queue(channel)) {
279 struct efx_rx_queue *rx_queue =
280 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100281
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000282 efx_rx_flush_packet(channel);
Jon Coopercce28792013-10-02 11:04:14 +0100283 efx_fast_push_rx_descriptors(rx_queue, true);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100284 }
285
Peter Dunningc9368352015-07-08 10:05:10 +0100286 /* Update BQL */
287 efx_for_each_channel_tx_queue(tx_queue, channel) {
288 if (tx_queue->bytes_compl) {
289 netdev_tx_completed_queue(tx_queue->core_txq,
290 tx_queue->pkts_compl, tx_queue->bytes_compl);
291 }
292 }
293
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000294 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100295}
296
Ben Hutchings8ceee662008-04-27 12:55:59 +0100297/* NAPI poll handler
298 *
299 * NAPI guarantees serialisation of polls of the same device, which
300 * provides the guarantee required by efx_process_channel().
301 */
Bert Kenward539de7c2016-08-11 13:02:09 +0100302static void efx_update_irq_mod(struct efx_nic *efx, struct efx_channel *channel)
303{
304 int step = efx->irq_mod_step_us;
305
306 if (channel->irq_mod_score < irq_adapt_low_thresh) {
307 if (channel->irq_moderation_us > step) {
308 channel->irq_moderation_us -= step;
309 efx->type->push_irq_moderation(channel);
310 }
311 } else if (channel->irq_mod_score > irq_adapt_high_thresh) {
312 if (channel->irq_moderation_us <
313 efx->irq_rx_moderation_us) {
314 channel->irq_moderation_us += step;
315 efx->type->push_irq_moderation(channel);
316 }
317 }
318
319 channel->irq_count = 0;
320 channel->irq_mod_score = 0;
321}
322
Ben Hutchings8ceee662008-04-27 12:55:59 +0100323static int efx_poll(struct napi_struct *napi, int budget)
324{
325 struct efx_channel *channel =
326 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000327 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000328 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100329
Ben Hutchings62776d02010-06-23 11:30:07 +0000330 netif_vdbg(efx, intr, efx->net_dev,
331 "channel %d NAPI poll executing on CPU %d\n",
332 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100333
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000334 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100335
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000336 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000337 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000338 efx->irq_rx_adaptive &&
339 unlikely(++channel->irq_count == 1000)) {
Bert Kenward539de7c2016-08-11 13:02:09 +0100340 efx_update_irq_mod(efx, channel);
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000341 }
342
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000343 efx_filter_rfs_expire(channel);
344
Ben Hutchings8ceee662008-04-27 12:55:59 +0100345 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800346 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100347 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100348 * interrupts have already been disabled.
349 */
Bert Kenwardf820c0a2017-02-06 16:50:55 +0000350 if (napi_complete_done(napi, spent))
351 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100352 }
353
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000354 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100355}
356
Ben Hutchings8ceee662008-04-27 12:55:59 +0100357/* Create event queue
358 * Event queue memory allocations are done only once. If the channel
359 * is reset, the memory buffer will be reused; this guards against
360 * errors during channel reset and also simplifies interrupt handling.
361 */
362static int efx_probe_eventq(struct efx_channel *channel)
363{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000364 struct efx_nic *efx = channel->efx;
365 unsigned long entries;
366
Ben Hutchings86ee5302012-01-09 19:51:22 +0000367 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000368 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100369
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000370 /* Build an event queue with room for one event per tx and rx buffer,
371 * plus some extra for link state events and MCDI completions. */
372 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
Edward Creee01b16a2016-12-02 15:51:33 +0000373 EFX_WARN_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000374 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
375
Ben Hutchings152b6a62009-11-29 03:43:56 +0000376 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100377}
378
379/* Prepare channel's event queue */
Jon Cooper261e4d92013-04-15 18:51:54 +0100380static int efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100381{
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100382 struct efx_nic *efx = channel->efx;
Jon Cooper261e4d92013-04-15 18:51:54 +0100383 int rc;
384
385 EFX_WARN_ON_PARANOID(channel->eventq_init);
386
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100387 netif_dbg(efx, drv, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000388 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100389
Jon Cooper261e4d92013-04-15 18:51:54 +0100390 rc = efx_nic_init_eventq(channel);
391 if (rc == 0) {
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100392 efx->type->push_irq_moderation(channel);
Jon Cooper261e4d92013-04-15 18:51:54 +0100393 channel->eventq_read_ptr = 0;
394 channel->eventq_init = true;
395 }
396 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100397}
398
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000399/* Enable event queue processing and NAPI */
Alexandre Rames36763262014-07-22 14:03:25 +0100400void efx_start_eventq(struct efx_channel *channel)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000401{
402 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
403 "chan %d start event queue\n", channel->channel);
404
Ben Hutchings514bedb2012-10-05 19:30:16 +0100405 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000406 channel->enabled = true;
407 smp_wmb();
408
409 napi_enable(&channel->napi_str);
410 efx_nic_eventq_read_ack(channel);
411}
412
413/* Disable event queue processing and NAPI */
Alexandre Rames36763262014-07-22 14:03:25 +0100414void efx_stop_eventq(struct efx_channel *channel)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000415{
416 if (!channel->enabled)
417 return;
418
419 napi_disable(&channel->napi_str);
420 channel->enabled = false;
421}
422
Ben Hutchings8ceee662008-04-27 12:55:59 +0100423static void efx_fini_eventq(struct efx_channel *channel)
424{
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100425 if (!channel->eventq_init)
426 return;
427
Ben Hutchings62776d02010-06-23 11:30:07 +0000428 netif_dbg(channel->efx, drv, channel->efx->net_dev,
429 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100430
Ben Hutchings152b6a62009-11-29 03:43:56 +0000431 efx_nic_fini_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100432 channel->eventq_init = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100433}
434
435static void efx_remove_eventq(struct efx_channel *channel)
436{
Ben Hutchings62776d02010-06-23 11:30:07 +0000437 netif_dbg(channel->efx, drv, channel->efx->net_dev,
438 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100439
Ben Hutchings152b6a62009-11-29 03:43:56 +0000440 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100441}
442
443/**************************************************************************
444 *
445 * Channel handling
446 *
447 *************************************************************************/
448
Ben Hutchings7f967c02012-02-13 23:45:02 +0000449/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000450static struct efx_channel *
451efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
452{
453 struct efx_channel *channel;
454 struct efx_rx_queue *rx_queue;
455 struct efx_tx_queue *tx_queue;
456 int j;
457
Ben Hutchings7f967c02012-02-13 23:45:02 +0000458 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
459 if (!channel)
460 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000461
Ben Hutchings7f967c02012-02-13 23:45:02 +0000462 channel->efx = efx;
463 channel->channel = i;
464 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000465
Ben Hutchings7f967c02012-02-13 23:45:02 +0000466 for (j = 0; j < EFX_TXQ_TYPES; j++) {
467 tx_queue = &channel->tx_queue[j];
468 tx_queue->efx = efx;
469 tx_queue->queue = i * EFX_TXQ_TYPES + j;
470 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000471 }
472
Ben Hutchings46426102010-09-10 06:42:33 +0000473 rx_queue = &channel->rx_queue;
474 rx_queue->efx = efx;
Kees Cook7aa14022017-10-24 01:45:59 -0700475 timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0);
Ben Hutchings46426102010-09-10 06:42:33 +0000476
477 return channel;
478}
479
Ben Hutchings7f967c02012-02-13 23:45:02 +0000480/* Allocate and initialise a channel structure, copying parameters
481 * (but not resources) from an old channel structure.
482 */
483static struct efx_channel *
484efx_copy_channel(const struct efx_channel *old_channel)
485{
486 struct efx_channel *channel;
487 struct efx_rx_queue *rx_queue;
488 struct efx_tx_queue *tx_queue;
489 int j;
490
491 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
492 if (!channel)
493 return NULL;
494
495 *channel = *old_channel;
496
497 channel->napi_dev = NULL;
Bert Kenward46d054f2016-11-11 15:56:51 +0000498 INIT_HLIST_NODE(&channel->napi_str.napi_hash_node);
499 channel->napi_str.napi_id = 0;
500 channel->napi_str.state = 0;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000501 memset(&channel->eventq, 0, sizeof(channel->eventq));
502
503 for (j = 0; j < EFX_TXQ_TYPES; j++) {
504 tx_queue = &channel->tx_queue[j];
505 if (tx_queue->channel)
506 tx_queue->channel = channel;
507 tx_queue->buffer = NULL;
508 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
509 }
510
511 rx_queue = &channel->rx_queue;
512 rx_queue->buffer = NULL;
513 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
Kees Cook7aa14022017-10-24 01:45:59 -0700514 timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000515
516 return channel;
517}
518
Ben Hutchings8ceee662008-04-27 12:55:59 +0100519static int efx_probe_channel(struct efx_channel *channel)
520{
521 struct efx_tx_queue *tx_queue;
522 struct efx_rx_queue *rx_queue;
523 int rc;
524
Ben Hutchings62776d02010-06-23 11:30:07 +0000525 netif_dbg(channel->efx, probe, channel->efx->net_dev,
526 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100527
Ben Hutchings7f967c02012-02-13 23:45:02 +0000528 rc = channel->type->pre_probe(channel);
529 if (rc)
530 goto fail;
531
Ben Hutchings8ceee662008-04-27 12:55:59 +0100532 rc = efx_probe_eventq(channel);
533 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000534 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100535
536 efx_for_each_channel_tx_queue(tx_queue, channel) {
537 rc = efx_probe_tx_queue(tx_queue);
538 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000539 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100540 }
541
542 efx_for_each_channel_rx_queue(rx_queue, channel) {
543 rc = efx_probe_rx_queue(rx_queue);
544 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000545 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100546 }
547
Ben Hutchings8ceee662008-04-27 12:55:59 +0100548 return 0;
549
Ben Hutchings7f967c02012-02-13 23:45:02 +0000550fail:
551 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100552 return rc;
553}
554
Ben Hutchings7f967c02012-02-13 23:45:02 +0000555static void
556efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
557{
558 struct efx_nic *efx = channel->efx;
559 const char *type;
560 int number;
561
562 number = channel->channel;
563 if (efx->tx_channel_offset == 0) {
564 type = "";
565 } else if (channel->channel < efx->tx_channel_offset) {
566 type = "-rx";
567 } else {
568 type = "-tx";
569 number -= efx->tx_channel_offset;
570 }
571 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
572}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100573
Ben Hutchings56536e92008-12-12 21:37:02 -0800574static void efx_set_channel_names(struct efx_nic *efx)
575{
576 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800577
Ben Hutchings7f967c02012-02-13 23:45:02 +0000578 efx_for_each_channel(channel, efx)
579 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100580 efx->msi_context[channel->channel].name,
581 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800582}
583
Ben Hutchings46426102010-09-10 06:42:33 +0000584static int efx_probe_channels(struct efx_nic *efx)
585{
586 struct efx_channel *channel;
587 int rc;
588
589 /* Restart special buffer allocation */
590 efx->next_buffer_table = 0;
591
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000592 /* Probe channels in reverse, so that any 'extra' channels
593 * use the start of the buffer table. This allows the traffic
594 * channels to be resized without moving them or wasting the
595 * entries before them.
596 */
597 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000598 rc = efx_probe_channel(channel);
599 if (rc) {
600 netif_err(efx, probe, efx->net_dev,
601 "failed to create channel %d\n",
602 channel->channel);
603 goto fail;
604 }
605 }
606 efx_set_channel_names(efx);
607
608 return 0;
609
610fail:
611 efx_remove_channels(efx);
612 return rc;
613}
614
Ben Hutchings8ceee662008-04-27 12:55:59 +0100615/* Channels are shutdown and reinitialised whilst the NIC is running
616 * to propagate configuration changes (mtu, checksum offload), or
617 * to clear hardware error conditions
618 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000619static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100620{
Andrew Rybchenkoebfcd0f2016-06-15 17:43:20 +0100621 netdev_features_t old_features = efx->net_dev->features;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000622 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100623 struct efx_tx_queue *tx_queue;
624 struct efx_rx_queue *rx_queue;
625 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000626 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100627
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100628 /* Calculate the rx buffer allocation parameters required to
629 * support the current MTU, including padding for header
630 * alignment and overruns.
631 */
Jon Cooper43a37392012-10-18 15:49:54 +0100632 efx->rx_dma_len = (efx->rx_prefix_size +
Ben Hutchings272baee2013-01-29 23:33:14 +0000633 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
634 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000635 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Andrew Rybchenko2ec03012013-11-16 11:02:27 +0400636 efx->rx_ip_align + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000637 if (rx_buf_len <= PAGE_SIZE) {
Jon Coopere8c68c02013-03-08 10:18:28 +0000638 efx->rx_scatter = efx->type->always_rx_scatter;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000639 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000640 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000641 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000642 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000643 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
644 EFX_RX_BUF_ALIGNMENT) >
645 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000646 efx->rx_scatter = true;
647 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
648 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000649 } else {
650 efx->rx_scatter = false;
651 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000652 }
653
Daniel Pieczko1648a232013-02-13 10:54:41 +0000654 efx_rx_config_page_split(efx);
655 if (efx->rx_buffer_order)
656 netif_dbg(efx, drv, efx->net_dev,
657 "RX buf len=%u; page order=%u batch=%u\n",
658 efx->rx_dma_len, efx->rx_buffer_order,
659 efx->rx_pages_per_batch);
660 else
661 netif_dbg(efx, drv, efx->net_dev,
662 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
663 efx->rx_dma_len, efx->rx_page_buf_step,
664 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000665
Andrew Rybchenkoebfcd0f2016-06-15 17:43:20 +0100666 /* Restore previously fixed features in hw_features and remove
667 * features which are fixed now
668 */
669 efx->net_dev->hw_features |= efx->net_dev->features;
670 efx->net_dev->hw_features &= ~efx->fixed_features;
671 efx->net_dev->features |= efx->fixed_features;
672 if (efx->net_dev->features != old_features)
673 netdev_features_change(efx->net_dev);
674
Jon Coopere8c68c02013-03-08 10:18:28 +0000675 /* RX filters may also have scatter-enabled flags */
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000676 if (efx->rx_scatter != old_rx_scatter)
Ben Hutchingsadd72472012-11-08 01:46:53 +0000677 efx->type->filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100678
Ben Hutchings14bf718fb2012-05-22 01:27:58 +0100679 /* We must keep at least one descriptor in a TX ring empty.
680 * We could avoid this when the queue size does not exactly
681 * match the hardware ring size, but it's not that important.
682 * Therefore we stop the queue when one more skb might fill
683 * the ring completely. We wake it when half way back to
684 * empty.
685 */
686 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
687 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
688
Ben Hutchings8ceee662008-04-27 12:55:59 +0100689 /* Initialise the channels */
690 efx_for_each_channel(channel, efx) {
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100691 efx_for_each_channel_tx_queue(tx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100692 efx_init_tx_queue(tx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100693 atomic_inc(&efx->active_queues);
694 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100695
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000696 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100697 efx_init_rx_queue(rx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100698 atomic_inc(&efx->active_queues);
Jon Coopercce28792013-10-02 11:04:14 +0100699 efx_stop_eventq(channel);
700 efx_fast_push_rx_descriptors(rx_queue, false);
701 efx_start_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000702 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100703
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000704 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100705 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000706
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000707 efx_ptp_start_datapath(efx);
708
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000709 if (netif_device_present(efx->net_dev))
710 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100711}
712
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000713static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100714{
715 struct efx_channel *channel;
716 struct efx_tx_queue *tx_queue;
717 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100718 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100719
720 EFX_ASSERT_RESET_SERIALISED(efx);
721 BUG_ON(efx->port_enabled);
722
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000723 efx_ptp_stop_datapath(efx);
724
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100725 /* Stop RX refill */
726 efx_for_each_channel(channel, efx) {
727 efx_for_each_channel_rx_queue(rx_queue, channel)
728 rx_queue->refill_enabled = false;
729 }
730
Ben Hutchings8ceee662008-04-27 12:55:59 +0100731 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000732 /* RX packet processing is pipelined, so wait for the
733 * NAPI handler to complete. At least event queue 0
734 * might be kept active by non-data events, so don't
735 * use napi_synchronize() but actually disable NAPI
736 * temporarily.
737 */
738 if (efx_channel_has_rx_queue(channel)) {
739 efx_stop_eventq(channel);
740 efx_start_eventq(channel);
741 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100742 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100743
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100744 rc = efx->type->fini_dmaq(efx);
Edward Cree5a6681e2016-11-28 18:55:34 +0000745 if (rc) {
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100746 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
747 } else {
748 netif_dbg(efx, drv, efx->net_dev,
749 "successfully flushed all queues\n");
750 }
751
752 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100753 efx_for_each_channel_rx_queue(rx_queue, channel)
754 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000755 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100756 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100757 }
758}
759
760static void efx_remove_channel(struct efx_channel *channel)
761{
762 struct efx_tx_queue *tx_queue;
763 struct efx_rx_queue *rx_queue;
764
Ben Hutchings62776d02010-06-23 11:30:07 +0000765 netif_dbg(channel->efx, drv, channel->efx->net_dev,
766 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100767
768 efx_for_each_channel_rx_queue(rx_queue, channel)
769 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000770 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100771 efx_remove_tx_queue(tx_queue);
772 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100773 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100774}
775
Ben Hutchings46426102010-09-10 06:42:33 +0000776static void efx_remove_channels(struct efx_nic *efx)
777{
778 struct efx_channel *channel;
779
780 efx_for_each_channel(channel, efx)
781 efx_remove_channel(channel);
782}
783
784int
785efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
786{
787 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
788 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000789 unsigned i, next_buffer_table = 0;
Jon Cooper261e4d92013-04-15 18:51:54 +0100790 int rc, rc2;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100791
792 rc = efx_check_disabled(efx);
793 if (rc)
794 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000795
796 /* Not all channels should be reallocated. We must avoid
797 * reallocating their buffer table entries.
798 */
799 efx_for_each_channel(channel, efx) {
800 struct efx_rx_queue *rx_queue;
801 struct efx_tx_queue *tx_queue;
802
803 if (channel->type->copy)
804 continue;
805 next_buffer_table = max(next_buffer_table,
806 channel->eventq.index +
807 channel->eventq.entries);
808 efx_for_each_channel_rx_queue(rx_queue, channel)
809 next_buffer_table = max(next_buffer_table,
810 rx_queue->rxd.index +
811 rx_queue->rxd.entries);
812 efx_for_each_channel_tx_queue(tx_queue, channel)
813 next_buffer_table = max(next_buffer_table,
814 tx_queue->txd.index +
815 tx_queue->txd.entries);
816 }
Ben Hutchings46426102010-09-10 06:42:33 +0000817
Ben Hutchings29c69a42013-01-28 19:01:06 +0000818 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000819 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100820 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000821
Ben Hutchings7f967c02012-02-13 23:45:02 +0000822 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000823 memset(other_channel, 0, sizeof(other_channel));
824 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000825 channel = efx->channel[i];
826 if (channel->type->copy)
827 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000828 if (!channel) {
829 rc = -ENOMEM;
830 goto out;
831 }
832 other_channel[i] = channel;
833 }
834
835 /* Swap entry counts and channel pointers */
836 old_rxq_entries = efx->rxq_entries;
837 old_txq_entries = efx->txq_entries;
838 efx->rxq_entries = rxq_entries;
839 efx->txq_entries = txq_entries;
840 for (i = 0; i < efx->n_channels; i++) {
841 channel = efx->channel[i];
842 efx->channel[i] = other_channel[i];
843 other_channel[i] = channel;
844 }
845
Ben Hutchings7f967c02012-02-13 23:45:02 +0000846 /* Restart buffer table allocation */
847 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000848
Ben Hutchingse8f14992010-12-07 19:47:34 +0000849 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000850 channel = efx->channel[i];
851 if (!channel->type->copy)
852 continue;
853 rc = efx_probe_channel(channel);
854 if (rc)
855 goto rollback;
856 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000857 }
Ben Hutchings46426102010-09-10 06:42:33 +0000858
Ben Hutchings7f967c02012-02-13 23:45:02 +0000859out:
860 /* Destroy unused channel structures */
861 for (i = 0; i < efx->n_channels; i++) {
862 channel = other_channel[i];
863 if (channel && channel->type->copy) {
864 efx_fini_napi_channel(channel);
865 efx_remove_channel(channel);
866 kfree(channel);
867 }
868 }
869
Jon Cooper261e4d92013-04-15 18:51:54 +0100870 rc2 = efx_soft_enable_interrupts(efx);
871 if (rc2) {
872 rc = rc ? rc : rc2;
873 netif_err(efx, drv, efx->net_dev,
874 "unable to restart interrupts on channel reallocation\n");
875 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
876 } else {
877 efx_start_all(efx);
Peter Dunning9c568fd2017-02-17 15:50:43 +0000878 efx_device_attach_if_not_resetting(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +0100879 }
Ben Hutchings46426102010-09-10 06:42:33 +0000880 return rc;
881
882rollback:
883 /* Swap back */
884 efx->rxq_entries = old_rxq_entries;
885 efx->txq_entries = old_txq_entries;
886 for (i = 0; i < efx->n_channels; i++) {
887 channel = efx->channel[i];
888 efx->channel[i] = other_channel[i];
889 other_channel[i] = channel;
890 }
891 goto out;
892}
893
Steve Hodgson90d683a2010-06-01 11:19:39 +0000894void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100895{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000896 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100897}
898
kbuild test robote7345ba2018-01-26 17:00:39 +0000899static bool efx_default_channel_want_txqs(struct efx_channel *channel)
Edward Cree2935e3c2018-01-25 17:26:06 +0000900{
901 return channel->channel - channel->efx->tx_channel_offset <
902 channel->efx->n_tx_channels;
903}
904
Ben Hutchings7f967c02012-02-13 23:45:02 +0000905static const struct efx_channel_type efx_default_channel_type = {
906 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100907 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000908 .get_name = efx_get_channel_name,
909 .copy = efx_copy_channel,
Edward Cree2935e3c2018-01-25 17:26:06 +0000910 .want_txqs = efx_default_channel_want_txqs,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000911 .keep_eventq = false,
Edward Cree2935e3c2018-01-25 17:26:06 +0000912 .want_pio = true,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000913};
914
915int efx_channel_dummy_op_int(struct efx_channel *channel)
916{
917 return 0;
918}
919
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100920void efx_channel_dummy_op_void(struct efx_channel *channel)
921{
922}
923
Ben Hutchings8ceee662008-04-27 12:55:59 +0100924/**************************************************************************
925 *
926 * Port handling
927 *
928 **************************************************************************/
929
930/* This ensures that the kernel is kept informed (via
931 * netif_carrier_on/off) of the link status, and also maintains the
932 * link status's stop on the port's TX queue.
933 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000934void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100935{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000936 struct efx_link_state *link_state = &efx->link_state;
937
Ben Hutchings8ceee662008-04-27 12:55:59 +0100938 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
939 * that no events are triggered between unregister_netdev() and the
940 * driver unloading. A more general condition is that NETDEV_CHANGE
941 * can only be generated between NETDEV_UP and NETDEV_DOWN */
942 if (!netif_running(efx->net_dev))
943 return;
944
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000945 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100946 efx->n_link_state_changes++;
947
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000948 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100949 netif_carrier_on(efx->net_dev);
950 else
951 netif_carrier_off(efx->net_dev);
952 }
953
954 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000955 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000956 netif_info(efx, link, efx->net_dev,
Ben Hutchings964e6132012-11-19 23:08:22 +0000957 "link up at %uMbps %s-duplex (MTU %d)\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000958 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings964e6132012-11-19 23:08:22 +0000959 efx->net_dev->mtu);
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000960 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000961 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100962}
963
Edward Creec2ab85d2018-01-10 18:00:14 +0000964void efx_link_set_advertising(struct efx_nic *efx,
965 const unsigned long *advertising)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000966{
Edward Creec2ab85d2018-01-10 18:00:14 +0000967 memcpy(efx->link_advertising, advertising,
968 sizeof(__ETHTOOL_DECLARE_LINK_MODE_MASK()));
969
970 efx->link_advertising[0] |= ADVERTISED_Autoneg;
971 if (advertising[0] & ADVERTISED_Pause)
972 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
973 else
974 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
975 if (advertising[0] & ADVERTISED_Asym_Pause)
976 efx->wanted_fc ^= EFX_FC_TX;
977}
978
979/* Equivalent to efx_link_set_advertising with all-zeroes, except does not
980 * force the Autoneg bit on.
981 */
982void efx_link_clear_advertising(struct efx_nic *efx)
983{
984 bitmap_zero(efx->link_advertising, __ETHTOOL_LINK_MODE_MASK_NBITS);
985 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000986}
987
David S. Millerb56269462011-05-17 17:53:22 -0400988void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000989{
990 efx->wanted_fc = wanted_fc;
Edward Creec2ab85d2018-01-10 18:00:14 +0000991 if (efx->link_advertising[0]) {
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000992 if (wanted_fc & EFX_FC_RX)
Edward Creec2ab85d2018-01-10 18:00:14 +0000993 efx->link_advertising[0] |= (ADVERTISED_Pause |
994 ADVERTISED_Asym_Pause);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000995 else
Edward Creec2ab85d2018-01-10 18:00:14 +0000996 efx->link_advertising[0] &= ~(ADVERTISED_Pause |
997 ADVERTISED_Asym_Pause);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000998 if (wanted_fc & EFX_FC_TX)
Edward Creec2ab85d2018-01-10 18:00:14 +0000999 efx->link_advertising[0] ^= ADVERTISED_Asym_Pause;
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001000 }
1001}
1002
Ben Hutchings115122a2009-03-04 09:52:52 +00001003static void efx_fini_port(struct efx_nic *efx);
1004
Edward Cree0d322412015-05-20 11:10:03 +01001005/* We assume that efx->type->reconfigure_mac will always try to sync RX
1006 * filters and therefore needs to read-lock the filter table against freeing
1007 */
1008void efx_mac_reconfigure(struct efx_nic *efx)
1009{
1010 down_read(&efx->filter_sem);
1011 efx->type->reconfigure_mac(efx);
1012 up_read(&efx->filter_sem);
1013}
1014
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001015/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
1016 * the MAC appropriately. All other PHY configuration changes are pushed
1017 * through phy_op->set_settings(), and pushed asynchronously to the MAC
1018 * through efx_monitor().
1019 *
1020 * Callers must hold the mac_lock
1021 */
1022int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001023{
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001024 enum efx_phy_mode phy_mode;
1025 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001026
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001027 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001028
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001029 /* Disable PHY transmit in mac level loopbacks */
1030 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001031 if (LOOPBACK_INTERNAL(efx))
1032 efx->phy_mode |= PHY_MODE_TX_DISABLED;
1033 else
1034 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001035
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001036 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001037
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001038 if (rc)
1039 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001040
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001041 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001042}
1043
1044/* Reinitialise the MAC to pick up new PHY settings, even if the port is
1045 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001046int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001047{
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001048 int rc;
1049
Ben Hutchings8ceee662008-04-27 12:55:59 +01001050 EFX_ASSERT_RESET_SERIALISED(efx);
1051
1052 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001053 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001054 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001055
1056 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001057}
1058
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001059/* Asynchronous work item for changing MAC promiscuity and multicast
1060 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
1061 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001062static void efx_mac_work(struct work_struct *data)
1063{
1064 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
1065
1066 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +01001067 if (efx->port_enabled)
Edward Cree0d322412015-05-20 11:10:03 +01001068 efx_mac_reconfigure(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001069 mutex_unlock(&efx->mac_lock);
1070}
1071
Ben Hutchings8ceee662008-04-27 12:55:59 +01001072static int efx_probe_port(struct efx_nic *efx)
1073{
1074 int rc;
1075
Ben Hutchings62776d02010-06-23 11:30:07 +00001076 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001077
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001078 if (phy_flash_cfg)
1079 efx->phy_mode = PHY_MODE_SPECIAL;
1080
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001081 /* Connect up MAC/PHY operations table */
1082 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001083 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001084 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001085
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001086 /* Initialise MAC address to permanent address */
Edward Creecd84ff42014-03-07 18:27:41 +00001087 ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001088
1089 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001090}
1091
1092static int efx_init_port(struct efx_nic *efx)
1093{
1094 int rc;
1095
Ben Hutchings62776d02010-06-23 11:30:07 +00001096 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001097
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001098 mutex_lock(&efx->mac_lock);
1099
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001100 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001101 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001102 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001103
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001104 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001105
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001106 /* Reconfigure the MAC before creating dma queues (required for
1107 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Edward Cree0d322412015-05-20 11:10:03 +01001108 efx_mac_reconfigure(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001109
1110 /* Ensure the PHY advertises the correct flow control settings */
1111 rc = efx->phy_op->reconfigure(efx);
Edward Cree267d9d72015-05-06 00:59:18 +01001112 if (rc && rc != -EPERM)
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001113 goto fail2;
1114
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001115 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001116 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001117
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001118fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001119 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001120fail1:
1121 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001122 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001123}
1124
Ben Hutchings8ceee662008-04-27 12:55:59 +01001125static void efx_start_port(struct efx_nic *efx)
1126{
Ben Hutchings62776d02010-06-23 11:30:07 +00001127 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001128 BUG_ON(efx->port_enabled);
1129
1130 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001131 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001132
Ben Hutchingsd615c032013-10-08 17:33:20 +01001133 /* Ensure MAC ingress/egress is enabled */
Edward Cree0d322412015-05-20 11:10:03 +01001134 efx_mac_reconfigure(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001135
Ben Hutchings8ceee662008-04-27 12:55:59 +01001136 mutex_unlock(&efx->mac_lock);
1137}
1138
Ben Hutchingsd615c032013-10-08 17:33:20 +01001139/* Cancel work for MAC reconfiguration, periodic hardware monitoring
1140 * and the async self-test, wait for them to finish and prevent them
1141 * being scheduled again. This doesn't cover online resets, which
1142 * should only be cancelled when removing the device.
1143 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001144static void efx_stop_port(struct efx_nic *efx)
1145{
Ben Hutchings62776d02010-06-23 11:30:07 +00001146 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001147
Ben Hutchingsd615c032013-10-08 17:33:20 +01001148 EFX_ASSERT_RESET_SERIALISED(efx);
1149
Ben Hutchings8ceee662008-04-27 12:55:59 +01001150 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001151 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001152 mutex_unlock(&efx->mac_lock);
1153
1154 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001155 netif_addr_lock_bh(efx->net_dev);
1156 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsd615c032013-10-08 17:33:20 +01001157
1158 cancel_delayed_work_sync(&efx->monitor_work);
1159 efx_selftest_async_cancel(efx);
1160 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001161}
1162
1163static void efx_fini_port(struct efx_nic *efx)
1164{
Ben Hutchings62776d02010-06-23 11:30:07 +00001165 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001166
1167 if (!efx->port_initialized)
1168 return;
1169
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001170 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001171 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001172
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001173 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001174 efx_link_status_changed(efx);
1175}
1176
1177static void efx_remove_port(struct efx_nic *efx)
1178{
Ben Hutchings62776d02010-06-23 11:30:07 +00001179 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001180
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001181 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001182}
1183
1184/**************************************************************************
1185 *
1186 * NIC handling
1187 *
1188 **************************************************************************/
1189
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01001190static LIST_HEAD(efx_primary_list);
1191static LIST_HEAD(efx_unassociated_list);
1192
1193static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
1194{
1195 return left->type == right->type &&
1196 left->vpd_sn && right->vpd_sn &&
1197 !strcmp(left->vpd_sn, right->vpd_sn);
1198}
1199
1200static void efx_associate(struct efx_nic *efx)
1201{
1202 struct efx_nic *other, *next;
1203
1204 if (efx->primary == efx) {
1205 /* Adding primary function; look for secondaries */
1206
1207 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
1208 list_add_tail(&efx->node, &efx_primary_list);
1209
1210 list_for_each_entry_safe(other, next, &efx_unassociated_list,
1211 node) {
1212 if (efx_same_controller(efx, other)) {
1213 list_del(&other->node);
1214 netif_dbg(other, probe, other->net_dev,
1215 "moving to secondary list of %s %s\n",
1216 pci_name(efx->pci_dev),
1217 efx->net_dev->name);
1218 list_add_tail(&other->node,
1219 &efx->secondary_list);
1220 other->primary = efx;
1221 }
1222 }
1223 } else {
1224 /* Adding secondary function; look for primary */
1225
1226 list_for_each_entry(other, &efx_primary_list, node) {
1227 if (efx_same_controller(efx, other)) {
1228 netif_dbg(efx, probe, efx->net_dev,
1229 "adding to secondary list of %s %s\n",
1230 pci_name(other->pci_dev),
1231 other->net_dev->name);
1232 list_add_tail(&efx->node,
1233 &other->secondary_list);
1234 efx->primary = other;
1235 return;
1236 }
1237 }
1238
1239 netif_dbg(efx, probe, efx->net_dev,
1240 "adding to unassociated list\n");
1241 list_add_tail(&efx->node, &efx_unassociated_list);
1242 }
1243}
1244
1245static void efx_dissociate(struct efx_nic *efx)
1246{
1247 struct efx_nic *other, *next;
1248
1249 list_del(&efx->node);
1250 efx->primary = NULL;
1251
1252 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
1253 list_del(&other->node);
1254 netif_dbg(other, probe, other->net_dev,
1255 "moving to unassociated list\n");
1256 list_add_tail(&other->node, &efx_unassociated_list);
1257 other->primary = NULL;
1258 }
1259}
1260
Ben Hutchings8ceee662008-04-27 12:55:59 +01001261/* This configures the PCI device to enable I/O and DMA. */
1262static int efx_init_io(struct efx_nic *efx)
1263{
1264 struct pci_dev *pci_dev = efx->pci_dev;
1265 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001266 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Shradha Shah02246a72015-05-06 00:58:14 +01001267 int rc, bar;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001268
Ben Hutchings62776d02010-06-23 11:30:07 +00001269 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001270
Edward Cree03714bb2017-12-18 16:55:50 +00001271 bar = efx->type->mem_bar(efx);
Shradha Shah02246a72015-05-06 00:58:14 +01001272
Ben Hutchings8ceee662008-04-27 12:55:59 +01001273 rc = pci_enable_device(pci_dev);
1274 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001275 netif_err(efx, probe, efx->net_dev,
1276 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001277 goto fail1;
1278 }
1279
1280 pci_set_master(pci_dev);
1281
1282 /* Set the PCI DMA mask. Try all possibilities from our
1283 * genuine mask down to 32 bits, because some architectures
1284 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1285 * masks event though they reject 46 bit masks.
1286 */
1287 while (dma_mask > 0x7fffffffUL) {
Christoph Hellwig8722b8f2015-11-10 14:45:42 -08001288 rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
1289 if (rc == 0)
1290 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001291 dma_mask >>= 1;
1292 }
1293 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001294 netif_err(efx, probe, efx->net_dev,
1295 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001296 goto fail2;
1297 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001298 netif_dbg(efx, probe, efx->net_dev,
1299 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001300
Shradha Shah02246a72015-05-06 00:58:14 +01001301 efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
1302 rc = pci_request_region(pci_dev, bar, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001303 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001304 netif_err(efx, probe, efx->net_dev,
1305 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001306 rc = -EIO;
1307 goto fail3;
1308 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001309 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001310 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001311 netif_err(efx, probe, efx->net_dev,
1312 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001313 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001314 rc = -ENOMEM;
1315 goto fail4;
1316 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001317 netif_dbg(efx, probe, efx->net_dev,
1318 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001319 (unsigned long long)efx->membase_phys, mem_map_size,
1320 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001321
1322 return 0;
1323
1324 fail4:
Shradha Shah02246a72015-05-06 00:58:14 +01001325 pci_release_region(efx->pci_dev, bar);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001326 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001327 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001328 fail2:
1329 pci_disable_device(efx->pci_dev);
1330 fail1:
1331 return rc;
1332}
1333
1334static void efx_fini_io(struct efx_nic *efx)
1335{
Shradha Shah02246a72015-05-06 00:58:14 +01001336 int bar;
1337
Ben Hutchings62776d02010-06-23 11:30:07 +00001338 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001339
1340 if (efx->membase) {
1341 iounmap(efx->membase);
1342 efx->membase = NULL;
1343 }
1344
1345 if (efx->membase_phys) {
Edward Cree03714bb2017-12-18 16:55:50 +00001346 bar = efx->type->mem_bar(efx);
Shradha Shah02246a72015-05-06 00:58:14 +01001347 pci_release_region(efx->pci_dev, bar);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001348 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001349 }
1350
Daniel Pieczko6598dad2015-06-02 11:41:00 +01001351 /* Don't disable bus-mastering if VFs are assigned */
1352 if (!pci_vfs_assigned(efx->pci_dev))
1353 pci_disable_device(efx->pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001354}
1355
Edward Cree42356d92018-03-08 15:45:17 +00001356void efx_set_default_rx_indir_table(struct efx_nic *efx,
1357 struct efx_rss_context *ctx)
Jon Cooper267c0152015-05-06 00:59:38 +01001358{
1359 size_t i;
1360
Edward Cree42356d92018-03-08 15:45:17 +00001361 for (i = 0; i < ARRAY_SIZE(ctx->rx_indir_table); i++)
1362 ctx->rx_indir_table[i] =
Jon Cooper267c0152015-05-06 00:59:38 +01001363 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001364}
1365
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001366static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001367{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001368 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001369 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001370 int cpu;
1371
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001372 if (rss_cpus) {
1373 count = rss_cpus;
1374 } else {
1375 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1376 netif_warn(efx, probe, efx->net_dev,
1377 "RSS disabled due to allocation failure\n");
1378 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001379 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001380
1381 count = 0;
1382 for_each_online_cpu(cpu) {
1383 if (!cpumask_test_cpu(cpu, thread_mask)) {
1384 ++count;
1385 cpumask_or(thread_mask, thread_mask,
Bartosz Golaszewski06931e62015-05-26 15:11:28 +02001386 topology_sibling_cpumask(cpu));
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001387 }
1388 }
1389
1390 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001391 }
1392
Bert Kenward271a8b42017-04-12 17:06:52 +01001393 if (count > EFX_MAX_RX_QUEUES) {
1394 netif_cond_dbg(efx, probe, efx->net_dev, !rss_cpus, warn,
1395 "Reducing number of rx queues from %u to %u.\n",
1396 count, EFX_MAX_RX_QUEUES);
1397 count = EFX_MAX_RX_QUEUES;
1398 }
1399
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001400 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1401 * table entries that are inaccessible to VFs
1402 */
Shradha Shah7fa8d542015-05-06 00:55:13 +01001403#ifdef CONFIG_SFC_SRIOV
1404 if (efx->type->sriov_wanted) {
1405 if (efx->type->sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1406 count > efx_vf_size(efx)) {
1407 netif_warn(efx, probe, efx->net_dev,
1408 "Reducing number of RSS channels from %u to %u for "
1409 "VF support. Increase vf-msix-limit to use more "
1410 "channels on the PF.\n",
1411 count, efx_vf_size(efx));
1412 count = efx_vf_size(efx);
1413 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001414 }
Shradha Shah7fa8d542015-05-06 00:55:13 +01001415#endif
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001416
Ben Hutchings46123d02008-09-01 12:47:33 +01001417 return count;
1418}
1419
1420/* Probe the number and type of interrupts we are able to obtain, and
1421 * the resulting numbers of channels and RX queues.
1422 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001423static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001424{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001425 unsigned int extra_channels = 0;
1426 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001427 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001428
Ben Hutchings7f967c02012-02-13 23:45:02 +00001429 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1430 if (efx->extra_channel_type[i])
1431 ++extra_channels;
1432
Ben Hutchings8ceee662008-04-27 12:55:59 +01001433 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001434 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001435 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001436
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001437 n_channels = efx_wanted_parallelism(efx);
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001438 if (efx_separate_tx_channels)
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001439 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001440 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001441 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001442
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001443 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001444 xentries[i].entry = i;
Alexander Gordeev184603d2014-02-18 11:12:00 +01001445 rc = pci_enable_msix_range(efx->pci_dev,
1446 xentries, 1, n_channels);
1447 if (rc < 0) {
1448 /* Fall back to single channel MSI */
Alexander Gordeev184603d2014-02-18 11:12:00 +01001449 netif_err(efx, drv, efx->net_dev,
1450 "could not enable MSI-X\n");
Andrew Rybchenko62980cb2017-02-13 14:59:04 +00001451 if (efx->type->min_interrupt_mode >= EFX_INT_MODE_MSI)
1452 efx->interrupt_mode = EFX_INT_MODE_MSI;
1453 else
1454 return rc;
Alexander Gordeev184603d2014-02-18 11:12:00 +01001455 } else if (rc < n_channels) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001456 netif_err(efx, drv, efx->net_dev,
1457 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001458 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001459 netif_err(efx, drv, efx->net_dev,
1460 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001461 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001462 }
1463
Alexander Gordeev184603d2014-02-18 11:12:00 +01001464 if (rc > 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001465 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001466 if (n_channels > extra_channels)
1467 n_channels -= extra_channels;
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001468 if (efx_separate_tx_channels) {
1469 efx->n_tx_channels = min(max(n_channels / 2,
1470 1U),
1471 efx->max_tx_channels);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001472 efx->n_rx_channels = max(n_channels -
1473 efx->n_tx_channels,
1474 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001475 } else {
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001476 efx->n_tx_channels = min(n_channels,
1477 efx->max_tx_channels);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001478 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001479 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001480 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001481 efx_get_channel(efx, i)->irq =
1482 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001483 }
1484 }
1485
1486 /* Try single interrupt MSI */
1487 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001488 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001489 efx->n_rx_channels = 1;
1490 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001491 rc = pci_enable_msi(efx->pci_dev);
1492 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001493 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001494 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001495 netif_err(efx, drv, efx->net_dev,
1496 "could not enable MSI\n");
Andrew Rybchenko62980cb2017-02-13 14:59:04 +00001497 if (efx->type->min_interrupt_mode >= EFX_INT_MODE_LEGACY)
1498 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1499 else
1500 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001501 }
1502 }
1503
1504 /* Assume legacy interrupts */
1505 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001506 efx->n_channels = 1 + (efx_separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001507 efx->n_rx_channels = 1;
1508 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001509 efx->legacy_irq = efx->pci_dev->irq;
1510 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001511
Ben Hutchings7f967c02012-02-13 23:45:02 +00001512 /* Assign extra channels if possible */
Edward Cree2935e3c2018-01-25 17:26:06 +00001513 efx->n_extra_tx_channels = 0;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001514 j = efx->n_channels;
1515 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1516 if (!efx->extra_channel_type[i])
1517 continue;
1518 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1519 efx->n_channels <= extra_channels) {
1520 efx->extra_channel_type[i]->handle_no_channel(efx);
1521 } else {
1522 --j;
1523 efx_get_channel(efx, j)->type =
1524 efx->extra_channel_type[i];
Edward Cree2935e3c2018-01-25 17:26:06 +00001525 if (efx_channel_has_tx_queues(efx_get_channel(efx, j)))
1526 efx->n_extra_tx_channels++;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001527 }
1528 }
1529
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001530 /* RSS might be usable on VFs even if it is disabled on the PF */
Shradha Shah7fa8d542015-05-06 00:55:13 +01001531#ifdef CONFIG_SFC_SRIOV
1532 if (efx->type->sriov_wanted) {
1533 efx->rss_spread = ((efx->n_rx_channels > 1 ||
1534 !efx->type->sriov_wanted(efx)) ?
1535 efx->n_rx_channels : efx_vf_size(efx));
1536 return 0;
1537 }
1538#endif
1539 efx->rss_spread = efx->n_rx_channels;
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001540
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001541 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001542}
1543
Jon Cooper261e4d92013-04-15 18:51:54 +01001544static int efx_soft_enable_interrupts(struct efx_nic *efx)
Ben Hutchingsd8291182012-10-05 23:35:41 +01001545{
Jon Cooper261e4d92013-04-15 18:51:54 +01001546 struct efx_channel *channel, *end_channel;
1547 int rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001548
1549 BUG_ON(efx->state == STATE_DISABLED);
1550
1551 efx->irq_soft_enabled = true;
1552 smp_wmb();
1553
1554 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001555 if (!channel->type->keep_eventq) {
1556 rc = efx_init_eventq(channel);
1557 if (rc)
1558 goto fail;
1559 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001560 efx_start_eventq(channel);
1561 }
1562
1563 efx_mcdi_mode_event(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +01001564
1565 return 0;
1566fail:
1567 end_channel = channel;
1568 efx_for_each_channel(channel, efx) {
1569 if (channel == end_channel)
1570 break;
1571 efx_stop_eventq(channel);
1572 if (!channel->type->keep_eventq)
1573 efx_fini_eventq(channel);
1574 }
1575
1576 return rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001577}
1578
1579static void efx_soft_disable_interrupts(struct efx_nic *efx)
1580{
1581 struct efx_channel *channel;
1582
1583 if (efx->state == STATE_DISABLED)
1584 return;
1585
1586 efx_mcdi_mode_poll(efx);
1587
1588 efx->irq_soft_enabled = false;
1589 smp_wmb();
1590
1591 if (efx->legacy_irq)
1592 synchronize_irq(efx->legacy_irq);
1593
1594 efx_for_each_channel(channel, efx) {
1595 if (channel->irq)
1596 synchronize_irq(channel->irq);
1597
1598 efx_stop_eventq(channel);
1599 if (!channel->type->keep_eventq)
1600 efx_fini_eventq(channel);
1601 }
Ben Hutchingscade7152013-08-27 23:12:31 +01001602
1603 /* Flush the asynchronous MCDI request queue */
1604 efx_mcdi_flush_async(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01001605}
1606
Jon Cooper261e4d92013-04-15 18:51:54 +01001607static int efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001608{
Jon Cooper261e4d92013-04-15 18:51:54 +01001609 struct efx_channel *channel, *end_channel;
1610 int rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001611
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001612 BUG_ON(efx->state == STATE_DISABLED);
1613
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001614 if (efx->eeh_disabled_legacy_irq) {
1615 enable_irq(efx->legacy_irq);
1616 efx->eeh_disabled_legacy_irq = false;
1617 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001618
Ben Hutchings86094f72013-08-21 19:51:04 +01001619 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001620
1621 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001622 if (channel->type->keep_eventq) {
1623 rc = efx_init_eventq(channel);
1624 if (rc)
1625 goto fail;
1626 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001627 }
1628
Jon Cooper261e4d92013-04-15 18:51:54 +01001629 rc = efx_soft_enable_interrupts(efx);
1630 if (rc)
1631 goto fail;
1632
1633 return 0;
1634
1635fail:
1636 end_channel = channel;
1637 efx_for_each_channel(channel, efx) {
1638 if (channel == end_channel)
1639 break;
1640 if (channel->type->keep_eventq)
1641 efx_fini_eventq(channel);
1642 }
1643
1644 efx->type->irq_disable_non_ev(efx);
1645
1646 return rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001647}
1648
Ben Hutchingsd8291182012-10-05 23:35:41 +01001649static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001650{
1651 struct efx_channel *channel;
1652
Ben Hutchingsd8291182012-10-05 23:35:41 +01001653 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001654
1655 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001656 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001657 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001658 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001659
Ben Hutchings86094f72013-08-21 19:51:04 +01001660 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001661}
1662
Ben Hutchings8ceee662008-04-27 12:55:59 +01001663static void efx_remove_interrupts(struct efx_nic *efx)
1664{
1665 struct efx_channel *channel;
1666
1667 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001668 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001669 channel->irq = 0;
1670 pci_disable_msi(efx->pci_dev);
1671 pci_disable_msix(efx->pci_dev);
1672
1673 /* Remove legacy interrupt */
1674 efx->legacy_irq = 0;
1675}
1676
Ben Hutchings8831da72008-09-01 12:47:48 +01001677static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001678{
Ben Hutchings602a5322011-05-16 17:32:39 +01001679 struct efx_channel *channel;
1680 struct efx_tx_queue *tx_queue;
1681
Ben Hutchings97653432011-01-12 18:26:56 +00001682 efx->tx_channel_offset =
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001683 efx_separate_tx_channels ?
1684 efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001685
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001686 /* We need to mark which channels really have RX and TX
1687 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001688 * RX-only and TX-only channels.
1689 */
1690 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001691 if (channel->channel < efx->n_rx_channels)
1692 channel->rx_queue.core_index = channel->channel;
1693 else
1694 channel->rx_queue.core_index = -1;
1695
Ben Hutchings602a5322011-05-16 17:32:39 +01001696 efx_for_each_channel_tx_queue(tx_queue, channel)
1697 tx_queue->queue -= (efx->tx_channel_offset *
1698 EFX_TXQ_TYPES);
1699 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001700}
1701
1702static int efx_probe_nic(struct efx_nic *efx)
1703{
1704 int rc;
1705
Ben Hutchings62776d02010-06-23 11:30:07 +00001706 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001707
1708 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001709 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001710 if (rc)
1711 return rc;
1712
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001713 do {
1714 if (!efx->max_channels || !efx->max_tx_channels) {
1715 netif_err(efx, drv, efx->net_dev,
1716 "Insufficient resources to allocate"
1717 " any channels\n");
1718 rc = -ENOSPC;
1719 goto fail1;
1720 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001721
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001722 /* Determine the number of channels and queues by trying
1723 * to hook in MSI-X interrupts.
1724 */
1725 rc = efx_probe_interrupts(efx);
1726 if (rc)
1727 goto fail1;
Daniel Pieczko52ad7622014-04-01 13:10:34 +01001728
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001729 efx_set_channels(efx);
1730
1731 /* dimension_resources can fail with EAGAIN */
1732 rc = efx->type->dimension_resources(efx);
1733 if (rc != 0 && rc != -EAGAIN)
1734 goto fail2;
1735
1736 if (rc == -EAGAIN)
1737 /* try again with new max_channels */
1738 efx_remove_interrupts(efx);
1739
1740 } while (rc == -EAGAIN);
Ben Hutchings28e47c42012-02-15 01:58:49 +00001741
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001742 if (efx->n_channels > 1)
Edward Cree42356d92018-03-08 15:45:17 +00001743 netdev_rss_key_fill(efx->rss_context.rx_hash_key,
1744 sizeof(efx->rss_context.rx_hash_key));
1745 efx_set_default_rx_indir_table(efx, &efx->rss_context);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001746
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001747 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1748 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001749
1750 /* Initialise the interrupt moderation settings */
Bert Kenward539de7c2016-08-11 13:02:09 +01001751 efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
Ben Hutchings9e393b32011-09-05 07:43:04 +00001752 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1753 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001754
1755 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001756
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001757fail2:
1758 efx_remove_interrupts(efx);
1759fail1:
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001760 efx->type->remove(efx);
1761 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001762}
1763
1764static void efx_remove_nic(struct efx_nic *efx)
1765{
Ben Hutchings62776d02010-06-23 11:30:07 +00001766 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001767
1768 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001769 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001770}
1771
Ben Hutchingsadd72472012-11-08 01:46:53 +00001772static int efx_probe_filters(struct efx_nic *efx)
1773{
1774 int rc;
1775
1776 spin_lock_init(&efx->filter_lock);
Edward Cree0d322412015-05-20 11:10:03 +01001777 init_rwsem(&efx->filter_sem);
Martin Habetsd2489532016-06-15 17:48:49 +01001778 mutex_lock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01001779 down_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001780 rc = efx->type->filter_table_probe(efx);
1781 if (rc)
Edward Cree0d322412015-05-20 11:10:03 +01001782 goto out_unlock;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001783
1784#ifdef CONFIG_RFS_ACCEL
1785 if (efx->type->offload_features & NETIF_F_NTUPLE) {
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001786 struct efx_channel *channel;
1787 int i, success = 1;
1788
1789 efx_for_each_channel(channel, efx) {
1790 channel->rps_flow_id =
1791 kcalloc(efx->type->max_rx_ip_filters,
1792 sizeof(*channel->rps_flow_id),
1793 GFP_KERNEL);
1794 if (!channel->rps_flow_id)
1795 success = 0;
1796 else
1797 for (i = 0;
1798 i < efx->type->max_rx_ip_filters;
1799 ++i)
1800 channel->rps_flow_id[i] =
1801 RPS_FLOW_ID_INVALID;
1802 }
1803
1804 if (!success) {
1805 efx_for_each_channel(channel, efx)
1806 kfree(channel->rps_flow_id);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001807 efx->type->filter_table_remove(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001808 rc = -ENOMEM;
1809 goto out_unlock;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001810 }
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001811
1812 efx->rps_expire_index = efx->rps_expire_channel = 0;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001813 }
1814#endif
Edward Cree0d322412015-05-20 11:10:03 +01001815out_unlock:
1816 up_write(&efx->filter_sem);
Martin Habetsd2489532016-06-15 17:48:49 +01001817 mutex_unlock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01001818 return rc;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001819}
1820
1821static void efx_remove_filters(struct efx_nic *efx)
1822{
1823#ifdef CONFIG_RFS_ACCEL
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001824 struct efx_channel *channel;
1825
1826 efx_for_each_channel(channel, efx)
1827 kfree(channel->rps_flow_id);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001828#endif
Edward Cree0d322412015-05-20 11:10:03 +01001829 down_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001830 efx->type->filter_table_remove(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001831 up_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001832}
1833
1834static void efx_restore_filters(struct efx_nic *efx)
1835{
Edward Cree0d322412015-05-20 11:10:03 +01001836 down_read(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001837 efx->type->filter_table_restore(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001838 up_read(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001839}
1840
Ben Hutchings8ceee662008-04-27 12:55:59 +01001841/**************************************************************************
1842 *
1843 * NIC startup/shutdown
1844 *
1845 *************************************************************************/
1846
1847static int efx_probe_all(struct efx_nic *efx)
1848{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001849 int rc;
1850
Ben Hutchings8ceee662008-04-27 12:55:59 +01001851 rc = efx_probe_nic(efx);
1852 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001853 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001854 goto fail1;
1855 }
1856
Ben Hutchings8ceee662008-04-27 12:55:59 +01001857 rc = efx_probe_port(efx);
1858 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001859 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001860 goto fail2;
1861 }
1862
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001863 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1864 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1865 rc = -EINVAL;
1866 goto fail3;
1867 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001868 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001869
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001870#ifdef CONFIG_SFC_SRIOV
1871 rc = efx->type->vswitching_probe(efx);
1872 if (rc) /* not fatal; the PF will still work fine */
1873 netif_warn(efx, probe, efx->net_dev,
1874 "failed to setup vswitching rc=%d;"
1875 " VFs may not function\n", rc);
1876#endif
1877
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001878 rc = efx_probe_filters(efx);
1879 if (rc) {
1880 netif_err(efx, probe, efx->net_dev,
1881 "failed to create filter tables\n");
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001882 goto fail4;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001883 }
1884
Ben Hutchings7f967c02012-02-13 23:45:02 +00001885 rc = efx_probe_channels(efx);
1886 if (rc)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001887 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001888
Ben Hutchings8ceee662008-04-27 12:55:59 +01001889 return 0;
1890
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001891 fail5:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001892 efx_remove_filters(efx);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001893 fail4:
1894#ifdef CONFIG_SFC_SRIOV
1895 efx->type->vswitching_remove(efx);
1896#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01001897 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001898 efx_remove_port(efx);
1899 fail2:
1900 efx_remove_nic(efx);
1901 fail1:
1902 return rc;
1903}
1904
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001905/* If the interface is supposed to be running but is not, start
1906 * the hardware and software data path, regular activity for the port
1907 * (MAC statistics, link polling, etc.) and schedule the port to be
1908 * reconfigured. Interrupts must already be enabled. This function
1909 * is safe to call multiple times, so long as the NIC is not disabled.
1910 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001911 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001912static void efx_start_all(struct efx_nic *efx)
1913{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001914 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001915 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001916
1917 /* Check that it is appropriate to restart the interface. All
1918 * of these flags are safe to read under just the rtnl lock */
Edward Creee2835462014-04-16 19:27:48 +01001919 if (efx->port_enabled || !netif_running(efx->net_dev) ||
1920 efx->reset_pending)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001921 return;
1922
Ben Hutchings8ceee662008-04-27 12:55:59 +01001923 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001924 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001925
Alexandre Rames626950d2013-01-14 17:20:22 +00001926 /* Start the hardware monitor if there is one */
1927 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001928 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1929 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001930
Edward Cree5a6681e2016-11-28 18:55:34 +00001931 /* Link state detection is normally event-driven; we have
Alexandre Rames626950d2013-01-14 17:20:22 +00001932 * to poll now because we could have missed a change
1933 */
Edward Cree5a6681e2016-11-28 18:55:34 +00001934 mutex_lock(&efx->mac_lock);
1935 if (efx->phy_op->poll(efx))
1936 efx_link_status_changed(efx);
1937 mutex_unlock(&efx->mac_lock);
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001938
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001939 efx->type->start_stats(efx);
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001940 efx->type->pull_stats(efx);
1941 spin_lock_bh(&efx->stats_lock);
1942 efx->type->update_stats(efx, NULL, NULL);
1943 spin_unlock_bh(&efx->stats_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001944}
1945
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001946/* Quiesce the hardware and software data path, and regular activity
1947 * for the port without bringing the link down. Safe to call multiple
1948 * times with the NIC in almost any state, but interrupts should be
1949 * enabled. Requires the RTNL lock.
1950 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001951static void efx_stop_all(struct efx_nic *efx)
1952{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001953 EFX_ASSERT_RESET_SERIALISED(efx);
1954
1955 /* port_enabled can be read safely under the rtnl lock */
1956 if (!efx->port_enabled)
1957 return;
1958
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001959 /* update stats before we go down so we can accurately count
1960 * rx_nodesc_drops
1961 */
1962 efx->type->pull_stats(efx);
1963 spin_lock_bh(&efx->stats_lock);
1964 efx->type->update_stats(efx, NULL, NULL);
1965 spin_unlock_bh(&efx->stats_lock);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001966 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001967 efx_stop_port(efx);
1968
Ben Hutchings29c69a42013-01-28 19:01:06 +00001969 /* Stop the kernel transmit interface. This is only valid if
1970 * the device is stopped or detached; otherwise the watchdog
1971 * may fire immediately.
1972 */
1973 WARN_ON(netif_running(efx->net_dev) &&
1974 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001975 netif_tx_disable(efx->net_dev);
1976
1977 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001978}
1979
1980static void efx_remove_all(struct efx_nic *efx)
1981{
Ben Hutchings46426102010-09-10 06:42:33 +00001982 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001983 efx_remove_filters(efx);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001984#ifdef CONFIG_SFC_SRIOV
1985 efx->type->vswitching_remove(efx);
1986#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01001987 efx_remove_port(efx);
1988 efx_remove_nic(efx);
1989}
1990
Ben Hutchings8ceee662008-04-27 12:55:59 +01001991/**************************************************************************
1992 *
1993 * Interrupt moderation
1994 *
1995 **************************************************************************/
Bert Kenward539de7c2016-08-11 13:02:09 +01001996unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001997{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001998 if (usecs == 0)
1999 return 0;
Bert Kenward539de7c2016-08-11 13:02:09 +01002000 if (usecs * 1000 < efx->timer_quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00002001 return 1; /* never round down to 0 */
Bert Kenward539de7c2016-08-11 13:02:09 +01002002 return usecs * 1000 / efx->timer_quantum_ns;
2003}
2004
2005unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
2006{
2007 /* We must round up when converting ticks to microseconds
2008 * because we round down when converting the other way.
2009 */
2010 return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00002011}
2012
Ben Hutchings8ceee662008-04-27 12:55:59 +01002013/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00002014int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
2015 unsigned int rx_usecs, bool rx_adaptive,
2016 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002017{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002018 struct efx_channel *channel;
Bert Kenwardd95e3292016-08-11 13:02:36 +01002019 unsigned int timer_max_us;
2020
Ben Hutchings8ceee662008-04-27 12:55:59 +01002021 EFX_ASSERT_RESET_SERIALISED(efx);
2022
Bert Kenwardd95e3292016-08-11 13:02:36 +01002023 timer_max_us = efx->timer_max_ns / 1000;
2024
2025 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
Ben Hutchings9e393b32011-09-05 07:43:04 +00002026 return -EINVAL;
2027
Bert Kenward539de7c2016-08-11 13:02:09 +01002028 if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
Ben Hutchings9e393b32011-09-05 07:43:04 +00002029 !rx_may_override_tx) {
2030 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
2031 "RX and TX IRQ moderation must be equal\n");
2032 return -EINVAL;
2033 }
2034
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00002035 efx->irq_rx_adaptive = rx_adaptive;
Bert Kenward539de7c2016-08-11 13:02:09 +01002036 efx->irq_rx_moderation_us = rx_usecs;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002037 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00002038 if (efx_channel_has_rx_queue(channel))
Bert Kenward539de7c2016-08-11 13:02:09 +01002039 channel->irq_moderation_us = rx_usecs;
Ben Hutchings525da902011-02-07 23:04:38 +00002040 else if (efx_channel_has_tx_queues(channel))
Bert Kenward539de7c2016-08-11 13:02:09 +01002041 channel->irq_moderation_us = tx_usecs;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002042 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00002043
2044 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002045}
2046
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002047void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
2048 unsigned int *rx_usecs, bool *rx_adaptive)
2049{
2050 *rx_adaptive = efx->irq_rx_adaptive;
Bert Kenward539de7c2016-08-11 13:02:09 +01002051 *rx_usecs = efx->irq_rx_moderation_us;
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002052
2053 /* If channels are shared between RX and TX, so is IRQ
2054 * moderation. Otherwise, IRQ moderation is the same for all
2055 * TX channels and is not adaptive.
2056 */
Bert Kenward539de7c2016-08-11 13:02:09 +01002057 if (efx->tx_channel_offset == 0) {
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002058 *tx_usecs = *rx_usecs;
Bert Kenward539de7c2016-08-11 13:02:09 +01002059 } else {
2060 struct efx_channel *tx_channel;
2061
2062 tx_channel = efx->channel[efx->tx_channel_offset];
2063 *tx_usecs = tx_channel->irq_moderation_us;
2064 }
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002065}
2066
Ben Hutchings8ceee662008-04-27 12:55:59 +01002067/**************************************************************************
2068 *
2069 * Hardware monitor
2070 *
2071 **************************************************************************/
2072
Ben Hutchingse254c272010-09-20 08:44:10 +00002073/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002074static void efx_monitor(struct work_struct *data)
2075{
2076 struct efx_nic *efx = container_of(data, struct efx_nic,
2077 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002078
Ben Hutchings62776d02010-06-23 11:30:07 +00002079 netif_vdbg(efx, timer, efx->net_dev,
2080 "hardware monitor executing on CPU %d\n",
2081 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002082 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002083
Ben Hutchings8ceee662008-04-27 12:55:59 +01002084 /* If the mac_lock is already held then it is likely a port
2085 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00002086 * most of the work of monitor() anyway. */
2087 if (mutex_trylock(&efx->mac_lock)) {
2088 if (efx->port_enabled)
2089 efx->type->monitor(efx);
2090 mutex_unlock(&efx->mac_lock);
2091 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002092
Ben Hutchings8ceee662008-04-27 12:55:59 +01002093 queue_delayed_work(efx->workqueue, &efx->monitor_work,
2094 efx_monitor_interval);
2095}
2096
2097/**************************************************************************
2098 *
2099 * ioctls
2100 *
2101 *************************************************************************/
2102
2103/* Net device ioctl
2104 * Context: process, rtnl_lock() held.
2105 */
2106static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
2107{
Ben Hutchings767e4682008-09-01 12:43:14 +01002108 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00002109 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002110
Stuart Hodgson7c236c42012-09-03 11:09:36 +01002111 if (cmd == SIOCSHWTSTAMP)
Ben Hutchings433dc9b2013-11-14 01:26:21 +00002112 return efx_ptp_set_ts_config(efx, ifr);
2113 if (cmd == SIOCGHWTSTAMP)
2114 return efx_ptp_get_ts_config(efx, ifr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01002115
Ben Hutchings68e7f452009-04-29 08:05:08 +00002116 /* Convert phy_id from older PRTAD/DEVAD format */
2117 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
2118 (data->phy_id & 0xfc00) == 0x0400)
2119 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
2120
2121 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002122}
2123
2124/**************************************************************************
2125 *
2126 * NAPI interface
2127 *
2128 **************************************************************************/
2129
Ben Hutchings7f967c02012-02-13 23:45:02 +00002130static void efx_init_napi_channel(struct efx_channel *channel)
2131{
2132 struct efx_nic *efx = channel->efx;
2133
2134 channel->napi_dev = efx->net_dev;
2135 netif_napi_add(channel->napi_dev, &channel->napi_str,
2136 efx_poll, napi_weight);
2137}
2138
Ben Hutchingse8f14992010-12-07 19:47:34 +00002139static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002140{
2141 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002142
Ben Hutchings7f967c02012-02-13 23:45:02 +00002143 efx_for_each_channel(channel, efx)
2144 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00002145}
2146
2147static void efx_fini_napi_channel(struct efx_channel *channel)
2148{
Eric Dumazet973334a2016-11-16 06:01:47 -08002149 if (channel->napi_dev)
Ben Hutchingse8f14992010-12-07 19:47:34 +00002150 netif_napi_del(&channel->napi_str);
Eric Dumazet973334a2016-11-16 06:01:47 -08002151
Ben Hutchingse8f14992010-12-07 19:47:34 +00002152 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002153}
2154
2155static void efx_fini_napi(struct efx_nic *efx)
2156{
2157 struct efx_channel *channel;
2158
Ben Hutchingse8f14992010-12-07 19:47:34 +00002159 efx_for_each_channel(channel, efx)
2160 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002161}
2162
2163/**************************************************************************
2164 *
2165 * Kernel netpoll interface
2166 *
2167 *************************************************************************/
2168
2169#ifdef CONFIG_NET_POLL_CONTROLLER
2170
2171/* Although in the common case interrupts will be disabled, this is not
2172 * guaranteed. However, all our work happens inside the NAPI callback,
2173 * so no locking is required.
2174 */
2175static void efx_netpoll(struct net_device *net_dev)
2176{
Ben Hutchings767e4682008-09-01 12:43:14 +01002177 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002178 struct efx_channel *channel;
2179
Ben Hutchings64ee3122008-09-01 12:47:38 +01002180 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002181 efx_schedule_channel(channel);
2182}
2183
2184#endif
2185
2186/**************************************************************************
2187 *
2188 * Kernel net device interface
2189 *
2190 *************************************************************************/
2191
2192/* Context: process, rtnl_lock() held. */
Shradha Shahe340be92015-05-20 11:11:03 +01002193int efx_net_open(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002194{
Ben Hutchings767e4682008-09-01 12:43:14 +01002195 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002196 int rc;
2197
Ben Hutchings62776d02010-06-23 11:30:07 +00002198 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
2199 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01002200
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002201 rc = efx_check_disabled(efx);
2202 if (rc)
2203 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01002204 if (efx->phy_mode & PHY_MODE_SPECIAL)
2205 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002206 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
2207 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01002208
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00002209 /* Notify the kernel of the link state polled during driver load,
2210 * before the monitor starts running */
2211 efx_link_status_changed(efx);
2212
Ben Hutchings8ceee662008-04-27 12:55:59 +01002213 efx_start_all(efx);
Peter Dunning9c568fd2017-02-17 15:50:43 +00002214 if (efx->state == STATE_DISABLED || efx->reset_pending)
2215 netif_device_detach(efx->net_dev);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002216 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002217 return 0;
2218}
2219
2220/* Context: process, rtnl_lock() held.
2221 * Note that the kernel will ignore our return code; this method
2222 * should really be a void.
2223 */
Shradha Shahe340be92015-05-20 11:11:03 +01002224int efx_net_stop(struct net_device *net_dev)
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 Hutchings62776d02010-06-23 11:30:07 +00002228 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
2229 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01002230
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002231 /* Stop the device and flush all the channels */
2232 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002233
2234 return 0;
2235}
2236
Ben Hutchings5b9e2072008-05-16 21:18:14 +01002237/* Context: process, dev_base_lock or RTNL held, non-blocking. */
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002238static void efx_net_stats(struct net_device *net_dev,
2239 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002240{
Ben Hutchings767e4682008-09-01 12:43:14 +01002241 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002242
Ben Hutchings55edc6e2009-11-25 16:11:35 +00002243 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00002244 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01002245 spin_unlock_bh(&efx->stats_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002246}
2247
2248/* Context: netif_tx_lock held, BHs disabled. */
2249static void efx_watchdog(struct net_device *net_dev)
2250{
Ben Hutchings767e4682008-09-01 12:43:14 +01002251 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002252
Ben Hutchings62776d02010-06-23 11:30:07 +00002253 netif_err(efx, tx_err, efx->net_dev,
2254 "TX stuck with port_enabled=%d: resetting channels\n",
2255 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002256
Ben Hutchings739bb23d2008-11-04 20:35:36 +00002257 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002258}
2259
2260
2261/* Context: process, rtnl_lock() held. */
2262static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2263{
Ben Hutchings767e4682008-09-01 12:43:14 +01002264 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002265 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002266
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002267 rc = efx_check_disabled(efx);
2268 if (rc)
2269 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002270
Ben Hutchings62776d02010-06-23 11:30:07 +00002271 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002272
Ben Hutchings29c69a42013-01-28 19:01:06 +00002273 efx_device_detach_sync(efx);
2274 efx_stop_all(efx);
2275
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002276 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002277 net_dev->mtu = new_mtu;
Edward Cree0d322412015-05-20 11:10:03 +01002278 efx_mac_reconfigure(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002279 mutex_unlock(&efx->mac_lock);
2280
Ben Hutchings8ceee662008-04-27 12:55:59 +01002281 efx_start_all(efx);
Peter Dunning9c568fd2017-02-17 15:50:43 +00002282 efx_device_attach_if_not_resetting(efx);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002283 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002284}
2285
2286static int efx_set_mac_address(struct net_device *net_dev, void *data)
2287{
Ben Hutchings767e4682008-09-01 12:43:14 +01002288 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002289 struct sockaddr *addr = data;
Ben Hutchingse0b3ae32014-02-12 18:59:54 +00002290 u8 *new_addr = addr->sa_data;
Shradha Shahcfc77c22015-05-20 11:09:30 +01002291 u8 old_addr[6];
2292 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002293
Ben Hutchings8ceee662008-04-27 12:55:59 +01002294 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002295 netif_err(efx, drv, efx->net_dev,
2296 "invalid ethernet MAC address requested: %pM\n",
2297 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002298 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002299 }
2300
Shradha Shahcfc77c22015-05-20 11:09:30 +01002301 /* save old address */
2302 ether_addr_copy(old_addr, net_dev->dev_addr);
Edward Creecd84ff42014-03-07 18:27:41 +00002303 ether_addr_copy(net_dev->dev_addr, new_addr);
Shradha Shah910c8782015-05-20 11:12:48 +01002304 if (efx->type->set_mac_address) {
2305 rc = efx->type->set_mac_address(efx);
Shradha Shahcfc77c22015-05-20 11:09:30 +01002306 if (rc) {
2307 ether_addr_copy(net_dev->dev_addr, old_addr);
2308 return rc;
2309 }
2310 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002311
2312 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002313 mutex_lock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01002314 efx_mac_reconfigure(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002315 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002316
2317 return 0;
2318}
2319
Ben Hutchingsa816f752008-09-01 12:49:12 +01002320/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002321static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002322{
Ben Hutchings767e4682008-09-01 12:43:14 +01002323 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002324
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002325 if (efx->port_enabled)
2326 queue_work(efx->workqueue, &efx->mac_work);
2327 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002328}
2329
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002330static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002331{
2332 struct efx_nic *efx = netdev_priv(net_dev);
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002333 int rc;
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002334
2335 /* If disabling RX n-tuple filtering, clear existing filters */
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002336 if (net_dev->features & ~data & NETIF_F_NTUPLE) {
2337 rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2338 if (rc)
2339 return rc;
2340 }
2341
Edward Cree69787292017-10-31 14:29:47 +00002342 /* If Rx VLAN filter is changed, update filters via mac_reconfigure.
2343 * If rx-fcs is changed, mac_reconfigure updates that too.
2344 */
2345 if ((net_dev->features ^ data) & (NETIF_F_HW_VLAN_CTAG_FILTER |
2346 NETIF_F_RXFCS)) {
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002347 /* efx_set_rx_mode() will schedule MAC work to update filters
2348 * when a new features are finally set in net_dev.
2349 */
2350 efx_set_rx_mode(net_dev);
2351 }
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002352
2353 return 0;
2354}
2355
Wei Yongjunb40296f2017-01-11 16:16:12 +00002356static int efx_get_phys_port_id(struct net_device *net_dev,
2357 struct netdev_phys_item_id *ppid)
Bert Kenward08a7b29b2017-01-10 16:23:33 +00002358{
2359 struct efx_nic *efx = netdev_priv(net_dev);
2360
2361 if (efx->type->get_phys_port_id)
2362 return efx->type->get_phys_port_id(efx, ppid);
2363 else
2364 return -EOPNOTSUPP;
2365}
2366
Bert Kenwardac019f02017-01-10 16:23:56 +00002367static int efx_get_phys_port_name(struct net_device *net_dev,
2368 char *name, size_t len)
2369{
2370 struct efx_nic *efx = netdev_priv(net_dev);
2371
2372 if (snprintf(name, len, "p%u", efx->port_num) >= len)
2373 return -EINVAL;
2374 return 0;
2375}
2376
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002377static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
2378{
2379 struct efx_nic *efx = netdev_priv(net_dev);
2380
2381 if (efx->type->vlan_rx_add_vid)
2382 return efx->type->vlan_rx_add_vid(efx, proto, vid);
2383 else
2384 return -EOPNOTSUPP;
2385}
2386
2387static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
2388{
2389 struct efx_nic *efx = netdev_priv(net_dev);
2390
2391 if (efx->type->vlan_rx_kill_vid)
2392 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
2393 else
2394 return -EOPNOTSUPP;
2395}
2396
Jon Coopere5fbd972017-02-08 16:52:10 +00002397static int efx_udp_tunnel_type_map(enum udp_parsable_tunnel_type in)
2398{
2399 switch (in) {
2400 case UDP_TUNNEL_TYPE_VXLAN:
2401 return TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
2402 case UDP_TUNNEL_TYPE_GENEVE:
2403 return TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
2404 default:
2405 return -1;
2406 }
2407}
2408
2409static void efx_udp_tunnel_add(struct net_device *dev, struct udp_tunnel_info *ti)
2410{
2411 struct efx_nic *efx = netdev_priv(dev);
2412 struct efx_udp_tunnel tnl;
2413 int efx_tunnel_type;
2414
2415 efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
2416 if (efx_tunnel_type < 0)
2417 return;
2418
2419 tnl.type = (u16)efx_tunnel_type;
2420 tnl.port = ti->port;
2421
2422 if (efx->type->udp_tnl_add_port)
2423 (void)efx->type->udp_tnl_add_port(efx, tnl);
2424}
2425
2426static void efx_udp_tunnel_del(struct net_device *dev, struct udp_tunnel_info *ti)
2427{
2428 struct efx_nic *efx = netdev_priv(dev);
2429 struct efx_udp_tunnel tnl;
2430 int efx_tunnel_type;
2431
2432 efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
2433 if (efx_tunnel_type < 0)
2434 return;
2435
2436 tnl.type = (u16)efx_tunnel_type;
2437 tnl.port = ti->port;
2438
Dan Carpenterc04ca612017-03-22 12:10:02 +03002439 if (efx->type->udp_tnl_del_port)
Jon Coopere5fbd972017-02-08 16:52:10 +00002440 (void)efx->type->udp_tnl_del_port(efx, tnl);
2441}
2442
Shradha Shah7fa8d542015-05-06 00:55:13 +01002443static const struct net_device_ops efx_netdev_ops = {
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002444 .ndo_open = efx_net_open,
2445 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002446 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002447 .ndo_tx_timeout = efx_watchdog,
2448 .ndo_start_xmit = efx_hard_start_xmit,
2449 .ndo_validate_addr = eth_validate_addr,
2450 .ndo_do_ioctl = efx_ioctl,
2451 .ndo_change_mtu = efx_change_mtu,
2452 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002453 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002454 .ndo_set_features = efx_set_features,
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002455 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
2456 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002457#ifdef CONFIG_SFC_SRIOV
Shradha Shah7fa8d542015-05-06 00:55:13 +01002458 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2459 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2460 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2461 .ndo_get_vf_config = efx_sriov_get_vf_config,
Edward Cree4392dc62015-05-20 11:12:13 +01002462 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002463#endif
Bert Kenward08a7b29b2017-01-10 16:23:33 +00002464 .ndo_get_phys_port_id = efx_get_phys_port_id,
Bert Kenwardac019f02017-01-10 16:23:56 +00002465 .ndo_get_phys_port_name = efx_get_phys_port_name,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002466#ifdef CONFIG_NET_POLL_CONTROLLER
2467 .ndo_poll_controller = efx_netpoll,
2468#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002469 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002470#ifdef CONFIG_RFS_ACCEL
2471 .ndo_rx_flow_steer = efx_filter_rfs,
2472#endif
Jon Coopere5fbd972017-02-08 16:52:10 +00002473 .ndo_udp_tunnel_add = efx_udp_tunnel_add,
2474 .ndo_udp_tunnel_del = efx_udp_tunnel_del,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002475};
2476
Ben Hutchings7dde5962008-12-12 22:09:38 -08002477static void efx_update_name(struct efx_nic *efx)
2478{
2479 strcpy(efx->name, efx->net_dev->name);
2480 efx_mtd_rename(efx);
2481 efx_set_channel_names(efx);
2482}
2483
Ben Hutchings8ceee662008-04-27 12:55:59 +01002484static int efx_netdev_event(struct notifier_block *this,
2485 unsigned long event, void *ptr)
2486{
Jiri Pirko351638e2013-05-28 01:30:21 +00002487 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002488
Shradha Shah7fa8d542015-05-06 00:55:13 +01002489 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
Ben Hutchings7dde5962008-12-12 22:09:38 -08002490 event == NETDEV_CHANGENAME)
2491 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002492
2493 return NOTIFY_DONE;
2494}
2495
2496static struct notifier_block efx_netdev_notifier = {
2497 .notifier_call = efx_netdev_event,
2498};
2499
Ben Hutchings06d5e192008-12-12 21:47:23 -08002500static ssize_t
2501show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2502{
2503 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2504 return sprintf(buf, "%d\n", efx->phy_type);
2505}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002506static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002507
Edward Creee7fef9b2015-05-27 13:14:01 +01002508#ifdef CONFIG_SFC_MCDI_LOGGING
2509static ssize_t show_mcdi_log(struct device *dev, struct device_attribute *attr,
2510 char *buf)
2511{
2512 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2513 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2514
2515 return scnprintf(buf, PAGE_SIZE, "%d\n", mcdi->logging_enabled);
2516}
2517static ssize_t set_mcdi_log(struct device *dev, struct device_attribute *attr,
2518 const char *buf, size_t count)
2519{
2520 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2521 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2522 bool enable = count > 0 && *buf != '0';
2523
2524 mcdi->logging_enabled = enable;
2525 return count;
2526}
2527static DEVICE_ATTR(mcdi_logging, 0644, show_mcdi_log, set_mcdi_log);
2528#endif
2529
Ben Hutchings8ceee662008-04-27 12:55:59 +01002530static int efx_register_netdev(struct efx_nic *efx)
2531{
2532 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002533 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002534 int rc;
2535
2536 net_dev->watchdog_timeo = 5 * HZ;
2537 net_dev->irq = efx->pci_dev->irq;
Shradha Shah7fa8d542015-05-06 00:55:13 +01002538 net_dev->netdev_ops = &efx_netdev_ops;
2539 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
Ben Hutchings8127d662013-08-29 19:19:29 +01002540 net_dev->priv_flags |= IFF_UNICAST_FLT;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002541 net_dev->ethtool_ops = &efx_ethtool_ops;
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002542 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Bert Kenwardcd94e512016-10-18 17:47:45 +01002543 net_dev->min_mtu = EFX_MIN_MTU;
2544 net_dev->max_mtu = EFX_MAX_MTU;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002545
Ben Hutchings7dde5962008-12-12 22:09:38 -08002546 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002547
Ben Hutchings7153f622012-07-27 20:50:52 +01002548 /* Enable resets to be scheduled and check whether any were
2549 * already requested. If so, the NIC is probably hosed so we
2550 * abort.
2551 */
2552 efx->state = STATE_READY;
2553 smp_mb(); /* ensure we change state before checking reset_pending */
2554 if (efx->reset_pending) {
2555 netif_err(efx, probe, efx->net_dev,
2556 "aborting probe due to scheduled reset\n");
2557 rc = -EIO;
2558 goto fail_locked;
2559 }
2560
Ben Hutchingsaed06282009-08-26 08:16:27 +00002561 rc = dev_alloc_name(net_dev, net_dev->name);
2562 if (rc < 0)
2563 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002564 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002565
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002566 /* Always start with carrier off; PHY events will detect the link */
2567 netif_carrier_off(net_dev);
2568
Ben Hutchingsaed06282009-08-26 08:16:27 +00002569 rc = register_netdevice(net_dev);
2570 if (rc)
2571 goto fail_locked;
2572
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002573 efx_for_each_channel(channel, efx) {
2574 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002575 efx_for_each_channel_tx_queue(tx_queue, channel)
2576 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002577 }
2578
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002579 efx_associate(efx);
2580
Ben Hutchings7dde5962008-12-12 22:09:38 -08002581 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002582
Ben Hutchings06d5e192008-12-12 21:47:23 -08002583 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2584 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002585 netif_err(efx, drv, efx->net_dev,
2586 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002587 goto fail_registered;
2588 }
Edward Creee7fef9b2015-05-27 13:14:01 +01002589#ifdef CONFIG_SFC_MCDI_LOGGING
2590 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2591 if (rc) {
2592 netif_err(efx, drv, efx->net_dev,
2593 "failed to init net dev attributes\n");
2594 goto fail_attr_mcdi_logging;
2595 }
2596#endif
Ben Hutchings06d5e192008-12-12 21:47:23 -08002597
Ben Hutchings8ceee662008-04-27 12:55:59 +01002598 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002599
Edward Creee7fef9b2015-05-27 13:14:01 +01002600#ifdef CONFIG_SFC_MCDI_LOGGING
2601fail_attr_mcdi_logging:
2602 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2603#endif
Ben Hutchings7153f622012-07-27 20:50:52 +01002604fail_registered:
2605 rtnl_lock();
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002606 efx_dissociate(efx);
Ben Hutchings7153f622012-07-27 20:50:52 +01002607 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002608fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002609 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002610 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002611 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002612 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002613}
2614
2615static void efx_unregister_netdev(struct efx_nic *efx)
2616{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002617 if (!efx->net_dev)
2618 return;
2619
Ben Hutchings767e4682008-09-01 12:43:14 +01002620 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002621
Edward Creee7fef9b2015-05-27 13:14:01 +01002622 if (efx_dev_registered(efx)) {
2623 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2624#ifdef CONFIG_SFC_MCDI_LOGGING
2625 device_remove_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2626#endif
2627 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2628 unregister_netdev(efx->net_dev);
2629 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002630}
2631
2632/**************************************************************************
2633 *
2634 * Device reset and suspend
2635 *
2636 **************************************************************************/
2637
Ben Hutchings2467ca42008-09-01 12:48:50 +01002638/* Tears down the entire software state and most of the hardware state
2639 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002640void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002641{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002642 EFX_ASSERT_RESET_SERIALISED(efx);
2643
Edward Creee2835462014-04-16 19:27:48 +01002644 if (method == RESET_TYPE_MCDI_TIMEOUT)
2645 efx->type->prepare_flr(efx);
2646
Ben Hutchings2467ca42008-09-01 12:48:50 +01002647 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002648 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002649
2650 mutex_lock(&efx->mac_lock);
Jon Cooper087e9022015-05-20 11:11:35 +01002651 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2652 method != RESET_TYPE_DATAPATH)
Steve Hodgson4b988282009-01-29 17:50:51 +00002653 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002654 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002655}
2656
Ben Hutchings2467ca42008-09-01 12:48:50 +01002657/* This function will always ensure that the locks acquired in
2658 * efx_reset_down() are released. A failure return code indicates
2659 * that we were unable to reinitialise the hardware, and the
2660 * driver should be disabled. If ok is false, then the rx and tx
2661 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002662int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002663{
2664 int rc;
2665
Ben Hutchings2467ca42008-09-01 12:48:50 +01002666 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002667
Edward Creee2835462014-04-16 19:27:48 +01002668 if (method == RESET_TYPE_MCDI_TIMEOUT)
2669 efx->type->finish_flr(efx);
2670
2671 /* Ensure that SRAM is initialised even if we're disabling the device */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002672 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002673 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002674 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002675 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002676 }
2677
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002678 if (!ok)
2679 goto fail;
2680
Jon Cooper087e9022015-05-20 11:11:35 +01002681 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2682 method != RESET_TYPE_DATAPATH) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002683 rc = efx->phy_op->init(efx);
2684 if (rc)
2685 goto fail;
Edward Cree267d9d72015-05-06 00:59:18 +01002686 rc = efx->phy_op->reconfigure(efx);
2687 if (rc && rc != -EPERM)
Ben Hutchings62776d02010-06-23 11:30:07 +00002688 netif_err(efx, drv, efx->net_dev,
2689 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002690 }
2691
Jon Cooper261e4d92013-04-15 18:51:54 +01002692 rc = efx_enable_interrupts(efx);
2693 if (rc)
2694 goto fail;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01002695
2696#ifdef CONFIG_SFC_SRIOV
2697 rc = efx->type->vswitching_restore(efx);
2698 if (rc) /* not fatal; the PF will still work fine */
2699 netif_warn(efx, probe, efx->net_dev,
2700 "failed to restore vswitching rc=%d;"
2701 " VFs may not function\n", rc);
2702#endif
2703
Edward Cree42356d92018-03-08 15:45:17 +00002704 if (efx->type->rx_restore_rss_contexts)
2705 efx->type->rx_restore_rss_contexts(efx);
Edward Cree0d322412015-05-20 11:10:03 +01002706 down_read(&efx->filter_sem);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002707 efx_restore_filters(efx);
Edward Cree0d322412015-05-20 11:10:03 +01002708 up_read(&efx->filter_sem);
Shradha Shah7fa8d542015-05-06 00:55:13 +01002709 if (efx->type->sriov_reset)
2710 efx->type->sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002711
2712 mutex_unlock(&efx->mac_lock);
2713
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002714 efx_start_all(efx);
2715
Jon Coopere5fbd972017-02-08 16:52:10 +00002716 if (efx->type->udp_tnl_push_ports)
2717 efx->type->udp_tnl_push_ports(efx);
2718
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002719 return 0;
2720
2721fail:
2722 efx->port_initialized = false;
2723
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002724 mutex_unlock(&efx->mac_lock);
2725
Ben Hutchings8ceee662008-04-27 12:55:59 +01002726 return rc;
2727}
2728
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002729/* Reset the NIC using the specified method. Note that the reset may
2730 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002731 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002732 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002733 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002734int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002735{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002736 int rc, rc2;
2737 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002738
Ben Hutchings62776d02010-06-23 11:30:07 +00002739 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2740 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002741
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002742 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002743 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002744
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002745 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002746 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002747 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002748 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002749 }
2750
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002751 /* Clear flags for the scopes we covered. We assume the NIC and
2752 * driver are now quiescent so that there is no race here.
2753 */
Edward Creee2835462014-04-16 19:27:48 +01002754 if (method < RESET_TYPE_MAX_METHOD)
2755 efx->reset_pending &= -(1 << (method + 1));
2756 else /* it doesn't fit into the well-ordered scope hierarchy */
2757 __clear_bit(method, &efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002758
2759 /* Reinitialise bus-mastering, which may have been turned off before
2760 * the reset was scheduled. This is still appropriate, even in the
2761 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2762 * can respond to requests. */
2763 pci_set_master(efx->pci_dev);
2764
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002765out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002766 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002767 disabled = rc ||
2768 method == RESET_TYPE_DISABLE ||
2769 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002770 rc2 = efx_reset_up(efx, method, !disabled);
2771 if (rc2) {
2772 disabled = true;
2773 if (!rc)
2774 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002775 }
2776
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002777 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002778 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002779 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002780 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002781 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002782 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Peter Dunning9c568fd2017-02-17 15:50:43 +00002783 efx_device_attach_if_not_resetting(efx);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002784 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002785 return rc;
2786}
2787
Alexandre Rames626950d2013-01-14 17:20:22 +00002788/* Try recovery mechanisms.
2789 * For now only EEH is supported.
2790 * Returns 0 if the recovery mechanisms are unsuccessful.
2791 * Returns a non-zero value otherwise.
2792 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002793int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002794{
2795#ifdef CONFIG_EEH
2796 /* A PCI error can occur and not be seen by EEH because nothing
2797 * happens on the PCI bus. In this case the driver may fail and
2798 * schedule a 'recover or reset', leading to this recovery handler.
2799 * Manually call the eeh failure check function.
2800 */
Benjamin Herrenschmidt12a89db2015-03-23 14:00:47 +11002801 struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002802 if (eeh_dev_check_failure(eehdev)) {
2803 /* The EEH mechanisms will handle the error and reset the
2804 * device if necessary.
2805 */
2806 return 1;
2807 }
2808#endif
2809 return 0;
2810}
2811
Jon Cooper74cd60a2013-09-16 14:18:51 +01002812static void efx_wait_for_bist_end(struct efx_nic *efx)
2813{
2814 int i;
2815
2816 for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
2817 if (efx_mcdi_poll_reboot(efx))
2818 goto out;
2819 msleep(BIST_WAIT_DELAY_MS);
2820 }
2821
2822 netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
2823out:
2824 /* Either way unset the BIST flag. If we found no reboot we probably
2825 * won't recover, but we should try.
2826 */
2827 efx->mc_bist_for_other_fn = false;
2828}
2829
Ben Hutchings8ceee662008-04-27 12:55:59 +01002830/* The worker thread exists so that code that cannot sleep can
2831 * schedule a reset for later.
2832 */
2833static void efx_reset_work(struct work_struct *data)
2834{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002835 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002836 unsigned long pending;
2837 enum reset_type method;
2838
Mark Rutland6aa7de02017-10-23 14:07:29 -07002839 pending = READ_ONCE(efx->reset_pending);
Alexandre Rames626950d2013-01-14 17:20:22 +00002840 method = fls(pending) - 1;
2841
Jon Cooper74cd60a2013-09-16 14:18:51 +01002842 if (method == RESET_TYPE_MC_BIST)
2843 efx_wait_for_bist_end(efx);
2844
Alexandre Rames626950d2013-01-14 17:20:22 +00002845 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2846 method == RESET_TYPE_RECOVER_OR_ALL) &&
2847 efx_try_recovery(efx))
2848 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002849
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002850 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002851 return;
2852
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002853 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002854
2855 /* We checked the state in efx_schedule_reset() but it may
2856 * have changed by now. Now that we have the RTNL lock,
2857 * it cannot change again.
2858 */
2859 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002860 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002861
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002862 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002863}
2864
2865void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2866{
2867 enum reset_type method;
2868
Alexandre Rames626950d2013-01-14 17:20:22 +00002869 if (efx->state == STATE_RECOVERY) {
2870 netif_dbg(efx, drv, efx->net_dev,
2871 "recovering: skip scheduling %s reset\n",
2872 RESET_TYPE(type));
2873 return;
2874 }
2875
Ben Hutchings8ceee662008-04-27 12:55:59 +01002876 switch (type) {
2877 case RESET_TYPE_INVISIBLE:
2878 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002879 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002880 case RESET_TYPE_WORLD:
2881 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002882 case RESET_TYPE_RECOVER_OR_DISABLE:
Jon Cooper087e9022015-05-20 11:11:35 +01002883 case RESET_TYPE_DATAPATH:
Jon Cooper74cd60a2013-09-16 14:18:51 +01002884 case RESET_TYPE_MC_BIST:
Edward Creee2835462014-04-16 19:27:48 +01002885 case RESET_TYPE_MCDI_TIMEOUT:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002886 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002887 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2888 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002889 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002890 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002891 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002892 netif_dbg(efx, drv, efx->net_dev,
2893 "scheduling %s reset for %s\n",
2894 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002895 break;
2896 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002897
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002898 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002899 smp_mb(); /* ensure we change reset_pending before checking state */
2900
2901 /* If we're not READY then just leave the flags set as the cue
2902 * to abort probing or reschedule the reset later.
2903 */
Mark Rutland6aa7de02017-10-23 14:07:29 -07002904 if (READ_ONCE(efx->state) != STATE_READY)
Ben Hutchings7153f622012-07-27 20:50:52 +01002905 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002906
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002907 /* efx_process_channel() will no longer read events once a
2908 * reset is scheduled. So switch back to poll'd MCDI completions. */
2909 efx_mcdi_mode_poll(efx);
2910
Steve Hodgson1ab00622008-12-12 21:33:02 -08002911 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002912}
2913
2914/**************************************************************************
2915 *
2916 * List of NICs we support
2917 *
2918 **************************************************************************/
2919
2920/* PCI device ID table */
Benoit Taine9baa3c32014-08-08 15:56:03 +02002921static const struct pci_device_id efx_pci_table[] = {
Ben Hutchings547c4742011-12-02 18:23:56 +00002922 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002923 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002924 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002925 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8127d662013-08-29 19:19:29 +01002926 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
2927 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Shradha Shah6f7f8aa2015-05-06 01:00:07 +01002928 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
2929 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
Mateusz Wrzesinski3b06a002014-07-14 08:38:49 +01002930 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
2931 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Bert Kenwarddd248f12015-11-30 09:05:47 +00002932 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
2933 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
2934 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
2935 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2936 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
2937 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
Edward Creeaae5a312017-12-18 16:56:34 +00002938 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
2939 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2940 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
2941 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002942 {0} /* end of list */
2943};
2944
2945/**************************************************************************
2946 *
Ben Hutchings37594332009-11-23 16:05:45 +00002947 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002948 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002949 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002950 * Needed so all function pointers are valid and do not have to be tested
2951 * before use
2952 *
2953 **************************************************************************/
2954int efx_port_dummy_op_int(struct efx_nic *efx)
2955{
2956 return 0;
2957}
2958void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002959
2960static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002961{
2962 return false;
2963}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002964
stephen hemminger6c8c2512011-04-14 05:50:12 +00002965static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002966 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002967 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002968 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002969 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002970};
2971
Ben Hutchings8ceee662008-04-27 12:55:59 +01002972/**************************************************************************
2973 *
2974 * Data housekeeping
2975 *
2976 **************************************************************************/
2977
2978/* This zeroes out and then fills in the invariants in a struct
2979 * efx_nic (including all sub-structures).
2980 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002981static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002982 struct pci_dev *pci_dev, struct net_device *net_dev)
2983{
Andrew Rybchenko6f9f6ec2017-02-13 14:57:39 +00002984 int rc = -ENOMEM, i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002985
2986 /* Initialise common structures */
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002987 INIT_LIST_HEAD(&efx->node);
2988 INIT_LIST_HEAD(&efx->secondary_list);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002989 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002990#ifdef CONFIG_SFC_MTD
2991 INIT_LIST_HEAD(&efx->mtd_list);
2992#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002993 INIT_WORK(&efx->reset_work, efx_reset_work);
2994 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002995 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002996 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002997 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002998 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002999 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01003000
3001 efx->net_dev = net_dev;
Jon Cooper43a37392012-10-18 15:49:54 +01003002 efx->rx_prefix_size = efx->type->rx_prefix_size;
Andrew Rybchenko2ec03012013-11-16 11:02:27 +04003003 efx->rx_ip_align =
3004 NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
Jon Cooper43a37392012-10-18 15:49:54 +01003005 efx->rx_packet_hash_offset =
3006 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
Jon Cooperbd9a2652013-11-18 12:54:41 +00003007 efx->rx_packet_ts_offset =
3008 efx->type->rx_ts_offset - efx->type->rx_prefix_size;
Edward Cree42356d92018-03-08 15:45:17 +00003009 INIT_LIST_HEAD(&efx->rss_context.list);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003010 spin_lock_init(&efx->stats_lock);
Edward Cree71827442017-12-18 16:56:19 +00003011 efx->vi_stride = EFX_DEFAULT_VI_STRIDE;
Edward Creec1be4822017-12-21 09:00:26 +00003012 efx->num_mac_stats = MC_CMD_MAC_NSTATS;
3013 BUILD_BUG_ON(MC_CMD_MAC_NSTATS - 1 != MC_CMD_MAC_GENERATION_END);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003014 mutex_init(&efx->mac_lock);
3015 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00003016 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08003017 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00003018 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003019
3020 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00003021 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
3022 if (!efx->channel[i])
3023 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01003024 efx->msi_context[i].efx = efx;
3025 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003026 }
3027
Ben Hutchings8ceee662008-04-27 12:55:59 +01003028 /* Higher numbered interrupt modes are less capable! */
Andrew Rybchenko6f9f6ec2017-02-13 14:57:39 +00003029 if (WARN_ON_ONCE(efx->type->max_interrupt_mode >
3030 efx->type->min_interrupt_mode)) {
3031 rc = -EIO;
3032 goto fail;
3033 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003034 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
3035 interrupt_mode);
Andrew Rybchenko6f9f6ec2017-02-13 14:57:39 +00003036 efx->interrupt_mode = min(efx->type->min_interrupt_mode,
3037 interrupt_mode);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003038
Ben Hutchings6977dc62008-12-26 13:44:39 -08003039 /* Would be good to use the net_dev name, but we're too early */
3040 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
3041 pci_name(pci_dev));
3042 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003043 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00003044 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01003045
Ben Hutchings8ceee662008-04-27 12:55:59 +01003046 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00003047
3048fail:
3049 efx_fini_struct(efx);
Andrew Rybchenko6f9f6ec2017-02-13 14:57:39 +00003050 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003051}
3052
3053static void efx_fini_struct(struct efx_nic *efx)
3054{
Ben Hutchings8313aca2010-09-10 06:41:57 +00003055 int i;
3056
3057 for (i = 0; i < EFX_MAX_CHANNELS; i++)
3058 kfree(efx->channel[i]);
3059
Ben Hutchingsef215e62013-12-05 20:13:22 +00003060 kfree(efx->vpd_sn);
3061
Ben Hutchings8ceee662008-04-27 12:55:59 +01003062 if (efx->workqueue) {
3063 destroy_workqueue(efx->workqueue);
3064 efx->workqueue = NULL;
3065 }
3066}
3067
Edward Creee4d112e2014-07-15 11:58:12 +01003068void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
3069{
3070 u64 n_rx_nodesc_trunc = 0;
3071 struct efx_channel *channel;
3072
3073 efx_for_each_channel(channel, efx)
3074 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
3075 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
3076 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
3077}
3078
Edward Cree42356d92018-03-08 15:45:17 +00003079/* RSS contexts. We're using linked lists and crappy O(n) algorithms, because
3080 * (a) this is an infrequent control-plane operation and (b) n is small (max 64)
3081 */
3082struct efx_rss_context *efx_alloc_rss_context_entry(struct list_head *head)
3083{
3084 struct efx_rss_context *ctx, *new;
3085 u32 id = 1; /* Don't use zero, that refers to the master RSS context */
3086
3087 /* Search for first gap in the numbering */
3088 list_for_each_entry(ctx, head, list) {
3089 if (ctx->user_id != id)
3090 break;
3091 id++;
3092 /* Check for wrap. If this happens, we have nearly 2^32
3093 * allocated RSS contexts, which seems unlikely.
3094 */
3095 if (WARN_ON_ONCE(!id))
3096 return NULL;
3097 }
3098
3099 /* Create the new entry */
3100 new = kmalloc(sizeof(struct efx_rss_context), GFP_KERNEL);
3101 if (!new)
3102 return NULL;
3103 new->context_id = EFX_EF10_RSS_CONTEXT_INVALID;
3104 new->rx_hash_udp_4tuple = false;
3105
3106 /* Insert the new entry into the gap */
3107 new->user_id = id;
3108 list_add_tail(&new->list, &ctx->list);
3109 return new;
3110}
3111
3112struct efx_rss_context *efx_find_rss_context_entry(u32 id, struct list_head *head)
3113{
3114 struct efx_rss_context *ctx;
3115
3116 list_for_each_entry(ctx, head, list)
3117 if (ctx->user_id == id)
3118 return ctx;
3119 return NULL;
3120}
3121
3122void efx_free_rss_context_entry(struct efx_rss_context *ctx)
3123{
3124 list_del(&ctx->list);
3125 kfree(ctx);
3126}
3127
Ben Hutchings8ceee662008-04-27 12:55:59 +01003128/**************************************************************************
3129 *
3130 * PCI interface
3131 *
3132 **************************************************************************/
3133
3134/* Main body of final NIC shutdown code
3135 * This is called only at module unload (or hotplug removal).
3136 */
3137static void efx_pci_remove_main(struct efx_nic *efx)
3138{
Ben Hutchings7153f622012-07-27 20:50:52 +01003139 /* Flush reset_work. It can no longer be scheduled since we
3140 * are not READY.
3141 */
3142 BUG_ON(efx->state == STATE_READY);
3143 cancel_work_sync(&efx->reset_work);
3144
Ben Hutchingsd8291182012-10-05 23:35:41 +01003145 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00003146 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003147 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00003148 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003149 efx_fini_napi(efx);
3150 efx_remove_all(efx);
3151}
3152
3153/* Final NIC shutdown
Daniel Pieczko2a3fc312015-06-02 11:40:46 +01003154 * This is called only at module unload (or hotplug removal). A PF can call
3155 * this on its VFs to ensure they are unbound first.
Ben Hutchings8ceee662008-04-27 12:55:59 +01003156 */
3157static void efx_pci_remove(struct pci_dev *pci_dev)
3158{
3159 struct efx_nic *efx;
3160
3161 efx = pci_get_drvdata(pci_dev);
3162 if (!efx)
3163 return;
3164
3165 /* Mark the NIC as fini, then stop the interface */
3166 rtnl_lock();
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01003167 efx_dissociate(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003168 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003169 efx_disable_interrupts(efx);
Edward Creeea6bb992015-06-15 18:27:54 +01003170 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003171 rtnl_unlock();
3172
Shradha Shah7fa8d542015-05-06 00:55:13 +01003173 if (efx->type->sriov_fini)
3174 efx->type->sriov_fini(efx);
3175
Ben Hutchings8ceee662008-04-27 12:55:59 +01003176 efx_unregister_netdev(efx);
3177
Ben Hutchings7dde5962008-12-12 22:09:38 -08003178 efx_mtd_remove(efx);
3179
Ben Hutchings8ceee662008-04-27 12:55:59 +01003180 efx_pci_remove_main(efx);
3181
Ben Hutchings8ceee662008-04-27 12:55:59 +01003182 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00003183 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003184
Ben Hutchings8ceee662008-04-27 12:55:59 +01003185 efx_fini_struct(efx);
3186 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00003187
3188 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003189};
3190
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003191/* NIC VPD information
3192 * Called during probe to display the part number of the
3193 * installed NIC. VPD is potentially very large but this should
3194 * always appear within the first 512 bytes.
3195 */
3196#define SFC_VPD_LEN 512
Ben Hutchingsef215e62013-12-05 20:13:22 +00003197static void efx_probe_vpd_strings(struct efx_nic *efx)
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003198{
3199 struct pci_dev *dev = efx->pci_dev;
3200 char vpd_data[SFC_VPD_LEN];
3201 ssize_t vpd_size;
Ben Hutchingsef215e62013-12-05 20:13:22 +00003202 int ro_start, ro_size, i, j;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003203
3204 /* Get the vpd data from the device */
3205 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
3206 if (vpd_size <= 0) {
3207 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
3208 return;
3209 }
3210
3211 /* Get the Read only section */
Ben Hutchingsef215e62013-12-05 20:13:22 +00003212 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
3213 if (ro_start < 0) {
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003214 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
3215 return;
3216 }
3217
Ben Hutchingsef215e62013-12-05 20:13:22 +00003218 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
3219 j = ro_size;
3220 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003221 if (i + j > vpd_size)
3222 j = vpd_size - i;
3223
3224 /* Get the Part number */
3225 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
3226 if (i < 0) {
3227 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
3228 return;
3229 }
3230
3231 j = pci_vpd_info_field_size(&vpd_data[i]);
3232 i += PCI_VPD_INFO_FLD_HDR_SIZE;
3233 if (i + j > vpd_size) {
3234 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
3235 return;
3236 }
3237
3238 netif_info(efx, drv, efx->net_dev,
3239 "Part Number : %.*s\n", j, &vpd_data[i]);
Ben Hutchingsef215e62013-12-05 20:13:22 +00003240
3241 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
3242 j = ro_size;
3243 i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
3244 if (i < 0) {
3245 netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
3246 return;
3247 }
3248
3249 j = pci_vpd_info_field_size(&vpd_data[i]);
3250 i += PCI_VPD_INFO_FLD_HDR_SIZE;
3251 if (i + j > vpd_size) {
3252 netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
3253 return;
3254 }
3255
3256 efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
3257 if (!efx->vpd_sn)
3258 return;
3259
3260 snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003261}
3262
3263
Ben Hutchings8ceee662008-04-27 12:55:59 +01003264/* Main body of NIC initialisation
3265 * This is called at module load (or hotplug insertion, theoretically).
3266 */
3267static int efx_pci_probe_main(struct efx_nic *efx)
3268{
3269 int rc;
3270
3271 /* Do start-of-day initialisation */
3272 rc = efx_probe_all(efx);
3273 if (rc)
3274 goto fail1;
3275
Ben Hutchingse8f14992010-12-07 19:47:34 +00003276 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003277
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00003278 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003279 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00003280 netif_err(efx, probe, efx->net_dev,
3281 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00003282 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003283 }
3284
3285 rc = efx_init_port(efx);
3286 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00003287 netif_err(efx, probe, efx->net_dev,
3288 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00003289 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003290 }
3291
Ben Hutchings152b6a62009-11-29 03:43:56 +00003292 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003293 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00003294 goto fail5;
Jon Cooper261e4d92013-04-15 18:51:54 +01003295 rc = efx_enable_interrupts(efx);
3296 if (rc)
3297 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003298
3299 return 0;
3300
Jon Cooper261e4d92013-04-15 18:51:54 +01003301 fail6:
3302 efx_nic_fini_interrupt(efx);
Ben Hutchings278c0622009-11-23 16:05:12 +00003303 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003304 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003305 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00003306 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003307 fail3:
3308 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003309 efx_remove_all(efx);
3310 fail1:
3311 return rc;
3312}
3313
Jon Cooper8a531402017-02-08 16:51:18 +00003314static int efx_pci_probe_post_io(struct efx_nic *efx)
3315{
3316 struct net_device *net_dev = efx->net_dev;
3317 int rc = efx_pci_probe_main(efx);
3318
3319 if (rc)
3320 return rc;
3321
3322 if (efx->type->sriov_init) {
3323 rc = efx->type->sriov_init(efx);
3324 if (rc)
3325 netif_err(efx, probe, efx->net_dev,
3326 "SR-IOV can't be enabled rc %d\n", rc);
3327 }
3328
3329 /* Determine netdevice features */
3330 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Edward Cree69787292017-10-31 14:29:47 +00003331 NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
Jon Cooper8a531402017-02-08 16:51:18 +00003332 if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
3333 net_dev->features |= NETIF_F_TSO6;
3334 /* Check whether device supports TSO */
3335 if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
3336 net_dev->features &= ~NETIF_F_ALL_TSO;
3337 /* Mask for features that also apply to VLAN devices */
3338 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
3339 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
3340 NETIF_F_RXCSUM);
3341
Edward Cree69787292017-10-31 14:29:47 +00003342 net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
3343
3344 /* Disable receiving frames with bad FCS, by default. */
3345 net_dev->features &= ~NETIF_F_RXALL;
Jon Cooper8a531402017-02-08 16:51:18 +00003346
3347 /* Disable VLAN filtering by default. It may be enforced if
3348 * the feature is fixed (i.e. VLAN filters are required to
3349 * receive VLAN tagged packets due to vPort restrictions).
3350 */
3351 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
3352 net_dev->features |= efx->fixed_features;
3353
3354 rc = efx_register_netdev(efx);
3355 if (!rc)
3356 return 0;
3357
3358 efx_pci_remove_main(efx);
3359 return rc;
3360}
3361
Ben Hutchings8ceee662008-04-27 12:55:59 +01003362/* NIC initialisation
3363 *
3364 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00003365 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003366 * sets up and registers the network devices with the kernel and hooks
3367 * the interrupt service routine. It does not prepare the device for
3368 * transmission; this is left to the first time one of the network
3369 * interfaces is brought up (i.e. efx_net_open).
3370 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05003371static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00003372 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01003373{
Ben Hutchings8ceee662008-04-27 12:55:59 +01003374 struct net_device *net_dev;
3375 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003376 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003377
3378 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00003379 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
3380 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003381 if (!net_dev)
3382 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01003383 efx = netdev_priv(net_dev);
3384 efx->type = (const struct efx_nic_type *) entry->driver_data;
Andrew Rybchenkoebfcd0f2016-06-15 17:43:20 +01003385 efx->fixed_features |= NETIF_F_HIGHDMA;
Andrew Rybchenkoeb7cfd82016-06-15 17:51:36 +01003386
Ben Hutchings8ceee662008-04-27 12:55:59 +01003387 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00003388 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01003389 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003390 if (rc)
3391 goto fail1;
3392
Ben Hutchings62776d02010-06-23 11:30:07 +00003393 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01003394 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003395
Shradha Shah6f7f8aa2015-05-06 01:00:07 +01003396 if (!efx->type->is_vf)
3397 efx_probe_vpd_strings(efx);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003398
Ben Hutchings8ceee662008-04-27 12:55:59 +01003399 /* Set up basic I/O (BAR mappings etc) */
3400 rc = efx_init_io(efx);
3401 if (rc)
3402 goto fail2;
3403
Jon Cooper8a531402017-02-08 16:51:18 +00003404 rc = efx_pci_probe_post_io(efx);
3405 if (rc) {
3406 /* On failure, retry once immediately.
3407 * If we aborted probe due to a scheduled reset, dismiss it.
3408 */
3409 efx->reset_pending = 0;
3410 rc = efx_pci_probe_post_io(efx);
3411 if (rc) {
3412 /* On another failure, retry once more
3413 * after a 50-305ms delay.
3414 */
3415 unsigned char r;
3416
3417 get_random_bytes(&r, 1);
3418 msleep((unsigned int)r + 50);
3419 efx->reset_pending = 0;
3420 rc = efx_pci_probe_post_io(efx);
3421 }
3422 }
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003423 if (rc)
3424 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08003425
Ben Hutchings62776d02010-06-23 11:30:07 +00003426 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003427
Ben Hutchings7c431612012-01-27 17:23:58 +00003428 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003429 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00003430 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003431 rtnl_unlock();
Bert Kenward09a04202015-12-23 08:58:15 +00003432 if (rc && rc != -EPERM)
Ben Hutchings7c431612012-01-27 17:23:58 +00003433 netif_warn(efx, probe, efx->net_dev,
3434 "failed to create MTDs (%d)\n", rc);
3435
Alexandre Rames626950d2013-01-14 17:20:22 +00003436 rc = pci_enable_pcie_error_reporting(pci_dev);
3437 if (rc && rc != -EINVAL)
Bert Kenward09a04202015-12-23 08:58:15 +00003438 netif_notice(efx, probe, efx->net_dev,
3439 "PCIE error reporting unavailable (%d).\n",
3440 rc);
Alexandre Rames626950d2013-01-14 17:20:22 +00003441
Jon Coopere5fbd972017-02-08 16:52:10 +00003442 if (efx->type->udp_tnl_push_ports)
3443 efx->type->udp_tnl_push_ports(efx);
3444
Ben Hutchings8ceee662008-04-27 12:55:59 +01003445 return 0;
3446
Ben Hutchings8ceee662008-04-27 12:55:59 +01003447 fail3:
3448 efx_fini_io(efx);
3449 fail2:
3450 efx_fini_struct(efx);
3451 fail1:
Steve Hodgson5e2a9112010-02-12 12:32:27 -08003452 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00003453 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003454 free_netdev(net_dev);
3455 return rc;
3456}
3457
Shradha Shah834e23d2015-05-06 00:55:58 +01003458/* efx_pci_sriov_configure returns the actual number of Virtual Functions
3459 * enabled on success
3460 */
3461#ifdef CONFIG_SFC_SRIOV
3462static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
3463{
3464 int rc;
3465 struct efx_nic *efx = pci_get_drvdata(dev);
3466
3467 if (efx->type->sriov_configure) {
3468 rc = efx->type->sriov_configure(efx, num_vfs);
3469 if (rc)
3470 return rc;
3471 else
3472 return num_vfs;
3473 } else
3474 return -EOPNOTSUPP;
3475}
3476#endif
3477
Ben Hutchings89c758f2009-11-29 03:43:07 +00003478static int efx_pm_freeze(struct device *dev)
3479{
3480 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3481
Ben Hutchings61da0262012-07-27 19:35:39 +01003482 rtnl_lock();
3483
Ben Hutchings6032fb52012-07-27 19:35:47 +01003484 if (efx->state != STATE_DISABLED) {
3485 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003486
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01003487 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003488
Ben Hutchings6032fb52012-07-27 19:35:47 +01003489 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003490 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01003491 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00003492
Ben Hutchings61da0262012-07-27 19:35:39 +01003493 rtnl_unlock();
3494
Ben Hutchings89c758f2009-11-29 03:43:07 +00003495 return 0;
3496}
3497
3498static int efx_pm_thaw(struct device *dev)
3499{
Jon Cooper261e4d92013-04-15 18:51:54 +01003500 int rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003501 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3502
Ben Hutchings61da0262012-07-27 19:35:39 +01003503 rtnl_lock();
3504
Ben Hutchings6032fb52012-07-27 19:35:47 +01003505 if (efx->state != STATE_DISABLED) {
Jon Cooper261e4d92013-04-15 18:51:54 +01003506 rc = efx_enable_interrupts(efx);
3507 if (rc)
3508 goto fail;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003509
Ben Hutchings6032fb52012-07-27 19:35:47 +01003510 mutex_lock(&efx->mac_lock);
3511 efx->phy_op->reconfigure(efx);
3512 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003513
Ben Hutchings6032fb52012-07-27 19:35:47 +01003514 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003515
Peter Dunning9c568fd2017-02-17 15:50:43 +00003516 efx_device_attach_if_not_resetting(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003517
Ben Hutchings6032fb52012-07-27 19:35:47 +01003518 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003519
Ben Hutchings6032fb52012-07-27 19:35:47 +01003520 efx->type->resume_wol(efx);
3521 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00003522
Ben Hutchings61da0262012-07-27 19:35:39 +01003523 rtnl_unlock();
3524
Steve Hodgson319ba642010-06-01 11:17:24 +00003525 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
3526 queue_work(reset_workqueue, &efx->reset_work);
3527
Ben Hutchings89c758f2009-11-29 03:43:07 +00003528 return 0;
Jon Cooper261e4d92013-04-15 18:51:54 +01003529
3530fail:
3531 rtnl_unlock();
3532
3533 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003534}
3535
3536static int efx_pm_poweroff(struct device *dev)
3537{
3538 struct pci_dev *pci_dev = to_pci_dev(dev);
3539 struct efx_nic *efx = pci_get_drvdata(pci_dev);
3540
3541 efx->type->fini(efx);
3542
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01003543 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003544
3545 pci_save_state(pci_dev);
3546 return pci_set_power_state(pci_dev, PCI_D3hot);
3547}
3548
3549/* Used for both resume and restore */
3550static int efx_pm_resume(struct device *dev)
3551{
3552 struct pci_dev *pci_dev = to_pci_dev(dev);
3553 struct efx_nic *efx = pci_get_drvdata(pci_dev);
3554 int rc;
3555
3556 rc = pci_set_power_state(pci_dev, PCI_D0);
3557 if (rc)
3558 return rc;
3559 pci_restore_state(pci_dev);
3560 rc = pci_enable_device(pci_dev);
3561 if (rc)
3562 return rc;
3563 pci_set_master(efx->pci_dev);
3564 rc = efx->type->reset(efx, RESET_TYPE_ALL);
3565 if (rc)
3566 return rc;
3567 rc = efx->type->init(efx);
3568 if (rc)
3569 return rc;
Jon Cooper261e4d92013-04-15 18:51:54 +01003570 rc = efx_pm_thaw(dev);
3571 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003572}
3573
3574static int efx_pm_suspend(struct device *dev)
3575{
3576 int rc;
3577
3578 efx_pm_freeze(dev);
3579 rc = efx_pm_poweroff(dev);
3580 if (rc)
3581 efx_pm_resume(dev);
3582 return rc;
3583}
3584
Ben Hutchings18e83e42012-01-05 19:05:20 +00003585static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00003586 .suspend = efx_pm_suspend,
3587 .resume = efx_pm_resume,
3588 .freeze = efx_pm_freeze,
3589 .thaw = efx_pm_thaw,
3590 .poweroff = efx_pm_poweroff,
3591 .restore = efx_pm_resume,
3592};
3593
Alexandre Rames626950d2013-01-14 17:20:22 +00003594/* A PCI error affecting this device was detected.
3595 * At this point MMIO and DMA may be disabled.
3596 * Stop the software path and request a slot reset.
3597 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003598static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
3599 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00003600{
3601 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3602 struct efx_nic *efx = pci_get_drvdata(pdev);
3603
3604 if (state == pci_channel_io_perm_failure)
3605 return PCI_ERS_RESULT_DISCONNECT;
3606
3607 rtnl_lock();
3608
3609 if (efx->state != STATE_DISABLED) {
3610 efx->state = STATE_RECOVERY;
3611 efx->reset_pending = 0;
3612
3613 efx_device_detach_sync(efx);
3614
3615 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003616 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00003617
3618 status = PCI_ERS_RESULT_NEED_RESET;
3619 } else {
3620 /* If the interface is disabled we don't want to do anything
3621 * with it.
3622 */
3623 status = PCI_ERS_RESULT_RECOVERED;
3624 }
3625
3626 rtnl_unlock();
3627
3628 pci_disable_device(pdev);
3629
3630 return status;
3631}
3632
Joe Perchesdbedd442015-03-06 20:49:12 -08003633/* Fake a successful reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003634static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00003635{
3636 struct efx_nic *efx = pci_get_drvdata(pdev);
3637 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3638 int rc;
3639
3640 if (pci_enable_device(pdev)) {
3641 netif_err(efx, hw, efx->net_dev,
3642 "Cannot re-enable PCI device after reset.\n");
3643 status = PCI_ERS_RESULT_DISCONNECT;
3644 }
3645
3646 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
3647 if (rc) {
3648 netif_err(efx, hw, efx->net_dev,
3649 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3650 /* Non-fatal error. Continue. */
3651 }
3652
3653 return status;
3654}
3655
3656/* Perform the actual reset and resume I/O operations. */
3657static void efx_io_resume(struct pci_dev *pdev)
3658{
3659 struct efx_nic *efx = pci_get_drvdata(pdev);
3660 int rc;
3661
3662 rtnl_lock();
3663
3664 if (efx->state == STATE_DISABLED)
3665 goto out;
3666
3667 rc = efx_reset(efx, RESET_TYPE_ALL);
3668 if (rc) {
3669 netif_err(efx, hw, efx->net_dev,
3670 "efx_reset failed after PCI error (%d)\n", rc);
3671 } else {
3672 efx->state = STATE_READY;
3673 netif_dbg(efx, hw, efx->net_dev,
3674 "Done resetting and resuming IO after PCI error.\n");
3675 }
3676
3677out:
3678 rtnl_unlock();
3679}
3680
3681/* For simplicity and reliability, we always require a slot reset and try to
3682 * reset the hardware when a pci error affecting the device is detected.
3683 * We leave both the link_reset and mmio_enabled callback unimplemented:
3684 * with our request for slot reset the mmio_enabled callback will never be
3685 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3686 */
Julia Lawallc300366b2015-11-14 11:06:57 +01003687static const struct pci_error_handlers efx_err_handlers = {
Alexandre Rames626950d2013-01-14 17:20:22 +00003688 .error_detected = efx_io_error_detected,
3689 .slot_reset = efx_io_slot_reset,
3690 .resume = efx_io_resume,
3691};
3692
Ben Hutchings8ceee662008-04-27 12:55:59 +01003693static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003694 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003695 .id_table = efx_pci_table,
3696 .probe = efx_pci_probe,
3697 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003698 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003699 .err_handler = &efx_err_handlers,
Shradha Shah834e23d2015-05-06 00:55:58 +01003700#ifdef CONFIG_SFC_SRIOV
3701 .sriov_configure = efx_pci_sriov_configure,
3702#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003703};
3704
3705/**************************************************************************
3706 *
3707 * Kernel module interface
3708 *
3709 *************************************************************************/
3710
3711module_param(interrupt_mode, uint, 0444);
3712MODULE_PARM_DESC(interrupt_mode,
3713 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3714
3715static int __init efx_init_module(void)
3716{
3717 int rc;
3718
3719 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3720
3721 rc = register_netdevice_notifier(&efx_netdev_notifier);
3722 if (rc)
3723 goto err_notifier;
3724
Shradha Shah7fa8d542015-05-06 00:55:13 +01003725#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003726 rc = efx_init_sriov();
3727 if (rc)
3728 goto err_sriov;
Shradha Shah7fa8d542015-05-06 00:55:13 +01003729#endif
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003730
Steve Hodgson1ab00622008-12-12 21:33:02 -08003731 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3732 if (!reset_workqueue) {
3733 rc = -ENOMEM;
3734 goto err_reset;
3735 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003736
3737 rc = pci_register_driver(&efx_pci_driver);
3738 if (rc < 0)
3739 goto err_pci;
3740
3741 return 0;
3742
3743 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003744 destroy_workqueue(reset_workqueue);
3745 err_reset:
Shradha Shah7fa8d542015-05-06 00:55:13 +01003746#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003747 efx_fini_sriov();
3748 err_sriov:
Shradha Shah7fa8d542015-05-06 00:55:13 +01003749#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003750 unregister_netdevice_notifier(&efx_netdev_notifier);
3751 err_notifier:
3752 return rc;
3753}
3754
3755static void __exit efx_exit_module(void)
3756{
3757 printk(KERN_INFO "Solarflare NET driver unloading\n");
3758
3759 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003760 destroy_workqueue(reset_workqueue);
Shradha Shah7fa8d542015-05-06 00:55:13 +01003761#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003762 efx_fini_sriov();
Shradha Shah7fa8d542015-05-06 00:55:13 +01003763#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003764 unregister_netdevice_notifier(&efx_netdev_notifier);
3765
3766}
3767
3768module_init(efx_init_module);
3769module_exit(efx_exit_module);
3770
Ben Hutchings906bb262009-11-29 15:16:19 +00003771MODULE_AUTHOR("Solarflare Communications and "
3772 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings6a350fd2014-02-12 19:00:07 +00003773MODULE_DESCRIPTION("Solarflare network driver");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003774MODULE_LICENSE("GPL");
3775MODULE_DEVICE_TABLE(pci, efx_pci_table);
Edward Cree14077e92017-01-03 15:46:00 +00003776MODULE_VERSION(EFX_DRIVER_VERSION);