sfc: Work around unreliable strap pins
[deliverable/linux.git] / drivers / net / sfc / efx.c
CommitLineData
8ceee660
BH
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>
aa6ef27e 22#include <linux/topology.h>
8ceee660
BH
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"
8ceee660
BH
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 */
40static struct workqueue_struct *refill_workqueue;
41
1ab00629
SH
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 */
46static struct workqueue_struct *reset_workqueue;
47
8ceee660
BH
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 */
dc8cfa55 60static int lro = true;
8ceee660
BH
61module_param(lro, int, 0644);
62MODULE_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 */
dc8cfa55 73static unsigned int separate_tx_and_rx_channels = true;
8ceee660
BH
74
75/* This is the weight assigned to each of the (per-channel) virtual
76 * NAPI devices.
77 */
78static 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 */
84unsigned int efx_monitor_interval = 1 * HZ;
85
8ceee660
BH
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 */
93static 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 */
101static 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 */
112static 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 */
119static 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 */
128static unsigned int rss_cpus;
129module_param(rss_cpus, uint, 0444);
130MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
131
132/**************************************************************************
133 *
134 * Utility functions and prototypes
135 *
136 *************************************************************************/
137static void efx_remove_channel(struct efx_channel *channel);
138static void efx_remove_port(struct efx_nic *efx);
139static void efx_fini_napi(struct efx_nic *efx);
140static void efx_fini_channels(struct efx_nic *efx);
141
142#define EFX_ASSERT_RESET_SERIALISED(efx) \
143 do { \
3c78708f 144 if (efx->state == STATE_RUNNING) \
8ceee660
BH
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 */
4d566063 161static int efx_process_channel(struct efx_channel *channel, int rx_quota)
8ceee660 162{
42cbe2d7
BH
163 struct efx_nic *efx = channel->efx;
164 int rx_packets;
8ceee660 165
42cbe2d7 166 if (unlikely(efx->reset_pending != RESET_TYPE_NONE ||
8ceee660 167 !channel->enabled))
42cbe2d7 168 return 0;
8ceee660 169
42cbe2d7
BH
170 rx_packets = falcon_process_eventq(channel, rx_quota);
171 if (rx_packets == 0)
172 return 0;
8ceee660
BH
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
42cbe2d7 184 efx_fast_push_rx_descriptors(&efx->rx_queue[channel->channel]);
8ceee660 185
42cbe2d7 186 return rx_packets;
8ceee660
BH
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 */
195static inline void efx_channel_processed(struct efx_channel *channel)
196{
5b9e207c
BH
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. */
dc8cfa55 200 channel->work_pending = false;
5b9e207c
BH
201 smp_wmb();
202
8ceee660
BH
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 */
211static 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;
8ceee660
BH
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
42cbe2d7 221 rx_packets = efx_process_channel(channel, budget);
8ceee660
BH
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 */
244void 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);
64ee3120 255 if (channel->irq)
8ceee660
BH
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 */
91ad757c 262 efx_process_channel(channel, efx->type->evq_size);
8ceee660
BH
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 */
277static 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 */
bc3c90a2 285static void efx_init_eventq(struct efx_channel *channel)
8ceee660
BH
286{
287 EFX_LOG(channel->efx, "chan %d init event queue\n", channel->channel);
288
289 channel->eventq_read_ptr = 0;
290
bc3c90a2 291 falcon_init_eventq(channel);
8ceee660
BH
292}
293
294static 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
301static 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
8ceee660
BH
314static 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 */
bc3c90a2 357static void efx_init_channels(struct efx_nic *efx)
8ceee660
BH
358{
359 struct efx_tx_queue *tx_queue;
360 struct efx_rx_queue *rx_queue;
361 struct efx_channel *channel;
8ceee660 362
f7f13b0b
BH
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);
8ceee660
BH
371
372 /* Initialise the channels */
373 efx_for_each_channel(channel, efx) {
374 EFX_LOG(channel->efx, "init chan %d\n", channel->channel);
375
bc3c90a2 376 efx_init_eventq(channel);
8ceee660 377
bc3c90a2
BH
378 efx_for_each_channel_tx_queue(tx_queue, channel)
379 efx_init_tx_queue(tx_queue);
8ceee660
BH
380
381 /* The rx buffer allocation strategy is MTU dependent */
382 efx_rx_strategy(channel);
383
bc3c90a2
BH
384 efx_for_each_channel_rx_queue(rx_queue, channel)
385 efx_init_rx_queue(rx_queue);
8ceee660
BH
386
387 WARN_ON(channel->rx_pkt != NULL);
388 efx_rx_strategy(channel);
389 }
8ceee660
BH
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 */
397static 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
5b9e207c
BH
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. */
dc8cfa55
BH
410 channel->work_pending = false;
411 channel->enabled = true;
5b9e207c 412 smp_wmb();
8ceee660
BH
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 */
425static 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
dc8cfa55 434 channel->enabled = false;
8ceee660
BH
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
444static 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;
6bc5d3a9 449 int rc;
8ceee660
BH
450
451 EFX_ASSERT_RESET_SERIALISED(efx);
452 BUG_ON(efx->port_enabled);
453
6bc5d3a9
BH
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
8ceee660
BH
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);
8ceee660
BH
467 efx_fini_eventq(channel);
468 }
469}
470
471static 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
487void 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 */
502static void efx_link_status_changed(struct efx_nic *efx)
503{
8ceee660
BH
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
8c8661e4
BH
511 if (efx->port_inhibited) {
512 netif_carrier_off(efx->net_dev);
513 return;
514 }
515
dc8cfa55 516 if (efx->link_up != netif_carrier_ok(efx->net_dev)) {
8ceee660
BH
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 */
8c8661e4 557void __efx_reconfigure_port(struct efx_nic *efx)
8ceee660
BH
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
a816f75a
BH
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
8ceee660
BH
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. */
578void 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 */
590static 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
601static 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 {
e174961c
JB
616 EFX_ERR(efx, "invalid MAC address %pM\n",
617 efx->mac_address);
8ceee660
BH
618 if (!allow_bad_hwaddr) {
619 rc = -EINVAL;
620 goto err;
621 }
622 random_ether_addr(efx->net_dev->dev_addr);
e174961c
JB
623 EFX_INFO(efx, "using locally-generated MAC %pM\n",
624 efx->net_dev->dev_addr);
8ceee660
BH
625 }
626
627 return 0;
628
629 err:
630 efx_remove_port(efx);
631 return rc;
632}
633
634static 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
dc8cfa55 645 efx->port_initialized = true;
8c8661e4 646 efx->stats_enabled = true;
8ceee660
BH
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 */
657static 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);
dc8cfa55 663 efx->port_enabled = true;
8ceee660
BH
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 */
672static void efx_stop_port(struct efx_nic *efx)
673{
674 EFX_LOG(efx, "stop port\n");
675
676 mutex_lock(&efx->mac_lock);
dc8cfa55 677 efx->port_enabled = false;
8ceee660
BH
678 mutex_unlock(&efx->mac_lock);
679
680 /* Serialise against efx_set_multicast_list() */
55668611 681 if (efx_dev_registered(efx)) {
b9e40857
DM
682 netif_addr_lock_bh(efx->net_dev);
683 netif_addr_unlock_bh(efx->net_dev);
8ceee660
BH
684 }
685}
686
687static 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);
dc8cfa55 695 efx->port_initialized = false;
8ceee660 696
dc8cfa55 697 efx->link_up = false;
8ceee660
BH
698 efx_link_status_changed(efx);
699}
700
701static 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. */
715static 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) {
086ea356
BH
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,
8ceee660
BH
771 efx->type->mem_map_size);
772 rc = -ENOMEM;
773 goto fail4;
774 }
086ea356
BH
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);
8ceee660
BH
778
779 return 0;
780
781 fail4:
e1074a0d 782 pci_release_region(efx->pci_dev, efx->type->mem_bar);
8ceee660 783 fail3:
2c118e0f 784 efx->membase_phys = 0;
8ceee660
BH
785 fail2:
786 pci_disable_device(efx->pci_dev);
787 fail1:
788 return rc;
789}
790
791static 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);
2c118e0f 802 efx->membase_phys = 0;
8ceee660
BH
803 }
804
805 pci_disable_device(efx->pci_dev);
806}
807
46123d04
BH
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. */
811static 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 */
8ceee660
BH
833static void efx_probe_interrupts(struct efx_nic *efx)
834{
46123d04
BH
835 int max_channels =
836 min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
8ceee660
BH
837 int rc, i;
838
839 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
46123d04
BH
840 struct msix_entry xentries[EFX_MAX_CHANNELS];
841 int wanted_ints;
aa6ef27e 842
46123d04
BH
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();
8831da7b 848 efx->n_rx_queues = min(wanted_ints, max_channels);
8ceee660 849
8831da7b 850 for (i = 0; i < efx->n_rx_queues; i++)
8ceee660 851 xentries[i].entry = i;
8831da7b 852 rc = pci_enable_msix(efx->pci_dev, xentries, efx->n_rx_queues);
8ceee660 853 if (rc > 0) {
8831da7b
BH
854 EFX_BUG_ON_PARANOID(rc >= efx->n_rx_queues);
855 efx->n_rx_queues = rc;
8ceee660 856 rc = pci_enable_msix(efx->pci_dev, xentries,
8831da7b 857 efx->n_rx_queues);
8ceee660
BH
858 }
859
860 if (rc == 0) {
8831da7b 861 for (i = 0; i < efx->n_rx_queues; i++)
8ceee660 862 efx->channel[i].irq = xentries[i].vector;
8ceee660
BH
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) {
8831da7b 872 efx->n_rx_queues = 1;
8ceee660
BH
873 rc = pci_enable_msi(efx->pci_dev);
874 if (rc == 0) {
875 efx->channel[0].irq = efx->pci_dev->irq;
8ceee660
BH
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) {
8831da7b 884 efx->n_rx_queues = 1;
8ceee660
BH
885 efx->legacy_irq = efx->pci_dev->irq;
886 }
887}
888
889static void efx_remove_interrupts(struct efx_nic *efx)
890{
891 struct efx_channel *channel;
892
893 /* Remove MSI/MSI-X interrupts */
64ee3120 894 efx_for_each_channel(channel, efx)
8ceee660
BH
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
8831da7b 903static void efx_set_channels(struct efx_nic *efx)
8ceee660
BH
904{
905 struct efx_tx_queue *tx_queue;
906 struct efx_rx_queue *rx_queue;
8ceee660 907
60ac1065
BH
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 }
8ceee660 915
8831da7b
BH
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;
8ceee660
BH
919 }
920}
921
922static 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
8831da7b 937 efx_set_channels(efx);
8ceee660
BH
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
945static 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
959static 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. */
1005static 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;
55668611 1017 if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
8ceee660
BH
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);
dacccc74
SH
1023 if (efx_dev_registered(efx))
1024 efx_wake_queue(efx);
8ceee660
BH
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. */
1040static 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. */
b3475645 1048 efx_for_each_rx_queue(rx_queue, efx)
8ceee660 1049 cancel_delayed_work_sync(&rx_queue->work);
8ceee660
BH
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. */
1061static 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);
64ee3120 1075 efx_for_each_channel(channel, efx) {
8ceee660
BH
1076 if (channel->irq)
1077 synchronize_irq(channel->irq);
b3475645 1078 }
8ceee660
BH
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. */
8ceee660
BH
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 */
55668611 1098 if (efx_dev_registered(efx)) {
dacccc74 1099 efx_stop_queue(efx);
8ceee660
BH
1100 netif_tx_lock_bh(efx->net_dev);
1101 netif_tx_unlock_bh(efx->net_dev);
1102 }
1103}
1104
1105static 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 */
bc3c90a2 1116void efx_flush_queues(struct efx_nic *efx)
8ceee660 1117{
8ceee660
BH
1118 EFX_ASSERT_RESET_SERIALISED(efx);
1119
1120 efx_stop_all(efx);
1121
1122 efx_fini_channels(efx);
bc3c90a2 1123 efx_init_channels(efx);
8ceee660
BH
1124
1125 efx_start_all(efx);
8ceee660
BH
1126}
1127
1128/**************************************************************************
1129 *
1130 * Interrupt moderation
1131 *
1132 **************************************************************************/
1133
1134/* Set interrupt moderation parameters */
1135void 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 */
1157static 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
8ceee660
BH
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 */
1193static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1194{
767e468c 1195 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
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
1208static 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
1225static 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 */
1247static void efx_netpoll(struct net_device *net_dev)
1248{
767e468c 1249 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
1250 struct efx_channel *channel;
1251
64ee3120 1252 efx_for_each_channel(channel, efx)
8ceee660
BH
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. */
1265static int efx_net_open(struct net_device *net_dev)
1266{
767e468c 1267 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
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
f8b87c17
BH
1273 if (efx->phy_mode & PHY_MODE_SPECIAL)
1274 return -EBUSY;
1275
8ceee660
BH
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 */
1284static int efx_net_stop(struct net_device *net_dev)
1285{
767e468c 1286 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
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);
bc3c90a2 1294 efx_init_channels(efx);
8ceee660
BH
1295
1296 return 0;
1297}
1298
5b9e207c 1299/* Context: process, dev_base_lock or RTNL held, non-blocking. */
8ceee660
BH
1300static struct net_device_stats *efx_net_stats(struct net_device *net_dev)
1301{
767e468c 1302 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
1303 struct efx_mac_stats *mac_stats = &efx->mac_stats;
1304 struct net_device_stats *stats = &net_dev->stats;
1305
5b9e207c
BH
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 */
8ceee660
BH
1310 if (!spin_trylock(&efx->stats_lock))
1311 return stats;
8c8661e4 1312 if (efx->stats_enabled) {
8ceee660
BH
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. */
1347static void efx_watchdog(struct net_device *net_dev)
1348{
767e468c 1349 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660 1350
739bb23d
BH
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);
8ceee660 1354
739bb23d 1355 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
8ceee660
BH
1356}
1357
1358
1359/* Context: process, rtnl_lock() held. */
1360static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1361{
767e468c 1362 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
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;
bc3c90a2 1376 efx_init_channels(efx);
8ceee660
BH
1377
1378 efx_start_all(efx);
1379 return rc;
8ceee660
BH
1380}
1381
1382static int efx_set_mac_address(struct net_device *net_dev, void *data)
1383{
767e468c 1384 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
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)) {
e174961c
JB
1391 EFX_ERR(efx, "invalid ethernet MAC address requested: %pM\n",
1392 new_addr);
8ceee660
BH
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
a816f75a 1404/* Context: netif_addr_lock held, BHs disabled. */
8ceee660
BH
1405static void efx_set_multicast_list(struct net_device *net_dev)
1406{
767e468c 1407 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
1408 struct dev_mc_list *mc_list = net_dev->mc_list;
1409 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
a816f75a
BH
1410 bool promiscuous = !!(net_dev->flags & IFF_PROMISC);
1411 bool changed = (efx->promiscuous != promiscuous);
8ceee660
BH
1412 u32 crc;
1413 int bit;
1414 int i;
1415
a816f75a 1416 efx->promiscuous = promiscuous;
8ceee660
BH
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
a816f75a
BH
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
8ceee660
BH
1438 /* Create and activate new global multicast hash table */
1439 falcon_set_multicast_hash(efx);
1440}
1441
c3ecb9f3
SH
1442static 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
8ceee660
BH
1458static int efx_netdev_event(struct notifier_block *this,
1459 unsigned long event, void *ptr)
1460{
d3208b5e 1461 struct net_device *net_dev = ptr;
8ceee660 1462
c3ecb9f3 1463 if (net_dev->netdev_ops == &efx_netdev_ops && event == NETDEV_CHANGENAME) {
767e468c 1464 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
1465
1466 strcpy(efx->name, net_dev->name);
f4150724 1467 efx_mtd_rename(efx);
8ceee660
BH
1468 }
1469
1470 return NOTIFY_DONE;
1471}
1472
1473static struct notifier_block efx_netdev_notifier = {
1474 .notifier_call = efx_netdev_event,
1475};
1476
1477static 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;
c3ecb9f3 1484 net_dev->netdev_ops = &efx_netdev_ops;
8ceee660
BH
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
1505static 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
767e468c 1512 BUG_ON(netdev_priv(efx->net_dev) != efx);
8ceee660
BH
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
55668611 1520 if (efx_dev_registered(efx)) {
8ceee660
BH
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
2467ca46
BH
1532/* Tears down the entire software state and most of the hardware state
1533 * before reset. */
8c8661e4 1534void efx_reset_down(struct efx_nic *efx, struct ethtool_cmd *ecmd)
8ceee660
BH
1535{
1536 int rc;
1537
1538 EFX_ASSERT_RESET_SERIALISED(efx);
1539
2467ca46
BH
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);
8c8661e4 1543 efx->stats_enabled = false;
2467ca46
BH
1544 spin_unlock(&efx->stats_lock);
1545
1546 efx_stop_all(efx);
1547 mutex_lock(&efx->mac_lock);
f4150724 1548 mutex_lock(&efx->spi_lock);
2467ca46 1549
8ceee660 1550 rc = falcon_xmac_get_settings(efx, ecmd);
2467ca46 1551 if (rc)
8ceee660 1552 EFX_ERR(efx, "could not back up PHY settings\n");
8ceee660
BH
1553
1554 efx_fini_channels(efx);
8ceee660
BH
1555}
1556
2467ca46
BH
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. */
8c8661e4 1562int efx_reset_up(struct efx_nic *efx, struct ethtool_cmd *ecmd, bool ok)
8ceee660
BH
1563{
1564 int rc;
1565
2467ca46 1566 EFX_ASSERT_RESET_SERIALISED(efx);
8ceee660 1567
2467ca46 1568 rc = falcon_init_nic(efx);
8ceee660 1569 if (rc) {
2467ca46
BH
1570 EFX_ERR(efx, "failed to initialise NIC\n");
1571 ok = false;
8ceee660
BH
1572 }
1573
2467ca46
BH
1574 if (ok) {
1575 efx_init_channels(efx);
8ceee660 1576
2467ca46
BH
1577 if (falcon_xmac_set_settings(efx, ecmd))
1578 EFX_ERR(efx, "could not restore PHY settings\n");
1579 }
1580
f4150724 1581 mutex_unlock(&efx->spi_lock);
2467ca46
BH
1582 mutex_unlock(&efx->mac_lock);
1583
8c8661e4 1584 if (ok) {
2467ca46 1585 efx_start_all(efx);
8c8661e4
BH
1586 efx->stats_enabled = true;
1587 }
8ceee660
BH
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 */
1600static 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
8ceee660
BH
1616 EFX_INFO(efx, "resetting (%d)\n", method);
1617
2467ca46 1618 efx_reset_down(efx, &ecmd);
8ceee660
BH
1619
1620 rc = falcon_reset_hw(efx, method);
1621 if (rc) {
1622 EFX_ERR(efx, "failed to reset hardware\n");
2467ca46 1623 goto fail;
8ceee660
BH
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
8ceee660
BH
1635 /* Leave device stopped if necessary */
1636 if (method == RESET_TYPE_DISABLE) {
8ceee660 1637 rc = -EIO;
2467ca46 1638 goto fail;
8ceee660
BH
1639 }
1640
2467ca46 1641 rc = efx_reset_up(efx, &ecmd, true);
8ceee660 1642 if (rc)
2467ca46 1643 goto disable;
8ceee660 1644
8ceee660 1645 EFX_LOG(efx, "reset complete\n");
8ceee660
BH
1646 unlock_rtnl:
1647 rtnl_unlock();
1648 return 0;
1649
2467ca46
BH
1650 fail:
1651 efx_reset_up(efx, &ecmd, false);
1652 disable:
8ceee660
BH
1653 EFX_ERR(efx, "has been disabled\n");
1654 efx->state = STATE_DISABLED;
1655
8ceee660
BH
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 */
1665static 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
1672void 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
1ab00629 1706 queue_work(reset_workqueue, &efx->reset_work);
8ceee660
BH
1707}
1708
1709/**************************************************************************
1710 *
1711 * List of NICs we support
1712 *
1713 **************************************************************************/
1714
1715/* PCI device ID table */
1716static 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 *
01aad7b6 1728 * Can be used for some unimplemented operations
8ceee660
BH
1729 * Needed so all function pointers are valid and do not have to be tested
1730 * before use
1731 *
1732 **************************************************************************/
1733int efx_port_dummy_op_int(struct efx_nic *efx)
1734{
1735 return 0;
1736}
1737void efx_port_dummy_op_void(struct efx_nic *efx) {}
dc8cfa55 1738void efx_port_dummy_op_blink(struct efx_nic *efx, bool blink) {}
8ceee660
BH
1739
1740static 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,
8ceee660
BH
1746};
1747
8ceee660 1748static struct efx_board efx_dummy_board_info = {
01aad7b6
BH
1749 .init = efx_port_dummy_op_int,
1750 .init_leds = efx_port_dummy_op_int,
1751 .set_fault_led = efx_port_dummy_op_blink,
a17102b1 1752 .monitor = efx_port_dummy_op_int,
01aad7b6
BH
1753 .blink = efx_port_dummy_op_blink,
1754 .fini = efx_port_dummy_op_void,
8ceee660
BH
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 */
1766static 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;
1ab00629 1772 int i;
8ceee660
BH
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);
f4150724 1778 mutex_init(&efx->spi_lock);
8ceee660
BH
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;
dc8cfa55 1788 efx->rx_checksum_enabled = true;
8ceee660
BH
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;
dc8cfa55 1801 channel->work_pending = false;
8ceee660 1802 }
60ac1065 1803 for (i = 0; i < EFX_TX_QUEUE_COUNT; i++) {
8ceee660
BH
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 */
b9b39b62 1809 tx_queue->tso_headers_free = NULL;
8ceee660
BH
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");
1ab00629
SH
1841 if (!efx->workqueue)
1842 return -ENOMEM;
8d9853d9 1843
8ceee660 1844 return 0;
8ceee660
BH
1845}
1846
1847static 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 */
1864static 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 */
37b5a603 1876 efx->board_info.fini(efx);
8ceee660
BH
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 */
1886static 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
f4150724
BH
1894 efx_mtd_remove(efx);
1895
8ceee660
BH
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. */
1ab00629 1913 cancel_work_sync(&efx->reset_work);
8ceee660
BH
1914
1915 efx_pci_remove_main(efx);
1916
1917out:
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 */
1929static 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
bc3c90a2 1961 efx_init_channels(efx);
8ceee660
BH
1962
1963 rc = falcon_init_interrupt(efx);
1964 if (rc)
bc3c90a2 1965 goto fail6;
8ceee660
BH
1966
1967 return 0;
1968
8ceee660 1969 fail6:
bc3c90a2 1970 efx_fini_channels(efx);
8ceee660
BH
1971 efx_fini_port(efx);
1972 fail5:
1973 fail4:
a17102b1 1974 efx->board_info.fini(efx);
8ceee660
BH
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 */
1992static 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;
b9b39b62
BH
2004 net_dev->features |= (NETIF_F_IP_CSUM | NETIF_F_SG |
2005 NETIF_F_HIGHDMA | NETIF_F_TSO);
8ceee660
BH
2006 if (lro)
2007 net_dev->features |= NETIF_F_LRO;
28506563
BH
2008 /* Mask for features that also apply to VLAN devices */
2009 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
740847da 2010 NETIF_F_HIGHDMA | NETIF_F_TSO);
767e468c 2011 efx = netdev_priv(net_dev);
8ceee660
BH
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. */
1ab00629 2035 cancel_work_sync(&efx->reset_work);
8ceee660
BH
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
f4150724 2063 efx_mtd_probe(efx); /* allowed to fail */
8ceee660
BH
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
2079static 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
2092module_param(interrupt_mode, uint, 0444);
2093MODULE_PARM_DESC(interrupt_mode,
2094 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2095
2096static 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 }
1ab00629
SH
2111 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2112 if (!reset_workqueue) {
2113 rc = -ENOMEM;
2114 goto err_reset;
2115 }
8ceee660
BH
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:
1ab00629
SH
2124 destroy_workqueue(reset_workqueue);
2125 err_reset:
8ceee660
BH
2126 destroy_workqueue(refill_workqueue);
2127 err_refill:
2128 unregister_netdevice_notifier(&efx_netdev_notifier);
2129 err_notifier:
2130 return rc;
2131}
2132
2133static void __exit efx_exit_module(void)
2134{
2135 printk(KERN_INFO "Solarflare NET driver unloading\n");
2136
2137 pci_unregister_driver(&efx_pci_driver);
1ab00629 2138 destroy_workqueue(reset_workqueue);
8ceee660
BH
2139 destroy_workqueue(refill_workqueue);
2140 unregister_netdevice_notifier(&efx_netdev_notifier);
2141
2142}
2143
2144module_init(efx_init_module);
2145module_exit(efx_exit_module);
2146
2147MODULE_AUTHOR("Michael Brown <mbrown@fensystems.co.uk> and "
2148 "Solarflare Communications");
2149MODULE_DESCRIPTION("Solarflare Communications network driver");
2150MODULE_LICENSE("GPL");
2151MODULE_DEVICE_TABLE(pci, efx_pci_table);
This page took 0.199755 seconds and 5 git commands to generate.