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(dev: efx->net_dev, addr: 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(lock: &efx->mac_lock);
153 return 0;
154
155fail:
156 mutex_unlock(lock: &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(new: &efx->node, head: &efx_primary_list);
205
206 list_for_each_entry_safe(other, next, &efx_unassociated_list,
207 node) {
208 if (efx_same_controller(left: efx, right: other)) {
209 list_del(entry: &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(new: &other->node,
215 head: &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(left: efx, right: 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(new: &efx->node,
229 head: &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(new: &efx->node, head: &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(entry: &efx->node);
246 efx->primary = NULL;
247
248 list_for_each_entry_safe(other, next, &efx->secondary_list, node) {
249 list_del(entry: &other->node);
250 netif_dbg(other, probe, other->net_dev,
251 "moving to unassociated list\n");
252 list_add_tail(new: &other->node, head: &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(buffer: efx->rss_context.rx_hash_key,
301 len: sizeof(efx->rss_context.rx_hash_key));
302 efx_set_default_rx_indir_table(efx, ctx: &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_usecs: tx_irq_mod_usec, rx_usecs: rx_irq_mod_usec, rx_adaptive: true,
307 rx_may_override_tx: 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(dev: net_dev);
496 struct mii_ioctl_data *data = if_mii(rq: ifr);
497
498 if (cmd == SIOCSHWTSTAMP)
499 return efx_ptp_set_ts_config(efx, ifr);
500 if (cmd == SIOCGHWTSTAMP)
501 return efx_ptp_get_ts_config(efx, ifr);
502
503 /* Convert phy_id from older PRTAD/DEVAD format */
504 if ((cmd == SIOCGMIIREG || cmd == SIOCSMIIREG) &&
505 (data->phy_id & 0xfc00) == 0x0400)
506 data->phy_id ^= MDIO_PHY_ID_C45 | 0x0400;
507
508 return mdio_mii_ioctl(mdio: &efx->mdio, mii_data: data, cmd);
509}
510
511/**************************************************************************
512 *
513 * Kernel net device interface
514 *
515 *************************************************************************/
516
517/* Context: process, rtnl_lock() held. */
518int efx_net_open(struct net_device *net_dev)
519{
520 struct efx_nic *efx = efx_netdev_priv(dev: net_dev);
521 int rc;
522
523 netif_dbg(efx, ifup, efx->net_dev, "opening device on CPU %d\n",
524 raw_smp_processor_id());
525
526 rc = efx_check_disabled(efx);
527 if (rc)
528 return rc;
529 if (efx->phy_mode & PHY_MODE_SPECIAL)
530 return -EBUSY;
531 if (efx_mcdi_poll_reboot(efx) && efx_reset(efx, method: RESET_TYPE_ALL))
532 return -EIO;
533
534 /* Notify the kernel of the link state polled during driver load,
535 * before the monitor starts running */
536 efx_link_status_changed(efx);
537
538 efx_start_all(efx);
539 if (efx->state == STATE_DISABLED || efx->reset_pending)
540 netif_device_detach(dev: efx->net_dev);
541 else
542 efx->state = STATE_NET_UP;
543
544 return 0;
545}
546
547/* Context: process, rtnl_lock() held.
548 * Note that the kernel will ignore our return code; this method
549 * should really be a void.
550 */
551int efx_net_stop(struct net_device *net_dev)
552{
553 struct efx_nic *efx = efx_netdev_priv(dev: net_dev);
554
555 netif_dbg(efx, ifdown, efx->net_dev, "closing on CPU %d\n",
556 raw_smp_processor_id());
557
558 /* Stop the device and flush all the channels */
559 efx_stop_all(efx);
560
561 return 0;
562}
563
564static int efx_vlan_rx_add_vid(struct net_device *net_dev, __be16 proto, u16 vid)
565{
566 struct efx_nic *efx = efx_netdev_priv(dev: net_dev);
567
568 if (efx->type->vlan_rx_add_vid)
569 return efx->type->vlan_rx_add_vid(efx, proto, vid);
570 else
571 return -EOPNOTSUPP;
572}
573
574static int efx_vlan_rx_kill_vid(struct net_device *net_dev, __be16 proto, u16 vid)
575{
576 struct efx_nic *efx = efx_netdev_priv(dev: net_dev);
577
578 if (efx->type->vlan_rx_kill_vid)
579 return efx->type->vlan_rx_kill_vid(efx, proto, vid);
580 else
581 return -EOPNOTSUPP;
582}
583
584static const struct net_device_ops efx_netdev_ops = {
585 .ndo_open = efx_net_open,
586 .ndo_stop = efx_net_stop,
587 .ndo_get_stats64 = efx_net_stats,
588 .ndo_tx_timeout = efx_watchdog,
589 .ndo_start_xmit = efx_hard_start_xmit,
590 .ndo_validate_addr = eth_validate_addr,
591 .ndo_eth_ioctl = efx_ioctl,
592 .ndo_change_mtu = efx_change_mtu,
593 .ndo_set_mac_address = efx_set_mac_address,
594 .ndo_set_rx_mode = efx_set_rx_mode,
595 .ndo_set_features = efx_set_features,
596 .ndo_features_check = efx_features_check,
597 .ndo_vlan_rx_add_vid = efx_vlan_rx_add_vid,
598 .ndo_vlan_rx_kill_vid = efx_vlan_rx_kill_vid,
599#ifdef CONFIG_SFC_SRIOV
600 .ndo_set_vf_mac = efx_sriov_set_vf_mac,
601 .ndo_set_vf_vlan = efx_sriov_set_vf_vlan,
602 .ndo_set_vf_spoofchk = efx_sriov_set_vf_spoofchk,
603 .ndo_get_vf_config = efx_sriov_get_vf_config,
604 .ndo_set_vf_link_state = efx_sriov_set_vf_link_state,
605#endif
606 .ndo_get_phys_port_id = efx_get_phys_port_id,
607 .ndo_get_phys_port_name = efx_get_phys_port_name,
608#ifdef CONFIG_RFS_ACCEL
609 .ndo_rx_flow_steer = efx_filter_rfs,
610#endif
611 .ndo_xdp_xmit = efx_xdp_xmit,
612 .ndo_bpf = efx_xdp
613};
614
615static int efx_xdp_setup_prog(struct efx_nic *efx, struct bpf_prog *prog)
616{
617 struct bpf_prog *old_prog;
618
619 if (efx->xdp_rxq_info_failed) {
620 netif_err(efx, drv, efx->net_dev,
621 "Unable to bind XDP program due to previous failure of rxq_info\n");
622 return -EINVAL;
623 }
624
625 if (prog && efx->net_dev->mtu > efx_xdp_max_mtu(efx)) {
626 netif_err(efx, drv, efx->net_dev,
627 "Unable to configure XDP with MTU of %d (max: %d)\n",
628 efx->net_dev->mtu, efx_xdp_max_mtu(efx));
629 return -EINVAL;
630 }
631
632 old_prog = rtnl_dereference(efx->xdp_prog);
633 rcu_assign_pointer(efx->xdp_prog, prog);
634 /* Release the reference that was originally passed by the caller. */
635 if (old_prog)
636 bpf_prog_put(prog: old_prog);
637
638 return 0;
639}
640
641/* Context: process, rtnl_lock() held. */
642static int efx_xdp(struct net_device *dev, struct netdev_bpf *xdp)
643{
644 struct efx_nic *efx = efx_netdev_priv(dev);
645
646 switch (xdp->command) {
647 case XDP_SETUP_PROG:
648 return efx_xdp_setup_prog(efx, prog: xdp->prog);
649 default:
650 return -EINVAL;
651 }
652}
653
654static int efx_xdp_xmit(struct net_device *dev, int n, struct xdp_frame **xdpfs,
655 u32 flags)
656{
657 struct efx_nic *efx = efx_netdev_priv(dev);
658
659 if (!netif_running(dev))
660 return -EINVAL;
661
662 return efx_xdp_tx_buffers(efx, n, xdpfs, flush: flags & XDP_XMIT_FLUSH);
663}
664
665static void efx_update_name(struct efx_nic *efx)
666{
667 strcpy(p: efx->name, q: efx->net_dev->name);
668 efx_mtd_rename(efx);
669 efx_set_channel_names(efx);
670}
671
672static int efx_netdev_event(struct notifier_block *this,
673 unsigned long event, void *ptr)
674{
675 struct net_device *net_dev = netdev_notifier_info_to_dev(info: ptr);
676
677 if ((net_dev->netdev_ops == &efx_netdev_ops) &&
678 event == NETDEV_CHANGENAME)
679 efx_update_name(efx: efx_netdev_priv(dev: net_dev));
680
681 return NOTIFY_DONE;
682}
683
684static struct notifier_block efx_netdev_notifier = {
685 .notifier_call = efx_netdev_event,
686};
687
688static ssize_t phy_type_show(struct device *dev,
689 struct device_attribute *attr, char *buf)
690{
691 struct efx_nic *efx = dev_get_drvdata(dev);
692 return sprintf(buf, fmt: "%d\n", efx->phy_type);
693}
694static DEVICE_ATTR_RO(phy_type);
695
696static int efx_register_netdev(struct efx_nic *efx)
697{
698 struct net_device *net_dev = efx->net_dev;
699 struct efx_channel *channel;
700 int rc;
701
702 net_dev->watchdog_timeo = 5 * HZ;
703 net_dev->irq = efx->pci_dev->irq;
704 net_dev->netdev_ops = &efx_netdev_ops;
705 if (efx_nic_rev(efx) >= EFX_REV_HUNT_A0)
706 net_dev->priv_flags |= IFF_UNICAST_FLT;
707 net_dev->ethtool_ops = &efx_ethtool_ops;
708 netif_set_tso_max_segs(dev: net_dev, EFX_TSO_MAX_SEGS);
709 net_dev->min_mtu = EFX_MIN_MTU;
710 net_dev->max_mtu = EFX_MAX_MTU;
711
712 rtnl_lock();
713
714 /* Enable resets to be scheduled and check whether any were
715 * already requested. If so, the NIC is probably hosed so we
716 * abort.
717 */
718 if (efx->reset_pending) {
719 pci_err(efx->pci_dev, "aborting probe due to scheduled reset\n");
720 rc = -EIO;
721 goto fail_locked;
722 }
723
724 rc = dev_alloc_name(dev: net_dev, name: net_dev->name);
725 if (rc < 0)
726 goto fail_locked;
727 efx_update_name(efx);
728
729 /* Always start with carrier off; PHY events will detect the link */
730 netif_carrier_off(dev: net_dev);
731
732 rc = register_netdevice(dev: net_dev);
733 if (rc)
734 goto fail_locked;
735
736 efx_for_each_channel(channel, efx) {
737 struct efx_tx_queue *tx_queue;
738 efx_for_each_channel_tx_queue(tx_queue, channel)
739 efx_init_tx_queue_core_txq(tx_queue);
740 }
741
742 efx_associate(efx);
743
744 efx->state = STATE_NET_DOWN;
745
746 rtnl_unlock();
747
748 rc = device_create_file(device: &efx->pci_dev->dev, entry: &dev_attr_phy_type);
749 if (rc) {
750 netif_err(efx, drv, efx->net_dev,
751 "failed to init net dev attributes\n");
752 goto fail_registered;
753 }
754
755 efx_init_mcdi_logging(efx);
756
757 return 0;
758
759fail_registered:
760 rtnl_lock();
761 efx_dissociate(efx);
762 unregister_netdevice(dev: net_dev);
763fail_locked:
764 efx->state = STATE_UNINIT;
765 rtnl_unlock();
766 netif_err(efx, drv, efx->net_dev, "could not register net dev\n");
767 return rc;
768}
769
770static void efx_unregister_netdev(struct efx_nic *efx)
771{
772 if (!efx->net_dev)
773 return;
774
775 if (WARN_ON(efx_netdev_priv(efx->net_dev) != efx))
776 return;
777
778 if (efx_dev_registered(efx)) {
779 strscpy(p: efx->name, q: pci_name(pdev: efx->pci_dev), size: sizeof(efx->name));
780 efx_fini_mcdi_logging(efx);
781 device_remove_file(dev: &efx->pci_dev->dev, attr: &dev_attr_phy_type);
782 unregister_netdev(dev: efx->net_dev);
783 }
784}
785
786/**************************************************************************
787 *
788 * List of NICs we support
789 *
790 **************************************************************************/
791
792/* PCI device ID table */
793static const struct pci_device_id efx_pci_table[] = {
794 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0903), /* SFC9120 PF */
795 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
796 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1903), /* SFC9120 VF */
797 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
798 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0923), /* SFC9140 PF */
799 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
800 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1923), /* SFC9140 VF */
801 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
802 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0a03), /* SFC9220 PF */
803 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
804 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1a03), /* SFC9220 VF */
805 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
806 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x0b03), /* SFC9250 PF */
807 .driver_data = (unsigned long) &efx_hunt_a0_nic_type},
808 {PCI_DEVICE(PCI_VENDOR_ID_SOLARFLARE, 0x1b03), /* SFC9250 VF */
809 .driver_data = (unsigned long) &efx_hunt_a0_vf_nic_type},
810 {0} /* end of list */
811};
812
813/**************************************************************************
814 *
815 * Data housekeeping
816 *
817 **************************************************************************/
818
819void efx_update_sw_stats(struct efx_nic *efx, u64 *stats)
820{
821 u64 n_rx_nodesc_trunc = 0;
822 struct efx_channel *channel;
823
824 efx_for_each_channel(channel, efx)
825 n_rx_nodesc_trunc += channel->n_rx_nodesc_trunc;
826 stats[GENERIC_STAT_rx_nodesc_trunc] = n_rx_nodesc_trunc;
827 stats[GENERIC_STAT_rx_noskb_drops] = atomic_read(v: &efx->n_rx_noskb_drops);
828}
829
830/**************************************************************************
831 *
832 * PCI interface
833 *
834 **************************************************************************/
835
836/* Main body of final NIC shutdown code
837 * This is called only at module unload (or hotplug removal).
838 */
839static void efx_pci_remove_main(struct efx_nic *efx)
840{
841 /* Flush reset_work. It can no longer be scheduled since we
842 * are not READY.
843 */
844 WARN_ON(efx_net_active(efx->state));
845 efx_flush_reset_workqueue(efx);
846
847 efx_disable_interrupts(efx);
848 efx_clear_interrupt_affinity(efx);
849 efx_nic_fini_interrupt(efx);
850 efx_fini_port(efx);
851 efx->type->fini(efx);
852 efx_fini_napi(efx);
853 efx_remove_all(efx);
854}
855
856/* Final NIC shutdown
857 * This is called only at module unload (or hotplug removal). A PF can call
858 * this on its VFs to ensure they are unbound first.
859 */
860static void efx_pci_remove(struct pci_dev *pci_dev)
861{
862 struct efx_probe_data *probe_data;
863 struct efx_nic *efx;
864
865 efx = pci_get_drvdata(pdev: pci_dev);
866 if (!efx)
867 return;
868
869 /* Mark the NIC as fini, then stop the interface */
870 rtnl_lock();
871 efx_dissociate(efx);
872 dev_close(dev: efx->net_dev);
873 efx_disable_interrupts(efx);
874 efx->state = STATE_UNINIT;
875 rtnl_unlock();
876
877 if (efx->type->sriov_fini)
878 efx->type->sriov_fini(efx);
879
880 efx_fini_devlink_lock(efx);
881 efx_unregister_netdev(efx);
882
883 efx_mtd_remove(efx);
884
885 efx_pci_remove_main(efx);
886
887 efx_fini_io(efx);
888 pci_dbg(efx->pci_dev, "shutdown successful\n");
889
890 efx_fini_devlink_and_unlock(efx);
891 efx_fini_struct(efx);
892 free_netdev(dev: efx->net_dev);
893 probe_data = container_of(efx, struct efx_probe_data, efx);
894 kfree(objp: probe_data);
895};
896
897/* NIC VPD information
898 * Called during probe to display the part number of the
899 * installed NIC.
900 */
901static void efx_probe_vpd_strings(struct efx_nic *efx)
902{
903 struct pci_dev *dev = efx->pci_dev;
904 unsigned int vpd_size, kw_len;
905 u8 *vpd_data;
906 int start;
907
908 vpd_data = pci_vpd_alloc(dev, size: &vpd_size);
909 if (IS_ERR(ptr: vpd_data)) {
910 pci_warn(dev, "Unable to read VPD\n");
911 return;
912 }
913
914 start = pci_vpd_find_ro_info_keyword(buf: vpd_data, len: vpd_size,
915 PCI_VPD_RO_KEYWORD_PARTNO, size: &kw_len);
916 if (start < 0)
917 pci_err(dev, "Part number not found or incomplete\n");
918 else
919 pci_info(dev, "Part Number : %.*s\n", kw_len, vpd_data + start);
920
921 start = pci_vpd_find_ro_info_keyword(buf: vpd_data, len: vpd_size,
922 PCI_VPD_RO_KEYWORD_SERIALNO, size: &kw_len);
923 if (start < 0)
924 pci_err(dev, "Serial number not found or incomplete\n");
925 else
926 efx->vpd_sn = kmemdup_nul(s: vpd_data + start, len: kw_len, GFP_KERNEL);
927
928 kfree(objp: vpd_data);
929}
930
931
932/* Main body of NIC initialisation
933 * This is called at module load (or hotplug insertion, theoretically).
934 */
935static int efx_pci_probe_main(struct efx_nic *efx)
936{
937 int rc;
938
939 /* Do start-of-day initialisation */
940 rc = efx_probe_all(efx);
941 if (rc)
942 goto fail1;
943
944 efx_init_napi(efx);
945
946 down_write(sem: &efx->filter_sem);
947 rc = efx->type->init(efx);
948 up_write(sem: &efx->filter_sem);
949 if (rc) {
950 pci_err(efx->pci_dev, "failed to initialise NIC\n");
951 goto fail3;
952 }
953
954 rc = efx_init_port(efx);
955 if (rc) {
956 netif_err(efx, probe, efx->net_dev,
957 "failed to initialise port\n");
958 goto fail4;
959 }
960
961 rc = efx_nic_init_interrupt(efx);
962 if (rc)
963 goto fail5;
964
965 efx_set_interrupt_affinity(efx);
966 rc = efx_enable_interrupts(efx);
967 if (rc)
968 goto fail6;
969
970 return 0;
971
972 fail6:
973 efx_clear_interrupt_affinity(efx);
974 efx_nic_fini_interrupt(efx);
975 fail5:
976 efx_fini_port(efx);
977 fail4:
978 efx->type->fini(efx);
979 fail3:
980 efx_fini_napi(efx);
981 efx_remove_all(efx);
982 fail1:
983 return rc;
984}
985
986static int efx_pci_probe_post_io(struct efx_nic *efx)
987{
988 struct net_device *net_dev = efx->net_dev;
989 int rc = efx_pci_probe_main(efx);
990
991 if (rc)
992 return rc;
993
994 if (efx->type->sriov_init) {
995 rc = efx->type->sriov_init(efx);
996 if (rc)
997 pci_err(efx->pci_dev, "SR-IOV can't be enabled rc %d\n",
998 rc);
999 }
1000
1001 /* Determine netdevice features */
1002 net_dev->features |= efx->type->offload_features;
1003
1004 /* Add TSO features */
1005 if (efx->type->tso_versions && efx->type->tso_versions(efx))
1006 net_dev->features |= NETIF_F_TSO | NETIF_F_TSO6;
1007
1008 /* Mask for features that also apply to VLAN devices */
1009 net_dev->vlan_features |= (NETIF_F_HW_CSUM | NETIF_F_SG |
1010 NETIF_F_HIGHDMA | NETIF_F_ALL_TSO |
1011 NETIF_F_RXCSUM);
1012
1013 /* Determine user configurable features */
1014 net_dev->hw_features |= net_dev->features & ~efx->fixed_features;
1015
1016 /* Disable receiving frames with bad FCS, by default. */
1017 net_dev->features &= ~NETIF_F_RXALL;
1018
1019 /* Disable VLAN filtering by default. It may be enforced if
1020 * the feature is fixed (i.e. VLAN filters are required to
1021 * receive VLAN tagged packets due to vPort restrictions).
1022 */
1023 net_dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1024 net_dev->features |= efx->fixed_features;
1025
1026 net_dev->xdp_features = NETDEV_XDP_ACT_BASIC |
1027 NETDEV_XDP_ACT_REDIRECT |
1028 NETDEV_XDP_ACT_NDO_XMIT;
1029
1030 /* devlink creation, registration and lock */
1031 rc = efx_probe_devlink_and_lock(efx);
1032 if (rc)
1033 pci_err(efx->pci_dev, "devlink registration failed");
1034
1035 rc = efx_register_netdev(efx);
1036 efx_probe_devlink_unlock(efx);
1037 if (!rc)
1038 return 0;
1039
1040 efx_pci_remove_main(efx);
1041 return rc;
1042}
1043
1044/* NIC initialisation
1045 *
1046 * This is called at module load (or hotplug insertion,
1047 * theoretically). It sets up PCI mappings, resets the NIC,
1048 * sets up and registers the network devices with the kernel and hooks
1049 * the interrupt service routine. It does not prepare the device for
1050 * transmission; this is left to the first time one of the network
1051 * interfaces is brought up (i.e. efx_net_open).
1052 */
1053static int efx_pci_probe(struct pci_dev *pci_dev,
1054 const struct pci_device_id *entry)
1055{
1056 struct efx_probe_data *probe_data, **probe_ptr;
1057 struct net_device *net_dev;
1058 struct efx_nic *efx;
1059 int rc;
1060
1061 /* Allocate probe data and struct efx_nic */
1062 probe_data = kzalloc(size: sizeof(*probe_data), GFP_KERNEL);
1063 if (!probe_data)
1064 return -ENOMEM;
1065 probe_data->pci_dev = pci_dev;
1066 efx = &probe_data->efx;
1067
1068 /* Allocate and initialise a struct net_device */
1069 net_dev = alloc_etherdev_mq(sizeof(probe_data), EFX_MAX_CORE_TX_QUEUES);
1070 if (!net_dev) {
1071 rc = -ENOMEM;
1072 goto fail0;
1073 }
1074 probe_ptr = netdev_priv(dev: net_dev);
1075 *probe_ptr = probe_data;
1076 efx->net_dev = net_dev;
1077 efx->type = (const struct efx_nic_type *) entry->driver_data;
1078 efx->fixed_features |= NETIF_F_HIGHDMA;
1079
1080 pci_set_drvdata(pdev: pci_dev, data: efx);
1081 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
1082 rc = efx_init_struct(efx, pci_dev);
1083 if (rc)
1084 goto fail1;
1085 efx->mdio.dev = net_dev;
1086
1087 pci_info(pci_dev, "Solarflare NIC detected\n");
1088
1089 if (!efx->type->is_vf)
1090 efx_probe_vpd_strings(efx);
1091
1092 /* Set up basic I/O (BAR mappings etc) */
1093 rc = efx_init_io(efx, bar: efx->type->mem_bar(efx), dma_mask: efx->type->max_dma_mask,
1094 mem_map_size: efx->type->mem_map_size(efx));
1095 if (rc)
1096 goto fail2;
1097
1098 rc = efx_pci_probe_post_io(efx);
1099 if (rc) {
1100 /* On failure, retry once immediately.
1101 * If we aborted probe due to a scheduled reset, dismiss it.
1102 */
1103 efx->reset_pending = 0;
1104 rc = efx_pci_probe_post_io(efx);
1105 if (rc) {
1106 /* On another failure, retry once more
1107 * after a 50-305ms delay.
1108 */
1109 unsigned char r;
1110
1111 get_random_bytes(buf: &r, len: 1);
1112 msleep(msecs: (unsigned int)r + 50);
1113 efx->reset_pending = 0;
1114 rc = efx_pci_probe_post_io(efx);
1115 }
1116 }
1117 if (rc)
1118 goto fail3;
1119
1120 netif_dbg(efx, probe, efx->net_dev, "initialisation successful\n");
1121
1122 /* Try to create MTDs, but allow this to fail */
1123 rtnl_lock();
1124 rc = efx_mtd_probe(efx);
1125 rtnl_unlock();
1126 if (rc && rc != -EPERM)
1127 netif_warn(efx, probe, efx->net_dev,
1128 "failed to create MTDs (%d)\n", rc);
1129
1130 if (efx->type->udp_tnl_push_ports)
1131 efx->type->udp_tnl_push_ports(efx);
1132
1133 return 0;
1134
1135 fail3:
1136 efx_fini_io(efx);
1137 fail2:
1138 efx_fini_struct(efx);
1139 fail1:
1140 WARN_ON(rc > 0);
1141 netif_dbg(efx, drv, efx->net_dev, "initialisation failed. rc=%d\n", rc);
1142 free_netdev(dev: net_dev);
1143 fail0:
1144 kfree(objp: probe_data);
1145 return rc;
1146}
1147
1148/* efx_pci_sriov_configure returns the actual number of Virtual Functions
1149 * enabled on success
1150 */
1151#ifdef CONFIG_SFC_SRIOV
1152static int efx_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
1153{
1154 int rc;
1155 struct efx_nic *efx = pci_get_drvdata(pdev: dev);
1156
1157 if (efx->type->sriov_configure) {
1158 rc = efx->type->sriov_configure(efx, num_vfs);
1159 if (rc)
1160 return rc;
1161 else
1162 return num_vfs;
1163 } else
1164 return -EOPNOTSUPP;
1165}
1166#endif
1167
1168static int efx_pm_freeze(struct device *dev)
1169{
1170 struct efx_nic *efx = dev_get_drvdata(dev);
1171
1172 rtnl_lock();
1173
1174 if (efx_net_active(state: efx->state)) {
1175 efx_device_detach_sync(efx);
1176
1177 efx_stop_all(efx);
1178 efx_disable_interrupts(efx);
1179
1180 efx->state = efx_freeze(state: efx->state);
1181 }
1182
1183 rtnl_unlock();
1184
1185 return 0;
1186}
1187
1188static void efx_pci_shutdown(struct pci_dev *pci_dev)
1189{
1190 struct efx_nic *efx = pci_get_drvdata(pdev: pci_dev);
1191
1192 if (!efx)
1193 return;
1194
1195 efx_pm_freeze(dev: &pci_dev->dev);
1196 pci_disable_device(dev: pci_dev);
1197}
1198
1199static int efx_pm_thaw(struct device *dev)
1200{
1201 int rc;
1202 struct efx_nic *efx = dev_get_drvdata(dev);
1203
1204 rtnl_lock();
1205
1206 if (efx_frozen(state: efx->state)) {
1207 rc = efx_enable_interrupts(efx);
1208 if (rc)
1209 goto fail;
1210
1211 mutex_lock(&efx->mac_lock);
1212 efx_mcdi_port_reconfigure(efx);
1213 mutex_unlock(lock: &efx->mac_lock);
1214
1215 efx_start_all(efx);
1216
1217 efx_device_attach_if_not_resetting(efx);
1218
1219 efx->state = efx_thaw(state: efx->state);
1220
1221 efx->type->resume_wol(efx);
1222 }
1223
1224 rtnl_unlock();
1225
1226 /* Reschedule any quenched resets scheduled during efx_pm_freeze() */
1227 efx_queue_reset_work(efx);
1228
1229 return 0;
1230
1231fail:
1232 rtnl_unlock();
1233
1234 return rc;
1235}
1236
1237static int efx_pm_poweroff(struct device *dev)
1238{
1239 struct pci_dev *pci_dev = to_pci_dev(dev);
1240 struct efx_nic *efx = pci_get_drvdata(pdev: pci_dev);
1241
1242 efx->type->fini(efx);
1243
1244 efx->reset_pending = 0;
1245
1246 pci_save_state(dev: pci_dev);
1247 return pci_set_power_state(dev: pci_dev, PCI_D3hot);
1248}
1249
1250/* Used for both resume and restore */
1251static int efx_pm_resume(struct device *dev)
1252{
1253 struct pci_dev *pci_dev = to_pci_dev(dev);
1254 struct efx_nic *efx = pci_get_drvdata(pdev: pci_dev);
1255 int rc;
1256
1257 rc = pci_set_power_state(dev: pci_dev, PCI_D0);
1258 if (rc)
1259 return rc;
1260 pci_restore_state(dev: pci_dev);
1261 rc = pci_enable_device(dev: pci_dev);
1262 if (rc)
1263 return rc;
1264 pci_set_master(dev: efx->pci_dev);
1265 rc = efx->type->reset(efx, RESET_TYPE_ALL);
1266 if (rc)
1267 return rc;
1268 down_write(sem: &efx->filter_sem);
1269 rc = efx->type->init(efx);
1270 up_write(sem: &efx->filter_sem);
1271 if (rc)
1272 return rc;
1273 rc = efx_pm_thaw(dev);
1274 return rc;
1275}
1276
1277static int efx_pm_suspend(struct device *dev)
1278{
1279 int rc;
1280
1281 efx_pm_freeze(dev);
1282 rc = efx_pm_poweroff(dev);
1283 if (rc)
1284 efx_pm_resume(dev);
1285 return rc;
1286}
1287
1288static const struct dev_pm_ops efx_pm_ops = {
1289 .suspend = efx_pm_suspend,
1290 .resume = efx_pm_resume,
1291 .freeze = efx_pm_freeze,
1292 .thaw = efx_pm_thaw,
1293 .poweroff = efx_pm_poweroff,
1294 .restore = efx_pm_resume,
1295};
1296
1297static struct pci_driver efx_pci_driver = {
1298 .name = KBUILD_MODNAME,
1299 .id_table = efx_pci_table,
1300 .probe = efx_pci_probe,
1301 .remove = efx_pci_remove,
1302 .driver.pm = &efx_pm_ops,
1303 .shutdown = efx_pci_shutdown,
1304 .err_handler = &efx_err_handlers,
1305#ifdef CONFIG_SFC_SRIOV
1306 .sriov_configure = efx_pci_sriov_configure,
1307#endif
1308};
1309
1310/**************************************************************************
1311 *
1312 * Kernel module interface
1313 *
1314 *************************************************************************/
1315
1316static int __init efx_init_module(void)
1317{
1318 int rc;
1319
1320 printk(KERN_INFO "Solarflare NET driver\n");
1321
1322 rc = register_netdevice_notifier(nb: &efx_netdev_notifier);
1323 if (rc)
1324 goto err_notifier;
1325
1326 rc = efx_create_reset_workqueue();
1327 if (rc)
1328 goto err_reset;
1329
1330 rc = pci_register_driver(&efx_pci_driver);
1331 if (rc < 0)
1332 goto err_pci;
1333
1334 rc = pci_register_driver(&ef100_pci_driver);
1335 if (rc < 0)
1336 goto err_pci_ef100;
1337
1338 return 0;
1339
1340 err_pci_ef100:
1341 pci_unregister_driver(dev: &efx_pci_driver);
1342 err_pci:
1343 efx_destroy_reset_workqueue();
1344 err_reset:
1345 unregister_netdevice_notifier(nb: &efx_netdev_notifier);
1346 err_notifier:
1347 return rc;
1348}
1349
1350static void __exit efx_exit_module(void)
1351{
1352 printk(KERN_INFO "Solarflare NET driver unloading\n");
1353
1354 pci_unregister_driver(dev: &ef100_pci_driver);
1355 pci_unregister_driver(dev: &efx_pci_driver);
1356 efx_destroy_reset_workqueue();
1357 unregister_netdevice_notifier(nb: &efx_netdev_notifier);
1358
1359}
1360
1361module_init(efx_init_module);
1362module_exit(efx_exit_module);
1363
1364MODULE_AUTHOR("Solarflare Communications and "
1365 "Michael Brown <mbrown@fensystems.co.uk>");
1366MODULE_DESCRIPTION("Solarflare network driver");
1367MODULE_LICENSE("GPL");
1368MODULE_DEVICE_TABLE(pci, efx_pci_table);
1369

source code of linux/drivers/net/ethernet/sfc/efx.c