efx.c 41.2 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2
/****************************************************************************
B
Ben Hutchings 已提交
3
 * Driver for Solarflare network controllers and boards
4
 * Copyright 2005-2006 Fen Systems Ltd.
B
Ben Hutchings 已提交
5
 * Copyright 2005-2013 Solarflare Communications Inc.
6 7 8 9 10 11 12 13 14 15 16 17
 */

#include <linux/module.h>
#include <linux/pci.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/delay.h>
#include <linux/notifier.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/in.h>
#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 24
#include <net/gre.h>
#include <net/udp_tunnel.h>
25
#include "efx.h"
26 27 28 29
#include "efx_common.h"
#include "efx_channels.h"
#include "rx_common.h"
#include "tx_common.h"
B
Ben Hutchings 已提交
30
#include "nic.h"
31
#include "io.h"
32
#include "selftest.h"
33
#include "sriov.h"
34

35
#include "mcdi.h"
36
#include "mcdi_pcol.h"
37
#include "workarounds.h"
38

39 40 41 42 43 44 45
/**************************************************************************
 *
 * Type name strings
 *
 **************************************************************************
 */

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/* UDP tunnel type names */
static const char *const efx_udp_tunnel_type_names[] = {
	[TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN] = "vxlan",
	[TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE] = "geneve",
};

void efx_get_udp_tunnel_type_name(u16 type, char *buf, size_t buflen)
{
	if (type < ARRAY_SIZE(efx_udp_tunnel_type_names) &&
	    efx_udp_tunnel_type_names[type] != NULL)
		snprintf(buf, buflen, "%s", efx_udp_tunnel_type_names[type]);
	else
		snprintf(buf, buflen, "type %d", type);
}

61 62 63 64 65 66 67 68 69
/**************************************************************************
 *
 * Configurable values
 *
 *************************************************************************/

/*
 * Use separate channels for TX and RX events
 *
70 71
 * Set this to 1 to use separate channels for TX and RX. It allows us
 * to control interrupt affinity separately for TX and RX.
72
 *
73
 * This is only used in MSI-X interrupt mode
74
 */
75 76 77
bool efx_separate_tx_channels;
module_param(efx_separate_tx_channels, bool, 0444);
MODULE_PARM_DESC(efx_separate_tx_channels,
78
		 "Use separate channels for TX and RX");
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

/* Initial interrupt moderation settings.  They can be modified after
 * module load with ethtool.
 *
 * The default for RX should strike a balance between increasing the
 * round-trip latency and reducing overhead.
 */
static unsigned int rx_irq_mod_usec = 60;

/* Initial interrupt moderation settings.  They can be modified after
 * module load with ethtool.
 *
 * This default is chosen to ensure that a 10G link does not go idle
 * while a TX queue is stopped after it has become full.  A queue is
 * restarted when it drops below half full.  The time this takes (assuming
 * worst case 3 descriptors per packet and 1024 descriptors) is
 *   512 / 3 * 1.2 = 205 usec.
 */
static unsigned int tx_irq_mod_usec = 150;

99 100
static bool phy_flash_cfg;
module_param(phy_flash_cfg, bool, 0644);
101 102
MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");

103 104 105 106 107 108 109
static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
			 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
			 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
			 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
module_param(debug, uint, 0);
MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");

110 111 112 113 114
/**************************************************************************
 *
 * Utility functions and prototypes
 *
 *************************************************************************/
115

116
static void efx_remove_port(struct efx_nic *efx);
117 118
static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog);
static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp);
119 120
static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
			u32 flags);
121 122 123

#define EFX_ASSERT_RESET_SERIALISED(efx)		\
	do {						\
124
		if ((efx->state == STATE_READY) ||	\
125
		    (efx->state == STATE_RECOVERY) ||	\
126
		    (efx->state == STATE_DISABLED))	\
127 128 129 130 131 132 133 134 135
			ASSERT_RTNL();			\
	} while (0)

/**************************************************************************
 *
 * Port handling
 *
 **************************************************************************/

136 137
static void efx_fini_port(struct efx_nic *efx);

138 139 140 141
static int efx_probe_port(struct efx_nic *efx)
{
	int rc;

142
	netif_dbg(efx, probe, efx->net_dev, "create port\n");
143

144 145 146
	if (phy_flash_cfg)
		efx->phy_mode = PHY_MODE_SPECIAL;

147 148
	/* Connect up MAC/PHY operations table */
	rc = efx->type->probe_port(efx);
149
	if (rc)
150
		return rc;
151

152
	/* Initialise MAC address to permanent address */
153
	ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
154 155 156 157 158 159 160 161

	return 0;
}

static int efx_init_port(struct efx_nic *efx)
{
	int rc;

162
	netif_dbg(efx, drv, efx->net_dev, "init port\n");
163

164 165
	mutex_lock(&efx->mac_lock);

166
	rc = efx->phy_op->init(efx);
167
	if (rc)
168
		goto fail1;
169

170
	efx->port_initialized = true;
171

B
Ben Hutchings 已提交
172 173
	/* Reconfigure the MAC before creating dma queues (required for
	 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
174
	efx_mac_reconfigure(efx);
B
Ben Hutchings 已提交
175 176 177

	/* Ensure the PHY advertises the correct flow control settings */
	rc = efx->phy_op->reconfigure(efx);
178
	if (rc && rc != -EPERM)
B
Ben Hutchings 已提交
179 180
		goto fail2;

181
	mutex_unlock(&efx->mac_lock);
182
	return 0;
183

184
fail2:
185
	efx->phy_op->fini(efx);
186 187
fail1:
	mutex_unlock(&efx->mac_lock);
188
	return rc;
189 190 191 192
}

static void efx_fini_port(struct efx_nic *efx)
{
193
	netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
194 195 196 197

	if (!efx->port_initialized)
		return;

198
	efx->phy_op->fini(efx);
199
	efx->port_initialized = false;
200

201
	efx->link_state.up = false;
202 203 204 205 206
	efx_link_status_changed(efx);
}

static void efx_remove_port(struct efx_nic *efx)
{
207
	netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
208

209
	efx->type->remove_port(efx);
210 211 212 213 214 215 216 217
}

/**************************************************************************
 *
 * NIC handling
 *
 **************************************************************************/

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
static LIST_HEAD(efx_primary_list);
static LIST_HEAD(efx_unassociated_list);

static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
{
	return left->type == right->type &&
		left->vpd_sn && right->vpd_sn &&
		!strcmp(left->vpd_sn, right->vpd_sn);
}

