blob: c28cd9daf94963d20c391c7508560a429f2eff37 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01002 * Driver for Solarflare network controllers and boards
Ben Hutchings8ceee662008-04-27 12:55:59 +01003 * Copyright 2005-2006 Fen Systems Ltd.
Ben Hutchingsf7a6d2c2013-08-29 23:32:48 +01004 * Copyright 2005-2013 Solarflare Communications Inc.
Ben Hutchings8ceee662008-04-27 12:55:59 +01005 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11#include <linux/module.h>
12#include <linux/pci.h>
13#include <linux/netdevice.h>
14#include <linux/etherdevice.h>
15#include <linux/delay.h>
16#include <linux/notifier.h>
17#include <linux/ip.h>
18#include <linux/tcp.h>
19#include <linux/in.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010020#include <linux/ethtool.h>
Ben Hutchingsaa6ef272008-07-18 19:03:10 +010021#include <linux/topology.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Alexandre Rames626950d2013-01-14 17:20:22 +000023#include <linux/aer.h>
Alexandre Ramesb28405b2013-03-21 16:41:43 +000024#include <linux/interrupt.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010025#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010026#include "efx.h"
Ben Hutchings744093c2009-11-29 15:12:08 +000027#include "nic.h"
Ben Hutchingsdd407812012-02-28 23:40:21 +000028#include "selftest.h"
Shradha Shah7fa8d542015-05-06 00:55:13 +010029#include "sriov.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010030
Ben Hutchings8880f4e2009-11-29 15:15:41 +000031#include "mcdi.h"
Steve Hodgsonfd371e32010-06-01 11:17:51 +000032#include "workarounds.h"
Ben Hutchings8880f4e2009-11-29 15:15:41 +000033
Ben Hutchingsc4593022009-11-23 16:08:17 +000034/**************************************************************************
35 *
36 * Type name strings
37 *
38 **************************************************************************
39 */
40
41/* Loopback mode names (see LOOPBACK_MODE()) */
42const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000043const char *const efx_loopback_mode_names[] = {
Ben Hutchingsc4593022009-11-23 16:08:17 +000044 [LOOPBACK_NONE] = "NONE",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000045 [LOOPBACK_DATA] = "DATAPATH",
Ben Hutchingsc4593022009-11-23 16:08:17 +000046 [LOOPBACK_GMAC] = "GMAC",
47 [LOOPBACK_XGMII] = "XGMII",
48 [LOOPBACK_XGXS] = "XGXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000049 [LOOPBACK_XAUI] = "XAUI",
50 [LOOPBACK_GMII] = "GMII",
51 [LOOPBACK_SGMII] = "SGMII",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000052 [LOOPBACK_XGBR] = "XGBR",
53 [LOOPBACK_XFI] = "XFI",
54 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
55 [LOOPBACK_GMII_FAR] = "GMII_FAR",
56 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
57 [LOOPBACK_XFI_FAR] = "XFI_FAR",
Ben Hutchingsc4593022009-11-23 16:08:17 +000058 [LOOPBACK_GPHY] = "GPHY",
59 [LOOPBACK_PHYXS] = "PHYXS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000060 [LOOPBACK_PCS] = "PCS",
61 [LOOPBACK_PMAPMD] = "PMA/PMD",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000062 [LOOPBACK_XPORT] = "XPORT",
63 [LOOPBACK_XGMII_WS] = "XGMII_WS",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000064 [LOOPBACK_XAUI_WS] = "XAUI_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000065 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
66 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000067 [LOOPBACK_GMII_WS] = "GMII_WS",
Ben Hutchingse58f69f2009-11-29 15:08:41 +000068 [LOOPBACK_XFI_WS] = "XFI_WS",
69 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
Ben Hutchings9c636ba2012-01-05 17:19:45 +000070 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
Ben Hutchingsc4593022009-11-23 16:08:17 +000071};
72
Ben Hutchingsc4593022009-11-23 16:08:17 +000073const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
Ben Hutchings18e83e42012-01-05 19:05:20 +000074const char *const efx_reset_type_names[] = {
Alexandre Rames626950d2013-01-14 17:20:22 +000075 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
76 [RESET_TYPE_ALL] = "ALL",
77 [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
78 [RESET_TYPE_WORLD] = "WORLD",
79 [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
Jon Cooper087e9022015-05-20 11:11:35 +010080 [RESET_TYPE_DATAPATH] = "DATAPATH",
Edward Creee2835462014-04-16 19:27:48 +010081 [RESET_TYPE_MC_BIST] = "MC_BIST",
Alexandre Rames626950d2013-01-14 17:20:22 +000082 [RESET_TYPE_DISABLE] = "DISABLE",
83 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
84 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
Alexandre Rames3de82b92013-06-13 11:36:15 +010085 [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
Alexandre Rames626950d2013-01-14 17:20:22 +000086 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
87 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
Edward Creee2835462014-04-16 19:27:48 +010088 [RESET_TYPE_MCDI_TIMEOUT] = "MCDI_TIMEOUT (FLR)",
Ben Hutchingsc4593022009-11-23 16:08:17 +000089};
90
Steve Hodgson1ab00622008-12-12 21:33:02 -080091/* Reset workqueue. If any NIC has a hardware failure then a reset will be
92 * queued onto this work queue. This is not a per-nic work queue, because
93 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
94 */
95static struct workqueue_struct *reset_workqueue;
96
Jon Cooper74cd60a2013-09-16 14:18:51 +010097/* How often and how many times to poll for a reset while waiting for a
98 * BIST that another function started to complete.
99 */
100#define BIST_WAIT_DELAY_MS 100
101#define BIST_WAIT_DELAY_COUNT 100
102
Ben Hutchings8ceee662008-04-27 12:55:59 +0100103/**************************************************************************
104 *
105 * Configurable values
106 *
107 *************************************************************************/
108
109/*
Ben Hutchings8ceee662008-04-27 12:55:59 +0100110 * Use separate channels for TX and RX events
111 *
Neil Turton28b581a2008-12-12 21:41:06 -0800112 * Set this to 1 to use separate channels for TX and RX. It allows us
113 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100114 *
Neil Turton28b581a2008-12-12 21:41:06 -0800115 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100116 */
Shradha Shahb0fbdae2015-08-28 10:55:42 +0100117bool efx_separate_tx_channels;
118module_param(efx_separate_tx_channels, bool, 0444);
119MODULE_PARM_DESC(efx_separate_tx_channels,
Neil Turton28b581a2008-12-12 21:41:06 -0800120 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100121
122/* This is the weight assigned to each of the (per-channel) virtual
123 * NAPI devices.
124 */
125static int napi_weight = 64;
126
127/* This is the time (in jiffies) between invocations of the hardware
Alexandre Rames626950d2013-01-14 17:20:22 +0000128 * monitor.
129 * On Falcon-based NICs, this will:
Ben Hutchingse254c272010-09-20 08:44:10 +0000130 * - Check the on-board hardware monitor;
131 * - Poll the link state and reconfigure the hardware as necessary.
Alexandre Rames626950d2013-01-14 17:20:22 +0000132 * On Siena-based NICs for power systems with EEH support, this will give EEH a
133 * chance to start.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100134 */
stephen hemmingerd2156972010-10-18 05:27:31 +0000135static unsigned int efx_monitor_interval = 1 * HZ;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100136
Ben Hutchings8ceee662008-04-27 12:55:59 +0100137/* Initial interrupt moderation settings. They can be modified after
138 * module load with ethtool.
139 *
140 * The default for RX should strike a balance between increasing the
141 * round-trip latency and reducing overhead.
142 */
143static unsigned int rx_irq_mod_usec = 60;
144
145/* Initial interrupt moderation settings. They can be modified after
146 * module load with ethtool.
147 *
148 * This default is chosen to ensure that a 10G link does not go idle
149 * while a TX queue is stopped after it has become full. A queue is
150 * restarted when it drops below half full. The time this takes (assuming
151 * worst case 3 descriptors per packet and 1024 descriptors) is
152 * 512 / 3 * 1.2 = 205 usec.
153 */
154static unsigned int tx_irq_mod_usec = 150;
155
156/* This is the first interrupt mode to try out of:
157 * 0 => MSI-X
158 * 1 => MSI
159 * 2 => legacy
160 */
161static unsigned int interrupt_mode;
162
163/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
164 * i.e. the number of CPUs among which we may distribute simultaneous
165 * interrupt handling.
166 *
167 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
Ben Hutchingscdb08f82011-12-20 01:08:05 +0000168 * The default (0) means to assign an interrupt to each core.
Ben Hutchings8ceee662008-04-27 12:55:59 +0100169 */
170static unsigned int rss_cpus;
171module_param(rss_cpus, uint, 0444);
172MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
173
Ben Hutchingsb9cc9772012-11-28 04:12:41 +0000174static bool phy_flash_cfg;
175module_param(phy_flash_cfg, bool, 0644);
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800176MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
177
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000178static unsigned irq_adapt_low_thresh = 8000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000179module_param(irq_adapt_low_thresh, uint, 0644);
180MODULE_PARM_DESC(irq_adapt_low_thresh,
181 "Threshold score for reducing IRQ moderation");
182
Ben Hutchingse7bed9c2012-02-28 18:44:13 +0000183static unsigned irq_adapt_high_thresh = 16000;
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000184module_param(irq_adapt_high_thresh, uint, 0644);
185MODULE_PARM_DESC(irq_adapt_high_thresh,
186 "Threshold score for increasing IRQ moderation");
187
Ben Hutchings62776d02010-06-23 11:30:07 +0000188static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
189 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
190 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
191 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
192module_param(debug, uint, 0);
193MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
194
Ben Hutchings8ceee662008-04-27 12:55:59 +0100195/**************************************************************************
196 *
197 * Utility functions and prototypes
198 *
199 *************************************************************************/
Ben Hutchings46426102010-09-10 06:42:33 +0000200
Jon Cooper261e4d92013-04-15 18:51:54 +0100201static int efx_soft_enable_interrupts(struct efx_nic *efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100202static void efx_soft_disable_interrupts(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000203static void efx_remove_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000204static void efx_remove_channels(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000205static const struct efx_channel_type efx_default_channel_type;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100206static void efx_remove_port(struct efx_nic *efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +0000207static void efx_init_napi_channel(struct efx_channel *channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100208static void efx_fini_napi(struct efx_nic *efx);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000209static void efx_fini_napi_channel(struct efx_channel *channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000210static void efx_fini_struct(struct efx_nic *efx);
211static void efx_start_all(struct efx_nic *efx);
212static void efx_stop_all(struct efx_nic *efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100213
214#define EFX_ASSERT_RESET_SERIALISED(efx) \
215 do { \
Ben Hutchingsf16aeea2012-07-27 19:31:16 +0100216 if ((efx->state == STATE_READY) || \
Alexandre Rames626950d2013-01-14 17:20:22 +0000217 (efx->state == STATE_RECOVERY) || \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000218 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100219 ASSERT_RTNL(); \
220 } while (0)
221
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100222static int efx_check_disabled(struct efx_nic *efx)
223{
Alexandre Rames626950d2013-01-14 17:20:22 +0000224 if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100225 netif_err(efx, drv, efx->net_dev,
226 "device is disabled due to earlier errors\n");
227 return -EIO;
228 }
229 return 0;
230}
231
Ben Hutchings8ceee662008-04-27 12:55:59 +0100232/**************************************************************************
233 *
234 * Event queue processing
235 *
236 *************************************************************************/
237
238/* Process channel's event queue
239 *
240 * This function is responsible for processing the event queue of a
241 * single channel. The caller must guarantee that this function will
242 * never be concurrently called more than once on the same channel,
243 * though different channels may be being processed concurrently.
244 */
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000245static int efx_process_channel(struct efx_channel *channel, int budget)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100246{
Peter Dunningc9368352015-07-08 10:05:10 +0100247 struct efx_tx_queue *tx_queue;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000248 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100249
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000250 if (unlikely(!channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100251 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100252
Peter Dunningc9368352015-07-08 10:05:10 +0100253 efx_for_each_channel_tx_queue(tx_queue, channel) {
254 tx_queue->pkts_compl = 0;
255 tx_queue->bytes_compl = 0;
256 }
257
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000258 spent = efx_nic_process_eventq(channel, budget);
Ben Hutchingsd9ab7002012-02-13 23:29:16 +0000259 if (spent && efx_channel_has_rx_queue(channel)) {
260 struct efx_rx_queue *rx_queue =
261 efx_channel_get_rx_queue(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100262
Ben Hutchingsff734ef2013-01-29 23:33:14 +0000263 efx_rx_flush_packet(channel);
Jon Coopercce28792013-10-02 11:04:14 +0100264 efx_fast_push_rx_descriptors(rx_queue, true);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100265 }
266
Peter Dunningc9368352015-07-08 10:05:10 +0100267 /* Update BQL */
268 efx_for_each_channel_tx_queue(tx_queue, channel) {
269 if (tx_queue->bytes_compl) {
270 netdev_tx_completed_queue(tx_queue->core_txq,
271 tx_queue->pkts_compl, tx_queue->bytes_compl);
272 }
273 }
274
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000275 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100276}
277
Ben Hutchings8ceee662008-04-27 12:55:59 +0100278/* NAPI poll handler
279 *
280 * NAPI guarantees serialisation of polls of the same device, which
281 * provides the guarantee required by efx_process_channel().
282 */
Bert Kenward539de7c2016-08-11 13:02:09 +0100283static void efx_update_irq_mod(struct efx_nic *efx, struct efx_channel *channel)
284{
285 int step = efx->irq_mod_step_us;
286
287 if (channel->irq_mod_score < irq_adapt_low_thresh) {
288 if (channel->irq_moderation_us > step) {
289 channel->irq_moderation_us -= step;
290 efx->type->push_irq_moderation(channel);
291 }
292 } else if (channel->irq_mod_score > irq_adapt_high_thresh) {
293 if (channel->irq_moderation_us <
294 efx->irq_rx_moderation_us) {
295 channel->irq_moderation_us += step;
296 efx->type->push_irq_moderation(channel);
297 }
298 }
299
300 channel->irq_count = 0;
301 channel->irq_mod_score = 0;
302}
303
Ben Hutchings8ceee662008-04-27 12:55:59 +0100304static int efx_poll(struct napi_struct *napi, int budget)
305{
306 struct efx_channel *channel =
307 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings62776d02010-06-23 11:30:07 +0000308 struct efx_nic *efx = channel->efx;
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000309 int spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100310
Ben Hutchings62776d02010-06-23 11:30:07 +0000311 netif_vdbg(efx, intr, efx->net_dev,
312 "channel %d NAPI poll executing on CPU %d\n",
313 channel->channel, raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +0100314
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000315 spent = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100316
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000317 if (spent < budget) {
Ben Hutchings9d9a6972012-02-10 23:01:48 +0000318 if (efx_channel_has_rx_queue(channel) &&
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000319 efx->irq_rx_adaptive &&
320 unlikely(++channel->irq_count == 1000)) {
Bert Kenward539de7c2016-08-11 13:02:09 +0100321 efx_update_irq_mod(efx, channel);
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000322 }
323
Ben Hutchings64d8ad62011-01-05 00:50:41 +0000324 efx_filter_rfs_expire(channel);
325
Ben Hutchings8ceee662008-04-27 12:55:59 +0100326 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800327 * only wait for napi_complete(), this isn't a problem
Ben Hutchings514bedb2012-10-05 19:30:16 +0100328 * since efx_nic_eventq_read_ack() will have no effect if
Ben Hutchings8ceee662008-04-27 12:55:59 +0100329 * interrupts have already been disabled.
330 */
Bert Kenwardf820c0a2017-02-06 16:50:55 +0000331 if (napi_complete_done(napi, spent))
332 efx_nic_eventq_read_ack(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100333 }
334
Ben Hutchingsfa236e12010-04-28 09:29:42 +0000335 return spent;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100336}
337
Ben Hutchings8ceee662008-04-27 12:55:59 +0100338/* Create event queue
339 * Event queue memory allocations are done only once. If the channel
340 * is reset, the memory buffer will be reused; this guards against
341 * errors during channel reset and also simplifies interrupt handling.
342 */
343static int efx_probe_eventq(struct efx_channel *channel)
344{
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000345 struct efx_nic *efx = channel->efx;
346 unsigned long entries;
347
Ben Hutchings86ee5302012-01-09 19:51:22 +0000348 netif_dbg(efx, probe, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000349 "chan %d create event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100350
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000351 /* Build an event queue with room for one event per tx and rx buffer,
352 * plus some extra for link state events and MCDI completions. */
353 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
Edward Creee01b16a2016-12-02 15:51:33 +0000354 EFX_WARN_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
Steve Hodgsonecc910f2010-09-10 06:42:22 +0000355 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
356
Ben Hutchings152b6a62009-11-29 03:43:56 +0000357 return efx_nic_probe_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100358}
359
360/* Prepare channel's event queue */
Jon Cooper261e4d92013-04-15 18:51:54 +0100361static int efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100362{
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100363 struct efx_nic *efx = channel->efx;
Jon Cooper261e4d92013-04-15 18:51:54 +0100364 int rc;
365
366 EFX_WARN_ON_PARANOID(channel->eventq_init);
367
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100368 netif_dbg(efx, drv, efx->net_dev,
Ben Hutchings62776d02010-06-23 11:30:07 +0000369 "chan %d init event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100370
Jon Cooper261e4d92013-04-15 18:51:54 +0100371 rc = efx_nic_init_eventq(channel);
372 if (rc == 0) {
Ben Hutchings15acb1c2013-05-01 16:30:17 +0100373 efx->type->push_irq_moderation(channel);
Jon Cooper261e4d92013-04-15 18:51:54 +0100374 channel->eventq_read_ptr = 0;
375 channel->eventq_init = true;
376 }
377 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100378}
379
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000380/* Enable event queue processing and NAPI */
Alexandre Rames36763262014-07-22 14:03:25 +0100381void efx_start_eventq(struct efx_channel *channel)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000382{
383 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
384 "chan %d start event queue\n", channel->channel);
385
Ben Hutchings514bedb2012-10-05 19:30:16 +0100386 /* Make sure the NAPI handler sees the enabled flag set */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000387 channel->enabled = true;
388 smp_wmb();
389
390 napi_enable(&channel->napi_str);
391 efx_nic_eventq_read_ack(channel);
392}
393
394/* Disable event queue processing and NAPI */
Alexandre Rames36763262014-07-22 14:03:25 +0100395void efx_stop_eventq(struct efx_channel *channel)
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000396{
397 if (!channel->enabled)
398 return;
399
400 napi_disable(&channel->napi_str);
401 channel->enabled = false;
402}
403
Ben Hutchings8ceee662008-04-27 12:55:59 +0100404static void efx_fini_eventq(struct efx_channel *channel)
405{
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100406 if (!channel->eventq_init)
407 return;
408
Ben Hutchings62776d02010-06-23 11:30:07 +0000409 netif_dbg(channel->efx, drv, channel->efx->net_dev,
410 "chan %d fini event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100411
Ben Hutchings152b6a62009-11-29 03:43:56 +0000412 efx_nic_fini_eventq(channel);
Ben Hutchingsbe3fc092012-10-08 18:21:51 +0100413 channel->eventq_init = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100414}
415
416static void efx_remove_eventq(struct efx_channel *channel)
417{
Ben Hutchings62776d02010-06-23 11:30:07 +0000418 netif_dbg(channel->efx, drv, channel->efx->net_dev,
419 "chan %d remove event queue\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100420
Ben Hutchings152b6a62009-11-29 03:43:56 +0000421 efx_nic_remove_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100422}
423
424/**************************************************************************
425 *
426 * Channel handling
427 *
428 *************************************************************************/
429
Ben Hutchings7f967c02012-02-13 23:45:02 +0000430/* Allocate and initialise a channel structure. */
Ben Hutchings46426102010-09-10 06:42:33 +0000431static struct efx_channel *
432efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
433{
434 struct efx_channel *channel;
435 struct efx_rx_queue *rx_queue;
436 struct efx_tx_queue *tx_queue;
437 int j;
438
Ben Hutchings7f967c02012-02-13 23:45:02 +0000439 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
440 if (!channel)
441 return NULL;
Ben Hutchings46426102010-09-10 06:42:33 +0000442
Ben Hutchings7f967c02012-02-13 23:45:02 +0000443 channel->efx = efx;
444 channel->channel = i;
445 channel->type = &efx_default_channel_type;
Ben Hutchings46426102010-09-10 06:42:33 +0000446
Ben Hutchings7f967c02012-02-13 23:45:02 +0000447 for (j = 0; j < EFX_TXQ_TYPES; j++) {
448 tx_queue = &channel->tx_queue[j];
449 tx_queue->efx = efx;
450 tx_queue->queue = i * EFX_TXQ_TYPES + j;
451 tx_queue->channel = channel;
Ben Hutchings46426102010-09-10 06:42:33 +0000452 }
453
Ben Hutchings46426102010-09-10 06:42:33 +0000454 rx_queue = &channel->rx_queue;
455 rx_queue->efx = efx;
456 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
457 (unsigned long)rx_queue);
458
459 return channel;
460}
461
Ben Hutchings7f967c02012-02-13 23:45:02 +0000462/* Allocate and initialise a channel structure, copying parameters
463 * (but not resources) from an old channel structure.
464 */
465static struct efx_channel *
466efx_copy_channel(const struct efx_channel *old_channel)
467{
468 struct efx_channel *channel;
469 struct efx_rx_queue *rx_queue;
470 struct efx_tx_queue *tx_queue;
471 int j;
472
473 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
474 if (!channel)
475 return NULL;
476
477 *channel = *old_channel;
478
479 channel->napi_dev = NULL;
Bert Kenward46d054f2016-11-11 15:56:51 +0000480 INIT_HLIST_NODE(&channel->napi_str.napi_hash_node);
481 channel->napi_str.napi_id = 0;
482 channel->napi_str.state = 0;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000483 memset(&channel->eventq, 0, sizeof(channel->eventq));
484
485 for (j = 0; j < EFX_TXQ_TYPES; j++) {
486 tx_queue = &channel->tx_queue[j];
487 if (tx_queue->channel)
488 tx_queue->channel = channel;
489 tx_queue->buffer = NULL;
490 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
491 }
492
493 rx_queue = &channel->rx_queue;
494 rx_queue->buffer = NULL;
495 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
496 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
497 (unsigned long)rx_queue);
498
499 return channel;
500}
501
Ben Hutchings8ceee662008-04-27 12:55:59 +0100502static int efx_probe_channel(struct efx_channel *channel)
503{
504 struct efx_tx_queue *tx_queue;
505 struct efx_rx_queue *rx_queue;
506 int rc;
507
Ben Hutchings62776d02010-06-23 11:30:07 +0000508 netif_dbg(channel->efx, probe, channel->efx->net_dev,
509 "creating channel %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100510
Ben Hutchings7f967c02012-02-13 23:45:02 +0000511 rc = channel->type->pre_probe(channel);
512 if (rc)
513 goto fail;
514
Ben Hutchings8ceee662008-04-27 12:55:59 +0100515 rc = efx_probe_eventq(channel);
516 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000517 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100518
519 efx_for_each_channel_tx_queue(tx_queue, channel) {
520 rc = efx_probe_tx_queue(tx_queue);
521 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000522 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100523 }
524
525 efx_for_each_channel_rx_queue(rx_queue, channel) {
526 rc = efx_probe_rx_queue(rx_queue);
527 if (rc)
Ben Hutchings7f967c02012-02-13 23:45:02 +0000528 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100529 }
530
Ben Hutchings8ceee662008-04-27 12:55:59 +0100531 return 0;
532
Ben Hutchings7f967c02012-02-13 23:45:02 +0000533fail:
534 efx_remove_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100535 return rc;
536}
537
Ben Hutchings7f967c02012-02-13 23:45:02 +0000538static void
539efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
540{
541 struct efx_nic *efx = channel->efx;
542 const char *type;
543 int number;
544
545 number = channel->channel;
546 if (efx->tx_channel_offset == 0) {
547 type = "";
548 } else if (channel->channel < efx->tx_channel_offset) {
549 type = "-rx";
550 } else {
551 type = "-tx";
552 number -= efx->tx_channel_offset;
553 }
554 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
555}
Ben Hutchings8ceee662008-04-27 12:55:59 +0100556
Ben Hutchings56536e92008-12-12 21:37:02 -0800557static void efx_set_channel_names(struct efx_nic *efx)
558{
559 struct efx_channel *channel;
Ben Hutchings56536e92008-12-12 21:37:02 -0800560
Ben Hutchings7f967c02012-02-13 23:45:02 +0000561 efx_for_each_channel(channel, efx)
562 channel->type->get_name(channel,
Ben Hutchingsd8291182012-10-05 23:35:41 +0100563 efx->msi_context[channel->channel].name,
564 sizeof(efx->msi_context[0].name));
Ben Hutchings56536e92008-12-12 21:37:02 -0800565}
566
Ben Hutchings46426102010-09-10 06:42:33 +0000567static int efx_probe_channels(struct efx_nic *efx)
568{
569 struct efx_channel *channel;
570 int rc;
571
572 /* Restart special buffer allocation */
573 efx->next_buffer_table = 0;
574
Ben Hutchingsc92aaff2012-02-21 23:22:00 +0000575 /* Probe channels in reverse, so that any 'extra' channels
576 * use the start of the buffer table. This allows the traffic
577 * channels to be resized without moving them or wasting the
578 * entries before them.
579 */
580 efx_for_each_channel_rev(channel, efx) {
Ben Hutchings46426102010-09-10 06:42:33 +0000581 rc = efx_probe_channel(channel);
582 if (rc) {
583 netif_err(efx, probe, efx->net_dev,
584 "failed to create channel %d\n",
585 channel->channel);
586 goto fail;
587 }
588 }
589 efx_set_channel_names(efx);
590
591 return 0;
592
593fail:
594 efx_remove_channels(efx);
595 return rc;
596}
597
Ben Hutchings8ceee662008-04-27 12:55:59 +0100598/* Channels are shutdown and reinitialised whilst the NIC is running
599 * to propagate configuration changes (mtu, checksum offload), or
600 * to clear hardware error conditions
601 */
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000602static void efx_start_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100603{
Andrew Rybchenkoebfcd0f2016-06-15 17:43:20 +0100604 netdev_features_t old_features = efx->net_dev->features;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000605 bool old_rx_scatter = efx->rx_scatter;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100606 struct efx_tx_queue *tx_queue;
607 struct efx_rx_queue *rx_queue;
608 struct efx_channel *channel;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000609 size_t rx_buf_len;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100610
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100611 /* Calculate the rx buffer allocation parameters required to
612 * support the current MTU, including padding for header
613 * alignment and overruns.
614 */
Jon Cooper43a37392012-10-18 15:49:54 +0100615 efx->rx_dma_len = (efx->rx_prefix_size +
Ben Hutchings272baee2013-01-29 23:33:14 +0000616 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
617 efx->type->rx_buffer_padding);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000618 rx_buf_len = (sizeof(struct efx_rx_page_state) +
Andrew Rybchenko2ec03012013-11-16 11:02:27 +0400619 efx->rx_ip_align + efx->rx_dma_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000620 if (rx_buf_len <= PAGE_SIZE) {
Jon Coopere8c68c02013-03-08 10:18:28 +0000621 efx->rx_scatter = efx->type->always_rx_scatter;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000622 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000623 } else if (efx->type->can_rx_scatter) {
Ben Hutchings950c54d2013-05-13 12:01:22 +0000624 BUILD_BUG_ON(EFX_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000625 BUILD_BUG_ON(sizeof(struct efx_rx_page_state) +
Ben Hutchings950c54d2013-05-13 12:01:22 +0000626 2 * ALIGN(NET_IP_ALIGN + EFX_RX_USR_BUF_SIZE,
627 EFX_RX_BUF_ALIGNMENT) >
628 PAGE_SIZE);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000629 efx->rx_scatter = true;
630 efx->rx_dma_len = EFX_RX_USR_BUF_SIZE;
631 efx->rx_buffer_order = 0;
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000632 } else {
633 efx->rx_scatter = false;
634 efx->rx_buffer_order = get_order(rx_buf_len);
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000635 }
636
Daniel Pieczko1648a232013-02-13 10:54:41 +0000637 efx_rx_config_page_split(efx);
638 if (efx->rx_buffer_order)
639 netif_dbg(efx, drv, efx->net_dev,
640 "RX buf len=%u; page order=%u batch=%u\n",
641 efx->rx_dma_len, efx->rx_buffer_order,
642 efx->rx_pages_per_batch);
643 else
644 netif_dbg(efx, drv, efx->net_dev,
645 "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
646 efx->rx_dma_len, efx->rx_page_buf_step,
647 efx->rx_bufs_per_page, efx->rx_pages_per_batch);
Daniel Pieczko27689352013-02-13 10:54:41 +0000648
Andrew Rybchenkoebfcd0f2016-06-15 17:43:20 +0100649 /* Restore previously fixed features in hw_features and remove
650 * features which are fixed now
651 */
652 efx->net_dev->hw_features |= efx->net_dev->features;
653 efx->net_dev->hw_features &= ~efx->fixed_features;
654 efx->net_dev->features |= efx->fixed_features;
655 if (efx->net_dev->features != old_features)
656 netdev_features_change(efx->net_dev);
657
Jon Coopere8c68c02013-03-08 10:18:28 +0000658 /* RX filters may also have scatter-enabled flags */
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000659 if (efx->rx_scatter != old_rx_scatter)
Ben Hutchingsadd72472012-11-08 01:46:53 +0000660 efx->type->filter_update_rx_scatter(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100661
Ben Hutchings14bf7182012-05-22 01:27:58 +0100662 /* We must keep at least one descriptor in a TX ring empty.
663 * We could avoid this when the queue size does not exactly
664 * match the hardware ring size, but it's not that important.
665 * Therefore we stop the queue when one more skb might fill
666 * the ring completely. We wake it when half way back to
667 * empty.
668 */
669 efx->txq_stop_thresh = efx->txq_entries - efx_tx_max_skb_descs(efx);
670 efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
671
Ben Hutchings8ceee662008-04-27 12:55:59 +0100672 /* Initialise the channels */
673 efx_for_each_channel(channel, efx) {
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100674 efx_for_each_channel_tx_queue(tx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100675 efx_init_tx_queue(tx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100676 atomic_inc(&efx->active_queues);
677 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100678
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000679 efx_for_each_channel_rx_queue(rx_queue, channel) {
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100680 efx_init_rx_queue(rx_queue);
Alexandre Rames3881d8a2013-06-10 11:03:21 +0100681 atomic_inc(&efx->active_queues);
Jon Coopercce28792013-10-02 11:04:14 +0100682 efx_stop_eventq(channel);
683 efx_fast_push_rx_descriptors(rx_queue, false);
684 efx_start_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000685 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100686
Ben Hutchings85740cdf2013-01-29 23:33:15 +0000687 WARN_ON(channel->rx_pkt_n_frags);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100688 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000689
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000690 efx_ptp_start_datapath(efx);
691
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000692 if (netif_device_present(efx->net_dev))
693 netif_tx_wake_all_queues(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100694}
695
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000696static void efx_stop_datapath(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100697{
698 struct efx_channel *channel;
699 struct efx_tx_queue *tx_queue;
700 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100701 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100702
703 EFX_ASSERT_RESET_SERIALISED(efx);
704 BUG_ON(efx->port_enabled);
705
Alexandre Rames2ea4dc22013-11-08 10:20:31 +0000706 efx_ptp_stop_datapath(efx);
707
Ben Hutchingsd8aec742013-05-27 16:52:54 +0100708 /* Stop RX refill */
709 efx_for_each_channel(channel, efx) {
710 efx_for_each_channel_rx_queue(rx_queue, channel)
711 rx_queue->refill_enabled = false;
712 }
713
Ben Hutchings8ceee662008-04-27 12:55:59 +0100714 efx_for_each_channel(channel, efx) {
Ben Hutchings9f2cb712012-02-08 00:11:20 +0000715 /* RX packet processing is pipelined, so wait for the
716 * NAPI handler to complete. At least event queue 0
717 * might be kept active by non-data events, so don't
718 * use napi_synchronize() but actually disable NAPI
719 * temporarily.
720 */
721 if (efx_channel_has_rx_queue(channel)) {
722 efx_stop_eventq(channel);
723 efx_start_eventq(channel);
724 }
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100725 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100726
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100727 rc = efx->type->fini_dmaq(efx);
Edward Cree5a6681e2016-11-28 18:55:34 +0000728 if (rc) {
Ben Hutchingse42c3d82013-05-27 16:52:54 +0100729 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
730 } else {
731 netif_dbg(efx, drv, efx->net_dev,
732 "successfully flushed all queues\n");
733 }
734
735 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100736 efx_for_each_channel_rx_queue(rx_queue, channel)
737 efx_fini_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000738 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100739 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100740 }
741}
742
743static void efx_remove_channel(struct efx_channel *channel)
744{
745 struct efx_tx_queue *tx_queue;
746 struct efx_rx_queue *rx_queue;
747
Ben Hutchings62776d02010-06-23 11:30:07 +0000748 netif_dbg(channel->efx, drv, channel->efx->net_dev,
749 "destroy chan %d\n", channel->channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100750
751 efx_for_each_channel_rx_queue(rx_queue, channel)
752 efx_remove_rx_queue(rx_queue);
Ben Hutchings94b274b2011-01-10 21:18:20 +0000753 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100754 efx_remove_tx_queue(tx_queue);
755 efx_remove_eventq(channel);
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100756 channel->type->post_remove(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100757}
758
Ben Hutchings46426102010-09-10 06:42:33 +0000759static void efx_remove_channels(struct efx_nic *efx)
760{
761 struct efx_channel *channel;
762
763 efx_for_each_channel(channel, efx)
764 efx_remove_channel(channel);
765}
766
767int
768efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
769{
770 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
771 u32 old_rxq_entries, old_txq_entries;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000772 unsigned i, next_buffer_table = 0;
Jon Cooper261e4d92013-04-15 18:51:54 +0100773 int rc, rc2;
Ben Hutchings8b7325b2012-07-27 20:46:41 +0100774
775 rc = efx_check_disabled(efx);
776 if (rc)
777 return rc;
Ben Hutchings7f967c02012-02-13 23:45:02 +0000778
779 /* Not all channels should be reallocated. We must avoid
780 * reallocating their buffer table entries.
781 */
782 efx_for_each_channel(channel, efx) {
783 struct efx_rx_queue *rx_queue;
784 struct efx_tx_queue *tx_queue;
785
786 if (channel->type->copy)
787 continue;
788 next_buffer_table = max(next_buffer_table,
789 channel->eventq.index +
790 channel->eventq.entries);
791 efx_for_each_channel_rx_queue(rx_queue, channel)
792 next_buffer_table = max(next_buffer_table,
793 rx_queue->rxd.index +
794 rx_queue->rxd.entries);
795 efx_for_each_channel_tx_queue(tx_queue, channel)
796 next_buffer_table = max(next_buffer_table,
797 tx_queue->txd.index +
798 tx_queue->txd.entries);
799 }
Ben Hutchings46426102010-09-10 06:42:33 +0000800
Ben Hutchings29c69a42013-01-28 19:01:06 +0000801 efx_device_detach_sync(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000802 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +0100803 efx_soft_disable_interrupts(efx);
Ben Hutchings46426102010-09-10 06:42:33 +0000804
Ben Hutchings7f967c02012-02-13 23:45:02 +0000805 /* Clone channels (where possible) */
Ben Hutchings46426102010-09-10 06:42:33 +0000806 memset(other_channel, 0, sizeof(other_channel));
807 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000808 channel = efx->channel[i];
809 if (channel->type->copy)
810 channel = channel->type->copy(channel);
Ben Hutchings46426102010-09-10 06:42:33 +0000811 if (!channel) {
812 rc = -ENOMEM;
813 goto out;
814 }
815 other_channel[i] = channel;
816 }
817
818 /* Swap entry counts and channel pointers */
819 old_rxq_entries = efx->rxq_entries;
820 old_txq_entries = efx->txq_entries;
821 efx->rxq_entries = rxq_entries;
822 efx->txq_entries = txq_entries;
823 for (i = 0; i < efx->n_channels; i++) {
824 channel = efx->channel[i];
825 efx->channel[i] = other_channel[i];
826 other_channel[i] = channel;
827 }
828
Ben Hutchings7f967c02012-02-13 23:45:02 +0000829 /* Restart buffer table allocation */
830 efx->next_buffer_table = next_buffer_table;
Ben Hutchings46426102010-09-10 06:42:33 +0000831
Ben Hutchingse8f14992010-12-07 19:47:34 +0000832 for (i = 0; i < efx->n_channels; i++) {
Ben Hutchings7f967c02012-02-13 23:45:02 +0000833 channel = efx->channel[i];
834 if (!channel->type->copy)
835 continue;
836 rc = efx_probe_channel(channel);
837 if (rc)
838 goto rollback;
839 efx_init_napi_channel(efx->channel[i]);
Ben Hutchingse8f14992010-12-07 19:47:34 +0000840 }
Ben Hutchings46426102010-09-10 06:42:33 +0000841
Ben Hutchings7f967c02012-02-13 23:45:02 +0000842out:
843 /* Destroy unused channel structures */
844 for (i = 0; i < efx->n_channels; i++) {
845 channel = other_channel[i];
846 if (channel && channel->type->copy) {
847 efx_fini_napi_channel(channel);
848 efx_remove_channel(channel);
849 kfree(channel);
850 }
851 }
852
Jon Cooper261e4d92013-04-15 18:51:54 +0100853 rc2 = efx_soft_enable_interrupts(efx);
854 if (rc2) {
855 rc = rc ? rc : rc2;
856 netif_err(efx, drv, efx->net_dev,
857 "unable to restart interrupts on channel reallocation\n");
858 efx_schedule_reset(efx, RESET_TYPE_DISABLE);
859 } else {
860 efx_start_all(efx);
861 netif_device_attach(efx->net_dev);
862 }
Ben Hutchings46426102010-09-10 06:42:33 +0000863 return rc;
864
865rollback:
866 /* Swap back */
867 efx->rxq_entries = old_rxq_entries;
868 efx->txq_entries = old_txq_entries;
869 for (i = 0; i < efx->n_channels; i++) {
870 channel = efx->channel[i];
871 efx->channel[i] = other_channel[i];
872 other_channel[i] = channel;
873 }
874 goto out;
875}
876
Steve Hodgson90d683a2010-06-01 11:19:39 +0000877void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100878{
Steve Hodgson90d683a2010-06-01 11:19:39 +0000879 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100880}
881
Ben Hutchings7f967c02012-02-13 23:45:02 +0000882static const struct efx_channel_type efx_default_channel_type = {
883 .pre_probe = efx_channel_dummy_op_int,
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100884 .post_remove = efx_channel_dummy_op_void,
Ben Hutchings7f967c02012-02-13 23:45:02 +0000885 .get_name = efx_get_channel_name,
886 .copy = efx_copy_channel,
887 .keep_eventq = false,
888};
889
890int efx_channel_dummy_op_int(struct efx_channel *channel)
891{
892 return 0;
893}
894
Stuart Hodgsonc31e5f92012-07-18 09:52:11 +0100895void efx_channel_dummy_op_void(struct efx_channel *channel)
896{
897}
898
Ben Hutchings8ceee662008-04-27 12:55:59 +0100899/**************************************************************************
900 *
901 * Port handling
902 *
903 **************************************************************************/
904
905/* This ensures that the kernel is kept informed (via
906 * netif_carrier_on/off) of the link status, and also maintains the
907 * link status's stop on the port's TX queue.
908 */
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +0000909void efx_link_status_changed(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100910{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000911 struct efx_link_state *link_state = &efx->link_state;
912
Ben Hutchings8ceee662008-04-27 12:55:59 +0100913 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
914 * that no events are triggered between unregister_netdev() and the
915 * driver unloading. A more general condition is that NETDEV_CHANGE
916 * can only be generated between NETDEV_UP and NETDEV_DOWN */
917 if (!netif_running(efx->net_dev))
918 return;
919
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000920 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100921 efx->n_link_state_changes++;
922
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000923 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100924 netif_carrier_on(efx->net_dev);
925 else
926 netif_carrier_off(efx->net_dev);
927 }
928
929 /* Status message for kernel log */
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000930 if (link_state->up)
Ben Hutchings62776d02010-06-23 11:30:07 +0000931 netif_info(efx, link, efx->net_dev,
Ben Hutchings964e6132012-11-19 23:08:22 +0000932 "link up at %uMbps %s-duplex (MTU %d)\n",
Ben Hutchings62776d02010-06-23 11:30:07 +0000933 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings964e6132012-11-19 23:08:22 +0000934 efx->net_dev->mtu);
Ben Hutchings2aa9ef12012-01-09 19:53:41 +0000935 else
Ben Hutchings62776d02010-06-23 11:30:07 +0000936 netif_info(efx, link, efx->net_dev, "link down\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100937}
938
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000939void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
940{
941 efx->link_advertising = advertising;
942 if (advertising) {
943 if (advertising & ADVERTISED_Pause)
944 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
945 else
946 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
947 if (advertising & ADVERTISED_Asym_Pause)
948 efx->wanted_fc ^= EFX_FC_TX;
949 }
950}
951
David S. Millerb56269462011-05-17 17:53:22 -0400952void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000953{
954 efx->wanted_fc = wanted_fc;
955 if (efx->link_advertising) {
956 if (wanted_fc & EFX_FC_RX)
957 efx->link_advertising |= (ADVERTISED_Pause |
958 ADVERTISED_Asym_Pause);
959 else
960 efx->link_advertising &= ~(ADVERTISED_Pause |
961 ADVERTISED_Asym_Pause);
962 if (wanted_fc & EFX_FC_TX)
963 efx->link_advertising ^= ADVERTISED_Asym_Pause;
964 }
965}
966
Ben Hutchings115122a2009-03-04 09:52:52 +0000967static void efx_fini_port(struct efx_nic *efx);
968
Edward Cree0d322412015-05-20 11:10:03 +0100969/* We assume that efx->type->reconfigure_mac will always try to sync RX
970 * filters and therefore needs to read-lock the filter table against freeing
971 */
972void efx_mac_reconfigure(struct efx_nic *efx)
973{
974 down_read(&efx->filter_sem);
975 efx->type->reconfigure_mac(efx);
976 up_read(&efx->filter_sem);
977}
978
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000979/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
980 * the MAC appropriately. All other PHY configuration changes are pushed
981 * through phy_op->set_settings(), and pushed asynchronously to the MAC
982 * through efx_monitor().
983 *
984 * Callers must hold the mac_lock
985 */
986int __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100987{
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000988 enum efx_phy_mode phy_mode;
989 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100990
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000991 WARN_ON(!mutex_is_locked(&efx->mac_lock));
Ben Hutchings8ceee662008-04-27 12:55:59 +0100992
Ben Hutchingsd3245b22009-11-29 03:42:41 +0000993 /* Disable PHY transmit in mac level loopbacks */
994 phy_mode = efx->phy_mode;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800995 if (LOOPBACK_INTERNAL(efx))
996 efx->phy_mode |= PHY_MODE_TX_DISABLED;
997 else
998 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800999
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001000 rc = efx->type->reconfigure_port(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001001
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001002 if (rc)
1003 efx->phy_mode = phy_mode;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001004
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001005 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001006}
1007
1008/* Reinitialise the MAC to pick up new PHY settings, even if the port is
1009 * disabled. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001010int efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001011{
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001012 int rc;
1013
Ben Hutchings8ceee662008-04-27 12:55:59 +01001014 EFX_ASSERT_RESET_SERIALISED(efx);
1015
1016 mutex_lock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001017 rc = __efx_reconfigure_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001018 mutex_unlock(&efx->mac_lock);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001019
1020 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001021}
1022
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001023/* Asynchronous work item for changing MAC promiscuity and multicast
1024 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
1025 * MAC directly. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001026static void efx_mac_work(struct work_struct *data)
1027{
1028 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
1029
1030 mutex_lock(&efx->mac_lock);
Ben Hutchings30b81cd2011-09-13 19:47:48 +01001031 if (efx->port_enabled)
Edward Cree0d322412015-05-20 11:10:03 +01001032 efx_mac_reconfigure(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001033 mutex_unlock(&efx->mac_lock);
1034}
1035
Ben Hutchings8ceee662008-04-27 12:55:59 +01001036static int efx_probe_port(struct efx_nic *efx)
1037{
1038 int rc;
1039
Ben Hutchings62776d02010-06-23 11:30:07 +00001040 netif_dbg(efx, probe, efx->net_dev, "create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001041
Steve Hodgsonff3b00a2009-12-23 13:46:36 +00001042 if (phy_flash_cfg)
1043 efx->phy_mode = PHY_MODE_SPECIAL;
1044
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001045 /* Connect up MAC/PHY operations table */
1046 rc = efx->type->probe_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001047 if (rc)
Ben Hutchingse42de262010-09-10 06:41:19 +00001048 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001049
Ben Hutchingse332bcb2011-12-20 01:22:51 +00001050 /* Initialise MAC address to permanent address */
Edward Creecd84ff42014-03-07 18:27:41 +00001051 ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001052
1053 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001054}
1055
1056static int efx_init_port(struct efx_nic *efx)
1057{
1058 int rc;
1059
Ben Hutchings62776d02010-06-23 11:30:07 +00001060 netif_dbg(efx, drv, efx->net_dev, "init port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001061
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001062 mutex_lock(&efx->mac_lock);
1063
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001064 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001065 if (rc)
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001066 goto fail1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001067
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001068 efx->port_initialized = true;
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001069
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001070 /* Reconfigure the MAC before creating dma queues (required for
1071 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
Edward Cree0d322412015-05-20 11:10:03 +01001072 efx_mac_reconfigure(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001073
1074 /* Ensure the PHY advertises the correct flow control settings */
1075 rc = efx->phy_op->reconfigure(efx);
Edward Cree267d9d72015-05-06 00:59:18 +01001076 if (rc && rc != -EPERM)
Ben Hutchingsd3245b22009-11-29 03:42:41 +00001077 goto fail2;
1078
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001079 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001080 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001081
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001082fail2:
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001083 efx->phy_op->fini(efx);
Ben Hutchings1dfc5ce2009-11-25 16:11:19 +00001084fail1:
1085 mutex_unlock(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001086 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001087}
1088
Ben Hutchings8ceee662008-04-27 12:55:59 +01001089static void efx_start_port(struct efx_nic *efx)
1090{
Ben Hutchings62776d02010-06-23 11:30:07 +00001091 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001092 BUG_ON(efx->port_enabled);
1093
1094 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001095 efx->port_enabled = true;
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001096
Ben Hutchingsd615c032013-10-08 17:33:20 +01001097 /* Ensure MAC ingress/egress is enabled */
Edward Cree0d322412015-05-20 11:10:03 +01001098 efx_mac_reconfigure(efx);
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00001099
Ben Hutchings8ceee662008-04-27 12:55:59 +01001100 mutex_unlock(&efx->mac_lock);
1101}
1102
Ben Hutchingsd615c032013-10-08 17:33:20 +01001103/* Cancel work for MAC reconfiguration, periodic hardware monitoring
1104 * and the async self-test, wait for them to finish and prevent them
1105 * being scheduled again. This doesn't cover online resets, which
1106 * should only be cancelled when removing the device.
1107 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001108static void efx_stop_port(struct efx_nic *efx)
1109{
Ben Hutchings62776d02010-06-23 11:30:07 +00001110 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001111
Ben Hutchingsd615c032013-10-08 17:33:20 +01001112 EFX_ASSERT_RESET_SERIALISED(efx);
1113
Ben Hutchings8ceee662008-04-27 12:55:59 +01001114 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001115 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001116 mutex_unlock(&efx->mac_lock);
1117
1118 /* Serialise against efx_set_multicast_list() */
Ben Hutchings73ba7b62012-01-09 19:47:08 +00001119 netif_addr_lock_bh(efx->net_dev);
1120 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchingsd615c032013-10-08 17:33:20 +01001121
1122 cancel_delayed_work_sync(&efx->monitor_work);
1123 efx_selftest_async_cancel(efx);
1124 cancel_work_sync(&efx->mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001125}
1126
1127static void efx_fini_port(struct efx_nic *efx)
1128{
Ben Hutchings62776d02010-06-23 11:30:07 +00001129 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001130
1131 if (!efx->port_initialized)
1132 return;
1133
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001134 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001135 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001136
Ben Hutchingseb50c0d2009-11-23 16:06:30 +00001137 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001138 efx_link_status_changed(efx);
1139}
1140
1141static void efx_remove_port(struct efx_nic *efx)
1142{
Ben Hutchings62776d02010-06-23 11:30:07 +00001143 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001144
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001145 efx->type->remove_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001146}
1147
1148/**************************************************************************
1149 *
1150 * NIC handling
1151 *
1152 **************************************************************************/
1153
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01001154static LIST_HEAD(efx_primary_list);
1155static LIST_HEAD(efx_unassociated_list);
1156
1157static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
1158{
1159 return left->type == right->type &&
1160 left->vpd_sn && right->vpd_sn &&
1161 !strcmp(left->vpd_sn, right->vpd_sn);
1162}
1163
1164static void efx_associate(struct efx_nic *efx)
1165{
1166 struct efx_nic *other, *next;
1167
1168 if (efx->primary == efx) {
1169 /* Adding primary function; look for secondaries */
1170
1171 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
1172 list_add_tail(&efx->node, &efx_primary_list);
1173
1174 list_for_each_entry_safe(other, next, &efx_unassociated_list,
1175 node) {
1176 if (efx_same_controller(efx, other)) {
1177 list_del(&other->node);
1178 netif_dbg(other, probe, other->net_dev,
1179 "moving to secondary list of %s %s\n",
1180 pci_name(efx->pci_dev),
1181 efx->net_dev->name);
1182 list_add_tail(&other->node,
1183 &efx->secondary_list);
1184 other->primary = efx;
1185 }
1186 }
1187 } else {
1188 /* Adding secondary function; look for primary */
1189
1190 list_for_each_entry(other, &efx_primary_list, node) {
1191 if (efx_same_controller(efx, other)) {
1192 netif_dbg(efx, probe, efx->net_dev,
1193 "adding to secondary list of %s %s\n",
1194 pci_name(other->pci_dev),
1195 other->net_dev->name);
1196 list_add_tail(&efx->node,
1197 &other->secondary_list);
1198 efx->primary = other;
1199 return;
1200 }
1201 }
1202
1203 netif_dbg(efx, probe, efx->net_dev,
1204 "adding to unassociated list\n");
1205 list_add_tail(&efx->node, &efx_unassociated_list);
1206 }
1207}
1208
1209static void efx_dissociate(struct efx_nic *efx)
1210{
1211 struct efx_nic *other, *next;
1212
1213 list_del(&efx->node);
1214 efx->primary = NULL;
1215
1216 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
1217 list_del(&other->node);
1218 netif_dbg(other, probe, other->net_dev,
1219 "moving to unassociated list\n");
1220 list_add_tail(&other->node, &efx_unassociated_list);
1221 other->primary = NULL;
1222 }
1223}
1224
Ben Hutchings8ceee662008-04-27 12:55:59 +01001225/* This configures the PCI device to enable I/O and DMA. */
1226static int efx_init_io(struct efx_nic *efx)
1227{
1228 struct pci_dev *pci_dev = efx->pci_dev;
1229 dma_addr_t dma_mask = efx->type->max_dma_mask;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001230 unsigned int mem_map_size = efx->type->mem_map_size(efx);
Shradha Shah02246a72015-05-06 00:58:14 +01001231 int rc, bar;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001232
Ben Hutchings62776d02010-06-23 11:30:07 +00001233 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001234
Shradha Shah02246a72015-05-06 00:58:14 +01001235 bar = efx->type->mem_bar;
1236
Ben Hutchings8ceee662008-04-27 12:55:59 +01001237 rc = pci_enable_device(pci_dev);
1238 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001239 netif_err(efx, probe, efx->net_dev,
1240 "failed to enable PCI device\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001241 goto fail1;
1242 }
1243
1244 pci_set_master(pci_dev);
1245
1246 /* Set the PCI DMA mask. Try all possibilities from our
1247 * genuine mask down to 32 bits, because some architectures
1248 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1249 * masks event though they reject 46 bit masks.
1250 */
1251 while (dma_mask > 0x7fffffffUL) {
Christoph Hellwig8722b8f2015-11-10 14:45:42 -08001252 rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
1253 if (rc == 0)
1254 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001255 dma_mask >>= 1;
1256 }
1257 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001258 netif_err(efx, probe, efx->net_dev,
1259 "could not find a suitable DMA mask\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001260 goto fail2;
1261 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001262 netif_dbg(efx, probe, efx->net_dev,
1263 "using DMA mask %llx\n", (unsigned long long) dma_mask);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001264
Shradha Shah02246a72015-05-06 00:58:14 +01001265 efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
1266 rc = pci_request_region(pci_dev, bar, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001267 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001268 netif_err(efx, probe, efx->net_dev,
1269 "request for memory BAR failed\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001270 rc = -EIO;
1271 goto fail3;
1272 }
Ben Hutchingsb1057982012-09-19 00:56:47 +01001273 efx->membase = ioremap_nocache(efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001274 if (!efx->membase) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001275 netif_err(efx, probe, efx->net_dev,
1276 "could not map memory BAR at %llx+%x\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001277 (unsigned long long)efx->membase_phys, mem_map_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001278 rc = -ENOMEM;
1279 goto fail4;
1280 }
Ben Hutchings62776d02010-06-23 11:30:07 +00001281 netif_dbg(efx, probe, efx->net_dev,
1282 "memory BAR at %llx+%x (virtual %p)\n",
Ben Hutchingsb1057982012-09-19 00:56:47 +01001283 (unsigned long long)efx->membase_phys, mem_map_size,
1284 efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001285
1286 return 0;
1287
1288 fail4:
Shradha Shah02246a72015-05-06 00:58:14 +01001289 pci_release_region(efx->pci_dev, bar);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001290 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +01001291 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001292 fail2:
1293 pci_disable_device(efx->pci_dev);
1294 fail1:
1295 return rc;
1296}
1297
1298static void efx_fini_io(struct efx_nic *efx)
1299{
Shradha Shah02246a72015-05-06 00:58:14 +01001300 int bar;
1301
Ben Hutchings62776d02010-06-23 11:30:07 +00001302 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001303
1304 if (efx->membase) {
1305 iounmap(efx->membase);
1306 efx->membase = NULL;
1307 }
1308
1309 if (efx->membase_phys) {
Shradha Shah02246a72015-05-06 00:58:14 +01001310 bar = efx->type->mem_bar;
1311 pci_release_region(efx->pci_dev, bar);
Ben Hutchings2c118e02008-05-16 21:15:29 +01001312 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001313 }
1314
Daniel Pieczko6598dad2015-06-02 11:41:00 +01001315 /* Don't disable bus-mastering if VFs are assigned */
1316 if (!pci_vfs_assigned(efx->pci_dev))
1317 pci_disable_device(efx->pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001318}
1319
Jon Cooper267c0152015-05-06 00:59:38 +01001320void efx_set_default_rx_indir_table(struct efx_nic *efx)
1321{
1322 size_t i;
1323
1324 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
1325 efx->rx_indir_table[i] =
1326 ethtool_rxfh_indir_default(i, efx->rss_spread);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001327}
1328
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001329static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
Ben Hutchings46123d02008-09-01 12:47:33 +01001330{
Ben Hutchingscdb08f82011-12-20 01:08:05 +00001331 cpumask_var_t thread_mask;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001332 unsigned int count;
Ben Hutchings46123d02008-09-01 12:47:33 +01001333 int cpu;
1334
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001335 if (rss_cpus) {
1336 count = rss_cpus;
1337 } else {
1338 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1339 netif_warn(efx, probe, efx->net_dev,
1340 "RSS disabled due to allocation failure\n");
1341 return 1;
Ben Hutchings46123d02008-09-01 12:47:33 +01001342 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001343
1344 count = 0;
1345 for_each_online_cpu(cpu) {
1346 if (!cpumask_test_cpu(cpu, thread_mask)) {
1347 ++count;
1348 cpumask_or(thread_mask, thread_mask,
Bartosz Golaszewski06931e62015-05-26 15:11:28 +02001349 topology_sibling_cpumask(cpu));
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001350 }
1351 }
1352
1353 free_cpumask_var(thread_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +01001354 }
1355
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001356 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1357 * table entries that are inaccessible to VFs
1358 */
Shradha Shah7fa8d542015-05-06 00:55:13 +01001359#ifdef CONFIG_SFC_SRIOV
1360 if (efx->type->sriov_wanted) {
1361 if (efx->type->sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1362 count > efx_vf_size(efx)) {
1363 netif_warn(efx, probe, efx->net_dev,
1364 "Reducing number of RSS channels from %u to %u for "
1365 "VF support. Increase vf-msix-limit to use more "
1366 "channels on the PF.\n",
1367 count, efx_vf_size(efx));
1368 count = efx_vf_size(efx);
1369 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001370 }
Shradha Shah7fa8d542015-05-06 00:55:13 +01001371#endif
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001372
Ben Hutchings46123d02008-09-01 12:47:33 +01001373 return count;
1374}
1375
1376/* Probe the number and type of interrupts we are able to obtain, and
1377 * the resulting numbers of channels and RX queues.
1378 */
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001379static int efx_probe_interrupts(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001380{
Ben Hutchings7f967c02012-02-13 23:45:02 +00001381 unsigned int extra_channels = 0;
1382 unsigned int i, j;
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001383 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001384
Ben Hutchings7f967c02012-02-13 23:45:02 +00001385 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1386 if (efx->extra_channel_type[i])
1387 ++extra_channels;
1388
Ben Hutchings8ceee662008-04-27 12:55:59 +01001389 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +01001390 struct msix_entry xentries[EFX_MAX_CHANNELS];
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001391 unsigned int n_channels;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001392
Ben Hutchingsa9a525062012-02-14 20:15:57 +00001393 n_channels = efx_wanted_parallelism(efx);
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001394 if (efx_separate_tx_channels)
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001395 n_channels *= 2;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001396 n_channels += extra_channels;
Ben Hutchingsb1057982012-09-19 00:56:47 +01001397 n_channels = min(n_channels, efx->max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +01001398
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001399 for (i = 0; i < n_channels; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001400 xentries[i].entry = i;
Alexander Gordeev184603d2014-02-18 11:12:00 +01001401 rc = pci_enable_msix_range(efx->pci_dev,
1402 xentries, 1, n_channels);
1403 if (rc < 0) {
1404 /* Fall back to single channel MSI */
1405 efx->interrupt_mode = EFX_INT_MODE_MSI;
1406 netif_err(efx, drv, efx->net_dev,
1407 "could not enable MSI-X\n");
1408 } else if (rc < n_channels) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001409 netif_err(efx, drv, efx->net_dev,
1410 "WARNING: Insufficient MSI-X vectors"
Ben Hutchingsa16e5b22012-02-14 00:40:12 +00001411 " available (%d < %u).\n", rc, n_channels);
Ben Hutchings62776d02010-06-23 11:30:07 +00001412 netif_err(efx, drv, efx->net_dev,
1413 "WARNING: Performance may be reduced.\n");
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001414 n_channels = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001415 }
1416
Alexander Gordeev184603d2014-02-18 11:12:00 +01001417 if (rc > 0) {
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001418 efx->n_channels = n_channels;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001419 if (n_channels > extra_channels)
1420 n_channels -= extra_channels;
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001421 if (efx_separate_tx_channels) {
1422 efx->n_tx_channels = min(max(n_channels / 2,
1423 1U),
1424 efx->max_tx_channels);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001425 efx->n_rx_channels = max(n_channels -
1426 efx->n_tx_channels,
1427 1U);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001428 } else {
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001429 efx->n_tx_channels = min(n_channels,
1430 efx->max_tx_channels);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001431 efx->n_rx_channels = n_channels;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001432 }
Ben Hutchings7f967c02012-02-13 23:45:02 +00001433 for (i = 0; i < efx->n_channels; i++)
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001434 efx_get_channel(efx, i)->irq =
1435 xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001436 }
1437 }
1438
1439 /* Try single interrupt MSI */
1440 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Neil Turton28b581a2008-12-12 21:41:06 -08001441 efx->n_channels = 1;
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001442 efx->n_rx_channels = 1;
1443 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001444 rc = pci_enable_msi(efx->pci_dev);
1445 if (rc == 0) {
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001446 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001447 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00001448 netif_err(efx, drv, efx->net_dev,
1449 "could not enable MSI\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001450 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1451 }
1452 }
1453
1454 /* Assume legacy interrupts */
1455 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001456 efx->n_channels = 1 + (efx_separate_tx_channels ? 1 : 0);
Ben Hutchingsa4900ac2010-04-28 09:30:43 +00001457 efx->n_rx_channels = 1;
1458 efx->n_tx_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001459 efx->legacy_irq = efx->pci_dev->irq;
1460 }
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001461
Ben Hutchings7f967c02012-02-13 23:45:02 +00001462 /* Assign extra channels if possible */
1463 j = efx->n_channels;
1464 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1465 if (!efx->extra_channel_type[i])
1466 continue;
1467 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1468 efx->n_channels <= extra_channels) {
1469 efx->extra_channel_type[i]->handle_no_channel(efx);
1470 } else {
1471 --j;
1472 efx_get_channel(efx, j)->type =
1473 efx->extra_channel_type[i];
1474 }
1475 }
1476
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001477 /* RSS might be usable on VFs even if it is disabled on the PF */
Shradha Shah7fa8d542015-05-06 00:55:13 +01001478#ifdef CONFIG_SFC_SRIOV
1479 if (efx->type->sriov_wanted) {
1480 efx->rss_spread = ((efx->n_rx_channels > 1 ||
1481 !efx->type->sriov_wanted(efx)) ?
1482 efx->n_rx_channels : efx_vf_size(efx));
1483 return 0;
1484 }
1485#endif
1486 efx->rss_spread = efx->n_rx_channels;
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00001487
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001488 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001489}
1490
Jon Cooper261e4d92013-04-15 18:51:54 +01001491static int efx_soft_enable_interrupts(struct efx_nic *efx)
Ben Hutchingsd8291182012-10-05 23:35:41 +01001492{
Jon Cooper261e4d92013-04-15 18:51:54 +01001493 struct efx_channel *channel, *end_channel;
1494 int rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001495
1496 BUG_ON(efx->state == STATE_DISABLED);
1497
1498 efx->irq_soft_enabled = true;
1499 smp_wmb();
1500
1501 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001502 if (!channel->type->keep_eventq) {
1503 rc = efx_init_eventq(channel);
1504 if (rc)
1505 goto fail;
1506 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001507 efx_start_eventq(channel);
1508 }
1509
1510 efx_mcdi_mode_event(efx);
Jon Cooper261e4d92013-04-15 18:51:54 +01001511
1512 return 0;
1513fail:
1514 end_channel = channel;
1515 efx_for_each_channel(channel, efx) {
1516 if (channel == end_channel)
1517 break;
1518 efx_stop_eventq(channel);
1519 if (!channel->type->keep_eventq)
1520 efx_fini_eventq(channel);
1521 }
1522
1523 return rc;
Ben Hutchingsd8291182012-10-05 23:35:41 +01001524}
1525
1526static void efx_soft_disable_interrupts(struct efx_nic *efx)
1527{
1528 struct efx_channel *channel;
1529
1530 if (efx->state == STATE_DISABLED)
1531 return;
1532
1533 efx_mcdi_mode_poll(efx);
1534
1535 efx->irq_soft_enabled = false;
1536 smp_wmb();
1537
1538 if (efx->legacy_irq)
1539 synchronize_irq(efx->legacy_irq);
1540
1541 efx_for_each_channel(channel, efx) {
1542 if (channel->irq)
1543 synchronize_irq(channel->irq);
1544
1545 efx_stop_eventq(channel);
1546 if (!channel->type->keep_eventq)
1547 efx_fini_eventq(channel);
1548 }
Ben Hutchingscade7152013-08-27 23:12:31 +01001549
1550 /* Flush the asynchronous MCDI request queue */
1551 efx_mcdi_flush_async(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01001552}
1553
Jon Cooper261e4d92013-04-15 18:51:54 +01001554static int efx_enable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001555{
Jon Cooper261e4d92013-04-15 18:51:54 +01001556 struct efx_channel *channel, *end_channel;
1557 int rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001558
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001559 BUG_ON(efx->state == STATE_DISABLED);
1560
Alexandre Ramesb28405b2013-03-21 16:41:43 +00001561 if (efx->eeh_disabled_legacy_irq) {
1562 enable_irq(efx->legacy_irq);
1563 efx->eeh_disabled_legacy_irq = false;
1564 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001565
Ben Hutchings86094f72013-08-21 19:51:04 +01001566 efx->type->irq_enable_master(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001567
1568 efx_for_each_channel(channel, efx) {
Jon Cooper261e4d92013-04-15 18:51:54 +01001569 if (channel->type->keep_eventq) {
1570 rc = efx_init_eventq(channel);
1571 if (rc)
1572 goto fail;
1573 }
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001574 }
1575
Jon Cooper261e4d92013-04-15 18:51:54 +01001576 rc = efx_soft_enable_interrupts(efx);
1577 if (rc)
1578 goto fail;
1579
1580 return 0;
1581
1582fail:
1583 end_channel = channel;
1584 efx_for_each_channel(channel, efx) {
1585 if (channel == end_channel)
1586 break;
1587 if (channel->type->keep_eventq)
1588 efx_fini_eventq(channel);
1589 }
1590
1591 efx->type->irq_disable_non_ev(efx);
1592
1593 return rc;
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001594}
1595
Ben Hutchingsd8291182012-10-05 23:35:41 +01001596static void efx_disable_interrupts(struct efx_nic *efx)
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001597{
1598 struct efx_channel *channel;
1599
Ben Hutchingsd8291182012-10-05 23:35:41 +01001600 efx_soft_disable_interrupts(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001601
1602 efx_for_each_channel(channel, efx) {
Ben Hutchingsd8291182012-10-05 23:35:41 +01001603 if (channel->type->keep_eventq)
Ben Hutchings7f967c02012-02-13 23:45:02 +00001604 efx_fini_eventq(channel);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001605 }
Ben Hutchingsd8291182012-10-05 23:35:41 +01001606
Ben Hutchings86094f72013-08-21 19:51:04 +01001607 efx->type->irq_disable_non_ev(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001608}
1609
Ben Hutchings8ceee662008-04-27 12:55:59 +01001610static void efx_remove_interrupts(struct efx_nic *efx)
1611{
1612 struct efx_channel *channel;
1613
1614 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001615 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001616 channel->irq = 0;
1617 pci_disable_msi(efx->pci_dev);
1618 pci_disable_msix(efx->pci_dev);
1619
1620 /* Remove legacy interrupt */
1621 efx->legacy_irq = 0;
1622}
1623
Ben Hutchings8831da72008-09-01 12:47:48 +01001624static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001625{
Ben Hutchings602a5322011-05-16 17:32:39 +01001626 struct efx_channel *channel;
1627 struct efx_tx_queue *tx_queue;
1628
Ben Hutchings97653432011-01-12 18:26:56 +00001629 efx->tx_channel_offset =
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001630 efx_separate_tx_channels ?
1631 efx->n_channels - efx->n_tx_channels : 0;
Ben Hutchings602a5322011-05-16 17:32:39 +01001632
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001633 /* We need to mark which channels really have RX and TX
1634 * queues, and adjust the TX queue numbers if we have separate
Ben Hutchings602a5322011-05-16 17:32:39 +01001635 * RX-only and TX-only channels.
1636 */
1637 efx_for_each_channel(channel, efx) {
Stuart Hodgson79d68b32012-07-16 17:08:33 +01001638 if (channel->channel < efx->n_rx_channels)
1639 channel->rx_queue.core_index = channel->channel;
1640 else
1641 channel->rx_queue.core_index = -1;
1642
Ben Hutchings602a5322011-05-16 17:32:39 +01001643 efx_for_each_channel_tx_queue(tx_queue, channel)
1644 tx_queue->queue -= (efx->tx_channel_offset *
1645 EFX_TXQ_TYPES);
1646 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001647}
1648
1649static int efx_probe_nic(struct efx_nic *efx)
1650{
1651 int rc;
1652
Ben Hutchings62776d02010-06-23 11:30:07 +00001653 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001654
1655 /* Carry out hardware-type specific initialisation */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001656 rc = efx->type->probe(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001657 if (rc)
1658 return rc;
1659
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001660 do {
1661 if (!efx->max_channels || !efx->max_tx_channels) {
1662 netif_err(efx, drv, efx->net_dev,
1663 "Insufficient resources to allocate"
1664 " any channels\n");
1665 rc = -ENOSPC;
1666 goto fail1;
1667 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001668
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001669 /* Determine the number of channels and queues by trying
1670 * to hook in MSI-X interrupts.
1671 */
1672 rc = efx_probe_interrupts(efx);
1673 if (rc)
1674 goto fail1;
Daniel Pieczko52ad7622014-04-01 13:10:34 +01001675
Shradha Shahb0fbdae2015-08-28 10:55:42 +01001676 efx_set_channels(efx);
1677
1678 /* dimension_resources can fail with EAGAIN */
1679 rc = efx->type->dimension_resources(efx);
1680 if (rc != 0 && rc != -EAGAIN)
1681 goto fail2;
1682
1683 if (rc == -EAGAIN)
1684 /* try again with new max_channels */
1685 efx_remove_interrupts(efx);
1686
1687 } while (rc == -EAGAIN);
Ben Hutchings28e47c42012-02-15 01:58:49 +00001688
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001689 if (efx->n_channels > 1)
Jon Cooper267c0152015-05-06 00:59:38 +01001690 netdev_rss_key_fill(&efx->rx_hash_key,
1691 sizeof(efx->rx_hash_key));
1692 efx_set_default_rx_indir_table(efx);
Ben Hutchings5d3a6fc2010-06-25 07:05:43 +00001693
Ben Hutchingsc4f4adc2010-09-27 08:31:07 +00001694 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1695 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001696
1697 /* Initialise the interrupt moderation settings */
Bert Kenward539de7c2016-08-11 13:02:09 +01001698 efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
Ben Hutchings9e393b32011-09-05 07:43:04 +00001699 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1700 true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001701
1702 return 0;
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001703
Ben Hutchingsc15eed22013-08-29 00:45:48 +01001704fail2:
1705 efx_remove_interrupts(efx);
1706fail1:
Ben Hutchings64d8ad62011-01-05 00:50:41 +00001707 efx->type->remove(efx);
1708 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001709}
1710
1711static void efx_remove_nic(struct efx_nic *efx)
1712{
Ben Hutchings62776d02010-06-23 11:30:07 +00001713 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001714
1715 efx_remove_interrupts(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001716 efx->type->remove(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001717}
1718
Ben Hutchingsadd72472012-11-08 01:46:53 +00001719static int efx_probe_filters(struct efx_nic *efx)
1720{
1721 int rc;
1722
1723 spin_lock_init(&efx->filter_lock);
Edward Cree0d322412015-05-20 11:10:03 +01001724 init_rwsem(&efx->filter_sem);
Martin Habetsd2489532016-06-15 17:48:49 +01001725 mutex_lock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01001726 down_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001727 rc = efx->type->filter_table_probe(efx);
1728 if (rc)
Edward Cree0d322412015-05-20 11:10:03 +01001729 goto out_unlock;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001730
1731#ifdef CONFIG_RFS_ACCEL
1732 if (efx->type->offload_features & NETIF_F_NTUPLE) {
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001733 struct efx_channel *channel;
1734 int i, success = 1;
1735
1736 efx_for_each_channel(channel, efx) {
1737 channel->rps_flow_id =
1738 kcalloc(efx->type->max_rx_ip_filters,
1739 sizeof(*channel->rps_flow_id),
1740 GFP_KERNEL);
1741 if (!channel->rps_flow_id)
1742 success = 0;
1743 else
1744 for (i = 0;
1745 i < efx->type->max_rx_ip_filters;
1746 ++i)
1747 channel->rps_flow_id[i] =
1748 RPS_FLOW_ID_INVALID;
1749 }
1750
1751 if (!success) {
1752 efx_for_each_channel(channel, efx)
1753 kfree(channel->rps_flow_id);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001754 efx->type->filter_table_remove(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001755 rc = -ENOMEM;
1756 goto out_unlock;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001757 }
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001758
1759 efx->rps_expire_index = efx->rps_expire_channel = 0;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001760 }
1761#endif
Edward Cree0d322412015-05-20 11:10:03 +01001762out_unlock:
1763 up_write(&efx->filter_sem);
Martin Habetsd2489532016-06-15 17:48:49 +01001764 mutex_unlock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01001765 return rc;
Ben Hutchingsadd72472012-11-08 01:46:53 +00001766}
1767
1768static void efx_remove_filters(struct efx_nic *efx)
1769{
1770#ifdef CONFIG_RFS_ACCEL
Jon Cooperfaf8dcc2016-05-31 19:12:32 +01001771 struct efx_channel *channel;
1772
1773 efx_for_each_channel(channel, efx)
1774 kfree(channel->rps_flow_id);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001775#endif
Edward Cree0d322412015-05-20 11:10:03 +01001776 down_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001777 efx->type->filter_table_remove(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001778 up_write(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001779}
1780
1781static void efx_restore_filters(struct efx_nic *efx)
1782{
Edward Cree0d322412015-05-20 11:10:03 +01001783 down_read(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001784 efx->type->filter_table_restore(efx);
Edward Cree0d322412015-05-20 11:10:03 +01001785 up_read(&efx->filter_sem);
Ben Hutchingsadd72472012-11-08 01:46:53 +00001786}
1787
Ben Hutchings8ceee662008-04-27 12:55:59 +01001788/**************************************************************************
1789 *
1790 * NIC startup/shutdown
1791 *
1792 *************************************************************************/
1793
1794static int efx_probe_all(struct efx_nic *efx)
1795{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001796 int rc;
1797
Ben Hutchings8ceee662008-04-27 12:55:59 +01001798 rc = efx_probe_nic(efx);
1799 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001800 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001801 goto fail1;
1802 }
1803
Ben Hutchings8ceee662008-04-27 12:55:59 +01001804 rc = efx_probe_port(efx);
1805 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00001806 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001807 goto fail2;
1808 }
1809
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00001810 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1811 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1812 rc = -EINVAL;
1813 goto fail3;
1814 }
Steve Hodgsonecc910f2010-09-10 06:42:22 +00001815 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001816
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001817#ifdef CONFIG_SFC_SRIOV
1818 rc = efx->type->vswitching_probe(efx);
1819 if (rc) /* not fatal; the PF will still work fine */
1820 netif_warn(efx, probe, efx->net_dev,
1821 "failed to setup vswitching rc=%d;"
1822 " VFs may not function\n", rc);
1823#endif
1824
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001825 rc = efx_probe_filters(efx);
1826 if (rc) {
1827 netif_err(efx, probe, efx->net_dev,
1828 "failed to create filter tables\n");
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001829 goto fail4;
Ben Hutchings64eebcf2010-09-20 08:43:07 +00001830 }
1831
Ben Hutchings7f967c02012-02-13 23:45:02 +00001832 rc = efx_probe_channels(efx);
1833 if (rc)
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001834 goto fail5;
Ben Hutchings7f967c02012-02-13 23:45:02 +00001835
Ben Hutchings8ceee662008-04-27 12:55:59 +01001836 return 0;
1837
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001838 fail5:
Ben Hutchings7f967c02012-02-13 23:45:02 +00001839 efx_remove_filters(efx);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001840 fail4:
1841#ifdef CONFIG_SFC_SRIOV
1842 efx->type->vswitching_remove(efx);
1843#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01001844 fail3:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001845 efx_remove_port(efx);
1846 fail2:
1847 efx_remove_nic(efx);
1848 fail1:
1849 return rc;
1850}
1851
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001852/* If the interface is supposed to be running but is not, start
1853 * the hardware and software data path, regular activity for the port
1854 * (MAC statistics, link polling, etc.) and schedule the port to be
1855 * reconfigured. Interrupts must already be enabled. This function
1856 * is safe to call multiple times, so long as the NIC is not disabled.
1857 * Requires the RTNL lock.
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001858 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001859static void efx_start_all(struct efx_nic *efx)
1860{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001861 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001862 BUG_ON(efx->state == STATE_DISABLED);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001863
1864 /* Check that it is appropriate to restart the interface. All
1865 * of these flags are safe to read under just the rtnl lock */
Edward Creee2835462014-04-16 19:27:48 +01001866 if (efx->port_enabled || !netif_running(efx->net_dev) ||
1867 efx->reset_pending)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001868 return;
1869
Ben Hutchings8ceee662008-04-27 12:55:59 +01001870 efx_start_port(efx);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001871 efx_start_datapath(efx);
Ben Hutchings8880f4e2009-11-29 15:15:41 +00001872
Alexandre Rames626950d2013-01-14 17:20:22 +00001873 /* Start the hardware monitor if there is one */
1874 if (efx->type->monitor != NULL)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001875 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1876 efx_monitor_interval);
Alexandre Rames626950d2013-01-14 17:20:22 +00001877
Edward Cree5a6681e2016-11-28 18:55:34 +00001878 /* Link state detection is normally event-driven; we have
Alexandre Rames626950d2013-01-14 17:20:22 +00001879 * to poll now because we could have missed a change
1880 */
Edward Cree5a6681e2016-11-28 18:55:34 +00001881 mutex_lock(&efx->mac_lock);
1882 if (efx->phy_op->poll(efx))
1883 efx_link_status_changed(efx);
1884 mutex_unlock(&efx->mac_lock);
Ben Hutchings55edc6e2009-11-25 16:11:35 +00001885
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001886 efx->type->start_stats(efx);
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001887 efx->type->pull_stats(efx);
1888 spin_lock_bh(&efx->stats_lock);
1889 efx->type->update_stats(efx, NULL, NULL);
1890 spin_unlock_bh(&efx->stats_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001891}
1892
Ben Hutchings8b7325b2012-07-27 20:46:41 +01001893/* Quiesce the hardware and software data path, and regular activity
1894 * for the port without bringing the link down. Safe to call multiple
1895 * times with the NIC in almost any state, but interrupts should be
1896 * enabled. Requires the RTNL lock.
1897 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001898static void efx_stop_all(struct efx_nic *efx)
1899{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001900 EFX_ASSERT_RESET_SERIALISED(efx);
1901
1902 /* port_enabled can be read safely under the rtnl lock */
1903 if (!efx->port_enabled)
1904 return;
1905
Jon Cooperf8f3b5a2013-09-30 17:36:50 +01001906 /* update stats before we go down so we can accurately count
1907 * rx_nodesc_drops
1908 */
1909 efx->type->pull_stats(efx);
1910 spin_lock_bh(&efx->stats_lock);
1911 efx->type->update_stats(efx, NULL, NULL);
1912 spin_unlock_bh(&efx->stats_lock);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00001913 efx->type->stop_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001914 efx_stop_port(efx);
1915
Ben Hutchings29c69a42013-01-28 19:01:06 +00001916 /* Stop the kernel transmit interface. This is only valid if
1917 * the device is stopped or detached; otherwise the watchdog
1918 * may fire immediately.
1919 */
1920 WARN_ON(netif_running(efx->net_dev) &&
1921 netif_device_present(efx->net_dev));
Ben Hutchings9f2cb712012-02-08 00:11:20 +00001922 netif_tx_disable(efx->net_dev);
1923
1924 efx_stop_datapath(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001925}
1926
1927static void efx_remove_all(struct efx_nic *efx)
1928{
Ben Hutchings46426102010-09-10 06:42:33 +00001929 efx_remove_channels(efx);
Ben Hutchings7f967c02012-02-13 23:45:02 +00001930 efx_remove_filters(efx);
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01001931#ifdef CONFIG_SFC_SRIOV
1932 efx->type->vswitching_remove(efx);
1933#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01001934 efx_remove_port(efx);
1935 efx_remove_nic(efx);
1936}
1937
Ben Hutchings8ceee662008-04-27 12:55:59 +01001938/**************************************************************************
1939 *
1940 * Interrupt moderation
1941 *
1942 **************************************************************************/
Bert Kenward539de7c2016-08-11 13:02:09 +01001943unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001944{
Ben Hutchingsb548f972011-09-05 07:41:44 +00001945 if (usecs == 0)
1946 return 0;
Bert Kenward539de7c2016-08-11 13:02:09 +01001947 if (usecs * 1000 < efx->timer_quantum_ns)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001948 return 1; /* never round down to 0 */
Bert Kenward539de7c2016-08-11 13:02:09 +01001949 return usecs * 1000 / efx->timer_quantum_ns;
1950}
1951
1952unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
1953{
1954 /* We must round up when converting ticks to microseconds
1955 * because we round down when converting the other way.
1956 */
1957 return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001958}
1959
Ben Hutchings8ceee662008-04-27 12:55:59 +01001960/* Set interrupt moderation parameters */
Ben Hutchings9e393b32011-09-05 07:43:04 +00001961int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1962 unsigned int rx_usecs, bool rx_adaptive,
1963 bool rx_may_override_tx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001964{
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001965 struct efx_channel *channel;
Bert Kenwardd95e3292016-08-11 13:02:36 +01001966 unsigned int timer_max_us;
1967
Ben Hutchings8ceee662008-04-27 12:55:59 +01001968 EFX_ASSERT_RESET_SERIALISED(efx);
1969
Bert Kenwardd95e3292016-08-11 13:02:36 +01001970 timer_max_us = efx->timer_max_ns / 1000;
1971
1972 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
Ben Hutchings9e393b32011-09-05 07:43:04 +00001973 return -EINVAL;
1974
Bert Kenward539de7c2016-08-11 13:02:09 +01001975 if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
Ben Hutchings9e393b32011-09-05 07:43:04 +00001976 !rx_may_override_tx) {
1977 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1978 "RX and TX IRQ moderation must be equal\n");
1979 return -EINVAL;
1980 }
1981
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001982 efx->irq_rx_adaptive = rx_adaptive;
Bert Kenward539de7c2016-08-11 13:02:09 +01001983 efx->irq_rx_moderation_us = rx_usecs;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001984 efx_for_each_channel(channel, efx) {
Ben Hutchings525da902011-02-07 23:04:38 +00001985 if (efx_channel_has_rx_queue(channel))
Bert Kenward539de7c2016-08-11 13:02:09 +01001986 channel->irq_moderation_us = rx_usecs;
Ben Hutchings525da902011-02-07 23:04:38 +00001987 else if (efx_channel_has_tx_queues(channel))
Bert Kenward539de7c2016-08-11 13:02:09 +01001988 channel->irq_moderation_us = tx_usecs;
Ben Hutchingsf7d12cd2010-09-10 06:41:47 +00001989 }
Ben Hutchings9e393b32011-09-05 07:43:04 +00001990
1991 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001992}
1993
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001994void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1995 unsigned int *rx_usecs, bool *rx_adaptive)
1996{
1997 *rx_adaptive = efx->irq_rx_adaptive;
Bert Kenward539de7c2016-08-11 13:02:09 +01001998 *rx_usecs = efx->irq_rx_moderation_us;
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00001999
2000 /* If channels are shared between RX and TX, so is IRQ
2001 * moderation. Otherwise, IRQ moderation is the same for all
2002 * TX channels and is not adaptive.
2003 */
Bert Kenward539de7c2016-08-11 13:02:09 +01002004 if (efx->tx_channel_offset == 0) {
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002005 *tx_usecs = *rx_usecs;
Bert Kenward539de7c2016-08-11 13:02:09 +01002006 } else {
2007 struct efx_channel *tx_channel;
2008
2009 tx_channel = efx->channel[efx->tx_channel_offset];
2010 *tx_usecs = tx_channel->irq_moderation_us;
2011 }
Ben Hutchingsa0c4faf2011-09-05 07:42:25 +00002012}
2013
Ben Hutchings8ceee662008-04-27 12:55:59 +01002014/**************************************************************************
2015 *
2016 * Hardware monitor
2017 *
2018 **************************************************************************/
2019
Ben Hutchingse254c272010-09-20 08:44:10 +00002020/* Run periodically off the general workqueue */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002021static void efx_monitor(struct work_struct *data)
2022{
2023 struct efx_nic *efx = container_of(data, struct efx_nic,
2024 monitor_work.work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002025
Ben Hutchings62776d02010-06-23 11:30:07 +00002026 netif_vdbg(efx, timer, efx->net_dev,
2027 "hardware monitor executing on CPU %d\n",
2028 raw_smp_processor_id());
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002029 BUG_ON(efx->type->monitor == NULL);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002030
Ben Hutchings8ceee662008-04-27 12:55:59 +01002031 /* If the mac_lock is already held then it is likely a port
2032 * reconfiguration is already in place, which will likely do
Ben Hutchingse254c272010-09-20 08:44:10 +00002033 * most of the work of monitor() anyway. */
2034 if (mutex_trylock(&efx->mac_lock)) {
2035 if (efx->port_enabled)
2036 efx->type->monitor(efx);
2037 mutex_unlock(&efx->mac_lock);
2038 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002039
Ben Hutchings8ceee662008-04-27 12:55:59 +01002040 queue_delayed_work(efx->workqueue, &efx->monitor_work,
2041 efx_monitor_interval);
2042}
2043
2044/**************************************************************************
2045 *
2046 * ioctls
2047 *
2048 *************************************************************************/
2049
2050/* Net device ioctl
2051 * Context: process, rtnl_lock() held.
2052 */
2053static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
2054{
Ben Hutchings767e4682008-09-01 12:43:14 +01002055 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00002056 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002057
Stuart Hodgson7c236c42012-09-03 11:09:36 +01002058 if (cmd == SIOCSHWTSTAMP)
Ben Hutchings433dc9b2013-11-14 01:26:21 +00002059 return efx_ptp_set_ts_config(efx, ifr);
2060 if (cmd == SIOCGHWTSTAMP)
2061 return efx_ptp_get_ts_config(efx, ifr);
Stuart Hodgson7c236c42012-09-03 11:09:36 +01002062
Ben Hutchings68e7f452009-04-29 08:05:08 +00002063 /* Convert phy_id from older PRTAD/DEVAD format */
2064 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
2065 (data->phy_id & 0xfc00) == 0x0400)
2066 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
2067
2068 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002069}
2070
2071/**************************************************************************
2072 *
2073 * NAPI interface
2074 *
2075 **************************************************************************/
2076
Ben Hutchings7f967c02012-02-13 23:45:02 +00002077static void efx_init_napi_channel(struct efx_channel *channel)
2078{
2079 struct efx_nic *efx = channel->efx;
2080
2081 channel->napi_dev = efx->net_dev;
2082 netif_napi_add(channel->napi_dev, &channel->napi_str,
2083 efx_poll, napi_weight);
2084}
2085
Ben Hutchingse8f14992010-12-07 19:47:34 +00002086static void efx_init_napi(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002087{
2088 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002089
Ben Hutchings7f967c02012-02-13 23:45:02 +00002090 efx_for_each_channel(channel, efx)
2091 efx_init_napi_channel(channel);
Ben Hutchingse8f14992010-12-07 19:47:34 +00002092}
2093
2094static void efx_fini_napi_channel(struct efx_channel *channel)
2095{
Eric Dumazet973334a2016-11-16 06:01:47 -08002096 if (channel->napi_dev)
Ben Hutchingse8f14992010-12-07 19:47:34 +00002097 netif_napi_del(&channel->napi_str);
Eric Dumazet973334a2016-11-16 06:01:47 -08002098
Ben Hutchingse8f14992010-12-07 19:47:34 +00002099 channel->napi_dev = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002100}
2101
2102static void efx_fini_napi(struct efx_nic *efx)
2103{
2104 struct efx_channel *channel;
2105
Ben Hutchingse8f14992010-12-07 19:47:34 +00002106 efx_for_each_channel(channel, efx)
2107 efx_fini_napi_channel(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002108}
2109
2110/**************************************************************************
2111 *
2112 * Kernel netpoll interface
2113 *
2114 *************************************************************************/
2115
2116#ifdef CONFIG_NET_POLL_CONTROLLER
2117
2118/* Although in the common case interrupts will be disabled, this is not
2119 * guaranteed. However, all our work happens inside the NAPI callback,
2120 * so no locking is required.
2121 */
2122static void efx_netpoll(struct net_device *net_dev)
2123{
Ben Hutchings767e4682008-09-01 12:43:14 +01002124 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002125 struct efx_channel *channel;
2126
Ben Hutchings64ee3122008-09-01 12:47:38 +01002127 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002128 efx_schedule_channel(channel);
2129}
2130
2131#endif
2132
2133/**************************************************************************
2134 *
2135 * Kernel net device interface
2136 *
2137 *************************************************************************/
2138
2139/* Context: process, rtnl_lock() held. */
Shradha Shahe340be92015-05-20 11:11:03 +01002140int efx_net_open(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002141{
Ben Hutchings767e4682008-09-01 12:43:14 +01002142 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002143 int rc;
2144
Ben Hutchings62776d02010-06-23 11:30:07 +00002145 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
2146 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01002147
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002148 rc = efx_check_disabled(efx);
2149 if (rc)
2150 return rc;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01002151 if (efx->phy_mode & PHY_MODE_SPECIAL)
2152 return -EBUSY;
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002153 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
2154 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01002155
Steve Hodgson78c1f0a2009-11-29 03:43:00 +00002156 /* Notify the kernel of the link state polled during driver load,
2157 * before the monitor starts running */
2158 efx_link_status_changed(efx);
2159
Ben Hutchings8ceee662008-04-27 12:55:59 +01002160 efx_start_all(efx);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002161 efx_selftest_async_start(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002162 return 0;
2163}
2164
2165/* Context: process, rtnl_lock() held.
2166 * Note that the kernel will ignore our return code; this method
2167 * should really be a void.
2168 */
Shradha Shahe340be92015-05-20 11:11:03 +01002169int efx_net_stop(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002170{
Ben Hutchings767e4682008-09-01 12:43:14 +01002171 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002172
Ben Hutchings62776d02010-06-23 11:30:07 +00002173 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
2174 raw_smp_processor_id());
Ben Hutchings8ceee662008-04-27 12:55:59 +01002175
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002176 /* Stop the device and flush all the channels */
2177 efx_stop_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002178
2179 return 0;
2180}
2181
Ben Hutchings5b9e2072008-05-16 21:18:14 +01002182/* Context: process, dev_base_lock or RTNL held, non-blocking. */
stephen hemmingerbc1f4472017-01-06 19:12:52 -08002183static void efx_net_stats(struct net_device *net_dev,
2184 struct rtnl_link_stats64 *stats)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002185{
Ben Hutchings767e4682008-09-01 12:43:14 +01002186 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002187
Ben Hutchings55edc6e2009-11-25 16:11:35 +00002188 spin_lock_bh(&efx->stats_lock);
Ben Hutchingscd0ecc92012-12-14 21:52:56 +00002189 efx->type->update_stats(efx, NULL, stats);
Ben Hutchings1cb34522011-09-02 23:23:00 +01002190 spin_unlock_bh(&efx->stats_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002191}
2192
2193/* Context: netif_tx_lock held, BHs disabled. */
2194static void efx_watchdog(struct net_device *net_dev)
2195{
Ben Hutchings767e4682008-09-01 12:43:14 +01002196 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002197
Ben Hutchings62776d02010-06-23 11:30:07 +00002198 netif_err(efx, tx_err, efx->net_dev,
2199 "TX stuck with port_enabled=%d: resetting channels\n",
2200 efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002201
Ben Hutchings739bb23d2008-11-04 20:35:36 +00002202 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002203}
2204
2205
2206/* Context: process, rtnl_lock() held. */
2207static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
2208{
Ben Hutchings767e4682008-09-01 12:43:14 +01002209 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002210 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002211
Ben Hutchings8b7325b2012-07-27 20:46:41 +01002212 rc = efx_check_disabled(efx);
2213 if (rc)
2214 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002215
Ben Hutchings62776d02010-06-23 11:30:07 +00002216 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002217
Ben Hutchings29c69a42013-01-28 19:01:06 +00002218 efx_device_detach_sync(efx);
2219 efx_stop_all(efx);
2220
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002221 mutex_lock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002222 net_dev->mtu = new_mtu;
Edward Cree0d322412015-05-20 11:10:03 +01002223 efx_mac_reconfigure(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002224 mutex_unlock(&efx->mac_lock);
2225
Ben Hutchings8ceee662008-04-27 12:55:59 +01002226 efx_start_all(efx);
Ben Hutchings29c69a42013-01-28 19:01:06 +00002227 netif_device_attach(efx->net_dev);
Ben Hutchings6c8eef42012-01-09 19:54:16 +00002228 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002229}
2230
2231static int efx_set_mac_address(struct net_device *net_dev, void *data)
2232{
Ben Hutchings767e4682008-09-01 12:43:14 +01002233 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002234 struct sockaddr *addr = data;
Ben Hutchingse0b3ae32014-02-12 18:59:54 +00002235 u8 *new_addr = addr->sa_data;
Shradha Shahcfc77c22015-05-20 11:09:30 +01002236 u8 old_addr[6];
2237 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002238
Ben Hutchings8ceee662008-04-27 12:55:59 +01002239 if (!is_valid_ether_addr(new_addr)) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002240 netif_err(efx, drv, efx->net_dev,
2241 "invalid ethernet MAC address requested: %pM\n",
2242 new_addr);
Danny Kukawka504f9b52012-02-21 02:07:49 +00002243 return -EADDRNOTAVAIL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002244 }
2245
Shradha Shahcfc77c22015-05-20 11:09:30 +01002246 /* save old address */
2247 ether_addr_copy(old_addr, net_dev->dev_addr);
Edward Creecd84ff42014-03-07 18:27:41 +00002248 ether_addr_copy(net_dev->dev_addr, new_addr);
Shradha Shah910c8782015-05-20 11:12:48 +01002249 if (efx->type->set_mac_address) {
2250 rc = efx->type->set_mac_address(efx);
Shradha Shahcfc77c22015-05-20 11:09:30 +01002251 if (rc) {
2252 ether_addr_copy(net_dev->dev_addr, old_addr);
2253 return rc;
2254 }
2255 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002256
2257 /* Reconfigure the MAC */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002258 mutex_lock(&efx->mac_lock);
Edward Cree0d322412015-05-20 11:10:03 +01002259 efx_mac_reconfigure(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002260 mutex_unlock(&efx->mac_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002261
2262 return 0;
2263}
2264
Ben Hutchingsa816f752008-09-01 12:49:12 +01002265/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002266static void efx_set_rx_mode(struct net_device *net_dev)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002267{
Ben Hutchings767e4682008-09-01 12:43:14 +01002268 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002269
Ben Hutchings8be4f3e2009-11-25 16:12:16 +00002270 if (efx->port_enabled)
2271 queue_work(efx->workqueue, &efx->mac_work);
2272 /* Otherwise efx_start_port() will do this */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002273}
2274
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002275static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002276{
2277 struct efx_nic *efx = netdev_priv(net_dev);
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002278 int rc;
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002279
2280 /* If disabling RX n-tuple filtering, clear existing filters */
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002281 if (net_dev->features & ~data & NETIF_F_NTUPLE) {
2282 rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2283 if (rc)
2284 return rc;
2285 }
2286
2287 /* If Rx VLAN filter is changed, update filters via mac_reconfigure */
2288 if ((net_dev->features ^ data) & NETIF_F_HW_VLAN_CTAG_FILTER) {
2289 /* efx_set_rx_mode() will schedule MAC work to update filters
2290 * when a new features are finally set in net_dev.
2291 */
2292 efx_set_rx_mode(net_dev);
2293 }
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002294
2295 return 0;
2296}
2297
Wei Yongjunb40296f2017-01-11 16:16:12 +00002298static int efx_get_phys_port_id(struct net_device *net_dev,
2299 struct netdev_phys_item_id *ppid)
Bert Kenward08a7b29b2017-01-10 16:23:33 +00002300{
2301 struct efx_nic *efx = netdev_priv(net_dev);
2302
2303 if (efx->type->get_phys_port_id)
2304 return efx->type->get_phys_port_id(efx, ppid);
2305 else
2306 return -EOPNOTSUPP;
2307}
2308
Bert Kenwardac019f02017-01-10 16:23:56 +00002309static int efx_get_phys_port_name(struct net_device *net_dev,
2310 char *name, size_t len)
2311{
2312 struct efx_nic *efx = netdev_priv(net_dev);
2313
2314 if (snprintf(name, len, "p%u", efx->port_num) >= len)
2315 return -EINVAL;
2316 return 0;
2317}
2318
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002319static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
2320{
2321 struct efx_nic *efx = netdev_priv(net_dev);
2322
2323 if (efx->type->vlan_rx_add_vid)
2324 return efx->type->vlan_rx_add_vid(efx, proto, vid);
2325 else
2326 return -EOPNOTSUPP;
2327}
2328
2329static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
2330{
2331 struct efx_nic *efx = netdev_priv(net_dev);
2332
2333 if (efx->type->vlan_rx_kill_vid)
2334 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
2335 else
2336 return -EOPNOTSUPP;
2337}
2338
Shradha Shah7fa8d542015-05-06 00:55:13 +01002339static const struct net_device_ops efx_netdev_ops = {
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002340 .ndo_open = efx_net_open,
2341 .ndo_stop = efx_net_stop,
Ben Hutchings44727022010-06-08 07:21:12 +00002342 .ndo_get_stats64 = efx_net_stats,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002343 .ndo_tx_timeout = efx_watchdog,
2344 .ndo_start_xmit = efx_hard_start_xmit,
2345 .ndo_validate_addr = eth_validate_addr,
2346 .ndo_do_ioctl = efx_ioctl,
2347 .ndo_change_mtu = efx_change_mtu,
2348 .ndo_set_mac_address = efx_set_mac_address,
Ben Hutchings0fca8c92012-01-09 19:54:44 +00002349 .ndo_set_rx_mode = efx_set_rx_mode,
Ben Hutchingsabfe9032011-04-05 15:00:02 +01002350 .ndo_set_features = efx_set_features,
Andrew Rybchenko4a53ea82016-06-15 17:48:32 +01002351 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
2352 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002353#ifdef CONFIG_SFC_SRIOV
Shradha Shah7fa8d542015-05-06 00:55:13 +01002354 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2355 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2356 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2357 .ndo_get_vf_config = efx_sriov_get_vf_config,
Edward Cree4392dc62015-05-20 11:12:13 +01002358 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00002359#endif
Bert Kenward08a7b29b2017-01-10 16:23:33 +00002360 .ndo_get_phys_port_id = efx_get_phys_port_id,
Bert Kenwardac019f02017-01-10 16:23:56 +00002361 .ndo_get_phys_port_name = efx_get_phys_port_name,
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002362#ifdef CONFIG_NET_POLL_CONTROLLER
2363 .ndo_poll_controller = efx_netpoll,
2364#endif
Ben Hutchings94b274b2011-01-10 21:18:20 +00002365 .ndo_setup_tc = efx_setup_tc,
Ben Hutchings64d8ad62011-01-05 00:50:41 +00002366#ifdef CONFIG_RFS_ACCEL
2367 .ndo_rx_flow_steer = efx_filter_rfs,
2368#endif
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08002369};
2370
Ben Hutchings7dde5962008-12-12 22:09:38 -08002371static void efx_update_name(struct efx_nic *efx)
2372{
2373 strcpy(efx->name, efx->net_dev->name);
2374 efx_mtd_rename(efx);
2375 efx_set_channel_names(efx);
2376}
2377
Ben Hutchings8ceee662008-04-27 12:55:59 +01002378static int efx_netdev_event(struct notifier_block *this,
2379 unsigned long event, void *ptr)
2380{
Jiri Pirko351638e2013-05-28 01:30:21 +00002381 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002382
Shradha Shah7fa8d542015-05-06 00:55:13 +01002383 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
Ben Hutchings7dde5962008-12-12 22:09:38 -08002384 event == NETDEV_CHANGENAME)
2385 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002386
2387 return NOTIFY_DONE;
2388}
2389
2390static struct notifier_block efx_netdev_notifier = {
2391 .notifier_call = efx_netdev_event,
2392};
2393
Ben Hutchings06d5e192008-12-12 21:47:23 -08002394static ssize_t
2395show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2396{
2397 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2398 return sprintf(buf, "%d\n", efx->phy_type);
2399}
Ben Hutchings776fbcc2013-06-18 17:45:40 +01002400static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
Ben Hutchings06d5e192008-12-12 21:47:23 -08002401
Edward Creee7fef9b2015-05-27 13:14:01 +01002402#ifdef CONFIG_SFC_MCDI_LOGGING
2403static ssize_t show_mcdi_log(struct device *dev, struct device_attribute *attr,
2404 char *buf)
2405{
2406 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2407 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2408
2409 return scnprintf(buf, PAGE_SIZE, "%d\n", mcdi->logging_enabled);
2410}
2411static ssize_t set_mcdi_log(struct device *dev, struct device_attribute *attr,
2412 const char *buf, size_t count)
2413{
2414 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2415 struct efx_mcdi_iface *mcdi = efx_mcdi(efx);
2416 bool enable = count > 0 && *buf != '0';
2417
2418 mcdi->logging_enabled = enable;
2419 return count;
2420}
2421static DEVICE_ATTR(mcdi_logging, 0644, show_mcdi_log, set_mcdi_log);
2422#endif
2423
Ben Hutchings8ceee662008-04-27 12:55:59 +01002424static int efx_register_netdev(struct efx_nic *efx)
2425{
2426 struct net_device *net_dev = efx->net_dev;
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002427 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002428 int rc;
2429
2430 net_dev->watchdog_timeo = 5 * HZ;
2431 net_dev->irq = efx->pci_dev->irq;
Shradha Shah7fa8d542015-05-06 00:55:13 +01002432 net_dev->netdev_ops = &efx_netdev_ops;
2433 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
Ben Hutchings8127d662013-08-29 19:19:29 +01002434 net_dev->priv_flags |= IFF_UNICAST_FLT;
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00002435 net_dev->ethtool_ops = &efx_ethtool_ops;
Ben Hutchings7e6d06f2012-07-30 15:57:44 +00002436 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
Bert Kenwardcd94e512016-10-18 17:47:45 +01002437 net_dev->min_mtu = EFX_MIN_MTU;
2438 net_dev->max_mtu = EFX_MAX_MTU;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002439
Ben Hutchings7dde5962008-12-12 22:09:38 -08002440 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00002441
Ben Hutchings7153f622012-07-27 20:50:52 +01002442 /* Enable resets to be scheduled and check whether any were
2443 * already requested. If so, the NIC is probably hosed so we
2444 * abort.
2445 */
2446 efx->state = STATE_READY;
2447 smp_mb(); /* ensure we change state before checking reset_pending */
2448 if (efx->reset_pending) {
2449 netif_err(efx, probe, efx->net_dev,
2450 "aborting probe due to scheduled reset\n");
2451 rc = -EIO;
2452 goto fail_locked;
2453 }
2454
Ben Hutchingsaed06282009-08-26 08:16:27 +00002455 rc = dev_alloc_name(net_dev, net_dev->name);
2456 if (rc < 0)
2457 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002458 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002459
Ben Hutchings8f8b3d52012-08-24 18:04:38 +01002460 /* Always start with carrier off; PHY events will detect the link */
2461 netif_carrier_off(net_dev);
2462
Ben Hutchingsaed06282009-08-26 08:16:27 +00002463 rc = register_netdevice(net_dev);
2464 if (rc)
2465 goto fail_locked;
2466
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002467 efx_for_each_channel(channel, efx) {
2468 struct efx_tx_queue *tx_queue;
Ben Hutchings60031fc2011-01-12 18:39:40 +00002469 efx_for_each_channel_tx_queue(tx_queue, channel)
2470 efx_init_tx_queue_core_txq(tx_queue);
Ben Hutchingsc04bfc62010-12-10 01:24:16 +00002471 }
2472
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002473 efx_associate(efx);
2474
Ben Hutchings7dde5962008-12-12 22:09:38 -08002475 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002476
Ben Hutchings06d5e192008-12-12 21:47:23 -08002477 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2478 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002479 netif_err(efx, drv, efx->net_dev,
2480 "failed to init net dev attributes\n");
Ben Hutchings06d5e192008-12-12 21:47:23 -08002481 goto fail_registered;
2482 }
Edward Creee7fef9b2015-05-27 13:14:01 +01002483#ifdef CONFIG_SFC_MCDI_LOGGING
2484 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2485 if (rc) {
2486 netif_err(efx, drv, efx->net_dev,
2487 "failed to init net dev attributes\n");
2488 goto fail_attr_mcdi_logging;
2489 }
2490#endif
Ben Hutchings06d5e192008-12-12 21:47:23 -08002491
Ben Hutchings8ceee662008-04-27 12:55:59 +01002492 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08002493
Edward Creee7fef9b2015-05-27 13:14:01 +01002494#ifdef CONFIG_SFC_MCDI_LOGGING
2495fail_attr_mcdi_logging:
2496 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2497#endif
Ben Hutchings7153f622012-07-27 20:50:52 +01002498fail_registered:
2499 rtnl_lock();
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002500 efx_dissociate(efx);
Ben Hutchings7153f622012-07-27 20:50:52 +01002501 unregister_netdevice(net_dev);
Ben Hutchingsaed06282009-08-26 08:16:27 +00002502fail_locked:
Ben Hutchings7153f622012-07-27 20:50:52 +01002503 efx->state = STATE_UNINIT;
Ben Hutchingsaed06282009-08-26 08:16:27 +00002504 rtnl_unlock();
Ben Hutchings62776d02010-06-23 11:30:07 +00002505 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
Ben Hutchingsaed06282009-08-26 08:16:27 +00002506 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002507}
2508
2509static void efx_unregister_netdev(struct efx_nic *efx)
2510{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002511 if (!efx->net_dev)
2512 return;
2513
Ben Hutchings767e4682008-09-01 12:43:14 +01002514 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002515
Edward Creee7fef9b2015-05-27 13:14:01 +01002516 if (efx_dev_registered(efx)) {
2517 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2518#ifdef CONFIG_SFC_MCDI_LOGGING
2519 device_remove_file(&efx->pci_dev->dev, &dev_attr_mcdi_logging);
2520#endif
2521 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2522 unregister_netdev(efx->net_dev);
2523 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002524}
2525
2526/**************************************************************************
2527 *
2528 * Device reset and suspend
2529 *
2530 **************************************************************************/
2531
Ben Hutchings2467ca42008-09-01 12:48:50 +01002532/* Tears down the entire software state and most of the hardware state
2533 * before reset. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002534void efx_reset_down(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002535{
Ben Hutchings8ceee662008-04-27 12:55:59 +01002536 EFX_ASSERT_RESET_SERIALISED(efx);
2537
Edward Creee2835462014-04-16 19:27:48 +01002538 if (method == RESET_TYPE_MCDI_TIMEOUT)
2539 efx->type->prepare_flr(efx);
2540
Ben Hutchings2467ca42008-09-01 12:48:50 +01002541 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002542 efx_disable_interrupts(efx);
Ben Hutchings5642cee2012-07-27 19:35:52 +01002543
2544 mutex_lock(&efx->mac_lock);
Jon Cooper087e9022015-05-20 11:11:35 +01002545 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2546 method != RESET_TYPE_DATAPATH)
Steve Hodgson4b988282009-01-29 17:50:51 +00002547 efx->phy_op->fini(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002548 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002549}
2550
Ben Hutchings2467ca42008-09-01 12:48:50 +01002551/* This function will always ensure that the locks acquired in
2552 * efx_reset_down() are released. A failure return code indicates
2553 * that we were unable to reinitialise the hardware, and the
2554 * driver should be disabled. If ok is false, then the rx and tx
2555 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002556int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002557{
2558 int rc;
2559
Ben Hutchings2467ca42008-09-01 12:48:50 +01002560 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002561
Edward Creee2835462014-04-16 19:27:48 +01002562 if (method == RESET_TYPE_MCDI_TIMEOUT)
2563 efx->type->finish_flr(efx);
2564
2565 /* Ensure that SRAM is initialised even if we're disabling the device */
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002566 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002567 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002568 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002569 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002570 }
2571
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002572 if (!ok)
2573 goto fail;
2574
Jon Cooper087e9022015-05-20 11:11:35 +01002575 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2576 method != RESET_TYPE_DATAPATH) {
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002577 rc = efx->phy_op->init(efx);
2578 if (rc)
2579 goto fail;
Edward Cree267d9d72015-05-06 00:59:18 +01002580 rc = efx->phy_op->reconfigure(efx);
2581 if (rc && rc != -EPERM)
Ben Hutchings62776d02010-06-23 11:30:07 +00002582 netif_err(efx, drv, efx->net_dev,
2583 "could not restore PHY settings\n");
Steve Hodgson4b988282009-01-29 17:50:51 +00002584 }
2585
Jon Cooper261e4d92013-04-15 18:51:54 +01002586 rc = efx_enable_interrupts(efx);
2587 if (rc)
2588 goto fail;
Daniel Pieczko6d8aaaf2015-05-06 00:57:34 +01002589
2590#ifdef CONFIG_SFC_SRIOV
2591 rc = efx->type->vswitching_restore(efx);
2592 if (rc) /* not fatal; the PF will still work fine */
2593 netif_warn(efx, probe, efx->net_dev,
2594 "failed to restore vswitching rc=%d;"
2595 " VFs may not function\n", rc);
2596#endif
2597
Edward Cree0d322412015-05-20 11:10:03 +01002598 down_read(&efx->filter_sem);
Ben Hutchings64eebcf2010-09-20 08:43:07 +00002599 efx_restore_filters(efx);
Edward Cree0d322412015-05-20 11:10:03 +01002600 up_read(&efx->filter_sem);
Shradha Shah7fa8d542015-05-06 00:55:13 +01002601 if (efx->type->sriov_reset)
2602 efx->type->sriov_reset(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01002603
2604 mutex_unlock(&efx->mac_lock);
2605
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002606 efx_start_all(efx);
2607
2608 return 0;
2609
2610fail:
2611 efx->port_initialized = false;
2612
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002613 mutex_unlock(&efx->mac_lock);
2614
Ben Hutchings8ceee662008-04-27 12:55:59 +01002615 return rc;
2616}
2617
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002618/* Reset the NIC using the specified method. Note that the reset may
2619 * fail, in which case the card will be left in an unusable state.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002620 *
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002621 * Caller must hold the rtnl_lock.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002622 */
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002623int efx_reset(struct efx_nic *efx, enum reset_type method)
Ben Hutchings8ceee662008-04-27 12:55:59 +01002624{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002625 int rc, rc2;
2626 bool disabled;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002627
Ben Hutchings62776d02010-06-23 11:30:07 +00002628 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2629 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002630
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01002631 efx_device_detach_sync(efx);
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002632 efx_reset_down(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002633
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002634 rc = efx->type->reset(efx, method);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002635 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00002636 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002637 goto out;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002638 }
2639
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002640 /* Clear flags for the scopes we covered. We assume the NIC and
2641 * driver are now quiescent so that there is no race here.
2642 */
Edward Creee2835462014-04-16 19:27:48 +01002643 if (method < RESET_TYPE_MAX_METHOD)
2644 efx->reset_pending &= -(1 << (method + 1));
2645 else /* it doesn't fit into the well-ordered scope hierarchy */
2646 __clear_bit(method, &efx->reset_pending);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002647
2648 /* Reinitialise bus-mastering, which may have been turned off before
2649 * the reset was scheduled. This is still appropriate, even in the
2650 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2651 * can respond to requests. */
2652 pci_set_master(efx->pci_dev);
2653
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002654out:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002655 /* Leave device stopped if necessary */
Alexandre Rames626950d2013-01-14 17:20:22 +00002656 disabled = rc ||
2657 method == RESET_TYPE_DISABLE ||
2658 method == RESET_TYPE_RECOVER_OR_DISABLE;
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002659 rc2 = efx_reset_up(efx, method, !disabled);
2660 if (rc2) {
2661 disabled = true;
2662 if (!rc)
2663 rc = rc2;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002664 }
2665
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002666 if (disabled) {
Ben Hutchingsf49a4582010-04-28 09:01:33 +00002667 dev_close(efx->net_dev);
Ben Hutchings62776d02010-06-23 11:30:07 +00002668 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002669 efx->state = STATE_DISABLED;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002670 } else {
Ben Hutchings62776d02010-06-23 11:30:07 +00002671 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
Ben Hutchingse4abce82011-05-16 18:51:24 +01002672 netif_device_attach(efx->net_dev);
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08002673 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002674 return rc;
2675}
2676
Alexandre Rames626950d2013-01-14 17:20:22 +00002677/* Try recovery mechanisms.
2678 * For now only EEH is supported.
2679 * Returns 0 if the recovery mechanisms are unsuccessful.
2680 * Returns a non-zero value otherwise.
2681 */
Alexandre Ramesb28405b2013-03-21 16:41:43 +00002682int efx_try_recovery(struct efx_nic *efx)
Alexandre Rames626950d2013-01-14 17:20:22 +00002683{
2684#ifdef CONFIG_EEH
2685 /* A PCI error can occur and not be seen by EEH because nothing
2686 * happens on the PCI bus. In this case the driver may fail and
2687 * schedule a 'recover or reset', leading to this recovery handler.
2688 * Manually call the eeh failure check function.
2689 */
Benjamin Herrenschmidt12a89db2015-03-23 14:00:47 +11002690 struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00002691 if (eeh_dev_check_failure(eehdev)) {
2692 /* The EEH mechanisms will handle the error and reset the
2693 * device if necessary.
2694 */
2695 return 1;
2696 }
2697#endif
2698 return 0;
2699}
2700
Jon Cooper74cd60a2013-09-16 14:18:51 +01002701static void efx_wait_for_bist_end(struct efx_nic *efx)
2702{
2703 int i;
2704
2705 for (i = 0; i < BIST_WAIT_DELAY_COUNT; ++i) {
2706 if (efx_mcdi_poll_reboot(efx))
2707 goto out;
2708 msleep(BIST_WAIT_DELAY_MS);
2709 }
2710
2711 netif_err(efx, drv, efx->net_dev, "Warning: No MC reboot after BIST mode\n");
2712out:
2713 /* Either way unset the BIST flag. If we found no reboot we probably
2714 * won't recover, but we should try.
2715 */
2716 efx->mc_bist_for_other_fn = false;
2717}
2718
Ben Hutchings8ceee662008-04-27 12:55:59 +01002719/* The worker thread exists so that code that cannot sleep can
2720 * schedule a reset for later.
2721 */
2722static void efx_reset_work(struct work_struct *data)
2723{
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002724 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
Alexandre Rames626950d2013-01-14 17:20:22 +00002725 unsigned long pending;
2726 enum reset_type method;
2727
2728 pending = ACCESS_ONCE(efx->reset_pending);
2729 method = fls(pending) - 1;
2730
Jon Cooper74cd60a2013-09-16 14:18:51 +01002731 if (method == RESET_TYPE_MC_BIST)
2732 efx_wait_for_bist_end(efx);
2733
Alexandre Rames626950d2013-01-14 17:20:22 +00002734 if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2735 method == RESET_TYPE_RECOVER_OR_ALL) &&
2736 efx_try_recovery(efx))
2737 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002738
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002739 if (!pending)
Steve Hodgson319ba642010-06-01 11:17:24 +00002740 return;
2741
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002742 rtnl_lock();
Ben Hutchings7153f622012-07-27 20:50:52 +01002743
2744 /* We checked the state in efx_schedule_reset() but it may
2745 * have changed by now. Now that we have the RTNL lock,
2746 * it cannot change again.
2747 */
2748 if (efx->state == STATE_READY)
Alexandre Rames626950d2013-01-14 17:20:22 +00002749 (void)efx_reset(efx, method);
Ben Hutchings7153f622012-07-27 20:50:52 +01002750
Ben Hutchingseb9f6742009-11-29 03:43:15 +00002751 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002752}
2753
2754void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2755{
2756 enum reset_type method;
2757
Alexandre Rames626950d2013-01-14 17:20:22 +00002758 if (efx->state == STATE_RECOVERY) {
2759 netif_dbg(efx, drv, efx->net_dev,
2760 "recovering: skip scheduling %s reset\n",
2761 RESET_TYPE(type));
2762 return;
2763 }
2764
Ben Hutchings8ceee662008-04-27 12:55:59 +01002765 switch (type) {
2766 case RESET_TYPE_INVISIBLE:
2767 case RESET_TYPE_ALL:
Alexandre Rames626950d2013-01-14 17:20:22 +00002768 case RESET_TYPE_RECOVER_OR_ALL:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002769 case RESET_TYPE_WORLD:
2770 case RESET_TYPE_DISABLE:
Alexandre Rames626950d2013-01-14 17:20:22 +00002771 case RESET_TYPE_RECOVER_OR_DISABLE:
Jon Cooper087e9022015-05-20 11:11:35 +01002772 case RESET_TYPE_DATAPATH:
Jon Cooper74cd60a2013-09-16 14:18:51 +01002773 case RESET_TYPE_MC_BIST:
Edward Creee2835462014-04-16 19:27:48 +01002774 case RESET_TYPE_MCDI_TIMEOUT:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002775 method = type;
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002776 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2777 RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002778 break;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002779 default:
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002780 method = efx->type->map_reset_reason(type);
Ben Hutchings62776d02010-06-23 11:30:07 +00002781 netif_dbg(efx, drv, efx->net_dev,
2782 "scheduling %s reset for %s\n",
2783 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings0e2a9c72011-06-24 20:50:07 +01002784 break;
2785 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002786
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01002787 set_bit(method, &efx->reset_pending);
Ben Hutchings7153f622012-07-27 20:50:52 +01002788 smp_mb(); /* ensure we change reset_pending before checking state */
2789
2790 /* If we're not READY then just leave the flags set as the cue
2791 * to abort probing or reschedule the reset later.
2792 */
2793 if (ACCESS_ONCE(efx->state) != STATE_READY)
2794 return;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002795
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002796 /* efx_process_channel() will no longer read events once a
2797 * reset is scheduled. So switch back to poll'd MCDI completions. */
2798 efx_mcdi_mode_poll(efx);
2799
Steve Hodgson1ab00622008-12-12 21:33:02 -08002800 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002801}
2802
2803/**************************************************************************
2804 *
2805 * List of NICs we support
2806 *
2807 **************************************************************************/
2808
2809/* PCI device ID table */
Benoit Taine9baa3c32014-08-08 15:56:03 +02002810static const struct pci_device_id efx_pci_table[] = {
Ben Hutchings547c4742011-12-02 18:23:56 +00002811 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002812 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings547c4742011-12-02 18:23:56 +00002813 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
Ben Hutchings8880f4e2009-11-29 15:15:41 +00002814 .driver_data = (unsigned long) &siena_a0_nic_type},
Ben Hutchings8127d662013-08-29 19:19:29 +01002815 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
2816 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Shradha Shah6f7f8aa2015-05-06 01:00:07 +01002817 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
2818 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
Mateusz Wrzesinski3b06a002014-07-14 08:38:49 +01002819 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
2820 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
Bert Kenwarddd248f12015-11-30 09:05:47 +00002821 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
2822 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
2823 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
2824 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
2825 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
2826 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
Ben Hutchings8ceee662008-04-27 12:55:59 +01002827 {0} /* end of list */
2828};
2829
2830/**************************************************************************
2831 *
Ben Hutchings37594332009-11-23 16:05:45 +00002832 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002833 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01002834 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01002835 * Needed so all function pointers are valid and do not have to be tested
2836 * before use
2837 *
2838 **************************************************************************/
2839int efx_port_dummy_op_int(struct efx_nic *efx)
2840{
2841 return 0;
2842}
2843void efx_port_dummy_op_void(struct efx_nic *efx) {}
stephen hemmingerd2156972010-10-18 05:27:31 +00002844
2845static bool efx_port_dummy_op_poll(struct efx_nic *efx)
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002846{
2847 return false;
2848}
Ben Hutchings8ceee662008-04-27 12:55:59 +01002849
stephen hemminger6c8c2512011-04-14 05:50:12 +00002850static const struct efx_phy_operations efx_dummy_phy_operations = {
Ben Hutchings8ceee662008-04-27 12:55:59 +01002851 .init = efx_port_dummy_op_int,
Ben Hutchingsd3245b22009-11-29 03:42:41 +00002852 .reconfigure = efx_port_dummy_op_int,
Steve Hodgsonfdaa9ae2009-11-28 05:34:05 +00002853 .poll = efx_port_dummy_op_poll,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002854 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002855};
2856
Ben Hutchings8ceee662008-04-27 12:55:59 +01002857/**************************************************************************
2858 *
2859 * Data housekeeping
2860 *
2861 **************************************************************************/
2862
2863/* This zeroes out and then fills in the invariants in a struct
2864 * efx_nic (including all sub-structures).
2865 */
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01002866static int efx_init_struct(struct efx_nic *efx,
Ben Hutchings8ceee662008-04-27 12:55:59 +01002867 struct pci_dev *pci_dev, struct net_device *net_dev)
2868{
Ben Hutchings46426102010-09-10 06:42:33 +00002869 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002870
2871 /* Initialise common structures */
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002872 INIT_LIST_HEAD(&efx->node);
2873 INIT_LIST_HEAD(&efx->secondary_list);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002874 spin_lock_init(&efx->biu_lock);
Ben Hutchings76884832009-11-29 15:10:44 +00002875#ifdef CONFIG_SFC_MTD
2876 INIT_LIST_HEAD(&efx->mtd_list);
2877#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01002878 INIT_WORK(&efx->reset_work, efx_reset_work);
2879 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
Ben Hutchingsdd407812012-02-28 23:40:21 +00002880 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002881 efx->pci_dev = pci_dev;
Ben Hutchings62776d02010-06-23 11:30:07 +00002882 efx->msg_enable = debug;
Ben Hutchingsf16aeea2012-07-27 19:31:16 +01002883 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002884 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01002885
2886 efx->net_dev = net_dev;
Jon Cooper43a37392012-10-18 15:49:54 +01002887 efx->rx_prefix_size = efx->type->rx_prefix_size;
Andrew Rybchenko2ec03012013-11-16 11:02:27 +04002888 efx->rx_ip_align =
2889 NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
Jon Cooper43a37392012-10-18 15:49:54 +01002890 efx->rx_packet_hash_offset =
2891 efx->type->rx_hash_offset - efx->type->rx_prefix_size;
Jon Cooperbd9a2652013-11-18 12:54:41 +00002892 efx->rx_packet_ts_offset =
2893 efx->type->rx_ts_offset - efx->type->rx_prefix_size;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002894 spin_lock_init(&efx->stats_lock);
2895 mutex_init(&efx->mac_lock);
2896 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00002897 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08002898 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings9f2cb712012-02-08 00:11:20 +00002899 init_waitqueue_head(&efx->flush_wq);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002900
2901 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
Ben Hutchings46426102010-09-10 06:42:33 +00002902 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2903 if (!efx->channel[i])
2904 goto fail;
Ben Hutchingsd8291182012-10-05 23:35:41 +01002905 efx->msi_context[i].efx = efx;
2906 efx->msi_context[i].index = i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002907 }
2908
Ben Hutchings8ceee662008-04-27 12:55:59 +01002909 /* Higher numbered interrupt modes are less capable! */
2910 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2911 interrupt_mode);
2912
Ben Hutchings6977dc62008-12-26 13:44:39 -08002913 /* Would be good to use the net_dev name, but we're too early */
2914 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2915 pci_name(pci_dev));
2916 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002917 if (!efx->workqueue)
Ben Hutchings46426102010-09-10 06:42:33 +00002918 goto fail;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002919
Ben Hutchings8ceee662008-04-27 12:55:59 +01002920 return 0;
Ben Hutchings46426102010-09-10 06:42:33 +00002921
2922fail:
2923 efx_fini_struct(efx);
2924 return -ENOMEM;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002925}
2926
2927static void efx_fini_struct(struct efx_nic *efx)
2928{
Ben Hutchings8313aca2010-09-10 06:41:57 +00002929 int i;
2930
2931 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2932 kfree(efx->channel[i]);
2933
Ben Hutchingsef215e62013-12-05 20:13:22 +00002934 kfree(efx->vpd_sn);
2935
Ben Hutchings8ceee662008-04-27 12:55:59 +01002936 if (efx->workqueue) {
2937 destroy_workqueue(efx->workqueue);
2938 efx->workqueue = NULL;
2939 }
2940}
2941
Edward Creee4d112e2014-07-15 11:58:12 +01002942void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
2943{
2944 u64 n_rx_nodesc_trunc = 0;
2945 struct efx_channel *channel;
2946
2947 efx_for_each_channel(channel, efx)
2948 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
2949 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
2950 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
2951}
2952
Ben Hutchings8ceee662008-04-27 12:55:59 +01002953/**************************************************************************
2954 *
2955 * PCI interface
2956 *
2957 **************************************************************************/
2958
2959/* Main body of final NIC shutdown code
2960 * This is called only at module unload (or hotplug removal).
2961 */
2962static void efx_pci_remove_main(struct efx_nic *efx)
2963{
Ben Hutchings7153f622012-07-27 20:50:52 +01002964 /* Flush reset_work. It can no longer be scheduled since we
2965 * are not READY.
2966 */
2967 BUG_ON(efx->state == STATE_READY);
2968 cancel_work_sync(&efx->reset_work);
2969
Ben Hutchingsd8291182012-10-05 23:35:41 +01002970 efx_disable_interrupts(efx);
Ben Hutchings152b6a62009-11-29 03:43:56 +00002971 efx_nic_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002972 efx_fini_port(efx);
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00002973 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002974 efx_fini_napi(efx);
2975 efx_remove_all(efx);
2976}
2977
2978/* Final NIC shutdown
Daniel Pieczko2a3fc312015-06-02 11:40:46 +01002979 * This is called only at module unload (or hotplug removal). A PF can call
2980 * this on its VFs to ensure they are unbound first.
Ben Hutchings8ceee662008-04-27 12:55:59 +01002981 */
2982static void efx_pci_remove(struct pci_dev *pci_dev)
2983{
2984 struct efx_nic *efx;
2985
2986 efx = pci_get_drvdata(pci_dev);
2987 if (!efx)
2988 return;
2989
2990 /* Mark the NIC as fini, then stop the interface */
2991 rtnl_lock();
Ben Hutchings0bcf4a62013-10-18 19:21:45 +01002992 efx_dissociate(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002993 dev_close(efx->net_dev);
Ben Hutchingsd8291182012-10-05 23:35:41 +01002994 efx_disable_interrupts(efx);
Edward Creeea6bb992015-06-15 18:27:54 +01002995 efx->state = STATE_UNINIT;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002996 rtnl_unlock();
2997
Shradha Shah7fa8d542015-05-06 00:55:13 +01002998 if (efx->type->sriov_fini)
2999 efx->type->sriov_fini(efx);
3000
Ben Hutchings8ceee662008-04-27 12:55:59 +01003001 efx_unregister_netdev(efx);
3002
Ben Hutchings7dde5962008-12-12 22:09:38 -08003003 efx_mtd_remove(efx);
3004
Ben Hutchings8ceee662008-04-27 12:55:59 +01003005 efx_pci_remove_main(efx);
3006
Ben Hutchings8ceee662008-04-27 12:55:59 +01003007 efx_fini_io(efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00003008 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003009
Ben Hutchings8ceee662008-04-27 12:55:59 +01003010 efx_fini_struct(efx);
3011 free_netdev(efx->net_dev);
Alexandre Rames626950d2013-01-14 17:20:22 +00003012
3013 pci_disable_pcie_error_reporting(pci_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003014};
3015
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003016/* NIC VPD information
3017 * Called during probe to display the part number of the
3018 * installed NIC. VPD is potentially very large but this should
3019 * always appear within the first 512 bytes.
3020 */
3021#define SFC_VPD_LEN 512
Ben Hutchingsef215e62013-12-05 20:13:22 +00003022static void efx_probe_vpd_strings(struct efx_nic *efx)
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003023{
3024 struct pci_dev *dev = efx->pci_dev;
3025 char vpd_data[SFC_VPD_LEN];
3026 ssize_t vpd_size;
Ben Hutchingsef215e62013-12-05 20:13:22 +00003027 int ro_start, ro_size, i, j;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003028
3029 /* Get the vpd data from the device */
3030 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
3031 if (vpd_size <= 0) {
3032 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
3033 return;
3034 }
3035
3036 /* Get the Read only section */
Ben Hutchingsef215e62013-12-05 20:13:22 +00003037 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
3038 if (ro_start < 0) {
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003039 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
3040 return;
3041 }
3042
Ben Hutchingsef215e62013-12-05 20:13:22 +00003043 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
3044 j = ro_size;
3045 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003046 if (i + j > vpd_size)
3047 j = vpd_size - i;
3048
3049 /* Get the Part number */
3050 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
3051 if (i < 0) {
3052 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
3053 return;
3054 }
3055
3056 j = pci_vpd_info_field_size(&vpd_data[i]);
3057 i += PCI_VPD_INFO_FLD_HDR_SIZE;
3058 if (i + j > vpd_size) {
3059 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
3060 return;
3061 }
3062
3063 netif_info(efx, drv, efx->net_dev,
3064 "Part Number : %.*s\n", j, &vpd_data[i]);
Ben Hutchingsef215e62013-12-05 20:13:22 +00003065
3066 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
3067 j = ro_size;
3068 i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
3069 if (i < 0) {
3070 netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
3071 return;
3072 }
3073
3074 j = pci_vpd_info_field_size(&vpd_data[i]);
3075 i += PCI_VPD_INFO_FLD_HDR_SIZE;
3076 if (i + j > vpd_size) {
3077 netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
3078 return;
3079 }
3080
3081 efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
3082 if (!efx->vpd_sn)
3083 return;
3084
3085 snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003086}
3087
3088
Ben Hutchings8ceee662008-04-27 12:55:59 +01003089/* Main body of NIC initialisation
3090 * This is called at module load (or hotplug insertion, theoretically).
3091 */
3092static int efx_pci_probe_main(struct efx_nic *efx)
3093{
3094 int rc;
3095
3096 /* Do start-of-day initialisation */
3097 rc = efx_probe_all(efx);
3098 if (rc)
3099 goto fail1;
3100
Ben Hutchingse8f14992010-12-07 19:47:34 +00003101 efx_init_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003102
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00003103 rc = efx->type->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003104 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00003105 netif_err(efx, probe, efx->net_dev,
3106 "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00003107 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003108 }
3109
3110 rc = efx_init_port(efx);
3111 if (rc) {
Ben Hutchings62776d02010-06-23 11:30:07 +00003112 netif_err(efx, probe, efx->net_dev,
3113 "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00003114 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003115 }
3116
Ben Hutchings152b6a62009-11-29 03:43:56 +00003117 rc = efx_nic_init_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003118 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00003119 goto fail5;
Jon Cooper261e4d92013-04-15 18:51:54 +01003120 rc = efx_enable_interrupts(efx);
3121 if (rc)
3122 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003123
3124 return 0;
3125
Jon Cooper261e4d92013-04-15 18:51:54 +01003126 fail6:
3127 efx_nic_fini_interrupt(efx);
Ben Hutchings278c0622009-11-23 16:05:12 +00003128 fail5:
Ben Hutchings8ceee662008-04-27 12:55:59 +01003129 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003130 fail4:
Ben Hutchingsef2b90e2009-11-29 03:42:31 +00003131 efx->type->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003132 fail3:
3133 efx_fini_napi(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003134 efx_remove_all(efx);
3135 fail1:
3136 return rc;
3137}
3138
3139/* NIC initialisation
3140 *
3141 * This is called at module load (or hotplug insertion,
Ben Hutchings73ba7b62012-01-09 19:47:08 +00003142 * theoretically). It sets up PCI mappings, resets the NIC,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003143 * sets up and registers the network devices with the kernel and hooks
3144 * the interrupt service routine. It does not prepare the device for
3145 * transmission; this is left to the first time one of the network
3146 * interfaces is brought up (i.e. efx_net_open).
3147 */
Bill Pemberton87d1fc12012-12-03 09:23:32 -05003148static int efx_pci_probe(struct pci_dev *pci_dev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +00003149 const struct pci_device_id *entry)
Ben Hutchings8ceee662008-04-27 12:55:59 +01003150{
Ben Hutchings8ceee662008-04-27 12:55:59 +01003151 struct net_device *net_dev;
3152 struct efx_nic *efx;
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003153 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003154
3155 /* Allocate and initialise a struct net_device and struct efx_nic */
Ben Hutchings94b274b2011-01-10 21:18:20 +00003156 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
3157 EFX_MAX_RX_QUEUES);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003158 if (!net_dev)
3159 return -ENOMEM;
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01003160 efx = netdev_priv(net_dev);
3161 efx->type = (const struct efx_nic_type *) entry->driver_data;
Andrew Rybchenkoebfcd0f2016-06-15 17:43:20 +01003162 efx->fixed_features |= NETIF_F_HIGHDMA;
Andrew Rybchenkoeb7cfd82016-06-15 17:51:36 +01003163
Ben Hutchings8ceee662008-04-27 12:55:59 +01003164 pci_set_drvdata(pci_dev, efx);
Ben Hutchings62776d02010-06-23 11:30:07 +00003165 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
Ben Hutchingsadeb15a2012-08-02 01:39:38 +01003166 rc = efx_init_struct(efx, pci_dev, net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003167 if (rc)
3168 goto fail1;
3169
Ben Hutchings62776d02010-06-23 11:30:07 +00003170 netif_info(efx, probe, efx->net_dev,
Ben Hutchingsff79c8a2011-07-13 16:21:24 +01003171 "Solarflare NIC detected\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003172
Shradha Shah6f7f8aa2015-05-06 01:00:07 +01003173 if (!efx->type->is_vf)
3174 efx_probe_vpd_strings(efx);
Ben Hutchings460eeaa2012-03-05 15:35:39 +00003175
Ben Hutchings8ceee662008-04-27 12:55:59 +01003176 /* Set up basic I/O (BAR mappings etc) */
3177 rc = efx_init_io(efx);
3178 if (rc)
3179 goto fail2;
3180
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003181 rc = efx_pci_probe_main(efx);
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003182 if (rc)
3183 goto fail3;
Steve Hodgsonfa402b22008-12-12 22:08:16 -08003184
Edward Cree46d1efd2016-11-17 10:52:36 +00003185 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
3186 NETIF_F_TSO | NETIF_F_RXCSUM);
3187 if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
3188 net_dev->features |= NETIF_F_TSO6;
3189 /* Check whether device supports TSO */
3190 if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
3191 net_dev->features &= ~NETIF_F_ALL_TSO;
3192 /* Mask for features that also apply to VLAN devices */
3193 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
3194 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
3195 NETIF_F_RXCSUM);
3196
3197 net_dev->hw_features = net_dev->features & ~efx->fixed_features;
3198
3199 /* Disable VLAN filtering by default. It may be enforced if
3200 * the feature is fixed (i.e. VLAN filters are required to
3201 * receive VLAN tagged packets due to vPort restrictions).
3202 */
3203 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
3204 net_dev->features |= efx->fixed_features;
3205
Ben Hutchings8ceee662008-04-27 12:55:59 +01003206 rc = efx_register_netdev(efx);
3207 if (rc)
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003208 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01003209
Shradha Shah7fa8d542015-05-06 00:55:13 +01003210 if (efx->type->sriov_init) {
3211 rc = efx->type->sriov_init(efx);
3212 if (rc)
3213 netif_err(efx, probe, efx->net_dev,
3214 "SR-IOV can't be enabled rc %d\n", rc);
3215 }
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003216
Ben Hutchings62776d02010-06-23 11:30:07 +00003217 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003218
Ben Hutchings7c431612012-01-27 17:23:58 +00003219 /* Try to create MTDs, but allow this to fail */
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003220 rtnl_lock();
Ben Hutchings7c431612012-01-27 17:23:58 +00003221 rc = efx_mtd_probe(efx);
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00003222 rtnl_unlock();
Bert Kenward09a04202015-12-23 08:58:15 +00003223 if (rc && rc != -EPERM)
Ben Hutchings7c431612012-01-27 17:23:58 +00003224 netif_warn(efx, probe, efx->net_dev,
3225 "failed to create MTDs (%d)\n", rc);
3226
Alexandre Rames626950d2013-01-14 17:20:22 +00003227 rc = pci_enable_pcie_error_reporting(pci_dev);
3228 if (rc && rc != -EINVAL)
Bert Kenward09a04202015-12-23 08:58:15 +00003229 netif_notice(efx, probe, efx->net_dev,
3230 "PCIE error reporting unavailable (%d).\n",
3231 rc);
Alexandre Rames626950d2013-01-14 17:20:22 +00003232
Ben Hutchings8ceee662008-04-27 12:55:59 +01003233 return 0;
3234
Ben Hutchings8ceee662008-04-27 12:55:59 +01003235 fail4:
Ben Hutchingsfadac6a2011-11-19 00:35:47 +00003236 efx_pci_remove_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003237 fail3:
3238 efx_fini_io(efx);
3239 fail2:
3240 efx_fini_struct(efx);
3241 fail1:
Steve Hodgson5e2a9112010-02-12 12:32:27 -08003242 WARN_ON(rc > 0);
Ben Hutchings62776d02010-06-23 11:30:07 +00003243 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
Ben Hutchings8ceee662008-04-27 12:55:59 +01003244 free_netdev(net_dev);
3245 return rc;
3246}
3247
Shradha Shah834e23d2015-05-06 00:55:58 +01003248/* efx_pci_sriov_configure returns the actual number of Virtual Functions
3249 * enabled on success
3250 */
3251#ifdef CONFIG_SFC_SRIOV
3252static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
3253{
3254 int rc;
3255 struct efx_nic *efx = pci_get_drvdata(dev);
3256
3257 if (efx->type->sriov_configure) {
3258 rc = efx->type->sriov_configure(efx, num_vfs);
3259 if (rc)
3260 return rc;
3261 else
3262 return num_vfs;
3263 } else
3264 return -EOPNOTSUPP;
3265}
3266#endif
3267
Ben Hutchings89c758f2009-11-29 03:43:07 +00003268static int efx_pm_freeze(struct device *dev)
3269{
3270 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3271
Ben Hutchings61da0262012-07-27 19:35:39 +01003272 rtnl_lock();
3273
Ben Hutchings6032fb52012-07-27 19:35:47 +01003274 if (efx->state != STATE_DISABLED) {
3275 efx->state = STATE_UNINIT;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003276
Daniel Pieczkoc2f3b8e2012-10-17 13:21:23 +01003277 efx_device_detach_sync(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003278
Ben Hutchings6032fb52012-07-27 19:35:47 +01003279 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003280 efx_disable_interrupts(efx);
Ben Hutchings6032fb52012-07-27 19:35:47 +01003281 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00003282
Ben Hutchings61da0262012-07-27 19:35:39 +01003283 rtnl_unlock();
3284
Ben Hutchings89c758f2009-11-29 03:43:07 +00003285 return 0;
3286}
3287
3288static int efx_pm_thaw(struct device *dev)
3289{
Jon Cooper261e4d92013-04-15 18:51:54 +01003290 int rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003291 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
3292
Ben Hutchings61da0262012-07-27 19:35:39 +01003293 rtnl_lock();
3294
Ben Hutchings6032fb52012-07-27 19:35:47 +01003295 if (efx->state != STATE_DISABLED) {
Jon Cooper261e4d92013-04-15 18:51:54 +01003296 rc = efx_enable_interrupts(efx);
3297 if (rc)
3298 goto fail;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003299
Ben Hutchings6032fb52012-07-27 19:35:47 +01003300 mutex_lock(&efx->mac_lock);
3301 efx->phy_op->reconfigure(efx);
3302 mutex_unlock(&efx->mac_lock);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003303
Ben Hutchings6032fb52012-07-27 19:35:47 +01003304 efx_start_all(efx);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003305
Ben Hutchings6032fb52012-07-27 19:35:47 +01003306 netif_device_attach(efx->net_dev);
Ben Hutchings89c758f2009-11-29 03:43:07 +00003307
Ben Hutchings6032fb52012-07-27 19:35:47 +01003308 efx->state = STATE_READY;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003309
Ben Hutchings6032fb52012-07-27 19:35:47 +01003310 efx->type->resume_wol(efx);
3311 }
Ben Hutchings89c758f2009-11-29 03:43:07 +00003312
Ben Hutchings61da0262012-07-27 19:35:39 +01003313 rtnl_unlock();
3314
Steve Hodgson319ba642010-06-01 11:17:24 +00003315 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
3316 queue_work(reset_workqueue, &efx->reset_work);
3317
Ben Hutchings89c758f2009-11-29 03:43:07 +00003318 return 0;
Jon Cooper261e4d92013-04-15 18:51:54 +01003319
3320fail:
3321 rtnl_unlock();
3322
3323 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003324}
3325
3326static int efx_pm_poweroff(struct device *dev)
3327{
3328 struct pci_dev *pci_dev = to_pci_dev(dev);
3329 struct efx_nic *efx = pci_get_drvdata(pci_dev);
3330
3331 efx->type->fini(efx);
3332
Ben Hutchingsa7d529a2011-06-24 20:46:31 +01003333 efx->reset_pending = 0;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003334
3335 pci_save_state(pci_dev);
3336 return pci_set_power_state(pci_dev, PCI_D3hot);
3337}
3338
3339/* Used for both resume and restore */
3340static int efx_pm_resume(struct device *dev)
3341{
3342 struct pci_dev *pci_dev = to_pci_dev(dev);
3343 struct efx_nic *efx = pci_get_drvdata(pci_dev);
3344 int rc;
3345
3346 rc = pci_set_power_state(pci_dev, PCI_D0);
3347 if (rc)
3348 return rc;
3349 pci_restore_state(pci_dev);
3350 rc = pci_enable_device(pci_dev);
3351 if (rc)
3352 return rc;
3353 pci_set_master(efx->pci_dev);
3354 rc = efx->type->reset(efx, RESET_TYPE_ALL);
3355 if (rc)
3356 return rc;
3357 rc = efx->type->init(efx);
3358 if (rc)
3359 return rc;
Jon Cooper261e4d92013-04-15 18:51:54 +01003360 rc = efx_pm_thaw(dev);
3361 return rc;
Ben Hutchings89c758f2009-11-29 03:43:07 +00003362}
3363
3364static int efx_pm_suspend(struct device *dev)
3365{
3366 int rc;
3367
3368 efx_pm_freeze(dev);
3369 rc = efx_pm_poweroff(dev);
3370 if (rc)
3371 efx_pm_resume(dev);
3372 return rc;
3373}
3374
Ben Hutchings18e83e42012-01-05 19:05:20 +00003375static const struct dev_pm_ops efx_pm_ops = {
Ben Hutchings89c758f2009-11-29 03:43:07 +00003376 .suspend = efx_pm_suspend,
3377 .resume = efx_pm_resume,
3378 .freeze = efx_pm_freeze,
3379 .thaw = efx_pm_thaw,
3380 .poweroff = efx_pm_poweroff,
3381 .restore = efx_pm_resume,
3382};
3383
Alexandre Rames626950d2013-01-14 17:20:22 +00003384/* A PCI error affecting this device was detected.
3385 * At this point MMIO and DMA may be disabled.
3386 * Stop the software path and request a slot reset.
3387 */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003388static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
3389 enum pci_channel_state state)
Alexandre Rames626950d2013-01-14 17:20:22 +00003390{
3391 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3392 struct efx_nic *efx = pci_get_drvdata(pdev);
3393
3394 if (state == pci_channel_io_perm_failure)
3395 return PCI_ERS_RESULT_DISCONNECT;
3396
3397 rtnl_lock();
3398
3399 if (efx->state != STATE_DISABLED) {
3400 efx->state = STATE_RECOVERY;
3401 efx->reset_pending = 0;
3402
3403 efx_device_detach_sync(efx);
3404
3405 efx_stop_all(efx);
Ben Hutchingsd8291182012-10-05 23:35:41 +01003406 efx_disable_interrupts(efx);
Alexandre Rames626950d2013-01-14 17:20:22 +00003407
3408 status = PCI_ERS_RESULT_NEED_RESET;
3409 } else {
3410 /* If the interface is disabled we don't want to do anything
3411 * with it.
3412 */
3413 status = PCI_ERS_RESULT_RECOVERED;
3414 }
3415
3416 rtnl_unlock();
3417
3418 pci_disable_device(pdev);
3419
3420 return status;
3421}
3422
Joe Perchesdbedd442015-03-06 20:49:12 -08003423/* Fake a successful reset, which will be performed later in efx_io_resume. */
stephen hemmingerdebd0032013-03-16 06:57:51 +00003424static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
Alexandre Rames626950d2013-01-14 17:20:22 +00003425{
3426 struct efx_nic *efx = pci_get_drvdata(pdev);
3427 pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3428 int rc;
3429
3430 if (pci_enable_device(pdev)) {
3431 netif_err(efx, hw, efx->net_dev,
3432 "Cannot re-enable PCI device after reset.\n");
3433 status = PCI_ERS_RESULT_DISCONNECT;
3434 }
3435
3436 rc = pci_cleanup_aer_uncorrect_error_status(pdev);
3437 if (rc) {
3438 netif_err(efx, hw, efx->net_dev,
3439 "pci_cleanup_aer_uncorrect_error_status failed (%d)\n", rc);
3440 /* Non-fatal error. Continue. */
3441 }
3442
3443 return status;
3444}
3445
3446/* Perform the actual reset and resume I/O operations. */
3447static void efx_io_resume(struct pci_dev *pdev)
3448{
3449 struct efx_nic *efx = pci_get_drvdata(pdev);
3450 int rc;
3451
3452 rtnl_lock();
3453
3454 if (efx->state == STATE_DISABLED)
3455 goto out;
3456
3457 rc = efx_reset(efx, RESET_TYPE_ALL);
3458 if (rc) {
3459 netif_err(efx, hw, efx->net_dev,
3460 "efx_reset failed after PCI error (%d)\n", rc);
3461 } else {
3462 efx->state = STATE_READY;
3463 netif_dbg(efx, hw, efx->net_dev,
3464 "Done resetting and resuming IO after PCI error.\n");
3465 }
3466
3467out:
3468 rtnl_unlock();
3469}
3470
3471/* For simplicity and reliability, we always require a slot reset and try to
3472 * reset the hardware when a pci error affecting the device is detected.
3473 * We leave both the link_reset and mmio_enabled callback unimplemented:
3474 * with our request for slot reset the mmio_enabled callback will never be
3475 * called, and the link_reset callback is not used by AER or EEH mechanisms.
3476 */
Julia Lawallc300366b2015-11-14 11:06:57 +01003477static const struct pci_error_handlers efx_err_handlers = {
Alexandre Rames626950d2013-01-14 17:20:22 +00003478 .error_detected = efx_io_error_detected,
3479 .slot_reset = efx_io_slot_reset,
3480 .resume = efx_io_resume,
3481};
3482
Ben Hutchings8ceee662008-04-27 12:55:59 +01003483static struct pci_driver efx_pci_driver = {
Ben Hutchingsc5d5f5f2010-06-23 11:30:26 +00003484 .name = KBUILD_MODNAME,
Ben Hutchings8ceee662008-04-27 12:55:59 +01003485 .id_table = efx_pci_table,
3486 .probe = efx_pci_probe,
3487 .remove = efx_pci_remove,
Ben Hutchings89c758f2009-11-29 03:43:07 +00003488 .driver.pm = &efx_pm_ops,
Alexandre Rames626950d2013-01-14 17:20:22 +00003489 .err_handler = &efx_err_handlers,
Shradha Shah834e23d2015-05-06 00:55:58 +01003490#ifdef CONFIG_SFC_SRIOV
3491 .sriov_configure = efx_pci_sriov_configure,
3492#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003493};
3494
3495/**************************************************************************
3496 *
3497 * Kernel module interface
3498 *
3499 *************************************************************************/
3500
3501module_param(interrupt_mode, uint, 0444);
3502MODULE_PARM_DESC(interrupt_mode,
3503 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3504
3505static int __init efx_init_module(void)
3506{
3507 int rc;
3508
3509 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
3510
3511 rc = register_netdevice_notifier(&efx_netdev_notifier);
3512 if (rc)
3513 goto err_notifier;
3514
Shradha Shah7fa8d542015-05-06 00:55:13 +01003515#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003516 rc = efx_init_sriov();
3517 if (rc)
3518 goto err_sriov;
Shradha Shah7fa8d542015-05-06 00:55:13 +01003519#endif
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003520
Steve Hodgson1ab00622008-12-12 21:33:02 -08003521 reset_workqueue = create_singlethread_workqueue("sfc_reset");
3522 if (!reset_workqueue) {
3523 rc = -ENOMEM;
3524 goto err_reset;
3525 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01003526
3527 rc = pci_register_driver(&efx_pci_driver);
3528 if (rc < 0)
3529 goto err_pci;
3530
3531 return 0;
3532
3533 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08003534 destroy_workqueue(reset_workqueue);
3535 err_reset:
Shradha Shah7fa8d542015-05-06 00:55:13 +01003536#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003537 efx_fini_sriov();
3538 err_sriov:
Shradha Shah7fa8d542015-05-06 00:55:13 +01003539#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003540 unregister_netdevice_notifier(&efx_netdev_notifier);
3541 err_notifier:
3542 return rc;
3543}
3544
3545static void __exit efx_exit_module(void)
3546{
3547 printk(KERN_INFO "Solarflare NET driver unloading\n");
3548
3549 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08003550 destroy_workqueue(reset_workqueue);
Shradha Shah7fa8d542015-05-06 00:55:13 +01003551#ifdef CONFIG_SFC_SRIOV
Ben Hutchingscd2d5b52012-02-14 00:48:07 +00003552 efx_fini_sriov();
Shradha Shah7fa8d542015-05-06 00:55:13 +01003553#endif
Ben Hutchings8ceee662008-04-27 12:55:59 +01003554 unregister_netdevice_notifier(&efx_netdev_notifier);
3555
3556}
3557
3558module_init(efx_init_module);
3559module_exit(efx_exit_module);
3560
Ben Hutchings906bb262009-11-29 15:16:19 +00003561MODULE_AUTHOR("Solarflare Communications and "
3562 "Michael Brown <mbrown@fensystems.co.uk>");
Ben Hutchings6a350fd2014-02-12 19:00:07 +00003563MODULE_DESCRIPTION("Solarflare network driver");
Ben Hutchings8ceee662008-04-27 12:55:59 +01003564MODULE_LICENSE("GPL");
3565MODULE_DEVICE_TABLE(pci, efx_pci_table);
Edward Cree14077e92017-01-03 15:46:00 +00003566MODULE_VERSION(EFX_DRIVER_VERSION);