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