blob: e5024bb8a46e1bb56389cb8bb6780d163f2beb3d [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
Ben Hutchings8ceee662008-04-27 12:55:59 +010080/* This controls whether or not the driver will initialise devices
81 * with invalid MAC addresses stored in the EEPROM or flash. If true,
82 * such devices will be initialised with a random locally-generated
83 * MAC address. This allows for loading the sfc_mtd driver to
84 * reprogram the flash, even if the flash contents (including the MAC
85 * address) have previously been erased.
86 */
87static unsigned int allow_bad_hwaddr;
88
89/* Initial interrupt moderation settings. They can be modified after
90 * module load with ethtool.
91 *
92 * The default for RX should strike a balance between increasing the
93 * round-trip latency and reducing overhead.
94 */
95static unsigned int rx_irq_mod_usec = 60;
96
97/* Initial interrupt moderation settings. They can be modified after
98 * module load with ethtool.
99 *
100 * This default is chosen to ensure that a 10G link does not go idle
101 * while a TX queue is stopped after it has become full. A queue is
102 * restarted when it drops below half full. The time this takes (assuming
103 * worst case 3 descriptors per packet and 1024 descriptors) is
104 * 512 / 3 * 1.2 = 205 usec.
105 */
106static unsigned int tx_irq_mod_usec = 150;
107
108/* This is the first interrupt mode to try out of:
109 * 0 => MSI-X
110 * 1 => MSI
111 * 2 => legacy
112 */
113static unsigned int interrupt_mode;
114
115/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
116 * i.e. the number of CPUs among which we may distribute simultaneous
117 * interrupt handling.
118 *
119 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
120 * The default (0) means to assign an interrupt to each package (level II cache)
121 */
122static unsigned int rss_cpus;
123module_param(rss_cpus, uint, 0444);
124MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
125
126/**************************************************************************
127 *
128 * Utility functions and prototypes
129 *
130 *************************************************************************/
131static void efx_remove_channel(struct efx_channel *channel);
132static void efx_remove_port(struct efx_nic *efx);
133static void efx_fini_napi(struct efx_nic *efx);
134static void efx_fini_channels(struct efx_nic *efx);
135
136#define EFX_ASSERT_RESET_SERIALISED(efx) \
137 do { \
Ben Hutchings3c787082008-09-01 12:49:08 +0100138 if (efx->state == STATE_RUNNING) \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100139 ASSERT_RTNL(); \
140 } while (0)
141
142/**************************************************************************
143 *
144 * Event queue processing
145 *
146 *************************************************************************/
147
148/* Process channel's event queue
149 *
150 * This function is responsible for processing the event queue of a
151 * single channel. The caller must guarantee that this function will
152 * never be concurrently called more than once on the same channel,
153 * though different channels may be being processed concurrently.
154 */
Ben Hutchings4d566062008-09-01 12:47:12 +0100155static int efx_process_channel(struct efx_channel *channel, int rx_quota)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100156{
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100157 struct efx_nic *efx = channel->efx;
158 int rx_packets;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100159
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100160 if (unlikely(efx->reset_pending != RESET_TYPE_NONE ||
Ben Hutchings8ceee662008-04-27 12:55:59 +0100161 !channel->enabled))
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100162 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100163
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100164 rx_packets = falcon_process_eventq(channel, rx_quota);
165 if (rx_packets == 0)
166 return 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100167
168 /* Deliver last RX packet. */
169 if (channel->rx_pkt) {
170 __efx_rx_packet(channel, channel->rx_pkt,
171 channel->rx_pkt_csummed);
172 channel->rx_pkt = NULL;
173 }
174
175 efx_flush_lro(channel);
176 efx_rx_strategy(channel);
177
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100178 efx_fast_push_rx_descriptors(&efx->rx_queue[channel->channel]);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100179
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100180 return rx_packets;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100181}
182
183/* Mark channel as finished processing
184 *
185 * Note that since we will not receive further interrupts for this
186 * channel before we finish processing and call the eventq_read_ack()
187 * method, there is no need to use the interrupt hold-off timers.
188 */
189static inline void efx_channel_processed(struct efx_channel *channel)
190{
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100191 /* The interrupt handler for this channel may set work_pending
192 * as soon as we acknowledge the events we've seen. Make sure
193 * it's cleared before then. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100194 channel->work_pending = false;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100195 smp_wmb();
196
Ben Hutchings8ceee662008-04-27 12:55:59 +0100197 falcon_eventq_read_ack(channel);
198}
199
200/* NAPI poll handler
201 *
202 * NAPI guarantees serialisation of polls of the same device, which
203 * provides the guarantee required by efx_process_channel().
204 */
205static int efx_poll(struct napi_struct *napi, int budget)
206{
207 struct efx_channel *channel =
208 container_of(napi, struct efx_channel, napi_str);
209 struct net_device *napi_dev = channel->napi_dev;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100210 int rx_packets;
211
212 EFX_TRACE(channel->efx, "channel %d NAPI poll executing on CPU %d\n",
213 channel->channel, raw_smp_processor_id());
214
Ben Hutchings42cbe2d2008-09-01 12:48:08 +0100215 rx_packets = efx_process_channel(channel, budget);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100216
217 if (rx_packets < budget) {
218 /* There is no race here; although napi_disable() will
219 * only wait for netif_rx_complete(), this isn't a problem
220 * since efx_channel_processed() will have no effect if
221 * interrupts have already been disabled.
222 */
223 netif_rx_complete(napi_dev, napi);
224 efx_channel_processed(channel);
225 }
226
227 return rx_packets;
228}
229
230/* Process the eventq of the specified channel immediately on this CPU
231 *
232 * Disable hardware generated interrupts, wait for any existing
233 * processing to finish, then directly poll (and ack ) the eventq.
234 * Finally reenable NAPI and interrupts.
235 *
236 * Since we are touching interrupts the caller should hold the suspend lock
237 */
238void efx_process_channel_now(struct efx_channel *channel)
239{
240 struct efx_nic *efx = channel->efx;
241
242 BUG_ON(!channel->used_flags);
243 BUG_ON(!channel->enabled);
244
245 /* Disable interrupts and wait for ISRs to complete */
246 falcon_disable_interrupts(efx);
247 if (efx->legacy_irq)
248 synchronize_irq(efx->legacy_irq);
Ben Hutchings64ee3122008-09-01 12:47:38 +0100249 if (channel->irq)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100250 synchronize_irq(channel->irq);
251
252 /* Wait for any NAPI processing to complete */
253 napi_disable(&channel->napi_str);
254
255 /* Poll the channel */
Ben Hutchings91ad7572008-05-16 21:14:27 +0100256 efx_process_channel(channel, efx->type->evq_size);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100257
258 /* Ack the eventq. This may cause an interrupt to be generated
259 * when they are reenabled */
260 efx_channel_processed(channel);
261
262 napi_enable(&channel->napi_str);
263 falcon_enable_interrupts(efx);
264}
265
266/* Create event queue
267 * Event queue memory allocations are done only once. If the channel
268 * is reset, the memory buffer will be reused; this guards against
269 * errors during channel reset and also simplifies interrupt handling.
270 */
271static int efx_probe_eventq(struct efx_channel *channel)
272{
273 EFX_LOG(channel->efx, "chan %d create event queue\n", channel->channel);
274
275 return falcon_probe_eventq(channel);
276}
277
278/* Prepare channel's event queue */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100279static void efx_init_eventq(struct efx_channel *channel)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100280{
281 EFX_LOG(channel->efx, "chan %d init event queue\n", channel->channel);
282
283 channel->eventq_read_ptr = 0;
284
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100285 falcon_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100286}
287
288static void efx_fini_eventq(struct efx_channel *channel)
289{
290 EFX_LOG(channel->efx, "chan %d fini event queue\n", channel->channel);
291
292 falcon_fini_eventq(channel);
293}
294
295static void efx_remove_eventq(struct efx_channel *channel)
296{
297 EFX_LOG(channel->efx, "chan %d remove event queue\n", channel->channel);
298
299 falcon_remove_eventq(channel);
300}
301
302/**************************************************************************
303 *
304 * Channel handling
305 *
306 *************************************************************************/
307
Ben Hutchings8ceee662008-04-27 12:55:59 +0100308static int efx_probe_channel(struct efx_channel *channel)
309{
310 struct efx_tx_queue *tx_queue;
311 struct efx_rx_queue *rx_queue;
312 int rc;
313
314 EFX_LOG(channel->efx, "creating channel %d\n", channel->channel);
315
316 rc = efx_probe_eventq(channel);
317 if (rc)
318 goto fail1;
319
320 efx_for_each_channel_tx_queue(tx_queue, channel) {
321 rc = efx_probe_tx_queue(tx_queue);
322 if (rc)
323 goto fail2;
324 }
325
326 efx_for_each_channel_rx_queue(rx_queue, channel) {
327 rc = efx_probe_rx_queue(rx_queue);
328 if (rc)
329 goto fail3;
330 }
331
332 channel->n_rx_frm_trunc = 0;
333
334 return 0;
335
336 fail3:
337 efx_for_each_channel_rx_queue(rx_queue, channel)
338 efx_remove_rx_queue(rx_queue);
339 fail2:
340 efx_for_each_channel_tx_queue(tx_queue, channel)
341 efx_remove_tx_queue(tx_queue);
342 fail1:
343 return rc;
344}
345
346
347/* Channels are shutdown and reinitialised whilst the NIC is running
348 * to propagate configuration changes (mtu, checksum offload), or
349 * to clear hardware error conditions
350 */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100351static void efx_init_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100352{
353 struct efx_tx_queue *tx_queue;
354 struct efx_rx_queue *rx_queue;
355 struct efx_channel *channel;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100356
Ben Hutchingsf7f13b02008-05-16 21:15:06 +0100357 /* Calculate the rx buffer allocation parameters required to
358 * support the current MTU, including padding for header
359 * alignment and overruns.
360 */
361 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
362 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
363 efx->type->rx_buffer_padding);
364 efx->rx_buffer_order = get_order(efx->rx_buffer_len);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100365
366 /* Initialise the channels */
367 efx_for_each_channel(channel, efx) {
368 EFX_LOG(channel->efx, "init chan %d\n", channel->channel);
369
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100370 efx_init_eventq(channel);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100371
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100372 efx_for_each_channel_tx_queue(tx_queue, channel)
373 efx_init_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100374
375 /* The rx buffer allocation strategy is MTU dependent */
376 efx_rx_strategy(channel);
377
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +0100378 efx_for_each_channel_rx_queue(rx_queue, channel)
379 efx_init_rx_queue(rx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100380
381 WARN_ON(channel->rx_pkt != NULL);
382 efx_rx_strategy(channel);
383 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100384}
385
386/* This enables event queue processing and packet transmission.
387 *
388 * Note that this function is not allowed to fail, since that would
389 * introduce too much complexity into the suspend/resume path.
390 */
391static void efx_start_channel(struct efx_channel *channel)
392{
393 struct efx_rx_queue *rx_queue;
394
395 EFX_LOG(channel->efx, "starting chan %d\n", channel->channel);
396
397 if (!(channel->efx->net_dev->flags & IFF_UP))
398 netif_napi_add(channel->napi_dev, &channel->napi_str,
399 efx_poll, napi_weight);
400
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100401 /* The interrupt handler for this channel may set work_pending
402 * as soon as we enable it. Make sure it's cleared before
403 * then. Similarly, make sure it sees the enabled flag set. */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100404 channel->work_pending = false;
405 channel->enabled = true;
Ben Hutchings5b9e2072008-05-16 21:18:14 +0100406 smp_wmb();
Ben Hutchings8ceee662008-04-27 12:55:59 +0100407
408 napi_enable(&channel->napi_str);
409
410 /* Load up RX descriptors */
411 efx_for_each_channel_rx_queue(rx_queue, channel)
412 efx_fast_push_rx_descriptors(rx_queue);
413}
414
415/* This disables event queue processing and packet transmission.
416 * This function does not guarantee that all queue processing
417 * (e.g. RX refill) is complete.
418 */
419static void efx_stop_channel(struct efx_channel *channel)
420{
421 struct efx_rx_queue *rx_queue;
422
423 if (!channel->enabled)
424 return;
425
426 EFX_LOG(channel->efx, "stop chan %d\n", channel->channel);
427
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100428 channel->enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100429 napi_disable(&channel->napi_str);
430
431 /* Ensure that any worker threads have exited or will be no-ops */
432 efx_for_each_channel_rx_queue(rx_queue, channel) {
433 spin_lock_bh(&rx_queue->add_lock);
434 spin_unlock_bh(&rx_queue->add_lock);
435 }
436}
437
438static void efx_fini_channels(struct efx_nic *efx)
439{
440 struct efx_channel *channel;
441 struct efx_tx_queue *tx_queue;
442 struct efx_rx_queue *rx_queue;
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100443 int rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100444
445 EFX_ASSERT_RESET_SERIALISED(efx);
446 BUG_ON(efx->port_enabled);
447
Ben Hutchings6bc5d3a2008-09-01 12:49:37 +0100448 rc = falcon_flush_queues(efx);
449 if (rc)
450 EFX_ERR(efx, "failed to flush queues\n");
451 else
452 EFX_LOG(efx, "successfully flushed all queues\n");
453
Ben Hutchings8ceee662008-04-27 12:55:59 +0100454 efx_for_each_channel(channel, efx) {
455 EFX_LOG(channel->efx, "shut down chan %d\n", channel->channel);
456
457 efx_for_each_channel_rx_queue(rx_queue, channel)
458 efx_fini_rx_queue(rx_queue);
459 efx_for_each_channel_tx_queue(tx_queue, channel)
460 efx_fini_tx_queue(tx_queue);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100461 efx_fini_eventq(channel);
462 }
463}
464
465static void efx_remove_channel(struct efx_channel *channel)
466{
467 struct efx_tx_queue *tx_queue;
468 struct efx_rx_queue *rx_queue;
469
470 EFX_LOG(channel->efx, "destroy chan %d\n", channel->channel);
471
472 efx_for_each_channel_rx_queue(rx_queue, channel)
473 efx_remove_rx_queue(rx_queue);
474 efx_for_each_channel_tx_queue(tx_queue, channel)
475 efx_remove_tx_queue(tx_queue);
476 efx_remove_eventq(channel);
477
478 channel->used_flags = 0;
479}
480
481void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue, int delay)
482{
483 queue_delayed_work(refill_workqueue, &rx_queue->work, delay);
484}
485
486/**************************************************************************
487 *
488 * Port handling
489 *
490 **************************************************************************/
491
492/* This ensures that the kernel is kept informed (via
493 * netif_carrier_on/off) of the link status, and also maintains the
494 * link status's stop on the port's TX queue.
495 */
496static void efx_link_status_changed(struct efx_nic *efx)
497{
Ben Hutchings8ceee662008-04-27 12:55:59 +0100498 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
499 * that no events are triggered between unregister_netdev() and the
500 * driver unloading. A more general condition is that NETDEV_CHANGE
501 * can only be generated between NETDEV_UP and NETDEV_DOWN */
502 if (!netif_running(efx->net_dev))
503 return;
504
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100505 if (efx->port_inhibited) {
506 netif_carrier_off(efx->net_dev);
507 return;
508 }
509
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100510 if (efx->link_up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100511 efx->n_link_state_changes++;
512
513 if (efx->link_up)
514 netif_carrier_on(efx->net_dev);
515 else
516 netif_carrier_off(efx->net_dev);
517 }
518
519 /* Status message for kernel log */
520 if (efx->link_up) {
521 struct mii_if_info *gmii = &efx->mii;
522 unsigned adv, lpa;
523 /* NONE here means direct XAUI from the controller, with no
524 * MDIO-attached device we can query. */
525 if (efx->phy_type != PHY_TYPE_NONE) {
526 adv = gmii_advertised(gmii);
527 lpa = gmii_lpa(gmii);
528 } else {
529 lpa = GM_LPA_10000 | LPA_DUPLEX;
530 adv = lpa;
531 }
532 EFX_INFO(efx, "link up at %dMbps %s-duplex "
533 "(adv %04x lpa %04x) (MTU %d)%s\n",
534 (efx->link_options & GM_LPA_10000 ? 10000 :
535 (efx->link_options & GM_LPA_1000 ? 1000 :
536 (efx->link_options & GM_LPA_100 ? 100 :
537 10))),
538 (efx->link_options & GM_LPA_DUPLEX ?
539 "full" : "half"),
540 adv, lpa,
541 efx->net_dev->mtu,
542 (efx->promiscuous ? " [PROMISC]" : ""));
543 } else {
544 EFX_INFO(efx, "link down\n");
545 }
546
547}
548
549/* This call reinitialises the MAC to pick up new PHY settings. The
550 * caller must hold the mac_lock */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100551void __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100552{
553 WARN_ON(!mutex_is_locked(&efx->mac_lock));
554
555 EFX_LOG(efx, "reconfiguring MAC from PHY settings on CPU %d\n",
556 raw_smp_processor_id());
557
Ben Hutchingsa816f752008-09-01 12:49:12 +0100558 /* Serialise the promiscuous flag with efx_set_multicast_list. */
559 if (efx_dev_registered(efx)) {
560 netif_addr_lock_bh(efx->net_dev);
561 netif_addr_unlock_bh(efx->net_dev);
562 }
563
Ben Hutchings8ceee662008-04-27 12:55:59 +0100564 falcon_reconfigure_xmac(efx);
565
566 /* Inform kernel of loss/gain of carrier */
567 efx_link_status_changed(efx);
568}
569
570/* Reinitialise the MAC to pick up new PHY settings, even if the port is
571 * disabled. */
572void efx_reconfigure_port(struct efx_nic *efx)
573{
574 EFX_ASSERT_RESET_SERIALISED(efx);
575
576 mutex_lock(&efx->mac_lock);
577 __efx_reconfigure_port(efx);
578 mutex_unlock(&efx->mac_lock);
579}
580
581/* Asynchronous efx_reconfigure_port work item. To speed up efx_flush_all()
582 * we don't efx_reconfigure_port() if the port is disabled. Care is taken
583 * in efx_stop_all() and efx_start_port() to prevent PHY events being lost */
584static void efx_reconfigure_work(struct work_struct *data)
585{
586 struct efx_nic *efx = container_of(data, struct efx_nic,
587 reconfigure_work);
588
589 mutex_lock(&efx->mac_lock);
590 if (efx->port_enabled)
591 __efx_reconfigure_port(efx);
592 mutex_unlock(&efx->mac_lock);
593}
594
595static int efx_probe_port(struct efx_nic *efx)
596{
597 int rc;
598
599 EFX_LOG(efx, "create port\n");
600
601 /* Connect up MAC/PHY operations table and read MAC address */
602 rc = falcon_probe_port(efx);
603 if (rc)
604 goto err;
605
606 /* Sanity check MAC address */
607 if (is_valid_ether_addr(efx->mac_address)) {
608 memcpy(efx->net_dev->dev_addr, efx->mac_address, ETH_ALEN);
609 } else {
Johannes Berge1749612008-10-27 15:59:26 -0700610 EFX_ERR(efx, "invalid MAC address %pM\n",
611 efx->mac_address);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100612 if (!allow_bad_hwaddr) {
613 rc = -EINVAL;
614 goto err;
615 }
616 random_ether_addr(efx->net_dev->dev_addr);
Johannes Berge1749612008-10-27 15:59:26 -0700617 EFX_INFO(efx, "using locally-generated MAC %pM\n",
618 efx->net_dev->dev_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100619 }
620
621 return 0;
622
623 err:
624 efx_remove_port(efx);
625 return rc;
626}
627
628static int efx_init_port(struct efx_nic *efx)
629{
630 int rc;
631
632 EFX_LOG(efx, "init port\n");
633
634 /* Initialise the MAC and PHY */
635 rc = falcon_init_xmac(efx);
636 if (rc)
637 return rc;
638
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100639 efx->port_initialized = true;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100640 efx->stats_enabled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100641
642 /* Reconfigure port to program MAC registers */
643 falcon_reconfigure_xmac(efx);
644
645 return 0;
646}
647
648/* Allow efx_reconfigure_port() to be scheduled, and close the window
649 * between efx_stop_port and efx_flush_all whereby a previously scheduled
650 * efx_reconfigure_port() may have been cancelled */
651static void efx_start_port(struct efx_nic *efx)
652{
653 EFX_LOG(efx, "start port\n");
654 BUG_ON(efx->port_enabled);
655
656 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100657 efx->port_enabled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100658 __efx_reconfigure_port(efx);
659 mutex_unlock(&efx->mac_lock);
660}
661
662/* Prevent efx_reconfigure_work and efx_monitor() from executing, and
663 * efx_set_multicast_list() from scheduling efx_reconfigure_work.
664 * efx_reconfigure_work can still be scheduled via NAPI processing
665 * until efx_flush_all() is called */
666static void efx_stop_port(struct efx_nic *efx)
667{
668 EFX_LOG(efx, "stop port\n");
669
670 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100671 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100672 mutex_unlock(&efx->mac_lock);
673
674 /* Serialise against efx_set_multicast_list() */
Ben Hutchings55668612008-05-16 21:16:10 +0100675 if (efx_dev_registered(efx)) {
David S. Millerb9e40852008-07-15 00:15:08 -0700676 netif_addr_lock_bh(efx->net_dev);
677 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100678 }
679}
680
681static void efx_fini_port(struct efx_nic *efx)
682{
683 EFX_LOG(efx, "shut down port\n");
684
685 if (!efx->port_initialized)
686 return;
687
688 falcon_fini_xmac(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100689 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100690
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100691 efx->link_up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100692 efx_link_status_changed(efx);
693}
694
695static void efx_remove_port(struct efx_nic *efx)
696{
697 EFX_LOG(efx, "destroying port\n");
698
699 falcon_remove_port(efx);
700}
701
702/**************************************************************************
703 *
704 * NIC handling
705 *
706 **************************************************************************/
707
708/* This configures the PCI device to enable I/O and DMA. */
709static int efx_init_io(struct efx_nic *efx)
710{
711 struct pci_dev *pci_dev = efx->pci_dev;
712 dma_addr_t dma_mask = efx->type->max_dma_mask;
713 int rc;
714
715 EFX_LOG(efx, "initialising I/O\n");
716
717 rc = pci_enable_device(pci_dev);
718 if (rc) {
719 EFX_ERR(efx, "failed to enable PCI device\n");
720 goto fail1;
721 }
722
723 pci_set_master(pci_dev);
724
725 /* Set the PCI DMA mask. Try all possibilities from our
726 * genuine mask down to 32 bits, because some architectures
727 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
728 * masks event though they reject 46 bit masks.
729 */
730 while (dma_mask > 0x7fffffffUL) {
731 if (pci_dma_supported(pci_dev, dma_mask) &&
732 ((rc = pci_set_dma_mask(pci_dev, dma_mask)) == 0))
733 break;
734 dma_mask >>= 1;
735 }
736 if (rc) {
737 EFX_ERR(efx, "could not find a suitable DMA mask\n");
738 goto fail2;
739 }
740 EFX_LOG(efx, "using DMA mask %llx\n", (unsigned long long) dma_mask);
741 rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
742 if (rc) {
743 /* pci_set_consistent_dma_mask() is not *allowed* to
744 * fail with a mask that pci_set_dma_mask() accepted,
745 * but just in case...
746 */
747 EFX_ERR(efx, "failed to set consistent DMA mask\n");
748 goto fail2;
749 }
750
751 efx->membase_phys = pci_resource_start(efx->pci_dev,
752 efx->type->mem_bar);
753 rc = pci_request_region(pci_dev, efx->type->mem_bar, "sfc");
754 if (rc) {
755 EFX_ERR(efx, "request for memory BAR failed\n");
756 rc = -EIO;
757 goto fail3;
758 }
759 efx->membase = ioremap_nocache(efx->membase_phys,
760 efx->type->mem_map_size);
761 if (!efx->membase) {
Ben Hutchings086ea352008-05-16 21:17:06 +0100762 EFX_ERR(efx, "could not map memory BAR %d at %llx+%x\n",
763 efx->type->mem_bar,
764 (unsigned long long)efx->membase_phys,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100765 efx->type->mem_map_size);
766 rc = -ENOMEM;
767 goto fail4;
768 }
Ben Hutchings086ea352008-05-16 21:17:06 +0100769 EFX_LOG(efx, "memory BAR %u at %llx+%x (virtual %p)\n",
770 efx->type->mem_bar, (unsigned long long)efx->membase_phys,
771 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100772
773 return 0;
774
775 fail4:
Ben Hutchingse1074a02008-09-01 12:49:15 +0100776 pci_release_region(efx->pci_dev, efx->type->mem_bar);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100777 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +0100778 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100779 fail2:
780 pci_disable_device(efx->pci_dev);
781 fail1:
782 return rc;
783}
784
785static void efx_fini_io(struct efx_nic *efx)
786{
787 EFX_LOG(efx, "shutting down I/O\n");
788
789 if (efx->membase) {
790 iounmap(efx->membase);
791 efx->membase = NULL;
792 }
793
794 if (efx->membase_phys) {
795 pci_release_region(efx->pci_dev, efx->type->mem_bar);
Ben Hutchings2c118e02008-05-16 21:15:29 +0100796 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100797 }
798
799 pci_disable_device(efx->pci_dev);
800}
801
Ben Hutchings46123d02008-09-01 12:47:33 +0100802/* Get number of RX queues wanted. Return number of online CPU
803 * packages in the expectation that an IRQ balancer will spread
804 * interrupts across them. */
805static int efx_wanted_rx_queues(void)
806{
807 cpumask_t core_mask;
808 int count;
809 int cpu;
810
811 cpus_clear(core_mask);
812 count = 0;
813 for_each_online_cpu(cpu) {
814 if (!cpu_isset(cpu, core_mask)) {
815 ++count;
816 cpus_or(core_mask, core_mask,
817 topology_core_siblings(cpu));
818 }
819 }
820
821 return count;
822}
823
824/* Probe the number and type of interrupts we are able to obtain, and
825 * the resulting numbers of channels and RX queues.
826 */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100827static void efx_probe_interrupts(struct efx_nic *efx)
828{
Ben Hutchings46123d02008-09-01 12:47:33 +0100829 int max_channels =
830 min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100831 int rc, i;
832
833 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +0100834 struct msix_entry xentries[EFX_MAX_CHANNELS];
835 int wanted_ints;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100836
Ben Hutchings46123d02008-09-01 12:47:33 +0100837 /* We want one RX queue and interrupt per CPU package
838 * (or as specified by the rss_cpus module parameter).
839 * We will need one channel per interrupt.
840 */
841 wanted_ints = rss_cpus ? rss_cpus : efx_wanted_rx_queues();
Ben Hutchings8831da72008-09-01 12:47:48 +0100842 efx->n_rx_queues = min(wanted_ints, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +0100843
Ben Hutchings8831da72008-09-01 12:47:48 +0100844 for (i = 0; i < efx->n_rx_queues; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100845 xentries[i].entry = i;
Ben Hutchings8831da72008-09-01 12:47:48 +0100846 rc = pci_enable_msix(efx->pci_dev, xentries, efx->n_rx_queues);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100847 if (rc > 0) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100848 EFX_BUG_ON_PARANOID(rc >= efx->n_rx_queues);
849 efx->n_rx_queues = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100850 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchings8831da72008-09-01 12:47:48 +0100851 efx->n_rx_queues);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100852 }
853
854 if (rc == 0) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100855 for (i = 0; i < efx->n_rx_queues; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100856 efx->channel[i].irq = xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100857 } else {
858 /* Fall back to single channel MSI */
859 efx->interrupt_mode = EFX_INT_MODE_MSI;
860 EFX_ERR(efx, "could not enable MSI-X\n");
861 }
862 }
863
864 /* Try single interrupt MSI */
865 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100866 efx->n_rx_queues = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100867 rc = pci_enable_msi(efx->pci_dev);
868 if (rc == 0) {
869 efx->channel[0].irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100870 } else {
871 EFX_ERR(efx, "could not enable MSI\n");
872 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
873 }
874 }
875
876 /* Assume legacy interrupts */
877 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100878 efx->n_rx_queues = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100879 efx->legacy_irq = efx->pci_dev->irq;
880 }
881}
882
883static void efx_remove_interrupts(struct efx_nic *efx)
884{
885 struct efx_channel *channel;
886
887 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100888 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100889 channel->irq = 0;
890 pci_disable_msi(efx->pci_dev);
891 pci_disable_msix(efx->pci_dev);
892
893 /* Remove legacy interrupt */
894 efx->legacy_irq = 0;
895}
896
Ben Hutchings8831da72008-09-01 12:47:48 +0100897static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100898{
899 struct efx_tx_queue *tx_queue;
900 struct efx_rx_queue *rx_queue;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100901
Ben Hutchings60ac1062008-09-01 12:44:59 +0100902 efx_for_each_tx_queue(tx_queue, efx) {
903 if (!EFX_INT_MODE_USE_MSI(efx) && separate_tx_and_rx_channels)
904 tx_queue->channel = &efx->channel[1];
905 else
906 tx_queue->channel = &efx->channel[0];
907 tx_queue->channel->used_flags |= EFX_USED_BY_TX;
908 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100909
Ben Hutchings8831da72008-09-01 12:47:48 +0100910 efx_for_each_rx_queue(rx_queue, efx) {
911 rx_queue->channel = &efx->channel[rx_queue->queue];
912 rx_queue->channel->used_flags |= EFX_USED_BY_RX;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100913 }
914}
915
916static int efx_probe_nic(struct efx_nic *efx)
917{
918 int rc;
919
920 EFX_LOG(efx, "creating NIC\n");
921
922 /* Carry out hardware-type specific initialisation */
923 rc = falcon_probe_nic(efx);
924 if (rc)
925 return rc;
926
927 /* Determine the number of channels and RX queues by trying to hook
928 * in MSI-X interrupts. */
929 efx_probe_interrupts(efx);
930
Ben Hutchings8831da72008-09-01 12:47:48 +0100931 efx_set_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100932
933 /* Initialise the interrupt moderation settings */
934 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec);
935
936 return 0;
937}
938
939static void efx_remove_nic(struct efx_nic *efx)
940{
941 EFX_LOG(efx, "destroying NIC\n");
942
943 efx_remove_interrupts(efx);
944 falcon_remove_nic(efx);
945}
946
947/**************************************************************************
948 *
949 * NIC startup/shutdown
950 *
951 *************************************************************************/
952
953static int efx_probe_all(struct efx_nic *efx)
954{
955 struct efx_channel *channel;
956 int rc;
957
958 /* Create NIC */
959 rc = efx_probe_nic(efx);
960 if (rc) {
961 EFX_ERR(efx, "failed to create NIC\n");
962 goto fail1;
963 }
964
965 /* Create port */
966 rc = efx_probe_port(efx);
967 if (rc) {
968 EFX_ERR(efx, "failed to create port\n");
969 goto fail2;
970 }
971
972 /* Create channels */
973 efx_for_each_channel(channel, efx) {
974 rc = efx_probe_channel(channel);
975 if (rc) {
976 EFX_ERR(efx, "failed to create channel %d\n",
977 channel->channel);
978 goto fail3;
979 }
980 }
981
982 return 0;
983
984 fail3:
985 efx_for_each_channel(channel, efx)
986 efx_remove_channel(channel);
987 efx_remove_port(efx);
988 fail2:
989 efx_remove_nic(efx);
990 fail1:
991 return rc;
992}
993
994/* Called after previous invocation(s) of efx_stop_all, restarts the
995 * port, kernel transmit queue, NAPI processing and hardware interrupts,
996 * and ensures that the port is scheduled to be reconfigured.
997 * This function is safe to call multiple times when the NIC is in any
998 * state. */
999static void efx_start_all(struct efx_nic *efx)
1000{
1001 struct efx_channel *channel;
1002
1003 EFX_ASSERT_RESET_SERIALISED(efx);
1004
1005 /* Check that it is appropriate to restart the interface. All
1006 * of these flags are safe to read under just the rtnl lock */
1007 if (efx->port_enabled)
1008 return;
1009 if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1010 return;
Ben Hutchings55668612008-05-16 21:16:10 +01001011 if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001012 return;
1013
1014 /* Mark the port as enabled so port reconfigurations can start, then
1015 * restart the transmit interface early so the watchdog timer stops */
1016 efx_start_port(efx);
Steve Hodgsondacccc72008-09-01 12:48:20 +01001017 if (efx_dev_registered(efx))
1018 efx_wake_queue(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001019
1020 efx_for_each_channel(channel, efx)
1021 efx_start_channel(channel);
1022
1023 falcon_enable_interrupts(efx);
1024
1025 /* Start hardware monitor if we're in RUNNING */
1026 if (efx->state == STATE_RUNNING)
1027 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1028 efx_monitor_interval);
1029}
1030
1031/* Flush all delayed work. Should only be called when no more delayed work
1032 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1033 * since we're holding the rtnl_lock at this point. */
1034static void efx_flush_all(struct efx_nic *efx)
1035{
1036 struct efx_rx_queue *rx_queue;
1037
1038 /* Make sure the hardware monitor is stopped */
1039 cancel_delayed_work_sync(&efx->monitor_work);
1040
1041 /* Ensure that all RX slow refills are complete. */
Ben Hutchingsb3475642008-05-16 21:15:49 +01001042 efx_for_each_rx_queue(rx_queue, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001043 cancel_delayed_work_sync(&rx_queue->work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001044
1045 /* Stop scheduled port reconfigurations */
1046 cancel_work_sync(&efx->reconfigure_work);
1047
1048}
1049
1050/* Quiesce hardware and software without bringing the link down.
1051 * Safe to call multiple times, when the nic and interface is in any
1052 * state. The caller is guaranteed to subsequently be in a position
1053 * to modify any hardware and software state they see fit without
1054 * taking locks. */
1055static void efx_stop_all(struct efx_nic *efx)
1056{
1057 struct efx_channel *channel;
1058
1059 EFX_ASSERT_RESET_SERIALISED(efx);
1060
1061 /* port_enabled can be read safely under the rtnl lock */
1062 if (!efx->port_enabled)
1063 return;
1064
1065 /* Disable interrupts and wait for ISR to complete */
1066 falcon_disable_interrupts(efx);
1067 if (efx->legacy_irq)
1068 synchronize_irq(efx->legacy_irq);
Ben Hutchings64ee3122008-09-01 12:47:38 +01001069 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001070 if (channel->irq)
1071 synchronize_irq(channel->irq);
Ben Hutchingsb3475642008-05-16 21:15:49 +01001072 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001073
1074 /* Stop all NAPI processing and synchronous rx refills */
1075 efx_for_each_channel(channel, efx)
1076 efx_stop_channel(channel);
1077
1078 /* Stop all asynchronous port reconfigurations. Since all
1079 * event processing has already been stopped, there is no
1080 * window to loose phy events */
1081 efx_stop_port(efx);
1082
1083 /* Flush reconfigure_work, refill_workqueue, monitor_work */
1084 efx_flush_all(efx);
1085
1086 /* Isolate the MAC from the TX and RX engines, so that queue
1087 * flushes will complete in a timely fashion. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001088 falcon_drain_tx_fifo(efx);
1089
1090 /* Stop the kernel transmit interface late, so the watchdog
1091 * timer isn't ticking over the flush */
Ben Hutchings55668612008-05-16 21:16:10 +01001092 if (efx_dev_registered(efx)) {
Steve Hodgsondacccc72008-09-01 12:48:20 +01001093 efx_stop_queue(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001094 netif_tx_lock_bh(efx->net_dev);
1095 netif_tx_unlock_bh(efx->net_dev);
1096 }
1097}
1098
1099static void efx_remove_all(struct efx_nic *efx)
1100{
1101 struct efx_channel *channel;
1102
1103 efx_for_each_channel(channel, efx)
1104 efx_remove_channel(channel);
1105 efx_remove_port(efx);
1106 efx_remove_nic(efx);
1107}
1108
1109/* A convinience function to safely flush all the queues */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001110void efx_flush_queues(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001111{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001112 EFX_ASSERT_RESET_SERIALISED(efx);
1113
1114 efx_stop_all(efx);
1115
1116 efx_fini_channels(efx);
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001117 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001118
1119 efx_start_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001120}
1121
1122/**************************************************************************
1123 *
1124 * Interrupt moderation
1125 *
1126 **************************************************************************/
1127
1128/* Set interrupt moderation parameters */
1129void efx_init_irq_moderation(struct efx_nic *efx, int tx_usecs, int rx_usecs)
1130{
1131 struct efx_tx_queue *tx_queue;
1132 struct efx_rx_queue *rx_queue;
1133
1134 EFX_ASSERT_RESET_SERIALISED(efx);
1135
1136 efx_for_each_tx_queue(tx_queue, efx)
1137 tx_queue->channel->irq_moderation = tx_usecs;
1138
1139 efx_for_each_rx_queue(rx_queue, efx)
1140 rx_queue->channel->irq_moderation = rx_usecs;
1141}
1142
1143/**************************************************************************
1144 *
1145 * Hardware monitor
1146 *
1147 **************************************************************************/
1148
1149/* Run periodically off the general workqueue. Serialised against
1150 * efx_reconfigure_port via the mac_lock */
1151static void efx_monitor(struct work_struct *data)
1152{
1153 struct efx_nic *efx = container_of(data, struct efx_nic,
1154 monitor_work.work);
1155 int rc = 0;
1156
1157 EFX_TRACE(efx, "hardware monitor executing on CPU %d\n",
1158 raw_smp_processor_id());
1159
1160
1161 /* If the mac_lock is already held then it is likely a port
1162 * reconfiguration is already in place, which will likely do
1163 * most of the work of check_hw() anyway. */
1164 if (!mutex_trylock(&efx->mac_lock)) {
1165 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1166 efx_monitor_interval);
1167 return;
1168 }
1169
1170 if (efx->port_enabled)
1171 rc = falcon_check_xmac(efx);
1172 mutex_unlock(&efx->mac_lock);
1173
Ben Hutchings8ceee662008-04-27 12:55:59 +01001174 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1175 efx_monitor_interval);
1176}
1177
1178/**************************************************************************
1179 *
1180 * ioctls
1181 *
1182 *************************************************************************/
1183
1184/* Net device ioctl
1185 * Context: process, rtnl_lock() held.
1186 */
1187static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1188{
Ben Hutchings767e4682008-09-01 12:43:14 +01001189 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001190
1191 EFX_ASSERT_RESET_SERIALISED(efx);
1192
1193 return generic_mii_ioctl(&efx->mii, if_mii(ifr), cmd, NULL);
1194}
1195
1196/**************************************************************************
1197 *
1198 * NAPI interface
1199 *
1200 **************************************************************************/
1201
1202static int efx_init_napi(struct efx_nic *efx)
1203{
1204 struct efx_channel *channel;
1205 int rc;
1206
1207 efx_for_each_channel(channel, efx) {
1208 channel->napi_dev = efx->net_dev;
1209 rc = efx_lro_init(&channel->lro_mgr, efx);
1210 if (rc)
1211 goto err;
1212 }
1213 return 0;
1214 err:
1215 efx_fini_napi(efx);
1216 return rc;
1217}
1218
1219static void efx_fini_napi(struct efx_nic *efx)
1220{
1221 struct efx_channel *channel;
1222
1223 efx_for_each_channel(channel, efx) {
1224 efx_lro_fini(&channel->lro_mgr);
1225 channel->napi_dev = NULL;
1226 }
1227}
1228
1229/**************************************************************************
1230 *
1231 * Kernel netpoll interface
1232 *
1233 *************************************************************************/
1234
1235#ifdef CONFIG_NET_POLL_CONTROLLER
1236
1237/* Although in the common case interrupts will be disabled, this is not
1238 * guaranteed. However, all our work happens inside the NAPI callback,
1239 * so no locking is required.
1240 */
1241static void efx_netpoll(struct net_device *net_dev)
1242{
Ben Hutchings767e4682008-09-01 12:43:14 +01001243 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001244 struct efx_channel *channel;
1245
Ben Hutchings64ee3122008-09-01 12:47:38 +01001246 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001247 efx_schedule_channel(channel);
1248}
1249
1250#endif
1251
1252/**************************************************************************
1253 *
1254 * Kernel net device interface
1255 *
1256 *************************************************************************/
1257
1258/* Context: process, rtnl_lock() held. */
1259static int efx_net_open(struct net_device *net_dev)
1260{
Ben Hutchings767e4682008-09-01 12:43:14 +01001261 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001262 EFX_ASSERT_RESET_SERIALISED(efx);
1263
1264 EFX_LOG(efx, "opening device %s on CPU %d\n", net_dev->name,
1265 raw_smp_processor_id());
1266
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001267 if (efx->phy_mode & PHY_MODE_SPECIAL)
1268 return -EBUSY;
1269
Ben Hutchings8ceee662008-04-27 12:55:59 +01001270 efx_start_all(efx);
1271 return 0;
1272}
1273
1274/* Context: process, rtnl_lock() held.
1275 * Note that the kernel will ignore our return code; this method
1276 * should really be a void.
1277 */
1278static int efx_net_stop(struct net_device *net_dev)
1279{
Ben Hutchings767e4682008-09-01 12:43:14 +01001280 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001281
1282 EFX_LOG(efx, "closing %s on CPU %d\n", net_dev->name,
1283 raw_smp_processor_id());
1284
1285 /* Stop the device and flush all the channels */
1286 efx_stop_all(efx);
1287 efx_fini_channels(efx);
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001288 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001289
1290 return 0;
1291}
1292
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001293/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001294static struct net_device_stats *efx_net_stats(struct net_device *net_dev)
1295{
Ben Hutchings767e4682008-09-01 12:43:14 +01001296 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001297 struct efx_mac_stats *mac_stats = &efx->mac_stats;
1298 struct net_device_stats *stats = &net_dev->stats;
1299
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001300 /* Update stats if possible, but do not wait if another thread
1301 * is updating them (or resetting the NIC); slightly stale
1302 * stats are acceptable.
1303 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001304 if (!spin_trylock(&efx->stats_lock))
1305 return stats;
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001306 if (efx->stats_enabled) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001307 falcon_update_stats_xmac(efx);
1308 falcon_update_nic_stats(efx);
1309 }
1310 spin_unlock(&efx->stats_lock);
1311
1312 stats->rx_packets = mac_stats->rx_packets;
1313 stats->tx_packets = mac_stats->tx_packets;
1314 stats->rx_bytes = mac_stats->rx_bytes;
1315 stats->tx_bytes = mac_stats->tx_bytes;
1316 stats->multicast = mac_stats->rx_multicast;
1317 stats->collisions = mac_stats->tx_collision;
1318 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1319 mac_stats->rx_length_error);
1320 stats->rx_over_errors = efx->n_rx_nodesc_drop_cnt;
1321 stats->rx_crc_errors = mac_stats->rx_bad;
1322 stats->rx_frame_errors = mac_stats->rx_align_error;
1323 stats->rx_fifo_errors = mac_stats->rx_overflow;
1324 stats->rx_missed_errors = mac_stats->rx_missed;
1325 stats->tx_window_errors = mac_stats->tx_late_collision;
1326
1327 stats->rx_errors = (stats->rx_length_errors +
1328 stats->rx_over_errors +
1329 stats->rx_crc_errors +
1330 stats->rx_frame_errors +
1331 stats->rx_fifo_errors +
1332 stats->rx_missed_errors +
1333 mac_stats->rx_symbol_error);
1334 stats->tx_errors = (stats->tx_window_errors +
1335 mac_stats->tx_bad);
1336
1337 return stats;
1338}
1339
1340/* Context: netif_tx_lock held, BHs disabled. */
1341static void efx_watchdog(struct net_device *net_dev)
1342{
Ben Hutchings767e4682008-09-01 12:43:14 +01001343 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001344
Ben Hutchings739bb232008-11-04 20:35:36 +00001345 EFX_ERR(efx, "TX stuck with stop_count=%d port_enabled=%d:"
1346 " resetting channels\n",
1347 atomic_read(&efx->netif_stop_count), efx->port_enabled);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001348
Ben Hutchings739bb232008-11-04 20:35:36 +00001349 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001350}
1351
1352
1353/* Context: process, rtnl_lock() held. */
1354static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1355{
Ben Hutchings767e4682008-09-01 12:43:14 +01001356 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001357 int rc = 0;
1358
1359 EFX_ASSERT_RESET_SERIALISED(efx);
1360
1361 if (new_mtu > EFX_MAX_MTU)
1362 return -EINVAL;
1363
1364 efx_stop_all(efx);
1365
1366 EFX_LOG(efx, "changing MTU to %d\n", new_mtu);
1367
1368 efx_fini_channels(efx);
1369 net_dev->mtu = new_mtu;
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001370 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001371
1372 efx_start_all(efx);
1373 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001374}
1375
1376static int efx_set_mac_address(struct net_device *net_dev, void *data)
1377{
Ben Hutchings767e4682008-09-01 12:43:14 +01001378 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001379 struct sockaddr *addr = data;
1380 char *new_addr = addr->sa_data;
1381
1382 EFX_ASSERT_RESET_SERIALISED(efx);
1383
1384 if (!is_valid_ether_addr(new_addr)) {
Johannes Berge1749612008-10-27 15:59:26 -07001385 EFX_ERR(efx, "invalid ethernet MAC address requested: %pM\n",
1386 new_addr);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001387 return -EINVAL;
1388 }
1389
1390 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1391
1392 /* Reconfigure the MAC */
1393 efx_reconfigure_port(efx);
1394
1395 return 0;
1396}
1397
Ben Hutchingsa816f752008-09-01 12:49:12 +01001398/* Context: netif_addr_lock held, BHs disabled. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001399static void efx_set_multicast_list(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 struct dev_mc_list *mc_list = net_dev->mc_list;
1403 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchingsa816f752008-09-01 12:49:12 +01001404 bool promiscuous = !!(net_dev->flags & IFF_PROMISC);
1405 bool changed = (efx->promiscuous != promiscuous);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001406 u32 crc;
1407 int bit;
1408 int i;
1409
Ben Hutchingsa816f752008-09-01 12:49:12 +01001410 efx->promiscuous = promiscuous;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001411
1412 /* Build multicast hash table */
1413 if (promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
1414 memset(mc_hash, 0xff, sizeof(*mc_hash));
1415 } else {
1416 memset(mc_hash, 0x00, sizeof(*mc_hash));
1417 for (i = 0; i < net_dev->mc_count; i++) {
1418 crc = ether_crc_le(ETH_ALEN, mc_list->dmi_addr);
1419 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1420 set_bit_le(bit, mc_hash->byte);
1421 mc_list = mc_list->next;
1422 }
1423 }
1424
Ben Hutchingsa816f752008-09-01 12:49:12 +01001425 if (!efx->port_enabled)
1426 /* Delay pushing settings until efx_start_port() */
1427 return;
1428
1429 if (changed)
1430 queue_work(efx->workqueue, &efx->reconfigure_work);
1431
Ben Hutchings8ceee662008-04-27 12:55:59 +01001432 /* Create and activate new global multicast hash table */
1433 falcon_set_multicast_hash(efx);
1434}
1435
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001436static const struct net_device_ops efx_netdev_ops = {
1437 .ndo_open = efx_net_open,
1438 .ndo_stop = efx_net_stop,
1439 .ndo_get_stats = efx_net_stats,
1440 .ndo_tx_timeout = efx_watchdog,
1441 .ndo_start_xmit = efx_hard_start_xmit,
1442 .ndo_validate_addr = eth_validate_addr,
1443 .ndo_do_ioctl = efx_ioctl,
1444 .ndo_change_mtu = efx_change_mtu,
1445 .ndo_set_mac_address = efx_set_mac_address,
1446 .ndo_set_multicast_list = efx_set_multicast_list,
1447#ifdef CONFIG_NET_POLL_CONTROLLER
1448 .ndo_poll_controller = efx_netpoll,
1449#endif
1450};
1451
Ben Hutchings8ceee662008-04-27 12:55:59 +01001452static int efx_netdev_event(struct notifier_block *this,
1453 unsigned long event, void *ptr)
1454{
Ben Hutchingsd3208b52008-05-16 21:20:00 +01001455 struct net_device *net_dev = ptr;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001456
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001457 if (net_dev->netdev_ops == &efx_netdev_ops && event == NETDEV_CHANGENAME) {
Ben Hutchings767e4682008-09-01 12:43:14 +01001458 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001459
1460 strcpy(efx->name, net_dev->name);
Ben Hutchingsf4150722008-11-04 20:34:28 +00001461 efx_mtd_rename(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001462 }
1463
1464 return NOTIFY_DONE;
1465}
1466
1467static struct notifier_block efx_netdev_notifier = {
1468 .notifier_call = efx_netdev_event,
1469};
1470
1471static int efx_register_netdev(struct efx_nic *efx)
1472{
1473 struct net_device *net_dev = efx->net_dev;
1474 int rc;
1475
1476 net_dev->watchdog_timeo = 5 * HZ;
1477 net_dev->irq = efx->pci_dev->irq;
Stephen Hemmingerc3ecb9f2008-11-21 17:32:54 -08001478 net_dev->netdev_ops = &efx_netdev_ops;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001479 SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
1480 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
1481
1482 /* Always start with carrier off; PHY events will detect the link */
1483 netif_carrier_off(efx->net_dev);
1484
1485 /* Clear MAC statistics */
1486 falcon_update_stats_xmac(efx);
1487 memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
1488
1489 rc = register_netdev(net_dev);
1490 if (rc) {
1491 EFX_ERR(efx, "could not register net dev\n");
1492 return rc;
1493 }
1494 strcpy(efx->name, net_dev->name);
1495
1496 return 0;
1497}
1498
1499static void efx_unregister_netdev(struct efx_nic *efx)
1500{
1501 struct efx_tx_queue *tx_queue;
1502
1503 if (!efx->net_dev)
1504 return;
1505
Ben Hutchings767e4682008-09-01 12:43:14 +01001506 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001507
1508 /* Free up any skbs still remaining. This has to happen before
1509 * we try to unregister the netdev as running their destructors
1510 * may be needed to get the device ref. count to 0. */
1511 efx_for_each_tx_queue(tx_queue, efx)
1512 efx_release_tx_buffers(tx_queue);
1513
Ben Hutchings55668612008-05-16 21:16:10 +01001514 if (efx_dev_registered(efx)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001515 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
1516 unregister_netdev(efx->net_dev);
1517 }
1518}
1519
1520/**************************************************************************
1521 *
1522 * Device reset and suspend
1523 *
1524 **************************************************************************/
1525
Ben Hutchings2467ca42008-09-01 12:48:50 +01001526/* Tears down the entire software state and most of the hardware state
1527 * before reset. */
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001528void efx_reset_down(struct efx_nic *efx, struct ethtool_cmd *ecmd)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001529{
1530 int rc;
1531
1532 EFX_ASSERT_RESET_SERIALISED(efx);
1533
Ben Hutchings2467ca42008-09-01 12:48:50 +01001534 /* The net_dev->get_stats handler is quite slow, and will fail
1535 * if a fetch is pending over reset. Serialise against it. */
1536 spin_lock(&efx->stats_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001537 efx->stats_enabled = false;
Ben Hutchings2467ca42008-09-01 12:48:50 +01001538 spin_unlock(&efx->stats_lock);
1539
1540 efx_stop_all(efx);
1541 mutex_lock(&efx->mac_lock);
Ben Hutchingsf4150722008-11-04 20:34:28 +00001542 mutex_lock(&efx->spi_lock);
Ben Hutchings2467ca42008-09-01 12:48:50 +01001543
Ben Hutchings8ceee662008-04-27 12:55:59 +01001544 rc = falcon_xmac_get_settings(efx, ecmd);
Ben Hutchings2467ca42008-09-01 12:48:50 +01001545 if (rc)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001546 EFX_ERR(efx, "could not back up PHY settings\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001547
1548 efx_fini_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001549}
1550
Ben Hutchings2467ca42008-09-01 12:48:50 +01001551/* This function will always ensure that the locks acquired in
1552 * efx_reset_down() are released. A failure return code indicates
1553 * that we were unable to reinitialise the hardware, and the
1554 * driver should be disabled. If ok is false, then the rx and tx
1555 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001556int efx_reset_up(struct efx_nic *efx, struct ethtool_cmd *ecmd, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001557{
1558 int rc;
1559
Ben Hutchings2467ca42008-09-01 12:48:50 +01001560 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001561
Ben Hutchings2467ca42008-09-01 12:48:50 +01001562 rc = falcon_init_nic(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001563 if (rc) {
Ben Hutchings2467ca42008-09-01 12:48:50 +01001564 EFX_ERR(efx, "failed to initialise NIC\n");
1565 ok = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001566 }
1567
Ben Hutchings2467ca42008-09-01 12:48:50 +01001568 if (ok) {
1569 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001570
Ben Hutchings2467ca42008-09-01 12:48:50 +01001571 if (falcon_xmac_set_settings(efx, ecmd))
1572 EFX_ERR(efx, "could not restore PHY settings\n");
1573 }
1574
Ben Hutchingsf4150722008-11-04 20:34:28 +00001575 mutex_unlock(&efx->spi_lock);
Ben Hutchings2467ca42008-09-01 12:48:50 +01001576 mutex_unlock(&efx->mac_lock);
1577
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001578 if (ok) {
Ben Hutchings2467ca42008-09-01 12:48:50 +01001579 efx_start_all(efx);
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001580 efx->stats_enabled = true;
1581 }
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
Ben Hutchings8ceee662008-04-27 12:55:59 +01001610 EFX_INFO(efx, "resetting (%d)\n", method);
1611
Ben Hutchings2467ca42008-09-01 12:48:50 +01001612 efx_reset_down(efx, &ecmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001613
1614 rc = falcon_reset_hw(efx, method);
1615 if (rc) {
1616 EFX_ERR(efx, "failed to reset hardware\n");
Ben Hutchings2467ca42008-09-01 12:48:50 +01001617 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001618 }
1619
1620 /* Allow resets to be rescheduled. */
1621 efx->reset_pending = RESET_TYPE_NONE;
1622
1623 /* Reinitialise bus-mastering, which may have been turned off before
1624 * the reset was scheduled. This is still appropriate, even in the
1625 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
1626 * can respond to requests. */
1627 pci_set_master(efx->pci_dev);
1628
Ben Hutchings8ceee662008-04-27 12:55:59 +01001629 /* Leave device stopped if necessary */
1630 if (method == RESET_TYPE_DISABLE) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001631 rc = -EIO;
Ben Hutchings2467ca42008-09-01 12:48:50 +01001632 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001633 }
1634
Ben Hutchings2467ca42008-09-01 12:48:50 +01001635 rc = efx_reset_up(efx, &ecmd, true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001636 if (rc)
Ben Hutchings2467ca42008-09-01 12:48:50 +01001637 goto disable;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001638
Ben Hutchings8ceee662008-04-27 12:55:59 +01001639 EFX_LOG(efx, "reset complete\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001640 unlock_rtnl:
1641 rtnl_unlock();
1642 return 0;
1643
Ben Hutchings2467ca42008-09-01 12:48:50 +01001644 fail:
1645 efx_reset_up(efx, &ecmd, false);
1646 disable:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001647 EFX_ERR(efx, "has been disabled\n");
1648 efx->state = STATE_DISABLED;
1649
Ben Hutchings8ceee662008-04-27 12:55:59 +01001650 rtnl_unlock();
1651 efx_unregister_netdev(efx);
1652 efx_fini_port(efx);
1653 return rc;
1654}
1655
1656/* The worker thread exists so that code that cannot sleep can
1657 * schedule a reset for later.
1658 */
1659static void efx_reset_work(struct work_struct *data)
1660{
1661 struct efx_nic *nic = container_of(data, struct efx_nic, reset_work);
1662
1663 efx_reset(nic);
1664}
1665
1666void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
1667{
1668 enum reset_type method;
1669
1670 if (efx->reset_pending != RESET_TYPE_NONE) {
1671 EFX_INFO(efx, "quenching already scheduled reset\n");
1672 return;
1673 }
1674
1675 switch (type) {
1676 case RESET_TYPE_INVISIBLE:
1677 case RESET_TYPE_ALL:
1678 case RESET_TYPE_WORLD:
1679 case RESET_TYPE_DISABLE:
1680 method = type;
1681 break;
1682 case RESET_TYPE_RX_RECOVERY:
1683 case RESET_TYPE_RX_DESC_FETCH:
1684 case RESET_TYPE_TX_DESC_FETCH:
1685 case RESET_TYPE_TX_SKIP:
1686 method = RESET_TYPE_INVISIBLE;
1687 break;
1688 default:
1689 method = RESET_TYPE_ALL;
1690 break;
1691 }
1692
1693 if (method != type)
1694 EFX_LOG(efx, "scheduling reset (%d:%d)\n", type, method);
1695 else
1696 EFX_LOG(efx, "scheduling reset (%d)\n", method);
1697
1698 efx->reset_pending = method;
1699
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001700 queue_work(efx->reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001701}
1702
1703/**************************************************************************
1704 *
1705 * List of NICs we support
1706 *
1707 **************************************************************************/
1708
1709/* PCI device ID table */
1710static struct pci_device_id efx_pci_table[] __devinitdata = {
1711 {PCI_DEVICE(EFX_VENDID_SFC, FALCON_A_P_DEVID),
1712 .driver_data = (unsigned long) &falcon_a_nic_type},
1713 {PCI_DEVICE(EFX_VENDID_SFC, FALCON_B_P_DEVID),
1714 .driver_data = (unsigned long) &falcon_b_nic_type},
1715 {0} /* end of list */
1716};
1717
1718/**************************************************************************
1719 *
1720 * Dummy PHY/MAC/Board operations
1721 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01001722 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01001723 * Needed so all function pointers are valid and do not have to be tested
1724 * before use
1725 *
1726 **************************************************************************/
1727int efx_port_dummy_op_int(struct efx_nic *efx)
1728{
1729 return 0;
1730}
1731void efx_port_dummy_op_void(struct efx_nic *efx) {}
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001732void efx_port_dummy_op_blink(struct efx_nic *efx, bool blink) {}
Ben Hutchings8ceee662008-04-27 12:55:59 +01001733
1734static struct efx_phy_operations efx_dummy_phy_operations = {
1735 .init = efx_port_dummy_op_int,
1736 .reconfigure = efx_port_dummy_op_void,
1737 .check_hw = efx_port_dummy_op_int,
1738 .fini = efx_port_dummy_op_void,
1739 .clear_interrupt = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001740};
1741
Ben Hutchings8ceee662008-04-27 12:55:59 +01001742static struct efx_board efx_dummy_board_info = {
Ben Hutchings01aad7b2008-09-01 12:48:36 +01001743 .init = efx_port_dummy_op_int,
1744 .init_leds = efx_port_dummy_op_int,
1745 .set_fault_led = efx_port_dummy_op_blink,
1746 .blink = efx_port_dummy_op_blink,
1747 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001748};
1749
1750/**************************************************************************
1751 *
1752 * Data housekeeping
1753 *
1754 **************************************************************************/
1755
1756/* This zeroes out and then fills in the invariants in a struct
1757 * efx_nic (including all sub-structures).
1758 */
1759static int efx_init_struct(struct efx_nic *efx, struct efx_nic_type *type,
1760 struct pci_dev *pci_dev, struct net_device *net_dev)
1761{
1762 struct efx_channel *channel;
1763 struct efx_tx_queue *tx_queue;
1764 struct efx_rx_queue *rx_queue;
1765 int i, rc;
1766
1767 /* Initialise common structures */
1768 memset(efx, 0, sizeof(*efx));
1769 spin_lock_init(&efx->biu_lock);
1770 spin_lock_init(&efx->phy_lock);
Ben Hutchingsf4150722008-11-04 20:34:28 +00001771 mutex_init(&efx->spi_lock);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001772 INIT_WORK(&efx->reset_work, efx_reset_work);
1773 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
1774 efx->pci_dev = pci_dev;
1775 efx->state = STATE_INIT;
1776 efx->reset_pending = RESET_TYPE_NONE;
1777 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
1778 efx->board_info = efx_dummy_board_info;
1779
1780 efx->net_dev = net_dev;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001781 efx->rx_checksum_enabled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001782 spin_lock_init(&efx->netif_stop_lock);
1783 spin_lock_init(&efx->stats_lock);
1784 mutex_init(&efx->mac_lock);
1785 efx->phy_op = &efx_dummy_phy_operations;
1786 efx->mii.dev = net_dev;
1787 INIT_WORK(&efx->reconfigure_work, efx_reconfigure_work);
1788 atomic_set(&efx->netif_stop_count, 1);
1789
1790 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
1791 channel = &efx->channel[i];
1792 channel->efx = efx;
1793 channel->channel = i;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001794 channel->work_pending = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001795 }
Ben Hutchings60ac1062008-09-01 12:44:59 +01001796 for (i = 0; i < EFX_TX_QUEUE_COUNT; i++) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001797 tx_queue = &efx->tx_queue[i];
1798 tx_queue->efx = efx;
1799 tx_queue->queue = i;
1800 tx_queue->buffer = NULL;
1801 tx_queue->channel = &efx->channel[0]; /* for safety */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001802 tx_queue->tso_headers_free = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001803 }
1804 for (i = 0; i < EFX_MAX_RX_QUEUES; i++) {
1805 rx_queue = &efx->rx_queue[i];
1806 rx_queue->efx = efx;
1807 rx_queue->queue = i;
1808 rx_queue->channel = &efx->channel[0]; /* for safety */
1809 rx_queue->buffer = NULL;
1810 spin_lock_init(&rx_queue->add_lock);
1811 INIT_DELAYED_WORK(&rx_queue->work, efx_rx_work);
1812 }
1813
1814 efx->type = type;
1815
1816 /* Sanity-check NIC type */
1817 EFX_BUG_ON_PARANOID(efx->type->txd_ring_mask &
1818 (efx->type->txd_ring_mask + 1));
1819 EFX_BUG_ON_PARANOID(efx->type->rxd_ring_mask &
1820 (efx->type->rxd_ring_mask + 1));
1821 EFX_BUG_ON_PARANOID(efx->type->evq_size &
1822 (efx->type->evq_size - 1));
1823 /* As close as we can get to guaranteeing that we don't overflow */
1824 EFX_BUG_ON_PARANOID(efx->type->evq_size <
1825 (efx->type->txd_ring_mask + 1 +
1826 efx->type->rxd_ring_mask + 1));
1827 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
1828
1829 /* Higher numbered interrupt modes are less capable! */
1830 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
1831 interrupt_mode);
1832
1833 efx->workqueue = create_singlethread_workqueue("sfc_work");
1834 if (!efx->workqueue) {
1835 rc = -ENOMEM;
1836 goto fail1;
1837 }
1838
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001839 efx->reset_workqueue = create_singlethread_workqueue("sfc_reset");
1840 if (!efx->reset_workqueue) {
1841 rc = -ENOMEM;
1842 goto fail2;
1843 }
1844
Ben Hutchings8ceee662008-04-27 12:55:59 +01001845 return 0;
1846
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001847 fail2:
1848 destroy_workqueue(efx->workqueue);
1849 efx->workqueue = NULL;
1850
Ben Hutchings8ceee662008-04-27 12:55:59 +01001851 fail1:
1852 return rc;
1853}
1854
1855static void efx_fini_struct(struct efx_nic *efx)
1856{
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001857 if (efx->reset_workqueue) {
1858 destroy_workqueue(efx->reset_workqueue);
1859 efx->reset_workqueue = NULL;
1860 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001861 if (efx->workqueue) {
1862 destroy_workqueue(efx->workqueue);
1863 efx->workqueue = NULL;
1864 }
1865}
1866
1867/**************************************************************************
1868 *
1869 * PCI interface
1870 *
1871 **************************************************************************/
1872
1873/* Main body of final NIC shutdown code
1874 * This is called only at module unload (or hotplug removal).
1875 */
1876static void efx_pci_remove_main(struct efx_nic *efx)
1877{
1878 EFX_ASSERT_RESET_SERIALISED(efx);
1879
1880 /* Skip everything if we never obtained a valid membase */
1881 if (!efx->membase)
1882 return;
1883
1884 efx_fini_channels(efx);
1885 efx_fini_port(efx);
1886
1887 /* Shutdown the board, then the NIC and board state */
Ben Hutchings37b5a602008-05-30 22:27:04 +01001888 efx->board_info.fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001889 falcon_fini_interrupt(efx);
1890
1891 efx_fini_napi(efx);
1892 efx_remove_all(efx);
1893}
1894
1895/* Final NIC shutdown
1896 * This is called only at module unload (or hotplug removal).
1897 */
1898static void efx_pci_remove(struct pci_dev *pci_dev)
1899{
1900 struct efx_nic *efx;
1901
1902 efx = pci_get_drvdata(pci_dev);
1903 if (!efx)
1904 return;
1905
Ben Hutchingsf4150722008-11-04 20:34:28 +00001906 efx_mtd_remove(efx);
1907
Ben Hutchings8ceee662008-04-27 12:55:59 +01001908 /* 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
Ben Hutchingsf4150722008-11-04 20:34:28 +00002074 efx_mtd_probe(efx); /* allowed to fail */
Ben Hutchings8ceee662008-04-27 12:55:59 +01002075 return 0;
2076
2077 fail5:
2078 efx_pci_remove_main(efx);
2079 fail4:
2080 fail3:
2081 efx_fini_io(efx);
2082 fail2:
2083 efx_fini_struct(efx);
2084 fail1:
2085 EFX_LOG(efx, "initialisation failed. rc=%d\n", rc);
2086 free_netdev(net_dev);
2087 return rc;
2088}
2089
2090static struct pci_driver efx_pci_driver = {
2091 .name = EFX_DRIVER_NAME,
2092 .id_table = efx_pci_table,
2093 .probe = efx_pci_probe,
2094 .remove = efx_pci_remove,
2095};
2096
2097/**************************************************************************
2098 *
2099 * Kernel module interface
2100 *
2101 *************************************************************************/
2102
2103module_param(interrupt_mode, uint, 0444);
2104MODULE_PARM_DESC(interrupt_mode,
2105 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2106
2107static int __init efx_init_module(void)
2108{
2109 int rc;
2110
2111 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2112
2113 rc = register_netdevice_notifier(&efx_netdev_notifier);
2114 if (rc)
2115 goto err_notifier;
2116
2117 refill_workqueue = create_workqueue("sfc_refill");
2118 if (!refill_workqueue) {
2119 rc = -ENOMEM;
2120 goto err_refill;
2121 }
2122
2123 rc = pci_register_driver(&efx_pci_driver);
2124 if (rc < 0)
2125 goto err_pci;
2126
2127 return 0;
2128
2129 err_pci:
2130 destroy_workqueue(refill_workqueue);
2131 err_refill:
2132 unregister_netdevice_notifier(&efx_netdev_notifier);
2133 err_notifier:
2134 return rc;
2135}
2136
2137static void __exit efx_exit_module(void)
2138{
2139 printk(KERN_INFO "Solarflare NET driver unloading\n");
2140
2141 pci_unregister_driver(&efx_pci_driver);
2142 destroy_workqueue(refill_workqueue);
2143 unregister_netdevice_notifier(&efx_netdev_notifier);
2144
2145}
2146
2147module_init(efx_init_module);
2148module_exit(efx_exit_module);
2149
2150MODULE_AUTHOR("Michael Brown <mbrown@fensystems.co.uk> and "
2151 "Solarflare Communications");
2152MODULE_DESCRIPTION("Solarflare Communications network driver");
2153MODULE_LICENSE("GPL");
2154MODULE_DEVICE_TABLE(pci, efx_pci_table);