1// SPDX-License-Identifier: GPL-2.0-only
2/****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2005-2006 Fen Systems Ltd.
5 * Copyright 2005-2013 Solarflare Communications Inc.
6 */
7
8#include <linux/filter.h>
9#include <linux/module.h>
10#include <linux/pci.h>
11#include <linux/netdevice.h>
12#include <linux/etherdevice.h>
13#include <linux/delay.h>
14#include <linux/notifier.h>
15#include <linux/ip.h>
16#include <linux/tcp.h>
17#include <linux/in.h>
18#include <linux/ethtool.h>
19#include <linux/topology.h>
20#include <linux/gfp.h>
21#include <linux/interrupt.h>
22#include "net_driver.h"
23#include <net/gre.h>
24#include <net/udp_tunnel.h>
25#include "efx.h"
26#include "efx_common.h"
27#include "efx_channels.h"
28#include "rx_common.h"
29#include "tx_common.h"
30#include "nic.h"
31#include "io.h"
32#include "selftest.h"
33#include "sriov.h"
34#ifdef CONFIG_SFC_SIENA_SRIOV
35#include "siena_sriov.h"
36#endif
37
38#include "mcdi_port_common.h"
39#include "mcdi_pcol.h"
40#include "workarounds.h"
41
42/**************************************************************************
43 *
44 * Configurable values
45 *
46 *************************************************************************/
47
48module_param_named(interrupt_mode, efx_siena_interrupt_mode, uint, 0444);
49MODULE_PARM_DESC(interrupt_mode,
50		 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
51
52module_param_named(rss_cpus, efx_siena_rss_cpus, uint, 0444);
53MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
54
55/*
56 * Use separate channels for TX and RX events
57 *
58 * Set this to 1 to use separate channels for TX and RX. It allows us
59 * to control interrupt affinity separately for TX and RX.
60 *
61 * This is only used in MSI-X interrupt mode
62 */
63bool efx_siena_separate_tx_channels;
64module_param_named(efx_separate_tx_channels, efx_siena_separate_tx_channels,
65		   bool, 0444);
66MODULE_PARM_DESC(efx_separate_tx_channels,
67		 "Use separate channels for TX and RX");
68
69/* Initial interrupt moderation settings.  They can be modified after
70 * module load with ethtool.
71 *
72 * The default for RX should strike a balance between increasing the
73 * round-trip latency and reducing overhead.
74 */
75static unsigned int rx_irq_mod_usec = 60;
76
77/* Initial interrupt moderation settings.  They can be modified after
78 * module load with ethtool.
79 *
80 * This default is chosen to ensure that a 10G link does not go idle
81 * while a TX queue is stopped after it has become full.  A queue is
82 * restarted when it drops below half full.  The time this takes (assuming
83 * worst case 3 descriptors per packet and 1024 descriptors) is
84 *   512 / 3 * 1.2 = 205 usec.
85 */
86static unsigned int tx_irq_mod_usec = 150;
87
88static bool phy_flash_cfg;
89module_param(phy_flash_cfg, bool, 0644);
90MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
91
92static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
93			 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
94			 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
95			 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
96module_param(debug, uint, 0);
97MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
98
99/**************************************************************************
100 *
101 * Utility functions and prototypes
102 *
103 *************************************************************************/
104
105static void efx_remove_port(struct efx_nic *efx);
106static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog);
107static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp);
108static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
109			u32 flags);
110
111#define EFX_ASSERT_RESET_SERIALISED(efx)		\
112	do {						\
113		if ((efx->state == STATE_READY) ||	\
114		    (efx->state == STATE_RECOVERY) ||	\
115		    (efx->state == STATE_DISABLED))	\
116			ASSERT_RTNL();			\
117	} while (0)
118
119/**************************************************************************
120 *
121 * Port handling
122 *
123 **************************************************************************/
124
125static void efx_fini_port(struct efx_nic *efx);
126
127static int efx_probe_port(struct efx_nic *efx)
128{
129	int rc;
130
131	netif_dbg(efx, probe, efx->net_dev, "create port\n");
132
133	if (phy_flash_cfg)
134		efx->phy_mode = PHY_MODE_SPECIAL;
135
136	/* Connect up MAC/PHY operations table */
137	rc = efx->type->probe_port(efx);
138	if (rc)
139		return rc;
140
141	/* Initialise MAC address to permanent address */
142	eth_hw_addr_set(efx->net_dev, efx->net_dev->perm_addr);
143
144	return 0;
145}
146
147static int efx_init_port(struct efx_nic *efx)
148{
149	int rc;
150
151	netif_dbg(efx, drv, efx->net_dev, "init port\n");
152
153	mutex_lock(&efx->mac_lock);
154
155	efx->port_initialized = true;
156
157	/* Ensure the PHY advertises the correct flow control settings */
158	rc = efx_siena_mcdi_port_reconfigure(efx);
159	if (rc && rc != -EPERM)
160		goto fail;
161
162	mutex_unlock(&efx->mac_lock);
163	return 0;
164
165fail:
166	mutex_unlock(&efx->mac_lock);
167	return rc;
168}
169
170static void efx_fini_port(struct efx_nic *efx)
171{
172	netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
173
174	if (!efx->port_initialized)
175		return;
176
177	efx->port_initialized = false;
178
179	efx->link_state.up = false;
180	efx_siena_link_status_changed(efx);
181}
182
183static void efx_remove_port(struct efx_nic *efx)
184{
185	netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
186
187	efx->type->remove_port(efx);
188}
189
190/**************************************************************************
191 *
192 * NIC handling
193 *
194 **************************************************************************/
195
196static LIST_HEAD(efx_primary_list);
197static LIST_HEAD(efx_unassociated_list);
198
199static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
200{
201	return left->type == right->type &&
202		left->vpd_sn && right->vpd_sn &&
203		!strcmp(left->vpd_sn, right->vpd_sn);
204}
205
206static void efx_associate(struct efx_nic *efx)
207{
208	struct efx_nic *other, *next;
209
210	if (efx->primary == efx) {
211		/* Adding primary function; look for secondaries */
212
213		netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
214		list_add_tail(&efx->node, &efx_primary_list);
215
216		list_for_each_entry_safe(other, next, &efx_unassociated_list,
217					 node) {
218			if (efx_same_controller(efx, other)) {
219				list_del(&other->node);
220				netif_dbg(other, probe, other->net_dev,
221					  "moving to secondary list of %s %s\n",
222					  pci_name(efx->pci_dev),
223					  efx->net_dev->name);
224				list_add_tail(&other->node,
225					      &efx->secondary_list);
226				other->primary = efx;
227			}
228		}
229	} else {
230		/* Adding secondary function; look for primary */
231
232		list_for_each_entry(other, &efx_primary_list, node) {
233			if (efx_same_controller(efx, other)) {
234				netif_dbg(efx, probe, efx->net_dev,
235					  "adding to secondary list of %s %s\n",
236					  pci_name(other->pci_dev),
237					  other->net_dev->name);
238				list_add_tail(&efx->node,
239					      &other->secondary_list);
240				efx->primary = other;
241				return;
242			}
243		}
244
245		netif_dbg(efx, probe, efx->net_dev,
246			  "adding to unassociated list\n");
247		list_add_tail(&efx->node, &efx_unassociated_list);
248	}
249}
250
251static void efx_dissociate(struct efx_nic *efx)
252{
253	struct efx_nic *other, *next;
254
255	list_del(&efx->node);
256	efx->primary = NULL;
257
258	list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
259		list_del(&other->node);
260		netif_dbg(other, probe, other->net_dev,
261			  "moving to unassociated list\n");
262		list_add_tail(&other->node, &efx_unassociated_list);
263		other->primary = NULL;
264	}
265}
266
267static int efx_probe_nic(struct efx_nic *efx)
268{
269	int rc;
270
271	netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
272
273	/* Carry out hardware-type specific initialisation */
274	rc = efx->type->probe(efx);
275	if (rc)
276		return rc;
277
278	do {
279		if (!efx->max_channels || !efx->max_tx_channels) {
280			netif_err(efx, drv, efx->net_dev,
281				  "Insufficient resources to allocate"
282				  " any channels\n");
283			rc = -ENOSPC;
284			goto fail1;
285		}
286
287		/* Determine the number of channels and queues by trying
288		 * to hook in MSI-X interrupts.
289		 */
290		rc = efx_siena_probe_interrupts(efx);
291		if (rc)
292			goto fail1;
293
294		rc = efx_siena_set_channels(efx);
295		if (rc)
296			goto fail1;
297
298		/* dimension_resources can fail with EAGAIN */
299		rc = efx->type->dimension_resources(efx);
300		if (rc != 0 && rc != -EAGAIN)
301			goto fail2;
302
303		if (rc == -EAGAIN)
304			/* try again with new max_channels */
305			efx_siena_remove_interrupts(efx);
306
307	} while (rc == -EAGAIN);
308
309	if (efx->n_channels > 1)
310		netdev_rss_key_fill(efx->rss_context.rx_hash_key,
311				    sizeof(efx->rss_context.rx_hash_key));
312	efx_siena_set_default_rx_indir_table(efx, &efx->rss_context);
313
314	/* Initialise the interrupt moderation settings */
315	efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
316	efx_siena_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec,
317				      true, true);
318
319	return 0;
320
321fail2:
322	efx_siena_remove_interrupts(efx);
323fail1:
324	efx->type->remove(efx);
325	return rc;
326}
327
328static void efx_remove_nic(struct efx_nic *efx)
329{
330	netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
331
332	efx_siena_remove_interrupts(efx);
333	efx->type->remove(efx);
334}
335
336/**************************************************************************
337 *
338 * NIC startup/shutdown
339 *
340 *************************************************************************/
341
342static int efx_probe_all(struct efx_nic *efx)
343{
344	int rc;
345
346	rc = efx_probe_nic(efx);
347	if (rc) {
348		netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
349		goto fail1;
350	}
351
352	rc = efx_probe_port(efx);
353	if (rc) {
354		netif_err(efx, probe, efx->net_dev, "failed to create port\n");
355		goto fail2;
356	}
357
358	BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
359	if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
360		rc = -EINVAL;
361		goto fail3;
362	}
363
364#ifdef CONFIG_SFC_SIENA_SRIOV
365	rc = efx->type->vswitching_probe(efx);
366	if (rc) /* not fatal; the PF will still work fine */
367		netif_warn(efx, probe, efx->net_dev,
368			   "failed to setup vswitching rc=%d;"
369			   " VFs may not function\n", rc);
370#endif
371
372	rc = efx_siena_probe_filters(efx);
373	if (rc) {
374		netif_err(efx, probe, efx->net_dev,
375			  "failed to create filter tables\n");
376		goto fail4;
377	}
378
379	rc = efx_siena_probe_channels(efx);
380	if (rc)
381		goto fail5;
382
383	return 0;
384
385 fail5:
386	efx_siena_remove_filters(efx);
387 fail4:
388#ifdef CONFIG_SFC_SIENA_SRIOV
389	efx->type->vswitching_remove(efx);
390#endif
391 fail3:
392	efx_remove_port(efx);
393 fail2:
394	efx_remove_nic(efx);
395 fail1:
396	return rc;
397}
398
399static void efx_remove_all(struct efx_nic *efx)
400{
401	rtnl_lock();
402	efx_xdp_setup_prog(efx, NULL);
403	rtnl_unlock();
404
405	efx_siena_remove_channels(efx);
406	efx_siena_remove_filters(efx);
407#ifdef CONFIG_SFC_SIENA_SRIOV
408	efx->type->vswitching_remove(efx);
409#endif
410	efx_remove_port(efx);
411	efx_remove_nic(efx);
412}
413
414/**************************************************************************
415 *
416 * Interrupt moderation
417 *
418 **************************************************************************/
419unsigned int efx_siena_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
420{
421	if (usecs == 0)
422		return 0;
423	if (usecs * 1000 < efx->timer_quantum_ns)
424		return 1; /* never round down to 0 */
425	return usecs * 1000 / efx->timer_quantum_ns;
426}
427
428/* Set interrupt moderation parameters */
429int efx_siena_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
430				  unsigned int rx_usecs, bool rx_adaptive,
431				  bool rx_may_override_tx)
432{
433	struct efx_channel *channel;
434	unsigned int timer_max_us;
435
436	EFX_ASSERT_RESET_SERIALISED(efx);
437
438	timer_max_us = efx->timer_max_ns / 1000;
439
440	if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
441		return -EINVAL;
442
443	if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
444	    !rx_may_override_tx) {
445		netif_err(efx, drv, efx->net_dev, "Channels are shared. "
446			  "RX and TX IRQ moderation must be equal\n");
447		return -EINVAL;
448	}
449
450	efx->irq_rx_adaptive = rx_adaptive;
451	efx->irq_rx_moderation_us = rx_usecs;
452	efx_for_each_channel(channel, efx) {
453		if (efx_channel_has_rx_queue(channel))
454			channel->irq_moderation_us = rx_usecs;
455		else if (efx_channel_has_tx_queues(channel))
456			channel->irq_moderation_us = tx_usecs;
457		else if (efx_channel_is_xdp_tx(channel))
458			channel->irq_moderation_us = tx_usecs;
459	}
460
461	return 0;
462}
463
464void efx_siena_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
465				  unsigned int *rx_usecs, bool *rx_adaptive)
466{
467	*rx_adaptive = efx->irq_rx_adaptive;
468	*rx_usecs = efx->irq_rx_moderation_us;
469
470	/* If channels are shared between RX and TX, so is IRQ
471	 * moderation.  Otherwise, IRQ moderation is the same for all
472	 * TX channels and is not adaptive.
473	 */
474	if (efx->tx_channel_offset == 0) {
475		*tx_usecs = *rx_usecs;
476	} else {
477		struct efx_channel *tx_channel;
478
479		tx_channel = efx->channel[efx->tx_channel_offset];
480		*tx_usecs = tx_channel->irq_moderation_us;
481	}
482}
483
484/**************************************************************************
485 *
486 * ioctls
487 *
488 *************************************************************************/
489
490/* Net device ioctl
491 * Context: process, rtnl_lock() held.
492 */
493static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
494{
495	struct efx_nic *efx = netdev_priv(net_dev);
496	struct mii_ioctl_data *data = if_mii(ifr);
497
498	if (cmd == SIOCSHWTSTAMP)
499		return efx_siena_ptp_set_ts_config(efx, ifr);
500	if (cmd == SIOCGHWTSTAMP)
501		return efx_siena_ptp_get_ts_config(efx, ifr);
502
503	/* Convert phy_id from older PRTAD/DEVAD format */
504	if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
505	    (data->phy_id & 0xfc00) == 0x0400)
506		data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
507
508	return mdio_mii_ioctl(&efx->mdio, data, cmd);
509}
510
511/**************************************************************************
512 *
513 * Kernel net device interface
514 *
515 *************************************************************************/
516
517/* Context: process, rtnl_lock() held. */
518static int efx_net_open(struct net_device *net_dev)
519{
520	struct efx_nic *efx = netdev_priv(net_dev);
521	int rc;
522
523	netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
524		  raw_smp_processor_id());
525
526	rc = efx_check_disabled(efx);
527	if (rc)
528		return rc;
529	if (efx->phy_mode & PHY_MODE_SPECIAL)
530		return -EBUSY;
531	if (efx_siena_mcdi_poll_reboot(efx) && efx_siena_reset(efx, RESET_TYPE_ALL))
532		return -EIO;
533
534	/* Notify the kernel of the link state polled during driver load,
535	 * before the monitor starts running */
536	efx_siena_link_status_changed(efx);
537
538	efx_siena_start_all(efx);
539	if (efx->state == STATE_DISABLED || efx->reset_pending)
540		netif_device_detach(efx->net_dev);
541	efx_siena_selftest_async_start(efx);
542	return 0;
543}
544
545/* Context: process, rtnl_lock() held.
546 * Note that the kernel will ignore our return code; this method
547 * should really be a void.
548 */
549static int efx_net_stop(struct net_device *net_dev)
550{
551	struct efx_nic *efx = netdev_priv(net_dev);
552
553	netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
554		  raw_smp_processor_id());
555
556	/* Stop the device and flush all the channels */
557	efx_siena_stop_all(efx);
558
559	return 0;
560}
561
562static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
563{
564	struct efx_nic *efx = netdev_priv(net_dev);
565
566	if (efx->type->vlan_rx_add_vid)
567		return efx->type->vlan_rx_add_vid(efx, proto, vid);
568	else
569		return -EOPNOTSUPP;
570}
571
572static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
573{
574	struct efx_nic *efx = netdev_priv(net_dev);
575
576	if (efx->type->vlan_rx_kill_vid)
577		return efx->type->vlan_rx_kill_vid(efx, proto, vid);
578	else
579		return -EOPNOTSUPP;
580}
581
582static const struct net_device_ops efx_netdev_ops = {
583	.ndo_open		= efx_net_open,
584	.ndo_stop		= efx_net_stop,
585	.ndo_get_stats64	= efx_siena_net_stats,
586	.ndo_tx_timeout		= efx_siena_watchdog,
587	.ndo_start_xmit		= efx_siena_hard_start_xmit,
588	.ndo_validate_addr	= eth_validate_addr,
589	.ndo_eth_ioctl		= efx_ioctl,
590	.ndo_change_mtu		= efx_siena_change_mtu,
591	.ndo_set_mac_address	= efx_siena_set_mac_address,
592	.ndo_set_rx_mode	= efx_siena_set_rx_mode,
593	.ndo_set_features	= efx_siena_set_features,
594	.ndo_features_check	= efx_siena_features_check,
595	.ndo_vlan_rx_add_vid	= efx_vlan_rx_add_vid,
596	.ndo_vlan_rx_kill_vid	= efx_vlan_rx_kill_vid,
597#ifdef CONFIG_SFC_SIENA_SRIOV
598	.ndo_set_vf_mac		= efx_sriov_set_vf_mac,
599	.ndo_set_vf_vlan	= efx_sriov_set_vf_vlan,
600	.ndo_set_vf_spoofchk	= efx_sriov_set_vf_spoofchk,
601	.ndo_get_vf_config	= efx_sriov_get_vf_config,
602	.ndo_set_vf_link_state  = efx_sriov_set_vf_link_state,
603#endif
604	.ndo_get_phys_port_id   = efx_siena_get_phys_port_id,
605	.ndo_get_phys_port_name	= efx_siena_get_phys_port_name,
606	.ndo_setup_tc		= efx_siena_setup_tc,
607#ifdef CONFIG_RFS_ACCEL
608	.ndo_rx_flow_steer	= efx_siena_filter_rfs,
609#endif
610	.ndo_xdp_xmit		= efx_xdp_xmit,
611	.ndo_bpf		= efx_xdp
612};
613
614static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
615{
616	struct bpf_prog *old_prog;
617
618	if (efx->xdp_rxq_info_failed) {
619		netif_err(efx, drv, efx->net_dev,
620			  "Unable to bind XDP program due to previous failure of rxq_info\n");
621		return -EINVAL;
622	}
623
624	if (prog && efx->net_dev->mtu > efx_siena_xdp_max_mtu(efx)) {
625		netif_err(efx, drv, efx->net_dev,
626			  "Unable to configure XDP with MTU of %d (max: %d)\n",
627			  efx->net_dev->mtu, efx_siena_xdp_max_mtu(efx));
628		return -EINVAL;
629	}
630
631	old_prog = rtnl_dereference(efx->xdp_prog);
632	rcu_assign_pointer(efx->xdp_prog, prog);
633	/* Release the reference that was originally passed by the caller. */
634	if (old_prog)
635		bpf_prog_put(old_prog);
636
637	return 0;
638}
639
640/* Context: process, rtnl_lock() held. */
641static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
642{
643	struct efx_nic *efx = netdev_priv(dev);
644
645	switch (xdp->command) {
646	case XDP_SETUP_PROG:
647		return efx_xdp_setup_prog(efx, xdp->prog);
648	default:
649		return -EINVAL;
650	}
651}
652
653static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
654			u32 flags)
655{
656	struct efx_nic *efx = netdev_priv(dev);
657
658	if (!netif_running(dev))
659		return -EINVAL;
660
661	return efx_siena_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH);
662}
663
664static void efx_update_name(struct efx_nic *efx)
665{
666	strcpy(efx->name, efx->net_dev->name);
667	efx_siena_mtd_rename(efx);
668	efx_siena_set_channel_names(efx);
669}
670
671static int efx_netdev_event(struct notifier_block *this,
672			    unsigned long event, void *ptr)
673{
674	struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
675
676	if ((net_dev->netdev_ops == &efx_netdev_ops) &&
677	    event == NETDEV_CHANGENAME)
678		efx_update_name(netdev_priv(net_dev));
679
680	return NOTIFY_DONE;
681}
682
683static struct notifier_block efx_netdev_notifier = {
684	.notifier_call = efx_netdev_event,
685};
686
687static ssize_t phy_type_show(struct device *dev,
688			     struct device_attribute *attr, char *buf)
689{
690	struct efx_nic *efx = dev_get_drvdata(dev);
691	return sprintf(buf, "%d\n", efx->phy_type);
692}
693static DEVICE_ATTR_RO(phy_type);
694
695static int efx_register_netdev(struct efx_nic *efx)
696{
697	struct net_device *net_dev = efx->net_dev;
698	struct efx_channel *channel;
699	int rc;
700
701	net_dev->watchdog_timeo = 5 * HZ;
702	net_dev->irq = efx->pci_dev->irq;
703	net_dev->netdev_ops = &efx_netdev_ops;
704	if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
705		net_dev->priv_flags |= IFF_UNICAST_FLT;
706	net_dev->ethtool_ops = &efx_siena_ethtool_ops;
707	netif_set_tso_max_segs(net_dev, EFX_TSO_MAX_SEGS);
708	net_dev->min_mtu = EFX_MIN_MTU;
709	net_dev->max_mtu = EFX_MAX_MTU;
710
711	rtnl_lock();
712
713	/* Enable resets to be scheduled and check whether any were
714	 * already requested.  If so, the NIC is probably hosed so we
715	 * abort.
716	 */
717	efx->state = STATE_READY;
718	smp_mb(); /* ensure we change state before checking reset_pending */
719	if (efx->reset_pending) {
720		pci_err(efx->pci_dev, "aborting probe due to scheduled reset\n");
721		rc = -EIO;
722		goto fail_locked;
723	}
724
725	rc = dev_alloc_name(net_dev, net_dev->name);
726	if (rc < 0)
727		goto fail_locked;
728	efx_update_name(efx);
729
730	/* Always start with carrier off; PHY events will detect the link */
731	netif_carrier_off(net_dev);
732
733	rc = register_netdevice(net_dev);
734	if (rc)
735		goto fail_locked;
736
737	efx_for_each_channel(channel, efx) {
738		struct efx_tx_queue *tx_queue;
739		efx_for_each_channel_tx_queue(tx_queue, channel)
740			efx_siena_init_tx_queue_core_txq(tx_queue);
741	}
742
743	efx_associate(efx);
744
745	rtnl_unlock();
746
747	rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
748	if (rc) {
749		netif_err(efx, drv, efx->net_dev,
750			  "failed to init net dev attributes\n");
751		goto fail_registered;
752	}
753
754	efx_siena_init_mcdi_logging(efx);
755
756	return 0;
757
758fail_registered:
759	rtnl_lock();
760	efx_dissociate(efx);
761	unregister_netdevice(net_dev);
762fail_locked:
763	efx->state = STATE_UNINIT;
764	rtnl_unlock();
765	netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
766	return rc;
767}
768
769static void efx_unregister_netdev(struct efx_nic *efx)
770{
771	if (!efx->net_dev)
772		return;
773
774	BUG_ON(netdev_priv(efx->net_dev) != efx);
775
776	if (efx_dev_registered(efx)) {
777		strscpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
778		efx_siena_fini_mcdi_logging(efx);
779		device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
780		unregister_netdev(efx->net_dev);
781	}
782}
783
784/**************************************************************************
785 *
786 * List of NICs we support
787 *
788 **************************************************************************/
789
790/* PCI device ID table */
791static const struct pci_device_id efx_pci_table[] = {
792	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803),	/* SFC9020 */
793	 .driver_data = (unsigned long)&siena_a0_nic_type},
794	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813),	/* SFL9021 */
795	 .driver_data = (unsigned long)&siena_a0_nic_type},
796	{0}			/* end of list */
797};
798
799/**************************************************************************
800 *
801 * Data housekeeping
802 *
803 **************************************************************************/
804
805void efx_siena_update_sw_stats(struct efx_nic *efx, u64 *stats)
806{
807	u64 n_rx_nodesc_trunc = 0;
808	struct efx_channel *channel;
809
810	efx_for_each_channel(channel, efx)
811		n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
812	stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
813	stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
814}
815
816/**************************************************************************
817 *
818 * PCI interface
819 *
820 **************************************************************************/
821
822/* Main body of final NIC shutdown code
823 * This is called only at module unload (or hotplug removal).
824 */
825static void efx_pci_remove_main(struct efx_nic *efx)
826{
827	/* Flush reset_work. It can no longer be scheduled since we
828	 * are not READY.
829	 */
830	BUG_ON(efx->state == STATE_READY);
831	efx_siena_flush_reset_workqueue(efx);
832
833	efx_siena_disable_interrupts(efx);
834	efx_siena_clear_interrupt_affinity(efx);
835	efx_siena_fini_interrupt(efx);
836	efx_fini_port(efx);
837	efx->type->fini(efx);
838	efx_siena_fini_napi(efx);
839	efx_remove_all(efx);
840}
841
842/* Final NIC shutdown
843 * This is called only at module unload (or hotplug removal).  A PF can call
844 * this on its VFs to ensure they are unbound first.
845 */
846static void efx_pci_remove(struct pci_dev *pci_dev)
847{
848	struct efx_nic *efx;
849
850	efx = pci_get_drvdata(pci_dev);
851	if (!efx)
852		return;
853
854	/* Mark the NIC as fini, then stop the interface */
855	rtnl_lock();
856	efx_dissociate(efx);
857	dev_close(efx->net_dev);
858	efx_siena_disable_interrupts(efx);
859	efx->state = STATE_UNINIT;
860	rtnl_unlock();
861
862	if (efx->type->sriov_fini)
863		efx->type->sriov_fini(efx);
864
865	efx_unregister_netdev(efx);
866
867	efx_siena_mtd_remove(efx);
868
869	efx_pci_remove_main(efx);
870
871	efx_siena_fini_io(efx);
872	netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
873
874	efx_siena_fini_struct(efx);
875	free_netdev(efx->net_dev);
876};
877
878/* NIC VPD information
879 * Called during probe to display the part number of the
880 * installed NIC.
881 */
882static void efx_probe_vpd_strings(struct efx_nic *efx)
883{
884	struct pci_dev *dev = efx->pci_dev;
885	unsigned int vpd_size, kw_len;
886	u8 *vpd_data;
887	int start;
888
889	vpd_data = pci_vpd_alloc(dev, &vpd_size);
890	if (IS_ERR(vpd_data)) {
891		pci_warn(dev, "Unable to read VPD\n");
892		return;
893	}
894
895	start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
896					     PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
897	if (start < 0)
898		pci_err(dev, "Part number not found or incomplete\n");
899	else
900		pci_info(dev, "Part Number : %.*s\n", kw_len, vpd_data + start);
901
902	start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
903					     PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);
904	if (start < 0)
905		pci_err(dev, "Serial number not found or incomplete\n");
906	else
907		efx->vpd_sn = kmemdup_nul(vpd_data + start, kw_len, GFP_KERNEL);
908
909	kfree(vpd_data);
910}
911
912
913/* Main body of NIC initialisation
914 * This is called at module load (or hotplug insertion, theoretically).
915 */
916static int efx_pci_probe_main(struct efx_nic *efx)
917{
918	int rc;
919
920	/* Do start-of-day initialisation */
921	rc = efx_probe_all(efx);
922	if (rc)
923		goto fail1;
924
925	efx_siena_init_napi(efx);
926
927	down_write(&efx->filter_sem);
928	rc = efx->type->init(efx);
929	up_write(&efx->filter_sem);
930	if (rc) {
931		pci_err(efx->pci_dev, "failed to initialise NIC\n");
932		goto fail3;
933	}
934
935	rc = efx_init_port(efx);
936	if (rc) {
937		netif_err(efx, probe, efx->net_dev,
938			  "failed to initialise port\n");
939		goto fail4;
940	}
941
942	rc = efx_siena_init_interrupt(efx);
943	if (rc)
944		goto fail5;
945
946	efx_siena_set_interrupt_affinity(efx);
947	rc = efx_siena_enable_interrupts(efx);
948	if (rc)
949		goto fail6;
950
951	return 0;
952
953 fail6:
954	efx_siena_clear_interrupt_affinity(efx);
955	efx_siena_fini_interrupt(efx);
956 fail5:
957	efx_fini_port(efx);
958 fail4:
959	efx->type->fini(efx);
960 fail3:
961	efx_siena_fini_napi(efx);
962	efx_remove_all(efx);
963 fail1:
964	return rc;
965}
966
967static int efx_pci_probe_post_io(struct efx_nic *efx)
968{
969	struct net_device *net_dev = efx->net_dev;
970	int rc = efx_pci_probe_main(efx);
971
972	if (rc)
973		return rc;
974
975	if (efx->type->sriov_init) {
976		rc = efx->type->sriov_init(efx);
977		if (rc)
978			pci_err(efx->pci_dev, "SR-IOV can't be enabled rc %d\n",
979				rc);
980	}
981
982	/* Determine netdevice features */
983	net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
984			      NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
985	if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
986		net_dev->features |= NETIF_F_TSO6;
987	/* Check whether device supports TSO */
988	if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
989		net_dev->features &= ~NETIF_F_ALL_TSO;
990	/* Mask for features that also apply to VLAN devices */
991	net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
992				   NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
993				   NETIF_F_RXCSUM);
994
995	net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
996
997	/* Disable receiving frames with bad FCS, by default. */
998	net_dev->features &= ~NETIF_F_RXALL;
999
1000	/* Disable VLAN filtering by default.  It may be enforced if
1001	 * the feature is fixed (i.e. VLAN filters are required to
1002	 * receive VLAN tagged packets due to vPort restrictions).
1003	 */
1004	net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1005	net_dev->features |= efx->fixed_features;
1006
1007	net_dev->xdp_features = NETDEV_XDP_ACT_BASIC |
1008				NETDEV_XDP_ACT_REDIRECT |
1009				NETDEV_XDP_ACT_NDO_XMIT;
1010
1011	rc = efx_register_netdev(efx);
1012	if (!rc)
1013		return 0;
1014
1015	efx_pci_remove_main(efx);
1016	return rc;
1017}
1018
1019/* NIC initialisation
1020 *
1021 * This is called at module load (or hotplug insertion,
1022 * theoretically).  It sets up PCI mappings, resets the NIC,
1023 * sets up and registers the network devices with the kernel and hooks
1024 * the interrupt service routine.  It does not prepare the device for
1025 * transmission; this is left to the first time one of the network
1026 * interfaces is brought up (i.e. efx_net_open).
1027 */
1028static int efx_pci_probe(struct pci_dev *pci_dev,
1029			 const struct pci_device_id *entry)
1030{
1031	struct net_device *net_dev;
1032	struct efx_nic *efx;
1033	int rc;
1034
1035	/* Allocate and initialise a struct net_device and struct efx_nic */
1036	net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
1037				     EFX_MAX_RX_QUEUES);
1038	if (!net_dev)
1039		return -ENOMEM;
1040	efx = netdev_priv(net_dev);
1041	efx->type = (const struct efx_nic_type *) entry->driver_data;
1042	efx->fixed_features |= NETIF_F_HIGHDMA;
1043
1044	pci_set_drvdata(pci_dev, efx);
1045	SET_NETDEV_DEV(net_dev, &pci_dev->dev);
1046	rc = efx_siena_init_struct(efx, pci_dev, net_dev);
1047	if (rc)
1048		goto fail1;
1049
1050	pci_info(pci_dev, "Solarflare NIC detected\n");
1051
1052	if (!efx->type->is_vf)
1053		efx_probe_vpd_strings(efx);
1054
1055	/* Set up basic I/O (BAR mappings etc) */
1056	rc = efx_siena_init_io(efx, efx->type->mem_bar(efx),
1057			       efx->type->max_dma_mask,
1058			       efx->type->mem_map_size(efx));
1059	if (rc)
1060		goto fail2;
1061
1062	rc = efx_pci_probe_post_io(efx);
1063	if (rc) {
1064		/* On failure, retry once immediately.
1065		 * If we aborted probe due to a scheduled reset, dismiss it.
1066		 */
1067		efx->reset_pending = 0;
1068		rc = efx_pci_probe_post_io(efx);
1069		if (rc) {
1070			/* On another failure, retry once more
1071			 * after a 50-305ms delay.
1072			 */
1073			unsigned char r;
1074
1075			get_random_bytes(&r, 1);
1076			msleep((unsigned int)r + 50);
1077			efx->reset_pending = 0;
1078			rc = efx_pci_probe_post_io(efx);
1079		}
1080	}
1081	if (rc)
1082		goto fail3;
1083
1084	netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
1085
1086	/* Try to create MTDs, but allow this to fail */
1087	rtnl_lock();
1088	rc = efx_mtd_probe(efx);
1089	rtnl_unlock();
1090	if (rc && rc != -EPERM)
1091		netif_warn(efx, probe, efx->net_dev,
1092			   "failed to create MTDs (%d)\n", rc);
1093
1094	if (efx->type->udp_tnl_push_ports)
1095		efx->type->udp_tnl_push_ports(efx);
1096
1097	return 0;
1098
1099 fail3:
1100	efx_siena_fini_io(efx);
1101 fail2:
1102	efx_siena_fini_struct(efx);
1103 fail1:
1104	WARN_ON(rc > 0);
1105	netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
1106	free_netdev(net_dev);
1107	return rc;
1108}
1109
1110/* efx_pci_sriov_configure returns the actual number of Virtual Functions
1111 * enabled on success
1112 */
1113#ifdef CONFIG_SFC_SIENA_SRIOV
1114static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1115{
1116	int rc;
1117	struct efx_nic *efx = pci_get_drvdata(dev);
1118
1119	if (efx->type->sriov_configure) {
1120		rc = efx->type->sriov_configure(efx, num_vfs);
1121		if (rc)
1122			return rc;
1123		else
1124			return num_vfs;
1125	} else
1126		return -EOPNOTSUPP;
1127}
1128#endif
1129
1130static int efx_pm_freeze(struct device *dev)
1131{
1132	struct efx_nic *efx = dev_get_drvdata(dev);
1133
1134	rtnl_lock();
1135
1136	if (efx->state != STATE_DISABLED) {
1137		efx->state = STATE_UNINIT;
1138
1139		efx_device_detach_sync(efx);
1140
1141		efx_siena_stop_all(efx);
1142		efx_siena_disable_interrupts(efx);
1143	}
1144
1145	rtnl_unlock();
1146
1147	return 0;
1148}
1149
1150static void efx_pci_shutdown(struct pci_dev *pci_dev)
1151{
1152	struct efx_nic *efx = pci_get_drvdata(pci_dev);
1153
1154	if (!efx)
1155		return;
1156
1157	efx_pm_freeze(&pci_dev->dev);
1158	pci_disable_device(pci_dev);
1159}
1160
1161static int efx_pm_thaw(struct device *dev)
1162{
1163	int rc;
1164	struct efx_nic *efx = dev_get_drvdata(dev);
1165
1166	rtnl_lock();
1167
1168	if (efx->state != STATE_DISABLED) {
1169		rc = efx_siena_enable_interrupts(efx);
1170		if (rc)
1171			goto fail;
1172
1173		mutex_lock(&efx->mac_lock);
1174		efx_siena_mcdi_port_reconfigure(efx);
1175		mutex_unlock(&efx->mac_lock);
1176
1177		efx_siena_start_all(efx);
1178
1179		efx_device_attach_if_not_resetting(efx);
1180
1181		efx->state = STATE_READY;
1182
1183		efx->type->resume_wol(efx);
1184	}
1185
1186	rtnl_unlock();
1187
1188	/* Reschedule any quenched resets scheduled during efx_pm_freeze() */
1189	efx_siena_queue_reset_work(efx);
1190
1191	return 0;
1192
1193fail:
1194	rtnl_unlock();
1195
1196	return rc;
1197}
1198
1199static int efx_pm_poweroff(struct device *dev)
1200{
1201	struct pci_dev *pci_dev = to_pci_dev(dev);
1202	struct efx_nic *efx = pci_get_drvdata(pci_dev);
1203
1204	efx->type->fini(efx);
1205
1206	efx->reset_pending = 0;
1207
1208	pci_save_state(pci_dev);
1209	return pci_set_power_state(pci_dev, PCI_D3hot);
1210}
1211
1212/* Used for both resume and restore */
1213static int efx_pm_resume(struct device *dev)
1214{
1215	struct pci_dev *pci_dev = to_pci_dev(dev);
1216	struct efx_nic *efx = pci_get_drvdata(pci_dev);
1217	int rc;
1218
1219	rc = pci_set_power_state(pci_dev, PCI_D0);
1220	if (rc)
1221		return rc;
1222	pci_restore_state(pci_dev);
1223	rc = pci_enable_device(pci_dev);
1224	if (rc)
1225		return rc;
1226	pci_set_master(efx->pci_dev);
1227	rc = efx->type->reset(efx, RESET_TYPE_ALL);
1228	if (rc)
1229		return rc;
1230	down_write(&efx->filter_sem);
1231	rc = efx->type->init(efx);
1232	up_write(&efx->filter_sem);
1233	if (rc)
1234		return rc;
1235	rc = efx_pm_thaw(dev);
1236	return rc;
1237}
1238
1239static int efx_pm_suspend(struct device *dev)
1240{
1241	int rc;
1242
1243	efx_pm_freeze(dev);
1244	rc = efx_pm_poweroff(dev);
1245	if (rc)
1246		efx_pm_resume(dev);
1247	return rc;
1248}
1249
1250static const struct dev_pm_ops efx_pm_ops = {
1251	.suspend	= efx_pm_suspend,
1252	.resume		= efx_pm_resume,
1253	.freeze		= efx_pm_freeze,
1254	.thaw		= efx_pm_thaw,
1255	.poweroff	= efx_pm_poweroff,
1256	.restore	= efx_pm_resume,
1257};
1258
1259static struct pci_driver efx_pci_driver = {
1260	.name		= KBUILD_MODNAME,
1261	.id_table	= efx_pci_table,
1262	.probe		= efx_pci_probe,
1263	.remove		= efx_pci_remove,
1264	.driver.pm	= &efx_pm_ops,
1265	.shutdown	= efx_pci_shutdown,
1266	.err_handler	= &efx_siena_err_handlers,
1267#ifdef CONFIG_SFC_SIENA_SRIOV
1268	.sriov_configure = efx_pci_sriov_configure,
1269#endif
1270};
1271
1272/**************************************************************************
1273 *
1274 * Kernel module interface
1275 *
1276 *************************************************************************/
1277
1278static int __init efx_init_module(void)
1279{
1280	int rc;
1281
1282	pr_info("Solarflare Siena driver\n");
1283
1284	rc = register_netdevice_notifier(&efx_netdev_notifier);
1285	if (rc)
1286		goto err_notifier;
1287
1288#ifdef CONFIG_SFC_SIENA_SRIOV
1289	rc = efx_init_sriov();
1290	if (rc)
1291		goto err_sriov;
1292#endif
1293
1294	rc = efx_siena_create_reset_workqueue();
1295	if (rc)
1296		goto err_reset;
1297
1298	rc = pci_register_driver(&efx_pci_driver);
1299	if (rc < 0)
1300		goto err_pci;
1301
1302	return 0;
1303
1304 err_pci:
1305	efx_siena_destroy_reset_workqueue();
1306 err_reset:
1307#ifdef CONFIG_SFC_SIENA_SRIOV
1308	efx_fini_sriov();
1309 err_sriov:
1310#endif
1311	unregister_netdevice_notifier(&efx_netdev_notifier);
1312 err_notifier:
1313	return rc;
1314}
1315
1316static void __exit efx_exit_module(void)
1317{
1318	pr_info("Solarflare Siena driver unloading\n");
1319
1320	pci_unregister_driver(&efx_pci_driver);
1321	efx_siena_destroy_reset_workqueue();
1322#ifdef CONFIG_SFC_SIENA_SRIOV
1323	efx_fini_sriov();
1324#endif
1325	unregister_netdevice_notifier(&efx_netdev_notifier);
1326
1327}
1328
1329module_init(efx_init_module);
1330module_exit(efx_exit_module);
1331
1332MODULE_AUTHOR("Solarflare Communications and "
1333	      "Michael Brown <mbrown@fensystems.co.uk>");
1334MODULE_DESCRIPTION("Solarflare Siena network driver");
1335MODULE_LICENSE("GPL");
1336MODULE_DEVICE_TABLE(pci, efx_pci_table);
1337