static void efx_associate(struct efx_nic *efx)
{
	struct efx_nic *other, *next;

	if (efx->primary == efx) {
		/* Adding primary function; look for secondaries */

		netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
		list_add_tail(&efx->node, &efx_primary_list);

		list_for_each_entry_safe(other, next, &efx_unassociated_list,
					 node) {
			if (efx_same_controller(efx, other)) {
				list_del(&other->node);
				netif_dbg(other, probe, other->net_dev,
					  "moving to secondary list of %s %s\n",
					  pci_name(efx->pci_dev),
					  efx->net_dev->name);
				list_add_tail(&other->node,
					      &efx->secondary_list);
				other->primary = efx;
			}
		}
	} else {
		/* Adding secondary function; look for primary */

		list_for_each_entry(other, &efx_primary_list, node) {
			if (efx_same_controller(efx, other)) {
				netif_dbg(efx, probe, efx->net_dev,
					  "adding to secondary list of %s %s\n",
					  pci_name(other->pci_dev),
					  other->net_dev->name);
				list_add_tail(&efx->node,
					      &other->secondary_list);
				efx->primary = other;
				return;
			}
		}

		netif_dbg(efx, probe, efx->net_dev,
			  "adding to unassociated list\n");
		list_add_tail(&efx->node, &efx_unassociated_list);
	}
}

static void efx_dissociate(struct efx_nic *efx)
{
	struct efx_nic *other, *next;

	list_del(&efx->node);
	efx->primary = NULL;

	list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
		list_del(&other->node);
		netif_dbg(other, probe, other->net_dev,
			  "moving to unassociated list\n");
		list_add_tail(&other->node, &efx_unassociated_list);
		other->primary = NULL;
	}
}

289 290 291 292
static int efx_probe_nic(struct efx_nic *efx)
{
	int rc;

293
	netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
294 295

	/* Carry out hardware-type specific initialisation */
296
	rc = efx->type->probe(efx);
297 298 299
	if (rc)
		return rc;

300 301 302 303 304 305 306 307
	do {
		if (!efx->max_channels || !efx->max_tx_channels) {
			netif_err(efx, drv, efx->net_dev,
				  "Insufficient resources to allocate"
				  " any channels\n");
			rc = -ENOSPC;
			goto fail1;
		}
308

309 310 311 312 313 314
		/* Determine the number of channels and queues by trying
		 * to hook in MSI-X interrupts.
		 */
		rc = efx_probe_interrupts(efx);
		if (rc)
			goto fail1;
315

316 317 318
		rc = efx_set_channels(efx);
		if (rc)
			goto fail1;
319 320 321 322 323 324 325 326 327 328 329

		/* dimension_resources can fail with EAGAIN */
		rc = efx->type->dimension_resources(efx);
		if (rc != 0 && rc != -EAGAIN)
			goto fail2;

		if (rc == -EAGAIN)
			/* try again with new max_channels */
			efx_remove_interrupts(efx);

	} while (rc == -EAGAIN);
330

331
	if (efx->n_channels > 1)
332 333 334
		netdev_rss_key_fill(efx->rss_context.rx_hash_key,
				    sizeof(efx->rss_context.rx_hash_key));
	efx_set_default_rx_indir_table(efx, &efx->rss_context);
335

336 337
	netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
	netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
338 339

	/* Initialise the interrupt moderation settings */
340
	efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
341 342
	efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
				true);
343 344

	return 0;
345

346 347 348
fail2:
	efx_remove_interrupts(efx);
fail1:
349 350
	efx->type->remove(efx);
	return rc;
351 352 353 354
}

static void efx_remove_nic(struct efx_nic *efx)
{
355
	netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
356 357

	efx_remove_interrupts(efx);
358
	efx->type->remove(efx);
359 360 361 362 363 364 365 366 367 368 369 370 371 372
}

/**************************************************************************
 *
 * NIC startup/shutdown
 *
 *************************************************************************/

static int efx_probe_all(struct efx_nic *efx)
{
	int rc;

	rc = efx_probe_nic(efx);
	if (rc) {
373
		netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
374 375 376 377 378
		goto fail1;
	}

	rc = efx_probe_port(efx);
	if (rc) {
379
		netif_err(efx, probe, efx->net_dev, "failed to create port\n");
380 381 382
		goto fail2;
	}

383 384 385 386 387
	BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
	if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
		rc = -EINVAL;
		goto fail3;
	}
388
	efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
389

390 391 392 393 394 395 396 397
#ifdef CONFIG_SFC_SRIOV
	rc = efx->type->vswitching_probe(efx);
	if (rc) /* not fatal; the PF will still work fine */
		netif_warn(efx, probe, efx->net_dev,
			   "failed to setup vswitching rc=%d;"
			   " VFs may not function\n", rc);
#endif

B
Ben Hutchings 已提交
398 399 400 401
	rc = efx_probe_filters(efx);
	if (rc) {
		netif_err(efx, probe, efx->net_dev,
			  "failed to create filter tables\n");
402
		goto fail4;
B
Ben Hutchings 已提交
403 404
	}

405 406
	rc = efx_probe_channels(efx);
	if (rc)
407
		goto fail5;
408

409 410
	return 0;

411
 fail5:
412
	efx_remove_filters(efx);
413 414 415 416
 fail4:
#ifdef CONFIG_SFC_SRIOV
	efx->type->vswitching_remove(efx);
#endif
417 418 419 420 421 422 423 424 425 426
 fail3:
	efx_remove_port(efx);
 fail2:
	efx_remove_nic(efx);
 fail1:
	return rc;
}

static void efx_remove_all(struct efx_nic *efx)
{
427 428 429 430
	rtnl_lock();
	efx_xdp_setup_prog(efx, NULL);
	rtnl_unlock();

431
	efx_remove_channels(efx);
432
	efx_remove_filters(efx);
433 434 435
#ifdef CONFIG_SFC_SRIOV
	efx->type->vswitching_remove(efx);
#endif
436 437 438 439 440 441 442 443 444
	efx_remove_port(efx);
	efx_remove_nic(efx);
}

/**************************************************************************
 *
 * Interrupt moderation
 *
 **************************************************************************/
445
unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
446
{
447 448
	if (usecs == 0)
		return 0;
449
	if (usecs * 1000 < efx->timer_quantum_ns)
450
		return 1; /* never round down to 0 */
451 452 453 454 455 456 457 458 459
	return usecs * 1000 / efx->timer_quantum_ns;
}

unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
{
	/* We must round up when converting ticks to microseconds
	 * because we round down when converting the other way.
	 */
	return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
460 461
}

462
/* Set interrupt moderation parameters */
463 464 465
int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
			    unsigned int rx_usecs, bool rx_adaptive,
			    bool rx_may_override_tx)
