blob: 4ebad613e6de66bd3ee2995b5700526c87f79a8c [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 "efx.h"
25#include "mdio_10g.h"
26#include "falcon.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010027
Ben Hutchingsc4593022009-11-23 16:08:17 +000028/**************************************************************************
29 *
30 * Type name strings
31 *
32 **************************************************************************
33 */
34
35/* Loopback mode names (see LOOPBACK_MODE()) */
36const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
37const char *efx_loopback_mode_names[] = {
38 [LOOPBACK_NONE] = "NONE",
39 [LOOPBACK_GMAC] = "GMAC",
40 [LOOPBACK_XGMII] = "XGMII",
41 [LOOPBACK_XGXS] = "XGXS",
42 [LOOPBACK_XAUI] = "XAUI",
43 [LOOPBACK_GPHY] = "GPHY",
44 [LOOPBACK_PHYXS] = "PHYXS",
45 [LOOPBACK_PCS] = "PCS",
46 [LOOPBACK_PMAPMD] = "PMA/PMD",
47 [LOOPBACK_NETWORK] = "NETWORK",
48};
49
50/* Interrupt mode names (see INT_MODE())) */
51const unsigned int efx_interrupt_mode_max = EFX_INT_MODE_MAX;
52const char *efx_interrupt_mode_names[] = {
53 [EFX_INT_MODE_MSIX] = "MSI-X",
54 [EFX_INT_MODE_MSI] = "MSI",
55 [EFX_INT_MODE_LEGACY] = "legacy",
56};
57
58const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
59const char *efx_reset_type_names[] = {
60 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
61 [RESET_TYPE_ALL] = "ALL",
62 [RESET_TYPE_WORLD] = "WORLD",
63 [RESET_TYPE_DISABLE] = "DISABLE",
64 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
65 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
66 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
67 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
68 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
69 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
70};
71
Ben Hutchings8ceee662008-04-27 12:55:59 +010072#define EFX_MAX_MTU (9 * 1024)
73
74/* RX slow fill workqueue. If memory allocation fails in the fast path,
75 * a work item is pushed onto this work queue to retry the allocation later,
76 * to avoid the NIC being starved of RX buffers. Since this is a per cpu
77 * workqueue, there is nothing to be gained in making it per NIC
78 */
79static struct workqueue_struct *refill_workqueue;
80
Steve Hodgson1ab00622008-12-12 21:33:02 -080081/* Reset workqueue. If any NIC has a hardware failure then a reset will be
82 * queued onto this work queue. This is not a per-nic work queue, because
83 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
84 */
85static struct workqueue_struct *reset_workqueue;
86
Ben Hutchings8ceee662008-04-27 12:55:59 +010087/**************************************************************************
88 *
89 * Configurable values
90 *
91 *************************************************************************/
92
93/*
Ben Hutchings8ceee662008-04-27 12:55:59 +010094 * Use separate channels for TX and RX events
95 *
Neil Turton28b581a2008-12-12 21:41:06 -080096 * Set this to 1 to use separate channels for TX and RX. It allows us
97 * to control interrupt affinity separately for TX and RX.
Ben Hutchings8ceee662008-04-27 12:55:59 +010098 *
Neil Turton28b581a2008-12-12 21:41:06 -080099 * This is only used in MSI-X interrupt mode
Ben Hutchings8ceee662008-04-27 12:55:59 +0100100 */
Neil Turton28b581a2008-12-12 21:41:06 -0800101static unsigned int separate_tx_channels;
102module_param(separate_tx_channels, uint, 0644);
103MODULE_PARM_DESC(separate_tx_channels,
104 "Use separate channels for TX and RX");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100105
106/* This is the weight assigned to each of the (per-channel) virtual
107 * NAPI devices.
108 */
109static int napi_weight = 64;
110
111/* This is the time (in jiffies) between invocations of the hardware
112 * monitor, which checks for known hardware bugs and resets the
113 * hardware and driver as necessary.
114 */
115unsigned int efx_monitor_interval = 1 * HZ;
116
Ben Hutchings8ceee662008-04-27 12:55:59 +0100117/* This controls whether or not the driver will initialise devices
118 * with invalid MAC addresses stored in the EEPROM or flash. If true,
119 * such devices will be initialised with a random locally-generated
120 * MAC address. This allows for loading the sfc_mtd driver to
121 * reprogram the flash, even if the flash contents (including the MAC
122 * address) have previously been erased.
123 */
124static unsigned int allow_bad_hwaddr;
125
126/* Initial interrupt moderation settings. They can be modified after
127 * module load with ethtool.
128 *
129 * The default for RX should strike a balance between increasing the
130 * round-trip latency and reducing overhead.
131 */
132static unsigned int rx_irq_mod_usec = 60;
133
134/* Initial interrupt moderation settings. They can be modified after
135 * module load with ethtool.
136 *
137 * This default is chosen to ensure that a 10G link does not go idle
138 * while a TX queue is stopped after it has become full. A queue is
139 * restarted when it drops below half full. The time this takes (assuming
140 * worst case 3 descriptors per packet and 1024 descriptors) is
141 * 512 / 3 * 1.2 = 205 usec.
142 */
143static unsigned int tx_irq_mod_usec = 150;
144
145/* This is the first interrupt mode to try out of:
146 * 0 => MSI-X
147 * 1 => MSI
148 * 2 => legacy
149 */
150static unsigned int interrupt_mode;
151
152/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
153 * i.e. the number of CPUs among which we may distribute simultaneous
154 * interrupt handling.
155 *
156 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
157 * The default (0) means to assign an interrupt to each package (level II cache)
158 */
159static unsigned int rss_cpus;
160module_param(rss_cpus, uint, 0444);
161MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
162
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800163static int phy_flash_cfg;
164module_param(phy_flash_cfg, int, 0644);
165MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
166
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000167static unsigned irq_adapt_low_thresh = 10000;
168module_param(irq_adapt_low_thresh, uint, 0644);
169MODULE_PARM_DESC(irq_adapt_low_thresh,
170 "Threshold score for reducing IRQ moderation");
171
172static unsigned irq_adapt_high_thresh = 20000;
173module_param(irq_adapt_high_thresh, uint, 0644);
174MODULE_PARM_DESC(irq_adapt_high_thresh,
175 "Threshold score for increasing IRQ moderation");
176
Ben Hutchings8ceee662008-04-27 12:55:59 +0100177/**************************************************************************
178 *
179 * Utility functions and prototypes
180 *
181 *************************************************************************/
182static void efx_remove_channel(struct efx_channel *channel);
183static void efx_remove_port(struct efx_nic *efx);
184static void efx_fini_napi(struct efx_nic *efx);
185static void efx_fini_channels(struct efx_nic *efx);
186
187#define EFX_ASSERT_RESET_SERIALISED(efx) \
188 do { \
Ben Hutchings332c1ce2009-11-25 16:08:52 +0000189 if ((efx->state == STATE_RUNNING) || \
190 (efx->state == STATE_DISABLED)) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100191 ASSERT_RTNL(); \
192 } while (0)
193
194/**************************************************************************
195 *
196 * Event queue processing
197 *
198 *************************************************************************/
199
200/* Process channel's event queue
201 *
202 * This function is responsible for processing the event queue of a
203 * single channel. The caller must guarantee that this function will
204 * never be concurrently called more than once on the same channel,
205 * though different channels may be being processed concurrently.
206 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100207static int efx_process_channel(struct efx_channel *channel, int rx_quota)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100208{
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100209 struct efx_nic *efx = channel->efx;
210 int rx_packets;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100211
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100212 if (unlikely(efx->reset_pending != RESET_TYPE_NONE ||
Ben Hutchings8ceee662008-04-27 12:55:59 +0100213 !channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100214 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100215
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100216 rx_packets = falcon_process_eventq(channel, rx_quota);
217 if (rx_packets == 0)
218 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100219
220 /* Deliver last RX packet. */
221 if (channel->rx_pkt) {
222 __efx_rx_packet(channel, channel->rx_pkt,
223 channel->rx_pkt_csummed);
224 channel->rx_pkt = NULL;
225 }
226
Ben Hutchings8ceee662008-04-27 12:55:59 +0100227 efx_rx_strategy(channel);
228
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100229 efx_fast_push_rx_descriptors(&efx->rx_queue[channel->channel]);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100230
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100231 return rx_packets;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100232}
233
234/* Mark channel as finished processing
235 *
236 * Note that since we will not receive further interrupts for this
237 * channel before we finish processing and call the eventq_read_ack()
238 * method, there is no need to use the interrupt hold-off timers.
239 */
240static inline void efx_channel_processed(struct efx_channel *channel)
241{
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100242 /* The interrupt handler for this channel may set work_pending
243 * as soon as we acknowledge the events we've seen. Make sure
244 * it's cleared before then. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100245 channel->work_pending = false;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100246 smp_wmb();
247
Ben Hutchings8ceee662008-04-27 12:55:59 +0100248 falcon_eventq_read_ack(channel);
249}
250
251/* NAPI poll handler
252 *
253 * NAPI guarantees serialisation of polls of the same device, which
254 * provides the guarantee required by efx_process_channel().
255 */
256static int efx_poll(struct napi_struct *napi, int budget)
257{
258 struct efx_channel *channel =
259 container_of(napi, struct efx_channel, napi_str);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100260 int rx_packets;
261
262 EFX_TRACE(channel->efx, "channel %d NAPI poll executing on CPU %d\n",
263 channel->channel, raw_smp_processor_id());
264
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100265 rx_packets = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100266
267 if (rx_packets < budget) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000268 struct efx_nic *efx = channel->efx;
269
270 if (channel->used_flags & EFX_USED_BY_RX &&
271 efx->irq_rx_adaptive &&
272 unlikely(++channel->irq_count == 1000)) {
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000273 if (unlikely(channel->irq_mod_score <
274 irq_adapt_low_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000275 if (channel->irq_moderation > 1) {
276 channel->irq_moderation -= 1;
277 falcon_set_int_moderation(channel);
278 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000279 } else if (unlikely(channel->irq_mod_score >
280 irq_adapt_high_thresh)) {
Ben Hutchings0d86ebd2009-10-23 08:32:13 +0000281 if (channel->irq_moderation <
282 efx->irq_rx_moderation) {
283 channel->irq_moderation += 1;
284 falcon_set_int_moderation(channel);
285 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000286 }
Ben Hutchings6fb70fd2009-03-20 13:30:37 +0000287 channel->irq_count = 0;
288 channel->irq_mod_score = 0;
289 }
290
Ben Hutchings8ceee662008-04-27 12:55:59 +0100291 /* There is no race here; although napi_disable() will
Ben Hutchings288379f2009-01-19 16:43:59 -0800292 * only wait for napi_complete(), this isn't a problem
Ben Hutchings8ceee662008-04-27 12:55:59 +0100293 * since efx_channel_processed() will have no effect if
294 * interrupts have already been disabled.
295 */
Ben Hutchings288379f2009-01-19 16:43:59 -0800296 napi_complete(napi);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100297 efx_channel_processed(channel);
298 }
299
300 return rx_packets;
301}
302
303/* Process the eventq of the specified channel immediately on this CPU
304 *
305 * Disable hardware generated interrupts, wait for any existing
306 * processing to finish, then directly poll (and ack ) the eventq.
307 * Finally reenable NAPI and interrupts.
308 *
309 * Since we are touching interrupts the caller should hold the suspend lock
310 */
311void efx_process_channel_now(struct efx_channel *channel)
312{
313 struct efx_nic *efx = channel->efx;
314
315 BUG_ON(!channel->used_flags);
316 BUG_ON(!channel->enabled);
317
318 /* Disable interrupts and wait for ISRs to complete */
319 falcon_disable_interrupts(efx);
320 if (efx->legacy_irq)
321 synchronize_irq(efx->legacy_irq);
Ben Hutchings64ee3122008-09-01 12:47:38 +0100322 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100323 synchronize_irq(channel->irq);
324
325 /* Wait for any NAPI processing to complete */
326 napi_disable(&channel->napi_str);
327
328 /* Poll the channel */
Ben Hutchings3ffeabd2009-10-23 08:30:58 +0000329 efx_process_channel(channel, EFX_EVQ_SIZE);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100330
331 /* Ack the eventq. This may cause an interrupt to be generated
332 * when they are reenabled */
333 efx_channel_processed(channel);
334
335 napi_enable(&channel->napi_str);
336 falcon_enable_interrupts(efx);
337}
338
339/* Create event queue
340 * Event queue memory allocations are done only once. If the channel
341 * is reset, the memory buffer will be reused; this guards against
342 * errors during channel reset and also simplifies interrupt handling.
343 */
344static int efx_probe_eventq(struct efx_channel *channel)
345{
346 EFX_LOG(channel->efx, "chan %d create event queue\n", channel->channel);
347
348 return falcon_probe_eventq(channel);
349}
350
351/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100352static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100353{
354 EFX_LOG(channel->efx, "chan %d init event queue\n", channel->channel);
355
356 channel->eventq_read_ptr = 0;
357
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100358 falcon_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100359}
360
361static void efx_fini_eventq(struct efx_channel *channel)
362{
363 EFX_LOG(channel->efx, "chan %d fini event queue\n", channel->channel);
364
365 falcon_fini_eventq(channel);
366}
367
368static void efx_remove_eventq(struct efx_channel *channel)
369{
370 EFX_LOG(channel->efx, "chan %d remove event queue\n", channel->channel);
371
372 falcon_remove_eventq(channel);
373}
374
375/**************************************************************************
376 *
377 * Channel handling
378 *
379 *************************************************************************/
380
Ben Hutchings8ceee662008-04-27 12:55:59 +0100381static int efx_probe_channel(struct efx_channel *channel)
382{
383 struct efx_tx_queue *tx_queue;
384 struct efx_rx_queue *rx_queue;
385 int rc;
386
387 EFX_LOG(channel->efx, "creating channel %d\n", channel->channel);
388
389 rc = efx_probe_eventq(channel);
390 if (rc)
391 goto fail1;
392
393 efx_for_each_channel_tx_queue(tx_queue, channel) {
394 rc = efx_probe_tx_queue(tx_queue);
395 if (rc)
396 goto fail2;
397 }
398
399 efx_for_each_channel_rx_queue(rx_queue, channel) {
400 rc = efx_probe_rx_queue(rx_queue);
401 if (rc)
402 goto fail3;
403 }
404
405 channel->n_rx_frm_trunc = 0;
406
407 return 0;
408
409 fail3:
410 efx_for_each_channel_rx_queue(rx_queue, channel)
411 efx_remove_rx_queue(rx_queue);
412 fail2:
413 efx_for_each_channel_tx_queue(tx_queue, channel)
414 efx_remove_tx_queue(tx_queue);
415 fail1:
416 return rc;
417}
418
419
Ben Hutchings56536e92008-12-12 21:37:02 -0800420static void efx_set_channel_names(struct efx_nic *efx)
421{
422 struct efx_channel *channel;
423 const char *type = "";
424 int number;
425
426 efx_for_each_channel(channel, efx) {
427 number = channel->channel;
428 if (efx->n_channels > efx->n_rx_queues) {
429 if (channel->channel < efx->n_rx_queues) {
430 type = "-rx";
431 } else {
432 type = "-tx";
433 number -= efx->n_rx_queues;
434 }
435 }
436 snprintf(channel->name, sizeof(channel->name),
437 "%s%s-%d", efx->name, type, number);
438 }
439}
440
Ben Hutchings8ceee662008-04-27 12:55:59 +0100441/* Channels are shutdown and reinitialised whilst the NIC is running
442 * to propagate configuration changes (mtu, checksum offload), or
443 * to clear hardware error conditions
444 */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100445static void efx_init_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100446{
447 struct efx_tx_queue *tx_queue;
448 struct efx_rx_queue *rx_queue;
449 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100450
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100451 /* Calculate the rx buffer allocation parameters required to
452 * support the current MTU, including padding for header
453 * alignment and overruns.
454 */
455 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
456 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
457 efx->type->rx_buffer_padding);
458 efx->rx_buffer_order = get_order(efx->rx_buffer_len);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100459
460 /* Initialise the channels */
461 efx_for_each_channel(channel, efx) {
462 EFX_LOG(channel->efx, "init chan %d\n", channel->channel);
463
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100464 efx_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100465
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100466 efx_for_each_channel_tx_queue(tx_queue, channel)
467 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100468
469 /* The rx buffer allocation strategy is MTU dependent */
470 efx_rx_strategy(channel);
471
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100472 efx_for_each_channel_rx_queue(rx_queue, channel)
473 efx_init_rx_queue(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100474
475 WARN_ON(channel->rx_pkt != NULL);
476 efx_rx_strategy(channel);
477 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100478}
479
480/* This enables event queue processing and packet transmission.
481 *
482 * Note that this function is not allowed to fail, since that would
483 * introduce too much complexity into the suspend/resume path.
484 */
485static void efx_start_channel(struct efx_channel *channel)
486{
487 struct efx_rx_queue *rx_queue;
488
489 EFX_LOG(channel->efx, "starting chan %d\n", channel->channel);
490
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100491 /* The interrupt handler for this channel may set work_pending
492 * as soon as we enable it. Make sure it's cleared before
493 * then. Similarly, make sure it sees the enabled flag set. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100494 channel->work_pending = false;
495 channel->enabled = true;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100496 smp_wmb();
Ben Hutchings8ceee662008-04-27 12:55:59 +0100497
498 napi_enable(&channel->napi_str);
499
500 /* Load up RX descriptors */
501 efx_for_each_channel_rx_queue(rx_queue, channel)
502 efx_fast_push_rx_descriptors(rx_queue);
503}
504
505/* This disables event queue processing and packet transmission.
506 * This function does not guarantee that all queue processing
507 * (e.g. RX refill) is complete.
508 */
509static void efx_stop_channel(struct efx_channel *channel)
510{
511 struct efx_rx_queue *rx_queue;
512
513 if (!channel->enabled)
514 return;
515
516 EFX_LOG(channel->efx, "stop chan %d\n", channel->channel);
517
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100518 channel->enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100519 napi_disable(&channel->napi_str);
520
521 /* Ensure that any worker threads have exited or will be no-ops */
522 efx_for_each_channel_rx_queue(rx_queue, channel) {
523 spin_lock_bh(&rx_queue->add_lock);
524 spin_unlock_bh(&rx_queue->add_lock);
525 }
526}
527
528static void efx_fini_channels(struct efx_nic *efx)
529{
530 struct efx_channel *channel;
531 struct efx_tx_queue *tx_queue;
532 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100533 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100534
535 EFX_ASSERT_RESET_SERIALISED(efx);
536 BUG_ON(efx->port_enabled);
537
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100538 rc = falcon_flush_queues(efx);
539 if (rc)
540 EFX_ERR(efx, "failed to flush queues\n");
541 else
542 EFX_LOG(efx, "successfully flushed all queues\n");
543
Ben Hutchings8ceee662008-04-27 12:55:59 +0100544 efx_for_each_channel(channel, efx) {
545 EFX_LOG(channel->efx, "shut down chan %d\n", channel->channel);
546
547 efx_for_each_channel_rx_queue(rx_queue, channel)
548 efx_fini_rx_queue(rx_queue);
549 efx_for_each_channel_tx_queue(tx_queue, channel)
550 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100551 efx_fini_eventq(channel);
552 }
553}
554
555static void efx_remove_channel(struct efx_channel *channel)
556{
557 struct efx_tx_queue *tx_queue;
558 struct efx_rx_queue *rx_queue;
559
560 EFX_LOG(channel->efx, "destroy chan %d\n", channel->channel);
561
562 efx_for_each_channel_rx_queue(rx_queue, channel)
563 efx_remove_rx_queue(rx_queue);
564 efx_for_each_channel_tx_queue(tx_queue, channel)
565 efx_remove_tx_queue(tx_queue);
566 efx_remove_eventq(channel);
567
568 channel->used_flags = 0;
569}
570
571void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue, int delay)
572{
573 queue_delayed_work(refill_workqueue, &rx_queue->work, delay);
574}
575
576/**************************************************************************
577 *
578 * Port handling
579 *
580 **************************************************************************/
581
582/* This ensures that the kernel is kept informed (via
583 * netif_carrier_on/off) of the link status, and also maintains the
584 * link status's stop on the port's TX queue.
585 */
586static void efx_link_status_changed(struct efx_nic *efx)
587{
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000588 struct efx_link_state *link_state = &efx->link_state;
589
Ben Hutchings8ceee662008-04-27 12:55:59 +0100590 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
591 * that no events are triggered between unregister_netdev() and the
592 * driver unloading. A more general condition is that NETDEV_CHANGE
593 * can only be generated between NETDEV_UP and NETDEV_DOWN */
594 if (!netif_running(efx->net_dev))
595 return;
596
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100597 if (efx->port_inhibited) {
598 netif_carrier_off(efx->net_dev);
599 return;
600 }
601
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000602 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100603 efx->n_link_state_changes++;
604
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000605 if (link_state->up)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100606 netif_carrier_on(efx->net_dev);
607 else
608 netif_carrier_off(efx->net_dev);
609 }
610
611 /* Status message for kernel log */
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000612 if (link_state->up) {
Ben Hutchingsf31a45d2008-12-12 21:43:33 -0800613 EFX_INFO(efx, "link up at %uMbps %s-duplex (MTU %d)%s\n",
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000614 link_state->speed, link_state->fd ? "full" : "half",
Ben Hutchings8ceee662008-04-27 12:55:59 +0100615 efx->net_dev->mtu,
616 (efx->promiscuous ? " [PROMISC]" : ""));
617 } else {
618 EFX_INFO(efx, "link down\n");
619 }
620
621}
622
Ben Hutchings115122a2009-03-04 09:52:52 +0000623static void efx_fini_port(struct efx_nic *efx);
624
Ben Hutchings8ceee662008-04-27 12:55:59 +0100625/* This call reinitialises the MAC to pick up new PHY settings. The
626 * caller must hold the mac_lock */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100627void __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100628{
629 WARN_ON(!mutex_is_locked(&efx->mac_lock));
630
631 EFX_LOG(efx, "reconfiguring MAC from PHY settings on CPU %d\n",
632 raw_smp_processor_id());
633
Ben Hutchingsa816f752008-09-01 12:49:12 +0100634 /* Serialise the promiscuous flag with efx_set_multicast_list. */
635 if (efx_dev_registered(efx)) {
636 netif_addr_lock_bh(efx->net_dev);
637 netif_addr_unlock_bh(efx->net_dev);
638 }
639
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800640 falcon_deconfigure_mac_wrapper(efx);
641
642 /* Reconfigure the PHY, disabling transmit in mac level loopback. */
643 if (LOOPBACK_INTERNAL(efx))
644 efx->phy_mode |= PHY_MODE_TX_DISABLED;
645 else
646 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
647 efx->phy_op->reconfigure(efx);
648
649 if (falcon_switch_mac(efx))
650 goto fail;
651
652 efx->mac_op->reconfigure(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100653
654 /* Inform kernel of loss/gain of carrier */
655 efx_link_status_changed(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800656 return;
657
658fail:
659 EFX_ERR(efx, "failed to reconfigure MAC\n");
Ben Hutchings115122a2009-03-04 09:52:52 +0000660 efx->port_enabled = false;
661 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100662}
663
664/* Reinitialise the MAC to pick up new PHY settings, even if the port is
665 * disabled. */
666void efx_reconfigure_port(struct efx_nic *efx)
667{
668 EFX_ASSERT_RESET_SERIALISED(efx);
669
670 mutex_lock(&efx->mac_lock);
671 __efx_reconfigure_port(efx);
672 mutex_unlock(&efx->mac_lock);
673}
674
675/* Asynchronous efx_reconfigure_port work item. To speed up efx_flush_all()
676 * we don't efx_reconfigure_port() if the port is disabled. Care is taken
677 * in efx_stop_all() and efx_start_port() to prevent PHY events being lost */
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800678static void efx_phy_work(struct work_struct *data)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100679{
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800680 struct efx_nic *efx = container_of(data, struct efx_nic, phy_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100681
682 mutex_lock(&efx->mac_lock);
683 if (efx->port_enabled)
684 __efx_reconfigure_port(efx);
685 mutex_unlock(&efx->mac_lock);
686}
687
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800688static void efx_mac_work(struct work_struct *data)
689{
690 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
691
692 mutex_lock(&efx->mac_lock);
693 if (efx->port_enabled)
694 efx->mac_op->irq(efx);
695 mutex_unlock(&efx->mac_lock);
696}
697
Ben Hutchings8ceee662008-04-27 12:55:59 +0100698static int efx_probe_port(struct efx_nic *efx)
699{
700 int rc;
701
702 EFX_LOG(efx, "create port\n");
703
704 /* Connect up MAC/PHY operations table and read MAC address */
705 rc = falcon_probe_port(efx);
706 if (rc)
707 goto err;
708
Ben Hutchings84ae48f2008-12-12 21:34:54 -0800709 if (phy_flash_cfg)
710 efx->phy_mode = PHY_MODE_SPECIAL;
711
Ben Hutchings8ceee662008-04-27 12:55:59 +0100712 /* Sanity check MAC address */
713 if (is_valid_ether_addr(efx->mac_address)) {
714 memcpy(efx->net_dev->dev_addr, efx->mac_address, ETH_ALEN);
715 } else {
Johannes Berge1749612008-10-27 15:59:26 -0700716 EFX_ERR(efx, "invalid MAC address %pM\n",
717 efx->mac_address);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100718 if (!allow_bad_hwaddr) {
719 rc = -EINVAL;
720 goto err;
721 }
722 random_ether_addr(efx->net_dev->dev_addr);
Johannes Berge1749612008-10-27 15:59:26 -0700723 EFX_INFO(efx, "using locally-generated MAC %pM\n",
724 efx->net_dev->dev_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100725 }
726
727 return 0;
728
729 err:
730 efx_remove_port(efx);
731 return rc;
732}
733
734static int efx_init_port(struct efx_nic *efx)
735{
736 int rc;
737
738 EFX_LOG(efx, "init port\n");
739
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800740 rc = efx->phy_op->init(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100741 if (rc)
742 return rc;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800743 mutex_lock(&efx->mac_lock);
Steve Hodgson4b988282009-01-29 17:50:51 +0000744 efx->phy_op->reconfigure(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800745 rc = falcon_switch_mac(efx);
746 mutex_unlock(&efx->mac_lock);
747 if (rc)
748 goto fail;
749 efx->mac_op->reconfigure(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100750
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100751 efx->port_initialized = true;
Ben Hutchings1974cc22009-01-29 18:00:07 +0000752 efx_stats_enable(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100753 return 0;
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800754
755fail:
756 efx->phy_op->fini(efx);
757 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100758}
759
760/* Allow efx_reconfigure_port() to be scheduled, and close the window
761 * between efx_stop_port and efx_flush_all whereby a previously scheduled
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800762 * efx_phy_work()/efx_mac_work() may have been cancelled */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100763static void efx_start_port(struct efx_nic *efx)
764{
765 EFX_LOG(efx, "start port\n");
766 BUG_ON(efx->port_enabled);
767
768 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100769 efx->port_enabled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100770 __efx_reconfigure_port(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800771 efx->mac_op->irq(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100772 mutex_unlock(&efx->mac_lock);
773}
774
Ben Hutchings766ca0f2008-12-12 21:59:24 -0800775/* Prevent efx_phy_work, efx_mac_work, and efx_monitor() from executing,
776 * and efx_set_multicast_list() from scheduling efx_phy_work. efx_phy_work
777 * and efx_mac_work may still be scheduled via NAPI processing until
778 * efx_flush_all() is called */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100779static void efx_stop_port(struct efx_nic *efx)
780{
781 EFX_LOG(efx, "stop port\n");
782
783 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100784 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100785 mutex_unlock(&efx->mac_lock);
786
787 /* Serialise against efx_set_multicast_list() */
Ben Hutchings55668612008-05-16 21:16:10 +0100788 if (efx_dev_registered(efx)) {
David S. Millerb9e40852008-07-15 00:15:08 -0700789 netif_addr_lock_bh(efx->net_dev);
790 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100791 }
792}
793
794static void efx_fini_port(struct efx_nic *efx)
795{
796 EFX_LOG(efx, "shut down port\n");
797
798 if (!efx->port_initialized)
799 return;
800
Ben Hutchings1974cc22009-01-29 18:00:07 +0000801 efx_stats_disable(efx);
Ben Hutchings177dfcd2008-12-12 21:50:08 -0800802 efx->phy_op->fini(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100803 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100804
Ben Hutchingseb50c0d2009-11-23 16:06:30 +0000805 efx->link_state.up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100806 efx_link_status_changed(efx);
807}
808
809static void efx_remove_port(struct efx_nic *efx)
810{
811 EFX_LOG(efx, "destroying port\n");
812
813 falcon_remove_port(efx);
814}
815
816/**************************************************************************
817 *
818 * NIC handling
819 *
820 **************************************************************************/
821
822/* This configures the PCI device to enable I/O and DMA. */
823static int efx_init_io(struct efx_nic *efx)
824{
825 struct pci_dev *pci_dev = efx->pci_dev;
826 dma_addr_t dma_mask = efx->type->max_dma_mask;
827 int rc;
828
829 EFX_LOG(efx, "initialising I/O\n");
830
831 rc = pci_enable_device(pci_dev);
832 if (rc) {
833 EFX_ERR(efx, "failed to enable PCI device\n");
834 goto fail1;
835 }
836
837 pci_set_master(pci_dev);
838
839 /* Set the PCI DMA mask. Try all possibilities from our
840 * genuine mask down to 32 bits, because some architectures
841 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
842 * masks event though they reject 46 bit masks.
843 */
844 while (dma_mask > 0x7fffffffUL) {
845 if (pci_dma_supported(pci_dev, dma_mask) &&
846 ((rc = pci_set_dma_mask(pci_dev, dma_mask)) == 0))
847 break;
848 dma_mask >>= 1;
849 }
850 if (rc) {
851 EFX_ERR(efx, "could not find a suitable DMA mask\n");
852 goto fail2;
853 }
854 EFX_LOG(efx, "using DMA mask %llx\n", (unsigned long long) dma_mask);
855 rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
856 if (rc) {
857 /* pci_set_consistent_dma_mask() is not *allowed* to
858 * fail with a mask that pci_set_dma_mask() accepted,
859 * but just in case...
860 */
861 EFX_ERR(efx, "failed to set consistent DMA mask\n");
862 goto fail2;
863 }
864
Ben Hutchingsdc803df2009-10-23 08:32:33 +0000865 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
866 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
Ben Hutchings8ceee662008-04-27 12:55:59 +0100867 if (rc) {
868 EFX_ERR(efx, "request for memory BAR failed\n");
869 rc = -EIO;
870 goto fail3;
871 }
872 efx->membase = ioremap_nocache(efx->membase_phys,
873 efx->type->mem_map_size);
874 if (!efx->membase) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +0000875 EFX_ERR(efx, "could not map memory BAR at %llx+%x\n",
Ben Hutchings086ea352008-05-16 21:17:06 +0100876 (unsigned long long)efx->membase_phys,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100877 efx->type->mem_map_size);
878 rc = -ENOMEM;
879 goto fail4;
880 }
Ben Hutchingsdc803df2009-10-23 08:32:33 +0000881 EFX_LOG(efx, "memory BAR at %llx+%x (virtual %p)\n",
882 (unsigned long long)efx->membase_phys,
Ben Hutchings086ea352008-05-16 21:17:06 +0100883 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100884
885 return 0;
886
887 fail4:
Ben Hutchingsdc803df2009-10-23 08:32:33 +0000888 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100889 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +0100890 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100891 fail2:
892 pci_disable_device(efx->pci_dev);
893 fail1:
894 return rc;
895}
896
897static void efx_fini_io(struct efx_nic *efx)
898{
899 EFX_LOG(efx, "shutting down I/O\n");
900
901 if (efx->membase) {
902 iounmap(efx->membase);
903 efx->membase = NULL;
904 }
905
906 if (efx->membase_phys) {
Ben Hutchingsdc803df2009-10-23 08:32:33 +0000907 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
Ben Hutchings2c118e02008-05-16 21:15:29 +0100908 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100909 }
910
911 pci_disable_device(efx->pci_dev);
912}
913
Ben Hutchings46123d02008-09-01 12:47:33 +0100914/* Get number of RX queues wanted. Return number of online CPU
915 * packages in the expectation that an IRQ balancer will spread
916 * interrupts across them. */
917static int efx_wanted_rx_queues(void)
918{
Rusty Russell2f8975f2009-01-10 21:58:09 -0800919 cpumask_var_t core_mask;
Ben Hutchings46123d02008-09-01 12:47:33 +0100920 int count;
921 int cpu;
922
Li Zefan79f55992009-06-15 14:58:26 +0800923 if (unlikely(!zalloc_cpumask_var(&core_mask, GFP_KERNEL))) {
Rusty Russell2f8975f2009-01-10 21:58:09 -0800924 printk(KERN_WARNING
Mike Travis3977d032009-05-12 10:48:36 +0000925 "sfc: RSS disabled due to allocation failure\n");
Rusty Russell2f8975f2009-01-10 21:58:09 -0800926 return 1;
927 }
928
Ben Hutchings46123d02008-09-01 12:47:33 +0100929 count = 0;
930 for_each_online_cpu(cpu) {
Rusty Russell2f8975f2009-01-10 21:58:09 -0800931 if (!cpumask_test_cpu(cpu, core_mask)) {
Ben Hutchings46123d02008-09-01 12:47:33 +0100932 ++count;
Rusty Russell2f8975f2009-01-10 21:58:09 -0800933 cpumask_or(core_mask, core_mask,
Rusty Russellfbd59a82009-01-10 21:58:08 -0800934 topology_core_cpumask(cpu));
Ben Hutchings46123d02008-09-01 12:47:33 +0100935 }
936 }
937
Rusty Russell2f8975f2009-01-10 21:58:09 -0800938 free_cpumask_var(core_mask);
Ben Hutchings46123d02008-09-01 12:47:33 +0100939 return count;
940}
941
942/* Probe the number and type of interrupts we are able to obtain, and
943 * the resulting numbers of channels and RX queues.
944 */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100945static void efx_probe_interrupts(struct efx_nic *efx)
946{
Ben Hutchings46123d02008-09-01 12:47:33 +0100947 int max_channels =
948 min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100949 int rc, i;
950
951 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +0100952 struct msix_entry xentries[EFX_MAX_CHANNELS];
953 int wanted_ints;
Neil Turton28b581a2008-12-12 21:41:06 -0800954 int rx_queues;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100955
Ben Hutchings46123d02008-09-01 12:47:33 +0100956 /* We want one RX queue and interrupt per CPU package
957 * (or as specified by the rss_cpus module parameter).
958 * We will need one channel per interrupt.
959 */
Neil Turton28b581a2008-12-12 21:41:06 -0800960 rx_queues = rss_cpus ? rss_cpus : efx_wanted_rx_queues();
961 wanted_ints = rx_queues + (separate_tx_channels ? 1 : 0);
962 wanted_ints = min(wanted_ints, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +0100963
Neil Turton28b581a2008-12-12 21:41:06 -0800964 for (i = 0; i < wanted_ints; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100965 xentries[i].entry = i;
Neil Turton28b581a2008-12-12 21:41:06 -0800966 rc = pci_enable_msix(efx->pci_dev, xentries, wanted_ints);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100967 if (rc > 0) {
Neil Turton28b581a2008-12-12 21:41:06 -0800968 EFX_ERR(efx, "WARNING: Insufficient MSI-X vectors"
969 " available (%d < %d).\n", rc, wanted_ints);
970 EFX_ERR(efx, "WARNING: Performance may be reduced.\n");
971 EFX_BUG_ON_PARANOID(rc >= wanted_ints);
972 wanted_ints = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100973 rc = pci_enable_msix(efx->pci_dev, xentries,
Neil Turton28b581a2008-12-12 21:41:06 -0800974 wanted_ints);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100975 }
976
977 if (rc == 0) {
Neil Turton28b581a2008-12-12 21:41:06 -0800978 efx->n_rx_queues = min(rx_queues, wanted_ints);
979 efx->n_channels = wanted_ints;
980 for (i = 0; i < wanted_ints; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100981 efx->channel[i].irq = xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100982 } else {
983 /* Fall back to single channel MSI */
984 efx->interrupt_mode = EFX_INT_MODE_MSI;
985 EFX_ERR(efx, "could not enable MSI-X\n");
986 }
987 }
988
989 /* Try single interrupt MSI */
990 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100991 efx->n_rx_queues = 1;
Neil Turton28b581a2008-12-12 21:41:06 -0800992 efx->n_channels = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100993 rc = pci_enable_msi(efx->pci_dev);
994 if (rc == 0) {
995 efx->channel[0].irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100996 } else {
997 EFX_ERR(efx, "could not enable MSI\n");
998 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
999 }
1000 }
1001
1002 /* Assume legacy interrupts */
1003 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Ben Hutchings8831da72008-09-01 12:47:48 +01001004 efx->n_rx_queues = 1;
Neil Turton28b581a2008-12-12 21:41:06 -08001005 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001006 efx->legacy_irq = efx->pci_dev->irq;
1007 }
1008}
1009
1010static void efx_remove_interrupts(struct efx_nic *efx)
1011{
1012 struct efx_channel *channel;
1013
1014 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +01001015 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001016 channel->irq = 0;
1017 pci_disable_msi(efx->pci_dev);
1018 pci_disable_msix(efx->pci_dev);
1019
1020 /* Remove legacy interrupt */
1021 efx->legacy_irq = 0;
1022}
1023
Ben Hutchings8831da72008-09-01 12:47:48 +01001024static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001025{
1026 struct efx_tx_queue *tx_queue;
1027 struct efx_rx_queue *rx_queue;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001028
Ben Hutchings60ac1062008-09-01 12:44:59 +01001029 efx_for_each_tx_queue(tx_queue, efx) {
Neil Turton28b581a2008-12-12 21:41:06 -08001030 if (separate_tx_channels)
1031 tx_queue->channel = &efx->channel[efx->n_channels-1];
Ben Hutchings60ac1062008-09-01 12:44:59 +01001032 else
1033 tx_queue->channel = &efx->channel[0];
1034 tx_queue->channel->used_flags |= EFX_USED_BY_TX;
1035 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001036
Ben Hutchings8831da72008-09-01 12:47:48 +01001037 efx_for_each_rx_queue(rx_queue, efx) {
1038 rx_queue->channel = &efx->channel[rx_queue->queue];
1039 rx_queue->channel->used_flags |= EFX_USED_BY_RX;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001040 }
1041}
1042
1043static int efx_probe_nic(struct efx_nic *efx)
1044{
1045 int rc;
1046
1047 EFX_LOG(efx, "creating NIC\n");
1048
1049 /* Carry out hardware-type specific initialisation */
1050 rc = falcon_probe_nic(efx);
1051 if (rc)
1052 return rc;
1053
1054 /* Determine the number of channels and RX queues by trying to hook
1055 * in MSI-X interrupts. */
1056 efx_probe_interrupts(efx);
1057
Ben Hutchings8831da72008-09-01 12:47:48 +01001058 efx_set_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001059
1060 /* Initialise the interrupt moderation settings */
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001061 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001062
1063 return 0;
1064}
1065
1066static void efx_remove_nic(struct efx_nic *efx)
1067{
1068 EFX_LOG(efx, "destroying NIC\n");
1069
1070 efx_remove_interrupts(efx);
1071 falcon_remove_nic(efx);
1072}
1073
1074/**************************************************************************
1075 *
1076 * NIC startup/shutdown
1077 *
1078 *************************************************************************/
1079
1080static int efx_probe_all(struct efx_nic *efx)
1081{
1082 struct efx_channel *channel;
1083 int rc;
1084
1085 /* Create NIC */
1086 rc = efx_probe_nic(efx);
1087 if (rc) {
1088 EFX_ERR(efx, "failed to create NIC\n");
1089 goto fail1;
1090 }
1091
1092 /* Create port */
1093 rc = efx_probe_port(efx);
1094 if (rc) {
1095 EFX_ERR(efx, "failed to create port\n");
1096 goto fail2;
1097 }
1098
1099 /* Create channels */
1100 efx_for_each_channel(channel, efx) {
1101 rc = efx_probe_channel(channel);
1102 if (rc) {
1103 EFX_ERR(efx, "failed to create channel %d\n",
1104 channel->channel);
1105 goto fail3;
1106 }
1107 }
Ben Hutchings56536e92008-12-12 21:37:02 -08001108 efx_set_channel_names(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001109
1110 return 0;
1111
1112 fail3:
1113 efx_for_each_channel(channel, efx)
1114 efx_remove_channel(channel);
1115 efx_remove_port(efx);
1116 fail2:
1117 efx_remove_nic(efx);
1118 fail1:
1119 return rc;
1120}
1121
1122/* Called after previous invocation(s) of efx_stop_all, restarts the
1123 * port, kernel transmit queue, NAPI processing and hardware interrupts,
1124 * and ensures that the port is scheduled to be reconfigured.
1125 * This function is safe to call multiple times when the NIC is in any
1126 * state. */
1127static void efx_start_all(struct efx_nic *efx)
1128{
1129 struct efx_channel *channel;
1130
1131 EFX_ASSERT_RESET_SERIALISED(efx);
1132
1133 /* Check that it is appropriate to restart the interface. All
1134 * of these flags are safe to read under just the rtnl lock */
1135 if (efx->port_enabled)
1136 return;
1137 if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1138 return;
Ben Hutchings55668612008-05-16 21:16:10 +01001139 if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001140 return;
1141
1142 /* Mark the port as enabled so port reconfigurations can start, then
1143 * restart the transmit interface early so the watchdog timer stops */
1144 efx_start_port(efx);
Steve Hodgsondacccc72008-09-01 12:48:20 +01001145 if (efx_dev_registered(efx))
1146 efx_wake_queue(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001147
1148 efx_for_each_channel(channel, efx)
1149 efx_start_channel(channel);
1150
1151 falcon_enable_interrupts(efx);
1152
1153 /* Start hardware monitor if we're in RUNNING */
1154 if (efx->state == STATE_RUNNING)
1155 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1156 efx_monitor_interval);
1157}
1158
1159/* Flush all delayed work. Should only be called when no more delayed work
1160 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1161 * since we're holding the rtnl_lock at this point. */
1162static void efx_flush_all(struct efx_nic *efx)
1163{
1164 struct efx_rx_queue *rx_queue;
1165
1166 /* Make sure the hardware monitor is stopped */
1167 cancel_delayed_work_sync(&efx->monitor_work);
1168
1169 /* Ensure that all RX slow refills are complete. */
Ben Hutchingsb3475642008-05-16 21:15:49 +01001170 efx_for_each_rx_queue(rx_queue, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001171 cancel_delayed_work_sync(&rx_queue->work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001172
1173 /* Stop scheduled port reconfigurations */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001174 cancel_work_sync(&efx->mac_work);
1175 cancel_work_sync(&efx->phy_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001176
1177}
1178
1179/* Quiesce hardware and software without bringing the link down.
1180 * Safe to call multiple times, when the nic and interface is in any
1181 * state. The caller is guaranteed to subsequently be in a position
1182 * to modify any hardware and software state they see fit without
1183 * taking locks. */
1184static void efx_stop_all(struct efx_nic *efx)
1185{
1186 struct efx_channel *channel;
1187
1188 EFX_ASSERT_RESET_SERIALISED(efx);
1189
1190 /* port_enabled can be read safely under the rtnl lock */
1191 if (!efx->port_enabled)
1192 return;
1193
1194 /* Disable interrupts and wait for ISR to complete */
1195 falcon_disable_interrupts(efx);
1196 if (efx->legacy_irq)
1197 synchronize_irq(efx->legacy_irq);
Ben Hutchings64ee3122008-09-01 12:47:38 +01001198 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001199 if (channel->irq)
1200 synchronize_irq(channel->irq);
Ben Hutchingsb3475642008-05-16 21:15:49 +01001201 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001202
1203 /* Stop all NAPI processing and synchronous rx refills */
1204 efx_for_each_channel(channel, efx)
1205 efx_stop_channel(channel);
1206
1207 /* Stop all asynchronous port reconfigurations. Since all
1208 * event processing has already been stopped, there is no
1209 * window to loose phy events */
1210 efx_stop_port(efx);
1211
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001212 /* Flush efx_phy_work, efx_mac_work, refill_workqueue, monitor_work */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001213 efx_flush_all(efx);
1214
1215 /* Isolate the MAC from the TX and RX engines, so that queue
1216 * flushes will complete in a timely fashion. */
Ben Hutchings5c8af3b2009-08-26 08:18:13 +00001217 falcon_deconfigure_mac_wrapper(efx);
1218 msleep(10); /* Let the Rx FIFO drain */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001219 falcon_drain_tx_fifo(efx);
1220
1221 /* Stop the kernel transmit interface late, so the watchdog
1222 * timer isn't ticking over the flush */
Ben Hutchings55668612008-05-16 21:16:10 +01001223 if (efx_dev_registered(efx)) {
Steve Hodgsondacccc72008-09-01 12:48:20 +01001224 efx_stop_queue(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001225 netif_tx_lock_bh(efx->net_dev);
1226 netif_tx_unlock_bh(efx->net_dev);
1227 }
1228}
1229
1230static void efx_remove_all(struct efx_nic *efx)
1231{
1232 struct efx_channel *channel;
1233
1234 efx_for_each_channel(channel, efx)
1235 efx_remove_channel(channel);
1236 efx_remove_port(efx);
1237 efx_remove_nic(efx);
1238}
1239
Ben Hutchings8ceee662008-04-27 12:55:59 +01001240/**************************************************************************
1241 *
1242 * Interrupt moderation
1243 *
1244 **************************************************************************/
1245
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001246static unsigned irq_mod_ticks(int usecs, int resolution)
1247{
1248 if (usecs <= 0)
1249 return 0; /* cannot receive interrupts ahead of time :-) */
1250 if (usecs < resolution)
1251 return 1; /* never round down to 0 */
1252 return usecs / resolution;
1253}
1254
Ben Hutchings8ceee662008-04-27 12:55:59 +01001255/* Set interrupt moderation parameters */
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001256void efx_init_irq_moderation(struct efx_nic *efx, int tx_usecs, int rx_usecs,
1257 bool rx_adaptive)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001258{
1259 struct efx_tx_queue *tx_queue;
1260 struct efx_rx_queue *rx_queue;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001261 unsigned tx_ticks = irq_mod_ticks(tx_usecs, FALCON_IRQ_MOD_RESOLUTION);
1262 unsigned rx_ticks = irq_mod_ticks(rx_usecs, FALCON_IRQ_MOD_RESOLUTION);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001263
1264 EFX_ASSERT_RESET_SERIALISED(efx);
1265
1266 efx_for_each_tx_queue(tx_queue, efx)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001267 tx_queue->channel->irq_moderation = tx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001268
Ben Hutchings6fb70fd2009-03-20 13:30:37 +00001269 efx->irq_rx_adaptive = rx_adaptive;
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001270 efx->irq_rx_moderation = rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001271 efx_for_each_rx_queue(rx_queue, efx)
Ben Hutchings0d86ebd2009-10-23 08:32:13 +00001272 rx_queue->channel->irq_moderation = rx_ticks;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001273}
1274
1275/**************************************************************************
1276 *
1277 * Hardware monitor
1278 *
1279 **************************************************************************/
1280
1281/* Run periodically off the general workqueue. Serialised against
1282 * efx_reconfigure_port via the mac_lock */
1283static void efx_monitor(struct work_struct *data)
1284{
1285 struct efx_nic *efx = container_of(data, struct efx_nic,
1286 monitor_work.work);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001287 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001288
1289 EFX_TRACE(efx, "hardware monitor executing on CPU %d\n",
1290 raw_smp_processor_id());
1291
Ben Hutchings8ceee662008-04-27 12:55:59 +01001292 /* If the mac_lock is already held then it is likely a port
1293 * reconfiguration is already in place, which will likely do
1294 * most of the work of check_hw() anyway. */
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001295 if (!mutex_trylock(&efx->mac_lock))
1296 goto out_requeue;
1297 if (!efx->port_enabled)
1298 goto out_unlock;
Ben Hutchings44838a42009-11-25 16:09:41 +00001299 rc = falcon_board(efx)->type->monitor(efx);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001300 if (rc) {
1301 EFX_ERR(efx, "Board sensor %s; shutting down PHY\n",
1302 (rc == -ERANGE) ? "reported fault" : "failed");
1303 efx->phy_mode |= PHY_MODE_LOW_POWER;
1304 falcon_sim_phy_event(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001305 }
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001306 efx->phy_op->poll(efx);
1307 efx->mac_op->poll(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001308
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001309out_unlock:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001310 mutex_unlock(&efx->mac_lock);
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001311out_requeue:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001312 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1313 efx_monitor_interval);
1314}
1315
1316/**************************************************************************
1317 *
1318 * ioctls
1319 *
1320 *************************************************************************/
1321
1322/* Net device ioctl
1323 * Context: process, rtnl_lock() held.
1324 */
1325static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1326{
Ben Hutchings767e4682008-09-01 12:43:14 +01001327 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings68e7f452009-04-29 08:05:08 +00001328 struct mii_ioctl_data *data = if_mii(ifr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001329
1330 EFX_ASSERT_RESET_SERIALISED(efx);
1331
Ben Hutchings68e7f452009-04-29 08:05:08 +00001332 /* Convert phy_id from older PRTAD/DEVAD format */
1333 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1334 (data->phy_id & 0xfc00) == 0x0400)
1335 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1336
1337 return mdio_mii_ioctl(&efx->mdio, data, cmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001338}
1339
1340/**************************************************************************
1341 *
1342 * NAPI interface
1343 *
1344 **************************************************************************/
1345
1346static int efx_init_napi(struct efx_nic *efx)
1347{
1348 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001349
1350 efx_for_each_channel(channel, efx) {
1351 channel->napi_dev = efx->net_dev;
Ben Hutchings718cff12009-04-14 19:47:46 -07001352 netif_napi_add(channel->napi_dev, &channel->napi_str,
1353 efx_poll, napi_weight);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001354 }
1355 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001356}
1357
1358static void efx_fini_napi(struct efx_nic *efx)
1359{
1360 struct efx_channel *channel;
1361
1362 efx_for_each_channel(channel, efx) {
Ben Hutchings718cff12009-04-14 19:47:46 -07001363 if (channel->napi_dev)
1364 netif_napi_del(&channel->napi_str);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001365 channel->napi_dev = NULL;
1366 }
1367}
1368
1369/**************************************************************************
1370 *
1371 * Kernel netpoll interface
1372 *
1373 *************************************************************************/
1374
1375#ifdef CONFIG_NET_POLL_CONTROLLER
1376
1377/* Although in the common case interrupts will be disabled, this is not
1378 * guaranteed. However, all our work happens inside the NAPI callback,
1379 * so no locking is required.
1380 */
1381static void efx_netpoll(struct net_device *net_dev)
1382{
Ben Hutchings767e4682008-09-01 12:43:14 +01001383 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001384 struct efx_channel *channel;
1385
Ben Hutchings64ee3122008-09-01 12:47:38 +01001386 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001387 efx_schedule_channel(channel);
1388}
1389
1390#endif
1391
1392/**************************************************************************
1393 *
1394 * Kernel net device interface
1395 *
1396 *************************************************************************/
1397
1398/* Context: process, rtnl_lock() held. */
1399static int efx_net_open(struct net_device *net_dev)
1400{
Ben Hutchings767e4682008-09-01 12:43:14 +01001401 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001402 EFX_ASSERT_RESET_SERIALISED(efx);
1403
1404 EFX_LOG(efx, "opening device %s on CPU %d\n", net_dev->name,
1405 raw_smp_processor_id());
1406
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001407 if (efx->state == STATE_DISABLED)
1408 return -EIO;
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001409 if (efx->phy_mode & PHY_MODE_SPECIAL)
1410 return -EBUSY;
1411
Ben Hutchings8ceee662008-04-27 12:55:59 +01001412 efx_start_all(efx);
1413 return 0;
1414}
1415
1416/* Context: process, rtnl_lock() held.
1417 * Note that the kernel will ignore our return code; this method
1418 * should really be a void.
1419 */
1420static int efx_net_stop(struct net_device *net_dev)
1421{
Ben Hutchings767e4682008-09-01 12:43:14 +01001422 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001423
1424 EFX_LOG(efx, "closing %s on CPU %d\n", net_dev->name,
1425 raw_smp_processor_id());
1426
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001427 if (efx->state != STATE_DISABLED) {
1428 /* Stop the device and flush all the channels */
1429 efx_stop_all(efx);
1430 efx_fini_channels(efx);
1431 efx_init_channels(efx);
1432 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001433
1434 return 0;
1435}
1436
Ben Hutchings1974cc22009-01-29 18:00:07 +00001437void efx_stats_disable(struct efx_nic *efx)
1438{
1439 spin_lock(&efx->stats_lock);
1440 ++efx->stats_disable_count;
1441 spin_unlock(&efx->stats_lock);
1442}
1443
1444void efx_stats_enable(struct efx_nic *efx)
1445{
1446 spin_lock(&efx->stats_lock);
1447 --efx->stats_disable_count;
1448 spin_unlock(&efx->stats_lock);
1449}
1450
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001451/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001452static struct net_device_stats *efx_net_stats(struct net_device *net_dev)
1453{
Ben Hutchings767e4682008-09-01 12:43:14 +01001454 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001455 struct efx_mac_stats *mac_stats = &efx->mac_stats;
1456 struct net_device_stats *stats = &net_dev->stats;
1457
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001458 /* Update stats if possible, but do not wait if another thread
Ben Hutchings1974cc22009-01-29 18:00:07 +00001459 * is updating them or if MAC stats fetches are temporarily
1460 * disabled; slightly stale stats are acceptable.
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001461 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001462 if (!spin_trylock(&efx->stats_lock))
1463 return stats;
Ben Hutchings1974cc22009-01-29 18:00:07 +00001464 if (!efx->stats_disable_count) {
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001465 efx->mac_op->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001466 falcon_update_nic_stats(efx);
1467 }
1468 spin_unlock(&efx->stats_lock);
1469
1470 stats->rx_packets = mac_stats->rx_packets;
1471 stats->tx_packets = mac_stats->tx_packets;
1472 stats->rx_bytes = mac_stats->rx_bytes;
1473 stats->tx_bytes = mac_stats->tx_bytes;
1474 stats->multicast = mac_stats->rx_multicast;
1475 stats->collisions = mac_stats->tx_collision;
1476 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1477 mac_stats->rx_length_error);
1478 stats->rx_over_errors = efx->n_rx_nodesc_drop_cnt;
1479 stats->rx_crc_errors = mac_stats->rx_bad;
1480 stats->rx_frame_errors = mac_stats->rx_align_error;
1481 stats->rx_fifo_errors = mac_stats->rx_overflow;
1482 stats->rx_missed_errors = mac_stats->rx_missed;
1483 stats->tx_window_errors = mac_stats->tx_late_collision;
1484
1485 stats->rx_errors = (stats->rx_length_errors +
1486 stats->rx_over_errors +
1487 stats->rx_crc_errors +
1488 stats->rx_frame_errors +
1489 stats->rx_fifo_errors +
1490 stats->rx_missed_errors +
1491 mac_stats->rx_symbol_error);
1492 stats->tx_errors = (stats->tx_window_errors +
1493 mac_stats->tx_bad);
1494
1495 return stats;
1496}
1497
1498/* Context: netif_tx_lock held, BHs disabled. */
1499static void efx_watchdog(struct net_device *net_dev)
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
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001503 EFX_ERR(efx, "TX stuck with stop_count=%d port_enabled=%d:"
1504 " resetting channels\n",
1505 atomic_read(&efx->netif_stop_count), efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001506
Ben Hutchings739bb23d2008-11-04 20:35:36 +00001507 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001508}
1509
1510
1511/* Context: process, rtnl_lock() held. */
1512static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1513{
Ben Hutchings767e4682008-09-01 12:43:14 +01001514 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001515 int rc = 0;
1516
1517 EFX_ASSERT_RESET_SERIALISED(efx);
1518
1519 if (new_mtu > EFX_MAX_MTU)
1520 return -EINVAL;
1521
1522 efx_stop_all(efx);
1523
1524 EFX_LOG(efx, "changing MTU to %d\n", new_mtu);
1525
1526 efx_fini_channels(efx);
1527 net_dev->mtu = new_mtu;
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001528 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001529
1530 efx_start_all(efx);
1531 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001532}
1533
1534static int efx_set_mac_address(struct net_device *net_dev, void *data)
1535{
Ben Hutchings767e4682008-09-01 12:43:14 +01001536 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001537 struct sockaddr *addr = data;
1538 char *new_addr = addr->sa_data;
1539
1540 EFX_ASSERT_RESET_SERIALISED(efx);
1541
1542 if (!is_valid_ether_addr(new_addr)) {
Johannes Berge1749612008-10-27 15:59:26 -07001543 EFX_ERR(efx, "invalid ethernet MAC address requested: %pM\n",
1544 new_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001545 return -EINVAL;
1546 }
1547
1548 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1549
1550 /* Reconfigure the MAC */
1551 efx_reconfigure_port(efx);
1552
1553 return 0;
1554}
1555
Ben Hutchingsa816f752008-09-01 12:49:12 +01001556/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001557static void efx_set_multicast_list(struct net_device *net_dev)
1558{
Ben Hutchings767e4682008-09-01 12:43:14 +01001559 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001560 struct dev_mc_list *mc_list = net_dev->mc_list;
1561 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchingsa816f752008-09-01 12:49:12 +01001562 bool promiscuous = !!(net_dev->flags & IFF_PROMISC);
1563 bool changed = (efx->promiscuous != promiscuous);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001564 u32 crc;
1565 int bit;
1566 int i;
1567
Ben Hutchingsa816f752008-09-01 12:49:12 +01001568 efx->promiscuous = promiscuous;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001569
1570 /* Build multicast hash table */
1571 if (promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
1572 memset(mc_hash, 0xff, sizeof(*mc_hash));
1573 } else {
1574 memset(mc_hash, 0x00, sizeof(*mc_hash));
1575 for (i = 0; i < net_dev->mc_count; i++) {
1576 crc = ether_crc_le(ETH_ALEN, mc_list->dmi_addr);
1577 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1578 set_bit_le(bit, mc_hash->byte);
1579 mc_list = mc_list->next;
1580 }
1581 }
1582
Ben Hutchingsa816f752008-09-01 12:49:12 +01001583 if (!efx->port_enabled)
1584 /* Delay pushing settings until efx_start_port() */
1585 return;
1586
1587 if (changed)
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001588 queue_work(efx->workqueue, &efx->phy_work);
Ben Hutchingsa816f752008-09-01 12:49:12 +01001589
Ben Hutchings8ceee662008-04-27 12:55:59 +01001590 /* Create and activate new global multicast hash table */
1591 falcon_set_multicast_hash(efx);
1592}
1593
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001594static const struct net_device_ops efx_netdev_ops = {
1595 .ndo_open = efx_net_open,
1596 .ndo_stop = efx_net_stop,
1597 .ndo_get_stats = efx_net_stats,
1598 .ndo_tx_timeout = efx_watchdog,
1599 .ndo_start_xmit = efx_hard_start_xmit,
1600 .ndo_validate_addr = eth_validate_addr,
1601 .ndo_do_ioctl = efx_ioctl,
1602 .ndo_change_mtu = efx_change_mtu,
1603 .ndo_set_mac_address = efx_set_mac_address,
1604 .ndo_set_multicast_list = efx_set_multicast_list,
1605#ifdef CONFIG_NET_POLL_CONTROLLER
1606 .ndo_poll_controller = efx_netpoll,
1607#endif
1608};
1609
Ben Hutchings7dde5962008-12-12 22:09:38 -08001610static void efx_update_name(struct efx_nic *efx)
1611{
1612 strcpy(efx->name, efx->net_dev->name);
1613 efx_mtd_rename(efx);
1614 efx_set_channel_names(efx);
1615}
1616
Ben Hutchings8ceee662008-04-27 12:55:59 +01001617static int efx_netdev_event(struct notifier_block *this,
1618 unsigned long event, void *ptr)
1619{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001620 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001621
Ben Hutchings7dde5962008-12-12 22:09:38 -08001622 if (net_dev->netdev_ops == &efx_netdev_ops &&
1623 event == NETDEV_CHANGENAME)
1624 efx_update_name(netdev_priv(net_dev));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001625
1626 return NOTIFY_DONE;
1627}
1628
1629static struct notifier_block efx_netdev_notifier = {
1630 .notifier_call = efx_netdev_event,
1631};
1632
Ben Hutchings06d5e192008-12-12 21:47:23 -08001633static ssize_t
1634show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
1635{
1636 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
1637 return sprintf(buf, "%d\n", efx->phy_type);
1638}
1639static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
1640
Ben Hutchings8ceee662008-04-27 12:55:59 +01001641static int efx_register_netdev(struct efx_nic *efx)
1642{
1643 struct net_device *net_dev = efx->net_dev;
1644 int rc;
1645
1646 net_dev->watchdog_timeo = 5 * HZ;
1647 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001648 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001649 SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
1650 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
1651
Ben Hutchings8ceee662008-04-27 12:55:59 +01001652 /* Clear MAC statistics */
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001653 efx->mac_op->update_stats(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001654 memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
1655
Ben Hutchings7dde5962008-12-12 22:09:38 -08001656 rtnl_lock();
Ben Hutchingsaed06282009-08-26 08:16:27 +00001657
1658 rc = dev_alloc_name(net_dev, net_dev->name);
1659 if (rc < 0)
1660 goto fail_locked;
Ben Hutchings7dde5962008-12-12 22:09:38 -08001661 efx_update_name(efx);
Ben Hutchingsaed06282009-08-26 08:16:27 +00001662
1663 rc = register_netdevice(net_dev);
1664 if (rc)
1665 goto fail_locked;
1666
1667 /* Always start with carrier off; PHY events will detect the link */
1668 netif_carrier_off(efx->net_dev);
1669
Ben Hutchings7dde5962008-12-12 22:09:38 -08001670 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01001671
Ben Hutchings06d5e192008-12-12 21:47:23 -08001672 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
1673 if (rc) {
1674 EFX_ERR(efx, "failed to init net dev attributes\n");
1675 goto fail_registered;
1676 }
1677
Ben Hutchings8ceee662008-04-27 12:55:59 +01001678 return 0;
Ben Hutchings06d5e192008-12-12 21:47:23 -08001679
Ben Hutchingsaed06282009-08-26 08:16:27 +00001680fail_locked:
1681 rtnl_unlock();
1682 EFX_ERR(efx, "could not register net dev\n");
1683 return rc;
1684
Ben Hutchings06d5e192008-12-12 21:47:23 -08001685fail_registered:
1686 unregister_netdev(net_dev);
1687 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001688}
1689
1690static void efx_unregister_netdev(struct efx_nic *efx)
1691{
1692 struct efx_tx_queue *tx_queue;
1693
1694 if (!efx->net_dev)
1695 return;
1696
Ben Hutchings767e4682008-09-01 12:43:14 +01001697 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001698
1699 /* Free up any skbs still remaining. This has to happen before
1700 * we try to unregister the netdev as running their destructors
1701 * may be needed to get the device ref. count to 0. */
1702 efx_for_each_tx_queue(tx_queue, efx)
1703 efx_release_tx_buffers(tx_queue);
1704
Ben Hutchings55668612008-05-16 21:16:10 +01001705 if (efx_dev_registered(efx)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001706 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
Ben Hutchings06d5e192008-12-12 21:47:23 -08001707 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001708 unregister_netdev(efx->net_dev);
1709 }
1710}
1711
1712/**************************************************************************
1713 *
1714 * Device reset and suspend
1715 *
1716 **************************************************************************/
1717
Ben Hutchings2467ca42008-09-01 12:48:50 +01001718/* Tears down the entire software state and most of the hardware state
1719 * before reset. */
Steve Hodgson4b988282009-01-29 17:50:51 +00001720void efx_reset_down(struct efx_nic *efx, enum reset_type method,
1721 struct ethtool_cmd *ecmd)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001722{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001723 EFX_ASSERT_RESET_SERIALISED(efx);
1724
Ben Hutchings1974cc22009-01-29 18:00:07 +00001725 efx_stats_disable(efx);
Ben Hutchings2467ca42008-09-01 12:48:50 +01001726 efx_stop_all(efx);
1727 mutex_lock(&efx->mac_lock);
Ben Hutchingsf4150722008-11-04 20:34:28 +00001728 mutex_lock(&efx->spi_lock);
Ben Hutchings2467ca42008-09-01 12:48:50 +01001729
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001730 efx->phy_op->get_settings(efx, ecmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001731
1732 efx_fini_channels(efx);
Steve Hodgson4b988282009-01-29 17:50:51 +00001733 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
1734 efx->phy_op->fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001735}
1736
Ben Hutchings2467ca42008-09-01 12:48:50 +01001737/* This function will always ensure that the locks acquired in
1738 * efx_reset_down() are released. A failure return code indicates
1739 * that we were unable to reinitialise the hardware, and the
1740 * driver should be disabled. If ok is false, then the rx and tx
1741 * engines are not restarted, pending a RESET_DISABLE. */
Steve Hodgson4b988282009-01-29 17:50:51 +00001742int efx_reset_up(struct efx_nic *efx, enum reset_type method,
1743 struct ethtool_cmd *ecmd, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001744{
1745 int rc;
1746
Ben Hutchings2467ca42008-09-01 12:48:50 +01001747 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001748
Ben Hutchings2467ca42008-09-01 12:48:50 +01001749 rc = falcon_init_nic(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001750 if (rc) {
Ben Hutchings2467ca42008-09-01 12:48:50 +01001751 EFX_ERR(efx, "failed to initialise NIC\n");
1752 ok = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001753 }
1754
Steve Hodgson4b988282009-01-29 17:50:51 +00001755 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
1756 if (ok) {
1757 rc = efx->phy_op->init(efx);
1758 if (rc)
1759 ok = false;
Ben Hutchings115122a2009-03-04 09:52:52 +00001760 }
1761 if (!ok)
Steve Hodgson4b988282009-01-29 17:50:51 +00001762 efx->port_initialized = false;
1763 }
1764
Ben Hutchings2467ca42008-09-01 12:48:50 +01001765 if (ok) {
1766 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001767
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001768 if (efx->phy_op->set_settings(efx, ecmd))
Ben Hutchings2467ca42008-09-01 12:48:50 +01001769 EFX_ERR(efx, "could not restore PHY settings\n");
1770 }
1771
Ben Hutchingsf4150722008-11-04 20:34:28 +00001772 mutex_unlock(&efx->spi_lock);
Ben Hutchings2467ca42008-09-01 12:48:50 +01001773 mutex_unlock(&efx->mac_lock);
1774
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001775 if (ok) {
Ben Hutchings2467ca42008-09-01 12:48:50 +01001776 efx_start_all(efx);
Ben Hutchings1974cc22009-01-29 18:00:07 +00001777 efx_stats_enable(efx);
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001778 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001779 return rc;
1780}
1781
1782/* Reset the NIC as transparently as possible. Do not reset the PHY
1783 * Note that the reset may fail, in which case the card will be left
1784 * in a most-probably-unusable state.
1785 *
1786 * This function will sleep. You cannot reset from within an atomic
1787 * state; use efx_schedule_reset() instead.
1788 *
1789 * Grabs the rtnl_lock.
1790 */
1791static int efx_reset(struct efx_nic *efx)
1792{
1793 struct ethtool_cmd ecmd;
1794 enum reset_type method = efx->reset_pending;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001795 int rc = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001796
1797 /* Serialise with kernel interfaces */
1798 rtnl_lock();
1799
1800 /* If we're not RUNNING then don't reset. Leave the reset_pending
1801 * flag set so that efx_pci_probe_main will be retried */
1802 if (efx->state != STATE_RUNNING) {
1803 EFX_INFO(efx, "scheduled reset quenched. NIC not RUNNING\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001804 goto out_unlock;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001805 }
1806
Ben Hutchingsc4593022009-11-23 16:08:17 +00001807 EFX_INFO(efx, "resetting (%s)\n", RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001808
Steve Hodgson4b988282009-01-29 17:50:51 +00001809 efx_reset_down(efx, method, &ecmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001810
1811 rc = falcon_reset_hw(efx, method);
1812 if (rc) {
1813 EFX_ERR(efx, "failed to reset hardware\n");
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001814 goto out_disable;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001815 }
1816
1817 /* Allow resets to be rescheduled. */
1818 efx->reset_pending = RESET_TYPE_NONE;
1819
1820 /* Reinitialise bus-mastering, which may have been turned off before
1821 * the reset was scheduled. This is still appropriate, even in the
1822 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
1823 * can respond to requests. */
1824 pci_set_master(efx->pci_dev);
1825
Ben Hutchings8ceee662008-04-27 12:55:59 +01001826 /* Leave device stopped if necessary */
1827 if (method == RESET_TYPE_DISABLE) {
Steve Hodgson4b988282009-01-29 17:50:51 +00001828 efx_reset_up(efx, method, &ecmd, false);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001829 rc = -EIO;
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001830 } else {
Steve Hodgson4b988282009-01-29 17:50:51 +00001831 rc = efx_reset_up(efx, method, &ecmd, true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001832 }
1833
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001834out_disable:
1835 if (rc) {
1836 EFX_ERR(efx, "has been disabled\n");
1837 efx->state = STATE_DISABLED;
1838 dev_close(efx->net_dev);
1839 } else {
1840 EFX_LOG(efx, "reset complete\n");
1841 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001842
Ben Hutchingsf4bd9542008-12-26 13:48:51 -08001843out_unlock:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001844 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01001845 return rc;
1846}
1847
1848/* The worker thread exists so that code that cannot sleep can
1849 * schedule a reset for later.
1850 */
1851static void efx_reset_work(struct work_struct *data)
1852{
1853 struct efx_nic *nic = container_of(data, struct efx_nic, reset_work);
1854
1855 efx_reset(nic);
1856}
1857
1858void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
1859{
1860 enum reset_type method;
1861
1862 if (efx->reset_pending != RESET_TYPE_NONE) {
1863 EFX_INFO(efx, "quenching already scheduled reset\n");
1864 return;
1865 }
1866
1867 switch (type) {
1868 case RESET_TYPE_INVISIBLE:
1869 case RESET_TYPE_ALL:
1870 case RESET_TYPE_WORLD:
1871 case RESET_TYPE_DISABLE:
1872 method = type;
1873 break;
1874 case RESET_TYPE_RX_RECOVERY:
1875 case RESET_TYPE_RX_DESC_FETCH:
1876 case RESET_TYPE_TX_DESC_FETCH:
1877 case RESET_TYPE_TX_SKIP:
1878 method = RESET_TYPE_INVISIBLE;
1879 break;
1880 default:
1881 method = RESET_TYPE_ALL;
1882 break;
1883 }
1884
1885 if (method != type)
Ben Hutchingsc4593022009-11-23 16:08:17 +00001886 EFX_LOG(efx, "scheduling %s reset for %s\n",
1887 RESET_TYPE(method), RESET_TYPE(type));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001888 else
Ben Hutchingsc4593022009-11-23 16:08:17 +00001889 EFX_LOG(efx, "scheduling %s reset\n", RESET_TYPE(method));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001890
1891 efx->reset_pending = method;
1892
Steve Hodgson1ab00622008-12-12 21:33:02 -08001893 queue_work(reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001894}
1895
1896/**************************************************************************
1897 *
1898 * List of NICs we support
1899 *
1900 **************************************************************************/
1901
1902/* PCI device ID table */
1903static struct pci_device_id efx_pci_table[] __devinitdata = {
1904 {PCI_DEVICE(EFX_VENDID_SFC, FALCON_A_P_DEVID),
1905 .driver_data = (unsigned long) &falcon_a_nic_type},
1906 {PCI_DEVICE(EFX_VENDID_SFC, FALCON_B_P_DEVID),
1907 .driver_data = (unsigned long) &falcon_b_nic_type},
1908 {0} /* end of list */
1909};
1910
1911/**************************************************************************
1912 *
Ben Hutchings37594332009-11-23 16:05:45 +00001913 * Dummy PHY/MAC operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01001914 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01001915 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01001916 * Needed so all function pointers are valid and do not have to be tested
1917 * before use
1918 *
1919 **************************************************************************/
1920int efx_port_dummy_op_int(struct efx_nic *efx)
1921{
1922 return 0;
1923}
1924void efx_port_dummy_op_void(struct efx_nic *efx) {}
Ben Hutchings398468e2009-11-23 16:03:45 +00001925void efx_port_dummy_op_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
1926{
1927}
Ben Hutchings8ceee662008-04-27 12:55:59 +01001928
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001929static struct efx_mac_operations efx_dummy_mac_operations = {
1930 .reconfigure = efx_port_dummy_op_void,
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001931 .poll = efx_port_dummy_op_void,
1932 .irq = efx_port_dummy_op_void,
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001933};
1934
Ben Hutchings8ceee662008-04-27 12:55:59 +01001935static struct efx_phy_operations efx_dummy_phy_operations = {
1936 .init = efx_port_dummy_op_int,
1937 .reconfigure = efx_port_dummy_op_void,
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001938 .poll = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001939 .fini = efx_port_dummy_op_void,
1940 .clear_interrupt = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001941};
1942
Ben Hutchings8ceee662008-04-27 12:55:59 +01001943/**************************************************************************
1944 *
1945 * Data housekeeping
1946 *
1947 **************************************************************************/
1948
1949/* This zeroes out and then fills in the invariants in a struct
1950 * efx_nic (including all sub-structures).
1951 */
1952static int efx_init_struct(struct efx_nic *efx, struct efx_nic_type *type,
1953 struct pci_dev *pci_dev, struct net_device *net_dev)
1954{
1955 struct efx_channel *channel;
1956 struct efx_tx_queue *tx_queue;
1957 struct efx_rx_queue *rx_queue;
Steve Hodgson1ab00622008-12-12 21:33:02 -08001958 int i;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001959
1960 /* Initialise common structures */
1961 memset(efx, 0, sizeof(*efx));
1962 spin_lock_init(&efx->biu_lock);
1963 spin_lock_init(&efx->phy_lock);
Ben Hutchingsf4150722008-11-04 20:34:28 +00001964 mutex_init(&efx->spi_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001965 INIT_WORK(&efx->reset_work, efx_reset_work);
1966 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
1967 efx->pci_dev = pci_dev;
1968 efx->state = STATE_INIT;
1969 efx->reset_pending = RESET_TYPE_NONE;
1970 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
Ben Hutchings8ceee662008-04-27 12:55:59 +01001971
1972 efx->net_dev = net_dev;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001973 efx->rx_checksum_enabled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001974 spin_lock_init(&efx->netif_stop_lock);
1975 spin_lock_init(&efx->stats_lock);
Ben Hutchings1974cc22009-01-29 18:00:07 +00001976 efx->stats_disable_count = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001977 mutex_init(&efx->mac_lock);
Ben Hutchings177dfcd2008-12-12 21:50:08 -08001978 efx->mac_op = &efx_dummy_mac_operations;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001979 efx->phy_op = &efx_dummy_phy_operations;
Ben Hutchings68e7f452009-04-29 08:05:08 +00001980 efx->mdio.dev = net_dev;
Ben Hutchings766ca0f2008-12-12 21:59:24 -08001981 INIT_WORK(&efx->phy_work, efx_phy_work);
1982 INIT_WORK(&efx->mac_work, efx_mac_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001983 atomic_set(&efx->netif_stop_count, 1);
1984
1985 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
1986 channel = &efx->channel[i];
1987 channel->efx = efx;
1988 channel->channel = i;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001989 channel->work_pending = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001990 }
Ben Hutchings60ac1062008-09-01 12:44:59 +01001991 for (i = 0; i < EFX_TX_QUEUE_COUNT; i++) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001992 tx_queue = &efx->tx_queue[i];
1993 tx_queue->efx = efx;
1994 tx_queue->queue = i;
1995 tx_queue->buffer = NULL;
1996 tx_queue->channel = &efx->channel[0]; /* for safety */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001997 tx_queue->tso_headers_free = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001998 }
1999 for (i = 0; i < EFX_MAX_RX_QUEUES; i++) {
2000 rx_queue = &efx->rx_queue[i];
2001 rx_queue->efx = efx;
2002 rx_queue->queue = i;
2003 rx_queue->channel = &efx->channel[0]; /* for safety */
2004 rx_queue->buffer = NULL;
2005 spin_lock_init(&rx_queue->add_lock);
2006 INIT_DELAYED_WORK(&rx_queue->work, efx_rx_work);
2007 }
2008
2009 efx->type = type;
2010
Ben Hutchings8ceee662008-04-27 12:55:59 +01002011 /* As close as we can get to guaranteeing that we don't overflow */
Ben Hutchings3ffeabd2009-10-23 08:30:58 +00002012 BUILD_BUG_ON(EFX_EVQ_SIZE < EFX_TXQ_SIZE + EFX_RXQ_SIZE);
2013
Ben Hutchings8ceee662008-04-27 12:55:59 +01002014 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2015
2016 /* Higher numbered interrupt modes are less capable! */
2017 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2018 interrupt_mode);
2019
Ben Hutchings6977dc62008-12-26 13:44:39 -08002020 /* Would be good to use the net_dev name, but we're too early */
2021 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2022 pci_name(pci_dev));
2023 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002024 if (!efx->workqueue)
2025 return -ENOMEM;
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002026
Ben Hutchings8ceee662008-04-27 12:55:59 +01002027 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002028}
2029
2030static void efx_fini_struct(struct efx_nic *efx)
2031{
2032 if (efx->workqueue) {
2033 destroy_workqueue(efx->workqueue);
2034 efx->workqueue = NULL;
2035 }
2036}
2037
2038/**************************************************************************
2039 *
2040 * PCI interface
2041 *
2042 **************************************************************************/
2043
2044/* Main body of final NIC shutdown code
2045 * This is called only at module unload (or hotplug removal).
2046 */
2047static void efx_pci_remove_main(struct efx_nic *efx)
2048{
Ben Hutchingsf01865f2009-10-23 08:31:37 +00002049 falcon_fini_interrupt(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002050 efx_fini_channels(efx);
2051 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002052 efx_fini_napi(efx);
2053 efx_remove_all(efx);
2054}
2055
2056/* Final NIC shutdown
2057 * This is called only at module unload (or hotplug removal).
2058 */
2059static void efx_pci_remove(struct pci_dev *pci_dev)
2060{
2061 struct efx_nic *efx;
2062
2063 efx = pci_get_drvdata(pci_dev);
2064 if (!efx)
2065 return;
2066
2067 /* Mark the NIC as fini, then stop the interface */
2068 rtnl_lock();
2069 efx->state = STATE_FINI;
2070 dev_close(efx->net_dev);
2071
2072 /* Allow any queued efx_resets() to complete */
2073 rtnl_unlock();
2074
Ben Hutchings8ceee662008-04-27 12:55:59 +01002075 efx_unregister_netdev(efx);
2076
Ben Hutchings7dde5962008-12-12 22:09:38 -08002077 efx_mtd_remove(efx);
2078
Ben Hutchings8ceee662008-04-27 12:55:59 +01002079 /* Wait for any scheduled resets to complete. No more will be
2080 * scheduled from this point because efx_stop_all() has been
2081 * called, we are no longer registered with driverlink, and
2082 * the net_device's have been removed. */
Steve Hodgson1ab00622008-12-12 21:33:02 -08002083 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002084
2085 efx_pci_remove_main(efx);
2086
Ben Hutchings8ceee662008-04-27 12:55:59 +01002087 efx_fini_io(efx);
2088 EFX_LOG(efx, "shutdown successful\n");
2089
2090 pci_set_drvdata(pci_dev, NULL);
2091 efx_fini_struct(efx);
2092 free_netdev(efx->net_dev);
2093};
2094
2095/* Main body of NIC initialisation
2096 * This is called at module load (or hotplug insertion, theoretically).
2097 */
2098static int efx_pci_probe_main(struct efx_nic *efx)
2099{
2100 int rc;
2101
2102 /* Do start-of-day initialisation */
2103 rc = efx_probe_all(efx);
2104 if (rc)
2105 goto fail1;
2106
2107 rc = efx_init_napi(efx);
2108 if (rc)
2109 goto fail2;
2110
Ben Hutchings8ceee662008-04-27 12:55:59 +01002111 rc = falcon_init_nic(efx);
2112 if (rc) {
2113 EFX_ERR(efx, "failed to initialise NIC\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002114 goto fail3;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002115 }
2116
2117 rc = efx_init_port(efx);
2118 if (rc) {
2119 EFX_ERR(efx, "failed to initialise port\n");
Ben Hutchings278c0622009-11-23 16:05:12 +00002120 goto fail4;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002121 }
2122
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01002123 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002124
2125 rc = falcon_init_interrupt(efx);
2126 if (rc)
Ben Hutchings278c0622009-11-23 16:05:12 +00002127 goto fail5;
Ben Hutchings8ceee662008-04-27 12:55:59 +01002128
2129 return 0;
2130
Ben Hutchings278c0622009-11-23 16:05:12 +00002131 fail5:
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01002132 efx_fini_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002133 efx_fini_port(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002134 fail4:
2135 fail3:
2136 efx_fini_napi(efx);
2137 fail2:
2138 efx_remove_all(efx);
2139 fail1:
2140 return rc;
2141}
2142
2143/* NIC initialisation
2144 *
2145 * This is called at module load (or hotplug insertion,
2146 * theoretically). It sets up PCI mappings, tests and resets the NIC,
2147 * sets up and registers the network devices with the kernel and hooks
2148 * the interrupt service routine. It does not prepare the device for
2149 * transmission; this is left to the first time one of the network
2150 * interfaces is brought up (i.e. efx_net_open).
2151 */
2152static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2153 const struct pci_device_id *entry)
2154{
2155 struct efx_nic_type *type = (struct efx_nic_type *) entry->driver_data;
2156 struct net_device *net_dev;
2157 struct efx_nic *efx;
2158 int i, rc;
2159
2160 /* Allocate and initialise a struct net_device and struct efx_nic */
2161 net_dev = alloc_etherdev(sizeof(*efx));
2162 if (!net_dev)
2163 return -ENOMEM;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01002164 net_dev->features |= (NETIF_F_IP_CSUM | NETIF_F_SG |
Ben Hutchings97bc5412009-05-19 16:19:08 -07002165 NETIF_F_HIGHDMA | NETIF_F_TSO |
2166 NETIF_F_GRO);
Ben Hutchings285065632008-09-01 12:46:54 +01002167 /* Mask for features that also apply to VLAN devices */
2168 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchings740847d2008-09-01 12:48:23 +01002169 NETIF_F_HIGHDMA | NETIF_F_TSO);
Ben Hutchings767e4682008-09-01 12:43:14 +01002170 efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002171 pci_set_drvdata(pci_dev, efx);
2172 rc = efx_init_struct(efx, type, pci_dev, net_dev);
2173 if (rc)
2174 goto fail1;
2175
2176 EFX_INFO(efx, "Solarflare Communications NIC detected\n");
2177
2178 /* Set up basic I/O (BAR mappings etc) */
2179 rc = efx_init_io(efx);
2180 if (rc)
2181 goto fail2;
2182
2183 /* No serialisation is required with the reset path because
2184 * we're in STATE_INIT. */
2185 for (i = 0; i < 5; i++) {
2186 rc = efx_pci_probe_main(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002187
2188 /* Serialise against efx_reset(). No more resets will be
2189 * scheduled since efx_stop_all() has been called, and we
2190 * have not and never have been registered with either
2191 * the rtnetlink or driverlink layers. */
Steve Hodgson1ab00622008-12-12 21:33:02 -08002192 cancel_work_sync(&efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002193
Steve Hodgsonfa402b22008-12-12 22:08:16 -08002194 if (rc == 0) {
2195 if (efx->reset_pending != RESET_TYPE_NONE) {
2196 /* If there was a scheduled reset during
2197 * probe, the NIC is probably hosed anyway */
2198 efx_pci_remove_main(efx);
2199 rc = -EIO;
2200 } else {
2201 break;
2202 }
2203 }
2204
Ben Hutchings8ceee662008-04-27 12:55:59 +01002205 /* Retry if a recoverably reset event has been scheduled */
2206 if ((efx->reset_pending != RESET_TYPE_INVISIBLE) &&
2207 (efx->reset_pending != RESET_TYPE_ALL))
2208 goto fail3;
2209
2210 efx->reset_pending = RESET_TYPE_NONE;
2211 }
2212
2213 if (rc) {
2214 EFX_ERR(efx, "Could not reset NIC\n");
2215 goto fail4;
2216 }
2217
2218 /* Switch to the running state before we expose the device to
2219 * the OS. This is to ensure that the initial gathering of
2220 * MAC stats succeeds. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002221 efx->state = STATE_RUNNING;
Ben Hutchings7dde5962008-12-12 22:09:38 -08002222
Ben Hutchings8ceee662008-04-27 12:55:59 +01002223 rc = efx_register_netdev(efx);
2224 if (rc)
2225 goto fail5;
2226
2227 EFX_LOG(efx, "initialisation successful\n");
Ben Hutchingsa5211bb2009-10-23 08:33:09 +00002228
2229 rtnl_lock();
2230 efx_mtd_probe(efx); /* allowed to fail */
2231 rtnl_unlock();
Ben Hutchings8ceee662008-04-27 12:55:59 +01002232 return 0;
2233
2234 fail5:
2235 efx_pci_remove_main(efx);
2236 fail4:
2237 fail3:
2238 efx_fini_io(efx);
2239 fail2:
2240 efx_fini_struct(efx);
2241 fail1:
2242 EFX_LOG(efx, "initialisation failed. rc=%d\n", rc);
2243 free_netdev(net_dev);
2244 return rc;
2245}
2246
2247static struct pci_driver efx_pci_driver = {
2248 .name = EFX_DRIVER_NAME,
2249 .id_table = efx_pci_table,
2250 .probe = efx_pci_probe,
2251 .remove = efx_pci_remove,
2252};
2253
2254/**************************************************************************
2255 *
2256 * Kernel module interface
2257 *
2258 *************************************************************************/
2259
2260module_param(interrupt_mode, uint, 0444);
2261MODULE_PARM_DESC(interrupt_mode,
2262 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2263
2264static int __init efx_init_module(void)
2265{
2266 int rc;
2267
2268 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2269
2270 rc = register_netdevice_notifier(&efx_netdev_notifier);
2271 if (rc)
2272 goto err_notifier;
2273
2274 refill_workqueue = create_workqueue("sfc_refill");
2275 if (!refill_workqueue) {
2276 rc = -ENOMEM;
2277 goto err_refill;
2278 }
Steve Hodgson1ab00622008-12-12 21:33:02 -08002279 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2280 if (!reset_workqueue) {
2281 rc = -ENOMEM;
2282 goto err_reset;
2283 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01002284
2285 rc = pci_register_driver(&efx_pci_driver);
2286 if (rc < 0)
2287 goto err_pci;
2288
2289 return 0;
2290
2291 err_pci:
Steve Hodgson1ab00622008-12-12 21:33:02 -08002292 destroy_workqueue(reset_workqueue);
2293 err_reset:
Ben Hutchings8ceee662008-04-27 12:55:59 +01002294 destroy_workqueue(refill_workqueue);
2295 err_refill:
2296 unregister_netdevice_notifier(&efx_netdev_notifier);
2297 err_notifier:
2298 return rc;
2299}
2300
2301static void __exit efx_exit_module(void)
2302{
2303 printk(KERN_INFO "Solarflare NET driver unloading\n");
2304
2305 pci_unregister_driver(&efx_pci_driver);
Steve Hodgson1ab00622008-12-12 21:33:02 -08002306 destroy_workqueue(reset_workqueue);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002307 destroy_workqueue(refill_workqueue);
2308 unregister_netdevice_notifier(&efx_netdev_notifier);
2309
2310}
2311
2312module_init(efx_init_module);
2313module_exit(efx_exit_module);
2314
2315MODULE_AUTHOR("Michael Brown <mbrown@fensystems.co.uk> and "
2316 "Solarflare Communications");
2317MODULE_DESCRIPTION("Solarflare Communications network driver");
2318MODULE_LICENSE("GPL");
2319MODULE_DEVICE_TABLE(pci, efx_pci_table);