Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / net / ethernet / sfc / siena / efx.c
blob59d3a60433798c48921d3f33e161703532832f27
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 */
8 #include <linux/filter.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/netdevice.h>
12 #include <linux/etherdevice.h>
13 #include <linux/delay.h>
14 #include <linux/notifier.h>
15 #include <linux/ip.h>
16 #include <linux/tcp.h>
17 #include <linux/in.h>
18 #include <linux/ethtool.h>
19 #include <linux/topology.h>
20 #include <linux/gfp.h>
21 #include <linux/interrupt.h>
22 #include "net_driver.h"
23 #include <net/gre.h>
24 #include <net/udp_tunnel.h>
25 #include "efx.h"
26 #include "efx_common.h"
27 #include "efx_channels.h"
28 #include "rx_common.h"
29 #include "tx_common.h"
30 #include "nic.h"
31 #include "io.h"
32 #include "selftest.h"
33 #include "sriov.h"
34 #ifdef CONFIG_SFC_SIENA_SRIOV
35 #include "siena_sriov.h"
36 #endif
38 #include "mcdi_port_common.h"
39 #include "mcdi_pcol.h"
40 #include "workarounds.h"
42 /**************************************************************************
44 * Configurable values
46 *************************************************************************/
48 module_param_named(interrupt_mode, efx_siena_interrupt_mode, uint, 0444);
49 MODULE_PARM_DESC(interrupt_mode,
50 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
52 module_param_named(rss_cpus, efx_siena_rss_cpus, uint, 0444);
53 MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
56 * Use separate channels for TX and RX events
58 * Set this to 1 to use separate channels for TX and RX. It allows us
59 * to control interrupt affinity separately for TX and RX.
61 * This is only used in MSI-X interrupt mode
63 bool efx_siena_separate_tx_channels;
64 module_param_named(efx_separate_tx_channels, efx_siena_separate_tx_channels,
65 bool, 0444);
66 MODULE_PARM_DESC(efx_separate_tx_channels,
67 "Use separate channels for TX and RX");
69 /* Initial interrupt moderation settings. They can be modified after
70 * module load with ethtool.
72 * The default for RX should strike a balance between increasing the
73 * round-trip latency and reducing overhead.
75 static unsigned int rx_irq_mod_usec = 60;
77 /* Initial interrupt moderation settings. They can be modified after
78 * module load with ethtool.
80 * This default is chosen to ensure that a 10G link does not go idle
81 * while a TX queue is stopped after it has become full. A queue is
82 * restarted when it drops below half full. The time this takes (assuming
83 * worst case 3 descriptors per packet and 1024 descriptors) is
84 * 512 / 3 * 1.2 = 205 usec.
86 static unsigned int tx_irq_mod_usec = 150;
88 static bool phy_flash_cfg;
89 module_param(phy_flash_cfg, bool, 0644);
90 MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
92 static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
93 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
94 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
95 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
96 module_param(debug, uint, 0);
97 MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
99 /**************************************************************************
101 * Utility functions and prototypes
103 *************************************************************************/
105 static void efx_remove_port(struct efx_nic *efx);
106 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog);
107 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp);
108 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
109 u32 flags);
111 #define EFX_ASSERT_RESET_SERIALISED(efx) \
112 do { \
113 if ((efx->state == STATE_READY) || \
114 (efx->state == STATE_RECOVERY) || \
115 (efx->state == STATE_DISABLED)) \
116 ASSERT_RTNL(); \
117 } while (0)
119 /**************************************************************************
121 * Port handling
123 **************************************************************************/
125 static void efx_fini_port(struct efx_nic *efx);
127 static int efx_probe_port(struct efx_nic *efx)
129 int rc;
131 netif_dbg(efx, probe, efx->net_dev, "create port\n");
133 if (phy_flash_cfg)
134 efx->phy_mode = PHY_MODE_SPECIAL;
136 /* Connect up MAC/PHY operations table */
137 rc = efx->type->probe_port(efx);
138 if (rc)
139 return rc;
141 /* Initialise MAC address to permanent address */
142 eth_hw_addr_set(efx->net_dev, efx->net_dev->perm_addr);
144 return 0;
147 static int efx_init_port(struct efx_nic *efx)
149 int rc;
151 netif_dbg(efx, drv, efx->net_dev, "init port\n");
153 mutex_lock(&efx->mac_lock);
155 efx->port_initialized = true;
157 /* Ensure the PHY advertises the correct flow control settings */
158 rc = efx_siena_mcdi_port_reconfigure(efx);
159 if (rc && rc != -EPERM)
160 goto fail;
162 mutex_unlock(&efx->mac_lock);
163 return 0;
165 fail:
166 mutex_unlock(&efx->mac_lock);
167 return rc;
170 static void efx_fini_port(struct efx_nic *efx)
172 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
174 if (!efx->port_initialized)
175 return;
177 efx->port_initialized = false;
179 efx->link_state.up = false;
180 efx_siena_link_status_changed(efx);
183 static void efx_remove_port(struct efx_nic *efx)
185 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
187 efx->type->remove_port(efx);
190 /**************************************************************************
192 * NIC handling
194 **************************************************************************/
196 static LIST_HEAD(efx_primary_list);
197 static LIST_HEAD(efx_unassociated_list);
199 static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
201 return left->type == right->type &&
202 left->vpd_sn && right->vpd_sn &&
203 !strcmp(left->vpd_sn, right->vpd_sn);
206 static void efx_associate(struct efx_nic *efx)
208 struct efx_nic *other, *next;
210 if (efx->primary == efx) {
211 /* Adding primary function; look for secondaries */
213 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
214 list_add_tail(&efx->node, &efx_primary_list);
216 list_for_each_entry_safe(other, next, &efx_unassociated_list,
217 node) {
218 if (efx_same_controller(efx, other)) {
219 list_del(&other->node);
220 netif_dbg(other, probe, other->net_dev,
221 "moving to secondary list of %s %s\n",
222 pci_name(efx->pci_dev),
223 efx->net_dev->name);
224 list_add_tail(&other->node,
225 &efx->secondary_list);
226 other->primary = efx;
229 } else {
230 /* Adding secondary function; look for primary */
232 list_for_each_entry(other, &efx_primary_list, node) {
233 if (efx_same_controller(efx, other)) {
234 netif_dbg(efx, probe, efx->net_dev,
235 "adding to secondary list of %s %s\n",
236 pci_name(other->pci_dev),
237 other->net_dev->name);
238 list_add_tail(&efx->node,
239 &other->secondary_list);
240 efx->primary = other;
241 return;
245 netif_dbg(efx, probe, efx->net_dev,
246 "adding to unassociated list\n");
247 list_add_tail(&efx->node, &efx_unassociated_list);
251 static void efx_dissociate(struct efx_nic *efx)
253 struct efx_nic *other, *next;
255 list_del(&efx->node);
256 efx->primary = NULL;
258 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
259 list_del(&other->node);
260 netif_dbg(other, probe, other->net_dev,
261 "moving to unassociated list\n");
262 list_add_tail(&other->node, &efx_unassociated_list);
263 other->primary = NULL;
267 static int efx_probe_nic(struct efx_nic *efx)
269 int rc;
271 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
273 /* Carry out hardware-type specific initialisation */
274 rc = efx->type->probe(efx);
275 if (rc)
276 return rc;
278 do {
279 if (!efx->max_channels || !efx->max_tx_channels) {
280 netif_err(efx, drv, efx->net_dev,
281 "Insufficient resources to allocate"
282 " any channels\n");
283 rc = -ENOSPC;
284 goto fail1;
287 /* Determine the number of channels and queues by trying
288 * to hook in MSI-X interrupts.
290 rc = efx_siena_probe_interrupts(efx);
291 if (rc)
292 goto fail1;
294 rc = efx_siena_set_channels(efx);
295 if (rc)
296 goto fail1;
298 /* dimension_resources can fail with EAGAIN */
299 rc = efx->type->dimension_resources(efx);
300 if (rc != 0 && rc != -EAGAIN)
301 goto fail2;
303 if (rc == -EAGAIN)
304 /* try again with new max_channels */
305 efx_siena_remove_interrupts(efx);
307 } while (rc == -EAGAIN);
309 if (efx->n_channels > 1)
310 netdev_rss_key_fill(efx->rss_context.rx_hash_key,
311 sizeof(efx->rss_context.rx_hash_key));
312 efx_siena_set_default_rx_indir_table(efx, &efx->rss_context);
314 /* Initialise the interrupt moderation settings */
315 efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
316 efx_siena_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec,
317 true, true);
319 return 0;
321 fail2:
322 efx_siena_remove_interrupts(efx);
323 fail1:
324 efx->type->remove(efx);
325 return rc;
328 static void efx_remove_nic(struct efx_nic *efx)
330 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
332 efx_siena_remove_interrupts(efx);
333 efx->type->remove(efx);
336 /**************************************************************************
338 * NIC startup/shutdown
340 *************************************************************************/
342 static int efx_probe_all(struct efx_nic *efx)
344 int rc;
346 rc = efx_probe_nic(efx);
347 if (rc) {
348 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
349 goto fail1;
352 rc = efx_probe_port(efx);
353 if (rc) {
354 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
355 goto fail2;
358 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
359 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
360 rc = -EINVAL;
361 goto fail3;
364 #ifdef CONFIG_SFC_SIENA_SRIOV
365 rc = efx->type->vswitching_probe(efx);
366 if (rc) /* not fatal; the PF will still work fine */
367 netif_warn(efx, probe, efx->net_dev,
368 "failed to setup vswitching rc=%d;"
369 " VFs may not function\n", rc);
370 #endif
372 rc = efx_siena_probe_filters(efx);
373 if (rc) {
374 netif_err(efx, probe, efx->net_dev,
375 "failed to create filter tables\n");
376 goto fail4;
379 rc = efx_siena_probe_channels(efx);
380 if (rc)
381 goto fail5;
383 return 0;
385 fail5:
386 efx_siena_remove_filters(efx);
387 fail4:
388 #ifdef CONFIG_SFC_SIENA_SRIOV
389 efx->type->vswitching_remove(efx);
390 #endif
391 fail3:
392 efx_remove_port(efx);
393 fail2:
394 efx_remove_nic(efx);
395 fail1:
396 return rc;
399 static void efx_remove_all(struct efx_nic *efx)
401 rtnl_lock();
402 efx_xdp_setup_prog(efx, NULL);
403 rtnl_unlock();
405 efx_siena_remove_channels(efx);
406 efx_siena_remove_filters(efx);
407 #ifdef CONFIG_SFC_SIENA_SRIOV
408 efx->type->vswitching_remove(efx);
409 #endif
410 efx_remove_port(efx);
411 efx_remove_nic(efx);
414 /**************************************************************************
416 * Interrupt moderation
418 **************************************************************************/
419 unsigned int efx_siena_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
421 if (usecs == 0)
422 return 0;
423 if (usecs * 1000 < efx->timer_quantum_ns)
424 return 1; /* never round down to 0 */
425 return usecs * 1000 / efx->timer_quantum_ns;
428 /* Set interrupt moderation parameters */
429 int efx_siena_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
430 unsigned int rx_usecs, bool rx_adaptive,
431 bool rx_may_override_tx)
433 struct efx_channel *channel;
434 unsigned int timer_max_us;
436 EFX_ASSERT_RESET_SERIALISED(efx);
438 timer_max_us = efx->timer_max_ns / 1000;
440 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
441 return -EINVAL;
443 if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
444 !rx_may_override_tx) {
445 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
446 "RX and TX IRQ moderation must be equal\n");
447 return -EINVAL;
450 efx->irq_rx_adaptive = rx_adaptive;
451 efx->irq_rx_moderation_us = rx_usecs;
452 efx_for_each_channel(channel, efx) {
453 if (efx_channel_has_rx_queue(channel))
454 channel->irq_moderation_us = rx_usecs;
455 else if (efx_channel_has_tx_queues(channel))
456 channel->irq_moderation_us = tx_usecs;
457 else if (efx_channel_is_xdp_tx(channel))
458 channel->irq_moderation_us = tx_usecs;
461 return 0;
464 void efx_siena_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
465 unsigned int *rx_usecs, bool *rx_adaptive)
467 *rx_adaptive = efx->irq_rx_adaptive;
468 *rx_usecs = efx->irq_rx_moderation_us;
470 /* If channels are shared between RX and TX, so is IRQ
471 * moderation. Otherwise, IRQ moderation is the same for all
472 * TX channels and is not adaptive.
474 if (efx->tx_channel_offset == 0) {
475 *tx_usecs = *rx_usecs;
476 } else {
477 struct efx_channel *tx_channel;
479 tx_channel = efx->channel[efx->tx_channel_offset];
480 *tx_usecs = tx_channel->irq_moderation_us;
484 /**************************************************************************
486 * ioctls
488 *************************************************************************/
490 /* Net device ioctl
491 * Context: process, rtnl_lock() held.
493 static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
495 struct efx_nic *efx = netdev_priv(net_dev);
496 struct mii_ioctl_data *data = if_mii(ifr);
498 /* Convert phy_id from older PRTAD/DEVAD format */
499 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
500 (data->phy_id & 0xfc00) == 0x0400)
501 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
503 return mdio_mii_ioctl(&efx->mdio, data, cmd);
506 /**************************************************************************
508 * Kernel net device interface
510 *************************************************************************/
512 /* Context: process, rtnl_lock() held. */
513 static int efx_net_open(struct net_device *net_dev)
515 struct efx_nic *efx = netdev_priv(net_dev);
516 int rc;
518 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
519 raw_smp_processor_id());
521 rc = efx_check_disabled(efx);
522 if (rc)
523 return rc;
524 if (efx->phy_mode & PHY_MODE_SPECIAL)
525 return -EBUSY;
526 if (efx_siena_mcdi_poll_reboot(efx) && efx_siena_reset(efx, RESET_TYPE_ALL))
527 return -EIO;
529 /* Notify the kernel of the link state polled during driver load,
530 * before the monitor starts running */
531 efx_siena_link_status_changed(efx);
533 efx_siena_start_all(efx);
534 if (efx->state == STATE_DISABLED || efx->reset_pending)
535 netif_device_detach(efx->net_dev);
536 efx_siena_selftest_async_start(efx);
537 return 0;
540 /* Context: process, rtnl_lock() held.
541 * Note that the kernel will ignore our return code; this method
542 * should really be a void.
544 static int efx_net_stop(struct net_device *net_dev)
546 struct efx_nic *efx = netdev_priv(net_dev);
548 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
549 raw_smp_processor_id());
551 /* Stop the device and flush all the channels */
552 efx_siena_stop_all(efx);
554 return 0;
557 static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
559 struct efx_nic *efx = netdev_priv(net_dev);
561 if (efx->type->vlan_rx_add_vid)
562 return efx->type->vlan_rx_add_vid(efx, proto, vid);
563 else
564 return -EOPNOTSUPP;
567 static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
569 struct efx_nic *efx = netdev_priv(net_dev);
571 if (efx->type->vlan_rx_kill_vid)
572 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
573 else
574 return -EOPNOTSUPP;
577 static int efx_siena_hwtstamp_set(struct net_device *net_dev,
578 struct kernel_hwtstamp_config *config,
579 struct netlink_ext_ack *extack)
581 struct efx_nic *efx = netdev_priv(net_dev);
583 return efx_siena_ptp_set_ts_config(efx, config, extack);
586 static int efx_siena_hwtstamp_get(struct net_device *net_dev,
587 struct kernel_hwtstamp_config *config)
589 struct efx_nic *efx = netdev_priv(net_dev);
591 return efx_siena_ptp_get_ts_config(efx, config);
594 static const struct net_device_ops efx_netdev_ops = {
595 .ndo_open = efx_net_open,
596 .ndo_stop = efx_net_stop,
597 .ndo_get_stats64 = efx_siena_net_stats,
598 .ndo_tx_timeout = efx_siena_watchdog,
599 .ndo_start_xmit = efx_siena_hard_start_xmit,
600 .ndo_validate_addr = eth_validate_addr,
601 .ndo_eth_ioctl = efx_ioctl,
602 .ndo_change_mtu = efx_siena_change_mtu,
603 .ndo_set_mac_address = efx_siena_set_mac_address,
604 .ndo_set_rx_mode = efx_siena_set_rx_mode,
605 .ndo_set_features = efx_siena_set_features,
606 .ndo_features_check = efx_siena_features_check,
607 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
608 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
609 .ndo_hwtstamp_set = efx_siena_hwtstamp_set,
610 .ndo_hwtstamp_get = efx_siena_hwtstamp_get,
611 #ifdef CONFIG_SFC_SIENA_SRIOV
612 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
613 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
614 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
615 .ndo_get_vf_config = efx_sriov_get_vf_config,
616 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
617 #endif
618 .ndo_get_phys_port_id = efx_siena_get_phys_port_id,
619 .ndo_get_phys_port_name = efx_siena_get_phys_port_name,
620 .ndo_setup_tc = efx_siena_setup_tc,
621 #ifdef CONFIG_RFS_ACCEL
622 .ndo_rx_flow_steer = efx_siena_filter_rfs,
623 #endif
624 .ndo_xdp_xmit = efx_xdp_xmit,
625 .ndo_bpf = efx_xdp
628 static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
630 struct bpf_prog *old_prog;
632 if (efx->xdp_rxq_info_failed) {
633 netif_err(efx, drv, efx->net_dev,
634 "Unable to bind XDP program due to previous failure of rxq_info\n");
635 return -EINVAL;
638 if (prog && efx->net_dev->mtu > efx_siena_xdp_max_mtu(efx)) {
639 netif_err(efx, drv, efx->net_dev,
640 "Unable to configure XDP with MTU of %d (max: %d)\n",
641 efx->net_dev->mtu, efx_siena_xdp_max_mtu(efx));
642 return -EINVAL;
645 old_prog = rtnl_dereference(efx->xdp_prog);
646 rcu_assign_pointer(efx->xdp_prog, prog);
647 /* Release the reference that was originally passed by the caller. */
648 if (old_prog)
649 bpf_prog_put(old_prog);
651 return 0;
654 /* Context: process, rtnl_lock() held. */
655 static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
657 struct efx_nic *efx = netdev_priv(dev);
659 switch (xdp->command) {
660 case XDP_SETUP_PROG:
661 return efx_xdp_setup_prog(efx, xdp->prog);
662 default:
663 return -EINVAL;
667 static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
668 u32 flags)
670 struct efx_nic *efx = netdev_priv(dev);
672 if (!netif_running(dev))
673 return -EINVAL;
675 return efx_siena_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH);
678 static void efx_update_name(struct efx_nic *efx)
680 strcpy(efx->name, efx->net_dev->name);
681 efx_siena_mtd_rename(efx);
682 efx_siena_set_channel_names(efx);
685 static int efx_netdev_event(struct notifier_block *this,
686 unsigned long event, void *ptr)
688 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
690 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
691 event == NETDEV_CHANGENAME)
692 efx_update_name(netdev_priv(net_dev));
694 return NOTIFY_DONE;
697 static struct notifier_block efx_netdev_notifier = {
698 .notifier_call = efx_netdev_event,
701 static ssize_t phy_type_show(struct device *dev,
702 struct device_attribute *attr, char *buf)
704 struct efx_nic *efx = dev_get_drvdata(dev);
705 return sprintf(buf, "%d\n", efx->phy_type);
707 static DEVICE_ATTR_RO(phy_type);
709 static int efx_register_netdev(struct efx_nic *efx)
711 struct net_device *net_dev = efx->net_dev;
712 struct efx_channel *channel;
713 int rc;
715 net_dev->watchdog_timeo = 5 * HZ;
716 net_dev->irq = efx->pci_dev->irq;
717 net_dev->netdev_ops = &efx_netdev_ops;
718 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
719 net_dev->priv_flags |= IFF_UNICAST_FLT;
720 net_dev->ethtool_ops = &efx_siena_ethtool_ops;
721 netif_set_tso_max_segs(net_dev, EFX_TSO_MAX_SEGS);
722 net_dev->min_mtu = EFX_MIN_MTU;
723 net_dev->max_mtu = EFX_MAX_MTU;
725 rtnl_lock();
727 /* Enable resets to be scheduled and check whether any were
728 * already requested. If so, the NIC is probably hosed so we
729 * abort.
731 efx->state = STATE_READY;
732 smp_mb(); /* ensure we change state before checking reset_pending */
733 if (efx->reset_pending) {
734 pci_err(efx->pci_dev, "aborting probe due to scheduled reset\n");
735 rc = -EIO;
736 goto fail_locked;
739 rc = dev_alloc_name(net_dev, net_dev->name);
740 if (rc < 0)
741 goto fail_locked;
742 efx_update_name(efx);
744 /* Always start with carrier off; PHY events will detect the link */
745 netif_carrier_off(net_dev);
747 rc = register_netdevice(net_dev);
748 if (rc)
749 goto fail_locked;
751 efx_for_each_channel(channel, efx) {
752 struct efx_tx_queue *tx_queue;
753 efx_for_each_channel_tx_queue(tx_queue, channel)
754 efx_siena_init_tx_queue_core_txq(tx_queue);
757 efx_associate(efx);
759 rtnl_unlock();
761 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
762 if (rc) {
763 netif_err(efx, drv, efx->net_dev,
764 "failed to init net dev attributes\n");
765 goto fail_registered;
768 efx_siena_init_mcdi_logging(efx);
770 return 0;
772 fail_registered:
773 rtnl_lock();
774 efx_dissociate(efx);
775 unregister_netdevice(net_dev);
776 fail_locked:
777 efx->state = STATE_UNINIT;
778 rtnl_unlock();
779 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
780 return rc;
783 static void efx_unregister_netdev(struct efx_nic *efx)
785 if (!efx->net_dev)
786 return;
788 BUG_ON(netdev_priv(efx->net_dev) != efx);
790 if (efx_dev_registered(efx)) {
791 strscpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
792 efx_siena_fini_mcdi_logging(efx);
793 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
794 unregister_netdev(efx->net_dev);
798 /**************************************************************************
800 * List of NICs we support
802 **************************************************************************/
804 /* PCI device ID table */
805 static const struct pci_device_id efx_pci_table[] = {
806 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
807 .driver_data = (unsigned long)&siena_a0_nic_type},
808 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
809 .driver_data = (unsigned long)&siena_a0_nic_type},
810 {0} /* end of list */
813 /**************************************************************************
815 * Data housekeeping
817 **************************************************************************/
819 void efx_siena_update_sw_stats(struct efx_nic *efx, u64 *stats)
821 u64 n_rx_nodesc_trunc = 0;
822 struct efx_channel *channel;
824 efx_for_each_channel(channel, efx)
825 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
826 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
827 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
830 /**************************************************************************
832 * PCI interface
834 **************************************************************************/
836 /* Main body of final NIC shutdown code
837 * This is called only at module unload (or hotplug removal).
839 static void efx_pci_remove_main(struct efx_nic *efx)
841 /* Flush reset_work. It can no longer be scheduled since we
842 * are not READY.
844 BUG_ON(efx->state == STATE_READY);
845 efx_siena_flush_reset_workqueue(efx);
847 efx_siena_disable_interrupts(efx);
848 efx_siena_clear_interrupt_affinity(efx);
849 efx_siena_fini_interrupt(efx);
850 efx_fini_port(efx);
851 efx->type->fini(efx);
852 efx_siena_fini_napi(efx);
853 efx_remove_all(efx);
856 /* Final NIC shutdown
857 * This is called only at module unload (or hotplug removal). A PF can call
858 * this on its VFs to ensure they are unbound first.
860 static void efx_pci_remove(struct pci_dev *pci_dev)
862 struct efx_nic *efx;
864 efx = pci_get_drvdata(pci_dev);
865 if (!efx)
866 return;
868 /* Mark the NIC as fini, then stop the interface */
869 rtnl_lock();
870 efx_dissociate(efx);
871 dev_close(efx->net_dev);
872 efx_siena_disable_interrupts(efx);
873 efx->state = STATE_UNINIT;
874 rtnl_unlock();
876 if (efx->type->sriov_fini)
877 efx->type->sriov_fini(efx);
879 efx_unregister_netdev(efx);
881 efx_siena_mtd_remove(efx);
883 efx_pci_remove_main(efx);
885 efx_siena_fini_io(efx);
886 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
888 efx_siena_fini_struct(efx);
889 free_netdev(efx->net_dev);
892 /* NIC VPD information
893 * Called during probe to display the part number of the
894 * installed NIC.
896 static void efx_probe_vpd_strings(struct efx_nic *efx)
898 struct pci_dev *dev = efx->pci_dev;
899 unsigned int vpd_size, kw_len;
900 u8 *vpd_data;
901 int start;
903 vpd_data = pci_vpd_alloc(dev, &vpd_size);
904 if (IS_ERR(vpd_data)) {
905 pci_warn(dev, "Unable to read VPD\n");
906 return;
909 start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
910 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
911 if (start < 0)
912 pci_err(dev, "Part number not found or incomplete\n");
913 else
914 pci_info(dev, "Part Number : %.*s\n", kw_len, vpd_data + start);
916 start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
917 PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);
918 if (start < 0)
919 pci_err(dev, "Serial number not found or incomplete\n");
920 else
921 efx->vpd_sn = kmemdup_nul(vpd_data + start, kw_len, GFP_KERNEL);
923 kfree(vpd_data);
927 /* Main body of NIC initialisation
928 * This is called at module load (or hotplug insertion, theoretically).
930 static int efx_pci_probe_main(struct efx_nic *efx)
932 int rc;
934 /* Do start-of-day initialisation */
935 rc = efx_probe_all(efx);
936 if (rc)
937 goto fail1;
939 efx_siena_init_napi(efx);
941 down_write(&efx->filter_sem);
942 rc = efx->type->init(efx);
943 up_write(&efx->filter_sem);
944 if (rc) {
945 pci_err(efx->pci_dev, "failed to initialise NIC\n");
946 goto fail3;
949 rc = efx_init_port(efx);
950 if (rc) {
951 netif_err(efx, probe, efx->net_dev,
952 "failed to initialise port\n");
953 goto fail4;
956 rc = efx_siena_init_interrupt(efx);
957 if (rc)
958 goto fail5;
960 efx_siena_set_interrupt_affinity(efx);
961 rc = efx_siena_enable_interrupts(efx);
962 if (rc)
963 goto fail6;
965 return 0;
967 fail6:
968 efx_siena_clear_interrupt_affinity(efx);
969 efx_siena_fini_interrupt(efx);
970 fail5:
971 efx_fini_port(efx);
972 fail4:
973 efx->type->fini(efx);
974 fail3:
975 efx_siena_fini_napi(efx);
976 efx_remove_all(efx);
977 fail1:
978 return rc;
981 static int efx_pci_probe_post_io(struct efx_nic *efx)
983 struct net_device *net_dev = efx->net_dev;
984 int rc = efx_pci_probe_main(efx);
986 if (rc)
987 return rc;
989 if (efx->type->sriov_init) {
990 rc = efx->type->sriov_init(efx);
991 if (rc)
992 pci_err(efx->pci_dev, "SR-IOV can't be enabled rc %d\n",
993 rc);
996 /* Determine netdevice features */
997 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
998 NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
999 if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
1000 net_dev->features |= NETIF_F_TSO6;
1001 /* Check whether device supports TSO */
1002 if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
1003 net_dev->features &= ~NETIF_F_ALL_TSO;
1004 /* Mask for features that also apply to VLAN devices */
1005 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
1006 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
1007 NETIF_F_RXCSUM);
1009 net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
1011 /* Disable receiving frames with bad FCS, by default. */
1012 net_dev->features &= ~NETIF_F_RXALL;
1014 /* Disable VLAN filtering by default. It may be enforced if
1015 * the feature is fixed (i.e. VLAN filters are required to
1016 * receive VLAN tagged packets due to vPort restrictions).
1018 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1019 net_dev->features |= efx->fixed_features;
1021 net_dev->xdp_features = NETDEV_XDP_ACT_BASIC |
1022 NETDEV_XDP_ACT_REDIRECT |
1023 NETDEV_XDP_ACT_NDO_XMIT;
1025 rc = efx_register_netdev(efx);
1026 if (!rc)
1027 return 0;
1029 efx_pci_remove_main(efx);
1030 return rc;
1033 /* NIC initialisation
1035 * This is called at module load (or hotplug insertion,
1036 * theoretically). It sets up PCI mappings, resets the NIC,
1037 * sets up and registers the network devices with the kernel and hooks
1038 * the interrupt service routine. It does not prepare the device for
1039 * transmission; this is left to the first time one of the network
1040 * interfaces is brought up (i.e. efx_net_open).
1042 static int efx_pci_probe(struct pci_dev *pci_dev,
1043 const struct pci_device_id *entry)
1045 struct net_device *net_dev;
1046 struct efx_nic *efx;
1047 int rc;
1049 /* Allocate and initialise a struct net_device and struct efx_nic */
1050 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
1051 EFX_MAX_RX_QUEUES);
1052 if (!net_dev)
1053 return -ENOMEM;
1054 efx = netdev_priv(net_dev);
1055 efx->type = (const struct efx_nic_type *) entry->driver_data;
1056 efx->fixed_features |= NETIF_F_HIGHDMA;
1058 pci_set_drvdata(pci_dev, efx);
1059 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
1060 rc = efx_siena_init_struct(efx, pci_dev, net_dev);
1061 if (rc)
1062 goto fail1;
1064 pci_info(pci_dev, "Solarflare NIC detected\n");
1066 if (!efx->type->is_vf)
1067 efx_probe_vpd_strings(efx);
1069 /* Set up basic I/O (BAR mappings etc) */
1070 rc = efx_siena_init_io(efx, efx->type->mem_bar(efx),
1071 efx->type->max_dma_mask,
1072 efx->type->mem_map_size(efx));
1073 if (rc)
1074 goto fail2;
1076 rc = efx_pci_probe_post_io(efx);
1077 if (rc) {
1078 /* On failure, retry once immediately.
1079 * If we aborted probe due to a scheduled reset, dismiss it.
1081 efx->reset_pending = 0;
1082 rc = efx_pci_probe_post_io(efx);
1083 if (rc) {
1084 /* On another failure, retry once more
1085 * after a 50-305ms delay.
1087 unsigned char r;
1089 get_random_bytes(&r, 1);
1090 msleep((unsigned int)r + 50);
1091 efx->reset_pending = 0;
1092 rc = efx_pci_probe_post_io(efx);
1095 if (rc)
1096 goto fail3;
1098 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
1100 /* Try to create MTDs, but allow this to fail */
1101 rtnl_lock();
1102 rc = efx_mtd_probe(efx);
1103 rtnl_unlock();
1104 if (rc && rc != -EPERM)
1105 netif_warn(efx, probe, efx->net_dev,
1106 "failed to create MTDs (%d)\n", rc);
1108 if (efx->type->udp_tnl_push_ports)
1109 efx->type->udp_tnl_push_ports(efx);
1111 return 0;
1113 fail3:
1114 efx_siena_fini_io(efx);
1115 fail2:
1116 efx_siena_fini_struct(efx);
1117 fail1:
1118 WARN_ON(rc > 0);
1119 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
1120 free_netdev(net_dev);
1121 return rc;
1124 /* efx_pci_sriov_configure returns the actual number of Virtual Functions
1125 * enabled on success
1127 #ifdef CONFIG_SFC_SIENA_SRIOV
1128 static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1130 int rc;
1131 struct efx_nic *efx = pci_get_drvdata(dev);
1133 if (efx->type->sriov_configure) {
1134 rc = efx->type->sriov_configure(efx, num_vfs);
1135 if (rc)
1136 return rc;
1137 else
1138 return num_vfs;
1139 } else
1140 return -EOPNOTSUPP;
1142 #endif
1144 static int efx_pm_freeze(struct device *dev)
1146 struct efx_nic *efx = dev_get_drvdata(dev);
1148 rtnl_lock();
1150 if (efx->state != STATE_DISABLED) {
1151 efx->state = STATE_UNINIT;
1153 efx_device_detach_sync(efx);
1155 efx_siena_stop_all(efx);
1156 efx_siena_disable_interrupts(efx);
1159 rtnl_unlock();
1161 return 0;
1164 static void efx_pci_shutdown(struct pci_dev *pci_dev)
1166 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1168 if (!efx)
1169 return;
1171 efx_pm_freeze(&pci_dev->dev);
1172 pci_disable_device(pci_dev);
1175 static int efx_pm_thaw(struct device *dev)
1177 int rc;
1178 struct efx_nic *efx = dev_get_drvdata(dev);
1180 rtnl_lock();
1182 if (efx->state != STATE_DISABLED) {
1183 rc = efx_siena_enable_interrupts(efx);
1184 if (rc)
1185 goto fail;
1187 mutex_lock(&efx->mac_lock);
1188 efx_siena_mcdi_port_reconfigure(efx);
1189 mutex_unlock(&efx->mac_lock);
1191 efx_siena_start_all(efx);
1193 efx_device_attach_if_not_resetting(efx);
1195 efx->state = STATE_READY;
1197 efx->type->resume_wol(efx);
1200 rtnl_unlock();
1202 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
1203 efx_siena_queue_reset_work(efx);
1205 return 0;
1207 fail:
1208 rtnl_unlock();
1210 return rc;
1213 static int efx_pm_poweroff(struct device *dev)
1215 struct pci_dev *pci_dev = to_pci_dev(dev);
1216 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1218 efx->type->fini(efx);
1220 efx->reset_pending = 0;
1222 pci_save_state(pci_dev);
1223 return pci_set_power_state(pci_dev, PCI_D3hot);
1226 /* Used for both resume and restore */
1227 static int efx_pm_resume(struct device *dev)
1229 struct pci_dev *pci_dev = to_pci_dev(dev);
1230 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1231 int rc;
1233 rc = pci_set_power_state(pci_dev, PCI_D0);
1234 if (rc)
1235 return rc;
1236 pci_restore_state(pci_dev);
1237 rc = pci_enable_device(pci_dev);
1238 if (rc)
1239 return rc;
1240 pci_set_master(efx->pci_dev);
1241 rc = efx->type->reset(efx, RESET_TYPE_ALL);
1242 if (rc)
1243 return rc;
1244 down_write(&efx->filter_sem);
1245 rc = efx->type->init(efx);
1246 up_write(&efx->filter_sem);
1247 if (rc)
1248 return rc;
1249 rc = efx_pm_thaw(dev);
1250 return rc;
1253 static int efx_pm_suspend(struct device *dev)
1255 int rc;
1257 efx_pm_freeze(dev);
1258 rc = efx_pm_poweroff(dev);
1259 if (rc)
1260 efx_pm_resume(dev);
1261 return rc;
1264 static const struct dev_pm_ops efx_pm_ops = {
1265 .suspend = efx_pm_suspend,
1266 .resume = efx_pm_resume,
1267 .freeze = efx_pm_freeze,
1268 .thaw = efx_pm_thaw,
1269 .poweroff = efx_pm_poweroff,
1270 .restore = efx_pm_resume,
1273 static struct pci_driver efx_pci_driver = {
1274 .name = KBUILD_MODNAME,
1275 .id_table = efx_pci_table,
1276 .probe = efx_pci_probe,
1277 .remove = efx_pci_remove,
1278 .driver.pm = &efx_pm_ops,
1279 .shutdown = efx_pci_shutdown,
1280 .err_handler = &efx_siena_err_handlers,
1281 #ifdef CONFIG_SFC_SIENA_SRIOV
1282 .sriov_configure = efx_pci_sriov_configure,
1283 #endif
1286 /**************************************************************************
1288 * Kernel module interface
1290 *************************************************************************/
1292 static int __init efx_init_module(void)
1294 int rc;
1296 pr_info("Solarflare Siena driver\n");
1298 rc = register_netdevice_notifier(&efx_netdev_notifier);
1299 if (rc)
1300 goto err_notifier;
1302 #ifdef CONFIG_SFC_SIENA_SRIOV
1303 rc = efx_init_sriov();
1304 if (rc)
1305 goto err_sriov;
1306 #endif
1308 rc = efx_siena_create_reset_workqueue();
1309 if (rc)
1310 goto err_reset;
1312 rc = pci_register_driver(&efx_pci_driver);
1313 if (rc < 0)
1314 goto err_pci;
1316 return 0;
1318 err_pci:
1319 efx_siena_destroy_reset_workqueue();
1320 err_reset:
1321 #ifdef CONFIG_SFC_SIENA_SRIOV
1322 efx_fini_sriov();
1323 err_sriov:
1324 #endif
1325 unregister_netdevice_notifier(&efx_netdev_notifier);
1326 err_notifier:
1327 return rc;
1330 static void __exit efx_exit_module(void)
1332 pr_info("Solarflare Siena driver unloading\n");
1334 pci_unregister_driver(&efx_pci_driver);
1335 efx_siena_destroy_reset_workqueue();
1336 #ifdef CONFIG_SFC_SIENA_SRIOV
1337 efx_fini_sriov();
1338 #endif
1339 unregister_netdevice_notifier(&efx_netdev_notifier);
1343 module_init(efx_init_module);
1344 module_exit(efx_exit_module);
1346 MODULE_AUTHOR("Solarflare Communications and "
1347 "Michael Brown <mbrown@fensystems.co.uk>");
1348 MODULE_DESCRIPTION("Solarflare Siena network driver");
1349 MODULE_LICENSE("GPL");
1350 MODULE_DEVICE_TABLE(pci, efx_pci_table);