466
{
467
	struct efx_channel *channel;
468 469
	unsigned int timer_max_us;

470 471
	EFX_ASSERT_RESET_SERIALISED(efx);

472 473 474
	timer_max_us = efx->timer_max_ns / 1000;

	if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
475 476
		return -EINVAL;

477
	if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
478 479 480 481 482 483
	    !rx_may_override_tx) {
		netif_err(efx, drv, efx->net_dev, "Channels are shared. "
			  "RX and TX IRQ moderation must be equal\n");
		return -EINVAL;
	}

484
	efx->irq_rx_adaptive = rx_adaptive;
485
	efx->irq_rx_moderation_us = rx_usecs;
486
	efx_for_each_channel(channel, efx) {
487
		if (efx_channel_has_rx_queue(channel))
488
			channel->irq_moderation_us = rx_usecs;
489
		else if (efx_channel_has_tx_queues(channel))
490
			channel->irq_moderation_us = tx_usecs;
491 492
		else if (efx_channel_is_xdp_tx(channel))
			channel->irq_moderation_us = tx_usecs;
493
	}
494 495

	return 0;
496 497
}

498 499 500 501
void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
			    unsigned int *rx_usecs, bool *rx_adaptive)
{
	*rx_adaptive = efx->irq_rx_adaptive;
502
	*rx_usecs = efx->irq_rx_moderation_us;
503 504 505 506 507

	/* If channels are shared between RX and TX, so is IRQ
	 * moderation.  Otherwise, IRQ moderation is the same for all
	 * TX channels and is not adaptive.
	 */
508
	if (efx->tx_channel_offset == 0) {
509
		*tx_usecs = *rx_usecs;
510 511 512 513 514 515
	} else {
		struct efx_channel *tx_channel;

		tx_channel = efx->channel[efx->tx_channel_offset];
		*tx_usecs = tx_channel->irq_moderation_us;
	}
516 517
}

518 519 520 521 522 523 524 525 526 527 528
/**************************************************************************
 *
 * ioctls
 *
 *************************************************************************/

/* Net device ioctl
 * Context: process, rtnl_lock() held.
 */
static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
{
529
	struct efx_nic *efx = netdev_priv(net_dev);
530
	struct mii_ioctl_data *data = if_mii(ifr);
531

532
	if (cmd == SIOCSHWTSTAMP)
533 534 535
		return efx_ptp_set_ts_config(efx, ifr);
	if (cmd == SIOCGHWTSTAMP)
		return efx_ptp_get_ts_config(efx, ifr);
536

537 538 539 540 541 542
	/* Convert phy_id from older PRTAD/DEVAD format */
	if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
	    (data->phy_id & 0xfc00) == 0x0400)
		data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;

	return mdio_mii_ioctl(&efx->mdio, data, cmd);
543 544 545 546 547 548 549 550 551
}

/**************************************************************************
 *
 * Kernel net device interface
 *
 *************************************************************************/

/* Context: process, rtnl_lock() held. */
552
int efx_net_open(struct net_device *net_dev)
553
{
554
	struct efx_nic *efx = netdev_priv(net_dev);
555 556
	int rc;

557 558
	netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
		  raw_smp_processor_id());
559

560 561 562
	rc = efx_check_disabled(efx);
	if (rc)
		return rc;
563 564
	if (efx->phy_mode & PHY_MODE_SPECIAL)
		return -EBUSY;
565 566
	if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
		return -EIO;
567

568 569 570 571
	/* Notify the kernel of the link state polled during driver load,
	 * before the monitor starts running */
	efx_link_status_changed(efx);

572
	efx_start_all(efx);
573 574
	if (efx->state == STATE_DISABLED || efx->reset_pending)
		netif_device_detach(efx->net_dev);
575
	efx_selftest_async_start(efx);
576 577 578 579 580 581 582
	return 0;
}

/* Context: process, rtnl_lock() held.
 * Note that the kernel will ignore our return code; this method
 * should really be a void.
 */
583
int efx_net_stop(struct net_device *net_dev)
584
{
585
	struct efx_nic *efx = netdev_priv(net_dev);
586

587 588
	netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
		  raw_smp_processor_id());
589

590 591
	/* Stop the device and flush all the channels */
	efx_stop_all(efx);
592 593 594 595 596

	return 0;
}

/* Context: netif_tx_lock held, BHs disabled. */
597
static void efx_watchdog(struct net_device *net_dev, unsigned int txqueue)
598
{
599
	struct efx_nic *efx = netdev_priv(net_dev);
600

601 602 603
	netif_err(efx, tx_err, efx->net_dev,
		  "TX stuck with port_enabled=%d: resetting channels\n",
		  efx->port_enabled);
604

605
	efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
606 607 608 609
}

static int efx_set_mac_address(struct net_device *net_dev, void *data)
{
610
	struct efx_nic *efx = netdev_priv(net_dev);
611
	struct sockaddr *addr = data;
612
	u8 *new_addr = addr->sa_data;
613 614
	u8 old_addr[6];
	int rc;
615 616

	if (!is_valid_ether_addr(new_addr)) {
617 618 619
		netif_err(efx, drv, efx->net_dev,
			  "invalid ethernet MAC address requested: %pM\n",
			  new_addr);
620
		return -EADDRNOTAVAIL;
621 622
	}

623 624
	/* save old address */
	ether_addr_copy(old_addr, net_dev->dev_addr);
625
	ether_addr_copy(net_dev->dev_addr, new_addr);
626 627
	if (efx->type->set_mac_address) {
		rc = efx->type->set_mac_address(efx);
628 629 630 631 632
		if (rc) {
			ether_addr_copy(net_dev->dev_addr, old_addr);
			return rc;
		}
	}
633 634

	/* Reconfigure the MAC */
B
Ben Hutchings 已提交
635
	mutex_lock(&efx->mac_lock);
636
	efx_mac_reconfigure(efx);
B
Ben Hutchings 已提交
637
	mutex_unlock(&efx->mac_lock);
638 639 640 641

	return 0;
}

642
/* Context: netif_addr_lock held, BHs disabled. */
643
static void efx_set_rx_mode(struct net_device *net_dev)
644
{
645
	struct efx_nic *efx = netdev_priv(net_dev);
646

647 648 649
	if (efx->port_enabled)
		queue_work(efx->workqueue, &efx->mac_work);
	/* Otherwise efx_start_port() will do this */
650 651
}

