]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/sfc/efx.c
sfc: Generalise link state monitoring
[mirror_ubuntu-bionic-kernel.git] / drivers / net / sfc / efx.c
CommitLineData
8ceee660
BH
1/****************************************************************************
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>
aa6ef27e 22#include <linux/topology.h>
8ceee660 23#include "net_driver.h"
8ceee660
BH
24#include "efx.h"
25#include "mdio_10g.h"
26#include "falcon.h"
8ceee660 27
c459302d
BH
28/**************************************************************************
29 *
30 * Type name strings
31 *
32 **************************************************************************
33 */
34
35/* Loopback mode names (see LOOPBACK_MODE()) */
36const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
37const char *efx_loopback_mode_names[] = {
38 [LOOPBACK_NONE] = "NONE",
39 [LOOPBACK_GMAC] = "GMAC",
40 [LOOPBACK_XGMII] = "XGMII",
41 [LOOPBACK_XGXS] = "XGXS",
42 [LOOPBACK_XAUI] = "XAUI",
43 [LOOPBACK_GPHY] = "GPHY",
44 [LOOPBACK_PHYXS] = "PHYXS",
45 [LOOPBACK_PCS] = "PCS",
46 [LOOPBACK_PMAPMD] = "PMA/PMD",
47 [LOOPBACK_NETWORK] = "NETWORK",
48};
49
50/* Interrupt mode names (see INT_MODE())) */
51const unsigned int efx_interrupt_mode_max = EFX_INT_MODE_MAX;
52const char *efx_interrupt_mode_names[] = {
53 [EFX_INT_MODE_MSIX] = "MSI-X",
54 [EFX_INT_MODE_MSI] = "MSI",
55 [EFX_INT_MODE_LEGACY] = "legacy",
56};
57
58const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
59const char *efx_reset_type_names[] = {
60 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
61 [RESET_TYPE_ALL] = "ALL",
62 [RESET_TYPE_WORLD] = "WORLD",
63 [RESET_TYPE_DISABLE] = "DISABLE",
64 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
65 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
66 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
67 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
68 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
69 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
70};
71
8ceee660
BH
72#define EFX_MAX_MTU (9 * 1024)
73
74/* RX slow fill workqueue. If memory allocation fails in the fast path,
75 * a work item is pushed onto this work queue to retry the allocation later,
76 * to avoid the NIC being starved of RX buffers. Since this is a per cpu
77 * workqueue, there is nothing to be gained in making it per NIC
78 */
79static struct workqueue_struct *refill_workqueue;
80
1ab00629
SH
81/* Reset workqueue. If any NIC has a hardware failure then a reset will be
82 * queued onto this work queue. This is not a per-nic work queue, because
83 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
84 */
85static struct workqueue_struct *reset_workqueue;
86
8ceee660
BH
87/**************************************************************************
88 *
89 * Configurable values
90 *
91 *************************************************************************/
92
8ceee660
BH
93/*
94 * Use separate channels for TX and RX events
95 *
28b581ab
NT
96 * Set this to 1 to use separate channels for TX and RX. It allows us
97 * to control interrupt affinity separately for TX and RX.
8ceee660 98 *
28b581ab 99 * This is only used in MSI-X interrupt mode
8ceee660 100 */
28b581ab
NT
101static unsigned int separate_tx_channels;
102module_param(separate_tx_channels, uint, 0644);
103MODULE_PARM_DESC(separate_tx_channels,
104 "Use separate channels for TX and RX");
8ceee660
BH
105
106/* This is the weight assigned to each of the (per-channel) virtual
107 * NAPI devices.
108 */
109static int napi_weight = 64;
110
111/* This is the time (in jiffies) between invocations of the hardware
112 * monitor, which checks for known hardware bugs and resets the
113 * hardware and driver as necessary.
114 */
115unsigned int efx_monitor_interval = 1 * HZ;
116
8ceee660
BH
117/* This controls whether or not the driver will initialise devices
118 * with invalid MAC addresses stored in the EEPROM or flash. If true,
119 * such devices will be initialised with a random locally-generated
120 * MAC address. This allows for loading the sfc_mtd driver to
121 * reprogram the flash, even if the flash contents (including the MAC
122 * address) have previously been erased.
123 */
124static unsigned int allow_bad_hwaddr;
125
126/* Initial interrupt moderation settings. They can be modified after
127 * module load with ethtool.
128 *
129 * The default for RX should strike a balance between increasing the
130 * round-trip latency and reducing overhead.
131 */
132static unsigned int rx_irq_mod_usec = 60;
133
134/* Initial interrupt moderation settings. They can be modified after
135 * module load with ethtool.
136 *
137 * This default is chosen to ensure that a 10G link does not go idle
138 * while a TX queue is stopped after it has become full. A queue is
139 * restarted when it drops below half full. The time this takes (assuming
140 * worst case 3 descriptors per packet and 1024 descriptors) is
141 * 512 / 3 * 1.2 = 205 usec.
142 */
143static unsigned int tx_irq_mod_usec = 150;
144
145/* This is the first interrupt mode to try out of:
146 * 0 => MSI-X
147 * 1 => MSI
148 * 2 => legacy
149 */
150static unsigned int interrupt_mode;
151
152/* This is the requested number of CPUs to use for Receive-Side Scaling (RSS),
153 * i.e. the number of CPUs among which we may distribute simultaneous
154 * interrupt handling.
155 *
156 * Cards without MSI-X will only target one CPU via legacy or MSI interrupt.
157 * The default (0) means to assign an interrupt to each package (level II cache)
158 */
159static unsigned int rss_cpus;
160module_param(rss_cpus, uint, 0444);
161MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
162
84ae48fe
BH
163static int phy_flash_cfg;
164module_param(phy_flash_cfg, int, 0644);
165MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
166
6fb70fd1
BH
167static unsigned irq_adapt_low_thresh = 10000;
168module_param(irq_adapt_low_thresh, uint, 0644);
169MODULE_PARM_DESC(irq_adapt_low_thresh,
170 "Threshold score for reducing IRQ moderation");
171
172static unsigned irq_adapt_high_thresh = 20000;
173module_param(irq_adapt_high_thresh, uint, 0644);
174MODULE_PARM_DESC(irq_adapt_high_thresh,
175 "Threshold score for increasing IRQ moderation");
176
8ceee660
BH
177/**************************************************************************
178 *
179 * Utility functions and prototypes
180 *
181 *************************************************************************/
182static void efx_remove_channel(struct efx_channel *channel);
183static void efx_remove_port(struct efx_nic *efx);
184static void efx_fini_napi(struct efx_nic *efx);
185static void efx_fini_channels(struct efx_nic *efx);
186
187#define EFX_ASSERT_RESET_SERIALISED(efx) \
188 do { \
332c1ce9
BH
189 if ((efx->state == STATE_RUNNING) || \
190 (efx->state == STATE_DISABLED)) \
8ceee660
BH
191 ASSERT_RTNL(); \
192 } while (0)
193
194/**************************************************************************
195 *
196 * Event queue processing
197 *
198 *************************************************************************/
199
200/* Process channel's event queue
201 *
202 * This function is responsible for processing the event queue of a
203 * single channel. The caller must guarantee that this function will
204 * never be concurrently called more than once on the same channel,
205 * though different channels may be being processed concurrently.
206 */
4d566063 207static int efx_process_channel(struct efx_channel *channel, int rx_quota)
8ceee660 208{
42cbe2d7
BH
209 struct efx_nic *efx = channel->efx;
210 int rx_packets;
8ceee660 211
42cbe2d7 212 if (unlikely(efx->reset_pending != RESET_TYPE_NONE ||
8ceee660 213 !channel->enabled))
42cbe2d7 214 return 0;
8ceee660 215
42cbe2d7
BH
216 rx_packets = falcon_process_eventq(channel, rx_quota);
217 if (rx_packets == 0)
218 return 0;
8ceee660
BH
219
220 /* Deliver last RX packet. */
221 if (channel->rx_pkt) {
222 __efx_rx_packet(channel, channel->rx_pkt,
223 channel->rx_pkt_csummed);
224 channel->rx_pkt = NULL;
225 }
226
8ceee660
BH
227 efx_rx_strategy(channel);
228
42cbe2d7 229 efx_fast_push_rx_descriptors(&efx->rx_queue[channel->channel]);
8ceee660 230
42cbe2d7 231 return rx_packets;
8ceee660
BH
232}
233
234/* Mark channel as finished processing
235 *
236 * Note that since we will not receive further interrupts for this
237 * channel before we finish processing and call the eventq_read_ack()
238 * method, there is no need to use the interrupt hold-off timers.
239 */
240static inline void efx_channel_processed(struct efx_channel *channel)
241{
5b9e207c
BH
242 /* The interrupt handler for this channel may set work_pending
243 * as soon as we acknowledge the events we've seen. Make sure
244 * it's cleared before then. */
dc8cfa55 245 channel->work_pending = false;
5b9e207c
BH
246 smp_wmb();
247
8ceee660
BH
248 falcon_eventq_read_ack(channel);
249}
250
251/* NAPI poll handler
252 *
253 * NAPI guarantees serialisation of polls of the same device, which
254 * provides the guarantee required by efx_process_channel().
255 */
256static int efx_poll(struct napi_struct *napi, int budget)
257{
258 struct efx_channel *channel =
259 container_of(napi, struct efx_channel, napi_str);
8ceee660
BH
260 int rx_packets;
261
262 EFX_TRACE(channel->efx, "channel %d NAPI poll executing on CPU %d\n",
263 channel->channel, raw_smp_processor_id());
264
42cbe2d7 265 rx_packets = efx_process_channel(channel, budget);
8ceee660
BH
266
267 if (rx_packets < budget) {
6fb70fd1
BH
268 struct efx_nic *efx = channel->efx;
269
270 if (channel->used_flags & EFX_USED_BY_RX &&
271 efx->irq_rx_adaptive &&
272 unlikely(++channel->irq_count == 1000)) {
6fb70fd1
BH
273 if (unlikely(channel->irq_mod_score <
274 irq_adapt_low_thresh)) {
0d86ebd8
BH
275 if (channel->irq_moderation > 1) {
276 channel->irq_moderation -= 1;
ef2b90ee 277 efx->type->push_irq_moderation(channel);
0d86ebd8 278 }
6fb70fd1
BH
279 } else if (unlikely(channel->irq_mod_score >
280 irq_adapt_high_thresh)) {
0d86ebd8
BH
281 if (channel->irq_moderation <
282 efx->irq_rx_moderation) {
283 channel->irq_moderation += 1;
ef2b90ee 284 efx->type->push_irq_moderation(channel);
0d86ebd8 285 }
6fb70fd1 286 }
6fb70fd1
BH
287 channel->irq_count = 0;
288 channel->irq_mod_score = 0;
289 }
290
8ceee660 291 /* There is no race here; although napi_disable() will
288379f0 292 * only wait for napi_complete(), this isn't a problem
8ceee660
BH
293 * since efx_channel_processed() will have no effect if
294 * interrupts have already been disabled.
295 */
288379f0 296 napi_complete(napi);
8ceee660
BH
297 efx_channel_processed(channel);
298 }
299
300 return rx_packets;
301}
302
303/* Process the eventq of the specified channel immediately on this CPU
304 *
305 * Disable hardware generated interrupts, wait for any existing
306 * processing to finish, then directly poll (and ack ) the eventq.
307 * Finally reenable NAPI and interrupts.
308 *
309 * Since we are touching interrupts the caller should hold the suspend lock
310 */
311void efx_process_channel_now(struct efx_channel *channel)
312{
313 struct efx_nic *efx = channel->efx;
314
315 BUG_ON(!channel->used_flags);
316 BUG_ON(!channel->enabled);
317
318 /* Disable interrupts and wait for ISRs to complete */
319 falcon_disable_interrupts(efx);
320 if (efx->legacy_irq)
321 synchronize_irq(efx->legacy_irq);
64ee3120 322 if (channel->irq)
8ceee660
BH
323 synchronize_irq(channel->irq);
324
325 /* Wait for any NAPI processing to complete */
326 napi_disable(&channel->napi_str);
327
328 /* Poll the channel */
3ffeabdd 329 efx_process_channel(channel, EFX_EVQ_SIZE);
8ceee660
BH
330
331 /* Ack the eventq. This may cause an interrupt to be generated
332 * when they are reenabled */
333 efx_channel_processed(channel);
334
335 napi_enable(&channel->napi_str);
336 falcon_enable_interrupts(efx);
337}
338
339/* Create event queue
340 * Event queue memory allocations are done only once. If the channel
341 * is reset, the memory buffer will be reused; this guards against
342 * errors during channel reset and also simplifies interrupt handling.
343 */
344static int efx_probe_eventq(struct efx_channel *channel)
345{
346 EFX_LOG(channel->efx, "chan %d create event queue\n", channel->channel);
347
348 return falcon_probe_eventq(channel);
349}
350
351/* Prepare channel's event queue */
bc3c90a2 352static void efx_init_eventq(struct efx_channel *channel)
8ceee660
BH
353{
354 EFX_LOG(channel->efx, "chan %d init event queue\n", channel->channel);
355
356 channel->eventq_read_ptr = 0;
357
bc3c90a2 358 falcon_init_eventq(channel);
8ceee660
BH
359}
360
361static void efx_fini_eventq(struct efx_channel *channel)
362{
363 EFX_LOG(channel->efx, "chan %d fini event queue\n", channel->channel);
364
365 falcon_fini_eventq(channel);
366}
367
368static void efx_remove_eventq(struct efx_channel *channel)
369{
370 EFX_LOG(channel->efx, "chan %d remove event queue\n", channel->channel);
371
372 falcon_remove_eventq(channel);
373}
374
375/**************************************************************************
376 *
377 * Channel handling
378 *
379 *************************************************************************/
380
8ceee660
BH
381static int efx_probe_channel(struct efx_channel *channel)
382{
383 struct efx_tx_queue *tx_queue;
384 struct efx_rx_queue *rx_queue;
385 int rc;
386
387 EFX_LOG(channel->efx, "creating channel %d\n", channel->channel);
388
389 rc = efx_probe_eventq(channel);
390 if (rc)
391 goto fail1;
392
393 efx_for_each_channel_tx_queue(tx_queue, channel) {
394 rc = efx_probe_tx_queue(tx_queue);
395 if (rc)
396 goto fail2;
397 }
398
399 efx_for_each_channel_rx_queue(rx_queue, channel) {
400 rc = efx_probe_rx_queue(rx_queue);
401 if (rc)
402 goto fail3;
403 }
404
405 channel->n_rx_frm_trunc = 0;
406
407 return 0;
408
409 fail3:
410 efx_for_each_channel_rx_queue(rx_queue, channel)
411 efx_remove_rx_queue(rx_queue);
412 fail2:
413 efx_for_each_channel_tx_queue(tx_queue, channel)
414 efx_remove_tx_queue(tx_queue);
415 fail1:
416 return rc;
417}
418
419
56536e9c
BH
420static void efx_set_channel_names(struct efx_nic *efx)
421{
422 struct efx_channel *channel;
423 const char *type = "";
424 int number;
425
426 efx_for_each_channel(channel, efx) {
427 number = channel->channel;
428 if (efx->n_channels > efx->n_rx_queues) {
429 if (channel->channel < efx->n_rx_queues) {
430 type = "-rx";
431 } else {
432 type = "-tx";
433 number -= efx->n_rx_queues;
434 }
435 }
436 snprintf(channel->name, sizeof(channel->name),
437 "%s%s-%d", efx->name, type, number);
438 }
439}
440
8ceee660
BH
441/* Channels are shutdown and reinitialised whilst the NIC is running
442 * to propagate configuration changes (mtu, checksum offload), or
443 * to clear hardware error conditions
444 */
bc3c90a2 445static void efx_init_channels(struct efx_nic *efx)
8ceee660
BH
446{
447 struct efx_tx_queue *tx_queue;
448 struct efx_rx_queue *rx_queue;
449 struct efx_channel *channel;
8ceee660 450
f7f13b0b
BH
451 /* Calculate the rx buffer allocation parameters required to
452 * support the current MTU, including padding for header
453 * alignment and overruns.
454 */
455 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
456 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
457 efx->type->rx_buffer_padding);
458 efx->rx_buffer_order = get_order(efx->rx_buffer_len);
8ceee660
BH
459
460 /* Initialise the channels */
461 efx_for_each_channel(channel, efx) {
462 EFX_LOG(channel->efx, "init chan %d\n", channel->channel);
463
bc3c90a2 464 efx_init_eventq(channel);
8ceee660 465
bc3c90a2
BH
466 efx_for_each_channel_tx_queue(tx_queue, channel)
467 efx_init_tx_queue(tx_queue);
8ceee660
BH
468
469 /* The rx buffer allocation strategy is MTU dependent */
470 efx_rx_strategy(channel);
471
bc3c90a2
BH
472 efx_for_each_channel_rx_queue(rx_queue, channel)
473 efx_init_rx_queue(rx_queue);
8ceee660
BH
474
475 WARN_ON(channel->rx_pkt != NULL);
476 efx_rx_strategy(channel);
477 }
8ceee660
BH
478}
479
480/* This enables event queue processing and packet transmission.
481 *
482 * Note that this function is not allowed to fail, since that would
483 * introduce too much complexity into the suspend/resume path.
484 */
485static void efx_start_channel(struct efx_channel *channel)
486{
487 struct efx_rx_queue *rx_queue;
488
489 EFX_LOG(channel->efx, "starting chan %d\n", channel->channel);
490
5b9e207c
BH
491 /* The interrupt handler for this channel may set work_pending
492 * as soon as we enable it. Make sure it's cleared before
493 * then. Similarly, make sure it sees the enabled flag set. */
dc8cfa55
BH
494 channel->work_pending = false;
495 channel->enabled = true;
5b9e207c 496 smp_wmb();
8ceee660
BH
497
498 napi_enable(&channel->napi_str);
499
500 /* Load up RX descriptors */
501 efx_for_each_channel_rx_queue(rx_queue, channel)
502 efx_fast_push_rx_descriptors(rx_queue);
503}
504
505/* This disables event queue processing and packet transmission.
506 * This function does not guarantee that all queue processing
507 * (e.g. RX refill) is complete.
508 */
509static void efx_stop_channel(struct efx_channel *channel)
510{
511 struct efx_rx_queue *rx_queue;
512
513 if (!channel->enabled)
514 return;
515
516 EFX_LOG(channel->efx, "stop chan %d\n", channel->channel);
517
dc8cfa55 518 channel->enabled = false;
8ceee660
BH
519 napi_disable(&channel->napi_str);
520
521 /* Ensure that any worker threads have exited or will be no-ops */
522 efx_for_each_channel_rx_queue(rx_queue, channel) {
523 spin_lock_bh(&rx_queue->add_lock);
524 spin_unlock_bh(&rx_queue->add_lock);
525 }
526}
527
528static void efx_fini_channels(struct efx_nic *efx)
529{
530 struct efx_channel *channel;
531 struct efx_tx_queue *tx_queue;
532 struct efx_rx_queue *rx_queue;
6bc5d3a9 533 int rc;
8ceee660
BH
534
535 EFX_ASSERT_RESET_SERIALISED(efx);
536 BUG_ON(efx->port_enabled);
537
6bc5d3a9
BH
538 rc = falcon_flush_queues(efx);
539 if (rc)
540 EFX_ERR(efx, "failed to flush queues\n");
541 else
542 EFX_LOG(efx, "successfully flushed all queues\n");
543
8ceee660
BH
544 efx_for_each_channel(channel, efx) {
545 EFX_LOG(channel->efx, "shut down chan %d\n", channel->channel);
546
547 efx_for_each_channel_rx_queue(rx_queue, channel)
548 efx_fini_rx_queue(rx_queue);
549 efx_for_each_channel_tx_queue(tx_queue, channel)
550 efx_fini_tx_queue(tx_queue);
8ceee660
BH
551 efx_fini_eventq(channel);
552 }
553}
554
555static void efx_remove_channel(struct efx_channel *channel)
556{
557 struct efx_tx_queue *tx_queue;
558 struct efx_rx_queue *rx_queue;
559
560 EFX_LOG(channel->efx, "destroy chan %d\n", channel->channel);
561
562 efx_for_each_channel_rx_queue(rx_queue, channel)
563 efx_remove_rx_queue(rx_queue);
564 efx_for_each_channel_tx_queue(tx_queue, channel)
565 efx_remove_tx_queue(tx_queue);
566 efx_remove_eventq(channel);
567
568 channel->used_flags = 0;
569}
570
571void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue, int delay)
572{
573 queue_delayed_work(refill_workqueue, &rx_queue->work, delay);
574}
575
576/**************************************************************************
577 *
578 * Port handling
579 *
580 **************************************************************************/
581
582/* This ensures that the kernel is kept informed (via
583 * netif_carrier_on/off) of the link status, and also maintains the
584 * link status's stop on the port's TX queue.
585 */
fdaa9aed 586void efx_link_status_changed(struct efx_nic *efx)
8ceee660 587{
eb50c0d6
BH
588 struct efx_link_state *link_state = &efx->link_state;
589
8ceee660
BH
590 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
591 * that no events are triggered between unregister_netdev() and the
592 * driver unloading. A more general condition is that NETDEV_CHANGE
593 * can only be generated between NETDEV_UP and NETDEV_DOWN */
594 if (!netif_running(efx->net_dev))
595 return;
596
8c8661e4
BH
597 if (efx->port_inhibited) {
598 netif_carrier_off(efx->net_dev);
599 return;
600 }
601
eb50c0d6 602 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
8ceee660
BH
603 efx->n_link_state_changes++;
604
eb50c0d6 605 if (link_state->up)
8ceee660
BH
606 netif_carrier_on(efx->net_dev);
607 else
608 netif_carrier_off(efx->net_dev);
609 }
610
611 /* Status message for kernel log */
eb50c0d6 612 if (link_state->up) {
f31a45d2 613 EFX_INFO(efx, "link up at %uMbps %s-duplex (MTU %d)%s\n",
eb50c0d6 614 link_state->speed, link_state->fd ? "full" : "half",
8ceee660
BH
615 efx->net_dev->mtu,
616 (efx->promiscuous ? " [PROMISC]" : ""));
617 } else {
618 EFX_INFO(efx, "link down\n");
619 }
620
621}
622
d3245b28
BH
623void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
624{
625 efx->link_advertising = advertising;
626 if (advertising) {
627 if (advertising & ADVERTISED_Pause)
628 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
629 else
630 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
631 if (advertising & ADVERTISED_Asym_Pause)
632 efx->wanted_fc ^= EFX_FC_TX;
633 }
634}
635
636void efx_link_set_wanted_fc(struct efx_nic *efx, enum efx_fc_type wanted_fc)
637{
638 efx->wanted_fc = wanted_fc;
639 if (efx->link_advertising) {
640 if (wanted_fc & EFX_FC_RX)
641 efx->link_advertising |= (ADVERTISED_Pause |
642 ADVERTISED_Asym_Pause);
643 else
644 efx->link_advertising &= ~(ADVERTISED_Pause |
645 ADVERTISED_Asym_Pause);
646 if (wanted_fc & EFX_FC_TX)
647 efx->link_advertising ^= ADVERTISED_Asym_Pause;
648 }
649}
650
115122af
BH
651static void efx_fini_port(struct efx_nic *efx);
652
d3245b28
BH
653/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
654 * the MAC appropriately. All other PHY configuration changes are pushed
655 * through phy_op->set_settings(), and pushed asynchronously to the MAC
656 * through efx_monitor().
657 *
658 * Callers must hold the mac_lock
659 */
660int __efx_reconfigure_port(struct efx_nic *efx)
8ceee660 661{
d3245b28
BH
662 enum efx_phy_mode phy_mode;
663 int rc;
8ceee660 664
d3245b28 665 WARN_ON(!mutex_is_locked(&efx->mac_lock));
8ceee660 666
a816f75a
BH
667 /* Serialise the promiscuous flag with efx_set_multicast_list. */
668 if (efx_dev_registered(efx)) {
669 netif_addr_lock_bh(efx->net_dev);
670 netif_addr_unlock_bh(efx->net_dev);
671 }
672
d3245b28
BH
673 /* Disable PHY transmit in mac level loopbacks */
674 phy_mode = efx->phy_mode;
177dfcd8
BH
675 if (LOOPBACK_INTERNAL(efx))
676 efx->phy_mode |= PHY_MODE_TX_DISABLED;
677 else
678 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
177dfcd8 679
d3245b28 680 rc = efx->type->reconfigure_port(efx);
8ceee660 681
d3245b28
BH
682 if (rc)
683 efx->phy_mode = phy_mode;
177dfcd8 684
d3245b28 685 return rc;
8ceee660
BH
686}
687
688/* Reinitialise the MAC to pick up new PHY settings, even if the port is
689 * disabled. */
d3245b28 690int efx_reconfigure_port(struct efx_nic *efx)
8ceee660 691{
d3245b28
BH
692 int rc;
693
8ceee660
BH
694 EFX_ASSERT_RESET_SERIALISED(efx);
695
696 mutex_lock(&efx->mac_lock);
d3245b28 697 rc = __efx_reconfigure_port(efx);
8ceee660 698 mutex_unlock(&efx->mac_lock);
d3245b28
BH
699
700 return rc;
8ceee660
BH
701}
702
8be4f3e6
BH
703/* Asynchronous work item for changing MAC promiscuity and multicast
704 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
705 * MAC directly. */
766ca0fa
BH
706static void efx_mac_work(struct work_struct *data)
707{
708 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
709
710 mutex_lock(&efx->mac_lock);
8be4f3e6 711 if (efx->port_enabled) {
ef2b90ee 712 efx->type->push_multicast_hash(efx);
8be4f3e6
BH
713 efx->mac_op->reconfigure(efx);
714 }
766ca0fa
BH
715 mutex_unlock(&efx->mac_lock);
716}
717
8ceee660
BH
718static int efx_probe_port(struct efx_nic *efx)
719{
720 int rc;
721
722 EFX_LOG(efx, "create port\n");
723
ef2b90ee
BH
724 /* Connect up MAC/PHY operations table */
725 rc = efx->type->probe_port(efx);
8ceee660
BH
726 if (rc)
727 goto err;
728
84ae48fe
BH
729 if (phy_flash_cfg)
730 efx->phy_mode = PHY_MODE_SPECIAL;
731
8ceee660
BH
732 /* Sanity check MAC address */
733 if (is_valid_ether_addr(efx->mac_address)) {
734 memcpy(efx->net_dev->dev_addr, efx->mac_address, ETH_ALEN);
735 } else {
e174961c
JB
736 EFX_ERR(efx, "invalid MAC address %pM\n",
737 efx->mac_address);
8ceee660
BH
738 if (!allow_bad_hwaddr) {
739 rc = -EINVAL;
740 goto err;
741 }
742 random_ether_addr(efx->net_dev->dev_addr);
e174961c
JB
743 EFX_INFO(efx, "using locally-generated MAC %pM\n",
744 efx->net_dev->dev_addr);
8ceee660
BH
745 }
746
747 return 0;
748
749 err:
750 efx_remove_port(efx);
751 return rc;
752}
753
754static int efx_init_port(struct efx_nic *efx)
755{
756 int rc;
757
758 EFX_LOG(efx, "init port\n");
759
1dfc5cea
BH
760 mutex_lock(&efx->mac_lock);
761
177dfcd8 762 rc = efx->phy_op->init(efx);
8ceee660 763 if (rc)
1dfc5cea 764 goto fail1;
8ceee660 765
dc8cfa55 766 efx->port_initialized = true;
1dfc5cea 767
d3245b28
BH
768 /* Reconfigure the MAC before creating dma queues (required for
769 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
770 efx->mac_op->reconfigure(efx);
771
772 /* Ensure the PHY advertises the correct flow control settings */
773 rc = efx->phy_op->reconfigure(efx);
774 if (rc)
775 goto fail2;
776
1dfc5cea 777 mutex_unlock(&efx->mac_lock);
8ceee660 778 return 0;
177dfcd8 779
1dfc5cea 780fail2:
177dfcd8 781 efx->phy_op->fini(efx);
1dfc5cea
BH
782fail1:
783 mutex_unlock(&efx->mac_lock);
177dfcd8 784 return rc;
8ceee660
BH
785}
786
8ceee660
BH
787static void efx_start_port(struct efx_nic *efx)
788{
789 EFX_LOG(efx, "start port\n");
790 BUG_ON(efx->port_enabled);
791
792 mutex_lock(&efx->mac_lock);
dc8cfa55 793 efx->port_enabled = true;
8be4f3e6
BH
794
795 /* efx_mac_work() might have been scheduled after efx_stop_port(),
796 * and then cancelled by efx_flush_all() */
ef2b90ee 797 efx->type->push_multicast_hash(efx);
8be4f3e6
BH
798 efx->mac_op->reconfigure(efx);
799
8ceee660
BH
800 mutex_unlock(&efx->mac_lock);
801}
802
fdaa9aed 803/* Prevent efx_mac_work() and efx_monitor() from working */
8ceee660
BH
804static void efx_stop_port(struct efx_nic *efx)
805{
806 EFX_LOG(efx, "stop port\n");
807
808 mutex_lock(&efx->mac_lock);
dc8cfa55 809 efx->port_enabled = false;
8ceee660
BH
810 mutex_unlock(&efx->mac_lock);
811
812 /* Serialise against efx_set_multicast_list() */
55668611 813 if (efx_dev_registered(efx)) {
b9e40857
DM
814 netif_addr_lock_bh(efx->net_dev);
815 netif_addr_unlock_bh(efx->net_dev);
8ceee660
BH
816 }
817}
818
819static void efx_fini_port(struct efx_nic *efx)
820{
821 EFX_LOG(efx, "shut down port\n");
822
823 if (!efx->port_initialized)
824 return;
825
177dfcd8 826 efx->phy_op->fini(efx);
dc8cfa55 827 efx->port_initialized = false;
8ceee660 828
eb50c0d6 829 efx->link_state.up = false;
8ceee660
BH
830 efx_link_status_changed(efx);
831}
832
833static void efx_remove_port(struct efx_nic *efx)
834{
835 EFX_LOG(efx, "destroying port\n");
836
ef2b90ee 837 efx->type->remove_port(efx);
8ceee660
BH
838}
839
840/**************************************************************************
841 *
842 * NIC handling
843 *
844 **************************************************************************/
845
846/* This configures the PCI device to enable I/O and DMA. */
847static int efx_init_io(struct efx_nic *efx)
848{
849 struct pci_dev *pci_dev = efx->pci_dev;
850 dma_addr_t dma_mask = efx->type->max_dma_mask;
851 int rc;
852
853 EFX_LOG(efx, "initialising I/O\n");
854
855 rc = pci_enable_device(pci_dev);
856 if (rc) {
857 EFX_ERR(efx, "failed to enable PCI device\n");
858 goto fail1;
859 }
860
861 pci_set_master(pci_dev);
862
863 /* Set the PCI DMA mask. Try all possibilities from our
864 * genuine mask down to 32 bits, because some architectures
865 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
866 * masks event though they reject 46 bit masks.
867 */
868 while (dma_mask > 0x7fffffffUL) {
869 if (pci_dma_supported(pci_dev, dma_mask) &&
870 ((rc = pci_set_dma_mask(pci_dev, dma_mask)) == 0))
871 break;
872 dma_mask >>= 1;
873 }
874 if (rc) {
875 EFX_ERR(efx, "could not find a suitable DMA mask\n");
876 goto fail2;
877 }
878 EFX_LOG(efx, "using DMA mask %llx\n", (unsigned long long) dma_mask);
879 rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
880 if (rc) {
881 /* pci_set_consistent_dma_mask() is not *allowed* to
882 * fail with a mask that pci_set_dma_mask() accepted,
883 * but just in case...
884 */
885 EFX_ERR(efx, "failed to set consistent DMA mask\n");
886 goto fail2;
887 }
888
dc803df8
BH
889 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
890 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
8ceee660
BH
891 if (rc) {
892 EFX_ERR(efx, "request for memory BAR failed\n");
893 rc = -EIO;
894 goto fail3;
895 }
896 efx->membase = ioremap_nocache(efx->membase_phys,
897 efx->type->mem_map_size);
898 if (!efx->membase) {
dc803df8 899 EFX_ERR(efx, "could not map memory BAR at %llx+%x\n",
086ea356 900 (unsigned long long)efx->membase_phys,
8ceee660
BH
901 efx->type->mem_map_size);
902 rc = -ENOMEM;
903 goto fail4;
904 }
dc803df8
BH
905 EFX_LOG(efx, "memory BAR at %llx+%x (virtual %p)\n",
906 (unsigned long long)efx->membase_phys,
086ea356 907 efx->type->mem_map_size, efx->membase);
8ceee660
BH
908
909 return 0;
910
911 fail4:
dc803df8 912 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
8ceee660 913 fail3:
2c118e0f 914 efx->membase_phys = 0;
8ceee660
BH
915 fail2:
916 pci_disable_device(efx->pci_dev);
917 fail1:
918 return rc;
919}
920
921static void efx_fini_io(struct efx_nic *efx)
922{
923 EFX_LOG(efx, "shutting down I/O\n");
924
925 if (efx->membase) {
926 iounmap(efx->membase);
927 efx->membase = NULL;
928 }
929
930 if (efx->membase_phys) {
dc803df8 931 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
2c118e0f 932 efx->membase_phys = 0;
8ceee660
BH
933 }
934
935 pci_disable_device(efx->pci_dev);
936}
937
46123d04
BH
938/* Get number of RX queues wanted. Return number of online CPU
939 * packages in the expectation that an IRQ balancer will spread
940 * interrupts across them. */
941static int efx_wanted_rx_queues(void)
942{
2f8975fb 943 cpumask_var_t core_mask;
46123d04
BH
944 int count;
945 int cpu;
946
79f55997 947 if (unlikely(!zalloc_cpumask_var(&core_mask, GFP_KERNEL))) {
2f8975fb 948 printk(KERN_WARNING
3977d033 949 "sfc: RSS disabled due to allocation failure\n");
2f8975fb
RR
950 return 1;
951 }
952
46123d04
BH
953 count = 0;
954 for_each_online_cpu(cpu) {
2f8975fb 955 if (!cpumask_test_cpu(cpu, core_mask)) {
46123d04 956 ++count;
2f8975fb 957 cpumask_or(core_mask, core_mask,
fbd59a8d 958 topology_core_cpumask(cpu));
46123d04
BH
959 }
960 }
961
2f8975fb 962 free_cpumask_var(core_mask);
46123d04
BH
963 return count;
964}
965
966/* Probe the number and type of interrupts we are able to obtain, and
967 * the resulting numbers of channels and RX queues.
968 */
8ceee660
BH
969static void efx_probe_interrupts(struct efx_nic *efx)
970{
46123d04
BH
971 int max_channels =
972 min_t(int, efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
8ceee660
BH
973 int rc, i;
974
975 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
46123d04
BH
976 struct msix_entry xentries[EFX_MAX_CHANNELS];
977 int wanted_ints;
28b581ab 978 int rx_queues;
aa6ef27e 979
46123d04
BH
980 /* We want one RX queue and interrupt per CPU package
981 * (or as specified by the rss_cpus module parameter).
982 * We will need one channel per interrupt.
983 */
28b581ab
NT
984 rx_queues = rss_cpus ? rss_cpus : efx_wanted_rx_queues();
985 wanted_ints = rx_queues + (separate_tx_channels ? 1 : 0);
986 wanted_ints = min(wanted_ints, max_channels);
8ceee660 987
28b581ab 988 for (i = 0; i < wanted_ints; i++)
8ceee660 989 xentries[i].entry = i;
28b581ab 990 rc = pci_enable_msix(efx->pci_dev, xentries, wanted_ints);
8ceee660 991 if (rc > 0) {
28b581ab
NT
992 EFX_ERR(efx, "WARNING: Insufficient MSI-X vectors"
993 " available (%d < %d).\n", rc, wanted_ints);
994 EFX_ERR(efx, "WARNING: Performance may be reduced.\n");
995 EFX_BUG_ON_PARANOID(rc >= wanted_ints);
996 wanted_ints = rc;
8ceee660 997 rc = pci_enable_msix(efx->pci_dev, xentries,
28b581ab 998 wanted_ints);
8ceee660
BH
999 }
1000
1001 if (rc == 0) {
28b581ab
NT
1002 efx->n_rx_queues = min(rx_queues, wanted_ints);
1003 efx->n_channels = wanted_ints;
1004 for (i = 0; i < wanted_ints; i++)
8ceee660 1005 efx->channel[i].irq = xentries[i].vector;
8ceee660
BH
1006 } else {
1007 /* Fall back to single channel MSI */
1008 efx->interrupt_mode = EFX_INT_MODE_MSI;
1009 EFX_ERR(efx, "could not enable MSI-X\n");
1010 }
1011 }
1012
1013 /* Try single interrupt MSI */
1014 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
8831da7b 1015 efx->n_rx_queues = 1;
28b581ab 1016 efx->n_channels = 1;
8ceee660
BH
1017 rc = pci_enable_msi(efx->pci_dev);
1018 if (rc == 0) {
1019 efx->channel[0].irq = efx->pci_dev->irq;
8ceee660
BH
1020 } else {
1021 EFX_ERR(efx, "could not enable MSI\n");
1022 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1023 }
1024 }
1025
1026 /* Assume legacy interrupts */
1027 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
8831da7b 1028 efx->n_rx_queues = 1;
28b581ab 1029 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
8ceee660
BH
1030 efx->legacy_irq = efx->pci_dev->irq;
1031 }
1032}
1033
1034static void efx_remove_interrupts(struct efx_nic *efx)
1035{
1036 struct efx_channel *channel;
1037
1038 /* Remove MSI/MSI-X interrupts */
64ee3120 1039 efx_for_each_channel(channel, efx)
8ceee660
BH
1040 channel->irq = 0;
1041 pci_disable_msi(efx->pci_dev);
1042 pci_disable_msix(efx->pci_dev);
1043
1044 /* Remove legacy interrupt */
1045 efx->legacy_irq = 0;
1046}
1047
8831da7b 1048static void efx_set_channels(struct efx_nic *efx)
8ceee660
BH
1049{
1050 struct efx_tx_queue *tx_queue;
1051 struct efx_rx_queue *rx_queue;
8ceee660 1052
60ac1065 1053 efx_for_each_tx_queue(tx_queue, efx) {
28b581ab
NT
1054 if (separate_tx_channels)
1055 tx_queue->channel = &efx->channel[efx->n_channels-1];
60ac1065
BH
1056 else
1057 tx_queue->channel = &efx->channel[0];
1058 tx_queue->channel->used_flags |= EFX_USED_BY_TX;
1059 }
8ceee660 1060
8831da7b
BH
1061 efx_for_each_rx_queue(rx_queue, efx) {
1062 rx_queue->channel = &efx->channel[rx_queue->queue];
1063 rx_queue->channel->used_flags |= EFX_USED_BY_RX;
8ceee660
BH
1064 }
1065}
1066
1067static int efx_probe_nic(struct efx_nic *efx)
1068{
1069 int rc;
1070
1071 EFX_LOG(efx, "creating NIC\n");
1072
1073 /* Carry out hardware-type specific initialisation */
ef2b90ee 1074 rc = efx->type->probe(efx);
8ceee660
BH
1075 if (rc)
1076 return rc;
1077
1078 /* Determine the number of channels and RX queues by trying to hook
1079 * in MSI-X interrupts. */
1080 efx_probe_interrupts(efx);
1081
8831da7b 1082 efx_set_channels(efx);
8ceee660
BH
1083
1084 /* Initialise the interrupt moderation settings */
6fb70fd1 1085 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true);
8ceee660
BH
1086
1087 return 0;
1088}
1089
1090static void efx_remove_nic(struct efx_nic *efx)
1091{
1092 EFX_LOG(efx, "destroying NIC\n");
1093
1094 efx_remove_interrupts(efx);
ef2b90ee 1095 efx->type->remove(efx);
8ceee660
BH
1096}
1097
1098/**************************************************************************
1099 *
1100 * NIC startup/shutdown
1101 *
1102 *************************************************************************/
1103
1104static int efx_probe_all(struct efx_nic *efx)
1105{
1106 struct efx_channel *channel;
1107 int rc;
1108
1109 /* Create NIC */
1110 rc = efx_probe_nic(efx);
1111 if (rc) {
1112 EFX_ERR(efx, "failed to create NIC\n");
1113 goto fail1;
1114 }
1115
1116 /* Create port */
1117 rc = efx_probe_port(efx);
1118 if (rc) {
1119 EFX_ERR(efx, "failed to create port\n");
1120 goto fail2;
1121 }
1122
1123 /* Create channels */
1124 efx_for_each_channel(channel, efx) {
1125 rc = efx_probe_channel(channel);
1126 if (rc) {
1127 EFX_ERR(efx, "failed to create channel %d\n",
1128 channel->channel);
1129 goto fail3;
1130 }
1131 }
56536e9c 1132 efx_set_channel_names(efx);
8ceee660
BH
1133
1134 return 0;
1135
1136 fail3:
1137 efx_for_each_channel(channel, efx)
1138 efx_remove_channel(channel);
1139 efx_remove_port(efx);
1140 fail2:
1141 efx_remove_nic(efx);
1142 fail1:
1143 return rc;
1144}
1145
1146/* Called after previous invocation(s) of efx_stop_all, restarts the
1147 * port, kernel transmit queue, NAPI processing and hardware interrupts,
1148 * and ensures that the port is scheduled to be reconfigured.
1149 * This function is safe to call multiple times when the NIC is in any
1150 * state. */
1151static void efx_start_all(struct efx_nic *efx)
1152{
1153 struct efx_channel *channel;
1154
1155 EFX_ASSERT_RESET_SERIALISED(efx);
1156
1157 /* Check that it is appropriate to restart the interface. All
1158 * of these flags are safe to read under just the rtnl lock */
1159 if (efx->port_enabled)
1160 return;
1161 if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1162 return;
55668611 1163 if (efx_dev_registered(efx) && !netif_running(efx->net_dev))
8ceee660
BH
1164 return;
1165
1166 /* Mark the port as enabled so port reconfigurations can start, then
1167 * restart the transmit interface early so the watchdog timer stops */
1168 efx_start_port(efx);
dacccc74
SH
1169 if (efx_dev_registered(efx))
1170 efx_wake_queue(efx);
8ceee660
BH
1171
1172 efx_for_each_channel(channel, efx)
1173 efx_start_channel(channel);
1174
1175 falcon_enable_interrupts(efx);
1176
78c1f0a0
SH
1177 /* Start the hardware monitor if there is one. Otherwise (we're link
1178 * event driven), we have to poll the PHY because after an event queue
1179 * flush, we could have a missed a link state change */
1180 if (efx->type->monitor != NULL) {
8ceee660
BH
1181 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1182 efx_monitor_interval);
78c1f0a0
SH
1183 } else {
1184 mutex_lock(&efx->mac_lock);
1185 if (efx->phy_op->poll(efx))
1186 efx_link_status_changed(efx);
1187 mutex_unlock(&efx->mac_lock);
1188 }
55edc6e6 1189
ef2b90ee 1190 efx->type->start_stats(efx);
8ceee660
BH
1191}
1192
1193/* Flush all delayed work. Should only be called when no more delayed work
1194 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1195 * since we're holding the rtnl_lock at this point. */
1196static void efx_flush_all(struct efx_nic *efx)
1197{
1198 struct efx_rx_queue *rx_queue;
1199
1200 /* Make sure the hardware monitor is stopped */
1201 cancel_delayed_work_sync(&efx->monitor_work);
1202
1203 /* Ensure that all RX slow refills are complete. */
b3475645 1204 efx_for_each_rx_queue(rx_queue, efx)
8ceee660 1205 cancel_delayed_work_sync(&rx_queue->work);
8ceee660
BH
1206
1207 /* Stop scheduled port reconfigurations */
766ca0fa 1208 cancel_work_sync(&efx->mac_work);
8ceee660
BH
1209}
1210
1211/* Quiesce hardware and software without bringing the link down.
1212 * Safe to call multiple times, when the nic and interface is in any
1213 * state. The caller is guaranteed to subsequently be in a position
1214 * to modify any hardware and software state they see fit without
1215 * taking locks. */
1216static void efx_stop_all(struct efx_nic *efx)
1217{
1218 struct efx_channel *channel;
1219
1220 EFX_ASSERT_RESET_SERIALISED(efx);
1221
1222 /* port_enabled can be read safely under the rtnl lock */
1223 if (!efx->port_enabled)
1224 return;
1225
ef2b90ee 1226 efx->type->stop_stats(efx);
55edc6e6 1227
8ceee660
BH
1228 /* Disable interrupts and wait for ISR to complete */
1229 falcon_disable_interrupts(efx);
1230 if (efx->legacy_irq)
1231 synchronize_irq(efx->legacy_irq);
64ee3120 1232 efx_for_each_channel(channel, efx) {
8ceee660
BH
1233 if (channel->irq)
1234 synchronize_irq(channel->irq);
b3475645 1235 }
8ceee660
BH
1236
1237 /* Stop all NAPI processing and synchronous rx refills */
1238 efx_for_each_channel(channel, efx)
1239 efx_stop_channel(channel);
1240
1241 /* Stop all asynchronous port reconfigurations. Since all
1242 * event processing has already been stopped, there is no
1243 * window to loose phy events */
1244 efx_stop_port(efx);
1245
fdaa9aed 1246 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
8ceee660
BH
1247 efx_flush_all(efx);
1248
8ceee660
BH
1249 /* Stop the kernel transmit interface late, so the watchdog
1250 * timer isn't ticking over the flush */
55668611 1251 if (efx_dev_registered(efx)) {
dacccc74 1252 efx_stop_queue(efx);
8ceee660
BH
1253 netif_tx_lock_bh(efx->net_dev);
1254 netif_tx_unlock_bh(efx->net_dev);
1255 }
1256}
1257
1258static void efx_remove_all(struct efx_nic *efx)
1259{
1260 struct efx_channel *channel;
1261
1262 efx_for_each_channel(channel, efx)
1263 efx_remove_channel(channel);
1264 efx_remove_port(efx);
1265 efx_remove_nic(efx);
1266}
1267
8ceee660
BH
1268/**************************************************************************
1269 *
1270 * Interrupt moderation
1271 *
1272 **************************************************************************/
1273
0d86ebd8
BH
1274static unsigned irq_mod_ticks(int usecs, int resolution)
1275{
1276 if (usecs <= 0)
1277 return 0; /* cannot receive interrupts ahead of time :-) */
1278 if (usecs < resolution)
1279 return 1; /* never round down to 0 */
1280 return usecs / resolution;
1281}
1282
8ceee660 1283/* Set interrupt moderation parameters */
6fb70fd1
BH
1284void efx_init_irq_moderation(struct efx_nic *efx, int tx_usecs, int rx_usecs,
1285 bool rx_adaptive)
8ceee660
BH
1286{
1287 struct efx_tx_queue *tx_queue;
1288 struct efx_rx_queue *rx_queue;
0d86ebd8
BH
1289 unsigned tx_ticks = irq_mod_ticks(tx_usecs, FALCON_IRQ_MOD_RESOLUTION);
1290 unsigned rx_ticks = irq_mod_ticks(rx_usecs, FALCON_IRQ_MOD_RESOLUTION);
8ceee660
BH
1291
1292 EFX_ASSERT_RESET_SERIALISED(efx);
1293
1294 efx_for_each_tx_queue(tx_queue, efx)
0d86ebd8 1295 tx_queue->channel->irq_moderation = tx_ticks;
8ceee660 1296
6fb70fd1 1297 efx->irq_rx_adaptive = rx_adaptive;
0d86ebd8 1298 efx->irq_rx_moderation = rx_ticks;
8ceee660 1299 efx_for_each_rx_queue(rx_queue, efx)
0d86ebd8 1300 rx_queue->channel->irq_moderation = rx_ticks;
8ceee660
BH
1301}
1302
1303/**************************************************************************
1304 *
1305 * Hardware monitor
1306 *
1307 **************************************************************************/
1308
1309/* Run periodically off the general workqueue. Serialised against
1310 * efx_reconfigure_port via the mac_lock */
1311static void efx_monitor(struct work_struct *data)
1312{
1313 struct efx_nic *efx = container_of(data, struct efx_nic,
1314 monitor_work.work);
8ceee660
BH
1315
1316 EFX_TRACE(efx, "hardware monitor executing on CPU %d\n",
1317 raw_smp_processor_id());
ef2b90ee 1318 BUG_ON(efx->type->monitor == NULL);
8ceee660 1319
8ceee660
BH
1320 /* If the mac_lock is already held then it is likely a port
1321 * reconfiguration is already in place, which will likely do
1322 * most of the work of check_hw() anyway. */
766ca0fa
BH
1323 if (!mutex_trylock(&efx->mac_lock))
1324 goto out_requeue;
1325 if (!efx->port_enabled)
1326 goto out_unlock;
ef2b90ee 1327 efx->type->monitor(efx);
8ceee660 1328
766ca0fa 1329out_unlock:
8ceee660 1330 mutex_unlock(&efx->mac_lock);
766ca0fa 1331out_requeue:
8ceee660
BH
1332 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1333 efx_monitor_interval);
1334}
1335
1336/**************************************************************************
1337 *
1338 * ioctls
1339 *
1340 *************************************************************************/
1341
1342/* Net device ioctl
1343 * Context: process, rtnl_lock() held.
1344 */
1345static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1346{
767e468c 1347 struct efx_nic *efx = netdev_priv(net_dev);
68e7f45e 1348 struct mii_ioctl_data *data = if_mii(ifr);
8ceee660
BH
1349
1350 EFX_ASSERT_RESET_SERIALISED(efx);
1351
68e7f45e
BH
1352 /* Convert phy_id from older PRTAD/DEVAD format */
1353 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1354 (data->phy_id & 0xfc00) == 0x0400)
1355 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1356
1357 return mdio_mii_ioctl(&efx->mdio, data, cmd);
8ceee660
BH
1358}
1359
1360/**************************************************************************
1361 *
1362 * NAPI interface
1363 *
1364 **************************************************************************/
1365
1366static int efx_init_napi(struct efx_nic *efx)
1367{
1368 struct efx_channel *channel;
8ceee660
BH
1369
1370 efx_for_each_channel(channel, efx) {
1371 channel->napi_dev = efx->net_dev;
718cff1e
BH
1372 netif_napi_add(channel->napi_dev, &channel->napi_str,
1373 efx_poll, napi_weight);
8ceee660
BH
1374 }
1375 return 0;
8ceee660
BH
1376}
1377
1378static void efx_fini_napi(struct efx_nic *efx)
1379{
1380 struct efx_channel *channel;
1381
1382 efx_for_each_channel(channel, efx) {
718cff1e
BH
1383 if (channel->napi_dev)
1384 netif_napi_del(&channel->napi_str);
8ceee660
BH
1385 channel->napi_dev = NULL;
1386 }
1387}
1388
1389/**************************************************************************
1390 *
1391 * Kernel netpoll interface
1392 *
1393 *************************************************************************/
1394
1395#ifdef CONFIG_NET_POLL_CONTROLLER
1396
1397/* Although in the common case interrupts will be disabled, this is not
1398 * guaranteed. However, all our work happens inside the NAPI callback,
1399 * so no locking is required.
1400 */
1401static void efx_netpoll(struct net_device *net_dev)
1402{
767e468c 1403 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
1404 struct efx_channel *channel;
1405
64ee3120 1406 efx_for_each_channel(channel, efx)
8ceee660
BH
1407 efx_schedule_channel(channel);
1408}
1409
1410#endif
1411
1412/**************************************************************************
1413 *
1414 * Kernel net device interface
1415 *
1416 *************************************************************************/
1417
1418/* Context: process, rtnl_lock() held. */
1419static int efx_net_open(struct net_device *net_dev)
1420{
767e468c 1421 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
1422 EFX_ASSERT_RESET_SERIALISED(efx);
1423
1424 EFX_LOG(efx, "opening device %s on CPU %d\n", net_dev->name,
1425 raw_smp_processor_id());
1426
f4bd954e
BH
1427 if (efx->state == STATE_DISABLED)
1428 return -EIO;
f8b87c17
BH
1429 if (efx->phy_mode & PHY_MODE_SPECIAL)
1430 return -EBUSY;
1431
78c1f0a0
SH
1432 /* Notify the kernel of the link state polled during driver load,
1433 * before the monitor starts running */
1434 efx_link_status_changed(efx);
1435
8ceee660
BH
1436 efx_start_all(efx);
1437 return 0;
1438}
1439
1440/* Context: process, rtnl_lock() held.
1441 * Note that the kernel will ignore our return code; this method
1442 * should really be a void.
1443 */
1444static int efx_net_stop(struct net_device *net_dev)
1445{
767e468c 1446 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
1447
1448 EFX_LOG(efx, "closing %s on CPU %d\n", net_dev->name,
1449 raw_smp_processor_id());
1450
f4bd954e
BH
1451 if (efx->state != STATE_DISABLED) {
1452 /* Stop the device and flush all the channels */
1453 efx_stop_all(efx);
1454 efx_fini_channels(efx);
1455 efx_init_channels(efx);
1456 }
8ceee660
BH
1457
1458 return 0;
1459}
1460
5b9e207c 1461/* Context: process, dev_base_lock or RTNL held, non-blocking. */
8ceee660
BH
1462static struct net_device_stats *efx_net_stats(struct net_device *net_dev)
1463{
767e468c 1464 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
1465 struct efx_mac_stats *mac_stats = &efx->mac_stats;
1466 struct net_device_stats *stats = &net_dev->stats;
1467
55edc6e6 1468 spin_lock_bh(&efx->stats_lock);
ef2b90ee 1469 efx->type->update_stats(efx);
55edc6e6 1470 spin_unlock_bh(&efx->stats_lock);
8ceee660
BH
1471
1472 stats->rx_packets = mac_stats->rx_packets;
1473 stats->tx_packets = mac_stats->tx_packets;
1474 stats->rx_bytes = mac_stats->rx_bytes;
1475 stats->tx_bytes = mac_stats->tx_bytes;
1476 stats->multicast = mac_stats->rx_multicast;
1477 stats->collisions = mac_stats->tx_collision;
1478 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1479 mac_stats->rx_length_error);
1480 stats->rx_over_errors = efx->n_rx_nodesc_drop_cnt;
1481 stats->rx_crc_errors = mac_stats->rx_bad;
1482 stats->rx_frame_errors = mac_stats->rx_align_error;
1483 stats->rx_fifo_errors = mac_stats->rx_overflow;
1484 stats->rx_missed_errors = mac_stats->rx_missed;
1485 stats->tx_window_errors = mac_stats->tx_late_collision;
1486
1487 stats->rx_errors = (stats->rx_length_errors +
1488 stats->rx_over_errors +
1489 stats->rx_crc_errors +
1490 stats->rx_frame_errors +
1491 stats->rx_fifo_errors +
1492 stats->rx_missed_errors +
1493 mac_stats->rx_symbol_error);
1494 stats->tx_errors = (stats->tx_window_errors +
1495 mac_stats->tx_bad);
1496
1497 return stats;
1498}
1499
1500/* Context: netif_tx_lock held, BHs disabled. */
1501static void efx_watchdog(struct net_device *net_dev)
1502{
767e468c 1503 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660 1504
739bb23d
BH
1505 EFX_ERR(efx, "TX stuck with stop_count=%d port_enabled=%d:"
1506 " resetting channels\n",
1507 atomic_read(&efx->netif_stop_count), efx->port_enabled);
8ceee660 1508
739bb23d 1509 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
8ceee660
BH
1510}
1511
1512
1513/* Context: process, rtnl_lock() held. */
1514static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1515{
767e468c 1516 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
1517 int rc = 0;
1518
1519 EFX_ASSERT_RESET_SERIALISED(efx);
1520
1521 if (new_mtu > EFX_MAX_MTU)
1522 return -EINVAL;
1523
1524 efx_stop_all(efx);
1525
1526 EFX_LOG(efx, "changing MTU to %d\n", new_mtu);
1527
1528 efx_fini_channels(efx);
d3245b28
BH
1529
1530 mutex_lock(&efx->mac_lock);
1531 /* Reconfigure the MAC before enabling the dma queues so that
1532 * the RX buffers don't overflow */
8ceee660 1533 net_dev->mtu = new_mtu;
d3245b28
BH
1534 efx->mac_op->reconfigure(efx);
1535 mutex_unlock(&efx->mac_lock);
1536
bc3c90a2 1537 efx_init_channels(efx);
8ceee660
BH
1538
1539 efx_start_all(efx);
1540 return rc;
8ceee660
BH
1541}
1542
1543static int efx_set_mac_address(struct net_device *net_dev, void *data)
1544{
767e468c 1545 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
1546 struct sockaddr *addr = data;
1547 char *new_addr = addr->sa_data;
1548
1549 EFX_ASSERT_RESET_SERIALISED(efx);
1550
1551 if (!is_valid_ether_addr(new_addr)) {
e174961c
JB
1552 EFX_ERR(efx, "invalid ethernet MAC address requested: %pM\n",
1553 new_addr);
8ceee660
BH
1554 return -EINVAL;
1555 }
1556
1557 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1558
1559 /* Reconfigure the MAC */
d3245b28
BH
1560 mutex_lock(&efx->mac_lock);
1561 efx->mac_op->reconfigure(efx);
1562 mutex_unlock(&efx->mac_lock);
8ceee660
BH
1563
1564 return 0;
1565}
1566
a816f75a 1567/* Context: netif_addr_lock held, BHs disabled. */
8ceee660
BH
1568static void efx_set_multicast_list(struct net_device *net_dev)
1569{
767e468c 1570 struct efx_nic *efx = netdev_priv(net_dev);
8ceee660
BH
1571 struct dev_mc_list *mc_list = net_dev->mc_list;
1572 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
8ceee660
BH
1573 u32 crc;
1574 int bit;
1575 int i;
1576
8be4f3e6 1577 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
8ceee660
BH
1578
1579 /* Build multicast hash table */
8be4f3e6 1580 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
8ceee660
BH
1581 memset(mc_hash, 0xff, sizeof(*mc_hash));
1582 } else {
1583 memset(mc_hash, 0x00, sizeof(*mc_hash));
1584 for (i = 0; i < net_dev->mc_count; i++) {
1585 crc = ether_crc_le(ETH_ALEN, mc_list->dmi_addr);
1586 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1587 set_bit_le(bit, mc_hash->byte);
1588 mc_list = mc_list->next;
1589 }
8ceee660 1590
8be4f3e6
BH
1591 /* Broadcast packets go through the multicast hash filter.
1592 * ether_crc_le() of the broadcast address is 0xbe2612ff
1593 * so we always add bit 0xff to the mask.
1594 */
1595 set_bit_le(0xff, mc_hash->byte);
1596 }
a816f75a 1597
8be4f3e6
BH
1598 if (efx->port_enabled)
1599 queue_work(efx->workqueue, &efx->mac_work);
1600 /* Otherwise efx_start_port() will do this */
8ceee660
BH
1601}
1602
c3ecb9f3
SH
1603static const struct net_device_ops efx_netdev_ops = {
1604 .ndo_open = efx_net_open,
1605 .ndo_stop = efx_net_stop,
1606 .ndo_get_stats = efx_net_stats,
1607 .ndo_tx_timeout = efx_watchdog,
1608 .ndo_start_xmit = efx_hard_start_xmit,
1609 .ndo_validate_addr = eth_validate_addr,
1610 .ndo_do_ioctl = efx_ioctl,
1611 .ndo_change_mtu = efx_change_mtu,
1612 .ndo_set_mac_address = efx_set_mac_address,
1613 .ndo_set_multicast_list = efx_set_multicast_list,
1614#ifdef CONFIG_NET_POLL_CONTROLLER
1615 .ndo_poll_controller = efx_netpoll,
1616#endif
1617};
1618
7dde596e
BH
1619static void efx_update_name(struct efx_nic *efx)
1620{
1621 strcpy(efx->name, efx->net_dev->name);
1622 efx_mtd_rename(efx);
1623 efx_set_channel_names(efx);
1624}
1625
8ceee660
BH
1626static int efx_netdev_event(struct notifier_block *this,
1627 unsigned long event, void *ptr)
1628{
d3208b5e 1629 struct net_device *net_dev = ptr;
8ceee660 1630
7dde596e
BH
1631 if (net_dev->netdev_ops == &efx_netdev_ops &&
1632 event == NETDEV_CHANGENAME)
1633 efx_update_name(netdev_priv(net_dev));
8ceee660
BH
1634
1635 return NOTIFY_DONE;
1636}
1637
1638static struct notifier_block efx_netdev_notifier = {
1639 .notifier_call = efx_netdev_event,
1640};
1641
06d5e193
BH
1642static ssize_t
1643show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
1644{
1645 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
1646 return sprintf(buf, "%d\n", efx->phy_type);
1647}
1648static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
1649
8ceee660
BH
1650static int efx_register_netdev(struct efx_nic *efx)
1651{
1652 struct net_device *net_dev = efx->net_dev;
1653 int rc;
1654
1655 net_dev->watchdog_timeo = 5 * HZ;
1656 net_dev->irq = efx->pci_dev->irq;
c3ecb9f3 1657 net_dev->netdev_ops = &efx_netdev_ops;
8ceee660
BH
1658 SET_NETDEV_DEV(net_dev, &efx->pci_dev->dev);
1659 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
1660
8ceee660 1661 /* Clear MAC statistics */
177dfcd8 1662 efx->mac_op->update_stats(efx);
8ceee660
BH
1663 memset(&efx->mac_stats, 0, sizeof(efx->mac_stats));
1664
7dde596e 1665 rtnl_lock();
aed0628d
BH
1666
1667 rc = dev_alloc_name(net_dev, net_dev->name);
1668 if (rc < 0)
1669 goto fail_locked;
7dde596e 1670 efx_update_name(efx);
aed0628d
BH
1671
1672 rc = register_netdevice(net_dev);
1673 if (rc)
1674 goto fail_locked;
1675
1676 /* Always start with carrier off; PHY events will detect the link */
1677 netif_carrier_off(efx->net_dev);
1678
7dde596e 1679 rtnl_unlock();
8ceee660 1680
06d5e193
BH
1681 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
1682 if (rc) {
1683 EFX_ERR(efx, "failed to init net dev attributes\n");
1684 goto fail_registered;
1685 }
1686
8ceee660 1687 return 0;
06d5e193 1688
aed0628d
BH
1689fail_locked:
1690 rtnl_unlock();
1691 EFX_ERR(efx, "could not register net dev\n");
1692 return rc;
1693
06d5e193
BH
1694fail_registered:
1695 unregister_netdev(net_dev);
1696 return rc;
8ceee660
BH
1697}
1698
1699static void efx_unregister_netdev(struct efx_nic *efx)
1700{
1701 struct efx_tx_queue *tx_queue;
1702
1703 if (!efx->net_dev)
1704 return;
1705
767e468c 1706 BUG_ON(netdev_priv(efx->net_dev) != efx);
8ceee660
BH
1707
1708 /* Free up any skbs still remaining. This has to happen before
1709 * we try to unregister the netdev as running their destructors
1710 * may be needed to get the device ref. count to 0. */
1711 efx_for_each_tx_queue(tx_queue, efx)
1712 efx_release_tx_buffers(tx_queue);
1713
55668611 1714 if (efx_dev_registered(efx)) {
8ceee660 1715 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
06d5e193 1716 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
8ceee660
BH
1717 unregister_netdev(efx->net_dev);
1718 }
1719}
1720
1721/**************************************************************************
1722 *
1723 * Device reset and suspend
1724 *
1725 **************************************************************************/
1726
2467ca46
BH
1727/* Tears down the entire software state and most of the hardware state
1728 * before reset. */
d3245b28 1729void efx_reset_down(struct efx_nic *efx, enum reset_type method)
8ceee660 1730{
8ceee660
BH
1731 EFX_ASSERT_RESET_SERIALISED(efx);
1732
2467ca46
BH
1733 efx_stop_all(efx);
1734 mutex_lock(&efx->mac_lock);
f4150724 1735 mutex_lock(&efx->spi_lock);
2467ca46 1736
8ceee660 1737 efx_fini_channels(efx);
4b988280
SH
1738 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
1739 efx->phy_op->fini(efx);
ef2b90ee 1740 efx->type->fini(efx);
8ceee660
BH
1741}
1742
2467ca46
BH
1743/* This function will always ensure that the locks acquired in
1744 * efx_reset_down() are released. A failure return code indicates
1745 * that we were unable to reinitialise the hardware, and the
1746 * driver should be disabled. If ok is false, then the rx and tx
1747 * engines are not restarted, pending a RESET_DISABLE. */
d3245b28 1748int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
8ceee660
BH
1749{
1750 int rc;
1751
2467ca46 1752 EFX_ASSERT_RESET_SERIALISED(efx);
8ceee660 1753
ef2b90ee 1754 rc = efx->type->init(efx);
8ceee660 1755 if (rc) {
2467ca46
BH
1756 EFX_ERR(efx, "failed to initialise NIC\n");
1757 ok = false;
8ceee660
BH
1758 }
1759
4b988280
SH
1760 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
1761 if (ok) {
1762 rc = efx->phy_op->init(efx);
1763 if (rc)
1764 ok = false;
d3245b28
BH
1765 if (efx->phy_op->reconfigure(efx))
1766 EFX_ERR(efx, "could not restore PHY settings\n");
115122af
BH
1767 }
1768 if (!ok)
4b988280
SH
1769 efx->port_initialized = false;
1770 }
1771
2467ca46 1772 if (ok) {
d3245b28 1773 efx->mac_op->reconfigure(efx);
8ceee660 1774
d3245b28 1775 efx_init_channels(efx);
2467ca46
BH
1776 }
1777
f4150724 1778 mutex_unlock(&efx->spi_lock);
2467ca46
BH
1779 mutex_unlock(&efx->mac_lock);
1780
55edc6e6 1781 if (ok)
2467ca46 1782 efx_start_all(efx);
8ceee660
BH
1783 return rc;
1784}
1785
1786/* Reset the NIC as transparently as possible. Do not reset the PHY
1787 * Note that the reset may fail, in which case the card will be left
1788 * in a most-probably-unusable state.
1789 *
1790 * This function will sleep. You cannot reset from within an atomic
1791 * state; use efx_schedule_reset() instead.
1792 *
1793 * Grabs the rtnl_lock.
1794 */
1795static int efx_reset(struct efx_nic *efx)
1796{
8ceee660 1797 enum reset_type method = efx->reset_pending;
f4bd954e 1798 int rc = 0;
8ceee660
BH
1799
1800 /* Serialise with kernel interfaces */
1801 rtnl_lock();
1802
1803 /* If we're not RUNNING then don't reset. Leave the reset_pending
1804 * flag set so that efx_pci_probe_main will be retried */
1805 if (efx->state != STATE_RUNNING) {
1806 EFX_INFO(efx, "scheduled reset quenched. NIC not RUNNING\n");
f4bd954e 1807 goto out_unlock;
8ceee660
BH
1808 }
1809
c459302d 1810 EFX_INFO(efx, "resetting (%s)\n", RESET_TYPE(method));
8ceee660 1811
d3245b28 1812 efx_reset_down(efx, method);
8ceee660 1813
ef2b90ee 1814 rc = efx->type->reset(efx, method);
8ceee660
BH
1815 if (rc) {
1816 EFX_ERR(efx, "failed to reset hardware\n");
f4bd954e 1817 goto out_disable;
8ceee660
BH
1818 }
1819
1820 /* Allow resets to be rescheduled. */
1821 efx->reset_pending = RESET_TYPE_NONE;
1822
1823 /* Reinitialise bus-mastering, which may have been turned off before
1824 * the reset was scheduled. This is still appropriate, even in the
1825 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
1826 * can respond to requests. */
1827 pci_set_master(efx->pci_dev);
1828
8ceee660
BH
1829 /* Leave device stopped if necessary */
1830 if (method == RESET_TYPE_DISABLE) {
d3245b28 1831 efx_reset_up(efx, method, false);
8ceee660 1832 rc = -EIO;
f4bd954e 1833 } else {
d3245b28 1834 rc = efx_reset_up(efx, method, true);
8ceee660
BH
1835 }
1836
f4bd954e
BH
1837out_disable:
1838 if (rc) {
1839 EFX_ERR(efx, "has been disabled\n");
1840 efx->state = STATE_DISABLED;
1841 dev_close(efx->net_dev);
1842 } else {
1843 EFX_LOG(efx, "reset complete\n");
1844 }
8ceee660 1845
f4bd954e 1846out_unlock:
8ceee660 1847 rtnl_unlock();
8ceee660
BH
1848 return rc;
1849}
1850
1851/* The worker thread exists so that code that cannot sleep can
1852 * schedule a reset for later.
1853 */
1854static void efx_reset_work(struct work_struct *data)
1855{
1856 struct efx_nic *nic = container_of(data, struct efx_nic, reset_work);
1857
1858 efx_reset(nic);
1859}
1860
1861void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
1862{
1863 enum reset_type method;
1864
1865 if (efx->reset_pending != RESET_TYPE_NONE) {
1866 EFX_INFO(efx, "quenching already scheduled reset\n");
1867 return;
1868 }
1869
1870 switch (type) {
1871 case RESET_TYPE_INVISIBLE:
1872 case RESET_TYPE_ALL:
1873 case RESET_TYPE_WORLD:
1874 case RESET_TYPE_DISABLE:
1875 method = type;
1876 break;
1877 case RESET_TYPE_RX_RECOVERY:
1878 case RESET_TYPE_RX_DESC_FETCH:
1879 case RESET_TYPE_TX_DESC_FETCH:
1880 case RESET_TYPE_TX_SKIP:
1881 method = RESET_TYPE_INVISIBLE;
1882 break;
1883 default:
1884 method = RESET_TYPE_ALL;
1885 break;
1886 }
1887
1888 if (method != type)
c459302d
BH
1889 EFX_LOG(efx, "scheduling %s reset for %s\n",
1890 RESET_TYPE(method), RESET_TYPE(type));
8ceee660 1891 else
c459302d 1892 EFX_LOG(efx, "scheduling %s reset\n", RESET_TYPE(method));
8ceee660
BH
1893
1894 efx->reset_pending = method;
1895
1ab00629 1896 queue_work(reset_workqueue, &efx->reset_work);
8ceee660
BH
1897}
1898
1899/**************************************************************************
1900 *
1901 * List of NICs we support
1902 *
1903 **************************************************************************/
1904
1905/* PCI device ID table */
1906static struct pci_device_id efx_pci_table[] __devinitdata = {
1907 {PCI_DEVICE(EFX_VENDID_SFC, FALCON_A_P_DEVID),
daeda630 1908 .driver_data = (unsigned long) &falcon_a1_nic_type},
8ceee660 1909 {PCI_DEVICE(EFX_VENDID_SFC, FALCON_B_P_DEVID),
daeda630 1910 .driver_data = (unsigned long) &falcon_b0_nic_type},
8ceee660
BH
1911 {0} /* end of list */
1912};
1913
1914/**************************************************************************
1915 *
3759433d 1916 * Dummy PHY/MAC operations
8ceee660 1917 *
01aad7b6 1918 * Can be used for some unimplemented operations
8ceee660
BH
1919 * Needed so all function pointers are valid and do not have to be tested
1920 * before use
1921 *
1922 **************************************************************************/
1923int efx_port_dummy_op_int(struct efx_nic *efx)
1924{
1925 return 0;
1926}
1927void efx_port_dummy_op_void(struct efx_nic *efx) {}
398468ed
BH
1928void efx_port_dummy_op_set_id_led(struct efx_nic *efx, enum efx_led_mode mode)
1929{
1930}
fdaa9aed
SH
1931bool efx_port_dummy_op_poll(struct efx_nic *efx)
1932{
1933 return false;
1934}
8ceee660
BH
1935
1936static struct efx_phy_operations efx_dummy_phy_operations = {
1937 .init = efx_port_dummy_op_int,
d3245b28 1938 .reconfigure = efx_port_dummy_op_int,
fdaa9aed 1939 .poll = efx_port_dummy_op_poll,
8ceee660 1940 .fini = efx_port_dummy_op_void,
8ceee660
BH
1941};
1942
8ceee660
BH
1943/**************************************************************************
1944 *
1945 * Data housekeeping
1946 *
1947 **************************************************************************/
1948
1949/* This zeroes out and then fills in the invariants in a struct
1950 * efx_nic (including all sub-structures).
1951 */
1952static int efx_init_struct(struct efx_nic *efx, struct efx_nic_type *type,
1953 struct pci_dev *pci_dev, struct net_device *net_dev)
1954{
1955 struct efx_channel *channel;
1956 struct efx_tx_queue *tx_queue;
1957 struct efx_rx_queue *rx_queue;
1ab00629 1958 int i;
8ceee660
BH
1959
1960 /* Initialise common structures */
1961 memset(efx, 0, sizeof(*efx));
1962 spin_lock_init(&efx->biu_lock);
ab867461 1963 mutex_init(&efx->mdio_lock);
f4150724 1964 mutex_init(&efx->spi_lock);
8ceee660
BH
1965 INIT_WORK(&efx->reset_work, efx_reset_work);
1966 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
1967 efx->pci_dev = pci_dev;
1968 efx->state = STATE_INIT;
1969 efx->reset_pending = RESET_TYPE_NONE;
1970 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
8ceee660
BH
1971
1972 efx->net_dev = net_dev;
dc8cfa55 1973 efx->rx_checksum_enabled = true;
8ceee660
BH
1974 spin_lock_init(&efx->netif_stop_lock);
1975 spin_lock_init(&efx->stats_lock);
1976 mutex_init(&efx->mac_lock);
b895d73e 1977 efx->mac_op = type->default_mac_ops;
8ceee660 1978 efx->phy_op = &efx_dummy_phy_operations;
68e7f45e 1979 efx->mdio.dev = net_dev;
766ca0fa 1980 INIT_WORK(&efx->mac_work, efx_mac_work);
8ceee660
BH
1981 atomic_set(&efx->netif_stop_count, 1);
1982
1983 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
1984 channel = &efx->channel[i];
1985 channel->efx = efx;
1986 channel->channel = i;
dc8cfa55 1987 channel->work_pending = false;
8ceee660 1988 }
60ac1065 1989 for (i = 0; i < EFX_TX_QUEUE_COUNT; i++) {
8ceee660
BH
1990 tx_queue = &efx->tx_queue[i];
1991 tx_queue->efx = efx;
1992 tx_queue->queue = i;
1993 tx_queue->buffer = NULL;
1994 tx_queue->channel = &efx->channel[0]; /* for safety */
b9b39b62 1995 tx_queue->tso_headers_free = NULL;
8ceee660
BH
1996 }
1997 for (i = 0; i < EFX_MAX_RX_QUEUES; i++) {
1998 rx_queue = &efx->rx_queue[i];
1999 rx_queue->efx = efx;
2000 rx_queue->queue = i;
2001 rx_queue->channel = &efx->channel[0]; /* for safety */
2002 rx_queue->buffer = NULL;
2003 spin_lock_init(&rx_queue->add_lock);
2004 INIT_DELAYED_WORK(&rx_queue->work, efx_rx_work);
2005 }
2006
2007 efx->type = type;
2008
8ceee660 2009 /* As close as we can get to guaranteeing that we don't overflow */
3ffeabdd
BH
2010 BUILD_BUG_ON(EFX_EVQ_SIZE < EFX_TXQ_SIZE + EFX_RXQ_SIZE);
2011
8ceee660
BH
2012 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2013
2014 /* Higher numbered interrupt modes are less capable! */
2015 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2016 interrupt_mode);
2017
6977dc63
BH
2018 /* Would be good to use the net_dev name, but we're too early */
2019 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2020 pci_name(pci_dev));
2021 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
1ab00629
SH
2022 if (!efx->workqueue)
2023 return -ENOMEM;
8d9853d9 2024
8ceee660 2025 return 0;
8ceee660
BH
2026}
2027
2028static void efx_fini_struct(struct efx_nic *efx)
2029{
2030 if (efx->workqueue) {
2031 destroy_workqueue(efx->workqueue);
2032 efx->workqueue = NULL;
2033 }
2034}
2035
2036/**************************************************************************
2037 *
2038 * PCI interface
2039 *
2040 **************************************************************************/
2041
2042/* Main body of final NIC shutdown code
2043 * This is called only at module unload (or hotplug removal).
2044 */
2045static void efx_pci_remove_main(struct efx_nic *efx)
2046{
f01865f0 2047 falcon_fini_interrupt(efx);
8ceee660
BH
2048 efx_fini_channels(efx);
2049 efx_fini_port(efx);
ef2b90ee 2050 efx->type->fini(efx);
8ceee660
BH
2051 efx_fini_napi(efx);
2052 efx_remove_all(efx);
2053}
2054
2055/* Final NIC shutdown
2056 * This is called only at module unload (or hotplug removal).
2057 */
2058static void efx_pci_remove(struct pci_dev *pci_dev)
2059{
2060 struct efx_nic *efx;
2061
2062 efx = pci_get_drvdata(pci_dev);
2063 if (!efx)
2064 return;
2065
2066 /* Mark the NIC as fini, then stop the interface */
2067 rtnl_lock();
2068 efx->state = STATE_FINI;
2069 dev_close(efx->net_dev);
2070
2071 /* Allow any queued efx_resets() to complete */
2072 rtnl_unlock();
2073
8ceee660
BH
2074 efx_unregister_netdev(efx);
2075
7dde596e
BH
2076 efx_mtd_remove(efx);
2077
8ceee660
BH
2078 /* Wait for any scheduled resets to complete. No more will be
2079 * scheduled from this point because efx_stop_all() has been
2080 * called, we are no longer registered with driverlink, and
2081 * the net_device's have been removed. */
1ab00629 2082 cancel_work_sync(&efx->reset_work);
8ceee660
BH
2083
2084 efx_pci_remove_main(efx);
2085
8ceee660
BH
2086 efx_fini_io(efx);
2087 EFX_LOG(efx, "shutdown successful\n");
2088
2089 pci_set_drvdata(pci_dev, NULL);
2090 efx_fini_struct(efx);
2091 free_netdev(efx->net_dev);
2092};
2093
2094/* Main body of NIC initialisation
2095 * This is called at module load (or hotplug insertion, theoretically).
2096 */
2097static int efx_pci_probe_main(struct efx_nic *efx)
2098{
2099 int rc;
2100
2101 /* Do start-of-day initialisation */
2102 rc = efx_probe_all(efx);
2103 if (rc)
2104 goto fail1;
2105
2106 rc = efx_init_napi(efx);
2107 if (rc)
2108 goto fail2;
2109
ef2b90ee 2110 rc = efx->type->init(efx);
8ceee660
BH
2111 if (rc) {
2112 EFX_ERR(efx, "failed to initialise NIC\n");
278c0621 2113 goto fail3;
8ceee660
BH
2114 }
2115
2116 rc = efx_init_port(efx);
2117 if (rc) {
2118 EFX_ERR(efx, "failed to initialise port\n");
278c0621 2119 goto fail4;
8ceee660
BH
2120 }
2121
bc3c90a2 2122 efx_init_channels(efx);
8ceee660
BH
2123
2124 rc = falcon_init_interrupt(efx);
2125 if (rc)
278c0621 2126 goto fail5;
8ceee660
BH
2127
2128 return 0;
2129
278c0621 2130 fail5:
bc3c90a2 2131 efx_fini_channels(efx);
8ceee660 2132 efx_fini_port(efx);
8ceee660 2133 fail4:
ef2b90ee 2134 efx->type->fini(efx);
8ceee660
BH
2135 fail3:
2136 efx_fini_napi(efx);
2137 fail2:
2138 efx_remove_all(efx);
2139 fail1:
2140 return rc;
2141}
2142
2143/* NIC initialisation
2144 *
2145 * This is called at module load (or hotplug insertion,
2146 * theoretically). It sets up PCI mappings, tests and resets the NIC,
2147 * sets up and registers the network devices with the kernel and hooks
2148 * the interrupt service routine. It does not prepare the device for
2149 * transmission; this is left to the first time one of the network
2150 * interfaces is brought up (i.e. efx_net_open).
2151 */
2152static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2153 const struct pci_device_id *entry)
2154{
2155 struct efx_nic_type *type = (struct efx_nic_type *) entry->driver_data;
2156 struct net_device *net_dev;
2157 struct efx_nic *efx;
2158 int i, rc;
2159
2160 /* Allocate and initialise a struct net_device and struct efx_nic */
2161 net_dev = alloc_etherdev(sizeof(*efx));
2162 if (!net_dev)
2163 return -ENOMEM;
b9b39b62 2164 net_dev->features |= (NETIF_F_IP_CSUM | NETIF_F_SG |
97bc5415
BH
2165 NETIF_F_HIGHDMA | NETIF_F_TSO |
2166 NETIF_F_GRO);
28506563
BH
2167 /* Mask for features that also apply to VLAN devices */
2168 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
740847da 2169 NETIF_F_HIGHDMA | NETIF_F_TSO);
767e468c 2170 efx = netdev_priv(net_dev);
8ceee660
BH
2171 pci_set_drvdata(pci_dev, efx);
2172 rc = efx_init_struct(efx, type, pci_dev, net_dev);
2173 if (rc)
2174 goto fail1;
2175
2176 EFX_INFO(efx, "Solarflare Communications NIC detected\n");
2177
2178 /* Set up basic I/O (BAR mappings etc) */
2179 rc = efx_init_io(efx);
2180 if (rc)
2181 goto fail2;
2182
2183 /* No serialisation is required with the reset path because
2184 * we're in STATE_INIT. */
2185 for (i = 0; i < 5; i++) {
2186 rc = efx_pci_probe_main(efx);
8ceee660
BH
2187
2188 /* Serialise against efx_reset(). No more resets will be
2189 * scheduled since efx_stop_all() has been called, and we
2190 * have not and never have been registered with either
2191 * the rtnetlink or driverlink layers. */
1ab00629 2192 cancel_work_sync(&efx->reset_work);
8ceee660 2193
fa402b2e
SH
2194 if (rc == 0) {
2195 if (efx->reset_pending != RESET_TYPE_NONE) {
2196 /* If there was a scheduled reset during
2197 * probe, the NIC is probably hosed anyway */
2198 efx_pci_remove_main(efx);
2199 rc = -EIO;
2200 } else {
2201 break;
2202 }
2203 }
2204
8ceee660
BH
2205 /* Retry if a recoverably reset event has been scheduled */
2206 if ((efx->reset_pending != RESET_TYPE_INVISIBLE) &&
2207 (efx->reset_pending != RESET_TYPE_ALL))
2208 goto fail3;
2209
2210 efx->reset_pending = RESET_TYPE_NONE;
2211 }
2212
2213 if (rc) {
2214 EFX_ERR(efx, "Could not reset NIC\n");
2215 goto fail4;
2216 }
2217
55edc6e6
BH
2218 /* Switch to the running state before we expose the device to the OS,
2219 * so that dev_open()|efx_start_all() will actually start the device */
8ceee660 2220 efx->state = STATE_RUNNING;
7dde596e 2221
8ceee660
BH
2222 rc = efx_register_netdev(efx);
2223 if (rc)
2224 goto fail5;
2225
2226 EFX_LOG(efx, "initialisation successful\n");
a5211bb5
BH
2227
2228 rtnl_lock();
2229 efx_mtd_probe(efx); /* allowed to fail */
2230 rtnl_unlock();
8ceee660
BH
2231 return 0;
2232
2233 fail5:
2234 efx_pci_remove_main(efx);
2235 fail4:
2236 fail3:
2237 efx_fini_io(efx);
2238 fail2:
2239 efx_fini_struct(efx);
2240 fail1:
2241 EFX_LOG(efx, "initialisation failed. rc=%d\n", rc);
2242 free_netdev(net_dev);
2243 return rc;
2244}
2245
2246static struct pci_driver efx_pci_driver = {
2247 .name = EFX_DRIVER_NAME,
2248 .id_table = efx_pci_table,
2249 .probe = efx_pci_probe,
2250 .remove = efx_pci_remove,
2251};
2252
2253/**************************************************************************
2254 *
2255 * Kernel module interface
2256 *
2257 *************************************************************************/
2258
2259module_param(interrupt_mode, uint, 0444);
2260MODULE_PARM_DESC(interrupt_mode,
2261 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2262
2263static int __init efx_init_module(void)
2264{
2265 int rc;
2266
2267 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2268
2269 rc = register_netdevice_notifier(&efx_netdev_notifier);
2270 if (rc)
2271 goto err_notifier;
2272
2273 refill_workqueue = create_workqueue("sfc_refill");
2274 if (!refill_workqueue) {
2275 rc = -ENOMEM;
2276 goto err_refill;
2277 }
1ab00629
SH
2278 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2279 if (!reset_workqueue) {
2280 rc = -ENOMEM;
2281 goto err_reset;
2282 }
8ceee660
BH
2283
2284 rc = pci_register_driver(&efx_pci_driver);
2285 if (rc < 0)
2286 goto err_pci;
2287
2288 return 0;
2289
2290 err_pci:
1ab00629
SH
2291 destroy_workqueue(reset_workqueue);
2292 err_reset:
8ceee660
BH
2293 destroy_workqueue(refill_workqueue);
2294 err_refill:
2295 unregister_netdevice_notifier(&efx_netdev_notifier);
2296 err_notifier:
2297 return rc;
2298}
2299
2300static void __exit efx_exit_module(void)
2301{
2302 printk(KERN_INFO "Solarflare NET driver unloading\n");
2303
2304 pci_unregister_driver(&efx_pci_driver);
1ab00629 2305 destroy_workqueue(reset_workqueue);
8ceee660
BH
2306 destroy_workqueue(refill_workqueue);
2307 unregister_netdevice_notifier(&efx_netdev_notifier);
2308
2309}
2310
2311module_init(efx_init_module);
2312module_exit(efx_exit_module);
2313
2314MODULE_AUTHOR("Michael Brown <mbrown@fensystems.co.uk> and "
2315 "Solarflare Communications");
2316MODULE_DESCRIPTION("Solarflare Communications network driver");
2317MODULE_LICENSE("GPL");
2318MODULE_DEVICE_TABLE(pci, efx_pci_table);