1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /****************************************************************************
3*4882a593Smuzhiyun * Driver for Solarflare network controllers and boards
4*4882a593Smuzhiyun * Copyright 2005-2006 Fen Systems Ltd.
5*4882a593Smuzhiyun * Copyright 2005-2013 Solarflare Communications Inc.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/pci.h>
10*4882a593Smuzhiyun #include <linux/netdevice.h>
11*4882a593Smuzhiyun #include <linux/etherdevice.h>
12*4882a593Smuzhiyun #include <linux/delay.h>
13*4882a593Smuzhiyun #include <linux/notifier.h>
14*4882a593Smuzhiyun #include <linux/ip.h>
15*4882a593Smuzhiyun #include <linux/tcp.h>
16*4882a593Smuzhiyun #include <linux/in.h>
17*4882a593Smuzhiyun #include <linux/ethtool.h>
18*4882a593Smuzhiyun #include <linux/topology.h>
19*4882a593Smuzhiyun #include <linux/gfp.h>
20*4882a593Smuzhiyun #include <linux/aer.h>
21*4882a593Smuzhiyun #include <linux/interrupt.h>
22*4882a593Smuzhiyun #include "net_driver.h"
23*4882a593Smuzhiyun #include "efx.h"
24*4882a593Smuzhiyun #include "nic.h"
25*4882a593Smuzhiyun #include "selftest.h"
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun #include "workarounds.h"
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /**************************************************************************
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * Type name strings
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun **************************************************************************
34*4882a593Smuzhiyun */
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun /* Loopback mode names (see LOOPBACK_MODE()) */
37*4882a593Smuzhiyun const unsigned int ef4_loopback_mode_max = LOOPBACK_MAX;
38*4882a593Smuzhiyun const char *const ef4_loopback_mode_names[] = {
39*4882a593Smuzhiyun [LOOPBACK_NONE] = "NONE",
40*4882a593Smuzhiyun [LOOPBACK_DATA] = "DATAPATH",
41*4882a593Smuzhiyun [LOOPBACK_GMAC] = "GMAC",
42*4882a593Smuzhiyun [LOOPBACK_XGMII] = "XGMII",
43*4882a593Smuzhiyun [LOOPBACK_XGXS] = "XGXS",
44*4882a593Smuzhiyun [LOOPBACK_XAUI] = "XAUI",
45*4882a593Smuzhiyun [LOOPBACK_GMII] = "GMII",
46*4882a593Smuzhiyun [LOOPBACK_SGMII] = "SGMII",
47*4882a593Smuzhiyun [LOOPBACK_XGBR] = "XGBR",
48*4882a593Smuzhiyun [LOOPBACK_XFI] = "XFI",
49*4882a593Smuzhiyun [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
50*4882a593Smuzhiyun [LOOPBACK_GMII_FAR] = "GMII_FAR",
51*4882a593Smuzhiyun [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
52*4882a593Smuzhiyun [LOOPBACK_XFI_FAR] = "XFI_FAR",
53*4882a593Smuzhiyun [LOOPBACK_GPHY] = "GPHY",
54*4882a593Smuzhiyun [LOOPBACK_PHYXS] = "PHYXS",
55*4882a593Smuzhiyun [LOOPBACK_PCS] = "PCS",
56*4882a593Smuzhiyun [LOOPBACK_PMAPMD] = "PMA/PMD",
57*4882a593Smuzhiyun [LOOPBACK_XPORT] = "XPORT",
58*4882a593Smuzhiyun [LOOPBACK_XGMII_WS] = "XGMII_WS",
59*4882a593Smuzhiyun [LOOPBACK_XAUI_WS] = "XAUI_WS",
60*4882a593Smuzhiyun [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
61*4882a593Smuzhiyun [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
62*4882a593Smuzhiyun [LOOPBACK_GMII_WS] = "GMII_WS",
63*4882a593Smuzhiyun [LOOPBACK_XFI_WS] = "XFI_WS",
64*4882a593Smuzhiyun [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
65*4882a593Smuzhiyun [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
66*4882a593Smuzhiyun };
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun const unsigned int ef4_reset_type_max = RESET_TYPE_MAX;
69*4882a593Smuzhiyun const char *const ef4_reset_type_names[] = {
70*4882a593Smuzhiyun [RESET_TYPE_INVISIBLE] = "INVISIBLE",
71*4882a593Smuzhiyun [RESET_TYPE_ALL] = "ALL",
72*4882a593Smuzhiyun [RESET_TYPE_RECOVER_OR_ALL] = "RECOVER_OR_ALL",
73*4882a593Smuzhiyun [RESET_TYPE_WORLD] = "WORLD",
74*4882a593Smuzhiyun [RESET_TYPE_RECOVER_OR_DISABLE] = "RECOVER_OR_DISABLE",
75*4882a593Smuzhiyun [RESET_TYPE_DATAPATH] = "DATAPATH",
76*4882a593Smuzhiyun [RESET_TYPE_DISABLE] = "DISABLE",
77*4882a593Smuzhiyun [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
78*4882a593Smuzhiyun [RESET_TYPE_INT_ERROR] = "INT_ERROR",
79*4882a593Smuzhiyun [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
80*4882a593Smuzhiyun [RESET_TYPE_DMA_ERROR] = "DMA_ERROR",
81*4882a593Smuzhiyun [RESET_TYPE_TX_SKIP] = "TX_SKIP",
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /* Reset workqueue. If any NIC has a hardware failure then a reset will be
85*4882a593Smuzhiyun * queued onto this work queue. This is not a per-nic work queue, because
86*4882a593Smuzhiyun * ef4_reset_work() acquires the rtnl lock, so resets are naturally serialised.
87*4882a593Smuzhiyun */
88*4882a593Smuzhiyun static struct workqueue_struct *reset_workqueue;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* How often and how many times to poll for a reset while waiting for a
91*4882a593Smuzhiyun * BIST that another function started to complete.
92*4882a593Smuzhiyun */
93*4882a593Smuzhiyun #define BIST_WAIT_DELAY_MS 100
94*4882a593Smuzhiyun #define BIST_WAIT_DELAY_COUNT 100
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /**************************************************************************
97*4882a593Smuzhiyun *
98*4882a593Smuzhiyun * Configurable values
99*4882a593Smuzhiyun *
100*4882a593Smuzhiyun *************************************************************************/
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun * Use separate channels for TX and RX events
104*4882a593Smuzhiyun *
105*4882a593Smuzhiyun * Set this to 1 to use separate channels for TX and RX. It allows us
106*4882a593Smuzhiyun * to control interrupt affinity separately for TX and RX.
107*4882a593Smuzhiyun *
108*4882a593Smuzhiyun * This is only used in MSI-X interrupt mode
109*4882a593Smuzhiyun */
110*4882a593Smuzhiyun bool ef4_separate_tx_channels;
111*4882a593Smuzhiyun module_param(ef4_separate_tx_channels, bool, 0444);
112*4882a593Smuzhiyun MODULE_PARM_DESC(ef4_separate_tx_channels,
113*4882a593Smuzhiyun "Use separate channels for TX and RX");
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun /* This is the weight assigned to each of the (per-channel) virtual
116*4882a593Smuzhiyun * NAPI devices.
117*4882a593Smuzhiyun */
118*4882a593Smuzhiyun static int napi_weight = 64;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun /* This is the time (in jiffies) between invocations of the hardware
121*4882a593Smuzhiyun * monitor.
122*4882a593Smuzhiyun * On Falcon-based NICs, this will:
123*4882a593Smuzhiyun * - Check the on-board hardware monitor;
124*4882a593Smuzhiyun * - Poll the link state and reconfigure the hardware as necessary.
125*4882a593Smuzhiyun * On Siena-based NICs for power systems with EEH support, this will give EEH a
126*4882a593Smuzhiyun * chance to start.
127*4882a593Smuzhiyun */
128*4882a593Smuzhiyun static unsigned int ef4_monitor_interval = 1 * HZ;
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun /* Initial interrupt moderation settings. They can be modified after
131*4882a593Smuzhiyun * module load with ethtool.
132*4882a593Smuzhiyun *
133*4882a593Smuzhiyun * The default for RX should strike a balance between increasing the
134*4882a593Smuzhiyun * round-trip latency and reducing overhead.
135*4882a593Smuzhiyun */
136*4882a593Smuzhiyun static unsigned int rx_irq_mod_usec = 60;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /* Initial interrupt moderation settings. They can be modified after
139*4882a593Smuzhiyun * module load with ethtool.
140*4882a593Smuzhiyun *
141*4882a593Smuzhiyun * This default is chosen to ensure that a 10G link does not go idle
142*4882a593Smuzhiyun * while a TX queue is stopped after it has become full. A queue is
143*4882a593Smuzhiyun * restarted when it drops below half full. The time this takes (assuming
144*4882a593Smuzhiyun * worst case 3 descriptors per packet and 1024 descriptors) is
145*4882a593Smuzhiyun * 512 / 3 * 1.2 = 205 usec.
146*4882a593Smuzhiyun */
147*4882a593Smuzhiyun static unsigned int tx_irq_mod_usec = 150;
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun /* This is the first interrupt mode to try out of:
150*4882a593Smuzhiyun * 0 => MSI-X
151*4882a593Smuzhiyun * 1 => MSI
152*4882a593Smuzhiyun * 2 => legacy
153*4882a593Smuzhiyun */
154*4882a593Smuzhiyun static unsigned int interrupt_mode;
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
157*4882a593Smuzhiyun * i.e. the number of CPUs among which we may distribute simultaneous
158*4882a593Smuzhiyun * interrupt handling.
159*4882a593Smuzhiyun *
160*4882a593Smuzhiyun * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
161*4882a593Smuzhiyun * The default (0) means to assign an interrupt to each core.
162*4882a593Smuzhiyun */
163*4882a593Smuzhiyun static unsigned int rss_cpus;
164*4882a593Smuzhiyun module_param(rss_cpus, uint, 0444);
165*4882a593Smuzhiyun MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun static bool phy_flash_cfg;
168*4882a593Smuzhiyun module_param(phy_flash_cfg, bool, 0644);
169*4882a593Smuzhiyun MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun static unsigned irq_adapt_low_thresh = 8000;
172*4882a593Smuzhiyun module_param(irq_adapt_low_thresh, uint, 0644);
173*4882a593Smuzhiyun MODULE_PARM_DESC(irq_adapt_low_thresh,
174*4882a593Smuzhiyun "Threshold score for reducing IRQ moderation");
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun static unsigned irq_adapt_high_thresh = 16000;
177*4882a593Smuzhiyun module_param(irq_adapt_high_thresh, uint, 0644);
178*4882a593Smuzhiyun MODULE_PARM_DESC(irq_adapt_high_thresh,
179*4882a593Smuzhiyun "Threshold score for increasing IRQ moderation");
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
182*4882a593Smuzhiyun NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
183*4882a593Smuzhiyun NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
184*4882a593Smuzhiyun NETIF_MSG_TX_ERR | NETIF_MSG_HW);
185*4882a593Smuzhiyun module_param(debug, uint, 0);
186*4882a593Smuzhiyun MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /**************************************************************************
189*4882a593Smuzhiyun *
190*4882a593Smuzhiyun * Utility functions and prototypes
191*4882a593Smuzhiyun *
192*4882a593Smuzhiyun *************************************************************************/
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun static int ef4_soft_enable_interrupts(struct ef4_nic *efx);
195*4882a593Smuzhiyun static void ef4_soft_disable_interrupts(struct ef4_nic *efx);
196*4882a593Smuzhiyun static void ef4_remove_channel(struct ef4_channel *channel);
197*4882a593Smuzhiyun static void ef4_remove_channels(struct ef4_nic *efx);
198*4882a593Smuzhiyun static const struct ef4_channel_type ef4_default_channel_type;
199*4882a593Smuzhiyun static void ef4_remove_port(struct ef4_nic *efx);
200*4882a593Smuzhiyun static void ef4_init_napi_channel(struct ef4_channel *channel);
201*4882a593Smuzhiyun static void ef4_fini_napi(struct ef4_nic *efx);
202*4882a593Smuzhiyun static void ef4_fini_napi_channel(struct ef4_channel *channel);
203*4882a593Smuzhiyun static void ef4_fini_struct(struct ef4_nic *efx);
204*4882a593Smuzhiyun static void ef4_start_all(struct ef4_nic *efx);
205*4882a593Smuzhiyun static void ef4_stop_all(struct ef4_nic *efx);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun #define EF4_ASSERT_RESET_SERIALISED(efx) \
208*4882a593Smuzhiyun do { \
209*4882a593Smuzhiyun if ((efx->state == STATE_READY) || \
210*4882a593Smuzhiyun (efx->state == STATE_RECOVERY) || \
211*4882a593Smuzhiyun (efx->state == STATE_DISABLED)) \
212*4882a593Smuzhiyun ASSERT_RTNL(); \
213*4882a593Smuzhiyun } while (0)
214*4882a593Smuzhiyun
ef4_check_disabled(struct ef4_nic * efx)215*4882a593Smuzhiyun static int ef4_check_disabled(struct ef4_nic *efx)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun if (efx->state == STATE_DISABLED || efx->state == STATE_RECOVERY) {
218*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev,
219*4882a593Smuzhiyun "device is disabled due to earlier errors\n");
220*4882a593Smuzhiyun return -EIO;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun return 0;
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun /**************************************************************************
226*4882a593Smuzhiyun *
227*4882a593Smuzhiyun * Event queue processing
228*4882a593Smuzhiyun *
229*4882a593Smuzhiyun *************************************************************************/
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /* Process channel's event queue
232*4882a593Smuzhiyun *
233*4882a593Smuzhiyun * This function is responsible for processing the event queue of a
234*4882a593Smuzhiyun * single channel. The caller must guarantee that this function will
235*4882a593Smuzhiyun * never be concurrently called more than once on the same channel,
236*4882a593Smuzhiyun * though different channels may be being processed concurrently.
237*4882a593Smuzhiyun */
ef4_process_channel(struct ef4_channel * channel,int budget)238*4882a593Smuzhiyun static int ef4_process_channel(struct ef4_channel *channel, int budget)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun struct ef4_tx_queue *tx_queue;
241*4882a593Smuzhiyun int spent;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun if (unlikely(!channel->enabled))
244*4882a593Smuzhiyun return 0;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun ef4_for_each_channel_tx_queue(tx_queue, channel) {
247*4882a593Smuzhiyun tx_queue->pkts_compl = 0;
248*4882a593Smuzhiyun tx_queue->bytes_compl = 0;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun spent = ef4_nic_process_eventq(channel, budget);
252*4882a593Smuzhiyun if (spent && ef4_channel_has_rx_queue(channel)) {
253*4882a593Smuzhiyun struct ef4_rx_queue *rx_queue =
254*4882a593Smuzhiyun ef4_channel_get_rx_queue(channel);
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun ef4_rx_flush_packet(channel);
257*4882a593Smuzhiyun ef4_fast_push_rx_descriptors(rx_queue, true);
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun /* Update BQL */
261*4882a593Smuzhiyun ef4_for_each_channel_tx_queue(tx_queue, channel) {
262*4882a593Smuzhiyun if (tx_queue->bytes_compl) {
263*4882a593Smuzhiyun netdev_tx_completed_queue(tx_queue->core_txq,
264*4882a593Smuzhiyun tx_queue->pkts_compl, tx_queue->bytes_compl);
265*4882a593Smuzhiyun }
266*4882a593Smuzhiyun }
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun return spent;
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun /* NAPI poll handler
272*4882a593Smuzhiyun *
273*4882a593Smuzhiyun * NAPI guarantees serialisation of polls of the same device, which
274*4882a593Smuzhiyun * provides the guarantee required by ef4_process_channel().
275*4882a593Smuzhiyun */
ef4_update_irq_mod(struct ef4_nic * efx,struct ef4_channel * channel)276*4882a593Smuzhiyun static void ef4_update_irq_mod(struct ef4_nic *efx, struct ef4_channel *channel)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun int step = efx->irq_mod_step_us;
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun if (channel->irq_mod_score < irq_adapt_low_thresh) {
281*4882a593Smuzhiyun if (channel->irq_moderation_us > step) {
282*4882a593Smuzhiyun channel->irq_moderation_us -= step;
283*4882a593Smuzhiyun efx->type->push_irq_moderation(channel);
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun } else if (channel->irq_mod_score > irq_adapt_high_thresh) {
286*4882a593Smuzhiyun if (channel->irq_moderation_us <
287*4882a593Smuzhiyun efx->irq_rx_moderation_us) {
288*4882a593Smuzhiyun channel->irq_moderation_us += step;
289*4882a593Smuzhiyun efx->type->push_irq_moderation(channel);
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun channel->irq_count = 0;
294*4882a593Smuzhiyun channel->irq_mod_score = 0;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun
ef4_poll(struct napi_struct * napi,int budget)297*4882a593Smuzhiyun static int ef4_poll(struct napi_struct *napi, int budget)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun struct ef4_channel *channel =
300*4882a593Smuzhiyun container_of(napi, struct ef4_channel, napi_str);
301*4882a593Smuzhiyun struct ef4_nic *efx = channel->efx;
302*4882a593Smuzhiyun int spent;
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun netif_vdbg(efx, intr, efx->net_dev,
305*4882a593Smuzhiyun "channel %d NAPI poll executing on CPU %d\n",
306*4882a593Smuzhiyun channel->channel, raw_smp_processor_id());
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun spent = ef4_process_channel(channel, budget);
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun if (spent < budget) {
311*4882a593Smuzhiyun if (ef4_channel_has_rx_queue(channel) &&
312*4882a593Smuzhiyun efx->irq_rx_adaptive &&
313*4882a593Smuzhiyun unlikely(++channel->irq_count == 1000)) {
314*4882a593Smuzhiyun ef4_update_irq_mod(efx, channel);
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun ef4_filter_rfs_expire(channel);
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun /* There is no race here; although napi_disable() will
320*4882a593Smuzhiyun * only wait for napi_complete(), this isn't a problem
321*4882a593Smuzhiyun * since ef4_nic_eventq_read_ack() will have no effect if
322*4882a593Smuzhiyun * interrupts have already been disabled.
323*4882a593Smuzhiyun */
324*4882a593Smuzhiyun napi_complete_done(napi, spent);
325*4882a593Smuzhiyun ef4_nic_eventq_read_ack(channel);
326*4882a593Smuzhiyun }
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun return spent;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun
331*4882a593Smuzhiyun /* Create event queue
332*4882a593Smuzhiyun * Event queue memory allocations are done only once. If the channel
333*4882a593Smuzhiyun * is reset, the memory buffer will be reused; this guards against
334*4882a593Smuzhiyun * errors during channel reset and also simplifies interrupt handling.
335*4882a593Smuzhiyun */
ef4_probe_eventq(struct ef4_channel * channel)336*4882a593Smuzhiyun static int ef4_probe_eventq(struct ef4_channel *channel)
337*4882a593Smuzhiyun {
338*4882a593Smuzhiyun struct ef4_nic *efx = channel->efx;
339*4882a593Smuzhiyun unsigned long entries;
340*4882a593Smuzhiyun
341*4882a593Smuzhiyun netif_dbg(efx, probe, efx->net_dev,
342*4882a593Smuzhiyun "chan %d create event queue\n", channel->channel);
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun /* Build an event queue with room for one event per tx and rx buffer,
345*4882a593Smuzhiyun * plus some extra for link state events and MCDI completions. */
346*4882a593Smuzhiyun entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
347*4882a593Smuzhiyun EF4_BUG_ON_PARANOID(entries > EF4_MAX_EVQ_SIZE);
348*4882a593Smuzhiyun channel->eventq_mask = max(entries, EF4_MIN_EVQ_SIZE) - 1;
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun return ef4_nic_probe_eventq(channel);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun /* Prepare channel's event queue */
ef4_init_eventq(struct ef4_channel * channel)354*4882a593Smuzhiyun static int ef4_init_eventq(struct ef4_channel *channel)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun struct ef4_nic *efx = channel->efx;
357*4882a593Smuzhiyun int rc;
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun EF4_WARN_ON_PARANOID(channel->eventq_init);
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev,
362*4882a593Smuzhiyun "chan %d init event queue\n", channel->channel);
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun rc = ef4_nic_init_eventq(channel);
365*4882a593Smuzhiyun if (rc == 0) {
366*4882a593Smuzhiyun efx->type->push_irq_moderation(channel);
367*4882a593Smuzhiyun channel->eventq_read_ptr = 0;
368*4882a593Smuzhiyun channel->eventq_init = true;
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun return rc;
371*4882a593Smuzhiyun }
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun /* Enable event queue processing and NAPI */
ef4_start_eventq(struct ef4_channel * channel)374*4882a593Smuzhiyun void ef4_start_eventq(struct ef4_channel *channel)
375*4882a593Smuzhiyun {
376*4882a593Smuzhiyun netif_dbg(channel->efx, ifup, channel->efx->net_dev,
377*4882a593Smuzhiyun "chan %d start event queue\n", channel->channel);
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun /* Make sure the NAPI handler sees the enabled flag set */
380*4882a593Smuzhiyun channel->enabled = true;
381*4882a593Smuzhiyun smp_wmb();
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun napi_enable(&channel->napi_str);
384*4882a593Smuzhiyun ef4_nic_eventq_read_ack(channel);
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun /* Disable event queue processing and NAPI */
ef4_stop_eventq(struct ef4_channel * channel)388*4882a593Smuzhiyun void ef4_stop_eventq(struct ef4_channel *channel)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun if (!channel->enabled)
391*4882a593Smuzhiyun return;
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun napi_disable(&channel->napi_str);
394*4882a593Smuzhiyun channel->enabled = false;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
ef4_fini_eventq(struct ef4_channel * channel)397*4882a593Smuzhiyun static void ef4_fini_eventq(struct ef4_channel *channel)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun if (!channel->eventq_init)
400*4882a593Smuzhiyun return;
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun netif_dbg(channel->efx, drv, channel->efx->net_dev,
403*4882a593Smuzhiyun "chan %d fini event queue\n", channel->channel);
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun ef4_nic_fini_eventq(channel);
406*4882a593Smuzhiyun channel->eventq_init = false;
407*4882a593Smuzhiyun }
408*4882a593Smuzhiyun
ef4_remove_eventq(struct ef4_channel * channel)409*4882a593Smuzhiyun static void ef4_remove_eventq(struct ef4_channel *channel)
410*4882a593Smuzhiyun {
411*4882a593Smuzhiyun netif_dbg(channel->efx, drv, channel->efx->net_dev,
412*4882a593Smuzhiyun "chan %d remove event queue\n", channel->channel);
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun ef4_nic_remove_eventq(channel);
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun /**************************************************************************
418*4882a593Smuzhiyun *
419*4882a593Smuzhiyun * Channel handling
420*4882a593Smuzhiyun *
421*4882a593Smuzhiyun *************************************************************************/
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun /* Allocate and initialise a channel structure. */
424*4882a593Smuzhiyun static struct ef4_channel *
ef4_alloc_channel(struct ef4_nic * efx,int i,struct ef4_channel * old_channel)425*4882a593Smuzhiyun ef4_alloc_channel(struct ef4_nic *efx, int i, struct ef4_channel *old_channel)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun struct ef4_channel *channel;
428*4882a593Smuzhiyun struct ef4_rx_queue *rx_queue;
429*4882a593Smuzhiyun struct ef4_tx_queue *tx_queue;
430*4882a593Smuzhiyun int j;
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun channel = kzalloc(sizeof(*channel), GFP_KERNEL);
433*4882a593Smuzhiyun if (!channel)
434*4882a593Smuzhiyun return NULL;
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun channel->efx = efx;
437*4882a593Smuzhiyun channel->channel = i;
438*4882a593Smuzhiyun channel->type = &ef4_default_channel_type;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun for (j = 0; j < EF4_TXQ_TYPES; j++) {
441*4882a593Smuzhiyun tx_queue = &channel->tx_queue[j];
442*4882a593Smuzhiyun tx_queue->efx = efx;
443*4882a593Smuzhiyun tx_queue->queue = i * EF4_TXQ_TYPES + j;
444*4882a593Smuzhiyun tx_queue->channel = channel;
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun rx_queue = &channel->rx_queue;
448*4882a593Smuzhiyun rx_queue->efx = efx;
449*4882a593Smuzhiyun timer_setup(&rx_queue->slow_fill, ef4_rx_slow_fill, 0);
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun return channel;
452*4882a593Smuzhiyun }
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun /* Allocate and initialise a channel structure, copying parameters
455*4882a593Smuzhiyun * (but not resources) from an old channel structure.
456*4882a593Smuzhiyun */
457*4882a593Smuzhiyun static struct ef4_channel *
ef4_copy_channel(const struct ef4_channel * old_channel)458*4882a593Smuzhiyun ef4_copy_channel(const struct ef4_channel *old_channel)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun struct ef4_channel *channel;
461*4882a593Smuzhiyun struct ef4_rx_queue *rx_queue;
462*4882a593Smuzhiyun struct ef4_tx_queue *tx_queue;
463*4882a593Smuzhiyun int j;
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun channel = kmalloc(sizeof(*channel), GFP_KERNEL);
466*4882a593Smuzhiyun if (!channel)
467*4882a593Smuzhiyun return NULL;
468*4882a593Smuzhiyun
469*4882a593Smuzhiyun *channel = *old_channel;
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun channel->napi_dev = NULL;
472*4882a593Smuzhiyun INIT_HLIST_NODE(&channel->napi_str.napi_hash_node);
473*4882a593Smuzhiyun channel->napi_str.napi_id = 0;
474*4882a593Smuzhiyun channel->napi_str.state = 0;
475*4882a593Smuzhiyun memset(&channel->eventq, 0, sizeof(channel->eventq));
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun for (j = 0; j < EF4_TXQ_TYPES; j++) {
478*4882a593Smuzhiyun tx_queue = &channel->tx_queue[j];
479*4882a593Smuzhiyun if (tx_queue->channel)
480*4882a593Smuzhiyun tx_queue->channel = channel;
481*4882a593Smuzhiyun tx_queue->buffer = NULL;
482*4882a593Smuzhiyun memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun rx_queue = &channel->rx_queue;
486*4882a593Smuzhiyun rx_queue->buffer = NULL;
487*4882a593Smuzhiyun memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
488*4882a593Smuzhiyun timer_setup(&rx_queue->slow_fill, ef4_rx_slow_fill, 0);
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun return channel;
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun
ef4_probe_channel(struct ef4_channel * channel)493*4882a593Smuzhiyun static int ef4_probe_channel(struct ef4_channel *channel)
494*4882a593Smuzhiyun {
495*4882a593Smuzhiyun struct ef4_tx_queue *tx_queue;
496*4882a593Smuzhiyun struct ef4_rx_queue *rx_queue;
497*4882a593Smuzhiyun int rc;
498*4882a593Smuzhiyun
499*4882a593Smuzhiyun netif_dbg(channel->efx, probe, channel->efx->net_dev,
500*4882a593Smuzhiyun "creating channel %d\n", channel->channel);
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun rc = channel->type->pre_probe(channel);
503*4882a593Smuzhiyun if (rc)
504*4882a593Smuzhiyun goto fail;
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun rc = ef4_probe_eventq(channel);
507*4882a593Smuzhiyun if (rc)
508*4882a593Smuzhiyun goto fail;
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun ef4_for_each_channel_tx_queue(tx_queue, channel) {
511*4882a593Smuzhiyun rc = ef4_probe_tx_queue(tx_queue);
512*4882a593Smuzhiyun if (rc)
513*4882a593Smuzhiyun goto fail;
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun ef4_for_each_channel_rx_queue(rx_queue, channel) {
517*4882a593Smuzhiyun rc = ef4_probe_rx_queue(rx_queue);
518*4882a593Smuzhiyun if (rc)
519*4882a593Smuzhiyun goto fail;
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun return 0;
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun fail:
525*4882a593Smuzhiyun ef4_remove_channel(channel);
526*4882a593Smuzhiyun return rc;
527*4882a593Smuzhiyun }
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun static void
ef4_get_channel_name(struct ef4_channel * channel,char * buf,size_t len)530*4882a593Smuzhiyun ef4_get_channel_name(struct ef4_channel *channel, char *buf, size_t len)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun struct ef4_nic *efx = channel->efx;
533*4882a593Smuzhiyun const char *type;
534*4882a593Smuzhiyun int number;
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun number = channel->channel;
537*4882a593Smuzhiyun if (efx->tx_channel_offset == 0) {
538*4882a593Smuzhiyun type = "";
539*4882a593Smuzhiyun } else if (channel->channel < efx->tx_channel_offset) {
540*4882a593Smuzhiyun type = "-rx";
541*4882a593Smuzhiyun } else {
542*4882a593Smuzhiyun type = "-tx";
543*4882a593Smuzhiyun number -= efx->tx_channel_offset;
544*4882a593Smuzhiyun }
545*4882a593Smuzhiyun snprintf(buf, len, "%s%s-%d", efx->name, type, number);
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun
ef4_set_channel_names(struct ef4_nic * efx)548*4882a593Smuzhiyun static void ef4_set_channel_names(struct ef4_nic *efx)
549*4882a593Smuzhiyun {
550*4882a593Smuzhiyun struct ef4_channel *channel;
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun ef4_for_each_channel(channel, efx)
553*4882a593Smuzhiyun channel->type->get_name(channel,
554*4882a593Smuzhiyun efx->msi_context[channel->channel].name,
555*4882a593Smuzhiyun sizeof(efx->msi_context[0].name));
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun
ef4_probe_channels(struct ef4_nic * efx)558*4882a593Smuzhiyun static int ef4_probe_channels(struct ef4_nic *efx)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun struct ef4_channel *channel;
561*4882a593Smuzhiyun int rc;
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun /* Restart special buffer allocation */
564*4882a593Smuzhiyun efx->next_buffer_table = 0;
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun /* Probe channels in reverse, so that any 'extra' channels
567*4882a593Smuzhiyun * use the start of the buffer table. This allows the traffic
568*4882a593Smuzhiyun * channels to be resized without moving them or wasting the
569*4882a593Smuzhiyun * entries before them.
570*4882a593Smuzhiyun */
571*4882a593Smuzhiyun ef4_for_each_channel_rev(channel, efx) {
572*4882a593Smuzhiyun rc = ef4_probe_channel(channel);
573*4882a593Smuzhiyun if (rc) {
574*4882a593Smuzhiyun netif_err(efx, probe, efx->net_dev,
575*4882a593Smuzhiyun "failed to create channel %d\n",
576*4882a593Smuzhiyun channel->channel);
577*4882a593Smuzhiyun goto fail;
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun ef4_set_channel_names(efx);
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun return 0;
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun fail:
585*4882a593Smuzhiyun ef4_remove_channels(efx);
586*4882a593Smuzhiyun return rc;
587*4882a593Smuzhiyun }
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun /* Channels are shutdown and reinitialised whilst the NIC is running
590*4882a593Smuzhiyun * to propagate configuration changes (mtu, checksum offload), or
591*4882a593Smuzhiyun * to clear hardware error conditions
592*4882a593Smuzhiyun */
ef4_start_datapath(struct ef4_nic * efx)593*4882a593Smuzhiyun static void ef4_start_datapath(struct ef4_nic *efx)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun netdev_features_t old_features = efx->net_dev->features;
596*4882a593Smuzhiyun bool old_rx_scatter = efx->rx_scatter;
597*4882a593Smuzhiyun struct ef4_tx_queue *tx_queue;
598*4882a593Smuzhiyun struct ef4_rx_queue *rx_queue;
599*4882a593Smuzhiyun struct ef4_channel *channel;
600*4882a593Smuzhiyun size_t rx_buf_len;
601*4882a593Smuzhiyun
602*4882a593Smuzhiyun /* Calculate the rx buffer allocation parameters required to
603*4882a593Smuzhiyun * support the current MTU, including padding for header
604*4882a593Smuzhiyun * alignment and overruns.
605*4882a593Smuzhiyun */
606*4882a593Smuzhiyun efx->rx_dma_len = (efx->rx_prefix_size +
607*4882a593Smuzhiyun EF4_MAX_FRAME_LEN(efx->net_dev->mtu) +
608*4882a593Smuzhiyun efx->type->rx_buffer_padding);
609*4882a593Smuzhiyun rx_buf_len = (sizeof(struct ef4_rx_page_state) +
610*4882a593Smuzhiyun efx->rx_ip_align + efx->rx_dma_len);
611*4882a593Smuzhiyun if (rx_buf_len <= PAGE_SIZE) {
612*4882a593Smuzhiyun efx->rx_scatter = efx->type->always_rx_scatter;
613*4882a593Smuzhiyun efx->rx_buffer_order = 0;
614*4882a593Smuzhiyun } else if (efx->type->can_rx_scatter) {
615*4882a593Smuzhiyun BUILD_BUG_ON(EF4_RX_USR_BUF_SIZE % L1_CACHE_BYTES);
616*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct ef4_rx_page_state) +
617*4882a593Smuzhiyun 2 * ALIGN(NET_IP_ALIGN + EF4_RX_USR_BUF_SIZE,
618*4882a593Smuzhiyun EF4_RX_BUF_ALIGNMENT) >
619*4882a593Smuzhiyun PAGE_SIZE);
620*4882a593Smuzhiyun efx->rx_scatter = true;
621*4882a593Smuzhiyun efx->rx_dma_len = EF4_RX_USR_BUF_SIZE;
622*4882a593Smuzhiyun efx->rx_buffer_order = 0;
623*4882a593Smuzhiyun } else {
624*4882a593Smuzhiyun efx->rx_scatter = false;
625*4882a593Smuzhiyun efx->rx_buffer_order = get_order(rx_buf_len);
626*4882a593Smuzhiyun }
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun ef4_rx_config_page_split(efx);
629*4882a593Smuzhiyun if (efx->rx_buffer_order)
630*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev,
631*4882a593Smuzhiyun "RX buf len=%u; page order=%u batch=%u\n",
632*4882a593Smuzhiyun efx->rx_dma_len, efx->rx_buffer_order,
633*4882a593Smuzhiyun efx->rx_pages_per_batch);
634*4882a593Smuzhiyun else
635*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev,
636*4882a593Smuzhiyun "RX buf len=%u step=%u bpp=%u; page batch=%u\n",
637*4882a593Smuzhiyun efx->rx_dma_len, efx->rx_page_buf_step,
638*4882a593Smuzhiyun efx->rx_bufs_per_page, efx->rx_pages_per_batch);
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun /* Restore previously fixed features in hw_features and remove
641*4882a593Smuzhiyun * features which are fixed now
642*4882a593Smuzhiyun */
643*4882a593Smuzhiyun efx->net_dev->hw_features |= efx->net_dev->features;
644*4882a593Smuzhiyun efx->net_dev->hw_features &= ~efx->fixed_features;
645*4882a593Smuzhiyun efx->net_dev->features |= efx->fixed_features;
646*4882a593Smuzhiyun if (efx->net_dev->features != old_features)
647*4882a593Smuzhiyun netdev_features_change(efx->net_dev);
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun /* RX filters may also have scatter-enabled flags */
650*4882a593Smuzhiyun if (efx->rx_scatter != old_rx_scatter)
651*4882a593Smuzhiyun efx->type->filter_update_rx_scatter(efx);
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun /* We must keep at least one descriptor in a TX ring empty.
654*4882a593Smuzhiyun * We could avoid this when the queue size does not exactly
655*4882a593Smuzhiyun * match the hardware ring size, but it's not that important.
656*4882a593Smuzhiyun * Therefore we stop the queue when one more skb might fill
657*4882a593Smuzhiyun * the ring completely. We wake it when half way back to
658*4882a593Smuzhiyun * empty.
659*4882a593Smuzhiyun */
660*4882a593Smuzhiyun efx->txq_stop_thresh = efx->txq_entries - ef4_tx_max_skb_descs(efx);
661*4882a593Smuzhiyun efx->txq_wake_thresh = efx->txq_stop_thresh / 2;
662*4882a593Smuzhiyun
663*4882a593Smuzhiyun /* Initialise the channels */
664*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
665*4882a593Smuzhiyun ef4_for_each_channel_tx_queue(tx_queue, channel) {
666*4882a593Smuzhiyun ef4_init_tx_queue(tx_queue);
667*4882a593Smuzhiyun atomic_inc(&efx->active_queues);
668*4882a593Smuzhiyun }
669*4882a593Smuzhiyun
670*4882a593Smuzhiyun ef4_for_each_channel_rx_queue(rx_queue, channel) {
671*4882a593Smuzhiyun ef4_init_rx_queue(rx_queue);
672*4882a593Smuzhiyun atomic_inc(&efx->active_queues);
673*4882a593Smuzhiyun ef4_stop_eventq(channel);
674*4882a593Smuzhiyun ef4_fast_push_rx_descriptors(rx_queue, false);
675*4882a593Smuzhiyun ef4_start_eventq(channel);
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun WARN_ON(channel->rx_pkt_n_frags);
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun if (netif_device_present(efx->net_dev))
682*4882a593Smuzhiyun netif_tx_wake_all_queues(efx->net_dev);
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun
ef4_stop_datapath(struct ef4_nic * efx)685*4882a593Smuzhiyun static void ef4_stop_datapath(struct ef4_nic *efx)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun struct ef4_channel *channel;
688*4882a593Smuzhiyun struct ef4_tx_queue *tx_queue;
689*4882a593Smuzhiyun struct ef4_rx_queue *rx_queue;
690*4882a593Smuzhiyun int rc;
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun EF4_ASSERT_RESET_SERIALISED(efx);
693*4882a593Smuzhiyun BUG_ON(efx->port_enabled);
694*4882a593Smuzhiyun
695*4882a593Smuzhiyun /* Stop RX refill */
696*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
697*4882a593Smuzhiyun ef4_for_each_channel_rx_queue(rx_queue, channel)
698*4882a593Smuzhiyun rx_queue->refill_enabled = false;
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun
701*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
702*4882a593Smuzhiyun /* RX packet processing is pipelined, so wait for the
703*4882a593Smuzhiyun * NAPI handler to complete. At least event queue 0
704*4882a593Smuzhiyun * might be kept active by non-data events, so don't
705*4882a593Smuzhiyun * use napi_synchronize() but actually disable NAPI
706*4882a593Smuzhiyun * temporarily.
707*4882a593Smuzhiyun */
708*4882a593Smuzhiyun if (ef4_channel_has_rx_queue(channel)) {
709*4882a593Smuzhiyun ef4_stop_eventq(channel);
710*4882a593Smuzhiyun ef4_start_eventq(channel);
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun rc = efx->type->fini_dmaq(efx);
715*4882a593Smuzhiyun if (rc && EF4_WORKAROUND_7803(efx)) {
716*4882a593Smuzhiyun /* Schedule a reset to recover from the flush failure. The
717*4882a593Smuzhiyun * descriptor caches reference memory we're about to free,
718*4882a593Smuzhiyun * but falcon_reconfigure_mac_wrapper() won't reconnect
719*4882a593Smuzhiyun * the MACs because of the pending reset.
720*4882a593Smuzhiyun */
721*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev,
722*4882a593Smuzhiyun "Resetting to recover from flush failure\n");
723*4882a593Smuzhiyun ef4_schedule_reset(efx, RESET_TYPE_ALL);
724*4882a593Smuzhiyun } else if (rc) {
725*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
726*4882a593Smuzhiyun } else {
727*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev,
728*4882a593Smuzhiyun "successfully flushed all queues\n");
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
732*4882a593Smuzhiyun ef4_for_each_channel_rx_queue(rx_queue, channel)
733*4882a593Smuzhiyun ef4_fini_rx_queue(rx_queue);
734*4882a593Smuzhiyun ef4_for_each_possible_channel_tx_queue(tx_queue, channel)
735*4882a593Smuzhiyun ef4_fini_tx_queue(tx_queue);
736*4882a593Smuzhiyun }
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun
ef4_remove_channel(struct ef4_channel * channel)739*4882a593Smuzhiyun static void ef4_remove_channel(struct ef4_channel *channel)
740*4882a593Smuzhiyun {
741*4882a593Smuzhiyun struct ef4_tx_queue *tx_queue;
742*4882a593Smuzhiyun struct ef4_rx_queue *rx_queue;
743*4882a593Smuzhiyun
744*4882a593Smuzhiyun netif_dbg(channel->efx, drv, channel->efx->net_dev,
745*4882a593Smuzhiyun "destroy chan %d\n", channel->channel);
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun ef4_for_each_channel_rx_queue(rx_queue, channel)
748*4882a593Smuzhiyun ef4_remove_rx_queue(rx_queue);
749*4882a593Smuzhiyun ef4_for_each_possible_channel_tx_queue(tx_queue, channel)
750*4882a593Smuzhiyun ef4_remove_tx_queue(tx_queue);
751*4882a593Smuzhiyun ef4_remove_eventq(channel);
752*4882a593Smuzhiyun channel->type->post_remove(channel);
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun
ef4_remove_channels(struct ef4_nic * efx)755*4882a593Smuzhiyun static void ef4_remove_channels(struct ef4_nic *efx)
756*4882a593Smuzhiyun {
757*4882a593Smuzhiyun struct ef4_channel *channel;
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun ef4_for_each_channel(channel, efx)
760*4882a593Smuzhiyun ef4_remove_channel(channel);
761*4882a593Smuzhiyun }
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun int
ef4_realloc_channels(struct ef4_nic * efx,u32 rxq_entries,u32 txq_entries)764*4882a593Smuzhiyun ef4_realloc_channels(struct ef4_nic *efx, u32 rxq_entries, u32 txq_entries)
765*4882a593Smuzhiyun {
766*4882a593Smuzhiyun struct ef4_channel *other_channel[EF4_MAX_CHANNELS], *channel;
767*4882a593Smuzhiyun u32 old_rxq_entries, old_txq_entries;
768*4882a593Smuzhiyun unsigned i, next_buffer_table = 0;
769*4882a593Smuzhiyun int rc, rc2;
770*4882a593Smuzhiyun
771*4882a593Smuzhiyun rc = ef4_check_disabled(efx);
772*4882a593Smuzhiyun if (rc)
773*4882a593Smuzhiyun return rc;
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun /* Not all channels should be reallocated. We must avoid
776*4882a593Smuzhiyun * reallocating their buffer table entries.
777*4882a593Smuzhiyun */
778*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
779*4882a593Smuzhiyun struct ef4_rx_queue *rx_queue;
780*4882a593Smuzhiyun struct ef4_tx_queue *tx_queue;
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun if (channel->type->copy)
783*4882a593Smuzhiyun continue;
784*4882a593Smuzhiyun next_buffer_table = max(next_buffer_table,
785*4882a593Smuzhiyun channel->eventq.index +
786*4882a593Smuzhiyun channel->eventq.entries);
787*4882a593Smuzhiyun ef4_for_each_channel_rx_queue(rx_queue, channel)
788*4882a593Smuzhiyun next_buffer_table = max(next_buffer_table,
789*4882a593Smuzhiyun rx_queue->rxd.index +
790*4882a593Smuzhiyun rx_queue->rxd.entries);
791*4882a593Smuzhiyun ef4_for_each_channel_tx_queue(tx_queue, channel)
792*4882a593Smuzhiyun next_buffer_table = max(next_buffer_table,
793*4882a593Smuzhiyun tx_queue->txd.index +
794*4882a593Smuzhiyun tx_queue->txd.entries);
795*4882a593Smuzhiyun }
796*4882a593Smuzhiyun
797*4882a593Smuzhiyun ef4_device_detach_sync(efx);
798*4882a593Smuzhiyun ef4_stop_all(efx);
799*4882a593Smuzhiyun ef4_soft_disable_interrupts(efx);
800*4882a593Smuzhiyun
801*4882a593Smuzhiyun /* Clone channels (where possible) */
802*4882a593Smuzhiyun memset(other_channel, 0, sizeof(other_channel));
803*4882a593Smuzhiyun for (i = 0; i < efx->n_channels; i++) {
804*4882a593Smuzhiyun channel = efx->channel[i];
805*4882a593Smuzhiyun if (channel->type->copy)
806*4882a593Smuzhiyun channel = channel->type->copy(channel);
807*4882a593Smuzhiyun if (!channel) {
808*4882a593Smuzhiyun rc = -ENOMEM;
809*4882a593Smuzhiyun goto out;
810*4882a593Smuzhiyun }
811*4882a593Smuzhiyun other_channel[i] = channel;
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun /* Swap entry counts and channel pointers */
815*4882a593Smuzhiyun old_rxq_entries = efx->rxq_entries;
816*4882a593Smuzhiyun old_txq_entries = efx->txq_entries;
817*4882a593Smuzhiyun efx->rxq_entries = rxq_entries;
818*4882a593Smuzhiyun efx->txq_entries = txq_entries;
819*4882a593Smuzhiyun for (i = 0; i < efx->n_channels; i++) {
820*4882a593Smuzhiyun channel = efx->channel[i];
821*4882a593Smuzhiyun efx->channel[i] = other_channel[i];
822*4882a593Smuzhiyun other_channel[i] = channel;
823*4882a593Smuzhiyun }
824*4882a593Smuzhiyun
825*4882a593Smuzhiyun /* Restart buffer table allocation */
826*4882a593Smuzhiyun efx->next_buffer_table = next_buffer_table;
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun for (i = 0; i < efx->n_channels; i++) {
829*4882a593Smuzhiyun channel = efx->channel[i];
830*4882a593Smuzhiyun if (!channel->type->copy)
831*4882a593Smuzhiyun continue;
832*4882a593Smuzhiyun rc = ef4_probe_channel(channel);
833*4882a593Smuzhiyun if (rc)
834*4882a593Smuzhiyun goto rollback;
835*4882a593Smuzhiyun ef4_init_napi_channel(efx->channel[i]);
836*4882a593Smuzhiyun }
837*4882a593Smuzhiyun
838*4882a593Smuzhiyun out:
839*4882a593Smuzhiyun /* Destroy unused channel structures */
840*4882a593Smuzhiyun for (i = 0; i < efx->n_channels; i++) {
841*4882a593Smuzhiyun channel = other_channel[i];
842*4882a593Smuzhiyun if (channel && channel->type->copy) {
843*4882a593Smuzhiyun ef4_fini_napi_channel(channel);
844*4882a593Smuzhiyun ef4_remove_channel(channel);
845*4882a593Smuzhiyun kfree(channel);
846*4882a593Smuzhiyun }
847*4882a593Smuzhiyun }
848*4882a593Smuzhiyun
849*4882a593Smuzhiyun rc2 = ef4_soft_enable_interrupts(efx);
850*4882a593Smuzhiyun if (rc2) {
851*4882a593Smuzhiyun rc = rc ? rc : rc2;
852*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev,
853*4882a593Smuzhiyun "unable to restart interrupts on channel reallocation\n");
854*4882a593Smuzhiyun ef4_schedule_reset(efx, RESET_TYPE_DISABLE);
855*4882a593Smuzhiyun } else {
856*4882a593Smuzhiyun ef4_start_all(efx);
857*4882a593Smuzhiyun netif_device_attach(efx->net_dev);
858*4882a593Smuzhiyun }
859*4882a593Smuzhiyun return rc;
860*4882a593Smuzhiyun
861*4882a593Smuzhiyun rollback:
862*4882a593Smuzhiyun /* Swap back */
863*4882a593Smuzhiyun efx->rxq_entries = old_rxq_entries;
864*4882a593Smuzhiyun efx->txq_entries = old_txq_entries;
865*4882a593Smuzhiyun for (i = 0; i < efx->n_channels; i++) {
866*4882a593Smuzhiyun channel = efx->channel[i];
867*4882a593Smuzhiyun efx->channel[i] = other_channel[i];
868*4882a593Smuzhiyun other_channel[i] = channel;
869*4882a593Smuzhiyun }
870*4882a593Smuzhiyun goto out;
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun
ef4_schedule_slow_fill(struct ef4_rx_queue * rx_queue)873*4882a593Smuzhiyun void ef4_schedule_slow_fill(struct ef4_rx_queue *rx_queue)
874*4882a593Smuzhiyun {
875*4882a593Smuzhiyun mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
876*4882a593Smuzhiyun }
877*4882a593Smuzhiyun
878*4882a593Smuzhiyun static const struct ef4_channel_type ef4_default_channel_type = {
879*4882a593Smuzhiyun .pre_probe = ef4_channel_dummy_op_int,
880*4882a593Smuzhiyun .post_remove = ef4_channel_dummy_op_void,
881*4882a593Smuzhiyun .get_name = ef4_get_channel_name,
882*4882a593Smuzhiyun .copy = ef4_copy_channel,
883*4882a593Smuzhiyun .keep_eventq = false,
884*4882a593Smuzhiyun };
885*4882a593Smuzhiyun
ef4_channel_dummy_op_int(struct ef4_channel * channel)886*4882a593Smuzhiyun int ef4_channel_dummy_op_int(struct ef4_channel *channel)
887*4882a593Smuzhiyun {
888*4882a593Smuzhiyun return 0;
889*4882a593Smuzhiyun }
890*4882a593Smuzhiyun
ef4_channel_dummy_op_void(struct ef4_channel * channel)891*4882a593Smuzhiyun void ef4_channel_dummy_op_void(struct ef4_channel *channel)
892*4882a593Smuzhiyun {
893*4882a593Smuzhiyun }
894*4882a593Smuzhiyun
895*4882a593Smuzhiyun /**************************************************************************
896*4882a593Smuzhiyun *
897*4882a593Smuzhiyun * Port handling
898*4882a593Smuzhiyun *
899*4882a593Smuzhiyun **************************************************************************/
900*4882a593Smuzhiyun
901*4882a593Smuzhiyun /* This ensures that the kernel is kept informed (via
902*4882a593Smuzhiyun * netif_carrier_on/off) of the link status, and also maintains the
903*4882a593Smuzhiyun * link status's stop on the port's TX queue.
904*4882a593Smuzhiyun */
ef4_link_status_changed(struct ef4_nic * efx)905*4882a593Smuzhiyun void ef4_link_status_changed(struct ef4_nic *efx)
906*4882a593Smuzhiyun {
907*4882a593Smuzhiyun struct ef4_link_state *link_state = &efx->link_state;
908*4882a593Smuzhiyun
909*4882a593Smuzhiyun /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
910*4882a593Smuzhiyun * that no events are triggered between unregister_netdev() and the
911*4882a593Smuzhiyun * driver unloading. A more general condition is that NETDEV_CHANGE
912*4882a593Smuzhiyun * can only be generated between NETDEV_UP and NETDEV_DOWN */
913*4882a593Smuzhiyun if (!netif_running(efx->net_dev))
914*4882a593Smuzhiyun return;
915*4882a593Smuzhiyun
916*4882a593Smuzhiyun if (link_state->up != netif_carrier_ok(efx->net_dev)) {
917*4882a593Smuzhiyun efx->n_link_state_changes++;
918*4882a593Smuzhiyun
919*4882a593Smuzhiyun if (link_state->up)
920*4882a593Smuzhiyun netif_carrier_on(efx->net_dev);
921*4882a593Smuzhiyun else
922*4882a593Smuzhiyun netif_carrier_off(efx->net_dev);
923*4882a593Smuzhiyun }
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun /* Status message for kernel log */
926*4882a593Smuzhiyun if (link_state->up)
927*4882a593Smuzhiyun netif_info(efx, link, efx->net_dev,
928*4882a593Smuzhiyun "link up at %uMbps %s-duplex (MTU %d)\n",
929*4882a593Smuzhiyun link_state->speed, link_state->fd ? "full" : "half",
930*4882a593Smuzhiyun efx->net_dev->mtu);
931*4882a593Smuzhiyun else
932*4882a593Smuzhiyun netif_info(efx, link, efx->net_dev, "link down\n");
933*4882a593Smuzhiyun }
934*4882a593Smuzhiyun
ef4_link_set_advertising(struct ef4_nic * efx,u32 advertising)935*4882a593Smuzhiyun void ef4_link_set_advertising(struct ef4_nic *efx, u32 advertising)
936*4882a593Smuzhiyun {
937*4882a593Smuzhiyun efx->link_advertising = advertising;
938*4882a593Smuzhiyun if (advertising) {
939*4882a593Smuzhiyun if (advertising & ADVERTISED_Pause)
940*4882a593Smuzhiyun efx->wanted_fc |= (EF4_FC_TX | EF4_FC_RX);
941*4882a593Smuzhiyun else
942*4882a593Smuzhiyun efx->wanted_fc &= ~(EF4_FC_TX | EF4_FC_RX);
943*4882a593Smuzhiyun if (advertising & ADVERTISED_Asym_Pause)
944*4882a593Smuzhiyun efx->wanted_fc ^= EF4_FC_TX;
945*4882a593Smuzhiyun }
946*4882a593Smuzhiyun }
947*4882a593Smuzhiyun
ef4_link_set_wanted_fc(struct ef4_nic * efx,u8 wanted_fc)948*4882a593Smuzhiyun void ef4_link_set_wanted_fc(struct ef4_nic *efx, u8 wanted_fc)
949*4882a593Smuzhiyun {
950*4882a593Smuzhiyun efx->wanted_fc = wanted_fc;
951*4882a593Smuzhiyun if (efx->link_advertising) {
952*4882a593Smuzhiyun if (wanted_fc & EF4_FC_RX)
953*4882a593Smuzhiyun efx->link_advertising |= (ADVERTISED_Pause |
954*4882a593Smuzhiyun ADVERTISED_Asym_Pause);
955*4882a593Smuzhiyun else
956*4882a593Smuzhiyun efx->link_advertising &= ~(ADVERTISED_Pause |
957*4882a593Smuzhiyun ADVERTISED_Asym_Pause);
958*4882a593Smuzhiyun if (wanted_fc & EF4_FC_TX)
959*4882a593Smuzhiyun efx->link_advertising ^= ADVERTISED_Asym_Pause;
960*4882a593Smuzhiyun }
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun static void ef4_fini_port(struct ef4_nic *efx);
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun /* We assume that efx->type->reconfigure_mac will always try to sync RX
966*4882a593Smuzhiyun * filters and therefore needs to read-lock the filter table against freeing
967*4882a593Smuzhiyun */
ef4_mac_reconfigure(struct ef4_nic * efx)968*4882a593Smuzhiyun void ef4_mac_reconfigure(struct ef4_nic *efx)
969*4882a593Smuzhiyun {
970*4882a593Smuzhiyun down_read(&efx->filter_sem);
971*4882a593Smuzhiyun efx->type->reconfigure_mac(efx);
972*4882a593Smuzhiyun up_read(&efx->filter_sem);
973*4882a593Smuzhiyun }
974*4882a593Smuzhiyun
975*4882a593Smuzhiyun /* Push loopback/power/transmit disable settings to the PHY, and reconfigure
976*4882a593Smuzhiyun * the MAC appropriately. All other PHY configuration changes are pushed
977*4882a593Smuzhiyun * through phy_op->set_link_ksettings(), and pushed asynchronously to the MAC
978*4882a593Smuzhiyun * through ef4_monitor().
979*4882a593Smuzhiyun *
980*4882a593Smuzhiyun * Callers must hold the mac_lock
981*4882a593Smuzhiyun */
__ef4_reconfigure_port(struct ef4_nic * efx)982*4882a593Smuzhiyun int __ef4_reconfigure_port(struct ef4_nic *efx)
983*4882a593Smuzhiyun {
984*4882a593Smuzhiyun enum ef4_phy_mode phy_mode;
985*4882a593Smuzhiyun int rc;
986*4882a593Smuzhiyun
987*4882a593Smuzhiyun WARN_ON(!mutex_is_locked(&efx->mac_lock));
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun /* Disable PHY transmit in mac level loopbacks */
990*4882a593Smuzhiyun phy_mode = efx->phy_mode;
991*4882a593Smuzhiyun if (LOOPBACK_INTERNAL(efx))
992*4882a593Smuzhiyun efx->phy_mode |= PHY_MODE_TX_DISABLED;
993*4882a593Smuzhiyun else
994*4882a593Smuzhiyun efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
995*4882a593Smuzhiyun
996*4882a593Smuzhiyun rc = efx->type->reconfigure_port(efx);
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun if (rc)
999*4882a593Smuzhiyun efx->phy_mode = phy_mode;
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun return rc;
1002*4882a593Smuzhiyun }
1003*4882a593Smuzhiyun
1004*4882a593Smuzhiyun /* Reinitialise the MAC to pick up new PHY settings, even if the port is
1005*4882a593Smuzhiyun * disabled. */
ef4_reconfigure_port(struct ef4_nic * efx)1006*4882a593Smuzhiyun int ef4_reconfigure_port(struct ef4_nic *efx)
1007*4882a593Smuzhiyun {
1008*4882a593Smuzhiyun int rc;
1009*4882a593Smuzhiyun
1010*4882a593Smuzhiyun EF4_ASSERT_RESET_SERIALISED(efx);
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun mutex_lock(&efx->mac_lock);
1013*4882a593Smuzhiyun rc = __ef4_reconfigure_port(efx);
1014*4882a593Smuzhiyun mutex_unlock(&efx->mac_lock);
1015*4882a593Smuzhiyun
1016*4882a593Smuzhiyun return rc;
1017*4882a593Smuzhiyun }
1018*4882a593Smuzhiyun
1019*4882a593Smuzhiyun /* Asynchronous work item for changing MAC promiscuity and multicast
1020*4882a593Smuzhiyun * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
1021*4882a593Smuzhiyun * MAC directly. */
ef4_mac_work(struct work_struct * data)1022*4882a593Smuzhiyun static void ef4_mac_work(struct work_struct *data)
1023*4882a593Smuzhiyun {
1024*4882a593Smuzhiyun struct ef4_nic *efx = container_of(data, struct ef4_nic, mac_work);
1025*4882a593Smuzhiyun
1026*4882a593Smuzhiyun mutex_lock(&efx->mac_lock);
1027*4882a593Smuzhiyun if (efx->port_enabled)
1028*4882a593Smuzhiyun ef4_mac_reconfigure(efx);
1029*4882a593Smuzhiyun mutex_unlock(&efx->mac_lock);
1030*4882a593Smuzhiyun }
1031*4882a593Smuzhiyun
ef4_probe_port(struct ef4_nic * efx)1032*4882a593Smuzhiyun static int ef4_probe_port(struct ef4_nic *efx)
1033*4882a593Smuzhiyun {
1034*4882a593Smuzhiyun int rc;
1035*4882a593Smuzhiyun
1036*4882a593Smuzhiyun netif_dbg(efx, probe, efx->net_dev, "create port\n");
1037*4882a593Smuzhiyun
1038*4882a593Smuzhiyun if (phy_flash_cfg)
1039*4882a593Smuzhiyun efx->phy_mode = PHY_MODE_SPECIAL;
1040*4882a593Smuzhiyun
1041*4882a593Smuzhiyun /* Connect up MAC/PHY operations table */
1042*4882a593Smuzhiyun rc = efx->type->probe_port(efx);
1043*4882a593Smuzhiyun if (rc)
1044*4882a593Smuzhiyun return rc;
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun /* Initialise MAC address to permanent address */
1047*4882a593Smuzhiyun ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
1048*4882a593Smuzhiyun
1049*4882a593Smuzhiyun return 0;
1050*4882a593Smuzhiyun }
1051*4882a593Smuzhiyun
ef4_init_port(struct ef4_nic * efx)1052*4882a593Smuzhiyun static int ef4_init_port(struct ef4_nic *efx)
1053*4882a593Smuzhiyun {
1054*4882a593Smuzhiyun int rc;
1055*4882a593Smuzhiyun
1056*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev, "init port\n");
1057*4882a593Smuzhiyun
1058*4882a593Smuzhiyun mutex_lock(&efx->mac_lock);
1059*4882a593Smuzhiyun
1060*4882a593Smuzhiyun rc = efx->phy_op->init(efx);
1061*4882a593Smuzhiyun if (rc)
1062*4882a593Smuzhiyun goto fail1;
1063*4882a593Smuzhiyun
1064*4882a593Smuzhiyun efx->port_initialized = true;
1065*4882a593Smuzhiyun
1066*4882a593Smuzhiyun /* Reconfigure the MAC before creating dma queues (required for
1067*4882a593Smuzhiyun * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
1068*4882a593Smuzhiyun ef4_mac_reconfigure(efx);
1069*4882a593Smuzhiyun
1070*4882a593Smuzhiyun /* Ensure the PHY advertises the correct flow control settings */
1071*4882a593Smuzhiyun rc = efx->phy_op->reconfigure(efx);
1072*4882a593Smuzhiyun if (rc && rc != -EPERM)
1073*4882a593Smuzhiyun goto fail2;
1074*4882a593Smuzhiyun
1075*4882a593Smuzhiyun mutex_unlock(&efx->mac_lock);
1076*4882a593Smuzhiyun return 0;
1077*4882a593Smuzhiyun
1078*4882a593Smuzhiyun fail2:
1079*4882a593Smuzhiyun efx->phy_op->fini(efx);
1080*4882a593Smuzhiyun fail1:
1081*4882a593Smuzhiyun mutex_unlock(&efx->mac_lock);
1082*4882a593Smuzhiyun return rc;
1083*4882a593Smuzhiyun }
1084*4882a593Smuzhiyun
ef4_start_port(struct ef4_nic * efx)1085*4882a593Smuzhiyun static void ef4_start_port(struct ef4_nic *efx)
1086*4882a593Smuzhiyun {
1087*4882a593Smuzhiyun netif_dbg(efx, ifup, efx->net_dev, "start port\n");
1088*4882a593Smuzhiyun BUG_ON(efx->port_enabled);
1089*4882a593Smuzhiyun
1090*4882a593Smuzhiyun mutex_lock(&efx->mac_lock);
1091*4882a593Smuzhiyun efx->port_enabled = true;
1092*4882a593Smuzhiyun
1093*4882a593Smuzhiyun /* Ensure MAC ingress/egress is enabled */
1094*4882a593Smuzhiyun ef4_mac_reconfigure(efx);
1095*4882a593Smuzhiyun
1096*4882a593Smuzhiyun mutex_unlock(&efx->mac_lock);
1097*4882a593Smuzhiyun }
1098*4882a593Smuzhiyun
1099*4882a593Smuzhiyun /* Cancel work for MAC reconfiguration, periodic hardware monitoring
1100*4882a593Smuzhiyun * and the async self-test, wait for them to finish and prevent them
1101*4882a593Smuzhiyun * being scheduled again. This doesn't cover online resets, which
1102*4882a593Smuzhiyun * should only be cancelled when removing the device.
1103*4882a593Smuzhiyun */
ef4_stop_port(struct ef4_nic * efx)1104*4882a593Smuzhiyun static void ef4_stop_port(struct ef4_nic *efx)
1105*4882a593Smuzhiyun {
1106*4882a593Smuzhiyun netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
1107*4882a593Smuzhiyun
1108*4882a593Smuzhiyun EF4_ASSERT_RESET_SERIALISED(efx);
1109*4882a593Smuzhiyun
1110*4882a593Smuzhiyun mutex_lock(&efx->mac_lock);
1111*4882a593Smuzhiyun efx->port_enabled = false;
1112*4882a593Smuzhiyun mutex_unlock(&efx->mac_lock);
1113*4882a593Smuzhiyun
1114*4882a593Smuzhiyun /* Serialise against ef4_set_multicast_list() */
1115*4882a593Smuzhiyun netif_addr_lock_bh(efx->net_dev);
1116*4882a593Smuzhiyun netif_addr_unlock_bh(efx->net_dev);
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun cancel_delayed_work_sync(&efx->monitor_work);
1119*4882a593Smuzhiyun ef4_selftest_async_cancel(efx);
1120*4882a593Smuzhiyun cancel_work_sync(&efx->mac_work);
1121*4882a593Smuzhiyun }
1122*4882a593Smuzhiyun
ef4_fini_port(struct ef4_nic * efx)1123*4882a593Smuzhiyun static void ef4_fini_port(struct ef4_nic *efx)
1124*4882a593Smuzhiyun {
1125*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
1126*4882a593Smuzhiyun
1127*4882a593Smuzhiyun if (!efx->port_initialized)
1128*4882a593Smuzhiyun return;
1129*4882a593Smuzhiyun
1130*4882a593Smuzhiyun efx->phy_op->fini(efx);
1131*4882a593Smuzhiyun efx->port_initialized = false;
1132*4882a593Smuzhiyun
1133*4882a593Smuzhiyun efx->link_state.up = false;
1134*4882a593Smuzhiyun ef4_link_status_changed(efx);
1135*4882a593Smuzhiyun }
1136*4882a593Smuzhiyun
ef4_remove_port(struct ef4_nic * efx)1137*4882a593Smuzhiyun static void ef4_remove_port(struct ef4_nic *efx)
1138*4882a593Smuzhiyun {
1139*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
1140*4882a593Smuzhiyun
1141*4882a593Smuzhiyun efx->type->remove_port(efx);
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun /**************************************************************************
1145*4882a593Smuzhiyun *
1146*4882a593Smuzhiyun * NIC handling
1147*4882a593Smuzhiyun *
1148*4882a593Smuzhiyun **************************************************************************/
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun static LIST_HEAD(ef4_primary_list);
1151*4882a593Smuzhiyun static LIST_HEAD(ef4_unassociated_list);
1152*4882a593Smuzhiyun
ef4_same_controller(struct ef4_nic * left,struct ef4_nic * right)1153*4882a593Smuzhiyun static bool ef4_same_controller(struct ef4_nic *left, struct ef4_nic *right)
1154*4882a593Smuzhiyun {
1155*4882a593Smuzhiyun return left->type == right->type &&
1156*4882a593Smuzhiyun left->vpd_sn && right->vpd_sn &&
1157*4882a593Smuzhiyun !strcmp(left->vpd_sn, right->vpd_sn);
1158*4882a593Smuzhiyun }
1159*4882a593Smuzhiyun
ef4_associate(struct ef4_nic * efx)1160*4882a593Smuzhiyun static void ef4_associate(struct ef4_nic *efx)
1161*4882a593Smuzhiyun {
1162*4882a593Smuzhiyun struct ef4_nic *other, *next;
1163*4882a593Smuzhiyun
1164*4882a593Smuzhiyun if (efx->primary == efx) {
1165*4882a593Smuzhiyun /* Adding primary function; look for secondaries */
1166*4882a593Smuzhiyun
1167*4882a593Smuzhiyun netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
1168*4882a593Smuzhiyun list_add_tail(&efx->node, &ef4_primary_list);
1169*4882a593Smuzhiyun
1170*4882a593Smuzhiyun list_for_each_entry_safe(other, next, &ef4_unassociated_list,
1171*4882a593Smuzhiyun node) {
1172*4882a593Smuzhiyun if (ef4_same_controller(efx, other)) {
1173*4882a593Smuzhiyun list_del(&other->node);
1174*4882a593Smuzhiyun netif_dbg(other, probe, other->net_dev,
1175*4882a593Smuzhiyun "moving to secondary list of %s %s\n",
1176*4882a593Smuzhiyun pci_name(efx->pci_dev),
1177*4882a593Smuzhiyun efx->net_dev->name);
1178*4882a593Smuzhiyun list_add_tail(&other->node,
1179*4882a593Smuzhiyun &efx->secondary_list);
1180*4882a593Smuzhiyun other->primary = efx;
1181*4882a593Smuzhiyun }
1182*4882a593Smuzhiyun }
1183*4882a593Smuzhiyun } else {
1184*4882a593Smuzhiyun /* Adding secondary function; look for primary */
1185*4882a593Smuzhiyun
1186*4882a593Smuzhiyun list_for_each_entry(other, &ef4_primary_list, node) {
1187*4882a593Smuzhiyun if (ef4_same_controller(efx, other)) {
1188*4882a593Smuzhiyun netif_dbg(efx, probe, efx->net_dev,
1189*4882a593Smuzhiyun "adding to secondary list of %s %s\n",
1190*4882a593Smuzhiyun pci_name(other->pci_dev),
1191*4882a593Smuzhiyun other->net_dev->name);
1192*4882a593Smuzhiyun list_add_tail(&efx->node,
1193*4882a593Smuzhiyun &other->secondary_list);
1194*4882a593Smuzhiyun efx->primary = other;
1195*4882a593Smuzhiyun return;
1196*4882a593Smuzhiyun }
1197*4882a593Smuzhiyun }
1198*4882a593Smuzhiyun
1199*4882a593Smuzhiyun netif_dbg(efx, probe, efx->net_dev,
1200*4882a593Smuzhiyun "adding to unassociated list\n");
1201*4882a593Smuzhiyun list_add_tail(&efx->node, &ef4_unassociated_list);
1202*4882a593Smuzhiyun }
1203*4882a593Smuzhiyun }
1204*4882a593Smuzhiyun
ef4_dissociate(struct ef4_nic * efx)1205*4882a593Smuzhiyun static void ef4_dissociate(struct ef4_nic *efx)
1206*4882a593Smuzhiyun {
1207*4882a593Smuzhiyun struct ef4_nic *other, *next;
1208*4882a593Smuzhiyun
1209*4882a593Smuzhiyun list_del(&efx->node);
1210*4882a593Smuzhiyun efx->primary = NULL;
1211*4882a593Smuzhiyun
1212*4882a593Smuzhiyun list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
1213*4882a593Smuzhiyun list_del(&other->node);
1214*4882a593Smuzhiyun netif_dbg(other, probe, other->net_dev,
1215*4882a593Smuzhiyun "moving to unassociated list\n");
1216*4882a593Smuzhiyun list_add_tail(&other->node, &ef4_unassociated_list);
1217*4882a593Smuzhiyun other->primary = NULL;
1218*4882a593Smuzhiyun }
1219*4882a593Smuzhiyun }
1220*4882a593Smuzhiyun
1221*4882a593Smuzhiyun /* This configures the PCI device to enable I/O and DMA. */
ef4_init_io(struct ef4_nic * efx)1222*4882a593Smuzhiyun static int ef4_init_io(struct ef4_nic *efx)
1223*4882a593Smuzhiyun {
1224*4882a593Smuzhiyun struct pci_dev *pci_dev = efx->pci_dev;
1225*4882a593Smuzhiyun dma_addr_t dma_mask = efx->type->max_dma_mask;
1226*4882a593Smuzhiyun unsigned int mem_map_size = efx->type->mem_map_size(efx);
1227*4882a593Smuzhiyun int rc, bar;
1228*4882a593Smuzhiyun
1229*4882a593Smuzhiyun netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
1230*4882a593Smuzhiyun
1231*4882a593Smuzhiyun bar = efx->type->mem_bar;
1232*4882a593Smuzhiyun
1233*4882a593Smuzhiyun rc = pci_enable_device(pci_dev);
1234*4882a593Smuzhiyun if (rc) {
1235*4882a593Smuzhiyun netif_err(efx, probe, efx->net_dev,
1236*4882a593Smuzhiyun "failed to enable PCI device\n");
1237*4882a593Smuzhiyun goto fail1;
1238*4882a593Smuzhiyun }
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyun pci_set_master(pci_dev);
1241*4882a593Smuzhiyun
1242*4882a593Smuzhiyun /* Set the PCI DMA mask. Try all possibilities from our genuine mask
1243*4882a593Smuzhiyun * down to 32 bits, because some architectures will allow 40 bit
1244*4882a593Smuzhiyun * masks event though they reject 46 bit masks.
1245*4882a593Smuzhiyun */
1246*4882a593Smuzhiyun while (dma_mask > 0x7fffffffUL) {
1247*4882a593Smuzhiyun rc = dma_set_mask_and_coherent(&pci_dev->dev, dma_mask);
1248*4882a593Smuzhiyun if (rc == 0)
1249*4882a593Smuzhiyun break;
1250*4882a593Smuzhiyun dma_mask >>= 1;
1251*4882a593Smuzhiyun }
1252*4882a593Smuzhiyun if (rc) {
1253*4882a593Smuzhiyun netif_err(efx, probe, efx->net_dev,
1254*4882a593Smuzhiyun "could not find a suitable DMA mask\n");
1255*4882a593Smuzhiyun goto fail2;
1256*4882a593Smuzhiyun }
1257*4882a593Smuzhiyun netif_dbg(efx, probe, efx->net_dev,
1258*4882a593Smuzhiyun "using DMA mask %llx\n", (unsigned long long) dma_mask);
1259*4882a593Smuzhiyun
1260*4882a593Smuzhiyun efx->membase_phys = pci_resource_start(efx->pci_dev, bar);
1261*4882a593Smuzhiyun rc = pci_request_region(pci_dev, bar, "sfc");
1262*4882a593Smuzhiyun if (rc) {
1263*4882a593Smuzhiyun netif_err(efx, probe, efx->net_dev,
1264*4882a593Smuzhiyun "request for memory BAR failed\n");
1265*4882a593Smuzhiyun rc = -EIO;
1266*4882a593Smuzhiyun goto fail3;
1267*4882a593Smuzhiyun }
1268*4882a593Smuzhiyun efx->membase = ioremap(efx->membase_phys, mem_map_size);
1269*4882a593Smuzhiyun if (!efx->membase) {
1270*4882a593Smuzhiyun netif_err(efx, probe, efx->net_dev,
1271*4882a593Smuzhiyun "could not map memory BAR at %llx+%x\n",
1272*4882a593Smuzhiyun (unsigned long long)efx->membase_phys, mem_map_size);
1273*4882a593Smuzhiyun rc = -ENOMEM;
1274*4882a593Smuzhiyun goto fail4;
1275*4882a593Smuzhiyun }
1276*4882a593Smuzhiyun netif_dbg(efx, probe, efx->net_dev,
1277*4882a593Smuzhiyun "memory BAR at %llx+%x (virtual %p)\n",
1278*4882a593Smuzhiyun (unsigned long long)efx->membase_phys, mem_map_size,
1279*4882a593Smuzhiyun efx->membase);
1280*4882a593Smuzhiyun
1281*4882a593Smuzhiyun return 0;
1282*4882a593Smuzhiyun
1283*4882a593Smuzhiyun fail4:
1284*4882a593Smuzhiyun pci_release_region(efx->pci_dev, bar);
1285*4882a593Smuzhiyun fail3:
1286*4882a593Smuzhiyun efx->membase_phys = 0;
1287*4882a593Smuzhiyun fail2:
1288*4882a593Smuzhiyun pci_disable_device(efx->pci_dev);
1289*4882a593Smuzhiyun fail1:
1290*4882a593Smuzhiyun return rc;
1291*4882a593Smuzhiyun }
1292*4882a593Smuzhiyun
ef4_fini_io(struct ef4_nic * efx)1293*4882a593Smuzhiyun static void ef4_fini_io(struct ef4_nic *efx)
1294*4882a593Smuzhiyun {
1295*4882a593Smuzhiyun int bar;
1296*4882a593Smuzhiyun
1297*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
1298*4882a593Smuzhiyun
1299*4882a593Smuzhiyun if (efx->membase) {
1300*4882a593Smuzhiyun iounmap(efx->membase);
1301*4882a593Smuzhiyun efx->membase = NULL;
1302*4882a593Smuzhiyun }
1303*4882a593Smuzhiyun
1304*4882a593Smuzhiyun if (efx->membase_phys) {
1305*4882a593Smuzhiyun bar = efx->type->mem_bar;
1306*4882a593Smuzhiyun pci_release_region(efx->pci_dev, bar);
1307*4882a593Smuzhiyun efx->membase_phys = 0;
1308*4882a593Smuzhiyun }
1309*4882a593Smuzhiyun
1310*4882a593Smuzhiyun /* Don't disable bus-mastering if VFs are assigned */
1311*4882a593Smuzhiyun if (!pci_vfs_assigned(efx->pci_dev))
1312*4882a593Smuzhiyun pci_disable_device(efx->pci_dev);
1313*4882a593Smuzhiyun }
1314*4882a593Smuzhiyun
ef4_set_default_rx_indir_table(struct ef4_nic * efx)1315*4882a593Smuzhiyun void ef4_set_default_rx_indir_table(struct ef4_nic *efx)
1316*4882a593Smuzhiyun {
1317*4882a593Smuzhiyun size_t i;
1318*4882a593Smuzhiyun
1319*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
1320*4882a593Smuzhiyun efx->rx_indir_table[i] =
1321*4882a593Smuzhiyun ethtool_rxfh_indir_default(i, efx->rss_spread);
1322*4882a593Smuzhiyun }
1323*4882a593Smuzhiyun
ef4_wanted_parallelism(struct ef4_nic * efx)1324*4882a593Smuzhiyun static unsigned int ef4_wanted_parallelism(struct ef4_nic *efx)
1325*4882a593Smuzhiyun {
1326*4882a593Smuzhiyun cpumask_var_t thread_mask;
1327*4882a593Smuzhiyun unsigned int count;
1328*4882a593Smuzhiyun int cpu;
1329*4882a593Smuzhiyun
1330*4882a593Smuzhiyun if (rss_cpus) {
1331*4882a593Smuzhiyun count = rss_cpus;
1332*4882a593Smuzhiyun } else {
1333*4882a593Smuzhiyun if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1334*4882a593Smuzhiyun netif_warn(efx, probe, efx->net_dev,
1335*4882a593Smuzhiyun "RSS disabled due to allocation failure\n");
1336*4882a593Smuzhiyun return 1;
1337*4882a593Smuzhiyun }
1338*4882a593Smuzhiyun
1339*4882a593Smuzhiyun count = 0;
1340*4882a593Smuzhiyun for_each_online_cpu(cpu) {
1341*4882a593Smuzhiyun if (!cpumask_test_cpu(cpu, thread_mask)) {
1342*4882a593Smuzhiyun ++count;
1343*4882a593Smuzhiyun cpumask_or(thread_mask, thread_mask,
1344*4882a593Smuzhiyun topology_sibling_cpumask(cpu));
1345*4882a593Smuzhiyun }
1346*4882a593Smuzhiyun }
1347*4882a593Smuzhiyun
1348*4882a593Smuzhiyun free_cpumask_var(thread_mask);
1349*4882a593Smuzhiyun }
1350*4882a593Smuzhiyun
1351*4882a593Smuzhiyun if (count > EF4_MAX_RX_QUEUES) {
1352*4882a593Smuzhiyun netif_cond_dbg(efx, probe, efx->net_dev, !rss_cpus, warn,
1353*4882a593Smuzhiyun "Reducing number of rx queues from %u to %u.\n",
1354*4882a593Smuzhiyun count, EF4_MAX_RX_QUEUES);
1355*4882a593Smuzhiyun count = EF4_MAX_RX_QUEUES;
1356*4882a593Smuzhiyun }
1357*4882a593Smuzhiyun
1358*4882a593Smuzhiyun return count;
1359*4882a593Smuzhiyun }
1360*4882a593Smuzhiyun
1361*4882a593Smuzhiyun /* Probe the number and type of interrupts we are able to obtain, and
1362*4882a593Smuzhiyun * the resulting numbers of channels and RX queues.
1363*4882a593Smuzhiyun */
ef4_probe_interrupts(struct ef4_nic * efx)1364*4882a593Smuzhiyun static int ef4_probe_interrupts(struct ef4_nic *efx)
1365*4882a593Smuzhiyun {
1366*4882a593Smuzhiyun unsigned int extra_channels = 0;
1367*4882a593Smuzhiyun unsigned int i, j;
1368*4882a593Smuzhiyun int rc;
1369*4882a593Smuzhiyun
1370*4882a593Smuzhiyun for (i = 0; i < EF4_MAX_EXTRA_CHANNELS; i++)
1371*4882a593Smuzhiyun if (efx->extra_channel_type[i])
1372*4882a593Smuzhiyun ++extra_channels;
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyun if (efx->interrupt_mode == EF4_INT_MODE_MSIX) {
1375*4882a593Smuzhiyun struct msix_entry xentries[EF4_MAX_CHANNELS];
1376*4882a593Smuzhiyun unsigned int n_channels;
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun n_channels = ef4_wanted_parallelism(efx);
1379*4882a593Smuzhiyun if (ef4_separate_tx_channels)
1380*4882a593Smuzhiyun n_channels *= 2;
1381*4882a593Smuzhiyun n_channels += extra_channels;
1382*4882a593Smuzhiyun n_channels = min(n_channels, efx->max_channels);
1383*4882a593Smuzhiyun
1384*4882a593Smuzhiyun for (i = 0; i < n_channels; i++)
1385*4882a593Smuzhiyun xentries[i].entry = i;
1386*4882a593Smuzhiyun rc = pci_enable_msix_range(efx->pci_dev,
1387*4882a593Smuzhiyun xentries, 1, n_channels);
1388*4882a593Smuzhiyun if (rc < 0) {
1389*4882a593Smuzhiyun /* Fall back to single channel MSI */
1390*4882a593Smuzhiyun efx->interrupt_mode = EF4_INT_MODE_MSI;
1391*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev,
1392*4882a593Smuzhiyun "could not enable MSI-X\n");
1393*4882a593Smuzhiyun } else if (rc < n_channels) {
1394*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev,
1395*4882a593Smuzhiyun "WARNING: Insufficient MSI-X vectors"
1396*4882a593Smuzhiyun " available (%d < %u).\n", rc, n_channels);
1397*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev,
1398*4882a593Smuzhiyun "WARNING: Performance may be reduced.\n");
1399*4882a593Smuzhiyun n_channels = rc;
1400*4882a593Smuzhiyun }
1401*4882a593Smuzhiyun
1402*4882a593Smuzhiyun if (rc > 0) {
1403*4882a593Smuzhiyun efx->n_channels = n_channels;
1404*4882a593Smuzhiyun if (n_channels > extra_channels)
1405*4882a593Smuzhiyun n_channels -= extra_channels;
1406*4882a593Smuzhiyun if (ef4_separate_tx_channels) {
1407*4882a593Smuzhiyun efx->n_tx_channels = min(max(n_channels / 2,
1408*4882a593Smuzhiyun 1U),
1409*4882a593Smuzhiyun efx->max_tx_channels);
1410*4882a593Smuzhiyun efx->n_rx_channels = max(n_channels -
1411*4882a593Smuzhiyun efx->n_tx_channels,
1412*4882a593Smuzhiyun 1U);
1413*4882a593Smuzhiyun } else {
1414*4882a593Smuzhiyun efx->n_tx_channels = min(n_channels,
1415*4882a593Smuzhiyun efx->max_tx_channels);
1416*4882a593Smuzhiyun efx->n_rx_channels = n_channels;
1417*4882a593Smuzhiyun }
1418*4882a593Smuzhiyun for (i = 0; i < efx->n_channels; i++)
1419*4882a593Smuzhiyun ef4_get_channel(efx, i)->irq =
1420*4882a593Smuzhiyun xentries[i].vector;
1421*4882a593Smuzhiyun }
1422*4882a593Smuzhiyun }
1423*4882a593Smuzhiyun
1424*4882a593Smuzhiyun /* Try single interrupt MSI */
1425*4882a593Smuzhiyun if (efx->interrupt_mode == EF4_INT_MODE_MSI) {
1426*4882a593Smuzhiyun efx->n_channels = 1;
1427*4882a593Smuzhiyun efx->n_rx_channels = 1;
1428*4882a593Smuzhiyun efx->n_tx_channels = 1;
1429*4882a593Smuzhiyun rc = pci_enable_msi(efx->pci_dev);
1430*4882a593Smuzhiyun if (rc == 0) {
1431*4882a593Smuzhiyun ef4_get_channel(efx, 0)->irq = efx->pci_dev->irq;
1432*4882a593Smuzhiyun } else {
1433*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev,
1434*4882a593Smuzhiyun "could not enable MSI\n");
1435*4882a593Smuzhiyun efx->interrupt_mode = EF4_INT_MODE_LEGACY;
1436*4882a593Smuzhiyun }
1437*4882a593Smuzhiyun }
1438*4882a593Smuzhiyun
1439*4882a593Smuzhiyun /* Assume legacy interrupts */
1440*4882a593Smuzhiyun if (efx->interrupt_mode == EF4_INT_MODE_LEGACY) {
1441*4882a593Smuzhiyun efx->n_channels = 1 + (ef4_separate_tx_channels ? 1 : 0);
1442*4882a593Smuzhiyun efx->n_rx_channels = 1;
1443*4882a593Smuzhiyun efx->n_tx_channels = 1;
1444*4882a593Smuzhiyun efx->legacy_irq = efx->pci_dev->irq;
1445*4882a593Smuzhiyun }
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun /* Assign extra channels if possible */
1448*4882a593Smuzhiyun j = efx->n_channels;
1449*4882a593Smuzhiyun for (i = 0; i < EF4_MAX_EXTRA_CHANNELS; i++) {
1450*4882a593Smuzhiyun if (!efx->extra_channel_type[i])
1451*4882a593Smuzhiyun continue;
1452*4882a593Smuzhiyun if (efx->interrupt_mode != EF4_INT_MODE_MSIX ||
1453*4882a593Smuzhiyun efx->n_channels <= extra_channels) {
1454*4882a593Smuzhiyun efx->extra_channel_type[i]->handle_no_channel(efx);
1455*4882a593Smuzhiyun } else {
1456*4882a593Smuzhiyun --j;
1457*4882a593Smuzhiyun ef4_get_channel(efx, j)->type =
1458*4882a593Smuzhiyun efx->extra_channel_type[i];
1459*4882a593Smuzhiyun }
1460*4882a593Smuzhiyun }
1461*4882a593Smuzhiyun
1462*4882a593Smuzhiyun efx->rss_spread = efx->n_rx_channels;
1463*4882a593Smuzhiyun
1464*4882a593Smuzhiyun return 0;
1465*4882a593Smuzhiyun }
1466*4882a593Smuzhiyun
ef4_soft_enable_interrupts(struct ef4_nic * efx)1467*4882a593Smuzhiyun static int ef4_soft_enable_interrupts(struct ef4_nic *efx)
1468*4882a593Smuzhiyun {
1469*4882a593Smuzhiyun struct ef4_channel *channel, *end_channel;
1470*4882a593Smuzhiyun int rc;
1471*4882a593Smuzhiyun
1472*4882a593Smuzhiyun BUG_ON(efx->state == STATE_DISABLED);
1473*4882a593Smuzhiyun
1474*4882a593Smuzhiyun efx->irq_soft_enabled = true;
1475*4882a593Smuzhiyun smp_wmb();
1476*4882a593Smuzhiyun
1477*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
1478*4882a593Smuzhiyun if (!channel->type->keep_eventq) {
1479*4882a593Smuzhiyun rc = ef4_init_eventq(channel);
1480*4882a593Smuzhiyun if (rc)
1481*4882a593Smuzhiyun goto fail;
1482*4882a593Smuzhiyun }
1483*4882a593Smuzhiyun ef4_start_eventq(channel);
1484*4882a593Smuzhiyun }
1485*4882a593Smuzhiyun
1486*4882a593Smuzhiyun return 0;
1487*4882a593Smuzhiyun fail:
1488*4882a593Smuzhiyun end_channel = channel;
1489*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
1490*4882a593Smuzhiyun if (channel == end_channel)
1491*4882a593Smuzhiyun break;
1492*4882a593Smuzhiyun ef4_stop_eventq(channel);
1493*4882a593Smuzhiyun if (!channel->type->keep_eventq)
1494*4882a593Smuzhiyun ef4_fini_eventq(channel);
1495*4882a593Smuzhiyun }
1496*4882a593Smuzhiyun
1497*4882a593Smuzhiyun return rc;
1498*4882a593Smuzhiyun }
1499*4882a593Smuzhiyun
ef4_soft_disable_interrupts(struct ef4_nic * efx)1500*4882a593Smuzhiyun static void ef4_soft_disable_interrupts(struct ef4_nic *efx)
1501*4882a593Smuzhiyun {
1502*4882a593Smuzhiyun struct ef4_channel *channel;
1503*4882a593Smuzhiyun
1504*4882a593Smuzhiyun if (efx->state == STATE_DISABLED)
1505*4882a593Smuzhiyun return;
1506*4882a593Smuzhiyun
1507*4882a593Smuzhiyun efx->irq_soft_enabled = false;
1508*4882a593Smuzhiyun smp_wmb();
1509*4882a593Smuzhiyun
1510*4882a593Smuzhiyun if (efx->legacy_irq)
1511*4882a593Smuzhiyun synchronize_irq(efx->legacy_irq);
1512*4882a593Smuzhiyun
1513*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
1514*4882a593Smuzhiyun if (channel->irq)
1515*4882a593Smuzhiyun synchronize_irq(channel->irq);
1516*4882a593Smuzhiyun
1517*4882a593Smuzhiyun ef4_stop_eventq(channel);
1518*4882a593Smuzhiyun if (!channel->type->keep_eventq)
1519*4882a593Smuzhiyun ef4_fini_eventq(channel);
1520*4882a593Smuzhiyun }
1521*4882a593Smuzhiyun }
1522*4882a593Smuzhiyun
ef4_enable_interrupts(struct ef4_nic * efx)1523*4882a593Smuzhiyun static int ef4_enable_interrupts(struct ef4_nic *efx)
1524*4882a593Smuzhiyun {
1525*4882a593Smuzhiyun struct ef4_channel *channel, *end_channel;
1526*4882a593Smuzhiyun int rc;
1527*4882a593Smuzhiyun
1528*4882a593Smuzhiyun BUG_ON(efx->state == STATE_DISABLED);
1529*4882a593Smuzhiyun
1530*4882a593Smuzhiyun if (efx->eeh_disabled_legacy_irq) {
1531*4882a593Smuzhiyun enable_irq(efx->legacy_irq);
1532*4882a593Smuzhiyun efx->eeh_disabled_legacy_irq = false;
1533*4882a593Smuzhiyun }
1534*4882a593Smuzhiyun
1535*4882a593Smuzhiyun efx->type->irq_enable_master(efx);
1536*4882a593Smuzhiyun
1537*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
1538*4882a593Smuzhiyun if (channel->type->keep_eventq) {
1539*4882a593Smuzhiyun rc = ef4_init_eventq(channel);
1540*4882a593Smuzhiyun if (rc)
1541*4882a593Smuzhiyun goto fail;
1542*4882a593Smuzhiyun }
1543*4882a593Smuzhiyun }
1544*4882a593Smuzhiyun
1545*4882a593Smuzhiyun rc = ef4_soft_enable_interrupts(efx);
1546*4882a593Smuzhiyun if (rc)
1547*4882a593Smuzhiyun goto fail;
1548*4882a593Smuzhiyun
1549*4882a593Smuzhiyun return 0;
1550*4882a593Smuzhiyun
1551*4882a593Smuzhiyun fail:
1552*4882a593Smuzhiyun end_channel = channel;
1553*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
1554*4882a593Smuzhiyun if (channel == end_channel)
1555*4882a593Smuzhiyun break;
1556*4882a593Smuzhiyun if (channel->type->keep_eventq)
1557*4882a593Smuzhiyun ef4_fini_eventq(channel);
1558*4882a593Smuzhiyun }
1559*4882a593Smuzhiyun
1560*4882a593Smuzhiyun efx->type->irq_disable_non_ev(efx);
1561*4882a593Smuzhiyun
1562*4882a593Smuzhiyun return rc;
1563*4882a593Smuzhiyun }
1564*4882a593Smuzhiyun
ef4_disable_interrupts(struct ef4_nic * efx)1565*4882a593Smuzhiyun static void ef4_disable_interrupts(struct ef4_nic *efx)
1566*4882a593Smuzhiyun {
1567*4882a593Smuzhiyun struct ef4_channel *channel;
1568*4882a593Smuzhiyun
1569*4882a593Smuzhiyun ef4_soft_disable_interrupts(efx);
1570*4882a593Smuzhiyun
1571*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
1572*4882a593Smuzhiyun if (channel->type->keep_eventq)
1573*4882a593Smuzhiyun ef4_fini_eventq(channel);
1574*4882a593Smuzhiyun }
1575*4882a593Smuzhiyun
1576*4882a593Smuzhiyun efx->type->irq_disable_non_ev(efx);
1577*4882a593Smuzhiyun }
1578*4882a593Smuzhiyun
ef4_remove_interrupts(struct ef4_nic * efx)1579*4882a593Smuzhiyun static void ef4_remove_interrupts(struct ef4_nic *efx)
1580*4882a593Smuzhiyun {
1581*4882a593Smuzhiyun struct ef4_channel *channel;
1582*4882a593Smuzhiyun
1583*4882a593Smuzhiyun /* Remove MSI/MSI-X interrupts */
1584*4882a593Smuzhiyun ef4_for_each_channel(channel, efx)
1585*4882a593Smuzhiyun channel->irq = 0;
1586*4882a593Smuzhiyun pci_disable_msi(efx->pci_dev);
1587*4882a593Smuzhiyun pci_disable_msix(efx->pci_dev);
1588*4882a593Smuzhiyun
1589*4882a593Smuzhiyun /* Remove legacy interrupt */
1590*4882a593Smuzhiyun efx->legacy_irq = 0;
1591*4882a593Smuzhiyun }
1592*4882a593Smuzhiyun
ef4_set_channels(struct ef4_nic * efx)1593*4882a593Smuzhiyun static void ef4_set_channels(struct ef4_nic *efx)
1594*4882a593Smuzhiyun {
1595*4882a593Smuzhiyun struct ef4_channel *channel;
1596*4882a593Smuzhiyun struct ef4_tx_queue *tx_queue;
1597*4882a593Smuzhiyun
1598*4882a593Smuzhiyun efx->tx_channel_offset =
1599*4882a593Smuzhiyun ef4_separate_tx_channels ?
1600*4882a593Smuzhiyun efx->n_channels - efx->n_tx_channels : 0;
1601*4882a593Smuzhiyun
1602*4882a593Smuzhiyun /* We need to mark which channels really have RX and TX
1603*4882a593Smuzhiyun * queues, and adjust the TX queue numbers if we have separate
1604*4882a593Smuzhiyun * RX-only and TX-only channels.
1605*4882a593Smuzhiyun */
1606*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
1607*4882a593Smuzhiyun if (channel->channel < efx->n_rx_channels)
1608*4882a593Smuzhiyun channel->rx_queue.core_index = channel->channel;
1609*4882a593Smuzhiyun else
1610*4882a593Smuzhiyun channel->rx_queue.core_index = -1;
1611*4882a593Smuzhiyun
1612*4882a593Smuzhiyun ef4_for_each_channel_tx_queue(tx_queue, channel)
1613*4882a593Smuzhiyun tx_queue->queue -= (efx->tx_channel_offset *
1614*4882a593Smuzhiyun EF4_TXQ_TYPES);
1615*4882a593Smuzhiyun }
1616*4882a593Smuzhiyun }
1617*4882a593Smuzhiyun
ef4_probe_nic(struct ef4_nic * efx)1618*4882a593Smuzhiyun static int ef4_probe_nic(struct ef4_nic *efx)
1619*4882a593Smuzhiyun {
1620*4882a593Smuzhiyun int rc;
1621*4882a593Smuzhiyun
1622*4882a593Smuzhiyun netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
1623*4882a593Smuzhiyun
1624*4882a593Smuzhiyun /* Carry out hardware-type specific initialisation */
1625*4882a593Smuzhiyun rc = efx->type->probe(efx);
1626*4882a593Smuzhiyun if (rc)
1627*4882a593Smuzhiyun return rc;
1628*4882a593Smuzhiyun
1629*4882a593Smuzhiyun do {
1630*4882a593Smuzhiyun if (!efx->max_channels || !efx->max_tx_channels) {
1631*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev,
1632*4882a593Smuzhiyun "Insufficient resources to allocate"
1633*4882a593Smuzhiyun " any channels\n");
1634*4882a593Smuzhiyun rc = -ENOSPC;
1635*4882a593Smuzhiyun goto fail1;
1636*4882a593Smuzhiyun }
1637*4882a593Smuzhiyun
1638*4882a593Smuzhiyun /* Determine the number of channels and queues by trying
1639*4882a593Smuzhiyun * to hook in MSI-X interrupts.
1640*4882a593Smuzhiyun */
1641*4882a593Smuzhiyun rc = ef4_probe_interrupts(efx);
1642*4882a593Smuzhiyun if (rc)
1643*4882a593Smuzhiyun goto fail1;
1644*4882a593Smuzhiyun
1645*4882a593Smuzhiyun ef4_set_channels(efx);
1646*4882a593Smuzhiyun
1647*4882a593Smuzhiyun /* dimension_resources can fail with EAGAIN */
1648*4882a593Smuzhiyun rc = efx->type->dimension_resources(efx);
1649*4882a593Smuzhiyun if (rc != 0 && rc != -EAGAIN)
1650*4882a593Smuzhiyun goto fail2;
1651*4882a593Smuzhiyun
1652*4882a593Smuzhiyun if (rc == -EAGAIN)
1653*4882a593Smuzhiyun /* try again with new max_channels */
1654*4882a593Smuzhiyun ef4_remove_interrupts(efx);
1655*4882a593Smuzhiyun
1656*4882a593Smuzhiyun } while (rc == -EAGAIN);
1657*4882a593Smuzhiyun
1658*4882a593Smuzhiyun if (efx->n_channels > 1)
1659*4882a593Smuzhiyun netdev_rss_key_fill(&efx->rx_hash_key,
1660*4882a593Smuzhiyun sizeof(efx->rx_hash_key));
1661*4882a593Smuzhiyun ef4_set_default_rx_indir_table(efx);
1662*4882a593Smuzhiyun
1663*4882a593Smuzhiyun netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1664*4882a593Smuzhiyun netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
1665*4882a593Smuzhiyun
1666*4882a593Smuzhiyun /* Initialise the interrupt moderation settings */
1667*4882a593Smuzhiyun efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
1668*4882a593Smuzhiyun ef4_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1669*4882a593Smuzhiyun true);
1670*4882a593Smuzhiyun
1671*4882a593Smuzhiyun return 0;
1672*4882a593Smuzhiyun
1673*4882a593Smuzhiyun fail2:
1674*4882a593Smuzhiyun ef4_remove_interrupts(efx);
1675*4882a593Smuzhiyun fail1:
1676*4882a593Smuzhiyun efx->type->remove(efx);
1677*4882a593Smuzhiyun return rc;
1678*4882a593Smuzhiyun }
1679*4882a593Smuzhiyun
ef4_remove_nic(struct ef4_nic * efx)1680*4882a593Smuzhiyun static void ef4_remove_nic(struct ef4_nic *efx)
1681*4882a593Smuzhiyun {
1682*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
1683*4882a593Smuzhiyun
1684*4882a593Smuzhiyun ef4_remove_interrupts(efx);
1685*4882a593Smuzhiyun efx->type->remove(efx);
1686*4882a593Smuzhiyun }
1687*4882a593Smuzhiyun
ef4_probe_filters(struct ef4_nic * efx)1688*4882a593Smuzhiyun static int ef4_probe_filters(struct ef4_nic *efx)
1689*4882a593Smuzhiyun {
1690*4882a593Smuzhiyun int rc;
1691*4882a593Smuzhiyun
1692*4882a593Smuzhiyun spin_lock_init(&efx->filter_lock);
1693*4882a593Smuzhiyun init_rwsem(&efx->filter_sem);
1694*4882a593Smuzhiyun mutex_lock(&efx->mac_lock);
1695*4882a593Smuzhiyun down_write(&efx->filter_sem);
1696*4882a593Smuzhiyun rc = efx->type->filter_table_probe(efx);
1697*4882a593Smuzhiyun if (rc)
1698*4882a593Smuzhiyun goto out_unlock;
1699*4882a593Smuzhiyun
1700*4882a593Smuzhiyun #ifdef CONFIG_RFS_ACCEL
1701*4882a593Smuzhiyun if (efx->type->offload_features & NETIF_F_NTUPLE) {
1702*4882a593Smuzhiyun struct ef4_channel *channel;
1703*4882a593Smuzhiyun int i, success = 1;
1704*4882a593Smuzhiyun
1705*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
1706*4882a593Smuzhiyun channel->rps_flow_id =
1707*4882a593Smuzhiyun kcalloc(efx->type->max_rx_ip_filters,
1708*4882a593Smuzhiyun sizeof(*channel->rps_flow_id),
1709*4882a593Smuzhiyun GFP_KERNEL);
1710*4882a593Smuzhiyun if (!channel->rps_flow_id)
1711*4882a593Smuzhiyun success = 0;
1712*4882a593Smuzhiyun else
1713*4882a593Smuzhiyun for (i = 0;
1714*4882a593Smuzhiyun i < efx->type->max_rx_ip_filters;
1715*4882a593Smuzhiyun ++i)
1716*4882a593Smuzhiyun channel->rps_flow_id[i] =
1717*4882a593Smuzhiyun RPS_FLOW_ID_INVALID;
1718*4882a593Smuzhiyun }
1719*4882a593Smuzhiyun
1720*4882a593Smuzhiyun if (!success) {
1721*4882a593Smuzhiyun ef4_for_each_channel(channel, efx)
1722*4882a593Smuzhiyun kfree(channel->rps_flow_id);
1723*4882a593Smuzhiyun efx->type->filter_table_remove(efx);
1724*4882a593Smuzhiyun rc = -ENOMEM;
1725*4882a593Smuzhiyun goto out_unlock;
1726*4882a593Smuzhiyun }
1727*4882a593Smuzhiyun
1728*4882a593Smuzhiyun efx->rps_expire_index = efx->rps_expire_channel = 0;
1729*4882a593Smuzhiyun }
1730*4882a593Smuzhiyun #endif
1731*4882a593Smuzhiyun out_unlock:
1732*4882a593Smuzhiyun up_write(&efx->filter_sem);
1733*4882a593Smuzhiyun mutex_unlock(&efx->mac_lock);
1734*4882a593Smuzhiyun return rc;
1735*4882a593Smuzhiyun }
1736*4882a593Smuzhiyun
ef4_remove_filters(struct ef4_nic * efx)1737*4882a593Smuzhiyun static void ef4_remove_filters(struct ef4_nic *efx)
1738*4882a593Smuzhiyun {
1739*4882a593Smuzhiyun #ifdef CONFIG_RFS_ACCEL
1740*4882a593Smuzhiyun struct ef4_channel *channel;
1741*4882a593Smuzhiyun
1742*4882a593Smuzhiyun ef4_for_each_channel(channel, efx)
1743*4882a593Smuzhiyun kfree(channel->rps_flow_id);
1744*4882a593Smuzhiyun #endif
1745*4882a593Smuzhiyun down_write(&efx->filter_sem);
1746*4882a593Smuzhiyun efx->type->filter_table_remove(efx);
1747*4882a593Smuzhiyun up_write(&efx->filter_sem);
1748*4882a593Smuzhiyun }
1749*4882a593Smuzhiyun
ef4_restore_filters(struct ef4_nic * efx)1750*4882a593Smuzhiyun static void ef4_restore_filters(struct ef4_nic *efx)
1751*4882a593Smuzhiyun {
1752*4882a593Smuzhiyun down_read(&efx->filter_sem);
1753*4882a593Smuzhiyun efx->type->filter_table_restore(efx);
1754*4882a593Smuzhiyun up_read(&efx->filter_sem);
1755*4882a593Smuzhiyun }
1756*4882a593Smuzhiyun
1757*4882a593Smuzhiyun /**************************************************************************
1758*4882a593Smuzhiyun *
1759*4882a593Smuzhiyun * NIC startup/shutdown
1760*4882a593Smuzhiyun *
1761*4882a593Smuzhiyun *************************************************************************/
1762*4882a593Smuzhiyun
ef4_probe_all(struct ef4_nic * efx)1763*4882a593Smuzhiyun static int ef4_probe_all(struct ef4_nic *efx)
1764*4882a593Smuzhiyun {
1765*4882a593Smuzhiyun int rc;
1766*4882a593Smuzhiyun
1767*4882a593Smuzhiyun rc = ef4_probe_nic(efx);
1768*4882a593Smuzhiyun if (rc) {
1769*4882a593Smuzhiyun netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
1770*4882a593Smuzhiyun goto fail1;
1771*4882a593Smuzhiyun }
1772*4882a593Smuzhiyun
1773*4882a593Smuzhiyun rc = ef4_probe_port(efx);
1774*4882a593Smuzhiyun if (rc) {
1775*4882a593Smuzhiyun netif_err(efx, probe, efx->net_dev, "failed to create port\n");
1776*4882a593Smuzhiyun goto fail2;
1777*4882a593Smuzhiyun }
1778*4882a593Smuzhiyun
1779*4882a593Smuzhiyun BUILD_BUG_ON(EF4_DEFAULT_DMAQ_SIZE < EF4_RXQ_MIN_ENT);
1780*4882a593Smuzhiyun if (WARN_ON(EF4_DEFAULT_DMAQ_SIZE < EF4_TXQ_MIN_ENT(efx))) {
1781*4882a593Smuzhiyun rc = -EINVAL;
1782*4882a593Smuzhiyun goto fail3;
1783*4882a593Smuzhiyun }
1784*4882a593Smuzhiyun efx->rxq_entries = efx->txq_entries = EF4_DEFAULT_DMAQ_SIZE;
1785*4882a593Smuzhiyun
1786*4882a593Smuzhiyun rc = ef4_probe_filters(efx);
1787*4882a593Smuzhiyun if (rc) {
1788*4882a593Smuzhiyun netif_err(efx, probe, efx->net_dev,
1789*4882a593Smuzhiyun "failed to create filter tables\n");
1790*4882a593Smuzhiyun goto fail4;
1791*4882a593Smuzhiyun }
1792*4882a593Smuzhiyun
1793*4882a593Smuzhiyun rc = ef4_probe_channels(efx);
1794*4882a593Smuzhiyun if (rc)
1795*4882a593Smuzhiyun goto fail5;
1796*4882a593Smuzhiyun
1797*4882a593Smuzhiyun return 0;
1798*4882a593Smuzhiyun
1799*4882a593Smuzhiyun fail5:
1800*4882a593Smuzhiyun ef4_remove_filters(efx);
1801*4882a593Smuzhiyun fail4:
1802*4882a593Smuzhiyun fail3:
1803*4882a593Smuzhiyun ef4_remove_port(efx);
1804*4882a593Smuzhiyun fail2:
1805*4882a593Smuzhiyun ef4_remove_nic(efx);
1806*4882a593Smuzhiyun fail1:
1807*4882a593Smuzhiyun return rc;
1808*4882a593Smuzhiyun }
1809*4882a593Smuzhiyun
1810*4882a593Smuzhiyun /* If the interface is supposed to be running but is not, start
1811*4882a593Smuzhiyun * the hardware and software data path, regular activity for the port
1812*4882a593Smuzhiyun * (MAC statistics, link polling, etc.) and schedule the port to be
1813*4882a593Smuzhiyun * reconfigured. Interrupts must already be enabled. This function
1814*4882a593Smuzhiyun * is safe to call multiple times, so long as the NIC is not disabled.
1815*4882a593Smuzhiyun * Requires the RTNL lock.
1816*4882a593Smuzhiyun */
ef4_start_all(struct ef4_nic * efx)1817*4882a593Smuzhiyun static void ef4_start_all(struct ef4_nic *efx)
1818*4882a593Smuzhiyun {
1819*4882a593Smuzhiyun EF4_ASSERT_RESET_SERIALISED(efx);
1820*4882a593Smuzhiyun BUG_ON(efx->state == STATE_DISABLED);
1821*4882a593Smuzhiyun
1822*4882a593Smuzhiyun /* Check that it is appropriate to restart the interface. All
1823*4882a593Smuzhiyun * of these flags are safe to read under just the rtnl lock */
1824*4882a593Smuzhiyun if (efx->port_enabled || !netif_running(efx->net_dev) ||
1825*4882a593Smuzhiyun efx->reset_pending)
1826*4882a593Smuzhiyun return;
1827*4882a593Smuzhiyun
1828*4882a593Smuzhiyun ef4_start_port(efx);
1829*4882a593Smuzhiyun ef4_start_datapath(efx);
1830*4882a593Smuzhiyun
1831*4882a593Smuzhiyun /* Start the hardware monitor if there is one */
1832*4882a593Smuzhiyun if (efx->type->monitor != NULL)
1833*4882a593Smuzhiyun queue_delayed_work(efx->workqueue, &efx->monitor_work,
1834*4882a593Smuzhiyun ef4_monitor_interval);
1835*4882a593Smuzhiyun
1836*4882a593Smuzhiyun efx->type->start_stats(efx);
1837*4882a593Smuzhiyun efx->type->pull_stats(efx);
1838*4882a593Smuzhiyun spin_lock_bh(&efx->stats_lock);
1839*4882a593Smuzhiyun efx->type->update_stats(efx, NULL, NULL);
1840*4882a593Smuzhiyun spin_unlock_bh(&efx->stats_lock);
1841*4882a593Smuzhiyun }
1842*4882a593Smuzhiyun
1843*4882a593Smuzhiyun /* Quiesce the hardware and software data path, and regular activity
1844*4882a593Smuzhiyun * for the port without bringing the link down. Safe to call multiple
1845*4882a593Smuzhiyun * times with the NIC in almost any state, but interrupts should be
1846*4882a593Smuzhiyun * enabled. Requires the RTNL lock.
1847*4882a593Smuzhiyun */
ef4_stop_all(struct ef4_nic * efx)1848*4882a593Smuzhiyun static void ef4_stop_all(struct ef4_nic *efx)
1849*4882a593Smuzhiyun {
1850*4882a593Smuzhiyun EF4_ASSERT_RESET_SERIALISED(efx);
1851*4882a593Smuzhiyun
1852*4882a593Smuzhiyun /* port_enabled can be read safely under the rtnl lock */
1853*4882a593Smuzhiyun if (!efx->port_enabled)
1854*4882a593Smuzhiyun return;
1855*4882a593Smuzhiyun
1856*4882a593Smuzhiyun /* update stats before we go down so we can accurately count
1857*4882a593Smuzhiyun * rx_nodesc_drops
1858*4882a593Smuzhiyun */
1859*4882a593Smuzhiyun efx->type->pull_stats(efx);
1860*4882a593Smuzhiyun spin_lock_bh(&efx->stats_lock);
1861*4882a593Smuzhiyun efx->type->update_stats(efx, NULL, NULL);
1862*4882a593Smuzhiyun spin_unlock_bh(&efx->stats_lock);
1863*4882a593Smuzhiyun efx->type->stop_stats(efx);
1864*4882a593Smuzhiyun ef4_stop_port(efx);
1865*4882a593Smuzhiyun
1866*4882a593Smuzhiyun /* Stop the kernel transmit interface. This is only valid if
1867*4882a593Smuzhiyun * the device is stopped or detached; otherwise the watchdog
1868*4882a593Smuzhiyun * may fire immediately.
1869*4882a593Smuzhiyun */
1870*4882a593Smuzhiyun WARN_ON(netif_running(efx->net_dev) &&
1871*4882a593Smuzhiyun netif_device_present(efx->net_dev));
1872*4882a593Smuzhiyun netif_tx_disable(efx->net_dev);
1873*4882a593Smuzhiyun
1874*4882a593Smuzhiyun ef4_stop_datapath(efx);
1875*4882a593Smuzhiyun }
1876*4882a593Smuzhiyun
ef4_remove_all(struct ef4_nic * efx)1877*4882a593Smuzhiyun static void ef4_remove_all(struct ef4_nic *efx)
1878*4882a593Smuzhiyun {
1879*4882a593Smuzhiyun ef4_remove_channels(efx);
1880*4882a593Smuzhiyun ef4_remove_filters(efx);
1881*4882a593Smuzhiyun ef4_remove_port(efx);
1882*4882a593Smuzhiyun ef4_remove_nic(efx);
1883*4882a593Smuzhiyun }
1884*4882a593Smuzhiyun
1885*4882a593Smuzhiyun /**************************************************************************
1886*4882a593Smuzhiyun *
1887*4882a593Smuzhiyun * Interrupt moderation
1888*4882a593Smuzhiyun *
1889*4882a593Smuzhiyun **************************************************************************/
ef4_usecs_to_ticks(struct ef4_nic * efx,unsigned int usecs)1890*4882a593Smuzhiyun unsigned int ef4_usecs_to_ticks(struct ef4_nic *efx, unsigned int usecs)
1891*4882a593Smuzhiyun {
1892*4882a593Smuzhiyun if (usecs == 0)
1893*4882a593Smuzhiyun return 0;
1894*4882a593Smuzhiyun if (usecs * 1000 < efx->timer_quantum_ns)
1895*4882a593Smuzhiyun return 1; /* never round down to 0 */
1896*4882a593Smuzhiyun return usecs * 1000 / efx->timer_quantum_ns;
1897*4882a593Smuzhiyun }
1898*4882a593Smuzhiyun
ef4_ticks_to_usecs(struct ef4_nic * efx,unsigned int ticks)1899*4882a593Smuzhiyun unsigned int ef4_ticks_to_usecs(struct ef4_nic *efx, unsigned int ticks)
1900*4882a593Smuzhiyun {
1901*4882a593Smuzhiyun /* We must round up when converting ticks to microseconds
1902*4882a593Smuzhiyun * because we round down when converting the other way.
1903*4882a593Smuzhiyun */
1904*4882a593Smuzhiyun return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
1905*4882a593Smuzhiyun }
1906*4882a593Smuzhiyun
1907*4882a593Smuzhiyun /* Set interrupt moderation parameters */
ef4_init_irq_moderation(struct ef4_nic * efx,unsigned int tx_usecs,unsigned int rx_usecs,bool rx_adaptive,bool rx_may_override_tx)1908*4882a593Smuzhiyun int ef4_init_irq_moderation(struct ef4_nic *efx, unsigned int tx_usecs,
1909*4882a593Smuzhiyun unsigned int rx_usecs, bool rx_adaptive,
1910*4882a593Smuzhiyun bool rx_may_override_tx)
1911*4882a593Smuzhiyun {
1912*4882a593Smuzhiyun struct ef4_channel *channel;
1913*4882a593Smuzhiyun unsigned int timer_max_us;
1914*4882a593Smuzhiyun
1915*4882a593Smuzhiyun EF4_ASSERT_RESET_SERIALISED(efx);
1916*4882a593Smuzhiyun
1917*4882a593Smuzhiyun timer_max_us = efx->timer_max_ns / 1000;
1918*4882a593Smuzhiyun
1919*4882a593Smuzhiyun if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
1920*4882a593Smuzhiyun return -EINVAL;
1921*4882a593Smuzhiyun
1922*4882a593Smuzhiyun if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
1923*4882a593Smuzhiyun !rx_may_override_tx) {
1924*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1925*4882a593Smuzhiyun "RX and TX IRQ moderation must be equal\n");
1926*4882a593Smuzhiyun return -EINVAL;
1927*4882a593Smuzhiyun }
1928*4882a593Smuzhiyun
1929*4882a593Smuzhiyun efx->irq_rx_adaptive = rx_adaptive;
1930*4882a593Smuzhiyun efx->irq_rx_moderation_us = rx_usecs;
1931*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
1932*4882a593Smuzhiyun if (ef4_channel_has_rx_queue(channel))
1933*4882a593Smuzhiyun channel->irq_moderation_us = rx_usecs;
1934*4882a593Smuzhiyun else if (ef4_channel_has_tx_queues(channel))
1935*4882a593Smuzhiyun channel->irq_moderation_us = tx_usecs;
1936*4882a593Smuzhiyun }
1937*4882a593Smuzhiyun
1938*4882a593Smuzhiyun return 0;
1939*4882a593Smuzhiyun }
1940*4882a593Smuzhiyun
ef4_get_irq_moderation(struct ef4_nic * efx,unsigned int * tx_usecs,unsigned int * rx_usecs,bool * rx_adaptive)1941*4882a593Smuzhiyun void ef4_get_irq_moderation(struct ef4_nic *efx, unsigned int *tx_usecs,
1942*4882a593Smuzhiyun unsigned int *rx_usecs, bool *rx_adaptive)
1943*4882a593Smuzhiyun {
1944*4882a593Smuzhiyun *rx_adaptive = efx->irq_rx_adaptive;
1945*4882a593Smuzhiyun *rx_usecs = efx->irq_rx_moderation_us;
1946*4882a593Smuzhiyun
1947*4882a593Smuzhiyun /* If channels are shared between RX and TX, so is IRQ
1948*4882a593Smuzhiyun * moderation. Otherwise, IRQ moderation is the same for all
1949*4882a593Smuzhiyun * TX channels and is not adaptive.
1950*4882a593Smuzhiyun */
1951*4882a593Smuzhiyun if (efx->tx_channel_offset == 0) {
1952*4882a593Smuzhiyun *tx_usecs = *rx_usecs;
1953*4882a593Smuzhiyun } else {
1954*4882a593Smuzhiyun struct ef4_channel *tx_channel;
1955*4882a593Smuzhiyun
1956*4882a593Smuzhiyun tx_channel = efx->channel[efx->tx_channel_offset];
1957*4882a593Smuzhiyun *tx_usecs = tx_channel->irq_moderation_us;
1958*4882a593Smuzhiyun }
1959*4882a593Smuzhiyun }
1960*4882a593Smuzhiyun
1961*4882a593Smuzhiyun /**************************************************************************
1962*4882a593Smuzhiyun *
1963*4882a593Smuzhiyun * Hardware monitor
1964*4882a593Smuzhiyun *
1965*4882a593Smuzhiyun **************************************************************************/
1966*4882a593Smuzhiyun
1967*4882a593Smuzhiyun /* Run periodically off the general workqueue */
ef4_monitor(struct work_struct * data)1968*4882a593Smuzhiyun static void ef4_monitor(struct work_struct *data)
1969*4882a593Smuzhiyun {
1970*4882a593Smuzhiyun struct ef4_nic *efx = container_of(data, struct ef4_nic,
1971*4882a593Smuzhiyun monitor_work.work);
1972*4882a593Smuzhiyun
1973*4882a593Smuzhiyun netif_vdbg(efx, timer, efx->net_dev,
1974*4882a593Smuzhiyun "hardware monitor executing on CPU %d\n",
1975*4882a593Smuzhiyun raw_smp_processor_id());
1976*4882a593Smuzhiyun BUG_ON(efx->type->monitor == NULL);
1977*4882a593Smuzhiyun
1978*4882a593Smuzhiyun /* If the mac_lock is already held then it is likely a port
1979*4882a593Smuzhiyun * reconfiguration is already in place, which will likely do
1980*4882a593Smuzhiyun * most of the work of monitor() anyway. */
1981*4882a593Smuzhiyun if (mutex_trylock(&efx->mac_lock)) {
1982*4882a593Smuzhiyun if (efx->port_enabled)
1983*4882a593Smuzhiyun efx->type->monitor(efx);
1984*4882a593Smuzhiyun mutex_unlock(&efx->mac_lock);
1985*4882a593Smuzhiyun }
1986*4882a593Smuzhiyun
1987*4882a593Smuzhiyun queue_delayed_work(efx->workqueue, &efx->monitor_work,
1988*4882a593Smuzhiyun ef4_monitor_interval);
1989*4882a593Smuzhiyun }
1990*4882a593Smuzhiyun
1991*4882a593Smuzhiyun /**************************************************************************
1992*4882a593Smuzhiyun *
1993*4882a593Smuzhiyun * ioctls
1994*4882a593Smuzhiyun *
1995*4882a593Smuzhiyun *************************************************************************/
1996*4882a593Smuzhiyun
1997*4882a593Smuzhiyun /* Net device ioctl
1998*4882a593Smuzhiyun * Context: process, rtnl_lock() held.
1999*4882a593Smuzhiyun */
ef4_ioctl(struct net_device * net_dev,struct ifreq * ifr,int cmd)2000*4882a593Smuzhiyun static int ef4_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
2001*4882a593Smuzhiyun {
2002*4882a593Smuzhiyun struct ef4_nic *efx = netdev_priv(net_dev);
2003*4882a593Smuzhiyun struct mii_ioctl_data *data = if_mii(ifr);
2004*4882a593Smuzhiyun
2005*4882a593Smuzhiyun /* Convert phy_id from older PRTAD/DEVAD format */
2006*4882a593Smuzhiyun if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
2007*4882a593Smuzhiyun (data->phy_id & 0xfc00) == 0x0400)
2008*4882a593Smuzhiyun data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
2009*4882a593Smuzhiyun
2010*4882a593Smuzhiyun return mdio_mii_ioctl(&efx->mdio, data, cmd);
2011*4882a593Smuzhiyun }
2012*4882a593Smuzhiyun
2013*4882a593Smuzhiyun /**************************************************************************
2014*4882a593Smuzhiyun *
2015*4882a593Smuzhiyun * NAPI interface
2016*4882a593Smuzhiyun *
2017*4882a593Smuzhiyun **************************************************************************/
2018*4882a593Smuzhiyun
ef4_init_napi_channel(struct ef4_channel * channel)2019*4882a593Smuzhiyun static void ef4_init_napi_channel(struct ef4_channel *channel)
2020*4882a593Smuzhiyun {
2021*4882a593Smuzhiyun struct ef4_nic *efx = channel->efx;
2022*4882a593Smuzhiyun
2023*4882a593Smuzhiyun channel->napi_dev = efx->net_dev;
2024*4882a593Smuzhiyun netif_napi_add(channel->napi_dev, &channel->napi_str,
2025*4882a593Smuzhiyun ef4_poll, napi_weight);
2026*4882a593Smuzhiyun }
2027*4882a593Smuzhiyun
ef4_init_napi(struct ef4_nic * efx)2028*4882a593Smuzhiyun static void ef4_init_napi(struct ef4_nic *efx)
2029*4882a593Smuzhiyun {
2030*4882a593Smuzhiyun struct ef4_channel *channel;
2031*4882a593Smuzhiyun
2032*4882a593Smuzhiyun ef4_for_each_channel(channel, efx)
2033*4882a593Smuzhiyun ef4_init_napi_channel(channel);
2034*4882a593Smuzhiyun }
2035*4882a593Smuzhiyun
ef4_fini_napi_channel(struct ef4_channel * channel)2036*4882a593Smuzhiyun static void ef4_fini_napi_channel(struct ef4_channel *channel)
2037*4882a593Smuzhiyun {
2038*4882a593Smuzhiyun if (channel->napi_dev)
2039*4882a593Smuzhiyun netif_napi_del(&channel->napi_str);
2040*4882a593Smuzhiyun
2041*4882a593Smuzhiyun channel->napi_dev = NULL;
2042*4882a593Smuzhiyun }
2043*4882a593Smuzhiyun
ef4_fini_napi(struct ef4_nic * efx)2044*4882a593Smuzhiyun static void ef4_fini_napi(struct ef4_nic *efx)
2045*4882a593Smuzhiyun {
2046*4882a593Smuzhiyun struct ef4_channel *channel;
2047*4882a593Smuzhiyun
2048*4882a593Smuzhiyun ef4_for_each_channel(channel, efx)
2049*4882a593Smuzhiyun ef4_fini_napi_channel(channel);
2050*4882a593Smuzhiyun }
2051*4882a593Smuzhiyun
2052*4882a593Smuzhiyun /**************************************************************************
2053*4882a593Smuzhiyun *
2054*4882a593Smuzhiyun * Kernel net device interface
2055*4882a593Smuzhiyun *
2056*4882a593Smuzhiyun *************************************************************************/
2057*4882a593Smuzhiyun
2058*4882a593Smuzhiyun /* Context: process, rtnl_lock() held. */
ef4_net_open(struct net_device * net_dev)2059*4882a593Smuzhiyun int ef4_net_open(struct net_device *net_dev)
2060*4882a593Smuzhiyun {
2061*4882a593Smuzhiyun struct ef4_nic *efx = netdev_priv(net_dev);
2062*4882a593Smuzhiyun int rc;
2063*4882a593Smuzhiyun
2064*4882a593Smuzhiyun netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
2065*4882a593Smuzhiyun raw_smp_processor_id());
2066*4882a593Smuzhiyun
2067*4882a593Smuzhiyun rc = ef4_check_disabled(efx);
2068*4882a593Smuzhiyun if (rc)
2069*4882a593Smuzhiyun return rc;
2070*4882a593Smuzhiyun if (efx->phy_mode & PHY_MODE_SPECIAL)
2071*4882a593Smuzhiyun return -EBUSY;
2072*4882a593Smuzhiyun
2073*4882a593Smuzhiyun /* Notify the kernel of the link state polled during driver load,
2074*4882a593Smuzhiyun * before the monitor starts running */
2075*4882a593Smuzhiyun ef4_link_status_changed(efx);
2076*4882a593Smuzhiyun
2077*4882a593Smuzhiyun ef4_start_all(efx);
2078*4882a593Smuzhiyun ef4_selftest_async_start(efx);
2079*4882a593Smuzhiyun return 0;
2080*4882a593Smuzhiyun }
2081*4882a593Smuzhiyun
2082*4882a593Smuzhiyun /* Context: process, rtnl_lock() held.
2083*4882a593Smuzhiyun * Note that the kernel will ignore our return code; this method
2084*4882a593Smuzhiyun * should really be a void.
2085*4882a593Smuzhiyun */
ef4_net_stop(struct net_device * net_dev)2086*4882a593Smuzhiyun int ef4_net_stop(struct net_device *net_dev)
2087*4882a593Smuzhiyun {
2088*4882a593Smuzhiyun struct ef4_nic *efx = netdev_priv(net_dev);
2089*4882a593Smuzhiyun
2090*4882a593Smuzhiyun netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
2091*4882a593Smuzhiyun raw_smp_processor_id());
2092*4882a593Smuzhiyun
2093*4882a593Smuzhiyun /* Stop the device and flush all the channels */
2094*4882a593Smuzhiyun ef4_stop_all(efx);
2095*4882a593Smuzhiyun
2096*4882a593Smuzhiyun return 0;
2097*4882a593Smuzhiyun }
2098*4882a593Smuzhiyun
2099*4882a593Smuzhiyun /* Context: process, dev_base_lock or RTNL held, non-blocking. */
ef4_net_stats(struct net_device * net_dev,struct rtnl_link_stats64 * stats)2100*4882a593Smuzhiyun static void ef4_net_stats(struct net_device *net_dev,
2101*4882a593Smuzhiyun struct rtnl_link_stats64 *stats)
2102*4882a593Smuzhiyun {
2103*4882a593Smuzhiyun struct ef4_nic *efx = netdev_priv(net_dev);
2104*4882a593Smuzhiyun
2105*4882a593Smuzhiyun spin_lock_bh(&efx->stats_lock);
2106*4882a593Smuzhiyun efx->type->update_stats(efx, NULL, stats);
2107*4882a593Smuzhiyun spin_unlock_bh(&efx->stats_lock);
2108*4882a593Smuzhiyun }
2109*4882a593Smuzhiyun
2110*4882a593Smuzhiyun /* Context: netif_tx_lock held, BHs disabled. */
ef4_watchdog(struct net_device * net_dev,unsigned int txqueue)2111*4882a593Smuzhiyun static void ef4_watchdog(struct net_device *net_dev, unsigned int txqueue)
2112*4882a593Smuzhiyun {
2113*4882a593Smuzhiyun struct ef4_nic *efx = netdev_priv(net_dev);
2114*4882a593Smuzhiyun
2115*4882a593Smuzhiyun netif_err(efx, tx_err, efx->net_dev,
2116*4882a593Smuzhiyun "TX stuck with port_enabled=%d: resetting channels\n",
2117*4882a593Smuzhiyun efx->port_enabled);
2118*4882a593Smuzhiyun
2119*4882a593Smuzhiyun ef4_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
2120*4882a593Smuzhiyun }
2121*4882a593Smuzhiyun
2122*4882a593Smuzhiyun
2123*4882a593Smuzhiyun /* Context: process, rtnl_lock() held. */
ef4_change_mtu(struct net_device * net_dev,int new_mtu)2124*4882a593Smuzhiyun static int ef4_change_mtu(struct net_device *net_dev, int new_mtu)
2125*4882a593Smuzhiyun {
2126*4882a593Smuzhiyun struct ef4_nic *efx = netdev_priv(net_dev);
2127*4882a593Smuzhiyun int rc;
2128*4882a593Smuzhiyun
2129*4882a593Smuzhiyun rc = ef4_check_disabled(efx);
2130*4882a593Smuzhiyun if (rc)
2131*4882a593Smuzhiyun return rc;
2132*4882a593Smuzhiyun
2133*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
2134*4882a593Smuzhiyun
2135*4882a593Smuzhiyun ef4_device_detach_sync(efx);
2136*4882a593Smuzhiyun ef4_stop_all(efx);
2137*4882a593Smuzhiyun
2138*4882a593Smuzhiyun mutex_lock(&efx->mac_lock);
2139*4882a593Smuzhiyun net_dev->mtu = new_mtu;
2140*4882a593Smuzhiyun ef4_mac_reconfigure(efx);
2141*4882a593Smuzhiyun mutex_unlock(&efx->mac_lock);
2142*4882a593Smuzhiyun
2143*4882a593Smuzhiyun ef4_start_all(efx);
2144*4882a593Smuzhiyun netif_device_attach(efx->net_dev);
2145*4882a593Smuzhiyun return 0;
2146*4882a593Smuzhiyun }
2147*4882a593Smuzhiyun
ef4_set_mac_address(struct net_device * net_dev,void * data)2148*4882a593Smuzhiyun static int ef4_set_mac_address(struct net_device *net_dev, void *data)
2149*4882a593Smuzhiyun {
2150*4882a593Smuzhiyun struct ef4_nic *efx = netdev_priv(net_dev);
2151*4882a593Smuzhiyun struct sockaddr *addr = data;
2152*4882a593Smuzhiyun u8 *new_addr = addr->sa_data;
2153*4882a593Smuzhiyun u8 old_addr[6];
2154*4882a593Smuzhiyun int rc;
2155*4882a593Smuzhiyun
2156*4882a593Smuzhiyun if (!is_valid_ether_addr(new_addr)) {
2157*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev,
2158*4882a593Smuzhiyun "invalid ethernet MAC address requested: %pM\n",
2159*4882a593Smuzhiyun new_addr);
2160*4882a593Smuzhiyun return -EADDRNOTAVAIL;
2161*4882a593Smuzhiyun }
2162*4882a593Smuzhiyun
2163*4882a593Smuzhiyun /* save old address */
2164*4882a593Smuzhiyun ether_addr_copy(old_addr, net_dev->dev_addr);
2165*4882a593Smuzhiyun ether_addr_copy(net_dev->dev_addr, new_addr);
2166*4882a593Smuzhiyun if (efx->type->set_mac_address) {
2167*4882a593Smuzhiyun rc = efx->type->set_mac_address(efx);
2168*4882a593Smuzhiyun if (rc) {
2169*4882a593Smuzhiyun ether_addr_copy(net_dev->dev_addr, old_addr);
2170*4882a593Smuzhiyun return rc;
2171*4882a593Smuzhiyun }
2172*4882a593Smuzhiyun }
2173*4882a593Smuzhiyun
2174*4882a593Smuzhiyun /* Reconfigure the MAC */
2175*4882a593Smuzhiyun mutex_lock(&efx->mac_lock);
2176*4882a593Smuzhiyun ef4_mac_reconfigure(efx);
2177*4882a593Smuzhiyun mutex_unlock(&efx->mac_lock);
2178*4882a593Smuzhiyun
2179*4882a593Smuzhiyun return 0;
2180*4882a593Smuzhiyun }
2181*4882a593Smuzhiyun
2182*4882a593Smuzhiyun /* Context: netif_addr_lock held, BHs disabled. */
ef4_set_rx_mode(struct net_device * net_dev)2183*4882a593Smuzhiyun static void ef4_set_rx_mode(struct net_device *net_dev)
2184*4882a593Smuzhiyun {
2185*4882a593Smuzhiyun struct ef4_nic *efx = netdev_priv(net_dev);
2186*4882a593Smuzhiyun
2187*4882a593Smuzhiyun if (efx->port_enabled)
2188*4882a593Smuzhiyun queue_work(efx->workqueue, &efx->mac_work);
2189*4882a593Smuzhiyun /* Otherwise ef4_start_port() will do this */
2190*4882a593Smuzhiyun }
2191*4882a593Smuzhiyun
ef4_set_features(struct net_device * net_dev,netdev_features_t data)2192*4882a593Smuzhiyun static int ef4_set_features(struct net_device *net_dev, netdev_features_t data)
2193*4882a593Smuzhiyun {
2194*4882a593Smuzhiyun struct ef4_nic *efx = netdev_priv(net_dev);
2195*4882a593Smuzhiyun int rc;
2196*4882a593Smuzhiyun
2197*4882a593Smuzhiyun /* If disabling RX n-tuple filtering, clear existing filters */
2198*4882a593Smuzhiyun if (net_dev->features & ~data & NETIF_F_NTUPLE) {
2199*4882a593Smuzhiyun rc = efx->type->filter_clear_rx(efx, EF4_FILTER_PRI_MANUAL);
2200*4882a593Smuzhiyun if (rc)
2201*4882a593Smuzhiyun return rc;
2202*4882a593Smuzhiyun }
2203*4882a593Smuzhiyun
2204*4882a593Smuzhiyun /* If Rx VLAN filter is changed, update filters via mac_reconfigure */
2205*4882a593Smuzhiyun if ((net_dev->features ^ data) & NETIF_F_HW_VLAN_CTAG_FILTER) {
2206*4882a593Smuzhiyun /* ef4_set_rx_mode() will schedule MAC work to update filters
2207*4882a593Smuzhiyun * when a new features are finally set in net_dev.
2208*4882a593Smuzhiyun */
2209*4882a593Smuzhiyun ef4_set_rx_mode(net_dev);
2210*4882a593Smuzhiyun }
2211*4882a593Smuzhiyun
2212*4882a593Smuzhiyun return 0;
2213*4882a593Smuzhiyun }
2214*4882a593Smuzhiyun
2215*4882a593Smuzhiyun static const struct net_device_ops ef4_netdev_ops = {
2216*4882a593Smuzhiyun .ndo_open = ef4_net_open,
2217*4882a593Smuzhiyun .ndo_stop = ef4_net_stop,
2218*4882a593Smuzhiyun .ndo_get_stats64 = ef4_net_stats,
2219*4882a593Smuzhiyun .ndo_tx_timeout = ef4_watchdog,
2220*4882a593Smuzhiyun .ndo_start_xmit = ef4_hard_start_xmit,
2221*4882a593Smuzhiyun .ndo_validate_addr = eth_validate_addr,
2222*4882a593Smuzhiyun .ndo_do_ioctl = ef4_ioctl,
2223*4882a593Smuzhiyun .ndo_change_mtu = ef4_change_mtu,
2224*4882a593Smuzhiyun .ndo_set_mac_address = ef4_set_mac_address,
2225*4882a593Smuzhiyun .ndo_set_rx_mode = ef4_set_rx_mode,
2226*4882a593Smuzhiyun .ndo_set_features = ef4_set_features,
2227*4882a593Smuzhiyun .ndo_setup_tc = ef4_setup_tc,
2228*4882a593Smuzhiyun #ifdef CONFIG_RFS_ACCEL
2229*4882a593Smuzhiyun .ndo_rx_flow_steer = ef4_filter_rfs,
2230*4882a593Smuzhiyun #endif
2231*4882a593Smuzhiyun };
2232*4882a593Smuzhiyun
ef4_update_name(struct ef4_nic * efx)2233*4882a593Smuzhiyun static void ef4_update_name(struct ef4_nic *efx)
2234*4882a593Smuzhiyun {
2235*4882a593Smuzhiyun strcpy(efx->name, efx->net_dev->name);
2236*4882a593Smuzhiyun ef4_mtd_rename(efx);
2237*4882a593Smuzhiyun ef4_set_channel_names(efx);
2238*4882a593Smuzhiyun }
2239*4882a593Smuzhiyun
ef4_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)2240*4882a593Smuzhiyun static int ef4_netdev_event(struct notifier_block *this,
2241*4882a593Smuzhiyun unsigned long event, void *ptr)
2242*4882a593Smuzhiyun {
2243*4882a593Smuzhiyun struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
2244*4882a593Smuzhiyun
2245*4882a593Smuzhiyun if ((net_dev->netdev_ops == &ef4_netdev_ops) &&
2246*4882a593Smuzhiyun event == NETDEV_CHANGENAME)
2247*4882a593Smuzhiyun ef4_update_name(netdev_priv(net_dev));
2248*4882a593Smuzhiyun
2249*4882a593Smuzhiyun return NOTIFY_DONE;
2250*4882a593Smuzhiyun }
2251*4882a593Smuzhiyun
2252*4882a593Smuzhiyun static struct notifier_block ef4_netdev_notifier = {
2253*4882a593Smuzhiyun .notifier_call = ef4_netdev_event,
2254*4882a593Smuzhiyun };
2255*4882a593Smuzhiyun
2256*4882a593Smuzhiyun static ssize_t
show_phy_type(struct device * dev,struct device_attribute * attr,char * buf)2257*4882a593Smuzhiyun show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2258*4882a593Smuzhiyun {
2259*4882a593Smuzhiyun struct ef4_nic *efx = dev_get_drvdata(dev);
2260*4882a593Smuzhiyun return sprintf(buf, "%d\n", efx->phy_type);
2261*4882a593Smuzhiyun }
2262*4882a593Smuzhiyun static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
2263*4882a593Smuzhiyun
ef4_register_netdev(struct ef4_nic * efx)2264*4882a593Smuzhiyun static int ef4_register_netdev(struct ef4_nic *efx)
2265*4882a593Smuzhiyun {
2266*4882a593Smuzhiyun struct net_device *net_dev = efx->net_dev;
2267*4882a593Smuzhiyun struct ef4_channel *channel;
2268*4882a593Smuzhiyun int rc;
2269*4882a593Smuzhiyun
2270*4882a593Smuzhiyun net_dev->watchdog_timeo = 5 * HZ;
2271*4882a593Smuzhiyun net_dev->irq = efx->pci_dev->irq;
2272*4882a593Smuzhiyun net_dev->netdev_ops = &ef4_netdev_ops;
2273*4882a593Smuzhiyun net_dev->ethtool_ops = &ef4_ethtool_ops;
2274*4882a593Smuzhiyun net_dev->gso_max_segs = EF4_TSO_MAX_SEGS;
2275*4882a593Smuzhiyun net_dev->min_mtu = EF4_MIN_MTU;
2276*4882a593Smuzhiyun net_dev->max_mtu = EF4_MAX_MTU;
2277*4882a593Smuzhiyun
2278*4882a593Smuzhiyun rtnl_lock();
2279*4882a593Smuzhiyun
2280*4882a593Smuzhiyun /* Enable resets to be scheduled and check whether any were
2281*4882a593Smuzhiyun * already requested. If so, the NIC is probably hosed so we
2282*4882a593Smuzhiyun * abort.
2283*4882a593Smuzhiyun */
2284*4882a593Smuzhiyun efx->state = STATE_READY;
2285*4882a593Smuzhiyun smp_mb(); /* ensure we change state before checking reset_pending */
2286*4882a593Smuzhiyun if (efx->reset_pending) {
2287*4882a593Smuzhiyun netif_err(efx, probe, efx->net_dev,
2288*4882a593Smuzhiyun "aborting probe due to scheduled reset\n");
2289*4882a593Smuzhiyun rc = -EIO;
2290*4882a593Smuzhiyun goto fail_locked;
2291*4882a593Smuzhiyun }
2292*4882a593Smuzhiyun
2293*4882a593Smuzhiyun rc = dev_alloc_name(net_dev, net_dev->name);
2294*4882a593Smuzhiyun if (rc < 0)
2295*4882a593Smuzhiyun goto fail_locked;
2296*4882a593Smuzhiyun ef4_update_name(efx);
2297*4882a593Smuzhiyun
2298*4882a593Smuzhiyun /* Always start with carrier off; PHY events will detect the link */
2299*4882a593Smuzhiyun netif_carrier_off(net_dev);
2300*4882a593Smuzhiyun
2301*4882a593Smuzhiyun rc = register_netdevice(net_dev);
2302*4882a593Smuzhiyun if (rc)
2303*4882a593Smuzhiyun goto fail_locked;
2304*4882a593Smuzhiyun
2305*4882a593Smuzhiyun ef4_for_each_channel(channel, efx) {
2306*4882a593Smuzhiyun struct ef4_tx_queue *tx_queue;
2307*4882a593Smuzhiyun ef4_for_each_channel_tx_queue(tx_queue, channel)
2308*4882a593Smuzhiyun ef4_init_tx_queue_core_txq(tx_queue);
2309*4882a593Smuzhiyun }
2310*4882a593Smuzhiyun
2311*4882a593Smuzhiyun ef4_associate(efx);
2312*4882a593Smuzhiyun
2313*4882a593Smuzhiyun rtnl_unlock();
2314*4882a593Smuzhiyun
2315*4882a593Smuzhiyun rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2316*4882a593Smuzhiyun if (rc) {
2317*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev,
2318*4882a593Smuzhiyun "failed to init net dev attributes\n");
2319*4882a593Smuzhiyun goto fail_registered;
2320*4882a593Smuzhiyun }
2321*4882a593Smuzhiyun return 0;
2322*4882a593Smuzhiyun
2323*4882a593Smuzhiyun fail_registered:
2324*4882a593Smuzhiyun rtnl_lock();
2325*4882a593Smuzhiyun ef4_dissociate(efx);
2326*4882a593Smuzhiyun unregister_netdevice(net_dev);
2327*4882a593Smuzhiyun fail_locked:
2328*4882a593Smuzhiyun efx->state = STATE_UNINIT;
2329*4882a593Smuzhiyun rtnl_unlock();
2330*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
2331*4882a593Smuzhiyun return rc;
2332*4882a593Smuzhiyun }
2333*4882a593Smuzhiyun
ef4_unregister_netdev(struct ef4_nic * efx)2334*4882a593Smuzhiyun static void ef4_unregister_netdev(struct ef4_nic *efx)
2335*4882a593Smuzhiyun {
2336*4882a593Smuzhiyun if (!efx->net_dev)
2337*4882a593Smuzhiyun return;
2338*4882a593Smuzhiyun
2339*4882a593Smuzhiyun BUG_ON(netdev_priv(efx->net_dev) != efx);
2340*4882a593Smuzhiyun
2341*4882a593Smuzhiyun if (ef4_dev_registered(efx)) {
2342*4882a593Smuzhiyun strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2343*4882a593Smuzhiyun device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2344*4882a593Smuzhiyun unregister_netdev(efx->net_dev);
2345*4882a593Smuzhiyun }
2346*4882a593Smuzhiyun }
2347*4882a593Smuzhiyun
2348*4882a593Smuzhiyun /**************************************************************************
2349*4882a593Smuzhiyun *
2350*4882a593Smuzhiyun * Device reset and suspend
2351*4882a593Smuzhiyun *
2352*4882a593Smuzhiyun **************************************************************************/
2353*4882a593Smuzhiyun
2354*4882a593Smuzhiyun /* Tears down the entire software state and most of the hardware state
2355*4882a593Smuzhiyun * before reset. */
ef4_reset_down(struct ef4_nic * efx,enum reset_type method)2356*4882a593Smuzhiyun void ef4_reset_down(struct ef4_nic *efx, enum reset_type method)
2357*4882a593Smuzhiyun {
2358*4882a593Smuzhiyun EF4_ASSERT_RESET_SERIALISED(efx);
2359*4882a593Smuzhiyun
2360*4882a593Smuzhiyun ef4_stop_all(efx);
2361*4882a593Smuzhiyun ef4_disable_interrupts(efx);
2362*4882a593Smuzhiyun
2363*4882a593Smuzhiyun mutex_lock(&efx->mac_lock);
2364*4882a593Smuzhiyun if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2365*4882a593Smuzhiyun method != RESET_TYPE_DATAPATH)
2366*4882a593Smuzhiyun efx->phy_op->fini(efx);
2367*4882a593Smuzhiyun efx->type->fini(efx);
2368*4882a593Smuzhiyun }
2369*4882a593Smuzhiyun
2370*4882a593Smuzhiyun /* This function will always ensure that the locks acquired in
2371*4882a593Smuzhiyun * ef4_reset_down() are released. A failure return code indicates
2372*4882a593Smuzhiyun * that we were unable to reinitialise the hardware, and the
2373*4882a593Smuzhiyun * driver should be disabled. If ok is false, then the rx and tx
2374*4882a593Smuzhiyun * engines are not restarted, pending a RESET_DISABLE. */
ef4_reset_up(struct ef4_nic * efx,enum reset_type method,bool ok)2375*4882a593Smuzhiyun int ef4_reset_up(struct ef4_nic *efx, enum reset_type method, bool ok)
2376*4882a593Smuzhiyun {
2377*4882a593Smuzhiyun int rc;
2378*4882a593Smuzhiyun
2379*4882a593Smuzhiyun EF4_ASSERT_RESET_SERIALISED(efx);
2380*4882a593Smuzhiyun
2381*4882a593Smuzhiyun /* Ensure that SRAM is initialised even if we're disabling the device */
2382*4882a593Smuzhiyun rc = efx->type->init(efx);
2383*4882a593Smuzhiyun if (rc) {
2384*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
2385*4882a593Smuzhiyun goto fail;
2386*4882a593Smuzhiyun }
2387*4882a593Smuzhiyun
2388*4882a593Smuzhiyun if (!ok)
2389*4882a593Smuzhiyun goto fail;
2390*4882a593Smuzhiyun
2391*4882a593Smuzhiyun if (efx->port_initialized && method != RESET_TYPE_INVISIBLE &&
2392*4882a593Smuzhiyun method != RESET_TYPE_DATAPATH) {
2393*4882a593Smuzhiyun rc = efx->phy_op->init(efx);
2394*4882a593Smuzhiyun if (rc)
2395*4882a593Smuzhiyun goto fail;
2396*4882a593Smuzhiyun rc = efx->phy_op->reconfigure(efx);
2397*4882a593Smuzhiyun if (rc && rc != -EPERM)
2398*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev,
2399*4882a593Smuzhiyun "could not restore PHY settings\n");
2400*4882a593Smuzhiyun }
2401*4882a593Smuzhiyun
2402*4882a593Smuzhiyun rc = ef4_enable_interrupts(efx);
2403*4882a593Smuzhiyun if (rc)
2404*4882a593Smuzhiyun goto fail;
2405*4882a593Smuzhiyun
2406*4882a593Smuzhiyun down_read(&efx->filter_sem);
2407*4882a593Smuzhiyun ef4_restore_filters(efx);
2408*4882a593Smuzhiyun up_read(&efx->filter_sem);
2409*4882a593Smuzhiyun
2410*4882a593Smuzhiyun mutex_unlock(&efx->mac_lock);
2411*4882a593Smuzhiyun
2412*4882a593Smuzhiyun ef4_start_all(efx);
2413*4882a593Smuzhiyun
2414*4882a593Smuzhiyun return 0;
2415*4882a593Smuzhiyun
2416*4882a593Smuzhiyun fail:
2417*4882a593Smuzhiyun efx->port_initialized = false;
2418*4882a593Smuzhiyun
2419*4882a593Smuzhiyun mutex_unlock(&efx->mac_lock);
2420*4882a593Smuzhiyun
2421*4882a593Smuzhiyun return rc;
2422*4882a593Smuzhiyun }
2423*4882a593Smuzhiyun
2424*4882a593Smuzhiyun /* Reset the NIC using the specified method. Note that the reset may
2425*4882a593Smuzhiyun * fail, in which case the card will be left in an unusable state.
2426*4882a593Smuzhiyun *
2427*4882a593Smuzhiyun * Caller must hold the rtnl_lock.
2428*4882a593Smuzhiyun */
ef4_reset(struct ef4_nic * efx,enum reset_type method)2429*4882a593Smuzhiyun int ef4_reset(struct ef4_nic *efx, enum reset_type method)
2430*4882a593Smuzhiyun {
2431*4882a593Smuzhiyun int rc, rc2;
2432*4882a593Smuzhiyun bool disabled;
2433*4882a593Smuzhiyun
2434*4882a593Smuzhiyun netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2435*4882a593Smuzhiyun RESET_TYPE(method));
2436*4882a593Smuzhiyun
2437*4882a593Smuzhiyun ef4_device_detach_sync(efx);
2438*4882a593Smuzhiyun ef4_reset_down(efx, method);
2439*4882a593Smuzhiyun
2440*4882a593Smuzhiyun rc = efx->type->reset(efx, method);
2441*4882a593Smuzhiyun if (rc) {
2442*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
2443*4882a593Smuzhiyun goto out;
2444*4882a593Smuzhiyun }
2445*4882a593Smuzhiyun
2446*4882a593Smuzhiyun /* Clear flags for the scopes we covered. We assume the NIC and
2447*4882a593Smuzhiyun * driver are now quiescent so that there is no race here.
2448*4882a593Smuzhiyun */
2449*4882a593Smuzhiyun if (method < RESET_TYPE_MAX_METHOD)
2450*4882a593Smuzhiyun efx->reset_pending &= -(1 << (method + 1));
2451*4882a593Smuzhiyun else /* it doesn't fit into the well-ordered scope hierarchy */
2452*4882a593Smuzhiyun __clear_bit(method, &efx->reset_pending);
2453*4882a593Smuzhiyun
2454*4882a593Smuzhiyun /* Reinitialise bus-mastering, which may have been turned off before
2455*4882a593Smuzhiyun * the reset was scheduled. This is still appropriate, even in the
2456*4882a593Smuzhiyun * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2457*4882a593Smuzhiyun * can respond to requests. */
2458*4882a593Smuzhiyun pci_set_master(efx->pci_dev);
2459*4882a593Smuzhiyun
2460*4882a593Smuzhiyun out:
2461*4882a593Smuzhiyun /* Leave device stopped if necessary */
2462*4882a593Smuzhiyun disabled = rc ||
2463*4882a593Smuzhiyun method == RESET_TYPE_DISABLE ||
2464*4882a593Smuzhiyun method == RESET_TYPE_RECOVER_OR_DISABLE;
2465*4882a593Smuzhiyun rc2 = ef4_reset_up(efx, method, !disabled);
2466*4882a593Smuzhiyun if (rc2) {
2467*4882a593Smuzhiyun disabled = true;
2468*4882a593Smuzhiyun if (!rc)
2469*4882a593Smuzhiyun rc = rc2;
2470*4882a593Smuzhiyun }
2471*4882a593Smuzhiyun
2472*4882a593Smuzhiyun if (disabled) {
2473*4882a593Smuzhiyun dev_close(efx->net_dev);
2474*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev, "has been disabled\n");
2475*4882a593Smuzhiyun efx->state = STATE_DISABLED;
2476*4882a593Smuzhiyun } else {
2477*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
2478*4882a593Smuzhiyun netif_device_attach(efx->net_dev);
2479*4882a593Smuzhiyun }
2480*4882a593Smuzhiyun return rc;
2481*4882a593Smuzhiyun }
2482*4882a593Smuzhiyun
2483*4882a593Smuzhiyun /* Try recovery mechanisms.
2484*4882a593Smuzhiyun * For now only EEH is supported.
2485*4882a593Smuzhiyun * Returns 0 if the recovery mechanisms are unsuccessful.
2486*4882a593Smuzhiyun * Returns a non-zero value otherwise.
2487*4882a593Smuzhiyun */
ef4_try_recovery(struct ef4_nic * efx)2488*4882a593Smuzhiyun int ef4_try_recovery(struct ef4_nic *efx)
2489*4882a593Smuzhiyun {
2490*4882a593Smuzhiyun #ifdef CONFIG_EEH
2491*4882a593Smuzhiyun /* A PCI error can occur and not be seen by EEH because nothing
2492*4882a593Smuzhiyun * happens on the PCI bus. In this case the driver may fail and
2493*4882a593Smuzhiyun * schedule a 'recover or reset', leading to this recovery handler.
2494*4882a593Smuzhiyun * Manually call the eeh failure check function.
2495*4882a593Smuzhiyun */
2496*4882a593Smuzhiyun struct eeh_dev *eehdev = pci_dev_to_eeh_dev(efx->pci_dev);
2497*4882a593Smuzhiyun if (eeh_dev_check_failure(eehdev)) {
2498*4882a593Smuzhiyun /* The EEH mechanisms will handle the error and reset the
2499*4882a593Smuzhiyun * device if necessary.
2500*4882a593Smuzhiyun */
2501*4882a593Smuzhiyun return 1;
2502*4882a593Smuzhiyun }
2503*4882a593Smuzhiyun #endif
2504*4882a593Smuzhiyun return 0;
2505*4882a593Smuzhiyun }
2506*4882a593Smuzhiyun
2507*4882a593Smuzhiyun /* The worker thread exists so that code that cannot sleep can
2508*4882a593Smuzhiyun * schedule a reset for later.
2509*4882a593Smuzhiyun */
ef4_reset_work(struct work_struct * data)2510*4882a593Smuzhiyun static void ef4_reset_work(struct work_struct *data)
2511*4882a593Smuzhiyun {
2512*4882a593Smuzhiyun struct ef4_nic *efx = container_of(data, struct ef4_nic, reset_work);
2513*4882a593Smuzhiyun unsigned long pending;
2514*4882a593Smuzhiyun enum reset_type method;
2515*4882a593Smuzhiyun
2516*4882a593Smuzhiyun pending = READ_ONCE(efx->reset_pending);
2517*4882a593Smuzhiyun method = fls(pending) - 1;
2518*4882a593Smuzhiyun
2519*4882a593Smuzhiyun if ((method == RESET_TYPE_RECOVER_OR_DISABLE ||
2520*4882a593Smuzhiyun method == RESET_TYPE_RECOVER_OR_ALL) &&
2521*4882a593Smuzhiyun ef4_try_recovery(efx))
2522*4882a593Smuzhiyun return;
2523*4882a593Smuzhiyun
2524*4882a593Smuzhiyun if (!pending)
2525*4882a593Smuzhiyun return;
2526*4882a593Smuzhiyun
2527*4882a593Smuzhiyun rtnl_lock();
2528*4882a593Smuzhiyun
2529*4882a593Smuzhiyun /* We checked the state in ef4_schedule_reset() but it may
2530*4882a593Smuzhiyun * have changed by now. Now that we have the RTNL lock,
2531*4882a593Smuzhiyun * it cannot change again.
2532*4882a593Smuzhiyun */
2533*4882a593Smuzhiyun if (efx->state == STATE_READY)
2534*4882a593Smuzhiyun (void)ef4_reset(efx, method);
2535*4882a593Smuzhiyun
2536*4882a593Smuzhiyun rtnl_unlock();
2537*4882a593Smuzhiyun }
2538*4882a593Smuzhiyun
ef4_schedule_reset(struct ef4_nic * efx,enum reset_type type)2539*4882a593Smuzhiyun void ef4_schedule_reset(struct ef4_nic *efx, enum reset_type type)
2540*4882a593Smuzhiyun {
2541*4882a593Smuzhiyun enum reset_type method;
2542*4882a593Smuzhiyun
2543*4882a593Smuzhiyun if (efx->state == STATE_RECOVERY) {
2544*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev,
2545*4882a593Smuzhiyun "recovering: skip scheduling %s reset\n",
2546*4882a593Smuzhiyun RESET_TYPE(type));
2547*4882a593Smuzhiyun return;
2548*4882a593Smuzhiyun }
2549*4882a593Smuzhiyun
2550*4882a593Smuzhiyun switch (type) {
2551*4882a593Smuzhiyun case RESET_TYPE_INVISIBLE:
2552*4882a593Smuzhiyun case RESET_TYPE_ALL:
2553*4882a593Smuzhiyun case RESET_TYPE_RECOVER_OR_ALL:
2554*4882a593Smuzhiyun case RESET_TYPE_WORLD:
2555*4882a593Smuzhiyun case RESET_TYPE_DISABLE:
2556*4882a593Smuzhiyun case RESET_TYPE_RECOVER_OR_DISABLE:
2557*4882a593Smuzhiyun case RESET_TYPE_DATAPATH:
2558*4882a593Smuzhiyun method = type;
2559*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2560*4882a593Smuzhiyun RESET_TYPE(method));
2561*4882a593Smuzhiyun break;
2562*4882a593Smuzhiyun default:
2563*4882a593Smuzhiyun method = efx->type->map_reset_reason(type);
2564*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev,
2565*4882a593Smuzhiyun "scheduling %s reset for %s\n",
2566*4882a593Smuzhiyun RESET_TYPE(method), RESET_TYPE(type));
2567*4882a593Smuzhiyun break;
2568*4882a593Smuzhiyun }
2569*4882a593Smuzhiyun
2570*4882a593Smuzhiyun set_bit(method, &efx->reset_pending);
2571*4882a593Smuzhiyun smp_mb(); /* ensure we change reset_pending before checking state */
2572*4882a593Smuzhiyun
2573*4882a593Smuzhiyun /* If we're not READY then just leave the flags set as the cue
2574*4882a593Smuzhiyun * to abort probing or reschedule the reset later.
2575*4882a593Smuzhiyun */
2576*4882a593Smuzhiyun if (READ_ONCE(efx->state) != STATE_READY)
2577*4882a593Smuzhiyun return;
2578*4882a593Smuzhiyun
2579*4882a593Smuzhiyun queue_work(reset_workqueue, &efx->reset_work);
2580*4882a593Smuzhiyun }
2581*4882a593Smuzhiyun
2582*4882a593Smuzhiyun /**************************************************************************
2583*4882a593Smuzhiyun *
2584*4882a593Smuzhiyun * List of NICs we support
2585*4882a593Smuzhiyun *
2586*4882a593Smuzhiyun **************************************************************************/
2587*4882a593Smuzhiyun
2588*4882a593Smuzhiyun /* PCI device ID table */
2589*4882a593Smuzhiyun static const struct pci_device_id ef4_pci_table[] = {
2590*4882a593Smuzhiyun {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2591*4882a593Smuzhiyun PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
2592*4882a593Smuzhiyun .driver_data = (unsigned long) &falcon_a1_nic_type},
2593*4882a593Smuzhiyun {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2594*4882a593Smuzhiyun PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
2595*4882a593Smuzhiyun .driver_data = (unsigned long) &falcon_b0_nic_type},
2596*4882a593Smuzhiyun {0} /* end of list */
2597*4882a593Smuzhiyun };
2598*4882a593Smuzhiyun
2599*4882a593Smuzhiyun /**************************************************************************
2600*4882a593Smuzhiyun *
2601*4882a593Smuzhiyun * Dummy PHY/MAC operations
2602*4882a593Smuzhiyun *
2603*4882a593Smuzhiyun * Can be used for some unimplemented operations
2604*4882a593Smuzhiyun * Needed so all function pointers are valid and do not have to be tested
2605*4882a593Smuzhiyun * before use
2606*4882a593Smuzhiyun *
2607*4882a593Smuzhiyun **************************************************************************/
ef4_port_dummy_op_int(struct ef4_nic * efx)2608*4882a593Smuzhiyun int ef4_port_dummy_op_int(struct ef4_nic *efx)
2609*4882a593Smuzhiyun {
2610*4882a593Smuzhiyun return 0;
2611*4882a593Smuzhiyun }
ef4_port_dummy_op_void(struct ef4_nic * efx)2612*4882a593Smuzhiyun void ef4_port_dummy_op_void(struct ef4_nic *efx) {}
2613*4882a593Smuzhiyun
ef4_port_dummy_op_poll(struct ef4_nic * efx)2614*4882a593Smuzhiyun static bool ef4_port_dummy_op_poll(struct ef4_nic *efx)
2615*4882a593Smuzhiyun {
2616*4882a593Smuzhiyun return false;
2617*4882a593Smuzhiyun }
2618*4882a593Smuzhiyun
2619*4882a593Smuzhiyun static const struct ef4_phy_operations ef4_dummy_phy_operations = {
2620*4882a593Smuzhiyun .init = ef4_port_dummy_op_int,
2621*4882a593Smuzhiyun .reconfigure = ef4_port_dummy_op_int,
2622*4882a593Smuzhiyun .poll = ef4_port_dummy_op_poll,
2623*4882a593Smuzhiyun .fini = ef4_port_dummy_op_void,
2624*4882a593Smuzhiyun };
2625*4882a593Smuzhiyun
2626*4882a593Smuzhiyun /**************************************************************************
2627*4882a593Smuzhiyun *
2628*4882a593Smuzhiyun * Data housekeeping
2629*4882a593Smuzhiyun *
2630*4882a593Smuzhiyun **************************************************************************/
2631*4882a593Smuzhiyun
2632*4882a593Smuzhiyun /* This zeroes out and then fills in the invariants in a struct
2633*4882a593Smuzhiyun * ef4_nic (including all sub-structures).
2634*4882a593Smuzhiyun */
ef4_init_struct(struct ef4_nic * efx,struct pci_dev * pci_dev,struct net_device * net_dev)2635*4882a593Smuzhiyun static int ef4_init_struct(struct ef4_nic *efx,
2636*4882a593Smuzhiyun struct pci_dev *pci_dev, struct net_device *net_dev)
2637*4882a593Smuzhiyun {
2638*4882a593Smuzhiyun int i;
2639*4882a593Smuzhiyun
2640*4882a593Smuzhiyun /* Initialise common structures */
2641*4882a593Smuzhiyun INIT_LIST_HEAD(&efx->node);
2642*4882a593Smuzhiyun INIT_LIST_HEAD(&efx->secondary_list);
2643*4882a593Smuzhiyun spin_lock_init(&efx->biu_lock);
2644*4882a593Smuzhiyun #ifdef CONFIG_SFC_FALCON_MTD
2645*4882a593Smuzhiyun INIT_LIST_HEAD(&efx->mtd_list);
2646*4882a593Smuzhiyun #endif
2647*4882a593Smuzhiyun INIT_WORK(&efx->reset_work, ef4_reset_work);
2648*4882a593Smuzhiyun INIT_DELAYED_WORK(&efx->monitor_work, ef4_monitor);
2649*4882a593Smuzhiyun INIT_DELAYED_WORK(&efx->selftest_work, ef4_selftest_async_work);
2650*4882a593Smuzhiyun efx->pci_dev = pci_dev;
2651*4882a593Smuzhiyun efx->msg_enable = debug;
2652*4882a593Smuzhiyun efx->state = STATE_UNINIT;
2653*4882a593Smuzhiyun strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
2654*4882a593Smuzhiyun
2655*4882a593Smuzhiyun efx->net_dev = net_dev;
2656*4882a593Smuzhiyun efx->rx_prefix_size = efx->type->rx_prefix_size;
2657*4882a593Smuzhiyun efx->rx_ip_align =
2658*4882a593Smuzhiyun NET_IP_ALIGN ? (efx->rx_prefix_size + NET_IP_ALIGN) % 4 : 0;
2659*4882a593Smuzhiyun efx->rx_packet_hash_offset =
2660*4882a593Smuzhiyun efx->type->rx_hash_offset - efx->type->rx_prefix_size;
2661*4882a593Smuzhiyun efx->rx_packet_ts_offset =
2662*4882a593Smuzhiyun efx->type->rx_ts_offset - efx->type->rx_prefix_size;
2663*4882a593Smuzhiyun spin_lock_init(&efx->stats_lock);
2664*4882a593Smuzhiyun mutex_init(&efx->mac_lock);
2665*4882a593Smuzhiyun efx->phy_op = &ef4_dummy_phy_operations;
2666*4882a593Smuzhiyun efx->mdio.dev = net_dev;
2667*4882a593Smuzhiyun INIT_WORK(&efx->mac_work, ef4_mac_work);
2668*4882a593Smuzhiyun init_waitqueue_head(&efx->flush_wq);
2669*4882a593Smuzhiyun
2670*4882a593Smuzhiyun for (i = 0; i < EF4_MAX_CHANNELS; i++) {
2671*4882a593Smuzhiyun efx->channel[i] = ef4_alloc_channel(efx, i, NULL);
2672*4882a593Smuzhiyun if (!efx->channel[i])
2673*4882a593Smuzhiyun goto fail;
2674*4882a593Smuzhiyun efx->msi_context[i].efx = efx;
2675*4882a593Smuzhiyun efx->msi_context[i].index = i;
2676*4882a593Smuzhiyun }
2677*4882a593Smuzhiyun
2678*4882a593Smuzhiyun /* Higher numbered interrupt modes are less capable! */
2679*4882a593Smuzhiyun efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2680*4882a593Smuzhiyun interrupt_mode);
2681*4882a593Smuzhiyun
2682*4882a593Smuzhiyun /* Would be good to use the net_dev name, but we're too early */
2683*4882a593Smuzhiyun snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2684*4882a593Smuzhiyun pci_name(pci_dev));
2685*4882a593Smuzhiyun efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
2686*4882a593Smuzhiyun if (!efx->workqueue)
2687*4882a593Smuzhiyun goto fail;
2688*4882a593Smuzhiyun
2689*4882a593Smuzhiyun return 0;
2690*4882a593Smuzhiyun
2691*4882a593Smuzhiyun fail:
2692*4882a593Smuzhiyun ef4_fini_struct(efx);
2693*4882a593Smuzhiyun return -ENOMEM;
2694*4882a593Smuzhiyun }
2695*4882a593Smuzhiyun
ef4_fini_struct(struct ef4_nic * efx)2696*4882a593Smuzhiyun static void ef4_fini_struct(struct ef4_nic *efx)
2697*4882a593Smuzhiyun {
2698*4882a593Smuzhiyun int i;
2699*4882a593Smuzhiyun
2700*4882a593Smuzhiyun for (i = 0; i < EF4_MAX_CHANNELS; i++)
2701*4882a593Smuzhiyun kfree(efx->channel[i]);
2702*4882a593Smuzhiyun
2703*4882a593Smuzhiyun kfree(efx->vpd_sn);
2704*4882a593Smuzhiyun
2705*4882a593Smuzhiyun if (efx->workqueue) {
2706*4882a593Smuzhiyun destroy_workqueue(efx->workqueue);
2707*4882a593Smuzhiyun efx->workqueue = NULL;
2708*4882a593Smuzhiyun }
2709*4882a593Smuzhiyun }
2710*4882a593Smuzhiyun
ef4_update_sw_stats(struct ef4_nic * efx,u64 * stats)2711*4882a593Smuzhiyun void ef4_update_sw_stats(struct ef4_nic *efx, u64 *stats)
2712*4882a593Smuzhiyun {
2713*4882a593Smuzhiyun u64 n_rx_nodesc_trunc = 0;
2714*4882a593Smuzhiyun struct ef4_channel *channel;
2715*4882a593Smuzhiyun
2716*4882a593Smuzhiyun ef4_for_each_channel(channel, efx)
2717*4882a593Smuzhiyun n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
2718*4882a593Smuzhiyun stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
2719*4882a593Smuzhiyun stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
2720*4882a593Smuzhiyun }
2721*4882a593Smuzhiyun
2722*4882a593Smuzhiyun /**************************************************************************
2723*4882a593Smuzhiyun *
2724*4882a593Smuzhiyun * PCI interface
2725*4882a593Smuzhiyun *
2726*4882a593Smuzhiyun **************************************************************************/
2727*4882a593Smuzhiyun
2728*4882a593Smuzhiyun /* Main body of final NIC shutdown code
2729*4882a593Smuzhiyun * This is called only at module unload (or hotplug removal).
2730*4882a593Smuzhiyun */
ef4_pci_remove_main(struct ef4_nic * efx)2731*4882a593Smuzhiyun static void ef4_pci_remove_main(struct ef4_nic *efx)
2732*4882a593Smuzhiyun {
2733*4882a593Smuzhiyun /* Flush reset_work. It can no longer be scheduled since we
2734*4882a593Smuzhiyun * are not READY.
2735*4882a593Smuzhiyun */
2736*4882a593Smuzhiyun BUG_ON(efx->state == STATE_READY);
2737*4882a593Smuzhiyun cancel_work_sync(&efx->reset_work);
2738*4882a593Smuzhiyun
2739*4882a593Smuzhiyun ef4_disable_interrupts(efx);
2740*4882a593Smuzhiyun ef4_nic_fini_interrupt(efx);
2741*4882a593Smuzhiyun ef4_fini_port(efx);
2742*4882a593Smuzhiyun efx->type->fini(efx);
2743*4882a593Smuzhiyun ef4_fini_napi(efx);
2744*4882a593Smuzhiyun ef4_remove_all(efx);
2745*4882a593Smuzhiyun }
2746*4882a593Smuzhiyun
2747*4882a593Smuzhiyun /* Final NIC shutdown
2748*4882a593Smuzhiyun * This is called only at module unload (or hotplug removal). A PF can call
2749*4882a593Smuzhiyun * this on its VFs to ensure they are unbound first.
2750*4882a593Smuzhiyun */
ef4_pci_remove(struct pci_dev * pci_dev)2751*4882a593Smuzhiyun static void ef4_pci_remove(struct pci_dev *pci_dev)
2752*4882a593Smuzhiyun {
2753*4882a593Smuzhiyun struct ef4_nic *efx;
2754*4882a593Smuzhiyun
2755*4882a593Smuzhiyun efx = pci_get_drvdata(pci_dev);
2756*4882a593Smuzhiyun if (!efx)
2757*4882a593Smuzhiyun return;
2758*4882a593Smuzhiyun
2759*4882a593Smuzhiyun /* Mark the NIC as fini, then stop the interface */
2760*4882a593Smuzhiyun rtnl_lock();
2761*4882a593Smuzhiyun ef4_dissociate(efx);
2762*4882a593Smuzhiyun dev_close(efx->net_dev);
2763*4882a593Smuzhiyun ef4_disable_interrupts(efx);
2764*4882a593Smuzhiyun efx->state = STATE_UNINIT;
2765*4882a593Smuzhiyun rtnl_unlock();
2766*4882a593Smuzhiyun
2767*4882a593Smuzhiyun ef4_unregister_netdev(efx);
2768*4882a593Smuzhiyun
2769*4882a593Smuzhiyun ef4_mtd_remove(efx);
2770*4882a593Smuzhiyun
2771*4882a593Smuzhiyun ef4_pci_remove_main(efx);
2772*4882a593Smuzhiyun
2773*4882a593Smuzhiyun ef4_fini_io(efx);
2774*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
2775*4882a593Smuzhiyun
2776*4882a593Smuzhiyun ef4_fini_struct(efx);
2777*4882a593Smuzhiyun free_netdev(efx->net_dev);
2778*4882a593Smuzhiyun
2779*4882a593Smuzhiyun pci_disable_pcie_error_reporting(pci_dev);
2780*4882a593Smuzhiyun };
2781*4882a593Smuzhiyun
2782*4882a593Smuzhiyun /* NIC VPD information
2783*4882a593Smuzhiyun * Called during probe to display the part number of the
2784*4882a593Smuzhiyun * installed NIC. VPD is potentially very large but this should
2785*4882a593Smuzhiyun * always appear within the first 512 bytes.
2786*4882a593Smuzhiyun */
2787*4882a593Smuzhiyun #define SFC_VPD_LEN 512
ef4_probe_vpd_strings(struct ef4_nic * efx)2788*4882a593Smuzhiyun static void ef4_probe_vpd_strings(struct ef4_nic *efx)
2789*4882a593Smuzhiyun {
2790*4882a593Smuzhiyun struct pci_dev *dev = efx->pci_dev;
2791*4882a593Smuzhiyun char vpd_data[SFC_VPD_LEN];
2792*4882a593Smuzhiyun ssize_t vpd_size;
2793*4882a593Smuzhiyun int ro_start, ro_size, i, j;
2794*4882a593Smuzhiyun
2795*4882a593Smuzhiyun /* Get the vpd data from the device */
2796*4882a593Smuzhiyun vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2797*4882a593Smuzhiyun if (vpd_size <= 0) {
2798*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2799*4882a593Smuzhiyun return;
2800*4882a593Smuzhiyun }
2801*4882a593Smuzhiyun
2802*4882a593Smuzhiyun /* Get the Read only section */
2803*4882a593Smuzhiyun ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2804*4882a593Smuzhiyun if (ro_start < 0) {
2805*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2806*4882a593Smuzhiyun return;
2807*4882a593Smuzhiyun }
2808*4882a593Smuzhiyun
2809*4882a593Smuzhiyun ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
2810*4882a593Smuzhiyun j = ro_size;
2811*4882a593Smuzhiyun i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
2812*4882a593Smuzhiyun if (i + j > vpd_size)
2813*4882a593Smuzhiyun j = vpd_size - i;
2814*4882a593Smuzhiyun
2815*4882a593Smuzhiyun /* Get the Part number */
2816*4882a593Smuzhiyun i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2817*4882a593Smuzhiyun if (i < 0) {
2818*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2819*4882a593Smuzhiyun return;
2820*4882a593Smuzhiyun }
2821*4882a593Smuzhiyun
2822*4882a593Smuzhiyun j = pci_vpd_info_field_size(&vpd_data[i]);
2823*4882a593Smuzhiyun i += PCI_VPD_INFO_FLD_HDR_SIZE;
2824*4882a593Smuzhiyun if (i + j > vpd_size) {
2825*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2826*4882a593Smuzhiyun return;
2827*4882a593Smuzhiyun }
2828*4882a593Smuzhiyun
2829*4882a593Smuzhiyun netif_info(efx, drv, efx->net_dev,
2830*4882a593Smuzhiyun "Part Number : %.*s\n", j, &vpd_data[i]);
2831*4882a593Smuzhiyun
2832*4882a593Smuzhiyun i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
2833*4882a593Smuzhiyun j = ro_size;
2834*4882a593Smuzhiyun i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
2835*4882a593Smuzhiyun if (i < 0) {
2836*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
2837*4882a593Smuzhiyun return;
2838*4882a593Smuzhiyun }
2839*4882a593Smuzhiyun
2840*4882a593Smuzhiyun j = pci_vpd_info_field_size(&vpd_data[i]);
2841*4882a593Smuzhiyun i += PCI_VPD_INFO_FLD_HDR_SIZE;
2842*4882a593Smuzhiyun if (i + j > vpd_size) {
2843*4882a593Smuzhiyun netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
2844*4882a593Smuzhiyun return;
2845*4882a593Smuzhiyun }
2846*4882a593Smuzhiyun
2847*4882a593Smuzhiyun efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
2848*4882a593Smuzhiyun if (!efx->vpd_sn)
2849*4882a593Smuzhiyun return;
2850*4882a593Smuzhiyun
2851*4882a593Smuzhiyun snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
2852*4882a593Smuzhiyun }
2853*4882a593Smuzhiyun
2854*4882a593Smuzhiyun
2855*4882a593Smuzhiyun /* Main body of NIC initialisation
2856*4882a593Smuzhiyun * This is called at module load (or hotplug insertion, theoretically).
2857*4882a593Smuzhiyun */
ef4_pci_probe_main(struct ef4_nic * efx)2858*4882a593Smuzhiyun static int ef4_pci_probe_main(struct ef4_nic *efx)
2859*4882a593Smuzhiyun {
2860*4882a593Smuzhiyun int rc;
2861*4882a593Smuzhiyun
2862*4882a593Smuzhiyun /* Do start-of-day initialisation */
2863*4882a593Smuzhiyun rc = ef4_probe_all(efx);
2864*4882a593Smuzhiyun if (rc)
2865*4882a593Smuzhiyun goto fail1;
2866*4882a593Smuzhiyun
2867*4882a593Smuzhiyun ef4_init_napi(efx);
2868*4882a593Smuzhiyun
2869*4882a593Smuzhiyun rc = efx->type->init(efx);
2870*4882a593Smuzhiyun if (rc) {
2871*4882a593Smuzhiyun netif_err(efx, probe, efx->net_dev,
2872*4882a593Smuzhiyun "failed to initialise NIC\n");
2873*4882a593Smuzhiyun goto fail3;
2874*4882a593Smuzhiyun }
2875*4882a593Smuzhiyun
2876*4882a593Smuzhiyun rc = ef4_init_port(efx);
2877*4882a593Smuzhiyun if (rc) {
2878*4882a593Smuzhiyun netif_err(efx, probe, efx->net_dev,
2879*4882a593Smuzhiyun "failed to initialise port\n");
2880*4882a593Smuzhiyun goto fail4;
2881*4882a593Smuzhiyun }
2882*4882a593Smuzhiyun
2883*4882a593Smuzhiyun rc = ef4_nic_init_interrupt(efx);
2884*4882a593Smuzhiyun if (rc)
2885*4882a593Smuzhiyun goto fail5;
2886*4882a593Smuzhiyun rc = ef4_enable_interrupts(efx);
2887*4882a593Smuzhiyun if (rc)
2888*4882a593Smuzhiyun goto fail6;
2889*4882a593Smuzhiyun
2890*4882a593Smuzhiyun return 0;
2891*4882a593Smuzhiyun
2892*4882a593Smuzhiyun fail6:
2893*4882a593Smuzhiyun ef4_nic_fini_interrupt(efx);
2894*4882a593Smuzhiyun fail5:
2895*4882a593Smuzhiyun ef4_fini_port(efx);
2896*4882a593Smuzhiyun fail4:
2897*4882a593Smuzhiyun efx->type->fini(efx);
2898*4882a593Smuzhiyun fail3:
2899*4882a593Smuzhiyun ef4_fini_napi(efx);
2900*4882a593Smuzhiyun ef4_remove_all(efx);
2901*4882a593Smuzhiyun fail1:
2902*4882a593Smuzhiyun return rc;
2903*4882a593Smuzhiyun }
2904*4882a593Smuzhiyun
2905*4882a593Smuzhiyun /* NIC initialisation
2906*4882a593Smuzhiyun *
2907*4882a593Smuzhiyun * This is called at module load (or hotplug insertion,
2908*4882a593Smuzhiyun * theoretically). It sets up PCI mappings, resets the NIC,
2909*4882a593Smuzhiyun * sets up and registers the network devices with the kernel and hooks
2910*4882a593Smuzhiyun * the interrupt service routine. It does not prepare the device for
2911*4882a593Smuzhiyun * transmission; this is left to the first time one of the network
2912*4882a593Smuzhiyun * interfaces is brought up (i.e. ef4_net_open).
2913*4882a593Smuzhiyun */
ef4_pci_probe(struct pci_dev * pci_dev,const struct pci_device_id * entry)2914*4882a593Smuzhiyun static int ef4_pci_probe(struct pci_dev *pci_dev,
2915*4882a593Smuzhiyun const struct pci_device_id *entry)
2916*4882a593Smuzhiyun {
2917*4882a593Smuzhiyun struct net_device *net_dev;
2918*4882a593Smuzhiyun struct ef4_nic *efx;
2919*4882a593Smuzhiyun int rc;
2920*4882a593Smuzhiyun
2921*4882a593Smuzhiyun /* Allocate and initialise a struct net_device and struct ef4_nic */
2922*4882a593Smuzhiyun net_dev = alloc_etherdev_mqs(sizeof(*efx), EF4_MAX_CORE_TX_QUEUES,
2923*4882a593Smuzhiyun EF4_MAX_RX_QUEUES);
2924*4882a593Smuzhiyun if (!net_dev)
2925*4882a593Smuzhiyun return -ENOMEM;
2926*4882a593Smuzhiyun efx = netdev_priv(net_dev);
2927*4882a593Smuzhiyun efx->type = (const struct ef4_nic_type *) entry->driver_data;
2928*4882a593Smuzhiyun efx->fixed_features |= NETIF_F_HIGHDMA;
2929*4882a593Smuzhiyun
2930*4882a593Smuzhiyun pci_set_drvdata(pci_dev, efx);
2931*4882a593Smuzhiyun SET_NETDEV_DEV(net_dev, &pci_dev->dev);
2932*4882a593Smuzhiyun rc = ef4_init_struct(efx, pci_dev, net_dev);
2933*4882a593Smuzhiyun if (rc)
2934*4882a593Smuzhiyun goto fail1;
2935*4882a593Smuzhiyun
2936*4882a593Smuzhiyun netif_info(efx, probe, efx->net_dev,
2937*4882a593Smuzhiyun "Solarflare NIC detected\n");
2938*4882a593Smuzhiyun
2939*4882a593Smuzhiyun ef4_probe_vpd_strings(efx);
2940*4882a593Smuzhiyun
2941*4882a593Smuzhiyun /* Set up basic I/O (BAR mappings etc) */
2942*4882a593Smuzhiyun rc = ef4_init_io(efx);
2943*4882a593Smuzhiyun if (rc)
2944*4882a593Smuzhiyun goto fail2;
2945*4882a593Smuzhiyun
2946*4882a593Smuzhiyun rc = ef4_pci_probe_main(efx);
2947*4882a593Smuzhiyun if (rc)
2948*4882a593Smuzhiyun goto fail3;
2949*4882a593Smuzhiyun
2950*4882a593Smuzhiyun net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
2951*4882a593Smuzhiyun NETIF_F_RXCSUM);
2952*4882a593Smuzhiyun /* Mask for features that also apply to VLAN devices */
2953*4882a593Smuzhiyun net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
2954*4882a593Smuzhiyun NETIF_F_HIGHDMA | NETIF_F_RXCSUM);
2955*4882a593Smuzhiyun
2956*4882a593Smuzhiyun net_dev->hw_features = net_dev->features & ~efx->fixed_features;
2957*4882a593Smuzhiyun
2958*4882a593Smuzhiyun /* Disable VLAN filtering by default. It may be enforced if
2959*4882a593Smuzhiyun * the feature is fixed (i.e. VLAN filters are required to
2960*4882a593Smuzhiyun * receive VLAN tagged packets due to vPort restrictions).
2961*4882a593Smuzhiyun */
2962*4882a593Smuzhiyun net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
2963*4882a593Smuzhiyun net_dev->features |= efx->fixed_features;
2964*4882a593Smuzhiyun
2965*4882a593Smuzhiyun rc = ef4_register_netdev(efx);
2966*4882a593Smuzhiyun if (rc)
2967*4882a593Smuzhiyun goto fail4;
2968*4882a593Smuzhiyun
2969*4882a593Smuzhiyun netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
2970*4882a593Smuzhiyun
2971*4882a593Smuzhiyun /* Try to create MTDs, but allow this to fail */
2972*4882a593Smuzhiyun rtnl_lock();
2973*4882a593Smuzhiyun rc = ef4_mtd_probe(efx);
2974*4882a593Smuzhiyun rtnl_unlock();
2975*4882a593Smuzhiyun if (rc && rc != -EPERM)
2976*4882a593Smuzhiyun netif_warn(efx, probe, efx->net_dev,
2977*4882a593Smuzhiyun "failed to create MTDs (%d)\n", rc);
2978*4882a593Smuzhiyun
2979*4882a593Smuzhiyun rc = pci_enable_pcie_error_reporting(pci_dev);
2980*4882a593Smuzhiyun if (rc && rc != -EINVAL)
2981*4882a593Smuzhiyun netif_notice(efx, probe, efx->net_dev,
2982*4882a593Smuzhiyun "PCIE error reporting unavailable (%d).\n",
2983*4882a593Smuzhiyun rc);
2984*4882a593Smuzhiyun
2985*4882a593Smuzhiyun return 0;
2986*4882a593Smuzhiyun
2987*4882a593Smuzhiyun fail4:
2988*4882a593Smuzhiyun ef4_pci_remove_main(efx);
2989*4882a593Smuzhiyun fail3:
2990*4882a593Smuzhiyun ef4_fini_io(efx);
2991*4882a593Smuzhiyun fail2:
2992*4882a593Smuzhiyun ef4_fini_struct(efx);
2993*4882a593Smuzhiyun fail1:
2994*4882a593Smuzhiyun WARN_ON(rc > 0);
2995*4882a593Smuzhiyun netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
2996*4882a593Smuzhiyun free_netdev(net_dev);
2997*4882a593Smuzhiyun return rc;
2998*4882a593Smuzhiyun }
2999*4882a593Smuzhiyun
ef4_pm_freeze(struct device * dev)3000*4882a593Smuzhiyun static int ef4_pm_freeze(struct device *dev)
3001*4882a593Smuzhiyun {
3002*4882a593Smuzhiyun struct ef4_nic *efx = dev_get_drvdata(dev);
3003*4882a593Smuzhiyun
3004*4882a593Smuzhiyun rtnl_lock();
3005*4882a593Smuzhiyun
3006*4882a593Smuzhiyun if (efx->state != STATE_DISABLED) {
3007*4882a593Smuzhiyun efx->state = STATE_UNINIT;
3008*4882a593Smuzhiyun
3009*4882a593Smuzhiyun ef4_device_detach_sync(efx);
3010*4882a593Smuzhiyun
3011*4882a593Smuzhiyun ef4_stop_all(efx);
3012*4882a593Smuzhiyun ef4_disable_interrupts(efx);
3013*4882a593Smuzhiyun }
3014*4882a593Smuzhiyun
3015*4882a593Smuzhiyun rtnl_unlock();
3016*4882a593Smuzhiyun
3017*4882a593Smuzhiyun return 0;
3018*4882a593Smuzhiyun }
3019*4882a593Smuzhiyun
ef4_pm_thaw(struct device * dev)3020*4882a593Smuzhiyun static int ef4_pm_thaw(struct device *dev)
3021*4882a593Smuzhiyun {
3022*4882a593Smuzhiyun int rc;
3023*4882a593Smuzhiyun struct ef4_nic *efx = dev_get_drvdata(dev);
3024*4882a593Smuzhiyun
3025*4882a593Smuzhiyun rtnl_lock();
3026*4882a593Smuzhiyun
3027*4882a593Smuzhiyun if (efx->state != STATE_DISABLED) {
3028*4882a593Smuzhiyun rc = ef4_enable_interrupts(efx);
3029*4882a593Smuzhiyun if (rc)
3030*4882a593Smuzhiyun goto fail;
3031*4882a593Smuzhiyun
3032*4882a593Smuzhiyun mutex_lock(&efx->mac_lock);
3033*4882a593Smuzhiyun efx->phy_op->reconfigure(efx);
3034*4882a593Smuzhiyun mutex_unlock(&efx->mac_lock);
3035*4882a593Smuzhiyun
3036*4882a593Smuzhiyun ef4_start_all(efx);
3037*4882a593Smuzhiyun
3038*4882a593Smuzhiyun netif_device_attach(efx->net_dev);
3039*4882a593Smuzhiyun
3040*4882a593Smuzhiyun efx->state = STATE_READY;
3041*4882a593Smuzhiyun
3042*4882a593Smuzhiyun efx->type->resume_wol(efx);
3043*4882a593Smuzhiyun }
3044*4882a593Smuzhiyun
3045*4882a593Smuzhiyun rtnl_unlock();
3046*4882a593Smuzhiyun
3047*4882a593Smuzhiyun /* Reschedule any quenched resets scheduled during ef4_pm_freeze() */
3048*4882a593Smuzhiyun queue_work(reset_workqueue, &efx->reset_work);
3049*4882a593Smuzhiyun
3050*4882a593Smuzhiyun return 0;
3051*4882a593Smuzhiyun
3052*4882a593Smuzhiyun fail:
3053*4882a593Smuzhiyun rtnl_unlock();
3054*4882a593Smuzhiyun
3055*4882a593Smuzhiyun return rc;
3056*4882a593Smuzhiyun }
3057*4882a593Smuzhiyun
ef4_pm_poweroff(struct device * dev)3058*4882a593Smuzhiyun static int ef4_pm_poweroff(struct device *dev)
3059*4882a593Smuzhiyun {
3060*4882a593Smuzhiyun struct pci_dev *pci_dev = to_pci_dev(dev);
3061*4882a593Smuzhiyun struct ef4_nic *efx = pci_get_drvdata(pci_dev);
3062*4882a593Smuzhiyun
3063*4882a593Smuzhiyun efx->type->fini(efx);
3064*4882a593Smuzhiyun
3065*4882a593Smuzhiyun efx->reset_pending = 0;
3066*4882a593Smuzhiyun
3067*4882a593Smuzhiyun pci_save_state(pci_dev);
3068*4882a593Smuzhiyun return pci_set_power_state(pci_dev, PCI_D3hot);
3069*4882a593Smuzhiyun }
3070*4882a593Smuzhiyun
3071*4882a593Smuzhiyun /* Used for both resume and restore */
ef4_pm_resume(struct device * dev)3072*4882a593Smuzhiyun static int ef4_pm_resume(struct device *dev)
3073*4882a593Smuzhiyun {
3074*4882a593Smuzhiyun struct pci_dev *pci_dev = to_pci_dev(dev);
3075*4882a593Smuzhiyun struct ef4_nic *efx = pci_get_drvdata(pci_dev);
3076*4882a593Smuzhiyun int rc;
3077*4882a593Smuzhiyun
3078*4882a593Smuzhiyun rc = pci_set_power_state(pci_dev, PCI_D0);
3079*4882a593Smuzhiyun if (rc)
3080*4882a593Smuzhiyun return rc;
3081*4882a593Smuzhiyun pci_restore_state(pci_dev);
3082*4882a593Smuzhiyun rc = pci_enable_device(pci_dev);
3083*4882a593Smuzhiyun if (rc)
3084*4882a593Smuzhiyun return rc;
3085*4882a593Smuzhiyun pci_set_master(efx->pci_dev);
3086*4882a593Smuzhiyun rc = efx->type->reset(efx, RESET_TYPE_ALL);
3087*4882a593Smuzhiyun if (rc)
3088*4882a593Smuzhiyun return rc;
3089*4882a593Smuzhiyun rc = efx->type->init(efx);
3090*4882a593Smuzhiyun if (rc)
3091*4882a593Smuzhiyun return rc;
3092*4882a593Smuzhiyun rc = ef4_pm_thaw(dev);
3093*4882a593Smuzhiyun return rc;
3094*4882a593Smuzhiyun }
3095*4882a593Smuzhiyun
ef4_pm_suspend(struct device * dev)3096*4882a593Smuzhiyun static int ef4_pm_suspend(struct device *dev)
3097*4882a593Smuzhiyun {
3098*4882a593Smuzhiyun int rc;
3099*4882a593Smuzhiyun
3100*4882a593Smuzhiyun ef4_pm_freeze(dev);
3101*4882a593Smuzhiyun rc = ef4_pm_poweroff(dev);
3102*4882a593Smuzhiyun if (rc)
3103*4882a593Smuzhiyun ef4_pm_resume(dev);
3104*4882a593Smuzhiyun return rc;
3105*4882a593Smuzhiyun }
3106*4882a593Smuzhiyun
3107*4882a593Smuzhiyun static const struct dev_pm_ops ef4_pm_ops = {
3108*4882a593Smuzhiyun .suspend = ef4_pm_suspend,
3109*4882a593Smuzhiyun .resume = ef4_pm_resume,
3110*4882a593Smuzhiyun .freeze = ef4_pm_freeze,
3111*4882a593Smuzhiyun .thaw = ef4_pm_thaw,
3112*4882a593Smuzhiyun .poweroff = ef4_pm_poweroff,
3113*4882a593Smuzhiyun .restore = ef4_pm_resume,
3114*4882a593Smuzhiyun };
3115*4882a593Smuzhiyun
3116*4882a593Smuzhiyun /* A PCI error affecting this device was detected.
3117*4882a593Smuzhiyun * At this point MMIO and DMA may be disabled.
3118*4882a593Smuzhiyun * Stop the software path and request a slot reset.
3119*4882a593Smuzhiyun */
ef4_io_error_detected(struct pci_dev * pdev,pci_channel_state_t state)3120*4882a593Smuzhiyun static pci_ers_result_t ef4_io_error_detected(struct pci_dev *pdev,
3121*4882a593Smuzhiyun pci_channel_state_t state)
3122*4882a593Smuzhiyun {
3123*4882a593Smuzhiyun pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3124*4882a593Smuzhiyun struct ef4_nic *efx = pci_get_drvdata(pdev);
3125*4882a593Smuzhiyun
3126*4882a593Smuzhiyun if (state == pci_channel_io_perm_failure)
3127*4882a593Smuzhiyun return PCI_ERS_RESULT_DISCONNECT;
3128*4882a593Smuzhiyun
3129*4882a593Smuzhiyun rtnl_lock();
3130*4882a593Smuzhiyun
3131*4882a593Smuzhiyun if (efx->state != STATE_DISABLED) {
3132*4882a593Smuzhiyun efx->state = STATE_RECOVERY;
3133*4882a593Smuzhiyun efx->reset_pending = 0;
3134*4882a593Smuzhiyun
3135*4882a593Smuzhiyun ef4_device_detach_sync(efx);
3136*4882a593Smuzhiyun
3137*4882a593Smuzhiyun ef4_stop_all(efx);
3138*4882a593Smuzhiyun ef4_disable_interrupts(efx);
3139*4882a593Smuzhiyun
3140*4882a593Smuzhiyun status = PCI_ERS_RESULT_NEED_RESET;
3141*4882a593Smuzhiyun } else {
3142*4882a593Smuzhiyun /* If the interface is disabled we don't want to do anything
3143*4882a593Smuzhiyun * with it.
3144*4882a593Smuzhiyun */
3145*4882a593Smuzhiyun status = PCI_ERS_RESULT_RECOVERED;
3146*4882a593Smuzhiyun }
3147*4882a593Smuzhiyun
3148*4882a593Smuzhiyun rtnl_unlock();
3149*4882a593Smuzhiyun
3150*4882a593Smuzhiyun pci_disable_device(pdev);
3151*4882a593Smuzhiyun
3152*4882a593Smuzhiyun return status;
3153*4882a593Smuzhiyun }
3154*4882a593Smuzhiyun
3155*4882a593Smuzhiyun /* Fake a successful reset, which will be performed later in ef4_io_resume. */
ef4_io_slot_reset(struct pci_dev * pdev)3156*4882a593Smuzhiyun static pci_ers_result_t ef4_io_slot_reset(struct pci_dev *pdev)
3157*4882a593Smuzhiyun {
3158*4882a593Smuzhiyun struct ef4_nic *efx = pci_get_drvdata(pdev);
3159*4882a593Smuzhiyun pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
3160*4882a593Smuzhiyun
3161*4882a593Smuzhiyun if (pci_enable_device(pdev)) {
3162*4882a593Smuzhiyun netif_err(efx, hw, efx->net_dev,
3163*4882a593Smuzhiyun "Cannot re-enable PCI device after reset.\n");
3164*4882a593Smuzhiyun status = PCI_ERS_RESULT_DISCONNECT;
3165*4882a593Smuzhiyun }
3166*4882a593Smuzhiyun
3167*4882a593Smuzhiyun return status;
3168*4882a593Smuzhiyun }
3169*4882a593Smuzhiyun
3170*4882a593Smuzhiyun /* Perform the actual reset and resume I/O operations. */
ef4_io_resume(struct pci_dev * pdev)3171*4882a593Smuzhiyun static void ef4_io_resume(struct pci_dev *pdev)
3172*4882a593Smuzhiyun {
3173*4882a593Smuzhiyun struct ef4_nic *efx = pci_get_drvdata(pdev);
3174*4882a593Smuzhiyun int rc;
3175*4882a593Smuzhiyun
3176*4882a593Smuzhiyun rtnl_lock();
3177*4882a593Smuzhiyun
3178*4882a593Smuzhiyun if (efx->state == STATE_DISABLED)
3179*4882a593Smuzhiyun goto out;
3180*4882a593Smuzhiyun
3181*4882a593Smuzhiyun rc = ef4_reset(efx, RESET_TYPE_ALL);
3182*4882a593Smuzhiyun if (rc) {
3183*4882a593Smuzhiyun netif_err(efx, hw, efx->net_dev,
3184*4882a593Smuzhiyun "ef4_reset failed after PCI error (%d)\n", rc);
3185*4882a593Smuzhiyun } else {
3186*4882a593Smuzhiyun efx->state = STATE_READY;
3187*4882a593Smuzhiyun netif_dbg(efx, hw, efx->net_dev,
3188*4882a593Smuzhiyun "Done resetting and resuming IO after PCI error.\n");
3189*4882a593Smuzhiyun }
3190*4882a593Smuzhiyun
3191*4882a593Smuzhiyun out:
3192*4882a593Smuzhiyun rtnl_unlock();
3193*4882a593Smuzhiyun }
3194*4882a593Smuzhiyun
3195*4882a593Smuzhiyun /* For simplicity and reliability, we always require a slot reset and try to
3196*4882a593Smuzhiyun * reset the hardware when a pci error affecting the device is detected.
3197*4882a593Smuzhiyun * We leave both the link_reset and mmio_enabled callback unimplemented:
3198*4882a593Smuzhiyun * with our request for slot reset the mmio_enabled callback will never be
3199*4882a593Smuzhiyun * called, and the link_reset callback is not used by AER or EEH mechanisms.
3200*4882a593Smuzhiyun */
3201*4882a593Smuzhiyun static const struct pci_error_handlers ef4_err_handlers = {
3202*4882a593Smuzhiyun .error_detected = ef4_io_error_detected,
3203*4882a593Smuzhiyun .slot_reset = ef4_io_slot_reset,
3204*4882a593Smuzhiyun .resume = ef4_io_resume,
3205*4882a593Smuzhiyun };
3206*4882a593Smuzhiyun
3207*4882a593Smuzhiyun static struct pci_driver ef4_pci_driver = {
3208*4882a593Smuzhiyun .name = KBUILD_MODNAME,
3209*4882a593Smuzhiyun .id_table = ef4_pci_table,
3210*4882a593Smuzhiyun .probe = ef4_pci_probe,
3211*4882a593Smuzhiyun .remove = ef4_pci_remove,
3212*4882a593Smuzhiyun .driver.pm = &ef4_pm_ops,
3213*4882a593Smuzhiyun .err_handler = &ef4_err_handlers,
3214*4882a593Smuzhiyun };
3215*4882a593Smuzhiyun
3216*4882a593Smuzhiyun /**************************************************************************
3217*4882a593Smuzhiyun *
3218*4882a593Smuzhiyun * Kernel module interface
3219*4882a593Smuzhiyun *
3220*4882a593Smuzhiyun *************************************************************************/
3221*4882a593Smuzhiyun
3222*4882a593Smuzhiyun module_param(interrupt_mode, uint, 0444);
3223*4882a593Smuzhiyun MODULE_PARM_DESC(interrupt_mode,
3224*4882a593Smuzhiyun "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
3225*4882a593Smuzhiyun
ef4_init_module(void)3226*4882a593Smuzhiyun static int __init ef4_init_module(void)
3227*4882a593Smuzhiyun {
3228*4882a593Smuzhiyun int rc;
3229*4882a593Smuzhiyun
3230*4882a593Smuzhiyun printk(KERN_INFO "Solarflare Falcon driver v" EF4_DRIVER_VERSION "\n");
3231*4882a593Smuzhiyun
3232*4882a593Smuzhiyun rc = register_netdevice_notifier(&ef4_netdev_notifier);
3233*4882a593Smuzhiyun if (rc)
3234*4882a593Smuzhiyun goto err_notifier;
3235*4882a593Smuzhiyun
3236*4882a593Smuzhiyun reset_workqueue = create_singlethread_workqueue("sfc_reset");
3237*4882a593Smuzhiyun if (!reset_workqueue) {
3238*4882a593Smuzhiyun rc = -ENOMEM;
3239*4882a593Smuzhiyun goto err_reset;
3240*4882a593Smuzhiyun }
3241*4882a593Smuzhiyun
3242*4882a593Smuzhiyun rc = pci_register_driver(&ef4_pci_driver);
3243*4882a593Smuzhiyun if (rc < 0)
3244*4882a593Smuzhiyun goto err_pci;
3245*4882a593Smuzhiyun
3246*4882a593Smuzhiyun return 0;
3247*4882a593Smuzhiyun
3248*4882a593Smuzhiyun err_pci:
3249*4882a593Smuzhiyun destroy_workqueue(reset_workqueue);
3250*4882a593Smuzhiyun err_reset:
3251*4882a593Smuzhiyun unregister_netdevice_notifier(&ef4_netdev_notifier);
3252*4882a593Smuzhiyun err_notifier:
3253*4882a593Smuzhiyun return rc;
3254*4882a593Smuzhiyun }
3255*4882a593Smuzhiyun
ef4_exit_module(void)3256*4882a593Smuzhiyun static void __exit ef4_exit_module(void)
3257*4882a593Smuzhiyun {
3258*4882a593Smuzhiyun printk(KERN_INFO "Solarflare Falcon driver unloading\n");
3259*4882a593Smuzhiyun
3260*4882a593Smuzhiyun pci_unregister_driver(&ef4_pci_driver);
3261*4882a593Smuzhiyun destroy_workqueue(reset_workqueue);
3262*4882a593Smuzhiyun unregister_netdevice_notifier(&ef4_netdev_notifier);
3263*4882a593Smuzhiyun
3264*4882a593Smuzhiyun }
3265*4882a593Smuzhiyun
3266*4882a593Smuzhiyun module_init(ef4_init_module);
3267*4882a593Smuzhiyun module_exit(ef4_exit_module);
3268*4882a593Smuzhiyun
3269*4882a593Smuzhiyun MODULE_AUTHOR("Solarflare Communications and "
3270*4882a593Smuzhiyun "Michael Brown <mbrown@fensystems.co.uk>");
3271*4882a593Smuzhiyun MODULE_DESCRIPTION("Solarflare Falcon network driver");
3272*4882a593Smuzhiyun MODULE_LICENSE("GPL");
3273*4882a593Smuzhiyun MODULE_DEVICE_TABLE(pci, ef4_pci_table);
3274*4882a593Smuzhiyun MODULE_VERSION(EF4_DRIVER_VERSION);
3275