652
static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
653 654
{
	struct efx_nic *efx = netdev_priv(net_dev);
655
	int rc;
656 657

	/* If disabling RX n-tuple filtering, clear existing filters */
658 659 660 661 662 663
	if (net_dev->features & ~data & NETIF_F_NTUPLE) {
		rc = efx->type->filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
		if (rc)
			return rc;
	}

E
Edward Cree 已提交
664 665 666 667 668
	/* If Rx VLAN filter is changed, update filters via mac_reconfigure.
	 * If rx-fcs is changed, mac_reconfigure updates that too.
	 */
	if ((net_dev->features ^ data) & (NETIF_F_HW_VLAN_CTAG_FILTER |
					  NETIF_F_RXFCS)) {
669 670 671 672 673
		/* efx_set_rx_mode() will schedule MAC work to update filters
		 * when a new features are finally set in net_dev.
		 */
		efx_set_rx_mode(net_dev);
	}
674 675 676 677

	return 0;
}

678 679
static int efx_get_phys_port_id(struct net_device *net_dev,
				struct netdev_phys_item_id *ppid)
680 681 682 683 684 685 686 687 688
{
	struct efx_nic *efx = netdev_priv(net_dev);

	if (efx->type->get_phys_port_id)
		return efx->type->get_phys_port_id(efx, ppid);
	else
		return -EOPNOTSUPP;
}

689 690 691 692 693 694 695 696 697 698
static int efx_get_phys_port_name(struct net_device *net_dev,
				  char *name, size_t len)
{
	struct efx_nic *efx = netdev_priv(net_dev);

	if (snprintf(name, len, "p%u", efx->port_num) >= len)
		return -EINVAL;
	return 0;
}

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
{
	struct efx_nic *efx = netdev_priv(net_dev);

	if (efx->type->vlan_rx_add_vid)
		return efx->type->vlan_rx_add_vid(efx, proto, vid);
	else
		return -EOPNOTSUPP;
}

static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
{
	struct efx_nic *efx = netdev_priv(net_dev);

	if (efx->type->vlan_rx_kill_vid)
		return efx->type->vlan_rx_kill_vid(efx, proto, vid);
	else
		return -EOPNOTSUPP;
}

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
static int efx_udp_tunnel_type_map(enum udp_parsable_tunnel_type in)
{
	switch (in) {
	case UDP_TUNNEL_TYPE_VXLAN:
		return TUNNEL_ENCAP_UDP_PORT_ENTRY_VXLAN;
	case UDP_TUNNEL_TYPE_GENEVE:
		return TUNNEL_ENCAP_UDP_PORT_ENTRY_GENEVE;
	default:
		return -1;
	}
}

static void efx_udp_tunnel_add(struct net_device *dev, struct udp_tunnel_info *ti)
{
	struct efx_nic *efx = netdev_priv(dev);
	struct efx_udp_tunnel tnl;
	int efx_tunnel_type;

	efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
	if (efx_tunnel_type < 0)
		return;

	tnl.type = (u16)efx_tunnel_type;
	tnl.port = ti->port;

	if (efx->type->udp_tnl_add_port)
		(void)efx->type->udp_tnl_add_port(efx, tnl);
}

static void efx_udp_tunnel_del(struct net_device *dev, struct udp_tunnel_info *ti)
{
	struct efx_nic *efx = netdev_priv(dev);
	struct efx_udp_tunnel tnl;
	int efx_tunnel_type;

	efx_tunnel_type = efx_udp_tunnel_type_map(ti->type);
	if (efx_tunnel_type < 0)
		return;

	tnl.type = (u16)efx_tunnel_type;
	tnl.port = ti->port;

761
	if (efx->type->udp_tnl_del_port)
762 763 764
		(void)efx->type->udp_tnl_del_port(efx, tnl);
}

765
static const struct net_device_ops efx_netdev_ops = {
S
Stephen Hemminger 已提交
766 767
	.ndo_open		= efx_net_open,
	.ndo_stop		= efx_net_stop,
768
	.ndo_get_stats64	= efx_net_stats,
S
Stephen Hemminger 已提交
769 770 771 772 773 774
	.ndo_tx_timeout		= efx_watchdog,
	.ndo_start_xmit		= efx_hard_start_xmit,
	.ndo_validate_addr	= eth_validate_addr,
	.ndo_do_ioctl		= efx_ioctl,
	.ndo_change_mtu		= efx_change_mtu,
	.ndo_set_mac_address	= efx_set_mac_address,
775
	.ndo_set_rx_mode	= efx_set_rx_mode,
776
	.ndo_set_features	= efx_set_features,
777 778
	.ndo_vlan_rx_add_vid	= efx_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid	= efx_vlan_rx_kill_vid,
779
#ifdef CONFIG_SFC_SRIOV
780 781 782 783
	.ndo_set_vf_mac		= efx_sriov_set_vf_mac,
	.ndo_set_vf_vlan	= efx_sriov_set_vf_vlan,
	.ndo_set_vf_spoofchk	= efx_sriov_set_vf_spoofchk,
	.ndo_get_vf_config	= efx_sriov_get_vf_config,
784
	.ndo_set_vf_link_state  = efx_sriov_set_vf_link_state,
785
#endif
786
	.ndo_get_phys_port_id   = efx_get_phys_port_id,
787
	.ndo_get_phys_port_name	= efx_get_phys_port_name,
788
	.ndo_setup_tc		= efx_setup_tc,
789 790 791
#ifdef CONFIG_RFS_ACCEL
	.ndo_rx_flow_steer	= efx_filter_rfs,
#endif
792 793
	.ndo_udp_tunnel_add	= efx_udp_tunnel_add,
	.ndo_udp_tunnel_del	= efx_udp_tunnel_del,
794
	.ndo_xdp_xmit		= efx_xdp_xmit,
795
	.ndo_bpf		= efx_xdp
S
Stephen Hemminger 已提交
796 797
};

798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
{
	struct bpf_prog *old_prog;

	if (efx->xdp_rxq_info_failed) {
		netif_err(efx, drv, efx->net_dev,
			  "Unable to bind XDP program due to previous failure of rxq_info\n");
		return -EINVAL;
	}

	if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) {
		netif_err(efx, drv, efx->net_dev,
			  "Unable to configure XDP with MTU of %d (max: %d)\n",
			  efx->net_dev->mtu, efx_xdp_max_mtu(efx));
		return -EINVAL;
	}

	old_prog = rtnl_dereference(efx->xdp_prog);
	rcu_assign_pointer(efx->xdp_prog, prog);
	/* Release the reference that was originally passed by the caller. */
	if (old_prog)
		bpf_prog_put(old_prog);

	return 0;
}

