blob: e1e2f806056357f77b6489441d168fd73a6c29f3 [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 Hutchings8c8661e2008-09-01 12:49:02 +0100511 if (efx->port_inhibited) {
512 netif_carrier_off(efx->net_dev);
513 return;
514 }
515
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100516 if (efx->link_up != netif_carrier_ok(efx->net_dev)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +0100517 efx->n_link_state_changes++;
518
519 if (efx->link_up)
520 netif_carrier_on(efx->net_dev);
521 else
522 netif_carrier_off(efx->net_dev);
523 }
524
525 /* Status message for kernel log */
526 if (efx->link_up) {
527 struct mii_if_info *gmii = &efx->mii;
528 unsigned adv, lpa;
529 /* NONE here means direct XAUI from the controller, with no
530 * MDIO-attached device we can query. */
531 if (efx->phy_type != PHY_TYPE_NONE) {
532 adv = gmii_advertised(gmii);
533 lpa = gmii_lpa(gmii);
534 } else {
535 lpa = GM_LPA_10000 | LPA_DUPLEX;
536 adv = lpa;
537 }
538 EFX_INFO(efx, "link up at %dMbps %s-duplex "
539 "(adv %04x lpa %04x) (MTU %d)%s\n",
540 (efx->link_options & GM_LPA_10000 ? 10000 :
541 (efx->link_options & GM_LPA_1000 ? 1000 :
542 (efx->link_options & GM_LPA_100 ? 100 :
543 10))),
544 (efx->link_options & GM_LPA_DUPLEX ?
545 "full" : "half"),
546 adv, lpa,
547 efx->net_dev->mtu,
548 (efx->promiscuous ? " [PROMISC]" : ""));
549 } else {
550 EFX_INFO(efx, "link down\n");
551 }
552
553}
554
555/* This call reinitialises the MAC to pick up new PHY settings. The
556 * caller must hold the mac_lock */
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100557void __efx_reconfigure_port(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100558{
559 WARN_ON(!mutex_is_locked(&efx->mac_lock));
560
561 EFX_LOG(efx, "reconfiguring MAC from PHY settings on CPU %d\n",
562 raw_smp_processor_id());
563
564 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 {
610 DECLARE_MAC_BUF(mac);
611
612 EFX_ERR(efx, "invalid MAC address %s\n",
613 print_mac(mac, efx->mac_address));
614 if (!allow_bad_hwaddr) {
615 rc = -EINVAL;
616 goto err;
617 }
618 random_ether_addr(efx->net_dev->dev_addr);
619 EFX_INFO(efx, "using locally-generated MAC %s\n",
620 print_mac(mac, efx->net_dev->dev_addr));
621 }
622
623 return 0;
624
625 err:
626 efx_remove_port(efx);
627 return rc;
628}
629
630static int efx_init_port(struct efx_nic *efx)
631{
632 int rc;
633
634 EFX_LOG(efx, "init port\n");
635
636 /* Initialise the MAC and PHY */
637 rc = falcon_init_xmac(efx);
638 if (rc)
639 return rc;
640
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100641 efx->port_initialized = true;
Ben Hutchings8c8661e2008-09-01 12:49:02 +0100642 efx->stats_enabled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100643
644 /* Reconfigure port to program MAC registers */
645 falcon_reconfigure_xmac(efx);
646
647 return 0;
648}
649
650/* Allow efx_reconfigure_port() to be scheduled, and close the window
651 * between efx_stop_port and efx_flush_all whereby a previously scheduled
652 * efx_reconfigure_port() may have been cancelled */
653static void efx_start_port(struct efx_nic *efx)
654{
655 EFX_LOG(efx, "start port\n");
656 BUG_ON(efx->port_enabled);
657
658 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100659 efx->port_enabled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100660 __efx_reconfigure_port(efx);
661 mutex_unlock(&efx->mac_lock);
662}
663
664/* Prevent efx_reconfigure_work and efx_monitor() from executing, and
665 * efx_set_multicast_list() from scheduling efx_reconfigure_work.
666 * efx_reconfigure_work can still be scheduled via NAPI processing
667 * until efx_flush_all() is called */
668static void efx_stop_port(struct efx_nic *efx)
669{
670 EFX_LOG(efx, "stop port\n");
671
672 mutex_lock(&efx->mac_lock);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100673 efx->port_enabled = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100674 mutex_unlock(&efx->mac_lock);
675
676 /* Serialise against efx_set_multicast_list() */
Ben Hutchings55668612008-05-16 21:16:10 +0100677 if (efx_dev_registered(efx)) {
David S. Millerb9e40852008-07-15 00:15:08 -0700678 netif_addr_lock_bh(efx->net_dev);
679 netif_addr_unlock_bh(efx->net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100680 }
681}
682
683static void efx_fini_port(struct efx_nic *efx)
684{
685 EFX_LOG(efx, "shut down port\n");
686
687 if (!efx->port_initialized)
688 return;
689
690 falcon_fini_xmac(efx);
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100691 efx->port_initialized = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100692
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +0100693 efx->link_up = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100694 efx_link_status_changed(efx);
695}
696
697static void efx_remove_port(struct efx_nic *efx)
698{
699 EFX_LOG(efx, "destroying port\n");
700
701 falcon_remove_port(efx);
702}
703
704/**************************************************************************
705 *
706 * NIC handling
707 *
708 **************************************************************************/
709
710/* This configures the PCI device to enable I/O and DMA. */
711static int efx_init_io(struct efx_nic *efx)
712{
713 struct pci_dev *pci_dev = efx->pci_dev;
714 dma_addr_t dma_mask = efx->type->max_dma_mask;
715 int rc;
716
717 EFX_LOG(efx, "initialising I/O\n");
718
719 rc = pci_enable_device(pci_dev);
720 if (rc) {
721 EFX_ERR(efx, "failed to enable PCI device\n");
722 goto fail1;
723 }
724
725 pci_set_master(pci_dev);
726
727 /* Set the PCI DMA mask. Try all possibilities from our
728 * genuine mask down to 32 bits, because some architectures
729 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
730 * masks event though they reject 46 bit masks.
731 */
732 while (dma_mask > 0x7fffffffUL) {
733 if (pci_dma_supported(pci_dev, dma_mask) &&
734 ((rc = pci_set_dma_mask(pci_dev, dma_mask)) == 0))
735 break;
736 dma_mask >>= 1;
737 }
738 if (rc) {
739 EFX_ERR(efx, "could not find a suitable DMA mask\n");
740 goto fail2;
741 }
742 EFX_LOG(efx, "using DMA mask %llx\n", (unsigned long long) dma_mask);
743 rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
744 if (rc) {
745 /* pci_set_consistent_dma_mask() is not *allowed* to
746 * fail with a mask that pci_set_dma_mask() accepted,
747 * but just in case...
748 */
749 EFX_ERR(efx, "failed to set consistent DMA mask\n");
750 goto fail2;
751 }
752
753 efx->membase_phys = pci_resource_start(efx->pci_dev,
754 efx->type->mem_bar);
755 rc = pci_request_region(pci_dev, efx->type->mem_bar, "sfc");
756 if (rc) {
757 EFX_ERR(efx, "request for memory BAR failed\n");
758 rc = -EIO;
759 goto fail3;
760 }
761 efx->membase = ioremap_nocache(efx->membase_phys,
762 efx->type->mem_map_size);
763 if (!efx->membase) {
Ben Hutchings086ea352008-05-16 21:17:06 +0100764 EFX_ERR(efx, "could not map memory BAR %d at %llx+%x\n",
765 efx->type->mem_bar,
766 (unsigned long long)efx->membase_phys,
Ben Hutchings8ceee662008-04-27 12:55:59 +0100767 efx->type->mem_map_size);
768 rc = -ENOMEM;
769 goto fail4;
770 }
Ben Hutchings086ea352008-05-16 21:17:06 +0100771 EFX_LOG(efx, "memory BAR %u at %llx+%x (virtual %p)\n",
772 efx->type->mem_bar, (unsigned long long)efx->membase_phys,
773 efx->type->mem_map_size, efx->membase);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100774
775 return 0;
776
777 fail4:
778 release_mem_region(efx->membase_phys, efx->type->mem_map_size);
779 fail3:
Ben Hutchings2c118e02008-05-16 21:15:29 +0100780 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100781 fail2:
782 pci_disable_device(efx->pci_dev);
783 fail1:
784 return rc;
785}
786
787static void efx_fini_io(struct efx_nic *efx)
788{
789 EFX_LOG(efx, "shutting down I/O\n");
790
791 if (efx->membase) {
792 iounmap(efx->membase);
793 efx->membase = NULL;
794 }
795
796 if (efx->membase_phys) {
797 pci_release_region(efx->pci_dev, efx->type->mem_bar);
Ben Hutchings2c118e02008-05-16 21:15:29 +0100798 efx->membase_phys = 0;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100799 }
800
801 pci_disable_device(efx->pci_dev);
802}
803
Ben Hutchings46123d02008-09-01 12:47:33 +0100804/* Get number of RX queues wanted. Return number of online CPU
805 * packages in the expectation that an IRQ balancer will spread
806 * interrupts across them. */
807static int efx_wanted_rx_queues(void)
808{
809 cpumask_t core_mask;
810 int count;
811 int cpu;
812
813 cpus_clear(core_mask);
814 count = 0;
815 for_each_online_cpu(cpu) {
816 if (!cpu_isset(cpu, core_mask)) {
817 ++count;
818 cpus_or(core_mask, core_mask,
819 topology_core_siblings(cpu));
820 }
821 }
822
823 return count;
824}
825
826/* Probe the number and type of interrupts we are able to obtain, and
827 * the resulting numbers of channels and RX queues.
828 */
Ben Hutchings8ceee662008-04-27 12:55:59 +0100829static void efx_probe_interrupts(struct efx_nic *efx)
830{
Ben Hutchings46123d02008-09-01 12:47:33 +0100831 int max_channels =
832 min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100833 int rc, i;
834
835 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
Ben Hutchings46123d02008-09-01 12:47:33 +0100836 struct msix_entry xentries[EFX_MAX_CHANNELS];
837 int wanted_ints;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100838
Ben Hutchings46123d02008-09-01 12:47:33 +0100839 /* We want one RX queue and interrupt per CPU package
840 * (or as specified by the rss_cpus module parameter).
841 * We will need one channel per interrupt.
842 */
843 wanted_ints = rss_cpus ? rss_cpus : efx_wanted_rx_queues();
Ben Hutchings8831da72008-09-01 12:47:48 +0100844 efx->n_rx_queues = min(wanted_ints, max_channels);
Ben Hutchingsaa6ef272008-07-18 19:03:10 +0100845
Ben Hutchings8831da72008-09-01 12:47:48 +0100846 for (i = 0; i < efx->n_rx_queues; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100847 xentries[i].entry = i;
Ben Hutchings8831da72008-09-01 12:47:48 +0100848 rc = pci_enable_msix(efx->pci_dev, xentries, efx->n_rx_queues);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100849 if (rc > 0) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100850 EFX_BUG_ON_PARANOID(rc >= efx->n_rx_queues);
851 efx->n_rx_queues = rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100852 rc = pci_enable_msix(efx->pci_dev, xentries,
Ben Hutchings8831da72008-09-01 12:47:48 +0100853 efx->n_rx_queues);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100854 }
855
856 if (rc == 0) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100857 for (i = 0; i < efx->n_rx_queues; i++)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100858 efx->channel[i].irq = xentries[i].vector;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100859 } else {
860 /* Fall back to single channel MSI */
861 efx->interrupt_mode = EFX_INT_MODE_MSI;
862 EFX_ERR(efx, "could not enable MSI-X\n");
863 }
864 }
865
866 /* Try single interrupt MSI */
867 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100868 efx->n_rx_queues = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100869 rc = pci_enable_msi(efx->pci_dev);
870 if (rc == 0) {
871 efx->channel[0].irq = efx->pci_dev->irq;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100872 } else {
873 EFX_ERR(efx, "could not enable MSI\n");
874 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
875 }
876 }
877
878 /* Assume legacy interrupts */
879 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
Ben Hutchings8831da72008-09-01 12:47:48 +0100880 efx->n_rx_queues = 1;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100881 efx->legacy_irq = efx->pci_dev->irq;
882 }
883}
884
885static void efx_remove_interrupts(struct efx_nic *efx)
886{
887 struct efx_channel *channel;
888
889 /* Remove MSI/MSI-X interrupts */
Ben Hutchings64ee3122008-09-01 12:47:38 +0100890 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100891 channel->irq = 0;
892 pci_disable_msi(efx->pci_dev);
893 pci_disable_msix(efx->pci_dev);
894
895 /* Remove legacy interrupt */
896 efx->legacy_irq = 0;
897}
898
Ben Hutchings8831da72008-09-01 12:47:48 +0100899static void efx_set_channels(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100900{
901 struct efx_tx_queue *tx_queue;
902 struct efx_rx_queue *rx_queue;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100903
Ben Hutchings60ac1062008-09-01 12:44:59 +0100904 efx_for_each_tx_queue(tx_queue, efx) {
905 if (!EFX_INT_MODE_USE_MSI(efx) && separate_tx_and_rx_channels)
906 tx_queue->channel = &efx->channel[1];
907 else
908 tx_queue->channel = &efx->channel[0];
909 tx_queue->channel->used_flags |= EFX_USED_BY_TX;
910 }
Ben Hutchings8ceee662008-04-27 12:55:59 +0100911
Ben Hutchings8831da72008-09-01 12:47:48 +0100912 efx_for_each_rx_queue(rx_queue, efx) {
913 rx_queue->channel = &efx->channel[rx_queue->queue];
914 rx_queue->channel->used_flags |= EFX_USED_BY_RX;
Ben Hutchings8ceee662008-04-27 12:55:59 +0100915 }
916}
917
918static int efx_probe_nic(struct efx_nic *efx)
919{
920 int rc;
921
922 EFX_LOG(efx, "creating NIC\n");
923
924 /* Carry out hardware-type specific initialisation */
925 rc = falcon_probe_nic(efx);
926 if (rc)
927 return rc;
928
929 /* Determine the number of channels and RX queues by trying to hook
930 * in MSI-X interrupts. */
931 efx_probe_interrupts(efx);
932
Ben Hutchings8831da72008-09-01 12:47:48 +0100933 efx_set_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +0100934
935 /* Initialise the interrupt moderation settings */
936 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec);
937
938 return 0;
939}
940
941static void efx_remove_nic(struct efx_nic *efx)
942{
943 EFX_LOG(efx, "destroying NIC\n");
944
945 efx_remove_interrupts(efx);
946 falcon_remove_nic(efx);
947}
948
949/**************************************************************************
950 *
951 * NIC startup/shutdown
952 *
953 *************************************************************************/
954
955static int efx_probe_all(struct efx_nic *efx)
956{
957 struct efx_channel *channel;
958 int rc;
959
960 /* Create NIC */
961 rc = efx_probe_nic(efx);
962 if (rc) {
963 EFX_ERR(efx, "failed to create NIC\n");
964 goto fail1;
965 }
966
967 /* Create port */
968 rc = efx_probe_port(efx);
969 if (rc) {
970 EFX_ERR(efx, "failed to create port\n");
971 goto fail2;
972 }
973
974 /* Create channels */
975 efx_for_each_channel(channel, efx) {
976 rc = efx_probe_channel(channel);
977 if (rc) {
978 EFX_ERR(efx, "failed to create channel %d\n",
979 channel->channel);
980 goto fail3;
981 }
982 }
983
984 return 0;
985
986 fail3:
987 efx_for_each_channel(channel, efx)
988 efx_remove_channel(channel);
989 efx_remove_port(efx);
990 fail2:
991 efx_remove_nic(efx);
992 fail1:
993 return rc;
994}
995
996/* Called after previous invocation(s) of efx_stop_all, restarts the
997 * port, kernel transmit queue, NAPI processing and hardware interrupts,
998 * and ensures that the port is scheduled to be reconfigured.
999 * This function is safe to call multiple times when the NIC is in any
1000 * state. */
1001static void efx_start_all(struct efx_nic *efx)
1002{
1003 struct efx_channel *channel;
1004
1005 EFX_ASSERT_RESET_SERIALISED(efx);
1006
1007 /* Check that it is appropriate to restart the interface. All
1008 * of these flags are safe to read under just the rtnl lock */
1009 if (efx->port_enabled)
1010 return;
1011 if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1012 return;
Ben Hutchings55668612008-05-16 21:16:10 +01001013 if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
Ben Hutchings8ceee662008-04-27 12:55:59 +01001014 return;
1015
1016 /* Mark the port as enabled so port reconfigurations can start, then
1017 * restart the transmit interface early so the watchdog timer stops */
1018 efx_start_port(efx);
Steve Hodgsondacccc72008-09-01 12:48:20 +01001019 if (efx_dev_registered(efx))
1020 efx_wake_queue(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001021
1022 efx_for_each_channel(channel, efx)
1023 efx_start_channel(channel);
1024
1025 falcon_enable_interrupts(efx);
1026
1027 /* Start hardware monitor if we're in RUNNING */
1028 if (efx->state == STATE_RUNNING)
1029 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1030 efx_monitor_interval);
1031}
1032
1033/* Flush all delayed work. Should only be called when no more delayed work
1034 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1035 * since we're holding the rtnl_lock at this point. */
1036static void efx_flush_all(struct efx_nic *efx)
1037{
1038 struct efx_rx_queue *rx_queue;
1039
1040 /* Make sure the hardware monitor is stopped */
1041 cancel_delayed_work_sync(&efx->monitor_work);
1042
1043 /* Ensure that all RX slow refills are complete. */
Ben Hutchingsb3475642008-05-16 21:15:49 +01001044 efx_for_each_rx_queue(rx_queue, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001045 cancel_delayed_work_sync(&rx_queue->work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001046
1047 /* Stop scheduled port reconfigurations */
1048 cancel_work_sync(&efx->reconfigure_work);
1049
1050}
1051
1052/* Quiesce hardware and software without bringing the link down.
1053 * Safe to call multiple times, when the nic and interface is in any
1054 * state. The caller is guaranteed to subsequently be in a position
1055 * to modify any hardware and software state they see fit without
1056 * taking locks. */
1057static void efx_stop_all(struct efx_nic *efx)
1058{
1059 struct efx_channel *channel;
1060
1061 EFX_ASSERT_RESET_SERIALISED(efx);
1062
1063 /* port_enabled can be read safely under the rtnl lock */
1064 if (!efx->port_enabled)
1065 return;
1066
1067 /* Disable interrupts and wait for ISR to complete */
1068 falcon_disable_interrupts(efx);
1069 if (efx->legacy_irq)
1070 synchronize_irq(efx->legacy_irq);
Ben Hutchings64ee3122008-09-01 12:47:38 +01001071 efx_for_each_channel(channel, efx) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001072 if (channel->irq)
1073 synchronize_irq(channel->irq);
Ben Hutchingsb3475642008-05-16 21:15:49 +01001074 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001075
1076 /* Stop all NAPI processing and synchronous rx refills */
1077 efx_for_each_channel(channel, efx)
1078 efx_stop_channel(channel);
1079
1080 /* Stop all asynchronous port reconfigurations. Since all
1081 * event processing has already been stopped, there is no
1082 * window to loose phy events */
1083 efx_stop_port(efx);
1084
1085 /* Flush reconfigure_work, refill_workqueue, monitor_work */
1086 efx_flush_all(efx);
1087
1088 /* Isolate the MAC from the TX and RX engines, so that queue
1089 * flushes will complete in a timely fashion. */
1090 falcon_deconfigure_mac_wrapper(efx);
1091 falcon_drain_tx_fifo(efx);
1092
1093 /* Stop the kernel transmit interface late, so the watchdog
1094 * timer isn't ticking over the flush */
Ben Hutchings55668612008-05-16 21:16:10 +01001095 if (efx_dev_registered(efx)) {
Steve Hodgsondacccc72008-09-01 12:48:20 +01001096 efx_stop_queue(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001097 netif_tx_lock_bh(efx->net_dev);
1098 netif_tx_unlock_bh(efx->net_dev);
1099 }
1100}
1101
1102static void efx_remove_all(struct efx_nic *efx)
1103{
1104 struct efx_channel *channel;
1105
1106 efx_for_each_channel(channel, efx)
1107 efx_remove_channel(channel);
1108 efx_remove_port(efx);
1109 efx_remove_nic(efx);
1110}
1111
1112/* A convinience function to safely flush all the queues */
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001113void efx_flush_queues(struct efx_nic *efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001114{
Ben Hutchings8ceee662008-04-27 12:55:59 +01001115 EFX_ASSERT_RESET_SERIALISED(efx);
1116
1117 efx_stop_all(efx);
1118
1119 efx_fini_channels(efx);
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001120 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001121
1122 efx_start_all(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001123}
1124
1125/**************************************************************************
1126 *
1127 * Interrupt moderation
1128 *
1129 **************************************************************************/
1130
1131/* Set interrupt moderation parameters */
1132void efx_init_irq_moderation(struct efx_nic *efx, int tx_usecs, int rx_usecs)
1133{
1134 struct efx_tx_queue *tx_queue;
1135 struct efx_rx_queue *rx_queue;
1136
1137 EFX_ASSERT_RESET_SERIALISED(efx);
1138
1139 efx_for_each_tx_queue(tx_queue, efx)
1140 tx_queue->channel->irq_moderation = tx_usecs;
1141
1142 efx_for_each_rx_queue(rx_queue, efx)
1143 rx_queue->channel->irq_moderation = rx_usecs;
1144}
1145
1146/**************************************************************************
1147 *
1148 * Hardware monitor
1149 *
1150 **************************************************************************/
1151
1152/* Run periodically off the general workqueue. Serialised against
1153 * efx_reconfigure_port via the mac_lock */
1154static void efx_monitor(struct work_struct *data)
1155{
1156 struct efx_nic *efx = container_of(data, struct efx_nic,
1157 monitor_work.work);
1158 int rc = 0;
1159
1160 EFX_TRACE(efx, "hardware monitor executing on CPU %d\n",
1161 raw_smp_processor_id());
1162
1163
1164 /* If the mac_lock is already held then it is likely a port
1165 * reconfiguration is already in place, which will likely do
1166 * most of the work of check_hw() anyway. */
1167 if (!mutex_trylock(&efx->mac_lock)) {
1168 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1169 efx_monitor_interval);
1170 return;
1171 }
1172
1173 if (efx->port_enabled)
1174 rc = falcon_check_xmac(efx);
1175 mutex_unlock(&efx->mac_lock);
1176
1177 if (rc) {
1178 if (monitor_reset) {
1179 EFX_ERR(efx, "hardware monitor detected a fault: "
1180 "triggering reset\n");
1181 efx_schedule_reset(efx, RESET_TYPE_MONITOR);
1182 } else {
1183 EFX_ERR(efx, "hardware monitor detected a fault, "
1184 "skipping reset\n");
1185 }
1186 }
1187
1188 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1189 efx_monitor_interval);
1190}
1191
1192/**************************************************************************
1193 *
1194 * ioctls
1195 *
1196 *************************************************************************/
1197
1198/* Net device ioctl
1199 * Context: process, rtnl_lock() held.
1200 */
1201static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1202{
Ben Hutchings767e4682008-09-01 12:43:14 +01001203 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001204
1205 EFX_ASSERT_RESET_SERIALISED(efx);
1206
1207 return generic_mii_ioctl(&efx->mii, if_mii(ifr), cmd, NULL);
1208}
1209
1210/**************************************************************************
1211 *
1212 * NAPI interface
1213 *
1214 **************************************************************************/
1215
1216static int efx_init_napi(struct efx_nic *efx)
1217{
1218 struct efx_channel *channel;
1219 int rc;
1220
1221 efx_for_each_channel(channel, efx) {
1222 channel->napi_dev = efx->net_dev;
1223 rc = efx_lro_init(&channel->lro_mgr, efx);
1224 if (rc)
1225 goto err;
1226 }
1227 return 0;
1228 err:
1229 efx_fini_napi(efx);
1230 return rc;
1231}
1232
1233static void efx_fini_napi(struct efx_nic *efx)
1234{
1235 struct efx_channel *channel;
1236
1237 efx_for_each_channel(channel, efx) {
1238 efx_lro_fini(&channel->lro_mgr);
1239 channel->napi_dev = NULL;
1240 }
1241}
1242
1243/**************************************************************************
1244 *
1245 * Kernel netpoll interface
1246 *
1247 *************************************************************************/
1248
1249#ifdef CONFIG_NET_POLL_CONTROLLER
1250
1251/* Although in the common case interrupts will be disabled, this is not
1252 * guaranteed. However, all our work happens inside the NAPI callback,
1253 * so no locking is required.
1254 */
1255static void efx_netpoll(struct net_device *net_dev)
1256{
Ben Hutchings767e4682008-09-01 12:43:14 +01001257 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001258 struct efx_channel *channel;
1259
Ben Hutchings64ee3122008-09-01 12:47:38 +01001260 efx_for_each_channel(channel, efx)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001261 efx_schedule_channel(channel);
1262}
1263
1264#endif
1265
1266/**************************************************************************
1267 *
1268 * Kernel net device interface
1269 *
1270 *************************************************************************/
1271
1272/* Context: process, rtnl_lock() held. */
1273static int efx_net_open(struct net_device *net_dev)
1274{
Ben Hutchings767e4682008-09-01 12:43:14 +01001275 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001276 EFX_ASSERT_RESET_SERIALISED(efx);
1277
1278 EFX_LOG(efx, "opening device %s on CPU %d\n", net_dev->name,
1279 raw_smp_processor_id());
1280
Ben Hutchingsf8b87c12008-09-01 12:48:17 +01001281 if (efx->phy_mode & PHY_MODE_SPECIAL)
1282 return -EBUSY;
1283
Ben Hutchings8ceee662008-04-27 12:55:59 +01001284 efx_start_all(efx);
1285 return 0;
1286}
1287
1288/* Context: process, rtnl_lock() held.
1289 * Note that the kernel will ignore our return code; this method
1290 * should really be a void.
1291 */
1292static int efx_net_stop(struct net_device *net_dev)
1293{
Ben Hutchings767e4682008-09-01 12:43:14 +01001294 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001295
1296 EFX_LOG(efx, "closing %s on CPU %d\n", net_dev->name,
1297 raw_smp_processor_id());
1298
1299 /* Stop the device and flush all the channels */
1300 efx_stop_all(efx);
1301 efx_fini_channels(efx);
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001302 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001303
1304 return 0;
1305}
1306
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001307/* Context: process, dev_base_lock or RTNL held, non-blocking. */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001308static struct net_device_stats *efx_net_stats(struct net_device *net_dev)
1309{
Ben Hutchings767e4682008-09-01 12:43:14 +01001310 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001311 struct efx_mac_stats *mac_stats = &efx->mac_stats;
1312 struct net_device_stats *stats = &net_dev->stats;
1313
Ben Hutchings5b9e2072008-05-16 21:18:14 +01001314 /* Update stats if possible, but do not wait if another thread
1315 * is updating them (or resetting the NIC); slightly stale
1316 * stats are acceptable.
1317 */
Ben Hutchings8ceee662008-04-27 12:55:59 +01001318 if (!spin_trylock(&efx->stats_lock))
1319 return stats;
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001320 if (efx->stats_enabled) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001321 falcon_update_stats_xmac(efx);
1322 falcon_update_nic_stats(efx);
1323 }
1324 spin_unlock(&efx->stats_lock);
1325
1326 stats->rx_packets = mac_stats->rx_packets;
1327 stats->tx_packets = mac_stats->tx_packets;
1328 stats->rx_bytes = mac_stats->rx_bytes;
1329 stats->tx_bytes = mac_stats->tx_bytes;
1330 stats->multicast = mac_stats->rx_multicast;
1331 stats->collisions = mac_stats->tx_collision;
1332 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1333 mac_stats->rx_length_error);
1334 stats->rx_over_errors = efx->n_rx_nodesc_drop_cnt;
1335 stats->rx_crc_errors = mac_stats->rx_bad;
1336 stats->rx_frame_errors = mac_stats->rx_align_error;
1337 stats->rx_fifo_errors = mac_stats->rx_overflow;
1338 stats->rx_missed_errors = mac_stats->rx_missed;
1339 stats->tx_window_errors = mac_stats->tx_late_collision;
1340
1341 stats->rx_errors = (stats->rx_length_errors +
1342 stats->rx_over_errors +
1343 stats->rx_crc_errors +
1344 stats->rx_frame_errors +
1345 stats->rx_fifo_errors +
1346 stats->rx_missed_errors +
1347 mac_stats->rx_symbol_error);
1348 stats->tx_errors = (stats->tx_window_errors +
1349 mac_stats->tx_bad);
1350
1351 return stats;
1352}
1353
1354/* Context: netif_tx_lock held, BHs disabled. */
1355static void efx_watchdog(struct net_device *net_dev)
1356{
Ben Hutchings767e4682008-09-01 12:43:14 +01001357 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001358
1359 EFX_ERR(efx, "TX stuck with stop_count=%d port_enabled=%d: %s\n",
1360 atomic_read(&efx->netif_stop_count), efx->port_enabled,
1361 monitor_reset ? "resetting channels" : "skipping reset");
1362
1363 if (monitor_reset)
1364 efx_schedule_reset(efx, RESET_TYPE_MONITOR);
1365}
1366
1367
1368/* Context: process, rtnl_lock() held. */
1369static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1370{
Ben Hutchings767e4682008-09-01 12:43:14 +01001371 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001372 int rc = 0;
1373
1374 EFX_ASSERT_RESET_SERIALISED(efx);
1375
1376 if (new_mtu > EFX_MAX_MTU)
1377 return -EINVAL;
1378
1379 efx_stop_all(efx);
1380
1381 EFX_LOG(efx, "changing MTU to %d\n", new_mtu);
1382
1383 efx_fini_channels(efx);
1384 net_dev->mtu = new_mtu;
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001385 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001386
1387 efx_start_all(efx);
1388 return rc;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001389}
1390
1391static int efx_set_mac_address(struct net_device *net_dev, void *data)
1392{
Ben Hutchings767e4682008-09-01 12:43:14 +01001393 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001394 struct sockaddr *addr = data;
1395 char *new_addr = addr->sa_data;
1396
1397 EFX_ASSERT_RESET_SERIALISED(efx);
1398
1399 if (!is_valid_ether_addr(new_addr)) {
1400 DECLARE_MAC_BUF(mac);
1401 EFX_ERR(efx, "invalid ethernet MAC address requested: %s\n",
1402 print_mac(mac, new_addr));
1403 return -EINVAL;
1404 }
1405
1406 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1407
1408 /* Reconfigure the MAC */
1409 efx_reconfigure_port(efx);
1410
1411 return 0;
1412}
1413
1414/* Context: netif_tx_lock held, BHs disabled. */
1415static void efx_set_multicast_list(struct net_device *net_dev)
1416{
Ben Hutchings767e4682008-09-01 12:43:14 +01001417 struct efx_nic *efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001418 struct dev_mc_list *mc_list = net_dev->mc_list;
1419 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001420 bool promiscuous;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001421 u32 crc;
1422 int bit;
1423 int i;
1424
1425 /* Set per-MAC promiscuity flag and reconfigure MAC if necessary */
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001426 promiscuous = !!(net_dev->flags & IFF_PROMISC);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001427 if (efx->promiscuous != promiscuous) {
1428 efx->promiscuous = promiscuous;
1429 /* Close the window between efx_stop_port() and efx_flush_all()
1430 * by only queuing work when the port is enabled. */
1431 if (efx->port_enabled)
1432 queue_work(efx->workqueue, &efx->reconfigure_work);
1433 }
1434
1435 /* Build multicast hash table */
1436 if (promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
1437 memset(mc_hash, 0xff, sizeof(*mc_hash));
1438 } else {
1439 memset(mc_hash, 0x00, sizeof(*mc_hash));
1440 for (i = 0; i < net_dev->mc_count; i++) {
1441 crc = ether_crc_le(ETH_ALEN, mc_list->dmi_addr);
1442 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1443 set_bit_le(bit, mc_hash->byte);
1444 mc_list = mc_list->next;
1445 }
1446 }
1447
1448 /* Create and activate new global multicast hash table */
1449 falcon_set_multicast_hash(efx);
1450}
1451
1452static 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
1457 if (net_dev->open == efx_net_open && 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);
1461 }
1462
1463 return NOTIFY_DONE;
1464}
1465
1466static struct notifier_block efx_netdev_notifier = {
1467 .notifier_call = efx_netdev_event,
1468};
1469
1470static int efx_register_netdev(struct efx_nic *efx)
1471{
1472 struct net_device *net_dev = efx->net_dev;
1473 int rc;
1474
1475 net_dev->watchdog_timeo = 5 * HZ;
1476 net_dev->irq = efx->pci_dev->irq;
1477 net_dev->open = efx_net_open;
1478 net_dev->stop = efx_net_stop;
1479 net_dev->get_stats = efx_net_stats;
1480 net_dev->tx_timeout = &efx_watchdog;
1481 net_dev->hard_start_xmit = efx_hard_start_xmit;
1482 net_dev->do_ioctl = efx_ioctl;
1483 net_dev->change_mtu = efx_change_mtu;
1484 net_dev->set_mac_address = efx_set_mac_address;
1485 net_dev->set_multicast_list = efx_set_multicast_list;
1486#ifdef CONFIG_NET_POLL_CONTROLLER
1487 net_dev->poll_controller = efx_netpoll;
1488#endif
1489 SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
1490 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
1491
1492 /* Always start with carrier off; PHY events will detect the link */
1493 netif_carrier_off(efx->net_dev);
1494
1495 /* Clear MAC statistics */
1496 falcon_update_stats_xmac(efx);
1497 memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
1498
1499 rc = register_netdev(net_dev);
1500 if (rc) {
1501 EFX_ERR(efx, "could not register net dev\n");
1502 return rc;
1503 }
1504 strcpy(efx->name, net_dev->name);
1505
1506 return 0;
1507}
1508
1509static void efx_unregister_netdev(struct efx_nic *efx)
1510{
1511 struct efx_tx_queue *tx_queue;
1512
1513 if (!efx->net_dev)
1514 return;
1515
Ben Hutchings767e4682008-09-01 12:43:14 +01001516 BUG_ON(netdev_priv(efx->net_dev) != efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001517
1518 /* Free up any skbs still remaining. This has to happen before
1519 * we try to unregister the netdev as running their destructors
1520 * may be needed to get the device ref. count to 0. */
1521 efx_for_each_tx_queue(tx_queue, efx)
1522 efx_release_tx_buffers(tx_queue);
1523
Ben Hutchings55668612008-05-16 21:16:10 +01001524 if (efx_dev_registered(efx)) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001525 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
1526 unregister_netdev(efx->net_dev);
1527 }
1528}
1529
1530/**************************************************************************
1531 *
1532 * Device reset and suspend
1533 *
1534 **************************************************************************/
1535
Ben Hutchings2467ca42008-09-01 12:48:50 +01001536/* Tears down the entire software state and most of the hardware state
1537 * before reset. */
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001538void efx_reset_down(struct efx_nic *efx, struct ethtool_cmd *ecmd)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001539{
1540 int rc;
1541
1542 EFX_ASSERT_RESET_SERIALISED(efx);
1543
Ben Hutchings2467ca42008-09-01 12:48:50 +01001544 /* The net_dev->get_stats handler is quite slow, and will fail
1545 * if a fetch is pending over reset. Serialise against it. */
1546 spin_lock(&efx->stats_lock);
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001547 efx->stats_enabled = false;
Ben Hutchings2467ca42008-09-01 12:48:50 +01001548 spin_unlock(&efx->stats_lock);
1549
1550 efx_stop_all(efx);
1551 mutex_lock(&efx->mac_lock);
1552
Ben Hutchings8ceee662008-04-27 12:55:59 +01001553 rc = falcon_xmac_get_settings(efx, ecmd);
Ben Hutchings2467ca42008-09-01 12:48:50 +01001554 if (rc)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001555 EFX_ERR(efx, "could not back up PHY settings\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001556
1557 efx_fini_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001558}
1559
Ben Hutchings2467ca42008-09-01 12:48:50 +01001560/* This function will always ensure that the locks acquired in
1561 * efx_reset_down() are released. A failure return code indicates
1562 * that we were unable to reinitialise the hardware, and the
1563 * driver should be disabled. If ok is false, then the rx and tx
1564 * engines are not restarted, pending a RESET_DISABLE. */
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001565int efx_reset_up(struct efx_nic *efx, struct ethtool_cmd *ecmd, bool ok)
Ben Hutchings8ceee662008-04-27 12:55:59 +01001566{
1567 int rc;
1568
Ben Hutchings2467ca42008-09-01 12:48:50 +01001569 EFX_ASSERT_RESET_SERIALISED(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001570
Ben Hutchings2467ca42008-09-01 12:48:50 +01001571 rc = falcon_init_nic(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001572 if (rc) {
Ben Hutchings2467ca42008-09-01 12:48:50 +01001573 EFX_ERR(efx, "failed to initialise NIC\n");
1574 ok = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001575 }
1576
Ben Hutchings2467ca42008-09-01 12:48:50 +01001577 if (ok) {
1578 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001579
Ben Hutchings2467ca42008-09-01 12:48:50 +01001580 if (falcon_xmac_set_settings(efx, ecmd))
1581 EFX_ERR(efx, "could not restore PHY settings\n");
1582 }
1583
1584 mutex_unlock(&efx->mac_lock);
1585
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001586 if (ok) {
Ben Hutchings2467ca42008-09-01 12:48:50 +01001587 efx_start_all(efx);
Ben Hutchings8c8661e2008-09-01 12:49:02 +01001588 efx->stats_enabled = true;
1589 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001590 return rc;
1591}
1592
1593/* Reset the NIC as transparently as possible. Do not reset the PHY
1594 * Note that the reset may fail, in which case the card will be left
1595 * in a most-probably-unusable state.
1596 *
1597 * This function will sleep. You cannot reset from within an atomic
1598 * state; use efx_schedule_reset() instead.
1599 *
1600 * Grabs the rtnl_lock.
1601 */
1602static int efx_reset(struct efx_nic *efx)
1603{
1604 struct ethtool_cmd ecmd;
1605 enum reset_type method = efx->reset_pending;
1606 int rc;
1607
1608 /* Serialise with kernel interfaces */
1609 rtnl_lock();
1610
1611 /* If we're not RUNNING then don't reset. Leave the reset_pending
1612 * flag set so that efx_pci_probe_main will be retried */
1613 if (efx->state != STATE_RUNNING) {
1614 EFX_INFO(efx, "scheduled reset quenched. NIC not RUNNING\n");
1615 goto unlock_rtnl;
1616 }
1617
1618 efx->state = STATE_RESETTING;
1619 EFX_INFO(efx, "resetting (%d)\n", method);
1620
Ben Hutchings2467ca42008-09-01 12:48:50 +01001621 efx_reset_down(efx, &ecmd);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001622
1623 rc = falcon_reset_hw(efx, method);
1624 if (rc) {
1625 EFX_ERR(efx, "failed to reset hardware\n");
Ben Hutchings2467ca42008-09-01 12:48:50 +01001626 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001627 }
1628
1629 /* Allow resets to be rescheduled. */
1630 efx->reset_pending = RESET_TYPE_NONE;
1631
1632 /* Reinitialise bus-mastering, which may have been turned off before
1633 * the reset was scheduled. This is still appropriate, even in the
1634 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
1635 * can respond to requests. */
1636 pci_set_master(efx->pci_dev);
1637
Ben Hutchings8ceee662008-04-27 12:55:59 +01001638 /* Leave device stopped if necessary */
1639 if (method == RESET_TYPE_DISABLE) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001640 rc = -EIO;
Ben Hutchings2467ca42008-09-01 12:48:50 +01001641 goto fail;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001642 }
1643
Ben Hutchings2467ca42008-09-01 12:48:50 +01001644 rc = efx_reset_up(efx, &ecmd, true);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001645 if (rc)
Ben Hutchings2467ca42008-09-01 12:48:50 +01001646 goto disable;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001647
Ben Hutchings8ceee662008-04-27 12:55:59 +01001648 EFX_LOG(efx, "reset complete\n");
Ben Hutchings8ceee662008-04-27 12:55:59 +01001649 efx->state = STATE_RUNNING;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001650 unlock_rtnl:
1651 rtnl_unlock();
1652 return 0;
1653
Ben Hutchings2467ca42008-09-01 12:48:50 +01001654 fail:
1655 efx_reset_up(efx, &ecmd, false);
1656 disable:
Ben Hutchings8ceee662008-04-27 12:55:59 +01001657 EFX_ERR(efx, "has been disabled\n");
1658 efx->state = STATE_DISABLED;
1659
Ben Hutchings8ceee662008-04-27 12:55:59 +01001660 rtnl_unlock();
1661 efx_unregister_netdev(efx);
1662 efx_fini_port(efx);
1663 return rc;
1664}
1665
1666/* The worker thread exists so that code that cannot sleep can
1667 * schedule a reset for later.
1668 */
1669static void efx_reset_work(struct work_struct *data)
1670{
1671 struct efx_nic *nic = container_of(data, struct efx_nic, reset_work);
1672
1673 efx_reset(nic);
1674}
1675
1676void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
1677{
1678 enum reset_type method;
1679
1680 if (efx->reset_pending != RESET_TYPE_NONE) {
1681 EFX_INFO(efx, "quenching already scheduled reset\n");
1682 return;
1683 }
1684
1685 switch (type) {
1686 case RESET_TYPE_INVISIBLE:
1687 case RESET_TYPE_ALL:
1688 case RESET_TYPE_WORLD:
1689 case RESET_TYPE_DISABLE:
1690 method = type;
1691 break;
1692 case RESET_TYPE_RX_RECOVERY:
1693 case RESET_TYPE_RX_DESC_FETCH:
1694 case RESET_TYPE_TX_DESC_FETCH:
1695 case RESET_TYPE_TX_SKIP:
1696 method = RESET_TYPE_INVISIBLE;
1697 break;
1698 default:
1699 method = RESET_TYPE_ALL;
1700 break;
1701 }
1702
1703 if (method != type)
1704 EFX_LOG(efx, "scheduling reset (%d:%d)\n", type, method);
1705 else
1706 EFX_LOG(efx, "scheduling reset (%d)\n", method);
1707
1708 efx->reset_pending = method;
1709
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001710 queue_work(efx->reset_workqueue, &efx->reset_work);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001711}
1712
1713/**************************************************************************
1714 *
1715 * List of NICs we support
1716 *
1717 **************************************************************************/
1718
1719/* PCI device ID table */
1720static struct pci_device_id efx_pci_table[] __devinitdata = {
1721 {PCI_DEVICE(EFX_VENDID_SFC, FALCON_A_P_DEVID),
1722 .driver_data = (unsigned long) &falcon_a_nic_type},
1723 {PCI_DEVICE(EFX_VENDID_SFC, FALCON_B_P_DEVID),
1724 .driver_data = (unsigned long) &falcon_b_nic_type},
1725 {0} /* end of list */
1726};
1727
1728/**************************************************************************
1729 *
1730 * Dummy PHY/MAC/Board operations
1731 *
Ben Hutchings01aad7b2008-09-01 12:48:36 +01001732 * Can be used for some unimplemented operations
Ben Hutchings8ceee662008-04-27 12:55:59 +01001733 * Needed so all function pointers are valid and do not have to be tested
1734 * before use
1735 *
1736 **************************************************************************/
1737int efx_port_dummy_op_int(struct efx_nic *efx)
1738{
1739 return 0;
1740}
1741void efx_port_dummy_op_void(struct efx_nic *efx) {}
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001742void efx_port_dummy_op_blink(struct efx_nic *efx, bool blink) {}
Ben Hutchings8ceee662008-04-27 12:55:59 +01001743
1744static struct efx_phy_operations efx_dummy_phy_operations = {
1745 .init = efx_port_dummy_op_int,
1746 .reconfigure = efx_port_dummy_op_void,
1747 .check_hw = efx_port_dummy_op_int,
1748 .fini = efx_port_dummy_op_void,
1749 .clear_interrupt = efx_port_dummy_op_void,
1750 .reset_xaui = efx_port_dummy_op_void,
1751};
1752
Ben Hutchings8ceee662008-04-27 12:55:59 +01001753static struct efx_board efx_dummy_board_info = {
Ben Hutchings01aad7b2008-09-01 12:48:36 +01001754 .init = efx_port_dummy_op_int,
1755 .init_leds = efx_port_dummy_op_int,
1756 .set_fault_led = efx_port_dummy_op_blink,
1757 .blink = efx_port_dummy_op_blink,
1758 .fini = efx_port_dummy_op_void,
Ben Hutchings8ceee662008-04-27 12:55:59 +01001759};
1760
1761/**************************************************************************
1762 *
1763 * Data housekeeping
1764 *
1765 **************************************************************************/
1766
1767/* This zeroes out and then fills in the invariants in a struct
1768 * efx_nic (including all sub-structures).
1769 */
1770static int efx_init_struct(struct efx_nic *efx, struct efx_nic_type *type,
1771 struct pci_dev *pci_dev, struct net_device *net_dev)
1772{
1773 struct efx_channel *channel;
1774 struct efx_tx_queue *tx_queue;
1775 struct efx_rx_queue *rx_queue;
1776 int i, rc;
1777
1778 /* Initialise common structures */
1779 memset(efx, 0, sizeof(*efx));
1780 spin_lock_init(&efx->biu_lock);
1781 spin_lock_init(&efx->phy_lock);
1782 INIT_WORK(&efx->reset_work, efx_reset_work);
1783 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
1784 efx->pci_dev = pci_dev;
1785 efx->state = STATE_INIT;
1786 efx->reset_pending = RESET_TYPE_NONE;
1787 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
1788 efx->board_info = efx_dummy_board_info;
1789
1790 efx->net_dev = net_dev;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001791 efx->rx_checksum_enabled = true;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001792 spin_lock_init(&efx->netif_stop_lock);
1793 spin_lock_init(&efx->stats_lock);
1794 mutex_init(&efx->mac_lock);
1795 efx->phy_op = &efx_dummy_phy_operations;
1796 efx->mii.dev = net_dev;
1797 INIT_WORK(&efx->reconfigure_work, efx_reconfigure_work);
1798 atomic_set(&efx->netif_stop_count, 1);
1799
1800 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
1801 channel = &efx->channel[i];
1802 channel->efx = efx;
1803 channel->channel = i;
Ben Hutchingsdc8cfa52008-09-01 12:46:50 +01001804 channel->work_pending = false;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001805 }
Ben Hutchings60ac1062008-09-01 12:44:59 +01001806 for (i = 0; i < EFX_TX_QUEUE_COUNT; i++) {
Ben Hutchings8ceee662008-04-27 12:55:59 +01001807 tx_queue = &efx->tx_queue[i];
1808 tx_queue->efx = efx;
1809 tx_queue->queue = i;
1810 tx_queue->buffer = NULL;
1811 tx_queue->channel = &efx->channel[0]; /* for safety */
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01001812 tx_queue->tso_headers_free = NULL;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001813 }
1814 for (i = 0; i < EFX_MAX_RX_QUEUES; i++) {
1815 rx_queue = &efx->rx_queue[i];
1816 rx_queue->efx = efx;
1817 rx_queue->queue = i;
1818 rx_queue->channel = &efx->channel[0]; /* for safety */
1819 rx_queue->buffer = NULL;
1820 spin_lock_init(&rx_queue->add_lock);
1821 INIT_DELAYED_WORK(&rx_queue->work, efx_rx_work);
1822 }
1823
1824 efx->type = type;
1825
1826 /* Sanity-check NIC type */
1827 EFX_BUG_ON_PARANOID(efx->type->txd_ring_mask &
1828 (efx->type->txd_ring_mask + 1));
1829 EFX_BUG_ON_PARANOID(efx->type->rxd_ring_mask &
1830 (efx->type->rxd_ring_mask + 1));
1831 EFX_BUG_ON_PARANOID(efx->type->evq_size &
1832 (efx->type->evq_size - 1));
1833 /* As close as we can get to guaranteeing that we don't overflow */
1834 EFX_BUG_ON_PARANOID(efx->type->evq_size <
1835 (efx->type->txd_ring_mask + 1 +
1836 efx->type->rxd_ring_mask + 1));
1837 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
1838
1839 /* Higher numbered interrupt modes are less capable! */
1840 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
1841 interrupt_mode);
1842
1843 efx->workqueue = create_singlethread_workqueue("sfc_work");
1844 if (!efx->workqueue) {
1845 rc = -ENOMEM;
1846 goto fail1;
1847 }
1848
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001849 efx->reset_workqueue = create_singlethread_workqueue("sfc_reset");
1850 if (!efx->reset_workqueue) {
1851 rc = -ENOMEM;
1852 goto fail2;
1853 }
1854
Ben Hutchings8ceee662008-04-27 12:55:59 +01001855 return 0;
1856
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001857 fail2:
1858 destroy_workqueue(efx->workqueue);
1859 efx->workqueue = NULL;
1860
Ben Hutchings8ceee662008-04-27 12:55:59 +01001861 fail1:
1862 return rc;
1863}
1864
1865static void efx_fini_struct(struct efx_nic *efx)
1866{
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001867 if (efx->reset_workqueue) {
1868 destroy_workqueue(efx->reset_workqueue);
1869 efx->reset_workqueue = NULL;
1870 }
Ben Hutchings8ceee662008-04-27 12:55:59 +01001871 if (efx->workqueue) {
1872 destroy_workqueue(efx->workqueue);
1873 efx->workqueue = NULL;
1874 }
1875}
1876
1877/**************************************************************************
1878 *
1879 * PCI interface
1880 *
1881 **************************************************************************/
1882
1883/* Main body of final NIC shutdown code
1884 * This is called only at module unload (or hotplug removal).
1885 */
1886static void efx_pci_remove_main(struct efx_nic *efx)
1887{
1888 EFX_ASSERT_RESET_SERIALISED(efx);
1889
1890 /* Skip everything if we never obtained a valid membase */
1891 if (!efx->membase)
1892 return;
1893
1894 efx_fini_channels(efx);
1895 efx_fini_port(efx);
1896
1897 /* Shutdown the board, then the NIC and board state */
Ben Hutchings37b5a602008-05-30 22:27:04 +01001898 efx->board_info.fini(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001899 falcon_fini_interrupt(efx);
1900
1901 efx_fini_napi(efx);
1902 efx_remove_all(efx);
1903}
1904
1905/* Final NIC shutdown
1906 * This is called only at module unload (or hotplug removal).
1907 */
1908static void efx_pci_remove(struct pci_dev *pci_dev)
1909{
1910 struct efx_nic *efx;
1911
1912 efx = pci_get_drvdata(pci_dev);
1913 if (!efx)
1914 return;
1915
1916 /* Mark the NIC as fini, then stop the interface */
1917 rtnl_lock();
1918 efx->state = STATE_FINI;
1919 dev_close(efx->net_dev);
1920
1921 /* Allow any queued efx_resets() to complete */
1922 rtnl_unlock();
1923
1924 if (efx->membase == NULL)
1925 goto out;
1926
1927 efx_unregister_netdev(efx);
1928
1929 /* Wait for any scheduled resets to complete. No more will be
1930 * scheduled from this point because efx_stop_all() has been
1931 * called, we are no longer registered with driverlink, and
1932 * the net_device's have been removed. */
Ben Hutchings8d9853d2008-07-18 19:01:20 +01001933 flush_workqueue(efx->reset_workqueue);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001934
1935 efx_pci_remove_main(efx);
1936
1937out:
1938 efx_fini_io(efx);
1939 EFX_LOG(efx, "shutdown successful\n");
1940
1941 pci_set_drvdata(pci_dev, NULL);
1942 efx_fini_struct(efx);
1943 free_netdev(efx->net_dev);
1944};
1945
1946/* Main body of NIC initialisation
1947 * This is called at module load (or hotplug insertion, theoretically).
1948 */
1949static int efx_pci_probe_main(struct efx_nic *efx)
1950{
1951 int rc;
1952
1953 /* Do start-of-day initialisation */
1954 rc = efx_probe_all(efx);
1955 if (rc)
1956 goto fail1;
1957
1958 rc = efx_init_napi(efx);
1959 if (rc)
1960 goto fail2;
1961
1962 /* Initialise the board */
1963 rc = efx->board_info.init(efx);
1964 if (rc) {
1965 EFX_ERR(efx, "failed to initialise board\n");
1966 goto fail3;
1967 }
1968
1969 rc = falcon_init_nic(efx);
1970 if (rc) {
1971 EFX_ERR(efx, "failed to initialise NIC\n");
1972 goto fail4;
1973 }
1974
1975 rc = efx_init_port(efx);
1976 if (rc) {
1977 EFX_ERR(efx, "failed to initialise port\n");
1978 goto fail5;
1979 }
1980
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001981 efx_init_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001982
1983 rc = falcon_init_interrupt(efx);
1984 if (rc)
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001985 goto fail6;
Ben Hutchings8ceee662008-04-27 12:55:59 +01001986
1987 return 0;
1988
Ben Hutchings8ceee662008-04-27 12:55:59 +01001989 fail6:
Ben Hutchingsbc3c90a2008-09-01 12:48:46 +01001990 efx_fini_channels(efx);
Ben Hutchings8ceee662008-04-27 12:55:59 +01001991 efx_fini_port(efx);
1992 fail5:
1993 fail4:
1994 fail3:
1995 efx_fini_napi(efx);
1996 fail2:
1997 efx_remove_all(efx);
1998 fail1:
1999 return rc;
2000}
2001
2002/* NIC initialisation
2003 *
2004 * This is called at module load (or hotplug insertion,
2005 * theoretically). It sets up PCI mappings, tests and resets the NIC,
2006 * sets up and registers the network devices with the kernel and hooks
2007 * the interrupt service routine. It does not prepare the device for
2008 * transmission; this is left to the first time one of the network
2009 * interfaces is brought up (i.e. efx_net_open).
2010 */
2011static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2012 const struct pci_device_id *entry)
2013{
2014 struct efx_nic_type *type = (struct efx_nic_type *) entry->driver_data;
2015 struct net_device *net_dev;
2016 struct efx_nic *efx;
2017 int i, rc;
2018
2019 /* Allocate and initialise a struct net_device and struct efx_nic */
2020 net_dev = alloc_etherdev(sizeof(*efx));
2021 if (!net_dev)
2022 return -ENOMEM;
Ben Hutchingsb9b39b62008-05-07 12:51:12 +01002023 net_dev->features |= (NETIF_F_IP_CSUM | NETIF_F_SG |
2024 NETIF_F_HIGHDMA | NETIF_F_TSO);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002025 if (lro)
2026 net_dev->features |= NETIF_F_LRO;
Ben Hutchings285065632008-09-01 12:46:54 +01002027 /* Mask for features that also apply to VLAN devices */
2028 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
Ben Hutchings740847d2008-09-01 12:48:23 +01002029 NETIF_F_HIGHDMA | NETIF_F_TSO);
Ben Hutchings767e4682008-09-01 12:43:14 +01002030 efx = netdev_priv(net_dev);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002031 pci_set_drvdata(pci_dev, efx);
2032 rc = efx_init_struct(efx, type, pci_dev, net_dev);
2033 if (rc)
2034 goto fail1;
2035
2036 EFX_INFO(efx, "Solarflare Communications NIC detected\n");
2037
2038 /* Set up basic I/O (BAR mappings etc) */
2039 rc = efx_init_io(efx);
2040 if (rc)
2041 goto fail2;
2042
2043 /* No serialisation is required with the reset path because
2044 * we're in STATE_INIT. */
2045 for (i = 0; i < 5; i++) {
2046 rc = efx_pci_probe_main(efx);
2047 if (rc == 0)
2048 break;
2049
2050 /* Serialise against efx_reset(). No more resets will be
2051 * scheduled since efx_stop_all() has been called, and we
2052 * have not and never have been registered with either
2053 * the rtnetlink or driverlink layers. */
Ben Hutchings8d9853d2008-07-18 19:01:20 +01002054 flush_workqueue(efx->reset_workqueue);
Ben Hutchings8ceee662008-04-27 12:55:59 +01002055
2056 /* Retry if a recoverably reset event has been scheduled */
2057 if ((efx->reset_pending != RESET_TYPE_INVISIBLE) &&
2058 (efx->reset_pending != RESET_TYPE_ALL))
2059 goto fail3;
2060
2061 efx->reset_pending = RESET_TYPE_NONE;
2062 }
2063
2064 if (rc) {
2065 EFX_ERR(efx, "Could not reset NIC\n");
2066 goto fail4;
2067 }
2068
2069 /* Switch to the running state before we expose the device to
2070 * the OS. This is to ensure that the initial gathering of
2071 * MAC stats succeeds. */
2072 rtnl_lock();
2073 efx->state = STATE_RUNNING;
2074 rtnl_unlock();
2075
2076 rc = efx_register_netdev(efx);
2077 if (rc)
2078 goto fail5;
2079
2080 EFX_LOG(efx, "initialisation successful\n");
2081
2082 return 0;
2083
2084 fail5:
2085 efx_pci_remove_main(efx);
2086 fail4:
2087 fail3:
2088 efx_fini_io(efx);
2089 fail2:
2090 efx_fini_struct(efx);
2091 fail1:
2092 EFX_LOG(efx, "initialisation failed. rc=%d\n", rc);
2093 free_netdev(net_dev);
2094 return rc;
2095}
2096
2097static struct pci_driver efx_pci_driver = {
2098 .name = EFX_DRIVER_NAME,
2099 .id_table = efx_pci_table,
2100 .probe = efx_pci_probe,
2101 .remove = efx_pci_remove,
2102};
2103
2104/**************************************************************************
2105 *
2106 * Kernel module interface
2107 *
2108 *************************************************************************/
2109
2110module_param(interrupt_mode, uint, 0444);
2111MODULE_PARM_DESC(interrupt_mode,
2112 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2113
2114static int __init efx_init_module(void)
2115{
2116 int rc;
2117
2118 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2119
2120 rc = register_netdevice_notifier(&efx_netdev_notifier);
2121 if (rc)
2122 goto err_notifier;
2123
2124 refill_workqueue = create_workqueue("sfc_refill");
2125 if (!refill_workqueue) {
2126 rc = -ENOMEM;
2127 goto err_refill;
2128 }
2129
2130 rc = pci_register_driver(&efx_pci_driver);
2131 if (rc < 0)
2132 goto err_pci;
2133
2134 return 0;
2135
2136 err_pci:
2137 destroy_workqueue(refill_workqueue);
2138 err_refill:
2139 unregister_netdevice_notifier(&efx_netdev_notifier);
2140 err_notifier:
2141 return rc;
2142}
2143
2144static void __exit efx_exit_module(void)
2145{
2146 printk(KERN_INFO "Solarflare NET driver unloading\n");
2147
2148 pci_unregister_driver(&efx_pci_driver);
2149 destroy_workqueue(refill_workqueue);
2150 unregister_netdevice_notifier(&efx_netdev_notifier);
2151
2152}
2153
2154module_init(efx_init_module);
2155module_exit(efx_exit_module);
2156
2157MODULE_AUTHOR("Michael Brown <mbrown@fensystems.co.uk> and "
2158 "Solarflare Communications");
2159MODULE_DESCRIPTION("Solarflare Communications network driver");
2160MODULE_LICENSE("GPL");
2161MODULE_DEVICE_TABLE(pci, efx_pci_table);