Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2005-2006 Fen Systems Ltd.
5 * Copyright 2005-2013 Solarflare Communications Inc.
6 */
7
8#include <linux/filter.h>
9#include <linux/module.h>
10#include <linux/pci.h>
11#include <linux/netdevice.h>
12#include <linux/etherdevice.h>
13#include <linux/delay.h>
14#include <linux/notifier.h>
15#include <linux/ip.h>
16#include <linux/tcp.h>
17#include <linux/in.h>
18#include <linux/ethtool.h>
19#include <linux/topology.h>
20#include <linux/gfp.h>
21#include <linux/interrupt.h>
22#include "net_driver.h"
23#include <net/gre.h>
24#include <net/udp_tunnel.h>
25#include <net/netdev_queues.h>
26#include "efx.h"
27#include "efx_common.h"
28#include "efx_channels.h"
29#include "ef100.h"
30#include "rx_common.h"
31#include "tx_common.h"
32#include "nic.h"
33#include "io.h"
34#include "selftest.h"
35#include "sriov.h"
36#include "efx_devlink.h"
37
38#include "mcdi_port_common.h"
39#include "mcdi_pcol.h"
40#include "workarounds.h"
41
42/**************************************************************************
43 *
44 * Configurable values
45 *
46 *************************************************************************/
47
48module_param_named(interrupt_mode, efx_interrupt_mode, uint, 0444);
49MODULE_PARM_DESC(interrupt_mode,
50 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
51
52module_param(rss_cpus, uint, 0444);
53MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
54
55/*
56 * Use separate channels for TX and RX events
57 *
58 * Set this to 1 to use separate channels for TX and RX. It allows us
59 * to control interrupt affinity separately for TX and RX.
60 *
61 * This is only used in MSI-X interrupt mode
62 */
63bool efx_separate_tx_channels;
64module_param(efx_separate_tx_channels, bool, 0444);
65MODULE_PARM_DESC(efx_separate_tx_channels,
66 "Use separate channels for TX and RX");
67
68/* Initial interrupt moderation settings. They can be modified after
69 * module load with ethtool.
70 *
71 * The default for RX should strike a balance between increasing the
72 * round-trip latency and reducing overhead.
73 */
74static unsigned int rx_irq_mod_usec = 60;
75
76/* Initial interrupt moderation settings. They can be modified after
77 * module load with ethtool.
78 *
79 * This default is chosen to ensure that a 10G link does not go idle
80 * while a TX queue is stopped after it has become full. A queue is
81 * restarted when it drops below half full. The time this takes (assuming
82 * worst case 3 descriptors per packet and 1024 descriptors) is
83 * 512 / 3 * 1.2 = 205 usec.
84 */
85static unsigned int tx_irq_mod_usec = 150;
86
87static bool phy_flash_cfg;
88module_param(phy_flash_cfg, bool, 0644);
89MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
90
91static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
92 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
93 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
94 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
95module_param(debug, uint, 0);
96MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
97
98/**************************************************************************
99 *
100 * Utility functions and prototypes
101 *
102 *************************************************************************/
103
104static void efx_remove_port(struct efx_nic *efx);
105static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog);
106static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp);
107static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
108 u32 flags);
109
110/**************************************************************************
111 *
112 * Port handling
113 *
114 **************************************************************************/
115
116static void efx_fini_port(struct efx_nic *efx);
117
118static int efx_probe_port(struct efx_nic *efx)
119{
120 int rc;
121
122 netif_dbg(efx, probe, efx->net_dev, "create port\n");
123
124 if (phy_flash_cfg)
125 efx->phy_mode = PHY_MODE_SPECIAL;
126
127 /* Connect up MAC/PHY operations table */
128 rc = efx->type->probe_port(efx);
129 if (rc)
130 return rc;
131
132 /* Initialise MAC address to permanent address */
133 eth_hw_addr_set(efx->net_dev, efx->net_dev->perm_addr);
134
135 return 0;
136}
137
138static int efx_init_port(struct efx_nic *efx)
139{
140 int rc;
141
142 netif_dbg(efx, drv, efx->net_dev, "init port\n");
143
144 mutex_lock(&efx->mac_lock);
145
146 efx->port_initialized = true;
147
148 /* Ensure the PHY advertises the correct flow control settings */
149 rc = efx_mcdi_port_reconfigure(efx);
150 if (rc && rc != -EPERM)
151 goto fail;
152
153 mutex_unlock(&efx->mac_lock);
154 return 0;
155
156fail:
157 mutex_unlock(&efx->mac_lock);
158 return rc;
159}
160
161static void efx_fini_port(struct efx_nic *efx)
162{
163 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
164
165 if (!efx->port_initialized)
166 return;
167
168 efx->port_initialized = false;
169
170 efx->link_state.up = false;
171 efx_link_status_changed(efx);
172}
173
174static void efx_remove_port(struct efx_nic *efx)
175{
176 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
177
178 efx->type->remove_port(efx);
179}
180
181/**************************************************************************
182 *
183 * NIC handling
184 *
185 **************************************************************************/
186
187static LIST_HEAD(efx_primary_list);
188static LIST_HEAD(efx_unassociated_list);
189
190static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
191{
192 return left->type == right->type &&
193 left->vpd_sn && right->vpd_sn &&
194 !strcmp(left->vpd_sn, right->vpd_sn);
195}
196
197static void efx_associate(struct efx_nic *efx)
198{
199 struct efx_nic *other, *next;
200
201 if (efx->primary == efx) {
202 /* Adding primary function; look for secondaries */
203
204 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
205 list_add_tail(&efx->node, &efx_primary_list);
206
207 list_for_each_entry_safe(other, next, &efx_unassociated_list,
208 node) {
209 if (efx_same_controller(efx, other)) {
210 list_del(&other->node);
211 netif_dbg(other, probe, other->net_dev,
212 "moving to secondary list of %s %s\n",
213 pci_name(efx->pci_dev),
214 efx->net_dev->name);
215 list_add_tail(&other->node,
216 &efx->secondary_list);
217 other->primary = efx;
218 }
219 }
220 } else {
221 /* Adding secondary function; look for primary */
222
223 list_for_each_entry(other, &efx_primary_list, node) {
224 if (efx_same_controller(efx, other)) {
225 netif_dbg(efx, probe, efx->net_dev,
226 "adding to secondary list of %s %s\n",
227 pci_name(other->pci_dev),
228 other->net_dev->name);
229 list_add_tail(&efx->node,
230 &other->secondary_list);
231 efx->primary = other;
232 return;
233 }
234 }
235
236 netif_dbg(efx, probe, efx->net_dev,
237 "adding to unassociated list\n");
238 list_add_tail(&efx->node, &efx_unassociated_list);
239 }
240}
241
242static void efx_dissociate(struct efx_nic *efx)
243{
244 struct efx_nic *other, *next;
245
246 list_del(&efx->node);
247 efx->primary = NULL;
248
249 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
250 list_del(&other->node);
251 netif_dbg(other, probe, other->net_dev,
252 "moving to unassociated list\n");
253 list_add_tail(&other->node, &efx_unassociated_list);
254 other->primary = NULL;
255 }
256}
257
258static int efx_probe_nic(struct efx_nic *efx)
259{
260 int rc;
261
262 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
263
264 /* Carry out hardware-type specific initialisation */
265 rc = efx->type->probe(efx);
266 if (rc)
267 return rc;
268
269 do {
270 if (!efx->max_channels || !efx->max_tx_channels) {
271 netif_err(efx, drv, efx->net_dev,
272 "Insufficient resources to allocate"
273 " any channels\n");
274 rc = -ENOSPC;
275 goto fail1;
276 }
277
278 /* Determine the number of channels and queues by trying
279 * to hook in MSI-X interrupts.
280 */
281 rc = efx_probe_interrupts(efx);
282 if (rc)
283 goto fail1;
284
285 rc = efx_set_channels(efx);
286 if (rc)
287 goto fail1;
288
289 /* dimension_resources can fail with EAGAIN */
290 rc = efx->type->dimension_resources(efx);
291 if (rc != 0 && rc != -EAGAIN)
292 goto fail2;
293
294 if (rc == -EAGAIN)
295 /* try again with new max_channels */
296 efx_remove_interrupts(efx);
297
298 } while (rc == -EAGAIN);
299
300 if (efx->n_channels > 1)
301 netdev_rss_key_fill(efx->rss_context.rx_hash_key,
302 sizeof(efx->rss_context.rx_hash_key));
303 efx_set_default_rx_indir_table(efx, efx->rss_context.rx_indir_table);
304
305 /* Initialise the interrupt moderation settings */
306 efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
307 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
308 true);
309
310 return 0;
311
312fail2:
313 efx_remove_interrupts(efx);
314fail1:
315 efx->type->remove(efx);
316 return rc;
317}
318
319static void efx_remove_nic(struct efx_nic *efx)
320{
321 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
322
323 efx_remove_interrupts(efx);
324 efx->type->remove(efx);
325}
326
327/**************************************************************************
328 *
329 * NIC startup/shutdown
330 *
331 *************************************************************************/
332
333static int efx_probe_all(struct efx_nic *efx)
334{
335 int rc;
336
337 rc = efx_probe_nic(efx);
338 if (rc) {
339 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
340 goto fail1;
341 }
342
343 rc = efx_probe_port(efx);
344 if (rc) {
345 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
346 goto fail2;
347 }
348
349 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
350 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
351 rc = -EINVAL;
352 goto fail3;
353 }
354
355#ifdef CONFIG_SFC_SRIOV
356 rc = efx->type->vswitching_probe(efx);
357 if (rc) /* not fatal; the PF will still work fine */
358 netif_warn(efx, probe, efx->net_dev,
359 "failed to setup vswitching rc=%d;"
360 " VFs may not function\n", rc);
361#endif
362
363 rc = efx_probe_filters(efx);
364 if (rc) {
365 netif_err(efx, probe, efx->net_dev,
366 "failed to create filter tables\n");
367 goto fail4;
368 }
369
370 rc = efx_probe_channels(efx);
371 if (rc)
372 goto fail5;
373
374 efx->state = STATE_NET_DOWN;
375
376 return 0;
377
378 fail5:
379 efx_remove_filters(efx);
380 fail4:
381#ifdef CONFIG_SFC_SRIOV
382 efx->type->vswitching_remove(efx);
383#endif
384 fail3:
385 efx_remove_port(efx);
386 fail2:
387 efx_remove_nic(efx);
388 fail1:
389 return rc;
390}
391
392static void efx_remove_all(struct efx_nic *efx)
393{
394 rtnl_lock();
395 efx_xdp_setup_prog(efx, NULL);
396 rtnl_unlock();
397
398 efx_remove_channels(efx);
399 efx_remove_filters(efx);
400#ifdef CONFIG_SFC_SRIOV
401 efx->type->vswitching_remove(efx);
402#endif
403 efx_remove_port(efx);
404 efx_remove_nic(efx);
405}
406
407/**************************************************************************
408 *
409 * Interrupt moderation
410 *
411 **************************************************************************/
412unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
413{
414 if (usecs == 0)
415 return 0;
416 if (usecs * 1000 < efx->timer_quantum_ns)
417 return 1; /* never round down to 0 */
418 return usecs * 1000 / efx->timer_quantum_ns;
419}
420
421/* Set interrupt moderation parameters */
422int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
423 unsigned int rx_usecs, bool rx_adaptive,
424 bool rx_may_override_tx)
425{
426 struct efx_channel *channel;
427 unsigned int timer_max_us;
428
429 EFX_ASSERT_RESET_SERIALISED(efx);
430
431 timer_max_us = efx->timer_max_ns / 1000;
432
433 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
434 return -EINVAL;
435
436 if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
437 !rx_may_override_tx) {
438 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
439 "RX and TX IRQ moderation must be equal\n");
440 return -EINVAL;
441 }
442
443 efx->irq_rx_adaptive = rx_adaptive;
444 efx->irq_rx_moderation_us = rx_usecs;
445 efx_for_each_channel(channel, efx) {
446 if (efx_channel_has_rx_queue(channel))
447 channel->irq_moderation_us = rx_usecs;
448 else if (efx_channel_has_tx_queues(channel))
449 channel->irq_moderation_us = tx_usecs;
450 else if (efx_channel_is_xdp_tx(channel))
451 channel->irq_moderation_us = tx_usecs;
452 }
453
454 return 0;
455}
456
457void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
458 unsigned int *rx_usecs, bool *rx_adaptive)
459{
460 *rx_adaptive = efx->irq_rx_adaptive;
461 *rx_usecs = efx->irq_rx_moderation_us;
462
463 /* If channels are shared between RX and TX, so is IRQ
464 * moderation. Otherwise, IRQ moderation is the same for all
465 * TX channels and is not adaptive.
466 */
467 if (efx->tx_channel_offset == 0) {
468 *tx_usecs = *rx_usecs;
469 } else {
470 struct efx_channel *tx_channel;
471
472 tx_channel = efx->channel[efx->tx_channel_offset];
473 *tx_usecs = tx_channel->irq_moderation_us;
474 }
475}
476
477/**************************************************************************
478 *
479 * ioctls
480 *
481 *************************************************************************/
482
483/* Net device ioctl
484 * Context: process, rtnl_lock() held.
485 */
486static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
487{
488 struct efx_nic *efx = efx_netdev_priv(net_dev);
489 struct mii_ioctl_data *data = if_mii(ifr);
490
491 /* Convert phy_id from older PRTAD/DEVAD format */
492 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
493 (data->phy_id & 0xfc00) == 0x0400)
494 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
495
496 return mdio_mii_ioctl(&efx->mdio, data, cmd);
497}
498
499/**************************************************************************
500 *
501 * Kernel net device interface
502 *
503 *************************************************************************/
504
505/* Context: process, rtnl_lock() held. */
506int efx_net_open(struct net_device *net_dev)
507{
508 struct efx_nic *efx = efx_netdev_priv(net_dev);
509 int rc;
510
511 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
512 raw_smp_processor_id());
513
514 rc = efx_check_disabled(efx);
515 if (rc)
516 return rc;
517 if (efx->phy_mode & PHY_MODE_SPECIAL)
518 return -EBUSY;
519 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
520 return -EIO;
521
522 /* Notify the kernel of the link state polled during driver load,
523 * before the monitor starts running */
524 efx_link_status_changed(efx);
525
526 efx_start_all(efx);
527 if (efx->state == STATE_DISABLED || efx->reset_pending)
528 netif_device_detach(efx->net_dev);
529 else
530 efx->state = STATE_NET_UP;
531
532 return 0;
533}
534
535/* Context: process, rtnl_lock() held.
536 * Note that the kernel will ignore our return code; this method
537 * should really be a void.
538 */
539int efx_net_stop(struct net_device *net_dev)
540{
541 struct efx_nic *efx = efx_netdev_priv(net_dev);
542
543 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
544 raw_smp_processor_id());
545
546 /* Stop the device and flush all the channels */
547 efx_stop_all(efx);
548
549 return 0;
550}
551
552static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
553{
554 struct efx_nic *efx = efx_netdev_priv(net_dev);
555
556 if (efx->type->vlan_rx_add_vid)
557 return efx->type->vlan_rx_add_vid(efx, proto, vid);
558 else
559 return -EOPNOTSUPP;
560}
561
562static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
563{
564 struct efx_nic *efx = efx_netdev_priv(net_dev);
565
566 if (efx->type->vlan_rx_kill_vid)
567 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
568 else
569 return -EOPNOTSUPP;
570}
571
572static int efx_hwtstamp_set(struct net_device *net_dev,
573 struct kernel_hwtstamp_config *config,
574 struct netlink_ext_ack *extack)
575{
576 struct efx_nic *efx = efx_netdev_priv(net_dev);
577
578 return efx_ptp_set_ts_config(efx, config, extack);
579}
580
581static int efx_hwtstamp_get(struct net_device *net_dev,
582 struct kernel_hwtstamp_config *config)
583{
584 struct efx_nic *efx = efx_netdev_priv(net_dev);
585
586 return efx_ptp_get_ts_config(efx, config);
587}
588
589static const struct net_device_ops efx_netdev_ops = {
590 .ndo_open = efx_net_open,
591 .ndo_stop = efx_net_stop,
592 .ndo_get_stats64 = efx_net_stats,
593 .ndo_tx_timeout = efx_watchdog,
594 .ndo_start_xmit = efx_hard_start_xmit,
595 .ndo_validate_addr = eth_validate_addr,
596 .ndo_eth_ioctl = efx_ioctl,
597 .ndo_change_mtu = efx_change_mtu,
598 .ndo_set_mac_address = efx_set_mac_address,
599 .ndo_set_rx_mode = efx_set_rx_mode,
600 .ndo_set_features = efx_set_features,
601 .ndo_features_check = efx_features_check,
602 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
603 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
604 .ndo_hwtstamp_set = efx_hwtstamp_set,
605 .ndo_hwtstamp_get = efx_hwtstamp_get,
606#ifdef CONFIG_SFC_SRIOV
607 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
608 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
609 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
610 .ndo_get_vf_config = efx_sriov_get_vf_config,
611 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
612#endif
613 .ndo_get_phys_port_id = efx_get_phys_port_id,
614 .ndo_get_phys_port_name = efx_get_phys_port_name,
615#ifdef CONFIG_RFS_ACCEL
616 .ndo_rx_flow_steer = efx_filter_rfs,
617#endif
618 .ndo_xdp_xmit = efx_xdp_xmit,
619 .ndo_bpf = efx_xdp
620};
621
622static void efx_get_queue_stats_rx(struct net_device *net_dev, int idx,
623 struct netdev_queue_stats_rx *stats)
624{
625 struct efx_nic *efx = efx_netdev_priv(net_dev);
626 struct efx_rx_queue *rx_queue;
627 struct efx_channel *channel;
628
629 channel = efx_get_channel(efx, idx);
630 rx_queue = efx_channel_get_rx_queue(channel);
631 /* Count only packets since last time datapath was started */
632 stats->packets = rx_queue->rx_packets - rx_queue->old_rx_packets;
633 stats->bytes = rx_queue->rx_bytes - rx_queue->old_rx_bytes;
634 stats->hw_drops = efx_get_queue_stat_rx_hw_drops(channel) -
635 channel->old_n_rx_hw_drops;
636 stats->hw_drop_overruns = channel->n_rx_nodesc_trunc -
637 channel->old_n_rx_hw_drop_overruns;
638}
639
640static void efx_get_queue_stats_tx(struct net_device *net_dev, int idx,
641 struct netdev_queue_stats_tx *stats)
642{
643 struct efx_nic *efx = efx_netdev_priv(net_dev);
644 struct efx_tx_queue *tx_queue;
645 struct efx_channel *channel;
646
647 channel = efx_get_tx_channel(efx, idx);
648 stats->packets = 0;
649 stats->bytes = 0;
650 stats->hw_gso_packets = 0;
651 stats->hw_gso_wire_packets = 0;
652 efx_for_each_channel_tx_queue(tx_queue, channel) {
653 stats->packets += tx_queue->complete_packets -
654 tx_queue->old_complete_packets;
655 stats->bytes += tx_queue->complete_bytes -
656 tx_queue->old_complete_bytes;
657 /* Note that, unlike stats->packets and stats->bytes,
658 * these count TXes enqueued, rather than completed,
659 * which may not be what users expect.
660 */
661 stats->hw_gso_packets += tx_queue->tso_bursts -
662 tx_queue->old_tso_bursts;
663 stats->hw_gso_wire_packets += tx_queue->tso_packets -
664 tx_queue->old_tso_packets;
665 }
666}
667
668static void efx_get_base_stats(struct net_device *net_dev,
669 struct netdev_queue_stats_rx *rx,
670 struct netdev_queue_stats_tx *tx)
671{
672 struct efx_nic *efx = efx_netdev_priv(net_dev);
673 struct efx_tx_queue *tx_queue;
674 struct efx_rx_queue *rx_queue;
675 struct efx_channel *channel;
676
677 rx->packets = 0;
678 rx->bytes = 0;
679 rx->hw_drops = 0;
680 rx->hw_drop_overruns = 0;
681 tx->packets = 0;
682 tx->bytes = 0;
683 tx->hw_gso_packets = 0;
684 tx->hw_gso_wire_packets = 0;
685
686 /* Count all packets on non-core queues, and packets before last
687 * datapath start on core queues.
688 */
689 efx_for_each_channel(channel, efx) {
690 rx_queue = efx_channel_get_rx_queue(channel);
691 if (channel->channel >= net_dev->real_num_rx_queues) {
692 rx->packets += rx_queue->rx_packets;
693 rx->bytes += rx_queue->rx_bytes;
694 rx->hw_drops += efx_get_queue_stat_rx_hw_drops(channel);
695 rx->hw_drop_overruns += channel->n_rx_nodesc_trunc;
696 } else {
697 rx->packets += rx_queue->old_rx_packets;
698 rx->bytes += rx_queue->old_rx_bytes;
699 rx->hw_drops += channel->old_n_rx_hw_drops;
700 rx->hw_drop_overruns += channel->old_n_rx_hw_drop_overruns;
701 }
702 efx_for_each_channel_tx_queue(tx_queue, channel) {
703 if (channel->channel < efx->tx_channel_offset ||
704 channel->channel >= efx->tx_channel_offset +
705 net_dev->real_num_tx_queues) {
706 tx->packets += tx_queue->complete_packets;
707 tx->bytes += tx_queue->complete_bytes;
708 tx->hw_gso_packets += tx_queue->tso_bursts;
709 tx->hw_gso_wire_packets += tx_queue->tso_packets;
710 } else {
711 tx->packets += tx_queue->old_complete_packets;
712 tx->bytes += tx_queue->old_complete_bytes;
713 tx->hw_gso_packets += tx_queue->old_tso_bursts;
714 tx->hw_gso_wire_packets += tx_queue->old_tso_packets;
715 }
716 /* Include XDP TX in device-wide stats */
717 tx->packets += tx_queue->complete_xdp_packets;
718 tx->bytes += tx_queue->complete_xdp_bytes;
719 }
720 }
721}
722
723static const struct netdev_stat_ops efx_stat_ops = {
724 .get_queue_stats_rx = efx_get_queue_stats_rx,
725 .get_queue_stats_tx = efx_get_queue_stats_tx,
726 .get_base_stats = efx_get_base_stats,
727};
728
729static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
730{
731 struct bpf_prog *old_prog;
732
733 if (efx->xdp_rxq_info_failed) {
734 netif_err(efx, drv, efx->net_dev,
735 "Unable to bind XDP program due to previous failure of rxq_info\n");
736 return -EINVAL;
737 }
738
739 if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) {
740 netif_err(efx, drv, efx->net_dev,
741 "Unable to configure XDP with MTU of %d (max: %d)\n",
742 efx->net_dev->mtu, efx_xdp_max_mtu(efx));
743 return -EINVAL;
744 }
745
746 old_prog = rtnl_dereference(efx->xdp_prog);
747 rcu_assign_pointer(efx->xdp_prog, prog);
748 /* Release the reference that was originally passed by the caller. */
749 if (old_prog)
750 bpf_prog_put(old_prog);
751
752 return 0;
753}
754
755/* Context: process, rtnl_lock() held. */
756static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
757{
758 struct efx_nic *efx = efx_netdev_priv(dev);
759
760 switch (xdp->command) {
761 case XDP_SETUP_PROG:
762 return efx_xdp_setup_prog(efx, xdp->prog);
763 default:
764 return -EINVAL;
765 }
766}
767
768static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
769 u32 flags)
770{
771 struct efx_nic *efx = efx_netdev_priv(dev);
772
773 if (!netif_running(dev))
774 return -EINVAL;
775
776 return efx_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH);
777}
778
779static void efx_update_name(struct efx_nic *efx)
780{
781 strcpy(efx->name, efx->net_dev->name);
782 efx_mtd_rename(efx);
783 efx_set_channel_names(efx);
784}
785
786static int efx_netdev_event(struct notifier_block *this,
787 unsigned long event, void *ptr)
788{
789 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
790
791 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
792 event == NETDEV_CHANGENAME)
793 efx_update_name(efx_netdev_priv(net_dev));
794
795 return NOTIFY_DONE;
796}
797
798static struct notifier_block efx_netdev_notifier = {
799 .notifier_call = efx_netdev_event,
800};
801
802static ssize_t phy_type_show(struct device *dev,
803 struct device_attribute *attr, char *buf)
804{
805 struct efx_nic *efx = dev_get_drvdata(dev);
806 return sprintf(buf, "%d\n", efx->phy_type);
807}
808static DEVICE_ATTR_RO(phy_type);
809
810static int efx_register_netdev(struct efx_nic *efx)
811{
812 struct net_device *net_dev = efx->net_dev;
813 struct efx_channel *channel;
814 int rc;
815
816 net_dev->watchdog_timeo = 5 * HZ;
817 net_dev->irq = efx->pci_dev->irq;
818 net_dev->netdev_ops = &efx_netdev_ops;
819 net_dev->stat_ops = &efx_stat_ops;
820 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
821 net_dev->priv_flags |= IFF_UNICAST_FLT;
822 net_dev->ethtool_ops = &efx_ethtool_ops;
823 netif_set_tso_max_segs(net_dev, EFX_TSO_MAX_SEGS);
824 net_dev->min_mtu = EFX_MIN_MTU;
825 net_dev->max_mtu = EFX_MAX_MTU;
826
827 rtnl_lock();
828
829 /* Enable resets to be scheduled and check whether any were
830 * already requested. If so, the NIC is probably hosed so we
831 * abort.
832 */
833 if (efx->reset_pending) {
834 pci_err(efx->pci_dev, "aborting probe due to scheduled reset\n");
835 rc = -EIO;
836 goto fail_locked;
837 }
838
839 rc = dev_alloc_name(net_dev, net_dev->name);
840 if (rc < 0)
841 goto fail_locked;
842 efx_update_name(efx);
843
844 /* Always start with carrier off; PHY events will detect the link */
845 netif_carrier_off(net_dev);
846
847 rc = register_netdevice(net_dev);
848 if (rc)
849 goto fail_locked;
850
851 efx_for_each_channel(channel, efx) {
852 struct efx_tx_queue *tx_queue;
853 efx_for_each_channel_tx_queue(tx_queue, channel)
854 efx_init_tx_queue_core_txq(tx_queue);
855 }
856
857 efx_associate(efx);
858
859 efx->state = STATE_NET_DOWN;
860
861 rtnl_unlock();
862
863 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
864 if (rc) {
865 netif_err(efx, drv, efx->net_dev,
866 "failed to init net dev attributes\n");
867 goto fail_registered;
868 }
869
870 efx_init_mcdi_logging(efx);
871
872 return 0;
873
874fail_registered:
875 rtnl_lock();
876 efx_dissociate(efx);
877 unregister_netdevice(net_dev);
878fail_locked:
879 efx->state = STATE_UNINIT;
880 rtnl_unlock();
881 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
882 return rc;
883}
884
885static void efx_unregister_netdev(struct efx_nic *efx)
886{
887 if (!efx->net_dev)
888 return;
889
890 if (WARN_ON(efx_netdev_priv(efx->net_dev) != efx))
891 return;
892
893 if (efx_dev_registered(efx)) {
894 strscpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
895 efx_fini_mcdi_logging(efx);
896 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
897 unregister_netdev(efx->net_dev);
898 }
899}
900
901/**************************************************************************
902 *
903 * List of NICs we support
904 *
905 **************************************************************************/
906
907/* PCI device ID table */
908static const struct pci_device_id efx_pci_table[] = {
909 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
910 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
911 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
912 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
913 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
914 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
915 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
916 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
917 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
918 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
919 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
920 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
921 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
922 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
923 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
924 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
925 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0c03), /* X4 PF (FF/LL) */
926 .driver_data = (unsigned long)&efx_x4_nic_type},
927 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x2c03), /* X4 PF (FF only) */
928 .driver_data = (unsigned long)&efx_x4_nic_type},
929 {0} /* end of list */
930};
931
932/**************************************************************************
933 *
934 * Data housekeeping
935 *
936 **************************************************************************/
937
938void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
939{
940 u64 n_rx_nodesc_trunc = 0;
941 struct efx_channel *channel;
942
943 efx_for_each_channel(channel, efx)
944 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
945 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
946 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
947}
948
949/**************************************************************************
950 *
951 * PCI interface
952 *
953 **************************************************************************/
954
955/* Main body of final NIC shutdown code
956 * This is called only at module unload (or hotplug removal).
957 */
958static void efx_pci_remove_main(struct efx_nic *efx)
959{
960 /* Flush reset_work. It can no longer be scheduled since we
961 * are not READY.
962 */
963 WARN_ON(efx_net_active(efx->state));
964 efx_flush_reset_workqueue(efx);
965
966 efx_disable_interrupts(efx);
967 efx_clear_interrupt_affinity(efx);
968 efx_nic_fini_interrupt(efx);
969 efx_fini_port(efx);
970 efx->type->fini(efx);
971 efx_fini_napi(efx);
972 efx_remove_all(efx);
973}
974
975/* Final NIC shutdown
976 * This is called only at module unload (or hotplug removal). A PF can call
977 * this on its VFs to ensure they are unbound first.
978 */
979static void efx_pci_remove(struct pci_dev *pci_dev)
980{
981 struct efx_probe_data *probe_data;
982 struct efx_nic *efx;
983
984 efx = pci_get_drvdata(pci_dev);
985 if (!efx)
986 return;
987
988 /* Mark the NIC as fini, then stop the interface */
989 rtnl_lock();
990 efx_dissociate(efx);
991 dev_close(efx->net_dev);
992 efx_disable_interrupts(efx);
993 efx->state = STATE_UNINIT;
994 rtnl_unlock();
995
996 if (efx->type->sriov_fini)
997 efx->type->sriov_fini(efx);
998
999 efx_fini_devlink_lock(efx);
1000 efx_unregister_netdev(efx);
1001
1002 efx_mtd_remove(efx);
1003
1004 efx_pci_remove_main(efx);
1005
1006 efx_fini_io(efx);
1007 pci_dbg(efx->pci_dev, "shutdown successful\n");
1008
1009 efx_fini_devlink_and_unlock(efx);
1010 efx_fini_struct(efx);
1011 free_netdev(efx->net_dev);
1012 probe_data = container_of(efx, struct efx_probe_data, efx);
1013 kfree(probe_data);
1014};
1015
1016/* NIC VPD information
1017 * Called during probe to display the part number of the
1018 * installed NIC.
1019 */
1020static void efx_probe_vpd_strings(struct efx_nic *efx)
1021{
1022 struct pci_dev *dev = efx->pci_dev;
1023 unsigned int vpd_size, kw_len;
1024 u8 *vpd_data;
1025 int start;
1026
1027 vpd_data = pci_vpd_alloc(dev, &vpd_size);
1028 if (IS_ERR(vpd_data)) {
1029 pci_warn(dev, "Unable to read VPD\n");
1030 return;
1031 }
1032
1033 start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
1034 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
1035 if (start < 0)
1036 pci_err(dev, "Part number not found or incomplete\n");
1037 else
1038 pci_info(dev, "Part Number : %.*s\n", kw_len, vpd_data + start);
1039
1040 start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
1041 PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);
1042 if (start < 0)
1043 pci_err(dev, "Serial number not found or incomplete\n");
1044 else
1045 efx->vpd_sn = kmemdup_nul(vpd_data + start, kw_len, GFP_KERNEL);
1046
1047 kfree(vpd_data);
1048}
1049
1050
1051/* Main body of NIC initialisation
1052 * This is called at module load (or hotplug insertion, theoretically).
1053 */
1054static int efx_pci_probe_main(struct efx_nic *efx)
1055{
1056 int rc;
1057
1058 /* Do start-of-day initialisation */
1059 rc = efx_probe_all(efx);
1060 if (rc)
1061 goto fail1;
1062
1063 efx_init_napi(efx);
1064
1065 down_write(&efx->filter_sem);
1066 rc = efx->type->init(efx);
1067 up_write(&efx->filter_sem);
1068 if (rc) {
1069 pci_err(efx->pci_dev, "failed to initialise NIC\n");
1070 goto fail3;
1071 }
1072
1073 rc = efx_init_port(efx);
1074 if (rc) {
1075 netif_err(efx, probe, efx->net_dev,
1076 "failed to initialise port\n");
1077 goto fail4;
1078 }
1079
1080 rc = efx_nic_init_interrupt(efx);
1081 if (rc)
1082 goto fail5;
1083
1084 efx_set_interrupt_affinity(efx);
1085 rc = efx_enable_interrupts(efx);
1086 if (rc)
1087 goto fail6;
1088
1089 return 0;
1090
1091 fail6:
1092 efx_clear_interrupt_affinity(efx);
1093 efx_nic_fini_interrupt(efx);
1094 fail5:
1095 efx_fini_port(efx);
1096 fail4:
1097 efx->type->fini(efx);
1098 fail3:
1099 efx_fini_napi(efx);
1100 efx_remove_all(efx);
1101 fail1:
1102 return rc;
1103}
1104
1105static int efx_pci_probe_post_io(struct efx_nic *efx)
1106{
1107 struct net_device *net_dev = efx->net_dev;
1108 int rc = efx_pci_probe_main(efx);
1109
1110 if (rc)
1111 return rc;
1112
1113 if (efx->type->sriov_init) {
1114 rc = efx->type->sriov_init(efx);
1115 if (rc)
1116 pci_err(efx->pci_dev, "SR-IOV can't be enabled rc %d\n",
1117 rc);
1118 }
1119
1120 /* Determine netdevice features */
1121 net_dev->features |= efx->type->offload_features;
1122
1123 /* Add TSO features */
1124 if (efx->type->tso_versions && efx->type->tso_versions(efx))
1125 net_dev->features |= NETIF_F_TSO | NETIF_F_TSO6;
1126
1127 /* Mask for features that also apply to VLAN devices */
1128 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
1129 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
1130 NETIF_F_RXCSUM);
1131
1132 /* Determine user configurable features */
1133 net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
1134
1135 /* Disable receiving frames with bad FCS, by default. */
1136 net_dev->features &= ~NETIF_F_RXALL;
1137
1138 /* Disable VLAN filtering by default. It may be enforced if
1139 * the feature is fixed (i.e. VLAN filters are required to
1140 * receive VLAN tagged packets due to vPort restrictions).
1141 */
1142 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1143 net_dev->features |= efx->fixed_features;
1144
1145 net_dev->xdp_features = NETDEV_XDP_ACT_BASIC |
1146 NETDEV_XDP_ACT_REDIRECT |
1147 NETDEV_XDP_ACT_NDO_XMIT;
1148
1149 /* devlink creation, registration and lock */
1150 rc = efx_probe_devlink_and_lock(efx);
1151 if (rc)
1152 pci_err(efx->pci_dev, "devlink registration failed");
1153
1154 rc = efx_register_netdev(efx);
1155 efx_probe_devlink_unlock(efx);
1156 if (!rc)
1157 return 0;
1158
1159 efx_pci_remove_main(efx);
1160 return rc;
1161}
1162
1163/* NIC initialisation
1164 *
1165 * This is called at module load (or hotplug insertion,
1166 * theoretically). It sets up PCI mappings, resets the NIC,
1167 * sets up and registers the network devices with the kernel and hooks
1168 * the interrupt service routine. It does not prepare the device for
1169 * transmission; this is left to the first time one of the network
1170 * interfaces is brought up (i.e. efx_net_open).
1171 */
1172static int efx_pci_probe(struct pci_dev *pci_dev,
1173 const struct pci_device_id *entry)
1174{
1175 struct efx_probe_data *probe_data, **probe_ptr;
1176 struct net_device *net_dev;
1177 struct efx_nic *efx;
1178 int rc;
1179
1180 /* Allocate probe data and struct efx_nic */
1181 probe_data = kzalloc(sizeof(*probe_data), GFP_KERNEL);
1182 if (!probe_data)
1183 return -ENOMEM;
1184 probe_data->pci_dev = pci_dev;
1185 efx = &probe_data->efx;
1186
1187 /* Allocate and initialise a struct net_device */
1188 net_dev = alloc_etherdev_mq(sizeof(probe_data), EFX_MAX_CORE_TX_QUEUES);
1189 if (!net_dev) {
1190 rc = -ENOMEM;
1191 goto fail0;
1192 }
1193 probe_ptr = netdev_priv(net_dev);
1194 *probe_ptr = probe_data;
1195 efx->net_dev = net_dev;
1196 efx->type = (const struct efx_nic_type *) entry->driver_data;
1197 efx->fixed_features |= NETIF_F_HIGHDMA;
1198
1199 pci_set_drvdata(pci_dev, efx);
1200 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
1201 rc = efx_init_struct(efx, pci_dev);
1202 if (rc)
1203 goto fail1;
1204 efx->mdio.dev = net_dev;
1205
1206 pci_info(pci_dev, "Solarflare NIC detected\n");
1207
1208 if (!efx->type->is_vf)
1209 efx_probe_vpd_strings(efx);
1210
1211 /* Set up basic I/O (BAR mappings etc) */
1212 rc = efx_init_io(efx, efx->type->mem_bar(efx), efx->type->max_dma_mask,
1213 efx->type->mem_map_size(efx));
1214 if (rc)
1215 goto fail2;
1216
1217 rc = efx_pci_probe_post_io(efx);
1218 if (rc) {
1219 /* On failure, retry once immediately.
1220 * If we aborted probe due to a scheduled reset, dismiss it.
1221 */
1222 efx->reset_pending = 0;
1223 rc = efx_pci_probe_post_io(efx);
1224 if (rc) {
1225 /* On another failure, retry once more
1226 * after a 50-305ms delay.
1227 */
1228 unsigned char r;
1229
1230 get_random_bytes(&r, 1);
1231 msleep((unsigned int)r + 50);
1232 efx->reset_pending = 0;
1233 rc = efx_pci_probe_post_io(efx);
1234 }
1235 }
1236 if (rc)
1237 goto fail3;
1238
1239 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
1240
1241 /* Try to create MTDs, but allow this to fail */
1242 rtnl_lock();
1243 rc = efx_mtd_probe(efx);
1244 rtnl_unlock();
1245 if (rc && rc != -EPERM)
1246 netif_warn(efx, probe, efx->net_dev,
1247 "failed to create MTDs (%d)\n", rc);
1248
1249 if (efx->type->udp_tnl_push_ports)
1250 efx->type->udp_tnl_push_ports(efx);
1251
1252 return 0;
1253
1254 fail3:
1255 efx_fini_io(efx);
1256 fail2:
1257 efx_fini_struct(efx);
1258 fail1:
1259 WARN_ON(rc > 0);
1260 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
1261 free_netdev(net_dev);
1262 fail0:
1263 kfree(probe_data);
1264 return rc;
1265}
1266
1267/* efx_pci_sriov_configure returns the actual number of Virtual Functions
1268 * enabled on success
1269 */
1270#ifdef CONFIG_SFC_SRIOV
1271static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1272{
1273 int rc;
1274 struct efx_nic *efx = pci_get_drvdata(dev);
1275
1276 if (efx->type->sriov_configure) {
1277 rc = efx->type->sriov_configure(efx, num_vfs);
1278 if (rc)
1279 return rc;
1280 else
1281 return num_vfs;
1282 } else
1283 return -EOPNOTSUPP;
1284}
1285#endif
1286
1287static int efx_pm_freeze(struct device *dev)
1288{
1289 struct efx_nic *efx = dev_get_drvdata(dev);
1290
1291 rtnl_lock();
1292
1293 if (efx_net_active(efx->state)) {
1294 efx_device_detach_sync(efx);
1295
1296 efx_stop_all(efx);
1297 efx_disable_interrupts(efx);
1298
1299 efx->state = efx_freeze(efx->state);
1300 }
1301
1302 rtnl_unlock();
1303
1304 return 0;
1305}
1306
1307static void efx_pci_shutdown(struct pci_dev *pci_dev)
1308{
1309 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1310
1311 if (!efx)
1312 return;
1313
1314 efx_pm_freeze(&pci_dev->dev);
1315 pci_disable_device(pci_dev);
1316}
1317
1318static int efx_pm_thaw(struct device *dev)
1319{
1320 int rc;
1321 struct efx_nic *efx = dev_get_drvdata(dev);
1322
1323 rtnl_lock();
1324
1325 if (efx_frozen(efx->state)) {
1326 rc = efx_enable_interrupts(efx);
1327 if (rc)
1328 goto fail;
1329
1330 mutex_lock(&efx->mac_lock);
1331 efx_mcdi_port_reconfigure(efx);
1332 mutex_unlock(&efx->mac_lock);
1333
1334 efx_start_all(efx);
1335
1336 efx_device_attach_if_not_resetting(efx);
1337
1338 efx->state = efx_thaw(efx->state);
1339
1340 efx->type->resume_wol(efx);
1341 }
1342
1343 rtnl_unlock();
1344
1345 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
1346 efx_queue_reset_work(efx);
1347
1348 return 0;
1349
1350fail:
1351 rtnl_unlock();
1352
1353 return rc;
1354}
1355
1356static int efx_pm_poweroff(struct device *dev)
1357{
1358 struct pci_dev *pci_dev = to_pci_dev(dev);
1359 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1360
1361 efx->type->fini(efx);
1362
1363 efx->reset_pending = 0;
1364
1365 pci_save_state(pci_dev);
1366 return pci_set_power_state(pci_dev, PCI_D3hot);
1367}
1368
1369/* Used for both resume and restore */
1370static int efx_pm_resume(struct device *dev)
1371{
1372 struct pci_dev *pci_dev = to_pci_dev(dev);
1373 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1374 int rc;
1375
1376 rc = pci_set_power_state(pci_dev, PCI_D0);
1377 if (rc)
1378 return rc;
1379 pci_restore_state(pci_dev);
1380 rc = pci_enable_device(pci_dev);
1381 if (rc)
1382 return rc;
1383 pci_set_master(efx->pci_dev);
1384 rc = efx->type->reset(efx, RESET_TYPE_ALL);
1385 if (rc)
1386 return rc;
1387 down_write(&efx->filter_sem);
1388 rc = efx->type->init(efx);
1389 up_write(&efx->filter_sem);
1390 if (rc)
1391 return rc;
1392 rc = efx_pm_thaw(dev);
1393 return rc;
1394}
1395
1396static int efx_pm_suspend(struct device *dev)
1397{
1398 int rc;
1399
1400 efx_pm_freeze(dev);
1401 rc = efx_pm_poweroff(dev);
1402 if (rc)
1403 efx_pm_resume(dev);
1404 return rc;
1405}
1406
1407static const struct dev_pm_ops efx_pm_ops = {
1408 .suspend = efx_pm_suspend,
1409 .resume = efx_pm_resume,
1410 .freeze = efx_pm_freeze,
1411 .thaw = efx_pm_thaw,
1412 .poweroff = efx_pm_poweroff,
1413 .restore = efx_pm_resume,
1414};
1415
1416static struct pci_driver efx_pci_driver = {
1417 .name = KBUILD_MODNAME,
1418 .id_table = efx_pci_table,
1419 .probe = efx_pci_probe,
1420 .remove = efx_pci_remove,
1421 .driver.pm = &efx_pm_ops,
1422 .shutdown = efx_pci_shutdown,
1423 .err_handler = &efx_err_handlers,
1424#ifdef CONFIG_SFC_SRIOV
1425 .sriov_configure = efx_pci_sriov_configure,
1426#endif
1427};
1428
1429/**************************************************************************
1430 *
1431 * Kernel module interface
1432 *
1433 *************************************************************************/
1434
1435static int __init efx_init_module(void)
1436{
1437 int rc;
1438
1439 printk(KERN_INFO "Solarflare NET driver\n");
1440
1441 rc = register_netdevice_notifier(&efx_netdev_notifier);
1442 if (rc)
1443 goto err_notifier;
1444
1445 rc = efx_create_reset_workqueue();
1446 if (rc)
1447 goto err_reset;
1448
1449 rc = pci_register_driver(&efx_pci_driver);
1450 if (rc < 0)
1451 goto err_pci;
1452
1453 rc = pci_register_driver(&ef100_pci_driver);
1454 if (rc < 0)
1455 goto err_pci_ef100;
1456
1457 return 0;
1458
1459 err_pci_ef100:
1460 pci_unregister_driver(&efx_pci_driver);
1461 err_pci:
1462 efx_destroy_reset_workqueue();
1463 err_reset:
1464 unregister_netdevice_notifier(&efx_netdev_notifier);
1465 err_notifier:
1466 return rc;
1467}
1468
1469static void __exit efx_exit_module(void)
1470{
1471 printk(KERN_INFO "Solarflare NET driver unloading\n");
1472
1473 pci_unregister_driver(&ef100_pci_driver);
1474 pci_unregister_driver(&efx_pci_driver);
1475 efx_destroy_reset_workqueue();
1476 unregister_netdevice_notifier(&efx_netdev_notifier);
1477
1478}
1479
1480module_init(efx_init_module);
1481module_exit(efx_exit_module);
1482
1483MODULE_AUTHOR("Solarflare Communications and "
1484 "Michael Brown <mbrown@fensystems.co.uk>");
1485MODULE_DESCRIPTION("Solarflare network driver");
1486MODULE_LICENSE("GPL");
1487MODULE_DEVICE_TABLE(pci, efx_pci_table);
1/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2005-2011 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>
22#include <linux/topology.h>
23#include <linux/gfp.h>
24#include <linux/cpu_rmap.h>
25#include "net_driver.h"
26#include "efx.h"
27#include "nic.h"
28#include "selftest.h"
29
30#include "mcdi.h"
31#include "workarounds.h"
32
33/**************************************************************************
34 *
35 * Type name strings
36 *
37 **************************************************************************
38 */
39
40/* Loopback mode names (see LOOPBACK_MODE()) */
41const unsigned int efx_loopback_mode_max = LOOPBACK_MAX;
42const char *const efx_loopback_mode_names[] = {
43 [LOOPBACK_NONE] = "NONE",
44 [LOOPBACK_DATA] = "DATAPATH",
45 [LOOPBACK_GMAC] = "GMAC",
46 [LOOPBACK_XGMII] = "XGMII",
47 [LOOPBACK_XGXS] = "XGXS",
48 [LOOPBACK_XAUI] = "XAUI",
49 [LOOPBACK_GMII] = "GMII",
50 [LOOPBACK_SGMII] = "SGMII",
51 [LOOPBACK_XGBR] = "XGBR",
52 [LOOPBACK_XFI] = "XFI",
53 [LOOPBACK_XAUI_FAR] = "XAUI_FAR",
54 [LOOPBACK_GMII_FAR] = "GMII_FAR",
55 [LOOPBACK_SGMII_FAR] = "SGMII_FAR",
56 [LOOPBACK_XFI_FAR] = "XFI_FAR",
57 [LOOPBACK_GPHY] = "GPHY",
58 [LOOPBACK_PHYXS] = "PHYXS",
59 [LOOPBACK_PCS] = "PCS",
60 [LOOPBACK_PMAPMD] = "PMA/PMD",
61 [LOOPBACK_XPORT] = "XPORT",
62 [LOOPBACK_XGMII_WS] = "XGMII_WS",
63 [LOOPBACK_XAUI_WS] = "XAUI_WS",
64 [LOOPBACK_XAUI_WS_FAR] = "XAUI_WS_FAR",
65 [LOOPBACK_XAUI_WS_NEAR] = "XAUI_WS_NEAR",
66 [LOOPBACK_GMII_WS] = "GMII_WS",
67 [LOOPBACK_XFI_WS] = "XFI_WS",
68 [LOOPBACK_XFI_WS_FAR] = "XFI_WS_FAR",
69 [LOOPBACK_PHYXS_WS] = "PHYXS_WS",
70};
71
72const unsigned int efx_reset_type_max = RESET_TYPE_MAX;
73const char *const efx_reset_type_names[] = {
74 [RESET_TYPE_INVISIBLE] = "INVISIBLE",
75 [RESET_TYPE_ALL] = "ALL",
76 [RESET_TYPE_WORLD] = "WORLD",
77 [RESET_TYPE_DISABLE] = "DISABLE",
78 [RESET_TYPE_TX_WATCHDOG] = "TX_WATCHDOG",
79 [RESET_TYPE_INT_ERROR] = "INT_ERROR",
80 [RESET_TYPE_RX_RECOVERY] = "RX_RECOVERY",
81 [RESET_TYPE_RX_DESC_FETCH] = "RX_DESC_FETCH",
82 [RESET_TYPE_TX_DESC_FETCH] = "TX_DESC_FETCH",
83 [RESET_TYPE_TX_SKIP] = "TX_SKIP",
84 [RESET_TYPE_MC_FAILURE] = "MC_FAILURE",
85};
86
87#define EFX_MAX_MTU (9 * 1024)
88
89/* Reset workqueue. If any NIC has a hardware failure then a reset will be
90 * queued onto this work queue. This is not a per-nic work queue, because
91 * efx_reset_work() acquires the rtnl lock, so resets are naturally serialised.
92 */
93static struct workqueue_struct *reset_workqueue;
94
95/**************************************************************************
96 *
97 * Configurable values
98 *
99 *************************************************************************/
100
101/*
102 * Use separate channels for TX and RX events
103 *
104 * Set this to 1 to use separate channels for TX and RX. It allows us
105 * to control interrupt affinity separately for TX and RX.
106 *
107 * This is only used in MSI-X interrupt mode
108 */
109static unsigned int separate_tx_channels;
110module_param(separate_tx_channels, uint, 0444);
111MODULE_PARM_DESC(separate_tx_channels,
112 "Use separate channels for TX and RX");
113
114/* This is the weight assigned to each of the (per-channel) virtual
115 * NAPI devices.
116 */
117static int napi_weight = 64;
118
119/* This is the time (in jiffies) between invocations of the hardware
120 * monitor. On Falcon-based NICs, this will:
121 * - Check the on-board hardware monitor;
122 * - Poll the link state and reconfigure the hardware as necessary.
123 */
124static unsigned int efx_monitor_interval = 1 * HZ;
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 core.
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
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
167static unsigned irq_adapt_low_thresh = 8000;
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 = 16000;
173module_param(irq_adapt_high_thresh, uint, 0644);
174MODULE_PARM_DESC(irq_adapt_high_thresh,
175 "Threshold score for increasing IRQ moderation");
176
177static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
178 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
179 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
180 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
181module_param(debug, uint, 0);
182MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
183
184/**************************************************************************
185 *
186 * Utility functions and prototypes
187 *
188 *************************************************************************/
189
190static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq);
191static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq);
192static void efx_remove_channel(struct efx_channel *channel);
193static void efx_remove_channels(struct efx_nic *efx);
194static const struct efx_channel_type efx_default_channel_type;
195static void efx_remove_port(struct efx_nic *efx);
196static void efx_init_napi_channel(struct efx_channel *channel);
197static void efx_fini_napi(struct efx_nic *efx);
198static void efx_fini_napi_channel(struct efx_channel *channel);
199static void efx_fini_struct(struct efx_nic *efx);
200static void efx_start_all(struct efx_nic *efx);
201static void efx_stop_all(struct efx_nic *efx);
202
203#define EFX_ASSERT_RESET_SERIALISED(efx) \
204 do { \
205 if ((efx->state == STATE_RUNNING) || \
206 (efx->state == STATE_DISABLED)) \
207 ASSERT_RTNL(); \
208 } while (0)
209
210/**************************************************************************
211 *
212 * Event queue processing
213 *
214 *************************************************************************/
215
216/* Process channel's event queue
217 *
218 * This function is responsible for processing the event queue of a
219 * single channel. The caller must guarantee that this function will
220 * never be concurrently called more than once on the same channel,
221 * though different channels may be being processed concurrently.
222 */
223static int efx_process_channel(struct efx_channel *channel, int budget)
224{
225 int spent;
226
227 if (unlikely(!channel->enabled))
228 return 0;
229
230 spent = efx_nic_process_eventq(channel, budget);
231 if (spent && efx_channel_has_rx_queue(channel)) {
232 struct efx_rx_queue *rx_queue =
233 efx_channel_get_rx_queue(channel);
234
235 /* Deliver last RX packet. */
236 if (channel->rx_pkt) {
237 __efx_rx_packet(channel, channel->rx_pkt);
238 channel->rx_pkt = NULL;
239 }
240 if (rx_queue->enabled) {
241 efx_rx_strategy(channel);
242 efx_fast_push_rx_descriptors(rx_queue);
243 }
244 }
245
246 return spent;
247}
248
249/* Mark channel as finished processing
250 *
251 * Note that since we will not receive further interrupts for this
252 * channel before we finish processing and call the eventq_read_ack()
253 * method, there is no need to use the interrupt hold-off timers.
254 */
255static inline void efx_channel_processed(struct efx_channel *channel)
256{
257 /* The interrupt handler for this channel may set work_pending
258 * as soon as we acknowledge the events we've seen. Make sure
259 * it's cleared before then. */
260 channel->work_pending = false;
261 smp_wmb();
262
263 efx_nic_eventq_read_ack(channel);
264}
265
266/* NAPI poll handler
267 *
268 * NAPI guarantees serialisation of polls of the same device, which
269 * provides the guarantee required by efx_process_channel().
270 */
271static int efx_poll(struct napi_struct *napi, int budget)
272{
273 struct efx_channel *channel =
274 container_of(napi, struct efx_channel, napi_str);
275 struct efx_nic *efx = channel->efx;
276 int spent;
277
278 netif_vdbg(efx, intr, efx->net_dev,
279 "channel %d NAPI poll executing on CPU %d\n",
280 channel->channel, raw_smp_processor_id());
281
282 spent = efx_process_channel(channel, budget);
283
284 if (spent < budget) {
285 if (efx_channel_has_rx_queue(channel) &&
286 efx->irq_rx_adaptive &&
287 unlikely(++channel->irq_count == 1000)) {
288 if (unlikely(channel->irq_mod_score <
289 irq_adapt_low_thresh)) {
290 if (channel->irq_moderation > 1) {
291 channel->irq_moderation -= 1;
292 efx->type->push_irq_moderation(channel);
293 }
294 } else if (unlikely(channel->irq_mod_score >
295 irq_adapt_high_thresh)) {
296 if (channel->irq_moderation <
297 efx->irq_rx_moderation) {
298 channel->irq_moderation += 1;
299 efx->type->push_irq_moderation(channel);
300 }
301 }
302 channel->irq_count = 0;
303 channel->irq_mod_score = 0;
304 }
305
306 efx_filter_rfs_expire(channel);
307
308 /* There is no race here; although napi_disable() will
309 * only wait for napi_complete(), this isn't a problem
310 * since efx_channel_processed() will have no effect if
311 * interrupts have already been disabled.
312 */
313 napi_complete(napi);
314 efx_channel_processed(channel);
315 }
316
317 return spent;
318}
319
320/* Process the eventq of the specified channel immediately on this CPU
321 *
322 * Disable hardware generated interrupts, wait for any existing
323 * processing to finish, then directly poll (and ack ) the eventq.
324 * Finally reenable NAPI and interrupts.
325 *
326 * This is for use only during a loopback self-test. It must not
327 * deliver any packets up the stack as this can result in deadlock.
328 */
329void efx_process_channel_now(struct efx_channel *channel)
330{
331 struct efx_nic *efx = channel->efx;
332
333 BUG_ON(channel->channel >= efx->n_channels);
334 BUG_ON(!channel->enabled);
335 BUG_ON(!efx->loopback_selftest);
336
337 /* Disable interrupts and wait for ISRs to complete */
338 efx_nic_disable_interrupts(efx);
339 if (efx->legacy_irq) {
340 synchronize_irq(efx->legacy_irq);
341 efx->legacy_irq_enabled = false;
342 }
343 if (channel->irq)
344 synchronize_irq(channel->irq);
345
346 /* Wait for any NAPI processing to complete */
347 napi_disable(&channel->napi_str);
348
349 /* Poll the channel */
350 efx_process_channel(channel, channel->eventq_mask + 1);
351
352 /* Ack the eventq. This may cause an interrupt to be generated
353 * when they are reenabled */
354 efx_channel_processed(channel);
355
356 napi_enable(&channel->napi_str);
357 if (efx->legacy_irq)
358 efx->legacy_irq_enabled = true;
359 efx_nic_enable_interrupts(efx);
360}
361
362/* Create event queue
363 * Event queue memory allocations are done only once. If the channel
364 * is reset, the memory buffer will be reused; this guards against
365 * errors during channel reset and also simplifies interrupt handling.
366 */
367static int efx_probe_eventq(struct efx_channel *channel)
368{
369 struct efx_nic *efx = channel->efx;
370 unsigned long entries;
371
372 netif_dbg(efx, probe, efx->net_dev,
373 "chan %d create event queue\n", channel->channel);
374
375 /* Build an event queue with room for one event per tx and rx buffer,
376 * plus some extra for link state events and MCDI completions. */
377 entries = roundup_pow_of_two(efx->rxq_entries + efx->txq_entries + 128);
378 EFX_BUG_ON_PARANOID(entries > EFX_MAX_EVQ_SIZE);
379 channel->eventq_mask = max(entries, EFX_MIN_EVQ_SIZE) - 1;
380
381 return efx_nic_probe_eventq(channel);
382}
383
384/* Prepare channel's event queue */
385static void efx_init_eventq(struct efx_channel *channel)
386{
387 netif_dbg(channel->efx, drv, channel->efx->net_dev,
388 "chan %d init event queue\n", channel->channel);
389
390 channel->eventq_read_ptr = 0;
391
392 efx_nic_init_eventq(channel);
393}
394
395/* Enable event queue processing and NAPI */
396static void efx_start_eventq(struct efx_channel *channel)
397{
398 netif_dbg(channel->efx, ifup, channel->efx->net_dev,
399 "chan %d start event queue\n", channel->channel);
400
401 /* The interrupt handler for this channel may set work_pending
402 * as soon as we enable it. Make sure it's cleared before
403 * then. Similarly, make sure it sees the enabled flag set.
404 */
405 channel->work_pending = false;
406 channel->enabled = true;
407 smp_wmb();
408
409 napi_enable(&channel->napi_str);
410 efx_nic_eventq_read_ack(channel);
411}
412
413/* Disable event queue processing and NAPI */
414static void efx_stop_eventq(struct efx_channel *channel)
415{
416 if (!channel->enabled)
417 return;
418
419 napi_disable(&channel->napi_str);
420 channel->enabled = false;
421}
422
423static void efx_fini_eventq(struct efx_channel *channel)
424{
425 netif_dbg(channel->efx, drv, channel->efx->net_dev,
426 "chan %d fini event queue\n", channel->channel);
427
428 efx_nic_fini_eventq(channel);
429}
430
431static void efx_remove_eventq(struct efx_channel *channel)
432{
433 netif_dbg(channel->efx, drv, channel->efx->net_dev,
434 "chan %d remove event queue\n", channel->channel);
435
436 efx_nic_remove_eventq(channel);
437}
438
439/**************************************************************************
440 *
441 * Channel handling
442 *
443 *************************************************************************/
444
445/* Allocate and initialise a channel structure. */
446static struct efx_channel *
447efx_alloc_channel(struct efx_nic *efx, int i, struct efx_channel *old_channel)
448{
449 struct efx_channel *channel;
450 struct efx_rx_queue *rx_queue;
451 struct efx_tx_queue *tx_queue;
452 int j;
453
454 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
455 if (!channel)
456 return NULL;
457
458 channel->efx = efx;
459 channel->channel = i;
460 channel->type = &efx_default_channel_type;
461
462 for (j = 0; j < EFX_TXQ_TYPES; j++) {
463 tx_queue = &channel->tx_queue[j];
464 tx_queue->efx = efx;
465 tx_queue->queue = i * EFX_TXQ_TYPES + j;
466 tx_queue->channel = channel;
467 }
468
469 rx_queue = &channel->rx_queue;
470 rx_queue->efx = efx;
471 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
472 (unsigned long)rx_queue);
473
474 return channel;
475}
476
477/* Allocate and initialise a channel structure, copying parameters
478 * (but not resources) from an old channel structure.
479 */
480static struct efx_channel *
481efx_copy_channel(const struct efx_channel *old_channel)
482{
483 struct efx_channel *channel;
484 struct efx_rx_queue *rx_queue;
485 struct efx_tx_queue *tx_queue;
486 int j;
487
488 channel = kmalloc(sizeof(*channel), GFP_KERNEL);
489 if (!channel)
490 return NULL;
491
492 *channel = *old_channel;
493
494 channel->napi_dev = NULL;
495 memset(&channel->eventq, 0, sizeof(channel->eventq));
496
497 for (j = 0; j < EFX_TXQ_TYPES; j++) {
498 tx_queue = &channel->tx_queue[j];
499 if (tx_queue->channel)
500 tx_queue->channel = channel;
501 tx_queue->buffer = NULL;
502 memset(&tx_queue->txd, 0, sizeof(tx_queue->txd));
503 }
504
505 rx_queue = &channel->rx_queue;
506 rx_queue->buffer = NULL;
507 memset(&rx_queue->rxd, 0, sizeof(rx_queue->rxd));
508 setup_timer(&rx_queue->slow_fill, efx_rx_slow_fill,
509 (unsigned long)rx_queue);
510
511 return channel;
512}
513
514static int efx_probe_channel(struct efx_channel *channel)
515{
516 struct efx_tx_queue *tx_queue;
517 struct efx_rx_queue *rx_queue;
518 int rc;
519
520 netif_dbg(channel->efx, probe, channel->efx->net_dev,
521 "creating channel %d\n", channel->channel);
522
523 rc = channel->type->pre_probe(channel);
524 if (rc)
525 goto fail;
526
527 rc = efx_probe_eventq(channel);
528 if (rc)
529 goto fail;
530
531 efx_for_each_channel_tx_queue(tx_queue, channel) {
532 rc = efx_probe_tx_queue(tx_queue);
533 if (rc)
534 goto fail;
535 }
536
537 efx_for_each_channel_rx_queue(rx_queue, channel) {
538 rc = efx_probe_rx_queue(rx_queue);
539 if (rc)
540 goto fail;
541 }
542
543 channel->n_rx_frm_trunc = 0;
544
545 return 0;
546
547fail:
548 efx_remove_channel(channel);
549 return rc;
550}
551
552static void
553efx_get_channel_name(struct efx_channel *channel, char *buf, size_t len)
554{
555 struct efx_nic *efx = channel->efx;
556 const char *type;
557 int number;
558
559 number = channel->channel;
560 if (efx->tx_channel_offset == 0) {
561 type = "";
562 } else if (channel->channel < efx->tx_channel_offset) {
563 type = "-rx";
564 } else {
565 type = "-tx";
566 number -= efx->tx_channel_offset;
567 }
568 snprintf(buf, len, "%s%s-%d", efx->name, type, number);
569}
570
571static void efx_set_channel_names(struct efx_nic *efx)
572{
573 struct efx_channel *channel;
574
575 efx_for_each_channel(channel, efx)
576 channel->type->get_name(channel,
577 efx->channel_name[channel->channel],
578 sizeof(efx->channel_name[0]));
579}
580
581static int efx_probe_channels(struct efx_nic *efx)
582{
583 struct efx_channel *channel;
584 int rc;
585
586 /* Restart special buffer allocation */
587 efx->next_buffer_table = 0;
588
589 /* Probe channels in reverse, so that any 'extra' channels
590 * use the start of the buffer table. This allows the traffic
591 * channels to be resized without moving them or wasting the
592 * entries before them.
593 */
594 efx_for_each_channel_rev(channel, efx) {
595 rc = efx_probe_channel(channel);
596 if (rc) {
597 netif_err(efx, probe, efx->net_dev,
598 "failed to create channel %d\n",
599 channel->channel);
600 goto fail;
601 }
602 }
603 efx_set_channel_names(efx);
604
605 return 0;
606
607fail:
608 efx_remove_channels(efx);
609 return rc;
610}
611
612/* Channels are shutdown and reinitialised whilst the NIC is running
613 * to propagate configuration changes (mtu, checksum offload), or
614 * to clear hardware error conditions
615 */
616static void efx_start_datapath(struct efx_nic *efx)
617{
618 struct efx_tx_queue *tx_queue;
619 struct efx_rx_queue *rx_queue;
620 struct efx_channel *channel;
621
622 /* Calculate the rx buffer allocation parameters required to
623 * support the current MTU, including padding for header
624 * alignment and overruns.
625 */
626 efx->rx_buffer_len = (max(EFX_PAGE_IP_ALIGN, NET_IP_ALIGN) +
627 EFX_MAX_FRAME_LEN(efx->net_dev->mtu) +
628 efx->type->rx_buffer_hash_size +
629 efx->type->rx_buffer_padding);
630 efx->rx_buffer_order = get_order(efx->rx_buffer_len +
631 sizeof(struct efx_rx_page_state));
632
633 /* Initialise the channels */
634 efx_for_each_channel(channel, efx) {
635 efx_for_each_channel_tx_queue(tx_queue, channel)
636 efx_init_tx_queue(tx_queue);
637
638 /* The rx buffer allocation strategy is MTU dependent */
639 efx_rx_strategy(channel);
640
641 efx_for_each_channel_rx_queue(rx_queue, channel) {
642 efx_init_rx_queue(rx_queue);
643 efx_nic_generate_fill_event(rx_queue);
644 }
645
646 WARN_ON(channel->rx_pkt != NULL);
647 efx_rx_strategy(channel);
648 }
649
650 if (netif_device_present(efx->net_dev))
651 netif_tx_wake_all_queues(efx->net_dev);
652}
653
654static void efx_stop_datapath(struct efx_nic *efx)
655{
656 struct efx_channel *channel;
657 struct efx_tx_queue *tx_queue;
658 struct efx_rx_queue *rx_queue;
659 struct pci_dev *dev = efx->pci_dev;
660 int rc;
661
662 EFX_ASSERT_RESET_SERIALISED(efx);
663 BUG_ON(efx->port_enabled);
664
665 /* Only perform flush if dma is enabled */
666 if (dev->is_busmaster) {
667 rc = efx_nic_flush_queues(efx);
668
669 if (rc && EFX_WORKAROUND_7803(efx)) {
670 /* Schedule a reset to recover from the flush failure. The
671 * descriptor caches reference memory we're about to free,
672 * but falcon_reconfigure_mac_wrapper() won't reconnect
673 * the MACs because of the pending reset. */
674 netif_err(efx, drv, efx->net_dev,
675 "Resetting to recover from flush failure\n");
676 efx_schedule_reset(efx, RESET_TYPE_ALL);
677 } else if (rc) {
678 netif_err(efx, drv, efx->net_dev, "failed to flush queues\n");
679 } else {
680 netif_dbg(efx, drv, efx->net_dev,
681 "successfully flushed all queues\n");
682 }
683 }
684
685 efx_for_each_channel(channel, efx) {
686 /* RX packet processing is pipelined, so wait for the
687 * NAPI handler to complete. At least event queue 0
688 * might be kept active by non-data events, so don't
689 * use napi_synchronize() but actually disable NAPI
690 * temporarily.
691 */
692 if (efx_channel_has_rx_queue(channel)) {
693 efx_stop_eventq(channel);
694 efx_start_eventq(channel);
695 }
696
697 efx_for_each_channel_rx_queue(rx_queue, channel)
698 efx_fini_rx_queue(rx_queue);
699 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
700 efx_fini_tx_queue(tx_queue);
701 }
702}
703
704static void efx_remove_channel(struct efx_channel *channel)
705{
706 struct efx_tx_queue *tx_queue;
707 struct efx_rx_queue *rx_queue;
708
709 netif_dbg(channel->efx, drv, channel->efx->net_dev,
710 "destroy chan %d\n", channel->channel);
711
712 efx_for_each_channel_rx_queue(rx_queue, channel)
713 efx_remove_rx_queue(rx_queue);
714 efx_for_each_possible_channel_tx_queue(tx_queue, channel)
715 efx_remove_tx_queue(tx_queue);
716 efx_remove_eventq(channel);
717}
718
719static void efx_remove_channels(struct efx_nic *efx)
720{
721 struct efx_channel *channel;
722
723 efx_for_each_channel(channel, efx)
724 efx_remove_channel(channel);
725}
726
727int
728efx_realloc_channels(struct efx_nic *efx, u32 rxq_entries, u32 txq_entries)
729{
730 struct efx_channel *other_channel[EFX_MAX_CHANNELS], *channel;
731 u32 old_rxq_entries, old_txq_entries;
732 unsigned i, next_buffer_table = 0;
733 int rc = 0;
734
735 /* Not all channels should be reallocated. We must avoid
736 * reallocating their buffer table entries.
737 */
738 efx_for_each_channel(channel, efx) {
739 struct efx_rx_queue *rx_queue;
740 struct efx_tx_queue *tx_queue;
741
742 if (channel->type->copy)
743 continue;
744 next_buffer_table = max(next_buffer_table,
745 channel->eventq.index +
746 channel->eventq.entries);
747 efx_for_each_channel_rx_queue(rx_queue, channel)
748 next_buffer_table = max(next_buffer_table,
749 rx_queue->rxd.index +
750 rx_queue->rxd.entries);
751 efx_for_each_channel_tx_queue(tx_queue, channel)
752 next_buffer_table = max(next_buffer_table,
753 tx_queue->txd.index +
754 tx_queue->txd.entries);
755 }
756
757 efx_stop_all(efx);
758 efx_stop_interrupts(efx, true);
759
760 /* Clone channels (where possible) */
761 memset(other_channel, 0, sizeof(other_channel));
762 for (i = 0; i < efx->n_channels; i++) {
763 channel = efx->channel[i];
764 if (channel->type->copy)
765 channel = channel->type->copy(channel);
766 if (!channel) {
767 rc = -ENOMEM;
768 goto out;
769 }
770 other_channel[i] = channel;
771 }
772
773 /* Swap entry counts and channel pointers */
774 old_rxq_entries = efx->rxq_entries;
775 old_txq_entries = efx->txq_entries;
776 efx->rxq_entries = rxq_entries;
777 efx->txq_entries = txq_entries;
778 for (i = 0; i < efx->n_channels; i++) {
779 channel = efx->channel[i];
780 efx->channel[i] = other_channel[i];
781 other_channel[i] = channel;
782 }
783
784 /* Restart buffer table allocation */
785 efx->next_buffer_table = next_buffer_table;
786
787 for (i = 0; i < efx->n_channels; i++) {
788 channel = efx->channel[i];
789 if (!channel->type->copy)
790 continue;
791 rc = efx_probe_channel(channel);
792 if (rc)
793 goto rollback;
794 efx_init_napi_channel(efx->channel[i]);
795 }
796
797out:
798 /* Destroy unused channel structures */
799 for (i = 0; i < efx->n_channels; i++) {
800 channel = other_channel[i];
801 if (channel && channel->type->copy) {
802 efx_fini_napi_channel(channel);
803 efx_remove_channel(channel);
804 kfree(channel);
805 }
806 }
807
808 efx_start_interrupts(efx, true);
809 efx_start_all(efx);
810 return rc;
811
812rollback:
813 /* Swap back */
814 efx->rxq_entries = old_rxq_entries;
815 efx->txq_entries = old_txq_entries;
816 for (i = 0; i < efx->n_channels; i++) {
817 channel = efx->channel[i];
818 efx->channel[i] = other_channel[i];
819 other_channel[i] = channel;
820 }
821 goto out;
822}
823
824void efx_schedule_slow_fill(struct efx_rx_queue *rx_queue)
825{
826 mod_timer(&rx_queue->slow_fill, jiffies + msecs_to_jiffies(100));
827}
828
829static const struct efx_channel_type efx_default_channel_type = {
830 .pre_probe = efx_channel_dummy_op_int,
831 .get_name = efx_get_channel_name,
832 .copy = efx_copy_channel,
833 .keep_eventq = false,
834};
835
836int efx_channel_dummy_op_int(struct efx_channel *channel)
837{
838 return 0;
839}
840
841/**************************************************************************
842 *
843 * Port handling
844 *
845 **************************************************************************/
846
847/* This ensures that the kernel is kept informed (via
848 * netif_carrier_on/off) of the link status, and also maintains the
849 * link status's stop on the port's TX queue.
850 */
851void efx_link_status_changed(struct efx_nic *efx)
852{
853 struct efx_link_state *link_state = &efx->link_state;
854
855 /* SFC Bug 5356: A net_dev notifier is registered, so we must ensure
856 * that no events are triggered between unregister_netdev() and the
857 * driver unloading. A more general condition is that NETDEV_CHANGE
858 * can only be generated between NETDEV_UP and NETDEV_DOWN */
859 if (!netif_running(efx->net_dev))
860 return;
861
862 if (link_state->up != netif_carrier_ok(efx->net_dev)) {
863 efx->n_link_state_changes++;
864
865 if (link_state->up)
866 netif_carrier_on(efx->net_dev);
867 else
868 netif_carrier_off(efx->net_dev);
869 }
870
871 /* Status message for kernel log */
872 if (link_state->up)
873 netif_info(efx, link, efx->net_dev,
874 "link up at %uMbps %s-duplex (MTU %d)%s\n",
875 link_state->speed, link_state->fd ? "full" : "half",
876 efx->net_dev->mtu,
877 (efx->promiscuous ? " [PROMISC]" : ""));
878 else
879 netif_info(efx, link, efx->net_dev, "link down\n");
880}
881
882void efx_link_set_advertising(struct efx_nic *efx, u32 advertising)
883{
884 efx->link_advertising = advertising;
885 if (advertising) {
886 if (advertising & ADVERTISED_Pause)
887 efx->wanted_fc |= (EFX_FC_TX | EFX_FC_RX);
888 else
889 efx->wanted_fc &= ~(EFX_FC_TX | EFX_FC_RX);
890 if (advertising & ADVERTISED_Asym_Pause)
891 efx->wanted_fc ^= EFX_FC_TX;
892 }
893}
894
895void efx_link_set_wanted_fc(struct efx_nic *efx, u8 wanted_fc)
896{
897 efx->wanted_fc = wanted_fc;
898 if (efx->link_advertising) {
899 if (wanted_fc & EFX_FC_RX)
900 efx->link_advertising |= (ADVERTISED_Pause |
901 ADVERTISED_Asym_Pause);
902 else
903 efx->link_advertising &= ~(ADVERTISED_Pause |
904 ADVERTISED_Asym_Pause);
905 if (wanted_fc & EFX_FC_TX)
906 efx->link_advertising ^= ADVERTISED_Asym_Pause;
907 }
908}
909
910static void efx_fini_port(struct efx_nic *efx);
911
912/* Push loopback/power/transmit disable settings to the PHY, and reconfigure
913 * the MAC appropriately. All other PHY configuration changes are pushed
914 * through phy_op->set_settings(), and pushed asynchronously to the MAC
915 * through efx_monitor().
916 *
917 * Callers must hold the mac_lock
918 */
919int __efx_reconfigure_port(struct efx_nic *efx)
920{
921 enum efx_phy_mode phy_mode;
922 int rc;
923
924 WARN_ON(!mutex_is_locked(&efx->mac_lock));
925
926 /* Serialise the promiscuous flag with efx_set_rx_mode. */
927 netif_addr_lock_bh(efx->net_dev);
928 netif_addr_unlock_bh(efx->net_dev);
929
930 /* Disable PHY transmit in mac level loopbacks */
931 phy_mode = efx->phy_mode;
932 if (LOOPBACK_INTERNAL(efx))
933 efx->phy_mode |= PHY_MODE_TX_DISABLED;
934 else
935 efx->phy_mode &= ~PHY_MODE_TX_DISABLED;
936
937 rc = efx->type->reconfigure_port(efx);
938
939 if (rc)
940 efx->phy_mode = phy_mode;
941
942 return rc;
943}
944
945/* Reinitialise the MAC to pick up new PHY settings, even if the port is
946 * disabled. */
947int efx_reconfigure_port(struct efx_nic *efx)
948{
949 int rc;
950
951 EFX_ASSERT_RESET_SERIALISED(efx);
952
953 mutex_lock(&efx->mac_lock);
954 rc = __efx_reconfigure_port(efx);
955 mutex_unlock(&efx->mac_lock);
956
957 return rc;
958}
959
960/* Asynchronous work item for changing MAC promiscuity and multicast
961 * hash. Avoid a drain/rx_ingress enable by reconfiguring the current
962 * MAC directly. */
963static void efx_mac_work(struct work_struct *data)
964{
965 struct efx_nic *efx = container_of(data, struct efx_nic, mac_work);
966
967 mutex_lock(&efx->mac_lock);
968 if (efx->port_enabled)
969 efx->type->reconfigure_mac(efx);
970 mutex_unlock(&efx->mac_lock);
971}
972
973static int efx_probe_port(struct efx_nic *efx)
974{
975 int rc;
976
977 netif_dbg(efx, probe, efx->net_dev, "create port\n");
978
979 if (phy_flash_cfg)
980 efx->phy_mode = PHY_MODE_SPECIAL;
981
982 /* Connect up MAC/PHY operations table */
983 rc = efx->type->probe_port(efx);
984 if (rc)
985 return rc;
986
987 /* Initialise MAC address to permanent address */
988 memcpy(efx->net_dev->dev_addr, efx->net_dev->perm_addr, ETH_ALEN);
989
990 return 0;
991}
992
993static int efx_init_port(struct efx_nic *efx)
994{
995 int rc;
996
997 netif_dbg(efx, drv, efx->net_dev, "init port\n");
998
999 mutex_lock(&efx->mac_lock);
1000
1001 rc = efx->phy_op->init(efx);
1002 if (rc)
1003 goto fail1;
1004
1005 efx->port_initialized = true;
1006
1007 /* Reconfigure the MAC before creating dma queues (required for
1008 * Falcon/A1 where RX_INGR_EN/TX_DRAIN_EN isn't supported) */
1009 efx->type->reconfigure_mac(efx);
1010
1011 /* Ensure the PHY advertises the correct flow control settings */
1012 rc = efx->phy_op->reconfigure(efx);
1013 if (rc)
1014 goto fail2;
1015
1016 mutex_unlock(&efx->mac_lock);
1017 return 0;
1018
1019fail2:
1020 efx->phy_op->fini(efx);
1021fail1:
1022 mutex_unlock(&efx->mac_lock);
1023 return rc;
1024}
1025
1026static void efx_start_port(struct efx_nic *efx)
1027{
1028 netif_dbg(efx, ifup, efx->net_dev, "start port\n");
1029 BUG_ON(efx->port_enabled);
1030
1031 mutex_lock(&efx->mac_lock);
1032 efx->port_enabled = true;
1033
1034 /* efx_mac_work() might have been scheduled after efx_stop_port(),
1035 * and then cancelled by efx_flush_all() */
1036 efx->type->reconfigure_mac(efx);
1037
1038 mutex_unlock(&efx->mac_lock);
1039}
1040
1041/* Prevent efx_mac_work() and efx_monitor() from working */
1042static void efx_stop_port(struct efx_nic *efx)
1043{
1044 netif_dbg(efx, ifdown, efx->net_dev, "stop port\n");
1045
1046 mutex_lock(&efx->mac_lock);
1047 efx->port_enabled = false;
1048 mutex_unlock(&efx->mac_lock);
1049
1050 /* Serialise against efx_set_multicast_list() */
1051 netif_addr_lock_bh(efx->net_dev);
1052 netif_addr_unlock_bh(efx->net_dev);
1053}
1054
1055static void efx_fini_port(struct efx_nic *efx)
1056{
1057 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
1058
1059 if (!efx->port_initialized)
1060 return;
1061
1062 efx->phy_op->fini(efx);
1063 efx->port_initialized = false;
1064
1065 efx->link_state.up = false;
1066 efx_link_status_changed(efx);
1067}
1068
1069static void efx_remove_port(struct efx_nic *efx)
1070{
1071 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
1072
1073 efx->type->remove_port(efx);
1074}
1075
1076/**************************************************************************
1077 *
1078 * NIC handling
1079 *
1080 **************************************************************************/
1081
1082/* This configures the PCI device to enable I/O and DMA. */
1083static int efx_init_io(struct efx_nic *efx)
1084{
1085 struct pci_dev *pci_dev = efx->pci_dev;
1086 dma_addr_t dma_mask = efx->type->max_dma_mask;
1087 int rc;
1088
1089 netif_dbg(efx, probe, efx->net_dev, "initialising I/O\n");
1090
1091 rc = pci_enable_device(pci_dev);
1092 if (rc) {
1093 netif_err(efx, probe, efx->net_dev,
1094 "failed to enable PCI device\n");
1095 goto fail1;
1096 }
1097
1098 pci_set_master(pci_dev);
1099
1100 /* Set the PCI DMA mask. Try all possibilities from our
1101 * genuine mask down to 32 bits, because some architectures
1102 * (e.g. x86_64 with iommu_sac_force set) will allow 40 bit
1103 * masks event though they reject 46 bit masks.
1104 */
1105 while (dma_mask > 0x7fffffffUL) {
1106 if (pci_dma_supported(pci_dev, dma_mask)) {
1107 rc = pci_set_dma_mask(pci_dev, dma_mask);
1108 if (rc == 0)
1109 break;
1110 }
1111 dma_mask >>= 1;
1112 }
1113 if (rc) {
1114 netif_err(efx, probe, efx->net_dev,
1115 "could not find a suitable DMA mask\n");
1116 goto fail2;
1117 }
1118 netif_dbg(efx, probe, efx->net_dev,
1119 "using DMA mask %llx\n", (unsigned long long) dma_mask);
1120 rc = pci_set_consistent_dma_mask(pci_dev, dma_mask);
1121 if (rc) {
1122 /* pci_set_consistent_dma_mask() is not *allowed* to
1123 * fail with a mask that pci_set_dma_mask() accepted,
1124 * but just in case...
1125 */
1126 netif_err(efx, probe, efx->net_dev,
1127 "failed to set consistent DMA mask\n");
1128 goto fail2;
1129 }
1130
1131 efx->membase_phys = pci_resource_start(efx->pci_dev, EFX_MEM_BAR);
1132 rc = pci_request_region(pci_dev, EFX_MEM_BAR, "sfc");
1133 if (rc) {
1134 netif_err(efx, probe, efx->net_dev,
1135 "request for memory BAR failed\n");
1136 rc = -EIO;
1137 goto fail3;
1138 }
1139 efx->membase = ioremap_nocache(efx->membase_phys,
1140 efx->type->mem_map_size);
1141 if (!efx->membase) {
1142 netif_err(efx, probe, efx->net_dev,
1143 "could not map memory BAR at %llx+%x\n",
1144 (unsigned long long)efx->membase_phys,
1145 efx->type->mem_map_size);
1146 rc = -ENOMEM;
1147 goto fail4;
1148 }
1149 netif_dbg(efx, probe, efx->net_dev,
1150 "memory BAR at %llx+%x (virtual %p)\n",
1151 (unsigned long long)efx->membase_phys,
1152 efx->type->mem_map_size, efx->membase);
1153
1154 return 0;
1155
1156 fail4:
1157 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
1158 fail3:
1159 efx->membase_phys = 0;
1160 fail2:
1161 pci_disable_device(efx->pci_dev);
1162 fail1:
1163 return rc;
1164}
1165
1166static void efx_fini_io(struct efx_nic *efx)
1167{
1168 netif_dbg(efx, drv, efx->net_dev, "shutting down I/O\n");
1169
1170 if (efx->membase) {
1171 iounmap(efx->membase);
1172 efx->membase = NULL;
1173 }
1174
1175 if (efx->membase_phys) {
1176 pci_release_region(efx->pci_dev, EFX_MEM_BAR);
1177 efx->membase_phys = 0;
1178 }
1179
1180 pci_disable_device(efx->pci_dev);
1181}
1182
1183static unsigned int efx_wanted_parallelism(struct efx_nic *efx)
1184{
1185 cpumask_var_t thread_mask;
1186 unsigned int count;
1187 int cpu;
1188
1189 if (rss_cpus) {
1190 count = rss_cpus;
1191 } else {
1192 if (unlikely(!zalloc_cpumask_var(&thread_mask, GFP_KERNEL))) {
1193 netif_warn(efx, probe, efx->net_dev,
1194 "RSS disabled due to allocation failure\n");
1195 return 1;
1196 }
1197
1198 count = 0;
1199 for_each_online_cpu(cpu) {
1200 if (!cpumask_test_cpu(cpu, thread_mask)) {
1201 ++count;
1202 cpumask_or(thread_mask, thread_mask,
1203 topology_thread_cpumask(cpu));
1204 }
1205 }
1206
1207 free_cpumask_var(thread_mask);
1208 }
1209
1210 /* If RSS is requested for the PF *and* VFs then we can't write RSS
1211 * table entries that are inaccessible to VFs
1212 */
1213 if (efx_sriov_wanted(efx) && efx_vf_size(efx) > 1 &&
1214 count > efx_vf_size(efx)) {
1215 netif_warn(efx, probe, efx->net_dev,
1216 "Reducing number of RSS channels from %u to %u for "
1217 "VF support. Increase vf-msix-limit to use more "
1218 "channels on the PF.\n",
1219 count, efx_vf_size(efx));
1220 count = efx_vf_size(efx);
1221 }
1222
1223 return count;
1224}
1225
1226static int
1227efx_init_rx_cpu_rmap(struct efx_nic *efx, struct msix_entry *xentries)
1228{
1229#ifdef CONFIG_RFS_ACCEL
1230 unsigned int i;
1231 int rc;
1232
1233 efx->net_dev->rx_cpu_rmap = alloc_irq_cpu_rmap(efx->n_rx_channels);
1234 if (!efx->net_dev->rx_cpu_rmap)
1235 return -ENOMEM;
1236 for (i = 0; i < efx->n_rx_channels; i++) {
1237 rc = irq_cpu_rmap_add(efx->net_dev->rx_cpu_rmap,
1238 xentries[i].vector);
1239 if (rc) {
1240 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
1241 efx->net_dev->rx_cpu_rmap = NULL;
1242 return rc;
1243 }
1244 }
1245#endif
1246 return 0;
1247}
1248
1249/* Probe the number and type of interrupts we are able to obtain, and
1250 * the resulting numbers of channels and RX queues.
1251 */
1252static int efx_probe_interrupts(struct efx_nic *efx)
1253{
1254 unsigned int max_channels =
1255 min(efx->type->phys_addr_channels, EFX_MAX_CHANNELS);
1256 unsigned int extra_channels = 0;
1257 unsigned int i, j;
1258 int rc;
1259
1260 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++)
1261 if (efx->extra_channel_type[i])
1262 ++extra_channels;
1263
1264 if (efx->interrupt_mode == EFX_INT_MODE_MSIX) {
1265 struct msix_entry xentries[EFX_MAX_CHANNELS];
1266 unsigned int n_channels;
1267
1268 n_channels = efx_wanted_parallelism(efx);
1269 if (separate_tx_channels)
1270 n_channels *= 2;
1271 n_channels += extra_channels;
1272 n_channels = min(n_channels, max_channels);
1273
1274 for (i = 0; i < n_channels; i++)
1275 xentries[i].entry = i;
1276 rc = pci_enable_msix(efx->pci_dev, xentries, n_channels);
1277 if (rc > 0) {
1278 netif_err(efx, drv, efx->net_dev,
1279 "WARNING: Insufficient MSI-X vectors"
1280 " available (%d < %u).\n", rc, n_channels);
1281 netif_err(efx, drv, efx->net_dev,
1282 "WARNING: Performance may be reduced.\n");
1283 EFX_BUG_ON_PARANOID(rc >= n_channels);
1284 n_channels = rc;
1285 rc = pci_enable_msix(efx->pci_dev, xentries,
1286 n_channels);
1287 }
1288
1289 if (rc == 0) {
1290 efx->n_channels = n_channels;
1291 if (n_channels > extra_channels)
1292 n_channels -= extra_channels;
1293 if (separate_tx_channels) {
1294 efx->n_tx_channels = max(n_channels / 2, 1U);
1295 efx->n_rx_channels = max(n_channels -
1296 efx->n_tx_channels,
1297 1U);
1298 } else {
1299 efx->n_tx_channels = n_channels;
1300 efx->n_rx_channels = n_channels;
1301 }
1302 rc = efx_init_rx_cpu_rmap(efx, xentries);
1303 if (rc) {
1304 pci_disable_msix(efx->pci_dev);
1305 return rc;
1306 }
1307 for (i = 0; i < efx->n_channels; i++)
1308 efx_get_channel(efx, i)->irq =
1309 xentries[i].vector;
1310 } else {
1311 /* Fall back to single channel MSI */
1312 efx->interrupt_mode = EFX_INT_MODE_MSI;
1313 netif_err(efx, drv, efx->net_dev,
1314 "could not enable MSI-X\n");
1315 }
1316 }
1317
1318 /* Try single interrupt MSI */
1319 if (efx->interrupt_mode == EFX_INT_MODE_MSI) {
1320 efx->n_channels = 1;
1321 efx->n_rx_channels = 1;
1322 efx->n_tx_channels = 1;
1323 rc = pci_enable_msi(efx->pci_dev);
1324 if (rc == 0) {
1325 efx_get_channel(efx, 0)->irq = efx->pci_dev->irq;
1326 } else {
1327 netif_err(efx, drv, efx->net_dev,
1328 "could not enable MSI\n");
1329 efx->interrupt_mode = EFX_INT_MODE_LEGACY;
1330 }
1331 }
1332
1333 /* Assume legacy interrupts */
1334 if (efx->interrupt_mode == EFX_INT_MODE_LEGACY) {
1335 efx->n_channels = 1 + (separate_tx_channels ? 1 : 0);
1336 efx->n_rx_channels = 1;
1337 efx->n_tx_channels = 1;
1338 efx->legacy_irq = efx->pci_dev->irq;
1339 }
1340
1341 /* Assign extra channels if possible */
1342 j = efx->n_channels;
1343 for (i = 0; i < EFX_MAX_EXTRA_CHANNELS; i++) {
1344 if (!efx->extra_channel_type[i])
1345 continue;
1346 if (efx->interrupt_mode != EFX_INT_MODE_MSIX ||
1347 efx->n_channels <= extra_channels) {
1348 efx->extra_channel_type[i]->handle_no_channel(efx);
1349 } else {
1350 --j;
1351 efx_get_channel(efx, j)->type =
1352 efx->extra_channel_type[i];
1353 }
1354 }
1355
1356 /* RSS might be usable on VFs even if it is disabled on the PF */
1357 efx->rss_spread = ((efx->n_rx_channels > 1 || !efx_sriov_wanted(efx)) ?
1358 efx->n_rx_channels : efx_vf_size(efx));
1359
1360 return 0;
1361}
1362
1363/* Enable interrupts, then probe and start the event queues */
1364static void efx_start_interrupts(struct efx_nic *efx, bool may_keep_eventq)
1365{
1366 struct efx_channel *channel;
1367
1368 if (efx->legacy_irq)
1369 efx->legacy_irq_enabled = true;
1370 efx_nic_enable_interrupts(efx);
1371
1372 efx_for_each_channel(channel, efx) {
1373 if (!channel->type->keep_eventq || !may_keep_eventq)
1374 efx_init_eventq(channel);
1375 efx_start_eventq(channel);
1376 }
1377
1378 efx_mcdi_mode_event(efx);
1379}
1380
1381static void efx_stop_interrupts(struct efx_nic *efx, bool may_keep_eventq)
1382{
1383 struct efx_channel *channel;
1384
1385 efx_mcdi_mode_poll(efx);
1386
1387 efx_nic_disable_interrupts(efx);
1388 if (efx->legacy_irq) {
1389 synchronize_irq(efx->legacy_irq);
1390 efx->legacy_irq_enabled = false;
1391 }
1392
1393 efx_for_each_channel(channel, efx) {
1394 if (channel->irq)
1395 synchronize_irq(channel->irq);
1396
1397 efx_stop_eventq(channel);
1398 if (!channel->type->keep_eventq || !may_keep_eventq)
1399 efx_fini_eventq(channel);
1400 }
1401}
1402
1403static void efx_remove_interrupts(struct efx_nic *efx)
1404{
1405 struct efx_channel *channel;
1406
1407 /* Remove MSI/MSI-X interrupts */
1408 efx_for_each_channel(channel, efx)
1409 channel->irq = 0;
1410 pci_disable_msi(efx->pci_dev);
1411 pci_disable_msix(efx->pci_dev);
1412
1413 /* Remove legacy interrupt */
1414 efx->legacy_irq = 0;
1415}
1416
1417static void efx_set_channels(struct efx_nic *efx)
1418{
1419 struct efx_channel *channel;
1420 struct efx_tx_queue *tx_queue;
1421
1422 efx->tx_channel_offset =
1423 separate_tx_channels ? efx->n_channels - efx->n_tx_channels : 0;
1424
1425 /* We need to adjust the TX queue numbers if we have separate
1426 * RX-only and TX-only channels.
1427 */
1428 efx_for_each_channel(channel, efx) {
1429 efx_for_each_channel_tx_queue(tx_queue, channel)
1430 tx_queue->queue -= (efx->tx_channel_offset *
1431 EFX_TXQ_TYPES);
1432 }
1433}
1434
1435static int efx_probe_nic(struct efx_nic *efx)
1436{
1437 size_t i;
1438 int rc;
1439
1440 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
1441
1442 /* Carry out hardware-type specific initialisation */
1443 rc = efx->type->probe(efx);
1444 if (rc)
1445 return rc;
1446
1447 /* Determine the number of channels and queues by trying to hook
1448 * in MSI-X interrupts. */
1449 rc = efx_probe_interrupts(efx);
1450 if (rc)
1451 goto fail;
1452
1453 efx->type->dimension_resources(efx);
1454
1455 if (efx->n_channels > 1)
1456 get_random_bytes(&efx->rx_hash_key, sizeof(efx->rx_hash_key));
1457 for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
1458 efx->rx_indir_table[i] =
1459 ethtool_rxfh_indir_default(i, efx->rss_spread);
1460
1461 efx_set_channels(efx);
1462 netif_set_real_num_tx_queues(efx->net_dev, efx->n_tx_channels);
1463 netif_set_real_num_rx_queues(efx->net_dev, efx->n_rx_channels);
1464
1465 /* Initialise the interrupt moderation settings */
1466 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
1467 true);
1468
1469 return 0;
1470
1471fail:
1472 efx->type->remove(efx);
1473 return rc;
1474}
1475
1476static void efx_remove_nic(struct efx_nic *efx)
1477{
1478 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
1479
1480 efx_remove_interrupts(efx);
1481 efx->type->remove(efx);
1482}
1483
1484/**************************************************************************
1485 *
1486 * NIC startup/shutdown
1487 *
1488 *************************************************************************/
1489
1490static int efx_probe_all(struct efx_nic *efx)
1491{
1492 int rc;
1493
1494 rc = efx_probe_nic(efx);
1495 if (rc) {
1496 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
1497 goto fail1;
1498 }
1499
1500 rc = efx_probe_port(efx);
1501 if (rc) {
1502 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
1503 goto fail2;
1504 }
1505
1506 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
1507 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
1508 rc = -EINVAL;
1509 goto fail3;
1510 }
1511 efx->rxq_entries = efx->txq_entries = EFX_DEFAULT_DMAQ_SIZE;
1512
1513 rc = efx_probe_filters(efx);
1514 if (rc) {
1515 netif_err(efx, probe, efx->net_dev,
1516 "failed to create filter tables\n");
1517 goto fail3;
1518 }
1519
1520 rc = efx_probe_channels(efx);
1521 if (rc)
1522 goto fail4;
1523
1524 return 0;
1525
1526 fail4:
1527 efx_remove_filters(efx);
1528 fail3:
1529 efx_remove_port(efx);
1530 fail2:
1531 efx_remove_nic(efx);
1532 fail1:
1533 return rc;
1534}
1535
1536/* Called after previous invocation(s) of efx_stop_all, restarts the port,
1537 * kernel transmit queues and NAPI processing, and ensures that the port is
1538 * scheduled to be reconfigured. This function is safe to call multiple
1539 * times when the NIC is in any state.
1540 */
1541static void efx_start_all(struct efx_nic *efx)
1542{
1543 EFX_ASSERT_RESET_SERIALISED(efx);
1544
1545 /* Check that it is appropriate to restart the interface. All
1546 * of these flags are safe to read under just the rtnl lock */
1547 if (efx->port_enabled)
1548 return;
1549 if ((efx->state != STATE_RUNNING) && (efx->state != STATE_INIT))
1550 return;
1551 if (!netif_running(efx->net_dev))
1552 return;
1553
1554 efx_start_port(efx);
1555 efx_start_datapath(efx);
1556
1557 /* Start the hardware monitor if there is one. Otherwise (we're link
1558 * event driven), we have to poll the PHY because after an event queue
1559 * flush, we could have a missed a link state change */
1560 if (efx->type->monitor != NULL) {
1561 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1562 efx_monitor_interval);
1563 } else {
1564 mutex_lock(&efx->mac_lock);
1565 if (efx->phy_op->poll(efx))
1566 efx_link_status_changed(efx);
1567 mutex_unlock(&efx->mac_lock);
1568 }
1569
1570 efx->type->start_stats(efx);
1571}
1572
1573/* Flush all delayed work. Should only be called when no more delayed work
1574 * will be scheduled. This doesn't flush pending online resets (efx_reset),
1575 * since we're holding the rtnl_lock at this point. */
1576static void efx_flush_all(struct efx_nic *efx)
1577{
1578 /* Make sure the hardware monitor and event self-test are stopped */
1579 cancel_delayed_work_sync(&efx->monitor_work);
1580 efx_selftest_async_cancel(efx);
1581 /* Stop scheduled port reconfigurations */
1582 cancel_work_sync(&efx->mac_work);
1583}
1584
1585/* Quiesce hardware and software without bringing the link down.
1586 * Safe to call multiple times, when the nic and interface is in any
1587 * state. The caller is guaranteed to subsequently be in a position
1588 * to modify any hardware and software state they see fit without
1589 * taking locks. */
1590static void efx_stop_all(struct efx_nic *efx)
1591{
1592 EFX_ASSERT_RESET_SERIALISED(efx);
1593
1594 /* port_enabled can be read safely under the rtnl lock */
1595 if (!efx->port_enabled)
1596 return;
1597
1598 efx->type->stop_stats(efx);
1599 efx_stop_port(efx);
1600
1601 /* Flush efx_mac_work(), refill_workqueue, monitor_work */
1602 efx_flush_all(efx);
1603
1604 /* Stop the kernel transmit interface late, so the watchdog
1605 * timer isn't ticking over the flush */
1606 netif_tx_disable(efx->net_dev);
1607
1608 efx_stop_datapath(efx);
1609}
1610
1611static void efx_remove_all(struct efx_nic *efx)
1612{
1613 efx_remove_channels(efx);
1614 efx_remove_filters(efx);
1615 efx_remove_port(efx);
1616 efx_remove_nic(efx);
1617}
1618
1619/**************************************************************************
1620 *
1621 * Interrupt moderation
1622 *
1623 **************************************************************************/
1624
1625static unsigned int irq_mod_ticks(unsigned int usecs, unsigned int quantum_ns)
1626{
1627 if (usecs == 0)
1628 return 0;
1629 if (usecs * 1000 < quantum_ns)
1630 return 1; /* never round down to 0 */
1631 return usecs * 1000 / quantum_ns;
1632}
1633
1634/* Set interrupt moderation parameters */
1635int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
1636 unsigned int rx_usecs, bool rx_adaptive,
1637 bool rx_may_override_tx)
1638{
1639 struct efx_channel *channel;
1640 unsigned int irq_mod_max = DIV_ROUND_UP(efx->type->timer_period_max *
1641 efx->timer_quantum_ns,
1642 1000);
1643 unsigned int tx_ticks;
1644 unsigned int rx_ticks;
1645
1646 EFX_ASSERT_RESET_SERIALISED(efx);
1647
1648 if (tx_usecs > irq_mod_max || rx_usecs > irq_mod_max)
1649 return -EINVAL;
1650
1651 tx_ticks = irq_mod_ticks(tx_usecs, efx->timer_quantum_ns);
1652 rx_ticks = irq_mod_ticks(rx_usecs, efx->timer_quantum_ns);
1653
1654 if (tx_ticks != rx_ticks && efx->tx_channel_offset == 0 &&
1655 !rx_may_override_tx) {
1656 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
1657 "RX and TX IRQ moderation must be equal\n");
1658 return -EINVAL;
1659 }
1660
1661 efx->irq_rx_adaptive = rx_adaptive;
1662 efx->irq_rx_moderation = rx_ticks;
1663 efx_for_each_channel(channel, efx) {
1664 if (efx_channel_has_rx_queue(channel))
1665 channel->irq_moderation = rx_ticks;
1666 else if (efx_channel_has_tx_queues(channel))
1667 channel->irq_moderation = tx_ticks;
1668 }
1669
1670 return 0;
1671}
1672
1673void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
1674 unsigned int *rx_usecs, bool *rx_adaptive)
1675{
1676 /* We must round up when converting ticks to microseconds
1677 * because we round down when converting the other way.
1678 */
1679
1680 *rx_adaptive = efx->irq_rx_adaptive;
1681 *rx_usecs = DIV_ROUND_UP(efx->irq_rx_moderation *
1682 efx->timer_quantum_ns,
1683 1000);
1684
1685 /* If channels are shared between RX and TX, so is IRQ
1686 * moderation. Otherwise, IRQ moderation is the same for all
1687 * TX channels and is not adaptive.
1688 */
1689 if (efx->tx_channel_offset == 0)
1690 *tx_usecs = *rx_usecs;
1691 else
1692 *tx_usecs = DIV_ROUND_UP(
1693 efx->channel[efx->tx_channel_offset]->irq_moderation *
1694 efx->timer_quantum_ns,
1695 1000);
1696}
1697
1698/**************************************************************************
1699 *
1700 * Hardware monitor
1701 *
1702 **************************************************************************/
1703
1704/* Run periodically off the general workqueue */
1705static void efx_monitor(struct work_struct *data)
1706{
1707 struct efx_nic *efx = container_of(data, struct efx_nic,
1708 monitor_work.work);
1709
1710 netif_vdbg(efx, timer, efx->net_dev,
1711 "hardware monitor executing on CPU %d\n",
1712 raw_smp_processor_id());
1713 BUG_ON(efx->type->monitor == NULL);
1714
1715 /* If the mac_lock is already held then it is likely a port
1716 * reconfiguration is already in place, which will likely do
1717 * most of the work of monitor() anyway. */
1718 if (mutex_trylock(&efx->mac_lock)) {
1719 if (efx->port_enabled)
1720 efx->type->monitor(efx);
1721 mutex_unlock(&efx->mac_lock);
1722 }
1723
1724 queue_delayed_work(efx->workqueue, &efx->monitor_work,
1725 efx_monitor_interval);
1726}
1727
1728/**************************************************************************
1729 *
1730 * ioctls
1731 *
1732 *************************************************************************/
1733
1734/* Net device ioctl
1735 * Context: process, rtnl_lock() held.
1736 */
1737static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
1738{
1739 struct efx_nic *efx = netdev_priv(net_dev);
1740 struct mii_ioctl_data *data = if_mii(ifr);
1741
1742 EFX_ASSERT_RESET_SERIALISED(efx);
1743
1744 /* Convert phy_id from older PRTAD/DEVAD format */
1745 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
1746 (data->phy_id & 0xfc00) == 0x0400)
1747 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
1748
1749 return mdio_mii_ioctl(&efx->mdio, data, cmd);
1750}
1751
1752/**************************************************************************
1753 *
1754 * NAPI interface
1755 *
1756 **************************************************************************/
1757
1758static void efx_init_napi_channel(struct efx_channel *channel)
1759{
1760 struct efx_nic *efx = channel->efx;
1761
1762 channel->napi_dev = efx->net_dev;
1763 netif_napi_add(channel->napi_dev, &channel->napi_str,
1764 efx_poll, napi_weight);
1765}
1766
1767static void efx_init_napi(struct efx_nic *efx)
1768{
1769 struct efx_channel *channel;
1770
1771 efx_for_each_channel(channel, efx)
1772 efx_init_napi_channel(channel);
1773}
1774
1775static void efx_fini_napi_channel(struct efx_channel *channel)
1776{
1777 if (channel->napi_dev)
1778 netif_napi_del(&channel->napi_str);
1779 channel->napi_dev = NULL;
1780}
1781
1782static void efx_fini_napi(struct efx_nic *efx)
1783{
1784 struct efx_channel *channel;
1785
1786 efx_for_each_channel(channel, efx)
1787 efx_fini_napi_channel(channel);
1788}
1789
1790/**************************************************************************
1791 *
1792 * Kernel netpoll interface
1793 *
1794 *************************************************************************/
1795
1796#ifdef CONFIG_NET_POLL_CONTROLLER
1797
1798/* Although in the common case interrupts will be disabled, this is not
1799 * guaranteed. However, all our work happens inside the NAPI callback,
1800 * so no locking is required.
1801 */
1802static void efx_netpoll(struct net_device *net_dev)
1803{
1804 struct efx_nic *efx = netdev_priv(net_dev);
1805 struct efx_channel *channel;
1806
1807 efx_for_each_channel(channel, efx)
1808 efx_schedule_channel(channel);
1809}
1810
1811#endif
1812
1813/**************************************************************************
1814 *
1815 * Kernel net device interface
1816 *
1817 *************************************************************************/
1818
1819/* Context: process, rtnl_lock() held. */
1820static int efx_net_open(struct net_device *net_dev)
1821{
1822 struct efx_nic *efx = netdev_priv(net_dev);
1823 EFX_ASSERT_RESET_SERIALISED(efx);
1824
1825 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
1826 raw_smp_processor_id());
1827
1828 if (efx->state == STATE_DISABLED)
1829 return -EIO;
1830 if (efx->phy_mode & PHY_MODE_SPECIAL)
1831 return -EBUSY;
1832 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
1833 return -EIO;
1834
1835 /* Notify the kernel of the link state polled during driver load,
1836 * before the monitor starts running */
1837 efx_link_status_changed(efx);
1838
1839 efx_start_all(efx);
1840 efx_selftest_async_start(efx);
1841 return 0;
1842}
1843
1844/* Context: process, rtnl_lock() held.
1845 * Note that the kernel will ignore our return code; this method
1846 * should really be a void.
1847 */
1848static int efx_net_stop(struct net_device *net_dev)
1849{
1850 struct efx_nic *efx = netdev_priv(net_dev);
1851
1852 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
1853 raw_smp_processor_id());
1854
1855 if (efx->state != STATE_DISABLED) {
1856 /* Stop the device and flush all the channels */
1857 efx_stop_all(efx);
1858 }
1859
1860 return 0;
1861}
1862
1863/* Context: process, dev_base_lock or RTNL held, non-blocking. */
1864static struct rtnl_link_stats64 *efx_net_stats(struct net_device *net_dev,
1865 struct rtnl_link_stats64 *stats)
1866{
1867 struct efx_nic *efx = netdev_priv(net_dev);
1868 struct efx_mac_stats *mac_stats = &efx->mac_stats;
1869
1870 spin_lock_bh(&efx->stats_lock);
1871
1872 efx->type->update_stats(efx);
1873
1874 stats->rx_packets = mac_stats->rx_packets;
1875 stats->tx_packets = mac_stats->tx_packets;
1876 stats->rx_bytes = mac_stats->rx_bytes;
1877 stats->tx_bytes = mac_stats->tx_bytes;
1878 stats->rx_dropped = efx->n_rx_nodesc_drop_cnt;
1879 stats->multicast = mac_stats->rx_multicast;
1880 stats->collisions = mac_stats->tx_collision;
1881 stats->rx_length_errors = (mac_stats->rx_gtjumbo +
1882 mac_stats->rx_length_error);
1883 stats->rx_crc_errors = mac_stats->rx_bad;
1884 stats->rx_frame_errors = mac_stats->rx_align_error;
1885 stats->rx_fifo_errors = mac_stats->rx_overflow;
1886 stats->rx_missed_errors = mac_stats->rx_missed;
1887 stats->tx_window_errors = mac_stats->tx_late_collision;
1888
1889 stats->rx_errors = (stats->rx_length_errors +
1890 stats->rx_crc_errors +
1891 stats->rx_frame_errors +
1892 mac_stats->rx_symbol_error);
1893 stats->tx_errors = (stats->tx_window_errors +
1894 mac_stats->tx_bad);
1895
1896 spin_unlock_bh(&efx->stats_lock);
1897
1898 return stats;
1899}
1900
1901/* Context: netif_tx_lock held, BHs disabled. */
1902static void efx_watchdog(struct net_device *net_dev)
1903{
1904 struct efx_nic *efx = netdev_priv(net_dev);
1905
1906 netif_err(efx, tx_err, efx->net_dev,
1907 "TX stuck with port_enabled=%d: resetting channels\n",
1908 efx->port_enabled);
1909
1910 efx_schedule_reset(efx, RESET_TYPE_TX_WATCHDOG);
1911}
1912
1913
1914/* Context: process, rtnl_lock() held. */
1915static int efx_change_mtu(struct net_device *net_dev, int new_mtu)
1916{
1917 struct efx_nic *efx = netdev_priv(net_dev);
1918
1919 EFX_ASSERT_RESET_SERIALISED(efx);
1920
1921 if (new_mtu > EFX_MAX_MTU)
1922 return -EINVAL;
1923
1924 efx_stop_all(efx);
1925
1926 netif_dbg(efx, drv, efx->net_dev, "changing MTU to %d\n", new_mtu);
1927
1928 mutex_lock(&efx->mac_lock);
1929 /* Reconfigure the MAC before enabling the dma queues so that
1930 * the RX buffers don't overflow */
1931 net_dev->mtu = new_mtu;
1932 efx->type->reconfigure_mac(efx);
1933 mutex_unlock(&efx->mac_lock);
1934
1935 efx_start_all(efx);
1936 return 0;
1937}
1938
1939static int efx_set_mac_address(struct net_device *net_dev, void *data)
1940{
1941 struct efx_nic *efx = netdev_priv(net_dev);
1942 struct sockaddr *addr = data;
1943 char *new_addr = addr->sa_data;
1944
1945 EFX_ASSERT_RESET_SERIALISED(efx);
1946
1947 if (!is_valid_ether_addr(new_addr)) {
1948 netif_err(efx, drv, efx->net_dev,
1949 "invalid ethernet MAC address requested: %pM\n",
1950 new_addr);
1951 return -EADDRNOTAVAIL;
1952 }
1953
1954 memcpy(net_dev->dev_addr, new_addr, net_dev->addr_len);
1955 efx_sriov_mac_address_changed(efx);
1956
1957 /* Reconfigure the MAC */
1958 mutex_lock(&efx->mac_lock);
1959 efx->type->reconfigure_mac(efx);
1960 mutex_unlock(&efx->mac_lock);
1961
1962 return 0;
1963}
1964
1965/* Context: netif_addr_lock held, BHs disabled. */
1966static void efx_set_rx_mode(struct net_device *net_dev)
1967{
1968 struct efx_nic *efx = netdev_priv(net_dev);
1969 struct netdev_hw_addr *ha;
1970 union efx_multicast_hash *mc_hash = &efx->multicast_hash;
1971 u32 crc;
1972 int bit;
1973
1974 efx->promiscuous = !!(net_dev->flags & IFF_PROMISC);
1975
1976 /* Build multicast hash table */
1977 if (efx->promiscuous || (net_dev->flags & IFF_ALLMULTI)) {
1978 memset(mc_hash, 0xff, sizeof(*mc_hash));
1979 } else {
1980 memset(mc_hash, 0x00, sizeof(*mc_hash));
1981 netdev_for_each_mc_addr(ha, net_dev) {
1982 crc = ether_crc_le(ETH_ALEN, ha->addr);
1983 bit = crc & (EFX_MCAST_HASH_ENTRIES - 1);
1984 set_bit_le(bit, mc_hash->byte);
1985 }
1986
1987 /* Broadcast packets go through the multicast hash filter.
1988 * ether_crc_le() of the broadcast address is 0xbe2612ff
1989 * so we always add bit 0xff to the mask.
1990 */
1991 set_bit_le(0xff, mc_hash->byte);
1992 }
1993
1994 if (efx->port_enabled)
1995 queue_work(efx->workqueue, &efx->mac_work);
1996 /* Otherwise efx_start_port() will do this */
1997}
1998
1999static int efx_set_features(struct net_device *net_dev, netdev_features_t data)
2000{
2001 struct efx_nic *efx = netdev_priv(net_dev);
2002
2003 /* If disabling RX n-tuple filtering, clear existing filters */
2004 if (net_dev->features & ~data & NETIF_F_NTUPLE)
2005 efx_filter_clear_rx(efx, EFX_FILTER_PRI_MANUAL);
2006
2007 return 0;
2008}
2009
2010static const struct net_device_ops efx_netdev_ops = {
2011 .ndo_open = efx_net_open,
2012 .ndo_stop = efx_net_stop,
2013 .ndo_get_stats64 = efx_net_stats,
2014 .ndo_tx_timeout = efx_watchdog,
2015 .ndo_start_xmit = efx_hard_start_xmit,
2016 .ndo_validate_addr = eth_validate_addr,
2017 .ndo_do_ioctl = efx_ioctl,
2018 .ndo_change_mtu = efx_change_mtu,
2019 .ndo_set_mac_address = efx_set_mac_address,
2020 .ndo_set_rx_mode = efx_set_rx_mode,
2021 .ndo_set_features = efx_set_features,
2022#ifdef CONFIG_SFC_SRIOV
2023 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
2024 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
2025 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
2026 .ndo_get_vf_config = efx_sriov_get_vf_config,
2027#endif
2028#ifdef CONFIG_NET_POLL_CONTROLLER
2029 .ndo_poll_controller = efx_netpoll,
2030#endif
2031 .ndo_setup_tc = efx_setup_tc,
2032#ifdef CONFIG_RFS_ACCEL
2033 .ndo_rx_flow_steer = efx_filter_rfs,
2034#endif
2035};
2036
2037static void efx_update_name(struct efx_nic *efx)
2038{
2039 strcpy(efx->name, efx->net_dev->name);
2040 efx_mtd_rename(efx);
2041 efx_set_channel_names(efx);
2042}
2043
2044static int efx_netdev_event(struct notifier_block *this,
2045 unsigned long event, void *ptr)
2046{
2047 struct net_device *net_dev = ptr;
2048
2049 if (net_dev->netdev_ops == &efx_netdev_ops &&
2050 event == NETDEV_CHANGENAME)
2051 efx_update_name(netdev_priv(net_dev));
2052
2053 return NOTIFY_DONE;
2054}
2055
2056static struct notifier_block efx_netdev_notifier = {
2057 .notifier_call = efx_netdev_event,
2058};
2059
2060static ssize_t
2061show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
2062{
2063 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2064 return sprintf(buf, "%d\n", efx->phy_type);
2065}
2066static DEVICE_ATTR(phy_type, 0644, show_phy_type, NULL);
2067
2068static int efx_register_netdev(struct efx_nic *efx)
2069{
2070 struct net_device *net_dev = efx->net_dev;
2071 struct efx_channel *channel;
2072 int rc;
2073
2074 net_dev->watchdog_timeo = 5 * HZ;
2075 net_dev->irq = efx->pci_dev->irq;
2076 net_dev->netdev_ops = &efx_netdev_ops;
2077 SET_ETHTOOL_OPS(net_dev, &efx_ethtool_ops);
2078 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
2079
2080 rtnl_lock();
2081
2082 rc = dev_alloc_name(net_dev, net_dev->name);
2083 if (rc < 0)
2084 goto fail_locked;
2085 efx_update_name(efx);
2086
2087 rc = register_netdevice(net_dev);
2088 if (rc)
2089 goto fail_locked;
2090
2091 efx_for_each_channel(channel, efx) {
2092 struct efx_tx_queue *tx_queue;
2093 efx_for_each_channel_tx_queue(tx_queue, channel)
2094 efx_init_tx_queue_core_txq(tx_queue);
2095 }
2096
2097 /* Always start with carrier off; PHY events will detect the link */
2098 netif_carrier_off(net_dev);
2099
2100 rtnl_unlock();
2101
2102 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2103 if (rc) {
2104 netif_err(efx, drv, efx->net_dev,
2105 "failed to init net dev attributes\n");
2106 goto fail_registered;
2107 }
2108
2109 return 0;
2110
2111fail_locked:
2112 rtnl_unlock();
2113 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
2114 return rc;
2115
2116fail_registered:
2117 unregister_netdev(net_dev);
2118 return rc;
2119}
2120
2121static void efx_unregister_netdev(struct efx_nic *efx)
2122{
2123 struct efx_channel *channel;
2124 struct efx_tx_queue *tx_queue;
2125
2126 if (!efx->net_dev)
2127 return;
2128
2129 BUG_ON(netdev_priv(efx->net_dev) != efx);
2130
2131 /* Free up any skbs still remaining. This has to happen before
2132 * we try to unregister the netdev as running their destructors
2133 * may be needed to get the device ref. count to 0. */
2134 efx_for_each_channel(channel, efx) {
2135 efx_for_each_channel_tx_queue(tx_queue, channel)
2136 efx_release_tx_buffers(tx_queue);
2137 }
2138
2139 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
2140 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
2141 unregister_netdev(efx->net_dev);
2142}
2143
2144/**************************************************************************
2145 *
2146 * Device reset and suspend
2147 *
2148 **************************************************************************/
2149
2150/* Tears down the entire software state and most of the hardware state
2151 * before reset. */
2152void efx_reset_down(struct efx_nic *efx, enum reset_type method)
2153{
2154 EFX_ASSERT_RESET_SERIALISED(efx);
2155
2156 efx_stop_all(efx);
2157 mutex_lock(&efx->mac_lock);
2158
2159 efx_stop_interrupts(efx, false);
2160 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE)
2161 efx->phy_op->fini(efx);
2162 efx->type->fini(efx);
2163}
2164
2165/* This function will always ensure that the locks acquired in
2166 * efx_reset_down() are released. A failure return code indicates
2167 * that we were unable to reinitialise the hardware, and the
2168 * driver should be disabled. If ok is false, then the rx and tx
2169 * engines are not restarted, pending a RESET_DISABLE. */
2170int efx_reset_up(struct efx_nic *efx, enum reset_type method, bool ok)
2171{
2172 int rc;
2173
2174 EFX_ASSERT_RESET_SERIALISED(efx);
2175
2176 rc = efx->type->init(efx);
2177 if (rc) {
2178 netif_err(efx, drv, efx->net_dev, "failed to initialise NIC\n");
2179 goto fail;
2180 }
2181
2182 if (!ok)
2183 goto fail;
2184
2185 if (efx->port_initialized && method != RESET_TYPE_INVISIBLE) {
2186 rc = efx->phy_op->init(efx);
2187 if (rc)
2188 goto fail;
2189 if (efx->phy_op->reconfigure(efx))
2190 netif_err(efx, drv, efx->net_dev,
2191 "could not restore PHY settings\n");
2192 }
2193
2194 efx->type->reconfigure_mac(efx);
2195
2196 efx_start_interrupts(efx, false);
2197 efx_restore_filters(efx);
2198 efx_sriov_reset(efx);
2199
2200 mutex_unlock(&efx->mac_lock);
2201
2202 efx_start_all(efx);
2203
2204 return 0;
2205
2206fail:
2207 efx->port_initialized = false;
2208
2209 mutex_unlock(&efx->mac_lock);
2210
2211 return rc;
2212}
2213
2214/* Reset the NIC using the specified method. Note that the reset may
2215 * fail, in which case the card will be left in an unusable state.
2216 *
2217 * Caller must hold the rtnl_lock.
2218 */
2219int efx_reset(struct efx_nic *efx, enum reset_type method)
2220{
2221 int rc, rc2;
2222 bool disabled;
2223
2224 netif_info(efx, drv, efx->net_dev, "resetting (%s)\n",
2225 RESET_TYPE(method));
2226
2227 netif_device_detach(efx->net_dev);
2228 efx_reset_down(efx, method);
2229
2230 rc = efx->type->reset(efx, method);
2231 if (rc) {
2232 netif_err(efx, drv, efx->net_dev, "failed to reset hardware\n");
2233 goto out;
2234 }
2235
2236 /* Clear flags for the scopes we covered. We assume the NIC and
2237 * driver are now quiescent so that there is no race here.
2238 */
2239 efx->reset_pending &= -(1 << (method + 1));
2240
2241 /* Reinitialise bus-mastering, which may have been turned off before
2242 * the reset was scheduled. This is still appropriate, even in the
2243 * RESET_TYPE_DISABLE since this driver generally assumes the hardware
2244 * can respond to requests. */
2245 pci_set_master(efx->pci_dev);
2246
2247out:
2248 /* Leave device stopped if necessary */
2249 disabled = rc || method == RESET_TYPE_DISABLE;
2250 rc2 = efx_reset_up(efx, method, !disabled);
2251 if (rc2) {
2252 disabled = true;
2253 if (!rc)
2254 rc = rc2;
2255 }
2256
2257 if (disabled) {
2258 dev_close(efx->net_dev);
2259 netif_err(efx, drv, efx->net_dev, "has been disabled\n");
2260 efx->state = STATE_DISABLED;
2261 } else {
2262 netif_dbg(efx, drv, efx->net_dev, "reset complete\n");
2263 netif_device_attach(efx->net_dev);
2264 }
2265 return rc;
2266}
2267
2268/* The worker thread exists so that code that cannot sleep can
2269 * schedule a reset for later.
2270 */
2271static void efx_reset_work(struct work_struct *data)
2272{
2273 struct efx_nic *efx = container_of(data, struct efx_nic, reset_work);
2274 unsigned long pending = ACCESS_ONCE(efx->reset_pending);
2275
2276 if (!pending)
2277 return;
2278
2279 /* If we're not RUNNING then don't reset. Leave the reset_pending
2280 * flags set so that efx_pci_probe_main will be retried */
2281 if (efx->state != STATE_RUNNING) {
2282 netif_info(efx, drv, efx->net_dev,
2283 "scheduled reset quenched. NIC not RUNNING\n");
2284 return;
2285 }
2286
2287 rtnl_lock();
2288 (void)efx_reset(efx, fls(pending) - 1);
2289 rtnl_unlock();
2290}
2291
2292void efx_schedule_reset(struct efx_nic *efx, enum reset_type type)
2293{
2294 enum reset_type method;
2295
2296 switch (type) {
2297 case RESET_TYPE_INVISIBLE:
2298 case RESET_TYPE_ALL:
2299 case RESET_TYPE_WORLD:
2300 case RESET_TYPE_DISABLE:
2301 method = type;
2302 netif_dbg(efx, drv, efx->net_dev, "scheduling %s reset\n",
2303 RESET_TYPE(method));
2304 break;
2305 default:
2306 method = efx->type->map_reset_reason(type);
2307 netif_dbg(efx, drv, efx->net_dev,
2308 "scheduling %s reset for %s\n",
2309 RESET_TYPE(method), RESET_TYPE(type));
2310 break;
2311 }
2312
2313 set_bit(method, &efx->reset_pending);
2314
2315 /* efx_process_channel() will no longer read events once a
2316 * reset is scheduled. So switch back to poll'd MCDI completions. */
2317 efx_mcdi_mode_poll(efx);
2318
2319 queue_work(reset_workqueue, &efx->reset_work);
2320}
2321
2322/**************************************************************************
2323 *
2324 * List of NICs we support
2325 *
2326 **************************************************************************/
2327
2328/* PCI device ID table */
2329static DEFINE_PCI_DEVICE_TABLE(efx_pci_table) = {
2330 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2331 PCI_DEVICE_ID_SOLARFLARE_SFC4000A_0),
2332 .driver_data = (unsigned long) &falcon_a1_nic_type},
2333 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE,
2334 PCI_DEVICE_ID_SOLARFLARE_SFC4000B),
2335 .driver_data = (unsigned long) &falcon_b0_nic_type},
2336 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
2337 .driver_data = (unsigned long) &siena_a0_nic_type},
2338 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
2339 .driver_data = (unsigned long) &siena_a0_nic_type},
2340 {0} /* end of list */
2341};
2342
2343/**************************************************************************
2344 *
2345 * Dummy PHY/MAC operations
2346 *
2347 * Can be used for some unimplemented operations
2348 * Needed so all function pointers are valid and do not have to be tested
2349 * before use
2350 *
2351 **************************************************************************/
2352int efx_port_dummy_op_int(struct efx_nic *efx)
2353{
2354 return 0;
2355}
2356void efx_port_dummy_op_void(struct efx_nic *efx) {}
2357
2358static bool efx_port_dummy_op_poll(struct efx_nic *efx)
2359{
2360 return false;
2361}
2362
2363static const struct efx_phy_operations efx_dummy_phy_operations = {
2364 .init = efx_port_dummy_op_int,
2365 .reconfigure = efx_port_dummy_op_int,
2366 .poll = efx_port_dummy_op_poll,
2367 .fini = efx_port_dummy_op_void,
2368};
2369
2370/**************************************************************************
2371 *
2372 * Data housekeeping
2373 *
2374 **************************************************************************/
2375
2376/* This zeroes out and then fills in the invariants in a struct
2377 * efx_nic (including all sub-structures).
2378 */
2379static int efx_init_struct(struct efx_nic *efx, const struct efx_nic_type *type,
2380 struct pci_dev *pci_dev, struct net_device *net_dev)
2381{
2382 int i;
2383
2384 /* Initialise common structures */
2385 memset(efx, 0, sizeof(*efx));
2386 spin_lock_init(&efx->biu_lock);
2387#ifdef CONFIG_SFC_MTD
2388 INIT_LIST_HEAD(&efx->mtd_list);
2389#endif
2390 INIT_WORK(&efx->reset_work, efx_reset_work);
2391 INIT_DELAYED_WORK(&efx->monitor_work, efx_monitor);
2392 INIT_DELAYED_WORK(&efx->selftest_work, efx_selftest_async_work);
2393 efx->pci_dev = pci_dev;
2394 efx->msg_enable = debug;
2395 efx->state = STATE_INIT;
2396 strlcpy(efx->name, pci_name(pci_dev), sizeof(efx->name));
2397
2398 efx->net_dev = net_dev;
2399 spin_lock_init(&efx->stats_lock);
2400 mutex_init(&efx->mac_lock);
2401 efx->phy_op = &efx_dummy_phy_operations;
2402 efx->mdio.dev = net_dev;
2403 INIT_WORK(&efx->mac_work, efx_mac_work);
2404 init_waitqueue_head(&efx->flush_wq);
2405
2406 for (i = 0; i < EFX_MAX_CHANNELS; i++) {
2407 efx->channel[i] = efx_alloc_channel(efx, i, NULL);
2408 if (!efx->channel[i])
2409 goto fail;
2410 }
2411
2412 efx->type = type;
2413
2414 EFX_BUG_ON_PARANOID(efx->type->phys_addr_channels > EFX_MAX_CHANNELS);
2415
2416 /* Higher numbered interrupt modes are less capable! */
2417 efx->interrupt_mode = max(efx->type->max_interrupt_mode,
2418 interrupt_mode);
2419
2420 /* Would be good to use the net_dev name, but we're too early */
2421 snprintf(efx->workqueue_name, sizeof(efx->workqueue_name), "sfc%s",
2422 pci_name(pci_dev));
2423 efx->workqueue = create_singlethread_workqueue(efx->workqueue_name);
2424 if (!efx->workqueue)
2425 goto fail;
2426
2427 return 0;
2428
2429fail:
2430 efx_fini_struct(efx);
2431 return -ENOMEM;
2432}
2433
2434static void efx_fini_struct(struct efx_nic *efx)
2435{
2436 int i;
2437
2438 for (i = 0; i < EFX_MAX_CHANNELS; i++)
2439 kfree(efx->channel[i]);
2440
2441 if (efx->workqueue) {
2442 destroy_workqueue(efx->workqueue);
2443 efx->workqueue = NULL;
2444 }
2445}
2446
2447/**************************************************************************
2448 *
2449 * PCI interface
2450 *
2451 **************************************************************************/
2452
2453/* Main body of final NIC shutdown code
2454 * This is called only at module unload (or hotplug removal).
2455 */
2456static void efx_pci_remove_main(struct efx_nic *efx)
2457{
2458#ifdef CONFIG_RFS_ACCEL
2459 free_irq_cpu_rmap(efx->net_dev->rx_cpu_rmap);
2460 efx->net_dev->rx_cpu_rmap = NULL;
2461#endif
2462 efx_stop_interrupts(efx, false);
2463 efx_nic_fini_interrupt(efx);
2464 efx_fini_port(efx);
2465 efx->type->fini(efx);
2466 efx_fini_napi(efx);
2467 efx_remove_all(efx);
2468}
2469
2470/* Final NIC shutdown
2471 * This is called only at module unload (or hotplug removal).
2472 */
2473static void efx_pci_remove(struct pci_dev *pci_dev)
2474{
2475 struct efx_nic *efx;
2476
2477 efx = pci_get_drvdata(pci_dev);
2478 if (!efx)
2479 return;
2480
2481 /* Mark the NIC as fini, then stop the interface */
2482 rtnl_lock();
2483 efx->state = STATE_FINI;
2484 dev_close(efx->net_dev);
2485
2486 /* Allow any queued efx_resets() to complete */
2487 rtnl_unlock();
2488
2489 efx_stop_interrupts(efx, false);
2490 efx_sriov_fini(efx);
2491 efx_unregister_netdev(efx);
2492
2493 efx_mtd_remove(efx);
2494
2495 /* Wait for any scheduled resets to complete. No more will be
2496 * scheduled from this point because efx_stop_all() has been
2497 * called, we are no longer registered with driverlink, and
2498 * the net_device's have been removed. */
2499 cancel_work_sync(&efx->reset_work);
2500
2501 efx_pci_remove_main(efx);
2502
2503 efx_fini_io(efx);
2504 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
2505
2506 efx_fini_struct(efx);
2507 pci_set_drvdata(pci_dev, NULL);
2508 free_netdev(efx->net_dev);
2509};
2510
2511/* NIC VPD information
2512 * Called during probe to display the part number of the
2513 * installed NIC. VPD is potentially very large but this should
2514 * always appear within the first 512 bytes.
2515 */
2516#define SFC_VPD_LEN 512
2517static void efx_print_product_vpd(struct efx_nic *efx)
2518{
2519 struct pci_dev *dev = efx->pci_dev;
2520 char vpd_data[SFC_VPD_LEN];
2521 ssize_t vpd_size;
2522 int i, j;
2523
2524 /* Get the vpd data from the device */
2525 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
2526 if (vpd_size <= 0) {
2527 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
2528 return;
2529 }
2530
2531 /* Get the Read only section */
2532 i = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
2533 if (i < 0) {
2534 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
2535 return;
2536 }
2537
2538 j = pci_vpd_lrdt_size(&vpd_data[i]);
2539 i += PCI_VPD_LRDT_TAG_SIZE;
2540 if (i + j > vpd_size)
2541 j = vpd_size - i;
2542
2543 /* Get the Part number */
2544 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
2545 if (i < 0) {
2546 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
2547 return;
2548 }
2549
2550 j = pci_vpd_info_field_size(&vpd_data[i]);
2551 i += PCI_VPD_INFO_FLD_HDR_SIZE;
2552 if (i + j > vpd_size) {
2553 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
2554 return;
2555 }
2556
2557 netif_info(efx, drv, efx->net_dev,
2558 "Part Number : %.*s\n", j, &vpd_data[i]);
2559}
2560
2561
2562/* Main body of NIC initialisation
2563 * This is called at module load (or hotplug insertion, theoretically).
2564 */
2565static int efx_pci_probe_main(struct efx_nic *efx)
2566{
2567 int rc;
2568
2569 /* Do start-of-day initialisation */
2570 rc = efx_probe_all(efx);
2571 if (rc)
2572 goto fail1;
2573
2574 efx_init_napi(efx);
2575
2576 rc = efx->type->init(efx);
2577 if (rc) {
2578 netif_err(efx, probe, efx->net_dev,
2579 "failed to initialise NIC\n");
2580 goto fail3;
2581 }
2582
2583 rc = efx_init_port(efx);
2584 if (rc) {
2585 netif_err(efx, probe, efx->net_dev,
2586 "failed to initialise port\n");
2587 goto fail4;
2588 }
2589
2590 rc = efx_nic_init_interrupt(efx);
2591 if (rc)
2592 goto fail5;
2593 efx_start_interrupts(efx, false);
2594
2595 return 0;
2596
2597 fail5:
2598 efx_fini_port(efx);
2599 fail4:
2600 efx->type->fini(efx);
2601 fail3:
2602 efx_fini_napi(efx);
2603 efx_remove_all(efx);
2604 fail1:
2605 return rc;
2606}
2607
2608/* NIC initialisation
2609 *
2610 * This is called at module load (or hotplug insertion,
2611 * theoretically). It sets up PCI mappings, resets the NIC,
2612 * sets up and registers the network devices with the kernel and hooks
2613 * the interrupt service routine. It does not prepare the device for
2614 * transmission; this is left to the first time one of the network
2615 * interfaces is brought up (i.e. efx_net_open).
2616 */
2617static int __devinit efx_pci_probe(struct pci_dev *pci_dev,
2618 const struct pci_device_id *entry)
2619{
2620 const struct efx_nic_type *type = (const struct efx_nic_type *) entry->driver_data;
2621 struct net_device *net_dev;
2622 struct efx_nic *efx;
2623 int rc;
2624
2625 /* Allocate and initialise a struct net_device and struct efx_nic */
2626 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
2627 EFX_MAX_RX_QUEUES);
2628 if (!net_dev)
2629 return -ENOMEM;
2630 net_dev->features |= (type->offload_features | NETIF_F_SG |
2631 NETIF_F_HIGHDMA | NETIF_F_TSO |
2632 NETIF_F_RXCSUM);
2633 if (type->offload_features & NETIF_F_V6_CSUM)
2634 net_dev->features |= NETIF_F_TSO6;
2635 /* Mask for features that also apply to VLAN devices */
2636 net_dev->vlan_features |= (NETIF_F_ALL_CSUM | NETIF_F_SG |
2637 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
2638 NETIF_F_RXCSUM);
2639 /* All offloads can be toggled */
2640 net_dev->hw_features = net_dev->features & ~NETIF_F_HIGHDMA;
2641 efx = netdev_priv(net_dev);
2642 pci_set_drvdata(pci_dev, efx);
2643 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
2644 rc = efx_init_struct(efx, type, pci_dev, net_dev);
2645 if (rc)
2646 goto fail1;
2647
2648 netif_info(efx, probe, efx->net_dev,
2649 "Solarflare NIC detected\n");
2650
2651 efx_print_product_vpd(efx);
2652
2653 /* Set up basic I/O (BAR mappings etc) */
2654 rc = efx_init_io(efx);
2655 if (rc)
2656 goto fail2;
2657
2658 rc = efx_pci_probe_main(efx);
2659
2660 /* Serialise against efx_reset(). No more resets will be
2661 * scheduled since efx_stop_all() has been called, and we have
2662 * not and never have been registered.
2663 */
2664 cancel_work_sync(&efx->reset_work);
2665
2666 if (rc)
2667 goto fail3;
2668
2669 /* If there was a scheduled reset during probe, the NIC is
2670 * probably hosed anyway.
2671 */
2672 if (efx->reset_pending) {
2673 rc = -EIO;
2674 goto fail4;
2675 }
2676
2677 /* Switch to the running state before we expose the device to the OS,
2678 * so that dev_open()|efx_start_all() will actually start the device */
2679 efx->state = STATE_RUNNING;
2680
2681 rc = efx_register_netdev(efx);
2682 if (rc)
2683 goto fail4;
2684
2685 rc = efx_sriov_init(efx);
2686 if (rc)
2687 netif_err(efx, probe, efx->net_dev,
2688 "SR-IOV can't be enabled rc %d\n", rc);
2689
2690 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
2691
2692 /* Try to create MTDs, but allow this to fail */
2693 rtnl_lock();
2694 rc = efx_mtd_probe(efx);
2695 rtnl_unlock();
2696 if (rc)
2697 netif_warn(efx, probe, efx->net_dev,
2698 "failed to create MTDs (%d)\n", rc);
2699
2700 return 0;
2701
2702 fail4:
2703 efx_pci_remove_main(efx);
2704 fail3:
2705 efx_fini_io(efx);
2706 fail2:
2707 efx_fini_struct(efx);
2708 fail1:
2709 pci_set_drvdata(pci_dev, NULL);
2710 WARN_ON(rc > 0);
2711 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
2712 free_netdev(net_dev);
2713 return rc;
2714}
2715
2716static int efx_pm_freeze(struct device *dev)
2717{
2718 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2719
2720 efx->state = STATE_FINI;
2721
2722 netif_device_detach(efx->net_dev);
2723
2724 efx_stop_all(efx);
2725 efx_stop_interrupts(efx, false);
2726
2727 return 0;
2728}
2729
2730static int efx_pm_thaw(struct device *dev)
2731{
2732 struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
2733
2734 efx->state = STATE_INIT;
2735
2736 efx_start_interrupts(efx, false);
2737
2738 mutex_lock(&efx->mac_lock);
2739 efx->phy_op->reconfigure(efx);
2740 mutex_unlock(&efx->mac_lock);
2741
2742 efx_start_all(efx);
2743
2744 netif_device_attach(efx->net_dev);
2745
2746 efx->state = STATE_RUNNING;
2747
2748 efx->type->resume_wol(efx);
2749
2750 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
2751 queue_work(reset_workqueue, &efx->reset_work);
2752
2753 return 0;
2754}
2755
2756static int efx_pm_poweroff(struct device *dev)
2757{
2758 struct pci_dev *pci_dev = to_pci_dev(dev);
2759 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2760
2761 efx->type->fini(efx);
2762
2763 efx->reset_pending = 0;
2764
2765 pci_save_state(pci_dev);
2766 return pci_set_power_state(pci_dev, PCI_D3hot);
2767}
2768
2769/* Used for both resume and restore */
2770static int efx_pm_resume(struct device *dev)
2771{
2772 struct pci_dev *pci_dev = to_pci_dev(dev);
2773 struct efx_nic *efx = pci_get_drvdata(pci_dev);
2774 int rc;
2775
2776 rc = pci_set_power_state(pci_dev, PCI_D0);
2777 if (rc)
2778 return rc;
2779 pci_restore_state(pci_dev);
2780 rc = pci_enable_device(pci_dev);
2781 if (rc)
2782 return rc;
2783 pci_set_master(efx->pci_dev);
2784 rc = efx->type->reset(efx, RESET_TYPE_ALL);
2785 if (rc)
2786 return rc;
2787 rc = efx->type->init(efx);
2788 if (rc)
2789 return rc;
2790 efx_pm_thaw(dev);
2791 return 0;
2792}
2793
2794static int efx_pm_suspend(struct device *dev)
2795{
2796 int rc;
2797
2798 efx_pm_freeze(dev);
2799 rc = efx_pm_poweroff(dev);
2800 if (rc)
2801 efx_pm_resume(dev);
2802 return rc;
2803}
2804
2805static const struct dev_pm_ops efx_pm_ops = {
2806 .suspend = efx_pm_suspend,
2807 .resume = efx_pm_resume,
2808 .freeze = efx_pm_freeze,
2809 .thaw = efx_pm_thaw,
2810 .poweroff = efx_pm_poweroff,
2811 .restore = efx_pm_resume,
2812};
2813
2814static struct pci_driver efx_pci_driver = {
2815 .name = KBUILD_MODNAME,
2816 .id_table = efx_pci_table,
2817 .probe = efx_pci_probe,
2818 .remove = efx_pci_remove,
2819 .driver.pm = &efx_pm_ops,
2820};
2821
2822/**************************************************************************
2823 *
2824 * Kernel module interface
2825 *
2826 *************************************************************************/
2827
2828module_param(interrupt_mode, uint, 0444);
2829MODULE_PARM_DESC(interrupt_mode,
2830 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
2831
2832static int __init efx_init_module(void)
2833{
2834 int rc;
2835
2836 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
2837
2838 rc = register_netdevice_notifier(&efx_netdev_notifier);
2839 if (rc)
2840 goto err_notifier;
2841
2842 rc = efx_init_sriov();
2843 if (rc)
2844 goto err_sriov;
2845
2846 reset_workqueue = create_singlethread_workqueue("sfc_reset");
2847 if (!reset_workqueue) {
2848 rc = -ENOMEM;
2849 goto err_reset;
2850 }
2851
2852 rc = pci_register_driver(&efx_pci_driver);
2853 if (rc < 0)
2854 goto err_pci;
2855
2856 return 0;
2857
2858 err_pci:
2859 destroy_workqueue(reset_workqueue);
2860 err_reset:
2861 efx_fini_sriov();
2862 err_sriov:
2863 unregister_netdevice_notifier(&efx_netdev_notifier);
2864 err_notifier:
2865 return rc;
2866}
2867
2868static void __exit efx_exit_module(void)
2869{
2870 printk(KERN_INFO "Solarflare NET driver unloading\n");
2871
2872 pci_unregister_driver(&efx_pci_driver);
2873 destroy_workqueue(reset_workqueue);
2874 efx_fini_sriov();
2875 unregister_netdevice_notifier(&efx_netdev_notifier);
2876
2877}
2878
2879module_init(efx_init_module);
2880module_exit(efx_exit_module);
2881
2882MODULE_AUTHOR("Solarflare Communications and "
2883 "Michael Brown <mbrown@fensystems.co.uk>");
2884MODULE_DESCRIPTION("Solarflare Communications network driver");
2885MODULE_LICENSE("GPL");
2886MODULE_DEVICE_TABLE(pci, efx_pci_table);