blob: f34dbf2c5b69376dbbe752de27c9b68888eb556b [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"
24#include "gmii.h"
25#include "ethtool.h"
26#include "tx.h"
27#include "rx.h"
28#include "efx.h"
29#include "mdio_10g.h"
30#include "falcon.h"
Ben Hutchings8ceee662008-04-27 12:55:59 +010031#include "mac.h"
32
33#define EFX_MAX_MTU (9 * 1024)
34
35/* RX slow fill workqueue. If memory allocation fails in the fast path,
36 * a work item is pushed onto this work queue to retry the allocation later,
37 * to avoid the NIC being starved of RX buffers. Since this is a per cpu
38 * workqueue, there is nothing to be gained in making it per NIC
39 */
40static struct workqueue_struct *refill_workqueue;
41
42/**************************************************************************
43 *
44 * Configurable values
45 *
46 *************************************************************************/
47
48/*
49 * Enable large receive offload (LRO) aka soft segment reassembly (SSR)
50 *
51 * This sets the default for new devices. It can be controlled later
52 * using ethtool.
53 */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010054static int lro = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +010055module_param(lro, int, 0644);
56MODULE_PARM_DESC(lro, "Large receive offload acceleration");
57
58/*
59 * Use separate channels for TX and RX events
60 *
61 * Set this to 1 to use separate channels for TX and RX. It allows us to
62 * apply a higher level of interrupt moderation to TX events.
63 *
64 * This is forced to 0 for MSI interrupt mode as the interrupt vector
65 * is not written
66 */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010067static unsigned int separate_tx_and_rx_channels = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +010068
69/* This is the weight assigned to each of the (per-channel) virtual
70 * NAPI devices.
71 */
72static int napi_weight = 64;
73
74/* This is the time (in jiffies) between invocations of the hardware
75 * monitor, which checks for known hardware bugs and resets the
76 * hardware and driver as necessary.
77 */
78unsigned int efx_monitor_interval = 1 * HZ;
79
80/* This controls whether or not the hardware monitor will trigger a
81 * reset when it detects an error condition.
82 */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +010083static unsigned int monitor_reset = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +010084
85/* This controls whether or not the driver will initialise devices
86 * with invalid MAC addresses stored in the EEPROM or flash. If true,
87 * such devices will be initialised with a random locally-generated
88 * MAC address. This allows for loading the sfc_mtd driver to
89 * reprogram the flash, even if the flash contents (including the MAC
90 * address) have previously been erased.
91 */
92static unsigned int allow_bad_hwaddr;
93
94/* Initial interrupt moderation settings. They can be modified after
95 * module load with ethtool.
96 *
97 * The default for RX should strike a balance between increasing the
98 * round-trip latency and reducing overhead.
99 */
100static unsigned int rx_irq_mod_usec = 60;
101
102/* Initial interrupt moderation settings. They can be modified after
103 * module load with ethtool.
104 *
105 * This default is chosen to ensure that a 10G link does not go idle
106 * while a TX queue is stopped after it has become full. A queue is
107 * restarted when it drops below half full. The time this takes (assuming
108 * worst case 3 descriptors per packet and 1024 descriptors) is
109 * 512 / 3 * 1.2 = 205 usec.
110 */
111static unsigned int tx_irq_mod_usec = 150;
112
113/* This is the first interrupt mode to try out of:
114 * 0 => MSI-X
115 * 1 => MSI
116 * 2 => legacy
117 */
118static unsigned int interrupt_mode;
119
120/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
121 * i.e. the number of CPUs among which we may distribute simultaneous
122 * interrupt handling.
123 *
124 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
125 * The default (0) means to assign an interrupt to each package (level II cache)
126 */
127static unsigned int rss_cpus;
128module_param(rss_cpus, uint, 0444);
129MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
130
131/**************************************************************************
132 *
133 * Utility functions and prototypes
134 *
135 *************************************************************************/
136static void efx_remove_channel(struct efx_channel *channel);
137static void efx_remove_port(struct efx_nic *efx);
138static void efx_fini_napi(struct efx_nic *efx);
139static void efx_fini_channels(struct efx_nic *efx);
140
141#define EFX_ASSERT_RESET_SERIALISED(efx) \
142 do { \
143 if ((efx->state == STATE_RUNNING) || \
144 (efx->state == STATE_RESETTING)) \
145 ASSERT_RTNL(); \
146 } while (0)
147
148/**************************************************************************
149 *
150 * Event queue processing
151 *
152 *************************************************************************/
153
154/* Process channel's event queue
155 *
156 * This function is responsible for processing the event queue of a
157 * single channel. The caller must guarantee that this function will
158 * never be concurrently called more than once on the same channel,
159 * though different channels may be being processed concurrently.
160 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100161static int efx_process_channel(struct efx_channel *channel, int rx_quota)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100162{
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100163 struct efx_nic *efx = channel->efx;
164 int rx_packets;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100165
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100166 if (unlikely(efx->reset_pending != RESET_TYPE_NONE ||
Ben Hutchings8ceee662008-04-27 12:55:59 +0100167 !channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100168 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100169
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100170 rx_packets = falcon_process_eventq(channel, rx_quota);
171 if (rx_packets == 0)
172 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100173
174 /* Deliver last RX packet. */
175 if (channel->rx_pkt) {
176 __efx_rx_packet(channel, channel->rx_pkt,
177 channel->rx_pkt_csummed);
178 channel->rx_pkt = NULL;
179 }
180
181 efx_flush_lro(channel);
182 efx_rx_strategy(channel);
183
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100184 efx_fast_push_rx_descriptors(&efx->rx_queue[channel->channel]);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100185
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100186 return rx_packets;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100187}
188
189/* Mark channel as finished processing
190 *
191 * Note that since we will not receive further interrupts for this
192 * channel before we finish processing and call the eventq_read_ack()
193 * method, there is no need to use the interrupt hold-off timers.
194 */
195static inline void efx_channel_processed(struct efx_channel *channel)
196{
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100197 /* The interrupt handler for this channel may set work_pending
198 * as soon as we acknowledge the events we've seen. Make sure
199 * it's cleared before then. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100200 channel->work_pending = false;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100201 smp_wmb();
202
Ben Hutchings8ceee662008-04-27 12:55:59 +0100203 falcon_eventq_read_ack(channel);
204}
205
206/* NAPI poll handler
207 *
208 * NAPI guarantees serialisation of polls of the same device, which
209 * provides the guarantee required by efx_process_channel().
210 */
211static int efx_poll(struct napi_struct *napi, int budget)
212{
213 struct efx_channel *channel =
214 container_of(napi, struct efx_channel, napi_str);
215 struct net_device *napi_dev = channel->napi_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100216 int rx_packets;
217
218 EFX_TRACE(channel->efx, "channel %d NAPI poll executing on CPU %d\n",
219 channel->channel, raw_smp_processor_id());
220
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100221 rx_packets = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100222
223 if (rx_packets < budget) {
224 /* There is no race here; although napi_disable() will
225 * only wait for netif_rx_complete(), this isn't a problem
226 * since efx_channel_processed() will have no effect if
227 * interrupts have already been disabled.
228 */
229 netif_rx_complete(napi_dev, napi);
230 efx_channel_processed(channel);
231 }
232
233 return rx_packets;
234}
235
236/* Process the eventq of the specified channel immediately on this CPU
237 *
238 * Disable hardware generated interrupts, wait for any existing
239 * processing to finish, then directly poll (and ack ) the eventq.
240 * Finally reenable NAPI and interrupts.
241 *
242 * Since we are touching interrupts the caller should hold the suspend lock
243 */
244void efx_process_channel_now(struct efx_channel *channel)
245{
246 struct efx_nic *efx = channel->efx;
247
248 BUG_ON(!channel->used_flags);
249 BUG_ON(!channel->enabled);
250
251 /* Disable interrupts and wait for ISRs to complete */
252 falcon_disable_interrupts(efx);
253 if (efx->legacy_irq)
254 synchronize_irq(efx->legacy_irq);
Ben Hutchings64ee3122008-09-01 12:47:38 +0100255 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100256 synchronize_irq(channel->irq);
257
258 /* Wait for any NAPI processing to complete */
259 napi_disable(&channel->napi_str);
260
261 /* Poll the channel */
Ben Hutchings91ad7572008-05-16 21:14:27 +0100262 efx_process_channel(channel, efx->type->evq_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100263
264 /* Ack the eventq. This may cause an interrupt to be generated
265 * when they are reenabled */
266 efx_channel_processed(channel);
267
268 napi_enable(&channel->napi_str);
269 falcon_enable_interrupts(efx);
270}
271
272/* Create event queue
273 * Event queue memory allocations are done only once. If the channel
274 * is reset, the memory buffer will be reused; this guards against
275 * errors during channel reset and also simplifies interrupt handling.
276 */
277static int efx_probe_eventq(struct efx_channel *channel)
278{
279 EFX_LOG(channel->efx, "chan %d create event queue\n", channel->channel);
280
281 return falcon_probe_eventq(channel);
282}
283
284/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100285static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100286{
287 EFX_LOG(channel->efx, "chan %d init event queue\n", channel->channel);
288
289 channel->eventq_read_ptr = 0;
290
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100291 falcon_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100292}
293
294static void efx_fini_eventq(struct efx_channel *channel)
295{
296 EFX_LOG(channel->efx, "chan %d fini event queue\n", channel->channel);
297
298 falcon_fini_eventq(channel);
299}
300
301static void efx_remove_eventq(struct efx_channel *channel)
302{
303 EFX_LOG(channel->efx, "chan %d remove event queue\n", channel->channel);
304
305 falcon_remove_eventq(channel);
306}
307
308/**************************************************************************
309 *
310 * Channel handling
311 *
312 *************************************************************************/
313
Ben Hutchings8ceee662008-04-27 12:55:59 +0100314static int efx_probe_channel(struct efx_channel *channel)
315{
316 struct efx_tx_queue *tx_queue;
317 struct efx_rx_queue *rx_queue;
318 int rc;
319
320 EFX_LOG(channel->efx, "creating channel %d\n", channel->channel);
321
322 rc = efx_probe_eventq(channel);
323 if (rc)
324 goto fail1;
325
326 efx_for_each_channel_tx_queue(tx_queue, channel) {
327 rc = efx_probe_tx_queue(tx_queue);
328 if (rc)
329 goto fail2;
330 }
331
332 efx_for_each_channel_rx_queue(rx_queue, channel) {
333 rc = efx_probe_rx_queue(rx_queue);
334 if (rc)
335 goto fail3;
336 }
337
338 channel->n_rx_frm_trunc = 0;
339
340 return 0;
341
342 fail3:
343 efx_for_each_channel_rx_queue(rx_queue, channel)
344 efx_remove_rx_queue(rx_queue);
345 fail2:
346 efx_for_each_channel_tx_queue(tx_queue, channel)
347 efx_remove_tx_queue(tx_queue);
348 fail1:
349 return rc;
350}
351
352
353/* Channels are shutdown and reinitialised whilst the NIC is running
354 * to propagate configuration changes (mtu, checksum offload), or
355 * to clear hardware error conditions
356 */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100357static void efx_init_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100358{
359 struct efx_tx_queue *tx_queue;
360 struct efx_rx_queue *rx_queue;
361 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100362
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100363 /* Calculate the rx buffer allocation parameters required to
364 * support the current MTU, including padding for header
365 * alignment and overruns.
366 */
367 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
368 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
369 efx->type->rx_buffer_padding);
370 efx->rx_buffer_order = get_order(efx->rx_buffer_len);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100371
372 /* Initialise the channels */
373 efx_for_each_channel(channel, efx) {
374 EFX_LOG(channel->efx, "init chan %d\n", channel->channel);
375
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100376 efx_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100377
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100378 efx_for_each_channel_tx_queue(tx_queue, channel)
379 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100380
381 /* The rx buffer allocation strategy is MTU dependent */
382 efx_rx_strategy(channel);
383
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100384 efx_for_each_channel_rx_queue(rx_queue, channel)
385 efx_init_rx_queue(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100386
387 WARN_ON(channel->rx_pkt != NULL);
388 efx_rx_strategy(channel);
389 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100390}
391
392/* This enables event queue processing and packet transmission.
393 *
394 * Note that this function is not allowed to fail, since that would
395 * introduce too much complexity into the suspend/resume path.
396 */
397static void efx_start_channel(struct efx_channel *channel)
398{
399 struct efx_rx_queue *rx_queue;
400
401 EFX_LOG(channel->efx, "starting chan %d\n", channel->channel);
402
403 if (!(channel->efx->net_dev->flags & IFF_UP))
404 netif_napi_add(channel->napi_dev, &channel->napi_str,
405 efx_poll, napi_weight);
406
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100407 /* The interrupt handler for this channel may set work_pending
408 * as soon as we enable it. Make sure it's cleared before
409 * then. Similarly, make sure it sees the enabled flag set. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100410 channel->work_pending = false;
411 channel->enabled = true;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100412 smp_wmb();
Ben Hutchings8ceee662008-04-27 12:55:59 +0100413
414 napi_enable(&channel->napi_str);
415
416 /* Load up RX descriptors */
417 efx_for_each_channel_rx_queue(rx_queue, channel)
418 efx_fast_push_rx_descriptors(rx_queue);
419}
420
421/* This disables event queue processing and packet transmission.
422 * This function does not guarantee that all queue processing
423 * (e.g. RX refill) is complete.
424 */
425static void efx_stop_channel(struct efx_channel *channel)
426{
427 struct efx_rx_queue *rx_queue;
428
429 if (!channel->enabled)
430 return;
431
432 EFX_LOG(channel->efx, "stop chan %d\n", channel->channel);
433
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100434 channel->enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100435 napi_disable(&channel->napi_str);
436
437 /* Ensure that any worker threads have exited or will be no-ops */
438 efx_for_each_channel_rx_queue(rx_queue, channel) {
439 spin_lock_bh(&rx_queue->add_lock);
440 spin_unlock_bh(&rx_queue->add_lock);
441 }
442}
443
444static void efx_fini_channels(struct efx_nic *efx)
445{
446 struct efx_channel *channel;
447 struct efx_tx_queue *tx_queue;
448 struct efx_rx_queue *rx_queue;
449
450 EFX_ASSERT_RESET_SERIALISED(efx);
451 BUG_ON(efx->port_enabled);
452
453 efx_for_each_channel(channel, efx) {
454 EFX_LOG(channel->efx, "shut down chan %d\n", channel->channel);
455
456 efx_for_each_channel_rx_queue(rx_queue, channel)
457 efx_fini_rx_queue(rx_queue);
458 efx_for_each_channel_tx_queue(tx_queue, channel)
459 efx_fini_tx_queue(tx_queue);
460 }
461
462 /* Do the event queues last so that we can handle flush events
463 * for all DMA queues. */
464 efx_for_each_channel(channel, efx) {
465 EFX_LOG(channel->efx, "shut down evq %d\n", channel->channel);
466
467 efx_fini_eventq(channel);
468 }
469}
470
471static void efx_remove_channel(struct efx_channel *channel)
472{
473 struct efx_tx_queue *tx_queue;
474 struct efx_rx_queue *rx_queue;
475
476 EFX_LOG(channel->efx, "destroy chan %d\n", channel->channel);
477
478 efx_for_each_channel_rx_queue(rx_queue, channel)
479 efx_remove_rx_queue(rx_queue);
480 efx_for_each_channel_tx_queue(tx_queue, channel)
481 efx_remove_tx_queue(tx_queue);
482 efx_remove_eventq(channel);
483
484 channel->used_flags = 0;
485}
486
487void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue, int delay)
488{
489 queue_delayed_work(refill_workqueue, &rx_queue->work, delay);
490}
491
492/**************************************************************************
493 *
494 * Port handling
495 *
496 **************************************************************************/
497
498/* This ensures that the kernel is kept informed (via
499 * netif_carrier_on/off) of the link status, and also maintains the
500 * link status's stop on the port's TX queue.
501 */
502static void efx_link_status_changed(struct efx_nic *efx)
503{
Ben Hutchings8ceee662008-04-27 12:55:59 +0100504 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
505 * that no events are triggered between unregister_netdev() and the
506 * driver unloading. A more general condition is that NETDEV_CHANGE
507 * can only be generated between NETDEV_UP and NETDEV_DOWN */
508 if (!netif_running(efx->net_dev))
509 return;
510
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100511 if (efx->link_up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100512 efx->n_link_state_changes++;
513
514 if (efx->link_up)
515 netif_carrier_on(efx->net_dev);
516 else
517 netif_carrier_off(efx->net_dev);
518 }
519
520 /* Status message for kernel log */
521 if (efx->link_up) {
522 struct mii_if_info *gmii = &efx->mii;
523 unsigned adv, lpa;
524 /* NONE here means direct XAUI from the controller, with no
525 * MDIO-attached device we can query. */
526 if (efx->phy_type != PHY_TYPE_NONE) {
527 adv = gmii_advertised(gmii);
528 lpa = gmii_lpa(gmii);
529 } else {
530 lpa = GM_LPA_10000 | LPA_DUPLEX;
531 adv = lpa;
532 }
533 EFX_INFO(efx, "link up at %dMbps %s-duplex "
534 "(adv %04x lpa %04x) (MTU %d)%s\n",
535 (efx->link_options & GM_LPA_10000 ? 10000 :
536 (efx->link_options & GM_LPA_1000 ? 1000 :
537 (efx->link_options & GM_LPA_100 ? 100 :
538 10))),
539 (efx->link_options & GM_LPA_DUPLEX ?
540 "full" : "half"),
541 adv, lpa,
542 efx->net_dev->mtu,
543 (efx->promiscuous ? " [PROMISC]" : ""));
544 } else {
545 EFX_INFO(efx, "link down\n");
546 }
547
548}
549
550/* This call reinitialises the MAC to pick up new PHY settings. The
551 * caller must hold the mac_lock */
552static void __efx_reconfigure_port(struct efx_nic *efx)
553{
554 WARN_ON(!mutex_is_locked(&efx->mac_lock));
555
556 EFX_LOG(efx, "reconfiguring MAC from PHY settings on CPU %d\n",
557 raw_smp_processor_id());
558
559 falcon_reconfigure_xmac(efx);
560
561 /* Inform kernel of loss/gain of carrier */
562 efx_link_status_changed(efx);
563}
564
565/* Reinitialise the MAC to pick up new PHY settings, even if the port is
566 * disabled. */
567void efx_reconfigure_port(struct efx_nic *efx)
568{
569 EFX_ASSERT_RESET_SERIALISED(efx);
570
571 mutex_lock(&efx->mac_lock);
572 __efx_reconfigure_port(efx);
573 mutex_unlock(&efx->mac_lock);
574}
575
576/* Asynchronous efx_reconfigure_port work item. To speed up efx_flush_all()
577 * we don't efx_reconfigure_port() if the port is disabled. Care is taken
578 * in efx_stop_all() and efx_start_port() to prevent PHY events being lost */
579static void efx_reconfigure_work(struct work_struct *data)
580{
581 struct efx_nic *efx = container_of(data, struct efx_nic,
582 reconfigure_work);
583
584 mutex_lock(&efx->mac_lock);
585 if (efx->port_enabled)
586 __efx_reconfigure_port(efx);
587 mutex_unlock(&efx->mac_lock);
588}
589
590static int efx_probe_port(struct efx_nic *efx)
591{
592 int rc;
593
594 EFX_LOG(efx, "create port\n");
595
596 /* Connect up MAC/PHY operations table and read MAC address */
597 rc = falcon_probe_port(efx);
598 if (rc)
599 goto err;
600
601 /* Sanity check MAC address */
602 if (is_valid_ether_addr(efx->mac_address)) {
603 memcpy(efx->net_dev->dev_addr, efx->mac_address, ETH_ALEN);
604 } else {
605 DECLARE_MAC_BUF(mac);
606
607 EFX_ERR(efx, "invalid MAC address %s\n",
608 print_mac(mac, efx->mac_address));
609 if (!allow_bad_hwaddr) {
610 rc = -EINVAL;
611 goto err;
612 }
613 random_ether_addr(efx->net_dev->dev_addr);
614 EFX_INFO(efx, "using locally-generated MAC %s\n",
615 print_mac(mac, efx->net_dev->dev_addr));
616 }
617
618 return 0;
619
620 err:
621 efx_remove_port(efx);
622 return rc;
623}
624
625static int efx_init_port(struct efx_nic *efx)
626{
627 int rc;
628
629 EFX_LOG(efx, "init port\n");
630
631 /* Initialise the MAC and PHY */
632 rc = falcon_init_xmac(efx);
633 if (rc)
634 return rc;
635
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100636 efx->port_initialized = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100637
638 /* Reconfigure port to program MAC registers */
639 falcon_reconfigure_xmac(efx);
640
641 return 0;
642}
643
644/* Allow efx_reconfigure_port() to be scheduled, and close the window
645 * between efx_stop_port and efx_flush_all whereby a previously scheduled
646 * efx_reconfigure_port() may have been cancelled */
647static void efx_start_port(struct efx_nic *efx)
648{
649 EFX_LOG(efx, "start port\n");
650 BUG_ON(efx->port_enabled);
651
652 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100653 efx->port_enabled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100654 __efx_reconfigure_port(efx);
655 mutex_unlock(&efx->mac_lock);
656}
657
658/* Prevent efx_reconfigure_work and efx_monitor() from executing, and
659 * efx_set_multicast_list() from scheduling efx_reconfigure_work.
660 * efx_reconfigure_work can still be scheduled via NAPI processing
661 * until efx_flush_all() is called */
662static void efx_stop_port(struct efx_nic *efx)
663{
664 EFX_LOG(efx, "stop port\n");
665
666 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100667 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100668 mutex_unlock(&efx->mac_lock);
669
670 /* Serialise against efx_set_multicast_list() */
Ben Hutchings55668612008-05-16 21:16:10 +0100671 if (efx_dev_registered(efx)) {
David S. Millerb9e40852008-07-15 00:15:08 -0700672 netif_addr_lock_bh(efx->net_dev);
673 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100674 }
675}
676
677static void efx_fini_port(struct efx_nic *efx)
678{
679 EFX_LOG(efx, "shut down port\n");
680
681 if (!efx->port_initialized)
682 return;
683
684 falcon_fini_xmac(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100685 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100686
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100687 efx->link_up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100688 efx_link_status_changed(efx);
689}
690
691static void efx_remove_port(struct efx_nic *efx)
692{
693 EFX_LOG(efx, "destroying port\n");
694
695 falcon_remove_port(efx);
696}
697
698/**************************************************************************
699 *
700 * NIC handling
701 *
702 **************************************************************************/
703
704/* This configures the PCI device to enable I/O and DMA. */
705static int efx_init_io(struct efx_nic *efx)
706{
707 struct pci_dev *pci_dev = efx->pci_dev;
708 dma_addr_t dma_mask = efx->type->max_dma_mask;
709 int rc;
710
711 EFX_LOG(efx, "initialising I/O\n");
712
713 rc = pci_enable_device(pci_dev);
714 if (rc) {
715 EFX_ERR(efx, "failed to enable PCI device\n");
716 goto fail1;
717 }
718
719 pci_set_master(pci_dev);
720
721 /* Set the PCI DMA mask. Try all possibilities from our
722 * genuine mask down to 32 bits, because some architectures
723 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
724 * masks event though they reject 46 bit masks.
725 */
726 while (dma_mask > 0x7fffffffUL) {
727 if (pci_dma_supported(pci_dev, dma_mask) &&
728 ((rc = pci_set_dma_mask(pci_dev, dma_mask)) == 0))
729 break;
730 dma_mask >>= 1;
731 }
732 if (rc) {
733 EFX_ERR(efx, "could not find a suitable DMA mask\n");
734 goto fail2;
735 }
736 EFX_LOG(efx, "using DMA mask %llx\n", (unsigned long long) dma_mask);
737 rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
738 if (rc) {
739 /* pci_set_consistent_dma_mask() is not *allowed* to
740 * fail with a mask that pci_set_dma_mask() accepted,
741 * but just in case...
742 */
743 EFX_ERR(efx, "failed to set consistent DMA mask\n");
744 goto fail2;
745 }
746
747 efx->membase_phys = pci_resource_start(efx->pci_dev,
748 efx->type->mem_bar);
749 rc = pci_request_region(pci_dev, efx->type->mem_bar, "sfc");
750 if (rc) {
751 EFX_ERR(efx, "request for memory BAR failed\n");
752 rc = -EIO;
753 goto fail3;
754 }
755 efx->membase = ioremap_nocache(efx->membase_phys,
756 efx->type->mem_map_size);
757 if (!efx->membase) {
Ben Hutchings086ea352008-05-16 21:17:06 +0100758 EFX_ERR(efx, "could not map memory BAR %d at %llx+%x\n",
759 efx->type->mem_bar,
760 (unsigned long long)efx->membase_phys,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100761 efx->type->mem_map_size);
762 rc = -ENOMEM;
763 goto fail4;
764 }
Ben Hutchings086ea352008-05-16 21:17:06 +0100765 EFX_LOG(efx, "memory BAR %u at %llx+%x (virtual %p)\n",
766 efx->type->mem_bar, (unsigned long long)efx->membase_phys,
767 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100768
769 return 0;
770
771 fail4:
772 release_mem_region(efx->membase_phys, efx->type->mem_map_size);
773 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +0100774 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100775 fail2:
776 pci_disable_device(efx->pci_dev);
777 fail1:
778 return rc;
779}
780
781static void efx_fini_io(struct efx_nic *efx)
782{
783 EFX_LOG(efx, "shutting down I/O\n");
784
785 if (efx->membase) {
786 iounmap(efx->membase);
787 efx->membase = NULL;
788 }
789
790 if (efx->membase_phys) {
791 pci_release_region(efx->pci_dev, efx->type->mem_bar);
Ben Hutchings2c118e02008-05-16 21:15:29 +0100792 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100793 }
794
795 pci_disable_device(efx->pci_dev);
796}
797
Ben Hutchings46123d02008-09-01 12:47:33 +0100798/* Get number of RX queues wanted. Return number of online CPU
799 * packages in the expectation that an IRQ balancer will spread
800 * interrupts across them. */
801static int efx_wanted_rx_queues(void)
802{
803 cpumask_t core_mask;
804 int count;
805 int cpu;
806
807 cpus_clear(core_mask);
808 count = 0;
809 for_each_online_cpu(cpu) {
810 if (!cpu_isset(cpu, core_mask)) {
811 ++count;
812 cpus_or(core_mask, core_mask,
813 topology_core_siblings(cpu));
814 }
815 }
816
817 return count;
818}
819
820/* Probe the number and type of interrupts we are able to obtain, and
821 * the resulting numbers of channels and RX queues.
822 */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100823static void efx_probe_interrupts(struct efx_nic *efx)
824{
Ben Hutchings46123d02008-09-01 12:47:33 +0100825 int max_channels =
826 min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100827 int rc, i;
828
829 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +0100830 struct msix_entry xentries[EFX_MAX_CHANNELS];
831 int wanted_ints;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100832
Ben Hutchings46123d02008-09-01 12:47:33 +0100833 /* We want one RX queue and interrupt per CPU package
834 * (or as specified by the rss_cpus module parameter).
835 * We will need one channel per interrupt.
836 */
837 wanted_ints = rss_cpus ? rss_cpus : efx_wanted_rx_queues();
Ben Hutchings8831da72008-09-01 12:47:48 +0100838 efx->n_rx_queues = min(wanted_ints, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +0100839
Ben Hutchings8831da72008-09-01 12:47:48 +0100840 for (i = 0; i < efx->n_rx_queues; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100841 xentries[i].entry = i;
Ben Hutchings8831da72008-09-01 12:47:48 +0100842 rc = pci_enable_msix(efx->pci_dev, xentries, efx->n_rx_queues);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100843 if (rc > 0) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100844 EFX_BUG_ON_PARANOID(rc >= efx->n_rx_queues);
845 efx->n_rx_queues = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100846 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchings8831da72008-09-01 12:47:48 +0100847 efx->n_rx_queues);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100848 }
849
850 if (rc == 0) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100851 for (i = 0; i < efx->n_rx_queues; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100852 efx->channel[i].irq = xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100853 } else {
854 /* Fall back to single channel MSI */
855 efx->interrupt_mode = EFX_INT_MODE_MSI;
856 EFX_ERR(efx, "could not enable MSI-X\n");
857 }
858 }
859
860 /* Try single interrupt MSI */
861 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100862 efx->n_rx_queues = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100863 rc = pci_enable_msi(efx->pci_dev);
864 if (rc == 0) {
865 efx->channel[0].irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100866 } else {
867 EFX_ERR(efx, "could not enable MSI\n");
868 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
869 }
870 }
871
872 /* Assume legacy interrupts */
873 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100874 efx->n_rx_queues = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100875 efx->legacy_irq = efx->pci_dev->irq;
876 }
877}
878
879static void efx_remove_interrupts(struct efx_nic *efx)
880{
881 struct efx_channel *channel;
882
883 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100884 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100885 channel->irq = 0;
886 pci_disable_msi(efx->pci_dev);
887 pci_disable_msix(efx->pci_dev);
888
889 /* Remove legacy interrupt */
890 efx->legacy_irq = 0;
891}
892
Ben Hutchings8831da72008-09-01 12:47:48 +0100893static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100894{
895 struct efx_tx_queue *tx_queue;
896 struct efx_rx_queue *rx_queue;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100897
Ben Hutchings60ac1062008-09-01 12:44:59 +0100898 efx_for_each_tx_queue(tx_queue, efx) {
899 if (!EFX_INT_MODE_USE_MSI(efx) && separate_tx_and_rx_channels)
900 tx_queue->channel = &efx->channel[1];
901 else
902 tx_queue->channel = &efx->channel[0];
903 tx_queue->channel->used_flags |= EFX_USED_BY_TX;
904 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100905
Ben Hutchings8831da72008-09-01 12:47:48 +0100906 efx_for_each_rx_queue(rx_queue, efx) {
907 rx_queue->channel = &efx->channel[rx_queue->queue];
908 rx_queue->channel->used_flags |= EFX_USED_BY_RX;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100909 }
910}
911
912static int efx_probe_nic(struct efx_nic *efx)
913{
914 int rc;
915
916 EFX_LOG(efx, "creating NIC\n");
917
918 /* Carry out hardware-type specific initialisation */
919 rc = falcon_probe_nic(efx);
920 if (rc)
921 return rc;
922
923 /* Determine the number of channels and RX queues by trying to hook
924 * in MSI-X interrupts. */
925 efx_probe_interrupts(efx);
926
Ben Hutchings8831da72008-09-01 12:47:48 +0100927 efx_set_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100928
929 /* Initialise the interrupt moderation settings */
930 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec);
931
932 return 0;
933}
934
935static void efx_remove_nic(struct efx_nic *efx)
936{
937 EFX_LOG(efx, "destroying NIC\n");
938
939 efx_remove_interrupts(efx);
940 falcon_remove_nic(efx);
941}
942
943/**************************************************************************
944 *
945 * NIC startup/shutdown
946 *
947 *************************************************************************/
948
949static int efx_probe_all(struct efx_nic *efx)
950{
951 struct efx_channel *channel;
952 int rc;
953
954 /* Create NIC */
955 rc = efx_probe_nic(efx);
956 if (rc) {
957 EFX_ERR(efx, "failed to create NIC\n");
958 goto fail1;
959 }
960
961 /* Create port */
962 rc = efx_probe_port(efx);
963 if (rc) {
964 EFX_ERR(efx, "failed to create port\n");
965 goto fail2;
966 }
967
968 /* Create channels */
969 efx_for_each_channel(channel, efx) {
970 rc = efx_probe_channel(channel);
971 if (rc) {
972 EFX_ERR(efx, "failed to create channel %d\n",
973 channel->channel);
974 goto fail3;
975 }
976 }
977
978 return 0;
979
980 fail3:
981 efx_for_each_channel(channel, efx)
982 efx_remove_channel(channel);
983 efx_remove_port(efx);
984 fail2:
985 efx_remove_nic(efx);
986 fail1:
987 return rc;
988}
989
990/* Called after previous invocation(s) of efx_stop_all, restarts the
991 * port, kernel transmit queue, NAPI processing and hardware interrupts,
992 * and ensures that the port is scheduled to be reconfigured.
993 * This function is safe to call multiple times when the NIC is in any
994 * state. */
995static void efx_start_all(struct efx_nic *efx)
996{
997 struct efx_channel *channel;
998
999 EFX_ASSERT_RESET_SERIALISED(efx);
1000
1001 /* Check that it is appropriate to restart the interface. All
1002 * of these flags are safe to read under just the rtnl lock */
1003 if (efx->port_enabled)
1004 return;
1005 if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1006 return;
Ben Hutchings55668612008-05-16 21:16:10 +01001007 if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001008 return;
1009
1010 /* Mark the port as enabled so port reconfigurations can start, then
1011 * restart the transmit interface early so the watchdog timer stops */
1012 efx_start_port(efx);
Steve Hodgsondacccc72008-09-01 12:48:20 +01001013 if (efx_dev_registered(efx))
1014 efx_wake_queue(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001015
1016 efx_for_each_channel(channel, efx)
1017 efx_start_channel(channel);
1018
1019 falcon_enable_interrupts(efx);
1020
1021 /* Start hardware monitor if we're in RUNNING */
1022 if (efx->state == STATE_RUNNING)
1023 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1024 efx_monitor_interval);
1025}
1026
1027/* Flush all delayed work. Should only be called when no more delayed work
1028 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1029 * since we're holding the rtnl_lock at this point. */
1030static void efx_flush_all(struct efx_nic *efx)
1031{
1032 struct efx_rx_queue *rx_queue;
1033
1034 /* Make sure the hardware monitor is stopped */
1035 cancel_delayed_work_sync(&efx->monitor_work);
1036
1037 /* Ensure that all RX slow refills are complete. */
Ben Hutchingsb3475642008-05-16 21:15:49 +01001038 efx_for_each_rx_queue(rx_queue, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001039 cancel_delayed_work_sync(&rx_queue->work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001040
1041 /* Stop scheduled port reconfigurations */
1042 cancel_work_sync(&efx->reconfigure_work);
1043
1044}
1045
1046/* Quiesce hardware and software without bringing the link down.
1047 * Safe to call multiple times, when the nic and interface is in any
1048 * state. The caller is guaranteed to subsequently be in a position
1049 * to modify any hardware and software state they see fit without
1050 * taking locks. */
1051static void efx_stop_all(struct efx_nic *efx)
1052{
1053 struct efx_channel *channel;
1054
1055 EFX_ASSERT_RESET_SERIALISED(efx);
1056
1057 /* port_enabled can be read safely under the rtnl lock */
1058 if (!efx->port_enabled)
1059 return;
1060
1061 /* Disable interrupts and wait for ISR to complete */
1062 falcon_disable_interrupts(efx);
1063 if (efx->legacy_irq)
1064 synchronize_irq(efx->legacy_irq);
Ben Hutchings64ee3122008-09-01 12:47:38 +01001065 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001066 if (channel->irq)
1067 synchronize_irq(channel->irq);
Ben Hutchingsb3475642008-05-16 21:15:49 +01001068 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001069
1070 /* Stop all NAPI processing and synchronous rx refills */
1071 efx_for_each_channel(channel, efx)
1072 efx_stop_channel(channel);
1073
1074 /* Stop all asynchronous port reconfigurations. Since all
1075 * event processing has already been stopped, there is no
1076 * window to loose phy events */
1077 efx_stop_port(efx);
1078
1079 /* Flush reconfigure_work, refill_workqueue, monitor_work */
1080 efx_flush_all(efx);
1081
1082 /* Isolate the MAC from the TX and RX engines, so that queue
1083 * flushes will complete in a timely fashion. */
1084 falcon_deconfigure_mac_wrapper(efx);
1085 falcon_drain_tx_fifo(efx);
1086
1087 /* Stop the kernel transmit interface late, so the watchdog
1088 * timer isn't ticking over the flush */
Ben Hutchings55668612008-05-16 21:16:10 +01001089 if (efx_dev_registered(efx)) {
Steve Hodgsondacccc72008-09-01 12:48:20 +01001090 efx_stop_queue(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001091 netif_tx_lock_bh(efx->net_dev);
1092 netif_tx_unlock_bh(efx->net_dev);
1093 }
1094}
1095
1096static void efx_remove_all(struct efx_nic *efx)
1097{
1098 struct efx_channel *channel;
1099
1100 efx_for_each_channel(channel, efx)
1101 efx_remove_channel(channel);
1102 efx_remove_port(efx);
1103 efx_remove_nic(efx);
1104}
1105
1106/* A convinience function to safely flush all the queues */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001107void efx_flush_queues(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001108{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001109 EFX_ASSERT_RESET_SERIALISED(efx);
1110
1111 efx_stop_all(efx);
1112
1113 efx_fini_channels(efx);
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001114 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001115
1116 efx_start_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001117}
1118
1119/**************************************************************************
1120 *
1121 * Interrupt moderation
1122 *
1123 **************************************************************************/
1124
1125/* Set interrupt moderation parameters */
1126void efx_init_irq_moderation(struct efx_nic *efx, int tx_usecs, int rx_usecs)
1127{
1128 struct efx_tx_queue *tx_queue;
1129 struct efx_rx_queue *rx_queue;
1130
1131 EFX_ASSERT_RESET_SERIALISED(efx);
1132
1133 efx_for_each_tx_queue(tx_queue, efx)
1134 tx_queue->channel->irq_moderation = tx_usecs;
1135
1136 efx_for_each_rx_queue(rx_queue, efx)
1137 rx_queue->channel->irq_moderation = rx_usecs;
1138}
1139
1140/**************************************************************************
1141 *
1142 * Hardware monitor
1143 *
1144 **************************************************************************/
1145
1146/* Run periodically off the general workqueue. Serialised against
1147 * efx_reconfigure_port via the mac_lock */
1148static void efx_monitor(struct work_struct *data)
1149{
1150 struct efx_nic *efx = container_of(data, struct efx_nic,
1151 monitor_work.work);
1152 int rc = 0;
1153
1154 EFX_TRACE(efx, "hardware monitor executing on CPU %d\n",
1155 raw_smp_processor_id());
1156
1157
1158 /* If the mac_lock is already held then it is likely a port
1159 * reconfiguration is already in place, which will likely do
1160 * most of the work of check_hw() anyway. */
1161 if (!mutex_trylock(&efx->mac_lock)) {
1162 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1163 efx_monitor_interval);
1164 return;
1165 }
1166
1167 if (efx->port_enabled)
1168 rc = falcon_check_xmac(efx);
1169 mutex_unlock(&efx->mac_lock);
1170
1171 if (rc) {
1172 if (monitor_reset) {
1173 EFX_ERR(efx, "hardware monitor detected a fault: "
1174 "triggering reset\n");
1175 efx_schedule_reset(efx, RESET_TYPE_MONITOR);
1176 } else {
1177 EFX_ERR(efx, "hardware monitor detected a fault, "
1178 "skipping reset\n");
1179 }
1180 }
1181
1182 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1183 efx_monitor_interval);
1184}
1185
1186/**************************************************************************
1187 *
1188 * ioctls
1189 *
1190 *************************************************************************/
1191
1192/* Net device ioctl
1193 * Context: process, rtnl_lock() held.
1194 */
1195static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1196{
Ben Hutchings767e4682008-09-01 12:43:14 +01001197 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001198
1199 EFX_ASSERT_RESET_SERIALISED(efx);
1200
1201 return generic_mii_ioctl(&efx->mii, if_mii(ifr), cmd, NULL);
1202}
1203
1204/**************************************************************************
1205 *
1206 * NAPI interface
1207 *
1208 **************************************************************************/
1209
1210static int efx_init_napi(struct efx_nic *efx)
1211{
1212 struct efx_channel *channel;
1213 int rc;
1214
1215 efx_for_each_channel(channel, efx) {
1216 channel->napi_dev = efx->net_dev;
1217 rc = efx_lro_init(&channel->lro_mgr, efx);
1218 if (rc)
1219 goto err;
1220 }
1221 return 0;
1222 err:
1223 efx_fini_napi(efx);
1224 return rc;
1225}
1226
1227static void efx_fini_napi(struct efx_nic *efx)
1228{
1229 struct efx_channel *channel;
1230
1231 efx_for_each_channel(channel, efx) {
1232 efx_lro_fini(&channel->lro_mgr);
1233 channel->napi_dev = NULL;
1234 }
1235}
1236
1237/**************************************************************************
1238 *
1239 * Kernel netpoll interface
1240 *
1241 *************************************************************************/
1242
1243#ifdef CONFIG_NET_POLL_CONTROLLER
1244
1245/* Although in the common case interrupts will be disabled, this is not
1246 * guaranteed. However, all our work happens inside the NAPI callback,
1247 * so no locking is required.
1248 */
1249static void efx_netpoll(struct net_device *net_dev)
1250{
Ben Hutchings767e4682008-09-01 12:43:14 +01001251 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001252 struct efx_channel *channel;
1253
Ben Hutchings64ee3122008-09-01 12:47:38 +01001254 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001255 efx_schedule_channel(channel);
1256}
1257
1258#endif
1259
1260/**************************************************************************
1261 *
1262 * Kernel net device interface
1263 *
1264 *************************************************************************/
1265
1266/* Context: process, rtnl_lock() held. */
1267static int efx_net_open(struct net_device *net_dev)
1268{
Ben Hutchings767e4682008-09-01 12:43:14 +01001269 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001270 EFX_ASSERT_RESET_SERIALISED(efx);
1271
1272 EFX_LOG(efx, "opening device %s on CPU %d\n", net_dev->name,
1273 raw_smp_processor_id());
1274
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001275 if (efx->phy_mode & PHY_MODE_SPECIAL)
1276 return -EBUSY;
1277
Ben Hutchings8ceee662008-04-27 12:55:59 +01001278 efx_start_all(efx);
1279 return 0;
1280}
1281
1282/* Context: process, rtnl_lock() held.
1283 * Note that the kernel will ignore our return code; this method
1284 * should really be a void.
1285 */
1286static int efx_net_stop(struct net_device *net_dev)
1287{
Ben Hutchings767e4682008-09-01 12:43:14 +01001288 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001289
1290 EFX_LOG(efx, "closing %s on CPU %d\n", net_dev->name,
1291 raw_smp_processor_id());
1292
1293 /* Stop the device and flush all the channels */
1294 efx_stop_all(efx);
1295 efx_fini_channels(efx);
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001296 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001297
1298 return 0;
1299}
1300
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001301/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001302static struct net_device_stats *efx_net_stats(struct net_device *net_dev)
1303{
Ben Hutchings767e4682008-09-01 12:43:14 +01001304 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001305 struct efx_mac_stats *mac_stats = &efx->mac_stats;
1306 struct net_device_stats *stats = &net_dev->stats;
1307
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001308 /* Update stats if possible, but do not wait if another thread
1309 * is updating them (or resetting the NIC); slightly stale
1310 * stats are acceptable.
1311 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001312 if (!spin_trylock(&efx->stats_lock))
1313 return stats;
1314 if (efx->state == STATE_RUNNING) {
1315 falcon_update_stats_xmac(efx);
1316 falcon_update_nic_stats(efx);
1317 }
1318 spin_unlock(&efx->stats_lock);
1319
1320 stats->rx_packets = mac_stats->rx_packets;
1321 stats->tx_packets = mac_stats->tx_packets;
1322 stats->rx_bytes = mac_stats->rx_bytes;
1323 stats->tx_bytes = mac_stats->tx_bytes;
1324 stats->multicast = mac_stats->rx_multicast;
1325 stats->collisions = mac_stats->tx_collision;
1326 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1327 mac_stats->rx_length_error);
1328 stats->rx_over_errors = efx->n_rx_nodesc_drop_cnt;
1329 stats->rx_crc_errors = mac_stats->rx_bad;
1330 stats->rx_frame_errors = mac_stats->rx_align_error;
1331 stats->rx_fifo_errors = mac_stats->rx_overflow;
1332 stats->rx_missed_errors = mac_stats->rx_missed;
1333 stats->tx_window_errors = mac_stats->tx_late_collision;
1334
1335 stats->rx_errors = (stats->rx_length_errors +
1336 stats->rx_over_errors +
1337 stats->rx_crc_errors +
1338 stats->rx_frame_errors +
1339 stats->rx_fifo_errors +
1340 stats->rx_missed_errors +
1341 mac_stats->rx_symbol_error);
1342 stats->tx_errors = (stats->tx_window_errors +
1343 mac_stats->tx_bad);
1344
1345 return stats;
1346}
1347
1348/* Context: netif_tx_lock held, BHs disabled. */
1349static void efx_watchdog(struct net_device *net_dev)
1350{
Ben Hutchings767e4682008-09-01 12:43:14 +01001351 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001352
1353 EFX_ERR(efx, "TX stuck with stop_count=%d port_enabled=%d: %s\n",
1354 atomic_read(&efx->netif_stop_count), efx->port_enabled,
1355 monitor_reset ? "resetting channels" : "skipping reset");
1356
1357 if (monitor_reset)
1358 efx_schedule_reset(efx, RESET_TYPE_MONITOR);
1359}
1360
1361
1362/* Context: process, rtnl_lock() held. */
1363static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1364{
Ben Hutchings767e4682008-09-01 12:43:14 +01001365 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001366 int rc = 0;
1367
1368 EFX_ASSERT_RESET_SERIALISED(efx);
1369
1370 if (new_mtu > EFX_MAX_MTU)
1371 return -EINVAL;
1372
1373 efx_stop_all(efx);
1374
1375 EFX_LOG(efx, "changing MTU to %d\n", new_mtu);
1376
1377 efx_fini_channels(efx);
1378 net_dev->mtu = new_mtu;
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001379 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001380
1381 efx_start_all(efx);
1382 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001383}
1384
1385static int efx_set_mac_address(struct net_device *net_dev, void *data)
1386{
Ben Hutchings767e4682008-09-01 12:43:14 +01001387 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001388 struct sockaddr *addr = data;
1389 char *new_addr = addr->sa_data;
1390
1391 EFX_ASSERT_RESET_SERIALISED(efx);
1392
1393 if (!is_valid_ether_addr(new_addr)) {
1394 DECLARE_MAC_BUF(mac);
1395 EFX_ERR(efx, "invalid ethernet MAC address requested: %s\n",
1396 print_mac(mac, new_addr));
1397 return -EINVAL;
1398 }
1399
1400 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1401
1402 /* Reconfigure the MAC */
1403 efx_reconfigure_port(efx);
1404
1405 return 0;
1406}
1407
1408/* Context: netif_tx_lock held, BHs disabled. */
1409static void efx_set_multicast_list(struct net_device *net_dev)
1410{
Ben Hutchings767e4682008-09-01 12:43:14 +01001411 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001412 struct dev_mc_list *mc_list = net_dev->mc_list;
1413 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001414 bool promiscuous;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001415 u32 crc;
1416 int bit;
1417 int i;
1418
1419 /* Set per-MAC promiscuity flag and reconfigure MAC if necessary */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001420 promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001421 if (efx->promiscuous != promiscuous) {
1422 efx->promiscuous = promiscuous;
1423 /* Close the window between efx_stop_port() and efx_flush_all()
1424 * by only queuing work when the port is enabled. */
1425 if (efx->port_enabled)
1426 queue_work(efx->workqueue, &efx->reconfigure_work);
1427 }
1428
1429 /* Build multicast hash table */
1430 if (promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
1431 memset(mc_hash, 0xff, sizeof(*mc_hash));
1432 } else {
1433 memset(mc_hash, 0x00, sizeof(*mc_hash));
1434 for (i = 0; i < net_dev->mc_count; i++) {
1435 crc = ether_crc_le(ETH_ALEN, mc_list->dmi_addr);
1436 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1437 set_bit_le(bit, mc_hash->byte);
1438 mc_list = mc_list->next;
1439 }
1440 }
1441
1442 /* Create and activate new global multicast hash table */
1443 falcon_set_multicast_hash(efx);
1444}
1445
1446static int efx_netdev_event(struct notifier_block *this,
1447 unsigned long event, void *ptr)
1448{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001449 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001450
1451 if (net_dev->open == efx_net_open && event == NETDEV_CHANGENAME) {
Ben Hutchings767e4682008-09-01 12:43:14 +01001452 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001453
1454 strcpy(efx->name, net_dev->name);
1455 }
1456
1457 return NOTIFY_DONE;
1458}
1459
1460static struct notifier_block efx_netdev_notifier = {
1461 .notifier_call = efx_netdev_event,
1462};
1463
1464static int efx_register_netdev(struct efx_nic *efx)
1465{
1466 struct net_device *net_dev = efx->net_dev;
1467 int rc;
1468
1469 net_dev->watchdog_timeo = 5 * HZ;
1470 net_dev->irq = efx->pci_dev->irq;
1471 net_dev->open = efx_net_open;
1472 net_dev->stop = efx_net_stop;
1473 net_dev->get_stats = efx_net_stats;
1474 net_dev->tx_timeout = &efx_watchdog;
1475 net_dev->hard_start_xmit = efx_hard_start_xmit;
1476 net_dev->do_ioctl = efx_ioctl;
1477 net_dev->change_mtu = efx_change_mtu;
1478 net_dev->set_mac_address = efx_set_mac_address;
1479 net_dev->set_multicast_list = efx_set_multicast_list;
1480#ifdef CONFIG_NET_POLL_CONTROLLER
1481 net_dev->poll_controller = efx_netpoll;
1482#endif
1483 SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
1484 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
1485
1486 /* Always start with carrier off; PHY events will detect the link */
1487 netif_carrier_off(efx->net_dev);
1488
1489 /* Clear MAC statistics */
1490 falcon_update_stats_xmac(efx);
1491 memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
1492
1493 rc = register_netdev(net_dev);
1494 if (rc) {
1495 EFX_ERR(efx, "could not register net dev\n");
1496 return rc;
1497 }
1498 strcpy(efx->name, net_dev->name);
1499
1500 return 0;
1501}
1502
1503static void efx_unregister_netdev(struct efx_nic *efx)
1504{
1505 struct efx_tx_queue *tx_queue;
1506
1507 if (!efx->net_dev)
1508 return;
1509
Ben Hutchings767e4682008-09-01 12:43:14 +01001510 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001511
1512 /* Free up any skbs still remaining. This has to happen before
1513 * we try to unregister the netdev as running their destructors
1514 * may be needed to get the device ref. count to 0. */
1515 efx_for_each_tx_queue(tx_queue, efx)
1516 efx_release_tx_buffers(tx_queue);
1517
Ben Hutchings55668612008-05-16 21:16:10 +01001518 if (efx_dev_registered(efx)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001519 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
1520 unregister_netdev(efx->net_dev);
1521 }
1522}
1523
1524/**************************************************************************
1525 *
1526 * Device reset and suspend
1527 *
1528 **************************************************************************/
1529
Ben Hutchings2467ca42008-09-01 12:48:50 +01001530/* Tears down the entire software state and most of the hardware state
1531 * before reset. */
1532static void efx_reset_down(struct efx_nic *efx, struct ethtool_cmd *ecmd)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001533{
1534 int rc;
1535
1536 EFX_ASSERT_RESET_SERIALISED(efx);
1537
Ben Hutchings2467ca42008-09-01 12:48:50 +01001538 /* The net_dev->get_stats handler is quite slow, and will fail
1539 * if a fetch is pending over reset. Serialise against it. */
1540 spin_lock(&efx->stats_lock);
1541 spin_unlock(&efx->stats_lock);
1542
1543 efx_stop_all(efx);
1544 mutex_lock(&efx->mac_lock);
1545
Ben Hutchings8ceee662008-04-27 12:55:59 +01001546 rc = falcon_xmac_get_settings(efx, ecmd);
Ben Hutchings2467ca42008-09-01 12:48:50 +01001547 if (rc)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001548 EFX_ERR(efx, "could not back up PHY settings\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001549
1550 efx_fini_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001551}
1552
Ben Hutchings2467ca42008-09-01 12:48:50 +01001553/* This function will always ensure that the locks acquired in
1554 * efx_reset_down() are released. A failure return code indicates
1555 * that we were unable to reinitialise the hardware, and the
1556 * driver should be disabled. If ok is false, then the rx and tx
1557 * engines are not restarted, pending a RESET_DISABLE. */
1558static int efx_reset_up(struct efx_nic *efx, struct ethtool_cmd *ecmd,
1559 bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001560{
1561 int rc;
1562
Ben Hutchings2467ca42008-09-01 12:48:50 +01001563 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001564
Ben Hutchings2467ca42008-09-01 12:48:50 +01001565 rc = falcon_init_nic(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001566 if (rc) {
Ben Hutchings2467ca42008-09-01 12:48:50 +01001567 EFX_ERR(efx, "failed to initialise NIC\n");
1568 ok = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001569 }
1570
Ben Hutchings2467ca42008-09-01 12:48:50 +01001571 if (ok) {
1572 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001573
Ben Hutchings2467ca42008-09-01 12:48:50 +01001574 if (falcon_xmac_set_settings(efx, ecmd))
1575 EFX_ERR(efx, "could not restore PHY settings\n");
1576 }
1577
1578 mutex_unlock(&efx->mac_lock);
1579
1580 if (ok)
1581 efx_start_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001582 return rc;
1583}
1584
1585/* Reset the NIC as transparently as possible. Do not reset the PHY
1586 * Note that the reset may fail, in which case the card will be left
1587 * in a most-probably-unusable state.
1588 *
1589 * This function will sleep. You cannot reset from within an atomic
1590 * state; use efx_schedule_reset() instead.
1591 *
1592 * Grabs the rtnl_lock.
1593 */
1594static int efx_reset(struct efx_nic *efx)
1595{
1596 struct ethtool_cmd ecmd;
1597 enum reset_type method = efx->reset_pending;
1598 int rc;
1599
1600 /* Serialise with kernel interfaces */
1601 rtnl_lock();
1602
1603 /* If we're not RUNNING then don't reset. Leave the reset_pending
1604 * flag set so that efx_pci_probe_main will be retried */
1605 if (efx->state != STATE_RUNNING) {
1606 EFX_INFO(efx, "scheduled reset quenched. NIC not RUNNING\n");
1607 goto unlock_rtnl;
1608 }
1609
1610 efx->state = STATE_RESETTING;
1611 EFX_INFO(efx, "resetting (%d)\n", method);
1612
Ben Hutchings2467ca42008-09-01 12:48:50 +01001613 efx_reset_down(efx, &ecmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001614
1615 rc = falcon_reset_hw(efx, method);
1616 if (rc) {
1617 EFX_ERR(efx, "failed to reset hardware\n");
Ben Hutchings2467ca42008-09-01 12:48:50 +01001618 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001619 }
1620
1621 /* Allow resets to be rescheduled. */
1622 efx->reset_pending = RESET_TYPE_NONE;
1623
1624 /* Reinitialise bus-mastering, which may have been turned off before
1625 * the reset was scheduled. This is still appropriate, even in the
1626 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
1627 * can respond to requests. */
1628 pci_set_master(efx->pci_dev);
1629
Ben Hutchings8ceee662008-04-27 12:55:59 +01001630 /* Leave device stopped if necessary */
1631 if (method == RESET_TYPE_DISABLE) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001632 rc = -EIO;
Ben Hutchings2467ca42008-09-01 12:48:50 +01001633 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001634 }
1635
Ben Hutchings2467ca42008-09-01 12:48:50 +01001636 rc = efx_reset_up(efx, &ecmd, true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001637 if (rc)
Ben Hutchings2467ca42008-09-01 12:48:50 +01001638 goto disable;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001639
Ben Hutchings8ceee662008-04-27 12:55:59 +01001640 EFX_LOG(efx, "reset complete\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001641 efx->state = STATE_RUNNING;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001642 unlock_rtnl:
1643 rtnl_unlock();
1644 return 0;
1645
Ben Hutchings2467ca42008-09-01 12:48:50 +01001646 fail:
1647 efx_reset_up(efx, &ecmd, false);
1648 disable:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001649 EFX_ERR(efx, "has been disabled\n");
1650 efx->state = STATE_DISABLED;
1651
Ben Hutchings8ceee662008-04-27 12:55:59 +01001652 rtnl_unlock();
1653 efx_unregister_netdev(efx);
1654 efx_fini_port(efx);
1655 return rc;
1656}
1657
1658/* The worker thread exists so that code that cannot sleep can
1659 * schedule a reset for later.
1660 */
1661static void efx_reset_work(struct work_struct *data)
1662{
1663 struct efx_nic *nic = container_of(data, struct efx_nic, reset_work);
1664
1665 efx_reset(nic);
1666}
1667
1668void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
1669{
1670 enum reset_type method;
1671
1672 if (efx->reset_pending != RESET_TYPE_NONE) {
1673 EFX_INFO(efx, "quenching already scheduled reset\n");
1674 return;
1675 }
1676
1677 switch (type) {
1678 case RESET_TYPE_INVISIBLE:
1679 case RESET_TYPE_ALL:
1680 case RESET_TYPE_WORLD:
1681 case RESET_TYPE_DISABLE:
1682 method = type;
1683 break;
1684 case RESET_TYPE_RX_RECOVERY:
1685 case RESET_TYPE_RX_DESC_FETCH:
1686 case RESET_TYPE_TX_DESC_FETCH:
1687 case RESET_TYPE_TX_SKIP:
1688 method = RESET_TYPE_INVISIBLE;
1689 break;
1690 default:
1691 method = RESET_TYPE_ALL;
1692 break;
1693 }
1694
1695 if (method != type)
1696 EFX_LOG(efx, "scheduling reset (%d:%d)\n", type, method);
1697 else
1698 EFX_LOG(efx, "scheduling reset (%d)\n", method);
1699
1700 efx->reset_pending = method;
1701
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001702 queue_work(efx->reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001703}
1704
1705/**************************************************************************
1706 *
1707 * List of NICs we support
1708 *
1709 **************************************************************************/
1710
1711/* PCI device ID table */
1712static struct pci_device_id efx_pci_table[] __devinitdata = {
1713 {PCI_DEVICE(EFX_VENDID_SFC, FALCON_A_P_DEVID),
1714 .driver_data = (unsigned long) &falcon_a_nic_type},
1715 {PCI_DEVICE(EFX_VENDID_SFC, FALCON_B_P_DEVID),
1716 .driver_data = (unsigned long) &falcon_b_nic_type},
1717 {0} /* end of list */
1718};
1719
1720/**************************************************************************
1721 *
1722 * Dummy PHY/MAC/Board operations
1723 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01001724 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01001725 * Needed so all function pointers are valid and do not have to be tested
1726 * before use
1727 *
1728 **************************************************************************/
1729int efx_port_dummy_op_int(struct efx_nic *efx)
1730{
1731 return 0;
1732}
1733void efx_port_dummy_op_void(struct efx_nic *efx) {}
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001734void efx_port_dummy_op_blink(struct efx_nic *efx, bool blink) {}
Ben Hutchings8ceee662008-04-27 12:55:59 +01001735
1736static struct efx_phy_operations efx_dummy_phy_operations = {
1737 .init = efx_port_dummy_op_int,
1738 .reconfigure = efx_port_dummy_op_void,
1739 .check_hw = efx_port_dummy_op_int,
1740 .fini = efx_port_dummy_op_void,
1741 .clear_interrupt = efx_port_dummy_op_void,
1742 .reset_xaui = efx_port_dummy_op_void,
1743};
1744
Ben Hutchings8ceee662008-04-27 12:55:59 +01001745static struct efx_board efx_dummy_board_info = {
Ben Hutchings01aad7b2008-09-01 12:48:36 +01001746 .init = efx_port_dummy_op_int,
1747 .init_leds = efx_port_dummy_op_int,
1748 .set_fault_led = efx_port_dummy_op_blink,
1749 .blink = efx_port_dummy_op_blink,
1750 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001751};
1752
1753/**************************************************************************
1754 *
1755 * Data housekeeping
1756 *
1757 **************************************************************************/
1758
1759/* This zeroes out and then fills in the invariants in a struct
1760 * efx_nic (including all sub-structures).
1761 */
1762static int efx_init_struct(struct efx_nic *efx, struct efx_nic_type *type,
1763 struct pci_dev *pci_dev, struct net_device *net_dev)
1764{
1765 struct efx_channel *channel;
1766 struct efx_tx_queue *tx_queue;
1767 struct efx_rx_queue *rx_queue;
1768 int i, rc;
1769
1770 /* Initialise common structures */
1771 memset(efx, 0, sizeof(*efx));
1772 spin_lock_init(&efx->biu_lock);
1773 spin_lock_init(&efx->phy_lock);
1774 INIT_WORK(&efx->reset_work, efx_reset_work);
1775 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
1776 efx->pci_dev = pci_dev;
1777 efx->state = STATE_INIT;
1778 efx->reset_pending = RESET_TYPE_NONE;
1779 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
1780 efx->board_info = efx_dummy_board_info;
1781
1782 efx->net_dev = net_dev;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001783 efx->rx_checksum_enabled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001784 spin_lock_init(&efx->netif_stop_lock);
1785 spin_lock_init(&efx->stats_lock);
1786 mutex_init(&efx->mac_lock);
1787 efx->phy_op = &efx_dummy_phy_operations;
1788 efx->mii.dev = net_dev;
1789 INIT_WORK(&efx->reconfigure_work, efx_reconfigure_work);
1790 atomic_set(&efx->netif_stop_count, 1);
1791
1792 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
1793 channel = &efx->channel[i];
1794 channel->efx = efx;
1795 channel->channel = i;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001796 channel->work_pending = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001797 }
Ben Hutchings60ac1062008-09-01 12:44:59 +01001798 for (i = 0; i < EFX_TX_QUEUE_COUNT; i++) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001799 tx_queue = &efx->tx_queue[i];
1800 tx_queue->efx = efx;
1801 tx_queue->queue = i;
1802 tx_queue->buffer = NULL;
1803 tx_queue->channel = &efx->channel[0]; /* for safety */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001804 tx_queue->tso_headers_free = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001805 }
1806 for (i = 0; i < EFX_MAX_RX_QUEUES; i++) {
1807 rx_queue = &efx->rx_queue[i];
1808 rx_queue->efx = efx;
1809 rx_queue->queue = i;
1810 rx_queue->channel = &efx->channel[0]; /* for safety */
1811 rx_queue->buffer = NULL;
1812 spin_lock_init(&rx_queue->add_lock);
1813 INIT_DELAYED_WORK(&rx_queue->work, efx_rx_work);
1814 }
1815
1816 efx->type = type;
1817
1818 /* Sanity-check NIC type */
1819 EFX_BUG_ON_PARANOID(efx->type->txd_ring_mask &
1820 (efx->type->txd_ring_mask + 1));
1821 EFX_BUG_ON_PARANOID(efx->type->rxd_ring_mask &
1822 (efx->type->rxd_ring_mask + 1));
1823 EFX_BUG_ON_PARANOID(efx->type->evq_size &
1824 (efx->type->evq_size - 1));
1825 /* As close as we can get to guaranteeing that we don't overflow */
1826 EFX_BUG_ON_PARANOID(efx->type->evq_size <
1827 (efx->type->txd_ring_mask + 1 +
1828 efx->type->rxd_ring_mask + 1));
1829 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
1830
1831 /* Higher numbered interrupt modes are less capable! */
1832 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
1833 interrupt_mode);
1834
1835 efx->workqueue = create_singlethread_workqueue("sfc_work");
1836 if (!efx->workqueue) {
1837 rc = -ENOMEM;
1838 goto fail1;
1839 }
1840
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001841 efx->reset_workqueue = create_singlethread_workqueue("sfc_reset");
1842 if (!efx->reset_workqueue) {
1843 rc = -ENOMEM;
1844 goto fail2;
1845 }
1846
Ben Hutchings8ceee662008-04-27 12:55:59 +01001847 return 0;
1848
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001849 fail2:
1850 destroy_workqueue(efx->workqueue);
1851 efx->workqueue = NULL;
1852
Ben Hutchings8ceee662008-04-27 12:55:59 +01001853 fail1:
1854 return rc;
1855}
1856
1857static void efx_fini_struct(struct efx_nic *efx)
1858{
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001859 if (efx->reset_workqueue) {
1860 destroy_workqueue(efx->reset_workqueue);
1861 efx->reset_workqueue = NULL;
1862 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001863 if (efx->workqueue) {
1864 destroy_workqueue(efx->workqueue);
1865 efx->workqueue = NULL;
1866 }
1867}
1868
1869/**************************************************************************
1870 *
1871 * PCI interface
1872 *
1873 **************************************************************************/
1874
1875/* Main body of final NIC shutdown code
1876 * This is called only at module unload (or hotplug removal).
1877 */
1878static void efx_pci_remove_main(struct efx_nic *efx)
1879{
1880 EFX_ASSERT_RESET_SERIALISED(efx);
1881
1882 /* Skip everything if we never obtained a valid membase */
1883 if (!efx->membase)
1884 return;
1885
1886 efx_fini_channels(efx);
1887 efx_fini_port(efx);
1888
1889 /* Shutdown the board, then the NIC and board state */
Ben Hutchings37b5a602008-05-30 22:27:04 +01001890 efx->board_info.fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001891 falcon_fini_interrupt(efx);
1892
1893 efx_fini_napi(efx);
1894 efx_remove_all(efx);
1895}
1896
1897/* Final NIC shutdown
1898 * This is called only at module unload (or hotplug removal).
1899 */
1900static void efx_pci_remove(struct pci_dev *pci_dev)
1901{
1902 struct efx_nic *efx;
1903
1904 efx = pci_get_drvdata(pci_dev);
1905 if (!efx)
1906 return;
1907
1908 /* Mark the NIC as fini, then stop the interface */
1909 rtnl_lock();
1910 efx->state = STATE_FINI;
1911 dev_close(efx->net_dev);
1912
1913 /* Allow any queued efx_resets() to complete */
1914 rtnl_unlock();
1915
1916 if (efx->membase == NULL)
1917 goto out;
1918
1919 efx_unregister_netdev(efx);
1920
1921 /* Wait for any scheduled resets to complete. No more will be
1922 * scheduled from this point because efx_stop_all() has been
1923 * called, we are no longer registered with driverlink, and
1924 * the net_device's have been removed. */
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001925 flush_workqueue(efx->reset_workqueue);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001926
1927 efx_pci_remove_main(efx);
1928
1929out:
1930 efx_fini_io(efx);
1931 EFX_LOG(efx, "shutdown successful\n");
1932
1933 pci_set_drvdata(pci_dev, NULL);
1934 efx_fini_struct(efx);
1935 free_netdev(efx->net_dev);
1936};
1937
1938/* Main body of NIC initialisation
1939 * This is called at module load (or hotplug insertion, theoretically).
1940 */
1941static int efx_pci_probe_main(struct efx_nic *efx)
1942{
1943 int rc;
1944
1945 /* Do start-of-day initialisation */
1946 rc = efx_probe_all(efx);
1947 if (rc)
1948 goto fail1;
1949
1950 rc = efx_init_napi(efx);
1951 if (rc)
1952 goto fail2;
1953
1954 /* Initialise the board */
1955 rc = efx->board_info.init(efx);
1956 if (rc) {
1957 EFX_ERR(efx, "failed to initialise board\n");
1958 goto fail3;
1959 }
1960
1961 rc = falcon_init_nic(efx);
1962 if (rc) {
1963 EFX_ERR(efx, "failed to initialise NIC\n");
1964 goto fail4;
1965 }
1966
1967 rc = efx_init_port(efx);
1968 if (rc) {
1969 EFX_ERR(efx, "failed to initialise port\n");
1970 goto fail5;
1971 }
1972
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001973 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001974
1975 rc = falcon_init_interrupt(efx);
1976 if (rc)
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001977 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001978
1979 return 0;
1980
Ben Hutchings8ceee662008-04-27 12:55:59 +01001981 fail6:
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001982 efx_fini_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001983 efx_fini_port(efx);
1984 fail5:
1985 fail4:
1986 fail3:
1987 efx_fini_napi(efx);
1988 fail2:
1989 efx_remove_all(efx);
1990 fail1:
1991 return rc;
1992}
1993
1994/* NIC initialisation
1995 *
1996 * This is called at module load (or hotplug insertion,
1997 * theoretically). It sets up PCI mappings, tests and resets the NIC,
1998 * sets up and registers the network devices with the kernel and hooks
1999 * the interrupt service routine. It does not prepare the device for
2000 * transmission; this is left to the first time one of the network
2001 * interfaces is brought up (i.e. efx_net_open).
2002 */
2003static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2004 const struct pci_device_id *entry)
2005{
2006 struct efx_nic_type *type = (struct efx_nic_type *) entry->driver_data;
2007 struct net_device *net_dev;
2008 struct efx_nic *efx;
2009 int i, rc;
2010
2011 /* Allocate and initialise a struct net_device and struct efx_nic */
2012 net_dev = alloc_etherdev(sizeof(*efx));
2013 if (!net_dev)
2014 return -ENOMEM;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01002015 net_dev->features |= (NETIF_F_IP_CSUM | NETIF_F_SG |
2016 NETIF_F_HIGHDMA | NETIF_F_TSO);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002017 if (lro)
2018 net_dev->features |= NETIF_F_LRO;
Ben Hutchings28506562008-09-01 12:46:54 +01002019 /* Mask for features that also apply to VLAN devices */
2020 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchings740847d2008-09-01 12:48:23 +01002021 NETIF_F_HIGHDMA | NETIF_F_TSO);
Ben Hutchings767e4682008-09-01 12:43:14 +01002022 efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002023 pci_set_drvdata(pci_dev, efx);
2024 rc = efx_init_struct(efx, type, pci_dev, net_dev);
2025 if (rc)
2026 goto fail1;
2027
2028 EFX_INFO(efx, "Solarflare Communications NIC detected\n");
2029
2030 /* Set up basic I/O (BAR mappings etc) */
2031 rc = efx_init_io(efx);
2032 if (rc)
2033 goto fail2;
2034
2035 /* No serialisation is required with the reset path because
2036 * we're in STATE_INIT. */
2037 for (i = 0; i < 5; i++) {
2038 rc = efx_pci_probe_main(efx);
2039 if (rc == 0)
2040 break;
2041
2042 /* Serialise against efx_reset(). No more resets will be
2043 * scheduled since efx_stop_all() has been called, and we
2044 * have not and never have been registered with either
2045 * the rtnetlink or driverlink layers. */
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002046 flush_workqueue(efx->reset_workqueue);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002047
2048 /* Retry if a recoverably reset event has been scheduled */
2049 if ((efx->reset_pending != RESET_TYPE_INVISIBLE) &&
2050 (efx->reset_pending != RESET_TYPE_ALL))
2051 goto fail3;
2052
2053 efx->reset_pending = RESET_TYPE_NONE;
2054 }
2055
2056 if (rc) {
2057 EFX_ERR(efx, "Could not reset NIC\n");
2058 goto fail4;
2059 }
2060
2061 /* Switch to the running state before we expose the device to
2062 * the OS. This is to ensure that the initial gathering of
2063 * MAC stats succeeds. */
2064 rtnl_lock();
2065 efx->state = STATE_RUNNING;
2066 rtnl_unlock();
2067
2068 rc = efx_register_netdev(efx);
2069 if (rc)
2070 goto fail5;
2071
2072 EFX_LOG(efx, "initialisation successful\n");
2073
2074 return 0;
2075
2076 fail5:
2077 efx_pci_remove_main(efx);
2078 fail4:
2079 fail3:
2080 efx_fini_io(efx);
2081 fail2:
2082 efx_fini_struct(efx);
2083 fail1:
2084 EFX_LOG(efx, "initialisation failed. rc=%d\n", rc);
2085 free_netdev(net_dev);
2086 return rc;
2087}
2088
2089static struct pci_driver efx_pci_driver = {
2090 .name = EFX_DRIVER_NAME,
2091 .id_table = efx_pci_table,
2092 .probe = efx_pci_probe,
2093 .remove = efx_pci_remove,
2094};
2095
2096/**************************************************************************
2097 *
2098 * Kernel module interface
2099 *
2100 *************************************************************************/
2101
2102module_param(interrupt_mode, uint, 0444);
2103MODULE_PARM_DESC(interrupt_mode,
2104 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2105
2106static int __init efx_init_module(void)
2107{
2108 int rc;
2109
2110 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2111
2112 rc = register_netdevice_notifier(&efx_netdev_notifier);
2113 if (rc)
2114 goto err_notifier;
2115
2116 refill_workqueue = create_workqueue("sfc_refill");
2117 if (!refill_workqueue) {
2118 rc = -ENOMEM;
2119 goto err_refill;
2120 }
2121
2122 rc = pci_register_driver(&efx_pci_driver);
2123 if (rc < 0)
2124 goto err_pci;
2125
2126 return 0;
2127
2128 err_pci:
2129 destroy_workqueue(refill_workqueue);
2130 err_refill:
2131 unregister_netdevice_notifier(&efx_netdev_notifier);
2132 err_notifier:
2133 return rc;
2134}
2135
2136static void __exit efx_exit_module(void)
2137{
2138 printk(KERN_INFO "Solarflare NET driver unloading\n");
2139
2140 pci_unregister_driver(&efx_pci_driver);
2141 destroy_workqueue(refill_workqueue);
2142 unregister_netdevice_notifier(&efx_netdev_notifier);
2143
2144}
2145
2146module_init(efx_init_module);
2147module_exit(efx_exit_module);
2148
2149MODULE_AUTHOR("Michael Brown <mbrown@fensystems.co.uk> and "
2150 "Solarflare Communications");
2151MODULE_DESCRIPTION("Solarflare Communications network driver");
2152MODULE_LICENSE("GPL");
2153MODULE_DEVICE_TABLE(pci, efx_pci_table);