Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /****************************************************************************
0003  * Driver for Solarflare network controllers and boards
0004  * Copyright 2005-2006 Fen Systems Ltd.
0005  * Copyright 2005-2013 Solarflare Communications Inc.
0006  */
0007 
0008 #include <linux/filter.h>
0009 #include <linux/module.h>
0010 #include <linux/pci.h>
0011 #include <linux/netdevice.h>
0012 #include <linux/etherdevice.h>
0013 #include <linux/delay.h>
0014 #include <linux/notifier.h>
0015 #include <linux/ip.h>
0016 #include <linux/tcp.h>
0017 #include <linux/in.h>
0018 #include <linux/ethtool.h>
0019 #include <linux/topology.h>
0020 #include <linux/gfp.h>
0021 #include <linux/aer.h>
0022 #include <linux/interrupt.h>
0023 #include "net_driver.h"
0024 #include <net/gre.h>
0025 #include <net/udp_tunnel.h>
0026 #include "efx.h"
0027 #include "efx_common.h"
0028 #include "efx_channels.h"
0029 #include "rx_common.h"
0030 #include "tx_common.h"
0031 #include "nic.h"
0032 #include "io.h"
0033 #include "selftest.h"
0034 #include "sriov.h"
0035 #ifdef CONFIG_SFC_SIENA_SRIOV
0036 #include "siena_sriov.h"
0037 #endif
0038 
0039 #include "mcdi_port_common.h"
0040 #include "mcdi_pcol.h"
0041 #include "workarounds.h"
0042 
0043 /**************************************************************************
0044  *
0045  * Configurable values
0046  *
0047  *************************************************************************/
0048 
0049 module_param_named(interrupt_mode, efx_siena_interrupt_mode, uint, 0444);
0050 MODULE_PARM_DESC(interrupt_mode,
0051          "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
0052 
0053 module_param_named(rss_cpus, efx_siena_rss_cpus, uint, 0444);
0054 MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
0055 
0056 /*
0057  * Use separate channels for TX and RX events
0058  *
0059  * Set this to 1 to use separate channels for TX and RX. It allows us
0060  * to control interrupt affinity separately for TX and RX.
0061  *
0062  * This is only used in MSI-X interrupt mode
0063  */
0064 bool efx_siena_separate_tx_channels;
0065 module_param_named(efx_separate_tx_channels, efx_siena_separate_tx_channels,
0066            bool, 0444);
0067 MODULE_PARM_DESC(efx_separate_tx_channels,
0068          "Use separate channels for TX and RX");
0069 
0070 /* Initial interrupt moderation settings.  They can be modified after
0071  * module load with ethtool.
0072  *
0073  * The default for RX should strike a balance between increasing the
0074  * round-trip latency and reducing overhead.
0075  */
0076 static unsigned int rx_irq_mod_usec = 60;
0077 
0078 /* Initial interrupt moderation settings.  They can be modified after
0079  * module load with ethtool.
0080  *
0081  * This default is chosen to ensure that a 10G link does not go idle
0082  * while a TX queue is stopped after it has become full.  A queue is
0083  * restarted when it drops below half full.  The time this takes (assuming
0084  * worst case 3 descriptors per packet and 1024 descriptors) is
0085  *   512 / 3 * 1.2 = 205 usec.
0086  */
0087 static unsigned int tx_irq_mod_usec = 150;
0088 
0089 static bool phy_flash_cfg;
0090 module_param(phy_flash_cfg, bool, 0644);
0091 MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
0092 
0093 static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
0094              NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
0095              NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
0096              NETIF_MSG_TX_ERR | NETIF_MSG_HW);
0097 module_param(debug, uint, 0);
0098 MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
0099 
0100 /**************************************************************************
0101  *
0102  * Utility functions and prototypes
0103  *
0104  *************************************************************************/
0105 
0106 static void efx_remove_port(struct efx_nic *efx);
0107 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog);
0108 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp);
0109 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
0110             u32 flags);
0111 
0112 #define EFX_ASSERT_RESET_SERIALISED(efx)        \
0113     do {                        \
0114         if ((efx->state == STATE_READY) ||  \
0115             (efx->state == STATE_RECOVERY) ||   \
0116             (efx->state == STATE_DISABLED)) \
0117             ASSERT_RTNL();          \
0118     } while (0)
0119 
0120 /**************************************************************************
0121  *
0122  * Port handling
0123  *
0124  **************************************************************************/
0125 
0126 static void efx_fini_port(struct efx_nic *efx);
0127 
0128 static int efx_probe_port(struct efx_nic *efx)
0129 {
0130     int rc;
0131 
0132     netif_dbg(efx, probe, efx->net_dev, "create port\n");
0133 
0134     if (phy_flash_cfg)
0135         efx->phy_mode = PHY_MODE_SPECIAL;
0136 
0137     /* Connect up MAC/PHY operations table */
0138     rc = efx->type->probe_port(efx);
0139     if (rc)
0140         return rc;
0141 
0142     /* Initialise MAC address to permanent address */
0143     eth_hw_addr_set(efx->net_dev, efx->net_dev->perm_addr);
0144 
0145     return 0;
0146 }
0147 
0148 static int efx_init_port(struct efx_nic *efx)
0149 {
0150     int rc;
0151 
0152     netif_dbg(efx, drv, efx->net_dev, "init port\n");
0153 
0154     mutex_lock(&efx->mac_lock);
0155 
0156     efx->port_initialized = true;
0157 
0158     /* Ensure the PHY advertises the correct flow control settings */
0159     rc = efx_siena_mcdi_port_reconfigure(efx);
0160     if (rc && rc != -EPERM)
0161         goto fail;
0162 
0163     mutex_unlock(&efx->mac_lock);
0164     return 0;
0165 
0166 fail:
0167     mutex_unlock(&efx->mac_lock);
0168     return rc;
0169 }
0170 
0171 static void efx_fini_port(struct efx_nic *efx)
0172 {
0173     netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
0174 
0175     if (!efx->port_initialized)
0176         return;
0177 
0178     efx->port_initialized = false;
0179 
0180     efx->link_state.up = false;
0181     efx_siena_link_status_changed(efx);
0182 }
0183 
0184 static void efx_remove_port(struct efx_nic *efx)
0185 {
0186     netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
0187 
0188     efx->type->remove_port(efx);
0189 }
0190 
0191 /**************************************************************************
0192  *
0193  * NIC handling
0194  *
0195  **************************************************************************/
0196 
0197 static LIST_HEAD(efx_primary_list);
0198 static LIST_HEAD(efx_unassociated_list);
0199 
0200 static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
0201 {
0202     return left->type == right->type &&
0203         left->vpd_sn && right->vpd_sn &&
0204         !strcmp(left->vpd_sn, right->vpd_sn);
0205 }
0206 
0207 static void efx_associate(struct efx_nic *efx)
0208 {
0209     struct efx_nic *other, *next;
0210 
0211     if (efx->primary == efx) {
0212         /* Adding primary function; look for secondaries */
0213 
0214         netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
0215         list_add_tail(&efx->node, &efx_primary_list);
0216 
0217         list_for_each_entry_safe(other, next, &efx_unassociated_list,
0218                      node) {
0219             if (efx_same_controller(efx, other)) {
0220                 list_del(&other->node);
0221                 netif_dbg(other, probe, other->net_dev,
0222                       "moving to secondary list of %s %s\n",
0223                       pci_name(efx->pci_dev),
0224                       efx->net_dev->name);
0225                 list_add_tail(&other->node,
0226                           &efx->secondary_list);
0227                 other->primary = efx;
0228             }
0229         }
0230     } else {
0231         /* Adding secondary function; look for primary */
0232 
0233         list_for_each_entry(other, &efx_primary_list, node) {
0234             if (efx_same_controller(efx, other)) {
0235                 netif_dbg(efx, probe, efx->net_dev,
0236                       "adding to secondary list of %s %s\n",
0237                       pci_name(other->pci_dev),
0238                       other->net_dev->name);
0239                 list_add_tail(&efx->node,
0240                           &other->secondary_list);
0241                 efx->primary = other;
0242                 return;
0243             }
0244         }
0245 
0246         netif_dbg(efx, probe, efx->net_dev,
0247               "adding to unassociated list\n");
0248         list_add_tail(&efx->node, &efx_unassociated_list);
0249     }
0250 }
0251 
0252 static void efx_dissociate(struct efx_nic *efx)
0253 {
0254     struct efx_nic *other, *next;
0255 
0256     list_del(&efx->node);
0257     efx->primary = NULL;
0258 
0259     list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
0260         list_del(&other->node);
0261         netif_dbg(other, probe, other->net_dev,
0262               "moving to unassociated list\n");
0263         list_add_tail(&other->node, &efx_unassociated_list);
0264         other->primary = NULL;
0265     }
0266 }
0267 
0268 static int efx_probe_nic(struct efx_nic *efx)
0269 {
0270     int rc;
0271 
0272     netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
0273 
0274     /* Carry out hardware-type specific initialisation */
0275     rc = efx->type->probe(efx);
0276     if (rc)
0277         return rc;
0278 
0279     do {
0280         if (!efx->max_channels || !efx->max_tx_channels) {
0281             netif_err(efx, drv, efx->net_dev,
0282                   "Insufficient resources to allocate"
0283                   " any channels\n");
0284             rc = -ENOSPC;
0285             goto fail1;
0286         }
0287 
0288         /* Determine the number of channels and queues by trying
0289          * to hook in MSI-X interrupts.
0290          */
0291         rc = efx_siena_probe_interrupts(efx);
0292         if (rc)
0293             goto fail1;
0294 
0295         rc = efx_siena_set_channels(efx);
0296         if (rc)
0297             goto fail1;
0298 
0299         /* dimension_resources can fail with EAGAIN */
0300         rc = efx->type->dimension_resources(efx);
0301         if (rc != 0 && rc != -EAGAIN)
0302             goto fail2;
0303 
0304         if (rc == -EAGAIN)
0305             /* try again with new max_channels */
0306             efx_siena_remove_interrupts(efx);
0307 
0308     } while (rc == -EAGAIN);
0309 
0310     if (efx->n_channels > 1)
0311         netdev_rss_key_fill(efx->rss_context.rx_hash_key,
0312                     sizeof(efx->rss_context.rx_hash_key));
0313     efx_siena_set_default_rx_indir_table(efx, &efx->rss_context);
0314 
0315     /* Initialise the interrupt moderation settings */
0316     efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
0317     efx_siena_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec,
0318                       true, true);
0319 
0320     return 0;
0321 
0322 fail2:
0323     efx_siena_remove_interrupts(efx);
0324 fail1:
0325     efx->type->remove(efx);
0326     return rc;
0327 }
0328 
0329 static void efx_remove_nic(struct efx_nic *efx)
0330 {
0331     netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
0332 
0333     efx_siena_remove_interrupts(efx);
0334     efx->type->remove(efx);
0335 }
0336 
0337 /**************************************************************************
0338  *
0339  * NIC startup/shutdown
0340  *
0341  *************************************************************************/
0342 
0343 static int efx_probe_all(struct efx_nic *efx)
0344 {
0345     int rc;
0346 
0347     rc = efx_probe_nic(efx);
0348     if (rc) {
0349         netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
0350         goto fail1;
0351     }
0352 
0353     rc = efx_probe_port(efx);
0354     if (rc) {
0355         netif_err(efx, probe, efx->net_dev, "failed to create port\n");
0356         goto fail2;
0357     }
0358 
0359     BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
0360     if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
0361         rc = -EINVAL;
0362         goto fail3;
0363     }
0364 
0365 #ifdef CONFIG_SFC_SIENA_SRIOV
0366     rc = efx->type->vswitching_probe(efx);
0367     if (rc) /* not fatal; the PF will still work fine */
0368         netif_warn(efx, probe, efx->net_dev,
0369                "failed to setup vswitching rc=%d;"
0370                " VFs may not function\n", rc);
0371 #endif
0372 
0373     rc = efx_siena_probe_filters(efx);
0374     if (rc) {
0375         netif_err(efx, probe, efx->net_dev,
0376               "failed to create filter tables\n");
0377         goto fail4;
0378     }
0379 
0380     rc = efx_siena_probe_channels(efx);
0381     if (rc)
0382         goto fail5;
0383 
0384     return 0;
0385 
0386  fail5:
0387     efx_siena_remove_filters(efx);
0388  fail4:
0389 #ifdef CONFIG_SFC_SIENA_SRIOV
0390     efx->type->vswitching_remove(efx);
0391 #endif
0392  fail3:
0393     efx_remove_port(efx);
0394  fail2:
0395     efx_remove_nic(efx);
0396  fail1:
0397     return rc;
0398 }
0399 
0400 static void efx_remove_all(struct efx_nic *efx)
0401 {
0402     rtnl_lock();
0403     efx_xdp_setup_prog(efx, NULL);
0404     rtnl_unlock();
0405 
0406     efx_siena_remove_channels(efx);
0407     efx_siena_remove_filters(efx);
0408 #ifdef CONFIG_SFC_SIENA_SRIOV
0409     efx->type->vswitching_remove(efx);
0410 #endif
0411     efx_remove_port(efx);
0412     efx_remove_nic(efx);
0413 }
0414 
0415 /**************************************************************************
0416  *
0417  * Interrupt moderation
0418  *
0419  **************************************************************************/
0420 unsigned int efx_siena_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
0421 {
0422     if (usecs == 0)
0423         return 0;
0424     if (usecs * 1000 < efx->timer_quantum_ns)
0425         return 1; /* never round down to 0 */
0426     return usecs * 1000 / efx->timer_quantum_ns;
0427 }
0428 
0429 /* Set interrupt moderation parameters */
0430 int efx_siena_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
0431                   unsigned int rx_usecs, bool rx_adaptive,
0432                   bool rx_may_override_tx)
0433 {
0434     struct efx_channel *channel;
0435     unsigned int timer_max_us;
0436 
0437     EFX_ASSERT_RESET_SERIALISED(efx);
0438 
0439     timer_max_us = efx->timer_max_ns / 1000;
0440 
0441     if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
0442         return -EINVAL;
0443 
0444     if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
0445         !rx_may_override_tx) {
0446         netif_err(efx, drv, efx->net_dev, "Channels are shared. "
0447               "RX and TX IRQ moderation must be equal\n");
0448         return -EINVAL;
0449     }
0450 
0451     efx->irq_rx_adaptive = rx_adaptive;
0452     efx->irq_rx_moderation_us = rx_usecs;
0453     efx_for_each_channel(channel, efx) {
0454         if (efx_channel_has_rx_queue(channel))
0455             channel->irq_moderation_us = rx_usecs;
0456         else if (efx_channel_has_tx_queues(channel))
0457             channel->irq_moderation_us = tx_usecs;
0458         else if (efx_channel_is_xdp_tx(channel))
0459             channel->irq_moderation_us = tx_usecs;
0460     }
0461 
0462     return 0;
0463 }
0464 
0465 void efx_siena_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
0466                   unsigned int *rx_usecs, bool *rx_adaptive)
0467 {
0468     *rx_adaptive = efx->irq_rx_adaptive;
0469     *rx_usecs = efx->irq_rx_moderation_us;
0470 
0471     /* If channels are shared between RX and TX, so is IRQ
0472      * moderation.  Otherwise, IRQ moderation is the same for all
0473      * TX channels and is not adaptive.
0474      */
0475     if (efx->tx_channel_offset == 0) {
0476         *tx_usecs = *rx_usecs;
0477     } else {
0478         struct efx_channel *tx_channel;
0479 
0480         tx_channel = efx->channel[efx->tx_channel_offset];
0481         *tx_usecs = tx_channel->irq_moderation_us;
0482     }
0483 }
0484 
0485 /**************************************************************************
0486  *
0487  * ioctls
0488  *
0489  *************************************************************************/
0490 
0491 /* Net device ioctl
0492  * Context: process, rtnl_lock() held.
0493  */
0494 static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
0495 {
0496     struct efx_nic *efx = netdev_priv(net_dev);
0497     struct mii_ioctl_data *data = if_mii(ifr);
0498 
0499     if (cmd == SIOCSHWTSTAMP)
0500         return efx_siena_ptp_set_ts_config(efx, ifr);
0501     if (cmd == SIOCGHWTSTAMP)
0502         return efx_siena_ptp_get_ts_config(efx, ifr);
0503 
0504     /* Convert phy_id from older PRTAD/DEVAD format */
0505     if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
0506         (data->phy_id & 0xfc00) == 0x0400)
0507         data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
0508 
0509     return mdio_mii_ioctl(&efx->mdio, data, cmd);
0510 }
0511 
0512 /**************************************************************************
0513  *
0514  * Kernel net device interface
0515  *
0516  *************************************************************************/
0517 
0518 /* Context: process, rtnl_lock() held. */
0519 static int efx_net_open(struct net_device *net_dev)
0520 {
0521     struct efx_nic *efx = netdev_priv(net_dev);
0522     int rc;
0523 
0524     netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
0525           raw_smp_processor_id());
0526 
0527     rc = efx_check_disabled(efx);
0528     if (rc)
0529         return rc;
0530     if (efx->phy_mode & PHY_MODE_SPECIAL)
0531         return -EBUSY;
0532     if (efx_siena_mcdi_poll_reboot(efx) && efx_siena_reset(efx, RESET_TYPE_ALL))
0533         return -EIO;
0534 
0535     /* Notify the kernel of the link state polled during driver load,
0536      * before the monitor starts running */
0537     efx_siena_link_status_changed(efx);
0538 
0539     efx_siena_start_all(efx);
0540     if (efx->state == STATE_DISABLED || efx->reset_pending)
0541         netif_device_detach(efx->net_dev);
0542     efx_siena_selftest_async_start(efx);
0543     return 0;
0544 }
0545 
0546 /* Context: process, rtnl_lock() held.
0547  * Note that the kernel will ignore our return code; this method
0548  * should really be a void.
0549  */
0550 static int efx_net_stop(struct net_device *net_dev)
0551 {
0552     struct efx_nic *efx = netdev_priv(net_dev);
0553 
0554     netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
0555           raw_smp_processor_id());
0556 
0557     /* Stop the device and flush all the channels */
0558     efx_siena_stop_all(efx);
0559 
0560     return 0;
0561 }
0562 
0563 static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
0564 {
0565     struct efx_nic *efx = netdev_priv(net_dev);
0566 
0567     if (efx->type->vlan_rx_add_vid)
0568         return efx->type->vlan_rx_add_vid(efx, proto, vid);
0569     else
0570         return -EOPNOTSUPP;
0571 }
0572 
0573 static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
0574 {
0575     struct efx_nic *efx = netdev_priv(net_dev);
0576 
0577     if (efx->type->vlan_rx_kill_vid)
0578         return efx->type->vlan_rx_kill_vid(efx, proto, vid);
0579     else
0580         return -EOPNOTSUPP;
0581 }
0582 
0583 static const struct net_device_ops efx_netdev_ops = {
0584     .ndo_open       = efx_net_open,
0585     .ndo_stop       = efx_net_stop,
0586     .ndo_get_stats64    = efx_siena_net_stats,
0587     .ndo_tx_timeout     = efx_siena_watchdog,
0588     .ndo_start_xmit     = efx_siena_hard_start_xmit,
0589     .ndo_validate_addr  = eth_validate_addr,
0590     .ndo_eth_ioctl      = efx_ioctl,
0591     .ndo_change_mtu     = efx_siena_change_mtu,
0592     .ndo_set_mac_address    = efx_siena_set_mac_address,
0593     .ndo_set_rx_mode    = efx_siena_set_rx_mode,
0594     .ndo_set_features   = efx_siena_set_features,
0595     .ndo_features_check = efx_siena_features_check,
0596     .ndo_vlan_rx_add_vid    = efx_vlan_rx_add_vid,
0597     .ndo_vlan_rx_kill_vid   = efx_vlan_rx_kill_vid,
0598 #ifdef CONFIG_SFC_SIENA_SRIOV
0599     .ndo_set_vf_mac     = efx_sriov_set_vf_mac,
0600     .ndo_set_vf_vlan    = efx_sriov_set_vf_vlan,
0601     .ndo_set_vf_spoofchk    = efx_sriov_set_vf_spoofchk,
0602     .ndo_get_vf_config  = efx_sriov_get_vf_config,
0603     .ndo_set_vf_link_state  = efx_sriov_set_vf_link_state,
0604 #endif
0605     .ndo_get_phys_port_id   = efx_siena_get_phys_port_id,
0606     .ndo_get_phys_port_name = efx_siena_get_phys_port_name,
0607     .ndo_setup_tc       = efx_siena_setup_tc,
0608 #ifdef CONFIG_RFS_ACCEL
0609     .ndo_rx_flow_steer  = efx_siena_filter_rfs,
0610 #endif
0611     .ndo_xdp_xmit       = efx_xdp_xmit,
0612     .ndo_bpf        = efx_xdp
0613 };
0614 
0615 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
0616 {
0617     struct bpf_prog *old_prog;
0618 
0619     if (efx->xdp_rxq_info_failed) {
0620         netif_err(efx, drv, efx->net_dev,
0621               "Unable to bind XDP program due to previous failure of rxq_info\n");
0622         return -EINVAL;
0623     }
0624 
0625     if (prog && efx->net_dev->mtu > efx_siena_xdp_max_mtu(efx)) {
0626         netif_err(efx, drv, efx->net_dev,
0627               "Unable to configure XDP with MTU of %d (max: %d)\n",
0628               efx->net_dev->mtu, efx_siena_xdp_max_mtu(efx));
0629         return -EINVAL;
0630     }
0631 
0632     old_prog = rtnl_dereference(efx->xdp_prog);
0633     rcu_assign_pointer(efx->xdp_prog, prog);
0634     /* Release the reference that was originally passed by the caller. */
0635     if (old_prog)
0636         bpf_prog_put(old_prog);
0637 
0638     return 0;
0639 }
0640 
0641 /* Context: process, rtnl_lock() held. */
0642 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
0643 {
0644     struct efx_nic *efx = netdev_priv(dev);
0645 
0646     switch (xdp->command) {
0647     case XDP_SETUP_PROG:
0648         return efx_xdp_setup_prog(efx, xdp->prog);
0649     default:
0650         return -EINVAL;
0651     }
0652 }
0653 
0654 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
0655             u32 flags)
0656 {
0657     struct efx_nic *efx = netdev_priv(dev);
0658 
0659     if (!netif_running(dev))
0660         return -EINVAL;
0661 
0662     return efx_siena_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH);
0663 }
0664 
0665 static void efx_update_name(struct efx_nic *efx)
0666 {
0667     strcpy(efx->name, efx->net_dev->name);
0668     efx_siena_mtd_rename(efx);
0669     efx_siena_set_channel_names(efx);
0670 }
0671 
0672 static int efx_netdev_event(struct notifier_block *this,
0673                 unsigned long event, void *ptr)
0674 {
0675     struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
0676 
0677     if ((net_dev->netdev_ops == &efx_netdev_ops) &&
0678         event == NETDEV_CHANGENAME)
0679         efx_update_name(netdev_priv(net_dev));
0680 
0681     return NOTIFY_DONE;
0682 }
0683 
0684 static struct notifier_block efx_netdev_notifier = {
0685     .notifier_call = efx_netdev_event,
0686 };
0687 
0688 static ssize_t phy_type_show(struct device *dev,
0689                  struct device_attribute *attr, char *buf)
0690 {
0691     struct efx_nic *efx = dev_get_drvdata(dev);
0692     return sprintf(buf, "%d\n", efx->phy_type);
0693 }
0694 static DEVICE_ATTR_RO(phy_type);
0695 
0696 static int efx_register_netdev(struct efx_nic *efx)
0697 {
0698     struct net_device *net_dev = efx->net_dev;
0699     struct efx_channel *channel;
0700     int rc;
0701 
0702     net_dev->watchdog_timeo = 5 * HZ;
0703     net_dev->irq = efx->pci_dev->irq;
0704     net_dev->netdev_ops = &efx_netdev_ops;
0705     if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
0706         net_dev->priv_flags |= IFF_UNICAST_FLT;
0707     net_dev->ethtool_ops = &efx_siena_ethtool_ops;
0708     netif_set_tso_max_segs(net_dev, EFX_TSO_MAX_SEGS);
0709     net_dev->min_mtu = EFX_MIN_MTU;
0710     net_dev->max_mtu = EFX_MAX_MTU;
0711 
0712     rtnl_lock();
0713 
0714     /* Enable resets to be scheduled and check whether any were
0715      * already requested.  If so, the NIC is probably hosed so we
0716      * abort.
0717      */
0718     efx->state = STATE_READY;
0719     smp_mb(); /* ensure we change state before checking reset_pending */
0720     if (efx->reset_pending) {
0721         pci_err(efx->pci_dev, "aborting probe due to scheduled reset\n");
0722         rc = -EIO;
0723         goto fail_locked;
0724     }
0725 
0726     rc = dev_alloc_name(net_dev, net_dev->name);
0727     if (rc < 0)
0728         goto fail_locked;
0729     efx_update_name(efx);
0730 
0731     /* Always start with carrier off; PHY events will detect the link */
0732     netif_carrier_off(net_dev);
0733 
0734     rc = register_netdevice(net_dev);
0735     if (rc)
0736         goto fail_locked;
0737 
0738     efx_for_each_channel(channel, efx) {
0739         struct efx_tx_queue *tx_queue;
0740         efx_for_each_channel_tx_queue(tx_queue, channel)
0741             efx_siena_init_tx_queue_core_txq(tx_queue);
0742     }
0743 
0744     efx_associate(efx);
0745 
0746     rtnl_unlock();
0747 
0748     rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
0749     if (rc) {
0750         netif_err(efx, drv, efx->net_dev,
0751               "failed to init net dev attributes\n");
0752         goto fail_registered;
0753     }
0754 
0755     efx_siena_init_mcdi_logging(efx);
0756 
0757     return 0;
0758 
0759 fail_registered:
0760     rtnl_lock();
0761     efx_dissociate(efx);
0762     unregister_netdevice(net_dev);
0763 fail_locked:
0764     efx->state = STATE_UNINIT;
0765     rtnl_unlock();
0766     netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
0767     return rc;
0768 }
0769 
0770 static void efx_unregister_netdev(struct efx_nic *efx)
0771 {
0772     if (!efx->net_dev)
0773         return;
0774 
0775     BUG_ON(netdev_priv(efx->net_dev) != efx);
0776 
0777     if (efx_dev_registered(efx)) {
0778         strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
0779         efx_siena_fini_mcdi_logging(efx);
0780         device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
0781         unregister_netdev(efx->net_dev);
0782     }
0783 }
0784 
0785 /**************************************************************************
0786  *
0787  * List of NICs we support
0788  *
0789  **************************************************************************/
0790 
0791 /* PCI device ID table */
0792 static const struct pci_device_id efx_pci_table[] = {
0793     {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803),  /* SFC9020 */
0794      .driver_data = (unsigned long)&siena_a0_nic_type},
0795     {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813),  /* SFL9021 */
0796      .driver_data = (unsigned long)&siena_a0_nic_type},
0797     {0}         /* end of list */
0798 };
0799 
0800 /**************************************************************************
0801  *
0802  * Data housekeeping
0803  *
0804  **************************************************************************/
0805 
0806 void efx_siena_update_sw_stats(struct efx_nic *efx, u64 *stats)
0807 {
0808     u64 n_rx_nodesc_trunc = 0;
0809     struct efx_channel *channel;
0810 
0811     efx_for_each_channel(channel, efx)
0812         n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
0813     stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
0814     stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
0815 }
0816 
0817 /**************************************************************************
0818  *
0819  * PCI interface
0820  *
0821  **************************************************************************/
0822 
0823 /* Main body of final NIC shutdown code
0824  * This is called only at module unload (or hotplug removal).
0825  */
0826 static void efx_pci_remove_main(struct efx_nic *efx)
0827 {
0828     /* Flush reset_work. It can no longer be scheduled since we
0829      * are not READY.
0830      */
0831     BUG_ON(efx->state == STATE_READY);
0832     efx_siena_flush_reset_workqueue(efx);
0833 
0834     efx_siena_disable_interrupts(efx);
0835     efx_siena_clear_interrupt_affinity(efx);
0836     efx_siena_fini_interrupt(efx);
0837     efx_fini_port(efx);
0838     efx->type->fini(efx);
0839     efx_siena_fini_napi(efx);
0840     efx_remove_all(efx);
0841 }
0842 
0843 /* Final NIC shutdown
0844  * This is called only at module unload (or hotplug removal).  A PF can call
0845  * this on its VFs to ensure they are unbound first.
0846  */
0847 static void efx_pci_remove(struct pci_dev *pci_dev)
0848 {
0849     struct efx_nic *efx;
0850 
0851     efx = pci_get_drvdata(pci_dev);
0852     if (!efx)
0853         return;
0854 
0855     /* Mark the NIC as fini, then stop the interface */
0856     rtnl_lock();
0857     efx_dissociate(efx);
0858     dev_close(efx->net_dev);
0859     efx_siena_disable_interrupts(efx);
0860     efx->state = STATE_UNINIT;
0861     rtnl_unlock();
0862 
0863     if (efx->type->sriov_fini)
0864         efx->type->sriov_fini(efx);
0865 
0866     efx_unregister_netdev(efx);
0867 
0868     efx_siena_mtd_remove(efx);
0869 
0870     efx_pci_remove_main(efx);
0871 
0872     efx_siena_fini_io(efx);
0873     netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
0874 
0875     efx_siena_fini_struct(efx);
0876     free_netdev(efx->net_dev);
0877 
0878     pci_disable_pcie_error_reporting(pci_dev);
0879 };
0880 
0881 /* NIC VPD information
0882  * Called during probe to display the part number of the
0883  * installed NIC.
0884  */
0885 static void efx_probe_vpd_strings(struct efx_nic *efx)
0886 {
0887     struct pci_dev *dev = efx->pci_dev;
0888     unsigned int vpd_size, kw_len;
0889     u8 *vpd_data;
0890     int start;
0891 
0892     vpd_data = pci_vpd_alloc(dev, &vpd_size);
0893     if (IS_ERR(vpd_data)) {
0894         pci_warn(dev, "Unable to read VPD\n");
0895         return;
0896     }
0897 
0898     start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
0899                          PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
0900     if (start < 0)
0901         pci_err(dev, "Part number not found or incomplete\n");
0902     else
0903         pci_info(dev, "Part Number : %.*s\n", kw_len, vpd_data + start);
0904 
0905     start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
0906                          PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);
0907     if (start < 0)
0908         pci_err(dev, "Serial number not found or incomplete\n");
0909     else
0910         efx->vpd_sn = kmemdup_nul(vpd_data + start, kw_len, GFP_KERNEL);
0911 
0912     kfree(vpd_data);
0913 }
0914 
0915 
0916 /* Main body of NIC initialisation
0917  * This is called at module load (or hotplug insertion, theoretically).
0918  */
0919 static int efx_pci_probe_main(struct efx_nic *efx)
0920 {
0921     int rc;
0922 
0923     /* Do start-of-day initialisation */
0924     rc = efx_probe_all(efx);
0925     if (rc)
0926         goto fail1;
0927 
0928     efx_siena_init_napi(efx);
0929 
0930     down_write(&efx->filter_sem);
0931     rc = efx->type->init(efx);
0932     up_write(&efx->filter_sem);
0933     if (rc) {
0934         pci_err(efx->pci_dev, "failed to initialise NIC\n");
0935         goto fail3;
0936     }
0937 
0938     rc = efx_init_port(efx);
0939     if (rc) {
0940         netif_err(efx, probe, efx->net_dev,
0941               "failed to initialise port\n");
0942         goto fail4;
0943     }
0944 
0945     rc = efx_siena_init_interrupt(efx);
0946     if (rc)
0947         goto fail5;
0948 
0949     efx_siena_set_interrupt_affinity(efx);
0950     rc = efx_siena_enable_interrupts(efx);
0951     if (rc)
0952         goto fail6;
0953 
0954     return 0;
0955 
0956  fail6:
0957     efx_siena_clear_interrupt_affinity(efx);
0958     efx_siena_fini_interrupt(efx);
0959  fail5:
0960     efx_fini_port(efx);
0961  fail4:
0962     efx->type->fini(efx);
0963  fail3:
0964     efx_siena_fini_napi(efx);
0965     efx_remove_all(efx);
0966  fail1:
0967     return rc;
0968 }
0969 
0970 static int efx_pci_probe_post_io(struct efx_nic *efx)
0971 {
0972     struct net_device *net_dev = efx->net_dev;
0973     int rc = efx_pci_probe_main(efx);
0974 
0975     if (rc)
0976         return rc;
0977 
0978     if (efx->type->sriov_init) {
0979         rc = efx->type->sriov_init(efx);
0980         if (rc)
0981             pci_err(efx->pci_dev, "SR-IOV can't be enabled rc %d\n",
0982                 rc);
0983     }
0984 
0985     /* Determine netdevice features */
0986     net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
0987                   NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
0988     if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
0989         net_dev->features |= NETIF_F_TSO6;
0990     /* Check whether device supports TSO */
0991     if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
0992         net_dev->features &= ~NETIF_F_ALL_TSO;
0993     /* Mask for features that also apply to VLAN devices */
0994     net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
0995                    NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
0996                    NETIF_F_RXCSUM);
0997 
0998     net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
0999 
1000     /* Disable receiving frames with bad FCS, by default. */
1001     net_dev->features &= ~NETIF_F_RXALL;
1002 
1003     /* Disable VLAN filtering by default.  It may be enforced if
1004      * the feature is fixed (i.e. VLAN filters are required to
1005      * receive VLAN tagged packets due to vPort restrictions).
1006      */
1007     net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1008     net_dev->features |= efx->fixed_features;
1009 
1010     rc = efx_register_netdev(efx);
1011     if (!rc)
1012         return 0;
1013 
1014     efx_pci_remove_main(efx);
1015     return rc;
1016 }
1017 
1018 /* NIC initialisation
1019  *
1020  * This is called at module load (or hotplug insertion,
1021  * theoretically).  It sets up PCI mappings, resets the NIC,
1022  * sets up and registers the network devices with the kernel and hooks
1023  * the interrupt service routine.  It does not prepare the device for
1024  * transmission; this is left to the first time one of the network
1025  * interfaces is brought up (i.e. efx_net_open).
1026  */
1027 static int efx_pci_probe(struct pci_dev *pci_dev,
1028              const struct pci_device_id *entry)
1029 {
1030     struct net_device *net_dev;
1031     struct efx_nic *efx;
1032     int rc;
1033 
1034     /* Allocate and initialise a struct net_device and struct efx_nic */
1035     net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
1036                      EFX_MAX_RX_QUEUES);
1037     if (!net_dev)
1038         return -ENOMEM;
1039     efx = netdev_priv(net_dev);
1040     efx->type = (const struct efx_nic_type *) entry->driver_data;
1041     efx->fixed_features |= NETIF_F_HIGHDMA;
1042 
1043     pci_set_drvdata(pci_dev, efx);
1044     SET_NETDEV_DEV(net_dev, &pci_dev->dev);
1045     rc = efx_siena_init_struct(efx, pci_dev, net_dev);
1046     if (rc)
1047         goto fail1;
1048 
1049     pci_info(pci_dev, "Solarflare NIC detected\n");
1050 
1051     if (!efx->type->is_vf)
1052         efx_probe_vpd_strings(efx);
1053 
1054     /* Set up basic I/O (BAR mappings etc) */
1055     rc = efx_siena_init_io(efx, efx->type->mem_bar(efx),
1056                    efx->type->max_dma_mask,
1057                    efx->type->mem_map_size(efx));
1058     if (rc)
1059         goto fail2;
1060 
1061     rc = efx_pci_probe_post_io(efx);
1062     if (rc) {
1063         /* On failure, retry once immediately.
1064          * If we aborted probe due to a scheduled reset, dismiss it.
1065          */
1066         efx->reset_pending = 0;
1067         rc = efx_pci_probe_post_io(efx);
1068         if (rc) {
1069             /* On another failure, retry once more
1070              * after a 50-305ms delay.
1071              */
1072             unsigned char r;
1073 
1074             get_random_bytes(&r, 1);
1075             msleep((unsigned int)r + 50);
1076             efx->reset_pending = 0;
1077             rc = efx_pci_probe_post_io(efx);
1078         }
1079     }
1080     if (rc)
1081         goto fail3;
1082 
1083     netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
1084 
1085     /* Try to create MTDs, but allow this to fail */
1086     rtnl_lock();
1087     rc = efx_mtd_probe(efx);
1088     rtnl_unlock();
1089     if (rc && rc != -EPERM)
1090         netif_warn(efx, probe, efx->net_dev,
1091                "failed to create MTDs (%d)\n", rc);
1092 
1093     (void)pci_enable_pcie_error_reporting(pci_dev);
1094 
1095     if (efx->type->udp_tnl_push_ports)
1096         efx->type->udp_tnl_push_ports(efx);
1097 
1098     return 0;
1099 
1100  fail3:
1101     efx_siena_fini_io(efx);
1102  fail2:
1103     efx_siena_fini_struct(efx);
1104  fail1:
1105     WARN_ON(rc > 0);
1106     netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
1107     free_netdev(net_dev);
1108     return rc;
1109 }
1110 
1111 /* efx_pci_sriov_configure returns the actual number of Virtual Functions
1112  * enabled on success
1113  */
1114 #ifdef CONFIG_SFC_SIENA_SRIOV
1115 static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1116 {
1117     int rc;
1118     struct efx_nic *efx = pci_get_drvdata(dev);
1119 
1120     if (efx->type->sriov_configure) {
1121         rc = efx->type->sriov_configure(efx, num_vfs);
1122         if (rc)
1123             return rc;
1124         else
1125             return num_vfs;
1126     } else
1127         return -EOPNOTSUPP;
1128 }
1129 #endif
1130 
1131 static int efx_pm_freeze(struct device *dev)
1132 {
1133     struct efx_nic *efx = dev_get_drvdata(dev);
1134 
1135     rtnl_lock();
1136 
1137     if (efx->state != STATE_DISABLED) {
1138         efx->state = STATE_UNINIT;
1139 
1140         efx_device_detach_sync(efx);
1141 
1142         efx_siena_stop_all(efx);
1143         efx_siena_disable_interrupts(efx);
1144     }
1145 
1146     rtnl_unlock();
1147 
1148     return 0;
1149 }
1150 
1151 static int efx_pm_thaw(struct device *dev)
1152 {
1153     int rc;
1154     struct efx_nic *efx = dev_get_drvdata(dev);
1155 
1156     rtnl_lock();
1157 
1158     if (efx->state != STATE_DISABLED) {
1159         rc = efx_siena_enable_interrupts(efx);
1160         if (rc)
1161             goto fail;
1162 
1163         mutex_lock(&efx->mac_lock);
1164         efx_siena_mcdi_port_reconfigure(efx);
1165         mutex_unlock(&efx->mac_lock);
1166 
1167         efx_siena_start_all(efx);
1168 
1169         efx_device_attach_if_not_resetting(efx);
1170 
1171         efx->state = STATE_READY;
1172 
1173         efx->type->resume_wol(efx);
1174     }
1175 
1176     rtnl_unlock();
1177 
1178     /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
1179     efx_siena_queue_reset_work(efx);
1180 
1181     return 0;
1182 
1183 fail:
1184     rtnl_unlock();
1185 
1186     return rc;
1187 }
1188 
1189 static int efx_pm_poweroff(struct device *dev)
1190 {
1191     struct pci_dev *pci_dev = to_pci_dev(dev);
1192     struct efx_nic *efx = pci_get_drvdata(pci_dev);
1193 
1194     efx->type->fini(efx);
1195 
1196     efx->reset_pending = 0;
1197 
1198     pci_save_state(pci_dev);
1199     return pci_set_power_state(pci_dev, PCI_D3hot);
1200 }
1201 
1202 /* Used for both resume and restore */
1203 static int efx_pm_resume(struct device *dev)
1204 {
1205     struct pci_dev *pci_dev = to_pci_dev(dev);
1206     struct efx_nic *efx = pci_get_drvdata(pci_dev);
1207     int rc;
1208 
1209     rc = pci_set_power_state(pci_dev, PCI_D0);
1210     if (rc)
1211         return rc;
1212     pci_restore_state(pci_dev);
1213     rc = pci_enable_device(pci_dev);
1214     if (rc)
1215         return rc;
1216     pci_set_master(efx->pci_dev);
1217     rc = efx->type->reset(efx, RESET_TYPE_ALL);
1218     if (rc)
1219         return rc;
1220     down_write(&efx->filter_sem);
1221     rc = efx->type->init(efx);
1222     up_write(&efx->filter_sem);
1223     if (rc)
1224         return rc;
1225     rc = efx_pm_thaw(dev);
1226     return rc;
1227 }
1228 
1229 static int efx_pm_suspend(struct device *dev)
1230 {
1231     int rc;
1232 
1233     efx_pm_freeze(dev);
1234     rc = efx_pm_poweroff(dev);
1235     if (rc)
1236         efx_pm_resume(dev);
1237     return rc;
1238 }
1239 
1240 static const struct dev_pm_ops efx_pm_ops = {
1241     .suspend    = efx_pm_suspend,
1242     .resume     = efx_pm_resume,
1243     .freeze     = efx_pm_freeze,
1244     .thaw       = efx_pm_thaw,
1245     .poweroff   = efx_pm_poweroff,
1246     .restore    = efx_pm_resume,
1247 };
1248 
1249 static struct pci_driver efx_pci_driver = {
1250     .name       = KBUILD_MODNAME,
1251     .id_table   = efx_pci_table,
1252     .probe      = efx_pci_probe,
1253     .remove     = efx_pci_remove,
1254     .driver.pm  = &efx_pm_ops,
1255     .err_handler    = &efx_siena_err_handlers,
1256 #ifdef CONFIG_SFC_SIENA_SRIOV
1257     .sriov_configure = efx_pci_sriov_configure,
1258 #endif
1259 };
1260 
1261 /**************************************************************************
1262  *
1263  * Kernel module interface
1264  *
1265  *************************************************************************/
1266 
1267 static int __init efx_init_module(void)
1268 {
1269     int rc;
1270 
1271     pr_info("Solarflare Siena driver\n");
1272 
1273     rc = register_netdevice_notifier(&efx_netdev_notifier);
1274     if (rc)
1275         goto err_notifier;
1276 
1277 #ifdef CONFIG_SFC_SIENA_SRIOV
1278     rc = efx_init_sriov();
1279     if (rc)
1280         goto err_sriov;
1281 #endif
1282 
1283     rc = efx_siena_create_reset_workqueue();
1284     if (rc)
1285         goto err_reset;
1286 
1287     rc = pci_register_driver(&efx_pci_driver);
1288     if (rc < 0)
1289         goto err_pci;
1290 
1291     return 0;
1292 
1293  err_pci:
1294     efx_siena_destroy_reset_workqueue();
1295  err_reset:
1296 #ifdef CONFIG_SFC_SIENA_SRIOV
1297     efx_fini_sriov();
1298  err_sriov:
1299 #endif
1300     unregister_netdevice_notifier(&efx_netdev_notifier);
1301  err_notifier:
1302     return rc;
1303 }
1304 
1305 static void __exit efx_exit_module(void)
1306 {
1307     pr_info("Solarflare Siena driver unloading\n");
1308 
1309     pci_unregister_driver(&efx_pci_driver);
1310     efx_siena_destroy_reset_workqueue();
1311 #ifdef CONFIG_SFC_SIENA_SRIOV
1312     efx_fini_sriov();
1313 #endif
1314     unregister_netdevice_notifier(&efx_netdev_notifier);
1315 
1316 }
1317 
1318 module_init(efx_init_module);
1319 module_exit(efx_exit_module);
1320 
1321 MODULE_AUTHOR("Solarflare Communications and "
1322           "Michael Brown <mbrown@fensystems.co.uk>");
1323 MODULE_DESCRIPTION("Solarflare Siena network driver");
1324 MODULE_LICENSE("GPL");
1325 MODULE_DEVICE_TABLE(pci, efx_pci_table);