sfc: Make reset_workqueue driver-global rather than per-NIC
[safe/jmp/linux-2.6] / drivers / net / sfc / efx.c
1 /****************************************************************************
2  * Driver for Solarflare Solarstorm network controllers and boards
3  * Copyright 2005-2006 Fen Systems Ltd.
4  * Copyright 2005-2008 Solarflare Communications Inc.
5  *
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.
9  */
10
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>
17 #include <linux/ip.h>
18 #include <linux/tcp.h>
19 #include <linux/in.h>
20 #include <linux/crc32.h>
21 #include <linux/ethtool.h>
22 #include <linux/topology.h>
23 #include "net_driver.h"
24 #include "gmii.h"
25 #include "ethtool.h"
26 #include "tx.h"
27 #include "rx.h"
28 #include "efx.h"
29 #include "mdio_10g.h"
30 #include "falcon.h"
31 #include "mac.h"
32
33 #define EFX_MAX_MTU (9 * 1024)
34
35 /* RX slow fill workqueue. If memory allocation fails in the fast path,
36  * a work item is pushed onto this work queue to retry the allocation later,
37  * to avoid the NIC being starved of RX buffers. Since this is a per cpu
38  * workqueue, there is nothing to be gained in making it per NIC
39  */
40 static struct workqueue_struct *refill_workqueue;
41
42 /* Reset workqueue. If any NIC has a hardware failure then a reset will be
43  * queued onto this work queue. This is not a per-nic work queue, because
44  * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
45  */
46 static struct workqueue_struct *reset_workqueue;
47
48 /**************************************************************************
49  *
50  * Configurable values
51  *
52  *************************************************************************/
53
54 /*
55  * Enable large receive offload (LRO) aka soft segment reassembly (SSR)
56  *
57  * This sets the default for new devices.  It can be controlled later
58  * using ethtool.
59  */
60 static int lro = true;
61 module_param(lro, int, 0644);
62 MODULE_PARM_DESC(lro, "Large receive offload acceleration");
63
64 /*
65  * Use separate channels for TX and RX events
66  *
67  * Set this to 1 to use separate channels for TX and RX. It allows us to
68  * apply a higher level of interrupt moderation to TX events.
69  *
70  * This is forced to 0 for MSI interrupt mode as the interrupt vector
71  * is not written
72  */
73 static unsigned int separate_tx_and_rx_channels = true;
74
75 /* This is the weight assigned to each of the (per-channel) virtual
76  * NAPI devices.
77  */
78 static int napi_weight = 64;
79
80 /* This is the time (in jiffies) between invocations of the hardware
81  * monitor, which checks for known hardware bugs and resets the
82  * hardware and driver as necessary.
83  */
84 unsigned int efx_monitor_interval = 1 * HZ;
85
86 /* This controls whether or not the driver will initialise devices
87  * with invalid MAC addresses stored in the EEPROM or flash.  If true,
88  * such devices will be initialised with a random locally-generated
89  * MAC address.  This allows for loading the sfc_mtd driver to
90  * reprogram the flash, even if the flash contents (including the MAC
91  * address) have previously been erased.
92  */
93 static unsigned int allow_bad_hwaddr;
94
95 /* Initial interrupt moderation settings.  They can be modified after
96  * module load with ethtool.
97  *
98  * The default for RX should strike a balance between increasing the
99  * round-trip latency and reducing overhead.
100  */
101 static unsigned int rx_irq_mod_usec = 60;
102
103 /* Initial interrupt moderation settings.  They can be modified after
104  * module load with ethtool.
105  *
106  * This default is chosen to ensure that a 10G link does not go idle
107  * while a TX queue is stopped after it has become full.  A queue is
108  * restarted when it drops below half full.  The time this takes (assuming
109  * worst case 3 descriptors per packet and 1024 descriptors) is
110  *   512 / 3 * 1.2 = 205 usec.
111  */
112 static unsigned int tx_irq_mod_usec = 150;
113
114 /* This is the first interrupt mode to try out of:
115  * 0 => MSI-X
116  * 1 => MSI
117  * 2 => legacy
118  */
119 static unsigned int interrupt_mode;
120
121 /* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
122  * i.e. the number of CPUs among which we may distribute simultaneous
123  * interrupt handling.
124  *
125  * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
126  * The default (0) means to assign an interrupt to each package (level II cache)
127  */
128 static unsigned int rss_cpus;
129 module_param(rss_cpus, uint, 0444);
130 MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
131
132 /**************************************************************************
133  *
134  * Utility functions and prototypes
135  *
136  *************************************************************************/
137 static void efx_remove_channel(struct efx_channel *channel);
138 static void efx_remove_port(struct efx_nic *efx);
139 static void efx_fini_napi(struct efx_nic *efx);
140 static void efx_fini_channels(struct efx_nic *efx);
141
142 #define EFX_ASSERT_RESET_SERIALISED(efx)                \
143         do {                                            \
144                 if (efx->state == STATE_RUNNING)        \
145                         ASSERT_RTNL();                  \
146         } while (0)
147
148 /**************************************************************************
149  *
150  * Event queue processing
151  *
152  *************************************************************************/
153
154 /* Process channel's event queue
155  *
156  * This function is responsible for processing the event queue of a
157  * single channel.  The caller must guarantee that this function will
158  * never be concurrently called more than once on the same channel,
159  * though different channels may be being processed concurrently.
160  */
161 static int efx_process_channel(struct efx_channel *channel, int rx_quota)
162 {
163         struct efx_nic *efx = channel->efx;
164         int rx_packets;
165
166         if (unlikely(efx->reset_pending != RESET_TYPE_NONE ||
167                      !channel->enabled))
168                 return 0;
169
170         rx_packets = falcon_process_eventq(channel, rx_quota);
171         if (rx_packets == 0)
172                 return 0;
173
174         /* Deliver last RX packet. */
175         if (channel->rx_pkt) {
176                 __efx_rx_packet(channel, channel->rx_pkt,
177                                 channel->rx_pkt_csummed);
178                 channel->rx_pkt = NULL;
179         }
180
181         efx_flush_lro(channel);
182         efx_rx_strategy(channel);
183
184         efx_fast_push_rx_descriptors(&efx->rx_queue[channel->channel]);
185
186         return rx_packets;
187 }
188
189 /* Mark channel as finished processing
190  *
191  * Note that since we will not receive further interrupts for this
192  * channel before we finish processing and call the eventq_read_ack()
193  * method, there is no need to use the interrupt hold-off timers.
194  */
195 static inline void efx_channel_processed(struct efx_channel *channel)
196 {
197         /* The interrupt handler for this channel may set work_pending
198          * as soon as we acknowledge the events we've seen.  Make sure
199          * it's cleared before then. */
200         channel->work_pending = false;
201         smp_wmb();
202
203         falcon_eventq_read_ack(channel);
204 }
205
206 /* NAPI poll handler
207  *
208  * NAPI guarantees serialisation of polls of the same device, which
209  * provides the guarantee required by efx_process_channel().
210  */
211 static int efx_poll(struct napi_struct *napi, int budget)
212 {
213         struct efx_channel *channel =
214                 container_of(napi, struct efx_channel, napi_str);
215         struct net_device *napi_dev = channel->napi_dev;
216         int rx_packets;
217
218         EFX_TRACE(channel->efx, "channel %d NAPI poll executing on CPU %d\n",
219                   channel->channel, raw_smp_processor_id());
220
221         rx_packets = efx_process_channel(channel, budget);
222
223         if (rx_packets < budget) {
224                 /* There is no race here; although napi_disable() will
225                  * only wait for netif_rx_complete(), this isn't a problem
226                  * since efx_channel_processed() will have no effect if
227                  * interrupts have already been disabled.
228                  */
229                 netif_rx_complete(napi_dev, napi);
230                 efx_channel_processed(channel);
231         }
232
233         return rx_packets;
234 }
235
236 /* Process the eventq of the specified channel immediately on this CPU
237  *
238  * Disable hardware generated interrupts, wait for any existing
239  * processing to finish, then directly poll (and ack ) the eventq.
240  * Finally reenable NAPI and interrupts.
241  *
242  * Since we are touching interrupts the caller should hold the suspend lock
243  */
244 void efx_process_channel_now(struct efx_channel *channel)
245 {
246         struct efx_nic *efx = channel->efx;
247
248         BUG_ON(!channel->used_flags);
249         BUG_ON(!channel->enabled);
250
251         /* Disable interrupts and wait for ISRs to complete */
252         falcon_disable_interrupts(efx);
253         if (efx->legacy_irq)
254                 synchronize_irq(efx->legacy_irq);
255         if (channel->irq)
256                 synchronize_irq(channel->irq);
257
258         /* Wait for any NAPI processing to complete */
259         napi_disable(&channel->napi_str);
260
261         /* Poll the channel */
262         efx_process_channel(channel, efx->type->evq_size);
263
264         /* Ack the eventq. This may cause an interrupt to be generated
265          * when they are reenabled */
266         efx_channel_processed(channel);
267
268         napi_enable(&channel->napi_str);
269         falcon_enable_interrupts(efx);
270 }
271
272 /* Create event queue
273  * Event queue memory allocations are done only once.  If the channel
274  * is reset, the memory buffer will be reused; this guards against
275  * errors during channel reset and also simplifies interrupt handling.
276  */
277 static int efx_probe_eventq(struct efx_channel *channel)
278 {
279         EFX_LOG(channel->efx, "chan %d create event queue\n", channel->channel);
280
281         return falcon_probe_eventq(channel);
282 }
283
284 /* Prepare channel's event queue */
285 static void efx_init_eventq(struct efx_channel *channel)
286 {
287         EFX_LOG(channel->efx, "chan %d init event queue\n", channel->channel);
288
289         channel->eventq_read_ptr = 0;
290
291         falcon_init_eventq(channel);
292 }
293
294 static void efx_fini_eventq(struct efx_channel *channel)
295 {
296         EFX_LOG(channel->efx, "chan %d fini event queue\n", channel->channel);
297
298         falcon_fini_eventq(channel);
299 }
300
301 static void efx_remove_eventq(struct efx_channel *channel)
302 {
303         EFX_LOG(channel->efx, "chan %d remove event queue\n", channel->channel);
304
305         falcon_remove_eventq(channel);
306 }
307
308 /**************************************************************************
309  *
310  * Channel handling
311  *
312  *************************************************************************/
313
314 static int efx_probe_channel(struct efx_channel *channel)
315 {
316         struct efx_tx_queue *tx_queue;
317         struct efx_rx_queue *rx_queue;
318         int rc;
319
320         EFX_LOG(channel->efx, "creating channel %d\n", channel->channel);
321
322         rc = efx_probe_eventq(channel);
323         if (rc)
324                 goto fail1;
325
326         efx_for_each_channel_tx_queue(tx_queue, channel) {
327                 rc = efx_probe_tx_queue(tx_queue);
328                 if (rc)
329                         goto fail2;
330         }
331
332         efx_for_each_channel_rx_queue(rx_queue, channel) {
333                 rc = efx_probe_rx_queue(rx_queue);
334                 if (rc)
335                         goto fail3;
336         }
337
338         channel->n_rx_frm_trunc = 0;
339
340         return 0;
341
342  fail3:
343         efx_for_each_channel_rx_queue(rx_queue, channel)
344                 efx_remove_rx_queue(rx_queue);
345  fail2:
346         efx_for_each_channel_tx_queue(tx_queue, channel)
347                 efx_remove_tx_queue(tx_queue);
348  fail1:
349         return rc;
350 }
351
352
353 /* Channels are shutdown and reinitialised whilst the NIC is running
354  * to propagate configuration changes (mtu, checksum offload), or
355  * to clear hardware error conditions
356  */
357 static void efx_init_channels(struct efx_nic *efx)
358 {
359         struct efx_tx_queue *tx_queue;
360         struct efx_rx_queue *rx_queue;
361         struct efx_channel *channel;
362
363         /* Calculate the rx buffer allocation parameters required to
364          * support the current MTU, including padding for header
365          * alignment and overruns.
366          */
367         efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
368                               EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
369                               efx->type->rx_buffer_padding);
370         efx->rx_buffer_order = get_order(efx->rx_buffer_len);
371
372         /* Initialise the channels */
373         efx_for_each_channel(channel, efx) {
374                 EFX_LOG(channel->efx, "init chan %d\n", channel->channel);
375
376                 efx_init_eventq(channel);
377
378                 efx_for_each_channel_tx_queue(tx_queue, channel)
379                         efx_init_tx_queue(tx_queue);
380
381                 /* The rx buffer allocation strategy is MTU dependent */
382                 efx_rx_strategy(channel);
383
384                 efx_for_each_channel_rx_queue(rx_queue, channel)
385                         efx_init_rx_queue(rx_queue);
386
387                 WARN_ON(channel->rx_pkt != NULL);
388                 efx_rx_strategy(channel);
389         }
390 }
391
392 /* This enables event queue processing and packet transmission.
393  *
394  * Note that this function is not allowed to fail, since that would
395  * introduce too much complexity into the suspend/resume path.
396  */
397 static void efx_start_channel(struct efx_channel *channel)
398 {
399         struct efx_rx_queue *rx_queue;
400
401         EFX_LOG(channel->efx, "starting chan %d\n", channel->channel);
402
403         if (!(channel->efx->net_dev->flags & IFF_UP))
404                 netif_napi_add(channel->napi_dev, &channel->napi_str,
405                                efx_poll, napi_weight);
406
407         /* The interrupt handler for this channel may set work_pending
408          * as soon as we enable it.  Make sure it's cleared before
409          * then.  Similarly, make sure it sees the enabled flag set. */
410         channel->work_pending = false;
411         channel->enabled = true;
412         smp_wmb();
413
414         napi_enable(&channel->napi_str);
415
416         /* Load up RX descriptors */
417         efx_for_each_channel_rx_queue(rx_queue, channel)
418                 efx_fast_push_rx_descriptors(rx_queue);
419 }
420
421 /* This disables event queue processing and packet transmission.
422  * This function does not guarantee that all queue processing
423  * (e.g. RX refill) is complete.
424  */
425 static void efx_stop_channel(struct efx_channel *channel)
426 {
427         struct efx_rx_queue *rx_queue;
428
429         if (!channel->enabled)
430                 return;
431
432         EFX_LOG(channel->efx, "stop chan %d\n", channel->channel);
433
434         channel->enabled = false;
435         napi_disable(&channel->napi_str);
436
437         /* Ensure that any worker threads have exited or will be no-ops */
438         efx_for_each_channel_rx_queue(rx_queue, channel) {
439                 spin_lock_bh(&rx_queue->add_lock);
440                 spin_unlock_bh(&rx_queue->add_lock);
441         }
442 }
443
444 static void efx_fini_channels(struct efx_nic *efx)
445 {
446         struct efx_channel *channel;
447         struct efx_tx_queue *tx_queue;
448         struct efx_rx_queue *rx_queue;
449         int rc;
450
451         EFX_ASSERT_RESET_SERIALISED(efx);
452         BUG_ON(efx->port_enabled);
453
454         rc = falcon_flush_queues(efx);
455         if (rc)
456                 EFX_ERR(efx, "failed to flush queues\n");
457         else
458                 EFX_LOG(efx, "successfully flushed all queues\n");
459
460         efx_for_each_channel(channel, efx) {
461                 EFX_LOG(channel->efx, "shut down chan %d\n", channel->channel);
462
463                 efx_for_each_channel_rx_queue(rx_queue, channel)
464                         efx_fini_rx_queue(rx_queue);
465                 efx_for_each_channel_tx_queue(tx_queue, channel)
466                         efx_fini_tx_queue(tx_queue);
467                 efx_fini_eventq(channel);
468         }
469 }
470
471 static void efx_remove_channel(struct efx_channel *channel)
472 {
473         struct efx_tx_queue *tx_queue;
474         struct efx_rx_queue *rx_queue;
475
476         EFX_LOG(channel->efx, "destroy chan %d\n", channel->channel);
477
478         efx_for_each_channel_rx_queue(rx_queue, channel)
479                 efx_remove_rx_queue(rx_queue);
480         efx_for_each_channel_tx_queue(tx_queue, channel)
481                 efx_remove_tx_queue(tx_queue);
482         efx_remove_eventq(channel);
483
484         channel->used_flags = 0;
485 }
486
487 void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue, int delay)
488 {
489         queue_delayed_work(refill_workqueue, &rx_queue->work, delay);
490 }
491
492 /**************************************************************************
493  *
494  * Port handling
495  *
496  **************************************************************************/
497
498 /* This ensures that the kernel is kept informed (via
499  * netif_carrier_on/off) of the link status, and also maintains the
500  * link status's stop on the port's TX queue.
501  */
502 static void efx_link_status_changed(struct efx_nic *efx)
503 {
504         /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
505          * that no events are triggered between unregister_netdev() and the
506          * driver unloading. A more general condition is that NETDEV_CHANGE
507          * can only be generated between NETDEV_UP and NETDEV_DOWN */
508         if (!netif_running(efx->net_dev))
509                 return;
510
511         if (efx->port_inhibited) {
512                 netif_carrier_off(efx->net_dev);
513                 return;
514         }
515
516         if (efx->link_up != netif_carrier_ok(efx->net_dev)) {
517                 efx->n_link_state_changes++;
518
519                 if (efx->link_up)
520                         netif_carrier_on(efx->net_dev);
521                 else
522                         netif_carrier_off(efx->net_dev);
523         }
524
525         /* Status message for kernel log */
526         if (efx->link_up) {
527                 struct mii_if_info *gmii = &efx->mii;
528                 unsigned adv, lpa;
529                 /* NONE here means direct XAUI from the controller, with no
530                  * MDIO-attached device we can query. */
531                 if (efx->phy_type != PHY_TYPE_NONE) {
532                         adv = gmii_advertised(gmii);
533                         lpa = gmii_lpa(gmii);
534                 } else {
535                         lpa = GM_LPA_10000 | LPA_DUPLEX;
536                         adv = lpa;
537                 }
538                 EFX_INFO(efx, "link up at %dMbps %s-duplex "
539                          "(adv %04x lpa %04x) (MTU %d)%s\n",
540                          (efx->link_options & GM_LPA_10000 ? 10000 :
541                           (efx->link_options & GM_LPA_1000 ? 1000 :
542                            (efx->link_options & GM_LPA_100 ? 100 :
543                             10))),
544                          (efx->link_options & GM_LPA_DUPLEX ?
545                           "full" : "half"),
546                          adv, lpa,
547                          efx->net_dev->mtu,
548                          (efx->promiscuous ? " [PROMISC]" : ""));
549         } else {
550                 EFX_INFO(efx, "link down\n");
551         }
552
553 }
554
555 /* This call reinitialises the MAC to pick up new PHY settings. The
556  * caller must hold the mac_lock */
557 void __efx_reconfigure_port(struct efx_nic *efx)
558 {
559         WARN_ON(!mutex_is_locked(&efx->mac_lock));
560
561         EFX_LOG(efx, "reconfiguring MAC from PHY settings on CPU %d\n",
562                 raw_smp_processor_id());
563
564         /* Serialise the promiscuous flag with efx_set_multicast_list. */
565         if (efx_dev_registered(efx)) {
566                 netif_addr_lock_bh(efx->net_dev);
567                 netif_addr_unlock_bh(efx->net_dev);
568         }
569
570         falcon_reconfigure_xmac(efx);
571
572         /* Inform kernel of loss/gain of carrier */
573         efx_link_status_changed(efx);
574 }
575
576 /* Reinitialise the MAC to pick up new PHY settings, even if the port is
577  * disabled. */
578 void efx_reconfigure_port(struct efx_nic *efx)
579 {
580         EFX_ASSERT_RESET_SERIALISED(efx);
581
582         mutex_lock(&efx->mac_lock);
583         __efx_reconfigure_port(efx);
584         mutex_unlock(&efx->mac_lock);
585 }
586
587 /* Asynchronous efx_reconfigure_port work item. To speed up efx_flush_all()
588  * we don't efx_reconfigure_port() if the port is disabled. Care is taken
589  * in efx_stop_all() and efx_start_port() to prevent PHY events being lost */
590 static void efx_reconfigure_work(struct work_struct *data)
591 {
592         struct efx_nic *efx = container_of(data, struct efx_nic,
593                                            reconfigure_work);
594
595         mutex_lock(&efx->mac_lock);
596         if (efx->port_enabled)
597                 __efx_reconfigure_port(efx);
598         mutex_unlock(&efx->mac_lock);
599 }
600
601 static int efx_probe_port(struct efx_nic *efx)
602 {
603         int rc;
604
605         EFX_LOG(efx, "create port\n");
606
607         /* Connect up MAC/PHY operations table and read MAC address */
608         rc = falcon_probe_port(efx);
609         if (rc)
610                 goto err;
611
612         /* Sanity check MAC address */
613         if (is_valid_ether_addr(efx->mac_address)) {
614                 memcpy(efx->net_dev->dev_addr, efx->mac_address, ETH_ALEN);
615         } else {
616                 EFX_ERR(efx, "invalid MAC address %pM\n",
617                         efx->mac_address);
618                 if (!allow_bad_hwaddr) {
619                         rc = -EINVAL;
620                         goto err;
621                 }
622                 random_ether_addr(efx->net_dev->dev_addr);
623                 EFX_INFO(efx, "using locally-generated MAC %pM\n",
624                          efx->net_dev->dev_addr);
625         }
626
627         return 0;
628
629  err:
630         efx_remove_port(efx);
631         return rc;
632 }
633
634 static int efx_init_port(struct efx_nic *efx)
635 {
636         int rc;
637
638         EFX_LOG(efx, "init port\n");
639
640         /* Initialise the MAC and PHY */
641         rc = falcon_init_xmac(efx);
642         if (rc)
643                 return rc;
644
645         efx->port_initialized = true;
646         efx->stats_enabled = true;
647
648         /* Reconfigure port to program MAC registers */
649         falcon_reconfigure_xmac(efx);
650
651         return 0;
652 }
653
654 /* Allow efx_reconfigure_port() to be scheduled, and close the window
655  * between efx_stop_port and efx_flush_all whereby a previously scheduled
656  * efx_reconfigure_port() may have been cancelled */
657 static void efx_start_port(struct efx_nic *efx)
658 {
659         EFX_LOG(efx, "start port\n");
660         BUG_ON(efx->port_enabled);
661
662         mutex_lock(&efx->mac_lock);
663         efx->port_enabled = true;
664         __efx_reconfigure_port(efx);
665         mutex_unlock(&efx->mac_lock);
666 }
667
668 /* Prevent efx_reconfigure_work and efx_monitor() from executing, and
669  * efx_set_multicast_list() from scheduling efx_reconfigure_work.
670  * efx_reconfigure_work can still be scheduled via NAPI processing
671  * until efx_flush_all() is called */
672 static void efx_stop_port(struct efx_nic *efx)
673 {
674         EFX_LOG(efx, "stop port\n");
675
676         mutex_lock(&efx->mac_lock);
677         efx->port_enabled = false;
678         mutex_unlock(&efx->mac_lock);
679
680         /* Serialise against efx_set_multicast_list() */
681         if (efx_dev_registered(efx)) {
682                 netif_addr_lock_bh(efx->net_dev);
683                 netif_addr_unlock_bh(efx->net_dev);
684         }
685 }
686
687 static void efx_fini_port(struct efx_nic *efx)
688 {
689         EFX_LOG(efx, "shut down port\n");
690
691         if (!efx->port_initialized)
692                 return;
693
694         falcon_fini_xmac(efx);
695         efx->port_initialized = false;
696
697         efx->link_up = false;
698         efx_link_status_changed(efx);
699 }
700
701 static void efx_remove_port(struct efx_nic *efx)
702 {
703         EFX_LOG(efx, "destroying port\n");
704
705         falcon_remove_port(efx);
706 }
707
708 /**************************************************************************
709  *
710  * NIC handling
711  *
712  **************************************************************************/
713
714 /* This configures the PCI device to enable I/O and DMA. */
715 static int efx_init_io(struct efx_nic *efx)
716 {
717         struct pci_dev *pci_dev = efx->pci_dev;
718         dma_addr_t dma_mask = efx->type->max_dma_mask;
719         int rc;
720
721         EFX_LOG(efx, "initialising I/O\n");
722
723         rc = pci_enable_device(pci_dev);
724         if (rc) {
725                 EFX_ERR(efx, "failed to enable PCI device\n");
726                 goto fail1;
727         }
728
729         pci_set_master(pci_dev);
730
731         /* Set the PCI DMA mask.  Try all possibilities from our
732          * genuine mask down to 32 bits, because some architectures
733          * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
734          * masks event though they reject 46 bit masks.
735          */
736         while (dma_mask > 0x7fffffffUL) {
737                 if (pci_dma_supported(pci_dev, dma_mask) &&
738                     ((rc = pci_set_dma_mask(pci_dev, dma_mask)) == 0))
739                         break;
740                 dma_mask >>= 1;
741         }
742         if (rc) {
743                 EFX_ERR(efx, "could not find a suitable DMA mask\n");
744                 goto fail2;
745         }
746         EFX_LOG(efx, "using DMA mask %llx\n", (unsigned long long) dma_mask);
747         rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
748         if (rc) {
749                 /* pci_set_consistent_dma_mask() is not *allowed* to
750                  * fail with a mask that pci_set_dma_mask() accepted,
751                  * but just in case...
752                  */
753                 EFX_ERR(efx, "failed to set consistent DMA mask\n");
754                 goto fail2;
755         }
756
757         efx->membase_phys = pci_resource_start(efx->pci_dev,
758                                                efx->type->mem_bar);
759         rc = pci_request_region(pci_dev, efx->type->mem_bar, "sfc");
760         if (rc) {
761                 EFX_ERR(efx, "request for memory BAR failed\n");
762                 rc = -EIO;
763                 goto fail3;
764         }
765         efx->membase = ioremap_nocache(efx->membase_phys,
766                                        efx->type->mem_map_size);
767         if (!efx->membase) {
768                 EFX_ERR(efx, "could not map memory BAR %d at %llx+%x\n",
769                         efx->type->mem_bar,
770                         (unsigned long long)efx->membase_phys,
771                         efx->type->mem_map_size);
772                 rc = -ENOMEM;
773                 goto fail4;
774         }
775         EFX_LOG(efx, "memory BAR %u at %llx+%x (virtual %p)\n",
776                 efx->type->mem_bar, (unsigned long long)efx->membase_phys,
777                 efx->type->mem_map_size, efx->membase);
778
779         return 0;
780
781  fail4:
782         pci_release_region(efx->pci_dev, efx->type->mem_bar);
783  fail3:
784         efx->membase_phys = 0;
785  fail2:
786         pci_disable_device(efx->pci_dev);
787  fail1:
788         return rc;
789 }
790
791 static void efx_fini_io(struct efx_nic *efx)
792 {
793         EFX_LOG(efx, "shutting down I/O\n");
794
795         if (efx->membase) {
796                 iounmap(efx->membase);
797                 efx->membase = NULL;
798         }
799
800         if (efx->membase_phys) {
801                 pci_release_region(efx->pci_dev, efx->type->mem_bar);
802                 efx->membase_phys = 0;
803         }
804
805         pci_disable_device(efx->pci_dev);
806 }
807
808 /* Get number of RX queues wanted.  Return number of online CPU
809  * packages in the expectation that an IRQ balancer will spread
810  * interrupts across them. */
811 static int efx_wanted_rx_queues(void)
812 {
813         cpumask_t core_mask;
814         int count;
815         int cpu;
816
817         cpus_clear(core_mask);
818         count = 0;
819         for_each_online_cpu(cpu) {
820                 if (!cpu_isset(cpu, core_mask)) {
821                         ++count;
822                         cpus_or(core_mask, core_mask,
823                                 topology_core_siblings(cpu));
824                 }
825         }
826
827         return count;
828 }
829
830 /* Probe the number and type of interrupts we are able to obtain, and
831  * the resulting numbers of channels and RX queues.
832  */
833 static void efx_probe_interrupts(struct efx_nic *efx)
834 {
835         int max_channels =
836                 min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
837         int rc, i;
838
839         if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
840                 struct msix_entry xentries[EFX_MAX_CHANNELS];
841                 int wanted_ints;
842
843                 /* We want one RX queue and interrupt per CPU package
844                  * (or as specified by the rss_cpus module parameter).
845                  * We will need one channel per interrupt.
846                  */
847                 wanted_ints = rss_cpus ? rss_cpus : efx_wanted_rx_queues();
848                 efx->n_rx_queues = min(wanted_ints, max_channels);
849
850                 for (i = 0; i < efx->n_rx_queues; i++)
851                         xentries[i].entry = i;
852                 rc = pci_enable_msix(efx->pci_dev, xentries, efx->n_rx_queues);
853                 if (rc > 0) {
854                         EFX_BUG_ON_PARANOID(rc >= efx->n_rx_queues);
855                         efx->n_rx_queues = rc;
856                         rc = pci_enable_msix(efx->pci_dev, xentries,
857                                              efx->n_rx_queues);
858                 }
859
860                 if (rc == 0) {
861                         for (i = 0; i < efx->n_rx_queues; i++)
862                                 efx->channel[i].irq = xentries[i].vector;
863                 } else {
864                         /* Fall back to single channel MSI */
865                         efx->interrupt_mode = EFX_INT_MODE_MSI;
866                         EFX_ERR(efx, "could not enable MSI-X\n");
867                 }
868         }
869
870         /* Try single interrupt MSI */
871         if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
872                 efx->n_rx_queues = 1;
873                 rc = pci_enable_msi(efx->pci_dev);
874                 if (rc == 0) {
875                         efx->channel[0].irq = efx->pci_dev->irq;
876                 } else {
877                         EFX_ERR(efx, "could not enable MSI\n");
878                         efx->interrupt_mode = EFX_INT_MODE_LEGACY;
879                 }
880         }
881
882         /* Assume legacy interrupts */
883         if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
884                 efx->n_rx_queues = 1;
885                 efx->legacy_irq = efx->pci_dev->irq;
886         }
887 }
888
889 static void efx_remove_interrupts(struct efx_nic *efx)
890 {
891         struct efx_channel *channel;
892
893         /* Remove MSI/MSI-X interrupts */
894         efx_for_each_channel(channel, efx)
895                 channel->irq = 0;
896         pci_disable_msi(efx->pci_dev);
897         pci_disable_msix(efx->pci_dev);
898
899         /* Remove legacy interrupt */
900         efx->legacy_irq = 0;
901 }
902
903 static void efx_set_channels(struct efx_nic *efx)
904 {
905         struct efx_tx_queue *tx_queue;
906         struct efx_rx_queue *rx_queue;
907
908         efx_for_each_tx_queue(tx_queue, efx) {
909                 if (!EFX_INT_MODE_USE_MSI(efx) && separate_tx_and_rx_channels)
910                         tx_queue->channel = &efx->channel[1];
911                 else
912                         tx_queue->channel = &efx->channel[0];
913                 tx_queue->channel->used_flags |= EFX_USED_BY_TX;
914         }
915
916         efx_for_each_rx_queue(rx_queue, efx) {
917                 rx_queue->channel = &efx->channel[rx_queue->queue];
918                 rx_queue->channel->used_flags |= EFX_USED_BY_RX;
919         }
920 }
921
922 static int efx_probe_nic(struct efx_nic *efx)
923 {
924         int rc;
925
926         EFX_LOG(efx, "creating NIC\n");
927
928         /* Carry out hardware-type specific initialisation */
929         rc = falcon_probe_nic(efx);
930         if (rc)
931                 return rc;
932
933         /* Determine the number of channels and RX queues by trying to hook
934          * in MSI-X interrupts. */
935         efx_probe_interrupts(efx);
936
937         efx_set_channels(efx);
938
939         /* Initialise the interrupt moderation settings */
940         efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec);
941
942         return 0;
943 }
944
945 static void efx_remove_nic(struct efx_nic *efx)
946 {
947         EFX_LOG(efx, "destroying NIC\n");
948
949         efx_remove_interrupts(efx);
950         falcon_remove_nic(efx);
951 }
952
953 /**************************************************************************
954  *
955  * NIC startup/shutdown
956  *
957  *************************************************************************/
958
959 static int efx_probe_all(struct efx_nic *efx)
960 {
961         struct efx_channel *channel;
962         int rc;
963
964         /* Create NIC */
965         rc = efx_probe_nic(efx);
966         if (rc) {
967                 EFX_ERR(efx, "failed to create NIC\n");
968                 goto fail1;
969         }
970
971         /* Create port */
972         rc = efx_probe_port(efx);
973         if (rc) {
974                 EFX_ERR(efx, "failed to create port\n");
975                 goto fail2;
976         }
977
978         /* Create channels */
979         efx_for_each_channel(channel, efx) {
980                 rc = efx_probe_channel(channel);
981                 if (rc) {
982                         EFX_ERR(efx, "failed to create channel %d\n",
983                                 channel->channel);
984                         goto fail3;
985                 }
986         }
987
988         return 0;
989
990  fail3:
991         efx_for_each_channel(channel, efx)
992                 efx_remove_channel(channel);
993         efx_remove_port(efx);
994  fail2:
995         efx_remove_nic(efx);
996  fail1:
997         return rc;
998 }
999
1000 /* Called after previous invocation(s) of efx_stop_all, restarts the
1001  * port, kernel transmit queue, NAPI processing and hardware interrupts,
1002  * and ensures that the port is scheduled to be reconfigured.
1003  * This function is safe to call multiple times when the NIC is in any
1004  * state. */
1005 static void efx_start_all(struct efx_nic *efx)
1006 {
1007         struct efx_channel *channel;
1008
1009         EFX_ASSERT_RESET_SERIALISED(efx);
1010
1011         /* Check that it is appropriate to restart the interface. All
1012          * of these flags are safe to read under just the rtnl lock */
1013         if (efx->port_enabled)
1014                 return;
1015         if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1016                 return;
1017         if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
1018                 return;
1019
1020         /* Mark the port as enabled so port reconfigurations can start, then
1021          * restart the transmit interface early so the watchdog timer stops */
1022         efx_start_port(efx);
1023         if (efx_dev_registered(efx))
1024                 efx_wake_queue(efx);
1025
1026         efx_for_each_channel(channel, efx)
1027                 efx_start_channel(channel);
1028
1029         falcon_enable_interrupts(efx);
1030
1031         /* Start hardware monitor if we're in RUNNING */
1032         if (efx->state == STATE_RUNNING)
1033                 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1034                                    efx_monitor_interval);
1035 }
1036
1037 /* Flush all delayed work. Should only be called when no more delayed work
1038  * will be scheduled. This doesn't flush pending online resets (efx_reset),
1039  * since we're holding the rtnl_lock at this point. */
1040 static void efx_flush_all(struct efx_nic *efx)
1041 {
1042         struct efx_rx_queue *rx_queue;
1043
1044         /* Make sure the hardware monitor is stopped */
1045         cancel_delayed_work_sync(&efx->monitor_work);
1046
1047         /* Ensure that all RX slow refills are complete. */
1048         efx_for_each_rx_queue(rx_queue, efx)
1049                 cancel_delayed_work_sync(&rx_queue->work);
1050
1051         /* Stop scheduled port reconfigurations */
1052         cancel_work_sync(&efx->reconfigure_work);
1053
1054 }
1055
1056 /* Quiesce hardware and software without bringing the link down.
1057  * Safe to call multiple times, when the nic and interface is in any
1058  * state. The caller is guaranteed to subsequently be in a position
1059  * to modify any hardware and software state they see fit without
1060  * taking locks. */
1061 static void efx_stop_all(struct efx_nic *efx)
1062 {
1063         struct efx_channel *channel;
1064
1065         EFX_ASSERT_RESET_SERIALISED(efx);
1066
1067         /* port_enabled can be read safely under the rtnl lock */
1068         if (!efx->port_enabled)
1069                 return;
1070
1071         /* Disable interrupts and wait for ISR to complete */
1072         falcon_disable_interrupts(efx);
1073         if (efx->legacy_irq)
1074                 synchronize_irq(efx->legacy_irq);
1075         efx_for_each_channel(channel, efx) {
1076                 if (channel->irq)
1077                         synchronize_irq(channel->irq);
1078         }
1079
1080         /* Stop all NAPI processing and synchronous rx refills */
1081         efx_for_each_channel(channel, efx)
1082                 efx_stop_channel(channel);
1083
1084         /* Stop all asynchronous port reconfigurations. Since all
1085          * event processing has already been stopped, there is no
1086          * window to loose phy events */
1087         efx_stop_port(efx);
1088
1089         /* Flush reconfigure_work, refill_workqueue, monitor_work */
1090         efx_flush_all(efx);
1091
1092         /* Isolate the MAC from the TX and RX engines, so that queue
1093          * flushes will complete in a timely fashion. */
1094         falcon_drain_tx_fifo(efx);
1095
1096         /* Stop the kernel transmit interface late, so the watchdog
1097          * timer isn't ticking over the flush */
1098         if (efx_dev_registered(efx)) {
1099                 efx_stop_queue(efx);
1100                 netif_tx_lock_bh(efx->net_dev);
1101                 netif_tx_unlock_bh(efx->net_dev);
1102         }
1103 }
1104
1105 static void efx_remove_all(struct efx_nic *efx)
1106 {
1107         struct efx_channel *channel;
1108
1109         efx_for_each_channel(channel, efx)
1110                 efx_remove_channel(channel);
1111         efx_remove_port(efx);
1112         efx_remove_nic(efx);
1113 }
1114
1115 /* A convinience function to safely flush all the queues */
1116 void efx_flush_queues(struct efx_nic *efx)
1117 {
1118         EFX_ASSERT_RESET_SERIALISED(efx);
1119
1120         efx_stop_all(efx);
1121
1122         efx_fini_channels(efx);
1123         efx_init_channels(efx);
1124
1125         efx_start_all(efx);
1126 }
1127
1128 /**************************************************************************
1129  *
1130  * Interrupt moderation
1131  *
1132  **************************************************************************/
1133
1134 /* Set interrupt moderation parameters */
1135 void efx_init_irq_moderation(struct efx_nic *efx, int tx_usecs, int rx_usecs)
1136 {
1137         struct efx_tx_queue *tx_queue;
1138         struct efx_rx_queue *rx_queue;
1139
1140         EFX_ASSERT_RESET_SERIALISED(efx);
1141
1142         efx_for_each_tx_queue(tx_queue, efx)
1143                 tx_queue->channel->irq_moderation = tx_usecs;
1144
1145         efx_for_each_rx_queue(rx_queue, efx)
1146                 rx_queue->channel->irq_moderation = rx_usecs;
1147 }
1148
1149 /**************************************************************************
1150  *
1151  * Hardware monitor
1152  *
1153  **************************************************************************/
1154
1155 /* Run periodically off the general workqueue. Serialised against
1156  * efx_reconfigure_port via the mac_lock */
1157 static void efx_monitor(struct work_struct *data)
1158 {
1159         struct efx_nic *efx = container_of(data, struct efx_nic,
1160                                            monitor_work.work);
1161         int rc = 0;
1162
1163         EFX_TRACE(efx, "hardware monitor executing on CPU %d\n",
1164                   raw_smp_processor_id());
1165
1166
1167         /* If the mac_lock is already held then it is likely a port
1168          * reconfiguration is already in place, which will likely do
1169          * most of the work of check_hw() anyway. */
1170         if (!mutex_trylock(&efx->mac_lock)) {
1171                 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1172                                    efx_monitor_interval);
1173                 return;
1174         }
1175
1176         if (efx->port_enabled)
1177                 rc = falcon_check_xmac(efx);
1178         mutex_unlock(&efx->mac_lock);
1179
1180         queue_delayed_work(efx->workqueue, &efx->monitor_work,
1181                            efx_monitor_interval);
1182 }
1183
1184 /**************************************************************************
1185  *
1186  * ioctls
1187  *
1188  *************************************************************************/
1189
1190 /* Net device ioctl
1191  * Context: process, rtnl_lock() held.
1192  */
1193 static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1194 {
1195         struct efx_nic *efx = netdev_priv(net_dev);
1196
1197         EFX_ASSERT_RESET_SERIALISED(efx);
1198
1199         return generic_mii_ioctl(&efx->mii, if_mii(ifr), cmd, NULL);
1200 }
1201
1202 /**************************************************************************
1203  *
1204  * NAPI interface
1205  *
1206  **************************************************************************/
1207
1208 static int efx_init_napi(struct efx_nic *efx)
1209 {
1210         struct efx_channel *channel;
1211         int rc;
1212
1213         efx_for_each_channel(channel, efx) {
1214                 channel->napi_dev = efx->net_dev;
1215                 rc = efx_lro_init(&channel->lro_mgr, efx);
1216                 if (rc)
1217                         goto err;
1218         }
1219         return 0;
1220  err:
1221         efx_fini_napi(efx);
1222         return rc;
1223 }
1224
1225 static void efx_fini_napi(struct efx_nic *efx)
1226 {
1227         struct efx_channel *channel;
1228
1229         efx_for_each_channel(channel, efx) {
1230                 efx_lro_fini(&channel->lro_mgr);
1231                 channel->napi_dev = NULL;
1232         }
1233 }
1234
1235 /**************************************************************************
1236  *
1237  * Kernel netpoll interface
1238  *
1239  *************************************************************************/
1240
1241 #ifdef CONFIG_NET_POLL_CONTROLLER
1242
1243 /* Although in the common case interrupts will be disabled, this is not
1244  * guaranteed. However, all our work happens inside the NAPI callback,
1245  * so no locking is required.
1246  */
1247 static void efx_netpoll(struct net_device *net_dev)
1248 {
1249         struct efx_nic *efx = netdev_priv(net_dev);
1250         struct efx_channel *channel;
1251
1252         efx_for_each_channel(channel, efx)
1253                 efx_schedule_channel(channel);
1254 }
1255
1256 #endif
1257
1258 /**************************************************************************
1259  *
1260  * Kernel net device interface
1261  *
1262  *************************************************************************/
1263
1264 /* Context: process, rtnl_lock() held. */
1265 static int efx_net_open(struct net_device *net_dev)
1266 {
1267         struct efx_nic *efx = netdev_priv(net_dev);
1268         EFX_ASSERT_RESET_SERIALISED(efx);
1269
1270         EFX_LOG(efx, "opening device %s on CPU %d\n", net_dev->name,
1271                 raw_smp_processor_id());
1272
1273         if (efx->phy_mode & PHY_MODE_SPECIAL)
1274                 return -EBUSY;
1275
1276         efx_start_all(efx);
1277         return 0;
1278 }
1279
1280 /* Context: process, rtnl_lock() held.
1281  * Note that the kernel will ignore our return code; this method
1282  * should really be a void.
1283  */
1284 static int efx_net_stop(struct net_device *net_dev)
1285 {
1286         struct efx_nic *efx = netdev_priv(net_dev);
1287
1288         EFX_LOG(efx, "closing %s on CPU %d\n", net_dev->name,
1289                 raw_smp_processor_id());
1290
1291         /* Stop the device and flush all the channels */
1292         efx_stop_all(efx);
1293         efx_fini_channels(efx);
1294         efx_init_channels(efx);
1295
1296         return 0;
1297 }
1298
1299 /* Context: process, dev_base_lock or RTNL held, non-blocking. */
1300 static struct net_device_stats *efx_net_stats(struct net_device *net_dev)
1301 {
1302         struct efx_nic *efx = netdev_priv(net_dev);
1303         struct efx_mac_stats *mac_stats = &efx->mac_stats;
1304         struct net_device_stats *stats = &net_dev->stats;
1305
1306         /* Update stats if possible, but do not wait if another thread
1307          * is updating them (or resetting the NIC); slightly stale
1308          * stats are acceptable.
1309          */
1310         if (!spin_trylock(&efx->stats_lock))
1311                 return stats;
1312         if (efx->stats_enabled) {
1313                 falcon_update_stats_xmac(efx);
1314                 falcon_update_nic_stats(efx);
1315         }
1316         spin_unlock(&efx->stats_lock);
1317
1318         stats->rx_packets = mac_stats->rx_packets;
1319         stats->tx_packets = mac_stats->tx_packets;
1320         stats->rx_bytes = mac_stats->rx_bytes;
1321         stats->tx_bytes = mac_stats->tx_bytes;
1322         stats->multicast = mac_stats->rx_multicast;
1323         stats->collisions = mac_stats->tx_collision;
1324         stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1325                                    mac_stats->rx_length_error);
1326         stats->rx_over_errors = efx->n_rx_nodesc_drop_cnt;
1327         stats->rx_crc_errors = mac_stats->rx_bad;
1328         stats->rx_frame_errors = mac_stats->rx_align_error;
1329         stats->rx_fifo_errors = mac_stats->rx_overflow;
1330         stats->rx_missed_errors = mac_stats->rx_missed;
1331         stats->tx_window_errors = mac_stats->tx_late_collision;
1332
1333         stats->rx_errors = (stats->rx_length_errors +
1334                             stats->rx_over_errors +
1335                             stats->rx_crc_errors +
1336                             stats->rx_frame_errors +
1337                             stats->rx_fifo_errors +
1338                             stats->rx_missed_errors +
1339                             mac_stats->rx_symbol_error);
1340         stats->tx_errors = (stats->tx_window_errors +
1341                             mac_stats->tx_bad);
1342
1343         return stats;
1344 }
1345
1346 /* Context: netif_tx_lock held, BHs disabled. */
1347 static void efx_watchdog(struct net_device *net_dev)
1348 {
1349         struct efx_nic *efx = netdev_priv(net_dev);
1350
1351         EFX_ERR(efx, "TX stuck with stop_count=%d port_enabled=%d:"
1352                 " resetting channels\n",
1353                 atomic_read(&efx->netif_stop_count), efx->port_enabled);
1354
1355         efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
1356 }
1357
1358
1359 /* Context: process, rtnl_lock() held. */
1360 static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1361 {
1362         struct efx_nic *efx = netdev_priv(net_dev);
1363         int rc = 0;
1364
1365         EFX_ASSERT_RESET_SERIALISED(efx);
1366
1367         if (new_mtu > EFX_MAX_MTU)
1368                 return -EINVAL;
1369
1370         efx_stop_all(efx);
1371
1372         EFX_LOG(efx, "changing MTU to %d\n", new_mtu);
1373
1374         efx_fini_channels(efx);
1375         net_dev->mtu = new_mtu;
1376         efx_init_channels(efx);
1377
1378         efx_start_all(efx);
1379         return rc;
1380 }
1381
1382 static int efx_set_mac_address(struct net_device *net_dev, void *data)
1383 {
1384         struct efx_nic *efx = netdev_priv(net_dev);
1385         struct sockaddr *addr = data;
1386         char *new_addr = addr->sa_data;
1387
1388         EFX_ASSERT_RESET_SERIALISED(efx);
1389
1390         if (!is_valid_ether_addr(new_addr)) {
1391                 EFX_ERR(efx, "invalid ethernet MAC address requested: %pM\n",
1392                         new_addr);
1393                 return -EINVAL;
1394         }
1395
1396         memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1397
1398         /* Reconfigure the MAC */
1399         efx_reconfigure_port(efx);
1400
1401         return 0;
1402 }
1403
1404 /* Context: netif_addr_lock held, BHs disabled. */
1405 static void efx_set_multicast_list(struct net_device *net_dev)
1406 {
1407         struct efx_nic *efx = netdev_priv(net_dev);
1408         struct dev_mc_list *mc_list = net_dev->mc_list;
1409         union efx_multicast_hash *mc_hash = &efx->multicast_hash;
1410         bool promiscuous = !!(net_dev->flags & IFF_PROMISC);
1411         bool changed = (efx->promiscuous != promiscuous);
1412         u32 crc;
1413         int bit;
1414         int i;
1415
1416         efx->promiscuous = promiscuous;
1417
1418         /* Build multicast hash table */
1419         if (promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
1420                 memset(mc_hash, 0xff, sizeof(*mc_hash));
1421         } else {
1422                 memset(mc_hash, 0x00, sizeof(*mc_hash));
1423                 for (i = 0; i < net_dev->mc_count; i++) {
1424                         crc = ether_crc_le(ETH_ALEN, mc_list->dmi_addr);
1425                         bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1426                         set_bit_le(bit, mc_hash->byte);
1427                         mc_list = mc_list->next;
1428                 }
1429         }
1430
1431         if (!efx->port_enabled)
1432                 /* Delay pushing settings until efx_start_port() */
1433                 return;
1434
1435         if (changed)
1436                 queue_work(efx->workqueue, &efx->reconfigure_work);
1437
1438         /* Create and activate new global multicast hash table */
1439         falcon_set_multicast_hash(efx);
1440 }
1441
1442 static const struct net_device_ops efx_netdev_ops = {
1443         .ndo_open               = efx_net_open,
1444         .ndo_stop               = efx_net_stop,
1445         .ndo_get_stats          = efx_net_stats,
1446         .ndo_tx_timeout         = efx_watchdog,
1447         .ndo_start_xmit         = efx_hard_start_xmit,
1448         .ndo_validate_addr      = eth_validate_addr,
1449         .ndo_do_ioctl           = efx_ioctl,
1450         .ndo_change_mtu         = efx_change_mtu,
1451         .ndo_set_mac_address    = efx_set_mac_address,
1452         .ndo_set_multicast_list = efx_set_multicast_list,
1453 #ifdef CONFIG_NET_POLL_CONTROLLER
1454         .ndo_poll_controller = efx_netpoll,
1455 #endif
1456 };
1457
1458 static int efx_netdev_event(struct notifier_block *this,
1459                             unsigned long event, void *ptr)
1460 {
1461         struct net_device *net_dev = ptr;
1462
1463         if (net_dev->netdev_ops == &efx_netdev_ops && event == NETDEV_CHANGENAME) {
1464                 struct efx_nic *efx = netdev_priv(net_dev);
1465
1466                 strcpy(efx->name, net_dev->name);
1467                 efx_mtd_rename(efx);
1468         }
1469
1470         return NOTIFY_DONE;
1471 }
1472
1473 static struct notifier_block efx_netdev_notifier = {
1474         .notifier_call = efx_netdev_event,
1475 };
1476
1477 static int efx_register_netdev(struct efx_nic *efx)
1478 {
1479         struct net_device *net_dev = efx->net_dev;
1480         int rc;
1481
1482         net_dev->watchdog_timeo = 5 * HZ;
1483         net_dev->irq = efx->pci_dev->irq;
1484         net_dev->netdev_ops = &efx_netdev_ops;
1485         SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
1486         SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
1487
1488         /* Always start with carrier off; PHY events will detect the link */
1489         netif_carrier_off(efx->net_dev);
1490
1491         /* Clear MAC statistics */
1492         falcon_update_stats_xmac(efx);
1493         memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
1494
1495         rc = register_netdev(net_dev);
1496         if (rc) {
1497                 EFX_ERR(efx, "could not register net dev\n");
1498                 return rc;
1499         }
1500         strcpy(efx->name, net_dev->name);
1501
1502         return 0;
1503 }
1504
1505 static void efx_unregister_netdev(struct efx_nic *efx)
1506 {
1507         struct efx_tx_queue *tx_queue;
1508
1509         if (!efx->net_dev)
1510                 return;
1511
1512         BUG_ON(netdev_priv(efx->net_dev) != efx);
1513
1514         /* Free up any skbs still remaining. This has to happen before
1515          * we try to unregister the netdev as running their destructors
1516          * may be needed to get the device ref. count to 0. */
1517         efx_for_each_tx_queue(tx_queue, efx)
1518                 efx_release_tx_buffers(tx_queue);
1519
1520         if (efx_dev_registered(efx)) {
1521                 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
1522                 unregister_netdev(efx->net_dev);
1523         }
1524 }
1525
1526 /**************************************************************************
1527  *
1528  * Device reset and suspend
1529  *
1530  **************************************************************************/
1531
1532 /* Tears down the entire software state and most of the hardware state
1533  * before reset.  */
1534 void efx_reset_down(struct efx_nic *efx, struct ethtool_cmd *ecmd)
1535 {
1536         int rc;
1537
1538         EFX_ASSERT_RESET_SERIALISED(efx);
1539
1540         /* The net_dev->get_stats handler is quite slow, and will fail
1541          * if a fetch is pending over reset. Serialise against it. */
1542         spin_lock(&efx->stats_lock);
1543         efx->stats_enabled = false;
1544         spin_unlock(&efx->stats_lock);
1545
1546         efx_stop_all(efx);
1547         mutex_lock(&efx->mac_lock);
1548         mutex_lock(&efx->spi_lock);
1549
1550         rc = falcon_xmac_get_settings(efx, ecmd);
1551         if (rc)
1552                 EFX_ERR(efx, "could not back up PHY settings\n");
1553
1554         efx_fini_channels(efx);
1555 }
1556
1557 /* This function will always ensure that the locks acquired in
1558  * efx_reset_down() are released. A failure return code indicates
1559  * that we were unable to reinitialise the hardware, and the
1560  * driver should be disabled. If ok is false, then the rx and tx
1561  * engines are not restarted, pending a RESET_DISABLE. */
1562 int efx_reset_up(struct efx_nic *efx, struct ethtool_cmd *ecmd, bool ok)
1563 {
1564         int rc;
1565
1566         EFX_ASSERT_RESET_SERIALISED(efx);
1567
1568         rc = falcon_init_nic(efx);
1569         if (rc) {
1570                 EFX_ERR(efx, "failed to initialise NIC\n");
1571                 ok = false;
1572         }
1573
1574         if (ok) {
1575                 efx_init_channels(efx);
1576
1577                 if (falcon_xmac_set_settings(efx, ecmd))
1578                         EFX_ERR(efx, "could not restore PHY settings\n");
1579         }
1580
1581         mutex_unlock(&efx->spi_lock);
1582         mutex_unlock(&efx->mac_lock);
1583
1584         if (ok) {
1585                 efx_start_all(efx);
1586                 efx->stats_enabled = true;
1587         }
1588         return rc;
1589 }
1590
1591 /* Reset the NIC as transparently as possible. Do not reset the PHY
1592  * Note that the reset may fail, in which case the card will be left
1593  * in a most-probably-unusable state.
1594  *
1595  * This function will sleep.  You cannot reset from within an atomic
1596  * state; use efx_schedule_reset() instead.
1597  *
1598  * Grabs the rtnl_lock.
1599  */
1600 static int efx_reset(struct efx_nic *efx)
1601 {
1602         struct ethtool_cmd ecmd;
1603         enum reset_type method = efx->reset_pending;
1604         int rc;
1605
1606         /* Serialise with kernel interfaces */
1607         rtnl_lock();
1608
1609         /* If we're not RUNNING then don't reset. Leave the reset_pending
1610          * flag set so that efx_pci_probe_main will be retried */
1611         if (efx->state != STATE_RUNNING) {
1612                 EFX_INFO(efx, "scheduled reset quenched. NIC not RUNNING\n");
1613                 goto unlock_rtnl;
1614         }
1615
1616         EFX_INFO(efx, "resetting (%d)\n", method);
1617
1618         efx_reset_down(efx, &ecmd);
1619
1620         rc = falcon_reset_hw(efx, method);
1621         if (rc) {
1622                 EFX_ERR(efx, "failed to reset hardware\n");
1623                 goto fail;
1624         }
1625
1626         /* Allow resets to be rescheduled. */
1627         efx->reset_pending = RESET_TYPE_NONE;
1628
1629         /* Reinitialise bus-mastering, which may have been turned off before
1630          * the reset was scheduled. This is still appropriate, even in the
1631          * RESET_TYPE_DISABLE since this driver generally assumes the hardware
1632          * can respond to requests. */
1633         pci_set_master(efx->pci_dev);
1634
1635         /* Leave device stopped if necessary */
1636         if (method == RESET_TYPE_DISABLE) {
1637                 rc = -EIO;
1638                 goto fail;
1639         }
1640
1641         rc = efx_reset_up(efx, &ecmd, true);
1642         if (rc)
1643                 goto disable;
1644
1645         EFX_LOG(efx, "reset complete\n");
1646  unlock_rtnl:
1647         rtnl_unlock();
1648         return 0;
1649
1650  fail:
1651         efx_reset_up(efx, &ecmd, false);
1652  disable:
1653         EFX_ERR(efx, "has been disabled\n");
1654         efx->state = STATE_DISABLED;
1655
1656         rtnl_unlock();
1657         efx_unregister_netdev(efx);
1658         efx_fini_port(efx);
1659         return rc;
1660 }
1661
1662 /* The worker thread exists so that code that cannot sleep can
1663  * schedule a reset for later.
1664  */
1665 static void efx_reset_work(struct work_struct *data)
1666 {
1667         struct efx_nic *nic = container_of(data, struct efx_nic, reset_work);
1668
1669         efx_reset(nic);
1670 }
1671
1672 void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
1673 {
1674         enum reset_type method;
1675
1676         if (efx->reset_pending != RESET_TYPE_NONE) {
1677                 EFX_INFO(efx, "quenching already scheduled reset\n");
1678                 return;
1679         }
1680
1681         switch (type) {
1682         case RESET_TYPE_INVISIBLE:
1683         case RESET_TYPE_ALL:
1684         case RESET_TYPE_WORLD:
1685         case RESET_TYPE_DISABLE:
1686                 method = type;
1687                 break;
1688         case RESET_TYPE_RX_RECOVERY:
1689         case RESET_TYPE_RX_DESC_FETCH:
1690         case RESET_TYPE_TX_DESC_FETCH:
1691         case RESET_TYPE_TX_SKIP:
1692                 method = RESET_TYPE_INVISIBLE;
1693                 break;
1694         default:
1695                 method = RESET_TYPE_ALL;
1696                 break;
1697         }
1698
1699         if (method != type)
1700                 EFX_LOG(efx, "scheduling reset (%d:%d)\n", type, method);
1701         else
1702                 EFX_LOG(efx, "scheduling reset (%d)\n", method);
1703
1704         efx->reset_pending = method;
1705
1706         queue_work(reset_workqueue, &efx->reset_work);
1707 }
1708
1709 /**************************************************************************
1710  *
1711  * List of NICs we support
1712  *
1713  **************************************************************************/
1714
1715 /* PCI device ID table */
1716 static struct pci_device_id efx_pci_table[] __devinitdata = {
1717         {PCI_DEVICE(EFX_VENDID_SFC, FALCON_A_P_DEVID),
1718          .driver_data = (unsigned long) &falcon_a_nic_type},
1719         {PCI_DEVICE(EFX_VENDID_SFC, FALCON_B_P_DEVID),
1720          .driver_data = (unsigned long) &falcon_b_nic_type},
1721         {0}                     /* end of list */
1722 };
1723
1724 /**************************************************************************
1725  *
1726  * Dummy PHY/MAC/Board operations
1727  *
1728  * Can be used for some unimplemented operations
1729  * Needed so all function pointers are valid and do not have to be tested
1730  * before use
1731  *
1732  **************************************************************************/
1733 int efx_port_dummy_op_int(struct efx_nic *efx)
1734 {
1735         return 0;
1736 }
1737 void efx_port_dummy_op_void(struct efx_nic *efx) {}
1738 void efx_port_dummy_op_blink(struct efx_nic *efx, bool blink) {}
1739
1740 static struct efx_phy_operations efx_dummy_phy_operations = {
1741         .init            = efx_port_dummy_op_int,
1742         .reconfigure     = efx_port_dummy_op_void,
1743         .check_hw        = efx_port_dummy_op_int,
1744         .fini            = efx_port_dummy_op_void,
1745         .clear_interrupt = efx_port_dummy_op_void,
1746 };
1747
1748 static struct efx_board efx_dummy_board_info = {
1749         .init           = efx_port_dummy_op_int,
1750         .init_leds      = efx_port_dummy_op_int,
1751         .set_fault_led  = efx_port_dummy_op_blink,
1752         .monitor        = efx_port_dummy_op_int,
1753         .blink          = efx_port_dummy_op_blink,
1754         .fini           = efx_port_dummy_op_void,
1755 };
1756
1757 /**************************************************************************
1758  *
1759  * Data housekeeping
1760  *
1761  **************************************************************************/
1762
1763 /* This zeroes out and then fills in the invariants in a struct
1764  * efx_nic (including all sub-structures).
1765  */
1766 static int efx_init_struct(struct efx_nic *efx, struct efx_nic_type *type,
1767                            struct pci_dev *pci_dev, struct net_device *net_dev)
1768 {
1769         struct efx_channel *channel;
1770         struct efx_tx_queue *tx_queue;
1771         struct efx_rx_queue *rx_queue;
1772         int i;
1773
1774         /* Initialise common structures */
1775         memset(efx, 0, sizeof(*efx));
1776         spin_lock_init(&efx->biu_lock);
1777         spin_lock_init(&efx->phy_lock);
1778         mutex_init(&efx->spi_lock);
1779         INIT_WORK(&efx->reset_work, efx_reset_work);
1780         INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
1781         efx->pci_dev = pci_dev;
1782         efx->state = STATE_INIT;
1783         efx->reset_pending = RESET_TYPE_NONE;
1784         strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
1785         efx->board_info = efx_dummy_board_info;
1786
1787         efx->net_dev = net_dev;
1788         efx->rx_checksum_enabled = true;
1789         spin_lock_init(&efx->netif_stop_lock);
1790         spin_lock_init(&efx->stats_lock);
1791         mutex_init(&efx->mac_lock);
1792         efx->phy_op = &efx_dummy_phy_operations;
1793         efx->mii.dev = net_dev;
1794         INIT_WORK(&efx->reconfigure_work, efx_reconfigure_work);
1795         atomic_set(&efx->netif_stop_count, 1);
1796
1797         for (i = 0; i < EFX_MAX_CHANNELS; i++) {
1798                 channel = &efx->channel[i];
1799                 channel->efx = efx;
1800                 channel->channel = i;
1801                 channel->work_pending = false;
1802         }
1803         for (i = 0; i < EFX_TX_QUEUE_COUNT; i++) {
1804                 tx_queue = &efx->tx_queue[i];
1805                 tx_queue->efx = efx;
1806                 tx_queue->queue = i;
1807                 tx_queue->buffer = NULL;
1808                 tx_queue->channel = &efx->channel[0]; /* for safety */
1809                 tx_queue->tso_headers_free = NULL;
1810         }
1811         for (i = 0; i < EFX_MAX_RX_QUEUES; i++) {
1812                 rx_queue = &efx->rx_queue[i];
1813                 rx_queue->efx = efx;
1814                 rx_queue->queue = i;
1815                 rx_queue->channel = &efx->channel[0]; /* for safety */
1816                 rx_queue->buffer = NULL;
1817                 spin_lock_init(&rx_queue->add_lock);
1818                 INIT_DELAYED_WORK(&rx_queue->work, efx_rx_work);
1819         }
1820
1821         efx->type = type;
1822
1823         /* Sanity-check NIC type */
1824         EFX_BUG_ON_PARANOID(efx->type->txd_ring_mask &
1825                             (efx->type->txd_ring_mask + 1));
1826         EFX_BUG_ON_PARANOID(efx->type->rxd_ring_mask &
1827                             (efx->type->rxd_ring_mask + 1));
1828         EFX_BUG_ON_PARANOID(efx->type->evq_size &
1829                             (efx->type->evq_size - 1));
1830         /* As close as we can get to guaranteeing that we don't overflow */
1831         EFX_BUG_ON_PARANOID(efx->type->evq_size <
1832                             (efx->type->txd_ring_mask + 1 +
1833                              efx->type->rxd_ring_mask + 1));
1834         EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
1835
1836         /* Higher numbered interrupt modes are less capable! */
1837         efx->interrupt_mode = max(efx->type->max_interrupt_mode,
1838                                   interrupt_mode);
1839
1840         efx->workqueue = create_singlethread_workqueue("sfc_work");
1841         if (!efx->workqueue)
1842                 return -ENOMEM;
1843
1844         return 0;
1845 }
1846
1847 static void efx_fini_struct(struct efx_nic *efx)
1848 {
1849         if (efx->workqueue) {
1850                 destroy_workqueue(efx->workqueue);
1851                 efx->workqueue = NULL;
1852         }
1853 }
1854
1855 /**************************************************************************
1856  *
1857  * PCI interface
1858  *
1859  **************************************************************************/
1860
1861 /* Main body of final NIC shutdown code
1862  * This is called only at module unload (or hotplug removal).
1863  */
1864 static void efx_pci_remove_main(struct efx_nic *efx)
1865 {
1866         EFX_ASSERT_RESET_SERIALISED(efx);
1867
1868         /* Skip everything if we never obtained a valid membase */
1869         if (!efx->membase)
1870                 return;
1871
1872         efx_fini_channels(efx);
1873         efx_fini_port(efx);
1874
1875         /* Shutdown the board, then the NIC and board state */
1876         efx->board_info.fini(efx);
1877         falcon_fini_interrupt(efx);
1878
1879         efx_fini_napi(efx);
1880         efx_remove_all(efx);
1881 }
1882
1883 /* Final NIC shutdown
1884  * This is called only at module unload (or hotplug removal).
1885  */
1886 static void efx_pci_remove(struct pci_dev *pci_dev)
1887 {
1888         struct efx_nic *efx;
1889
1890         efx = pci_get_drvdata(pci_dev);
1891         if (!efx)
1892                 return;
1893
1894         efx_mtd_remove(efx);
1895
1896         /* Mark the NIC as fini, then stop the interface */
1897         rtnl_lock();
1898         efx->state = STATE_FINI;
1899         dev_close(efx->net_dev);
1900
1901         /* Allow any queued efx_resets() to complete */
1902         rtnl_unlock();
1903
1904         if (efx->membase == NULL)
1905                 goto out;
1906
1907         efx_unregister_netdev(efx);
1908
1909         /* Wait for any scheduled resets to complete. No more will be
1910          * scheduled from this point because efx_stop_all() has been
1911          * called, we are no longer registered with driverlink, and
1912          * the net_device's have been removed. */
1913         cancel_work_sync(&efx->reset_work);
1914
1915         efx_pci_remove_main(efx);
1916
1917 out:
1918         efx_fini_io(efx);
1919         EFX_LOG(efx, "shutdown successful\n");
1920
1921         pci_set_drvdata(pci_dev, NULL);
1922         efx_fini_struct(efx);
1923         free_netdev(efx->net_dev);
1924 };
1925
1926 /* Main body of NIC initialisation
1927  * This is called at module load (or hotplug insertion, theoretically).
1928  */
1929 static int efx_pci_probe_main(struct efx_nic *efx)
1930 {
1931         int rc;
1932
1933         /* Do start-of-day initialisation */
1934         rc = efx_probe_all(efx);
1935         if (rc)
1936                 goto fail1;
1937
1938         rc = efx_init_napi(efx);
1939         if (rc)
1940                 goto fail2;
1941
1942         /* Initialise the board */
1943         rc = efx->board_info.init(efx);
1944         if (rc) {
1945                 EFX_ERR(efx, "failed to initialise board\n");
1946                 goto fail3;
1947         }
1948
1949         rc = falcon_init_nic(efx);
1950         if (rc) {
1951                 EFX_ERR(efx, "failed to initialise NIC\n");
1952                 goto fail4;
1953         }
1954
1955         rc = efx_init_port(efx);
1956         if (rc) {
1957                 EFX_ERR(efx, "failed to initialise port\n");
1958                 goto fail5;
1959         }
1960
1961         efx_init_channels(efx);
1962
1963         rc = falcon_init_interrupt(efx);
1964         if (rc)
1965                 goto fail6;
1966
1967         return 0;
1968
1969  fail6:
1970         efx_fini_channels(efx);
1971         efx_fini_port(efx);
1972  fail5:
1973  fail4:
1974         efx->board_info.fini(efx);
1975  fail3:
1976         efx_fini_napi(efx);
1977  fail2:
1978         efx_remove_all(efx);
1979  fail1:
1980         return rc;
1981 }
1982
1983 /* NIC initialisation
1984  *
1985  * This is called at module load (or hotplug insertion,
1986  * theoretically).  It sets up PCI mappings, tests and resets the NIC,
1987  * sets up and registers the network devices with the kernel and hooks
1988  * the interrupt service routine.  It does not prepare the device for
1989  * transmission; this is left to the first time one of the network
1990  * interfaces is brought up (i.e. efx_net_open).
1991  */
1992 static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
1993                                    const struct pci_device_id *entry)
1994 {
1995         struct efx_nic_type *type = (struct efx_nic_type *) entry->driver_data;
1996         struct net_device *net_dev;
1997         struct efx_nic *efx;
1998         int i, rc;
1999
2000         /* Allocate and initialise a struct net_device and struct efx_nic */
2001         net_dev = alloc_etherdev(sizeof(*efx));
2002         if (!net_dev)
2003                 return -ENOMEM;
2004         net_dev->features |= (NETIF_F_IP_CSUM | NETIF_F_SG |
2005                               NETIF_F_HIGHDMA | NETIF_F_TSO);
2006         if (lro)
2007                 net_dev->features |= NETIF_F_LRO;
2008         /* Mask for features that also apply to VLAN devices */
2009         net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
2010                                    NETIF_F_HIGHDMA | NETIF_F_TSO);
2011         efx = netdev_priv(net_dev);
2012         pci_set_drvdata(pci_dev, efx);
2013         rc = efx_init_struct(efx, type, pci_dev, net_dev);
2014         if (rc)
2015                 goto fail1;
2016
2017         EFX_INFO(efx, "Solarflare Communications NIC detected\n");
2018
2019         /* Set up basic I/O (BAR mappings etc) */
2020         rc = efx_init_io(efx);
2021         if (rc)
2022                 goto fail2;
2023
2024         /* No serialisation is required with the reset path because
2025          * we're in STATE_INIT. */
2026         for (i = 0; i < 5; i++) {
2027                 rc = efx_pci_probe_main(efx);
2028                 if (rc == 0)
2029                         break;
2030
2031                 /* Serialise against efx_reset(). No more resets will be
2032                  * scheduled since efx_stop_all() has been called, and we
2033                  * have not and never have been registered with either
2034                  * the rtnetlink or driverlink layers. */
2035                 cancel_work_sync(&efx->reset_work);
2036
2037                 /* Retry if a recoverably reset event has been scheduled */
2038                 if ((efx->reset_pending != RESET_TYPE_INVISIBLE) &&
2039                     (efx->reset_pending != RESET_TYPE_ALL))
2040                         goto fail3;
2041
2042                 efx->reset_pending = RESET_TYPE_NONE;
2043         }
2044
2045         if (rc) {
2046                 EFX_ERR(efx, "Could not reset NIC\n");
2047                 goto fail4;
2048         }
2049
2050         /* Switch to the running state before we expose the device to
2051          * the OS.  This is to ensure that the initial gathering of
2052          * MAC stats succeeds. */
2053         rtnl_lock();
2054         efx->state = STATE_RUNNING;
2055         rtnl_unlock();
2056
2057         rc = efx_register_netdev(efx);
2058         if (rc)
2059                 goto fail5;
2060
2061         EFX_LOG(efx, "initialisation successful\n");
2062
2063         efx_mtd_probe(efx); /* allowed to fail */
2064         return 0;
2065
2066  fail5:
2067         efx_pci_remove_main(efx);
2068  fail4:
2069  fail3:
2070         efx_fini_io(efx);
2071  fail2:
2072         efx_fini_struct(efx);
2073  fail1:
2074         EFX_LOG(efx, "initialisation failed. rc=%d\n", rc);
2075         free_netdev(net_dev);
2076         return rc;
2077 }
2078
2079 static struct pci_driver efx_pci_driver = {
2080         .name           = EFX_DRIVER_NAME,
2081         .id_table       = efx_pci_table,
2082         .probe          = efx_pci_probe,
2083         .remove         = efx_pci_remove,
2084 };
2085
2086 /**************************************************************************
2087  *
2088  * Kernel module interface
2089  *
2090  *************************************************************************/
2091
2092 module_param(interrupt_mode, uint, 0444);
2093 MODULE_PARM_DESC(interrupt_mode,
2094                  "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2095
2096 static int __init efx_init_module(void)
2097 {
2098         int rc;
2099
2100         printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2101
2102         rc = register_netdevice_notifier(&efx_netdev_notifier);
2103         if (rc)
2104                 goto err_notifier;
2105
2106         refill_workqueue = create_workqueue("sfc_refill");
2107         if (!refill_workqueue) {
2108                 rc = -ENOMEM;
2109                 goto err_refill;
2110         }
2111         reset_workqueue = create_singlethread_workqueue("sfc_reset");
2112         if (!reset_workqueue) {
2113                 rc = -ENOMEM;
2114                 goto err_reset;
2115         }
2116
2117         rc = pci_register_driver(&efx_pci_driver);
2118         if (rc < 0)
2119                 goto err_pci;
2120
2121         return 0;
2122
2123  err_pci:
2124         destroy_workqueue(reset_workqueue);
2125  err_reset:
2126         destroy_workqueue(refill_workqueue);
2127  err_refill:
2128         unregister_netdevice_notifier(&efx_netdev_notifier);
2129  err_notifier:
2130         return rc;
2131 }
2132
2133 static void __exit efx_exit_module(void)
2134 {
2135         printk(KERN_INFO "Solarflare NET driver unloading\n");
2136
2137         pci_unregister_driver(&efx_pci_driver);
2138         destroy_workqueue(reset_workqueue);
2139         destroy_workqueue(refill_workqueue);
2140         unregister_netdevice_notifier(&efx_netdev_notifier);
2141
2142 }
2143
2144 module_init(efx_init_module);
2145 module_exit(efx_exit_module);
2146
2147 MODULE_AUTHOR("Michael Brown <mbrown@fensystems.co.uk> and "
2148               "Solarflare Communications");
2149 MODULE_DESCRIPTION("Solarflare Communications network driver");
2150 MODULE_LICENSE("GPL");
2151 MODULE_DEVICE_TABLE(pci, efx_pci_table);