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 "efx.h"
26#include "efx_common.h"
27#include "efx_channels.h"
28#include "ef100.h"
29#include "rx_common.h"
30#include "tx_common.h"
31#include "nic.h"
32#include "io.h"
33#include "selftest.h"
34#include "sriov.h"
35#include "efx_devlink.h"
36
37#include "mcdi_port_common.h"
38#include "mcdi_pcol.h"
39#include "workarounds.h"
40
41/**************************************************************************
42 *
43 * Configurable values
44 *
45 *************************************************************************/
46
47module_param_named(interrupt_mode, efx_interrupt_mode, uint, 0444);
48MODULE_PARM_DESC(interrupt_mode,
49 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
50
51module_param(rss_cpus, uint, 0444);
52MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
53
54/*
55 * Use separate channels for TX and RX events
56 *
57 * Set this to 1 to use separate channels for TX and RX. It allows us
58 * to control interrupt affinity separately for TX and RX.
59 *
60 * This is only used in MSI-X interrupt mode
61 */
62bool efx_separate_tx_channels;
63module_param(efx_separate_tx_channels, bool, 0444);
64MODULE_PARM_DESC(efx_separate_tx_channels,
65 "Use separate channels for TX and RX");
66
67/* Initial interrupt moderation settings. They can be modified after
68 * module load with ethtool.
69 *
70 * The default for RX should strike a balance between increasing the
71 * round-trip latency and reducing overhead.
72 */
73static unsigned int rx_irq_mod_usec = 60;
74
75/* Initial interrupt moderation settings. They can be modified after
76 * module load with ethtool.
77 *
78 * This default is chosen to ensure that a 10G link does not go idle
79 * while a TX queue is stopped after it has become full. A queue is
80 * restarted when it drops below half full. The time this takes (assuming
81 * worst case 3 descriptors per packet and 1024 descriptors) is
82 * 512 / 3 * 1.2 = 205 usec.
83 */
84static unsigned int tx_irq_mod_usec = 150;
85
86static bool phy_flash_cfg;
87module_param(phy_flash_cfg, bool, 0644);
88MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
89
90static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
91 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
92 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
93 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
94module_param(debug, uint, 0);
95MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
96
97/**************************************************************************
98 *
99 * Utility functions and prototypes
100 *
101 *************************************************************************/
102
103static void efx_remove_port(struct efx_nic *efx);
104static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog);
105static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp);
106static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
107 u32 flags);
108
109/**************************************************************************
110 *
111 * Port handling
112 *
113 **************************************************************************/
114
115static void efx_fini_port(struct efx_nic *efx);
116
117static int efx_probe_port(struct efx_nic *efx)
118{
119 int rc;
120
121 netif_dbg(efx, probe, efx->net_dev, "create port\n");
122
123 if (phy_flash_cfg)
124 efx->phy_mode = PHY_MODE_SPECIAL;
125
126 /* Connect up MAC/PHY operations table */
127 rc = efx->type->probe_port(efx);
128 if (rc)
129 return rc;
130
131 /* Initialise MAC address to permanent address */
132 eth_hw_addr_set(efx->net_dev, efx->net_dev->perm_addr);
133
134 return 0;
135}
136
137static int efx_init_port(struct efx_nic *efx)
138{
139 int rc;
140
141 netif_dbg(efx, drv, efx->net_dev, "init port\n");
142
143 mutex_lock(&efx->mac_lock);
144
145 efx->port_initialized = true;
146
147 /* Ensure the PHY advertises the correct flow control settings */
148 rc = efx_mcdi_port_reconfigure(efx);
149 if (rc && rc != -EPERM)
150 goto fail;
151
152 mutex_unlock(&efx->mac_lock);
153 return 0;
154
155fail:
156 mutex_unlock(&efx->mac_lock);
157 return rc;
158}
159
160static void efx_fini_port(struct efx_nic *efx)
161{
162 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
163
164 if (!efx->port_initialized)
165 return;
166
167 efx->port_initialized = false;
168
169 efx->link_state.up = false;
170 efx_link_status_changed(efx);
171}
172
173static void efx_remove_port(struct efx_nic *efx)
174{
175 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
176
177 efx->type->remove_port(efx);
178}
179
180/**************************************************************************
181 *
182 * NIC handling
183 *
184 **************************************************************************/
185
186static LIST_HEAD(efx_primary_list);
187static LIST_HEAD(efx_unassociated_list);
188
189static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
190{
191 return left->type == right->type &&
192 left->vpd_sn && right->vpd_sn &&
193 !strcmp(left->vpd_sn, right->vpd_sn);
194}
195
196static void efx_associate(struct efx_nic *efx)
197{
198 struct efx_nic *other, *next;
199
200 if (efx->primary == efx) {
201 /* Adding primary function; look for secondaries */
202
203 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
204 list_add_tail(&efx->node, &efx_primary_list);
205
206 list_for_each_entry_safe(other, next, &efx_unassociated_list,
207 node) {
208 if (efx_same_controller(efx, other)) {
209 list_del(&other->node);
210 netif_dbg(other, probe, other->net_dev,
211 "moving to secondary list of %s %s\n",
212 pci_name(efx->pci_dev),
213 efx->net_dev->name);
214 list_add_tail(&other->node,
215 &efx->secondary_list);
216 other->primary = efx;
217 }
218 }
219 } else {
220 /* Adding secondary function; look for primary */
221
222 list_for_each_entry(other, &efx_primary_list, node) {
223 if (efx_same_controller(efx, other)) {
224 netif_dbg(efx, probe, efx->net_dev,
225 "adding to secondary list of %s %s\n",
226 pci_name(other->pci_dev),
227 other->net_dev->name);
228 list_add_tail(&efx->node,
229 &other->secondary_list);
230 efx->primary = other;
231 return;
232 }
233 }
234
235 netif_dbg(efx, probe, efx->net_dev,
236 "adding to unassociated list\n");
237 list_add_tail(&efx->node, &efx_unassociated_list);
238 }
239}
240
241static void efx_dissociate(struct efx_nic *efx)
242{
243 struct efx_nic *other, *next;
244
245 list_del(&efx->node);
246 efx->primary = NULL;
247
248 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
249 list_del(&other->node);
250 netif_dbg(other, probe, other->net_dev,
251 "moving to unassociated list\n");
252 list_add_tail(&other->node, &efx_unassociated_list);
253 other->primary = NULL;
254 }
255}
256
257static int efx_probe_nic(struct efx_nic *efx)
258{
259 int rc;
260
261 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
262
263 /* Carry out hardware-type specific initialisation */
264 rc = efx->type->probe(efx);
265 if (rc)
266 return rc;
267
268 do {
269 if (!efx->max_channels || !efx->max_tx_channels) {
270 netif_err(efx, drv, efx->net_dev,
271 "Insufficient resources to allocate"
272 " any channels\n");
273 rc = -ENOSPC;
274 goto fail1;
275 }
276
277 /* Determine the number of channels and queues by trying
278 * to hook in MSI-X interrupts.
279 */
280 rc = efx_probe_interrupts(efx);
281 if (rc)
282 goto fail1;
283
284 rc = efx_set_channels(efx);
285 if (rc)
286 goto fail1;
287
288 /* dimension_resources can fail with EAGAIN */
289 rc = efx->type->dimension_resources(efx);
290 if (rc != 0 && rc != -EAGAIN)
291 goto fail2;
292
293 if (rc == -EAGAIN)
294 /* try again with new max_channels */
295 efx_remove_interrupts(efx);
296
297 } while (rc == -EAGAIN);
298
299 if (efx->n_channels > 1)
300 netdev_rss_key_fill(efx->rss_context.rx_hash_key,
301 sizeof(efx->rss_context.rx_hash_key));
302 efx_set_default_rx_indir_table(efx, &efx->rss_context);
303
304 /* Initialise the interrupt moderation settings */
305 efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
306 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
307 true);
308
309 return 0;
310
311fail2:
312 efx_remove_interrupts(efx);
313fail1:
314 efx->type->remove(efx);
315 return rc;
316}
317
318static void efx_remove_nic(struct efx_nic *efx)
319{
320 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
321
322 efx_remove_interrupts(efx);
323 efx->type->remove(efx);
324}
325
326/**************************************************************************
327 *
328 * NIC startup/shutdown
329 *
330 *************************************************************************/
331
332static int efx_probe_all(struct efx_nic *efx)
333{
334 int rc;
335
336 rc = efx_probe_nic(efx);
337 if (rc) {
338 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
339 goto fail1;
340 }
341
342 rc = efx_probe_port(efx);
343 if (rc) {
344 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
345 goto fail2;
346 }
347
348 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
349 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
350 rc = -EINVAL;
351 goto fail3;
352 }
353
354#ifdef CONFIG_SFC_SRIOV
355 rc = efx->type->vswitching_probe(efx);
356 if (rc) /* not fatal; the PF will still work fine */
357 netif_warn(efx, probe, efx->net_dev,
358 "failed to setup vswitching rc=%d;"
359 " VFs may not function\n", rc);
360#endif
361
362 rc = efx_probe_filters(efx);
363 if (rc) {
364 netif_err(efx, probe, efx->net_dev,
365 "failed to create filter tables\n");
366 goto fail4;
367 }
368
369 rc = efx_probe_channels(efx);
370 if (rc)
371 goto fail5;
372
373 efx->state = STATE_NET_DOWN;
374
375 return 0;
376
377 fail5:
378 efx_remove_filters(efx);
379 fail4:
380#ifdef CONFIG_SFC_SRIOV
381 efx->type->vswitching_remove(efx);
382#endif
383 fail3:
384 efx_remove_port(efx);
385 fail2:
386 efx_remove_nic(efx);
387 fail1:
388 return rc;
389}
390
391static void efx_remove_all(struct efx_nic *efx)
392{
393 rtnl_lock();
394 efx_xdp_setup_prog(efx, NULL);
395 rtnl_unlock();
396
397 efx_remove_channels(efx);
398 efx_remove_filters(efx);
399#ifdef CONFIG_SFC_SRIOV
400 efx->type->vswitching_remove(efx);
401#endif
402 efx_remove_port(efx);
403 efx_remove_nic(efx);
404}
405
406/**************************************************************************
407 *
408 * Interrupt moderation
409 *
410 **************************************************************************/
411unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
412{
413 if (usecs == 0)
414 return 0;
415 if (usecs * 1000 < efx->timer_quantum_ns)
416 return 1; /* never round down to 0 */
417 return usecs * 1000 / efx->timer_quantum_ns;
418}
419
420unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
421{
422 /* We must round up when converting ticks to microseconds
423 * because we round down when converting the other way.
424 */
425 return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
426}
427
428/* Set interrupt moderation parameters */
429int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
430 unsigned int rx_usecs, bool rx_adaptive,
431 bool rx_may_override_tx)
432{
433 struct efx_channel *channel;
434 unsigned int timer_max_us;
435
436 EFX_ASSERT_RESET_SERIALISED(efx);
437
438 timer_max_us = efx->timer_max_ns / 1000;
439
440 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
441 return -EINVAL;
442
443 if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
444 !rx_may_override_tx) {
445 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
446 "RX and TX IRQ moderation must be equal\n");
447 return -EINVAL;
448 }
449
450 efx->irq_rx_adaptive = rx_adaptive;
451 efx->irq_rx_moderation_us = rx_usecs;
452 efx_for_each_channel(channel, efx) {
453 if (efx_channel_has_rx_queue(channel))
454 channel->irq_moderation_us = rx_usecs;
455 else if (efx_channel_has_tx_queues(channel))
456 channel->irq_moderation_us = tx_usecs;
457 else if (efx_channel_is_xdp_tx(channel))
458 channel->irq_moderation_us = tx_usecs;
459 }
460
461 return 0;
462}
463
464void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
465 unsigned int *rx_usecs, bool *rx_adaptive)
466{
467 *rx_adaptive = efx->irq_rx_adaptive;
468 *rx_usecs = efx->irq_rx_moderation_us;
469
470 /* If channels are shared between RX and TX, so is IRQ
471 * moderation. Otherwise, IRQ moderation is the same for all
472 * TX channels and is not adaptive.
473 */
474 if (efx->tx_channel_offset == 0) {
475 *tx_usecs = *rx_usecs;
476 } else {
477 struct efx_channel *tx_channel;
478
479 tx_channel = efx->channel[efx->tx_channel_offset];
480 *tx_usecs = tx_channel->irq_moderation_us;
481 }
482}
483
484/**************************************************************************
485 *
486 * ioctls
487 *
488 *************************************************************************/
489
490/* Net device ioctl
491 * Context: process, rtnl_lock() held.
492 */
493static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
494{
495 struct efx_nic *efx = efx_netdev_priv(net_dev);
496 struct mii_ioctl_data *data = if_mii(ifr);
497
498 /* Convert phy_id from older PRTAD/DEVAD format */
499 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
500 (data->phy_id & 0xfc00) == 0x0400)
501 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
502
503 return mdio_mii_ioctl(&efx->mdio, data, cmd);
504}
505
506/**************************************************************************
507 *
508 * Kernel net device interface
509 *
510 *************************************************************************/
511
512/* Context: process, rtnl_lock() held. */
513int efx_net_open(struct net_device *net_dev)
514{
515 struct efx_nic *efx = efx_netdev_priv(net_dev);
516 int rc;
517
518 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
519 raw_smp_processor_id());
520
521 rc = efx_check_disabled(efx);
522 if (rc)
523 return rc;
524 if (efx->phy_mode & PHY_MODE_SPECIAL)
525 return -EBUSY;
526 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
527 return -EIO;
528
529 /* Notify the kernel of the link state polled during driver load,
530 * before the monitor starts running */
531 efx_link_status_changed(efx);
532
533 efx_start_all(efx);
534 if (efx->state == STATE_DISABLED || efx->reset_pending)
535 netif_device_detach(efx->net_dev);
536 else
537 efx->state = STATE_NET_UP;
538
539 return 0;
540}
541
542/* Context: process, rtnl_lock() held.
543 * Note that the kernel will ignore our return code; this method
544 * should really be a void.
545 */
546int efx_net_stop(struct net_device *net_dev)
547{
548 struct efx_nic *efx = efx_netdev_priv(net_dev);
549
550 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
551 raw_smp_processor_id());
552
553 /* Stop the device and flush all the channels */
554 efx_stop_all(efx);
555
556 return 0;
557}
558
559static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
560{
561 struct efx_nic *efx = efx_netdev_priv(net_dev);
562
563 if (efx->type->vlan_rx_add_vid)
564 return efx->type->vlan_rx_add_vid(efx, proto, vid);
565 else
566 return -EOPNOTSUPP;
567}
568
569static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
570{
571 struct efx_nic *efx = efx_netdev_priv(net_dev);
572
573 if (efx->type->vlan_rx_kill_vid)
574 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
575 else
576 return -EOPNOTSUPP;
577}
578
579static int efx_hwtstamp_set(struct net_device *net_dev,
580 struct kernel_hwtstamp_config *config,
581 struct netlink_ext_ack *extack)
582{
583 struct efx_nic *efx = efx_netdev_priv(net_dev);
584
585 return efx_ptp_set_ts_config(efx, config, extack);
586}
587
588static int efx_hwtstamp_get(struct net_device *net_dev,
589 struct kernel_hwtstamp_config *config)
590{
591 struct efx_nic *efx = efx_netdev_priv(net_dev);
592
593 return efx_ptp_get_ts_config(efx, config);
594}
595
596static const struct net_device_ops efx_netdev_ops = {
597 .ndo_open = efx_net_open,
598 .ndo_stop = efx_net_stop,
599 .ndo_get_stats64 = efx_net_stats,
600 .ndo_tx_timeout = efx_watchdog,
601 .ndo_start_xmit = efx_hard_start_xmit,
602 .ndo_validate_addr = eth_validate_addr,
603 .ndo_eth_ioctl = efx_ioctl,
604 .ndo_change_mtu = efx_change_mtu,
605 .ndo_set_mac_address = efx_set_mac_address,
606 .ndo_set_rx_mode = efx_set_rx_mode,
607 .ndo_set_features = efx_set_features,
608 .ndo_features_check = efx_features_check,
609 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
610 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
611 .ndo_hwtstamp_set = efx_hwtstamp_set,
612 .ndo_hwtstamp_get = efx_hwtstamp_get,
613#ifdef CONFIG_SFC_SRIOV
614 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
615 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
616 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
617 .ndo_get_vf_config = efx_sriov_get_vf_config,
618 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
619#endif
620 .ndo_get_phys_port_id = efx_get_phys_port_id,
621 .ndo_get_phys_port_name = efx_get_phys_port_name,
622#ifdef CONFIG_RFS_ACCEL
623 .ndo_rx_flow_steer = efx_filter_rfs,
624#endif
625 .ndo_xdp_xmit = efx_xdp_xmit,
626 .ndo_bpf = efx_xdp
627};
628
629static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
630{
631 struct bpf_prog *old_prog;
632
633 if (efx->xdp_rxq_info_failed) {
634 netif_err(efx, drv, efx->net_dev,
635 "Unable to bind XDP program due to previous failure of rxq_info\n");
636 return -EINVAL;
637 }
638
639 if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) {
640 netif_err(efx, drv, efx->net_dev,
641 "Unable to configure XDP with MTU of %d (max: %d)\n",
642 efx->net_dev->mtu, efx_xdp_max_mtu(efx));
643 return -EINVAL;
644 }
645
646 old_prog = rtnl_dereference(efx->xdp_prog);
647 rcu_assign_pointer(efx->xdp_prog, prog);
648 /* Release the reference that was originally passed by the caller. */
649 if (old_prog)
650 bpf_prog_put(old_prog);
651
652 return 0;
653}
654
655/* Context: process, rtnl_lock() held. */
656static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
657{
658 struct efx_nic *efx = efx_netdev_priv(dev);
659
660 switch (xdp->command) {
661 case XDP_SETUP_PROG:
662 return efx_xdp_setup_prog(efx, xdp->prog);
663 default:
664 return -EINVAL;
665 }
666}
667
668static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
669 u32 flags)
670{
671 struct efx_nic *efx = efx_netdev_priv(dev);
672
673 if (!netif_running(dev))
674 return -EINVAL;
675
676 return efx_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH);
677}
678
679static void efx_update_name(struct efx_nic *efx)
680{
681 strcpy(efx->name, efx->net_dev->name);
682 efx_mtd_rename(efx);
683 efx_set_channel_names(efx);
684}
685
686static int efx_netdev_event(struct notifier_block *this,
687 unsigned long event, void *ptr)
688{
689 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
690
691 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
692 event == NETDEV_CHANGENAME)
693 efx_update_name(efx_netdev_priv(net_dev));
694
695 return NOTIFY_DONE;
696}
697
698static struct notifier_block efx_netdev_notifier = {
699 .notifier_call = efx_netdev_event,
700};
701
702static ssize_t phy_type_show(struct device *dev,
703 struct device_attribute *attr, char *buf)
704{
705 struct efx_nic *efx = dev_get_drvdata(dev);
706 return sprintf(buf, "%d\n", efx->phy_type);
707}
708static DEVICE_ATTR_RO(phy_type);
709
710static int efx_register_netdev(struct efx_nic *efx)
711{
712 struct net_device *net_dev = efx->net_dev;
713 struct efx_channel *channel;
714 int rc;
715
716 net_dev->watchdog_timeo = 5 * HZ;
717 net_dev->irq = efx->pci_dev->irq;
718 net_dev->netdev_ops = &efx_netdev_ops;
719 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
720 net_dev->priv_flags |= IFF_UNICAST_FLT;
721 net_dev->ethtool_ops = &efx_ethtool_ops;
722 netif_set_tso_max_segs(net_dev, EFX_TSO_MAX_SEGS);
723 net_dev->min_mtu = EFX_MIN_MTU;
724 net_dev->max_mtu = EFX_MAX_MTU;
725
726 rtnl_lock();
727
728 /* Enable resets to be scheduled and check whether any were
729 * already requested. If so, the NIC is probably hosed so we
730 * abort.
731 */
732 if (efx->reset_pending) {
733 pci_err(efx->pci_dev, "aborting probe due to scheduled reset\n");
734 rc = -EIO;
735 goto fail_locked;
736 }
737
738 rc = dev_alloc_name(net_dev, net_dev->name);
739 if (rc < 0)
740 goto fail_locked;
741 efx_update_name(efx);
742
743 /* Always start with carrier off; PHY events will detect the link */
744 netif_carrier_off(net_dev);
745
746 rc = register_netdevice(net_dev);
747 if (rc)
748 goto fail_locked;
749
750 efx_for_each_channel(channel, efx) {
751 struct efx_tx_queue *tx_queue;
752 efx_for_each_channel_tx_queue(tx_queue, channel)
753 efx_init_tx_queue_core_txq(tx_queue);
754 }
755
756 efx_associate(efx);
757
758 efx->state = STATE_NET_DOWN;
759
760 rtnl_unlock();
761
762 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
763 if (rc) {
764 netif_err(efx, drv, efx->net_dev,
765 "failed to init net dev attributes\n");
766 goto fail_registered;
767 }
768
769 efx_init_mcdi_logging(efx);
770
771 return 0;
772
773fail_registered:
774 rtnl_lock();
775 efx_dissociate(efx);
776 unregister_netdevice(net_dev);
777fail_locked:
778 efx->state = STATE_UNINIT;
779 rtnl_unlock();
780 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
781 return rc;
782}
783
784static void efx_unregister_netdev(struct efx_nic *efx)
785{
786 if (!efx->net_dev)
787 return;
788
789 if (WARN_ON(efx_netdev_priv(efx->net_dev) != efx))
790 return;
791
792 if (efx_dev_registered(efx)) {
793 strscpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
794 efx_fini_mcdi_logging(efx);
795 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
796 unregister_netdev(efx->net_dev);
797 }
798}
799
800/**************************************************************************
801 *
802 * List of NICs we support
803 *
804 **************************************************************************/
805
806/* PCI device ID table */
807static const struct pci_device_id efx_pci_table[] = {
808 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
809 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
810 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
811 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
812 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
813 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
814 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
815 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
816 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
817 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
818 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
819 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
820 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
821 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
822 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
823 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
824 {0} /* end of list */
825};
826
827/**************************************************************************
828 *
829 * Data housekeeping
830 *
831 **************************************************************************/
832
833void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
834{
835 u64 n_rx_nodesc_trunc = 0;
836 struct efx_channel *channel;
837
838 efx_for_each_channel(channel, efx)
839 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
840 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
841 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
842}
843
844/**************************************************************************
845 *
846 * PCI interface
847 *
848 **************************************************************************/
849
850/* Main body of final NIC shutdown code
851 * This is called only at module unload (or hotplug removal).
852 */
853static void efx_pci_remove_main(struct efx_nic *efx)
854{
855 /* Flush reset_work. It can no longer be scheduled since we
856 * are not READY.
857 */
858 WARN_ON(efx_net_active(efx->state));
859 efx_flush_reset_workqueue(efx);
860
861 efx_disable_interrupts(efx);
862 efx_clear_interrupt_affinity(efx);
863 efx_nic_fini_interrupt(efx);
864 efx_fini_port(efx);
865 efx->type->fini(efx);
866 efx_fini_napi(efx);
867 efx_remove_all(efx);
868}
869
870/* Final NIC shutdown
871 * This is called only at module unload (or hotplug removal). A PF can call
872 * this on its VFs to ensure they are unbound first.
873 */
874static void efx_pci_remove(struct pci_dev *pci_dev)
875{
876 struct efx_probe_data *probe_data;
877 struct efx_nic *efx;
878
879 efx = pci_get_drvdata(pci_dev);
880 if (!efx)
881 return;
882
883 /* Mark the NIC as fini, then stop the interface */
884 rtnl_lock();
885 efx_dissociate(efx);
886 dev_close(efx->net_dev);
887 efx_disable_interrupts(efx);
888 efx->state = STATE_UNINIT;
889 rtnl_unlock();
890
891 if (efx->type->sriov_fini)
892 efx->type->sriov_fini(efx);
893
894 efx_fini_devlink_lock(efx);
895 efx_unregister_netdev(efx);
896
897 efx_mtd_remove(efx);
898
899 efx_pci_remove_main(efx);
900
901 efx_fini_io(efx);
902 pci_dbg(efx->pci_dev, "shutdown successful\n");
903
904 efx_fini_devlink_and_unlock(efx);
905 efx_fini_struct(efx);
906 free_netdev(efx->net_dev);
907 probe_data = container_of(efx, struct efx_probe_data, efx);
908 kfree(probe_data);
909};
910
911/* NIC VPD information
912 * Called during probe to display the part number of the
913 * installed NIC.
914 */
915static void efx_probe_vpd_strings(struct efx_nic *efx)
916{
917 struct pci_dev *dev = efx->pci_dev;
918 unsigned int vpd_size, kw_len;
919 u8 *vpd_data;
920 int start;
921
922 vpd_data = pci_vpd_alloc(dev, &vpd_size);
923 if (IS_ERR(vpd_data)) {
924 pci_warn(dev, "Unable to read VPD\n");
925 return;
926 }
927
928 start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
929 PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
930 if (start < 0)
931 pci_err(dev, "Part number not found or incomplete\n");
932 else
933 pci_info(dev, "Part Number : %.*s\n", kw_len, vpd_data + start);
934
935 start = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
936 PCI_VPD_RO_KEYWORD_SERIALNO, &kw_len);
937 if (start < 0)
938 pci_err(dev, "Serial number not found or incomplete\n");
939 else
940 efx->vpd_sn = kmemdup_nul(vpd_data + start, kw_len, GFP_KERNEL);
941
942 kfree(vpd_data);
943}
944
945
946/* Main body of NIC initialisation
947 * This is called at module load (or hotplug insertion, theoretically).
948 */
949static int efx_pci_probe_main(struct efx_nic *efx)
950{
951 int rc;
952
953 /* Do start-of-day initialisation */
954 rc = efx_probe_all(efx);
955 if (rc)
956 goto fail1;
957
958 efx_init_napi(efx);
959
960 down_write(&efx->filter_sem);
961 rc = efx->type->init(efx);
962 up_write(&efx->filter_sem);
963 if (rc) {
964 pci_err(efx->pci_dev, "failed to initialise NIC\n");
965 goto fail3;
966 }
967
968 rc = efx_init_port(efx);
969 if (rc) {
970 netif_err(efx, probe, efx->net_dev,
971 "failed to initialise port\n");
972 goto fail4;
973 }
974
975 rc = efx_nic_init_interrupt(efx);
976 if (rc)
977 goto fail5;
978
979 efx_set_interrupt_affinity(efx);
980 rc = efx_enable_interrupts(efx);
981 if (rc)
982 goto fail6;
983
984 return 0;
985
986 fail6:
987 efx_clear_interrupt_affinity(efx);
988 efx_nic_fini_interrupt(efx);
989 fail5:
990 efx_fini_port(efx);
991 fail4:
992 efx->type->fini(efx);
993 fail3:
994 efx_fini_napi(efx);
995 efx_remove_all(efx);
996 fail1:
997 return rc;
998}
999
1000static int efx_pci_probe_post_io(struct efx_nic *efx)
1001{
1002 struct net_device *net_dev = efx->net_dev;
1003 int rc = efx_pci_probe_main(efx);
1004
1005 if (rc)
1006 return rc;
1007
1008 if (efx->type->sriov_init) {
1009 rc = efx->type->sriov_init(efx);
1010 if (rc)
1011 pci_err(efx->pci_dev, "SR-IOV can't be enabled rc %d\n",
1012 rc);
1013 }
1014
1015 /* Determine netdevice features */
1016 net_dev->features |= efx->type->offload_features;
1017
1018 /* Add TSO features */
1019 if (efx->type->tso_versions && efx->type->tso_versions(efx))
1020 net_dev->features |= NETIF_F_TSO | NETIF_F_TSO6;
1021
1022 /* Mask for features that also apply to VLAN devices */
1023 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
1024 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
1025 NETIF_F_RXCSUM);
1026
1027 /* Determine user configurable features */
1028 net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
1029
1030 /* Disable receiving frames with bad FCS, by default. */
1031 net_dev->features &= ~NETIF_F_RXALL;
1032
1033 /* Disable VLAN filtering by default. It may be enforced if
1034 * the feature is fixed (i.e. VLAN filters are required to
1035 * receive VLAN tagged packets due to vPort restrictions).
1036 */
1037 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1038 net_dev->features |= efx->fixed_features;
1039
1040 net_dev->xdp_features = NETDEV_XDP_ACT_BASIC |
1041 NETDEV_XDP_ACT_REDIRECT |
1042 NETDEV_XDP_ACT_NDO_XMIT;
1043
1044 /* devlink creation, registration and lock */
1045 rc = efx_probe_devlink_and_lock(efx);
1046 if (rc)
1047 pci_err(efx->pci_dev, "devlink registration failed");
1048
1049 rc = efx_register_netdev(efx);
1050 efx_probe_devlink_unlock(efx);
1051 if (!rc)
1052 return 0;
1053
1054 efx_pci_remove_main(efx);
1055 return rc;
1056}
1057
1058/* NIC initialisation
1059 *
1060 * This is called at module load (or hotplug insertion,
1061 * theoretically). It sets up PCI mappings, resets the NIC,
1062 * sets up and registers the network devices with the kernel and hooks
1063 * the interrupt service routine. It does not prepare the device for
1064 * transmission; this is left to the first time one of the network
1065 * interfaces is brought up (i.e. efx_net_open).
1066 */
1067static int efx_pci_probe(struct pci_dev *pci_dev,
1068 const struct pci_device_id *entry)
1069{
1070 struct efx_probe_data *probe_data, **probe_ptr;
1071 struct net_device *net_dev;
1072 struct efx_nic *efx;
1073 int rc;
1074
1075 /* Allocate probe data and struct efx_nic */
1076 probe_data = kzalloc(sizeof(*probe_data), GFP_KERNEL);
1077 if (!probe_data)
1078 return -ENOMEM;
1079 probe_data->pci_dev = pci_dev;
1080 efx = &probe_data->efx;
1081
1082 /* Allocate and initialise a struct net_device */
1083 net_dev = alloc_etherdev_mq(sizeof(probe_data), EFX_MAX_CORE_TX_QUEUES);
1084 if (!net_dev) {
1085 rc = -ENOMEM;
1086 goto fail0;
1087 }
1088 probe_ptr = netdev_priv(net_dev);
1089 *probe_ptr = probe_data;
1090 efx->net_dev = net_dev;
1091 efx->type = (const struct efx_nic_type *) entry->driver_data;
1092 efx->fixed_features |= NETIF_F_HIGHDMA;
1093
1094 pci_set_drvdata(pci_dev, efx);
1095 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
1096 rc = efx_init_struct(efx, pci_dev);
1097 if (rc)
1098 goto fail1;
1099 efx->mdio.dev = net_dev;
1100
1101 pci_info(pci_dev, "Solarflare NIC detected\n");
1102
1103 if (!efx->type->is_vf)
1104 efx_probe_vpd_strings(efx);
1105
1106 /* Set up basic I/O (BAR mappings etc) */
1107 rc = efx_init_io(efx, efx->type->mem_bar(efx), efx->type->max_dma_mask,
1108 efx->type->mem_map_size(efx));
1109 if (rc)
1110 goto fail2;
1111
1112 rc = efx_pci_probe_post_io(efx);
1113 if (rc) {
1114 /* On failure, retry once immediately.
1115 * If we aborted probe due to a scheduled reset, dismiss it.
1116 */
1117 efx->reset_pending = 0;
1118 rc = efx_pci_probe_post_io(efx);
1119 if (rc) {
1120 /* On another failure, retry once more
1121 * after a 50-305ms delay.
1122 */
1123 unsigned char r;
1124
1125 get_random_bytes(&r, 1);
1126 msleep((unsigned int)r + 50);
1127 efx->reset_pending = 0;
1128 rc = efx_pci_probe_post_io(efx);
1129 }
1130 }
1131 if (rc)
1132 goto fail3;
1133
1134 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
1135
1136 /* Try to create MTDs, but allow this to fail */
1137 rtnl_lock();
1138 rc = efx_mtd_probe(efx);
1139 rtnl_unlock();
1140 if (rc && rc != -EPERM)
1141 netif_warn(efx, probe, efx->net_dev,
1142 "failed to create MTDs (%d)\n", rc);
1143
1144 if (efx->type->udp_tnl_push_ports)
1145 efx->type->udp_tnl_push_ports(efx);
1146
1147 return 0;
1148
1149 fail3:
1150 efx_fini_io(efx);
1151 fail2:
1152 efx_fini_struct(efx);
1153 fail1:
1154 WARN_ON(rc > 0);
1155 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
1156 free_netdev(net_dev);
1157 fail0:
1158 kfree(probe_data);
1159 return rc;
1160}
1161
1162/* efx_pci_sriov_configure returns the actual number of Virtual Functions
1163 * enabled on success
1164 */
1165#ifdef CONFIG_SFC_SRIOV
1166static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1167{
1168 int rc;
1169 struct efx_nic *efx = pci_get_drvdata(dev);
1170
1171 if (efx->type->sriov_configure) {
1172 rc = efx->type->sriov_configure(efx, num_vfs);
1173 if (rc)
1174 return rc;
1175 else
1176 return num_vfs;
1177 } else
1178 return -EOPNOTSUPP;
1179}
1180#endif
1181
1182static int efx_pm_freeze(struct device *dev)
1183{
1184 struct efx_nic *efx = dev_get_drvdata(dev);
1185
1186 rtnl_lock();
1187
1188 if (efx_net_active(efx->state)) {
1189 efx_device_detach_sync(efx);
1190
1191 efx_stop_all(efx);
1192 efx_disable_interrupts(efx);
1193
1194 efx->state = efx_freeze(efx->state);
1195 }
1196
1197 rtnl_unlock();
1198
1199 return 0;
1200}
1201
1202static void efx_pci_shutdown(struct pci_dev *pci_dev)
1203{
1204 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1205
1206 if (!efx)
1207 return;
1208
1209 efx_pm_freeze(&pci_dev->dev);
1210 pci_disable_device(pci_dev);
1211}
1212
1213static int efx_pm_thaw(struct device *dev)
1214{
1215 int rc;
1216 struct efx_nic *efx = dev_get_drvdata(dev);
1217
1218 rtnl_lock();
1219
1220 if (efx_frozen(efx->state)) {
1221 rc = efx_enable_interrupts(efx);
1222 if (rc)
1223 goto fail;
1224
1225 mutex_lock(&efx->mac_lock);
1226 efx_mcdi_port_reconfigure(efx);
1227 mutex_unlock(&efx->mac_lock);
1228
1229 efx_start_all(efx);
1230
1231 efx_device_attach_if_not_resetting(efx);
1232
1233 efx->state = efx_thaw(efx->state);
1234
1235 efx->type->resume_wol(efx);
1236 }
1237
1238 rtnl_unlock();
1239
1240 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
1241 efx_queue_reset_work(efx);
1242
1243 return 0;
1244
1245fail:
1246 rtnl_unlock();
1247
1248 return rc;
1249}
1250
1251static int efx_pm_poweroff(struct device *dev)
1252{
1253 struct pci_dev *pci_dev = to_pci_dev(dev);
1254 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1255
1256 efx->type->fini(efx);
1257
1258 efx->reset_pending = 0;
1259
1260 pci_save_state(pci_dev);
1261 return pci_set_power_state(pci_dev, PCI_D3hot);
1262}
1263
1264/* Used for both resume and restore */
1265static int efx_pm_resume(struct device *dev)
1266{
1267 struct pci_dev *pci_dev = to_pci_dev(dev);
1268 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1269 int rc;
1270
1271 rc = pci_set_power_state(pci_dev, PCI_D0);
1272 if (rc)
1273 return rc;
1274 pci_restore_state(pci_dev);
1275 rc = pci_enable_device(pci_dev);
1276 if (rc)
1277 return rc;
1278 pci_set_master(efx->pci_dev);
1279 rc = efx->type->reset(efx, RESET_TYPE_ALL);
1280 if (rc)
1281 return rc;
1282 down_write(&efx->filter_sem);
1283 rc = efx->type->init(efx);
1284 up_write(&efx->filter_sem);
1285 if (rc)
1286 return rc;
1287 rc = efx_pm_thaw(dev);
1288 return rc;
1289}
1290
1291static int efx_pm_suspend(struct device *dev)
1292{
1293 int rc;
1294
1295 efx_pm_freeze(dev);
1296 rc = efx_pm_poweroff(dev);
1297 if (rc)
1298 efx_pm_resume(dev);
1299 return rc;
1300}
1301
1302static const struct dev_pm_ops efx_pm_ops = {
1303 .suspend = efx_pm_suspend,
1304 .resume = efx_pm_resume,
1305 .freeze = efx_pm_freeze,
1306 .thaw = efx_pm_thaw,
1307 .poweroff = efx_pm_poweroff,
1308 .restore = efx_pm_resume,
1309};
1310
1311static struct pci_driver efx_pci_driver = {
1312 .name = KBUILD_MODNAME,
1313 .id_table = efx_pci_table,
1314 .probe = efx_pci_probe,
1315 .remove = efx_pci_remove,
1316 .driver.pm = &efx_pm_ops,
1317 .shutdown = efx_pci_shutdown,
1318 .err_handler = &efx_err_handlers,
1319#ifdef CONFIG_SFC_SRIOV
1320 .sriov_configure = efx_pci_sriov_configure,
1321#endif
1322};
1323
1324/**************************************************************************
1325 *
1326 * Kernel module interface
1327 *
1328 *************************************************************************/
1329
1330static int __init efx_init_module(void)
1331{
1332 int rc;
1333
1334 printk(KERN_INFO "Solarflare NET driver\n");
1335
1336 rc = register_netdevice_notifier(&efx_netdev_notifier);
1337 if (rc)
1338 goto err_notifier;
1339
1340 rc = efx_create_reset_workqueue();
1341 if (rc)
1342 goto err_reset;
1343
1344 rc = pci_register_driver(&efx_pci_driver);
1345 if (rc < 0)
1346 goto err_pci;
1347
1348 rc = pci_register_driver(&ef100_pci_driver);
1349 if (rc < 0)
1350 goto err_pci_ef100;
1351
1352 return 0;
1353
1354 err_pci_ef100:
1355 pci_unregister_driver(&efx_pci_driver);
1356 err_pci:
1357 efx_destroy_reset_workqueue();
1358 err_reset:
1359 unregister_netdevice_notifier(&efx_netdev_notifier);
1360 err_notifier:
1361 return rc;
1362}
1363
1364static void __exit efx_exit_module(void)
1365{
1366 printk(KERN_INFO "Solarflare NET driver unloading\n");
1367
1368 pci_unregister_driver(&ef100_pci_driver);
1369 pci_unregister_driver(&efx_pci_driver);
1370 efx_destroy_reset_workqueue();
1371 unregister_netdevice_notifier(&efx_netdev_notifier);
1372
1373}
1374
1375module_init(efx_init_module);
1376module_exit(efx_exit_module);
1377
1378MODULE_AUTHOR("Solarflare Communications and "
1379 "Michael Brown <mbrown@fensystems.co.uk>");
1380MODULE_DESCRIPTION("Solarflare network driver");
1381MODULE_LICENSE("GPL");
1382MODULE_DEVICE_TABLE(pci, efx_pci_table);
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/module.h>
9#include <linux/pci.h>
10#include <linux/netdevice.h>
11#include <linux/etherdevice.h>
12#include <linux/delay.h>
13#include <linux/notifier.h>
14#include <linux/ip.h>
15#include <linux/tcp.h>
16#include <linux/in.h>
17#include <linux/ethtool.h>
18#include <linux/topology.h>
19#include <linux/gfp.h>
20#include <linux/aer.h>
21#include <linux/interrupt.h>
22#include "net_driver.h"
23#include <net/gre.h>
24#include <net/udp_tunnel.h>
25#include "efx.h"
26#include "efx_common.h"
27#include "efx_channels.h"
28#include "ef100.h"
29#include "rx_common.h"
30#include "tx_common.h"
31#include "nic.h"
32#include "io.h"
33#include "selftest.h"
34#include "sriov.h"
35
36#include "mcdi.h"
37#include "mcdi_pcol.h"
38#include "workarounds.h"
39
40/**************************************************************************
41 *
42 * Configurable values
43 *
44 *************************************************************************/
45
46module_param_named(interrupt_mode, efx_interrupt_mode, uint, 0444);
47MODULE_PARM_DESC(interrupt_mode,
48 "Interrupt mode (0=>MSIX 1=>MSI 2=>legacy)");
49
50module_param(rss_cpus, uint, 0444);
51MODULE_PARM_DESC(rss_cpus, "Number of CPUs to use for Receive-Side Scaling");
52
53/*
54 * Use separate channels for TX and RX events
55 *
56 * Set this to 1 to use separate channels for TX and RX. It allows us
57 * to control interrupt affinity separately for TX and RX.
58 *
59 * This is only used in MSI-X interrupt mode
60 */
61bool efx_separate_tx_channels;
62module_param(efx_separate_tx_channels, bool, 0444);
63MODULE_PARM_DESC(efx_separate_tx_channels,
64 "Use separate channels for TX and RX");
65
66/* Initial interrupt moderation settings. They can be modified after
67 * module load with ethtool.
68 *
69 * The default for RX should strike a balance between increasing the
70 * round-trip latency and reducing overhead.
71 */
72static unsigned int rx_irq_mod_usec = 60;
73
74/* Initial interrupt moderation settings. They can be modified after
75 * module load with ethtool.
76 *
77 * This default is chosen to ensure that a 10G link does not go idle
78 * while a TX queue is stopped after it has become full. A queue is
79 * restarted when it drops below half full. The time this takes (assuming
80 * worst case 3 descriptors per packet and 1024 descriptors) is
81 * 512 / 3 * 1.2 = 205 usec.
82 */
83static unsigned int tx_irq_mod_usec = 150;
84
85static bool phy_flash_cfg;
86module_param(phy_flash_cfg, bool, 0644);
87MODULE_PARM_DESC(phy_flash_cfg, "Set PHYs into reflash mode initially");
88
89static unsigned debug = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
90 NETIF_MSG_LINK | NETIF_MSG_IFDOWN |
91 NETIF_MSG_IFUP | NETIF_MSG_RX_ERR |
92 NETIF_MSG_TX_ERR | NETIF_MSG_HW);
93module_param(debug, uint, 0);
94MODULE_PARM_DESC(debug, "Bitmapped debugging message enable value");
95
96/**************************************************************************
97 *
98 * Utility functions and prototypes
99 *
100 *************************************************************************/
101
102static void efx_remove_port(struct efx_nic *efx);
103static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog);
104static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp);
105static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
106 u32 flags);
107
108#define EFX_ASSERT_RESET_SERIALISED(efx) \
109 do { \
110 if ((efx->state == STATE_READY) || \
111 (efx->state == STATE_RECOVERY) || \
112 (efx->state == STATE_DISABLED)) \
113 ASSERT_RTNL(); \
114 } while (0)
115
116/**************************************************************************
117 *
118 * Port handling
119 *
120 **************************************************************************/
121
122static void efx_fini_port(struct efx_nic *efx);
123
124static int efx_probe_port(struct efx_nic *efx)
125{
126 int rc;
127
128 netif_dbg(efx, probe, efx->net_dev, "create port\n");
129
130 if (phy_flash_cfg)
131 efx->phy_mode = PHY_MODE_SPECIAL;
132
133 /* Connect up MAC/PHY operations table */
134 rc = efx->type->probe_port(efx);
135 if (rc)
136 return rc;
137
138 /* Initialise MAC address to permanent address */
139 ether_addr_copy(efx->net_dev->dev_addr, efx->net_dev->perm_addr);
140
141 return 0;
142}
143
144static int efx_init_port(struct efx_nic *efx)
145{
146 int rc;
147
148 netif_dbg(efx, drv, efx->net_dev, "init port\n");
149
150 mutex_lock(&efx->mac_lock);
151
152 rc = efx->phy_op->init(efx);
153 if (rc)
154 goto fail1;
155
156 efx->port_initialized = true;
157
158 /* Ensure the PHY advertises the correct flow control settings */
159 rc = efx->phy_op->reconfigure(efx);
160 if (rc && rc != -EPERM)
161 goto fail2;
162
163 mutex_unlock(&efx->mac_lock);
164 return 0;
165
166fail2:
167 efx->phy_op->fini(efx);
168fail1:
169 mutex_unlock(&efx->mac_lock);
170 return rc;
171}
172
173static void efx_fini_port(struct efx_nic *efx)
174{
175 netif_dbg(efx, drv, efx->net_dev, "shut down port\n");
176
177 if (!efx->port_initialized)
178 return;
179
180 efx->phy_op->fini(efx);
181 efx->port_initialized = false;
182
183 efx->link_state.up = false;
184 efx_link_status_changed(efx);
185}
186
187static void efx_remove_port(struct efx_nic *efx)
188{
189 netif_dbg(efx, drv, efx->net_dev, "destroying port\n");
190
191 efx->type->remove_port(efx);
192}
193
194/**************************************************************************
195 *
196 * NIC handling
197 *
198 **************************************************************************/
199
200static LIST_HEAD(efx_primary_list);
201static LIST_HEAD(efx_unassociated_list);
202
203static bool efx_same_controller(struct efx_nic *left, struct efx_nic *right)
204{
205 return left->type == right->type &&
206 left->vpd_sn && right->vpd_sn &&
207 !strcmp(left->vpd_sn, right->vpd_sn);
208}
209
210static void efx_associate(struct efx_nic *efx)
211{
212 struct efx_nic *other, *next;
213
214 if (efx->primary == efx) {
215 /* Adding primary function; look for secondaries */
216
217 netif_dbg(efx, probe, efx->net_dev, "adding to primary list\n");
218 list_add_tail(&efx->node, &efx_primary_list);
219
220 list_for_each_entry_safe(other, next, &efx_unassociated_list,
221 node) {
222 if (efx_same_controller(efx, other)) {
223 list_del(&other->node);
224 netif_dbg(other, probe, other->net_dev,
225 "moving to secondary list of %s %s\n",
226 pci_name(efx->pci_dev),
227 efx->net_dev->name);
228 list_add_tail(&other->node,
229 &efx->secondary_list);
230 other->primary = efx;
231 }
232 }
233 } else {
234 /* Adding secondary function; look for primary */
235
236 list_for_each_entry(other, &efx_primary_list, node) {
237 if (efx_same_controller(efx, other)) {
238 netif_dbg(efx, probe, efx->net_dev,
239 "adding to secondary list of %s %s\n",
240 pci_name(other->pci_dev),
241 other->net_dev->name);
242 list_add_tail(&efx->node,
243 &other->secondary_list);
244 efx->primary = other;
245 return;
246 }
247 }
248
249 netif_dbg(efx, probe, efx->net_dev,
250 "adding to unassociated list\n");
251 list_add_tail(&efx->node, &efx_unassociated_list);
252 }
253}
254
255static void efx_dissociate(struct efx_nic *efx)
256{
257 struct efx_nic *other, *next;
258
259 list_del(&efx->node);
260 efx->primary = NULL;
261
262 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
263 list_del(&other->node);
264 netif_dbg(other, probe, other->net_dev,
265 "moving to unassociated list\n");
266 list_add_tail(&other->node, &efx_unassociated_list);
267 other->primary = NULL;
268 }
269}
270
271static int efx_probe_nic(struct efx_nic *efx)
272{
273 int rc;
274
275 netif_dbg(efx, probe, efx->net_dev, "creating NIC\n");
276
277 /* Carry out hardware-type specific initialisation */
278 rc = efx->type->probe(efx);
279 if (rc)
280 return rc;
281
282 do {
283 if (!efx->max_channels || !efx->max_tx_channels) {
284 netif_err(efx, drv, efx->net_dev,
285 "Insufficient resources to allocate"
286 " any channels\n");
287 rc = -ENOSPC;
288 goto fail1;
289 }
290
291 /* Determine the number of channels and queues by trying
292 * to hook in MSI-X interrupts.
293 */
294 rc = efx_probe_interrupts(efx);
295 if (rc)
296 goto fail1;
297
298 rc = efx_set_channels(efx);
299 if (rc)
300 goto fail1;
301
302 /* dimension_resources can fail with EAGAIN */
303 rc = efx->type->dimension_resources(efx);
304 if (rc != 0 && rc != -EAGAIN)
305 goto fail2;
306
307 if (rc == -EAGAIN)
308 /* try again with new max_channels */
309 efx_remove_interrupts(efx);
310
311 } while (rc == -EAGAIN);
312
313 if (efx->n_channels > 1)
314 netdev_rss_key_fill(efx->rss_context.rx_hash_key,
315 sizeof(efx->rss_context.rx_hash_key));
316 efx_set_default_rx_indir_table(efx, &efx->rss_context);
317
318 /* Initialise the interrupt moderation settings */
319 efx->irq_mod_step_us = DIV_ROUND_UP(efx->timer_quantum_ns, 1000);
320 efx_init_irq_moderation(efx, tx_irq_mod_usec, rx_irq_mod_usec, true,
321 true);
322
323 return 0;
324
325fail2:
326 efx_remove_interrupts(efx);
327fail1:
328 efx->type->remove(efx);
329 return rc;
330}
331
332static void efx_remove_nic(struct efx_nic *efx)
333{
334 netif_dbg(efx, drv, efx->net_dev, "destroying NIC\n");
335
336 efx_remove_interrupts(efx);
337 efx->type->remove(efx);
338}
339
340/**************************************************************************
341 *
342 * NIC startup/shutdown
343 *
344 *************************************************************************/
345
346static int efx_probe_all(struct efx_nic *efx)
347{
348 int rc;
349
350 rc = efx_probe_nic(efx);
351 if (rc) {
352 netif_err(efx, probe, efx->net_dev, "failed to create NIC\n");
353 goto fail1;
354 }
355
356 rc = efx_probe_port(efx);
357 if (rc) {
358 netif_err(efx, probe, efx->net_dev, "failed to create port\n");
359 goto fail2;
360 }
361
362 BUILD_BUG_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_RXQ_MIN_ENT);
363 if (WARN_ON(EFX_DEFAULT_DMAQ_SIZE < EFX_TXQ_MIN_ENT(efx))) {
364 rc = -EINVAL;
365 goto fail3;
366 }
367
368#ifdef CONFIG_SFC_SRIOV
369 rc = efx->type->vswitching_probe(efx);
370 if (rc) /* not fatal; the PF will still work fine */
371 netif_warn(efx, probe, efx->net_dev,
372 "failed to setup vswitching rc=%d;"
373 " VFs may not function\n", rc);
374#endif
375
376 rc = efx_probe_filters(efx);
377 if (rc) {
378 netif_err(efx, probe, efx->net_dev,
379 "failed to create filter tables\n");
380 goto fail4;
381 }
382
383 rc = efx_probe_channels(efx);
384 if (rc)
385 goto fail5;
386
387 return 0;
388
389 fail5:
390 efx_remove_filters(efx);
391 fail4:
392#ifdef CONFIG_SFC_SRIOV
393 efx->type->vswitching_remove(efx);
394#endif
395 fail3:
396 efx_remove_port(efx);
397 fail2:
398 efx_remove_nic(efx);
399 fail1:
400 return rc;
401}
402
403static void efx_remove_all(struct efx_nic *efx)
404{
405 rtnl_lock();
406 efx_xdp_setup_prog(efx, NULL);
407 rtnl_unlock();
408
409 efx_remove_channels(efx);
410 efx_remove_filters(efx);
411#ifdef CONFIG_SFC_SRIOV
412 efx->type->vswitching_remove(efx);
413#endif
414 efx_remove_port(efx);
415 efx_remove_nic(efx);
416}
417
418/**************************************************************************
419 *
420 * Interrupt moderation
421 *
422 **************************************************************************/
423unsigned int efx_usecs_to_ticks(struct efx_nic *efx, unsigned int usecs)
424{
425 if (usecs == 0)
426 return 0;
427 if (usecs * 1000 < efx->timer_quantum_ns)
428 return 1; /* never round down to 0 */
429 return usecs * 1000 / efx->timer_quantum_ns;
430}
431
432unsigned int efx_ticks_to_usecs(struct efx_nic *efx, unsigned int ticks)
433{
434 /* We must round up when converting ticks to microseconds
435 * because we round down when converting the other way.
436 */
437 return DIV_ROUND_UP(ticks * efx->timer_quantum_ns, 1000);
438}
439
440/* Set interrupt moderation parameters */
441int efx_init_irq_moderation(struct efx_nic *efx, unsigned int tx_usecs,
442 unsigned int rx_usecs, bool rx_adaptive,
443 bool rx_may_override_tx)
444{
445 struct efx_channel *channel;
446 unsigned int timer_max_us;
447
448 EFX_ASSERT_RESET_SERIALISED(efx);
449
450 timer_max_us = efx->timer_max_ns / 1000;
451
452 if (tx_usecs > timer_max_us || rx_usecs > timer_max_us)
453 return -EINVAL;
454
455 if (tx_usecs != rx_usecs && efx->tx_channel_offset == 0 &&
456 !rx_may_override_tx) {
457 netif_err(efx, drv, efx->net_dev, "Channels are shared. "
458 "RX and TX IRQ moderation must be equal\n");
459 return -EINVAL;
460 }
461
462 efx->irq_rx_adaptive = rx_adaptive;
463 efx->irq_rx_moderation_us = rx_usecs;
464 efx_for_each_channel(channel, efx) {
465 if (efx_channel_has_rx_queue(channel))
466 channel->irq_moderation_us = rx_usecs;
467 else if (efx_channel_has_tx_queues(channel))
468 channel->irq_moderation_us = tx_usecs;
469 else if (efx_channel_is_xdp_tx(channel))
470 channel->irq_moderation_us = tx_usecs;
471 }
472
473 return 0;
474}
475
476void efx_get_irq_moderation(struct efx_nic *efx, unsigned int *tx_usecs,
477 unsigned int *rx_usecs, bool *rx_adaptive)
478{
479 *rx_adaptive = efx->irq_rx_adaptive;
480 *rx_usecs = efx->irq_rx_moderation_us;
481
482 /* If channels are shared between RX and TX, so is IRQ
483 * moderation. Otherwise, IRQ moderation is the same for all
484 * TX channels and is not adaptive.
485 */
486 if (efx->tx_channel_offset == 0) {
487 *tx_usecs = *rx_usecs;
488 } else {
489 struct efx_channel *tx_channel;
490
491 tx_channel = efx->channel[efx->tx_channel_offset];
492 *tx_usecs = tx_channel->irq_moderation_us;
493 }
494}
495
496/**************************************************************************
497 *
498 * ioctls
499 *
500 *************************************************************************/
501
502/* Net device ioctl
503 * Context: process, rtnl_lock() held.
504 */
505static int efx_ioctl(struct net_device *net_dev, struct ifreq *ifr, int cmd)
506{
507 struct efx_nic *efx = netdev_priv(net_dev);
508 struct mii_ioctl_data *data = if_mii(ifr);
509
510 if (cmd == SIOCSHWTSTAMP)
511 return efx_ptp_set_ts_config(efx, ifr);
512 if (cmd == SIOCGHWTSTAMP)
513 return efx_ptp_get_ts_config(efx, ifr);
514
515 /* Convert phy_id from older PRTAD/DEVAD format */
516 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
517 (data->phy_id & 0xfc00) == 0x0400)
518 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
519
520 return mdio_mii_ioctl(&efx->mdio, data, cmd);
521}
522
523/**************************************************************************
524 *
525 * Kernel net device interface
526 *
527 *************************************************************************/
528
529/* Context: process, rtnl_lock() held. */
530int efx_net_open(struct net_device *net_dev)
531{
532 struct efx_nic *efx = netdev_priv(net_dev);
533 int rc;
534
535 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
536 raw_smp_processor_id());
537
538 rc = efx_check_disabled(efx);
539 if (rc)
540 return rc;
541 if (efx->phy_mode & PHY_MODE_SPECIAL)
542 return -EBUSY;
543 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, RESET_TYPE_ALL))
544 return -EIO;
545
546 /* Notify the kernel of the link state polled during driver load,
547 * before the monitor starts running */
548 efx_link_status_changed(efx);
549
550 efx_start_all(efx);
551 if (efx->state == STATE_DISABLED || efx->reset_pending)
552 netif_device_detach(efx->net_dev);
553 efx_selftest_async_start(efx);
554 return 0;
555}
556
557/* Context: process, rtnl_lock() held.
558 * Note that the kernel will ignore our return code; this method
559 * should really be a void.
560 */
561int efx_net_stop(struct net_device *net_dev)
562{
563 struct efx_nic *efx = netdev_priv(net_dev);
564
565 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
566 raw_smp_processor_id());
567
568 /* Stop the device and flush all the channels */
569 efx_stop_all(efx);
570
571 return 0;
572}
573
574static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
575{
576 struct efx_nic *efx = netdev_priv(net_dev);
577
578 if (efx->type->vlan_rx_add_vid)
579 return efx->type->vlan_rx_add_vid(efx, proto, vid);
580 else
581 return -EOPNOTSUPP;
582}
583
584static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
585{
586 struct efx_nic *efx = netdev_priv(net_dev);
587
588 if (efx->type->vlan_rx_kill_vid)
589 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
590 else
591 return -EOPNOTSUPP;
592}
593
594static const struct net_device_ops efx_netdev_ops = {
595 .ndo_open = efx_net_open,
596 .ndo_stop = efx_net_stop,
597 .ndo_get_stats64 = efx_net_stats,
598 .ndo_tx_timeout = efx_watchdog,
599 .ndo_start_xmit = efx_hard_start_xmit,
600 .ndo_validate_addr = eth_validate_addr,
601 .ndo_do_ioctl = efx_ioctl,
602 .ndo_change_mtu = efx_change_mtu,
603 .ndo_set_mac_address = efx_set_mac_address,
604 .ndo_set_rx_mode = efx_set_rx_mode,
605 .ndo_set_features = efx_set_features,
606 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
607 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
608#ifdef CONFIG_SFC_SRIOV
609 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
610 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
611 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
612 .ndo_get_vf_config = efx_sriov_get_vf_config,
613 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
614#endif
615 .ndo_get_phys_port_id = efx_get_phys_port_id,
616 .ndo_get_phys_port_name = efx_get_phys_port_name,
617 .ndo_setup_tc = efx_setup_tc,
618#ifdef CONFIG_RFS_ACCEL
619 .ndo_rx_flow_steer = efx_filter_rfs,
620#endif
621 .ndo_udp_tunnel_add = udp_tunnel_nic_add_port,
622 .ndo_udp_tunnel_del = udp_tunnel_nic_del_port,
623 .ndo_xdp_xmit = efx_xdp_xmit,
624 .ndo_bpf = efx_xdp
625};
626
627static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
628{
629 struct bpf_prog *old_prog;
630
631 if (efx->xdp_rxq_info_failed) {
632 netif_err(efx, drv, efx->net_dev,
633 "Unable to bind XDP program due to previous failure of rxq_info\n");
634 return -EINVAL;
635 }
636
637 if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) {
638 netif_err(efx, drv, efx->net_dev,
639 "Unable to configure XDP with MTU of %d (max: %d)\n",
640 efx->net_dev->mtu, efx_xdp_max_mtu(efx));
641 return -EINVAL;
642 }
643
644 old_prog = rtnl_dereference(efx->xdp_prog);
645 rcu_assign_pointer(efx->xdp_prog, prog);
646 /* Release the reference that was originally passed by the caller. */
647 if (old_prog)
648 bpf_prog_put(old_prog);
649
650 return 0;
651}
652
653/* Context: process, rtnl_lock() held. */
654static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
655{
656 struct efx_nic *efx = netdev_priv(dev);
657
658 switch (xdp->command) {
659 case XDP_SETUP_PROG:
660 return efx_xdp_setup_prog(efx, xdp->prog);
661 default:
662 return -EINVAL;
663 }
664}
665
666static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
667 u32 flags)
668{
669 struct efx_nic *efx = netdev_priv(dev);
670
671 if (!netif_running(dev))
672 return -EINVAL;
673
674 return efx_xdp_tx_buffers(efx, n, xdpfs, flags & XDP_XMIT_FLUSH);
675}
676
677static void efx_update_name(struct efx_nic *efx)
678{
679 strcpy(efx->name, efx->net_dev->name);
680 efx_mtd_rename(efx);
681 efx_set_channel_names(efx);
682}
683
684static int efx_netdev_event(struct notifier_block *this,
685 unsigned long event, void *ptr)
686{
687 struct net_device *net_dev = netdev_notifier_info_to_dev(ptr);
688
689 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
690 event == NETDEV_CHANGENAME)
691 efx_update_name(netdev_priv(net_dev));
692
693 return NOTIFY_DONE;
694}
695
696static struct notifier_block efx_netdev_notifier = {
697 .notifier_call = efx_netdev_event,
698};
699
700static ssize_t
701show_phy_type(struct device *dev, struct device_attribute *attr, char *buf)
702{
703 struct efx_nic *efx = dev_get_drvdata(dev);
704 return sprintf(buf, "%d\n", efx->phy_type);
705}
706static DEVICE_ATTR(phy_type, 0444, show_phy_type, NULL);
707
708static int efx_register_netdev(struct efx_nic *efx)
709{
710 struct net_device *net_dev = efx->net_dev;
711 struct efx_channel *channel;
712 int rc;
713
714 net_dev->watchdog_timeo = 5 * HZ;
715 net_dev->irq = efx->pci_dev->irq;
716 net_dev->netdev_ops = &efx_netdev_ops;
717 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
718 net_dev->priv_flags |= IFF_UNICAST_FLT;
719 net_dev->ethtool_ops = &efx_ethtool_ops;
720 net_dev->gso_max_segs = EFX_TSO_MAX_SEGS;
721 net_dev->min_mtu = EFX_MIN_MTU;
722 net_dev->max_mtu = EFX_MAX_MTU;
723
724 rtnl_lock();
725
726 /* Enable resets to be scheduled and check whether any were
727 * already requested. If so, the NIC is probably hosed so we
728 * abort.
729 */
730 efx->state = STATE_READY;
731 smp_mb(); /* ensure we change state before checking reset_pending */
732 if (efx->reset_pending) {
733 netif_err(efx, probe, efx->net_dev,
734 "aborting probe due to scheduled reset\n");
735 rc = -EIO;
736 goto fail_locked;
737 }
738
739 rc = dev_alloc_name(net_dev, net_dev->name);
740 if (rc < 0)
741 goto fail_locked;
742 efx_update_name(efx);
743
744 /* Always start with carrier off; PHY events will detect the link */
745 netif_carrier_off(net_dev);
746
747 rc = register_netdevice(net_dev);
748 if (rc)
749 goto fail_locked;
750
751 efx_for_each_channel(channel, efx) {
752 struct efx_tx_queue *tx_queue;
753 efx_for_each_channel_tx_queue(tx_queue, channel)
754 efx_init_tx_queue_core_txq(tx_queue);
755 }
756
757 efx_associate(efx);
758
759 rtnl_unlock();
760
761 rc = device_create_file(&efx->pci_dev->dev, &dev_attr_phy_type);
762 if (rc) {
763 netif_err(efx, drv, efx->net_dev,
764 "failed to init net dev attributes\n");
765 goto fail_registered;
766 }
767
768 efx_init_mcdi_logging(efx);
769
770 return 0;
771
772fail_registered:
773 rtnl_lock();
774 efx_dissociate(efx);
775 unregister_netdevice(net_dev);
776fail_locked:
777 efx->state = STATE_UNINIT;
778 rtnl_unlock();
779 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
780 return rc;
781}
782
783static void efx_unregister_netdev(struct efx_nic *efx)
784{
785 if (!efx->net_dev)
786 return;
787
788 BUG_ON(netdev_priv(efx->net_dev) != efx);
789
790 if (efx_dev_registered(efx)) {
791 strlcpy(efx->name, pci_name(efx->pci_dev), sizeof(efx->name));
792 efx_fini_mcdi_logging(efx);
793 device_remove_file(&efx->pci_dev->dev, &dev_attr_phy_type);
794 unregister_netdev(efx->net_dev);
795 }
796}
797
798/**************************************************************************
799 *
800 * List of NICs we support
801 *
802 **************************************************************************/
803
804/* PCI device ID table */
805static const struct pci_device_id efx_pci_table[] = {
806 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0803), /* SFC9020 */
807 .driver_data = (unsigned long) &siena_a0_nic_type},
808 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0813), /* SFL9021 */
809 .driver_data = (unsigned long) &siena_a0_nic_type},
810 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
811 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
812 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
813 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
814 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
815 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
816 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
817 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
818 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
819 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
820 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
821 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
822 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
823 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
824 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
825 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
826 {0} /* end of list */
827};
828
829/**************************************************************************
830 *
831 * Data housekeeping
832 *
833 **************************************************************************/
834
835void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
836{
837 u64 n_rx_nodesc_trunc = 0;
838 struct efx_channel *channel;
839
840 efx_for_each_channel(channel, efx)
841 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
842 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
843 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(&efx->n_rx_noskb_drops);
844}
845
846/**************************************************************************
847 *
848 * PCI interface
849 *
850 **************************************************************************/
851
852/* Main body of final NIC shutdown code
853 * This is called only at module unload (or hotplug removal).
854 */
855static void efx_pci_remove_main(struct efx_nic *efx)
856{
857 /* Flush reset_work. It can no longer be scheduled since we
858 * are not READY.
859 */
860 BUG_ON(efx->state == STATE_READY);
861 efx_flush_reset_workqueue(efx);
862
863 efx_disable_interrupts(efx);
864 efx_clear_interrupt_affinity(efx);
865 efx_nic_fini_interrupt(efx);
866 efx_fini_port(efx);
867 efx->type->fini(efx);
868 efx_fini_napi(efx);
869 efx_remove_all(efx);
870}
871
872/* Final NIC shutdown
873 * This is called only at module unload (or hotplug removal). A PF can call
874 * this on its VFs to ensure they are unbound first.
875 */
876static void efx_pci_remove(struct pci_dev *pci_dev)
877{
878 struct efx_nic *efx;
879
880 efx = pci_get_drvdata(pci_dev);
881 if (!efx)
882 return;
883
884 /* Mark the NIC as fini, then stop the interface */
885 rtnl_lock();
886 efx_dissociate(efx);
887 dev_close(efx->net_dev);
888 efx_disable_interrupts(efx);
889 efx->state = STATE_UNINIT;
890 rtnl_unlock();
891
892 if (efx->type->sriov_fini)
893 efx->type->sriov_fini(efx);
894
895 efx_unregister_netdev(efx);
896
897 efx_mtd_remove(efx);
898
899 efx_pci_remove_main(efx);
900
901 efx_fini_io(efx);
902 netif_dbg(efx, drv, efx->net_dev, "shutdown successful\n");
903
904 efx_fini_struct(efx);
905 free_netdev(efx->net_dev);
906
907 pci_disable_pcie_error_reporting(pci_dev);
908};
909
910/* NIC VPD information
911 * Called during probe to display the part number of the
912 * installed NIC. VPD is potentially very large but this should
913 * always appear within the first 512 bytes.
914 */
915#define SFC_VPD_LEN 512
916static void efx_probe_vpd_strings(struct efx_nic *efx)
917{
918 struct pci_dev *dev = efx->pci_dev;
919 char vpd_data[SFC_VPD_LEN];
920 ssize_t vpd_size;
921 int ro_start, ro_size, i, j;
922
923 /* Get the vpd data from the device */
924 vpd_size = pci_read_vpd(dev, 0, sizeof(vpd_data), vpd_data);
925 if (vpd_size <= 0) {
926 netif_err(efx, drv, efx->net_dev, "Unable to read VPD\n");
927 return;
928 }
929
930 /* Get the Read only section */
931 ro_start = pci_vpd_find_tag(vpd_data, 0, vpd_size, PCI_VPD_LRDT_RO_DATA);
932 if (ro_start < 0) {
933 netif_err(efx, drv, efx->net_dev, "VPD Read-only not found\n");
934 return;
935 }
936
937 ro_size = pci_vpd_lrdt_size(&vpd_data[ro_start]);
938 j = ro_size;
939 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
940 if (i + j > vpd_size)
941 j = vpd_size - i;
942
943 /* Get the Part number */
944 i = pci_vpd_find_info_keyword(vpd_data, i, j, "PN");
945 if (i < 0) {
946 netif_err(efx, drv, efx->net_dev, "Part number not found\n");
947 return;
948 }
949
950 j = pci_vpd_info_field_size(&vpd_data[i]);
951 i += PCI_VPD_INFO_FLD_HDR_SIZE;
952 if (i + j > vpd_size) {
953 netif_err(efx, drv, efx->net_dev, "Incomplete part number\n");
954 return;
955 }
956
957 netif_info(efx, drv, efx->net_dev,
958 "Part Number : %.*s\n", j, &vpd_data[i]);
959
960 i = ro_start + PCI_VPD_LRDT_TAG_SIZE;
961 j = ro_size;
962 i = pci_vpd_find_info_keyword(vpd_data, i, j, "SN");
963 if (i < 0) {
964 netif_err(efx, drv, efx->net_dev, "Serial number not found\n");
965 return;
966 }
967
968 j = pci_vpd_info_field_size(&vpd_data[i]);
969 i += PCI_VPD_INFO_FLD_HDR_SIZE;
970 if (i + j > vpd_size) {
971 netif_err(efx, drv, efx->net_dev, "Incomplete serial number\n");
972 return;
973 }
974
975 efx->vpd_sn = kmalloc(j + 1, GFP_KERNEL);
976 if (!efx->vpd_sn)
977 return;
978
979 snprintf(efx->vpd_sn, j + 1, "%s", &vpd_data[i]);
980}
981
982
983/* Main body of NIC initialisation
984 * This is called at module load (or hotplug insertion, theoretically).
985 */
986static int efx_pci_probe_main(struct efx_nic *efx)
987{
988 int rc;
989
990 /* Do start-of-day initialisation */
991 rc = efx_probe_all(efx);
992 if (rc)
993 goto fail1;
994
995 efx_init_napi(efx);
996
997 down_write(&efx->filter_sem);
998 rc = efx->type->init(efx);
999 up_write(&efx->filter_sem);
1000 if (rc) {
1001 netif_err(efx, probe, efx->net_dev,
1002 "failed to initialise NIC\n");
1003 goto fail3;
1004 }
1005
1006 rc = efx_init_port(efx);
1007 if (rc) {
1008 netif_err(efx, probe, efx->net_dev,
1009 "failed to initialise port\n");
1010 goto fail4;
1011 }
1012
1013 rc = efx_nic_init_interrupt(efx);
1014 if (rc)
1015 goto fail5;
1016
1017 efx_set_interrupt_affinity(efx);
1018 rc = efx_enable_interrupts(efx);
1019 if (rc)
1020 goto fail6;
1021
1022 return 0;
1023
1024 fail6:
1025 efx_clear_interrupt_affinity(efx);
1026 efx_nic_fini_interrupt(efx);
1027 fail5:
1028 efx_fini_port(efx);
1029 fail4:
1030 efx->type->fini(efx);
1031 fail3:
1032 efx_fini_napi(efx);
1033 efx_remove_all(efx);
1034 fail1:
1035 return rc;
1036}
1037
1038static int efx_pci_probe_post_io(struct efx_nic *efx)
1039{
1040 struct net_device *net_dev = efx->net_dev;
1041 int rc = efx_pci_probe_main(efx);
1042
1043 if (rc)
1044 return rc;
1045
1046 if (efx->type->sriov_init) {
1047 rc = efx->type->sriov_init(efx);
1048 if (rc)
1049 netif_err(efx, probe, efx->net_dev,
1050 "SR-IOV can't be enabled rc %d\n", rc);
1051 }
1052
1053 /* Determine netdevice features */
1054 net_dev->features |= (efx->type->offload_features | NETIF_F_SG |
1055 NETIF_F_TSO | NETIF_F_RXCSUM | NETIF_F_RXALL);
1056 if (efx->type->offload_features & (NETIF_F_IPV6_CSUM | NETIF_F_HW_CSUM))
1057 net_dev->features |= NETIF_F_TSO6;
1058 /* Check whether device supports TSO */
1059 if (!efx->type->tso_versions || !efx->type->tso_versions(efx))
1060 net_dev->features &= ~NETIF_F_ALL_TSO;
1061 /* Mask for features that also apply to VLAN devices */
1062 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
1063 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
1064 NETIF_F_RXCSUM);
1065
1066 net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
1067
1068 /* Disable receiving frames with bad FCS, by default. */
1069 net_dev->features &= ~NETIF_F_RXALL;
1070
1071 /* Disable VLAN filtering by default. It may be enforced if
1072 * the feature is fixed (i.e. VLAN filters are required to
1073 * receive VLAN tagged packets due to vPort restrictions).
1074 */
1075 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1076 net_dev->features |= efx->fixed_features;
1077
1078 rc = efx_register_netdev(efx);
1079 if (!rc)
1080 return 0;
1081
1082 efx_pci_remove_main(efx);
1083 return rc;
1084}
1085
1086/* NIC initialisation
1087 *
1088 * This is called at module load (or hotplug insertion,
1089 * theoretically). It sets up PCI mappings, resets the NIC,
1090 * sets up and registers the network devices with the kernel and hooks
1091 * the interrupt service routine. It does not prepare the device for
1092 * transmission; this is left to the first time one of the network
1093 * interfaces is brought up (i.e. efx_net_open).
1094 */
1095static int efx_pci_probe(struct pci_dev *pci_dev,
1096 const struct pci_device_id *entry)
1097{
1098 struct net_device *net_dev;
1099 struct efx_nic *efx;
1100 int rc;
1101
1102 /* Allocate and initialise a struct net_device and struct efx_nic */
1103 net_dev = alloc_etherdev_mqs(sizeof(*efx), EFX_MAX_CORE_TX_QUEUES,
1104 EFX_MAX_RX_QUEUES);
1105 if (!net_dev)
1106 return -ENOMEM;
1107 efx = netdev_priv(net_dev);
1108 efx->type = (const struct efx_nic_type *) entry->driver_data;
1109 efx->fixed_features |= NETIF_F_HIGHDMA;
1110
1111 pci_set_drvdata(pci_dev, efx);
1112 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
1113 rc = efx_init_struct(efx, pci_dev, net_dev);
1114 if (rc)
1115 goto fail1;
1116
1117 netif_info(efx, probe, efx->net_dev,
1118 "Solarflare NIC detected\n");
1119
1120 if (!efx->type->is_vf)
1121 efx_probe_vpd_strings(efx);
1122
1123 /* Set up basic I/O (BAR mappings etc) */
1124 rc = efx_init_io(efx, efx->type->mem_bar(efx), efx->type->max_dma_mask,
1125 efx->type->mem_map_size(efx));
1126 if (rc)
1127 goto fail2;
1128
1129 rc = efx_pci_probe_post_io(efx);
1130 if (rc) {
1131 /* On failure, retry once immediately.
1132 * If we aborted probe due to a scheduled reset, dismiss it.
1133 */
1134 efx->reset_pending = 0;
1135 rc = efx_pci_probe_post_io(efx);
1136 if (rc) {
1137 /* On another failure, retry once more
1138 * after a 50-305ms delay.
1139 */
1140 unsigned char r;
1141
1142 get_random_bytes(&r, 1);
1143 msleep((unsigned int)r + 50);
1144 efx->reset_pending = 0;
1145 rc = efx_pci_probe_post_io(efx);
1146 }
1147 }
1148 if (rc)
1149 goto fail3;
1150
1151 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
1152
1153 /* Try to create MTDs, but allow this to fail */
1154 rtnl_lock();
1155 rc = efx_mtd_probe(efx);
1156 rtnl_unlock();
1157 if (rc && rc != -EPERM)
1158 netif_warn(efx, probe, efx->net_dev,
1159 "failed to create MTDs (%d)\n", rc);
1160
1161 (void)pci_enable_pcie_error_reporting(pci_dev);
1162
1163 if (efx->type->udp_tnl_push_ports)
1164 efx->type->udp_tnl_push_ports(efx);
1165
1166 return 0;
1167
1168 fail3:
1169 efx_fini_io(efx);
1170 fail2:
1171 efx_fini_struct(efx);
1172 fail1:
1173 WARN_ON(rc > 0);
1174 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
1175 free_netdev(net_dev);
1176 return rc;
1177}
1178
1179/* efx_pci_sriov_configure returns the actual number of Virtual Functions
1180 * enabled on success
1181 */
1182#ifdef CONFIG_SFC_SRIOV
1183static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1184{
1185 int rc;
1186 struct efx_nic *efx = pci_get_drvdata(dev);
1187
1188 if (efx->type->sriov_configure) {
1189 rc = efx->type->sriov_configure(efx, num_vfs);
1190 if (rc)
1191 return rc;
1192 else
1193 return num_vfs;
1194 } else
1195 return -EOPNOTSUPP;
1196}
1197#endif
1198
1199static int efx_pm_freeze(struct device *dev)
1200{
1201 struct efx_nic *efx = dev_get_drvdata(dev);
1202
1203 rtnl_lock();
1204
1205 if (efx->state != STATE_DISABLED) {
1206 efx->state = STATE_UNINIT;
1207
1208 efx_device_detach_sync(efx);
1209
1210 efx_stop_all(efx);
1211 efx_disable_interrupts(efx);
1212 }
1213
1214 rtnl_unlock();
1215
1216 return 0;
1217}
1218
1219static int efx_pm_thaw(struct device *dev)
1220{
1221 int rc;
1222 struct efx_nic *efx = dev_get_drvdata(dev);
1223
1224 rtnl_lock();
1225
1226 if (efx->state != STATE_DISABLED) {
1227 rc = efx_enable_interrupts(efx);
1228 if (rc)
1229 goto fail;
1230
1231 mutex_lock(&efx->mac_lock);
1232 efx->phy_op->reconfigure(efx);
1233 mutex_unlock(&efx->mac_lock);
1234
1235 efx_start_all(efx);
1236
1237 efx_device_attach_if_not_resetting(efx);
1238
1239 efx->state = STATE_READY;
1240
1241 efx->type->resume_wol(efx);
1242 }
1243
1244 rtnl_unlock();
1245
1246 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
1247 efx_queue_reset_work(efx);
1248
1249 return 0;
1250
1251fail:
1252 rtnl_unlock();
1253
1254 return rc;
1255}
1256
1257static int efx_pm_poweroff(struct device *dev)
1258{
1259 struct pci_dev *pci_dev = to_pci_dev(dev);
1260 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1261
1262 efx->type->fini(efx);
1263
1264 efx->reset_pending = 0;
1265
1266 pci_save_state(pci_dev);
1267 return pci_set_power_state(pci_dev, PCI_D3hot);
1268}
1269
1270/* Used for both resume and restore */
1271static int efx_pm_resume(struct device *dev)
1272{
1273 struct pci_dev *pci_dev = to_pci_dev(dev);
1274 struct efx_nic *efx = pci_get_drvdata(pci_dev);
1275 int rc;
1276
1277 rc = pci_set_power_state(pci_dev, PCI_D0);
1278 if (rc)
1279 return rc;
1280 pci_restore_state(pci_dev);
1281 rc = pci_enable_device(pci_dev);
1282 if (rc)
1283 return rc;
1284 pci_set_master(efx->pci_dev);
1285 rc = efx->type->reset(efx, RESET_TYPE_ALL);
1286 if (rc)
1287 return rc;
1288 down_write(&efx->filter_sem);
1289 rc = efx->type->init(efx);
1290 up_write(&efx->filter_sem);
1291 if (rc)
1292 return rc;
1293 rc = efx_pm_thaw(dev);
1294 return rc;
1295}
1296
1297static int efx_pm_suspend(struct device *dev)
1298{
1299 int rc;
1300
1301 efx_pm_freeze(dev);
1302 rc = efx_pm_poweroff(dev);
1303 if (rc)
1304 efx_pm_resume(dev);
1305 return rc;
1306}
1307
1308static const struct dev_pm_ops efx_pm_ops = {
1309 .suspend = efx_pm_suspend,
1310 .resume = efx_pm_resume,
1311 .freeze = efx_pm_freeze,
1312 .thaw = efx_pm_thaw,
1313 .poweroff = efx_pm_poweroff,
1314 .restore = efx_pm_resume,
1315};
1316
1317static struct pci_driver efx_pci_driver = {
1318 .name = KBUILD_MODNAME,
1319 .id_table = efx_pci_table,
1320 .probe = efx_pci_probe,
1321 .remove = efx_pci_remove,
1322 .driver.pm = &efx_pm_ops,
1323 .err_handler = &efx_err_handlers,
1324#ifdef CONFIG_SFC_SRIOV
1325 .sriov_configure = efx_pci_sriov_configure,
1326#endif
1327};
1328
1329/**************************************************************************
1330 *
1331 * Kernel module interface
1332 *
1333 *************************************************************************/
1334
1335static int __init efx_init_module(void)
1336{
1337 int rc;
1338
1339 printk(KERN_INFO "Solarflare NET driver v" EFX_DRIVER_VERSION "\n");
1340
1341 rc = register_netdevice_notifier(&efx_netdev_notifier);
1342 if (rc)
1343 goto err_notifier;
1344
1345#ifdef CONFIG_SFC_SRIOV
1346 rc = efx_init_sriov();
1347 if (rc)
1348 goto err_sriov;
1349#endif
1350
1351 rc = efx_create_reset_workqueue();
1352 if (rc)
1353 goto err_reset;
1354
1355 rc = pci_register_driver(&efx_pci_driver);
1356 if (rc < 0)
1357 goto err_pci;
1358
1359 rc = pci_register_driver(&ef100_pci_driver);
1360 if (rc < 0)
1361 goto err_pci_ef100;
1362
1363 return 0;
1364
1365 err_pci_ef100:
1366 pci_unregister_driver(&efx_pci_driver);
1367 err_pci:
1368 efx_destroy_reset_workqueue();
1369 err_reset:
1370#ifdef CONFIG_SFC_SRIOV
1371 efx_fini_sriov();
1372 err_sriov:
1373#endif
1374 unregister_netdevice_notifier(&efx_netdev_notifier);
1375 err_notifier:
1376 return rc;
1377}
1378
1379static void __exit efx_exit_module(void)
1380{
1381 printk(KERN_INFO "Solarflare NET driver unloading\n");
1382
1383 pci_unregister_driver(&ef100_pci_driver);
1384 pci_unregister_driver(&efx_pci_driver);
1385 efx_destroy_reset_workqueue();
1386#ifdef CONFIG_SFC_SRIOV
1387 efx_fini_sriov();
1388#endif
1389 unregister_netdevice_notifier(&efx_netdev_notifier);
1390
1391}
1392
1393module_init(efx_init_module);
1394module_exit(efx_exit_module);
1395
1396MODULE_AUTHOR("Solarflare Communications and "
1397 "Michael Brown <mbrown@fensystems.co.uk>");
1398MODULE_DESCRIPTION("Solarflare network driver");
1399MODULE_LICENSE("GPL");
1400MODULE_DEVICE_TABLE(pci, efx_pci_table);
1401MODULE_VERSION(EFX_DRIVER_VERSION);