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