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