1 /****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2005-2008 Solarflare Communications Inc.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/delay.h>
16 #include <linux/notifier.h>
18 #include <linux/tcp.h>
20 #include <linux/crc32.h>
21 #include <linux/ethtool.h>
22 #include <linux/topology.h>
23 #include "net_driver.h"
31 #define EFX_MAX_MTU (9 * 1024)
33 /* RX slow fill workqueue. If memory allocation fails in the fast path,
34 * a work item is pushed onto this work queue to retry the allocation later,
35 * to avoid the NIC being starved of RX buffers. Since this is a per cpu
36 * workqueue, there is nothing to be gained in making it per NIC
38 static struct workqueue_struct *refill_workqueue;
40 /* Reset workqueue. If any NIC has a hardware failure then a reset will be
41 * queued onto this work queue. This is not a per-nic work queue, because
42 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
44 static struct workqueue_struct *reset_workqueue;
46 /**************************************************************************
50 *************************************************************************/
53 * Use separate channels for TX and RX events
55 * Set this to 1 to use separate channels for TX and RX. It allows us
56 * to control interrupt affinity separately for TX and RX.
58 * This is only used in MSI-X interrupt mode
60 static unsigned int separate_tx_channels;
61 module_param(separate_tx_channels, uint, 0644);
62 MODULE_PARM_DESC(separate_tx_channels,
63 "Use separate channels for TX and RX");
65 /* This is the weight assigned to each of the (per-channel) virtual
68 static int napi_weight = 64;
70 /* This is the time (in jiffies) between invocations of the hardware
71 * monitor, which checks for known hardware bugs and resets the
72 * hardware and driver as necessary.
74 unsigned int efx_monitor_interval = 1 * HZ;
76 /* This controls whether or not the driver will initialise devices
77 * with invalid MAC addresses stored in the EEPROM or flash. If true,
78 * such devices will be initialised with a random locally-generated
79 * MAC address. This allows for loading the sfc_mtd driver to
80 * reprogram the flash, even if the flash contents (including the MAC
81 * address) have previously been erased.
83 static unsigned int allow_bad_hwaddr;
85 /* Initial interrupt moderation settings. They can be modified after
86 * module load with ethtool.
88 * The default for RX should strike a balance between increasing the
89 * round-trip latency and reducing overhead.
91 static unsigned int rx_irq_mod_usec = 60;
93 /* Initial interrupt moderation settings. They can be modified after
94 * module load with ethtool.
96 * This default is chosen to ensure that a 10G link does not go idle
97 * while a TX queue is stopped after it has become full. A queue is
98 * restarted when it drops below half full. The time this takes (assuming
99 * worst case 3 descriptors per packet and 1024 descriptors) is
100 * 512 / 3 * 1.2 = 205 usec.
102 static unsigned int tx_irq_mod_usec = 150;
104 /* This is the first interrupt mode to try out of:
109 static unsigned int interrupt_mode;
111 /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
112 * i.e. the number of CPUs among which we may distribute simultaneous
113 * interrupt handling.
115 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
116 * The default (0) means to assign an interrupt to each package (level II cache)
118 static unsigned int rss_cpus;
119 module_param(rss_cpus, uint, 0444);
120 MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
122 static int phy_flash_cfg;
123 module_param(phy_flash_cfg, int, 0644);
124 MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
126 static unsigned irq_adapt_low_thresh = 10000;
127 module_param(irq_adapt_low_thresh, uint, 0644);
128 MODULE_PARM_DESC(irq_adapt_low_thresh,
129 "Threshold score for reducing IRQ moderation");
131 static unsigned irq_adapt_high_thresh = 20000;
132 module_param(irq_adapt_high_thresh, uint, 0644);
133 MODULE_PARM_DESC(irq_adapt_high_thresh,
134 "Threshold score for increasing IRQ moderation");
136 /**************************************************************************
138 * Utility functions and prototypes
140 *************************************************************************/
141 static void efx_remove_channel(struct efx_channel *channel);
142 static void efx_remove_port(struct efx_nic *efx);
143 static void efx_fini_napi(struct efx_nic *efx);
144 static void efx_fini_channels(struct efx_nic *efx);
146 #define EFX_ASSERT_RESET_SERIALISED(efx) \
148 if (efx->state == STATE_RUNNING) \
152 /**************************************************************************
154 * Event queue processing
156 *************************************************************************/
158 /* Process channel's event queue
160 * This function is responsible for processing the event queue of a
161 * single channel. The caller must guarantee that this function will
162 * never be concurrently called more than once on the same channel,
163 * though different channels may be being processed concurrently.
165 static int efx_process_channel(struct efx_channel *channel, int rx_quota)
167 struct efx_nic *efx = channel->efx;
170 if (unlikely(efx->reset_pending != RESET_TYPE_NONE ||
174 rx_packets = falcon_process_eventq(channel, rx_quota);
178 /* Deliver last RX packet. */
179 if (channel->rx_pkt) {
180 __efx_rx_packet(channel, channel->rx_pkt,
181 channel->rx_pkt_csummed);
182 channel->rx_pkt = NULL;
185 efx_rx_strategy(channel);
187 efx_fast_push_rx_descriptors(&efx->rx_queue[channel->channel]);
192 /* Mark channel as finished processing
194 * Note that since we will not receive further interrupts for this
195 * channel before we finish processing and call the eventq_read_ack()
196 * method, there is no need to use the interrupt hold-off timers.
198 static inline void efx_channel_processed(struct efx_channel *channel)
200 /* The interrupt handler for this channel may set work_pending
201 * as soon as we acknowledge the events we've seen. Make sure
202 * it's cleared before then. */
203 channel->work_pending = false;
206 falcon_eventq_read_ack(channel);
211 * NAPI guarantees serialisation of polls of the same device, which
212 * provides the guarantee required by efx_process_channel().
214 static int efx_poll(struct napi_struct *napi, int budget)
216 struct efx_channel *channel =
217 container_of(napi, struct efx_channel, napi_str);
220 EFX_TRACE(channel->efx, "channel %d NAPI poll executing on CPU %d\n",
221 channel->channel, raw_smp_processor_id());
223 rx_packets = efx_process_channel(channel, budget);
225 if (rx_packets < budget) {
226 struct efx_nic *efx = channel->efx;
228 if (channel->used_flags & EFX_USED_BY_RX &&
229 efx->irq_rx_adaptive &&
230 unlikely(++channel->irq_count == 1000)) {
231 unsigned old_irq_moderation = channel->irq_moderation;
233 if (unlikely(channel->irq_mod_score <
234 irq_adapt_low_thresh)) {
235 channel->irq_moderation =
237 channel->irq_moderation -
238 FALCON_IRQ_MOD_RESOLUTION,
239 FALCON_IRQ_MOD_RESOLUTION);
240 } else if (unlikely(channel->irq_mod_score >
241 irq_adapt_high_thresh)) {
242 channel->irq_moderation =
243 min(channel->irq_moderation +
244 FALCON_IRQ_MOD_RESOLUTION,
245 efx->irq_rx_moderation);
248 if (channel->irq_moderation != old_irq_moderation)
249 falcon_set_int_moderation(channel);
251 channel->irq_count = 0;
252 channel->irq_mod_score = 0;
255 /* There is no race here; although napi_disable() will
256 * only wait for napi_complete(), this isn't a problem
257 * since efx_channel_processed() will have no effect if
258 * interrupts have already been disabled.
261 efx_channel_processed(channel);
267 /* Process the eventq of the specified channel immediately on this CPU
269 * Disable hardware generated interrupts, wait for any existing
270 * processing to finish, then directly poll (and ack ) the eventq.
271 * Finally reenable NAPI and interrupts.
273 * Since we are touching interrupts the caller should hold the suspend lock
275 void efx_process_channel_now(struct efx_channel *channel)
277 struct efx_nic *efx = channel->efx;
279 BUG_ON(!channel->used_flags);
280 BUG_ON(!channel->enabled);
282 /* Disable interrupts and wait for ISRs to complete */
283 falcon_disable_interrupts(efx);
285 synchronize_irq(efx->legacy_irq);
287 synchronize_irq(channel->irq);
289 /* Wait for any NAPI processing to complete */
290 napi_disable(&channel->napi_str);
292 /* Poll the channel */
293 efx_process_channel(channel, efx->type->evq_size);
295 /* Ack the eventq. This may cause an interrupt to be generated
296 * when they are reenabled */
297 efx_channel_processed(channel);
299 napi_enable(&channel->napi_str);
300 falcon_enable_interrupts(efx);
303 /* Create event queue
304 * Event queue memory allocations are done only once. If the channel
305 * is reset, the memory buffer will be reused; this guards against
306 * errors during channel reset and also simplifies interrupt handling.
308 static int efx_probe_eventq(struct efx_channel *channel)
310 EFX_LOG(channel->efx, "chan %d create event queue\n", channel->channel);
312 return falcon_probe_eventq(channel);
315 /* Prepare channel's event queue */
316 static void efx_init_eventq(struct efx_channel *channel)
318 EFX_LOG(channel->efx, "chan %d init event queue\n", channel->channel);
320 channel->eventq_read_ptr = 0;
322 falcon_init_eventq(channel);
325 static void efx_fini_eventq(struct efx_channel *channel)
327 EFX_LOG(channel->efx, "chan %d fini event queue\n", channel->channel);
329 falcon_fini_eventq(channel);
332 static void efx_remove_eventq(struct efx_channel *channel)
334 EFX_LOG(channel->efx, "chan %d remove event queue\n", channel->channel);
336 falcon_remove_eventq(channel);
339 /**************************************************************************
343 *************************************************************************/
345 static int efx_probe_channel(struct efx_channel *channel)
347 struct efx_tx_queue *tx_queue;
348 struct efx_rx_queue *rx_queue;
351 EFX_LOG(channel->efx, "creating channel %d\n", channel->channel);
353 rc = efx_probe_eventq(channel);
357 efx_for_each_channel_tx_queue(tx_queue, channel) {
358 rc = efx_probe_tx_queue(tx_queue);
363 efx_for_each_channel_rx_queue(rx_queue, channel) {
364 rc = efx_probe_rx_queue(rx_queue);
369 channel->n_rx_frm_trunc = 0;
374 efx_for_each_channel_rx_queue(rx_queue, channel)
375 efx_remove_rx_queue(rx_queue);
377 efx_for_each_channel_tx_queue(tx_queue, channel)
378 efx_remove_tx_queue(tx_queue);
384 static void efx_set_channel_names(struct efx_nic *efx)
386 struct efx_channel *channel;
387 const char *type = "";
390 efx_for_each_channel(channel, efx) {
391 number = channel->channel;
392 if (efx->n_channels > efx->n_rx_queues) {
393 if (channel->channel < efx->n_rx_queues) {
397 number -= efx->n_rx_queues;
400 snprintf(channel->name, sizeof(channel->name),
401 "%s%s-%d", efx->name, type, number);
405 /* Channels are shutdown and reinitialised whilst the NIC is running
406 * to propagate configuration changes (mtu, checksum offload), or
407 * to clear hardware error conditions
409 static void efx_init_channels(struct efx_nic *efx)
411 struct efx_tx_queue *tx_queue;
412 struct efx_rx_queue *rx_queue;
413 struct efx_channel *channel;
415 /* Calculate the rx buffer allocation parameters required to
416 * support the current MTU, including padding for header
417 * alignment and overruns.
419 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
420 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
421 efx->type->rx_buffer_padding);
422 efx->rx_buffer_order = get_order(efx->rx_buffer_len);
424 /* Initialise the channels */
425 efx_for_each_channel(channel, efx) {
426 EFX_LOG(channel->efx, "init chan %d\n", channel->channel);
428 efx_init_eventq(channel);
430 efx_for_each_channel_tx_queue(tx_queue, channel)
431 efx_init_tx_queue(tx_queue);
433 /* The rx buffer allocation strategy is MTU dependent */
434 efx_rx_strategy(channel);
436 efx_for_each_channel_rx_queue(rx_queue, channel)
437 efx_init_rx_queue(rx_queue);
439 WARN_ON(channel->rx_pkt != NULL);
440 efx_rx_strategy(channel);
444 /* This enables event queue processing and packet transmission.
446 * Note that this function is not allowed to fail, since that would
447 * introduce too much complexity into the suspend/resume path.
449 static void efx_start_channel(struct efx_channel *channel)
451 struct efx_rx_queue *rx_queue;
453 EFX_LOG(channel->efx, "starting chan %d\n", channel->channel);
455 /* The interrupt handler for this channel may set work_pending
456 * as soon as we enable it. Make sure it's cleared before
457 * then. Similarly, make sure it sees the enabled flag set. */
458 channel->work_pending = false;
459 channel->enabled = true;
462 napi_enable(&channel->napi_str);
464 /* Load up RX descriptors */
465 efx_for_each_channel_rx_queue(rx_queue, channel)
466 efx_fast_push_rx_descriptors(rx_queue);
469 /* This disables event queue processing and packet transmission.
470 * This function does not guarantee that all queue processing
471 * (e.g. RX refill) is complete.
473 static void efx_stop_channel(struct efx_channel *channel)
475 struct efx_rx_queue *rx_queue;
477 if (!channel->enabled)
480 EFX_LOG(channel->efx, "stop chan %d\n", channel->channel);
482 channel->enabled = false;
483 napi_disable(&channel->napi_str);
485 /* Ensure that any worker threads have exited or will be no-ops */
486 efx_for_each_channel_rx_queue(rx_queue, channel) {
487 spin_lock_bh(&rx_queue->add_lock);
488 spin_unlock_bh(&rx_queue->add_lock);
492 static void efx_fini_channels(struct efx_nic *efx)
494 struct efx_channel *channel;
495 struct efx_tx_queue *tx_queue;
496 struct efx_rx_queue *rx_queue;
499 EFX_ASSERT_RESET_SERIALISED(efx);
500 BUG_ON(efx->port_enabled);
502 rc = falcon_flush_queues(efx);
504 EFX_ERR(efx, "failed to flush queues\n");
506 EFX_LOG(efx, "successfully flushed all queues\n");
508 efx_for_each_channel(channel, efx) {
509 EFX_LOG(channel->efx, "shut down chan %d\n", channel->channel);
511 efx_for_each_channel_rx_queue(rx_queue, channel)
512 efx_fini_rx_queue(rx_queue);
513 efx_for_each_channel_tx_queue(tx_queue, channel)
514 efx_fini_tx_queue(tx_queue);
515 efx_fini_eventq(channel);
519 static void efx_remove_channel(struct efx_channel *channel)
521 struct efx_tx_queue *tx_queue;
522 struct efx_rx_queue *rx_queue;
524 EFX_LOG(channel->efx, "destroy chan %d\n", channel->channel);
526 efx_for_each_channel_rx_queue(rx_queue, channel)
527 efx_remove_rx_queue(rx_queue);
528 efx_for_each_channel_tx_queue(tx_queue, channel)
529 efx_remove_tx_queue(tx_queue);
530 efx_remove_eventq(channel);
532 channel->used_flags = 0;
535 void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue, int delay)
537 queue_delayed_work(refill_workqueue, &rx_queue->work, delay);
540 /**************************************************************************
544 **************************************************************************/
546 /* This ensures that the kernel is kept informed (via
547 * netif_carrier_on/off) of the link status, and also maintains the
548 * link status's stop on the port's TX queue.
550 static void efx_link_status_changed(struct efx_nic *efx)
552 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
553 * that no events are triggered between unregister_netdev() and the
554 * driver unloading. A more general condition is that NETDEV_CHANGE
555 * can only be generated between NETDEV_UP and NETDEV_DOWN */
556 if (!netif_running(efx->net_dev))
559 if (efx->port_inhibited) {
560 netif_carrier_off(efx->net_dev);
564 if (efx->link_up != netif_carrier_ok(efx->net_dev)) {
565 efx->n_link_state_changes++;
568 netif_carrier_on(efx->net_dev);
570 netif_carrier_off(efx->net_dev);
573 /* Status message for kernel log */
575 EFX_INFO(efx, "link up at %uMbps %s-duplex (MTU %d)%s\n",
576 efx->link_speed, efx->link_fd ? "full" : "half",
578 (efx->promiscuous ? " [PROMISC]" : ""));
580 EFX_INFO(efx, "link down\n");
585 static void efx_fini_port(struct efx_nic *efx);
587 /* This call reinitialises the MAC to pick up new PHY settings. The
588 * caller must hold the mac_lock */
589 void __efx_reconfigure_port(struct efx_nic *efx)
591 WARN_ON(!mutex_is_locked(&efx->mac_lock));
593 EFX_LOG(efx, "reconfiguring MAC from PHY settings on CPU %d\n",
594 raw_smp_processor_id());
596 /* Serialise the promiscuous flag with efx_set_multicast_list. */
597 if (efx_dev_registered(efx)) {
598 netif_addr_lock_bh(efx->net_dev);
599 netif_addr_unlock_bh(efx->net_dev);
602 falcon_deconfigure_mac_wrapper(efx);
604 /* Reconfigure the PHY, disabling transmit in mac level loopback. */
605 if (LOOPBACK_INTERNAL(efx))
606 efx->phy_mode |= PHY_MODE_TX_DISABLED;
608 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
609 efx->phy_op->reconfigure(efx);
611 if (falcon_switch_mac(efx))
614 efx->mac_op->reconfigure(efx);
616 /* Inform kernel of loss/gain of carrier */
617 efx_link_status_changed(efx);
621 EFX_ERR(efx, "failed to reconfigure MAC\n");
622 efx->port_enabled = false;
626 /* Reinitialise the MAC to pick up new PHY settings, even if the port is
628 void efx_reconfigure_port(struct efx_nic *efx)
630 EFX_ASSERT_RESET_SERIALISED(efx);
632 mutex_lock(&efx->mac_lock);
633 __efx_reconfigure_port(efx);
634 mutex_unlock(&efx->mac_lock);
637 /* Asynchronous efx_reconfigure_port work item. To speed up efx_flush_all()
638 * we don't efx_reconfigure_port() if the port is disabled. Care is taken
639 * in efx_stop_all() and efx_start_port() to prevent PHY events being lost */
640 static void efx_phy_work(struct work_struct *data)
642 struct efx_nic *efx = container_of(data, struct efx_nic, phy_work);
644 mutex_lock(&efx->mac_lock);
645 if (efx->port_enabled)
646 __efx_reconfigure_port(efx);
647 mutex_unlock(&efx->mac_lock);
650 static void efx_mac_work(struct work_struct *data)
652 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
654 mutex_lock(&efx->mac_lock);
655 if (efx->port_enabled)
656 efx->mac_op->irq(efx);
657 mutex_unlock(&efx->mac_lock);
660 static int efx_probe_port(struct efx_nic *efx)
664 EFX_LOG(efx, "create port\n");
666 /* Connect up MAC/PHY operations table and read MAC address */
667 rc = falcon_probe_port(efx);
672 efx->phy_mode = PHY_MODE_SPECIAL;
674 /* Sanity check MAC address */
675 if (is_valid_ether_addr(efx->mac_address)) {
676 memcpy(efx->net_dev->dev_addr, efx->mac_address, ETH_ALEN);
678 EFX_ERR(efx, "invalid MAC address %pM\n",
680 if (!allow_bad_hwaddr) {
684 random_ether_addr(efx->net_dev->dev_addr);
685 EFX_INFO(efx, "using locally-generated MAC %pM\n",
686 efx->net_dev->dev_addr);
692 efx_remove_port(efx);
696 static int efx_init_port(struct efx_nic *efx)
700 EFX_LOG(efx, "init port\n");
702 rc = efx->phy_op->init(efx);
705 mutex_lock(&efx->mac_lock);
706 efx->phy_op->reconfigure(efx);
707 rc = falcon_switch_mac(efx);
708 mutex_unlock(&efx->mac_lock);
711 efx->mac_op->reconfigure(efx);
713 efx->port_initialized = true;
714 efx_stats_enable(efx);
718 efx->phy_op->fini(efx);
722 /* Allow efx_reconfigure_port() to be scheduled, and close the window
723 * between efx_stop_port and efx_flush_all whereby a previously scheduled
724 * efx_phy_work()/efx_mac_work() may have been cancelled */
725 static void efx_start_port(struct efx_nic *efx)
727 EFX_LOG(efx, "start port\n");
728 BUG_ON(efx->port_enabled);
730 mutex_lock(&efx->mac_lock);
731 efx->port_enabled = true;
732 __efx_reconfigure_port(efx);
733 efx->mac_op->irq(efx);
734 mutex_unlock(&efx->mac_lock);
737 /* Prevent efx_phy_work, efx_mac_work, and efx_monitor() from executing,
738 * and efx_set_multicast_list() from scheduling efx_phy_work. efx_phy_work
739 * and efx_mac_work may still be scheduled via NAPI processing until
740 * efx_flush_all() is called */
741 static void efx_stop_port(struct efx_nic *efx)
743 EFX_LOG(efx, "stop port\n");
745 mutex_lock(&efx->mac_lock);
746 efx->port_enabled = false;
747 mutex_unlock(&efx->mac_lock);
749 /* Serialise against efx_set_multicast_list() */
750 if (efx_dev_registered(efx)) {
751 netif_addr_lock_bh(efx->net_dev);
752 netif_addr_unlock_bh(efx->net_dev);
756 static void efx_fini_port(struct efx_nic *efx)
758 EFX_LOG(efx, "shut down port\n");
760 if (!efx->port_initialized)
763 efx_stats_disable(efx);
764 efx->phy_op->fini(efx);
765 efx->port_initialized = false;
767 efx->link_up = false;
768 efx_link_status_changed(efx);
771 static void efx_remove_port(struct efx_nic *efx)
773 EFX_LOG(efx, "destroying port\n");
775 falcon_remove_port(efx);
778 /**************************************************************************
782 **************************************************************************/
784 /* This configures the PCI device to enable I/O and DMA. */
785 static int efx_init_io(struct efx_nic *efx)
787 struct pci_dev *pci_dev = efx->pci_dev;
788 dma_addr_t dma_mask = efx->type->max_dma_mask;
791 EFX_LOG(efx, "initialising I/O\n");
793 rc = pci_enable_device(pci_dev);
795 EFX_ERR(efx, "failed to enable PCI device\n");
799 pci_set_master(pci_dev);
801 /* Set the PCI DMA mask. Try all possibilities from our
802 * genuine mask down to 32 bits, because some architectures
803 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
804 * masks event though they reject 46 bit masks.
806 while (dma_mask > 0x7fffffffUL) {
807 if (pci_dma_supported(pci_dev, dma_mask) &&
808 ((rc = pci_set_dma_mask(pci_dev, dma_mask)) == 0))
813 EFX_ERR(efx, "could not find a suitable DMA mask\n");
816 EFX_LOG(efx, "using DMA mask %llx\n", (unsigned long long) dma_mask);
817 rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
819 /* pci_set_consistent_dma_mask() is not *allowed* to
820 * fail with a mask that pci_set_dma_mask() accepted,
821 * but just in case...
823 EFX_ERR(efx, "failed to set consistent DMA mask\n");
827 efx->membase_phys = pci_resource_start(efx->pci_dev,
829 rc = pci_request_region(pci_dev, efx->type->mem_bar, "sfc");
831 EFX_ERR(efx, "request for memory BAR failed\n");
835 efx->membase = ioremap_nocache(efx->membase_phys,
836 efx->type->mem_map_size);
838 EFX_ERR(efx, "could not map memory BAR %d at %llx+%x\n",
840 (unsigned long long)efx->membase_phys,
841 efx->type->mem_map_size);
845 EFX_LOG(efx, "memory BAR %u at %llx+%x (virtual %p)\n",
846 efx->type->mem_bar, (unsigned long long)efx->membase_phys,
847 efx->type->mem_map_size, efx->membase);
852 pci_release_region(efx->pci_dev, efx->type->mem_bar);
854 efx->membase_phys = 0;
856 pci_disable_device(efx->pci_dev);
861 static void efx_fini_io(struct efx_nic *efx)
863 EFX_LOG(efx, "shutting down I/O\n");
866 iounmap(efx->membase);
870 if (efx->membase_phys) {
871 pci_release_region(efx->pci_dev, efx->type->mem_bar);
872 efx->membase_phys = 0;
875 pci_disable_device(efx->pci_dev);
878 /* Get number of RX queues wanted. Return number of online CPU
879 * packages in the expectation that an IRQ balancer will spread
880 * interrupts across them. */
881 static int efx_wanted_rx_queues(void)
883 cpumask_var_t core_mask;
887 if (unlikely(!alloc_cpumask_var(&core_mask, GFP_KERNEL))) {
889 "sfc: RSS disabled due to allocation failure\n");
893 cpumask_clear(core_mask);
895 for_each_online_cpu(cpu) {
896 if (!cpumask_test_cpu(cpu, core_mask)) {
898 cpumask_or(core_mask, core_mask,
899 topology_core_cpumask(cpu));
903 free_cpumask_var(core_mask);
907 /* Probe the number and type of interrupts we are able to obtain, and
908 * the resulting numbers of channels and RX queues.
910 static void efx_probe_interrupts(struct efx_nic *efx)
913 min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
916 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
917 struct msix_entry xentries[EFX_MAX_CHANNELS];
921 /* We want one RX queue and interrupt per CPU package
922 * (or as specified by the rss_cpus module parameter).
923 * We will need one channel per interrupt.
925 rx_queues = rss_cpus ? rss_cpus : efx_wanted_rx_queues();
926 wanted_ints = rx_queues + (separate_tx_channels ? 1 : 0);
927 wanted_ints = min(wanted_ints, max_channels);
929 for (i = 0; i < wanted_ints; i++)
930 xentries[i].entry = i;
931 rc = pci_enable_msix(efx->pci_dev, xentries, wanted_ints);
933 EFX_ERR(efx, "WARNING: Insufficient MSI-X vectors"
934 " available (%d < %d).\n", rc, wanted_ints);
935 EFX_ERR(efx, "WARNING: Performance may be reduced.\n");
936 EFX_BUG_ON_PARANOID(rc >= wanted_ints);
938 rc = pci_enable_msix(efx->pci_dev, xentries,
943 efx->n_rx_queues = min(rx_queues, wanted_ints);
944 efx->n_channels = wanted_ints;
945 for (i = 0; i < wanted_ints; i++)
946 efx->channel[i].irq = xentries[i].vector;
948 /* Fall back to single channel MSI */
949 efx->interrupt_mode = EFX_INT_MODE_MSI;
950 EFX_ERR(efx, "could not enable MSI-X\n");
954 /* Try single interrupt MSI */
955 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
956 efx->n_rx_queues = 1;
958 rc = pci_enable_msi(efx->pci_dev);
960 efx->channel[0].irq = efx->pci_dev->irq;
962 EFX_ERR(efx, "could not enable MSI\n");
963 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
967 /* Assume legacy interrupts */
968 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
969 efx->n_rx_queues = 1;
970 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
971 efx->legacy_irq = efx->pci_dev->irq;
975 static void efx_remove_interrupts(struct efx_nic *efx)
977 struct efx_channel *channel;
979 /* Remove MSI/MSI-X interrupts */
980 efx_for_each_channel(channel, efx)
982 pci_disable_msi(efx->pci_dev);
983 pci_disable_msix(efx->pci_dev);
985 /* Remove legacy interrupt */
989 static void efx_set_channels(struct efx_nic *efx)
991 struct efx_tx_queue *tx_queue;
992 struct efx_rx_queue *rx_queue;
994 efx_for_each_tx_queue(tx_queue, efx) {
995 if (separate_tx_channels)
996 tx_queue->channel = &efx->channel[efx->n_channels-1];
998 tx_queue->channel = &efx->channel[0];
999 tx_queue->channel->used_flags |= EFX_USED_BY_TX;
1002 efx_for_each_rx_queue(rx_queue, efx) {
1003 rx_queue->channel = &efx->channel[rx_queue->queue];
1004 rx_queue->channel->used_flags |= EFX_USED_BY_RX;
1008 static int efx_probe_nic(struct efx_nic *efx)
1012 EFX_LOG(efx, "creating NIC\n");
1014 /* Carry out hardware-type specific initialisation */
1015 rc = falcon_probe_nic(efx);
1019 /* Determine the number of channels and RX queues by trying to hook
1020 * in MSI-X interrupts. */
1021 efx_probe_interrupts(efx);
1023 efx_set_channels(efx);
1025 /* Initialise the interrupt moderation settings */
1026 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true);
1031 static void efx_remove_nic(struct efx_nic *efx)
1033 EFX_LOG(efx, "destroying NIC\n");
1035 efx_remove_interrupts(efx);
1036 falcon_remove_nic(efx);
1039 /**************************************************************************
1041 * NIC startup/shutdown
1043 *************************************************************************/
1045 static int efx_probe_all(struct efx_nic *efx)
1047 struct efx_channel *channel;
1051 rc = efx_probe_nic(efx);
1053 EFX_ERR(efx, "failed to create NIC\n");
1058 rc = efx_probe_port(efx);
1060 EFX_ERR(efx, "failed to create port\n");
1064 /* Create channels */
1065 efx_for_each_channel(channel, efx) {
1066 rc = efx_probe_channel(channel);
1068 EFX_ERR(efx, "failed to create channel %d\n",
1073 efx_set_channel_names(efx);
1078 efx_for_each_channel(channel, efx)
1079 efx_remove_channel(channel);
1080 efx_remove_port(efx);
1082 efx_remove_nic(efx);
1087 /* Called after previous invocation(s) of efx_stop_all, restarts the
1088 * port, kernel transmit queue, NAPI processing and hardware interrupts,
1089 * and ensures that the port is scheduled to be reconfigured.
1090 * This function is safe to call multiple times when the NIC is in any
1092 static void efx_start_all(struct efx_nic *efx)
1094 struct efx_channel *channel;
1096 EFX_ASSERT_RESET_SERIALISED(efx);
1098 /* Check that it is appropriate to restart the interface. All
1099 * of these flags are safe to read under just the rtnl lock */
1100 if (efx->port_enabled)
1102 if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1104 if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
1107 /* Mark the port as enabled so port reconfigurations can start, then
1108 * restart the transmit interface early so the watchdog timer stops */
1109 efx_start_port(efx);
1110 if (efx_dev_registered(efx))
1111 efx_wake_queue(efx);
1113 efx_for_each_channel(channel, efx)
1114 efx_start_channel(channel);
1116 falcon_enable_interrupts(efx);
1118 /* Start hardware monitor if we're in RUNNING */
1119 if (efx->state == STATE_RUNNING)
1120 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1121 efx_monitor_interval);
1124 /* Flush all delayed work. Should only be called when no more delayed work
1125 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1126 * since we're holding the rtnl_lock at this point. */
1127 static void efx_flush_all(struct efx_nic *efx)
1129 struct efx_rx_queue *rx_queue;
1131 /* Make sure the hardware monitor is stopped */
1132 cancel_delayed_work_sync(&efx->monitor_work);
1134 /* Ensure that all RX slow refills are complete. */
1135 efx_for_each_rx_queue(rx_queue, efx)
1136 cancel_delayed_work_sync(&rx_queue->work);
1138 /* Stop scheduled port reconfigurations */
1139 cancel_work_sync(&efx->mac_work);
1140 cancel_work_sync(&efx->phy_work);
1144 /* Quiesce hardware and software without bringing the link down.
1145 * Safe to call multiple times, when the nic and interface is in any
1146 * state. The caller is guaranteed to subsequently be in a position
1147 * to modify any hardware and software state they see fit without
1149 static void efx_stop_all(struct efx_nic *efx)
1151 struct efx_channel *channel;
1153 EFX_ASSERT_RESET_SERIALISED(efx);
1155 /* port_enabled can be read safely under the rtnl lock */
1156 if (!efx->port_enabled)
1159 /* Disable interrupts and wait for ISR to complete */
1160 falcon_disable_interrupts(efx);
1161 if (efx->legacy_irq)
1162 synchronize_irq(efx->legacy_irq);
1163 efx_for_each_channel(channel, efx) {
1165 synchronize_irq(channel->irq);
1168 /* Stop all NAPI processing and synchronous rx refills */
1169 efx_for_each_channel(channel, efx)
1170 efx_stop_channel(channel);
1172 /* Stop all asynchronous port reconfigurations. Since all
1173 * event processing has already been stopped, there is no
1174 * window to loose phy events */
1177 /* Flush efx_phy_work, efx_mac_work, refill_workqueue, monitor_work */
1180 /* Isolate the MAC from the TX and RX engines, so that queue
1181 * flushes will complete in a timely fashion. */
1182 falcon_drain_tx_fifo(efx);
1184 /* Stop the kernel transmit interface late, so the watchdog
1185 * timer isn't ticking over the flush */
1186 if (efx_dev_registered(efx)) {
1187 efx_stop_queue(efx);
1188 netif_tx_lock_bh(efx->net_dev);
1189 netif_tx_unlock_bh(efx->net_dev);
1193 static void efx_remove_all(struct efx_nic *efx)
1195 struct efx_channel *channel;
1197 efx_for_each_channel(channel, efx)
1198 efx_remove_channel(channel);
1199 efx_remove_port(efx);
1200 efx_remove_nic(efx);
1203 /* A convinience function to safely flush all the queues */
1204 void efx_flush_queues(struct efx_nic *efx)
1206 EFX_ASSERT_RESET_SERIALISED(efx);
1210 efx_fini_channels(efx);
1211 efx_init_channels(efx);
1216 /**************************************************************************
1218 * Interrupt moderation
1220 **************************************************************************/
1222 /* Set interrupt moderation parameters */
1223 void efx_init_irq_moderation(struct efx_nic *efx, int tx_usecs, int rx_usecs,
1226 struct efx_tx_queue *tx_queue;
1227 struct efx_rx_queue *rx_queue;
1229 EFX_ASSERT_RESET_SERIALISED(efx);
1231 efx_for_each_tx_queue(tx_queue, efx)
1232 tx_queue->channel->irq_moderation = tx_usecs;
1234 efx->irq_rx_adaptive = rx_adaptive;
1235 efx->irq_rx_moderation = rx_usecs;
1236 efx_for_each_rx_queue(rx_queue, efx)
1237 rx_queue->channel->irq_moderation = rx_usecs;
1240 /**************************************************************************
1244 **************************************************************************/
1246 /* Run periodically off the general workqueue. Serialised against
1247 * efx_reconfigure_port via the mac_lock */
1248 static void efx_monitor(struct work_struct *data)
1250 struct efx_nic *efx = container_of(data, struct efx_nic,
1254 EFX_TRACE(efx, "hardware monitor executing on CPU %d\n",
1255 raw_smp_processor_id());
1257 /* If the mac_lock is already held then it is likely a port
1258 * reconfiguration is already in place, which will likely do
1259 * most of the work of check_hw() anyway. */
1260 if (!mutex_trylock(&efx->mac_lock))
1262 if (!efx->port_enabled)
1264 rc = efx->board_info.monitor(efx);
1266 EFX_ERR(efx, "Board sensor %s; shutting down PHY\n",
1267 (rc == -ERANGE) ? "reported fault" : "failed");
1268 efx->phy_mode |= PHY_MODE_LOW_POWER;
1269 falcon_sim_phy_event(efx);
1271 efx->phy_op->poll(efx);
1272 efx->mac_op->poll(efx);
1275 mutex_unlock(&efx->mac_lock);
1277 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1278 efx_monitor_interval);
1281 /**************************************************************************
1285 *************************************************************************/
1288 * Context: process, rtnl_lock() held.
1290 static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1292 struct efx_nic *efx = netdev_priv(net_dev);
1293 struct mii_ioctl_data *data = if_mii(ifr);
1295 EFX_ASSERT_RESET_SERIALISED(efx);
1297 /* Convert phy_id from older PRTAD/DEVAD format */
1298 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1299 (data->phy_id & 0xfc00) == 0x0400)
1300 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1302 return mdio_mii_ioctl(&efx->mdio, data, cmd);
1305 /**************************************************************************
1309 **************************************************************************/
1311 static int efx_init_napi(struct efx_nic *efx)
1313 struct efx_channel *channel;
1315 efx_for_each_channel(channel, efx) {
1316 channel->napi_dev = efx->net_dev;
1317 netif_napi_add(channel->napi_dev, &channel->napi_str,
1318 efx_poll, napi_weight);
1323 static void efx_fini_napi(struct efx_nic *efx)
1325 struct efx_channel *channel;
1327 efx_for_each_channel(channel, efx) {
1328 if (channel->napi_dev)
1329 netif_napi_del(&channel->napi_str);
1330 channel->napi_dev = NULL;
1334 /**************************************************************************
1336 * Kernel netpoll interface
1338 *************************************************************************/
1340 #ifdef CONFIG_NET_POLL_CONTROLLER
1342 /* Although in the common case interrupts will be disabled, this is not
1343 * guaranteed. However, all our work happens inside the NAPI callback,
1344 * so no locking is required.
1346 static void efx_netpoll(struct net_device *net_dev)
1348 struct efx_nic *efx = netdev_priv(net_dev);
1349 struct efx_channel *channel;
1351 efx_for_each_channel(channel, efx)
1352 efx_schedule_channel(channel);
1357 /**************************************************************************
1359 * Kernel net device interface
1361 *************************************************************************/
1363 /* Context: process, rtnl_lock() held. */
1364 static int efx_net_open(struct net_device *net_dev)
1366 struct efx_nic *efx = netdev_priv(net_dev);
1367 EFX_ASSERT_RESET_SERIALISED(efx);
1369 EFX_LOG(efx, "opening device %s on CPU %d\n", net_dev->name,
1370 raw_smp_processor_id());
1372 if (efx->state == STATE_DISABLED)
1374 if (efx->phy_mode & PHY_MODE_SPECIAL)
1381 /* Context: process, rtnl_lock() held.
1382 * Note that the kernel will ignore our return code; this method
1383 * should really be a void.
1385 static int efx_net_stop(struct net_device *net_dev)
1387 struct efx_nic *efx = netdev_priv(net_dev);
1389 EFX_LOG(efx, "closing %s on CPU %d\n", net_dev->name,
1390 raw_smp_processor_id());
1392 if (efx->state != STATE_DISABLED) {
1393 /* Stop the device and flush all the channels */
1395 efx_fini_channels(efx);
1396 efx_init_channels(efx);
1402 void efx_stats_disable(struct efx_nic *efx)
1404 spin_lock(&efx->stats_lock);
1405 ++efx->stats_disable_count;
1406 spin_unlock(&efx->stats_lock);
1409 void efx_stats_enable(struct efx_nic *efx)
1411 spin_lock(&efx->stats_lock);
1412 --efx->stats_disable_count;
1413 spin_unlock(&efx->stats_lock);
1416 /* Context: process, dev_base_lock or RTNL held, non-blocking. */
1417 static struct net_device_stats *efx_net_stats(struct net_device *net_dev)
1419 struct efx_nic *efx = netdev_priv(net_dev);
1420 struct efx_mac_stats *mac_stats = &efx->mac_stats;
1421 struct net_device_stats *stats = &net_dev->stats;
1423 /* Update stats if possible, but do not wait if another thread
1424 * is updating them or if MAC stats fetches are temporarily
1425 * disabled; slightly stale stats are acceptable.
1427 if (!spin_trylock(&efx->stats_lock))
1429 if (!efx->stats_disable_count) {
1430 efx->mac_op->update_stats(efx);
1431 falcon_update_nic_stats(efx);
1433 spin_unlock(&efx->stats_lock);
1435 stats->rx_packets = mac_stats->rx_packets;
1436 stats->tx_packets = mac_stats->tx_packets;
1437 stats->rx_bytes = mac_stats->rx_bytes;
1438 stats->tx_bytes = mac_stats->tx_bytes;
1439 stats->multicast = mac_stats->rx_multicast;
1440 stats->collisions = mac_stats->tx_collision;
1441 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1442 mac_stats->rx_length_error);
1443 stats->rx_over_errors = efx->n_rx_nodesc_drop_cnt;
1444 stats->rx_crc_errors = mac_stats->rx_bad;
1445 stats->rx_frame_errors = mac_stats->rx_align_error;
1446 stats->rx_fifo_errors = mac_stats->rx_overflow;
1447 stats->rx_missed_errors = mac_stats->rx_missed;
1448 stats->tx_window_errors = mac_stats->tx_late_collision;
1450 stats->rx_errors = (stats->rx_length_errors +
1451 stats->rx_over_errors +
1452 stats->rx_crc_errors +
1453 stats->rx_frame_errors +
1454 stats->rx_fifo_errors +
1455 stats->rx_missed_errors +
1456 mac_stats->rx_symbol_error);
1457 stats->tx_errors = (stats->tx_window_errors +
1463 /* Context: netif_tx_lock held, BHs disabled. */
1464 static void efx_watchdog(struct net_device *net_dev)
1466 struct efx_nic *efx = netdev_priv(net_dev);
1468 EFX_ERR(efx, "TX stuck with stop_count=%d port_enabled=%d:"
1469 " resetting channels\n",
1470 atomic_read(&efx->netif_stop_count), efx->port_enabled);
1472 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
1476 /* Context: process, rtnl_lock() held. */
1477 static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1479 struct efx_nic *efx = netdev_priv(net_dev);
1482 EFX_ASSERT_RESET_SERIALISED(efx);
1484 if (new_mtu > EFX_MAX_MTU)
1489 EFX_LOG(efx, "changing MTU to %d\n", new_mtu);
1491 efx_fini_channels(efx);
1492 net_dev->mtu = new_mtu;
1493 efx_init_channels(efx);
1499 static int efx_set_mac_address(struct net_device *net_dev, void *data)
1501 struct efx_nic *efx = netdev_priv(net_dev);
1502 struct sockaddr *addr = data;
1503 char *new_addr = addr->sa_data;
1505 EFX_ASSERT_RESET_SERIALISED(efx);
1507 if (!is_valid_ether_addr(new_addr)) {
1508 EFX_ERR(efx, "invalid ethernet MAC address requested: %pM\n",
1513 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1515 /* Reconfigure the MAC */
1516 efx_reconfigure_port(efx);
1521 /* Context: netif_addr_lock held, BHs disabled. */
1522 static void efx_set_multicast_list(struct net_device *net_dev)
1524 struct efx_nic *efx = netdev_priv(net_dev);
1525 struct dev_mc_list *mc_list = net_dev->mc_list;
1526 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
1527 bool promiscuous = !!(net_dev->flags & IFF_PROMISC);
1528 bool changed = (efx->promiscuous != promiscuous);
1533 efx->promiscuous = promiscuous;
1535 /* Build multicast hash table */
1536 if (promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
1537 memset(mc_hash, 0xff, sizeof(*mc_hash));
1539 memset(mc_hash, 0x00, sizeof(*mc_hash));
1540 for (i = 0; i < net_dev->mc_count; i++) {
1541 crc = ether_crc_le(ETH_ALEN, mc_list->dmi_addr);
1542 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1543 set_bit_le(bit, mc_hash->byte);
1544 mc_list = mc_list->next;
1548 if (!efx->port_enabled)
1549 /* Delay pushing settings until efx_start_port() */
1553 queue_work(efx->workqueue, &efx->phy_work);
1555 /* Create and activate new global multicast hash table */
1556 falcon_set_multicast_hash(efx);
1559 static const struct net_device_ops efx_netdev_ops = {
1560 .ndo_open = efx_net_open,
1561 .ndo_stop = efx_net_stop,
1562 .ndo_get_stats = efx_net_stats,
1563 .ndo_tx_timeout = efx_watchdog,
1564 .ndo_start_xmit = efx_hard_start_xmit,
1565 .ndo_validate_addr = eth_validate_addr,
1566 .ndo_do_ioctl = efx_ioctl,
1567 .ndo_change_mtu = efx_change_mtu,
1568 .ndo_set_mac_address = efx_set_mac_address,
1569 .ndo_set_multicast_list = efx_set_multicast_list,
1570 #ifdef CONFIG_NET_POLL_CONTROLLER
1571 .ndo_poll_controller = efx_netpoll,
1575 static void efx_update_name(struct efx_nic *efx)
1577 strcpy(efx->name, efx->net_dev->name);
1578 efx_mtd_rename(efx);
1579 efx_set_channel_names(efx);
1582 static int efx_netdev_event(struct notifier_block *this,
1583 unsigned long event, void *ptr)
1585 struct net_device *net_dev = ptr;
1587 if (net_dev->netdev_ops == &efx_netdev_ops &&
1588 event == NETDEV_CHANGENAME)
1589 efx_update_name(netdev_priv(net_dev));
1594 static struct notifier_block efx_netdev_notifier = {
1595 .notifier_call = efx_netdev_event,
1599 show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
1601 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
1602 return sprintf(buf, "%d\n", efx->phy_type);
1604 static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
1606 static int efx_register_netdev(struct efx_nic *efx)
1608 struct net_device *net_dev = efx->net_dev;
1611 net_dev->watchdog_timeo = 5 * HZ;
1612 net_dev->irq = efx->pci_dev->irq;
1613 net_dev->netdev_ops = &efx_netdev_ops;
1614 SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
1615 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
1617 /* Always start with carrier off; PHY events will detect the link */
1618 netif_carrier_off(efx->net_dev);
1620 /* Clear MAC statistics */
1621 efx->mac_op->update_stats(efx);
1622 memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
1624 rc = register_netdev(net_dev);
1626 EFX_ERR(efx, "could not register net dev\n");
1631 efx_update_name(efx);
1634 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
1636 EFX_ERR(efx, "failed to init net dev attributes\n");
1637 goto fail_registered;
1643 unregister_netdev(net_dev);
1647 static void efx_unregister_netdev(struct efx_nic *efx)
1649 struct efx_tx_queue *tx_queue;
1654 BUG_ON(netdev_priv(efx->net_dev) != efx);
1656 /* Free up any skbs still remaining. This has to happen before
1657 * we try to unregister the netdev as running their destructors
1658 * may be needed to get the device ref. count to 0. */
1659 efx_for_each_tx_queue(tx_queue, efx)
1660 efx_release_tx_buffers(tx_queue);
1662 if (efx_dev_registered(efx)) {
1663 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
1664 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
1665 unregister_netdev(efx->net_dev);
1669 /**************************************************************************
1671 * Device reset and suspend
1673 **************************************************************************/
1675 /* Tears down the entire software state and most of the hardware state
1677 void efx_reset_down(struct efx_nic *efx, enum reset_type method,
1678 struct ethtool_cmd *ecmd)
1680 EFX_ASSERT_RESET_SERIALISED(efx);
1682 efx_stats_disable(efx);
1684 mutex_lock(&efx->mac_lock);
1685 mutex_lock(&efx->spi_lock);
1687 efx->phy_op->get_settings(efx, ecmd);
1689 efx_fini_channels(efx);
1690 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
1691 efx->phy_op->fini(efx);
1694 /* This function will always ensure that the locks acquired in
1695 * efx_reset_down() are released. A failure return code indicates
1696 * that we were unable to reinitialise the hardware, and the
1697 * driver should be disabled. If ok is false, then the rx and tx
1698 * engines are not restarted, pending a RESET_DISABLE. */
1699 int efx_reset_up(struct efx_nic *efx, enum reset_type method,
1700 struct ethtool_cmd *ecmd, bool ok)
1704 EFX_ASSERT_RESET_SERIALISED(efx);
1706 rc = falcon_init_nic(efx);
1708 EFX_ERR(efx, "failed to initialise NIC\n");
1712 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
1714 rc = efx->phy_op->init(efx);
1719 efx->port_initialized = false;
1723 efx_init_channels(efx);
1725 if (efx->phy_op->set_settings(efx, ecmd))
1726 EFX_ERR(efx, "could not restore PHY settings\n");
1729 mutex_unlock(&efx->spi_lock);
1730 mutex_unlock(&efx->mac_lock);
1734 efx_stats_enable(efx);
1739 /* Reset the NIC as transparently as possible. Do not reset the PHY
1740 * Note that the reset may fail, in which case the card will be left
1741 * in a most-probably-unusable state.
1743 * This function will sleep. You cannot reset from within an atomic
1744 * state; use efx_schedule_reset() instead.
1746 * Grabs the rtnl_lock.
1748 static int efx_reset(struct efx_nic *efx)
1750 struct ethtool_cmd ecmd;
1751 enum reset_type method = efx->reset_pending;
1754 /* Serialise with kernel interfaces */
1757 /* If we're not RUNNING then don't reset. Leave the reset_pending
1758 * flag set so that efx_pci_probe_main will be retried */
1759 if (efx->state != STATE_RUNNING) {
1760 EFX_INFO(efx, "scheduled reset quenched. NIC not RUNNING\n");
1764 EFX_INFO(efx, "resetting (%d)\n", method);
1766 efx_reset_down(efx, method, &ecmd);
1768 rc = falcon_reset_hw(efx, method);
1770 EFX_ERR(efx, "failed to reset hardware\n");
1774 /* Allow resets to be rescheduled. */
1775 efx->reset_pending = RESET_TYPE_NONE;
1777 /* Reinitialise bus-mastering, which may have been turned off before
1778 * the reset was scheduled. This is still appropriate, even in the
1779 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
1780 * can respond to requests. */
1781 pci_set_master(efx->pci_dev);
1783 /* Leave device stopped if necessary */
1784 if (method == RESET_TYPE_DISABLE) {
1785 efx_reset_up(efx, method, &ecmd, false);
1788 rc = efx_reset_up(efx, method, &ecmd, true);
1793 EFX_ERR(efx, "has been disabled\n");
1794 efx->state = STATE_DISABLED;
1795 dev_close(efx->net_dev);
1797 EFX_LOG(efx, "reset complete\n");
1805 /* The worker thread exists so that code that cannot sleep can
1806 * schedule a reset for later.
1808 static void efx_reset_work(struct work_struct *data)
1810 struct efx_nic *nic = container_of(data, struct efx_nic, reset_work);
1815 void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
1817 enum reset_type method;
1819 if (efx->reset_pending != RESET_TYPE_NONE) {
1820 EFX_INFO(efx, "quenching already scheduled reset\n");
1825 case RESET_TYPE_INVISIBLE:
1826 case RESET_TYPE_ALL:
1827 case RESET_TYPE_WORLD:
1828 case RESET_TYPE_DISABLE:
1831 case RESET_TYPE_RX_RECOVERY:
1832 case RESET_TYPE_RX_DESC_FETCH:
1833 case RESET_TYPE_TX_DESC_FETCH:
1834 case RESET_TYPE_TX_SKIP:
1835 method = RESET_TYPE_INVISIBLE;
1838 method = RESET_TYPE_ALL;
1843 EFX_LOG(efx, "scheduling reset (%d:%d)\n", type, method);
1845 EFX_LOG(efx, "scheduling reset (%d)\n", method);
1847 efx->reset_pending = method;
1849 queue_work(reset_workqueue, &efx->reset_work);
1852 /**************************************************************************
1854 * List of NICs we support
1856 **************************************************************************/
1858 /* PCI device ID table */
1859 static struct pci_device_id efx_pci_table[] __devinitdata = {
1860 {PCI_DEVICE(EFX_VENDID_SFC, FALCON_A_P_DEVID),
1861 .driver_data = (unsigned long) &falcon_a_nic_type},
1862 {PCI_DEVICE(EFX_VENDID_SFC, FALCON_B_P_DEVID),
1863 .driver_data = (unsigned long) &falcon_b_nic_type},
1864 {0} /* end of list */
1867 /**************************************************************************
1869 * Dummy PHY/MAC/Board operations
1871 * Can be used for some unimplemented operations
1872 * Needed so all function pointers are valid and do not have to be tested
1875 **************************************************************************/
1876 int efx_port_dummy_op_int(struct efx_nic *efx)
1880 void efx_port_dummy_op_void(struct efx_nic *efx) {}
1881 void efx_port_dummy_op_blink(struct efx_nic *efx, bool blink) {}
1883 static struct efx_mac_operations efx_dummy_mac_operations = {
1884 .reconfigure = efx_port_dummy_op_void,
1885 .poll = efx_port_dummy_op_void,
1886 .irq = efx_port_dummy_op_void,
1889 static struct efx_phy_operations efx_dummy_phy_operations = {
1890 .init = efx_port_dummy_op_int,
1891 .reconfigure = efx_port_dummy_op_void,
1892 .poll = efx_port_dummy_op_void,
1893 .fini = efx_port_dummy_op_void,
1894 .clear_interrupt = efx_port_dummy_op_void,
1897 static struct efx_board efx_dummy_board_info = {
1898 .init = efx_port_dummy_op_int,
1899 .init_leds = efx_port_dummy_op_void,
1900 .set_id_led = efx_port_dummy_op_blink,
1901 .monitor = efx_port_dummy_op_int,
1902 .blink = efx_port_dummy_op_blink,
1903 .fini = efx_port_dummy_op_void,
1906 /**************************************************************************
1910 **************************************************************************/
1912 /* This zeroes out and then fills in the invariants in a struct
1913 * efx_nic (including all sub-structures).
1915 static int efx_init_struct(struct efx_nic *efx, struct efx_nic_type *type,
1916 struct pci_dev *pci_dev, struct net_device *net_dev)
1918 struct efx_channel *channel;
1919 struct efx_tx_queue *tx_queue;
1920 struct efx_rx_queue *rx_queue;
1923 /* Initialise common structures */
1924 memset(efx, 0, sizeof(*efx));
1925 spin_lock_init(&efx->biu_lock);
1926 spin_lock_init(&efx->phy_lock);
1927 mutex_init(&efx->spi_lock);
1928 INIT_WORK(&efx->reset_work, efx_reset_work);
1929 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
1930 efx->pci_dev = pci_dev;
1931 efx->state = STATE_INIT;
1932 efx->reset_pending = RESET_TYPE_NONE;
1933 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
1934 efx->board_info = efx_dummy_board_info;
1936 efx->net_dev = net_dev;
1937 efx->rx_checksum_enabled = true;
1938 spin_lock_init(&efx->netif_stop_lock);
1939 spin_lock_init(&efx->stats_lock);
1940 efx->stats_disable_count = 1;
1941 mutex_init(&efx->mac_lock);
1942 efx->mac_op = &efx_dummy_mac_operations;
1943 efx->phy_op = &efx_dummy_phy_operations;
1944 efx->mdio.dev = net_dev;
1945 INIT_WORK(&efx->phy_work, efx_phy_work);
1946 INIT_WORK(&efx->mac_work, efx_mac_work);
1947 atomic_set(&efx->netif_stop_count, 1);
1949 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
1950 channel = &efx->channel[i];
1952 channel->channel = i;
1953 channel->work_pending = false;
1955 for (i = 0; i < EFX_TX_QUEUE_COUNT; i++) {
1956 tx_queue = &efx->tx_queue[i];
1957 tx_queue->efx = efx;
1958 tx_queue->queue = i;
1959 tx_queue->buffer = NULL;
1960 tx_queue->channel = &efx->channel[0]; /* for safety */
1961 tx_queue->tso_headers_free = NULL;
1963 for (i = 0; i < EFX_MAX_RX_QUEUES; i++) {
1964 rx_queue = &efx->rx_queue[i];
1965 rx_queue->efx = efx;
1966 rx_queue->queue = i;
1967 rx_queue->channel = &efx->channel[0]; /* for safety */
1968 rx_queue->buffer = NULL;
1969 spin_lock_init(&rx_queue->add_lock);
1970 INIT_DELAYED_WORK(&rx_queue->work, efx_rx_work);
1975 /* Sanity-check NIC type */
1976 EFX_BUG_ON_PARANOID(efx->type->txd_ring_mask &
1977 (efx->type->txd_ring_mask + 1));
1978 EFX_BUG_ON_PARANOID(efx->type->rxd_ring_mask &
1979 (efx->type->rxd_ring_mask + 1));
1980 EFX_BUG_ON_PARANOID(efx->type->evq_size &
1981 (efx->type->evq_size - 1));
1982 /* As close as we can get to guaranteeing that we don't overflow */
1983 EFX_BUG_ON_PARANOID(efx->type->evq_size <
1984 (efx->type->txd_ring_mask + 1 +
1985 efx->type->rxd_ring_mask + 1));
1986 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
1988 /* Higher numbered interrupt modes are less capable! */
1989 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
1992 /* Would be good to use the net_dev name, but we're too early */
1993 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
1995 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
1996 if (!efx->workqueue)
2002 static void efx_fini_struct(struct efx_nic *efx)
2004 if (efx->workqueue) {
2005 destroy_workqueue(efx->workqueue);
2006 efx->workqueue = NULL;
2010 /**************************************************************************
2014 **************************************************************************/
2016 /* Main body of final NIC shutdown code
2017 * This is called only at module unload (or hotplug removal).
2019 static void efx_pci_remove_main(struct efx_nic *efx)
2021 EFX_ASSERT_RESET_SERIALISED(efx);
2023 /* Skip everything if we never obtained a valid membase */
2027 efx_fini_channels(efx);
2030 /* Shutdown the board, then the NIC and board state */
2031 efx->board_info.fini(efx);
2032 falcon_fini_interrupt(efx);
2035 efx_remove_all(efx);
2038 /* Final NIC shutdown
2039 * This is called only at module unload (or hotplug removal).
2041 static void efx_pci_remove(struct pci_dev *pci_dev)
2043 struct efx_nic *efx;
2045 efx = pci_get_drvdata(pci_dev);
2049 /* Mark the NIC as fini, then stop the interface */
2051 efx->state = STATE_FINI;
2052 dev_close(efx->net_dev);
2054 /* Allow any queued efx_resets() to complete */
2057 if (efx->membase == NULL)
2060 efx_unregister_netdev(efx);
2062 efx_mtd_remove(efx);
2064 /* Wait for any scheduled resets to complete. No more will be
2065 * scheduled from this point because efx_stop_all() has been
2066 * called, we are no longer registered with driverlink, and
2067 * the net_device's have been removed. */
2068 cancel_work_sync(&efx->reset_work);
2070 efx_pci_remove_main(efx);
2074 EFX_LOG(efx, "shutdown successful\n");
2076 pci_set_drvdata(pci_dev, NULL);
2077 efx_fini_struct(efx);
2078 free_netdev(efx->net_dev);
2081 /* Main body of NIC initialisation
2082 * This is called at module load (or hotplug insertion, theoretically).
2084 static int efx_pci_probe_main(struct efx_nic *efx)
2088 /* Do start-of-day initialisation */
2089 rc = efx_probe_all(efx);
2093 rc = efx_init_napi(efx);
2097 /* Initialise the board */
2098 rc = efx->board_info.init(efx);
2100 EFX_ERR(efx, "failed to initialise board\n");
2104 rc = falcon_init_nic(efx);
2106 EFX_ERR(efx, "failed to initialise NIC\n");
2110 rc = efx_init_port(efx);
2112 EFX_ERR(efx, "failed to initialise port\n");
2116 efx_init_channels(efx);
2118 rc = falcon_init_interrupt(efx);
2125 efx_fini_channels(efx);
2129 efx->board_info.fini(efx);
2133 efx_remove_all(efx);
2138 /* NIC initialisation
2140 * This is called at module load (or hotplug insertion,
2141 * theoretically). It sets up PCI mappings, tests and resets the NIC,
2142 * sets up and registers the network devices with the kernel and hooks
2143 * the interrupt service routine. It does not prepare the device for
2144 * transmission; this is left to the first time one of the network
2145 * interfaces is brought up (i.e. efx_net_open).
2147 static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2148 const struct pci_device_id *entry)
2150 struct efx_nic_type *type = (struct efx_nic_type *) entry->driver_data;
2151 struct net_device *net_dev;
2152 struct efx_nic *efx;
2155 /* Allocate and initialise a struct net_device and struct efx_nic */
2156 net_dev = alloc_etherdev(sizeof(*efx));
2159 net_dev->features |= (NETIF_F_IP_CSUM | NETIF_F_SG |
2160 NETIF_F_HIGHDMA | NETIF_F_TSO |
2162 /* Mask for features that also apply to VLAN devices */
2163 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
2164 NETIF_F_HIGHDMA | NETIF_F_TSO);
2165 efx = netdev_priv(net_dev);
2166 pci_set_drvdata(pci_dev, efx);
2167 rc = efx_init_struct(efx, type, pci_dev, net_dev);
2171 EFX_INFO(efx, "Solarflare Communications NIC detected\n");
2173 /* Set up basic I/O (BAR mappings etc) */
2174 rc = efx_init_io(efx);
2178 /* No serialisation is required with the reset path because
2179 * we're in STATE_INIT. */
2180 for (i = 0; i < 5; i++) {
2181 rc = efx_pci_probe_main(efx);
2183 /* Serialise against efx_reset(). No more resets will be
2184 * scheduled since efx_stop_all() has been called, and we
2185 * have not and never have been registered with either
2186 * the rtnetlink or driverlink layers. */
2187 cancel_work_sync(&efx->reset_work);
2190 if (efx->reset_pending != RESET_TYPE_NONE) {
2191 /* If there was a scheduled reset during
2192 * probe, the NIC is probably hosed anyway */
2193 efx_pci_remove_main(efx);
2200 /* Retry if a recoverably reset event has been scheduled */
2201 if ((efx->reset_pending != RESET_TYPE_INVISIBLE) &&
2202 (efx->reset_pending != RESET_TYPE_ALL))
2205 efx->reset_pending = RESET_TYPE_NONE;
2209 EFX_ERR(efx, "Could not reset NIC\n");
2213 /* Switch to the running state before we expose the device to
2214 * the OS. This is to ensure that the initial gathering of
2215 * MAC stats succeeds. */
2216 efx->state = STATE_RUNNING;
2218 efx_mtd_probe(efx); /* allowed to fail */
2220 rc = efx_register_netdev(efx);
2224 EFX_LOG(efx, "initialisation successful\n");
2228 efx_pci_remove_main(efx);
2233 efx_fini_struct(efx);
2235 EFX_LOG(efx, "initialisation failed. rc=%d\n", rc);
2236 free_netdev(net_dev);
2240 static struct pci_driver efx_pci_driver = {
2241 .name = EFX_DRIVER_NAME,
2242 .id_table = efx_pci_table,
2243 .probe = efx_pci_probe,
2244 .remove = efx_pci_remove,
2247 /**************************************************************************
2249 * Kernel module interface
2251 *************************************************************************/
2253 module_param(interrupt_mode, uint, 0444);
2254 MODULE_PARM_DESC(interrupt_mode,
2255 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2257 static int __init efx_init_module(void)
2261 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2263 rc = register_netdevice_notifier(&efx_netdev_notifier);
2267 refill_workqueue = create_workqueue("sfc_refill");
2268 if (!refill_workqueue) {
2272 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2273 if (!reset_workqueue) {
2278 rc = pci_register_driver(&efx_pci_driver);
2285 destroy_workqueue(reset_workqueue);
2287 destroy_workqueue(refill_workqueue);
2289 unregister_netdevice_notifier(&efx_netdev_notifier);
2294 static void __exit efx_exit_module(void)
2296 printk(KERN_INFO "Solarflare NET driver unloading\n");
2298 pci_unregister_driver(&efx_pci_driver);
2299 destroy_workqueue(reset_workqueue);
2300 destroy_workqueue(refill_workqueue);
2301 unregister_netdevice_notifier(&efx_netdev_notifier);
2305 module_init(efx_init_module);
2306 module_exit(efx_exit_module);
2308 MODULE_AUTHOR("Michael Brown <mbrown@fensystems.co.uk> and "
2309 "Solarflare Communications");
2310 MODULE_DESCRIPTION("Solarflare Communications network driver");
2311 MODULE_LICENSE("GPL");
2312 MODULE_DEVICE_TABLE(pci, efx_pci_table);