2 * Copyright (c) 2010-2011 Solarflare Communications, Inc.
5 * This software was developed in part by Philip Paeps under contract for
6 * Solarflare Communications, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
41 #include <sys/socket.h>
42 #include <sys/taskqueue.h>
43 #include <sys/sockio.h>
44 #include <sys/sysctl.h>
45 #include <sys/syslog.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
50 #include <net/ethernet.h>
52 #include <net/if_media.h>
53 #include <net/if_types.h>
55 #include "common/efx.h"
59 #include "sfxge_version.h"
61 #define SFXGE_CAP (IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM | \
62 IFCAP_RXCSUM | IFCAP_TXCSUM | IFCAP_TSO | \
63 IFCAP_RXCSUM_IPV6 | IFCAP_TXCSUM_IPV6 | \
64 IFCAP_JUMBO_MTU | IFCAP_LRO | \
65 IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE)
66 #define SFXGE_CAP_ENABLE SFXGE_CAP
67 #define SFXGE_CAP_FIXED (IFCAP_VLAN_MTU | IFCAP_RXCSUM | IFCAP_VLAN_HWCSUM | \
69 IFCAP_JUMBO_MTU | IFCAP_LINKSTATE)
71 MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver");
74 SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD, 0,
75 "SFXGE driver parameters");
77 #define SFXGE_PARAM_RX_RING SFXGE_PARAM(rx_ring)
78 static int sfxge_rx_ring_entries = SFXGE_NDESCS;
79 TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries);
80 SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN,
81 &sfxge_rx_ring_entries, 0,
82 "Maximum number of descriptors in a receive ring");
84 #define SFXGE_PARAM_TX_RING SFXGE_PARAM(tx_ring)
85 static int sfxge_tx_ring_entries = SFXGE_NDESCS;
86 TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries);
87 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN,
88 &sfxge_tx_ring_entries, 0,
89 "Maximum number of descriptors in a transmit ring");
93 sfxge_reset(void *arg, int npending);
96 sfxge_start(struct sfxge_softc *sc)
100 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
102 if (sc->init_state == SFXGE_STARTED)
105 if (sc->init_state != SFXGE_REGISTERED) {
110 if ((rc = efx_nic_init(sc->enp)) != 0)
113 /* Start processing interrupts. */
114 if ((rc = sfxge_intr_start(sc)) != 0)
117 /* Start processing events. */
118 if ((rc = sfxge_ev_start(sc)) != 0)
121 /* Start the receiver side. */
122 if ((rc = sfxge_rx_start(sc)) != 0)
125 /* Start the transmitter side. */
126 if ((rc = sfxge_tx_start(sc)) != 0)
129 /* Fire up the port. */
130 if ((rc = sfxge_port_start(sc)) != 0)
133 sc->init_state = SFXGE_STARTED;
135 /* Tell the stack we're running. */
136 sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
137 sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
154 efx_nic_fini(sc->enp);
157 device_printf(sc->dev, "sfxge_start: %d\n", rc);
163 sfxge_if_init(void *arg)
165 struct sfxge_softc *sc;
167 sc = (struct sfxge_softc *)arg;
169 SFXGE_ADAPTER_LOCK(sc);
170 (void)sfxge_start(sc);
171 SFXGE_ADAPTER_UNLOCK(sc);
175 sfxge_stop(struct sfxge_softc *sc)
177 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
179 if (sc->init_state != SFXGE_STARTED)
182 sc->init_state = SFXGE_REGISTERED;
187 /* Stop the transmitter. */
190 /* Stop the receiver. */
193 /* Stop processing events. */
196 /* Stop processing interrupts. */
199 efx_nic_fini(sc->enp);
201 sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
205 sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
207 struct sfxge_softc *sc;
211 ifr = (struct ifreq *)data;
217 SFXGE_ADAPTER_LOCK(sc);
218 if (ifp->if_flags & IFF_UP) {
219 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
220 if ((ifp->if_flags ^ sc->if_flags) &
221 (IFF_PROMISC | IFF_ALLMULTI)) {
222 sfxge_mac_filter_set(sc);
227 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
229 sc->if_flags = ifp->if_flags;
230 SFXGE_ADAPTER_UNLOCK(sc);
233 if (ifr->ifr_mtu == ifp->if_mtu) {
236 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
238 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
239 ifp->if_mtu = ifr->ifr_mtu;
242 /* Restart required */
243 SFXGE_ADAPTER_LOCK(sc);
245 ifp->if_mtu = ifr->ifr_mtu;
246 error = sfxge_start(sc);
247 SFXGE_ADAPTER_UNLOCK(sc);
249 ifp->if_flags &= ~IFF_UP;
250 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
257 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
258 sfxge_mac_filter_set(sc);
261 SFXGE_ADAPTER_LOCK(sc);
264 * The networking core already rejects attempts to
265 * enable capabilities we don't have. We still have
266 * to reject attempts to disable capabilities that we
267 * can't (yet) disable.
269 if (~ifr->ifr_reqcap & SFXGE_CAP_FIXED) {
271 SFXGE_ADAPTER_UNLOCK(sc);
275 ifp->if_capenable = ifr->ifr_reqcap;
276 if (ifp->if_capenable & IFCAP_TXCSUM)
277 ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
279 ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
280 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
281 ifp->if_hwassist |= (CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
283 ifp->if_hwassist &= ~(CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
284 if (ifp->if_capenable & IFCAP_TSO)
285 ifp->if_hwassist |= CSUM_TSO;
287 ifp->if_hwassist &= ~CSUM_TSO;
289 SFXGE_ADAPTER_UNLOCK(sc);
293 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
296 error = ether_ioctl(ifp, command, data);
303 sfxge_ifnet_fini(struct ifnet *ifp)
305 struct sfxge_softc *sc = ifp->if_softc;
307 SFXGE_ADAPTER_LOCK(sc);
309 SFXGE_ADAPTER_UNLOCK(sc);
311 ifmedia_removeall(&sc->media);
317 sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
319 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
326 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
327 ifp->if_init = sfxge_if_init;
329 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
330 ifp->if_ioctl = sfxge_if_ioctl;
332 ifp->if_capabilities = SFXGE_CAP;
333 ifp->if_capenable = SFXGE_CAP_ENABLE;
334 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
335 CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
337 ether_ifattach(ifp, encp->enc_mac_addr);
339 ifp->if_transmit = sfxge_if_transmit;
340 ifp->if_qflush = sfxge_if_qflush;
342 if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
348 ether_ifdetach(sc->ifnet);
353 sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
355 KASSERT(sc->buffer_table_next + n <=
356 efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
357 ("buffer table full"));
359 *idp = sc->buffer_table_next;
360 sc->buffer_table_next += n;
364 sfxge_bar_init(struct sfxge_softc *sc)
366 efsys_bar_t *esbp = &sc->bar;
368 esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR);
369 if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
370 &esbp->esb_rid, RF_ACTIVE)) == NULL) {
371 device_printf(sc->dev, "Cannot allocate BAR region %d\n",
375 esbp->esb_tag = rman_get_bustag(esbp->esb_res);
376 esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
378 SFXGE_BAR_LOCK_INIT(esbp, device_get_nameunit(sc->dev));
384 sfxge_bar_fini(struct sfxge_softc *sc)
386 efsys_bar_t *esbp = &sc->bar;
388 bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
390 SFXGE_BAR_LOCK_DESTROY(esbp);
394 sfxge_create(struct sfxge_softc *sc)
399 char rss_param_name[sizeof(SFXGE_PARAM(%d.max_rss_channels))];
403 SFXGE_ADAPTER_LOCK_INIT(sc, device_get_nameunit(sc->dev));
405 sc->max_rss_channels = 0;
406 snprintf(rss_param_name, sizeof(rss_param_name),
407 SFXGE_PARAM(%d.max_rss_channels),
408 (int)device_get_unit(dev));
409 TUNABLE_INT_FETCH(rss_param_name, &sc->max_rss_channels);
411 sc->stats_node = SYSCTL_ADD_NODE(
412 device_get_sysctl_ctx(dev),
413 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
414 OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics");
415 if (sc->stats_node == NULL) {
420 TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
422 (void) pci_enable_busmaster(dev);
424 /* Initialize DMA mappings. */
425 if ((error = sfxge_dma_init(sc)) != 0)
428 /* Map the device registers. */
429 if ((error = sfxge_bar_init(sc)) != 0)
432 error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
434 KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
436 /* Create the common code nic object. */
437 SFXGE_EFSYS_LOCK_INIT(&sc->enp_lock,
438 device_get_nameunit(sc->dev), "nic");
439 if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
440 &sc->bar, &sc->enp_lock, &enp)) != 0)
444 if (!ISP2(sfxge_rx_ring_entries) ||
445 !(sfxge_rx_ring_entries & EFX_RXQ_NDESCS_MASK)) {
446 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
447 SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries,
448 EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS);
450 goto fail_rx_ring_entries;
452 sc->rxq_entries = sfxge_rx_ring_entries;
454 if (!ISP2(sfxge_tx_ring_entries) ||
455 !(sfxge_tx_ring_entries & EFX_TXQ_NDESCS_MASK)) {
456 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
457 SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries,
458 EFX_TXQ_MINNDESCS, EFX_TXQ_MAXNDESCS);
460 goto fail_tx_ring_entries;
462 sc->txq_entries = sfxge_tx_ring_entries;
464 /* Initialize MCDI to talk to the microcontroller. */
465 if ((error = sfxge_mcdi_init(sc)) != 0)
468 /* Probe the NIC and build the configuration data area. */
469 if ((error = efx_nic_probe(enp)) != 0)
472 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
473 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
474 OID_AUTO, "version", CTLFLAG_RD,
475 SFXGE_VERSION_STRING, 0,
478 /* Initialize the NVRAM. */
479 if ((error = efx_nvram_init(enp)) != 0)
482 /* Initialize the VPD. */
483 if ((error = efx_vpd_init(enp)) != 0)
487 if ((error = efx_nic_reset(enp)) != 0)
490 /* Initialize buffer table allocation. */
491 sc->buffer_table_next = 0;
493 /* Set up interrupts. */
494 if ((error = sfxge_intr_init(sc)) != 0)
497 /* Initialize event processing state. */
498 if ((error = sfxge_ev_init(sc)) != 0)
501 /* Initialize receive state. */
502 if ((error = sfxge_rx_init(sc)) != 0)
505 /* Initialize transmit state. */
506 if ((error = sfxge_tx_init(sc)) != 0)
509 /* Initialize port state. */
510 if ((error = sfxge_port_init(sc)) != 0)
513 sc->init_state = SFXGE_INITIALIZED;
536 efx_nic_unprobe(enp);
542 fail_tx_ring_entries:
543 fail_rx_ring_entries:
545 efx_nic_destroy(enp);
546 SFXGE_EFSYS_LOCK_DESTROY(&sc->enp_lock);
550 (void) pci_disable_busmaster(sc->dev);
554 SFXGE_ADAPTER_LOCK_DESTROY(sc);
559 sfxge_destroy(struct sfxge_softc *sc)
563 /* Clean up port state. */
566 /* Clean up transmit state. */
569 /* Clean up receive state. */
572 /* Clean up event processing state. */
575 /* Clean up interrupts. */
578 /* Tear down common code subsystems. */
579 efx_nic_reset(sc->enp);
580 efx_vpd_fini(sc->enp);
581 efx_nvram_fini(sc->enp);
582 efx_nic_unprobe(sc->enp);
584 /* Tear down MCDI. */
587 /* Destroy common code context. */
590 efx_nic_destroy(enp);
592 /* Free DMA memory. */
595 /* Free mapped BARs. */
598 (void) pci_disable_busmaster(sc->dev);
600 taskqueue_drain(taskqueue_thread, &sc->task_reset);
602 /* Destroy the softc lock. */
603 SFXGE_ADAPTER_LOCK_DESTROY(sc);
607 sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
609 struct sfxge_softc *sc = arg1;
610 efx_vpd_value_t value;
613 value.evv_tag = arg2 >> 16;
614 value.evv_keyword = arg2 & 0xffff;
615 if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
619 return (SYSCTL_OUT(req, value.evv_value, value.evv_length));
623 sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
624 efx_vpd_tag_t tag, const char *keyword)
626 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
627 efx_vpd_value_t value;
629 /* Check whether VPD tag/keyword is present */
631 value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
632 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
636 ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
637 sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
638 sfxge_vpd_handler, "A", "");
642 sfxge_vpd_init(struct sfxge_softc *sc)
644 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
645 struct sysctl_oid *vpd_node;
646 struct sysctl_oid_list *vpd_list;
648 efx_vpd_value_t value;
651 if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0)
653 sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
654 if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
657 /* Copy ID (product name) into device description, and log it. */
658 value.evv_tag = EFX_VPD_ID;
659 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
660 value.evv_value[value.evv_length] = 0;
661 device_set_desc_copy(sc->dev, value.evv_value);
662 device_printf(sc->dev, "%s\n", value.evv_value);
665 vpd_node = SYSCTL_ADD_NODE(
666 ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
667 OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
668 vpd_list = SYSCTL_CHILDREN(vpd_node);
670 /* Add sysctls for all expected and any vendor-defined keywords. */
671 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
672 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
673 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
676 for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
677 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
678 for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
679 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
684 free(sc->vpd_data, M_SFXGE);
690 sfxge_vpd_fini(struct sfxge_softc *sc)
692 free(sc->vpd_data, M_SFXGE);
696 sfxge_reset(void *arg, int npending)
698 struct sfxge_softc *sc;
703 sc = (struct sfxge_softc *)arg;
705 SFXGE_ADAPTER_LOCK(sc);
707 if (sc->init_state != SFXGE_STARTED)
711 efx_nic_reset(sc->enp);
712 if ((rc = sfxge_start(sc)) != 0)
713 device_printf(sc->dev,
714 "reset failed (%d); interface is now stopped\n",
718 SFXGE_ADAPTER_UNLOCK(sc);
722 sfxge_schedule_reset(struct sfxge_softc *sc)
724 taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
728 sfxge_attach(device_t dev)
730 struct sfxge_softc *sc;
734 sc = device_get_softc(dev);
737 /* Allocate ifnet. */
738 ifp = if_alloc(IFT_ETHER);
740 device_printf(dev, "Couldn't allocate ifnet\n");
746 /* Initialize hardware. */
747 if ((error = sfxge_create(sc)) != 0)
750 /* Create the ifnet for the port. */
751 if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
754 if ((error = sfxge_vpd_init(sc)) != 0)
757 sc->init_state = SFXGE_REGISTERED;
762 sfxge_ifnet_fini(ifp);
774 sfxge_detach(device_t dev)
776 struct sfxge_softc *sc;
778 sc = device_get_softc(dev);
782 /* Destroy the ifnet. */
783 sfxge_ifnet_fini(sc->ifnet);
785 /* Tear down hardware. */
792 sfxge_probe(device_t dev)
794 uint16_t pci_vendor_id;
795 uint16_t pci_device_id;
799 pci_vendor_id = pci_get_vendor(dev);
800 pci_device_id = pci_get_device(dev);
802 rc = efx_family(pci_vendor_id, pci_device_id, &family);
806 KASSERT(family == EFX_FAMILY_SIENA, ("impossible controller family"));
807 device_set_desc(dev, "Solarflare SFC9000 family");
811 static device_method_t sfxge_methods[] = {
812 DEVMETHOD(device_probe, sfxge_probe),
813 DEVMETHOD(device_attach, sfxge_attach),
814 DEVMETHOD(device_detach, sfxge_detach),
819 static devclass_t sfxge_devclass;
821 static driver_t sfxge_driver = {
824 sizeof(struct sfxge_softc)
827 DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);