blob: d4dc92042d777794c4f267ae10a847e186a12877 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
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>
Ben Hutchingsaa6ef272008-07-18 19:03:10 +010022#include <linux/topology.h>
Ben Hutchings8ceee662008-04-27 12:55:59 +010023#include "net_driver.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010024#include "ethtool.h"
25#include "tx.h"
26#include "rx.h"
27#include "efx.h"
28#include "mdio_10g.h"
29#include "falcon.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010030
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
Steve Hodgson1ab00622008-12-12 21:33:02 -080040/* 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
Ben Hutchings8ceee662008-04-27 12:55:59 +010046/**************************************************************************
47 *
48 * Configurable values
49 *
50 *************************************************************************/
51
52/*
Ben Hutchings8ceee662008-04-27 12:55:59 +010053 * Use separate channels for TX and RX events
54 *
Neil Turton28b581a2008-12-12 21:41:06 -080055 * 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.
Ben Hutchings8ceee662008-04-27 12:55:59 +010057 *
Neil Turton28b581a2008-12-12 21:41:06 -080058 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +010059 */
Neil Turton28b581a2008-12-12 21:41:06 -080060static 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");
Ben Hutchings8ceee662008-04-27 12:55:59 +010064
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
Ben Hutchings8ceee662008-04-27 12:55:59 +010076/* 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
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800122static int phy_flash_cfg;
123module_param(phy_flash_cfg, int, 0644);
124MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
125
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000126static 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
Ben Hutchings8ceee662008-04-27 12:55:59 +0100136/**************************************************************************
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 { \
Ben Hutchings3c787082008-09-01 12:49:08 +0100148 if (efx->state == STATE_RUNNING) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100149 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 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100165static int efx_process_channel(struct efx_channel *channel, int rx_quota)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100166{
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100167 struct efx_nic *efx = channel->efx;
168 int rx_packets;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100169
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100170 if (unlikely(efx->reset_pending != RESET_TYPE_NONE ||
Ben Hutchings8ceee662008-04-27 12:55:59 +0100171 !channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100172 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100173
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100174 rx_packets = falcon_process_eventq(channel, rx_quota);
175 if (rx_packets == 0)
176 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100177
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
Ben Hutchings8ceee662008-04-27 12:55:59 +0100185 efx_rx_strategy(channel);
186
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100187 efx_fast_push_rx_descriptors(&efx->rx_queue[channel->channel]);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100188
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100189 return rx_packets;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100190}
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{
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100200 /* 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. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100203 channel->work_pending = false;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100204 smp_wmb();
205
Ben Hutchings8ceee662008-04-27 12:55:59 +0100206 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);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100218 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
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100223 rx_packets = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100224
225 if (rx_packets < budget) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000226 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)) {
231 unsigned old_irq_moderation = channel->irq_moderation;
232
233 if (unlikely(channel->irq_mod_score <
234 irq_adapt_low_thresh)) {
235 channel->irq_moderation =
236 max_t(int,
237 channel->irq_moderation -
238 FALCON_IRQ_MOD_RESOLUTION,
239 FALCON_IRQ_MOD_RESOLUTION);
240 } else if (unlikely(channel->irq_mod_score >
241 irq_adapt_high_thresh)) {
242 channel->irq_moderation =
243 min(channel->irq_moderation +
244 FALCON_IRQ_MOD_RESOLUTION,
245 efx->irq_rx_moderation);
246 }
247
248 if (channel->irq_moderation != old_irq_moderation)
249 falcon_set_int_moderation(channel);
250
251 channel->irq_count = 0;
252 channel->irq_mod_score = 0;
253 }
254
Ben Hutchings8ceee662008-04-27 12:55:59 +0100255 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800256 * only wait for napi_complete(), this isn't a problem
Ben Hutchings8ceee662008-04-27 12:55:59 +0100257 * since efx_channel_processed() will have no effect if
258 * interrupts have already been disabled.
259 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800260 napi_complete(napi);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100261 efx_channel_processed(channel);
262 }
263
264 return rx_packets;
265}
266
267/* Process the eventq of the specified channel immediately on this CPU
268 *
269 * Disable hardware generated interrupts, wait for any existing
270 * processing to finish, then directly poll (and ack ) the eventq.
271 * Finally reenable NAPI and interrupts.
272 *
273 * Since we are touching interrupts the caller should hold the suspend lock
274 */
275void efx_process_channel_now(struct efx_channel *channel)
276{
277 struct efx_nic *efx = channel->efx;
278
279 BUG_ON(!channel->used_flags);
280 BUG_ON(!channel->enabled);
281
282 /* Disable interrupts and wait for ISRs to complete */
283 falcon_disable_interrupts(efx);
284 if (efx->legacy_irq)
285 synchronize_irq(efx->legacy_irq);
Ben Hutchings64ee3122008-09-01 12:47:38 +0100286 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100287 synchronize_irq(channel->irq);
288
289 /* Wait for any NAPI processing to complete */
290 napi_disable(&channel->napi_str);
291
292 /* Poll the channel */
Ben Hutchings91ad7572008-05-16 21:14:27 +0100293 efx_process_channel(channel, efx->type->evq_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100294
295 /* Ack the eventq. This may cause an interrupt to be generated
296 * when they are reenabled */
297 efx_channel_processed(channel);
298
299 napi_enable(&channel->napi_str);
300 falcon_enable_interrupts(efx);
301}
302
303/* Create event queue
304 * Event queue memory allocations are done only once. If the channel
305 * is reset, the memory buffer will be reused; this guards against
306 * errors during channel reset and also simplifies interrupt handling.
307 */
308static int efx_probe_eventq(struct efx_channel *channel)
309{
310 EFX_LOG(channel->efx, "chan %d create event queue\n", channel->channel);
311
312 return falcon_probe_eventq(channel);
313}
314
315/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100316static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100317{
318 EFX_LOG(channel->efx, "chan %d init event queue\n", channel->channel);
319
320 channel->eventq_read_ptr = 0;
321
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100322 falcon_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100323}
324
325static void efx_fini_eventq(struct efx_channel *channel)
326{
327 EFX_LOG(channel->efx, "chan %d fini event queue\n", channel->channel);
328
329 falcon_fini_eventq(channel);
330}
331
332static void efx_remove_eventq(struct efx_channel *channel)
333{
334 EFX_LOG(channel->efx, "chan %d remove event queue\n", channel->channel);
335
336 falcon_remove_eventq(channel);
337}
338
339/**************************************************************************
340 *
341 * Channel handling
342 *
343 *************************************************************************/
344
Ben Hutchings8ceee662008-04-27 12:55:59 +0100345static int efx_probe_channel(struct efx_channel *channel)
346{
347 struct efx_tx_queue *tx_queue;
348 struct efx_rx_queue *rx_queue;
349 int rc;
350
351 EFX_LOG(channel->efx, "creating channel %d\n", channel->channel);
352
353 rc = efx_probe_eventq(channel);
354 if (rc)
355 goto fail1;
356
357 efx_for_each_channel_tx_queue(tx_queue, channel) {
358 rc = efx_probe_tx_queue(tx_queue);
359 if (rc)
360 goto fail2;
361 }
362
363 efx_for_each_channel_rx_queue(rx_queue, channel) {
364 rc = efx_probe_rx_queue(rx_queue);
365 if (rc)
366 goto fail3;
367 }
368
369 channel->n_rx_frm_trunc = 0;
370
371 return 0;
372
373 fail3:
374 efx_for_each_channel_rx_queue(rx_queue, channel)
375 efx_remove_rx_queue(rx_queue);
376 fail2:
377 efx_for_each_channel_tx_queue(tx_queue, channel)
378 efx_remove_tx_queue(tx_queue);
379 fail1:
380 return rc;
381}
382
383
Ben Hutchings56536e92008-12-12 21:37:02 -0800384static void efx_set_channel_names(struct efx_nic *efx)
385{
386 struct efx_channel *channel;
387 const char *type = "";
388 int number;
389
390 efx_for_each_channel(channel, efx) {
391 number = channel->channel;
392 if (efx->n_channels > efx->n_rx_queues) {
393 if (channel->channel < efx->n_rx_queues) {
394 type = "-rx";
395 } else {
396 type = "-tx";
397 number -= efx->n_rx_queues;
398 }
399 }
400 snprintf(channel->name, sizeof(channel->name),
401 "%s%s-%d", efx->name, type, number);
402 }
403}
404
Ben Hutchings8ceee662008-04-27 12:55:59 +0100405/* Channels are shutdown and reinitialised whilst the NIC is running
406 * to propagate configuration changes (mtu, checksum offload), or
407 * to clear hardware error conditions
408 */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100409static void efx_init_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100410{
411 struct efx_tx_queue *tx_queue;
412 struct efx_rx_queue *rx_queue;
413 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100414
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100415 /* Calculate the rx buffer allocation parameters required to
416 * support the current MTU, including padding for header
417 * alignment and overruns.
418 */
419 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
420 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
421 efx->type->rx_buffer_padding);
422 efx->rx_buffer_order = get_order(efx->rx_buffer_len);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100423
424 /* Initialise the channels */
425 efx_for_each_channel(channel, efx) {
426 EFX_LOG(channel->efx, "init chan %d\n", channel->channel);
427
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100428 efx_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100429
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100430 efx_for_each_channel_tx_queue(tx_queue, channel)
431 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100432
433 /* The rx buffer allocation strategy is MTU dependent */
434 efx_rx_strategy(channel);
435
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100436 efx_for_each_channel_rx_queue(rx_queue, channel)
437 efx_init_rx_queue(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100438
439 WARN_ON(channel->rx_pkt != NULL);
440 efx_rx_strategy(channel);
441 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100442}
443
444/* This enables event queue processing and packet transmission.
445 *
446 * Note that this function is not allowed to fail, since that would
447 * introduce too much complexity into the suspend/resume path.
448 */
449static void efx_start_channel(struct efx_channel *channel)
450{
451 struct efx_rx_queue *rx_queue;
452
453 EFX_LOG(channel->efx, "starting chan %d\n", channel->channel);
454
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100455 /* The interrupt handler for this channel may set work_pending
456 * as soon as we enable it. Make sure it's cleared before
457 * then. Similarly, make sure it sees the enabled flag set. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100458 channel->work_pending = false;
459 channel->enabled = true;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100460 smp_wmb();
Ben Hutchings8ceee662008-04-27 12:55:59 +0100461
462 napi_enable(&channel->napi_str);
463
464 /* Load up RX descriptors */
465 efx_for_each_channel_rx_queue(rx_queue, channel)
466 efx_fast_push_rx_descriptors(rx_queue);
467}
468
469/* This disables event queue processing and packet transmission.
470 * This function does not guarantee that all queue processing
471 * (e.g. RX refill) is complete.
472 */
473static void efx_stop_channel(struct efx_channel *channel)
474{
475 struct efx_rx_queue *rx_queue;
476
477 if (!channel->enabled)
478 return;
479
480 EFX_LOG(channel->efx, "stop chan %d\n", channel->channel);
481
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100482 channel->enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100483 napi_disable(&channel->napi_str);
484
485 /* Ensure that any worker threads have exited or will be no-ops */
486 efx_for_each_channel_rx_queue(rx_queue, channel) {
487 spin_lock_bh(&rx_queue->add_lock);
488 spin_unlock_bh(&rx_queue->add_lock);
489 }
490}
491
492static void efx_fini_channels(struct efx_nic *efx)
493{
494 struct efx_channel *channel;
495 struct efx_tx_queue *tx_queue;
496 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100497 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100498
499 EFX_ASSERT_RESET_SERIALISED(efx);
500 BUG_ON(efx->port_enabled);
501
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100502 rc = falcon_flush_queues(efx);
503 if (rc)
504 EFX_ERR(efx, "failed to flush queues\n");
505 else
506 EFX_LOG(efx, "successfully flushed all queues\n");
507
Ben Hutchings8ceee662008-04-27 12:55:59 +0100508 efx_for_each_channel(channel, efx) {
509 EFX_LOG(channel->efx, "shut down chan %d\n", channel->channel);
510
511 efx_for_each_channel_rx_queue(rx_queue, channel)
512 efx_fini_rx_queue(rx_queue);
513 efx_for_each_channel_tx_queue(tx_queue, channel)
514 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100515 efx_fini_eventq(channel);
516 }
517}
518
519static void efx_remove_channel(struct efx_channel *channel)
520{
521 struct efx_tx_queue *tx_queue;
522 struct efx_rx_queue *rx_queue;
523
524 EFX_LOG(channel->efx, "destroy chan %d\n", channel->channel);
525
526 efx_for_each_channel_rx_queue(rx_queue, channel)
527 efx_remove_rx_queue(rx_queue);
528 efx_for_each_channel_tx_queue(tx_queue, channel)
529 efx_remove_tx_queue(tx_queue);
530 efx_remove_eventq(channel);
531
532 channel->used_flags = 0;
533}
534
535void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue, int delay)
536{
537 queue_delayed_work(refill_workqueue, &rx_queue->work, delay);
538}
539
540/**************************************************************************
541 *
542 * Port handling
543 *
544 **************************************************************************/
545
546/* This ensures that the kernel is kept informed (via
547 * netif_carrier_on/off) of the link status, and also maintains the
548 * link status's stop on the port's TX queue.
549 */
550static void efx_link_status_changed(struct efx_nic *efx)
551{
Ben Hutchings8ceee662008-04-27 12:55:59 +0100552 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
553 * that no events are triggered between unregister_netdev() and the
554 * driver unloading. A more general condition is that NETDEV_CHANGE
555 * can only be generated between NETDEV_UP and NETDEV_DOWN */
556 if (!netif_running(efx->net_dev))
557 return;
558
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100559 if (efx->port_inhibited) {
560 netif_carrier_off(efx->net_dev);
561 return;
562 }
563
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100564 if (efx->link_up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100565 efx->n_link_state_changes++;
566
567 if (efx->link_up)
568 netif_carrier_on(efx->net_dev);
569 else
570 netif_carrier_off(efx->net_dev);
571 }
572
573 /* Status message for kernel log */
574 if (efx->link_up) {
Ben Hutchingsf31a45d2008-12-12 21:43:33 -0800575 EFX_INFO(efx, "link up at %uMbps %s-duplex (MTU %d)%s\n",
576 efx->link_speed, efx->link_fd ? "full" : "half",
Ben Hutchings8ceee662008-04-27 12:55:59 +0100577 efx->net_dev->mtu,
578 (efx->promiscuous ? " [PROMISC]" : ""));
579 } else {
580 EFX_INFO(efx, "link down\n");
581 }
582
583}
584
Ben Hutchings115122a2009-03-04 09:52:52 +0000585static void efx_fini_port(struct efx_nic *efx);
586
Ben Hutchings8ceee662008-04-27 12:55:59 +0100587/* This call reinitialises the MAC to pick up new PHY settings. The
588 * caller must hold the mac_lock */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100589void __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100590{
591 WARN_ON(!mutex_is_locked(&efx->mac_lock));
592
593 EFX_LOG(efx, "reconfiguring MAC from PHY settings on CPU %d\n",
594 raw_smp_processor_id());
595
Ben Hutchingsa816f752008-09-01 12:49:12 +0100596 /* Serialise the promiscuous flag with efx_set_multicast_list. */
597 if (efx_dev_registered(efx)) {
598 netif_addr_lock_bh(efx->net_dev);
599 netif_addr_unlock_bh(efx->net_dev);
600 }
601
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800602 falcon_deconfigure_mac_wrapper(efx);
603
604 /* Reconfigure the PHY, disabling transmit in mac level loopback. */
605 if (LOOPBACK_INTERNAL(efx))
606 efx->phy_mode |= PHY_MODE_TX_DISABLED;
607 else
608 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
609 efx->phy_op->reconfigure(efx);
610
611 if (falcon_switch_mac(efx))
612 goto fail;
613
614 efx->mac_op->reconfigure(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100615
616 /* Inform kernel of loss/gain of carrier */
617 efx_link_status_changed(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800618 return;
619
620fail:
621 EFX_ERR(efx, "failed to reconfigure MAC\n");
Ben Hutchings115122a2009-03-04 09:52:52 +0000622 efx->port_enabled = false;
623 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100624}
625
626/* Reinitialise the MAC to pick up new PHY settings, even if the port is
627 * disabled. */
628void efx_reconfigure_port(struct efx_nic *efx)
629{
630 EFX_ASSERT_RESET_SERIALISED(efx);
631
632 mutex_lock(&efx->mac_lock);
633 __efx_reconfigure_port(efx);
634 mutex_unlock(&efx->mac_lock);
635}
636
637/* Asynchronous efx_reconfigure_port work item. To speed up efx_flush_all()
638 * we don't efx_reconfigure_port() if the port is disabled. Care is taken
639 * in efx_stop_all() and efx_start_port() to prevent PHY events being lost */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800640static void efx_phy_work(struct work_struct *data)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100641{
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800642 struct efx_nic *efx = container_of(data, struct efx_nic, phy_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100643
644 mutex_lock(&efx->mac_lock);
645 if (efx->port_enabled)
646 __efx_reconfigure_port(efx);
647 mutex_unlock(&efx->mac_lock);
648}
649
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800650static void efx_mac_work(struct work_struct *data)
651{
652 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
653
654 mutex_lock(&efx->mac_lock);
655 if (efx->port_enabled)
656 efx->mac_op->irq(efx);
657 mutex_unlock(&efx->mac_lock);
658}
659
Ben Hutchings8ceee662008-04-27 12:55:59 +0100660static int efx_probe_port(struct efx_nic *efx)
661{
662 int rc;
663
664 EFX_LOG(efx, "create port\n");
665
666 /* Connect up MAC/PHY operations table and read MAC address */
667 rc = falcon_probe_port(efx);
668 if (rc)
669 goto err;
670
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800671 if (phy_flash_cfg)
672 efx->phy_mode = PHY_MODE_SPECIAL;
673
Ben Hutchings8ceee662008-04-27 12:55:59 +0100674 /* Sanity check MAC address */
675 if (is_valid_ether_addr(efx->mac_address)) {
676 memcpy(efx->net_dev->dev_addr, efx->mac_address, ETH_ALEN);
677 } else {
Johannes Berge1749612008-10-27 15:59:26 -0700678 EFX_ERR(efx, "invalid MAC address %pM\n",
679 efx->mac_address);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100680 if (!allow_bad_hwaddr) {
681 rc = -EINVAL;
682 goto err;
683 }
684 random_ether_addr(efx->net_dev->dev_addr);
Johannes Berge1749612008-10-27 15:59:26 -0700685 EFX_INFO(efx, "using locally-generated MAC %pM\n",
686 efx->net_dev->dev_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100687 }
688
689 return 0;
690
691 err:
692 efx_remove_port(efx);
693 return rc;
694}
695
696static int efx_init_port(struct efx_nic *efx)
697{
698 int rc;
699
700 EFX_LOG(efx, "init port\n");
701
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800702 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100703 if (rc)
704 return rc;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800705 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +0000706 efx->phy_op->reconfigure(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800707 rc = falcon_switch_mac(efx);
708 mutex_unlock(&efx->mac_lock);
709 if (rc)
710 goto fail;
711 efx->mac_op->reconfigure(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100712
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100713 efx->port_initialized = true;
Ben Hutchings1974cc22009-01-29 18:00:07 +0000714 efx_stats_enable(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100715 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800716
717fail:
718 efx->phy_op->fini(efx);
719 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100720}
721
722/* Allow efx_reconfigure_port() to be scheduled, and close the window
723 * between efx_stop_port and efx_flush_all whereby a previously scheduled
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800724 * efx_phy_work()/efx_mac_work() may have been cancelled */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100725static void efx_start_port(struct efx_nic *efx)
726{
727 EFX_LOG(efx, "start port\n");
728 BUG_ON(efx->port_enabled);
729
730 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100731 efx->port_enabled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100732 __efx_reconfigure_port(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800733 efx->mac_op->irq(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100734 mutex_unlock(&efx->mac_lock);
735}
736
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800737/* Prevent efx_phy_work, efx_mac_work, and efx_monitor() from executing,
738 * and efx_set_multicast_list() from scheduling efx_phy_work. efx_phy_work
739 * and efx_mac_work may still be scheduled via NAPI processing until
740 * efx_flush_all() is called */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100741static void efx_stop_port(struct efx_nic *efx)
742{
743 EFX_LOG(efx, "stop port\n");
744
745 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100746 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100747 mutex_unlock(&efx->mac_lock);
748
749 /* Serialise against efx_set_multicast_list() */
Ben Hutchings55668612008-05-16 21:16:10 +0100750 if (efx_dev_registered(efx)) {
David S. Millerb9e40852008-07-15 00:15:08 -0700751 netif_addr_lock_bh(efx->net_dev);
752 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100753 }
754}
755
756static void efx_fini_port(struct efx_nic *efx)
757{
758 EFX_LOG(efx, "shut down port\n");
759
760 if (!efx->port_initialized)
761 return;
762
Ben Hutchings1974cc22009-01-29 18:00:07 +0000763 efx_stats_disable(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800764 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100765 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100766
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100767 efx->link_up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100768 efx_link_status_changed(efx);
769}
770
771static void efx_remove_port(struct efx_nic *efx)
772{
773 EFX_LOG(efx, "destroying port\n");
774
775 falcon_remove_port(efx);
776}
777
778/**************************************************************************
779 *
780 * NIC handling
781 *
782 **************************************************************************/
783
784/* This configures the PCI device to enable I/O and DMA. */
785static int efx_init_io(struct efx_nic *efx)
786{
787 struct pci_dev *pci_dev = efx->pci_dev;
788 dma_addr_t dma_mask = efx->type->max_dma_mask;
789 int rc;
790
791 EFX_LOG(efx, "initialising I/O\n");
792
793 rc = pci_enable_device(pci_dev);
794 if (rc) {
795 EFX_ERR(efx, "failed to enable PCI device\n");
796 goto fail1;
797 }
798
799 pci_set_master(pci_dev);
800
801 /* Set the PCI DMA mask. Try all possibilities from our
802 * genuine mask down to 32 bits, because some architectures
803 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
804 * masks event though they reject 46 bit masks.
805 */
806 while (dma_mask > 0x7fffffffUL) {
807 if (pci_dma_supported(pci_dev, dma_mask) &&
808 ((rc = pci_set_dma_mask(pci_dev, dma_mask)) == 0))
809 break;
810 dma_mask >>= 1;
811 }
812 if (rc) {
813 EFX_ERR(efx, "could not find a suitable DMA mask\n");
814 goto fail2;
815 }
816 EFX_LOG(efx, "using DMA mask %llx\n", (unsigned long long) dma_mask);
817 rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
818 if (rc) {
819 /* pci_set_consistent_dma_mask() is not *allowed* to
820 * fail with a mask that pci_set_dma_mask() accepted,
821 * but just in case...
822 */
823 EFX_ERR(efx, "failed to set consistent DMA mask\n");
824 goto fail2;
825 }
826
827 efx->membase_phys = pci_resource_start(efx->pci_dev,
828 efx->type->mem_bar);
829 rc = pci_request_region(pci_dev, efx->type->mem_bar, "sfc");
830 if (rc) {
831 EFX_ERR(efx, "request for memory BAR failed\n");
832 rc = -EIO;
833 goto fail3;
834 }
835 efx->membase = ioremap_nocache(efx->membase_phys,
836 efx->type->mem_map_size);
837 if (!efx->membase) {
Ben Hutchings086ea352008-05-16 21:17:06 +0100838 EFX_ERR(efx, "could not map memory BAR %d at %llx+%x\n",
839 efx->type->mem_bar,
840 (unsigned long long)efx->membase_phys,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100841 efx->type->mem_map_size);
842 rc = -ENOMEM;
843 goto fail4;
844 }
Ben Hutchings086ea352008-05-16 21:17:06 +0100845 EFX_LOG(efx, "memory BAR %u at %llx+%x (virtual %p)\n",
846 efx->type->mem_bar, (unsigned long long)efx->membase_phys,
847 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100848
849 return 0;
850
851 fail4:
Ben Hutchingse1074a02008-09-01 12:49:15 +0100852 pci_release_region(efx->pci_dev, efx->type->mem_bar);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100853 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +0100854 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100855 fail2:
856 pci_disable_device(efx->pci_dev);
857 fail1:
858 return rc;
859}
860
861static void efx_fini_io(struct efx_nic *efx)
862{
863 EFX_LOG(efx, "shutting down I/O\n");
864
865 if (efx->membase) {
866 iounmap(efx->membase);
867 efx->membase = NULL;
868 }
869
870 if (efx->membase_phys) {
871 pci_release_region(efx->pci_dev, efx->type->mem_bar);
Ben Hutchings2c118e02008-05-16 21:15:29 +0100872 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100873 }
874
875 pci_disable_device(efx->pci_dev);
876}
877
Ben Hutchings46123d02008-09-01 12:47:33 +0100878/* Get number of RX queues wanted. Return number of online CPU
879 * packages in the expectation that an IRQ balancer will spread
880 * interrupts across them. */
881static int efx_wanted_rx_queues(void)
882{
Rusty Russell2f8975f2009-01-10 21:58:09 -0800883 cpumask_var_t core_mask;
Ben Hutchings46123d02008-09-01 12:47:33 +0100884 int count;
885 int cpu;
886
Mike Travis3977d032009-05-12 10:48:36 +0000887 if (unlikely(!alloc_cpumask_var(&core_mask, GFP_KERNEL))) {
Rusty Russell2f8975f2009-01-10 21:58:09 -0800888 printk(KERN_WARNING
Mike Travis3977d032009-05-12 10:48:36 +0000889 "sfc: RSS disabled due to allocation failure\n");
Rusty Russell2f8975f2009-01-10 21:58:09 -0800890 return 1;
891 }
892
893 cpumask_clear(core_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +0100894 count = 0;
895 for_each_online_cpu(cpu) {
Rusty Russell2f8975f2009-01-10 21:58:09 -0800896 if (!cpumask_test_cpu(cpu, core_mask)) {
Ben Hutchings46123d02008-09-01 12:47:33 +0100897 ++count;
Rusty Russell2f8975f2009-01-10 21:58:09 -0800898 cpumask_or(core_mask, core_mask,
Rusty Russellfbd59a82009-01-10 21:58:08 -0800899 topology_core_cpumask(cpu));
Ben Hutchings46123d02008-09-01 12:47:33 +0100900 }
901 }
902
Rusty Russell2f8975f2009-01-10 21:58:09 -0800903 free_cpumask_var(core_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +0100904 return count;
905}
906
907/* Probe the number and type of interrupts we are able to obtain, and
908 * the resulting numbers of channels and RX queues.
909 */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100910static void efx_probe_interrupts(struct efx_nic *efx)
911{
Ben Hutchings46123d02008-09-01 12:47:33 +0100912 int max_channels =
913 min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100914 int rc, i;
915
916 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +0100917 struct msix_entry xentries[EFX_MAX_CHANNELS];
918 int wanted_ints;
Neil Turton28b581a2008-12-12 21:41:06 -0800919 int rx_queues;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100920
Ben Hutchings46123d02008-09-01 12:47:33 +0100921 /* We want one RX queue and interrupt per CPU package
922 * (or as specified by the rss_cpus module parameter).
923 * We will need one channel per interrupt.
924 */
Neil Turton28b581a2008-12-12 21:41:06 -0800925 rx_queues = rss_cpus ? rss_cpus : efx_wanted_rx_queues();
926 wanted_ints = rx_queues + (separate_tx_channels ? 1 : 0);
927 wanted_ints = min(wanted_ints, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +0100928
Neil Turton28b581a2008-12-12 21:41:06 -0800929 for (i = 0; i < wanted_ints; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100930 xentries[i].entry = i;
Neil Turton28b581a2008-12-12 21:41:06 -0800931 rc = pci_enable_msix(efx->pci_dev, xentries, wanted_ints);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100932 if (rc > 0) {
Neil Turton28b581a2008-12-12 21:41:06 -0800933 EFX_ERR(efx, "WARNING: Insufficient MSI-X vectors"
934 " available (%d < %d).\n", rc, wanted_ints);
935 EFX_ERR(efx, "WARNING: Performance may be reduced.\n");
936 EFX_BUG_ON_PARANOID(rc >= wanted_ints);
937 wanted_ints = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100938 rc = pci_enable_msix(efx->pci_dev, xentries,
Neil Turton28b581a2008-12-12 21:41:06 -0800939 wanted_ints);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100940 }
941
942 if (rc == 0) {
Neil Turton28b581a2008-12-12 21:41:06 -0800943 efx->n_rx_queues = min(rx_queues, wanted_ints);
944 efx->n_channels = wanted_ints;
945 for (i = 0; i < wanted_ints; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100946 efx->channel[i].irq = xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100947 } else {
948 /* Fall back to single channel MSI */
949 efx->interrupt_mode = EFX_INT_MODE_MSI;
950 EFX_ERR(efx, "could not enable MSI-X\n");
951 }
952 }
953
954 /* Try single interrupt MSI */
955 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100956 efx->n_rx_queues = 1;
Neil Turton28b581a2008-12-12 21:41:06 -0800957 efx->n_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100958 rc = pci_enable_msi(efx->pci_dev);
959 if (rc == 0) {
960 efx->channel[0].irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100961 } else {
962 EFX_ERR(efx, "could not enable MSI\n");
963 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
964 }
965 }
966
967 /* Assume legacy interrupts */
968 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100969 efx->n_rx_queues = 1;
Neil Turton28b581a2008-12-12 21:41:06 -0800970 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100971 efx->legacy_irq = efx->pci_dev->irq;
972 }
973}
974
975static void efx_remove_interrupts(struct efx_nic *efx)
976{
977 struct efx_channel *channel;
978
979 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100980 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100981 channel->irq = 0;
982 pci_disable_msi(efx->pci_dev);
983 pci_disable_msix(efx->pci_dev);
984
985 /* Remove legacy interrupt */
986 efx->legacy_irq = 0;
987}
988
Ben Hutchings8831da72008-09-01 12:47:48 +0100989static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100990{
991 struct efx_tx_queue *tx_queue;
992 struct efx_rx_queue *rx_queue;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100993
Ben Hutchings60ac1062008-09-01 12:44:59 +0100994 efx_for_each_tx_queue(tx_queue, efx) {
Neil Turton28b581a2008-12-12 21:41:06 -0800995 if (separate_tx_channels)
996 tx_queue->channel = &efx->channel[efx->n_channels-1];
Ben Hutchings60ac1062008-09-01 12:44:59 +0100997 else
998 tx_queue->channel = &efx->channel[0];
999 tx_queue->channel->used_flags |= EFX_USED_BY_TX;
1000 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001001
Ben Hutchings8831da72008-09-01 12:47:48 +01001002 efx_for_each_rx_queue(rx_queue, efx) {
1003 rx_queue->channel = &efx->channel[rx_queue->queue];
1004 rx_queue->channel->used_flags |= EFX_USED_BY_RX;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001005 }
1006}
1007
1008static int efx_probe_nic(struct efx_nic *efx)
1009{
1010 int rc;
1011
1012 EFX_LOG(efx, "creating NIC\n");
1013
1014 /* Carry out hardware-type specific initialisation */
1015 rc = falcon_probe_nic(efx);
1016 if (rc)
1017 return rc;
1018
1019 /* Determine the number of channels and RX queues by trying to hook
1020 * in MSI-X interrupts. */
1021 efx_probe_interrupts(efx);
1022
Ben Hutchings8831da72008-09-01 12:47:48 +01001023 efx_set_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001024
1025 /* Initialise the interrupt moderation settings */
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001026 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001027
1028 return 0;
1029}
1030
1031static void efx_remove_nic(struct efx_nic *efx)
1032{
1033 EFX_LOG(efx, "destroying NIC\n");
1034
1035 efx_remove_interrupts(efx);
1036 falcon_remove_nic(efx);
1037}
1038
1039/**************************************************************************
1040 *
1041 * NIC startup/shutdown
1042 *
1043 *************************************************************************/
1044
1045static int efx_probe_all(struct efx_nic *efx)
1046{
1047 struct efx_channel *channel;
1048 int rc;
1049
1050 /* Create NIC */
1051 rc = efx_probe_nic(efx);
1052 if (rc) {
1053 EFX_ERR(efx, "failed to create NIC\n");
1054 goto fail1;
1055 }
1056
1057 /* Create port */
1058 rc = efx_probe_port(efx);
1059 if (rc) {
1060 EFX_ERR(efx, "failed to create port\n");
1061 goto fail2;
1062 }
1063
1064 /* Create channels */
1065 efx_for_each_channel(channel, efx) {
1066 rc = efx_probe_channel(channel);
1067 if (rc) {
1068 EFX_ERR(efx, "failed to create channel %d\n",
1069 channel->channel);
1070 goto fail3;
1071 }
1072 }
Ben Hutchings56536e92008-12-12 21:37:02 -08001073 efx_set_channel_names(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001074
1075 return 0;
1076
1077 fail3:
1078 efx_for_each_channel(channel, efx)
1079 efx_remove_channel(channel);
1080 efx_remove_port(efx);
1081 fail2:
1082 efx_remove_nic(efx);
1083 fail1:
1084 return rc;
1085}
1086
1087/* Called after previous invocation(s) of efx_stop_all, restarts the
1088 * port, kernel transmit queue, NAPI processing and hardware interrupts,
1089 * and ensures that the port is scheduled to be reconfigured.
1090 * This function is safe to call multiple times when the NIC is in any
1091 * state. */
1092static void efx_start_all(struct efx_nic *efx)
1093{
1094 struct efx_channel *channel;
1095
1096 EFX_ASSERT_RESET_SERIALISED(efx);
1097
1098 /* Check that it is appropriate to restart the interface. All
1099 * of these flags are safe to read under just the rtnl lock */
1100 if (efx->port_enabled)
1101 return;
1102 if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1103 return;
Ben Hutchings55668612008-05-16 21:16:10 +01001104 if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001105 return;
1106
1107 /* Mark the port as enabled so port reconfigurations can start, then
1108 * restart the transmit interface early so the watchdog timer stops */
1109 efx_start_port(efx);
Steve Hodgsondacccc72008-09-01 12:48:20 +01001110 if (efx_dev_registered(efx))
1111 efx_wake_queue(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001112
1113 efx_for_each_channel(channel, efx)
1114 efx_start_channel(channel);
1115
1116 falcon_enable_interrupts(efx);
1117
1118 /* Start hardware monitor if we're in RUNNING */
1119 if (efx->state == STATE_RUNNING)
1120 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1121 efx_monitor_interval);
1122}
1123
1124/* Flush all delayed work. Should only be called when no more delayed work
1125 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1126 * since we're holding the rtnl_lock at this point. */
1127static void efx_flush_all(struct efx_nic *efx)
1128{
1129 struct efx_rx_queue *rx_queue;
1130
1131 /* Make sure the hardware monitor is stopped */
1132 cancel_delayed_work_sync(&efx->monitor_work);
1133
1134 /* Ensure that all RX slow refills are complete. */
Ben Hutchingsb3475642008-05-16 21:15:49 +01001135 efx_for_each_rx_queue(rx_queue, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001136 cancel_delayed_work_sync(&rx_queue->work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001137
1138 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001139 cancel_work_sync(&efx->mac_work);
1140 cancel_work_sync(&efx->phy_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001141
1142}
1143
1144/* Quiesce hardware and software without bringing the link down.
1145 * Safe to call multiple times, when the nic and interface is in any
1146 * state. The caller is guaranteed to subsequently be in a position
1147 * to modify any hardware and software state they see fit without
1148 * taking locks. */
1149static void efx_stop_all(struct efx_nic *efx)
1150{
1151 struct efx_channel *channel;
1152
1153 EFX_ASSERT_RESET_SERIALISED(efx);
1154
1155 /* port_enabled can be read safely under the rtnl lock */
1156 if (!efx->port_enabled)
1157 return;
1158
1159 /* Disable interrupts and wait for ISR to complete */
1160 falcon_disable_interrupts(efx);
1161 if (efx->legacy_irq)
1162 synchronize_irq(efx->legacy_irq);
Ben Hutchings64ee3122008-09-01 12:47:38 +01001163 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001164 if (channel->irq)
1165 synchronize_irq(channel->irq);
Ben Hutchingsb3475642008-05-16 21:15:49 +01001166 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001167
1168 /* Stop all NAPI processing and synchronous rx refills */
1169 efx_for_each_channel(channel, efx)
1170 efx_stop_channel(channel);
1171
1172 /* Stop all asynchronous port reconfigurations. Since all
1173 * event processing has already been stopped, there is no
1174 * window to loose phy events */
1175 efx_stop_port(efx);
1176
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001177 /* Flush efx_phy_work, efx_mac_work, refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001178 efx_flush_all(efx);
1179
1180 /* Isolate the MAC from the TX and RX engines, so that queue
1181 * flushes will complete in a timely fashion. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001182 falcon_drain_tx_fifo(efx);
1183
1184 /* Stop the kernel transmit interface late, so the watchdog
1185 * timer isn't ticking over the flush */
Ben Hutchings55668612008-05-16 21:16:10 +01001186 if (efx_dev_registered(efx)) {
Steve Hodgsondacccc72008-09-01 12:48:20 +01001187 efx_stop_queue(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001188 netif_tx_lock_bh(efx->net_dev);
1189 netif_tx_unlock_bh(efx->net_dev);
1190 }
1191}
1192
1193static void efx_remove_all(struct efx_nic *efx)
1194{
1195 struct efx_channel *channel;
1196
1197 efx_for_each_channel(channel, efx)
1198 efx_remove_channel(channel);
1199 efx_remove_port(efx);
1200 efx_remove_nic(efx);
1201}
1202
1203/* A convinience function to safely flush all the queues */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001204void efx_flush_queues(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001205{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001206 EFX_ASSERT_RESET_SERIALISED(efx);
1207
1208 efx_stop_all(efx);
1209
1210 efx_fini_channels(efx);
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001211 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001212
1213 efx_start_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001214}
1215
1216/**************************************************************************
1217 *
1218 * Interrupt moderation
1219 *
1220 **************************************************************************/
1221
1222/* Set interrupt moderation parameters */
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001223void efx_init_irq_moderation(struct efx_nic *efx, int tx_usecs, int rx_usecs,
1224 bool rx_adaptive)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001225{
1226 struct efx_tx_queue *tx_queue;
1227 struct efx_rx_queue *rx_queue;
1228
1229 EFX_ASSERT_RESET_SERIALISED(efx);
1230
1231 efx_for_each_tx_queue(tx_queue, efx)
1232 tx_queue->channel->irq_moderation = tx_usecs;
1233
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001234 efx->irq_rx_adaptive = rx_adaptive;
1235 efx->irq_rx_moderation = rx_usecs;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001236 efx_for_each_rx_queue(rx_queue, efx)
1237 rx_queue->channel->irq_moderation = rx_usecs;
1238}
1239
1240/**************************************************************************
1241 *
1242 * Hardware monitor
1243 *
1244 **************************************************************************/
1245
1246/* Run periodically off the general workqueue. Serialised against
1247 * efx_reconfigure_port via the mac_lock */
1248static void efx_monitor(struct work_struct *data)
1249{
1250 struct efx_nic *efx = container_of(data, struct efx_nic,
1251 monitor_work.work);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001252 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001253
1254 EFX_TRACE(efx, "hardware monitor executing on CPU %d\n",
1255 raw_smp_processor_id());
1256
Ben Hutchings8ceee662008-04-27 12:55:59 +01001257 /* If the mac_lock is already held then it is likely a port
1258 * reconfiguration is already in place, which will likely do
1259 * most of the work of check_hw() anyway. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001260 if (!mutex_trylock(&efx->mac_lock))
1261 goto out_requeue;
1262 if (!efx->port_enabled)
1263 goto out_unlock;
1264 rc = efx->board_info.monitor(efx);
1265 if (rc) {
1266 EFX_ERR(efx, "Board sensor %s; shutting down PHY\n",
1267 (rc == -ERANGE) ? "reported fault" : "failed");
1268 efx->phy_mode |= PHY_MODE_LOW_POWER;
1269 falcon_sim_phy_event(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001270 }
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001271 efx->phy_op->poll(efx);
1272 efx->mac_op->poll(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001273
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001274out_unlock:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001275 mutex_unlock(&efx->mac_lock);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001276out_requeue:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001277 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1278 efx_monitor_interval);
1279}
1280
1281/**************************************************************************
1282 *
1283 * ioctls
1284 *
1285 *************************************************************************/
1286
1287/* Net device ioctl
1288 * Context: process, rtnl_lock() held.
1289 */
1290static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1291{
Ben Hutchings767e4682008-09-01 12:43:14 +01001292 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001293 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001294
1295 EFX_ASSERT_RESET_SERIALISED(efx);
1296
Ben Hutchings68e7f452009-04-29 08:05:08 +00001297 /* Convert phy_id from older PRTAD/DEVAD format */
1298 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1299 (data->phy_id & 0xfc00) == 0x0400)
1300 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1301
1302 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001303}
1304
1305/**************************************************************************
1306 *
1307 * NAPI interface
1308 *
1309 **************************************************************************/
1310
1311static int efx_init_napi(struct efx_nic *efx)
1312{
1313 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001314
1315 efx_for_each_channel(channel, efx) {
1316 channel->napi_dev = efx->net_dev;
Ben Hutchings718cff12009-04-14 19:47:46 -07001317 netif_napi_add(channel->napi_dev, &channel->napi_str,
1318 efx_poll, napi_weight);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001319 }
1320 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001321}
1322
1323static void efx_fini_napi(struct efx_nic *efx)
1324{
1325 struct efx_channel *channel;
1326
1327 efx_for_each_channel(channel, efx) {
Ben Hutchings718cff12009-04-14 19:47:46 -07001328 if (channel->napi_dev)
1329 netif_napi_del(&channel->napi_str);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001330 channel->napi_dev = NULL;
1331 }
1332}
1333
1334/**************************************************************************
1335 *
1336 * Kernel netpoll interface
1337 *
1338 *************************************************************************/
1339
1340#ifdef CONFIG_NET_POLL_CONTROLLER
1341
1342/* Although in the common case interrupts will be disabled, this is not
1343 * guaranteed. However, all our work happens inside the NAPI callback,
1344 * so no locking is required.
1345 */
1346static void efx_netpoll(struct net_device *net_dev)
1347{
Ben Hutchings767e4682008-09-01 12:43:14 +01001348 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001349 struct efx_channel *channel;
1350
Ben Hutchings64ee3122008-09-01 12:47:38 +01001351 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001352 efx_schedule_channel(channel);
1353}
1354
1355#endif
1356
1357/**************************************************************************
1358 *
1359 * Kernel net device interface
1360 *
1361 *************************************************************************/
1362
1363/* Context: process, rtnl_lock() held. */
1364static int efx_net_open(struct net_device *net_dev)
1365{
Ben Hutchings767e4682008-09-01 12:43:14 +01001366 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001367 EFX_ASSERT_RESET_SERIALISED(efx);
1368
1369 EFX_LOG(efx, "opening device %s on CPU %d\n", net_dev->name,
1370 raw_smp_processor_id());
1371
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001372 if (efx->state == STATE_DISABLED)
1373 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001374 if (efx->phy_mode & PHY_MODE_SPECIAL)
1375 return -EBUSY;
1376
Ben Hutchings8ceee662008-04-27 12:55:59 +01001377 efx_start_all(efx);
1378 return 0;
1379}
1380
1381/* Context: process, rtnl_lock() held.
1382 * Note that the kernel will ignore our return code; this method
1383 * should really be a void.
1384 */
1385static int efx_net_stop(struct net_device *net_dev)
1386{
Ben Hutchings767e4682008-09-01 12:43:14 +01001387 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001388
1389 EFX_LOG(efx, "closing %s on CPU %d\n", net_dev->name,
1390 raw_smp_processor_id());
1391
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001392 if (efx->state != STATE_DISABLED) {
1393 /* Stop the device and flush all the channels */
1394 efx_stop_all(efx);
1395 efx_fini_channels(efx);
1396 efx_init_channels(efx);
1397 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001398
1399 return 0;
1400}
1401
Ben Hutchings1974cc22009-01-29 18:00:07 +00001402void efx_stats_disable(struct efx_nic *efx)
1403{
1404 spin_lock(&efx->stats_lock);
1405 ++efx->stats_disable_count;
1406 spin_unlock(&efx->stats_lock);
1407}
1408
1409void efx_stats_enable(struct efx_nic *efx)
1410{
1411 spin_lock(&efx->stats_lock);
1412 --efx->stats_disable_count;
1413 spin_unlock(&efx->stats_lock);
1414}
1415
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001416/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001417static struct net_device_stats *efx_net_stats(struct net_device *net_dev)
1418{
Ben Hutchings767e4682008-09-01 12:43:14 +01001419 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001420 struct efx_mac_stats *mac_stats = &efx->mac_stats;
1421 struct net_device_stats *stats = &net_dev->stats;
1422
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001423 /* Update stats if possible, but do not wait if another thread
Ben Hutchings1974cc22009-01-29 18:00:07 +00001424 * is updating them or if MAC stats fetches are temporarily
1425 * disabled; slightly stale stats are acceptable.
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001426 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001427 if (!spin_trylock(&efx->stats_lock))
1428 return stats;
Ben Hutchings1974cc22009-01-29 18:00:07 +00001429 if (!efx->stats_disable_count) {
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001430 efx->mac_op->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001431 falcon_update_nic_stats(efx);
1432 }
1433 spin_unlock(&efx->stats_lock);
1434
1435 stats->rx_packets = mac_stats->rx_packets;
1436 stats->tx_packets = mac_stats->tx_packets;
1437 stats->rx_bytes = mac_stats->rx_bytes;
1438 stats->tx_bytes = mac_stats->tx_bytes;
1439 stats->multicast = mac_stats->rx_multicast;
1440 stats->collisions = mac_stats->tx_collision;
1441 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1442 mac_stats->rx_length_error);
1443 stats->rx_over_errors = efx->n_rx_nodesc_drop_cnt;
1444 stats->rx_crc_errors = mac_stats->rx_bad;
1445 stats->rx_frame_errors = mac_stats->rx_align_error;
1446 stats->rx_fifo_errors = mac_stats->rx_overflow;
1447 stats->rx_missed_errors = mac_stats->rx_missed;
1448 stats->tx_window_errors = mac_stats->tx_late_collision;
1449
1450 stats->rx_errors = (stats->rx_length_errors +
1451 stats->rx_over_errors +
1452 stats->rx_crc_errors +
1453 stats->rx_frame_errors +
1454 stats->rx_fifo_errors +
1455 stats->rx_missed_errors +
1456 mac_stats->rx_symbol_error);
1457 stats->tx_errors = (stats->tx_window_errors +
1458 mac_stats->tx_bad);
1459
1460 return stats;
1461}
1462
1463/* Context: netif_tx_lock held, BHs disabled. */
1464static void efx_watchdog(struct net_device *net_dev)
1465{
Ben Hutchings767e4682008-09-01 12:43:14 +01001466 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001467
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001468 EFX_ERR(efx, "TX stuck with stop_count=%d port_enabled=%d:"
1469 " resetting channels\n",
1470 atomic_read(&efx->netif_stop_count), efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001471
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001472 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001473}
1474
1475
1476/* Context: process, rtnl_lock() held. */
1477static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1478{
Ben Hutchings767e4682008-09-01 12:43:14 +01001479 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001480 int rc = 0;
1481
1482 EFX_ASSERT_RESET_SERIALISED(efx);
1483
1484 if (new_mtu > EFX_MAX_MTU)
1485 return -EINVAL;
1486
1487 efx_stop_all(efx);
1488
1489 EFX_LOG(efx, "changing MTU to %d\n", new_mtu);
1490
1491 efx_fini_channels(efx);
1492 net_dev->mtu = new_mtu;
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001493 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001494
1495 efx_start_all(efx);
1496 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001497}
1498
1499static int efx_set_mac_address(struct net_device *net_dev, void *data)
1500{
Ben Hutchings767e4682008-09-01 12:43:14 +01001501 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001502 struct sockaddr *addr = data;
1503 char *new_addr = addr->sa_data;
1504
1505 EFX_ASSERT_RESET_SERIALISED(efx);
1506
1507 if (!is_valid_ether_addr(new_addr)) {
Johannes Berge1749612008-10-27 15:59:26 -07001508 EFX_ERR(efx, "invalid ethernet MAC address requested: %pM\n",
1509 new_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001510 return -EINVAL;
1511 }
1512
1513 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1514
1515 /* Reconfigure the MAC */
1516 efx_reconfigure_port(efx);
1517
1518 return 0;
1519}
1520
Ben Hutchingsa816f752008-09-01 12:49:12 +01001521/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001522static void efx_set_multicast_list(struct net_device *net_dev)
1523{
Ben Hutchings767e4682008-09-01 12:43:14 +01001524 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001525 struct dev_mc_list *mc_list = net_dev->mc_list;
1526 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchingsa816f752008-09-01 12:49:12 +01001527 bool promiscuous = !!(net_dev->flags & IFF_PROMISC);
1528 bool changed = (efx->promiscuous != promiscuous);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001529 u32 crc;
1530 int bit;
1531 int i;
1532
Ben Hutchingsa816f752008-09-01 12:49:12 +01001533 efx->promiscuous = promiscuous;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001534
1535 /* Build multicast hash table */
1536 if (promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
1537 memset(mc_hash, 0xff, sizeof(*mc_hash));
1538 } else {
1539 memset(mc_hash, 0x00, sizeof(*mc_hash));
1540 for (i = 0; i < net_dev->mc_count; i++) {
1541 crc = ether_crc_le(ETH_ALEN, mc_list->dmi_addr);
1542 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1543 set_bit_le(bit, mc_hash->byte);
1544 mc_list = mc_list->next;
1545 }
1546 }
1547
Ben Hutchingsa816f752008-09-01 12:49:12 +01001548 if (!efx->port_enabled)
1549 /* Delay pushing settings until efx_start_port() */
1550 return;
1551
1552 if (changed)
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001553 queue_work(efx->workqueue, &efx->phy_work);
Ben Hutchingsa816f752008-09-01 12:49:12 +01001554
Ben Hutchings8ceee662008-04-27 12:55:59 +01001555 /* Create and activate new global multicast hash table */
1556 falcon_set_multicast_hash(efx);
1557}
1558
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001559static const struct net_device_ops efx_netdev_ops = {
1560 .ndo_open = efx_net_open,
1561 .ndo_stop = efx_net_stop,
1562 .ndo_get_stats = efx_net_stats,
1563 .ndo_tx_timeout = efx_watchdog,
1564 .ndo_start_xmit = efx_hard_start_xmit,
1565 .ndo_validate_addr = eth_validate_addr,
1566 .ndo_do_ioctl = efx_ioctl,
1567 .ndo_change_mtu = efx_change_mtu,
1568 .ndo_set_mac_address = efx_set_mac_address,
1569 .ndo_set_multicast_list = efx_set_multicast_list,
1570#ifdef CONFIG_NET_POLL_CONTROLLER
1571 .ndo_poll_controller = efx_netpoll,
1572#endif
1573};
1574
Ben Hutchings7dde5962008-12-12 22:09:38 -08001575static void efx_update_name(struct efx_nic *efx)
1576{
1577 strcpy(efx->name, efx->net_dev->name);
1578 efx_mtd_rename(efx);
1579 efx_set_channel_names(efx);
1580}
1581
Ben Hutchings8ceee662008-04-27 12:55:59 +01001582static int efx_netdev_event(struct notifier_block *this,
1583 unsigned long event, void *ptr)
1584{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001585 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001586
Ben Hutchings7dde5962008-12-12 22:09:38 -08001587 if (net_dev->netdev_ops == &efx_netdev_ops &&
1588 event == NETDEV_CHANGENAME)
1589 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001590
1591 return NOTIFY_DONE;
1592}
1593
1594static struct notifier_block efx_netdev_notifier = {
1595 .notifier_call = efx_netdev_event,
1596};
1597
Ben Hutchings06d5e192008-12-12 21:47:23 -08001598static ssize_t
1599show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
1600{
1601 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
1602 return sprintf(buf, "%d\n", efx->phy_type);
1603}
1604static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
1605
Ben Hutchings8ceee662008-04-27 12:55:59 +01001606static int efx_register_netdev(struct efx_nic *efx)
1607{
1608 struct net_device *net_dev = efx->net_dev;
1609 int rc;
1610
1611 net_dev->watchdog_timeo = 5 * HZ;
1612 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001613 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001614 SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
1615 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
1616
Ben Hutchings8ceee662008-04-27 12:55:59 +01001617 /* Clear MAC statistics */
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001618 efx->mac_op->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001619 memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
1620
Ben Hutchings7dde5962008-12-12 22:09:38 -08001621 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00001622
1623 rc = dev_alloc_name(net_dev, net_dev->name);
1624 if (rc < 0)
1625 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08001626 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00001627
1628 rc = register_netdevice(net_dev);
1629 if (rc)
1630 goto fail_locked;
1631
1632 /* Always start with carrier off; PHY events will detect the link */
1633 netif_carrier_off(efx->net_dev);
1634
Ben Hutchings7dde5962008-12-12 22:09:38 -08001635 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01001636
Ben Hutchings06d5e192008-12-12 21:47:23 -08001637 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
1638 if (rc) {
1639 EFX_ERR(efx, "failed to init net dev attributes\n");
1640 goto fail_registered;
1641 }
1642
Ben Hutchings8ceee662008-04-27 12:55:59 +01001643 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08001644
Ben Hutchingsaed06282009-08-26 08:16:27 +00001645fail_locked:
1646 rtnl_unlock();
1647 EFX_ERR(efx, "could not register net dev\n");
1648 return rc;
1649
Ben Hutchings06d5e192008-12-12 21:47:23 -08001650fail_registered:
1651 unregister_netdev(net_dev);
1652 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001653}
1654
1655static void efx_unregister_netdev(struct efx_nic *efx)
1656{
1657 struct efx_tx_queue *tx_queue;
1658
1659 if (!efx->net_dev)
1660 return;
1661
Ben Hutchings767e4682008-09-01 12:43:14 +01001662 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001663
1664 /* Free up any skbs still remaining. This has to happen before
1665 * we try to unregister the netdev as running their destructors
1666 * may be needed to get the device ref. count to 0. */
1667 efx_for_each_tx_queue(tx_queue, efx)
1668 efx_release_tx_buffers(tx_queue);
1669
Ben Hutchings55668612008-05-16 21:16:10 +01001670 if (efx_dev_registered(efx)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001671 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
Ben Hutchings06d5e192008-12-12 21:47:23 -08001672 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001673 unregister_netdev(efx->net_dev);
1674 }
1675}
1676
1677/**************************************************************************
1678 *
1679 * Device reset and suspend
1680 *
1681 **************************************************************************/
1682
Ben Hutchings2467ca42008-09-01 12:48:50 +01001683/* Tears down the entire software state and most of the hardware state
1684 * before reset. */
Steve Hodgson4b988282009-01-29 17:50:51 +00001685void efx_reset_down(struct efx_nic *efx, enum reset_type method,
1686 struct ethtool_cmd *ecmd)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001687{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001688 EFX_ASSERT_RESET_SERIALISED(efx);
1689
Ben Hutchings1974cc22009-01-29 18:00:07 +00001690 efx_stats_disable(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01001691 efx_stop_all(efx);
1692 mutex_lock(&efx->mac_lock);
Ben Hutchingsf4150722008-11-04 20:34:28 +00001693 mutex_lock(&efx->spi_lock);
Ben Hutchings2467ca42008-09-01 12:48:50 +01001694
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001695 efx->phy_op->get_settings(efx, ecmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001696
1697 efx_fini_channels(efx);
Steve Hodgson4b988282009-01-29 17:50:51 +00001698 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
1699 efx->phy_op->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001700}
1701
Ben Hutchings2467ca42008-09-01 12:48:50 +01001702/* This function will always ensure that the locks acquired in
1703 * efx_reset_down() are released. A failure return code indicates
1704 * that we were unable to reinitialise the hardware, and the
1705 * driver should be disabled. If ok is false, then the rx and tx
1706 * engines are not restarted, pending a RESET_DISABLE. */
Steve Hodgson4b988282009-01-29 17:50:51 +00001707int efx_reset_up(struct efx_nic *efx, enum reset_type method,
1708 struct ethtool_cmd *ecmd, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001709{
1710 int rc;
1711
Ben Hutchings2467ca42008-09-01 12:48:50 +01001712 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001713
Ben Hutchings2467ca42008-09-01 12:48:50 +01001714 rc = falcon_init_nic(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001715 if (rc) {
Ben Hutchings2467ca42008-09-01 12:48:50 +01001716 EFX_ERR(efx, "failed to initialise NIC\n");
1717 ok = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001718 }
1719
Steve Hodgson4b988282009-01-29 17:50:51 +00001720 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
1721 if (ok) {
1722 rc = efx->phy_op->init(efx);
1723 if (rc)
1724 ok = false;
Ben Hutchings115122a2009-03-04 09:52:52 +00001725 }
1726 if (!ok)
Steve Hodgson4b988282009-01-29 17:50:51 +00001727 efx->port_initialized = false;
1728 }
1729
Ben Hutchings2467ca42008-09-01 12:48:50 +01001730 if (ok) {
1731 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001732
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001733 if (efx->phy_op->set_settings(efx, ecmd))
Ben Hutchings2467ca42008-09-01 12:48:50 +01001734 EFX_ERR(efx, "could not restore PHY settings\n");
1735 }
1736
Ben Hutchingsf4150722008-11-04 20:34:28 +00001737 mutex_unlock(&efx->spi_lock);
Ben Hutchings2467ca42008-09-01 12:48:50 +01001738 mutex_unlock(&efx->mac_lock);
1739
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001740 if (ok) {
Ben Hutchings2467ca42008-09-01 12:48:50 +01001741 efx_start_all(efx);
Ben Hutchings1974cc22009-01-29 18:00:07 +00001742 efx_stats_enable(efx);
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001743 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001744 return rc;
1745}
1746
1747/* Reset the NIC as transparently as possible. Do not reset the PHY
1748 * Note that the reset may fail, in which case the card will be left
1749 * in a most-probably-unusable state.
1750 *
1751 * This function will sleep. You cannot reset from within an atomic
1752 * state; use efx_schedule_reset() instead.
1753 *
1754 * Grabs the rtnl_lock.
1755 */
1756static int efx_reset(struct efx_nic *efx)
1757{
1758 struct ethtool_cmd ecmd;
1759 enum reset_type method = efx->reset_pending;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001760 int rc = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001761
1762 /* Serialise with kernel interfaces */
1763 rtnl_lock();
1764
1765 /* If we're not RUNNING then don't reset. Leave the reset_pending
1766 * flag set so that efx_pci_probe_main will be retried */
1767 if (efx->state != STATE_RUNNING) {
1768 EFX_INFO(efx, "scheduled reset quenched. NIC not RUNNING\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001769 goto out_unlock;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001770 }
1771
Ben Hutchings8ceee662008-04-27 12:55:59 +01001772 EFX_INFO(efx, "resetting (%d)\n", method);
1773
Steve Hodgson4b988282009-01-29 17:50:51 +00001774 efx_reset_down(efx, method, &ecmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001775
1776 rc = falcon_reset_hw(efx, method);
1777 if (rc) {
1778 EFX_ERR(efx, "failed to reset hardware\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001779 goto out_disable;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001780 }
1781
1782 /* Allow resets to be rescheduled. */
1783 efx->reset_pending = RESET_TYPE_NONE;
1784
1785 /* Reinitialise bus-mastering, which may have been turned off before
1786 * the reset was scheduled. This is still appropriate, even in the
1787 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
1788 * can respond to requests. */
1789 pci_set_master(efx->pci_dev);
1790
Ben Hutchings8ceee662008-04-27 12:55:59 +01001791 /* Leave device stopped if necessary */
1792 if (method == RESET_TYPE_DISABLE) {
Steve Hodgson4b988282009-01-29 17:50:51 +00001793 efx_reset_up(efx, method, &ecmd, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001794 rc = -EIO;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001795 } else {
Steve Hodgson4b988282009-01-29 17:50:51 +00001796 rc = efx_reset_up(efx, method, &ecmd, true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001797 }
1798
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001799out_disable:
1800 if (rc) {
1801 EFX_ERR(efx, "has been disabled\n");
1802 efx->state = STATE_DISABLED;
1803 dev_close(efx->net_dev);
1804 } else {
1805 EFX_LOG(efx, "reset complete\n");
1806 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001807
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001808out_unlock:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001809 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01001810 return rc;
1811}
1812
1813/* The worker thread exists so that code that cannot sleep can
1814 * schedule a reset for later.
1815 */
1816static void efx_reset_work(struct work_struct *data)
1817{
1818 struct efx_nic *nic = container_of(data, struct efx_nic, reset_work);
1819
1820 efx_reset(nic);
1821}
1822
1823void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
1824{
1825 enum reset_type method;
1826
1827 if (efx->reset_pending != RESET_TYPE_NONE) {
1828 EFX_INFO(efx, "quenching already scheduled reset\n");
1829 return;
1830 }
1831
1832 switch (type) {
1833 case RESET_TYPE_INVISIBLE:
1834 case RESET_TYPE_ALL:
1835 case RESET_TYPE_WORLD:
1836 case RESET_TYPE_DISABLE:
1837 method = type;
1838 break;
1839 case RESET_TYPE_RX_RECOVERY:
1840 case RESET_TYPE_RX_DESC_FETCH:
1841 case RESET_TYPE_TX_DESC_FETCH:
1842 case RESET_TYPE_TX_SKIP:
1843 method = RESET_TYPE_INVISIBLE;
1844 break;
1845 default:
1846 method = RESET_TYPE_ALL;
1847 break;
1848 }
1849
1850 if (method != type)
1851 EFX_LOG(efx, "scheduling reset (%d:%d)\n", type, method);
1852 else
1853 EFX_LOG(efx, "scheduling reset (%d)\n", method);
1854
1855 efx->reset_pending = method;
1856
Steve Hodgson1ab00622008-12-12 21:33:02 -08001857 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001858}
1859
1860/**************************************************************************
1861 *
1862 * List of NICs we support
1863 *
1864 **************************************************************************/
1865
1866/* PCI device ID table */
1867static struct pci_device_id efx_pci_table[] __devinitdata = {
1868 {PCI_DEVICE(EFX_VENDID_SFC, FALCON_A_P_DEVID),
1869 .driver_data = (unsigned long) &falcon_a_nic_type},
1870 {PCI_DEVICE(EFX_VENDID_SFC, FALCON_B_P_DEVID),
1871 .driver_data = (unsigned long) &falcon_b_nic_type},
1872 {0} /* end of list */
1873};
1874
1875/**************************************************************************
1876 *
1877 * Dummy PHY/MAC/Board operations
1878 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01001879 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01001880 * Needed so all function pointers are valid and do not have to be tested
1881 * before use
1882 *
1883 **************************************************************************/
1884int efx_port_dummy_op_int(struct efx_nic *efx)
1885{
1886 return 0;
1887}
1888void efx_port_dummy_op_void(struct efx_nic *efx) {}
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001889void efx_port_dummy_op_blink(struct efx_nic *efx, bool blink) {}
Ben Hutchings8ceee662008-04-27 12:55:59 +01001890
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001891static struct efx_mac_operations efx_dummy_mac_operations = {
1892 .reconfigure = efx_port_dummy_op_void,
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001893 .poll = efx_port_dummy_op_void,
1894 .irq = efx_port_dummy_op_void,
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001895};
1896
Ben Hutchings8ceee662008-04-27 12:55:59 +01001897static struct efx_phy_operations efx_dummy_phy_operations = {
1898 .init = efx_port_dummy_op_int,
1899 .reconfigure = efx_port_dummy_op_void,
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001900 .poll = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001901 .fini = efx_port_dummy_op_void,
1902 .clear_interrupt = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001903};
1904
Ben Hutchings8ceee662008-04-27 12:55:59 +01001905static struct efx_board efx_dummy_board_info = {
Ben Hutchings01aad7b2008-09-01 12:48:36 +01001906 .init = efx_port_dummy_op_int,
Ben Hutchings8129d212009-02-27 13:08:03 +00001907 .init_leds = efx_port_dummy_op_void,
1908 .set_id_led = efx_port_dummy_op_blink,
Ben Hutchingsa17102b2008-12-12 21:28:20 -08001909 .monitor = efx_port_dummy_op_int,
Ben Hutchings01aad7b2008-09-01 12:48:36 +01001910 .blink = efx_port_dummy_op_blink,
1911 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001912};
1913
1914/**************************************************************************
1915 *
1916 * Data housekeeping
1917 *
1918 **************************************************************************/
1919
1920/* This zeroes out and then fills in the invariants in a struct
1921 * efx_nic (including all sub-structures).
1922 */
1923static int efx_init_struct(struct efx_nic *efx, struct efx_nic_type *type,
1924 struct pci_dev *pci_dev, struct net_device *net_dev)
1925{
1926 struct efx_channel *channel;
1927 struct efx_tx_queue *tx_queue;
1928 struct efx_rx_queue *rx_queue;
Steve Hodgson1ab00622008-12-12 21:33:02 -08001929 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001930
1931 /* Initialise common structures */
1932 memset(efx, 0, sizeof(*efx));
1933 spin_lock_init(&efx->biu_lock);
1934 spin_lock_init(&efx->phy_lock);
Ben Hutchingsf4150722008-11-04 20:34:28 +00001935 mutex_init(&efx->spi_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001936 INIT_WORK(&efx->reset_work, efx_reset_work);
1937 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
1938 efx->pci_dev = pci_dev;
1939 efx->state = STATE_INIT;
1940 efx->reset_pending = RESET_TYPE_NONE;
1941 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
1942 efx->board_info = efx_dummy_board_info;
1943
1944 efx->net_dev = net_dev;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001945 efx->rx_checksum_enabled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001946 spin_lock_init(&efx->netif_stop_lock);
1947 spin_lock_init(&efx->stats_lock);
Ben Hutchings1974cc22009-01-29 18:00:07 +00001948 efx->stats_disable_count = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001949 mutex_init(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001950 efx->mac_op = &efx_dummy_mac_operations;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001951 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00001952 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001953 INIT_WORK(&efx->phy_work, efx_phy_work);
1954 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001955 atomic_set(&efx->netif_stop_count, 1);
1956
1957 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
1958 channel = &efx->channel[i];
1959 channel->efx = efx;
1960 channel->channel = i;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001961 channel->work_pending = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001962 }
Ben Hutchings60ac1062008-09-01 12:44:59 +01001963 for (i = 0; i < EFX_TX_QUEUE_COUNT; i++) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001964 tx_queue = &efx->tx_queue[i];
1965 tx_queue->efx = efx;
1966 tx_queue->queue = i;
1967 tx_queue->buffer = NULL;
1968 tx_queue->channel = &efx->channel[0]; /* for safety */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001969 tx_queue->tso_headers_free = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001970 }
1971 for (i = 0; i < EFX_MAX_RX_QUEUES; i++) {
1972 rx_queue = &efx->rx_queue[i];
1973 rx_queue->efx = efx;
1974 rx_queue->queue = i;
1975 rx_queue->channel = &efx->channel[0]; /* for safety */
1976 rx_queue->buffer = NULL;
1977 spin_lock_init(&rx_queue->add_lock);
1978 INIT_DELAYED_WORK(&rx_queue->work, efx_rx_work);
1979 }
1980
1981 efx->type = type;
1982
1983 /* Sanity-check NIC type */
1984 EFX_BUG_ON_PARANOID(efx->type->txd_ring_mask &
1985 (efx->type->txd_ring_mask + 1));
1986 EFX_BUG_ON_PARANOID(efx->type->rxd_ring_mask &
1987 (efx->type->rxd_ring_mask + 1));
1988 EFX_BUG_ON_PARANOID(efx->type->evq_size &
1989 (efx->type->evq_size - 1));
1990 /* As close as we can get to guaranteeing that we don't overflow */
1991 EFX_BUG_ON_PARANOID(efx->type->evq_size <
1992 (efx->type->txd_ring_mask + 1 +
1993 efx->type->rxd_ring_mask + 1));
1994 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
1995
1996 /* Higher numbered interrupt modes are less capable! */
1997 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
1998 interrupt_mode);
1999
Ben Hutchings6977dc62008-12-26 13:44:39 -08002000 /* Would be good to use the net_dev name, but we're too early */
2001 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2002 pci_name(pci_dev));
2003 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002004 if (!efx->workqueue)
2005 return -ENOMEM;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002006
Ben Hutchings8ceee662008-04-27 12:55:59 +01002007 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002008}
2009
2010static void efx_fini_struct(struct efx_nic *efx)
2011{
2012 if (efx->workqueue) {
2013 destroy_workqueue(efx->workqueue);
2014 efx->workqueue = NULL;
2015 }
2016}
2017
2018/**************************************************************************
2019 *
2020 * PCI interface
2021 *
2022 **************************************************************************/
2023
2024/* Main body of final NIC shutdown code
2025 * This is called only at module unload (or hotplug removal).
2026 */
2027static void efx_pci_remove_main(struct efx_nic *efx)
2028{
2029 EFX_ASSERT_RESET_SERIALISED(efx);
2030
2031 /* Skip everything if we never obtained a valid membase */
2032 if (!efx->membase)
2033 return;
2034
2035 efx_fini_channels(efx);
2036 efx_fini_port(efx);
2037
2038 /* Shutdown the board, then the NIC and board state */
Ben Hutchings37b5a602008-05-30 22:27:04 +01002039 efx->board_info.fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002040 falcon_fini_interrupt(efx);
2041
2042 efx_fini_napi(efx);
2043 efx_remove_all(efx);
2044}
2045
2046/* Final NIC shutdown
2047 * This is called only at module unload (or hotplug removal).
2048 */
2049static void efx_pci_remove(struct pci_dev *pci_dev)
2050{
2051 struct efx_nic *efx;
2052
2053 efx = pci_get_drvdata(pci_dev);
2054 if (!efx)
2055 return;
2056
2057 /* Mark the NIC as fini, then stop the interface */
2058 rtnl_lock();
2059 efx->state = STATE_FINI;
2060 dev_close(efx->net_dev);
2061
2062 /* Allow any queued efx_resets() to complete */
2063 rtnl_unlock();
2064
2065 if (efx->membase == NULL)
2066 goto out;
2067
2068 efx_unregister_netdev(efx);
2069
Ben Hutchings7dde5962008-12-12 22:09:38 -08002070 efx_mtd_remove(efx);
2071
Ben Hutchings8ceee662008-04-27 12:55:59 +01002072 /* Wait for any scheduled resets to complete. No more will be
2073 * scheduled from this point because efx_stop_all() has been
2074 * called, we are no longer registered with driverlink, and
2075 * the net_device's have been removed. */
Steve Hodgson1ab00622008-12-12 21:33:02 -08002076 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002077
2078 efx_pci_remove_main(efx);
2079
2080out:
2081 efx_fini_io(efx);
2082 EFX_LOG(efx, "shutdown successful\n");
2083
2084 pci_set_drvdata(pci_dev, NULL);
2085 efx_fini_struct(efx);
2086 free_netdev(efx->net_dev);
2087};
2088
2089/* Main body of NIC initialisation
2090 * This is called at module load (or hotplug insertion, theoretically).
2091 */
2092static int efx_pci_probe_main(struct efx_nic *efx)
2093{
2094 int rc;
2095
2096 /* Do start-of-day initialisation */
2097 rc = efx_probe_all(efx);
2098 if (rc)
2099 goto fail1;
2100
2101 rc = efx_init_napi(efx);
2102 if (rc)
2103 goto fail2;
2104
2105 /* Initialise the board */
2106 rc = efx->board_info.init(efx);
2107 if (rc) {
2108 EFX_ERR(efx, "failed to initialise board\n");
2109 goto fail3;
2110 }
2111
2112 rc = falcon_init_nic(efx);
2113 if (rc) {
2114 EFX_ERR(efx, "failed to initialise NIC\n");
2115 goto fail4;
2116 }
2117
2118 rc = efx_init_port(efx);
2119 if (rc) {
2120 EFX_ERR(efx, "failed to initialise port\n");
2121 goto fail5;
2122 }
2123
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01002124 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002125
2126 rc = falcon_init_interrupt(efx);
2127 if (rc)
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01002128 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002129
2130 return 0;
2131
Ben Hutchings8ceee662008-04-27 12:55:59 +01002132 fail6:
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01002133 efx_fini_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002134 efx_fini_port(efx);
2135 fail5:
2136 fail4:
Ben Hutchingsa17102b2008-12-12 21:28:20 -08002137 efx->board_info.fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002138 fail3:
2139 efx_fini_napi(efx);
2140 fail2:
2141 efx_remove_all(efx);
2142 fail1:
2143 return rc;
2144}
2145
2146/* NIC initialisation
2147 *
2148 * This is called at module load (or hotplug insertion,
2149 * theoretically). It sets up PCI mappings, tests and resets the NIC,
2150 * sets up and registers the network devices with the kernel and hooks
2151 * the interrupt service routine. It does not prepare the device for
2152 * transmission; this is left to the first time one of the network
2153 * interfaces is brought up (i.e. efx_net_open).
2154 */
2155static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2156 const struct pci_device_id *entry)
2157{
2158 struct efx_nic_type *type = (struct efx_nic_type *) entry->driver_data;
2159 struct net_device *net_dev;
2160 struct efx_nic *efx;
2161 int i, rc;
2162
2163 /* Allocate and initialise a struct net_device and struct efx_nic */
2164 net_dev = alloc_etherdev(sizeof(*efx));
2165 if (!net_dev)
2166 return -ENOMEM;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01002167 net_dev->features |= (NETIF_F_IP_CSUM | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002168 NETIF_F_HIGHDMA | NETIF_F_TSO |
2169 NETIF_F_GRO);
Ben Hutchings285065632008-09-01 12:46:54 +01002170 /* Mask for features that also apply to VLAN devices */
2171 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchings740847d2008-09-01 12:48:23 +01002172 NETIF_F_HIGHDMA | NETIF_F_TSO);
Ben Hutchings767e4682008-09-01 12:43:14 +01002173 efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002174 pci_set_drvdata(pci_dev, efx);
2175 rc = efx_init_struct(efx, type, pci_dev, net_dev);
2176 if (rc)
2177 goto fail1;
2178
2179 EFX_INFO(efx, "Solarflare Communications NIC detected\n");
2180
2181 /* Set up basic I/O (BAR mappings etc) */
2182 rc = efx_init_io(efx);
2183 if (rc)
2184 goto fail2;
2185
2186 /* No serialisation is required with the reset path because
2187 * we're in STATE_INIT. */
2188 for (i = 0; i < 5; i++) {
2189 rc = efx_pci_probe_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002190
2191 /* Serialise against efx_reset(). No more resets will be
2192 * scheduled since efx_stop_all() has been called, and we
2193 * have not and never have been registered with either
2194 * the rtnetlink or driverlink layers. */
Steve Hodgson1ab00622008-12-12 21:33:02 -08002195 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002196
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002197 if (rc == 0) {
2198 if (efx->reset_pending != RESET_TYPE_NONE) {
2199 /* If there was a scheduled reset during
2200 * probe, the NIC is probably hosed anyway */
2201 efx_pci_remove_main(efx);
2202 rc = -EIO;
2203 } else {
2204 break;
2205 }
2206 }
2207
Ben Hutchings8ceee662008-04-27 12:55:59 +01002208 /* Retry if a recoverably reset event has been scheduled */
2209 if ((efx->reset_pending != RESET_TYPE_INVISIBLE) &&
2210 (efx->reset_pending != RESET_TYPE_ALL))
2211 goto fail3;
2212
2213 efx->reset_pending = RESET_TYPE_NONE;
2214 }
2215
2216 if (rc) {
2217 EFX_ERR(efx, "Could not reset NIC\n");
2218 goto fail4;
2219 }
2220
2221 /* Switch to the running state before we expose the device to
2222 * the OS. This is to ensure that the initial gathering of
2223 * MAC stats succeeds. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002224 efx->state = STATE_RUNNING;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002225
2226 efx_mtd_probe(efx); /* allowed to fail */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002227
2228 rc = efx_register_netdev(efx);
2229 if (rc)
2230 goto fail5;
2231
2232 EFX_LOG(efx, "initialisation successful\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01002233 return 0;
2234
2235 fail5:
2236 efx_pci_remove_main(efx);
2237 fail4:
2238 fail3:
2239 efx_fini_io(efx);
2240 fail2:
2241 efx_fini_struct(efx);
2242 fail1:
2243 EFX_LOG(efx, "initialisation failed. rc=%d\n", rc);
2244 free_netdev(net_dev);
2245 return rc;
2246}
2247
2248static struct pci_driver efx_pci_driver = {
2249 .name = EFX_DRIVER_NAME,
2250 .id_table = efx_pci_table,
2251 .probe = efx_pci_probe,
2252 .remove = efx_pci_remove,
2253};
2254
2255/**************************************************************************
2256 *
2257 * Kernel module interface
2258 *
2259 *************************************************************************/
2260
2261module_param(interrupt_mode, uint, 0444);
2262MODULE_PARM_DESC(interrupt_mode,
2263 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2264
2265static int __init efx_init_module(void)
2266{
2267 int rc;
2268
2269 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2270
2271 rc = register_netdevice_notifier(&efx_netdev_notifier);
2272 if (rc)
2273 goto err_notifier;
2274
2275 refill_workqueue = create_workqueue("sfc_refill");
2276 if (!refill_workqueue) {
2277 rc = -ENOMEM;
2278 goto err_refill;
2279 }
Steve Hodgson1ab00622008-12-12 21:33:02 -08002280 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2281 if (!reset_workqueue) {
2282 rc = -ENOMEM;
2283 goto err_reset;
2284 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002285
2286 rc = pci_register_driver(&efx_pci_driver);
2287 if (rc < 0)
2288 goto err_pci;
2289
2290 return 0;
2291
2292 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08002293 destroy_workqueue(reset_workqueue);
2294 err_reset:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002295 destroy_workqueue(refill_workqueue);
2296 err_refill:
2297 unregister_netdevice_notifier(&efx_netdev_notifier);
2298 err_notifier:
2299 return rc;
2300}
2301
2302static void __exit efx_exit_module(void)
2303{
2304 printk(KERN_INFO "Solarflare NET driver unloading\n");
2305
2306 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002307 destroy_workqueue(reset_workqueue);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002308 destroy_workqueue(refill_workqueue);
2309 unregister_netdevice_notifier(&efx_netdev_notifier);
2310
2311}
2312
2313module_init(efx_init_module);
2314module_exit(efx_exit_module);
2315
2316MODULE_AUTHOR("Michael Brown <mbrown@fensystems.co.uk> and "
2317 "Solarflare Communications");
2318MODULE_DESCRIPTION("Solarflare Communications network driver");
2319MODULE_LICENSE("GPL");
2320MODULE_DEVICE_TABLE(pci, efx_pci_table);