blob: ea31141b1737f2b721567d756aff3ad92068c9cf [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)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000231 if (unlikely(channel->irq_mod_score <
232 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000233 if (channel->irq_moderation > 1) {
234 channel->irq_moderation -= 1;
235 falcon_set_int_moderation(channel);
236 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000237 } else if (unlikely(channel->irq_mod_score >
238 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000239 if (channel->irq_moderation <
240 efx->irq_rx_moderation) {
241 channel->irq_moderation += 1;
242 falcon_set_int_moderation(channel);
243 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000244 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000245 channel->irq_count = 0;
246 channel->irq_mod_score = 0;
247 }
248
Ben Hutchings8ceee662008-04-27 12:55:59 +0100249 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800250 * only wait for napi_complete(), this isn't a problem
Ben Hutchings8ceee662008-04-27 12:55:59 +0100251 * since efx_channel_processed() will have no effect if
252 * interrupts have already been disabled.
253 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800254 napi_complete(napi);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100255 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);
Ben Hutchings64ee3122008-09-01 12:47:38 +0100280 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100281 synchronize_irq(channel->irq);
282
283 /* Wait for any NAPI processing to complete */
284 napi_disable(&channel->napi_str);
285
286 /* Poll the channel */
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000287 efx_process_channel(channel, EFX_EVQ_SIZE);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100288
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 */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100310static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100311{
312 EFX_LOG(channel->efx, "chan %d init event queue\n", channel->channel);
313
314 channel->eventq_read_ptr = 0;
315
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100316 falcon_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100317}
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
Ben Hutchings8ceee662008-04-27 12:55:59 +0100339static 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
Ben Hutchings56536e92008-12-12 21:37:02 -0800378static 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
Ben Hutchings8ceee662008-04-27 12:55:59 +0100399/* 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 */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100403static void efx_init_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100404{
405 struct efx_tx_queue *tx_queue;
406 struct efx_rx_queue *rx_queue;
407 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100408
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100409 /* 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);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100417
418 /* Initialise the channels */
419 efx_for_each_channel(channel, efx) {
420 EFX_LOG(channel->efx, "init chan %d\n", channel->channel);
421
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100422 efx_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100423
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100424 efx_for_each_channel_tx_queue(tx_queue, channel)
425 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100426
427 /* The rx buffer allocation strategy is MTU dependent */
428 efx_rx_strategy(channel);
429
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100430 efx_for_each_channel_rx_queue(rx_queue, channel)
431 efx_init_rx_queue(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100432
433 WARN_ON(channel->rx_pkt != NULL);
434 efx_rx_strategy(channel);
435 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100436}
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
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100449 /* 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. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100452 channel->work_pending = false;
453 channel->enabled = true;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100454 smp_wmb();
Ben Hutchings8ceee662008-04-27 12:55:59 +0100455
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
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100476 channel->enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100477 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;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100491 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100492
493 EFX_ASSERT_RESET_SERIALISED(efx);
494 BUG_ON(efx->port_enabled);
495
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100496 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
Ben Hutchings8ceee662008-04-27 12:55:59 +0100502 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);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100509 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{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000546 struct efx_link_state *link_state = &efx->link_state;
547
Ben Hutchings8ceee662008-04-27 12:55:59 +0100548 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
549 * that no events are triggered between unregister_netdev() and the
550 * driver unloading. A more general condition is that NETDEV_CHANGE
551 * can only be generated between NETDEV_UP and NETDEV_DOWN */
552 if (!netif_running(efx->net_dev))
553 return;
554
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100555 if (efx->port_inhibited) {
556 netif_carrier_off(efx->net_dev);
557 return;
558 }
559
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000560 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100561 efx->n_link_state_changes++;
562
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000563 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100564 netif_carrier_on(efx->net_dev);
565 else
566 netif_carrier_off(efx->net_dev);
567 }
568
569 /* Status message for kernel log */
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000570 if (link_state->up) {
Ben Hutchingsf31a45d2008-12-12 21:43:33 -0800571 EFX_INFO(efx, "link up at %uMbps %s-duplex (MTU %d)%s\n",
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000572 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings8ceee662008-04-27 12:55:59 +0100573 efx->net_dev->mtu,
574 (efx->promiscuous ? " [PROMISC]" : ""));
575 } else {
576 EFX_INFO(efx, "link down\n");
577 }
578
579}
580
Ben Hutchings115122a2009-03-04 09:52:52 +0000581static void efx_fini_port(struct efx_nic *efx);
582
Ben Hutchings8ceee662008-04-27 12:55:59 +0100583/* This call reinitialises the MAC to pick up new PHY settings. The
584 * caller must hold the mac_lock */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100585void __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100586{
587 WARN_ON(!mutex_is_locked(&efx->mac_lock));
588
589 EFX_LOG(efx, "reconfiguring MAC from PHY settings on CPU %d\n",
590 raw_smp_processor_id());
591
Ben Hutchingsa816f752008-09-01 12:49:12 +0100592 /* Serialise the promiscuous flag with efx_set_multicast_list. */
593 if (efx_dev_registered(efx)) {
594 netif_addr_lock_bh(efx->net_dev);
595 netif_addr_unlock_bh(efx->net_dev);
596 }
597
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800598 falcon_deconfigure_mac_wrapper(efx);
599
600 /* Reconfigure the PHY, disabling transmit in mac level loopback. */
601 if (LOOPBACK_INTERNAL(efx))
602 efx->phy_mode |= PHY_MODE_TX_DISABLED;
603 else
604 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
605 efx->phy_op->reconfigure(efx);
606
607 if (falcon_switch_mac(efx))
608 goto fail;
609
610 efx->mac_op->reconfigure(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100611
612 /* Inform kernel of loss/gain of carrier */
613 efx_link_status_changed(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800614 return;
615
616fail:
617 EFX_ERR(efx, "failed to reconfigure MAC\n");
Ben Hutchings115122a2009-03-04 09:52:52 +0000618 efx->port_enabled = false;
619 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100620}
621
622/* Reinitialise the MAC to pick up new PHY settings, even if the port is
623 * disabled. */
624void efx_reconfigure_port(struct efx_nic *efx)
625{
626 EFX_ASSERT_RESET_SERIALISED(efx);
627
628 mutex_lock(&efx->mac_lock);
629 __efx_reconfigure_port(efx);
630 mutex_unlock(&efx->mac_lock);
631}
632
633/* Asynchronous efx_reconfigure_port work item. To speed up efx_flush_all()
634 * we don't efx_reconfigure_port() if the port is disabled. Care is taken
635 * in efx_stop_all() and efx_start_port() to prevent PHY events being lost */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800636static void efx_phy_work(struct work_struct *data)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100637{
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800638 struct efx_nic *efx = container_of(data, struct efx_nic, phy_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100639
640 mutex_lock(&efx->mac_lock);
641 if (efx->port_enabled)
642 __efx_reconfigure_port(efx);
643 mutex_unlock(&efx->mac_lock);
644}
645
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800646static void efx_mac_work(struct work_struct *data)
647{
648 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
649
650 mutex_lock(&efx->mac_lock);
651 if (efx->port_enabled)
652 efx->mac_op->irq(efx);
653 mutex_unlock(&efx->mac_lock);
654}
655
Ben Hutchings8ceee662008-04-27 12:55:59 +0100656static int efx_probe_port(struct efx_nic *efx)
657{
658 int rc;
659
660 EFX_LOG(efx, "create port\n");
661
662 /* Connect up MAC/PHY operations table and read MAC address */
663 rc = falcon_probe_port(efx);
664 if (rc)
665 goto err;
666
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800667 if (phy_flash_cfg)
668 efx->phy_mode = PHY_MODE_SPECIAL;
669
Ben Hutchings8ceee662008-04-27 12:55:59 +0100670 /* Sanity check MAC address */
671 if (is_valid_ether_addr(efx->mac_address)) {
672 memcpy(efx->net_dev->dev_addr, efx->mac_address, ETH_ALEN);
673 } else {
Johannes Berge1749612008-10-27 15:59:26 -0700674 EFX_ERR(efx, "invalid MAC address %pM\n",
675 efx->mac_address);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100676 if (!allow_bad_hwaddr) {
677 rc = -EINVAL;
678 goto err;
679 }
680 random_ether_addr(efx->net_dev->dev_addr);
Johannes Berge1749612008-10-27 15:59:26 -0700681 EFX_INFO(efx, "using locally-generated MAC %pM\n",
682 efx->net_dev->dev_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100683 }
684
685 return 0;
686
687 err:
688 efx_remove_port(efx);
689 return rc;
690}
691
692static int efx_init_port(struct efx_nic *efx)
693{
694 int rc;
695
696 EFX_LOG(efx, "init port\n");
697
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800698 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100699 if (rc)
700 return rc;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800701 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +0000702 efx->phy_op->reconfigure(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800703 rc = falcon_switch_mac(efx);
704 mutex_unlock(&efx->mac_lock);
705 if (rc)
706 goto fail;
707 efx->mac_op->reconfigure(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100708
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100709 efx->port_initialized = true;
Ben Hutchings1974cc22009-01-29 18:00:07 +0000710 efx_stats_enable(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100711 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800712
713fail:
714 efx->phy_op->fini(efx);
715 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100716}
717
718/* Allow efx_reconfigure_port() to be scheduled, and close the window
719 * between efx_stop_port and efx_flush_all whereby a previously scheduled
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800720 * efx_phy_work()/efx_mac_work() may have been cancelled */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100721static void efx_start_port(struct efx_nic *efx)
722{
723 EFX_LOG(efx, "start port\n");
724 BUG_ON(efx->port_enabled);
725
726 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100727 efx->port_enabled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100728 __efx_reconfigure_port(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800729 efx->mac_op->irq(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100730 mutex_unlock(&efx->mac_lock);
731}
732
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800733/* Prevent efx_phy_work, efx_mac_work, and efx_monitor() from executing,
734 * and efx_set_multicast_list() from scheduling efx_phy_work. efx_phy_work
735 * and efx_mac_work may still be scheduled via NAPI processing until
736 * efx_flush_all() is called */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100737static void efx_stop_port(struct efx_nic *efx)
738{
739 EFX_LOG(efx, "stop port\n");
740
741 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100742 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100743 mutex_unlock(&efx->mac_lock);
744
745 /* Serialise against efx_set_multicast_list() */
Ben Hutchings55668612008-05-16 21:16:10 +0100746 if (efx_dev_registered(efx)) {
David S. Millerb9e40852008-07-15 00:15:08 -0700747 netif_addr_lock_bh(efx->net_dev);
748 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100749 }
750}
751
752static void efx_fini_port(struct efx_nic *efx)
753{
754 EFX_LOG(efx, "shut down port\n");
755
756 if (!efx->port_initialized)
757 return;
758
Ben Hutchings1974cc22009-01-29 18:00:07 +0000759 efx_stats_disable(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800760 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100761 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100762
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000763 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100764 efx_link_status_changed(efx);
765}
766
767static void efx_remove_port(struct efx_nic *efx)
768{
769 EFX_LOG(efx, "destroying port\n");
770
771 falcon_remove_port(efx);
772}
773
774/**************************************************************************
775 *
776 * NIC handling
777 *
778 **************************************************************************/
779
780/* This configures the PCI device to enable I/O and DMA. */
781static int efx_init_io(struct efx_nic *efx)
782{
783 struct pci_dev *pci_dev = efx->pci_dev;
784 dma_addr_t dma_mask = efx->type->max_dma_mask;
785 int rc;
786
787 EFX_LOG(efx, "initialising I/O\n");
788
789 rc = pci_enable_device(pci_dev);
790 if (rc) {
791 EFX_ERR(efx, "failed to enable PCI device\n");
792 goto fail1;
793 }
794
795 pci_set_master(pci_dev);
796
797 /* Set the PCI DMA mask. Try all possibilities from our
798 * genuine mask down to 32 bits, because some architectures
799 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
800 * masks event though they reject 46 bit masks.
801 */
802 while (dma_mask > 0x7fffffffUL) {
803 if (pci_dma_supported(pci_dev, dma_mask) &&
804 ((rc = pci_set_dma_mask(pci_dev, dma_mask)) == 0))
805 break;
806 dma_mask >>= 1;
807 }
808 if (rc) {
809 EFX_ERR(efx, "could not find a suitable DMA mask\n");
810 goto fail2;
811 }
812 EFX_LOG(efx, "using DMA mask %llx\n", (unsigned long long) dma_mask);
813 rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
814 if (rc) {
815 /* pci_set_consistent_dma_mask() is not *allowed* to
816 * fail with a mask that pci_set_dma_mask() accepted,
817 * but just in case...
818 */
819 EFX_ERR(efx, "failed to set consistent DMA mask\n");
820 goto fail2;
821 }
822
Ben Hutchingsdc803df2009-10-23 08:32:33 +0000823 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
824 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100825 if (rc) {
826 EFX_ERR(efx, "request for memory BAR failed\n");
827 rc = -EIO;
828 goto fail3;
829 }
830 efx->membase = ioremap_nocache(efx->membase_phys,
831 efx->type->mem_map_size);
832 if (!efx->membase) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +0000833 EFX_ERR(efx, "could not map memory BAR at %llx+%x\n",
Ben Hutchings086ea352008-05-16 21:17:06 +0100834 (unsigned long long)efx->membase_phys,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100835 efx->type->mem_map_size);
836 rc = -ENOMEM;
837 goto fail4;
838 }
Ben Hutchingsdc803df2009-10-23 08:32:33 +0000839 EFX_LOG(efx, "memory BAR at %llx+%x (virtual %p)\n",
840 (unsigned long long)efx->membase_phys,
Ben Hutchings086ea352008-05-16 21:17:06 +0100841 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100842
843 return 0;
844
845 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +0000846 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100847 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +0100848 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100849 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) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +0000865 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +0100866 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100867 }
868
869 pci_disable_device(efx->pci_dev);
870}
871
Ben Hutchings46123d02008-09-01 12:47:33 +0100872/* 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{
Rusty Russell2f8975f2009-01-10 21:58:09 -0800877 cpumask_var_t core_mask;
Ben Hutchings46123d02008-09-01 12:47:33 +0100878 int count;
879 int cpu;
880
Li Zefan79f55992009-06-15 14:58:26 +0800881 if (unlikely(!zalloc_cpumask_var(&core_mask, GFP_KERNEL))) {
Rusty Russell2f8975f2009-01-10 21:58:09 -0800882 printk(KERN_WARNING
Mike Travis3977d032009-05-12 10:48:36 +0000883 "sfc: RSS disabled due to allocation failure\n");
Rusty Russell2f8975f2009-01-10 21:58:09 -0800884 return 1;
885 }
886
Ben Hutchings46123d02008-09-01 12:47:33 +0100887 count = 0;
888 for_each_online_cpu(cpu) {
Rusty Russell2f8975f2009-01-10 21:58:09 -0800889 if (!cpumask_test_cpu(cpu, core_mask)) {
Ben Hutchings46123d02008-09-01 12:47:33 +0100890 ++count;
Rusty Russell2f8975f2009-01-10 21:58:09 -0800891 cpumask_or(core_mask, core_mask,
Rusty Russellfbd59a82009-01-10 21:58:08 -0800892 topology_core_cpumask(cpu));
Ben Hutchings46123d02008-09-01 12:47:33 +0100893 }
894 }
895
Rusty Russell2f8975f2009-01-10 21:58:09 -0800896 free_cpumask_var(core_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +0100897 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 */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100903static void efx_probe_interrupts(struct efx_nic *efx)
904{
Ben Hutchings46123d02008-09-01 12:47:33 +0100905 int max_channels =
906 min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100907 int rc, i;
908
909 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +0100910 struct msix_entry xentries[EFX_MAX_CHANNELS];
911 int wanted_ints;
Neil Turton28b581a2008-12-12 21:41:06 -0800912 int rx_queues;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100913
Ben Hutchings46123d02008-09-01 12:47:33 +0100914 /* 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 */
Neil Turton28b581a2008-12-12 21:41:06 -0800918 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);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +0100921
Neil Turton28b581a2008-12-12 21:41:06 -0800922 for (i = 0; i < wanted_ints; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100923 xentries[i].entry = i;
Neil Turton28b581a2008-12-12 21:41:06 -0800924 rc = pci_enable_msix(efx->pci_dev, xentries, wanted_ints);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100925 if (rc > 0) {
Neil Turton28b581a2008-12-12 21:41:06 -0800926 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;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100931 rc = pci_enable_msix(efx->pci_dev, xentries,
Neil Turton28b581a2008-12-12 21:41:06 -0800932 wanted_ints);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100933 }
934
935 if (rc == 0) {
Neil Turton28b581a2008-12-12 21:41:06 -0800936 efx->n_rx_queues = min(rx_queues, wanted_ints);
937 efx->n_channels = wanted_ints;
938 for (i = 0; i < wanted_ints; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100939 efx->channel[i].irq = xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100940 } 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) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100949 efx->n_rx_queues = 1;
Neil Turton28b581a2008-12-12 21:41:06 -0800950 efx->n_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100951 rc = pci_enable_msi(efx->pci_dev);
952 if (rc == 0) {
953 efx->channel[0].irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100954 } 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) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100962 efx->n_rx_queues = 1;
Neil Turton28b581a2008-12-12 21:41:06 -0800963 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100964 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 */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100973 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100974 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
Ben Hutchings8831da72008-09-01 12:47:48 +0100982static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100983{
984 struct efx_tx_queue *tx_queue;
985 struct efx_rx_queue *rx_queue;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100986
Ben Hutchings60ac1062008-09-01 12:44:59 +0100987 efx_for_each_tx_queue(tx_queue, efx) {
Neil Turton28b581a2008-12-12 21:41:06 -0800988 if (separate_tx_channels)
989 tx_queue->channel = &efx->channel[efx->n_channels-1];
Ben Hutchings60ac1062008-09-01 12:44:59 +0100990 else
991 tx_queue->channel = &efx->channel[0];
992 tx_queue->channel->used_flags |= EFX_USED_BY_TX;
993 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100994
Ben Hutchings8831da72008-09-01 12:47:48 +0100995 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;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100998 }
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
Ben Hutchings8831da72008-09-01 12:47:48 +01001016 efx_set_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001017
1018 /* Initialise the interrupt moderation settings */
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001019 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001020
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 }
Ben Hutchings56536e92008-12-12 21:37:02 -08001066 efx_set_channel_names(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001067
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;
Ben Hutchings55668612008-05-16 21:16:10 +01001097 if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001098 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);
Steve Hodgsondacccc72008-09-01 12:48:20 +01001103 if (efx_dev_registered(efx))
1104 efx_wake_queue(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001105
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. */
Ben Hutchingsb3475642008-05-16 21:15:49 +01001128 efx_for_each_rx_queue(rx_queue, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001129 cancel_delayed_work_sync(&rx_queue->work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001130
1131 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001132 cancel_work_sync(&efx->mac_work);
1133 cancel_work_sync(&efx->phy_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001134
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);
Ben Hutchings64ee3122008-09-01 12:47:38 +01001156 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001157 if (channel->irq)
1158 synchronize_irq(channel->irq);
Ben Hutchingsb3475642008-05-16 21:15:49 +01001159 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001160
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
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001170 /* Flush efx_phy_work, efx_mac_work, refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001171 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. */
Ben Hutchings5c8af3b2009-08-26 08:18:13 +00001175 falcon_deconfigure_mac_wrapper(efx);
1176 msleep(10); /* Let the Rx FIFO drain */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001177 falcon_drain_tx_fifo(efx);
1178
1179 /* Stop the kernel transmit interface late, so the watchdog
1180 * timer isn't ticking over the flush */
Ben Hutchings55668612008-05-16 21:16:10 +01001181 if (efx_dev_registered(efx)) {
Steve Hodgsondacccc72008-09-01 12:48:20 +01001182 efx_stop_queue(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001183 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 */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001199void efx_flush_queues(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001200{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001201 EFX_ASSERT_RESET_SERIALISED(efx);
1202
1203 efx_stop_all(efx);
1204
1205 efx_fini_channels(efx);
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001206 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001207
1208 efx_start_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001209}
1210
1211/**************************************************************************
1212 *
1213 * Interrupt moderation
1214 *
1215 **************************************************************************/
1216
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001217static 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
Ben Hutchings8ceee662008-04-27 12:55:59 +01001226/* Set interrupt moderation parameters */
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001227void efx_init_irq_moderation(struct efx_nic *efx, int tx_usecs, int rx_usecs,
1228 bool rx_adaptive)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001229{
1230 struct efx_tx_queue *tx_queue;
1231 struct efx_rx_queue *rx_queue;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001232 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);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001234
1235 EFX_ASSERT_RESET_SERIALISED(efx);
1236
1237 efx_for_each_tx_queue(tx_queue, efx)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001238 tx_queue->channel->irq_moderation = tx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001239
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001240 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001241 efx->irq_rx_moderation = rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001242 efx_for_each_rx_queue(rx_queue, efx)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001243 rx_queue->channel->irq_moderation = rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001244}
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);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001258 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001259
1260 EFX_TRACE(efx, "hardware monitor executing on CPU %d\n",
1261 raw_smp_processor_id());
1262
Ben Hutchings8ceee662008-04-27 12:55:59 +01001263 /* 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. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001266 if (!mutex_trylock(&efx->mac_lock))
1267 goto out_requeue;
1268 if (!efx->port_enabled)
1269 goto out_unlock;
Ben Hutchings278c0622009-11-23 16:05:12 +00001270 rc = falcon_board(efx)->monitor(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001271 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);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001276 }
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001277 efx->phy_op->poll(efx);
1278 efx->mac_op->poll(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001279
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001280out_unlock:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001281 mutex_unlock(&efx->mac_lock);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001282out_requeue:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001283 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{
Ben Hutchings767e4682008-09-01 12:43:14 +01001298 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001299 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001300
1301 EFX_ASSERT_RESET_SERIALISED(efx);
1302
Ben Hutchings68e7f452009-04-29 08:05:08 +00001303 /* 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);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001309}
1310
1311/**************************************************************************
1312 *
1313 * NAPI interface
1314 *
1315 **************************************************************************/
1316
1317static int efx_init_napi(struct efx_nic *efx)
1318{
1319 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001320
1321 efx_for_each_channel(channel, efx) {
1322 channel->napi_dev = efx->net_dev;
Ben Hutchings718cff12009-04-14 19:47:46 -07001323 netif_napi_add(channel->napi_dev, &channel->napi_str,
1324 efx_poll, napi_weight);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001325 }
1326 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001327}
1328
1329static void efx_fini_napi(struct efx_nic *efx)
1330{
1331 struct efx_channel *channel;
1332
1333 efx_for_each_channel(channel, efx) {
Ben Hutchings718cff12009-04-14 19:47:46 -07001334 if (channel->napi_dev)
1335 netif_napi_del(&channel->napi_str);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001336 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{
Ben Hutchings767e4682008-09-01 12:43:14 +01001354 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001355 struct efx_channel *channel;
1356
Ben Hutchings64ee3122008-09-01 12:47:38 +01001357 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001358 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{
Ben Hutchings767e4682008-09-01 12:43:14 +01001372 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001373 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
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001378 if (efx->state == STATE_DISABLED)
1379 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001380 if (efx->phy_mode & PHY_MODE_SPECIAL)
1381 return -EBUSY;
1382
Ben Hutchings8ceee662008-04-27 12:55:59 +01001383 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{
Ben Hutchings767e4682008-09-01 12:43:14 +01001393 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001394
1395 EFX_LOG(efx, "closing %s on CPU %d\n", net_dev->name,
1396 raw_smp_processor_id());
1397
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001398 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 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001404
1405 return 0;
1406}
1407
Ben Hutchings1974cc22009-01-29 18:00:07 +00001408void 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
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001422/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001423static struct net_device_stats *efx_net_stats(struct net_device *net_dev)
1424{
Ben Hutchings767e4682008-09-01 12:43:14 +01001425 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001426 struct efx_mac_stats *mac_stats = &efx->mac_stats;
1427 struct net_device_stats *stats = &net_dev->stats;
1428
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001429 /* Update stats if possible, but do not wait if another thread
Ben Hutchings1974cc22009-01-29 18:00:07 +00001430 * is updating them or if MAC stats fetches are temporarily
1431 * disabled; slightly stale stats are acceptable.
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001432 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001433 if (!spin_trylock(&efx->stats_lock))
1434 return stats;
Ben Hutchings1974cc22009-01-29 18:00:07 +00001435 if (!efx->stats_disable_count) {
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001436 efx->mac_op->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001437 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{
Ben Hutchings767e4682008-09-01 12:43:14 +01001472 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001473
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001474 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);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001477
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001478 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001479}
1480
1481
1482/* Context: process, rtnl_lock() held. */
1483static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1484{
Ben Hutchings767e4682008-09-01 12:43:14 +01001485 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001486 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;
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001499 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001500
1501 efx_start_all(efx);
1502 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001503}
1504
1505static int efx_set_mac_address(struct net_device *net_dev, void *data)
1506{
Ben Hutchings767e4682008-09-01 12:43:14 +01001507 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001508 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)) {
Johannes Berge1749612008-10-27 15:59:26 -07001514 EFX_ERR(efx, "invalid ethernet MAC address requested: %pM\n",
1515 new_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001516 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
Ben Hutchingsa816f752008-09-01 12:49:12 +01001527/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001528static void efx_set_multicast_list(struct net_device *net_dev)
1529{
Ben Hutchings767e4682008-09-01 12:43:14 +01001530 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001531 struct dev_mc_list *mc_list = net_dev->mc_list;
1532 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchingsa816f752008-09-01 12:49:12 +01001533 bool promiscuous = !!(net_dev->flags & IFF_PROMISC);
1534 bool changed = (efx->promiscuous != promiscuous);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001535 u32 crc;
1536 int bit;
1537 int i;
1538
Ben Hutchingsa816f752008-09-01 12:49:12 +01001539 efx->promiscuous = promiscuous;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001540
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
Ben Hutchingsa816f752008-09-01 12:49:12 +01001554 if (!efx->port_enabled)
1555 /* Delay pushing settings until efx_start_port() */
1556 return;
1557
1558 if (changed)
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001559 queue_work(efx->workqueue, &efx->phy_work);
Ben Hutchingsa816f752008-09-01 12:49:12 +01001560
Ben Hutchings8ceee662008-04-27 12:55:59 +01001561 /* Create and activate new global multicast hash table */
1562 falcon_set_multicast_hash(efx);
1563}
1564
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001565static 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
Ben Hutchings7dde5962008-12-12 22:09:38 -08001581static 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
Ben Hutchings8ceee662008-04-27 12:55:59 +01001588static int efx_netdev_event(struct notifier_block *this,
1589 unsigned long event, void *ptr)
1590{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001591 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001592
Ben Hutchings7dde5962008-12-12 22:09:38 -08001593 if (net_dev->netdev_ops == &efx_netdev_ops &&
1594 event == NETDEV_CHANGENAME)
1595 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001596
1597 return NOTIFY_DONE;
1598}
1599
1600static struct notifier_block efx_netdev_notifier = {
1601 .notifier_call = efx_netdev_event,
1602};
1603
Ben Hutchings06d5e192008-12-12 21:47:23 -08001604static 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
Ben Hutchings8ceee662008-04-27 12:55:59 +01001612static 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;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001619 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001620 SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
1621 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
1622
Ben Hutchings8ceee662008-04-27 12:55:59 +01001623 /* Clear MAC statistics */
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001624 efx->mac_op->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001625 memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
1626
Ben Hutchings7dde5962008-12-12 22:09:38 -08001627 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00001628
1629 rc = dev_alloc_name(net_dev, net_dev->name);
1630 if (rc < 0)
1631 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08001632 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00001633
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
Ben Hutchings7dde5962008-12-12 22:09:38 -08001641 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01001642
Ben Hutchings06d5e192008-12-12 21:47:23 -08001643 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
Ben Hutchings8ceee662008-04-27 12:55:59 +01001649 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08001650
Ben Hutchingsaed06282009-08-26 08:16:27 +00001651fail_locked:
1652 rtnl_unlock();
1653 EFX_ERR(efx, "could not register net dev\n");
1654 return rc;
1655
Ben Hutchings06d5e192008-12-12 21:47:23 -08001656fail_registered:
1657 unregister_netdev(net_dev);
1658 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001659}
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
Ben Hutchings767e4682008-09-01 12:43:14 +01001668 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001669
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
Ben Hutchings55668612008-05-16 21:16:10 +01001676 if (efx_dev_registered(efx)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001677 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
Ben Hutchings06d5e192008-12-12 21:47:23 -08001678 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001679 unregister_netdev(efx->net_dev);
1680 }
1681}
1682
1683/**************************************************************************
1684 *
1685 * Device reset and suspend
1686 *
1687 **************************************************************************/
1688
Ben Hutchings2467ca42008-09-01 12:48:50 +01001689/* Tears down the entire software state and most of the hardware state
1690 * before reset. */
Steve Hodgson4b988282009-01-29 17:50:51 +00001691void efx_reset_down(struct efx_nic *efx, enum reset_type method,
1692 struct ethtool_cmd *ecmd)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001693{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001694 EFX_ASSERT_RESET_SERIALISED(efx);
1695
Ben Hutchings1974cc22009-01-29 18:00:07 +00001696 efx_stats_disable(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01001697 efx_stop_all(efx);
1698 mutex_lock(&efx->mac_lock);
Ben Hutchingsf4150722008-11-04 20:34:28 +00001699 mutex_lock(&efx->spi_lock);
Ben Hutchings2467ca42008-09-01 12:48:50 +01001700
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001701 efx->phy_op->get_settings(efx, ecmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001702
1703 efx_fini_channels(efx);
Steve Hodgson4b988282009-01-29 17:50:51 +00001704 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
1705 efx->phy_op->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001706}
1707
Ben Hutchings2467ca42008-09-01 12:48:50 +01001708/* 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. */
Steve Hodgson4b988282009-01-29 17:50:51 +00001713int efx_reset_up(struct efx_nic *efx, enum reset_type method,
1714 struct ethtool_cmd *ecmd, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001715{
1716 int rc;
1717
Ben Hutchings2467ca42008-09-01 12:48:50 +01001718 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001719
Ben Hutchings2467ca42008-09-01 12:48:50 +01001720 rc = falcon_init_nic(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001721 if (rc) {
Ben Hutchings2467ca42008-09-01 12:48:50 +01001722 EFX_ERR(efx, "failed to initialise NIC\n");
1723 ok = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001724 }
1725
Steve Hodgson4b988282009-01-29 17:50:51 +00001726 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
1727 if (ok) {
1728 rc = efx->phy_op->init(efx);
1729 if (rc)
1730 ok = false;
Ben Hutchings115122a2009-03-04 09:52:52 +00001731 }
1732 if (!ok)
Steve Hodgson4b988282009-01-29 17:50:51 +00001733 efx->port_initialized = false;
1734 }
1735
Ben Hutchings2467ca42008-09-01 12:48:50 +01001736 if (ok) {
1737 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001738
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001739 if (efx->phy_op->set_settings(efx, ecmd))
Ben Hutchings2467ca42008-09-01 12:48:50 +01001740 EFX_ERR(efx, "could not restore PHY settings\n");
1741 }
1742
Ben Hutchingsf4150722008-11-04 20:34:28 +00001743 mutex_unlock(&efx->spi_lock);
Ben Hutchings2467ca42008-09-01 12:48:50 +01001744 mutex_unlock(&efx->mac_lock);
1745
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001746 if (ok) {
Ben Hutchings2467ca42008-09-01 12:48:50 +01001747 efx_start_all(efx);
Ben Hutchings1974cc22009-01-29 18:00:07 +00001748 efx_stats_enable(efx);
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001749 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001750 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;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001766 int rc = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001767
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");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001775 goto out_unlock;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001776 }
1777
Ben Hutchings8ceee662008-04-27 12:55:59 +01001778 EFX_INFO(efx, "resetting (%d)\n", method);
1779
Steve Hodgson4b988282009-01-29 17:50:51 +00001780 efx_reset_down(efx, method, &ecmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001781
1782 rc = falcon_reset_hw(efx, method);
1783 if (rc) {
1784 EFX_ERR(efx, "failed to reset hardware\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001785 goto out_disable;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001786 }
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
Ben Hutchings8ceee662008-04-27 12:55:59 +01001797 /* Leave device stopped if necessary */
1798 if (method == RESET_TYPE_DISABLE) {
Steve Hodgson4b988282009-01-29 17:50:51 +00001799 efx_reset_up(efx, method, &ecmd, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001800 rc = -EIO;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001801 } else {
Steve Hodgson4b988282009-01-29 17:50:51 +00001802 rc = efx_reset_up(efx, method, &ecmd, true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001803 }
1804
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001805out_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 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001813
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001814out_unlock:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001815 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01001816 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
Steve Hodgson1ab00622008-12-12 21:33:02 -08001863 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001864}
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 *
Ben Hutchings37594332009-11-23 16:05:45 +00001883 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01001884 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01001885 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01001886 * 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) {}
Ben Hutchings398468e2009-11-23 16:03:45 +00001895void efx_port_dummy_op_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
1896{
1897}
Ben Hutchings8ceee662008-04-27 12:55:59 +01001898
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001899static struct efx_mac_operations efx_dummy_mac_operations = {
1900 .reconfigure = efx_port_dummy_op_void,
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001901 .poll = efx_port_dummy_op_void,
1902 .irq = efx_port_dummy_op_void,
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001903};
1904
Ben Hutchings8ceee662008-04-27 12:55:59 +01001905static struct efx_phy_operations efx_dummy_phy_operations = {
1906 .init = efx_port_dummy_op_int,
1907 .reconfigure = efx_port_dummy_op_void,
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001908 .poll = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001909 .fini = efx_port_dummy_op_void,
1910 .clear_interrupt = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001911};
1912
Ben Hutchings8ceee662008-04-27 12:55:59 +01001913/**************************************************************************
1914 *
1915 * Data housekeeping
1916 *
1917 **************************************************************************/
1918
1919/* This zeroes out and then fills in the invariants in a struct
1920 * efx_nic (including all sub-structures).
1921 */
1922static int efx_init_struct(struct efx_nic *efx, struct efx_nic_type *type,
1923 struct pci_dev *pci_dev, struct net_device *net_dev)
1924{
1925 struct efx_channel *channel;
1926 struct efx_tx_queue *tx_queue;
1927 struct efx_rx_queue *rx_queue;
Steve Hodgson1ab00622008-12-12 21:33:02 -08001928 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001929
1930 /* Initialise common structures */
1931 memset(efx, 0, sizeof(*efx));
1932 spin_lock_init(&efx->biu_lock);
1933 spin_lock_init(&efx->phy_lock);
Ben Hutchingsf4150722008-11-04 20:34:28 +00001934 mutex_init(&efx->spi_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001935 INIT_WORK(&efx->reset_work, efx_reset_work);
1936 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
1937 efx->pci_dev = pci_dev;
1938 efx->state = STATE_INIT;
1939 efx->reset_pending = RESET_TYPE_NONE;
1940 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001941
1942 efx->net_dev = net_dev;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001943 efx->rx_checksum_enabled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001944 spin_lock_init(&efx->netif_stop_lock);
1945 spin_lock_init(&efx->stats_lock);
Ben Hutchings1974cc22009-01-29 18:00:07 +00001946 efx->stats_disable_count = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001947 mutex_init(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001948 efx->mac_op = &efx_dummy_mac_operations;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001949 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00001950 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001951 INIT_WORK(&efx->phy_work, efx_phy_work);
1952 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001953 atomic_set(&efx->netif_stop_count, 1);
1954
1955 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
1956 channel = &efx->channel[i];
1957 channel->efx = efx;
1958 channel->channel = i;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001959 channel->work_pending = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001960 }
Ben Hutchings60ac1062008-09-01 12:44:59 +01001961 for (i = 0; i < EFX_TX_QUEUE_COUNT; i++) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001962 tx_queue = &efx->tx_queue[i];
1963 tx_queue->efx = efx;
1964 tx_queue->queue = i;
1965 tx_queue->buffer = NULL;
1966 tx_queue->channel = &efx->channel[0]; /* for safety */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001967 tx_queue->tso_headers_free = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001968 }
1969 for (i = 0; i < EFX_MAX_RX_QUEUES; i++) {
1970 rx_queue = &efx->rx_queue[i];
1971 rx_queue->efx = efx;
1972 rx_queue->queue = i;
1973 rx_queue->channel = &efx->channel[0]; /* for safety */
1974 rx_queue->buffer = NULL;
1975 spin_lock_init(&rx_queue->add_lock);
1976 INIT_DELAYED_WORK(&rx_queue->work, efx_rx_work);
1977 }
1978
1979 efx->type = type;
1980
Ben Hutchings8ceee662008-04-27 12:55:59 +01001981 /* As close as we can get to guaranteeing that we don't overflow */
Ben Hutchings3ffeabd2009-10-23 08:30:58 +00001982 BUILD_BUG_ON(EFX_EVQ_SIZE < EFX_TXQ_SIZE + EFX_RXQ_SIZE);
1983
Ben Hutchings8ceee662008-04-27 12:55:59 +01001984 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
1985
1986 /* Higher numbered interrupt modes are less capable! */
1987 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
1988 interrupt_mode);
1989
Ben Hutchings6977dc62008-12-26 13:44:39 -08001990 /* Would be good to use the net_dev name, but we're too early */
1991 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
1992 pci_name(pci_dev));
1993 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08001994 if (!efx->workqueue)
1995 return -ENOMEM;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001996
Ben Hutchings8ceee662008-04-27 12:55:59 +01001997 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001998}
1999
2000static void efx_fini_struct(struct efx_nic *efx)
2001{
2002 if (efx->workqueue) {
2003 destroy_workqueue(efx->workqueue);
2004 efx->workqueue = NULL;
2005 }
2006}
2007
2008/**************************************************************************
2009 *
2010 * PCI interface
2011 *
2012 **************************************************************************/
2013
2014/* Main body of final NIC shutdown code
2015 * This is called only at module unload (or hotplug removal).
2016 */
2017static void efx_pci_remove_main(struct efx_nic *efx)
2018{
Ben Hutchingsf01865f2009-10-23 08:31:37 +00002019 falcon_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002020 efx_fini_channels(efx);
2021 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002022 efx_fini_napi(efx);
2023 efx_remove_all(efx);
2024}
2025
2026/* Final NIC shutdown
2027 * This is called only at module unload (or hotplug removal).
2028 */
2029static void efx_pci_remove(struct pci_dev *pci_dev)
2030{
2031 struct efx_nic *efx;
2032
2033 efx = pci_get_drvdata(pci_dev);
2034 if (!efx)
2035 return;
2036
2037 /* Mark the NIC as fini, then stop the interface */
2038 rtnl_lock();
2039 efx->state = STATE_FINI;
2040 dev_close(efx->net_dev);
2041
2042 /* Allow any queued efx_resets() to complete */
2043 rtnl_unlock();
2044
Ben Hutchings8ceee662008-04-27 12:55:59 +01002045 efx_unregister_netdev(efx);
2046
Ben Hutchings7dde5962008-12-12 22:09:38 -08002047 efx_mtd_remove(efx);
2048
Ben Hutchings8ceee662008-04-27 12:55:59 +01002049 /* Wait for any scheduled resets to complete. No more will be
2050 * scheduled from this point because efx_stop_all() has been
2051 * called, we are no longer registered with driverlink, and
2052 * the net_device's have been removed. */
Steve Hodgson1ab00622008-12-12 21:33:02 -08002053 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002054
2055 efx_pci_remove_main(efx);
2056
Ben Hutchings8ceee662008-04-27 12:55:59 +01002057 efx_fini_io(efx);
2058 EFX_LOG(efx, "shutdown successful\n");
2059
2060 pci_set_drvdata(pci_dev, NULL);
2061 efx_fini_struct(efx);
2062 free_netdev(efx->net_dev);
2063};
2064
2065/* Main body of NIC initialisation
2066 * This is called at module load (or hotplug insertion, theoretically).
2067 */
2068static int efx_pci_probe_main(struct efx_nic *efx)
2069{
2070 int rc;
2071
2072 /* Do start-of-day initialisation */
2073 rc = efx_probe_all(efx);
2074 if (rc)
2075 goto fail1;
2076
2077 rc = efx_init_napi(efx);
2078 if (rc)
2079 goto fail2;
2080
Ben Hutchings8ceee662008-04-27 12:55:59 +01002081 rc = falcon_init_nic(efx);
2082 if (rc) {
2083 EFX_ERR(efx, "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002084 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002085 }
2086
2087 rc = efx_init_port(efx);
2088 if (rc) {
2089 EFX_ERR(efx, "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002090 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002091 }
2092
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01002093 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002094
2095 rc = falcon_init_interrupt(efx);
2096 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002097 goto fail5;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002098
2099 return 0;
2100
Ben Hutchings278c0622009-11-23 16:05:12 +00002101 fail5:
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01002102 efx_fini_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002103 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002104 fail4:
2105 fail3:
2106 efx_fini_napi(efx);
2107 fail2:
2108 efx_remove_all(efx);
2109 fail1:
2110 return rc;
2111}
2112
2113/* NIC initialisation
2114 *
2115 * This is called at module load (or hotplug insertion,
2116 * theoretically). It sets up PCI mappings, tests and resets the NIC,
2117 * sets up and registers the network devices with the kernel and hooks
2118 * the interrupt service routine. It does not prepare the device for
2119 * transmission; this is left to the first time one of the network
2120 * interfaces is brought up (i.e. efx_net_open).
2121 */
2122static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2123 const struct pci_device_id *entry)
2124{
2125 struct efx_nic_type *type = (struct efx_nic_type *) entry->driver_data;
2126 struct net_device *net_dev;
2127 struct efx_nic *efx;
2128 int i, rc;
2129
2130 /* Allocate and initialise a struct net_device and struct efx_nic */
2131 net_dev = alloc_etherdev(sizeof(*efx));
2132 if (!net_dev)
2133 return -ENOMEM;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01002134 net_dev->features |= (NETIF_F_IP_CSUM | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002135 NETIF_F_HIGHDMA | NETIF_F_TSO |
2136 NETIF_F_GRO);
Ben Hutchings285065632008-09-01 12:46:54 +01002137 /* Mask for features that also apply to VLAN devices */
2138 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchings740847d2008-09-01 12:48:23 +01002139 NETIF_F_HIGHDMA | NETIF_F_TSO);
Ben Hutchings767e4682008-09-01 12:43:14 +01002140 efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002141 pci_set_drvdata(pci_dev, efx);
2142 rc = efx_init_struct(efx, type, pci_dev, net_dev);
2143 if (rc)
2144 goto fail1;
2145
2146 EFX_INFO(efx, "Solarflare Communications NIC detected\n");
2147
2148 /* Set up basic I/O (BAR mappings etc) */
2149 rc = efx_init_io(efx);
2150 if (rc)
2151 goto fail2;
2152
2153 /* No serialisation is required with the reset path because
2154 * we're in STATE_INIT. */
2155 for (i = 0; i < 5; i++) {
2156 rc = efx_pci_probe_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002157
2158 /* Serialise against efx_reset(). No more resets will be
2159 * scheduled since efx_stop_all() has been called, and we
2160 * have not and never have been registered with either
2161 * the rtnetlink or driverlink layers. */
Steve Hodgson1ab00622008-12-12 21:33:02 -08002162 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002163
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002164 if (rc == 0) {
2165 if (efx->reset_pending != RESET_TYPE_NONE) {
2166 /* If there was a scheduled reset during
2167 * probe, the NIC is probably hosed anyway */
2168 efx_pci_remove_main(efx);
2169 rc = -EIO;
2170 } else {
2171 break;
2172 }
2173 }
2174
Ben Hutchings8ceee662008-04-27 12:55:59 +01002175 /* Retry if a recoverably reset event has been scheduled */
2176 if ((efx->reset_pending != RESET_TYPE_INVISIBLE) &&
2177 (efx->reset_pending != RESET_TYPE_ALL))
2178 goto fail3;
2179
2180 efx->reset_pending = RESET_TYPE_NONE;
2181 }
2182
2183 if (rc) {
2184 EFX_ERR(efx, "Could not reset NIC\n");
2185 goto fail4;
2186 }
2187
2188 /* Switch to the running state before we expose the device to
2189 * the OS. This is to ensure that the initial gathering of
2190 * MAC stats succeeds. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002191 efx->state = STATE_RUNNING;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002192
Ben Hutchings8ceee662008-04-27 12:55:59 +01002193 rc = efx_register_netdev(efx);
2194 if (rc)
2195 goto fail5;
2196
2197 EFX_LOG(efx, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002198
2199 rtnl_lock();
2200 efx_mtd_probe(efx); /* allowed to fail */
2201 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002202 return 0;
2203
2204 fail5:
2205 efx_pci_remove_main(efx);
2206 fail4:
2207 fail3:
2208 efx_fini_io(efx);
2209 fail2:
2210 efx_fini_struct(efx);
2211 fail1:
2212 EFX_LOG(efx, "initialisation failed. rc=%d\n", rc);
2213 free_netdev(net_dev);
2214 return rc;
2215}
2216
2217static struct pci_driver efx_pci_driver = {
2218 .name = EFX_DRIVER_NAME,
2219 .id_table = efx_pci_table,
2220 .probe = efx_pci_probe,
2221 .remove = efx_pci_remove,
2222};
2223
2224/**************************************************************************
2225 *
2226 * Kernel module interface
2227 *
2228 *************************************************************************/
2229
2230module_param(interrupt_mode, uint, 0444);
2231MODULE_PARM_DESC(interrupt_mode,
2232 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2233
2234static int __init efx_init_module(void)
2235{
2236 int rc;
2237
2238 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2239
2240 rc = register_netdevice_notifier(&efx_netdev_notifier);
2241 if (rc)
2242 goto err_notifier;
2243
2244 refill_workqueue = create_workqueue("sfc_refill");
2245 if (!refill_workqueue) {
2246 rc = -ENOMEM;
2247 goto err_refill;
2248 }
Steve Hodgson1ab00622008-12-12 21:33:02 -08002249 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2250 if (!reset_workqueue) {
2251 rc = -ENOMEM;
2252 goto err_reset;
2253 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002254
2255 rc = pci_register_driver(&efx_pci_driver);
2256 if (rc < 0)
2257 goto err_pci;
2258
2259 return 0;
2260
2261 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08002262 destroy_workqueue(reset_workqueue);
2263 err_reset:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002264 destroy_workqueue(refill_workqueue);
2265 err_refill:
2266 unregister_netdevice_notifier(&efx_netdev_notifier);
2267 err_notifier:
2268 return rc;
2269}
2270
2271static void __exit efx_exit_module(void)
2272{
2273 printk(KERN_INFO "Solarflare NET driver unloading\n");
2274
2275 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002276 destroy_workqueue(reset_workqueue);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002277 destroy_workqueue(refill_workqueue);
2278 unregister_netdevice_notifier(&efx_netdev_notifier);
2279
2280}
2281
2282module_init(efx_init_module);
2283module_exit(efx_exit_module);
2284
2285MODULE_AUTHOR("Michael Brown <mbrown@fensystems.co.uk> and "
2286 "Solarflare Communications");
2287MODULE_DESCRIPTION("Solarflare Communications network driver");
2288MODULE_LICENSE("GPL");
2289MODULE_DEVICE_TABLE(pci, efx_pci_table);