/* Context: process, rtnl_lock() held. */
static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
{
	struct efx_nic *efx = netdev_priv(dev);
	struct bpf_prog *xdp_prog;

	switch (xdp->command) {
	case XDP_SETUP_PROG:
		return efx_xdp_setup_prog(efx, xdp->prog);
	case XDP_QUERY_PROG:
		xdp_prog = rtnl_dereference(efx->xdp_prog);
		xdp->prog_id = xdp_prog ? xdp_prog->aux->id : 0;
		return 0;
	default:
		return -EINVAL;
	}
}

842 843 844 845 846 847 848 849 850 851 852
static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
			u32 flags)
{
	struct efx_nic *efx = netdev_priv(dev);

	if (!netif_running(dev))
		return -EINVAL;

	return efx_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH);
}

853 854 855 856 857 858 859
static void efx_update_name(struct efx_nic *efx)
{
	strcpy(efx->name, efx->net_dev->name);
	efx_mtd_rename(efx);
	efx_set_channel_names(efx);
}

860 861 862
static int efx_netdev_event(struct notifier_block *this,
			    unsigned long event, void *ptr)
{
863
	struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
864

865
	if ((net_dev->netdev_ops == &efx_netdev_ops) &&
866 867
	    event == NETDEV_CHANGENAME)
		efx_update_name(netdev_priv(net_dev));
868 869 870 871 872 873 874 875

	return NOTIFY_DONE;
}

static struct notifier_block efx_netdev_notifier = {
	.notifier_call = efx_netdev_event,
};

B
Ben Hutchings 已提交
876 877 878
static ssize_t
show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
{
879
	struct efx_nic *efx = dev_get_drvdata(dev);
B
Ben Hutchings 已提交
880 881
	return sprintf(buf, "%d\n", efx->phy_type);
}
882
static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
B
Ben Hutchings 已提交
883

884 885 886
static int efx_register_netdev(struct efx_nic *efx)
{
	struct net_device *net_dev = efx->net_dev;
887
	struct efx_channel *channel;
888 889 890 891
	int rc;

	net_dev->watchdog_timeo = 5 * HZ;
	net_dev->irq = efx->pci_dev->irq;
892 893
	net_dev->netdev_ops = &efx_netdev_ops;
	if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
894
		net_dev->priv_flags |= IFF_UNICAST_FLT;
895
	net_dev->ethtool_ops = &efx_ethtool_ops;
896
	net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
897 898
	net_dev->min_mtu = EFX_MIN_MTU;
	net_dev->max_mtu = EFX_MAX_MTU;
899

900
	rtnl_lock();
901

902 903 904 905 906 907 908 909 910 911 912 913 914
	/* Enable resets to be scheduled and check whether any were
	 * already requested.  If so, the NIC is probably hosed so we
	 * abort.
	 */
	efx->state = STATE_READY;
	smp_mb(); /* ensure we change state before checking reset_pending */
	if (efx->reset_pending) {
		netif_err(efx, probe, efx->net_dev,
			  "aborting probe due to scheduled reset\n");
		rc = -EIO;
		goto fail_locked;
	}

915 916 917
	rc = dev_alloc_name(net_dev, net_dev->name);
	if (rc < 0)
		goto fail_locked;
918
	efx_update_name(efx);
919

920 921 922
	/* Always start with carrier off; PHY events will detect the link */
	netif_carrier_off(net_dev);

923 924 925 926
	rc = register_netdevice(net_dev);
	if (rc)
		goto fail_locked;

927 928
	efx_for_each_channel(channel, efx) {
		struct efx_tx_queue *tx_queue;
929 930
		efx_for_each_channel_tx_queue(tx_queue, channel)
			efx_init_tx_queue_core_txq(tx_queue);
931 932
	}

933 934
	efx_associate(efx);

935
	rtnl_unlock();
936

B
Ben Hutchings 已提交
937 938
	rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
	if (rc) {
939 940
		netif_err(efx, drv, efx->net_dev,
			  "failed to init net dev attributes\n");
B
Ben Hutchings 已提交
941 942
		goto fail_registered;
	}
943 944

	efx_init_mcdi_logging(efx);
B
Ben Hutchings 已提交
945

946
	return 0;
B
Ben Hutchings 已提交
947

948 949
fail_registered:
	rtnl_lock();
950
	efx_dissociate(efx);
951
	unregister_netdevice(net_dev);
952
fail_locked:
953
	efx->state = STATE_UNINIT;
954
	rtnl_unlock();
955
	netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
956
	return rc;
957 958 959 960 961 962 963
}

static void efx_unregister_netdev(struct efx_nic *efx)
{
	if (!efx->net_dev)
		return;

964
	BUG_ON(netdev_priv(efx->net_dev) != efx);
965

966 967
	if (efx_dev_registered(efx)) {
		strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
968
		efx_fini_mcdi_logging(efx);
969 970 971
		device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
		unregister_netdev(efx->net_dev);
	}
972 973 974 975 976 977 978 979 980
}

/**************************************************************************
 *
 * List of NICs we support
 *
 **************************************************************************/

/* PCI device ID table */
981
static const struct pci_device_id efx_pci_table[] = {
982
	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803),	/* SFC9020 */
983
	 .driver_data = (unsigned long) &siena_a0_nic_type},
984
	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813),	/* SFL9021 */
985
	 .driver_data = (unsigned long) &siena_a0_nic_type},
986 987
	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903),  /* SFC9120 PF */
	 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
988 989
	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903),  /* SFC9120 VF */
	 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
990 991
	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923),  /* SFC9140 PF */
	 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
992 993 994 995 996 997
	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923),  /* SFC9140 VF */
	 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03),  /* SFC9220 PF */
	 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03),  /* SFC9220 VF */
	 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
998 999 1000 1001
	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03),  /* SFC9250 PF */
	 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
	{PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03),  /* SFC9250 VF */
	 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
1002 1003 1004 1005 1006 1007 1008 1009 1010
	{0}			/* end of list */
};

/**************************************************************************
 *
 * Data housekeeping
 *
 **************************************************************************/

1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
{
	u64 n_rx_nodesc_trunc = 0;
	struct efx_channel *channel;

	efx_for_each_channel(channel, efx)
		n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
	stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
	stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
}

1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
/**************************************************************************
 *
 * PCI interface
 *
 **************************************************************************/

/* Main body of final NIC shutdown code
 * This is called only at module unload (or hotplug removal).
 */
