1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2874aeea5SJeff Kirsher /****************************************************************************
3f7a6d2c4SBen Hutchings * Driver for Solarflare network controllers and boards
4874aeea5SJeff Kirsher * Copyright 2005-2006 Fen Systems Ltd.
5f7a6d2c4SBen Hutchings * Copyright 2005-2013 Solarflare Communications Inc.
6874aeea5SJeff Kirsher */
7874aeea5SJeff Kirsher
8b6459415SJakub Kicinski #include <linux/filter.h>
9874aeea5SJeff Kirsher #include <linux/module.h>
10874aeea5SJeff Kirsher #include <linux/pci.h>
11874aeea5SJeff Kirsher #include <linux/netdevice.h>
12874aeea5SJeff Kirsher #include <linux/etherdevice.h>
13874aeea5SJeff Kirsher #include <linux/delay.h>
14874aeea5SJeff Kirsher #include <linux/notifier.h>
15874aeea5SJeff Kirsher #include <linux/ip.h>
16874aeea5SJeff Kirsher #include <linux/tcp.h>
17874aeea5SJeff Kirsher #include <linux/in.h>
18874aeea5SJeff Kirsher #include <linux/ethtool.h>
19874aeea5SJeff Kirsher #include <linux/topology.h>
20874aeea5SJeff Kirsher #include <linux/gfp.h>
21b28405b0SAlexandre Rames #include <linux/interrupt.h>
22874aeea5SJeff Kirsher #include "net_driver.h"
23e5fbd977SJon Cooper #include <net/gre.h>
24e5fbd977SJon Cooper #include <net/udp_tunnel.h>
25874aeea5SJeff Kirsher #include "efx.h"
26e1253f39SAlex Maftei (amaftei) #include "efx_common.h"
27e1253f39SAlex Maftei (amaftei) #include "efx_channels.h"
2851b35a45SEdward Cree #include "ef100.h"
29e1253f39SAlex Maftei (amaftei) #include "rx_common.h"
30e1253f39SAlex Maftei (amaftei) #include "tx_common.h"
31874aeea5SJeff Kirsher #include "nic.h"
3271827443SEdward Cree #include "io.h"
33dd40781eSBen Hutchings #include "selftest.h"
347fa8d547SShradha Shah #include "sriov.h"
35*998b85f0SMartin Habets #include "efx_devlink.h"
36874aeea5SJeff Kirsher
37c77289b4SEdward Cree #include "mcdi_port_common.h"
38e5fbd977SJon Cooper #include "mcdi_pcol.h"
39874aeea5SJeff Kirsher #include "workarounds.h"
40874aeea5SJeff Kirsher
41874aeea5SJeff Kirsher /**************************************************************************
42874aeea5SJeff Kirsher *
43874aeea5SJeff Kirsher * Configurable values
44874aeea5SJeff Kirsher *
45874aeea5SJeff Kirsher *************************************************************************/
46874aeea5SJeff Kirsher
47e4ff3232SEdward Cree module_param_named(interrupt_mode, efx_interrupt_mode, uint, 0444);
48e4ff3232SEdward Cree MODULE_PARM_DESC(interrupt_mode,
49e4ff3232SEdward Cree "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
50e4ff3232SEdward Cree
5167e6398eSEdward Cree module_param(rss_cpus, uint, 0444);
5267e6398eSEdward Cree MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
5367e6398eSEdward Cree
54874aeea5SJeff Kirsher /*
55874aeea5SJeff Kirsher * Use separate channels for TX and RX events
56874aeea5SJeff Kirsher *
57874aeea5SJeff Kirsher * Set this to 1 to use separate channels for TX and RX. It allows us
58874aeea5SJeff Kirsher * to control interrupt affinity separately for TX and RX.
59874aeea5SJeff Kirsher *
60874aeea5SJeff Kirsher * This is only used in MSI-X interrupt mode
61874aeea5SJeff Kirsher */
62b0fbdae1SShradha Shah bool efx_separate_tx_channels;
63b0fbdae1SShradha Shah module_param(efx_separate_tx_channels, bool, 0444);
64b0fbdae1SShradha Shah MODULE_PARM_DESC(efx_separate_tx_channels,
65874aeea5SJeff Kirsher "Use separate channels for TX and RX");
66874aeea5SJeff Kirsher
67874aeea5SJeff Kirsher /* Initial interrupt moderation settings. They can be modified after
68874aeea5SJeff Kirsher * module load with ethtool.
69874aeea5SJeff Kirsher *
70874aeea5SJeff Kirsher * The default for RX should strike a balance between increasing the
71874aeea5SJeff Kirsher * round-trip latency and reducing overhead.
72874aeea5SJeff Kirsher */
73874aeea5SJeff Kirsher static unsigned int rx_irq_mod_usec = 60;
74874aeea5SJeff Kirsher
75874aeea5SJeff Kirsher /* Initial interrupt moderation settings. They can be modified after
76874aeea5SJeff Kirsher * module load with ethtool.
77874aeea5SJeff Kirsher *
78874aeea5SJeff Kirsher * This default is chosen to ensure that a 10G link does not go idle
79874aeea5SJeff Kirsher * while a TX queue is stopped after it has become full. A queue is
80874aeea5SJeff Kirsher * restarted when it drops below half full. The time this takes (assuming
81874aeea5SJeff Kirsher * worst case 3 descriptors per packet and 1024 descriptors) is
82874aeea5SJeff Kirsher * 512 / 3 * 1.2 = 205 usec.
83874aeea5SJeff Kirsher */
84874aeea5SJeff Kirsher static unsigned int tx_irq_mod_usec = 150;
85874aeea5SJeff Kirsher
86b9cc977dSBen Hutchings static bool phy_flash_cfg;
87b9cc977dSBen Hutchings module_param(phy_flash_cfg, bool, 0644);
88874aeea5SJeff Kirsher MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
89874aeea5SJeff Kirsher
90874aeea5SJeff Kirsher static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
91874aeea5SJeff Kirsher NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
92874aeea5SJeff Kirsher NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
93874aeea5SJeff Kirsher NETIF_MSG_TX_ERR | NETIF_MSG_HW);
94874aeea5SJeff Kirsher module_param(debug, uint, 0);
95874aeea5SJeff Kirsher MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
96874aeea5SJeff Kirsher
97874aeea5SJeff Kirsher /**************************************************************************
98874aeea5SJeff Kirsher *
99874aeea5SJeff Kirsher * Utility functions and prototypes
100874aeea5SJeff Kirsher *
101874aeea5SJeff Kirsher *************************************************************************/
102874aeea5SJeff Kirsher
103874aeea5SJeff Kirsher static void efx_remove_port(struct efx_nic *efx);
104e45a4fedSCharles McLachlan static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog);
105e45a4fedSCharles McLachlan static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp);
106dfe44c1fSCharles McLachlan static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
107dfe44c1fSCharles McLachlan u32 flags);
108874aeea5SJeff Kirsher
109874aeea5SJeff Kirsher /**************************************************************************
110874aeea5SJeff Kirsher *
111874aeea5SJeff Kirsher * Port handling
112874aeea5SJeff Kirsher *
113874aeea5SJeff Kirsher **************************************************************************/
114874aeea5SJeff Kirsher
115874aeea5SJeff Kirsher static void efx_fini_port(struct efx_nic *efx);
116874aeea5SJeff Kirsher
efx_probe_port(struct efx_nic * efx)117874aeea5SJeff Kirsher static int efx_probe_port(struct efx_nic *efx)
118874aeea5SJeff Kirsher {
119874aeea5SJeff Kirsher int rc;
120874aeea5SJeff Kirsher
121874aeea5SJeff Kirsher netif_dbg(efx, probe, efx->net_dev, "create port\n");
122874aeea5SJeff Kirsher
123874aeea5SJeff Kirsher if (phy_flash_cfg)
124874aeea5SJeff Kirsher efx->phy_mode = PHY_MODE_SPECIAL;
125874aeea5SJeff Kirsher
126874aeea5SJeff Kirsher /* Connect up MAC/PHY operations table */
127874aeea5SJeff Kirsher rc = efx->type->probe_port(efx);
128874aeea5SJeff Kirsher if (rc)
129874aeea5SJeff Kirsher return rc;
130874aeea5SJeff Kirsher
131e332bcb3SBen Hutchings /* Initialise MAC address to permanent address */
132f3956ebbSJakub Kicinski eth_hw_addr_set(efx->net_dev, efx->net_dev->perm_addr);
133874aeea5SJeff Kirsher
134874aeea5SJeff Kirsher return 0;
135874aeea5SJeff Kirsher }
136874aeea5SJeff Kirsher
efx_init_port(struct efx_nic * efx)137874aeea5SJeff Kirsher static int efx_init_port(struct efx_nic *efx)
138874aeea5SJeff Kirsher {
139874aeea5SJeff Kirsher int rc;
140874aeea5SJeff Kirsher
141874aeea5SJeff Kirsher netif_dbg(efx, drv, efx->net_dev, "init port\n");
142874aeea5SJeff Kirsher
143874aeea5SJeff Kirsher mutex_lock(&efx->mac_lock);
144874aeea5SJeff Kirsher
145874aeea5SJeff Kirsher efx->port_initialized = true;
146874aeea5SJeff Kirsher
147874aeea5SJeff Kirsher /* Ensure the PHY advertises the correct flow control settings */
148c77289b4SEdward Cree rc = efx_mcdi_port_reconfigure(efx);
149267d9d73SEdward Cree if (rc && rc != -EPERM)
150c77289b4SEdward Cree goto fail;
151874aeea5SJeff Kirsher
152874aeea5SJeff Kirsher mutex_unlock(&efx->mac_lock);
153874aeea5SJeff Kirsher return 0;
154874aeea5SJeff Kirsher
155c77289b4SEdward Cree fail:
156874aeea5SJeff Kirsher mutex_unlock(&efx->mac_lock);
157874aeea5SJeff Kirsher return rc;
158874aeea5SJeff Kirsher }
159874aeea5SJeff Kirsher
efx_fini_port(struct efx_nic * efx)160874aeea5SJeff Kirsher static void efx_fini_port(struct efx_nic *efx)
161874aeea5SJeff Kirsher {
162874aeea5SJeff Kirsher netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
163874aeea5SJeff Kirsher
164874aeea5SJeff Kirsher if (!efx->port_initialized)
165874aeea5SJeff Kirsher return;
166874aeea5SJeff Kirsher
167874aeea5SJeff Kirsher efx->port_initialized = false;
168874aeea5SJeff Kirsher
169874aeea5SJeff Kirsher efx->link_state.up = false;
170874aeea5SJeff Kirsher efx_link_status_changed(efx);
171874aeea5SJeff Kirsher }
172874aeea5SJeff Kirsher
efx_remove_port(struct efx_nic * efx)173874aeea5SJeff Kirsher static void efx_remove_port(struct efx_nic *efx)
174874aeea5SJeff Kirsher {
175874aeea5SJeff Kirsher netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
176874aeea5SJeff Kirsher
177874aeea5SJeff Kirsher efx->type->remove_port(efx);
178874aeea5SJeff Kirsher }
179874aeea5SJeff Kirsher
180874aeea5SJeff Kirsher /**************************************************************************
181874aeea5SJeff Kirsher *
182874aeea5SJeff Kirsher * NIC handling
183874aeea5SJeff Kirsher *
184874aeea5SJeff Kirsher **************************************************************************/
185874aeea5SJeff Kirsher
1860bcf4a64SBen Hutchings static LIST_HEAD(efx_primary_list);
1870bcf4a64SBen Hutchings static LIST_HEAD(efx_unassociated_list);
1880bcf4a64SBen Hutchings
efx_same_controller(struct efx_nic * left,struct efx_nic * right)1890bcf4a64SBen Hutchings static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
1900bcf4a64SBen Hutchings {
1910bcf4a64SBen Hutchings return left->type == right->type &&
1920bcf4a64SBen Hutchings left->vpd_sn && right->vpd_sn &&
1930bcf4a64SBen Hutchings !strcmp(left->vpd_sn, right->vpd_sn);
1940bcf4a64SBen Hutchings }
1950bcf4a64SBen Hutchings
efx_associate(struct efx_nic * efx)1960bcf4a64SBen Hutchings static void efx_associate(struct efx_nic *efx)
1970bcf4a64SBen Hutchings {
1980bcf4a64SBen Hutchings struct efx_nic *other, *next;
1990bcf4a64SBen Hutchings
2000bcf4a64SBen Hutchings if (efx->primary == efx) {
2010bcf4a64SBen Hutchings /* Adding primary function; look for secondaries */
2020bcf4a64SBen Hutchings
2030bcf4a64SBen Hutchings netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
2040bcf4a64SBen Hutchings list_add_tail(&efx->node, &efx_primary_list);
2050bcf4a64SBen Hutchings
2060bcf4a64SBen Hutchings list_for_each_entry_safe(other, next, &efx_unassociated_list,
2070bcf4a64SBen Hutchings node) {
2080bcf4a64SBen Hutchings if (efx_same_controller(efx, other)) {
2090bcf4a64SBen Hutchings list_del(&other->node);
2100bcf4a64SBen Hutchings netif_dbg(other, probe, other->net_dev,
2110bcf4a64SBen Hutchings "moving to secondary list of %s %s\n",
2120bcf4a64SBen Hutchings pci_name(efx->pci_dev),
2130bcf4a64SBen Hutchings efx->net_dev->name);
2140bcf4a64SBen Hutchings list_add_tail(&other->node,
2150bcf4a64SBen Hutchings &efx->secondary_list);
2160bcf4a64SBen Hutchings other->primary = efx;
2170bcf4a64SBen Hutchings }
2180bcf4a64SBen Hutchings }
2190bcf4a64SBen Hutchings } else {
2200bcf4a64SBen Hutchings /* Adding secondary function; look for primary */
2210bcf4a64SBen Hutchings
2220bcf4a64SBen Hutchings list_for_each_entry(other, &efx_primary_list, node) {
2230bcf4a64SBen Hutchings if (efx_same_controller(efx, other)) {
2240bcf4a64SBen Hutchings netif_dbg(efx, probe, efx->net_dev,
2250bcf4a64SBen Hutchings "adding to secondary list of %s %s\n",
2260bcf4a64SBen Hutchings pci_name(other->pci_dev),
2270bcf4a64SBen Hutchings other->net_dev->name);
2280bcf4a64SBen Hutchings list_add_tail(&efx->node,
2290bcf4a64SBen Hutchings &other->secondary_list);
2300bcf4a64SBen Hutchings efx->primary = other;
2310bcf4a64SBen Hutchings return;
2320bcf4a64SBen Hutchings }
2330bcf4a64SBen Hutchings }
2340bcf4a64SBen Hutchings
2350bcf4a64SBen Hutchings netif_dbg(efx, probe, efx->net_dev,
2360bcf4a64SBen Hutchings "adding to unassociated list\n");
2370bcf4a64SBen Hutchings list_add_tail(&efx->node, &efx_unassociated_list);
2380bcf4a64SBen Hutchings }
2390bcf4a64SBen Hutchings }
2400bcf4a64SBen Hutchings
efx_dissociate(struct efx_nic * efx)2410bcf4a64SBen Hutchings static void efx_dissociate(struct efx_nic *efx)
2420bcf4a64SBen Hutchings {
2430bcf4a64SBen Hutchings struct efx_nic *other, *next;
2440bcf4a64SBen Hutchings
2450bcf4a64SBen Hutchings list_del(&efx->node);
2460bcf4a64SBen Hutchings efx->primary = NULL;
2470bcf4a64SBen Hutchings
2480bcf4a64SBen Hutchings list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
2490bcf4a64SBen Hutchings list_del(&other->node);
2500bcf4a64SBen Hutchings netif_dbg(other, probe, other->net_dev,
2510bcf4a64SBen Hutchings "moving to unassociated list\n");
2520bcf4a64SBen Hutchings list_add_tail(&other->node, &efx_unassociated_list);
2530bcf4a64SBen Hutchings other->primary = NULL;
2540bcf4a64SBen Hutchings }
2550bcf4a64SBen Hutchings }
2560bcf4a64SBen Hutchings
efx_probe_nic(struct efx_nic * efx)257874aeea5SJeff Kirsher static int efx_probe_nic(struct efx_nic *efx)
258874aeea5SJeff Kirsher {
259874aeea5SJeff Kirsher int rc;
260874aeea5SJeff Kirsher
261874aeea5SJeff Kirsher netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
262874aeea5SJeff Kirsher
263874aeea5SJeff Kirsher /* Carry out hardware-type specific initialisation */
264874aeea5SJeff Kirsher rc = efx->type->probe(efx);
265874aeea5SJeff Kirsher if (rc)
266874aeea5SJeff Kirsher return rc;
267874aeea5SJeff Kirsher
268b0fbdae1SShradha Shah do {
269b0fbdae1SShradha Shah if (!efx->max_channels || !efx->max_tx_channels) {
270b0fbdae1SShradha Shah netif_err(efx, drv, efx->net_dev,
271b0fbdae1SShradha Shah "Insufficient resources to allocate"
272b0fbdae1SShradha Shah " any channels\n");
273b0fbdae1SShradha Shah rc = -ENOSPC;
274b0fbdae1SShradha Shah goto fail1;
275b0fbdae1SShradha Shah }
276b0fbdae1SShradha Shah
277b0fbdae1SShradha Shah /* Determine the number of channels and queues by trying
278b0fbdae1SShradha Shah * to hook in MSI-X interrupts.
279b0fbdae1SShradha Shah */
280874aeea5SJeff Kirsher rc = efx_probe_interrupts(efx);
281874aeea5SJeff Kirsher if (rc)
282c15eed22SBen Hutchings goto fail1;
283874aeea5SJeff Kirsher
2843990a8ffSCharles McLachlan rc = efx_set_channels(efx);
2853990a8ffSCharles McLachlan if (rc)
2863990a8ffSCharles McLachlan goto fail1;
28752ad762bSDaniel Pieczko
288b0fbdae1SShradha Shah /* dimension_resources can fail with EAGAIN */
289c15eed22SBen Hutchings rc = efx->type->dimension_resources(efx);
290b0fbdae1SShradha Shah if (rc != 0 && rc != -EAGAIN)
291c15eed22SBen Hutchings goto fail2;
29228e47c49SBen Hutchings
293b0fbdae1SShradha Shah if (rc == -EAGAIN)
294b0fbdae1SShradha Shah /* try again with new max_channels */
295b0fbdae1SShradha Shah efx_remove_interrupts(efx);
296b0fbdae1SShradha Shah
297b0fbdae1SShradha Shah } while (rc == -EAGAIN);
298b0fbdae1SShradha Shah
299874aeea5SJeff Kirsher if (efx->n_channels > 1)
30042356d9aSEdward Cree netdev_rss_key_fill(efx->rss_context.rx_hash_key,
30142356d9aSEdward Cree sizeof(efx->rss_context.rx_hash_key));
30242356d9aSEdward Cree efx_set_default_rx_indir_table(efx, &efx->rss_context);
303874aeea5SJeff Kirsher
304874aeea5SJeff Kirsher /* Initialise the interrupt moderation settings */
305539de7c5SBert Kenward efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
3069e393b30SBen Hutchings efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
3079e393b30SBen Hutchings true);
308874aeea5SJeff Kirsher
309874aeea5SJeff Kirsher return 0;
310874aeea5SJeff Kirsher
311c15eed22SBen Hutchings fail2:
312c15eed22SBen Hutchings efx_remove_interrupts(efx);
313c15eed22SBen Hutchings fail1:
314874aeea5SJeff Kirsher efx->type->remove(efx);
315874aeea5SJeff Kirsher return rc;
316874aeea5SJeff Kirsher }
317874aeea5SJeff Kirsher
efx_remove_nic(struct efx_nic * efx)318874aeea5SJeff Kirsher static void efx_remove_nic(struct efx_nic *efx)
319874aeea5SJeff Kirsher {
320874aeea5SJeff Kirsher netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
321874aeea5SJeff Kirsher
322874aeea5SJeff Kirsher efx_remove_interrupts(efx);
323874aeea5SJeff Kirsher efx->type->remove(efx);
324874aeea5SJeff Kirsher }
325874aeea5SJeff Kirsher
326874aeea5SJeff Kirsher /**************************************************************************
327874aeea5SJeff Kirsher *
328874aeea5SJeff Kirsher * NIC startup/shutdown
329874aeea5SJeff Kirsher *
330874aeea5SJeff Kirsher *************************************************************************/
331874aeea5SJeff Kirsher
efx_probe_all(struct efx_nic * efx)332874aeea5SJeff Kirsher static int efx_probe_all(struct efx_nic *efx)
333874aeea5SJeff Kirsher {
334874aeea5SJeff Kirsher int rc;
335874aeea5SJeff Kirsher
336874aeea5SJeff Kirsher rc = efx_probe_nic(efx);
337874aeea5SJeff Kirsher if (rc) {
338874aeea5SJeff Kirsher netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
339874aeea5SJeff Kirsher goto fail1;
340874aeea5SJeff Kirsher }
341874aeea5SJeff Kirsher
342874aeea5SJeff Kirsher rc = efx_probe_port(efx);
343874aeea5SJeff Kirsher if (rc) {
344874aeea5SJeff Kirsher netif_err(efx, probe, efx->net_dev, "failed to create port\n");
345874aeea5SJeff Kirsher goto fail2;
346874aeea5SJeff Kirsher }
347874aeea5SJeff Kirsher
3487e6d06f0SBen Hutchings BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
3497e6d06f0SBen Hutchings if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
3507e6d06f0SBen Hutchings rc = -EINVAL;
3517e6d06f0SBen Hutchings goto fail3;
3527e6d06f0SBen Hutchings }
353874aeea5SJeff Kirsher
3546d8aaaf6SDaniel Pieczko #ifdef CONFIG_SFC_SRIOV
3556d8aaaf6SDaniel Pieczko rc = efx->type->vswitching_probe(efx);
3566d8aaaf6SDaniel Pieczko if (rc) /* not fatal; the PF will still work fine */
3576d8aaaf6SDaniel Pieczko netif_warn(efx, probe, efx->net_dev,
3586d8aaaf6SDaniel Pieczko "failed to setup vswitching rc=%d;"
3596d8aaaf6SDaniel Pieczko " VFs may not function\n", rc);
3606d8aaaf6SDaniel Pieczko #endif
3616d8aaaf6SDaniel Pieczko
362874aeea5SJeff Kirsher rc = efx_probe_filters(efx);
363874aeea5SJeff Kirsher if (rc) {
364874aeea5SJeff Kirsher netif_err(efx, probe, efx->net_dev,
365874aeea5SJeff Kirsher "failed to create filter tables\n");
3666d8aaaf6SDaniel Pieczko goto fail4;
367874aeea5SJeff Kirsher }
368874aeea5SJeff Kirsher
3697f967c01SBen Hutchings rc = efx_probe_channels(efx);
3707f967c01SBen Hutchings if (rc)
3716d8aaaf6SDaniel Pieczko goto fail5;
3727f967c01SBen Hutchings
373813cf9d1SJonathan Cooper efx->state = STATE_NET_DOWN;
374813cf9d1SJonathan Cooper
375874aeea5SJeff Kirsher return 0;
376874aeea5SJeff Kirsher
3776d8aaaf6SDaniel Pieczko fail5:
3787f967c01SBen Hutchings efx_remove_filters(efx);
3796d8aaaf6SDaniel Pieczko fail4:
3806d8aaaf6SDaniel Pieczko #ifdef CONFIG_SFC_SRIOV
3816d8aaaf6SDaniel Pieczko efx->type->vswitching_remove(efx);
3826d8aaaf6SDaniel Pieczko #endif
383874aeea5SJeff Kirsher fail3:
384874aeea5SJeff Kirsher efx_remove_port(efx);
385874aeea5SJeff Kirsher fail2:
386874aeea5SJeff Kirsher efx_remove_nic(efx);
387874aeea5SJeff Kirsher fail1:
388874aeea5SJeff Kirsher return rc;
389874aeea5SJeff Kirsher }
390874aeea5SJeff Kirsher
efx_remove_all(struct efx_nic * efx)391874aeea5SJeff Kirsher static void efx_remove_all(struct efx_nic *efx)
392874aeea5SJeff Kirsher {
393e45a4fedSCharles McLachlan rtnl_lock();
394e45a4fedSCharles McLachlan efx_xdp_setup_prog(efx, NULL);
395e45a4fedSCharles McLachlan rtnl_unlock();
396e45a4fedSCharles McLachlan
397874aeea5SJeff Kirsher efx_remove_channels(efx);
3987f967c01SBen Hutchings efx_remove_filters(efx);
3996d8aaaf6SDaniel Pieczko #ifdef CONFIG_SFC_SRIOV
4006d8aaaf6SDaniel Pieczko efx->type->vswitching_remove(efx);
4016d8aaaf6SDaniel Pieczko #endif
402874aeea5SJeff Kirsher efx_remove_port(efx);
403874aeea5SJeff Kirsher efx_remove_nic(efx);
404874aeea5SJeff Kirsher }
405874aeea5SJeff Kirsher
406874aeea5SJeff Kirsher /**************************************************************************
407874aeea5SJeff Kirsher *
408874aeea5SJeff Kirsher * Interrupt moderation
409874aeea5SJeff Kirsher *
410874aeea5SJeff Kirsher **************************************************************************/
efx_usecs_to_ticks(struct efx_nic * efx,unsigned int usecs)411539de7c5SBert Kenward unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
412874aeea5SJeff Kirsher {
413b548f976SBen Hutchings if (usecs == 0)
414b548f976SBen Hutchings return 0;
415539de7c5SBert Kenward if (usecs * 1000 < efx->timer_quantum_ns)
416874aeea5SJeff Kirsher return 1; /* never round down to 0 */
417539de7c5SBert Kenward return usecs * 1000 / efx->timer_quantum_ns;
418539de7c5SBert Kenward }
419539de7c5SBert Kenward
efx_ticks_to_usecs(struct efx_nic * efx,unsigned int ticks)420539de7c5SBert Kenward unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
421539de7c5SBert Kenward {
422539de7c5SBert Kenward /* We must round up when converting ticks to microseconds
423539de7c5SBert Kenward * because we round down when converting the other way.
424539de7c5SBert Kenward */
425539de7c5SBert Kenward return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
426874aeea5SJeff Kirsher }
427874aeea5SJeff Kirsher
428874aeea5SJeff Kirsher /* Set interrupt moderation parameters */
efx_init_irq_moderation(struct efx_nic * efx,unsigned int tx_usecs,unsigned int rx_usecs,bool rx_adaptive,bool rx_may_override_tx)4299e393b30SBen Hutchings int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
4309e393b30SBen Hutchings unsigned int rx_usecs, bool rx_adaptive,
4319e393b30SBen Hutchings bool rx_may_override_tx)
432874aeea5SJeff Kirsher {
433874aeea5SJeff Kirsher struct efx_channel *channel;
434d95e329aSBert Kenward unsigned int timer_max_us;
435d95e329aSBert Kenward
436874aeea5SJeff Kirsher EFX_ASSERT_RESET_SERIALISED(efx);
437874aeea5SJeff Kirsher
438d95e329aSBert Kenward timer_max_us = efx->timer_max_ns / 1000;
439d95e329aSBert Kenward
440d95e329aSBert Kenward if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
4419e393b30SBen Hutchings return -EINVAL;
4429e393b30SBen Hutchings
443539de7c5SBert Kenward if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
4449e393b30SBen Hutchings !rx_may_override_tx) {
4459e393b30SBen Hutchings netif_err(efx, drv, efx->net_dev, "Channels are shared. "
4469e393b30SBen Hutchings "RX and TX IRQ moderation must be equal\n");
4479e393b30SBen Hutchings return -EINVAL;
4489e393b30SBen Hutchings }
4499e393b30SBen Hutchings
450874aeea5SJeff Kirsher efx->irq_rx_adaptive = rx_adaptive;
451539de7c5SBert Kenward efx->irq_rx_moderation_us = rx_usecs;
452874aeea5SJeff Kirsher efx_for_each_channel(channel, efx) {
453874aeea5SJeff Kirsher if (efx_channel_has_rx_queue(channel))
454539de7c5SBert Kenward channel->irq_moderation_us = rx_usecs;
455874aeea5SJeff Kirsher else if (efx_channel_has_tx_queues(channel))
456539de7c5SBert Kenward channel->irq_moderation_us = tx_usecs;
4573990a8ffSCharles McLachlan else if (efx_channel_is_xdp_tx(channel))
4583990a8ffSCharles McLachlan channel->irq_moderation_us = tx_usecs;
459874aeea5SJeff Kirsher }
4609e393b30SBen Hutchings
4619e393b30SBen Hutchings return 0;
462874aeea5SJeff Kirsher }
463874aeea5SJeff Kirsher
efx_get_irq_moderation(struct efx_nic * efx,unsigned int * tx_usecs,unsigned int * rx_usecs,bool * rx_adaptive)464a0c4faf5SBen Hutchings void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
465a0c4faf5SBen Hutchings unsigned int *rx_usecs, bool *rx_adaptive)
466a0c4faf5SBen Hutchings {
467a0c4faf5SBen Hutchings *rx_adaptive = efx->irq_rx_adaptive;
468539de7c5SBert Kenward *rx_usecs = efx->irq_rx_moderation_us;
469a0c4faf5SBen Hutchings
470a0c4faf5SBen Hutchings /* If channels are shared between RX and TX, so is IRQ
471a0c4faf5SBen Hutchings * moderation. Otherwise, IRQ moderation is the same for all
472a0c4faf5SBen Hutchings * TX channels and is not adaptive.
473a0c4faf5SBen Hutchings */
474539de7c5SBert Kenward if (efx->tx_channel_offset == 0) {
475a0c4faf5SBen Hutchings *tx_usecs = *rx_usecs;
476539de7c5SBert Kenward } else {
477539de7c5SBert Kenward struct efx_channel *tx_channel;
478539de7c5SBert Kenward
479539de7c5SBert Kenward tx_channel = efx->channel[efx->tx_channel_offset];
480539de7c5SBert Kenward *tx_usecs = tx_channel->irq_moderation_us;
481539de7c5SBert Kenward }
482a0c4faf5SBen Hutchings }
483a0c4faf5SBen Hutchings
484874aeea5SJeff Kirsher /**************************************************************************
485874aeea5SJeff Kirsher *
486874aeea5SJeff Kirsher * ioctls
487874aeea5SJeff Kirsher *
488874aeea5SJeff Kirsher *************************************************************************/
489874aeea5SJeff Kirsher
490874aeea5SJeff Kirsher /* Net device ioctl
491874aeea5SJeff Kirsher * Context: process, rtnl_lock() held.
492874aeea5SJeff Kirsher */
efx_ioctl(struct net_device * net_dev,struct ifreq * ifr,int cmd)493874aeea5SJeff Kirsher static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
494874aeea5SJeff Kirsher {
4958cb03f4eSJonathan Cooper struct efx_nic *efx = efx_netdev_priv(net_dev);
496874aeea5SJeff Kirsher struct mii_ioctl_data *data = if_mii(ifr);
497874aeea5SJeff Kirsher
4987c236c43SStuart Hodgson if (cmd == SIOCSHWTSTAMP)
499433dc9b3SBen Hutchings return efx_ptp_set_ts_config(efx, ifr);
500433dc9b3SBen Hutchings if (cmd == SIOCGHWTSTAMP)
501433dc9b3SBen Hutchings return efx_ptp_get_ts_config(efx, ifr);
5027c236c43SStuart Hodgson
503874aeea5SJeff Kirsher /* Convert phy_id from older PRTAD/DEVAD format */
504874aeea5SJeff Kirsher if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
505874aeea5SJeff Kirsher (data->phy_id & 0xfc00) == 0x0400)
506874aeea5SJeff Kirsher data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
507874aeea5SJeff Kirsher
508874aeea5SJeff Kirsher return mdio_mii_ioctl(&efx->mdio, data, cmd);
509874aeea5SJeff Kirsher }
510874aeea5SJeff Kirsher
511874aeea5SJeff Kirsher /**************************************************************************
512874aeea5SJeff Kirsher *
513874aeea5SJeff Kirsher * Kernel net device interface
514874aeea5SJeff Kirsher *
515874aeea5SJeff Kirsher *************************************************************************/
516874aeea5SJeff Kirsher
517874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held. */
efx_net_open(struct net_device * net_dev)518e340be92SShradha Shah int efx_net_open(struct net_device *net_dev)
519874aeea5SJeff Kirsher {
5208cb03f4eSJonathan Cooper struct efx_nic *efx = efx_netdev_priv(net_dev);
5218b7325b4SBen Hutchings int rc;
5228b7325b4SBen Hutchings
523874aeea5SJeff Kirsher netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
524874aeea5SJeff Kirsher raw_smp_processor_id());
525874aeea5SJeff Kirsher
5268b7325b4SBen Hutchings rc = efx_check_disabled(efx);
5278b7325b4SBen Hutchings if (rc)
5288b7325b4SBen Hutchings return rc;
529874aeea5SJeff Kirsher if (efx->phy_mode & PHY_MODE_SPECIAL)
530874aeea5SJeff Kirsher return -EBUSY;
531874aeea5SJeff Kirsher if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
532874aeea5SJeff Kirsher return -EIO;
533874aeea5SJeff Kirsher
534874aeea5SJeff Kirsher /* Notify the kernel of the link state polled during driver load,
535874aeea5SJeff Kirsher * before the monitor starts running */
536874aeea5SJeff Kirsher efx_link_status_changed(efx);
537874aeea5SJeff Kirsher
538874aeea5SJeff Kirsher efx_start_all(efx);
5399c568fd8SPeter Dunning if (efx->state == STATE_DISABLED || efx->reset_pending)
5409c568fd8SPeter Dunning netif_device_detach(efx->net_dev);
541813cf9d1SJonathan Cooper else
542813cf9d1SJonathan Cooper efx->state = STATE_NET_UP;
543813cf9d1SJonathan Cooper
544874aeea5SJeff Kirsher return 0;
545874aeea5SJeff Kirsher }
546874aeea5SJeff Kirsher
547874aeea5SJeff Kirsher /* Context: process, rtnl_lock() held.
548874aeea5SJeff Kirsher * Note that the kernel will ignore our return code; this method
549874aeea5SJeff Kirsher * should really be a void.
550874aeea5SJeff Kirsher */
efx_net_stop(struct net_device * net_dev)551e340be92SShradha Shah int efx_net_stop(struct net_device *net_dev)
552874aeea5SJeff Kirsher {
5538cb03f4eSJonathan Cooper struct efx_nic *efx = efx_netdev_priv(net_dev);
554874aeea5SJeff Kirsher
555874aeea5SJeff Kirsher netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
556874aeea5SJeff Kirsher raw_smp_processor_id());
557874aeea5SJeff Kirsher
558874aeea5SJeff Kirsher /* Stop the device and flush all the channels */
559874aeea5SJeff Kirsher efx_stop_all(efx);
560874aeea5SJeff Kirsher
561874aeea5SJeff Kirsher return 0;
562874aeea5SJeff Kirsher }
563874aeea5SJeff Kirsher
efx_vlan_rx_add_vid(struct net_device * net_dev,__be16 proto,u16 vid)5644a53ea8aSAndrew Rybchenko static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
5654a53ea8aSAndrew Rybchenko {
5668cb03f4eSJonathan Cooper struct efx_nic *efx = efx_netdev_priv(net_dev);
5674a53ea8aSAndrew Rybchenko
5684a53ea8aSAndrew Rybchenko if (efx->type->vlan_rx_add_vid)
5694a53ea8aSAndrew Rybchenko return efx->type->vlan_rx_add_vid(efx, proto, vid);
5704a53ea8aSAndrew Rybchenko else
5714a53ea8aSAndrew Rybchenko return -EOPNOTSUPP;
5724a53ea8aSAndrew Rybchenko }
5734a53ea8aSAndrew Rybchenko
efx_vlan_rx_kill_vid(struct net_device * net_dev,__be16 proto,u16 vid)5744a53ea8aSAndrew Rybchenko static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
5754a53ea8aSAndrew Rybchenko {
5768cb03f4eSJonathan Cooper struct efx_nic *efx = efx_netdev_priv(net_dev);
5774a53ea8aSAndrew Rybchenko
5784a53ea8aSAndrew Rybchenko if (efx->type->vlan_rx_kill_vid)
5794a53ea8aSAndrew Rybchenko return efx->type->vlan_rx_kill_vid(efx, proto, vid);
5804a53ea8aSAndrew Rybchenko else
5814a53ea8aSAndrew Rybchenko return -EOPNOTSUPP;
5824a53ea8aSAndrew Rybchenko }
5834a53ea8aSAndrew Rybchenko
5847fa8d547SShradha Shah static const struct net_device_ops efx_netdev_ops = {
585874aeea5SJeff Kirsher .ndo_open = efx_net_open,
586874aeea5SJeff Kirsher .ndo_stop = efx_net_stop,
587874aeea5SJeff Kirsher .ndo_get_stats64 = efx_net_stats,
588874aeea5SJeff Kirsher .ndo_tx_timeout = efx_watchdog,
589874aeea5SJeff Kirsher .ndo_start_xmit = efx_hard_start_xmit,
590874aeea5SJeff Kirsher .ndo_validate_addr = eth_validate_addr,
591a7605370SArnd Bergmann .ndo_eth_ioctl = efx_ioctl,
592874aeea5SJeff Kirsher .ndo_change_mtu = efx_change_mtu,
593874aeea5SJeff Kirsher .ndo_set_mac_address = efx_set_mac_address,
5940fca8c97SBen Hutchings .ndo_set_rx_mode = efx_set_rx_mode,
595874aeea5SJeff Kirsher .ndo_set_features = efx_set_features,
59624b2c375SEdward Cree .ndo_features_check = efx_features_check,
5974a53ea8aSAndrew Rybchenko .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
5984a53ea8aSAndrew Rybchenko .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
599cd2d5b52SBen Hutchings #ifdef CONFIG_SFC_SRIOV
6007fa8d547SShradha Shah .ndo_set_vf_mac = efx_sriov_set_vf_mac,
6017fa8d547SShradha Shah .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
6027fa8d547SShradha Shah .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
6037fa8d547SShradha Shah .ndo_get_vf_config = efx_sriov_get_vf_config,
6044392dc69SEdward Cree .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
605cd2d5b52SBen Hutchings #endif
60608a7b29bSBert Kenward .ndo_get_phys_port_id = efx_get_phys_port_id,
607ac019f08SBert Kenward .ndo_get_phys_port_name = efx_get_phys_port_name,
608874aeea5SJeff Kirsher #ifdef CONFIG_RFS_ACCEL
609874aeea5SJeff Kirsher .ndo_rx_flow_steer = efx_filter_rfs,
610874aeea5SJeff Kirsher #endif
611dfe44c1fSCharles McLachlan .ndo_xdp_xmit = efx_xdp_xmit,
612e45a4fedSCharles McLachlan .ndo_bpf = efx_xdp
613874aeea5SJeff Kirsher };
614874aeea5SJeff Kirsher
efx_xdp_setup_prog(struct efx_nic * efx,struct bpf_prog * prog)615e45a4fedSCharles McLachlan static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
616e45a4fedSCharles McLachlan {
617e45a4fedSCharles McLachlan struct bpf_prog *old_prog;
618e45a4fedSCharles McLachlan
619e45a4fedSCharles McLachlan if (efx->xdp_rxq_info_failed) {
620e45a4fedSCharles McLachlan netif_err(efx, drv, efx->net_dev,
621e45a4fedSCharles McLachlan "Unable to bind XDP program due to previous failure of rxq_info\n");
622e45a4fedSCharles McLachlan return -EINVAL;
623e45a4fedSCharles McLachlan }
624e45a4fedSCharles McLachlan
625e45a4fedSCharles McLachlan if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) {
626e45a4fedSCharles McLachlan netif_err(efx, drv, efx->net_dev,
627e45a4fedSCharles McLachlan "Unable to configure XDP with MTU of %d (max: %d)\n",
628e45a4fedSCharles McLachlan efx->net_dev->mtu, efx_xdp_max_mtu(efx));
629e45a4fedSCharles McLachlan return -EINVAL;
630e45a4fedSCharles McLachlan }
631e45a4fedSCharles McLachlan
632e45a4fedSCharles McLachlan old_prog = rtnl_dereference(efx->xdp_prog);
633e45a4fedSCharles McLachlan rcu_assign_pointer(efx->xdp_prog, prog);
634e45a4fedSCharles McLachlan /* Release the reference that was originally passed by the caller. */
635e45a4fedSCharles McLachlan if (old_prog)
636e45a4fedSCharles McLachlan bpf_prog_put(old_prog);
637e45a4fedSCharles McLachlan
638e45a4fedSCharles McLachlan return 0;
639e45a4fedSCharles McLachlan }
640e45a4fedSCharles McLachlan
641e45a4fedSCharles McLachlan /* Context: process, rtnl_lock() held. */
efx_xdp(struct net_device * dev,struct netdev_bpf * xdp)642e45a4fedSCharles McLachlan static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
643e45a4fedSCharles McLachlan {
6448cb03f4eSJonathan Cooper struct efx_nic *efx = efx_netdev_priv(dev);
645e45a4fedSCharles McLachlan
646e45a4fedSCharles McLachlan switch (xdp->command) {
647e45a4fedSCharles McLachlan case XDP_SETUP_PROG:
648e45a4fedSCharles McLachlan return efx_xdp_setup_prog(efx, xdp->prog);
649e45a4fedSCharles McLachlan default:
650e45a4fedSCharles McLachlan return -EINVAL;
651e45a4fedSCharles McLachlan }
652e45a4fedSCharles McLachlan }
653e45a4fedSCharles McLachlan
efx_xdp_xmit(struct net_device * dev,int n,struct xdp_frame ** xdpfs,u32 flags)654dfe44c1fSCharles McLachlan static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
655dfe44c1fSCharles McLachlan u32 flags)
656dfe44c1fSCharles McLachlan {
6578cb03f4eSJonathan Cooper struct efx_nic *efx = efx_netdev_priv(dev);
658dfe44c1fSCharles McLachlan
659dfe44c1fSCharles McLachlan if (!netif_running(dev))
660dfe44c1fSCharles McLachlan return -EINVAL;
661dfe44c1fSCharles McLachlan
662dfe44c1fSCharles McLachlan return efx_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH);
663dfe44c1fSCharles McLachlan }
664dfe44c1fSCharles McLachlan
efx_update_name(struct efx_nic * efx)665874aeea5SJeff Kirsher static void efx_update_name(struct efx_nic *efx)
666874aeea5SJeff Kirsher {
667874aeea5SJeff Kirsher strcpy(efx->name, efx->net_dev->name);
668874aeea5SJeff Kirsher efx_mtd_rename(efx);
669874aeea5SJeff Kirsher efx_set_channel_names(efx);
670874aeea5SJeff Kirsher }
671874aeea5SJeff Kirsher
efx_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)672874aeea5SJeff Kirsher static int efx_netdev_event(struct notifier_block *this,
673874aeea5SJeff Kirsher unsigned long event, void *ptr)
674874aeea5SJeff Kirsher {
675351638e7SJiri Pirko struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
676874aeea5SJeff Kirsher
6777fa8d547SShradha Shah if ((net_dev->netdev_ops == &efx_netdev_ops) &&
678874aeea5SJeff Kirsher event == NETDEV_CHANGENAME)
6798cb03f4eSJonathan Cooper efx_update_name(efx_netdev_priv(net_dev));
680874aeea5SJeff Kirsher
681874aeea5SJeff Kirsher return NOTIFY_DONE;
682874aeea5SJeff Kirsher }
683874aeea5SJeff Kirsher
684874aeea5SJeff Kirsher static struct notifier_block efx_netdev_notifier = {
685874aeea5SJeff Kirsher .notifier_call = efx_netdev_event,
686874aeea5SJeff Kirsher };
687874aeea5SJeff Kirsher
phy_type_show(struct device * dev,struct device_attribute * attr,char * buf)6883880fc37SYueHaibing static ssize_t phy_type_show(struct device *dev,
6893880fc37SYueHaibing struct device_attribute *attr, char *buf)
690874aeea5SJeff Kirsher {
6913e03a8baSChuhong Yuan struct efx_nic *efx = dev_get_drvdata(dev);
692874aeea5SJeff Kirsher return sprintf(buf, "%d\n", efx->phy_type);
693874aeea5SJeff Kirsher }
6943880fc37SYueHaibing static DEVICE_ATTR_RO(phy_type);
695874aeea5SJeff Kirsher
efx_register_netdev(struct efx_nic * efx)696874aeea5SJeff Kirsher static int efx_register_netdev(struct efx_nic *efx)
697874aeea5SJeff Kirsher {
698874aeea5SJeff Kirsher struct net_device *net_dev = efx->net_dev;
699874aeea5SJeff Kirsher struct efx_channel *channel;
700874aeea5SJeff Kirsher int rc;
701874aeea5SJeff Kirsher
702874aeea5SJeff Kirsher net_dev->watchdog_timeo = 5 * HZ;
703874aeea5SJeff Kirsher net_dev->irq = efx->pci_dev->irq;
7047fa8d547SShradha Shah net_dev->netdev_ops = &efx_netdev_ops;
7057fa8d547SShradha Shah if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
7068127d661SBen Hutchings net_dev->priv_flags |= IFF_UNICAST_FLT;
7077ad24ea4SWilfried Klaebe net_dev->ethtool_ops = &efx_ethtool_ops;
708ee8b7a11SJakub Kicinski netif_set_tso_max_segs(net_dev, EFX_TSO_MAX_SEGS);
709cd94e519SBert Kenward net_dev->min_mtu = EFX_MIN_MTU;
710cd94e519SBert Kenward net_dev->max_mtu = EFX_MAX_MTU;
711874aeea5SJeff Kirsher
712874aeea5SJeff Kirsher rtnl_lock();
713874aeea5SJeff Kirsher
7147153f623SBen Hutchings /* Enable resets to be scheduled and check whether any were
7157153f623SBen Hutchings * already requested. If so, the NIC is probably hosed so we
7167153f623SBen Hutchings * abort.
7177153f623SBen Hutchings */
7187153f623SBen Hutchings if (efx->reset_pending) {
719fa44821aSHeiner Kallweit pci_err(efx->pci_dev, "aborting probe due to scheduled reset\n");
7207153f623SBen Hutchings rc = -EIO;
7217153f623SBen Hutchings goto fail_locked;
7227153f623SBen Hutchings }
7237153f623SBen Hutchings
724874aeea5SJeff Kirsher rc = dev_alloc_name(net_dev, net_dev->name);
725874aeea5SJeff Kirsher if (rc < 0)
726874aeea5SJeff Kirsher goto fail_locked;
727874aeea5SJeff Kirsher efx_update_name(efx);
728874aeea5SJeff Kirsher
7298f8b3d51SBen Hutchings /* Always start with carrier off; PHY events will detect the link */
7308f8b3d51SBen Hutchings netif_carrier_off(net_dev);
7318f8b3d51SBen Hutchings
732874aeea5SJeff Kirsher rc = register_netdevice(net_dev);
733874aeea5SJeff Kirsher if (rc)
734874aeea5SJeff Kirsher goto fail_locked;
735874aeea5SJeff Kirsher
736874aeea5SJeff Kirsher efx_for_each_channel(channel, efx) {
737874aeea5SJeff Kirsher struct efx_tx_queue *tx_queue;
738874aeea5SJeff Kirsher efx_for_each_channel_tx_queue(tx_queue, channel)
739874aeea5SJeff Kirsher efx_init_tx_queue_core_txq(tx_queue);
740874aeea5SJeff Kirsher }
741874aeea5SJeff Kirsher
7420bcf4a64SBen Hutchings efx_associate(efx);
7430bcf4a64SBen Hutchings
744813cf9d1SJonathan Cooper efx->state = STATE_NET_DOWN;
745813cf9d1SJonathan Cooper
746874aeea5SJeff Kirsher rtnl_unlock();
747874aeea5SJeff Kirsher
748874aeea5SJeff Kirsher rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
749874aeea5SJeff Kirsher if (rc) {
750874aeea5SJeff Kirsher netif_err(efx, drv, efx->net_dev,
751874aeea5SJeff Kirsher "failed to init net dev attributes\n");
752874aeea5SJeff Kirsher goto fail_registered;
753874aeea5SJeff Kirsher }
754b69f7a3eSAlex Maftei (amaftei)
755b69f7a3eSAlex Maftei (amaftei) efx_init_mcdi_logging(efx);
756874aeea5SJeff Kirsher
757874aeea5SJeff Kirsher return 0;
758874aeea5SJeff Kirsher
7597153f623SBen Hutchings fail_registered:
7607153f623SBen Hutchings rtnl_lock();
7610bcf4a64SBen Hutchings efx_dissociate(efx);
7627153f623SBen Hutchings unregister_netdevice(net_dev);
763874aeea5SJeff Kirsher fail_locked:
7647153f623SBen Hutchings efx->state = STATE_UNINIT;
765874aeea5SJeff Kirsher rtnl_unlock();
766874aeea5SJeff Kirsher netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
767874aeea5SJeff Kirsher return rc;
768874aeea5SJeff Kirsher }
769874aeea5SJeff Kirsher
efx_unregister_netdev(struct efx_nic * efx)770874aeea5SJeff Kirsher static void efx_unregister_netdev(struct efx_nic *efx)
771874aeea5SJeff Kirsher {
772874aeea5SJeff Kirsher if (!efx->net_dev)
773874aeea5SJeff Kirsher return;
774874aeea5SJeff Kirsher
7758cb03f4eSJonathan Cooper if (WARN_ON(efx_netdev_priv(efx->net_dev) != efx))
776b3fd0a86SJonathan Cooper return;
777874aeea5SJeff Kirsher
778e7fef9b4SEdward Cree if (efx_dev_registered(efx)) {
779f029c781SWolfram Sang strscpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
780b69f7a3eSAlex Maftei (amaftei) efx_fini_mcdi_logging(efx);
781874aeea5SJeff Kirsher device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
782e7fef9b4SEdward Cree unregister_netdev(efx->net_dev);
783e7fef9b4SEdward Cree }
784874aeea5SJeff Kirsher }
785874aeea5SJeff Kirsher
786874aeea5SJeff Kirsher /**************************************************************************
787874aeea5SJeff Kirsher *
788874aeea5SJeff Kirsher * List of NICs we support
789874aeea5SJeff Kirsher *
790874aeea5SJeff Kirsher **************************************************************************/
791874aeea5SJeff Kirsher
792874aeea5SJeff Kirsher /* PCI device ID table */
7939baa3c34SBenoit Taine static const struct pci_device_id efx_pci_table[] = {
7948127d661SBen Hutchings {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
7958127d661SBen Hutchings .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
7966f7f8aa6SShradha Shah {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
7976f7f8aa6SShradha Shah .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
7983b06a00eSMateusz Wrzesinski {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
7993b06a00eSMateusz Wrzesinski .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
800dd248f1bSBert Kenward {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
801dd248f1bSBert Kenward .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
802dd248f1bSBert Kenward {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
803dd248f1bSBert Kenward .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
804dd248f1bSBert Kenward {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
805dd248f1bSBert Kenward .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
806aae5a316SEdward Cree {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
807aae5a316SEdward Cree .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
808aae5a316SEdward Cree {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
809aae5a316SEdward Cree .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
810874aeea5SJeff Kirsher {0} /* end of list */
811874aeea5SJeff Kirsher };
812874aeea5SJeff Kirsher
813874aeea5SJeff Kirsher /**************************************************************************
814874aeea5SJeff Kirsher *
815874aeea5SJeff Kirsher * Data housekeeping
816874aeea5SJeff Kirsher *
817874aeea5SJeff Kirsher **************************************************************************/
818874aeea5SJeff Kirsher
efx_update_sw_stats(struct efx_nic * efx,u64 * stats)819e4d112e4SEdward Cree void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
820e4d112e4SEdward Cree {
821e4d112e4SEdward Cree u64 n_rx_nodesc_trunc = 0;
822e4d112e4SEdward Cree struct efx_channel *channel;
823e4d112e4SEdward Cree
824e4d112e4SEdward Cree efx_for_each_channel(channel, efx)
825e4d112e4SEdward Cree n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
826e4d112e4SEdward Cree stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
827e4d112e4SEdward Cree stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
828e4d112e4SEdward Cree }
829e4d112e4SEdward Cree
830874aeea5SJeff Kirsher /**************************************************************************
831874aeea5SJeff Kirsher *
832874aeea5SJeff Kirsher * PCI interface
833874aeea5SJeff Kirsher *
834874aeea5SJeff Kirsher **************************************************************************/
835874aeea5SJeff Kirsher
836874aeea5SJeff Kirsher /* Main body of final NIC shutdown code
837874aeea5SJeff Kirsher * This is called only at module unload (or hotplug removal).
838874aeea5SJeff Kirsher */
efx_pci_remove_main(struct efx_nic * efx)839874aeea5SJeff Kirsher static void efx_pci_remove_main(struct efx_nic *efx)
840874aeea5SJeff Kirsher {
8417153f623SBen Hutchings /* Flush reset_work. It can no longer be scheduled since we
8427153f623SBen Hutchings * are not READY.
8437153f623SBen Hutchings */
844813cf9d1SJonathan Cooper WARN_ON(efx_net_active(efx->state));
84582c64484SAlex Maftei (amaftei) efx_flush_reset_workqueue(efx);
8467153f623SBen Hutchings
847d8291187SBen Hutchings efx_disable_interrupts(efx);
848a83762d9SBert Kenward efx_clear_interrupt_affinity(efx);
849874aeea5SJeff Kirsher efx_nic_fini_interrupt(efx);
850874aeea5SJeff Kirsher efx_fini_port(efx);
851874aeea5SJeff Kirsher efx->type->fini(efx);
852874aeea5SJeff Kirsher efx_fini_napi(efx);
853874aeea5SJeff Kirsher efx_remove_all(efx);
854874aeea5SJeff Kirsher }
855874aeea5SJeff Kirsher
856874aeea5SJeff Kirsher /* Final NIC shutdown
8572a3fc311SDaniel Pieczko * This is called only at module unload (or hotplug removal). A PF can call
8582a3fc311SDaniel Pieczko * this on its VFs to ensure they are unbound first.
859874aeea5SJeff Kirsher */
efx_pci_remove(struct pci_dev * pci_dev)860874aeea5SJeff Kirsher static void efx_pci_remove(struct pci_dev *pci_dev)
861874aeea5SJeff Kirsher {
8627e773594SJonathan Cooper struct efx_probe_data *probe_data;
863874aeea5SJeff Kirsher struct efx_nic *efx;
864874aeea5SJeff Kirsher
865874aeea5SJeff Kirsher efx = pci_get_drvdata(pci_dev);
866874aeea5SJeff Kirsher if (!efx)
867874aeea5SJeff Kirsher return;
868874aeea5SJeff Kirsher
869874aeea5SJeff Kirsher /* Mark the NIC as fini, then stop the interface */
870874aeea5SJeff Kirsher rtnl_lock();
8710bcf4a64SBen Hutchings efx_dissociate(efx);
872874aeea5SJeff Kirsher dev_close(efx->net_dev);
873d8291187SBen Hutchings efx_disable_interrupts(efx);
874ea6bb99eSEdward Cree efx->state = STATE_UNINIT;
875874aeea5SJeff Kirsher rtnl_unlock();
876874aeea5SJeff Kirsher
8777fa8d547SShradha Shah if (efx->type->sriov_fini)
878d98a4ffeSShradha Shah efx->type->sriov_fini(efx);
8797fa8d547SShradha Shah
880*998b85f0SMartin Habets efx_fini_devlink_lock(efx);
881874aeea5SJeff Kirsher efx_unregister_netdev(efx);
882874aeea5SJeff Kirsher
883874aeea5SJeff Kirsher efx_mtd_remove(efx);
884874aeea5SJeff Kirsher
885874aeea5SJeff Kirsher efx_pci_remove_main(efx);
886874aeea5SJeff Kirsher
88766a65128SEdward Cree efx_fini_io(efx);
88898ff4c7cSJonathan Cooper pci_dbg(efx->pci_dev, "shutdown successful\n");
889874aeea5SJeff Kirsher
890*998b85f0SMartin Habets efx_fini_devlink_and_unlock(efx);
891874aeea5SJeff Kirsher efx_fini_struct(efx);
892874aeea5SJeff Kirsher free_netdev(efx->net_dev);
8937e773594SJonathan Cooper probe_data = container_of(efx, struct efx_probe_data, efx);
8947e773594SJonathan Cooper kfree(probe_data);
895874aeea5SJeff Kirsher };
896874aeea5SJeff Kirsher
897460eeaa0SBen Hutchings /* NIC VPD information
898460eeaa0SBen Hutchings * Called during probe to display the part number of the
8995119e20fSHeiner Kallweit * installed NIC.
900460eeaa0SBen Hutchings */
efx_probe_vpd_strings(struct efx_nic * efx)901ef215e64SBen Hutchings static void efx_probe_vpd_strings(struct efx_nic *efx)
902460eeaa0SBen Hutchings {
903460eeaa0SBen Hutchings struct pci_dev *dev = efx->pci_dev;
90437838aa4SHeiner Kallweit unsigned int vpd_size, kw_len;
9055119e20fSHeiner Kallweit u8 *vpd_data;
90637838aa4SHeiner Kallweit int start;
907460eeaa0SBen Hutchings
9085119e20fSHeiner Kallweit vpd_data = pci_vpd_alloc(dev, &vpd_size);
9095119e20fSHeiner Kallweit if (IS_ERR(vpd_data)) {
9105119e20fSHeiner Kallweit pci_warn(dev, "Unable to read VPD\n");
911460eeaa0SBen Hutchings return;
912460eeaa0SBen Hutchings }
913460eeaa0SBen Hutchings
91437838aa4SHeiner Kallweit start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
91537838aa4SHeiner Kallweit PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
91637838aa4SHeiner Kallweit if (start < 0)
91737838aa4SHeiner Kallweit pci_err(dev, "Part number not found or incomplete\n");
91837838aa4SHeiner Kallweit else
91937838aa4SHeiner Kallweit pci_info(dev, "Part Number : %.*s\n", kw_len, vpd_data + start);
920460eeaa0SBen Hutchings
92137838aa4SHeiner Kallweit start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
92237838aa4SHeiner Kallweit PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);
92337838aa4SHeiner Kallweit if (start < 0)
92437838aa4SHeiner Kallweit pci_err(dev, "Serial number not found or incomplete\n");
92537838aa4SHeiner Kallweit else
92637838aa4SHeiner Kallweit efx->vpd_sn = kmemdup_nul(vpd_data + start, kw_len, GFP_KERNEL);
927460eeaa0SBen Hutchings
9285119e20fSHeiner Kallweit kfree(vpd_data);
929460eeaa0SBen Hutchings }
930460eeaa0SBen Hutchings
931460eeaa0SBen Hutchings
932874aeea5SJeff Kirsher /* Main body of NIC initialisation
933874aeea5SJeff Kirsher * This is called at module load (or hotplug insertion, theoretically).
934874aeea5SJeff Kirsher */
efx_pci_probe_main(struct efx_nic * efx)935874aeea5SJeff Kirsher static int efx_pci_probe_main(struct efx_nic *efx)
936874aeea5SJeff Kirsher {
937874aeea5SJeff Kirsher int rc;
938874aeea5SJeff Kirsher
939874aeea5SJeff Kirsher /* Do start-of-day initialisation */
940874aeea5SJeff Kirsher rc = efx_probe_all(efx);
941874aeea5SJeff Kirsher if (rc)
942874aeea5SJeff Kirsher goto fail1;
943874aeea5SJeff Kirsher
944874aeea5SJeff Kirsher efx_init_napi(efx);
945874aeea5SJeff Kirsher
946193f2003SBert Kenward down_write(&efx->filter_sem);
947874aeea5SJeff Kirsher rc = efx->type->init(efx);
948193f2003SBert Kenward up_write(&efx->filter_sem);
949874aeea5SJeff Kirsher if (rc) {
950fa44821aSHeiner Kallweit pci_err(efx->pci_dev, "failed to initialise NIC\n");
951874aeea5SJeff Kirsher goto fail3;
952874aeea5SJeff Kirsher }
953874aeea5SJeff Kirsher
954874aeea5SJeff Kirsher rc = efx_init_port(efx);
955874aeea5SJeff Kirsher if (rc) {
956874aeea5SJeff Kirsher netif_err(efx, probe, efx->net_dev,
957874aeea5SJeff Kirsher "failed to initialise port\n");
958874aeea5SJeff Kirsher goto fail4;
959874aeea5SJeff Kirsher }
960874aeea5SJeff Kirsher
961874aeea5SJeff Kirsher rc = efx_nic_init_interrupt(efx);
962874aeea5SJeff Kirsher if (rc)
963874aeea5SJeff Kirsher goto fail5;
964a83762d9SBert Kenward
965a83762d9SBert Kenward efx_set_interrupt_affinity(efx);
966261e4d96SJon Cooper rc = efx_enable_interrupts(efx);
967261e4d96SJon Cooper if (rc)
968261e4d96SJon Cooper goto fail6;
969874aeea5SJeff Kirsher
970874aeea5SJeff Kirsher return 0;
971874aeea5SJeff Kirsher
972261e4d96SJon Cooper fail6:
973a83762d9SBert Kenward efx_clear_interrupt_affinity(efx);
974261e4d96SJon Cooper efx_nic_fini_interrupt(efx);
975874aeea5SJeff Kirsher fail5:
976874aeea5SJeff Kirsher efx_fini_port(efx);
977874aeea5SJeff Kirsher fail4:
978874aeea5SJeff Kirsher efx->type->fini(efx);
979874aeea5SJeff Kirsher fail3:
980874aeea5SJeff Kirsher efx_fini_napi(efx);
981874aeea5SJeff Kirsher efx_remove_all(efx);
982874aeea5SJeff Kirsher fail1:
983874aeea5SJeff Kirsher return rc;
984874aeea5SJeff Kirsher }
985874aeea5SJeff Kirsher
efx_pci_probe_post_io(struct efx_nic * efx)9868a531400SJon Cooper static int efx_pci_probe_post_io(struct efx_nic *efx)
9878a531400SJon Cooper {
9888a531400SJon Cooper struct net_device *net_dev = efx->net_dev;
9898a531400SJon Cooper int rc = efx_pci_probe_main(efx);
9908a531400SJon Cooper
9918a531400SJon Cooper if (rc)
9928a531400SJon Cooper return rc;
9938a531400SJon Cooper
9948a531400SJon Cooper if (efx->type->sriov_init) {
9958a531400SJon Cooper rc = efx->type->sriov_init(efx);
9968a531400SJon Cooper if (rc)
997fa44821aSHeiner Kallweit pci_err(efx->pci_dev, "SR-IOV can't be enabled rc %d\n",
998fa44821aSHeiner Kallweit rc);
9998a531400SJon Cooper }
10008a531400SJon Cooper
10018a531400SJon Cooper /* Determine netdevice features */
1002ca4a80e4SÍñigo Huguet net_dev->features |= efx->type->offload_features;
1003ca4a80e4SÍñigo Huguet
1004ca4a80e4SÍñigo Huguet /* Add TSO features */
1005ca4a80e4SÍñigo Huguet if (efx->type->tso_versions && efx->type->tso_versions(efx))
1006ca4a80e4SÍñigo Huguet net_dev->features |= NETIF_F_TSO | NETIF_F_TSO6;
1007ca4a80e4SÍñigo Huguet
10088a531400SJon Cooper /* Mask for features that also apply to VLAN devices */
10098a531400SJon Cooper net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
10108a531400SJon Cooper NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
10118a531400SJon Cooper NETIF_F_RXCSUM);
10128a531400SJon Cooper
1013ca4a80e4SÍñigo Huguet /* Determine user configurable features */
10146978729fSEdward Cree net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
10156978729fSEdward Cree
10166978729fSEdward Cree /* Disable receiving frames with bad FCS, by default. */
10176978729fSEdward Cree net_dev->features &= ~NETIF_F_RXALL;
10188a531400SJon Cooper
10198a531400SJon Cooper /* Disable VLAN filtering by default. It may be enforced if
10208a531400SJon Cooper * the feature is fixed (i.e. VLAN filters are required to
10218a531400SJon Cooper * receive VLAN tagged packets due to vPort restrictions).
10228a531400SJon Cooper */
10238a531400SJon Cooper net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
10248a531400SJon Cooper net_dev->features |= efx->fixed_features;
10258a531400SJon Cooper
10269b0651e4SLorenzo Bianconi net_dev->xdp_features = NETDEV_XDP_ACT_BASIC |
10279b0651e4SLorenzo Bianconi NETDEV_XDP_ACT_REDIRECT |
10289b0651e4SLorenzo Bianconi NETDEV_XDP_ACT_NDO_XMIT;
10299b0651e4SLorenzo Bianconi
1030*998b85f0SMartin Habets /* devlink creation, registration and lock */
1031*998b85f0SMartin Habets rc = efx_probe_devlink_and_lock(efx);
1032*998b85f0SMartin Habets if (rc)
1033*998b85f0SMartin Habets pci_err(efx->pci_dev, "devlink registration failed");
1034*998b85f0SMartin Habets
10358a531400SJon Cooper rc = efx_register_netdev(efx);
1036*998b85f0SMartin Habets efx_probe_devlink_unlock(efx);
10378a531400SJon Cooper if (!rc)
10388a531400SJon Cooper return 0;
10398a531400SJon Cooper
10408a531400SJon Cooper efx_pci_remove_main(efx);
10418a531400SJon Cooper return rc;
10428a531400SJon Cooper }
10438a531400SJon Cooper
1044874aeea5SJeff Kirsher /* NIC initialisation
1045874aeea5SJeff Kirsher *
1046874aeea5SJeff Kirsher * This is called at module load (or hotplug insertion,
104773ba7b68SBen Hutchings * theoretically). It sets up PCI mappings, resets the NIC,
1048874aeea5SJeff Kirsher * sets up and registers the network devices with the kernel and hooks
1049874aeea5SJeff Kirsher * the interrupt service routine. It does not prepare the device for
1050874aeea5SJeff Kirsher * transmission; this is left to the first time one of the network
1051874aeea5SJeff Kirsher * interfaces is brought up (i.e. efx_net_open).
1052874aeea5SJeff Kirsher */
efx_pci_probe(struct pci_dev * pci_dev,const struct pci_device_id * entry)105387d1fc11SBill Pemberton static int efx_pci_probe(struct pci_dev *pci_dev,
1054874aeea5SJeff Kirsher const struct pci_device_id *entry)
1055874aeea5SJeff Kirsher {
10567e773594SJonathan Cooper struct efx_probe_data *probe_data, **probe_ptr;
1057874aeea5SJeff Kirsher struct net_device *net_dev;
1058874aeea5SJeff Kirsher struct efx_nic *efx;
1059fadac6aaSBen Hutchings int rc;
1060874aeea5SJeff Kirsher
10617e773594SJonathan Cooper /* Allocate probe data and struct efx_nic */
10627e773594SJonathan Cooper probe_data = kzalloc(sizeof(*probe_data), GFP_KERNEL);
10637e773594SJonathan Cooper if (!probe_data)
10647e773594SJonathan Cooper return -ENOMEM;
10657e773594SJonathan Cooper probe_data->pci_dev = pci_dev;
10667e773594SJonathan Cooper efx = &probe_data->efx;
10677e773594SJonathan Cooper
10687e773594SJonathan Cooper /* Allocate and initialise a struct net_device */
10697e773594SJonathan Cooper net_dev = alloc_etherdev_mq(sizeof(probe_data), EFX_MAX_CORE_TX_QUEUES);
10706c412da5SChristophe JAILLET if (!net_dev) {
10716c412da5SChristophe JAILLET rc = -ENOMEM;
10726c412da5SChristophe JAILLET goto fail0;
10736c412da5SChristophe JAILLET }
10747e773594SJonathan Cooper probe_ptr = netdev_priv(net_dev);
10757e773594SJonathan Cooper *probe_ptr = probe_data;
10767e773594SJonathan Cooper efx->net_dev = net_dev;
1077adeb15aaSBen Hutchings efx->type = (const struct efx_nic_type *) entry->driver_data;
1078ebfcd0fdSAndrew Rybchenko efx->fixed_features |= NETIF_F_HIGHDMA;
1079eb7cfd8cSAndrew Rybchenko
1080874aeea5SJeff Kirsher pci_set_drvdata(pci_dev, efx);
1081874aeea5SJeff Kirsher SET_NETDEV_DEV(net_dev, &pci_dev->dev);
108262ac3ce5SJonathan Cooper rc = efx_init_struct(efx, pci_dev);
1083874aeea5SJeff Kirsher if (rc)
1084874aeea5SJeff Kirsher goto fail1;
108562ac3ce5SJonathan Cooper efx->mdio.dev = net_dev;
1086874aeea5SJeff Kirsher
1087fa44821aSHeiner Kallweit pci_info(pci_dev, "Solarflare NIC detected\n");
1088874aeea5SJeff Kirsher
10896f7f8aa6SShradha Shah if (!efx->type->is_vf)
1090ef215e64SBen Hutchings efx_probe_vpd_strings(efx);
1091460eeaa0SBen Hutchings
1092874aeea5SJeff Kirsher /* Set up basic I/O (BAR mappings etc) */
1093b1940451SAlex Maftei (amaftei) rc = efx_init_io(efx, efx->type->mem_bar(efx), efx->type->max_dma_mask,
1094b1940451SAlex Maftei (amaftei) efx->type->mem_map_size(efx));
1095874aeea5SJeff Kirsher if (rc)
1096874aeea5SJeff Kirsher goto fail2;
1097874aeea5SJeff Kirsher
10988a531400SJon Cooper rc = efx_pci_probe_post_io(efx);
10998a531400SJon Cooper if (rc) {
11008a531400SJon Cooper /* On failure, retry once immediately.
11018a531400SJon Cooper * If we aborted probe due to a scheduled reset, dismiss it.
11028a531400SJon Cooper */
11038a531400SJon Cooper efx->reset_pending = 0;
11048a531400SJon Cooper rc = efx_pci_probe_post_io(efx);
11058a531400SJon Cooper if (rc) {
11068a531400SJon Cooper /* On another failure, retry once more
11078a531400SJon Cooper * after a 50-305ms delay.
11088a531400SJon Cooper */
11098a531400SJon Cooper unsigned char r;
11108a531400SJon Cooper
11118a531400SJon Cooper get_random_bytes(&r, 1);
11128a531400SJon Cooper msleep((unsigned int)r + 50);
11138a531400SJon Cooper efx->reset_pending = 0;
11148a531400SJon Cooper rc = efx_pci_probe_post_io(efx);
11158a531400SJon Cooper }
11168a531400SJon Cooper }
1117fadac6aaSBen Hutchings if (rc)
1118874aeea5SJeff Kirsher goto fail3;
1119874aeea5SJeff Kirsher
1120874aeea5SJeff Kirsher netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
1121874aeea5SJeff Kirsher
11227c43161cSBen Hutchings /* Try to create MTDs, but allow this to fail */
1123874aeea5SJeff Kirsher rtnl_lock();
11247c43161cSBen Hutchings rc = efx_mtd_probe(efx);
1125874aeea5SJeff Kirsher rtnl_unlock();
112609a04204SBert Kenward if (rc && rc != -EPERM)
11277c43161cSBen Hutchings netif_warn(efx, probe, efx->net_dev,
11287c43161cSBen Hutchings "failed to create MTDs (%d)\n", rc);
11297c43161cSBen Hutchings
1130e5fbd977SJon Cooper if (efx->type->udp_tnl_push_ports)
1131e5fbd977SJon Cooper efx->type->udp_tnl_push_ports(efx);
1132e5fbd977SJon Cooper
1133874aeea5SJeff Kirsher return 0;
1134874aeea5SJeff Kirsher
1135874aeea5SJeff Kirsher fail3:
113666a65128SEdward Cree efx_fini_io(efx);
1137874aeea5SJeff Kirsher fail2:
1138874aeea5SJeff Kirsher efx_fini_struct(efx);
1139874aeea5SJeff Kirsher fail1:
1140874aeea5SJeff Kirsher WARN_ON(rc > 0);
1141874aeea5SJeff Kirsher netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
1142874aeea5SJeff Kirsher free_netdev(net_dev);
11436c412da5SChristophe JAILLET fail0:
11446c412da5SChristophe JAILLET kfree(probe_data);
1145874aeea5SJeff Kirsher return rc;
1146874aeea5SJeff Kirsher }
1147874aeea5SJeff Kirsher
1148834e23ddSShradha Shah /* efx_pci_sriov_configure returns the actual number of Virtual Functions
1149834e23ddSShradha Shah * enabled on success
1150834e23ddSShradha Shah */
1151834e23ddSShradha Shah #ifdef CONFIG_SFC_SRIOV
efx_pci_sriov_configure(struct pci_dev * dev,int num_vfs)1152834e23ddSShradha Shah static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1153834e23ddSShradha Shah {
1154834e23ddSShradha Shah int rc;
1155834e23ddSShradha Shah struct efx_nic *efx = pci_get_drvdata(dev);
1156834e23ddSShradha Shah
1157834e23ddSShradha Shah if (efx->type->sriov_configure) {
1158834e23ddSShradha Shah rc = efx->type->sriov_configure(efx, num_vfs);
1159834e23ddSShradha Shah if (rc)
1160834e23ddSShradha Shah return rc;
1161834e23ddSShradha Shah else
1162834e23ddSShradha Shah return num_vfs;
1163834e23ddSShradha Shah } else
1164834e23ddSShradha Shah return -EOPNOTSUPP;
1165834e23ddSShradha Shah }
1166834e23ddSShradha Shah #endif
1167834e23ddSShradha Shah
efx_pm_freeze(struct device * dev)1168874aeea5SJeff Kirsher static int efx_pm_freeze(struct device *dev)
1169874aeea5SJeff Kirsher {
11703e03a8baSChuhong Yuan struct efx_nic *efx = dev_get_drvdata(dev);
1171874aeea5SJeff Kirsher
117261da026dSBen Hutchings rtnl_lock();
117361da026dSBen Hutchings
1174813cf9d1SJonathan Cooper if (efx_net_active(efx->state)) {
1175c2f3b8e3SDaniel Pieczko efx_device_detach_sync(efx);
1176874aeea5SJeff Kirsher
1177874aeea5SJeff Kirsher efx_stop_all(efx);
1178d8291187SBen Hutchings efx_disable_interrupts(efx);
1179813cf9d1SJonathan Cooper
1180813cf9d1SJonathan Cooper efx->state = efx_freeze(efx->state);
11816032fb56SBen Hutchings }
1182874aeea5SJeff Kirsher
118361da026dSBen Hutchings rtnl_unlock();
118461da026dSBen Hutchings
1185874aeea5SJeff Kirsher return 0;
1186874aeea5SJeff Kirsher }
1187874aeea5SJeff Kirsher
efx_pci_shutdown(struct pci_dev * pci_dev)118841e3b072SPieter Jansen van Vuuren static void efx_pci_shutdown(struct pci_dev *pci_dev)
118941e3b072SPieter Jansen van Vuuren {
119041e3b072SPieter Jansen van Vuuren struct efx_nic *efx = pci_get_drvdata(pci_dev);
119141e3b072SPieter Jansen van Vuuren
119241e3b072SPieter Jansen van Vuuren if (!efx)
119341e3b072SPieter Jansen van Vuuren return;
119441e3b072SPieter Jansen van Vuuren
119541e3b072SPieter Jansen van Vuuren efx_pm_freeze(&pci_dev->dev);
119641e3b072SPieter Jansen van Vuuren pci_disable_device(pci_dev);
119741e3b072SPieter Jansen van Vuuren }
119841e3b072SPieter Jansen van Vuuren
efx_pm_thaw(struct device * dev)1199874aeea5SJeff Kirsher static int efx_pm_thaw(struct device *dev)
1200874aeea5SJeff Kirsher {
1201261e4d96SJon Cooper int rc;
12023e03a8baSChuhong Yuan struct efx_nic *efx = dev_get_drvdata(dev);
1203874aeea5SJeff Kirsher
120461da026dSBen Hutchings rtnl_lock();
120561da026dSBen Hutchings
1206813cf9d1SJonathan Cooper if (efx_frozen(efx->state)) {
1207261e4d96SJon Cooper rc = efx_enable_interrupts(efx);
1208261e4d96SJon Cooper if (rc)
1209261e4d96SJon Cooper goto fail;
1210874aeea5SJeff Kirsher
1211874aeea5SJeff Kirsher mutex_lock(&efx->mac_lock);
1212c77289b4SEdward Cree efx_mcdi_port_reconfigure(efx);
1213874aeea5SJeff Kirsher mutex_unlock(&efx->mac_lock);
1214874aeea5SJeff Kirsher
1215874aeea5SJeff Kirsher efx_start_all(efx);
1216874aeea5SJeff Kirsher
12179c568fd8SPeter Dunning efx_device_attach_if_not_resetting(efx);
1218874aeea5SJeff Kirsher
1219813cf9d1SJonathan Cooper efx->state = efx_thaw(efx->state);
1220874aeea5SJeff Kirsher
1221874aeea5SJeff Kirsher efx->type->resume_wol(efx);
12226032fb56SBen Hutchings }
1223874aeea5SJeff Kirsher
122461da026dSBen Hutchings rtnl_unlock();
122561da026dSBen Hutchings
1226874aeea5SJeff Kirsher /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
122782c64484SAlex Maftei (amaftei) efx_queue_reset_work(efx);
1228874aeea5SJeff Kirsher
1229874aeea5SJeff Kirsher return 0;
1230261e4d96SJon Cooper
1231261e4d96SJon Cooper fail:
1232261e4d96SJon Cooper rtnl_unlock();
1233261e4d96SJon Cooper
1234261e4d96SJon Cooper return rc;
1235874aeea5SJeff Kirsher }
1236874aeea5SJeff Kirsher
efx_pm_poweroff(struct device * dev)1237874aeea5SJeff Kirsher static int efx_pm_poweroff(struct device *dev)
1238874aeea5SJeff Kirsher {
1239874aeea5SJeff Kirsher struct pci_dev *pci_dev = to_pci_dev(dev);
1240874aeea5SJeff Kirsher struct efx_nic *efx = pci_get_drvdata(pci_dev);
1241874aeea5SJeff Kirsher
1242874aeea5SJeff Kirsher efx->type->fini(efx);
1243874aeea5SJeff Kirsher
1244874aeea5SJeff Kirsher efx->reset_pending = 0;
1245874aeea5SJeff Kirsher
1246874aeea5SJeff Kirsher pci_save_state(pci_dev);
1247874aeea5SJeff Kirsher return pci_set_power_state(pci_dev, PCI_D3hot);
1248874aeea5SJeff Kirsher }
1249874aeea5SJeff Kirsher
1250874aeea5SJeff Kirsher /* Used for both resume and restore */
efx_pm_resume(struct device * dev)1251874aeea5SJeff Kirsher static int efx_pm_resume(struct device *dev)
1252874aeea5SJeff Kirsher {
1253874aeea5SJeff Kirsher struct pci_dev *pci_dev = to_pci_dev(dev);
1254874aeea5SJeff Kirsher struct efx_nic *efx = pci_get_drvdata(pci_dev);
1255874aeea5SJeff Kirsher int rc;
1256874aeea5SJeff Kirsher
1257874aeea5SJeff Kirsher rc = pci_set_power_state(pci_dev, PCI_D0);
1258874aeea5SJeff Kirsher if (rc)
1259874aeea5SJeff Kirsher return rc;
1260874aeea5SJeff Kirsher pci_restore_state(pci_dev);
1261874aeea5SJeff Kirsher rc = pci_enable_device(pci_dev);
1262874aeea5SJeff Kirsher if (rc)
1263874aeea5SJeff Kirsher return rc;
1264874aeea5SJeff Kirsher pci_set_master(efx->pci_dev);
1265874aeea5SJeff Kirsher rc = efx->type->reset(efx, RESET_TYPE_ALL);
1266874aeea5SJeff Kirsher if (rc)
1267874aeea5SJeff Kirsher return rc;
1268193f2003SBert Kenward down_write(&efx->filter_sem);
1269874aeea5SJeff Kirsher rc = efx->type->init(efx);
1270193f2003SBert Kenward up_write(&efx->filter_sem);
1271874aeea5SJeff Kirsher if (rc)
1272874aeea5SJeff Kirsher return rc;
1273261e4d96SJon Cooper rc = efx_pm_thaw(dev);
1274261e4d96SJon Cooper return rc;
1275874aeea5SJeff Kirsher }
1276874aeea5SJeff Kirsher
efx_pm_suspend(struct device * dev)1277874aeea5SJeff Kirsher static int efx_pm_suspend(struct device *dev)
1278874aeea5SJeff Kirsher {
1279874aeea5SJeff Kirsher int rc;
1280874aeea5SJeff Kirsher
1281874aeea5SJeff Kirsher efx_pm_freeze(dev);
1282874aeea5SJeff Kirsher rc = efx_pm_poweroff(dev);
1283874aeea5SJeff Kirsher if (rc)
1284874aeea5SJeff Kirsher efx_pm_resume(dev);
1285874aeea5SJeff Kirsher return rc;
1286874aeea5SJeff Kirsher }
1287874aeea5SJeff Kirsher
128818e83e4cSBen Hutchings static const struct dev_pm_ops efx_pm_ops = {
1289874aeea5SJeff Kirsher .suspend = efx_pm_suspend,
1290874aeea5SJeff Kirsher .resume = efx_pm_resume,
1291874aeea5SJeff Kirsher .freeze = efx_pm_freeze,
1292874aeea5SJeff Kirsher .thaw = efx_pm_thaw,
1293874aeea5SJeff Kirsher .poweroff = efx_pm_poweroff,
1294874aeea5SJeff Kirsher .restore = efx_pm_resume,
1295874aeea5SJeff Kirsher };
1296874aeea5SJeff Kirsher
1297874aeea5SJeff Kirsher static struct pci_driver efx_pci_driver = {
1298874aeea5SJeff Kirsher .name = KBUILD_MODNAME,
1299874aeea5SJeff Kirsher .id_table = efx_pci_table,
1300874aeea5SJeff Kirsher .probe = efx_pci_probe,
1301874aeea5SJeff Kirsher .remove = efx_pci_remove,
1302874aeea5SJeff Kirsher .driver.pm = &efx_pm_ops,
130341e3b072SPieter Jansen van Vuuren .shutdown = efx_pci_shutdown,
1304626950dbSAlexandre Rames .err_handler = &efx_err_handlers,
1305834e23ddSShradha Shah #ifdef CONFIG_SFC_SRIOV
1306834e23ddSShradha Shah .sriov_configure = efx_pci_sriov_configure,
1307834e23ddSShradha Shah #endif
1308874aeea5SJeff Kirsher };
1309874aeea5SJeff Kirsher
1310874aeea5SJeff Kirsher /**************************************************************************
1311874aeea5SJeff Kirsher *
1312874aeea5SJeff Kirsher * Kernel module interface
1313874aeea5SJeff Kirsher *
1314874aeea5SJeff Kirsher *************************************************************************/
1315874aeea5SJeff Kirsher
efx_init_module(void)1316874aeea5SJeff Kirsher static int __init efx_init_module(void)
1317874aeea5SJeff Kirsher {
1318874aeea5SJeff Kirsher int rc;
1319874aeea5SJeff Kirsher
132060bd2a2dSEdward Cree printk(KERN_INFO "Solarflare NET driver\n");
1321874aeea5SJeff Kirsher
1322874aeea5SJeff Kirsher rc = register_netdevice_notifier(&efx_netdev_notifier);
1323874aeea5SJeff Kirsher if (rc)
1324874aeea5SJeff Kirsher goto err_notifier;
1325874aeea5SJeff Kirsher
132682c64484SAlex Maftei (amaftei) rc = efx_create_reset_workqueue();
132782c64484SAlex Maftei (amaftei) if (rc)
1328874aeea5SJeff Kirsher goto err_reset;
1329874aeea5SJeff Kirsher
1330874aeea5SJeff Kirsher rc = pci_register_driver(&efx_pci_driver);
1331874aeea5SJeff Kirsher if (rc < 0)
1332874aeea5SJeff Kirsher goto err_pci;
1333874aeea5SJeff Kirsher
133451b35a45SEdward Cree rc = pci_register_driver(&ef100_pci_driver);
133551b35a45SEdward Cree if (rc < 0)
133651b35a45SEdward Cree goto err_pci_ef100;
133751b35a45SEdward Cree
1338874aeea5SJeff Kirsher return 0;
1339874aeea5SJeff Kirsher
134051b35a45SEdward Cree err_pci_ef100:
134151b35a45SEdward Cree pci_unregister_driver(&efx_pci_driver);
1342874aeea5SJeff Kirsher err_pci:
134382c64484SAlex Maftei (amaftei) efx_destroy_reset_workqueue();
1344874aeea5SJeff Kirsher err_reset:
1345874aeea5SJeff Kirsher unregister_netdevice_notifier(&efx_netdev_notifier);
1346874aeea5SJeff Kirsher err_notifier:
1347874aeea5SJeff Kirsher return rc;
1348874aeea5SJeff Kirsher }
1349874aeea5SJeff Kirsher
efx_exit_module(void)1350874aeea5SJeff Kirsher static void __exit efx_exit_module(void)
1351874aeea5SJeff Kirsher {
1352874aeea5SJeff Kirsher printk(KERN_INFO "Solarflare NET driver unloading\n");
1353874aeea5SJeff Kirsher
135451b35a45SEdward Cree pci_unregister_driver(&ef100_pci_driver);
1355874aeea5SJeff Kirsher pci_unregister_driver(&efx_pci_driver);
135682c64484SAlex Maftei (amaftei) efx_destroy_reset_workqueue();
1357874aeea5SJeff Kirsher unregister_netdevice_notifier(&efx_netdev_notifier);
1358874aeea5SJeff Kirsher
1359874aeea5SJeff Kirsher }
1360874aeea5SJeff Kirsher
1361874aeea5SJeff Kirsher module_init(efx_init_module);
1362874aeea5SJeff Kirsher module_exit(efx_exit_module);
1363874aeea5SJeff Kirsher
1364874aeea5SJeff Kirsher MODULE_AUTHOR("Solarflare Communications and "
1365874aeea5SJeff Kirsher "Michael Brown <mbrown@fensystems.co.uk>");
13666a350fdbSBen Hutchings MODULE_DESCRIPTION("Solarflare network driver");
1367874aeea5SJeff Kirsher MODULE_LICENSE("GPL");
1368874aeea5SJeff Kirsher MODULE_DEVICE_TABLE(pci, efx_pci_table);
1369