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