static void efx_pci_remove_main(struct efx_nic *efx)
{
1033 1034 1035 1036
	/* Flush reset_work. It can no longer be scheduled since we
	 * are not READY.
	 */
	BUG_ON(efx->state == STATE_READY);
1037
	efx_flush_reset_workqueue(efx);
1038

B
Ben Hutchings 已提交
1039
	efx_disable_interrupts(efx);
1040
	efx_clear_interrupt_affinity(efx);
1041
	efx_nic_fini_interrupt(efx);
1042
	efx_fini_port(efx);
1043
	efx->type->fini(efx);
1044 1045 1046 1047 1048
	efx_fini_napi(efx);
	efx_remove_all(efx);
}

/* Final NIC shutdown
1049 1050
 * This is called only at module unload (or hotplug removal).  A PF can call
 * this on its VFs to ensure they are unbound first.
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
 */
static void efx_pci_remove(struct pci_dev *pci_dev)
{
	struct efx_nic *efx;

	efx = pci_get_drvdata(pci_dev);
	if (!efx)
		return;

	/* Mark the NIC as fini, then stop the interface */
	rtnl_lock();
1062
	efx_dissociate(efx);
1063
	dev_close(efx->net_dev);
B
Ben Hutchings 已提交
1064
	efx_disable_interrupts(efx);
1065
	efx->state = STATE_UNINIT;
1066 1067
	rtnl_unlock();

1068 1069 1070
	if (efx->type->sriov_fini)
		efx->type->sriov_fini(efx);

1071 1072
	efx_unregister_netdev(efx);

1073 1074
	efx_mtd_remove(efx);

1075 1076
	efx_pci_remove_main(efx);

1077
	efx_fini_io(efx, efx->type->mem_bar(efx));
1078
	netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
1079 1080 1081

	efx_fini_struct(efx);
	free_netdev(efx->net_dev);
1082 1083

	pci_disable_pcie_error_reporting(pci_dev);
1084 1085
};

1086 1087 1088 1089 1090 1091
/* NIC VPD information
 * Called during probe to display the part number of the
 * installed NIC.  VPD is potentially very large but this should
 * always appear within the first 512 bytes.
 */
#define SFC_VPD_LEN 512
1092
static void efx_probe_vpd_strings(struct efx_nic *efx)
1093 1094 1095 1096
{
	struct pci_dev *dev = efx->pci_dev;
	char vpd_data[SFC_VPD_LEN];
	ssize_t vpd_size;
1097
	int ro_start, ro_size, i, j;
1098 1099 1100 1101 1102 1103 1104 1105 1106

	/* Get the vpd data from the device */
	vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
	if (vpd_size <= 0) {
		netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
		return;
	}

	/* Get the Read only section */
1107 1108
	ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
	if (ro_start < 0) {
1109 1110 1111 1112
		netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
		return;
	}

1113 1114 1115
	ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
	j = ro_size;
	i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
	if (i + j > vpd_size)
		j = vpd_size - i;

	/* Get the Part number */
	i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
	if (i < 0) {
		netif_err(efx, drv, efx->net_dev, "Part number not found\n");
		return;
	}

	j = pci_vpd_info_field_size(&vpd_data[i]);
	i += PCI_VPD_INFO_FLD_HDR_SIZE;
	if (i + j > vpd_size) {
		netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
		return;
	}

	netif_info(efx, drv, efx->net_dev,
		   "Part Number : %.*s\n", j, &vpd_data[i]);
1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155

	i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
	j = ro_size;
	i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
	if (i < 0) {
		netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
		return;
	}

	j = pci_vpd_info_field_size(&vpd_data[i]);
	i += PCI_VPD_INFO_FLD_HDR_SIZE;
	if (i + j > vpd_size) {
		netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
		return;
	}

	efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
	if (!efx->vpd_sn)
		return;

	snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
1156 1157 1158
}


1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
/* Main body of NIC initialisation
 * This is called at module load (or hotplug insertion, theoretically).
 */
static int efx_pci_probe_main(struct efx_nic *efx)
{
	int rc;

	/* Do start-of-day initialisation */
	rc = efx_probe_all(efx);
	if (rc)
		goto fail1;

1171
	efx_init_napi(efx);
1172

1173
	down_write(&efx->filter_sem);
1174
	rc = efx->type->init(efx);
1175
	up_write(&efx->filter_sem);
1176
	if (rc) {
1177 1178
		netif_err(efx, probe, efx->net_dev,
			  "failed to initialise NIC\n");
1179
		goto fail3;
1180 1181 1182 1183
	}

	rc = efx_init_port(efx);
	if (rc) {
1184 1185
		netif_err(efx, probe, efx->net_dev,
			  "failed to initialise port\n");
1186
		goto fail4;
1187 1188
	}

1189
	rc = efx_nic_init_interrupt(efx);
1190
	if (rc)
1191
		goto fail5;
1192 1193

	efx_set_interrupt_affinity(efx);
1194 1195 1196
	rc = efx_enable_interrupts(efx);
	if (rc)
		goto fail6;
1197 1198 1199

	return 0;

1200
 fail6:
1201
	efx_clear_interrupt_affinity(efx);
1202
	efx_nic_fini_interrupt(efx);
1203
 fail5:
1204 1205
	efx_fini_port(efx);
 fail4:
1206
	efx->type->fini(efx);
1207 1208 1209 1210 1211 1212 1213
 fail3:
	efx_fini_napi(efx);
	efx_remove_all(efx);
 fail1:
	return rc;
}

1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
static int efx_pci_probe_post_io(struct efx_nic *efx)
{
	struct net_device *net_dev = efx->net_dev;
	int rc = efx_pci_probe_main(efx);

	if (rc)
		return rc;

	if (efx->type->sriov_init) {
		rc = efx->type->sriov_init(efx);
		if (rc)
			netif_err(efx, probe, efx->net_dev,
				  "SR-IOV can't be enabled rc %d\n", rc);
	}

	/* Determine netdevice features */
	net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
E
Edward Cree 已提交
1231
			      NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
		net_dev->features |= NETIF_F_TSO6;
	/* Check whether device supports TSO */
	if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
		net_dev->features &= ~NETIF_F_ALL_TSO;
	/* Mask for features that also apply to VLAN devices */
	net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
				   NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
				   NETIF_F_RXCSUM);

E
Edward Cree 已提交
1242 1243 1244 1245
	net_dev->hw_features |= net_dev->features & ~efx->fixed_features;

	/* Disable receiving frames with bad FCS, by default. */
	net_dev->features &= ~NETIF_F_RXALL;
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261

	/* Disable VLAN filtering by default.  It may be enforced if
	 * the feature is fixed (i.e. VLAN filters are required to
	 * receive VLAN tagged packets due to vPort restrictions).
	 */
	net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
	net_dev->features |= efx->fixed_features;

	rc = efx_register_netdev(efx);
	if (!rc)
		return 0;

	efx_pci_remove_main(efx);
	return rc;
}

