blob: 661828e8fdcf9a93017d0e5d1ada12715ba00d6a [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
Edward Cree3af0f342018-03-27 17:41:59 +0100343#ifdef CONFIG_RFS_ACCEL
344 /* Perhaps expire some ARFS filters */
345 schedule_work(&channel->filter_work);
346#endif
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000347
Ben Hutchings8ceee662008-04-27 12:55:59 +0100348 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800349 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100350 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100351 * interrupts have already been disabled.
352 */
Bert Kenwardf820c0a2017-02-06 16:50:55 +0000353 if (napi_complete_done(napi, spent))
354 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100355 }
356
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000357 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100358}
359
Ben Hutchings8ceee662008-04-27 12:55:59 +0100360/* Create event queue
361 * Event queue memory allocations are done only once. If the channel
362 * is reset, the memory buffer will be reused; this guards against
363 * errors during channel reset and also simplifies interrupt handling.
364 */
365static int efx_probe_eventq(struct efx_channel *channel)
366{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000367 struct efx_nic *efx = channel->efx;
368 unsigned long entries;
369
Ben Hutchings86ee5302012-01-09 19:51:22 +0000370 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000371 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100372
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000373 /* Build an event queue with room for one event per tx and rx buffer,
374 * plus some extra for link state events and MCDI completions. */
375 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
Edward Creee01b16a2016-12-02 15:51:33 +0000376 EFX_WARN_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000377 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
378
Ben Hutchings152b6a62009-11-29 03:43:56 +0000379 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100380}
381
382/* Prepare channel's event queue */
Jon Cooper261e4d92013-04-15 18:51:54 +0100383static int efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100384{
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100385 struct efx_nic *efx = channel->efx;
Jon Cooper261e4d92013-04-15 18:51:54 +0100386 int rc;
387
388 EFX_WARN_ON_PARANOID(channel->eventq_init);
389
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100390 netif_dbg(efx, drv, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000391 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100392
Jon Cooper261e4d92013-04-15 18:51:54 +0100393 rc = efx_nic_init_eventq(channel);
394 if (rc == 0) {
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100395 efx->type->push_irq_moderation(channel);
Jon Cooper261e4d92013-04-15 18:51:54 +0100396 channel->eventq_read_ptr = 0;
397 channel->eventq_init = true;
398 }
399 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100400}
401
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000402/* Enable event queue processing and NAPI */
Alexandre Rames36763262014-07-22 14:03:25 +0100403void efx_start_eventq(struct efx_channel *channel)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000404{
405 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
406 "chan %d start event queue\n", channel->channel);
407
Ben Hutchings514bedb2012-10-05 19:30:16 +0100408 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000409 channel->enabled = true;
410 smp_wmb();
411
412 napi_enable(&channel->napi_str);
413 efx_nic_eventq_read_ack(channel);
414}
415
416/* Disable event queue processing and NAPI */
Alexandre Rames36763262014-07-22 14:03:25 +0100417void efx_stop_eventq(struct efx_channel *channel)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000418{
419 if (!channel->enabled)
420 return;
421
422 napi_disable(&channel->napi_str);
423 channel->enabled = false;
424}
425
Ben Hutchings8ceee662008-04-27 12:55:59 +0100426static void efx_fini_eventq(struct efx_channel *channel)
427{
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100428 if (!channel->eventq_init)
429 return;
430
Ben Hutchings62776d02010-06-23 11:30:07 +0000431 netif_dbg(channel->efx, drv, channel->efx->net_dev,
432 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100433
Ben Hutchings152b6a62009-11-29 03:43:56 +0000434 efx_nic_fini_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100435 channel->eventq_init = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100436}
437
438static void efx_remove_eventq(struct efx_channel *channel)
439{
Ben Hutchings62776d02010-06-23 11:30:07 +0000440 netif_dbg(channel->efx, drv, channel->efx->net_dev,
441 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100442
Ben Hutchings152b6a62009-11-29 03:43:56 +0000443 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100444}
445
446/**************************************************************************
447 *
448 * Channel handling
449 *
450 *************************************************************************/
451
Ben Hutchings7f967c02012-02-13 23:45:02 +0000452/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000453static struct efx_channel *
454efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
455{
456 struct efx_channel *channel;
457 struct efx_rx_queue *rx_queue;
458 struct efx_tx_queue *tx_queue;
459 int j;
460
Ben Hutchings7f967c02012-02-13 23:45:02 +0000461 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
462 if (!channel)
463 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000464
Ben Hutchings7f967c02012-02-13 23:45:02 +0000465 channel->efx = efx;
466 channel->channel = i;
467 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000468
Ben Hutchings7f967c02012-02-13 23:45:02 +0000469 for (j = 0; j < EFX_TXQ_TYPES; j++) {
470 tx_queue = &channel->tx_queue[j];
471 tx_queue->efx = efx;
472 tx_queue->queue = i * EFX_TXQ_TYPES + j;
473 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000474 }
475
Edward Cree3af0f342018-03-27 17:41:59 +0100476#ifdef CONFIG_RFS_ACCEL
477 INIT_WORK(&channel->filter_work, efx_filter_rfs_expire);
478#endif
479
Ben Hutchings46426102010-09-10 06:42:33 +0000480 rx_queue = &channel->rx_queue;
481 rx_queue->efx = efx;
Kees Cook7aa14022017-10-24 01:45:59 -0700482 timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0);
Ben Hutchings46426102010-09-10 06:42:33 +0000483
484 return channel;
485}
486
Ben Hutchings7f967c02012-02-13 23:45:02 +0000487/* Allocate and initialise a channel structure, copying parameters
488 * (but not resources) from an old channel structure.
489 */
490static struct efx_channel *
491efx_copy_channel(const struct efx_channel *old_channel)
492{
493 struct efx_channel *channel;
494 struct efx_rx_queue *rx_queue;
495 struct efx_tx_queue *tx_queue;
496 int j;
497
498 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
499 if (!channel)
500 return NULL;
501
502 *channel = *old_channel;
503
504 channel->napi_dev = NULL;
Bert Kenward46d054f2016-11-11 15:56:51 +0000505 INIT_HLIST_NODE(&channel->napi_str.napi_hash_node);
506 channel->napi_str.napi_id = 0;
507 channel->napi_str.state = 0;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000508 memset(&channel->eventq, 0, sizeof(channel->eventq));
509
510 for (j = 0; j < EFX_TXQ_TYPES; j++) {
511 tx_queue = &channel->tx_queue[j];
512 if (tx_queue->channel)
513 tx_queue->channel = channel;
514 tx_queue->buffer = NULL;
515 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
516 }
517
518 rx_queue = &channel->rx_queue;
519 rx_queue->buffer = NULL;
520 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
Kees Cook7aa14022017-10-24 01:45:59 -0700521 timer_setup(&rx_queue->slow_fill, efx_rx_slow_fill, 0);
Edward Cree3af0f342018-03-27 17:41:59 +0100522#ifdef CONFIG_RFS_ACCEL
523 INIT_WORK(&channel->filter_work, efx_filter_rfs_expire);
524#endif
Ben Hutchings7f967c02012-02-13 23:45:02 +0000525
526 return channel;
527}
528
Ben Hutchings8ceee662008-04-27 12:55:59 +0100529static int efx_probe_channel(struct efx_channel *channel)
530{
531 struct efx_tx_queue *tx_queue;
532 struct efx_rx_queue *rx_queue;
533 int rc;
534
Ben Hutchings62776d02010-06-23 11:30:07 +0000535 netif_dbg(channel->efx, probe, channel->efx->net_dev,
536 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100537
Ben Hutchings7f967c02012-02-13 23:45:02 +0000538 rc = channel->type->pre_probe(channel);
539 if (rc)
540 goto fail;
541
Ben Hutchings8ceee662008-04-27 12:55:59 +0100542 rc = efx_probe_eventq(channel);
543 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000544 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100545
546 efx_for_each_channel_tx_queue(tx_queue, channel) {
547 rc = efx_probe_tx_queue(tx_queue);
548 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000549 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100550 }
551
552 efx_for_each_channel_rx_queue(rx_queue, channel) {
553 rc = efx_probe_rx_queue(rx_queue);
554 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000555 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100556 }
557
Ben Hutchings8ceee662008-04-27 12:55:59 +0100558 return 0;
559
Ben Hutchings7f967c02012-02-13 23:45:02 +0000560fail:
561 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100562 return rc;
563}
564
Ben Hutchings7f967c02012-02-13 23:45:02 +0000565static void
566efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
567{
568 struct efx_nic *efx = channel->efx;
569 const char *type;
570 int number;
571
572 number = channel->channel;
573 if (efx->tx_channel_offset == 0) {
574 type = "";
575 } else if (channel->channel < efx->tx_channel_offset) {
576 type = "-rx";
577 } else {
578 type = "-tx";
579 number -= efx->tx_channel_offset;
580 }
581 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
582}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100583
Ben Hutchings56536e92008-12-12 21:37:02 -0800584static void efx_set_channel_names(struct efx_nic *efx)
585{
586 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800587
Ben Hutchings7f967c02012-02-13 23:45:02 +0000588 efx_for_each_channel(channel, efx)
589 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100590 efx->msi_context[channel->channel].name,
591 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800592}
593
Ben Hutchings46426102010-09-10 06:42:33 +0000594static int efx_probe_channels(struct efx_nic *efx)
595{
596 struct efx_channel *channel;
597 int rc;
598
599 /* Restart special buffer allocation */
600 efx->next_buffer_table = 0;
601
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000602 /* Probe channels in reverse, so that any 'extra' channels
603 * use the start of the buffer table. This allows the traffic
604 * channels to be resized without moving them or wasting the
605 * entries before them.
606 */
607 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000608 rc = efx_probe_channel(channel);
609 if (rc) {
610 netif_err(efx, probe, efx->net_dev,
611 "failed to create channel %d\n",
612 channel->channel);
613 goto fail;
614 }
615 }
616 efx_set_channel_names(efx);
617
618 return 0;
619
620fail:
621 efx_remove_channels(efx);
622 return rc;
623}
624
Ben Hutchings8ceee662008-04-27 12:55:59 +0100625/* Channels are shutdown and reinitialised whilst the NIC is running
626 * to propagate configuration changes (mtu, checksum offload), or
627 * to clear hardware error conditions
628 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000629static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100630{
Andrew Rybchenkoebfcd0f2016-06-15 17:43:20 +0100631 netdev_features_t old_features = efx->net_dev->features;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000632 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100633 struct efx_tx_queue *tx_queue;
634 struct efx_rx_queue *rx_queue;
635 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000636 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100637
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100638 /* Calculate the rx buffer allocation parameters required to
639 * support the current MTU, including padding for header
640 * alignment and overruns.
641 */
Jon Cooper43a37392012-10-18 15:49:54 +0100642 efx->rx_dma_len = (efx->rx_prefix_size +
Ben Hutchings272baee2013-01-29 23:33:14 +0000643 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
644 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000645 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Andrew Rybchenko2ec03012013-11-16 11:02:27 +0400646 efx->rx_ip_align + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000647 if (rx_buf_len <= PAGE_SIZE) {
Jon Coopere8c68c02013-03-08 10:18:28 +0000648 efx->rx_scatter = efx->type->always_rx_scatter;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000649 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000650 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000651 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000652 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000653 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
654 EFX_RX_BUF_ALIGNMENT) >
655 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000656 efx->rx_scatter = true;
657 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
658 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000659 } else {
660 efx->rx_scatter = false;
661 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000662 }
663
Daniel Pieczko1648a232013-02-13 10:54:41 +0000664 efx_rx_config_page_split(efx);
665 if (efx->rx_buffer_order)
666 netif_dbg(efx, drv, efx->net_dev,
667 "RX buf len=%u; page order=%u batch=%u\n",
668 efx->rx_dma_len, efx->rx_buffer_order,
669 efx->rx_pages_per_batch);
670 else
671 netif_dbg(efx, drv, efx->net_dev,
672 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
673 efx->rx_dma_len, efx->rx_page_buf_step,
674 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000675
Andrew Rybchenkoebfcd0f2016-06-15 17:43:20 +0100676 /* Restore previously fixed features in hw_features and remove
677 * features which are fixed now
678 */
679 efx->net_dev->hw_features |= efx->net_dev->features;
680 efx->net_dev->hw_features &= ~efx->fixed_features;
681 efx->net_dev->features |= efx->fixed_features;
682 if (efx->net_dev->features != old_features)
683 netdev_features_change(efx->net_dev);
684
Jon Coopere8c68c02013-03-08 10:18:28 +0000685 /* RX filters may also have scatter-enabled flags */
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000686 if (efx->rx_scatter != old_rx_scatter)
Ben Hutchingsadd72472012-11-08 01:46:53 +0000687 efx->type->filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100688
Ben Hutchings14bf7182012-05-22 01:27:58 +0100689 /* We must keep at least one descriptor in a TX ring empty.
690 * We could avoid this when the queue size does not exactly
691 * match the hardware ring size, but it's not that important.
692 * Therefore we stop the queue when one more skb might fill
693 * the ring completely. We wake it when half way back to
694 * empty.
695 */
696 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
697 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
698
Ben Hutchings8ceee662008-04-27 12:55:59 +0100699 /* Initialise the channels */
700 efx_for_each_channel(channel, efx) {
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100701 efx_for_each_channel_tx_queue(tx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100702 efx_init_tx_queue(tx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100703 atomic_inc(&efx->active_queues);
704 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100705
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000706 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100707 efx_init_rx_queue(rx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100708 atomic_inc(&efx->active_queues);
Jon Coopercce28792013-10-02 11:04:14 +0100709 efx_stop_eventq(channel);
710 efx_fast_push_rx_descriptors(rx_queue, false);
711 efx_start_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000712 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100713
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000714 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100715 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000716
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000717 efx_ptp_start_datapath(efx);
718
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000719 if (netif_device_present(efx->net_dev))
720 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100721}
722
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000723static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100724{
725 struct efx_channel *channel;
726 struct efx_tx_queue *tx_queue;
727 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100728 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100729
730 EFX_ASSERT_RESET_SERIALISED(efx);
731 BUG_ON(efx->port_enabled);
732
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000733 efx_ptp_stop_datapath(efx);
734
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100735 /* Stop RX refill */
736 efx_for_each_channel(channel, efx) {
737 efx_for_each_channel_rx_queue(rx_queue, channel)
738 rx_queue->refill_enabled = false;
739 }
740
Ben Hutchings8ceee662008-04-27 12:55:59 +0100741 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000742 /* RX packet processing is pipelined, so wait for the
743 * NAPI handler to complete. At least event queue 0
744 * might be kept active by non-data events, so don't
745 * use napi_synchronize() but actually disable NAPI
746 * temporarily.
747 */
748 if (efx_channel_has_rx_queue(channel)) {
749 efx_stop_eventq(channel);
750 efx_start_eventq(channel);
751 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100752 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100753
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100754 rc = efx->type->fini_dmaq(efx);
Edward Cree5a6681e2016-11-28 18:55:34 +0000755 if (rc) {
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100756 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
757 } else {
758 netif_dbg(efx, drv, efx->net_dev,
759 "successfully flushed all queues\n");
760 }
761
762 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100763 efx_for_each_channel_rx_queue(rx_queue, channel)
764 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000765 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100766 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100767 }
768}
769
770static void efx_remove_channel(struct efx_channel *channel)
771{
772 struct efx_tx_queue *tx_queue;
773 struct efx_rx_queue *rx_queue;
774
Ben Hutchings62776d02010-06-23 11:30:07 +0000775 netif_dbg(channel->efx, drv, channel->efx->net_dev,
776 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100777
778 efx_for_each_channel_rx_queue(rx_queue, channel)
779 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000780 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100781 efx_remove_tx_queue(tx_queue);
782 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100783 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100784}
785
Ben Hutchings46426102010-09-10 06:42:33 +0000786static void efx_remove_channels(struct efx_nic *efx)
787{
788 struct efx_channel *channel;
789
790 efx_for_each_channel(channel, efx)
791 efx_remove_channel(channel);
792}
793
794int
795efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
796{
797 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
798 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000799 unsigned i, next_buffer_table = 0;
Jon Cooper261e4d92013-04-15 18:51:54 +0100800 int rc, rc2;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100801
802 rc = efx_check_disabled(efx);
803 if (rc)
804 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000805
806 /* Not all channels should be reallocated. We must avoid
807 * reallocating their buffer table entries.
808 */
809 efx_for_each_channel(channel, efx) {
810 struct efx_rx_queue *rx_queue;
811 struct efx_tx_queue *tx_queue;
812
813 if (channel->type->copy)
814 continue;
815 next_buffer_table = max(next_buffer_table,
816 channel->eventq.index +
817 channel->eventq.entries);
818 efx_for_each_channel_rx_queue(rx_queue, channel)
819 next_buffer_table = max(next_buffer_table,
820 rx_queue->rxd.index +
821 rx_queue->rxd.entries);
822 efx_for_each_channel_tx_queue(tx_queue, channel)
823 next_buffer_table = max(next_buffer_table,
824 tx_queue->txd.index +
825 tx_queue->txd.entries);
826 }
Ben Hutchings46426102010-09-10 06:42:33 +0000827
Ben Hutchings29c69a42013-01-28 19:01:06 +0000828 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000829 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100830 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000831
Ben Hutchings7f967c02012-02-13 23:45:02 +0000832 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000833 memset(other_channel, 0, sizeof(other_channel));
834 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000835 channel = efx->channel[i];
836 if (channel->type->copy)
837 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000838 if (!channel) {
839 rc = -ENOMEM;
840 goto out;
841 }
842 other_channel[i] = channel;
843 }
844
845 /* Swap entry counts and channel pointers */
846 old_rxq_entries = efx->rxq_entries;
847 old_txq_entries = efx->txq_entries;
848 efx->rxq_entries = rxq_entries;
849 efx->txq_entries = txq_entries;
850 for (i = 0; i < efx->n_channels; i++) {
851 channel = efx->channel[i];
852 efx->channel[i] = other_channel[i];
853 other_channel[i] = channel;
854 }
855
Ben Hutchings7f967c02012-02-13 23:45:02 +0000856 /* Restart buffer table allocation */
857 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000858
Ben Hutchingse8f14992010-12-07 19:47:34 +0000859 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000860 channel = efx->channel[i];
861 if (!channel->type->copy)
862 continue;
863 rc = efx_probe_channel(channel);
864 if (rc)
865 goto rollback;
866 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000867 }
Ben Hutchings46426102010-09-10 06:42:33 +0000868
Ben Hutchings7f967c02012-02-13 23:45:02 +0000869out:
870 /* Destroy unused channel structures */
871 for (i = 0; i < efx->n_channels; i++) {
872 channel = other_channel[i];
873 if (channel && channel->type->copy) {
874 efx_fini_napi_channel(channel);
875 efx_remove_channel(channel);
876 kfree(channel);
877 }
878 }
879
Jon Cooper261e4d92013-04-15 18:51:54 +0100880 rc2 = efx_soft_enable_interrupts(efx);
881 if (rc2) {
882 rc = rc ? rc : rc2;
883 netif_err(efx, drv, efx->net_dev,
884 "unable to restart interrupts on channel reallocation\n");
885 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
886 } else {
887 efx_start_all(efx);
Peter Dunning9c568fd2017-02-17 15:50:43 +0000888 efx_device_attach_if_not_resetting(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +0100889 }
Ben Hutchings46426102010-09-10 06:42:33 +0000890 return rc;
891
892rollback:
893 /* Swap back */
894 efx->rxq_entries = old_rxq_entries;
895 efx->txq_entries = old_txq_entries;
896 for (i = 0; i < efx->n_channels; i++) {
897 channel = efx->channel[i];
898 efx->channel[i] = other_channel[i];
899 other_channel[i] = channel;
900 }
901 goto out;
902}
903
Steve Hodgson90d683a2010-06-01 11:19:39 +0000904void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100905{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000906 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100907}
908
kbuild test robote7345ba2018-01-26 17:00:39 +0000909static bool efx_default_channel_want_txqs(struct efx_channel *channel)
Edward Cree2935e3c2018-01-25 17:26:06 +0000910{
911 return channel->channel - channel->efx->tx_channel_offset <
912 channel->efx->n_tx_channels;
913}
914
Ben Hutchings7f967c02012-02-13 23:45:02 +0000915static const struct efx_channel_type efx_default_channel_type = {
916 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100917 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000918 .get_name = efx_get_channel_name,
919 .copy = efx_copy_channel,
Edward Cree2935e3c2018-01-25 17:26:06 +0000920 .want_txqs = efx_default_channel_want_txqs,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000921 .keep_eventq = false,
Edward Cree2935e3c2018-01-25 17:26:06 +0000922 .want_pio = true,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000923};
924
925int efx_channel_dummy_op_int(struct efx_channel *channel)
926{
927 return 0;
928}
929
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100930void efx_channel_dummy_op_void(struct efx_channel *channel)
931{
932}
933
Ben Hutchings8ceee662008-04-27 12:55:59 +0100934/**************************************************************************
935 *
936 * Port handling
937 *
938 **************************************************************************/
939
940/* This ensures that the kernel is kept informed (via
941 * netif_carrier_on/off) of the link status, and also maintains the
942 * link status's stop on the port's TX queue.
943 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000944void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100945{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000946 struct efx_link_state *link_state = &efx->link_state;
947
Ben Hutchings8ceee662008-04-27 12:55:59 +0100948 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
949 * that no events are triggered between unregister_netdev() and the
950 * driver unloading. A more general condition is that NETDEV_CHANGE
951 * can only be generated between NETDEV_UP and NETDEV_DOWN */
952 if (!netif_running(efx->net_dev))
953 return;
954
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000955 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100956 efx->n_link_state_changes++;
957
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000958 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100959 netif_carrier_on(efx->net_dev);
960 else
961 netif_carrier_off(efx->net_dev);
962 }
963
964 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000965 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000966 netif_info(efx, link, efx->net_dev,
Ben Hutchings964e6132012-11-19 23:08:22 +0000967 "link up at %uMbps %s-duplex (MTU %d)\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000968 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings964e6132012-11-19 23:08:22 +0000969 efx->net_dev->mtu);
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000970 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000971 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100972}
973
Edward Creec2ab85d2018-01-10 18:00:14 +0000974void efx_link_set_advertising(struct efx_nic *efx,
975 const unsigned long *advertising)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000976{
Edward Creec2ab85d2018-01-10 18:00:14 +0000977 memcpy(efx->link_advertising, advertising,
978 sizeof(__ETHTOOL_DECLARE_LINK_MODE_MASK()));
979
980 efx->link_advertising[0] |= ADVERTISED_Autoneg;
981 if (advertising[0] & ADVERTISED_Pause)
982 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
983 else
984 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
985 if (advertising[0] & ADVERTISED_Asym_Pause)
986 efx->wanted_fc ^= EFX_FC_TX;
987}
988
989/* Equivalent to efx_link_set_advertising with all-zeroes, except does not
990 * force the Autoneg bit on.
991 */
992void efx_link_clear_advertising(struct efx_nic *efx)
993{
994 bitmap_zero(efx->link_advertising, __ETHTOOL_LINK_MODE_MASK_NBITS);
995 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000996}
997
David S. Millerb56269462011-05-17 17:53:22 -0400998void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000999{
1000 efx->wanted_fc = wanted_fc;
Edward Creec2ab85d2018-01-10 18:00:14 +00001001 if (efx->link_advertising[0]) {
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001002 if (wanted_fc & EFX_FC_RX)
Edward Creec2ab85d2018-01-10 18:00:14 +00001003 efx->link_advertising[0] |= (ADVERTISED_Pause |
1004 ADVERTISED_Asym_Pause);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001005 else
Edward Creec2ab85d2018-01-10 18:00:14 +00001006 efx->link_advertising[0] &= ~(ADVERTISED_Pause |
1007 ADVERTISED_Asym_Pause);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001008 if (wanted_fc & EFX_FC_TX)
Edward Creec2ab85d2018-01-10 18:00:14 +00001009 efx->link_advertising[0] ^= ADVERTISED_Asym_Pause;
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001010 }
1011}
1012
Ben Hutchings115122a2009-03-04 09:52:52 +00001013static void efx_fini_port(struct efx_nic *efx);
1014
Edward Cree0d322412015-05-20 11:10:03 +01001015/* We assume that efx->type->reconfigure_mac will always try to sync RX
1016 * filters and therefore needs to read-lock the filter table against freeing
1017 */
1018void efx_mac_reconfigure(struct efx_nic *efx)
1019{
1020 down_read(&efx->filter_sem);
1021 efx->type->reconfigure_mac(efx);
1022 up_read(&efx->filter_sem);
1023}
1024
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001025/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
1026 * the MAC appropriately. All other PHY configuration changes are pushed
1027 * through phy_op->set_settings(), and pushed asynchronously to the MAC
1028 * through efx_monitor().
1029 *
1030 * Callers must hold the mac_lock
1031 */
1032int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001033{
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001034 enum efx_phy_mode phy_mode;
1035 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001036
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001037 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001038
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001039 /* Disable PHY transmit in mac level loopbacks */
1040 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001041 if (LOOPBACK_INTERNAL(efx))
1042 efx->phy_mode |= PHY_MODE_TX_DISABLED;
1043 else
1044 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001045
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001046 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001047
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001048 if (rc)
1049 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001050
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001051 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001052}
1053
1054/* Reinitialise the MAC to pick up new PHY settings, even if the port is
1055 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001056int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001057{
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001058 int rc;
1059
Ben Hutchings8ceee662008-04-27 12:55:59 +01001060 EFX_ASSERT_RESET_SERIALISED(efx);
1061
1062 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001063 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001064 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001065
1066 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001067}
1068
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001069/* Asynchronous work item for changing MAC promiscuity and multicast
1070 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
1071 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001072static void efx_mac_work(struct work_struct *data)
1073{
1074 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
1075
1076 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +01001077 if (efx->port_enabled)
Edward Cree0d322412015-05-20 11:10:03 +01001078 efx_mac_reconfigure(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001079 mutex_unlock(&efx->mac_lock);
1080}
1081
Ben Hutchings8ceee662008-04-27 12:55:59 +01001082static int efx_probe_port(struct efx_nic *efx)
1083{
1084 int rc;
1085
Ben Hutchings62776d02010-06-23 11:30:07 +00001086 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001087
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001088 if (phy_flash_cfg)
1089 efx->phy_mode = PHY_MODE_SPECIAL;
1090
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001091 /* Connect up MAC/PHY operations table */
1092 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001093 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001094 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001095
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001096 /* Initialise MAC address to permanent address */
Edward Creecd84ff42014-03-07 18:27:41 +00001097 ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001098
1099 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001100}
1101
1102static int efx_init_port(struct efx_nic *efx)
1103{
1104 int rc;
1105
Ben Hutchings62776d02010-06-23 11:30:07 +00001106 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001107
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001108 mutex_lock(&efx->mac_lock);
1109
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001110 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001111 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001112 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001113
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001114 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001115
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001116 /* Reconfigure the MAC before creating dma queues (required for
1117 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Edward Cree0d322412015-05-20 11:10:03 +01001118 efx_mac_reconfigure(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001119
1120 /* Ensure the PHY advertises the correct flow control settings */
1121 rc = efx->phy_op->reconfigure(efx);
Edward Cree267d9d72015-05-06 00:59:18 +01001122 if (rc && rc != -EPERM)
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001123 goto fail2;
1124
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001125 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001126 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001127
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001128fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001129 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001130fail1:
1131 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001132 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001133}
1134
Ben Hutchings8ceee662008-04-27 12:55:59 +01001135static void efx_start_port(struct efx_nic *efx)
1136{
Ben Hutchings62776d02010-06-23 11:30:07 +00001137 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001138 BUG_ON(efx->port_enabled);
1139
1140 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001141 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001142
Ben Hutchingsd615c032013-10-08 17:33:20 +01001143 /* Ensure MAC ingress/egress is enabled */
Edward Cree0d322412015-05-20 11:10:03 +01001144 efx_mac_reconfigure(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001145
Ben Hutchings8ceee662008-04-27 12:55:59 +01001146 mutex_unlock(&efx->mac_lock);
1147}
1148
Ben Hutchingsd615c032013-10-08 17:33:20 +01001149/* Cancel work for MAC reconfiguration, periodic hardware monitoring
1150 * and the async self-test, wait for them to finish and prevent them
1151 * being scheduled again. This doesn't cover online resets, which
1152 * should only be cancelled when removing the device.
1153 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001154static void efx_stop_port(struct efx_nic *efx)
1155{
Ben Hutchings62776d02010-06-23 11:30:07 +00001156 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001157
Ben Hutchingsd615c032013-10-08 17:33:20 +01001158 EFX_ASSERT_RESET_SERIALISED(efx);
1159
Ben Hutchings8ceee662008-04-27 12:55:59 +01001160 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001161 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001162 mutex_unlock(&efx->mac_lock);
1163
1164 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001165 netif_addr_lock_bh(efx->net_dev);
1166 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsd615c032013-10-08 17:33:20 +01001167
1168 cancel_delayed_work_sync(&efx->monitor_work);
1169 efx_selftest_async_cancel(efx);
1170 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001171}
1172
1173static void efx_fini_port(struct efx_nic *efx)
1174{
Ben Hutchings62776d02010-06-23 11:30:07 +00001175 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001176
1177 if (!efx->port_initialized)
1178 return;
1179
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001180 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001181 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001182
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001183 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001184 efx_link_status_changed(efx);
1185}
1186
1187static void efx_remove_port(struct efx_nic *efx)
1188{
Ben Hutchings62776d02010-06-23 11:30:07 +00001189 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001190
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001191 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001192}
1193
1194/**************************************************************************
1195 *
1196 * NIC handling
1197 *
1198 **************************************************************************/
1199
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01001200static LIST_HEAD(efx_primary_list);
1201static LIST_HEAD(efx_unassociated_list);
1202
1203static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
1204{
1205 return left->type == right->type &&
1206 left->vpd_sn && right->vpd_sn &&
1207 !strcmp(left->vpd_sn, right->vpd_sn);
1208}
1209
1210static void efx_associate(struct efx_nic *efx)
1211{
1212 struct efx_nic *other, *next;
1213
1214 if (efx->primary == efx) {
1215 /* Adding primary function; look for secondaries */
1216
1217 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
1218 list_add_tail(&efx->node, &efx_primary_list);
1219
1220 list_for_each_entry_safe(other, next, &efx_unassociated_list,
1221 node) {
1222 if (efx_same_controller(efx, other)) {
1223 list_del(&other->node);
1224 netif_dbg(other, probe, other->net_dev,
1225 "moving to secondary list of %s %s\n",
1226 pci_name(efx->pci_dev),
1227 efx->net_dev->name);
1228 list_add_tail(&other->node,
1229 &efx->secondary_list);
1230 other->primary = efx;
1231 }
1232 }
1233 } else {
1234 /* Adding secondary function; look for primary */
1235
1236 list_for_each_entry(other, &efx_primary_list, node) {
1237 if (efx_same_controller(efx, other)) {
1238 netif_dbg(efx, probe, efx->net_dev,
1239 "adding to secondary list of %s %s\n",
1240 pci_name(other->pci_dev),
1241 other->net_dev->name);
1242 list_add_tail(&efx->node,
1243 &other->secondary_list);
1244 efx->primary = other;
1245 return;
1246 }
1247 }
1248
1249 netif_dbg(efx, probe, efx->net_dev,
1250 "adding to unassociated list\n");
1251 list_add_tail(&efx->node, &efx_unassociated_list);
1252 }
1253}
1254
1255static void efx_dissociate(struct efx_nic *efx)
1256{
1257 struct efx_nic *other, *next;
1258
1259 list_del(&efx->node);
1260 efx->primary = NULL;
1261
1262 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
1263 list_del(&other->node);
1264 netif_dbg(other, probe, other->net_dev,
1265 "moving to unassociated list\n");
1266 list_add_tail(&other->node, &efx_unassociated_list);
1267 other->primary = NULL;
1268 }
1269}
1270
Ben Hutchings8ceee662008-04-27 12:55:59 +01001271/* This configures the PCI device to enable I/O and DMA. */
1272static int efx_init_io(struct efx_nic *efx)
1273{
1274 struct pci_dev *pci_dev = efx->pci_dev;
1275 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001276 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Shradha Shah02246a72015-05-06 00:58:14 +01001277 int rc, bar;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001278
Ben Hutchings62776d02010-06-23 11:30:07 +00001279 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001280
Edward Cree03714bb2017-12-18 16:55:50 +00001281 bar = efx->type->mem_bar(efx);
Shradha Shah02246a72015-05-06 00:58:14 +01001282
Ben Hutchings8ceee662008-04-27 12:55:59 +01001283 rc = pci_enable_device(pci_dev);
1284 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001285 netif_err(efx, probe, efx->net_dev,
1286 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001287 goto fail1;
1288 }
1289
1290 pci_set_master(pci_dev);
1291
Christoph Hellwig06e95522018-04-27 09:13:24 +02001292 /* Set the PCI DMA mask. Try all possibilities from our genuine mask
1293 * down to 32 bits, because some architectures will allow 40 bit
Ben Hutchings8ceee662008-04-27 12:55:59 +01001294 * masks event though they reject 46 bit masks.
1295 */
1296 while (dma_mask > 0x7fffffffUL) {
Christoph Hellwig8722b8f2015-11-10 14:45:42 -08001297 rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
1298 if (rc == 0)
1299 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001300 dma_mask >>= 1;
1301 }
1302 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001303 netif_err(efx, probe, efx->net_dev,
1304 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001305 goto fail2;
1306 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001307 netif_dbg(efx, probe, efx->net_dev,
1308 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001309
Shradha Shah02246a72015-05-06 00:58:14 +01001310 efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
1311 rc = pci_request_region(pci_dev, bar, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001312 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001313 netif_err(efx, probe, efx->net_dev,
1314 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001315 rc = -EIO;
1316 goto fail3;
1317 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001318 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001319 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001320 netif_err(efx, probe, efx->net_dev,
1321 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001322 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001323 rc = -ENOMEM;
1324 goto fail4;
1325 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001326 netif_dbg(efx, probe, efx->net_dev,
1327 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001328 (unsigned long long)efx->membase_phys, mem_map_size,
1329 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001330
1331 return 0;
1332
1333 fail4:
Shradha Shah02246a72015-05-06 00:58:14 +01001334 pci_release_region(efx->pci_dev, bar);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001335 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001336 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001337 fail2:
1338 pci_disable_device(efx->pci_dev);
1339 fail1:
1340 return rc;
1341}
1342
1343static void efx_fini_io(struct efx_nic *efx)
1344{
Shradha Shah02246a72015-05-06 00:58:14 +01001345 int bar;
1346
Ben Hutchings62776d02010-06-23 11:30:07 +00001347 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001348
1349 if (efx->membase) {
1350 iounmap(efx->membase);
1351 efx->membase = NULL;
1352 }
1353
1354 if (efx->membase_phys) {
Edward Cree03714bb2017-12-18 16:55:50 +00001355 bar = efx->type->mem_bar(efx);
Shradha Shah02246a72015-05-06 00:58:14 +01001356 pci_release_region(efx->pci_dev, bar);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001357 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001358 }
1359
Daniel Pieczko6598dad2015-06-02 11:41:00 +01001360 /* Don't disable bus-mastering if VFs are assigned */
1361 if (!pci_vfs_assigned(efx->pci_dev))
1362 pci_disable_device(efx->pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001363}
1364
Edward Cree42356d92018-03-08 15:45:17 +00001365void efx_set_default_rx_indir_table(struct efx_nic *efx,
1366 struct efx_rss_context *ctx)
Jon Cooper267c0152015-05-06 00:59:38 +01001367{
1368 size_t i;
1369
Edward Cree42356d92018-03-08 15:45:17 +00001370 for (i = 0; i < ARRAY_SIZE(ctx->rx_indir_table); i++)
1371 ctx->rx_indir_table[i] =
Jon Cooper267c0152015-05-06 00:59:38 +01001372 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001373}
1374
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001375static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001376{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001377 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001378 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001379 int cpu;
1380
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001381 if (rss_cpus) {
1382 count = rss_cpus;
1383 } else {
1384 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1385 netif_warn(efx, probe, efx->net_dev,
1386 "RSS disabled due to allocation failure\n");
1387 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001388 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001389
1390 count = 0;
1391 for_each_online_cpu(cpu) {
1392 if (!cpumask_test_cpu(cpu, thread_mask)) {
1393 ++count;
1394 cpumask_or(thread_mask, thread_mask,
Bartosz Golaszewski06931e62015-05-26 15:11:28 +02001395 topology_sibling_cpumask(cpu));
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001396 }
1397 }
1398
1399 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001400 }
1401
Bert Kenward271a8b42017-04-12 17:06:52 +01001402 if (count > EFX_MAX_RX_QUEUES) {
1403 netif_cond_dbg(efx, probe, efx->net_dev, !rss_cpus, warn,
1404 "Reducing number of rx queues from %u to %u.\n",
1405 count, EFX_MAX_RX_QUEUES);
1406 count = EFX_MAX_RX_QUEUES;
1407 }
1408
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001409 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1410 * table entries that are inaccessible to VFs
1411 */
Shradha Shah7fa8d542015-05-06 00:55:13 +01001412#ifdef CONFIG_SFC_SRIOV
1413 if (efx->type->sriov_wanted) {
1414 if (efx->type->sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1415 count > efx_vf_size(efx)) {
1416 netif_warn(efx, probe, efx->net_dev,
1417 "Reducing number of RSS channels from %u to %u for "
1418 "VF support. Increase vf-msix-limit to use more "
1419 "channels on the PF.\n",
1420 count, efx_vf_size(efx));
1421 count = efx_vf_size(efx);
1422 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001423 }
Shradha Shah7fa8d542015-05-06 00:55:13 +01001424#endif
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001425
Ben Hutchings46123d02008-09-01 12:47:33 +01001426 return count;
1427}
1428
1429/* Probe the number and type of interrupts we are able to obtain, and
1430 * the resulting numbers of channels and RX queues.
1431 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001432static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001433{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001434 unsigned int extra_channels = 0;
1435 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001436 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001437
Ben Hutchings7f967c02012-02-13 23:45:02 +00001438 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1439 if (efx->extra_channel_type[i])
1440 ++extra_channels;
1441
Ben Hutchings8ceee662008-04-27 12:55:59 +01001442 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001443 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001444 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001445
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001446 n_channels = efx_wanted_parallelism(efx);
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001447 if (efx_separate_tx_channels)
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001448 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001449 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001450 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001451
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001452 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001453 xentries[i].entry = i;
Alexander Gordeev184603d2014-02-18 11:12:00 +01001454 rc = pci_enable_msix_range(efx->pci_dev,
1455 xentries, 1, n_channels);
1456 if (rc < 0) {
1457 /* Fall back to single channel MSI */
Alexander Gordeev184603d2014-02-18 11:12:00 +01001458 netif_err(efx, drv, efx->net_dev,
1459 "could not enable MSI-X\n");
Andrew Rybchenko62980cb2017-02-13 14:59:04 +00001460 if (efx->type->min_interrupt_mode >= EFX_INT_MODE_MSI)
1461 efx->interrupt_mode = EFX_INT_MODE_MSI;
1462 else
1463 return rc;
Alexander Gordeev184603d2014-02-18 11:12:00 +01001464 } else if (rc < n_channels) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001465 netif_err(efx, drv, efx->net_dev,
1466 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001467 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001468 netif_err(efx, drv, efx->net_dev,
1469 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001470 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001471 }
1472
Alexander Gordeev184603d2014-02-18 11:12:00 +01001473 if (rc > 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001474 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001475 if (n_channels > extra_channels)
1476 n_channels -= extra_channels;
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001477 if (efx_separate_tx_channels) {
1478 efx->n_tx_channels = min(max(n_channels / 2,
1479 1U),
1480 efx->max_tx_channels);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001481 efx->n_rx_channels = max(n_channels -
1482 efx->n_tx_channels,
1483 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001484 } else {
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001485 efx->n_tx_channels = min(n_channels,
1486 efx->max_tx_channels);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001487 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001488 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001489 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001490 efx_get_channel(efx, i)->irq =
1491 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001492 }
1493 }
1494
1495 /* Try single interrupt MSI */
1496 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001497 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001498 efx->n_rx_channels = 1;
1499 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001500 rc = pci_enable_msi(efx->pci_dev);
1501 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001502 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001503 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001504 netif_err(efx, drv, efx->net_dev,
1505 "could not enable MSI\n");
Andrew Rybchenko62980cb2017-02-13 14:59:04 +00001506 if (efx->type->min_interrupt_mode >= EFX_INT_MODE_LEGACY)
1507 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1508 else
1509 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001510 }
1511 }
1512
1513 /* Assume legacy interrupts */
1514 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001515 efx->n_channels = 1 + (efx_separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001516 efx->n_rx_channels = 1;
1517 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001518 efx->legacy_irq = efx->pci_dev->irq;
1519 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001520
Ben Hutchings7f967c02012-02-13 23:45:02 +00001521 /* Assign extra channels if possible */
Edward Cree2935e3c2018-01-25 17:26:06 +00001522 efx->n_extra_tx_channels = 0;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001523 j = efx->n_channels;
1524 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1525 if (!efx->extra_channel_type[i])
1526 continue;
1527 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1528 efx->n_channels <= extra_channels) {
1529 efx->extra_channel_type[i]->handle_no_channel(efx);
1530 } else {
1531 --j;
1532 efx_get_channel(efx, j)->type =
1533 efx->extra_channel_type[i];
Edward Cree2935e3c2018-01-25 17:26:06 +00001534 if (efx_channel_has_tx_queues(efx_get_channel(efx, j)))
1535 efx->n_extra_tx_channels++;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001536 }
1537 }
1538
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001539 /* RSS might be usable on VFs even if it is disabled on the PF */
Shradha Shah7fa8d542015-05-06 00:55:13 +01001540#ifdef CONFIG_SFC_SRIOV
1541 if (efx->type->sriov_wanted) {
1542 efx->rss_spread = ((efx->n_rx_channels > 1 ||
1543 !efx->type->sriov_wanted(efx)) ?
1544 efx->n_rx_channels : efx_vf_size(efx));
1545 return 0;
1546 }
1547#endif
1548 efx->rss_spread = efx->n_rx_channels;
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001549
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001550 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001551}
1552
Jon Cooper261e4d92013-04-15 18:51:54 +01001553static int efx_soft_enable_interrupts(struct efx_nic *efx)
Ben Hutchingsd8291182012-10-05 23:35:41 +01001554{
Jon Cooper261e4d92013-04-15 18:51:54 +01001555 struct efx_channel *channel, *end_channel;
1556 int rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001557
1558 BUG_ON(efx->state == STATE_DISABLED);
1559
1560 efx->irq_soft_enabled = true;
1561 smp_wmb();
1562
1563 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001564 if (!channel->type->keep_eventq) {
1565 rc = efx_init_eventq(channel);
1566 if (rc)
1567 goto fail;
1568 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001569 efx_start_eventq(channel);
1570 }
1571
1572 efx_mcdi_mode_event(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +01001573
1574 return 0;
1575fail:
1576 end_channel = channel;
1577 efx_for_each_channel(channel, efx) {
1578 if (channel == end_channel)
1579 break;
1580 efx_stop_eventq(channel);
1581 if (!channel->type->keep_eventq)
1582 efx_fini_eventq(channel);
1583 }
1584
1585 return rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001586}
1587
1588static void efx_soft_disable_interrupts(struct efx_nic *efx)
1589{
1590 struct efx_channel *channel;
1591
1592 if (efx->state == STATE_DISABLED)
1593 return;
1594
1595 efx_mcdi_mode_poll(efx);
1596
1597 efx->irq_soft_enabled = false;
1598 smp_wmb();
1599
1600 if (efx->legacy_irq)
1601 synchronize_irq(efx->legacy_irq);
1602
1603 efx_for_each_channel(channel, efx) {
1604 if (channel->irq)
1605 synchronize_irq(channel->irq);
1606
1607 efx_stop_eventq(channel);
1608 if (!channel->type->keep_eventq)
1609 efx_fini_eventq(channel);
1610 }
Ben Hutchingscade7152013-08-27 23:12:31 +01001611
1612 /* Flush the asynchronous MCDI request queue */
1613 efx_mcdi_flush_async(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01001614}
1615
Jon Cooper261e4d92013-04-15 18:51:54 +01001616static int efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001617{
Jon Cooper261e4d92013-04-15 18:51:54 +01001618 struct efx_channel *channel, *end_channel;
1619 int rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001620
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001621 BUG_ON(efx->state == STATE_DISABLED);
1622
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001623 if (efx->eeh_disabled_legacy_irq) {
1624 enable_irq(efx->legacy_irq);
1625 efx->eeh_disabled_legacy_irq = false;
1626 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001627
Ben Hutchings86094f72013-08-21 19:51:04 +01001628 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001629
1630 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001631 if (channel->type->keep_eventq) {
1632 rc = efx_init_eventq(channel);
1633 if (rc)
1634 goto fail;
1635 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001636 }
1637
Jon Cooper261e4d92013-04-15 18:51:54 +01001638 rc = efx_soft_enable_interrupts(efx);
1639 if (rc)
1640 goto fail;
1641
1642 return 0;
1643
1644fail:
1645 end_channel = channel;
1646 efx_for_each_channel(channel, efx) {
1647 if (channel == end_channel)
1648 break;
1649 if (channel->type->keep_eventq)
1650 efx_fini_eventq(channel);
1651 }
1652
1653 efx->type->irq_disable_non_ev(efx);
1654
1655 return rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001656}
1657
Ben Hutchingsd8291182012-10-05 23:35:41 +01001658static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001659{
1660 struct efx_channel *channel;
1661
Ben Hutchingsd8291182012-10-05 23:35:41 +01001662 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001663
1664 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001665 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001666 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001667 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001668
Ben Hutchings86094f72013-08-21 19:51:04 +01001669 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001670}
1671
Ben Hutchings8ceee662008-04-27 12:55:59 +01001672static void efx_remove_interrupts(struct efx_nic *efx)
1673{
1674 struct efx_channel *channel;
1675
1676 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001677 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001678 channel->irq = 0;
1679 pci_disable_msi(efx->pci_dev);
1680 pci_disable_msix(efx->pci_dev);
1681
1682 /* Remove legacy interrupt */
1683 efx->legacy_irq = 0;
1684}
1685
Ben Hutchings8831da72008-09-01 12:47:48 +01001686static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001687{
Ben Hutchings602a5322011-05-16 17:32:39 +01001688 struct efx_channel *channel;
1689 struct efx_tx_queue *tx_queue;
1690
Ben Hutchings97653432011-01-12 18:26:56 +00001691 efx->tx_channel_offset =
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001692 efx_separate_tx_channels ?
1693 efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001694
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001695 /* We need to mark which channels really have RX and TX
1696 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001697 * RX-only and TX-only channels.
1698 */
1699 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001700 if (channel->channel < efx->n_rx_channels)
1701 channel->rx_queue.core_index = channel->channel;
1702 else
1703 channel->rx_queue.core_index = -1;
1704
Ben Hutchings602a5322011-05-16 17:32:39 +01001705 efx_for_each_channel_tx_queue(tx_queue, channel)
1706 tx_queue->queue -= (efx->tx_channel_offset *
1707 EFX_TXQ_TYPES);
1708 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001709}
1710
1711static int efx_probe_nic(struct efx_nic *efx)
1712{
1713 int rc;
1714
Ben Hutchings62776d02010-06-23 11:30:07 +00001715 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001716
1717 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001718 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001719 if (rc)
1720 return rc;
1721
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001722 do {
1723 if (!efx->max_channels || !efx->max_tx_channels) {
1724 netif_err(efx, drv, efx->net_dev,
1725 "Insufficient resources to allocate"
1726 " any channels\n");
1727 rc = -ENOSPC;
1728 goto fail1;
1729 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001730
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001731 /* Determine the number of channels and queues by trying
1732 * to hook in MSI-X interrupts.
1733 */
1734 rc = efx_probe_interrupts(efx);
1735 if (rc)
1736 goto fail1;
Daniel Pieczko52ad7622014-04-01 13:10:34 +01001737
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001738 efx_set_channels(efx);
1739
1740 /* dimension_resources can fail with EAGAIN */
1741 rc = efx->type->dimension_resources(efx);
1742 if (rc != 0 && rc != -EAGAIN)
1743 goto fail2;
1744
1745 if (rc == -EAGAIN)
1746 /* try again with new max_channels */
1747 efx_remove_interrupts(efx);
1748
1749 } while (rc == -EAGAIN);
Ben Hutchings28e47c42012-02-15 01:58:49 +00001750
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001751 if (efx->n_channels > 1)
Edward Cree42356d92018-03-08 15:45:17 +00001752 netdev_rss_key_fill(efx->rss_context.rx_hash_key,
1753 sizeof(efx->rss_context.rx_hash_key));
1754 efx_set_default_rx_indir_table(efx, &efx->rss_context);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001755
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001756 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1757 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001758
1759 /* Initialise the interrupt moderation settings */
Bert Kenward539de7c2016-08-11 13:02:09 +01001760 efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
Ben Hutchings9e393b32011-09-05 07:43:04 +00001761 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1762 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001763
1764 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001765
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001766fail2:
1767 efx_remove_interrupts(efx);
1768fail1:
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001769 efx->type->remove(efx);
1770 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001771}
1772
1773static void efx_remove_nic(struct efx_nic *efx)
1774{
Ben Hutchings62776d02010-06-23 11:30:07 +00001775 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001776
1777 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001778 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001779}
1780
Ben Hutchingsadd72472012-11-08 01:46:53 +00001781static int efx_probe_filters(struct efx_nic *efx)
1782{
1783 int rc;
1784
Edward Cree0d322412015-05-20 11:10:03 +01001785 init_rwsem(&efx->filter_sem);
Martin Habetsd2489532016-06-15 17:48:49 +01001786 mutex_lock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01001787 down_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001788 rc = efx->type->filter_table_probe(efx);
1789 if (rc)
Edward Cree0d322412015-05-20 11:10:03 +01001790 goto out_unlock;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001791
1792#ifdef CONFIG_RFS_ACCEL
1793 if (efx->type->offload_features & NETIF_F_NTUPLE) {
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001794 struct efx_channel *channel;
1795 int i, success = 1;
1796
1797 efx_for_each_channel(channel, efx) {
1798 channel->rps_flow_id =
1799 kcalloc(efx->type->max_rx_ip_filters,
1800 sizeof(*channel->rps_flow_id),
1801 GFP_KERNEL);
1802 if (!channel->rps_flow_id)
1803 success = 0;
1804 else
1805 for (i = 0;
1806 i < efx->type->max_rx_ip_filters;
1807 ++i)
1808 channel->rps_flow_id[i] =
1809 RPS_FLOW_ID_INVALID;
1810 }
1811
1812 if (!success) {
1813 efx_for_each_channel(channel, efx)
1814 kfree(channel->rps_flow_id);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001815 efx->type->filter_table_remove(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001816 rc = -ENOMEM;
1817 goto out_unlock;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001818 }
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001819
1820 efx->rps_expire_index = efx->rps_expire_channel = 0;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001821 }
1822#endif
Edward Cree0d322412015-05-20 11:10:03 +01001823out_unlock:
1824 up_write(&efx->filter_sem);
Martin Habetsd2489532016-06-15 17:48:49 +01001825 mutex_unlock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01001826 return rc;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001827}
1828
1829static void efx_remove_filters(struct efx_nic *efx)
1830{
1831#ifdef CONFIG_RFS_ACCEL
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001832 struct efx_channel *channel;
1833
1834 efx_for_each_channel(channel, efx)
1835 kfree(channel->rps_flow_id);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001836#endif
Edward Cree0d322412015-05-20 11:10:03 +01001837 down_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001838 efx->type->filter_table_remove(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001839 up_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001840}
1841
1842static void efx_restore_filters(struct efx_nic *efx)
1843{
Edward Cree0d322412015-05-20 11:10:03 +01001844 down_read(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001845 efx->type->filter_table_restore(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001846 up_read(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001847}
1848
Ben Hutchings8ceee662008-04-27 12:55:59 +01001849/**************************************************************************
1850 *
1851 * NIC startup/shutdown
1852 *
1853 *************************************************************************/
1854
1855static int efx_probe_all(struct efx_nic *efx)
1856{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001857 int rc;
1858
Ben Hutchings8ceee662008-04-27 12:55:59 +01001859 rc = efx_probe_nic(efx);
1860 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001861 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001862 goto fail1;
1863 }
1864
Ben Hutchings8ceee662008-04-27 12:55:59 +01001865 rc = efx_probe_port(efx);
1866 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001867 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001868 goto fail2;
1869 }
1870
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001871 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1872 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1873 rc = -EINVAL;
1874 goto fail3;
1875 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001876 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001877
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001878#ifdef CONFIG_SFC_SRIOV
1879 rc = efx->type->vswitching_probe(efx);
1880 if (rc) /* not fatal; the PF will still work fine */
1881 netif_warn(efx, probe, efx->net_dev,
1882 "failed to setup vswitching rc=%d;"
1883 " VFs may not function\n", rc);
1884#endif
1885
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001886 rc = efx_probe_filters(efx);
1887 if (rc) {
1888 netif_err(efx, probe, efx->net_dev,
1889 "failed to create filter tables\n");
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001890 goto fail4;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001891 }
1892
Ben Hutchings7f967c02012-02-13 23:45:02 +00001893 rc = efx_probe_channels(efx);
1894 if (rc)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001895 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001896
Ben Hutchings8ceee662008-04-27 12:55:59 +01001897 return 0;
1898
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001899 fail5:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001900 efx_remove_filters(efx);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001901 fail4:
1902#ifdef CONFIG_SFC_SRIOV
1903 efx->type->vswitching_remove(efx);
1904#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01001905 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001906 efx_remove_port(efx);
1907 fail2:
1908 efx_remove_nic(efx);
1909 fail1:
1910 return rc;
1911}
1912
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001913/* If the interface is supposed to be running but is not, start
1914 * the hardware and software data path, regular activity for the port
1915 * (MAC statistics, link polling, etc.) and schedule the port to be
1916 * reconfigured. Interrupts must already be enabled. This function
1917 * is safe to call multiple times, so long as the NIC is not disabled.
1918 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001919 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001920static void efx_start_all(struct efx_nic *efx)
1921{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001922 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001923 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001924
1925 /* Check that it is appropriate to restart the interface. All
1926 * of these flags are safe to read under just the rtnl lock */
Edward Creee2835462014-04-16 19:27:48 +01001927 if (efx->port_enabled || !netif_running(efx->net_dev) ||
1928 efx->reset_pending)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001929 return;
1930
Ben Hutchings8ceee662008-04-27 12:55:59 +01001931 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001932 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001933
Alexandre Rames626950d2013-01-14 17:20:22 +00001934 /* Start the hardware monitor if there is one */
1935 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001936 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1937 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001938
Edward Cree5a6681e2016-11-28 18:55:34 +00001939 /* Link state detection is normally event-driven; we have
Alexandre Rames626950d2013-01-14 17:20:22 +00001940 * to poll now because we could have missed a change
1941 */
Edward Cree5a6681e2016-11-28 18:55:34 +00001942 mutex_lock(&efx->mac_lock);
1943 if (efx->phy_op->poll(efx))
1944 efx_link_status_changed(efx);
1945 mutex_unlock(&efx->mac_lock);
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001946
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001947 efx->type->start_stats(efx);
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001948 efx->type->pull_stats(efx);
1949 spin_lock_bh(&efx->stats_lock);
1950 efx->type->update_stats(efx, NULL, NULL);
1951 spin_unlock_bh(&efx->stats_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001952}
1953
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001954/* Quiesce the hardware and software data path, and regular activity
1955 * for the port without bringing the link down. Safe to call multiple
1956 * times with the NIC in almost any state, but interrupts should be
1957 * enabled. Requires the RTNL lock.
1958 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001959static void efx_stop_all(struct efx_nic *efx)
1960{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001961 EFX_ASSERT_RESET_SERIALISED(efx);
1962
1963 /* port_enabled can be read safely under the rtnl lock */
1964 if (!efx->port_enabled)
1965 return;
1966
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001967 /* update stats before we go down so we can accurately count
1968 * rx_nodesc_drops
1969 */
1970 efx->type->pull_stats(efx);
1971 spin_lock_bh(&efx->stats_lock);
1972 efx->type->update_stats(efx, NULL, NULL);
1973 spin_unlock_bh(&efx->stats_lock);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001974 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001975 efx_stop_port(efx);
1976
Ben Hutchings29c69a42013-01-28 19:01:06 +00001977 /* Stop the kernel transmit interface. This is only valid if
1978 * the device is stopped or detached; otherwise the watchdog
1979 * may fire immediately.
1980 */
1981 WARN_ON(netif_running(efx->net_dev) &&
1982 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001983 netif_tx_disable(efx->net_dev);
1984
1985 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001986}
1987
1988static void efx_remove_all(struct efx_nic *efx)
1989{
Ben Hutchings46426102010-09-10 06:42:33 +00001990 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001991 efx_remove_filters(efx);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001992#ifdef CONFIG_SFC_SRIOV
1993 efx->type->vswitching_remove(efx);
1994#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01001995 efx_remove_port(efx);
1996 efx_remove_nic(efx);
1997}
1998
Ben Hutchings8ceee662008-04-27 12:55:59 +01001999/**************************************************************************
2000 *
2001 * Interrupt moderation
2002 *
2003 **************************************************************************/
Bert Kenward539de7c2016-08-11 13:02:09 +01002004unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00002005{
Ben Hutchingsb548f972011-09-05 07:41:44 +00002006 if (usecs == 0)
2007 return 0;
Bert Kenward539de7c2016-08-11 13:02:09 +01002008 if (usecs * 1000 < efx->timer_quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00002009 return 1; /* never round down to 0 */
Bert Kenward539de7c2016-08-11 13:02:09 +01002010 return usecs * 1000 / efx->timer_quantum_ns;
2011}
2012
2013unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
2014{
2015 /* We must round up when converting ticks to microseconds
2016 * because we round down when converting the other way.
2017 */
2018 return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00002019}
2020
Ben Hutchings8ceee662008-04-27 12:55:59 +01002021/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00002022int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
2023 unsigned int rx_usecs, bool rx_adaptive,
2024 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002025{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002026 struct efx_channel *channel;
Bert Kenwardd95e3292016-08-11 13:02:36 +01002027 unsigned int timer_max_us;
2028
Ben Hutchings8ceee662008-04-27 12:55:59 +01002029 EFX_ASSERT_RESET_SERIALISED(efx);
2030
Bert Kenwardd95e3292016-08-11 13:02:36 +01002031 timer_max_us = efx->timer_max_ns / 1000;
2032
2033 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
Ben Hutchings9e393b32011-09-05 07:43:04 +00002034 return -EINVAL;
2035
Bert Kenward539de7c2016-08-11 13:02:09 +01002036 if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
Ben Hutchings9e393b32011-09-05 07:43:04 +00002037 !rx_may_override_tx) {
2038 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
2039 "RX and TX IRQ moderation must be equal\n");
2040 return -EINVAL;
2041 }
2042
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00002043 efx->irq_rx_adaptive = rx_adaptive;
Bert Kenward539de7c2016-08-11 13:02:09 +01002044 efx->irq_rx_moderation_us = rx_usecs;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002045 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00002046 if (efx_channel_has_rx_queue(channel))
Bert Kenward539de7c2016-08-11 13:02:09 +01002047 channel->irq_moderation_us = rx_usecs;
Ben Hutchings525da902011-02-07 23:04:38 +00002048 else if (efx_channel_has_tx_queues(channel))
Bert Kenward539de7c2016-08-11 13:02:09 +01002049 channel->irq_moderation_us = tx_usecs;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002050 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00002051
2052 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002053}
2054
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002055void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
2056 unsigned int *rx_usecs, bool *rx_adaptive)
2057{
2058 *rx_adaptive = efx->irq_rx_adaptive;
Bert Kenward539de7c2016-08-11 13:02:09 +01002059 *rx_usecs = efx->irq_rx_moderation_us;
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002060
2061 /* If channels are shared between RX and TX, so is IRQ
2062 * moderation. Otherwise, IRQ moderation is the same for all
2063 * TX channels and is not adaptive.
2064 */
Bert Kenward539de7c2016-08-11 13:02:09 +01002065 if (efx->tx_channel_offset == 0) {
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002066 *tx_usecs = *rx_usecs;
Bert Kenward539de7c2016-08-11 13:02:09 +01002067 } else {
2068 struct efx_channel *tx_channel;
2069
2070 tx_channel = efx->channel[efx->tx_channel_offset];
2071 *tx_usecs = tx_channel->irq_moderation_us;
2072 }
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002073}
2074
Ben Hutchings8ceee662008-04-27 12:55:59 +01002075/**************************************************************************
2076 *
2077 * Hardware monitor
2078 *
2079 **************************************************************************/
2080
Ben Hutchingse254c272010-09-20 08:44:10 +00002081/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002082static void efx_monitor(struct work_struct *data)
2083{
2084 struct efx_nic *efx = container_of(data, struct efx_nic,
2085 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002086
Ben Hutchings62776d02010-06-23 11:30:07 +00002087 netif_vdbg(efx, timer, efx->net_dev,
2088 "hardware monitor executing on CPU %d\n",
2089 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002090 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002091
Ben Hutchings8ceee662008-04-27 12:55:59 +01002092 /* If the mac_lock is already held then it is likely a port
2093 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00002094 * most of the work of monitor() anyway. */
2095 if (mutex_trylock(&efx->mac_lock)) {
2096 if (efx->port_enabled)
2097 efx->type->monitor(efx);
2098 mutex_unlock(&efx->mac_lock);
2099 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002100
Ben Hutchings8ceee662008-04-27 12:55:59 +01002101 queue_delayed_work(efx->workqueue, &efx->monitor_work,
2102 efx_monitor_interval);
2103}
2104
2105/**************************************************************************
2106 *
2107 * ioctls
2108 *
2109 *************************************************************************/
2110
2111/* Net device ioctl
2112 * Context: process, rtnl_lock() held.
2113 */
2114static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
2115{
Ben Hutchings767e4682008-09-01 12:43:14 +01002116 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00002117 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002118
Stuart Hodgson7c236c42012-09-03 11:09:36 +01002119 if (cmd == SIOCSHWTSTAMP)
Ben Hutchings433dc9b2013-11-14 01:26:21 +00002120 return efx_ptp_set_ts_config(efx, ifr);
2121 if (cmd == SIOCGHWTSTAMP)
2122 return efx_ptp_get_ts_config(efx, ifr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01002123
Ben Hutchings68e7f452009-04-29 08:05:08 +00002124 /* Convert phy_id from older PRTAD/DEVAD format */
2125 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
2126 (data->phy_id & 0xfc00) == 0x0400)
2127 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
2128
2129 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002130}
2131
2132/**************************************************************************
2133 *
2134 * NAPI interface
2135 *
2136 **************************************************************************/
2137
Ben Hutchings7f967c02012-02-13 23:45:02 +00002138static void efx_init_napi_channel(struct efx_channel *channel)
2139{
2140 struct efx_nic *efx = channel->efx;
2141
2142 channel->napi_dev = efx->net_dev;
2143 netif_napi_add(channel->napi_dev, &channel->napi_str,
2144 efx_poll, napi_weight);
2145}
2146
Ben Hutchingse8f14992010-12-07 19:47:34 +00002147static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002148{
2149 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002150
Ben Hutchings7f967c02012-02-13 23:45:02 +00002151 efx_for_each_channel(channel, efx)
2152 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00002153}
2154
2155static void efx_fini_napi_channel(struct efx_channel *channel)
2156{
Eric Dumazet973334a2016-11-16 06:01:47 -08002157 if (channel->napi_dev)
Ben Hutchingse8f14992010-12-07 19:47:34 +00002158 netif_napi_del(&channel->napi_str);
Eric Dumazet973334a2016-11-16 06:01:47 -08002159
Ben Hutchingse8f14992010-12-07 19:47:34 +00002160 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002161}
2162
2163static void efx_fini_napi(struct efx_nic *efx)
2164{
2165 struct efx_channel *channel;
2166
Ben Hutchingse8f14992010-12-07 19:47:34 +00002167 efx_for_each_channel(channel, efx)
2168 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002169}
2170
2171/**************************************************************************
2172 *
2173 * Kernel netpoll interface
2174 *
2175 *************************************************************************/
2176
2177#ifdef CONFIG_NET_POLL_CONTROLLER
2178
2179/* Although in the common case interrupts will be disabled, this is not
2180 * guaranteed. However, all our work happens inside the NAPI callback,
2181 * so no locking is required.
2182 */
2183static void efx_netpoll(struct net_device *net_dev)
2184{
Ben Hutchings767e4682008-09-01 12:43:14 +01002185 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002186 struct efx_channel *channel;
2187
Ben Hutchings64ee3122008-09-01 12:47:38 +01002188 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002189 efx_schedule_channel(channel);
2190}
2191
2192#endif
2193
2194/**************************************************************************
2195 *
2196 * Kernel net device interface
2197 *
2198 *************************************************************************/
2199
2200/* Context: process, rtnl_lock() held. */
Shradha Shahe340be92015-05-20 11:11:03 +01002201int efx_net_open(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002202{
Ben Hutchings767e4682008-09-01 12:43:14 +01002203 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002204 int rc;
2205
Ben Hutchings62776d02010-06-23 11:30:07 +00002206 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
2207 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01002208
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002209 rc = efx_check_disabled(efx);
2210 if (rc)
2211 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01002212 if (efx->phy_mode & PHY_MODE_SPECIAL)
2213 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002214 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
2215 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01002216
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00002217 /* Notify the kernel of the link state polled during driver load,
2218 * before the monitor starts running */
2219 efx_link_status_changed(efx);
2220
Ben Hutchings8ceee662008-04-27 12:55:59 +01002221 efx_start_all(efx);
Peter Dunning9c568fd2017-02-17 15:50:43 +00002222 if (efx->state == STATE_DISABLED || efx->reset_pending)
2223 netif_device_detach(efx->net_dev);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002224 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002225 return 0;
2226}
2227
2228/* Context: process, rtnl_lock() held.
2229 * Note that the kernel will ignore our return code; this method
2230 * should really be a void.
2231 */
Shradha Shahe340be92015-05-20 11:11:03 +01002232int efx_net_stop(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002233{
Ben Hutchings767e4682008-09-01 12:43:14 +01002234 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002235
Ben Hutchings62776d02010-06-23 11:30:07 +00002236 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
2237 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01002238
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002239 /* Stop the device and flush all the channels */
2240 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002241
2242 return 0;
2243}
2244
Ben Hutchings5b9e2072008-05-16 21:18:14 +01002245/* Context: process, dev_base_lock or RTNL held, non-blocking. */
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002246static void efx_net_stats(struct net_device *net_dev,
2247 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002248{
Ben Hutchings767e4682008-09-01 12:43:14 +01002249 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002250
Ben Hutchings55edc6e2009-11-25 16:11:35 +00002251 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00002252 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01002253 spin_unlock_bh(&efx->stats_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002254}
2255
2256/* Context: netif_tx_lock held, BHs disabled. */
2257static void efx_watchdog(struct net_device *net_dev)
2258{
Ben Hutchings767e4682008-09-01 12:43:14 +01002259 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002260
Ben Hutchings62776d02010-06-23 11:30:07 +00002261 netif_err(efx, tx_err, efx->net_dev,
2262 "TX stuck with port_enabled=%d: resetting channels\n",
2263 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002264
Ben Hutchings739bb23d2008-11-04 20:35:36 +00002265 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002266}
2267
2268
2269/* Context: process, rtnl_lock() held. */
2270static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2271{
Ben Hutchings767e4682008-09-01 12:43:14 +01002272 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002273 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002274
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002275 rc = efx_check_disabled(efx);
2276 if (rc)
2277 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002278
Ben Hutchings62776d02010-06-23 11:30:07 +00002279 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002280
Ben Hutchings29c69a42013-01-28 19:01:06 +00002281 efx_device_detach_sync(efx);
2282 efx_stop_all(efx);
2283
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002284 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002285 net_dev->mtu = new_mtu;
Edward Cree0d322412015-05-20 11:10:03 +01002286 efx_mac_reconfigure(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002287 mutex_unlock(&efx->mac_lock);
2288
Ben Hutchings8ceee662008-04-27 12:55:59 +01002289 efx_start_all(efx);
Peter Dunning9c568fd2017-02-17 15:50:43 +00002290 efx_device_attach_if_not_resetting(efx);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002291 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002292}
2293
2294static int efx_set_mac_address(struct net_device *net_dev, void *data)
2295{
Ben Hutchings767e4682008-09-01 12:43:14 +01002296 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002297 struct sockaddr *addr = data;
Ben Hutchingse0b3ae32014-02-12 18:59:54 +00002298 u8 *new_addr = addr->sa_data;
Shradha Shahcfc77c22015-05-20 11:09:30 +01002299 u8 old_addr[6];
2300 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002301
Ben Hutchings8ceee662008-04-27 12:55:59 +01002302 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002303 netif_err(efx, drv, efx->net_dev,
2304 "invalid ethernet MAC address requested: %pM\n",
2305 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002306 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002307 }
2308
Shradha Shahcfc77c22015-05-20 11:09:30 +01002309 /* save old address */
2310 ether_addr_copy(old_addr, net_dev->dev_addr);
Edward Creecd84ff42014-03-07 18:27:41 +00002311 ether_addr_copy(net_dev->dev_addr, new_addr);
Shradha Shah910c8782015-05-20 11:12:48 +01002312 if (efx->type->set_mac_address) {
2313 rc = efx->type->set_mac_address(efx);
Shradha Shahcfc77c22015-05-20 11:09:30 +01002314 if (rc) {
2315 ether_addr_copy(net_dev->dev_addr, old_addr);
2316 return rc;
2317 }
2318 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002319
2320 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002321 mutex_lock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01002322 efx_mac_reconfigure(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002323 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002324
2325 return 0;
2326}
2327
Ben Hutchingsa816f752008-09-01 12:49:12 +01002328/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002329static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002330{
Ben Hutchings767e4682008-09-01 12:43:14 +01002331 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002332
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002333 if (efx->port_enabled)
2334 queue_work(efx->workqueue, &efx->mac_work);
2335 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002336}
2337
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002338static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002339{
2340 struct efx_nic *efx = netdev_priv(net_dev);
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002341 int rc;
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002342
2343 /* If disabling RX n-tuple filtering, clear existing filters */
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002344 if (net_dev->features & ~data & NETIF_F_NTUPLE) {
2345 rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2346 if (rc)
2347 return rc;
2348 }
2349
Edward Cree69787292017-10-31 14:29:47 +00002350 /* If Rx VLAN filter is changed, update filters via mac_reconfigure.
2351 * If rx-fcs is changed, mac_reconfigure updates that too.
2352 */
2353 if ((net_dev->features ^ data) & (NETIF_F_HW_VLAN_CTAG_FILTER |
2354 NETIF_F_RXFCS)) {
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002355 /* efx_set_rx_mode() will schedule MAC work to update filters
2356 * when a new features are finally set in net_dev.
2357 */
2358 efx_set_rx_mode(net_dev);
2359 }
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002360
2361 return 0;
2362}
2363
Wei Yongjunb40296f2017-01-11 16:16:12 +00002364static int efx_get_phys_port_id(struct net_device *net_dev,
2365 struct netdev_phys_item_id *ppid)
Bert Kenward08a7b29b2017-01-10 16:23:33 +00002366{
2367 struct efx_nic *efx = netdev_priv(net_dev);
2368
2369 if (efx->type->get_phys_port_id)
2370 return efx->type->get_phys_port_id(efx, ppid);
2371 else
2372 return -EOPNOTSUPP;
2373}
2374
Bert Kenwardac019f02017-01-10 16:23:56 +00002375static int efx_get_phys_port_name(struct net_device *net_dev,
2376 char *name, size_t len)
2377{
2378 struct efx_nic *efx = netdev_priv(net_dev);
2379
2380 if (snprintf(name, len, "p%u", efx->port_num) >= len)
2381 return -EINVAL;
2382 return 0;
2383}
2384
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002385static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
2386{
2387 struct efx_nic *efx = netdev_priv(net_dev);
2388
2389 if (efx->type->vlan_rx_add_vid)
2390 return efx->type->vlan_rx_add_vid(efx, proto, vid);
2391 else
2392 return -EOPNOTSUPP;
2393}
2394
2395static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
2396{
2397 struct efx_nic *efx = netdev_priv(net_dev);
2398
2399 if (efx->type->vlan_rx_kill_vid)
2400 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
2401 else
2402 return -EOPNOTSUPP;
2403}
2404
Jon Coopere5fbd972017-02-08 16:52:10 +00002405static int efx_udp_tunnel_type_map(enum udp_parsable_tunnel_type in)
2406{
2407 switch (in) {
2408 case UDP_TUNNEL_TYPE_VXLAN:
2409 return TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
2410 case UDP_TUNNEL_TYPE_GENEVE:
2411 return TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
2412 default:
2413 return -1;
2414 }
2415}
2416
2417static void efx_udp_tunnel_add(struct net_device *dev, struct udp_tunnel_info *ti)
2418{
2419 struct efx_nic *efx = netdev_priv(dev);
2420 struct efx_udp_tunnel tnl;
2421 int efx_tunnel_type;
2422
2423 efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
2424 if (efx_tunnel_type < 0)
2425 return;
2426
2427 tnl.type = (u16)efx_tunnel_type;
2428 tnl.port = ti->port;
2429
2430 if (efx->type->udp_tnl_add_port)
2431 (void)efx->type->udp_tnl_add_port(efx, tnl);
2432}
2433
2434static void efx_udp_tunnel_del(struct net_device *dev, struct udp_tunnel_info *ti)
2435{
2436 struct efx_nic *efx = netdev_priv(dev);
2437 struct efx_udp_tunnel tnl;
2438 int efx_tunnel_type;
2439
2440 efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
2441 if (efx_tunnel_type < 0)
2442 return;
2443
2444 tnl.type = (u16)efx_tunnel_type;
2445 tnl.port = ti->port;
2446
Dan Carpenterc04ca612017-03-22 12:10:02 +03002447 if (efx->type->udp_tnl_del_port)
Jon Coopere5fbd972017-02-08 16:52:10 +00002448 (void)efx->type->udp_tnl_del_port(efx, tnl);
2449}
2450
Shradha Shah7fa8d542015-05-06 00:55:13 +01002451static const struct net_device_ops efx_netdev_ops = {
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002452 .ndo_open = efx_net_open,
2453 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002454 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002455 .ndo_tx_timeout = efx_watchdog,
2456 .ndo_start_xmit = efx_hard_start_xmit,
2457 .ndo_validate_addr = eth_validate_addr,
2458 .ndo_do_ioctl = efx_ioctl,
2459 .ndo_change_mtu = efx_change_mtu,
2460 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002461 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002462 .ndo_set_features = efx_set_features,
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002463 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
2464 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002465#ifdef CONFIG_SFC_SRIOV
Shradha Shah7fa8d542015-05-06 00:55:13 +01002466 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2467 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2468 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2469 .ndo_get_vf_config = efx_sriov_get_vf_config,
Edward Cree4392dc62015-05-20 11:12:13 +01002470 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002471#endif
Bert Kenward08a7b29b2017-01-10 16:23:33 +00002472 .ndo_get_phys_port_id = efx_get_phys_port_id,
Bert Kenwardac019f02017-01-10 16:23:56 +00002473 .ndo_get_phys_port_name = efx_get_phys_port_name,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002474#ifdef CONFIG_NET_POLL_CONTROLLER
2475 .ndo_poll_controller = efx_netpoll,
2476#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002477 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002478#ifdef CONFIG_RFS_ACCEL
2479 .ndo_rx_flow_steer = efx_filter_rfs,
2480#endif
Jon Coopere5fbd972017-02-08 16:52:10 +00002481 .ndo_udp_tunnel_add = efx_udp_tunnel_add,
2482 .ndo_udp_tunnel_del = efx_udp_tunnel_del,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002483};
2484
Ben Hutchings7dde5962008-12-12 22:09:38 -08002485static void efx_update_name(struct efx_nic *efx)
2486{
2487 strcpy(efx->name, efx->net_dev->name);
2488 efx_mtd_rename(efx);
2489 efx_set_channel_names(efx);
2490}
2491
Ben Hutchings8ceee662008-04-27 12:55:59 +01002492static int efx_netdev_event(struct notifier_block *this,
2493 unsigned long event, void *ptr)
2494{
Jiri Pirko351638e2013-05-28 01:30:21 +00002495 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002496
Shradha Shah7fa8d542015-05-06 00:55:13 +01002497 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
Ben Hutchings7dde5962008-12-12 22:09:38 -08002498 event == NETDEV_CHANGENAME)
2499 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002500
2501 return NOTIFY_DONE;
2502}
2503
2504static struct notifier_block efx_netdev_notifier = {
2505 .notifier_call = efx_netdev_event,
2506};
2507
Ben Hutchings06d5e192008-12-12 21:47:23 -08002508static ssize_t
2509show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2510{
2511 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2512 return sprintf(buf, "%d\n", efx->phy_type);
2513}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002514static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002515
Edward Creee7fef9b2015-05-27 13:14:01 +01002516#ifdef CONFIG_SFC_MCDI_LOGGING
2517static ssize_t show_mcdi_log(struct device *dev, struct device_attribute *attr,
2518 char *buf)
2519{
2520 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2521 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2522
2523 return scnprintf(buf, PAGE_SIZE, "%d\n", mcdi->logging_enabled);
2524}
2525static ssize_t set_mcdi_log(struct device *dev, struct device_attribute *attr,
2526 const char *buf, size_t count)
2527{
2528 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2529 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2530 bool enable = count > 0 && *buf != '0';
2531
2532 mcdi->logging_enabled = enable;
2533 return count;
2534}
2535static DEVICE_ATTR(mcdi_logging, 0644, show_mcdi_log, set_mcdi_log);
2536#endif
2537
Ben Hutchings8ceee662008-04-27 12:55:59 +01002538static int efx_register_netdev(struct efx_nic *efx)
2539{
2540 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002541 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002542 int rc;
2543
2544 net_dev->watchdog_timeo = 5 * HZ;
2545 net_dev->irq = efx->pci_dev->irq;
Shradha Shah7fa8d542015-05-06 00:55:13 +01002546 net_dev->netdev_ops = &efx_netdev_ops;
2547 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
Ben Hutchings8127d662013-08-29 19:19:29 +01002548 net_dev->priv_flags |= IFF_UNICAST_FLT;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002549 net_dev->ethtool_ops = &efx_ethtool_ops;
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002550 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Bert Kenwardcd94e512016-10-18 17:47:45 +01002551 net_dev->min_mtu = EFX_MIN_MTU;
2552 net_dev->max_mtu = EFX_MAX_MTU;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002553
Ben Hutchings7dde5962008-12-12 22:09:38 -08002554 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002555
Ben Hutchings7153f622012-07-27 20:50:52 +01002556 /* Enable resets to be scheduled and check whether any were
2557 * already requested. If so, the NIC is probably hosed so we
2558 * abort.
2559 */
2560 efx->state = STATE_READY;
2561 smp_mb(); /* ensure we change state before checking reset_pending */
2562 if (efx->reset_pending) {
2563 netif_err(efx, probe, efx->net_dev,
2564 "aborting probe due to scheduled reset\n");
2565 rc = -EIO;
2566 goto fail_locked;
2567 }
2568
Ben Hutchingsaed06282009-08-26 08:16:27 +00002569 rc = dev_alloc_name(net_dev, net_dev->name);
2570 if (rc < 0)
2571 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002572 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002573
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002574 /* Always start with carrier off; PHY events will detect the link */
2575 netif_carrier_off(net_dev);
2576
Ben Hutchingsaed06282009-08-26 08:16:27 +00002577 rc = register_netdevice(net_dev);
2578 if (rc)
2579 goto fail_locked;
2580
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002581 efx_for_each_channel(channel, efx) {
2582 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002583 efx_for_each_channel_tx_queue(tx_queue, channel)
2584 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002585 }
2586
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002587 efx_associate(efx);
2588
Ben Hutchings7dde5962008-12-12 22:09:38 -08002589 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002590
Ben Hutchings06d5e192008-12-12 21:47:23 -08002591 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2592 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002593 netif_err(efx, drv, efx->net_dev,
2594 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002595 goto fail_registered;
2596 }
Edward Creee7fef9b2015-05-27 13:14:01 +01002597#ifdef CONFIG_SFC_MCDI_LOGGING
2598 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2599 if (rc) {
2600 netif_err(efx, drv, efx->net_dev,
2601 "failed to init net dev attributes\n");
2602 goto fail_attr_mcdi_logging;
2603 }
2604#endif
Ben Hutchings06d5e192008-12-12 21:47:23 -08002605
Ben Hutchings8ceee662008-04-27 12:55:59 +01002606 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002607
Edward Creee7fef9b2015-05-27 13:14:01 +01002608#ifdef CONFIG_SFC_MCDI_LOGGING
2609fail_attr_mcdi_logging:
2610 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2611#endif
Ben Hutchings7153f622012-07-27 20:50:52 +01002612fail_registered:
2613 rtnl_lock();
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002614 efx_dissociate(efx);
Ben Hutchings7153f622012-07-27 20:50:52 +01002615 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002616fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002617 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002618 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002619 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002620 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002621}
2622
2623static void efx_unregister_netdev(struct efx_nic *efx)
2624{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002625 if (!efx->net_dev)
2626 return;
2627
Ben Hutchings767e4682008-09-01 12:43:14 +01002628 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002629
Edward Creee7fef9b2015-05-27 13:14:01 +01002630 if (efx_dev_registered(efx)) {
2631 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2632#ifdef CONFIG_SFC_MCDI_LOGGING
2633 device_remove_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2634#endif
2635 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2636 unregister_netdev(efx->net_dev);
2637 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002638}
2639
2640/**************************************************************************
2641 *
2642 * Device reset and suspend
2643 *
2644 **************************************************************************/
2645
Ben Hutchings2467ca42008-09-01 12:48:50 +01002646/* Tears down the entire software state and most of the hardware state
2647 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002648void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002649{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002650 EFX_ASSERT_RESET_SERIALISED(efx);
2651
Edward Creee2835462014-04-16 19:27:48 +01002652 if (method == RESET_TYPE_MCDI_TIMEOUT)
2653 efx->type->prepare_flr(efx);
2654
Ben Hutchings2467ca42008-09-01 12:48:50 +01002655 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002656 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002657
2658 mutex_lock(&efx->mac_lock);
Edward Creee0a65e32018-03-27 17:44:36 +01002659 mutex_lock(&efx->rss_lock);
Jon Cooper087e9022015-05-20 11:11:35 +01002660 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2661 method != RESET_TYPE_DATAPATH)
Steve Hodgson4b988282009-01-29 17:50:51 +00002662 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002663 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002664}
2665
Ben Hutchings2467ca42008-09-01 12:48:50 +01002666/* This function will always ensure that the locks acquired in
2667 * efx_reset_down() are released. A failure return code indicates
2668 * that we were unable to reinitialise the hardware, and the
2669 * driver should be disabled. If ok is false, then the rx and tx
2670 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002671int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002672{
2673 int rc;
2674
Ben Hutchings2467ca42008-09-01 12:48:50 +01002675 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002676
Edward Creee2835462014-04-16 19:27:48 +01002677 if (method == RESET_TYPE_MCDI_TIMEOUT)
2678 efx->type->finish_flr(efx);
2679
2680 /* Ensure that SRAM is initialised even if we're disabling the device */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002681 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002682 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002683 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002684 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002685 }
2686
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002687 if (!ok)
2688 goto fail;
2689
Jon Cooper087e9022015-05-20 11:11:35 +01002690 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2691 method != RESET_TYPE_DATAPATH) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002692 rc = efx->phy_op->init(efx);
2693 if (rc)
2694 goto fail;
Edward Cree267d9d72015-05-06 00:59:18 +01002695 rc = efx->phy_op->reconfigure(efx);
2696 if (rc && rc != -EPERM)
Ben Hutchings62776d02010-06-23 11:30:07 +00002697 netif_err(efx, drv, efx->net_dev,
2698 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002699 }
2700
Jon Cooper261e4d92013-04-15 18:51:54 +01002701 rc = efx_enable_interrupts(efx);
2702 if (rc)
2703 goto fail;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01002704
2705#ifdef CONFIG_SFC_SRIOV
2706 rc = efx->type->vswitching_restore(efx);
2707 if (rc) /* not fatal; the PF will still work fine */
2708 netif_warn(efx, probe, efx->net_dev,
2709 "failed to restore vswitching rc=%d;"
2710 " VFs may not function\n", rc);
2711#endif
2712
Edward Cree42356d92018-03-08 15:45:17 +00002713 if (efx->type->rx_restore_rss_contexts)
2714 efx->type->rx_restore_rss_contexts(efx);
Edward Creee0a65e32018-03-27 17:44:36 +01002715 mutex_unlock(&efx->rss_lock);
Edward Cree0d322412015-05-20 11:10:03 +01002716 down_read(&efx->filter_sem);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002717 efx_restore_filters(efx);
Edward Cree0d322412015-05-20 11:10:03 +01002718 up_read(&efx->filter_sem);
Shradha Shah7fa8d542015-05-06 00:55:13 +01002719 if (efx->type->sriov_reset)
2720 efx->type->sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002721
2722 mutex_unlock(&efx->mac_lock);
2723
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002724 efx_start_all(efx);
2725
Jon Coopere5fbd972017-02-08 16:52:10 +00002726 if (efx->type->udp_tnl_push_ports)
2727 efx->type->udp_tnl_push_ports(efx);
2728
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002729 return 0;
2730
2731fail:
2732 efx->port_initialized = false;
2733
Edward Creee0a65e32018-03-27 17:44:36 +01002734 mutex_unlock(&efx->rss_lock);
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002735 mutex_unlock(&efx->mac_lock);
2736
Ben Hutchings8ceee662008-04-27 12:55:59 +01002737 return rc;
2738}
2739
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002740/* Reset the NIC using the specified method. Note that the reset may
2741 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002742 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002743 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002744 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002745int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002746{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002747 int rc, rc2;
2748 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002749
Ben Hutchings62776d02010-06-23 11:30:07 +00002750 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2751 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002752
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002753 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002754 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002755
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002756 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002757 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002758 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002759 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002760 }
2761
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002762 /* Clear flags for the scopes we covered. We assume the NIC and
2763 * driver are now quiescent so that there is no race here.
2764 */
Edward Creee2835462014-04-16 19:27:48 +01002765 if (method < RESET_TYPE_MAX_METHOD)
2766 efx->reset_pending &= -(1 << (method + 1));
2767 else /* it doesn't fit into the well-ordered scope hierarchy */
2768 __clear_bit(method, &efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002769
2770 /* Reinitialise bus-mastering, which may have been turned off before
2771 * the reset was scheduled. This is still appropriate, even in the
2772 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2773 * can respond to requests. */
2774 pci_set_master(efx->pci_dev);
2775
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002776out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002777 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002778 disabled = rc ||
2779 method == RESET_TYPE_DISABLE ||
2780 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002781 rc2 = efx_reset_up(efx, method, !disabled);
2782 if (rc2) {
2783 disabled = true;
2784 if (!rc)
2785 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002786 }
2787
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002788 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002789 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002790 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002791 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002792 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002793 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Peter Dunning9c568fd2017-02-17 15:50:43 +00002794 efx_device_attach_if_not_resetting(efx);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002795 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002796 return rc;
2797}
2798
Alexandre Rames626950d2013-01-14 17:20:22 +00002799/* Try recovery mechanisms.
2800 * For now only EEH is supported.
2801 * Returns 0 if the recovery mechanisms are unsuccessful.
2802 * Returns a non-zero value otherwise.
2803 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002804int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002805{
2806#ifdef CONFIG_EEH
2807 /* A PCI error can occur and not be seen by EEH because nothing
2808 * happens on the PCI bus. In this case the driver may fail and
2809 * schedule a 'recover or reset', leading to this recovery handler.
2810 * Manually call the eeh failure check function.
2811 */
Benjamin Herrenschmidt12a89db2015-03-23 14:00:47 +11002812 struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002813 if (eeh_dev_check_failure(eehdev)) {
2814 /* The EEH mechanisms will handle the error and reset the
2815 * device if necessary.
2816 */
2817 return 1;
2818 }
2819#endif
2820 return 0;
2821}
2822
Jon Cooper74cd60a2013-09-16 14:18:51 +01002823static void efx_wait_for_bist_end(struct efx_nic *efx)
2824{
2825 int i;
2826
2827 for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
2828 if (efx_mcdi_poll_reboot(efx))
2829 goto out;
2830 msleep(BIST_WAIT_DELAY_MS);
2831 }
2832
2833 netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
2834out:
2835 /* Either way unset the BIST flag. If we found no reboot we probably
2836 * won't recover, but we should try.
2837 */
2838 efx->mc_bist_for_other_fn = false;
2839}
2840
Ben Hutchings8ceee662008-04-27 12:55:59 +01002841/* The worker thread exists so that code that cannot sleep can
2842 * schedule a reset for later.
2843 */
2844static void efx_reset_work(struct work_struct *data)
2845{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002846 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002847 unsigned long pending;
2848 enum reset_type method;
2849
Mark Rutland6aa7de02017-10-23 14:07:29 -07002850 pending = READ_ONCE(efx->reset_pending);
Alexandre Rames626950d2013-01-14 17:20:22 +00002851 method = fls(pending) - 1;
2852
Jon Cooper74cd60a2013-09-16 14:18:51 +01002853 if (method == RESET_TYPE_MC_BIST)
2854 efx_wait_for_bist_end(efx);
2855
Alexandre Rames626950d2013-01-14 17:20:22 +00002856 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2857 method == RESET_TYPE_RECOVER_OR_ALL) &&
2858 efx_try_recovery(efx))
2859 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002860
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002861 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002862 return;
2863
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002864 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002865
2866 /* We checked the state in efx_schedule_reset() but it may
2867 * have changed by now. Now that we have the RTNL lock,
2868 * it cannot change again.
2869 */
2870 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002871 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002872
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002873 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002874}
2875
2876void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2877{
2878 enum reset_type method;
2879
Alexandre Rames626950d2013-01-14 17:20:22 +00002880 if (efx->state == STATE_RECOVERY) {
2881 netif_dbg(efx, drv, efx->net_dev,
2882 "recovering: skip scheduling %s reset\n",
2883 RESET_TYPE(type));
2884 return;
2885 }
2886
Ben Hutchings8ceee662008-04-27 12:55:59 +01002887 switch (type) {
2888 case RESET_TYPE_INVISIBLE:
2889 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002890 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002891 case RESET_TYPE_WORLD:
2892 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002893 case RESET_TYPE_RECOVER_OR_DISABLE:
Jon Cooper087e9022015-05-20 11:11:35 +01002894 case RESET_TYPE_DATAPATH:
Jon Cooper74cd60a2013-09-16 14:18:51 +01002895 case RESET_TYPE_MC_BIST:
Edward Creee2835462014-04-16 19:27:48 +01002896 case RESET_TYPE_MCDI_TIMEOUT:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002897 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002898 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2899 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002900 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002901 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002902 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002903 netif_dbg(efx, drv, efx->net_dev,
2904 "scheduling %s reset for %s\n",
2905 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002906 break;
2907 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002908
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002909 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002910 smp_mb(); /* ensure we change reset_pending before checking state */
2911
2912 /* If we're not READY then just leave the flags set as the cue
2913 * to abort probing or reschedule the reset later.
2914 */
Mark Rutland6aa7de02017-10-23 14:07:29 -07002915 if (READ_ONCE(efx->state) != STATE_READY)
Ben Hutchings7153f622012-07-27 20:50:52 +01002916 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002917
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002918 /* efx_process_channel() will no longer read events once a
2919 * reset is scheduled. So switch back to poll'd MCDI completions. */
2920 efx_mcdi_mode_poll(efx);
2921
Steve Hodgson1ab00622008-12-12 21:33:02 -08002922 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002923}
2924
2925/**************************************************************************
2926 *
2927 * List of NICs we support
2928 *
2929 **************************************************************************/
2930
2931/* PCI device ID table */
Benoit Taine9baa3c32014-08-08 15:56:03 +02002932static const struct pci_device_id efx_pci_table[] = {
Ben Hutchings547c4742011-12-02 18:23:56 +00002933 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002934 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002935 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002936 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8127d662013-08-29 19:19:29 +01002937 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
2938 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Shradha Shah6f7f8aa2015-05-06 01:00:07 +01002939 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
2940 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
Mateusz Wrzesinski3b06a002014-07-14 08:38:49 +01002941 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
2942 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Bert Kenwarddd248f12015-11-30 09:05:47 +00002943 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
2944 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
2945 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
2946 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2947 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
2948 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
Edward Creeaae5a312017-12-18 16:56:34 +00002949 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
2950 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2951 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
2952 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002953 {0} /* end of list */
2954};
2955
2956/**************************************************************************
2957 *
Ben Hutchings37594332009-11-23 16:05:45 +00002958 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002959 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002960 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002961 * Needed so all function pointers are valid and do not have to be tested
2962 * before use
2963 *
2964 **************************************************************************/
2965int efx_port_dummy_op_int(struct efx_nic *efx)
2966{
2967 return 0;
2968}
2969void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002970
2971static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002972{
2973 return false;
2974}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002975
stephen hemminger6c8c2512011-04-14 05:50:12 +00002976static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002977 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002978 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002979 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002980 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002981};
2982
Ben Hutchings8ceee662008-04-27 12:55:59 +01002983/**************************************************************************
2984 *
2985 * Data housekeeping
2986 *
2987 **************************************************************************/
2988
2989/* This zeroes out and then fills in the invariants in a struct
2990 * efx_nic (including all sub-structures).
2991 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002992static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002993 struct pci_dev *pci_dev, struct net_device *net_dev)
2994{
Andrew Rybchenko6f9f6ec2017-02-13 14:57:39 +00002995 int rc = -ENOMEM, i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002996
2997 /* Initialise common structures */
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002998 INIT_LIST_HEAD(&efx->node);
2999 INIT_LIST_HEAD(&efx->secondary_list);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003000 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00003001#ifdef CONFIG_SFC_MTD
3002 INIT_LIST_HEAD(&efx->mtd_list);
3003#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003004 INIT_WORK(&efx->reset_work, efx_reset_work);
3005 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00003006 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003007 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00003008 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01003009 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003010 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01003011
3012 efx->net_dev = net_dev;
Jon Cooper43a37392012-10-18 15:49:54 +01003013 efx->rx_prefix_size = efx->type->rx_prefix_size;
Andrew Rybchenko2ec03012013-11-16 11:02:27 +04003014 efx->rx_ip_align =
3015 NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
Jon Cooper43a37392012-10-18 15:49:54 +01003016 efx->rx_packet_hash_offset =
3017 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
Jon Cooperbd9a2652013-11-18 12:54:41 +00003018 efx->rx_packet_ts_offset =
3019 efx->type->rx_ts_offset - efx->type->rx_prefix_size;
Edward Cree42356d92018-03-08 15:45:17 +00003020 INIT_LIST_HEAD(&efx->rss_context.list);
Edward Creee0a65e32018-03-27 17:44:36 +01003021 mutex_init(&efx->rss_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003022 spin_lock_init(&efx->stats_lock);
Edward Cree71827442017-12-18 16:56:19 +00003023 efx->vi_stride = EFX_DEFAULT_VI_STRIDE;
Edward Creec1be4822017-12-21 09:00:26 +00003024 efx->num_mac_stats = MC_CMD_MAC_NSTATS;
3025 BUILD_BUG_ON(MC_CMD_MAC_NSTATS - 1 != MC_CMD_MAC_GENERATION_END);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003026 mutex_init(&efx->mac_lock);
Edward Cree3af0f342018-03-27 17:41:59 +01003027#ifdef CONFIG_RFS_ACCEL
3028 mutex_init(&efx->rps_mutex);
Edward Creef8d62032018-04-24 17:09:30 +01003029 spin_lock_init(&efx->rps_hash_lock);
3030 /* Failure to allocate is not fatal, but may degrade ARFS performance */
3031 efx->rps_hash_table = kcalloc(EFX_ARFS_HASH_TABLE_SIZE,
3032 sizeof(*efx->rps_hash_table), GFP_KERNEL);
Edward Cree3af0f342018-03-27 17:41:59 +01003033#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003034 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00003035 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08003036 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00003037 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003038
3039 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00003040 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
3041 if (!efx->channel[i])
3042 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01003043 efx->msi_context[i].efx = efx;
3044 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003045 }
3046
Ben Hutchings8ceee662008-04-27 12:55:59 +01003047 /* Higher numbered interrupt modes are less capable! */
Andrew Rybchenko6f9f6ec2017-02-13 14:57:39 +00003048 if (WARN_ON_ONCE(efx->type->max_interrupt_mode >
3049 efx->type->min_interrupt_mode)) {
3050 rc = -EIO;
3051 goto fail;
3052 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003053 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
3054 interrupt_mode);
Andrew Rybchenko6f9f6ec2017-02-13 14:57:39 +00003055 efx->interrupt_mode = min(efx->type->min_interrupt_mode,
3056 interrupt_mode);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003057
Ben Hutchings6977dc62008-12-26 13:44:39 -08003058 /* Would be good to use the net_dev name, but we're too early */
3059 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
3060 pci_name(pci_dev));
3061 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003062 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00003063 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01003064
Ben Hutchings8ceee662008-04-27 12:55:59 +01003065 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00003066
3067fail:
3068 efx_fini_struct(efx);
Andrew Rybchenko6f9f6ec2017-02-13 14:57:39 +00003069 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003070}
3071
3072static void efx_fini_struct(struct efx_nic *efx)
3073{
Ben Hutchings8313aca2010-09-10 06:41:57 +00003074 int i;
3075
Edward Creef8d62032018-04-24 17:09:30 +01003076#ifdef CONFIG_RFS_ACCEL
3077 kfree(efx->rps_hash_table);
3078#endif
3079
Ben Hutchings8313aca2010-09-10 06:41:57 +00003080 for (i = 0; i < EFX_MAX_CHANNELS; i++)
3081 kfree(efx->channel[i]);
3082
Ben Hutchingsef215e62013-12-05 20:13:22 +00003083 kfree(efx->vpd_sn);
3084
Ben Hutchings8ceee662008-04-27 12:55:59 +01003085 if (efx->workqueue) {
3086 destroy_workqueue(efx->workqueue);
3087 efx->workqueue = NULL;
3088 }
3089}
3090
Edward Creee4d112e2014-07-15 11:58:12 +01003091void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
3092{
3093 u64 n_rx_nodesc_trunc = 0;
3094 struct efx_channel *channel;
3095
3096 efx_for_each_channel(channel, efx)
3097 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
3098 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
3099 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
3100}
3101
Edward Creef8d62032018-04-24 17:09:30 +01003102bool efx_filter_spec_equal(const struct efx_filter_spec *left,
3103 const struct efx_filter_spec *right)
3104{
3105 if ((left->match_flags ^ right->match_flags) |
3106 ((left->flags ^ right->flags) &
3107 (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
3108 return false;
3109
3110 return memcmp(&left->outer_vid, &right->outer_vid,
3111 sizeof(struct efx_filter_spec) -
3112 offsetof(struct efx_filter_spec, outer_vid)) == 0;
3113}
3114
3115u32 efx_filter_spec_hash(const struct efx_filter_spec *spec)
3116{
3117 BUILD_BUG_ON(offsetof(struct efx_filter_spec, outer_vid) & 3);
3118 return jhash2((const u32 *)&spec->outer_vid,
3119 (sizeof(struct efx_filter_spec) -
3120 offsetof(struct efx_filter_spec, outer_vid)) / 4,
3121 0);
3122}
3123
3124#ifdef CONFIG_RFS_ACCEL
3125bool efx_rps_check_rule(struct efx_arfs_rule *rule, unsigned int filter_idx,
3126 bool *force)
3127{
3128 if (rule->filter_id == EFX_ARFS_FILTER_ID_PENDING) {
3129 /* ARFS is currently updating this entry, leave it */
3130 return false;
3131 }
3132 if (rule->filter_id == EFX_ARFS_FILTER_ID_ERROR) {
3133 /* ARFS tried and failed to update this, so it's probably out
3134 * of date. Remove the filter and the ARFS rule entry.
3135 */
3136 rule->filter_id = EFX_ARFS_FILTER_ID_REMOVING;
3137 *force = true;
3138 return true;
3139 } else if (WARN_ON(rule->filter_id != filter_idx)) { /* can't happen */
3140 /* ARFS has moved on, so old filter is not needed. Since we did
3141 * not mark the rule with EFX_ARFS_FILTER_ID_REMOVING, it will
3142 * not be removed by efx_rps_hash_del() subsequently.
3143 */
3144 *force = true;
3145 return true;
3146 }
3147 /* Remove it iff ARFS wants to. */
3148 return true;
3149}
3150
3151struct hlist_head *efx_rps_hash_bucket(struct efx_nic *efx,
3152 const struct efx_filter_spec *spec)
3153{
3154 u32 hash = efx_filter_spec_hash(spec);
3155
3156 WARN_ON(!spin_is_locked(&efx->rps_hash_lock));
3157 if (!efx->rps_hash_table)
3158 return NULL;
3159 return &efx->rps_hash_table[hash % EFX_ARFS_HASH_TABLE_SIZE];
3160}
3161
3162struct efx_arfs_rule *efx_rps_hash_find(struct efx_nic *efx,
3163 const struct efx_filter_spec *spec)
3164{
3165 struct efx_arfs_rule *rule;
3166 struct hlist_head *head;
3167 struct hlist_node *node;
3168
3169 head = efx_rps_hash_bucket(efx, spec);
3170 if (!head)
3171 return NULL;
3172 hlist_for_each(node, head) {
3173 rule = container_of(node, struct efx_arfs_rule, node);
3174 if (efx_filter_spec_equal(spec, &rule->spec))
3175 return rule;
3176 }
3177 return NULL;
3178}
3179
3180struct efx_arfs_rule *efx_rps_hash_add(struct efx_nic *efx,
3181 const struct efx_filter_spec *spec,
3182 bool *new)
3183{
3184 struct efx_arfs_rule *rule;
3185 struct hlist_head *head;
3186 struct hlist_node *node;
3187
3188 head = efx_rps_hash_bucket(efx, spec);
3189 if (!head)
3190 return NULL;
3191 hlist_for_each(node, head) {
3192 rule = container_of(node, struct efx_arfs_rule, node);
3193 if (efx_filter_spec_equal(spec, &rule->spec)) {
3194 *new = false;
3195 return rule;
3196 }
3197 }
3198 rule = kmalloc(sizeof(*rule), GFP_ATOMIC);
3199 *new = true;
3200 if (rule) {
3201 memcpy(&rule->spec, spec, sizeof(rule->spec));
3202 hlist_add_head(&rule->node, head);
3203 }
3204 return rule;
3205}
3206
3207void efx_rps_hash_del(struct efx_nic *efx, const struct efx_filter_spec *spec)
3208{
3209 struct efx_arfs_rule *rule;
3210 struct hlist_head *head;
3211 struct hlist_node *node;
3212
3213 head = efx_rps_hash_bucket(efx, spec);
3214 if (WARN_ON(!head))
3215 return;
3216 hlist_for_each(node, head) {
3217 rule = container_of(node, struct efx_arfs_rule, node);
3218 if (efx_filter_spec_equal(spec, &rule->spec)) {
3219 /* Someone already reused the entry. We know that if
3220 * this check doesn't fire (i.e. filter_id == REMOVING)
3221 * then the REMOVING mark was put there by our caller,
3222 * because caller is holding a lock on filter table and
3223 * only holders of that lock set REMOVING.
3224 */
3225 if (rule->filter_id != EFX_ARFS_FILTER_ID_REMOVING)
3226 return;
3227 hlist_del(node);
3228 kfree(rule);
3229 return;
3230 }
3231 }
3232 /* We didn't find it. */
3233 WARN_ON(1);
3234}
3235#endif
3236
Edward Cree42356d92018-03-08 15:45:17 +00003237/* RSS contexts. We're using linked lists and crappy O(n) algorithms, because
3238 * (a) this is an infrequent control-plane operation and (b) n is small (max 64)
3239 */
Edward Creee0a65e32018-03-27 17:44:36 +01003240struct efx_rss_context *efx_alloc_rss_context_entry(struct efx_nic *efx)
Edward Cree42356d92018-03-08 15:45:17 +00003241{
Edward Creee0a65e32018-03-27 17:44:36 +01003242 struct list_head *head = &efx->rss_context.list;
Edward Cree42356d92018-03-08 15:45:17 +00003243 struct efx_rss_context *ctx, *new;
3244 u32 id = 1; /* Don't use zero, that refers to the master RSS context */
3245
Edward Creee0a65e32018-03-27 17:44:36 +01003246 WARN_ON(!mutex_is_locked(&efx->rss_lock));
3247
Edward Cree42356d92018-03-08 15:45:17 +00003248 /* Search for first gap in the numbering */
3249 list_for_each_entry(ctx, head, list) {
3250 if (ctx->user_id != id)
3251 break;
3252 id++;
3253 /* Check for wrap. If this happens, we have nearly 2^32
3254 * allocated RSS contexts, which seems unlikely.
3255 */
3256 if (WARN_ON_ONCE(!id))
3257 return NULL;
3258 }
3259
3260 /* Create the new entry */
3261 new = kmalloc(sizeof(struct efx_rss_context), GFP_KERNEL);
3262 if (!new)
3263 return NULL;
3264 new->context_id = EFX_EF10_RSS_CONTEXT_INVALID;
3265 new->rx_hash_udp_4tuple = false;
3266
3267 /* Insert the new entry into the gap */
3268 new->user_id = id;
3269 list_add_tail(&new->list, &ctx->list);
3270 return new;
3271}
3272
Edward Creee0a65e32018-03-27 17:44:36 +01003273struct efx_rss_context *efx_find_rss_context_entry(struct efx_nic *efx, u32 id)
Edward Cree42356d92018-03-08 15:45:17 +00003274{
Edward Creee0a65e32018-03-27 17:44:36 +01003275 struct list_head *head = &efx->rss_context.list;
Edward Cree42356d92018-03-08 15:45:17 +00003276 struct efx_rss_context *ctx;
3277
Edward Creee0a65e32018-03-27 17:44:36 +01003278 WARN_ON(!mutex_is_locked(&efx->rss_lock));
3279
Edward Cree42356d92018-03-08 15:45:17 +00003280 list_for_each_entry(ctx, head, list)
3281 if (ctx->user_id == id)
3282 return ctx;
3283 return NULL;
3284}
3285
3286void efx_free_rss_context_entry(struct efx_rss_context *ctx)
3287{
3288 list_del(&ctx->list);
3289 kfree(ctx);
3290}
3291
Ben Hutchings8ceee662008-04-27 12:55:59 +01003292/**************************************************************************
3293 *
3294 * PCI interface
3295 *
3296 **************************************************************************/
3297
3298/* Main body of final NIC shutdown code
3299 * This is called only at module unload (or hotplug removal).
3300 */
3301static void efx_pci_remove_main(struct efx_nic *efx)
3302{
Ben Hutchings7153f622012-07-27 20:50:52 +01003303 /* Flush reset_work. It can no longer be scheduled since we
3304 * are not READY.
3305 */
3306 BUG_ON(efx->state == STATE_READY);
3307 cancel_work_sync(&efx->reset_work);
3308
Ben Hutchingsd8291182012-10-05 23:35:41 +01003309 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00003310 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003311 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00003312 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003313 efx_fini_napi(efx);
3314 efx_remove_all(efx);
3315}
3316
3317/* Final NIC shutdown
Daniel Pieczko2a3fc312015-06-02 11:40:46 +01003318 * This is called only at module unload (or hotplug removal). A PF can call
3319 * this on its VFs to ensure they are unbound first.
Ben Hutchings8ceee662008-04-27 12:55:59 +01003320 */
3321static void efx_pci_remove(struct pci_dev *pci_dev)
3322{
3323 struct efx_nic *efx;
3324
3325 efx = pci_get_drvdata(pci_dev);
3326 if (!efx)
3327 return;
3328
3329 /* Mark the NIC as fini, then stop the interface */
3330 rtnl_lock();
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01003331 efx_dissociate(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003332 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003333 efx_disable_interrupts(efx);
Edward Creeea6bb992015-06-15 18:27:54 +01003334 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003335 rtnl_unlock();
3336
Shradha Shah7fa8d542015-05-06 00:55:13 +01003337 if (efx->type->sriov_fini)
3338 efx->type->sriov_fini(efx);
3339
Ben Hutchings8ceee662008-04-27 12:55:59 +01003340 efx_unregister_netdev(efx);
3341
Ben Hutchings7dde5962008-12-12 22:09:38 -08003342 efx_mtd_remove(efx);
3343
Ben Hutchings8ceee662008-04-27 12:55:59 +01003344 efx_pci_remove_main(efx);
3345
Ben Hutchings8ceee662008-04-27 12:55:59 +01003346 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00003347 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003348
Ben Hutchings8ceee662008-04-27 12:55:59 +01003349 efx_fini_struct(efx);
3350 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00003351
3352 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003353};
3354
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003355/* NIC VPD information
3356 * Called during probe to display the part number of the
3357 * installed NIC. VPD is potentially very large but this should
3358 * always appear within the first 512 bytes.
3359 */
3360#define SFC_VPD_LEN 512
Ben Hutchingsef215e62013-12-05 20:13:22 +00003361static void efx_probe_vpd_strings(struct efx_nic *efx)
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003362{
3363 struct pci_dev *dev = efx->pci_dev;
3364 char vpd_data[SFC_VPD_LEN];
3365 ssize_t vpd_size;
Ben Hutchingsef215e62013-12-05 20:13:22 +00003366 int ro_start, ro_size, i, j;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003367
3368 /* Get the vpd data from the device */
3369 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
3370 if (vpd_size <= 0) {
3371 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
3372 return;
3373 }
3374
3375 /* Get the Read only section */
Ben Hutchingsef215e62013-12-05 20:13:22 +00003376 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
3377 if (ro_start < 0) {
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003378 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
3379 return;
3380 }
3381
Ben Hutchingsef215e62013-12-05 20:13:22 +00003382 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
3383 j = ro_size;
3384 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003385 if (i + j > vpd_size)
3386 j = vpd_size - i;
3387
3388 /* Get the Part number */
3389 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
3390 if (i < 0) {
3391 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
3392 return;
3393 }
3394
3395 j = pci_vpd_info_field_size(&vpd_data[i]);
3396 i += PCI_VPD_INFO_FLD_HDR_SIZE;
3397 if (i + j > vpd_size) {
3398 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
3399 return;
3400 }
3401
3402 netif_info(efx, drv, efx->net_dev,
3403 "Part Number : %.*s\n", j, &vpd_data[i]);
Ben Hutchingsef215e62013-12-05 20:13:22 +00003404
3405 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
3406 j = ro_size;
3407 i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
3408 if (i < 0) {
3409 netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
3410 return;
3411 }
3412
3413 j = pci_vpd_info_field_size(&vpd_data[i]);
3414 i += PCI_VPD_INFO_FLD_HDR_SIZE;
3415 if (i + j > vpd_size) {
3416 netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
3417 return;
3418 }
3419
3420 efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
3421 if (!efx->vpd_sn)
3422 return;
3423
3424 snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003425}
3426
3427
Ben Hutchings8ceee662008-04-27 12:55:59 +01003428/* Main body of NIC initialisation
3429 * This is called at module load (or hotplug insertion, theoretically).
3430 */
3431static int efx_pci_probe_main(struct efx_nic *efx)
3432{
3433 int rc;
3434
3435 /* Do start-of-day initialisation */
3436 rc = efx_probe_all(efx);
3437 if (rc)
3438 goto fail1;
3439
Ben Hutchingse8f14992010-12-07 19:47:34 +00003440 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003441
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00003442 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003443 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00003444 netif_err(efx, probe, efx->net_dev,
3445 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00003446 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003447 }
3448
3449 rc = efx_init_port(efx);
3450 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00003451 netif_err(efx, probe, efx->net_dev,
3452 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00003453 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003454 }
3455
Ben Hutchings152b6a62009-11-29 03:43:56 +00003456 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003457 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00003458 goto fail5;
Jon Cooper261e4d92013-04-15 18:51:54 +01003459 rc = efx_enable_interrupts(efx);
3460 if (rc)
3461 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003462
3463 return 0;
3464
Jon Cooper261e4d92013-04-15 18:51:54 +01003465 fail6:
3466 efx_nic_fini_interrupt(efx);
Ben Hutchings278c0622009-11-23 16:05:12 +00003467 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003468 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003469 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00003470 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003471 fail3:
3472 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003473 efx_remove_all(efx);
3474 fail1:
3475 return rc;
3476}
3477
Jon Cooper8a531402017-02-08 16:51:18 +00003478static int efx_pci_probe_post_io(struct efx_nic *efx)
3479{
3480 struct net_device *net_dev = efx->net_dev;
3481 int rc = efx_pci_probe_main(efx);
3482
3483 if (rc)
3484 return rc;
3485
3486 if (efx->type->sriov_init) {
3487 rc = efx->type->sriov_init(efx);
3488 if (rc)
3489 netif_err(efx, probe, efx->net_dev,
3490 "SR-IOV can't be enabled rc %d\n", rc);
3491 }
3492
3493 /* Determine netdevice features */
3494 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Edward Cree69787292017-10-31 14:29:47 +00003495 NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
Jon Cooper8a531402017-02-08 16:51:18 +00003496 if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
3497 net_dev->features |= NETIF_F_TSO6;
3498 /* Check whether device supports TSO */
3499 if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
3500 net_dev->features &= ~NETIF_F_ALL_TSO;
3501 /* Mask for features that also apply to VLAN devices */
3502 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
3503 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
3504 NETIF_F_RXCSUM);
3505
Edward Cree69787292017-10-31 14:29:47 +00003506 net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
3507
3508 /* Disable receiving frames with bad FCS, by default. */
3509 net_dev->features &= ~NETIF_F_RXALL;
Jon Cooper8a531402017-02-08 16:51:18 +00003510
3511 /* Disable VLAN filtering by default. It may be enforced if
3512 * the feature is fixed (i.e. VLAN filters are required to
3513 * receive VLAN tagged packets due to vPort restrictions).
3514 */
3515 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
3516 net_dev->features |= efx->fixed_features;
3517
3518 rc = efx_register_netdev(efx);
3519 if (!rc)
3520 return 0;
3521
3522 efx_pci_remove_main(efx);
3523 return rc;
3524}
3525
Ben Hutchings8ceee662008-04-27 12:55:59 +01003526/* NIC initialisation
3527 *
3528 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00003529 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003530 * sets up and registers the network devices with the kernel and hooks
3531 * the interrupt service routine. It does not prepare the device for
3532 * transmission; this is left to the first time one of the network
3533 * interfaces is brought up (i.e. efx_net_open).
3534 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05003535static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00003536 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01003537{
Ben Hutchings8ceee662008-04-27 12:55:59 +01003538 struct net_device *net_dev;
3539 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003540 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003541
3542 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00003543 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
3544 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003545 if (!net_dev)
3546 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01003547 efx = netdev_priv(net_dev);
3548 efx->type = (const struct efx_nic_type *) entry->driver_data;
Andrew Rybchenkoebfcd0f2016-06-15 17:43:20 +01003549 efx->fixed_features |= NETIF_F_HIGHDMA;
Andrew Rybchenkoeb7cfd82016-06-15 17:51:36 +01003550
Ben Hutchings8ceee662008-04-27 12:55:59 +01003551 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00003552 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01003553 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003554 if (rc)
3555 goto fail1;
3556
Ben Hutchings62776d02010-06-23 11:30:07 +00003557 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01003558 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003559
Shradha Shah6f7f8aa2015-05-06 01:00:07 +01003560 if (!efx->type->is_vf)
3561 efx_probe_vpd_strings(efx);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003562
Ben Hutchings8ceee662008-04-27 12:55:59 +01003563 /* Set up basic I/O (BAR mappings etc) */
3564 rc = efx_init_io(efx);
3565 if (rc)
3566 goto fail2;
3567
Jon Cooper8a531402017-02-08 16:51:18 +00003568 rc = efx_pci_probe_post_io(efx);
3569 if (rc) {
3570 /* On failure, retry once immediately.
3571 * If we aborted probe due to a scheduled reset, dismiss it.
3572 */
3573 efx->reset_pending = 0;
3574 rc = efx_pci_probe_post_io(efx);
3575 if (rc) {
3576 /* On another failure, retry once more
3577 * after a 50-305ms delay.
3578 */
3579 unsigned char r;
3580
3581 get_random_bytes(&r, 1);
3582 msleep((unsigned int)r + 50);
3583 efx->reset_pending = 0;
3584 rc = efx_pci_probe_post_io(efx);
3585 }
3586 }
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003587 if (rc)
3588 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08003589
Ben Hutchings62776d02010-06-23 11:30:07 +00003590 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003591
Ben Hutchings7c431612012-01-27 17:23:58 +00003592 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003593 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00003594 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003595 rtnl_unlock();
Bert Kenward09a04202015-12-23 08:58:15 +00003596 if (rc && rc != -EPERM)
Ben Hutchings7c431612012-01-27 17:23:58 +00003597 netif_warn(efx, probe, efx->net_dev,
3598 "failed to create MTDs (%d)\n", rc);
3599
Alexandre Rames626950d2013-01-14 17:20:22 +00003600 rc = pci_enable_pcie_error_reporting(pci_dev);
3601 if (rc && rc != -EINVAL)
Bert Kenward09a04202015-12-23 08:58:15 +00003602 netif_notice(efx, probe, efx->net_dev,
3603 "PCIE error reporting unavailable (%d).\n",
3604 rc);
Alexandre Rames626950d2013-01-14 17:20:22 +00003605
Jon Coopere5fbd972017-02-08 16:52:10 +00003606 if (efx->type->udp_tnl_push_ports)
3607 efx->type->udp_tnl_push_ports(efx);
3608
Ben Hutchings8ceee662008-04-27 12:55:59 +01003609 return 0;
3610
Ben Hutchings8ceee662008-04-27 12:55:59 +01003611 fail3:
3612 efx_fini_io(efx);
3613 fail2:
3614 efx_fini_struct(efx);
3615 fail1:
Steve Hodgson5e2a9112010-02-12 12:32:27 -08003616 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00003617 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003618 free_netdev(net_dev);
3619 return rc;
3620}
3621
Shradha Shah834e23d2015-05-06 00:55:58 +01003622/* efx_pci_sriov_configure returns the actual number of Virtual Functions
3623 * enabled on success
3624 */
3625#ifdef CONFIG_SFC_SRIOV
3626static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
3627{
3628 int rc;
3629 struct efx_nic *efx = pci_get_drvdata(dev);
3630
3631 if (efx->type->sriov_configure) {
3632 rc = efx->type->sriov_configure(efx, num_vfs);
3633 if (rc)
3634 return rc;
3635 else
3636 return num_vfs;
3637 } else
3638 return -EOPNOTSUPP;
3639}
3640#endif
3641
Ben Hutchings89c758f2009-11-29 03:43:07 +00003642static int efx_pm_freeze(struct device *dev)
3643{
3644 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3645
Ben Hutchings61da0262012-07-27 19:35:39 +01003646 rtnl_lock();
3647
Ben Hutchings6032fb52012-07-27 19:35:47 +01003648 if (efx->state != STATE_DISABLED) {
3649 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003650
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01003651 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003652
Ben Hutchings6032fb52012-07-27 19:35:47 +01003653 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003654 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01003655 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00003656
Ben Hutchings61da0262012-07-27 19:35:39 +01003657 rtnl_unlock();
3658
Ben Hutchings89c758f2009-11-29 03:43:07 +00003659 return 0;
3660}
3661
3662static int efx_pm_thaw(struct device *dev)
3663{
Jon Cooper261e4d92013-04-15 18:51:54 +01003664 int rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003665 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3666
Ben Hutchings61da0262012-07-27 19:35:39 +01003667 rtnl_lock();
3668
Ben Hutchings6032fb52012-07-27 19:35:47 +01003669 if (efx->state != STATE_DISABLED) {
Jon Cooper261e4d92013-04-15 18:51:54 +01003670 rc = efx_enable_interrupts(efx);
3671 if (rc)
3672 goto fail;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003673
Ben Hutchings6032fb52012-07-27 19:35:47 +01003674 mutex_lock(&efx->mac_lock);
3675 efx->phy_op->reconfigure(efx);
3676 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003677
Ben Hutchings6032fb52012-07-27 19:35:47 +01003678 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003679
Peter Dunning9c568fd2017-02-17 15:50:43 +00003680 efx_device_attach_if_not_resetting(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003681
Ben Hutchings6032fb52012-07-27 19:35:47 +01003682 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003683
Ben Hutchings6032fb52012-07-27 19:35:47 +01003684 efx->type->resume_wol(efx);
3685 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00003686
Ben Hutchings61da0262012-07-27 19:35:39 +01003687 rtnl_unlock();
3688
Steve Hodgson319ba642010-06-01 11:17:24 +00003689 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
3690 queue_work(reset_workqueue, &efx->reset_work);
3691
Ben Hutchings89c758f2009-11-29 03:43:07 +00003692 return 0;
Jon Cooper261e4d92013-04-15 18:51:54 +01003693
3694fail:
3695 rtnl_unlock();
3696
3697 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003698}
3699
3700static int efx_pm_poweroff(struct device *dev)
3701{
3702 struct pci_dev *pci_dev = to_pci_dev(dev);
3703 struct efx_nic *efx = pci_get_drvdata(pci_dev);
3704
3705 efx->type->fini(efx);
3706
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01003707 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003708
3709 pci_save_state(pci_dev);
3710 return pci_set_power_state(pci_dev, PCI_D3hot);
3711}
3712
3713/* Used for both resume and restore */
3714static int efx_pm_resume(struct device *dev)
3715{
3716 struct pci_dev *pci_dev = to_pci_dev(dev);
3717 struct efx_nic *efx = pci_get_drvdata(pci_dev);
3718 int rc;
3719
3720 rc = pci_set_power_state(pci_dev, PCI_D0);
3721 if (rc)
3722 return rc;
3723 pci_restore_state(pci_dev);
3724 rc = pci_enable_device(pci_dev);
3725 if (rc)
3726 return rc;
3727 pci_set_master(efx->pci_dev);
3728 rc = efx->type->reset(efx, RESET_TYPE_ALL);
3729 if (rc)
3730 return rc;
3731 rc = efx->type->init(efx);
3732 if (rc)
3733 return rc;
Jon Cooper261e4d92013-04-15 18:51:54 +01003734 rc = efx_pm_thaw(dev);
3735 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003736}
3737
3738static int efx_pm_suspend(struct device *dev)
3739{
3740 int rc;
3741
3742 efx_pm_freeze(dev);
3743 rc = efx_pm_poweroff(dev);
3744 if (rc)
3745 efx_pm_resume(dev);
3746 return rc;
3747}
3748
Ben Hutchings18e83e42012-01-05 19:05:20 +00003749static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00003750 .suspend = efx_pm_suspend,
3751 .resume = efx_pm_resume,
3752 .freeze = efx_pm_freeze,
3753 .thaw = efx_pm_thaw,
3754 .poweroff = efx_pm_poweroff,
3755 .restore = efx_pm_resume,
3756};
3757
Alexandre Rames626950d2013-01-14 17:20:22 +00003758/* A PCI error affecting this device was detected.
3759 * At this point MMIO and DMA may be disabled.
3760 * Stop the software path and request a slot reset.
3761 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003762static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
3763 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00003764{
3765 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3766 struct efx_nic *efx = pci_get_drvdata(pdev);
3767
3768 if (state == pci_channel_io_perm_failure)
3769 return PCI_ERS_RESULT_DISCONNECT;
3770
3771 rtnl_lock();
3772
3773 if (efx->state != STATE_DISABLED) {
3774 efx->state = STATE_RECOVERY;
3775 efx->reset_pending = 0;
3776
3777 efx_device_detach_sync(efx);
3778
3779 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003780 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00003781
3782 status = PCI_ERS_RESULT_NEED_RESET;
3783 } else {
3784 /* If the interface is disabled we don't want to do anything
3785 * with it.
3786 */
3787 status = PCI_ERS_RESULT_RECOVERED;
3788 }
3789
3790 rtnl_unlock();
3791
3792 pci_disable_device(pdev);
3793
3794 return status;
3795}
3796
Joe Perchesdbedd442015-03-06 20:49:12 -08003797/* Fake a successful reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003798static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00003799{
3800 struct efx_nic *efx = pci_get_drvdata(pdev);
3801 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3802 int rc;
3803
3804 if (pci_enable_device(pdev)) {
3805 netif_err(efx, hw, efx->net_dev,
3806 "Cannot re-enable PCI device after reset.\n");
3807 status = PCI_ERS_RESULT_DISCONNECT;
3808 }
3809
3810 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
3811 if (rc) {
3812 netif_err(efx, hw, efx->net_dev,
3813 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3814 /* Non-fatal error. Continue. */
3815 }
3816
3817 return status;
3818}
3819
3820/* Perform the actual reset and resume I/O operations. */
3821static void efx_io_resume(struct pci_dev *pdev)
3822{
3823 struct efx_nic *efx = pci_get_drvdata(pdev);
3824 int rc;
3825
3826 rtnl_lock();
3827
3828 if (efx->state == STATE_DISABLED)
3829 goto out;
3830
3831 rc = efx_reset(efx, RESET_TYPE_ALL);
3832 if (rc) {
3833 netif_err(efx, hw, efx->net_dev,
3834 "efx_reset failed after PCI error (%d)\n", rc);
3835 } else {
3836 efx->state = STATE_READY;
3837 netif_dbg(efx, hw, efx->net_dev,
3838 "Done resetting and resuming IO after PCI error.\n");
3839 }
3840
3841out:
3842 rtnl_unlock();
3843}
3844
3845/* For simplicity and reliability, we always require a slot reset and try to
3846 * reset the hardware when a pci error affecting the device is detected.
3847 * We leave both the link_reset and mmio_enabled callback unimplemented:
3848 * with our request for slot reset the mmio_enabled callback will never be
3849 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3850 */
Julia Lawallc300366b2015-11-14 11:06:57 +01003851static const struct pci_error_handlers efx_err_handlers = {
Alexandre Rames626950d2013-01-14 17:20:22 +00003852 .error_detected = efx_io_error_detected,
3853 .slot_reset = efx_io_slot_reset,
3854 .resume = efx_io_resume,
3855};
3856
Ben Hutchings8ceee662008-04-27 12:55:59 +01003857static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003858 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003859 .id_table = efx_pci_table,
3860 .probe = efx_pci_probe,
3861 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003862 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003863 .err_handler = &efx_err_handlers,
Shradha Shah834e23d2015-05-06 00:55:58 +01003864#ifdef CONFIG_SFC_SRIOV
3865 .sriov_configure = efx_pci_sriov_configure,
3866#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003867};
3868
3869/**************************************************************************
3870 *
3871 * Kernel module interface
3872 *
3873 *************************************************************************/
3874
3875module_param(interrupt_mode, uint, 0444);
3876MODULE_PARM_DESC(interrupt_mode,
3877 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3878
3879static int __init efx_init_module(void)
3880{
3881 int rc;
3882
3883 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3884
3885 rc = register_netdevice_notifier(&efx_netdev_notifier);
3886 if (rc)
3887 goto err_notifier;
3888
Shradha Shah7fa8d542015-05-06 00:55:13 +01003889#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003890 rc = efx_init_sriov();
3891 if (rc)
3892 goto err_sriov;
Shradha Shah7fa8d542015-05-06 00:55:13 +01003893#endif
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003894
Steve Hodgson1ab00622008-12-12 21:33:02 -08003895 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3896 if (!reset_workqueue) {
3897 rc = -ENOMEM;
3898 goto err_reset;
3899 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003900
3901 rc = pci_register_driver(&efx_pci_driver);
3902 if (rc < 0)
3903 goto err_pci;
3904
3905 return 0;
3906
3907 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003908 destroy_workqueue(reset_workqueue);
3909 err_reset:
Shradha Shah7fa8d542015-05-06 00:55:13 +01003910#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003911 efx_fini_sriov();
3912 err_sriov:
Shradha Shah7fa8d542015-05-06 00:55:13 +01003913#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003914 unregister_netdevice_notifier(&efx_netdev_notifier);
3915 err_notifier:
3916 return rc;
3917}
3918
3919static void __exit efx_exit_module(void)
3920{
3921 printk(KERN_INFO "Solarflare NET driver unloading\n");
3922
3923 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003924 destroy_workqueue(reset_workqueue);
Shradha Shah7fa8d542015-05-06 00:55:13 +01003925#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003926 efx_fini_sriov();
Shradha Shah7fa8d542015-05-06 00:55:13 +01003927#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003928 unregister_netdevice_notifier(&efx_netdev_notifier);
3929
3930}
3931
3932module_init(efx_init_module);
3933module_exit(efx_exit_module);
3934
Ben Hutchings906bb262009-11-29 15:16:19 +00003935MODULE_AUTHOR("Solarflare Communications and "
3936 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings6a350fd2014-02-12 19:00:07 +00003937MODULE_DESCRIPTION("Solarflare network driver");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003938MODULE_LICENSE("GPL");
3939MODULE_DEVICE_TABLE(pci, efx_pci_table);
Edward Cree14077e92017-01-03 15:46:00 +00003940MODULE_VERSION(EFX_DRIVER_VERSION);