blob: ad4a354ce570e143a741e7ab7155ae84a8a5df34 [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
Bert Kenwarda83762d2018-04-19 17:37:25 +01001553#if defined(CONFIG_SMP)
1554static void efx_set_interrupt_affinity(struct efx_nic *efx)
1555{
1556 struct efx_channel *channel;
1557 unsigned int cpu;
1558
1559 efx_for_each_channel(channel, efx) {
1560 cpu = cpumask_local_spread(channel->channel,
1561 pcibus_to_node(efx->pci_dev->bus));
1562 irq_set_affinity_hint(channel->irq, cpumask_of(cpu));
1563 }
1564}
1565
1566static void efx_clear_interrupt_affinity(struct efx_nic *efx)
1567{
1568 struct efx_channel *channel;
1569
1570 efx_for_each_channel(channel, efx)
1571 irq_set_affinity_hint(channel->irq, NULL);
1572}
1573#else
1574static void
1575efx_set_interrupt_affinity(struct efx_nic *efx __attribute__ ((unused)))
1576{
1577}
1578
1579static void
1580efx_clear_interrupt_affinity(struct efx_nic *efx __attribute__ ((unused)))
1581{
1582}
1583#endif /* CONFIG_SMP */
1584
Jon Cooper261e4d92013-04-15 18:51:54 +01001585static int efx_soft_enable_interrupts(struct efx_nic *efx)
Ben Hutchingsd8291182012-10-05 23:35:41 +01001586{
Jon Cooper261e4d92013-04-15 18:51:54 +01001587 struct efx_channel *channel, *end_channel;
1588 int rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001589
1590 BUG_ON(efx->state == STATE_DISABLED);
1591
1592 efx->irq_soft_enabled = true;
1593 smp_wmb();
1594
1595 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001596 if (!channel->type->keep_eventq) {
1597 rc = efx_init_eventq(channel);
1598 if (rc)
1599 goto fail;
1600 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001601 efx_start_eventq(channel);
1602 }
1603
1604 efx_mcdi_mode_event(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +01001605
1606 return 0;
1607fail:
1608 end_channel = channel;
1609 efx_for_each_channel(channel, efx) {
1610 if (channel == end_channel)
1611 break;
1612 efx_stop_eventq(channel);
1613 if (!channel->type->keep_eventq)
1614 efx_fini_eventq(channel);
1615 }
1616
1617 return rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001618}
1619
1620static void efx_soft_disable_interrupts(struct efx_nic *efx)
1621{
1622 struct efx_channel *channel;
1623
1624 if (efx->state == STATE_DISABLED)
1625 return;
1626
1627 efx_mcdi_mode_poll(efx);
1628
1629 efx->irq_soft_enabled = false;
1630 smp_wmb();
1631
1632 if (efx->legacy_irq)
1633 synchronize_irq(efx->legacy_irq);
1634
1635 efx_for_each_channel(channel, efx) {
1636 if (channel->irq)
1637 synchronize_irq(channel->irq);
1638
1639 efx_stop_eventq(channel);
1640 if (!channel->type->keep_eventq)
1641 efx_fini_eventq(channel);
1642 }
Ben Hutchingscade7152013-08-27 23:12:31 +01001643
1644 /* Flush the asynchronous MCDI request queue */
1645 efx_mcdi_flush_async(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01001646}
1647
Jon Cooper261e4d92013-04-15 18:51:54 +01001648static int efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001649{
Jon Cooper261e4d92013-04-15 18:51:54 +01001650 struct efx_channel *channel, *end_channel;
1651 int rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001652
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001653 BUG_ON(efx->state == STATE_DISABLED);
1654
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001655 if (efx->eeh_disabled_legacy_irq) {
1656 enable_irq(efx->legacy_irq);
1657 efx->eeh_disabled_legacy_irq = false;
1658 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001659
Ben Hutchings86094f72013-08-21 19:51:04 +01001660 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001661
1662 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001663 if (channel->type->keep_eventq) {
1664 rc = efx_init_eventq(channel);
1665 if (rc)
1666 goto fail;
1667 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001668 }
1669
Jon Cooper261e4d92013-04-15 18:51:54 +01001670 rc = efx_soft_enable_interrupts(efx);
1671 if (rc)
1672 goto fail;
1673
1674 return 0;
1675
1676fail:
1677 end_channel = channel;
1678 efx_for_each_channel(channel, efx) {
1679 if (channel == end_channel)
1680 break;
1681 if (channel->type->keep_eventq)
1682 efx_fini_eventq(channel);
1683 }
1684
1685 efx->type->irq_disable_non_ev(efx);
1686
1687 return rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001688}
1689
Ben Hutchingsd8291182012-10-05 23:35:41 +01001690static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001691{
1692 struct efx_channel *channel;
1693
Ben Hutchingsd8291182012-10-05 23:35:41 +01001694 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001695
1696 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001697 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001698 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001699 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001700
Ben Hutchings86094f72013-08-21 19:51:04 +01001701 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001702}
1703
Ben Hutchings8ceee662008-04-27 12:55:59 +01001704static void efx_remove_interrupts(struct efx_nic *efx)
1705{
1706 struct efx_channel *channel;
1707
1708 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001709 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001710 channel->irq = 0;
1711 pci_disable_msi(efx->pci_dev);
1712 pci_disable_msix(efx->pci_dev);
1713
1714 /* Remove legacy interrupt */
1715 efx->legacy_irq = 0;
1716}
1717
Ben Hutchings8831da72008-09-01 12:47:48 +01001718static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001719{
Ben Hutchings602a5322011-05-16 17:32:39 +01001720 struct efx_channel *channel;
1721 struct efx_tx_queue *tx_queue;
1722
Ben Hutchings97653432011-01-12 18:26:56 +00001723 efx->tx_channel_offset =
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001724 efx_separate_tx_channels ?
1725 efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001726
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001727 /* We need to mark which channels really have RX and TX
1728 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001729 * RX-only and TX-only channels.
1730 */
1731 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001732 if (channel->channel < efx->n_rx_channels)
1733 channel->rx_queue.core_index = channel->channel;
1734 else
1735 channel->rx_queue.core_index = -1;
1736
Ben Hutchings602a5322011-05-16 17:32:39 +01001737 efx_for_each_channel_tx_queue(tx_queue, channel)
1738 tx_queue->queue -= (efx->tx_channel_offset *
1739 EFX_TXQ_TYPES);
1740 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001741}
1742
1743static int efx_probe_nic(struct efx_nic *efx)
1744{
1745 int rc;
1746
Ben Hutchings62776d02010-06-23 11:30:07 +00001747 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001748
1749 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001750 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001751 if (rc)
1752 return rc;
1753
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001754 do {
1755 if (!efx->max_channels || !efx->max_tx_channels) {
1756 netif_err(efx, drv, efx->net_dev,
1757 "Insufficient resources to allocate"
1758 " any channels\n");
1759 rc = -ENOSPC;
1760 goto fail1;
1761 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001762
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001763 /* Determine the number of channels and queues by trying
1764 * to hook in MSI-X interrupts.
1765 */
1766 rc = efx_probe_interrupts(efx);
1767 if (rc)
1768 goto fail1;
Daniel Pieczko52ad7622014-04-01 13:10:34 +01001769
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001770 efx_set_channels(efx);
1771
1772 /* dimension_resources can fail with EAGAIN */
1773 rc = efx->type->dimension_resources(efx);
1774 if (rc != 0 && rc != -EAGAIN)
1775 goto fail2;
1776
1777 if (rc == -EAGAIN)
1778 /* try again with new max_channels */
1779 efx_remove_interrupts(efx);
1780
1781 } while (rc == -EAGAIN);
Ben Hutchings28e47c42012-02-15 01:58:49 +00001782
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001783 if (efx->n_channels > 1)
Edward Cree42356d92018-03-08 15:45:17 +00001784 netdev_rss_key_fill(efx->rss_context.rx_hash_key,
1785 sizeof(efx->rss_context.rx_hash_key));
1786 efx_set_default_rx_indir_table(efx, &efx->rss_context);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001787
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001788 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1789 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001790
1791 /* Initialise the interrupt moderation settings */
Bert Kenward539de7c2016-08-11 13:02:09 +01001792 efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
Ben Hutchings9e393b32011-09-05 07:43:04 +00001793 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1794 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001795
1796 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001797
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001798fail2:
1799 efx_remove_interrupts(efx);
1800fail1:
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001801 efx->type->remove(efx);
1802 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001803}
1804
1805static void efx_remove_nic(struct efx_nic *efx)
1806{
Ben Hutchings62776d02010-06-23 11:30:07 +00001807 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001808
1809 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001810 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001811}
1812
Ben Hutchingsadd72472012-11-08 01:46:53 +00001813static int efx_probe_filters(struct efx_nic *efx)
1814{
1815 int rc;
1816
Edward Cree0d322412015-05-20 11:10:03 +01001817 init_rwsem(&efx->filter_sem);
Martin Habetsd2489532016-06-15 17:48:49 +01001818 mutex_lock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01001819 down_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001820 rc = efx->type->filter_table_probe(efx);
1821 if (rc)
Edward Cree0d322412015-05-20 11:10:03 +01001822 goto out_unlock;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001823
1824#ifdef CONFIG_RFS_ACCEL
1825 if (efx->type->offload_features & NETIF_F_NTUPLE) {
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001826 struct efx_channel *channel;
1827 int i, success = 1;
1828
1829 efx_for_each_channel(channel, efx) {
1830 channel->rps_flow_id =
1831 kcalloc(efx->type->max_rx_ip_filters,
1832 sizeof(*channel->rps_flow_id),
1833 GFP_KERNEL);
1834 if (!channel->rps_flow_id)
1835 success = 0;
1836 else
1837 for (i = 0;
1838 i < efx->type->max_rx_ip_filters;
1839 ++i)
1840 channel->rps_flow_id[i] =
1841 RPS_FLOW_ID_INVALID;
1842 }
1843
1844 if (!success) {
1845 efx_for_each_channel(channel, efx)
1846 kfree(channel->rps_flow_id);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001847 efx->type->filter_table_remove(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001848 rc = -ENOMEM;
1849 goto out_unlock;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001850 }
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001851
1852 efx->rps_expire_index = efx->rps_expire_channel = 0;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001853 }
1854#endif
Edward Cree0d322412015-05-20 11:10:03 +01001855out_unlock:
1856 up_write(&efx->filter_sem);
Martin Habetsd2489532016-06-15 17:48:49 +01001857 mutex_unlock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01001858 return rc;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001859}
1860
1861static void efx_remove_filters(struct efx_nic *efx)
1862{
1863#ifdef CONFIG_RFS_ACCEL
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001864 struct efx_channel *channel;
1865
1866 efx_for_each_channel(channel, efx)
1867 kfree(channel->rps_flow_id);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001868#endif
Edward Cree0d322412015-05-20 11:10:03 +01001869 down_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001870 efx->type->filter_table_remove(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001871 up_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001872}
1873
1874static void efx_restore_filters(struct efx_nic *efx)
1875{
Edward Cree0d322412015-05-20 11:10:03 +01001876 down_read(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001877 efx->type->filter_table_restore(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001878 up_read(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001879}
1880
Ben Hutchings8ceee662008-04-27 12:55:59 +01001881/**************************************************************************
1882 *
1883 * NIC startup/shutdown
1884 *
1885 *************************************************************************/
1886
1887static int efx_probe_all(struct efx_nic *efx)
1888{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001889 int rc;
1890
Ben Hutchings8ceee662008-04-27 12:55:59 +01001891 rc = efx_probe_nic(efx);
1892 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001893 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001894 goto fail1;
1895 }
1896
Ben Hutchings8ceee662008-04-27 12:55:59 +01001897 rc = efx_probe_port(efx);
1898 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001899 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001900 goto fail2;
1901 }
1902
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001903 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1904 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1905 rc = -EINVAL;
1906 goto fail3;
1907 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001908 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001909
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001910#ifdef CONFIG_SFC_SRIOV
1911 rc = efx->type->vswitching_probe(efx);
1912 if (rc) /* not fatal; the PF will still work fine */
1913 netif_warn(efx, probe, efx->net_dev,
1914 "failed to setup vswitching rc=%d;"
1915 " VFs may not function\n", rc);
1916#endif
1917
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001918 rc = efx_probe_filters(efx);
1919 if (rc) {
1920 netif_err(efx, probe, efx->net_dev,
1921 "failed to create filter tables\n");
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001922 goto fail4;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001923 }
1924
Ben Hutchings7f967c02012-02-13 23:45:02 +00001925 rc = efx_probe_channels(efx);
1926 if (rc)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001927 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001928
Ben Hutchings8ceee662008-04-27 12:55:59 +01001929 return 0;
1930
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001931 fail5:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001932 efx_remove_filters(efx);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001933 fail4:
1934#ifdef CONFIG_SFC_SRIOV
1935 efx->type->vswitching_remove(efx);
1936#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01001937 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001938 efx_remove_port(efx);
1939 fail2:
1940 efx_remove_nic(efx);
1941 fail1:
1942 return rc;
1943}
1944
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001945/* If the interface is supposed to be running but is not, start
1946 * the hardware and software data path, regular activity for the port
1947 * (MAC statistics, link polling, etc.) and schedule the port to be
1948 * reconfigured. Interrupts must already be enabled. This function
1949 * is safe to call multiple times, so long as the NIC is not disabled.
1950 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001951 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001952static void efx_start_all(struct efx_nic *efx)
1953{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001954 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001955 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001956
1957 /* Check that it is appropriate to restart the interface. All
1958 * of these flags are safe to read under just the rtnl lock */
Edward Creee2835462014-04-16 19:27:48 +01001959 if (efx->port_enabled || !netif_running(efx->net_dev) ||
1960 efx->reset_pending)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001961 return;
1962
Ben Hutchings8ceee662008-04-27 12:55:59 +01001963 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001964 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001965
Alexandre Rames626950d2013-01-14 17:20:22 +00001966 /* Start the hardware monitor if there is one */
1967 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001968 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1969 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001970
Edward Cree5a6681e2016-11-28 18:55:34 +00001971 /* Link state detection is normally event-driven; we have
Alexandre Rames626950d2013-01-14 17:20:22 +00001972 * to poll now because we could have missed a change
1973 */
Edward Cree5a6681e2016-11-28 18:55:34 +00001974 mutex_lock(&efx->mac_lock);
1975 if (efx->phy_op->poll(efx))
1976 efx_link_status_changed(efx);
1977 mutex_unlock(&efx->mac_lock);
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001978
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001979 efx->type->start_stats(efx);
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001980 efx->type->pull_stats(efx);
1981 spin_lock_bh(&efx->stats_lock);
1982 efx->type->update_stats(efx, NULL, NULL);
1983 spin_unlock_bh(&efx->stats_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001984}
1985
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001986/* Quiesce the hardware and software data path, and regular activity
1987 * for the port without bringing the link down. Safe to call multiple
1988 * times with the NIC in almost any state, but interrupts should be
1989 * enabled. Requires the RTNL lock.
1990 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001991static void efx_stop_all(struct efx_nic *efx)
1992{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001993 EFX_ASSERT_RESET_SERIALISED(efx);
1994
1995 /* port_enabled can be read safely under the rtnl lock */
1996 if (!efx->port_enabled)
1997 return;
1998
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001999 /* update stats before we go down so we can accurately count
2000 * rx_nodesc_drops
2001 */
2002 efx->type->pull_stats(efx);
2003 spin_lock_bh(&efx->stats_lock);
2004 efx->type->update_stats(efx, NULL, NULL);
2005 spin_unlock_bh(&efx->stats_lock);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002006 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002007 efx_stop_port(efx);
2008
Ben Hutchings29c69a42013-01-28 19:01:06 +00002009 /* Stop the kernel transmit interface. This is only valid if
2010 * the device is stopped or detached; otherwise the watchdog
2011 * may fire immediately.
2012 */
2013 WARN_ON(netif_running(efx->net_dev) &&
2014 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002015 netif_tx_disable(efx->net_dev);
2016
2017 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002018}
2019
2020static void efx_remove_all(struct efx_nic *efx)
2021{
Ben Hutchings46426102010-09-10 06:42:33 +00002022 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00002023 efx_remove_filters(efx);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01002024#ifdef CONFIG_SFC_SRIOV
2025 efx->type->vswitching_remove(efx);
2026#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002027 efx_remove_port(efx);
2028 efx_remove_nic(efx);
2029}
2030
Ben Hutchings8ceee662008-04-27 12:55:59 +01002031/**************************************************************************
2032 *
2033 * Interrupt moderation
2034 *
2035 **************************************************************************/
Bert Kenward539de7c2016-08-11 13:02:09 +01002036unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00002037{
Ben Hutchingsb548f972011-09-05 07:41:44 +00002038 if (usecs == 0)
2039 return 0;
Bert Kenward539de7c2016-08-11 13:02:09 +01002040 if (usecs * 1000 < efx->timer_quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00002041 return 1; /* never round down to 0 */
Bert Kenward539de7c2016-08-11 13:02:09 +01002042 return usecs * 1000 / efx->timer_quantum_ns;
2043}
2044
2045unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
2046{
2047 /* We must round up when converting ticks to microseconds
2048 * because we round down when converting the other way.
2049 */
2050 return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00002051}
2052
Ben Hutchings8ceee662008-04-27 12:55:59 +01002053/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00002054int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
2055 unsigned int rx_usecs, bool rx_adaptive,
2056 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002057{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002058 struct efx_channel *channel;
Bert Kenwardd95e3292016-08-11 13:02:36 +01002059 unsigned int timer_max_us;
2060
Ben Hutchings8ceee662008-04-27 12:55:59 +01002061 EFX_ASSERT_RESET_SERIALISED(efx);
2062
Bert Kenwardd95e3292016-08-11 13:02:36 +01002063 timer_max_us = efx->timer_max_ns / 1000;
2064
2065 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
Ben Hutchings9e393b32011-09-05 07:43:04 +00002066 return -EINVAL;
2067
Bert Kenward539de7c2016-08-11 13:02:09 +01002068 if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
Ben Hutchings9e393b32011-09-05 07:43:04 +00002069 !rx_may_override_tx) {
2070 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
2071 "RX and TX IRQ moderation must be equal\n");
2072 return -EINVAL;
2073 }
2074
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00002075 efx->irq_rx_adaptive = rx_adaptive;
Bert Kenward539de7c2016-08-11 13:02:09 +01002076 efx->irq_rx_moderation_us = rx_usecs;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002077 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00002078 if (efx_channel_has_rx_queue(channel))
Bert Kenward539de7c2016-08-11 13:02:09 +01002079 channel->irq_moderation_us = rx_usecs;
Ben Hutchings525da902011-02-07 23:04:38 +00002080 else if (efx_channel_has_tx_queues(channel))
Bert Kenward539de7c2016-08-11 13:02:09 +01002081 channel->irq_moderation_us = tx_usecs;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00002082 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00002083
2084 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002085}
2086
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002087void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
2088 unsigned int *rx_usecs, bool *rx_adaptive)
2089{
2090 *rx_adaptive = efx->irq_rx_adaptive;
Bert Kenward539de7c2016-08-11 13:02:09 +01002091 *rx_usecs = efx->irq_rx_moderation_us;
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002092
2093 /* If channels are shared between RX and TX, so is IRQ
2094 * moderation. Otherwise, IRQ moderation is the same for all
2095 * TX channels and is not adaptive.
2096 */
Bert Kenward539de7c2016-08-11 13:02:09 +01002097 if (efx->tx_channel_offset == 0) {
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002098 *tx_usecs = *rx_usecs;
Bert Kenward539de7c2016-08-11 13:02:09 +01002099 } else {
2100 struct efx_channel *tx_channel;
2101
2102 tx_channel = efx->channel[efx->tx_channel_offset];
2103 *tx_usecs = tx_channel->irq_moderation_us;
2104 }
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002105}
2106
Ben Hutchings8ceee662008-04-27 12:55:59 +01002107/**************************************************************************
2108 *
2109 * Hardware monitor
2110 *
2111 **************************************************************************/
2112
Ben Hutchingse254c272010-09-20 08:44:10 +00002113/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002114static void efx_monitor(struct work_struct *data)
2115{
2116 struct efx_nic *efx = container_of(data, struct efx_nic,
2117 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002118
Ben Hutchings62776d02010-06-23 11:30:07 +00002119 netif_vdbg(efx, timer, efx->net_dev,
2120 "hardware monitor executing on CPU %d\n",
2121 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002122 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002123
Ben Hutchings8ceee662008-04-27 12:55:59 +01002124 /* If the mac_lock is already held then it is likely a port
2125 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00002126 * most of the work of monitor() anyway. */
2127 if (mutex_trylock(&efx->mac_lock)) {
2128 if (efx->port_enabled)
2129 efx->type->monitor(efx);
2130 mutex_unlock(&efx->mac_lock);
2131 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002132
Ben Hutchings8ceee662008-04-27 12:55:59 +01002133 queue_delayed_work(efx->workqueue, &efx->monitor_work,
2134 efx_monitor_interval);
2135}
2136
2137/**************************************************************************
2138 *
2139 * ioctls
2140 *
2141 *************************************************************************/
2142
2143/* Net device ioctl
2144 * Context: process, rtnl_lock() held.
2145 */
2146static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
2147{
Ben Hutchings767e4682008-09-01 12:43:14 +01002148 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00002149 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002150
Stuart Hodgson7c236c42012-09-03 11:09:36 +01002151 if (cmd == SIOCSHWTSTAMP)
Ben Hutchings433dc9b2013-11-14 01:26:21 +00002152 return efx_ptp_set_ts_config(efx, ifr);
2153 if (cmd == SIOCGHWTSTAMP)
2154 return efx_ptp_get_ts_config(efx, ifr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01002155
Ben Hutchings68e7f452009-04-29 08:05:08 +00002156 /* Convert phy_id from older PRTAD/DEVAD format */
2157 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
2158 (data->phy_id & 0xfc00) == 0x0400)
2159 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
2160
2161 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002162}
2163
2164/**************************************************************************
2165 *
2166 * NAPI interface
2167 *
2168 **************************************************************************/
2169
Ben Hutchings7f967c02012-02-13 23:45:02 +00002170static void efx_init_napi_channel(struct efx_channel *channel)
2171{
2172 struct efx_nic *efx = channel->efx;
2173
2174 channel->napi_dev = efx->net_dev;
2175 netif_napi_add(channel->napi_dev, &channel->napi_str,
2176 efx_poll, napi_weight);
2177}
2178
Ben Hutchingse8f14992010-12-07 19:47:34 +00002179static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002180{
2181 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002182
Ben Hutchings7f967c02012-02-13 23:45:02 +00002183 efx_for_each_channel(channel, efx)
2184 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00002185}
2186
2187static void efx_fini_napi_channel(struct efx_channel *channel)
2188{
Eric Dumazet973334a2016-11-16 06:01:47 -08002189 if (channel->napi_dev)
Ben Hutchingse8f14992010-12-07 19:47:34 +00002190 netif_napi_del(&channel->napi_str);
Eric Dumazet973334a2016-11-16 06:01:47 -08002191
Ben Hutchingse8f14992010-12-07 19:47:34 +00002192 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002193}
2194
2195static void efx_fini_napi(struct efx_nic *efx)
2196{
2197 struct efx_channel *channel;
2198
Ben Hutchingse8f14992010-12-07 19:47:34 +00002199 efx_for_each_channel(channel, efx)
2200 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002201}
2202
2203/**************************************************************************
2204 *
2205 * Kernel netpoll interface
2206 *
2207 *************************************************************************/
2208
2209#ifdef CONFIG_NET_POLL_CONTROLLER
2210
2211/* Although in the common case interrupts will be disabled, this is not
2212 * guaranteed. However, all our work happens inside the NAPI callback,
2213 * so no locking is required.
2214 */
2215static void efx_netpoll(struct net_device *net_dev)
2216{
Ben Hutchings767e4682008-09-01 12:43:14 +01002217 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002218 struct efx_channel *channel;
2219
Ben Hutchings64ee3122008-09-01 12:47:38 +01002220 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002221 efx_schedule_channel(channel);
2222}
2223
2224#endif
2225
2226/**************************************************************************
2227 *
2228 * Kernel net device interface
2229 *
2230 *************************************************************************/
2231
2232/* Context: process, rtnl_lock() held. */
Shradha Shahe340be92015-05-20 11:11:03 +01002233int efx_net_open(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002234{
Ben Hutchings767e4682008-09-01 12:43:14 +01002235 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002236 int rc;
2237
Ben Hutchings62776d02010-06-23 11:30:07 +00002238 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
2239 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01002240
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002241 rc = efx_check_disabled(efx);
2242 if (rc)
2243 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01002244 if (efx->phy_mode & PHY_MODE_SPECIAL)
2245 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002246 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
2247 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01002248
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00002249 /* Notify the kernel of the link state polled during driver load,
2250 * before the monitor starts running */
2251 efx_link_status_changed(efx);
2252
Ben Hutchings8ceee662008-04-27 12:55:59 +01002253 efx_start_all(efx);
Peter Dunning9c568fd2017-02-17 15:50:43 +00002254 if (efx->state == STATE_DISABLED || efx->reset_pending)
2255 netif_device_detach(efx->net_dev);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002256 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002257 return 0;
2258}
2259
2260/* Context: process, rtnl_lock() held.
2261 * Note that the kernel will ignore our return code; this method
2262 * should really be a void.
2263 */
Shradha Shahe340be92015-05-20 11:11:03 +01002264int efx_net_stop(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002265{
Ben Hutchings767e4682008-09-01 12:43:14 +01002266 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002267
Ben Hutchings62776d02010-06-23 11:30:07 +00002268 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
2269 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01002270
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002271 /* Stop the device and flush all the channels */
2272 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002273
2274 return 0;
2275}
2276
Ben Hutchings5b9e2072008-05-16 21:18:14 +01002277/* Context: process, dev_base_lock or RTNL held, non-blocking. */
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002278static void efx_net_stats(struct net_device *net_dev,
2279 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002280{
Ben Hutchings767e4682008-09-01 12:43:14 +01002281 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002282
Ben Hutchings55edc6e2009-11-25 16:11:35 +00002283 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00002284 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01002285 spin_unlock_bh(&efx->stats_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002286}
2287
2288/* Context: netif_tx_lock held, BHs disabled. */
2289static void efx_watchdog(struct net_device *net_dev)
2290{
Ben Hutchings767e4682008-09-01 12:43:14 +01002291 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002292
Ben Hutchings62776d02010-06-23 11:30:07 +00002293 netif_err(efx, tx_err, efx->net_dev,
2294 "TX stuck with port_enabled=%d: resetting channels\n",
2295 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002296
Ben Hutchings739bb23d2008-11-04 20:35:36 +00002297 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002298}
2299
2300
2301/* Context: process, rtnl_lock() held. */
2302static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2303{
Ben Hutchings767e4682008-09-01 12:43:14 +01002304 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002305 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002306
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002307 rc = efx_check_disabled(efx);
2308 if (rc)
2309 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002310
Ben Hutchings62776d02010-06-23 11:30:07 +00002311 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002312
Ben Hutchings29c69a42013-01-28 19:01:06 +00002313 efx_device_detach_sync(efx);
2314 efx_stop_all(efx);
2315
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002316 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002317 net_dev->mtu = new_mtu;
Edward Cree0d322412015-05-20 11:10:03 +01002318 efx_mac_reconfigure(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002319 mutex_unlock(&efx->mac_lock);
2320
Ben Hutchings8ceee662008-04-27 12:55:59 +01002321 efx_start_all(efx);
Peter Dunning9c568fd2017-02-17 15:50:43 +00002322 efx_device_attach_if_not_resetting(efx);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002323 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002324}
2325
2326static int efx_set_mac_address(struct net_device *net_dev, void *data)
2327{
Ben Hutchings767e4682008-09-01 12:43:14 +01002328 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002329 struct sockaddr *addr = data;
Ben Hutchingse0b3ae32014-02-12 18:59:54 +00002330 u8 *new_addr = addr->sa_data;
Shradha Shahcfc77c22015-05-20 11:09:30 +01002331 u8 old_addr[6];
2332 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002333
Ben Hutchings8ceee662008-04-27 12:55:59 +01002334 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002335 netif_err(efx, drv, efx->net_dev,
2336 "invalid ethernet MAC address requested: %pM\n",
2337 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002338 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002339 }
2340
Shradha Shahcfc77c22015-05-20 11:09:30 +01002341 /* save old address */
2342 ether_addr_copy(old_addr, net_dev->dev_addr);
Edward Creecd84ff42014-03-07 18:27:41 +00002343 ether_addr_copy(net_dev->dev_addr, new_addr);
Shradha Shah910c8782015-05-20 11:12:48 +01002344 if (efx->type->set_mac_address) {
2345 rc = efx->type->set_mac_address(efx);
Shradha Shahcfc77c22015-05-20 11:09:30 +01002346 if (rc) {
2347 ether_addr_copy(net_dev->dev_addr, old_addr);
2348 return rc;
2349 }
2350 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002351
2352 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002353 mutex_lock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01002354 efx_mac_reconfigure(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002355 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002356
2357 return 0;
2358}
2359
Ben Hutchingsa816f752008-09-01 12:49:12 +01002360/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002361static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002362{
Ben Hutchings767e4682008-09-01 12:43:14 +01002363 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002364
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002365 if (efx->port_enabled)
2366 queue_work(efx->workqueue, &efx->mac_work);
2367 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002368}
2369
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002370static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002371{
2372 struct efx_nic *efx = netdev_priv(net_dev);
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002373 int rc;
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002374
2375 /* If disabling RX n-tuple filtering, clear existing filters */
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002376 if (net_dev->features & ~data & NETIF_F_NTUPLE) {
2377 rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2378 if (rc)
2379 return rc;
2380 }
2381
Edward Cree69787292017-10-31 14:29:47 +00002382 /* If Rx VLAN filter is changed, update filters via mac_reconfigure.
2383 * If rx-fcs is changed, mac_reconfigure updates that too.
2384 */
2385 if ((net_dev->features ^ data) & (NETIF_F_HW_VLAN_CTAG_FILTER |
2386 NETIF_F_RXFCS)) {
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002387 /* efx_set_rx_mode() will schedule MAC work to update filters
2388 * when a new features are finally set in net_dev.
2389 */
2390 efx_set_rx_mode(net_dev);
2391 }
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002392
2393 return 0;
2394}
2395
Wei Yongjunb40296f2017-01-11 16:16:12 +00002396static int efx_get_phys_port_id(struct net_device *net_dev,
2397 struct netdev_phys_item_id *ppid)
Bert Kenward08a7b29b2017-01-10 16:23:33 +00002398{
2399 struct efx_nic *efx = netdev_priv(net_dev);
2400
2401 if (efx->type->get_phys_port_id)
2402 return efx->type->get_phys_port_id(efx, ppid);
2403 else
2404 return -EOPNOTSUPP;
2405}
2406
Bert Kenwardac019f02017-01-10 16:23:56 +00002407static int efx_get_phys_port_name(struct net_device *net_dev,
2408 char *name, size_t len)
2409{
2410 struct efx_nic *efx = netdev_priv(net_dev);
2411
2412 if (snprintf(name, len, "p%u", efx->port_num) >= len)
2413 return -EINVAL;
2414 return 0;
2415}
2416
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002417static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
2418{
2419 struct efx_nic *efx = netdev_priv(net_dev);
2420
2421 if (efx->type->vlan_rx_add_vid)
2422 return efx->type->vlan_rx_add_vid(efx, proto, vid);
2423 else
2424 return -EOPNOTSUPP;
2425}
2426
2427static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
2428{
2429 struct efx_nic *efx = netdev_priv(net_dev);
2430
2431 if (efx->type->vlan_rx_kill_vid)
2432 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
2433 else
2434 return -EOPNOTSUPP;
2435}
2436
Jon Coopere5fbd972017-02-08 16:52:10 +00002437static int efx_udp_tunnel_type_map(enum udp_parsable_tunnel_type in)
2438{
2439 switch (in) {
2440 case UDP_TUNNEL_TYPE_VXLAN:
2441 return TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
2442 case UDP_TUNNEL_TYPE_GENEVE:
2443 return TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
2444 default:
2445 return -1;
2446 }
2447}
2448
2449static void efx_udp_tunnel_add(struct net_device *dev, struct udp_tunnel_info *ti)
2450{
2451 struct efx_nic *efx = netdev_priv(dev);
2452 struct efx_udp_tunnel tnl;
2453 int efx_tunnel_type;
2454
2455 efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
2456 if (efx_tunnel_type < 0)
2457 return;
2458
2459 tnl.type = (u16)efx_tunnel_type;
2460 tnl.port = ti->port;
2461
2462 if (efx->type->udp_tnl_add_port)
2463 (void)efx->type->udp_tnl_add_port(efx, tnl);
2464}
2465
2466static void efx_udp_tunnel_del(struct net_device *dev, struct udp_tunnel_info *ti)
2467{
2468 struct efx_nic *efx = netdev_priv(dev);
2469 struct efx_udp_tunnel tnl;
2470 int efx_tunnel_type;
2471
2472 efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
2473 if (efx_tunnel_type < 0)
2474 return;
2475
2476 tnl.type = (u16)efx_tunnel_type;
2477 tnl.port = ti->port;
2478
Dan Carpenterc04ca612017-03-22 12:10:02 +03002479 if (efx->type->udp_tnl_del_port)
Jon Coopere5fbd972017-02-08 16:52:10 +00002480 (void)efx->type->udp_tnl_del_port(efx, tnl);
2481}
2482
Shradha Shah7fa8d542015-05-06 00:55:13 +01002483static const struct net_device_ops efx_netdev_ops = {
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002484 .ndo_open = efx_net_open,
2485 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002486 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002487 .ndo_tx_timeout = efx_watchdog,
2488 .ndo_start_xmit = efx_hard_start_xmit,
2489 .ndo_validate_addr = eth_validate_addr,
2490 .ndo_do_ioctl = efx_ioctl,
2491 .ndo_change_mtu = efx_change_mtu,
2492 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002493 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002494 .ndo_set_features = efx_set_features,
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002495 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
2496 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002497#ifdef CONFIG_SFC_SRIOV
Shradha Shah7fa8d542015-05-06 00:55:13 +01002498 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2499 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2500 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2501 .ndo_get_vf_config = efx_sriov_get_vf_config,
Edward Cree4392dc62015-05-20 11:12:13 +01002502 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002503#endif
Bert Kenward08a7b29b2017-01-10 16:23:33 +00002504 .ndo_get_phys_port_id = efx_get_phys_port_id,
Bert Kenwardac019f02017-01-10 16:23:56 +00002505 .ndo_get_phys_port_name = efx_get_phys_port_name,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002506#ifdef CONFIG_NET_POLL_CONTROLLER
2507 .ndo_poll_controller = efx_netpoll,
2508#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002509 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002510#ifdef CONFIG_RFS_ACCEL
2511 .ndo_rx_flow_steer = efx_filter_rfs,
2512#endif
Jon Coopere5fbd972017-02-08 16:52:10 +00002513 .ndo_udp_tunnel_add = efx_udp_tunnel_add,
2514 .ndo_udp_tunnel_del = efx_udp_tunnel_del,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002515};
2516
Ben Hutchings7dde5962008-12-12 22:09:38 -08002517static void efx_update_name(struct efx_nic *efx)
2518{
2519 strcpy(efx->name, efx->net_dev->name);
2520 efx_mtd_rename(efx);
2521 efx_set_channel_names(efx);
2522}
2523
Ben Hutchings8ceee662008-04-27 12:55:59 +01002524static int efx_netdev_event(struct notifier_block *this,
2525 unsigned long event, void *ptr)
2526{
Jiri Pirko351638e2013-05-28 01:30:21 +00002527 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002528
Shradha Shah7fa8d542015-05-06 00:55:13 +01002529 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
Ben Hutchings7dde5962008-12-12 22:09:38 -08002530 event == NETDEV_CHANGENAME)
2531 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002532
2533 return NOTIFY_DONE;
2534}
2535
2536static struct notifier_block efx_netdev_notifier = {
2537 .notifier_call = efx_netdev_event,
2538};
2539
Ben Hutchings06d5e192008-12-12 21:47:23 -08002540static ssize_t
2541show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2542{
2543 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2544 return sprintf(buf, "%d\n", efx->phy_type);
2545}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002546static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002547
Edward Creee7fef9b2015-05-27 13:14:01 +01002548#ifdef CONFIG_SFC_MCDI_LOGGING
2549static ssize_t show_mcdi_log(struct device *dev, struct device_attribute *attr,
2550 char *buf)
2551{
2552 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2553 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2554
2555 return scnprintf(buf, PAGE_SIZE, "%d\n", mcdi->logging_enabled);
2556}
2557static ssize_t set_mcdi_log(struct device *dev, struct device_attribute *attr,
2558 const char *buf, size_t count)
2559{
2560 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2561 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2562 bool enable = count > 0 && *buf != '0';
2563
2564 mcdi->logging_enabled = enable;
2565 return count;
2566}
2567static DEVICE_ATTR(mcdi_logging, 0644, show_mcdi_log, set_mcdi_log);
2568#endif
2569
Ben Hutchings8ceee662008-04-27 12:55:59 +01002570static int efx_register_netdev(struct efx_nic *efx)
2571{
2572 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002573 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002574 int rc;
2575
2576 net_dev->watchdog_timeo = 5 * HZ;
2577 net_dev->irq = efx->pci_dev->irq;
Shradha Shah7fa8d542015-05-06 00:55:13 +01002578 net_dev->netdev_ops = &efx_netdev_ops;
2579 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
Ben Hutchings8127d662013-08-29 19:19:29 +01002580 net_dev->priv_flags |= IFF_UNICAST_FLT;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002581 net_dev->ethtool_ops = &efx_ethtool_ops;
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002582 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Bert Kenwardcd94e512016-10-18 17:47:45 +01002583 net_dev->min_mtu = EFX_MIN_MTU;
2584 net_dev->max_mtu = EFX_MAX_MTU;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002585
Ben Hutchings7dde5962008-12-12 22:09:38 -08002586 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002587
Ben Hutchings7153f622012-07-27 20:50:52 +01002588 /* Enable resets to be scheduled and check whether any were
2589 * already requested. If so, the NIC is probably hosed so we
2590 * abort.
2591 */
2592 efx->state = STATE_READY;
2593 smp_mb(); /* ensure we change state before checking reset_pending */
2594 if (efx->reset_pending) {
2595 netif_err(efx, probe, efx->net_dev,
2596 "aborting probe due to scheduled reset\n");
2597 rc = -EIO;
2598 goto fail_locked;
2599 }
2600
Ben Hutchingsaed06282009-08-26 08:16:27 +00002601 rc = dev_alloc_name(net_dev, net_dev->name);
2602 if (rc < 0)
2603 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002604 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002605
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002606 /* Always start with carrier off; PHY events will detect the link */
2607 netif_carrier_off(net_dev);
2608
Ben Hutchingsaed06282009-08-26 08:16:27 +00002609 rc = register_netdevice(net_dev);
2610 if (rc)
2611 goto fail_locked;
2612
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002613 efx_for_each_channel(channel, efx) {
2614 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002615 efx_for_each_channel_tx_queue(tx_queue, channel)
2616 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002617 }
2618
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002619 efx_associate(efx);
2620
Ben Hutchings7dde5962008-12-12 22:09:38 -08002621 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002622
Ben Hutchings06d5e192008-12-12 21:47:23 -08002623 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2624 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002625 netif_err(efx, drv, efx->net_dev,
2626 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002627 goto fail_registered;
2628 }
Edward Creee7fef9b2015-05-27 13:14:01 +01002629#ifdef CONFIG_SFC_MCDI_LOGGING
2630 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2631 if (rc) {
2632 netif_err(efx, drv, efx->net_dev,
2633 "failed to init net dev attributes\n");
2634 goto fail_attr_mcdi_logging;
2635 }
2636#endif
Ben Hutchings06d5e192008-12-12 21:47:23 -08002637
Ben Hutchings8ceee662008-04-27 12:55:59 +01002638 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002639
Edward Creee7fef9b2015-05-27 13:14:01 +01002640#ifdef CONFIG_SFC_MCDI_LOGGING
2641fail_attr_mcdi_logging:
2642 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2643#endif
Ben Hutchings7153f622012-07-27 20:50:52 +01002644fail_registered:
2645 rtnl_lock();
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002646 efx_dissociate(efx);
Ben Hutchings7153f622012-07-27 20:50:52 +01002647 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002648fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002649 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002650 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002651 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002652 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002653}
2654
2655static void efx_unregister_netdev(struct efx_nic *efx)
2656{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002657 if (!efx->net_dev)
2658 return;
2659
Ben Hutchings767e4682008-09-01 12:43:14 +01002660 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002661
Edward Creee7fef9b2015-05-27 13:14:01 +01002662 if (efx_dev_registered(efx)) {
2663 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2664#ifdef CONFIG_SFC_MCDI_LOGGING
2665 device_remove_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2666#endif
2667 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2668 unregister_netdev(efx->net_dev);
2669 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002670}
2671
2672/**************************************************************************
2673 *
2674 * Device reset and suspend
2675 *
2676 **************************************************************************/
2677
Ben Hutchings2467ca42008-09-01 12:48:50 +01002678/* Tears down the entire software state and most of the hardware state
2679 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002680void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002681{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002682 EFX_ASSERT_RESET_SERIALISED(efx);
2683
Edward Creee2835462014-04-16 19:27:48 +01002684 if (method == RESET_TYPE_MCDI_TIMEOUT)
2685 efx->type->prepare_flr(efx);
2686
Ben Hutchings2467ca42008-09-01 12:48:50 +01002687 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002688 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002689
2690 mutex_lock(&efx->mac_lock);
Edward Creee0a65e32018-03-27 17:44:36 +01002691 mutex_lock(&efx->rss_lock);
Jon Cooper087e9022015-05-20 11:11:35 +01002692 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2693 method != RESET_TYPE_DATAPATH)
Steve Hodgson4b988282009-01-29 17:50:51 +00002694 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002695 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002696}
2697
Ben Hutchings2467ca42008-09-01 12:48:50 +01002698/* This function will always ensure that the locks acquired in
2699 * efx_reset_down() are released. A failure return code indicates
2700 * that we were unable to reinitialise the hardware, and the
2701 * driver should be disabled. If ok is false, then the rx and tx
2702 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002703int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002704{
2705 int rc;
2706
Ben Hutchings2467ca42008-09-01 12:48:50 +01002707 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002708
Edward Creee2835462014-04-16 19:27:48 +01002709 if (method == RESET_TYPE_MCDI_TIMEOUT)
2710 efx->type->finish_flr(efx);
2711
2712 /* Ensure that SRAM is initialised even if we're disabling the device */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002713 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002714 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002715 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002716 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002717 }
2718
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002719 if (!ok)
2720 goto fail;
2721
Jon Cooper087e9022015-05-20 11:11:35 +01002722 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2723 method != RESET_TYPE_DATAPATH) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002724 rc = efx->phy_op->init(efx);
2725 if (rc)
2726 goto fail;
Edward Cree267d9d72015-05-06 00:59:18 +01002727 rc = efx->phy_op->reconfigure(efx);
2728 if (rc && rc != -EPERM)
Ben Hutchings62776d02010-06-23 11:30:07 +00002729 netif_err(efx, drv, efx->net_dev,
2730 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002731 }
2732
Jon Cooper261e4d92013-04-15 18:51:54 +01002733 rc = efx_enable_interrupts(efx);
2734 if (rc)
2735 goto fail;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01002736
2737#ifdef CONFIG_SFC_SRIOV
2738 rc = efx->type->vswitching_restore(efx);
2739 if (rc) /* not fatal; the PF will still work fine */
2740 netif_warn(efx, probe, efx->net_dev,
2741 "failed to restore vswitching rc=%d;"
2742 " VFs may not function\n", rc);
2743#endif
2744
Edward Cree42356d92018-03-08 15:45:17 +00002745 if (efx->type->rx_restore_rss_contexts)
2746 efx->type->rx_restore_rss_contexts(efx);
Edward Creee0a65e32018-03-27 17:44:36 +01002747 mutex_unlock(&efx->rss_lock);
Edward Cree0d322412015-05-20 11:10:03 +01002748 down_read(&efx->filter_sem);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002749 efx_restore_filters(efx);
Edward Cree0d322412015-05-20 11:10:03 +01002750 up_read(&efx->filter_sem);
Shradha Shah7fa8d542015-05-06 00:55:13 +01002751 if (efx->type->sriov_reset)
2752 efx->type->sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002753
2754 mutex_unlock(&efx->mac_lock);
2755
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002756 efx_start_all(efx);
2757
Jon Coopere5fbd972017-02-08 16:52:10 +00002758 if (efx->type->udp_tnl_push_ports)
2759 efx->type->udp_tnl_push_ports(efx);
2760
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002761 return 0;
2762
2763fail:
2764 efx->port_initialized = false;
2765
Edward Creee0a65e32018-03-27 17:44:36 +01002766 mutex_unlock(&efx->rss_lock);
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002767 mutex_unlock(&efx->mac_lock);
2768
Ben Hutchings8ceee662008-04-27 12:55:59 +01002769 return rc;
2770}
2771
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002772/* Reset the NIC using the specified method. Note that the reset may
2773 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002774 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002775 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002776 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002777int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002778{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002779 int rc, rc2;
2780 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002781
Ben Hutchings62776d02010-06-23 11:30:07 +00002782 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2783 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002784
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002785 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002786 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002787
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002788 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002789 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002790 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002791 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002792 }
2793
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002794 /* Clear flags for the scopes we covered. We assume the NIC and
2795 * driver are now quiescent so that there is no race here.
2796 */
Edward Creee2835462014-04-16 19:27:48 +01002797 if (method < RESET_TYPE_MAX_METHOD)
2798 efx->reset_pending &= -(1 << (method + 1));
2799 else /* it doesn't fit into the well-ordered scope hierarchy */
2800 __clear_bit(method, &efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002801
2802 /* Reinitialise bus-mastering, which may have been turned off before
2803 * the reset was scheduled. This is still appropriate, even in the
2804 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2805 * can respond to requests. */
2806 pci_set_master(efx->pci_dev);
2807
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002808out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002809 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002810 disabled = rc ||
2811 method == RESET_TYPE_DISABLE ||
2812 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002813 rc2 = efx_reset_up(efx, method, !disabled);
2814 if (rc2) {
2815 disabled = true;
2816 if (!rc)
2817 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002818 }
2819
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002820 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002821 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002822 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002823 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002824 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002825 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Peter Dunning9c568fd2017-02-17 15:50:43 +00002826 efx_device_attach_if_not_resetting(efx);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002827 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002828 return rc;
2829}
2830
Alexandre Rames626950d2013-01-14 17:20:22 +00002831/* Try recovery mechanisms.
2832 * For now only EEH is supported.
2833 * Returns 0 if the recovery mechanisms are unsuccessful.
2834 * Returns a non-zero value otherwise.
2835 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002836int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002837{
2838#ifdef CONFIG_EEH
2839 /* A PCI error can occur and not be seen by EEH because nothing
2840 * happens on the PCI bus. In this case the driver may fail and
2841 * schedule a 'recover or reset', leading to this recovery handler.
2842 * Manually call the eeh failure check function.
2843 */
Benjamin Herrenschmidt12a89db2015-03-23 14:00:47 +11002844 struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002845 if (eeh_dev_check_failure(eehdev)) {
2846 /* The EEH mechanisms will handle the error and reset the
2847 * device if necessary.
2848 */
2849 return 1;
2850 }
2851#endif
2852 return 0;
2853}
2854
Jon Cooper74cd60a2013-09-16 14:18:51 +01002855static void efx_wait_for_bist_end(struct efx_nic *efx)
2856{
2857 int i;
2858
2859 for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
2860 if (efx_mcdi_poll_reboot(efx))
2861 goto out;
2862 msleep(BIST_WAIT_DELAY_MS);
2863 }
2864
2865 netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
2866out:
2867 /* Either way unset the BIST flag. If we found no reboot we probably
2868 * won't recover, but we should try.
2869 */
2870 efx->mc_bist_for_other_fn = false;
2871}
2872
Ben Hutchings8ceee662008-04-27 12:55:59 +01002873/* The worker thread exists so that code that cannot sleep can
2874 * schedule a reset for later.
2875 */
2876static void efx_reset_work(struct work_struct *data)
2877{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002878 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002879 unsigned long pending;
2880 enum reset_type method;
2881
Mark Rutland6aa7de02017-10-23 14:07:29 -07002882 pending = READ_ONCE(efx->reset_pending);
Alexandre Rames626950d2013-01-14 17:20:22 +00002883 method = fls(pending) - 1;
2884
Jon Cooper74cd60a2013-09-16 14:18:51 +01002885 if (method == RESET_TYPE_MC_BIST)
2886 efx_wait_for_bist_end(efx);
2887
Alexandre Rames626950d2013-01-14 17:20:22 +00002888 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2889 method == RESET_TYPE_RECOVER_OR_ALL) &&
2890 efx_try_recovery(efx))
2891 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002892
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002893 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002894 return;
2895
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002896 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002897
2898 /* We checked the state in efx_schedule_reset() but it may
2899 * have changed by now. Now that we have the RTNL lock,
2900 * it cannot change again.
2901 */
2902 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002903 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002904
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002905 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002906}
2907
2908void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2909{
2910 enum reset_type method;
2911
Alexandre Rames626950d2013-01-14 17:20:22 +00002912 if (efx->state == STATE_RECOVERY) {
2913 netif_dbg(efx, drv, efx->net_dev,
2914 "recovering: skip scheduling %s reset\n",
2915 RESET_TYPE(type));
2916 return;
2917 }
2918
Ben Hutchings8ceee662008-04-27 12:55:59 +01002919 switch (type) {
2920 case RESET_TYPE_INVISIBLE:
2921 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002922 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002923 case RESET_TYPE_WORLD:
2924 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002925 case RESET_TYPE_RECOVER_OR_DISABLE:
Jon Cooper087e9022015-05-20 11:11:35 +01002926 case RESET_TYPE_DATAPATH:
Jon Cooper74cd60a2013-09-16 14:18:51 +01002927 case RESET_TYPE_MC_BIST:
Edward Creee2835462014-04-16 19:27:48 +01002928 case RESET_TYPE_MCDI_TIMEOUT:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002929 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002930 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2931 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002932 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002933 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002934 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002935 netif_dbg(efx, drv, efx->net_dev,
2936 "scheduling %s reset for %s\n",
2937 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002938 break;
2939 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002940
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002941 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002942 smp_mb(); /* ensure we change reset_pending before checking state */
2943
2944 /* If we're not READY then just leave the flags set as the cue
2945 * to abort probing or reschedule the reset later.
2946 */
Mark Rutland6aa7de02017-10-23 14:07:29 -07002947 if (READ_ONCE(efx->state) != STATE_READY)
Ben Hutchings7153f622012-07-27 20:50:52 +01002948 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002949
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002950 /* efx_process_channel() will no longer read events once a
2951 * reset is scheduled. So switch back to poll'd MCDI completions. */
2952 efx_mcdi_mode_poll(efx);
2953
Steve Hodgson1ab00622008-12-12 21:33:02 -08002954 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002955}
2956
2957/**************************************************************************
2958 *
2959 * List of NICs we support
2960 *
2961 **************************************************************************/
2962
2963/* PCI device ID table */
Benoit Taine9baa3c32014-08-08 15:56:03 +02002964static const struct pci_device_id efx_pci_table[] = {
Ben Hutchings547c4742011-12-02 18:23:56 +00002965 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002966 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002967 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002968 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8127d662013-08-29 19:19:29 +01002969 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
2970 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Shradha Shah6f7f8aa2015-05-06 01:00:07 +01002971 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
2972 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
Mateusz Wrzesinski3b06a002014-07-14 08:38:49 +01002973 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
2974 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Bert Kenwarddd248f12015-11-30 09:05:47 +00002975 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
2976 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
2977 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
2978 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2979 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
2980 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
Edward Creeaae5a312017-12-18 16:56:34 +00002981 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
2982 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2983 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
2984 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002985 {0} /* end of list */
2986};
2987
2988/**************************************************************************
2989 *
Ben Hutchings37594332009-11-23 16:05:45 +00002990 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002991 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002992 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002993 * Needed so all function pointers are valid and do not have to be tested
2994 * before use
2995 *
2996 **************************************************************************/
2997int efx_port_dummy_op_int(struct efx_nic *efx)
2998{
2999 return 0;
3000}
3001void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00003002
3003static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00003004{
3005 return false;
3006}
Ben Hutchings8ceee662008-04-27 12:55:59 +01003007
stephen hemminger6c8c2512011-04-14 05:50:12 +00003008static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01003009 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00003010 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00003011 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003012 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003013};
3014
Ben Hutchings8ceee662008-04-27 12:55:59 +01003015/**************************************************************************
3016 *
3017 * Data housekeeping
3018 *
3019 **************************************************************************/
3020
3021/* This zeroes out and then fills in the invariants in a struct
3022 * efx_nic (including all sub-structures).
3023 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01003024static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003025 struct pci_dev *pci_dev, struct net_device *net_dev)
3026{
Andrew Rybchenko6f9f6ec2017-02-13 14:57:39 +00003027 int rc = -ENOMEM, i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003028
3029 /* Initialise common structures */
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01003030 INIT_LIST_HEAD(&efx->node);
3031 INIT_LIST_HEAD(&efx->secondary_list);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003032 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00003033#ifdef CONFIG_SFC_MTD
3034 INIT_LIST_HEAD(&efx->mtd_list);
3035#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003036 INIT_WORK(&efx->reset_work, efx_reset_work);
3037 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00003038 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003039 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00003040 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01003041 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003042 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01003043
3044 efx->net_dev = net_dev;
Jon Cooper43a37392012-10-18 15:49:54 +01003045 efx->rx_prefix_size = efx->type->rx_prefix_size;
Andrew Rybchenko2ec03012013-11-16 11:02:27 +04003046 efx->rx_ip_align =
3047 NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
Jon Cooper43a37392012-10-18 15:49:54 +01003048 efx->rx_packet_hash_offset =
3049 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
Jon Cooperbd9a2652013-11-18 12:54:41 +00003050 efx->rx_packet_ts_offset =
3051 efx->type->rx_ts_offset - efx->type->rx_prefix_size;
Edward Cree42356d92018-03-08 15:45:17 +00003052 INIT_LIST_HEAD(&efx->rss_context.list);
Edward Creee0a65e32018-03-27 17:44:36 +01003053 mutex_init(&efx->rss_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003054 spin_lock_init(&efx->stats_lock);
Edward Cree71827442017-12-18 16:56:19 +00003055 efx->vi_stride = EFX_DEFAULT_VI_STRIDE;
Edward Creec1be4822017-12-21 09:00:26 +00003056 efx->num_mac_stats = MC_CMD_MAC_NSTATS;
3057 BUILD_BUG_ON(MC_CMD_MAC_NSTATS - 1 != MC_CMD_MAC_GENERATION_END);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003058 mutex_init(&efx->mac_lock);
Edward Cree3af0f342018-03-27 17:41:59 +01003059#ifdef CONFIG_RFS_ACCEL
3060 mutex_init(&efx->rps_mutex);
Edward Creef8d62032018-04-24 17:09:30 +01003061 spin_lock_init(&efx->rps_hash_lock);
3062 /* Failure to allocate is not fatal, but may degrade ARFS performance */
3063 efx->rps_hash_table = kcalloc(EFX_ARFS_HASH_TABLE_SIZE,
3064 sizeof(*efx->rps_hash_table), GFP_KERNEL);
Edward Cree3af0f342018-03-27 17:41:59 +01003065#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003066 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00003067 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08003068 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00003069 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003070
3071 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00003072 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
3073 if (!efx->channel[i])
3074 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01003075 efx->msi_context[i].efx = efx;
3076 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003077 }
3078
Ben Hutchings8ceee662008-04-27 12:55:59 +01003079 /* Higher numbered interrupt modes are less capable! */
Andrew Rybchenko6f9f6ec2017-02-13 14:57:39 +00003080 if (WARN_ON_ONCE(efx->type->max_interrupt_mode >
3081 efx->type->min_interrupt_mode)) {
3082 rc = -EIO;
3083 goto fail;
3084 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003085 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
3086 interrupt_mode);
Andrew Rybchenko6f9f6ec2017-02-13 14:57:39 +00003087 efx->interrupt_mode = min(efx->type->min_interrupt_mode,
3088 interrupt_mode);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003089
Ben Hutchings6977dc62008-12-26 13:44:39 -08003090 /* Would be good to use the net_dev name, but we're too early */
3091 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
3092 pci_name(pci_dev));
3093 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003094 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00003095 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01003096
Ben Hutchings8ceee662008-04-27 12:55:59 +01003097 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00003098
3099fail:
3100 efx_fini_struct(efx);
Andrew Rybchenko6f9f6ec2017-02-13 14:57:39 +00003101 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003102}
3103
3104static void efx_fini_struct(struct efx_nic *efx)
3105{
Ben Hutchings8313aca2010-09-10 06:41:57 +00003106 int i;
3107
Edward Creef8d62032018-04-24 17:09:30 +01003108#ifdef CONFIG_RFS_ACCEL
3109 kfree(efx->rps_hash_table);
3110#endif
3111
Ben Hutchings8313aca2010-09-10 06:41:57 +00003112 for (i = 0; i < EFX_MAX_CHANNELS; i++)
3113 kfree(efx->channel[i]);
3114
Ben Hutchingsef215e62013-12-05 20:13:22 +00003115 kfree(efx->vpd_sn);
3116
Ben Hutchings8ceee662008-04-27 12:55:59 +01003117 if (efx->workqueue) {
3118 destroy_workqueue(efx->workqueue);
3119 efx->workqueue = NULL;
3120 }
3121}
3122
Edward Creee4d112e2014-07-15 11:58:12 +01003123void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
3124{
3125 u64 n_rx_nodesc_trunc = 0;
3126 struct efx_channel *channel;
3127
3128 efx_for_each_channel(channel, efx)
3129 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
3130 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
3131 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
3132}
3133
Edward Creef8d62032018-04-24 17:09:30 +01003134bool efx_filter_spec_equal(const struct efx_filter_spec *left,
3135 const struct efx_filter_spec *right)
3136{
3137 if ((left->match_flags ^ right->match_flags) |
3138 ((left->flags ^ right->flags) &
3139 (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
3140 return false;
3141
3142 return memcmp(&left->outer_vid, &right->outer_vid,
3143 sizeof(struct efx_filter_spec) -
3144 offsetof(struct efx_filter_spec, outer_vid)) == 0;
3145}
3146
3147u32 efx_filter_spec_hash(const struct efx_filter_spec *spec)
3148{
3149 BUILD_BUG_ON(offsetof(struct efx_filter_spec, outer_vid) & 3);
3150 return jhash2((const u32 *)&spec->outer_vid,
3151 (sizeof(struct efx_filter_spec) -
3152 offsetof(struct efx_filter_spec, outer_vid)) / 4,
3153 0);
3154}
3155
3156#ifdef CONFIG_RFS_ACCEL
3157bool efx_rps_check_rule(struct efx_arfs_rule *rule, unsigned int filter_idx,
3158 bool *force)
3159{
3160 if (rule->filter_id == EFX_ARFS_FILTER_ID_PENDING) {
3161 /* ARFS is currently updating this entry, leave it */
3162 return false;
3163 }
3164 if (rule->filter_id == EFX_ARFS_FILTER_ID_ERROR) {
3165 /* ARFS tried and failed to update this, so it's probably out
3166 * of date. Remove the filter and the ARFS rule entry.
3167 */
3168 rule->filter_id = EFX_ARFS_FILTER_ID_REMOVING;
3169 *force = true;
3170 return true;
3171 } else if (WARN_ON(rule->filter_id != filter_idx)) { /* can't happen */
3172 /* ARFS has moved on, so old filter is not needed. Since we did
3173 * not mark the rule with EFX_ARFS_FILTER_ID_REMOVING, it will
3174 * not be removed by efx_rps_hash_del() subsequently.
3175 */
3176 *force = true;
3177 return true;
3178 }
3179 /* Remove it iff ARFS wants to. */
3180 return true;
3181}
3182
3183struct hlist_head *efx_rps_hash_bucket(struct efx_nic *efx,
3184 const struct efx_filter_spec *spec)
3185{
3186 u32 hash = efx_filter_spec_hash(spec);
3187
3188 WARN_ON(!spin_is_locked(&efx->rps_hash_lock));
3189 if (!efx->rps_hash_table)
3190 return NULL;
3191 return &efx->rps_hash_table[hash % EFX_ARFS_HASH_TABLE_SIZE];
3192}
3193
3194struct efx_arfs_rule *efx_rps_hash_find(struct efx_nic *efx,
3195 const struct efx_filter_spec *spec)
3196{
3197 struct efx_arfs_rule *rule;
3198 struct hlist_head *head;
3199 struct hlist_node *node;
3200
3201 head = efx_rps_hash_bucket(efx, spec);
3202 if (!head)
3203 return NULL;
3204 hlist_for_each(node, head) {
3205 rule = container_of(node, struct efx_arfs_rule, node);
3206 if (efx_filter_spec_equal(spec, &rule->spec))
3207 return rule;
3208 }
3209 return NULL;
3210}
3211
3212struct efx_arfs_rule *efx_rps_hash_add(struct efx_nic *efx,
3213 const struct efx_filter_spec *spec,
3214 bool *new)
3215{
3216 struct efx_arfs_rule *rule;
3217 struct hlist_head *head;
3218 struct hlist_node *node;
3219
3220 head = efx_rps_hash_bucket(efx, spec);
3221 if (!head)
3222 return NULL;
3223 hlist_for_each(node, head) {
3224 rule = container_of(node, struct efx_arfs_rule, node);
3225 if (efx_filter_spec_equal(spec, &rule->spec)) {
3226 *new = false;
3227 return rule;
3228 }
3229 }
3230 rule = kmalloc(sizeof(*rule), GFP_ATOMIC);
3231 *new = true;
3232 if (rule) {
3233 memcpy(&rule->spec, spec, sizeof(rule->spec));
3234 hlist_add_head(&rule->node, head);
3235 }
3236 return rule;
3237}
3238
3239void efx_rps_hash_del(struct efx_nic *efx, const struct efx_filter_spec *spec)
3240{
3241 struct efx_arfs_rule *rule;
3242 struct hlist_head *head;
3243 struct hlist_node *node;
3244
3245 head = efx_rps_hash_bucket(efx, spec);
3246 if (WARN_ON(!head))
3247 return;
3248 hlist_for_each(node, head) {
3249 rule = container_of(node, struct efx_arfs_rule, node);
3250 if (efx_filter_spec_equal(spec, &rule->spec)) {
3251 /* Someone already reused the entry. We know that if
3252 * this check doesn't fire (i.e. filter_id == REMOVING)
3253 * then the REMOVING mark was put there by our caller,
3254 * because caller is holding a lock on filter table and
3255 * only holders of that lock set REMOVING.
3256 */
3257 if (rule->filter_id != EFX_ARFS_FILTER_ID_REMOVING)
3258 return;
3259 hlist_del(node);
3260 kfree(rule);
3261 return;
3262 }
3263 }
3264 /* We didn't find it. */
3265 WARN_ON(1);
3266}
3267#endif
3268
Edward Cree42356d92018-03-08 15:45:17 +00003269/* RSS contexts. We're using linked lists and crappy O(n) algorithms, because
3270 * (a) this is an infrequent control-plane operation and (b) n is small (max 64)
3271 */
Edward Creee0a65e32018-03-27 17:44:36 +01003272struct efx_rss_context *efx_alloc_rss_context_entry(struct efx_nic *efx)
Edward Cree42356d92018-03-08 15:45:17 +00003273{
Edward Creee0a65e32018-03-27 17:44:36 +01003274 struct list_head *head = &efx->rss_context.list;
Edward Cree42356d92018-03-08 15:45:17 +00003275 struct efx_rss_context *ctx, *new;
3276 u32 id = 1; /* Don't use zero, that refers to the master RSS context */
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 /* Search for first gap in the numbering */
3281 list_for_each_entry(ctx, head, list) {
3282 if (ctx->user_id != id)
3283 break;
3284 id++;
3285 /* Check for wrap. If this happens, we have nearly 2^32
3286 * allocated RSS contexts, which seems unlikely.
3287 */
3288 if (WARN_ON_ONCE(!id))
3289 return NULL;
3290 }
3291
3292 /* Create the new entry */
3293 new = kmalloc(sizeof(struct efx_rss_context), GFP_KERNEL);
3294 if (!new)
3295 return NULL;
3296 new->context_id = EFX_EF10_RSS_CONTEXT_INVALID;
3297 new->rx_hash_udp_4tuple = false;
3298
3299 /* Insert the new entry into the gap */
3300 new->user_id = id;
3301 list_add_tail(&new->list, &ctx->list);
3302 return new;
3303}
3304
Edward Creee0a65e32018-03-27 17:44:36 +01003305struct efx_rss_context *efx_find_rss_context_entry(struct efx_nic *efx, u32 id)
Edward Cree42356d92018-03-08 15:45:17 +00003306{
Edward Creee0a65e32018-03-27 17:44:36 +01003307 struct list_head *head = &efx->rss_context.list;
Edward Cree42356d92018-03-08 15:45:17 +00003308 struct efx_rss_context *ctx;
3309
Edward Creee0a65e32018-03-27 17:44:36 +01003310 WARN_ON(!mutex_is_locked(&efx->rss_lock));
3311
Edward Cree42356d92018-03-08 15:45:17 +00003312 list_for_each_entry(ctx, head, list)
3313 if (ctx->user_id == id)
3314 return ctx;
3315 return NULL;
3316}
3317
3318void efx_free_rss_context_entry(struct efx_rss_context *ctx)
3319{
3320 list_del(&ctx->list);
3321 kfree(ctx);
3322}
3323
Ben Hutchings8ceee662008-04-27 12:55:59 +01003324/**************************************************************************
3325 *
3326 * PCI interface
3327 *
3328 **************************************************************************/
3329
3330/* Main body of final NIC shutdown code
3331 * This is called only at module unload (or hotplug removal).
3332 */
3333static void efx_pci_remove_main(struct efx_nic *efx)
3334{
Ben Hutchings7153f622012-07-27 20:50:52 +01003335 /* Flush reset_work. It can no longer be scheduled since we
3336 * are not READY.
3337 */
3338 BUG_ON(efx->state == STATE_READY);
3339 cancel_work_sync(&efx->reset_work);
3340
Ben Hutchingsd8291182012-10-05 23:35:41 +01003341 efx_disable_interrupts(efx);
Bert Kenwarda83762d2018-04-19 17:37:25 +01003342 efx_clear_interrupt_affinity(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00003343 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003344 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00003345 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003346 efx_fini_napi(efx);
3347 efx_remove_all(efx);
3348}
3349
3350/* Final NIC shutdown
Daniel Pieczko2a3fc312015-06-02 11:40:46 +01003351 * This is called only at module unload (or hotplug removal). A PF can call
3352 * this on its VFs to ensure they are unbound first.
Ben Hutchings8ceee662008-04-27 12:55:59 +01003353 */
3354static void efx_pci_remove(struct pci_dev *pci_dev)
3355{
3356 struct efx_nic *efx;
3357
3358 efx = pci_get_drvdata(pci_dev);
3359 if (!efx)
3360 return;
3361
3362 /* Mark the NIC as fini, then stop the interface */
3363 rtnl_lock();
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01003364 efx_dissociate(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003365 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003366 efx_disable_interrupts(efx);
Edward Creeea6bb992015-06-15 18:27:54 +01003367 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003368 rtnl_unlock();
3369
Shradha Shah7fa8d542015-05-06 00:55:13 +01003370 if (efx->type->sriov_fini)
3371 efx->type->sriov_fini(efx);
3372
Ben Hutchings8ceee662008-04-27 12:55:59 +01003373 efx_unregister_netdev(efx);
3374
Ben Hutchings7dde5962008-12-12 22:09:38 -08003375 efx_mtd_remove(efx);
3376
Ben Hutchings8ceee662008-04-27 12:55:59 +01003377 efx_pci_remove_main(efx);
3378
Ben Hutchings8ceee662008-04-27 12:55:59 +01003379 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00003380 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003381
Ben Hutchings8ceee662008-04-27 12:55:59 +01003382 efx_fini_struct(efx);
3383 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00003384
3385 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003386};
3387
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003388/* NIC VPD information
3389 * Called during probe to display the part number of the
3390 * installed NIC. VPD is potentially very large but this should
3391 * always appear within the first 512 bytes.
3392 */
3393#define SFC_VPD_LEN 512
Ben Hutchingsef215e62013-12-05 20:13:22 +00003394static void efx_probe_vpd_strings(struct efx_nic *efx)
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003395{
3396 struct pci_dev *dev = efx->pci_dev;
3397 char vpd_data[SFC_VPD_LEN];
3398 ssize_t vpd_size;
Ben Hutchingsef215e62013-12-05 20:13:22 +00003399 int ro_start, ro_size, i, j;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003400
3401 /* Get the vpd data from the device */
3402 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
3403 if (vpd_size <= 0) {
3404 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
3405 return;
3406 }
3407
3408 /* Get the Read only section */
Ben Hutchingsef215e62013-12-05 20:13:22 +00003409 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
3410 if (ro_start < 0) {
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003411 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
3412 return;
3413 }
3414
Ben Hutchingsef215e62013-12-05 20:13:22 +00003415 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
3416 j = ro_size;
3417 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003418 if (i + j > vpd_size)
3419 j = vpd_size - i;
3420
3421 /* Get the Part number */
3422 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
3423 if (i < 0) {
3424 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
3425 return;
3426 }
3427
3428 j = pci_vpd_info_field_size(&vpd_data[i]);
3429 i += PCI_VPD_INFO_FLD_HDR_SIZE;
3430 if (i + j > vpd_size) {
3431 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
3432 return;
3433 }
3434
3435 netif_info(efx, drv, efx->net_dev,
3436 "Part Number : %.*s\n", j, &vpd_data[i]);
Ben Hutchingsef215e62013-12-05 20:13:22 +00003437
3438 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
3439 j = ro_size;
3440 i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
3441 if (i < 0) {
3442 netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
3443 return;
3444 }
3445
3446 j = pci_vpd_info_field_size(&vpd_data[i]);
3447 i += PCI_VPD_INFO_FLD_HDR_SIZE;
3448 if (i + j > vpd_size) {
3449 netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
3450 return;
3451 }
3452
3453 efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
3454 if (!efx->vpd_sn)
3455 return;
3456
3457 snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003458}
3459
3460
Ben Hutchings8ceee662008-04-27 12:55:59 +01003461/* Main body of NIC initialisation
3462 * This is called at module load (or hotplug insertion, theoretically).
3463 */
3464static int efx_pci_probe_main(struct efx_nic *efx)
3465{
3466 int rc;
3467
3468 /* Do start-of-day initialisation */
3469 rc = efx_probe_all(efx);
3470 if (rc)
3471 goto fail1;
3472
Ben Hutchingse8f14992010-12-07 19:47:34 +00003473 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003474
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00003475 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003476 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00003477 netif_err(efx, probe, efx->net_dev,
3478 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00003479 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003480 }
3481
3482 rc = efx_init_port(efx);
3483 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00003484 netif_err(efx, probe, efx->net_dev,
3485 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00003486 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003487 }
3488
Ben Hutchings152b6a62009-11-29 03:43:56 +00003489 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003490 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00003491 goto fail5;
Bert Kenwarda83762d2018-04-19 17:37:25 +01003492
3493 efx_set_interrupt_affinity(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +01003494 rc = efx_enable_interrupts(efx);
3495 if (rc)
3496 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003497
3498 return 0;
3499
Jon Cooper261e4d92013-04-15 18:51:54 +01003500 fail6:
Bert Kenwarda83762d2018-04-19 17:37:25 +01003501 efx_clear_interrupt_affinity(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +01003502 efx_nic_fini_interrupt(efx);
Ben Hutchings278c0622009-11-23 16:05:12 +00003503 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003504 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003505 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00003506 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003507 fail3:
3508 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003509 efx_remove_all(efx);
3510 fail1:
3511 return rc;
3512}
3513
Jon Cooper8a531402017-02-08 16:51:18 +00003514static int efx_pci_probe_post_io(struct efx_nic *efx)
3515{
3516 struct net_device *net_dev = efx->net_dev;
3517 int rc = efx_pci_probe_main(efx);
3518
3519 if (rc)
3520 return rc;
3521
3522 if (efx->type->sriov_init) {
3523 rc = efx->type->sriov_init(efx);
3524 if (rc)
3525 netif_err(efx, probe, efx->net_dev,
3526 "SR-IOV can't be enabled rc %d\n", rc);
3527 }
3528
3529 /* Determine netdevice features */
3530 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
Edward Cree69787292017-10-31 14:29:47 +00003531 NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
Jon Cooper8a531402017-02-08 16:51:18 +00003532 if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
3533 net_dev->features |= NETIF_F_TSO6;
3534 /* Check whether device supports TSO */
3535 if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
3536 net_dev->features &= ~NETIF_F_ALL_TSO;
3537 /* Mask for features that also apply to VLAN devices */
3538 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
3539 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
3540 NETIF_F_RXCSUM);
3541
Edward Cree69787292017-10-31 14:29:47 +00003542 net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
3543
3544 /* Disable receiving frames with bad FCS, by default. */
3545 net_dev->features &= ~NETIF_F_RXALL;
Jon Cooper8a531402017-02-08 16:51:18 +00003546
3547 /* Disable VLAN filtering by default. It may be enforced if
3548 * the feature is fixed (i.e. VLAN filters are required to
3549 * receive VLAN tagged packets due to vPort restrictions).
3550 */
3551 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
3552 net_dev->features |= efx->fixed_features;
3553
3554 rc = efx_register_netdev(efx);
3555 if (!rc)
3556 return 0;
3557
3558 efx_pci_remove_main(efx);
3559 return rc;
3560}
3561
Ben Hutchings8ceee662008-04-27 12:55:59 +01003562/* NIC initialisation
3563 *
3564 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00003565 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003566 * sets up and registers the network devices with the kernel and hooks
3567 * the interrupt service routine. It does not prepare the device for
3568 * transmission; this is left to the first time one of the network
3569 * interfaces is brought up (i.e. efx_net_open).
3570 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05003571static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00003572 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01003573{
Ben Hutchings8ceee662008-04-27 12:55:59 +01003574 struct net_device *net_dev;
3575 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003576 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003577
3578 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00003579 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
3580 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003581 if (!net_dev)
3582 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01003583 efx = netdev_priv(net_dev);
3584 efx->type = (const struct efx_nic_type *) entry->driver_data;
Andrew Rybchenkoebfcd0f2016-06-15 17:43:20 +01003585 efx->fixed_features |= NETIF_F_HIGHDMA;
Andrew Rybchenkoeb7cfd82016-06-15 17:51:36 +01003586
Ben Hutchings8ceee662008-04-27 12:55:59 +01003587 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00003588 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01003589 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003590 if (rc)
3591 goto fail1;
3592
Ben Hutchings62776d02010-06-23 11:30:07 +00003593 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01003594 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003595
Shradha Shah6f7f8aa2015-05-06 01:00:07 +01003596 if (!efx->type->is_vf)
3597 efx_probe_vpd_strings(efx);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003598
Ben Hutchings8ceee662008-04-27 12:55:59 +01003599 /* Set up basic I/O (BAR mappings etc) */
3600 rc = efx_init_io(efx);
3601 if (rc)
3602 goto fail2;
3603
Jon Cooper8a531402017-02-08 16:51:18 +00003604 rc = efx_pci_probe_post_io(efx);
3605 if (rc) {
3606 /* On failure, retry once immediately.
3607 * If we aborted probe due to a scheduled reset, dismiss it.
3608 */
3609 efx->reset_pending = 0;
3610 rc = efx_pci_probe_post_io(efx);
3611 if (rc) {
3612 /* On another failure, retry once more
3613 * after a 50-305ms delay.
3614 */
3615 unsigned char r;
3616
3617 get_random_bytes(&r, 1);
3618 msleep((unsigned int)r + 50);
3619 efx->reset_pending = 0;
3620 rc = efx_pci_probe_post_io(efx);
3621 }
3622 }
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003623 if (rc)
3624 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08003625
Ben Hutchings62776d02010-06-23 11:30:07 +00003626 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003627
Ben Hutchings7c431612012-01-27 17:23:58 +00003628 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003629 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00003630 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003631 rtnl_unlock();
Bert Kenward09a04202015-12-23 08:58:15 +00003632 if (rc && rc != -EPERM)
Ben Hutchings7c431612012-01-27 17:23:58 +00003633 netif_warn(efx, probe, efx->net_dev,
3634 "failed to create MTDs (%d)\n", rc);
3635
Alexandre Rames626950d2013-01-14 17:20:22 +00003636 rc = pci_enable_pcie_error_reporting(pci_dev);
3637 if (rc && rc != -EINVAL)
Bert Kenward09a04202015-12-23 08:58:15 +00003638 netif_notice(efx, probe, efx->net_dev,
3639 "PCIE error reporting unavailable (%d).\n",
3640 rc);
Alexandre Rames626950d2013-01-14 17:20:22 +00003641
Jon Coopere5fbd972017-02-08 16:52:10 +00003642 if (efx->type->udp_tnl_push_ports)
3643 efx->type->udp_tnl_push_ports(efx);
3644
Ben Hutchings8ceee662008-04-27 12:55:59 +01003645 return 0;
3646
Ben Hutchings8ceee662008-04-27 12:55:59 +01003647 fail3:
3648 efx_fini_io(efx);
3649 fail2:
3650 efx_fini_struct(efx);
3651 fail1:
Steve Hodgson5e2a9112010-02-12 12:32:27 -08003652 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00003653 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003654 free_netdev(net_dev);
3655 return rc;
3656}
3657
Shradha Shah834e23d2015-05-06 00:55:58 +01003658/* efx_pci_sriov_configure returns the actual number of Virtual Functions
3659 * enabled on success
3660 */
3661#ifdef CONFIG_SFC_SRIOV
3662static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
3663{
3664 int rc;
3665 struct efx_nic *efx = pci_get_drvdata(dev);
3666
3667 if (efx->type->sriov_configure) {
3668 rc = efx->type->sriov_configure(efx, num_vfs);
3669 if (rc)
3670 return rc;
3671 else
3672 return num_vfs;
3673 } else
3674 return -EOPNOTSUPP;
3675}
3676#endif
3677
Ben Hutchings89c758f2009-11-29 03:43:07 +00003678static int efx_pm_freeze(struct device *dev)
3679{
3680 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3681
Ben Hutchings61da0262012-07-27 19:35:39 +01003682 rtnl_lock();
3683
Ben Hutchings6032fb52012-07-27 19:35:47 +01003684 if (efx->state != STATE_DISABLED) {
3685 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003686
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01003687 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003688
Ben Hutchings6032fb52012-07-27 19:35:47 +01003689 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003690 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01003691 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00003692
Ben Hutchings61da0262012-07-27 19:35:39 +01003693 rtnl_unlock();
3694
Ben Hutchings89c758f2009-11-29 03:43:07 +00003695 return 0;
3696}
3697
3698static int efx_pm_thaw(struct device *dev)
3699{
Jon Cooper261e4d92013-04-15 18:51:54 +01003700 int rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003701 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3702
Ben Hutchings61da0262012-07-27 19:35:39 +01003703 rtnl_lock();
3704
Ben Hutchings6032fb52012-07-27 19:35:47 +01003705 if (efx->state != STATE_DISABLED) {
Jon Cooper261e4d92013-04-15 18:51:54 +01003706 rc = efx_enable_interrupts(efx);
3707 if (rc)
3708 goto fail;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003709
Ben Hutchings6032fb52012-07-27 19:35:47 +01003710 mutex_lock(&efx->mac_lock);
3711 efx->phy_op->reconfigure(efx);
3712 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003713
Ben Hutchings6032fb52012-07-27 19:35:47 +01003714 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003715
Peter Dunning9c568fd2017-02-17 15:50:43 +00003716 efx_device_attach_if_not_resetting(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003717
Ben Hutchings6032fb52012-07-27 19:35:47 +01003718 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003719
Ben Hutchings6032fb52012-07-27 19:35:47 +01003720 efx->type->resume_wol(efx);
3721 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00003722
Ben Hutchings61da0262012-07-27 19:35:39 +01003723 rtnl_unlock();
3724
Steve Hodgson319ba642010-06-01 11:17:24 +00003725 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
3726 queue_work(reset_workqueue, &efx->reset_work);
3727
Ben Hutchings89c758f2009-11-29 03:43:07 +00003728 return 0;
Jon Cooper261e4d92013-04-15 18:51:54 +01003729
3730fail:
3731 rtnl_unlock();
3732
3733 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003734}
3735
3736static int efx_pm_poweroff(struct device *dev)
3737{
3738 struct pci_dev *pci_dev = to_pci_dev(dev);
3739 struct efx_nic *efx = pci_get_drvdata(pci_dev);
3740
3741 efx->type->fini(efx);
3742
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01003743 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003744
3745 pci_save_state(pci_dev);
3746 return pci_set_power_state(pci_dev, PCI_D3hot);
3747}
3748
3749/* Used for both resume and restore */
3750static int efx_pm_resume(struct device *dev)
3751{
3752 struct pci_dev *pci_dev = to_pci_dev(dev);
3753 struct efx_nic *efx = pci_get_drvdata(pci_dev);
3754 int rc;
3755
3756 rc = pci_set_power_state(pci_dev, PCI_D0);
3757 if (rc)
3758 return rc;
3759 pci_restore_state(pci_dev);
3760 rc = pci_enable_device(pci_dev);
3761 if (rc)
3762 return rc;
3763 pci_set_master(efx->pci_dev);
3764 rc = efx->type->reset(efx, RESET_TYPE_ALL);
3765 if (rc)
3766 return rc;
3767 rc = efx->type->init(efx);
3768 if (rc)
3769 return rc;
Jon Cooper261e4d92013-04-15 18:51:54 +01003770 rc = efx_pm_thaw(dev);
3771 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003772}
3773
3774static int efx_pm_suspend(struct device *dev)
3775{
3776 int rc;
3777
3778 efx_pm_freeze(dev);
3779 rc = efx_pm_poweroff(dev);
3780 if (rc)
3781 efx_pm_resume(dev);
3782 return rc;
3783}
3784
Ben Hutchings18e83e42012-01-05 19:05:20 +00003785static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00003786 .suspend = efx_pm_suspend,
3787 .resume = efx_pm_resume,
3788 .freeze = efx_pm_freeze,
3789 .thaw = efx_pm_thaw,
3790 .poweroff = efx_pm_poweroff,
3791 .restore = efx_pm_resume,
3792};
3793
Alexandre Rames626950d2013-01-14 17:20:22 +00003794/* A PCI error affecting this device was detected.
3795 * At this point MMIO and DMA may be disabled.
3796 * Stop the software path and request a slot reset.
3797 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003798static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
3799 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00003800{
3801 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3802 struct efx_nic *efx = pci_get_drvdata(pdev);
3803
3804 if (state == pci_channel_io_perm_failure)
3805 return PCI_ERS_RESULT_DISCONNECT;
3806
3807 rtnl_lock();
3808
3809 if (efx->state != STATE_DISABLED) {
3810 efx->state = STATE_RECOVERY;
3811 efx->reset_pending = 0;
3812
3813 efx_device_detach_sync(efx);
3814
3815 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003816 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00003817
3818 status = PCI_ERS_RESULT_NEED_RESET;
3819 } else {
3820 /* If the interface is disabled we don't want to do anything
3821 * with it.
3822 */
3823 status = PCI_ERS_RESULT_RECOVERED;
3824 }
3825
3826 rtnl_unlock();
3827
3828 pci_disable_device(pdev);
3829
3830 return status;
3831}
3832
Joe Perchesdbedd442015-03-06 20:49:12 -08003833/* Fake a successful reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003834static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00003835{
3836 struct efx_nic *efx = pci_get_drvdata(pdev);
3837 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3838 int rc;
3839
3840 if (pci_enable_device(pdev)) {
3841 netif_err(efx, hw, efx->net_dev,
3842 "Cannot re-enable PCI device after reset.\n");
3843 status = PCI_ERS_RESULT_DISCONNECT;
3844 }
3845
3846 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
3847 if (rc) {
3848 netif_err(efx, hw, efx->net_dev,
3849 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3850 /* Non-fatal error. Continue. */
3851 }
3852
3853 return status;
3854}
3855
3856/* Perform the actual reset and resume I/O operations. */
3857static void efx_io_resume(struct pci_dev *pdev)
3858{
3859 struct efx_nic *efx = pci_get_drvdata(pdev);
3860 int rc;
3861
3862 rtnl_lock();
3863
3864 if (efx->state == STATE_DISABLED)
3865 goto out;
3866
3867 rc = efx_reset(efx, RESET_TYPE_ALL);
3868 if (rc) {
3869 netif_err(efx, hw, efx->net_dev,
3870 "efx_reset failed after PCI error (%d)\n", rc);
3871 } else {
3872 efx->state = STATE_READY;
3873 netif_dbg(efx, hw, efx->net_dev,
3874 "Done resetting and resuming IO after PCI error.\n");
3875 }
3876
3877out:
3878 rtnl_unlock();
3879}
3880
3881/* For simplicity and reliability, we always require a slot reset and try to
3882 * reset the hardware when a pci error affecting the device is detected.
3883 * We leave both the link_reset and mmio_enabled callback unimplemented:
3884 * with our request for slot reset the mmio_enabled callback will never be
3885 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3886 */
Julia Lawallc300366b2015-11-14 11:06:57 +01003887static const struct pci_error_handlers efx_err_handlers = {
Alexandre Rames626950d2013-01-14 17:20:22 +00003888 .error_detected = efx_io_error_detected,
3889 .slot_reset = efx_io_slot_reset,
3890 .resume = efx_io_resume,
3891};
3892
Ben Hutchings8ceee662008-04-27 12:55:59 +01003893static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003894 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003895 .id_table = efx_pci_table,
3896 .probe = efx_pci_probe,
3897 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003898 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003899 .err_handler = &efx_err_handlers,
Shradha Shah834e23d2015-05-06 00:55:58 +01003900#ifdef CONFIG_SFC_SRIOV
3901 .sriov_configure = efx_pci_sriov_configure,
3902#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003903};
3904
3905/**************************************************************************
3906 *
3907 * Kernel module interface
3908 *
3909 *************************************************************************/
3910
3911module_param(interrupt_mode, uint, 0444);
3912MODULE_PARM_DESC(interrupt_mode,
3913 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3914
3915static int __init efx_init_module(void)
3916{
3917 int rc;
3918
3919 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3920
3921 rc = register_netdevice_notifier(&efx_netdev_notifier);
3922 if (rc)
3923 goto err_notifier;
3924
Shradha Shah7fa8d542015-05-06 00:55:13 +01003925#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003926 rc = efx_init_sriov();
3927 if (rc)
3928 goto err_sriov;
Shradha Shah7fa8d542015-05-06 00:55:13 +01003929#endif
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003930
Steve Hodgson1ab00622008-12-12 21:33:02 -08003931 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3932 if (!reset_workqueue) {
3933 rc = -ENOMEM;
3934 goto err_reset;
3935 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003936
3937 rc = pci_register_driver(&efx_pci_driver);
3938 if (rc < 0)
3939 goto err_pci;
3940
3941 return 0;
3942
3943 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003944 destroy_workqueue(reset_workqueue);
3945 err_reset:
Shradha Shah7fa8d542015-05-06 00:55:13 +01003946#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003947 efx_fini_sriov();
3948 err_sriov:
Shradha Shah7fa8d542015-05-06 00:55:13 +01003949#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003950 unregister_netdevice_notifier(&efx_netdev_notifier);
3951 err_notifier:
3952 return rc;
3953}
3954
3955static void __exit efx_exit_module(void)
3956{
3957 printk(KERN_INFO "Solarflare NET driver unloading\n");
3958
3959 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003960 destroy_workqueue(reset_workqueue);
Shradha Shah7fa8d542015-05-06 00:55:13 +01003961#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003962 efx_fini_sriov();
Shradha Shah7fa8d542015-05-06 00:55:13 +01003963#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003964 unregister_netdevice_notifier(&efx_netdev_notifier);
3965
3966}
3967
3968module_init(efx_init_module);
3969module_exit(efx_exit_module);
3970
Ben Hutchings906bb262009-11-29 15:16:19 +00003971MODULE_AUTHOR("Solarflare Communications and "
3972 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings6a350fd2014-02-12 19:00:07 +00003973MODULE_DESCRIPTION("Solarflare network driver");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003974MODULE_LICENSE("GPL");
3975MODULE_DEVICE_TABLE(pci, efx_pci_table);
Edward Cree14077e92017-01-03 15:46:00 +00003976MODULE_VERSION(EFX_DRIVER_VERSION);