1262 1263 1264
/* NIC initialisation
 *
 * This is called at module load (or hotplug insertion,
1265
 * theoretically).  It sets up PCI mappings, resets the NIC,
1266 1267 1268 1269 1270
 * sets up and registers the network devices with the kernel and hooks
 * the interrupt service routine.  It does not prepare the device for
 * transmission; this is left to the first time one of the network
 * interfaces is brought up (i.e. efx_net_open).
 */
B
Bill Pemberton 已提交
1271
static int efx_pci_probe(struct pci_dev *pci_dev,
1272
			 const struct pci_device_id *entry)
1273 1274 1275
{
	struct net_device *net_dev;
	struct efx_nic *efx;
1276
	int rc;
1277 1278

	/* Allocate and initialise a struct net_device and struct efx_nic */
1279 1280
	net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
				     EFX_MAX_RX_QUEUES);
1281 1282
	if (!net_dev)
		return -ENOMEM;
1283 1284
	efx = netdev_priv(net_dev);
	efx->type = (const struct efx_nic_type *) entry->driver_data;
1285
	efx->fixed_features |= NETIF_F_HIGHDMA;
1286

1287
	pci_set_drvdata(pci_dev, efx);
1288
	SET_NETDEV_DEV(net_dev, &pci_dev->dev);
1289
	rc = efx_init_struct(efx, pci_dev, net_dev);
1290 1291 1292
	if (rc)
		goto fail1;

1293
	netif_info(efx, probe, efx->net_dev,
1294
		   "Solarflare NIC detected\n");
1295

1296 1297
	if (!efx->type->is_vf)
		efx_probe_vpd_strings(efx);
1298

1299
	/* Set up basic I/O (BAR mappings etc) */
1300 1301
	rc = efx_init_io(efx, efx->type->mem_bar(efx), efx->type->max_dma_mask,
			 efx->type->mem_map_size(efx));
1302 1303 1304
	if (rc)
		goto fail2;

1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
	rc = efx_pci_probe_post_io(efx);
	if (rc) {
		/* On failure, retry once immediately.
		 * If we aborted probe due to a scheduled reset, dismiss it.
		 */
		efx->reset_pending = 0;
		rc = efx_pci_probe_post_io(efx);
		if (rc) {
			/* On another failure, retry once more
			 * after a 50-305ms delay.
			 */
			unsigned char r;

			get_random_bytes(&r, 1);
			msleep((unsigned int)r + 50);
			efx->reset_pending = 0;
			rc = efx_pci_probe_post_io(efx);
		}
	}
1324 1325
	if (rc)
		goto fail3;
1326

1327
	netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
1328

1329
	/* Try to create MTDs, but allow this to fail */
1330
	rtnl_lock();
1331
	rc = efx_mtd_probe(efx);
1332
	rtnl_unlock();
1333
	if (rc && rc != -EPERM)
1334 1335 1336
		netif_warn(efx, probe, efx->net_dev,
			   "failed to create MTDs (%d)\n", rc);

1337
	(void)pci_enable_pcie_error_reporting(pci_dev);
1338

1339 1340 1341
	if (efx->type->udp_tnl_push_ports)
		efx->type->udp_tnl_push_ports(efx);

1342 1343 1344
	return 0;

 fail3:
1345
	efx_fini_io(efx, efx->type->mem_bar(efx));
1346 1347 1348
 fail2:
	efx_fini_struct(efx);
 fail1:
S
Steve Hodgson 已提交
1349
	WARN_ON(rc > 0);
1350
	netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
1351 1352 1353 1354
	free_netdev(net_dev);
	return rc;
}

1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
/* efx_pci_sriov_configure returns the actual number of Virtual Functions
 * enabled on success
 */
#ifdef CONFIG_SFC_SRIOV
static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
{
	int rc;
	struct efx_nic *efx = pci_get_drvdata(dev);

	if (efx->type->sriov_configure) {
		rc = efx->type->sriov_configure(efx, num_vfs);
		if (rc)
			return rc;
		else
			return num_vfs;
	} else
		return -EOPNOTSUPP;
}
#endif

1375 1376
static int efx_pm_freeze(struct device *dev)
{
1377
	struct efx_nic *efx = dev_get_drvdata(dev);
1378

1379 1380
	rtnl_lock();

1381 1382
	if (efx->state != STATE_DISABLED) {
		efx->state = STATE_UNINIT;
1383

1384
		efx_device_detach_sync(efx);
1385

1386
		efx_stop_all(efx);
B
Ben Hutchings 已提交
1387
		efx_disable_interrupts(efx);
1388
	}
1389

1390 1391
	rtnl_unlock();

1392 1393 1394 1395 1396
	return 0;
}

static int efx_pm_thaw(struct device *dev)
{
1397
	int rc;
1398
	struct efx_nic *efx = dev_get_drvdata(dev);
1399

1400 1401
	rtnl_lock();

1402
	if (efx->state != STATE_DISABLED) {
1403 1404 1405
		rc = efx_enable_interrupts(efx);
		if (rc)
			goto fail;
1406

1407 1408 1409
		mutex_lock(&efx->mac_lock);
		efx->phy_op->reconfigure(efx);
		mutex_unlock(&efx->mac_lock);
1410

1411
		efx_start_all(efx);
1412

1413
		efx_device_attach_if_not_resetting(efx);
1414

1415
		efx->state = STATE_READY;
1416

1417 1418
		efx->type->resume_wol(efx);
	}
1419

1420 1421
	rtnl_unlock();

1422
	/* Reschedule any quenched resets scheduled during efx_pm_freeze() */
1423
	efx_queue_reset_work(efx);
1424

1425
	return 0;
1426 1427 1428 1429 1430

fail:
	rtnl_unlock();

	return rc;
1431 1432 1433 1434 1435 1436 1437 1438 1439
}

static int efx_pm_poweroff(struct device *dev)
{
	struct pci_dev *pci_dev = to_pci_dev(dev);
	struct efx_nic *efx = pci_get_drvdata(pci_dev);

	efx->type->fini(efx);

1440
	efx->reset_pending = 0;
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463

	pci_save_state(pci_dev);
	return pci_set_power_state(pci_dev, PCI_D3hot);
}

