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