/* Used for both resume and restore */
static int efx_pm_resume(struct device *dev)
{
	struct pci_dev *pci_dev = to_pci_dev(dev);
	struct efx_nic *efx = pci_get_drvdata(pci_dev);
	int rc;

	rc = pci_set_power_state(pci_dev, PCI_D0);
	if (rc)
		return rc;
	pci_restore_state(pci_dev);
	rc = pci_enable_device(pci_dev);
	if (rc)
		return rc;
	pci_set_master(efx->pci_dev);
	rc = efx->type->reset(efx, RESET_TYPE_ALL);
	if (rc)
		return rc;
1464
	down_write(&efx->filter_sem);
1465
	rc = efx->type->init(efx);
1466
	up_write(&efx->filter_sem);
1467 1468
	if (rc)
		return rc;
1469 1470
	rc = efx_pm_thaw(dev);
	return rc;
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
}

static int efx_pm_suspend(struct device *dev)
{
	int rc;

	efx_pm_freeze(dev);
	rc = efx_pm_poweroff(dev);
	if (rc)
		efx_pm_resume(dev);
	return rc;
}

1484
static const struct dev_pm_ops efx_pm_ops = {
1485 1486 1487 1488 1489 1490 1491 1492
	.suspend	= efx_pm_suspend,
	.resume		= efx_pm_resume,
	.freeze		= efx_pm_freeze,
	.thaw		= efx_pm_thaw,
	.poweroff	= efx_pm_poweroff,
	.restore	= efx_pm_resume,
};

1493 1494 1495 1496
/* A PCI error affecting this device was detected.
 * At this point MMIO and DMA may be disabled.
 * Stop the software path and request a slot reset.
 */
1497 1498
static pci_ers_result_t efx_io_error_detected(struct pci_dev *pdev,
					      enum pci_channel_state state)
1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
{
	pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;
	struct efx_nic *efx = pci_get_drvdata(pdev);

	if (state == pci_channel_io_perm_failure)
		return PCI_ERS_RESULT_DISCONNECT;

	rtnl_lock();

	if (efx->state != STATE_DISABLED) {
		efx->state = STATE_RECOVERY;
		efx->reset_pending = 0;

		efx_device_detach_sync(efx);

		efx_stop_all(efx);
B
Ben Hutchings 已提交
1515
		efx_disable_interrupts(efx);
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531

		status = PCI_ERS_RESULT_NEED_RESET;
	} else {
		/* If the interface is disabled we don't want to do anything
		 * with it.
		 */
		status = PCI_ERS_RESULT_RECOVERED;
	}

	rtnl_unlock();

	pci_disable_device(pdev);

	return status;
}

1532
/* Fake a successful reset, which will be performed later in efx_io_resume. */
1533
static pci_ers_result_t efx_io_slot_reset(struct pci_dev *pdev)
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
{
	struct efx_nic *efx = pci_get_drvdata(pdev);
	pci_ers_result_t status = PCI_ERS_RESULT_RECOVERED;

	if (pci_enable_device(pdev)) {
		netif_err(efx, hw, efx->net_dev,
			  "Cannot re-enable PCI device after reset.\n");
		status =  PCI_ERS_RESULT_DISCONNECT;
	}

	return status;
}

/* Perform the actual reset and resume I/O operations. */
static void efx_io_resume(struct pci_dev *pdev)
{
	struct efx_nic *efx = pci_get_drvdata(pdev);
	int rc;

	rtnl_lock();

	if (efx->state == STATE_DISABLED)
		goto out;

	rc = efx_reset(efx, RESET_TYPE_ALL);
	if (rc) {
		netif_err(efx, hw, efx->net_dev,
			  "efx_reset failed after PCI error (%d)\n", rc);
	} else {
		efx->state = STATE_READY;
		netif_dbg(efx, hw, efx->net_dev,
			  "Done resetting and resuming IO after PCI error.\n");
	}

out:
	rtnl_unlock();
}

/* For simplicity and reliability, we always require a slot reset and try to
 * reset the hardware when a pci error affecting the device is detected.
 * We leave both the link_reset and mmio_enabled callback unimplemented:
 * with our request for slot reset the mmio_enabled callback will never be
 * called, and the link_reset callback is not used by AER or EEH mechanisms.
 */
1578
static const struct pci_error_handlers efx_err_handlers = {
1579 1580 1581 1582 1583
	.error_detected = efx_io_error_detected,
	.slot_reset	= efx_io_slot_reset,
	.resume		= efx_io_resume,
};

1584
static struct pci_driver efx_pci_driver = {
1585
	.name		= KBUILD_MODNAME,
1586 1587 1588
	.id_table	= efx_pci_table,
	.probe		= efx_pci_probe,
	.remove		= efx_pci_remove,
1589
	.driver.pm	= &efx_pm_ops,
1590
	.err_handler	= &efx_err_handlers,
1591 1592 1593
#ifdef CONFIG_SFC_SRIOV
	.sriov_configure = efx_pci_sriov_configure,
#endif
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
};

/**************************************************************************
 *
 * Kernel module interface
 *
 *************************************************************************/

static int __init efx_init_module(void)
{
	int rc;

	printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");

	rc = register_netdevice_notifier(&efx_netdev_notifier);
	if (rc)
		goto err_notifier;

1612
#ifdef CONFIG_SFC_SRIOV
1613 1614 1615
	rc = efx_init_sriov();
	if (rc)
		goto err_sriov;
1616
#endif
1617

1618 1619
	rc = efx_create_reset_workqueue();
	if (rc)
1620
		goto err_reset;
1621 1622 1623 1624 1625 1626 1627 1628

	rc = pci_register_driver(&efx_pci_driver);
	if (rc < 0)
		goto err_pci;

	return 0;

 err_pci:
1629
	efx_destroy_reset_workqueue();
1630
 err_reset:
1631
#ifdef CONFIG_SFC_SRIOV
1632 1633
	efx_fini_sriov();
 err_sriov:
1634
#endif
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644
	unregister_netdevice_notifier(&efx_netdev_notifier);
 err_notifier:
	return rc;
}

static void __exit efx_exit_module(void)
{
	printk(KERN_INFO "Solarflare NET driver unloading\n");

	pci_unregister_driver(&efx_pci_driver);
1645
	efx_destroy_reset_workqueue();
1646
#ifdef CONFIG_SFC_SRIOV
1647
	efx_fini_sriov();
1648
#endif
1649 1650 1651 1652 1653 1654 1655
	unregister_netdevice_notifier(&efx_netdev_notifier);

}

module_init(efx_init_module);
module_exit(efx_exit_module);

1656 1657
MODULE_AUTHOR("Solarflare Communications and "
	      "Michael Brown <mbrown@fensystems.co.uk>");
B
Ben Hutchings 已提交
1658
MODULE_DESCRIPTION("Solarflare network driver");
1659 1660
MODULE_LICENSE("GPL");
MODULE_DEVICE_TABLE(pci, efx_pci_table);
1661
MODULE_VERSION(EFX_DRIVER_VERSION);