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_var.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
56 #include "common/efx.h"
61 #define SFXGE_CAP (IFCAP_VLAN_MTU | \
62 IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM | IFCAP_TSO | \
63 IFCAP_JUMBO_MTU | IFCAP_LRO | \
64 IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE)
65 #define SFXGE_CAP_ENABLE SFXGE_CAP
66 #define SFXGE_CAP_FIXED (IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM | \
67 IFCAP_JUMBO_MTU | IFCAP_LINKSTATE)
69 MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver");
72 SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD, 0,
73 "SFXGE driver parameters");
75 #define SFXGE_PARAM_RX_RING SFXGE_PARAM(rx_ring)
76 static int sfxge_rx_ring_entries = SFXGE_NDESCS;
77 TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries);
78 SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN,
79 &sfxge_rx_ring_entries, 0,
80 "Maximum number of descriptors in a receive ring");
82 #define SFXGE_PARAM_TX_RING SFXGE_PARAM(tx_ring)
83 static int sfxge_tx_ring_entries = SFXGE_NDESCS;
84 TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries);
85 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN,
86 &sfxge_tx_ring_entries, 0,
87 "Maximum number of descriptors in a transmit ring");
91 sfxge_reset(void *arg, int npending);
94 sfxge_start(struct sfxge_softc *sc)
98 sx_assert(&sc->softc_lock, LA_XLOCKED);
100 if (sc->init_state == SFXGE_STARTED)
103 if (sc->init_state != SFXGE_REGISTERED) {
108 if ((rc = efx_nic_init(sc->enp)) != 0)
111 /* Start processing interrupts. */
112 if ((rc = sfxge_intr_start(sc)) != 0)
115 /* Start processing events. */
116 if ((rc = sfxge_ev_start(sc)) != 0)
119 /* Start the receiver side. */
120 if ((rc = sfxge_rx_start(sc)) != 0)
123 /* Start the transmitter side. */
124 if ((rc = sfxge_tx_start(sc)) != 0)
127 /* Fire up the port. */
128 if ((rc = sfxge_port_start(sc)) != 0)
131 sc->init_state = SFXGE_STARTED;
133 /* Tell the stack we're running. */
134 sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
135 sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
152 efx_nic_fini(sc->enp);
155 device_printf(sc->dev, "sfxge_start: %d\n", rc);
161 sfxge_if_init(void *arg)
163 struct sfxge_softc *sc;
165 sc = (struct sfxge_softc *)arg;
167 sx_xlock(&sc->softc_lock);
168 (void)sfxge_start(sc);
169 sx_xunlock(&sc->softc_lock);
173 sfxge_stop(struct sfxge_softc *sc)
175 sx_assert(&sc->softc_lock, LA_XLOCKED);
177 if (sc->init_state != SFXGE_STARTED)
180 sc->init_state = SFXGE_REGISTERED;
185 /* Stop the transmitter. */
188 /* Stop the receiver. */
191 /* Stop processing events. */
194 /* Stop processing interrupts. */
197 efx_nic_fini(sc->enp);
199 sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
203 sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
205 struct sfxge_softc *sc;
209 ifr = (struct ifreq *)data;
215 sx_xlock(&sc->softc_lock);
216 if (ifp->if_flags & IFF_UP) {
217 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
218 if ((ifp->if_flags ^ sc->if_flags) &
219 (IFF_PROMISC | IFF_ALLMULTI)) {
220 sfxge_mac_filter_set(sc);
225 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
227 sc->if_flags = ifp->if_flags;
228 sx_xunlock(&sc->softc_lock);
231 if (ifr->ifr_mtu == ifp->if_mtu) {
234 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
236 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
237 ifp->if_mtu = ifr->ifr_mtu;
240 /* Restart required */
241 sx_xlock(&sc->softc_lock);
243 ifp->if_mtu = ifr->ifr_mtu;
244 error = sfxge_start(sc);
245 sx_xunlock(&sc->softc_lock);
247 ifp->if_flags &= ~IFF_UP;
248 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
255 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
256 sfxge_mac_filter_set(sc);
259 sx_xlock(&sc->softc_lock);
262 * The networking core already rejects attempts to
263 * enable capabilities we don't have. We still have
264 * to reject attempts to disable capabilities that we
265 * can't (yet) disable.
267 if (~ifr->ifr_reqcap & SFXGE_CAP_FIXED) {
269 sx_xunlock(&sc->softc_lock);
273 ifp->if_capenable = ifr->ifr_reqcap;
274 if (ifp->if_capenable & IFCAP_TXCSUM)
275 ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
277 ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
278 if (ifp->if_capenable & IFCAP_TSO)
279 ifp->if_hwassist |= CSUM_TSO;
281 ifp->if_hwassist &= ~CSUM_TSO;
283 sx_xunlock(&sc->softc_lock);
287 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
290 error = ether_ioctl(ifp, command, data);
297 sfxge_ifnet_fini(struct ifnet *ifp)
299 struct sfxge_softc *sc = ifp->if_softc;
301 sx_xlock(&sc->softc_lock);
303 sx_xunlock(&sc->softc_lock);
305 ifmedia_removeall(&sc->media);
311 sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
313 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
320 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
321 ifp->if_init = sfxge_if_init;
323 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
324 ifp->if_ioctl = sfxge_if_ioctl;
326 ifp->if_capabilities = SFXGE_CAP;
327 ifp->if_capenable = SFXGE_CAP_ENABLE;
328 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO;
330 ether_ifattach(ifp, encp->enc_mac_addr);
333 ifp->if_transmit = sfxge_if_transmit;
334 ifp->if_qflush = sfxge_if_qflush;
336 ifp->if_start = sfxge_if_start;
337 IFQ_SET_MAXLEN(&ifp->if_snd, sc->txq_entries - 1);
338 ifp->if_snd.ifq_drv_maxlen = sc->txq_entries - 1;
339 IFQ_SET_READY(&ifp->if_snd);
341 mtx_init(&sc->tx_lock, "txq", NULL, MTX_DEF);
344 if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
350 ether_ifdetach(sc->ifnet);
355 sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
357 KASSERT(sc->buffer_table_next + n <=
358 efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
359 ("buffer table full"));
361 *idp = sc->buffer_table_next;
362 sc->buffer_table_next += n;
366 sfxge_bar_init(struct sfxge_softc *sc)
368 efsys_bar_t *esbp = &sc->bar;
370 esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR);
371 if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
372 &esbp->esb_rid, RF_ACTIVE)) == NULL) {
373 device_printf(sc->dev, "Cannot allocate BAR region %d\n",
377 esbp->esb_tag = rman_get_bustag(esbp->esb_res);
378 esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
379 mtx_init(&esbp->esb_lock, "sfxge_efsys_bar", NULL, MTX_DEF);
385 sfxge_bar_fini(struct sfxge_softc *sc)
387 efsys_bar_t *esbp = &sc->bar;
389 bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
391 mtx_destroy(&esbp->esb_lock);
395 sfxge_create(struct sfxge_softc *sc)
403 sx_init(&sc->softc_lock, "sfxge_softc");
405 sc->stats_node = SYSCTL_ADD_NODE(
406 device_get_sysctl_ctx(dev),
407 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
408 OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics");
409 if (sc->stats_node == NULL) {
414 TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
416 (void) pci_enable_busmaster(dev);
418 /* Initialize DMA mappings. */
419 if ((error = sfxge_dma_init(sc)) != 0)
422 /* Map the device registers. */
423 if ((error = sfxge_bar_init(sc)) != 0)
426 error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
428 KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
430 /* Create the common code nic object. */
431 mtx_init(&sc->enp_lock, "sfxge_nic", NULL, MTX_DEF);
432 if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
433 &sc->bar, &sc->enp_lock, &enp)) != 0)
437 if (!ISP2(sfxge_rx_ring_entries) ||
438 !(sfxge_rx_ring_entries & EFX_RXQ_NDESCS_MASK)) {
439 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
440 SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries,
441 EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS);
443 goto fail_rx_ring_entries;
445 sc->rxq_entries = sfxge_rx_ring_entries;
447 if (!ISP2(sfxge_tx_ring_entries) ||
448 !(sfxge_tx_ring_entries & EFX_TXQ_NDESCS_MASK)) {
449 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
450 SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries,
451 EFX_TXQ_MINNDESCS, EFX_TXQ_MAXNDESCS);
453 goto fail_tx_ring_entries;
455 sc->txq_entries = sfxge_tx_ring_entries;
457 /* Initialize MCDI to talk to the microcontroller. */
458 if ((error = sfxge_mcdi_init(sc)) != 0)
461 /* Probe the NIC and build the configuration data area. */
462 if ((error = efx_nic_probe(enp)) != 0)
465 /* Initialize the NVRAM. */
466 if ((error = efx_nvram_init(enp)) != 0)
469 /* Initialize the VPD. */
470 if ((error = efx_vpd_init(enp)) != 0)
474 if ((error = efx_nic_reset(enp)) != 0)
477 /* Initialize buffer table allocation. */
478 sc->buffer_table_next = 0;
480 /* Set up interrupts. */
481 if ((error = sfxge_intr_init(sc)) != 0)
484 /* Initialize event processing state. */
485 if ((error = sfxge_ev_init(sc)) != 0)
488 /* Initialize receive state. */
489 if ((error = sfxge_rx_init(sc)) != 0)
492 /* Initialize transmit state. */
493 if ((error = sfxge_tx_init(sc)) != 0)
496 /* Initialize port state. */
497 if ((error = sfxge_port_init(sc)) != 0)
500 sc->init_state = SFXGE_INITIALIZED;
523 efx_nic_unprobe(enp);
529 fail_tx_ring_entries:
530 fail_rx_ring_entries:
532 efx_nic_destroy(enp);
533 mtx_destroy(&sc->enp_lock);
537 (void) pci_disable_busmaster(sc->dev);
541 sx_destroy(&sc->softc_lock);
546 sfxge_destroy(struct sfxge_softc *sc)
550 /* Clean up port state. */
553 /* Clean up transmit state. */
556 /* Clean up receive state. */
559 /* Clean up event processing state. */
562 /* Clean up interrupts. */
565 /* Tear down common code subsystems. */
566 efx_nic_reset(sc->enp);
567 efx_vpd_fini(sc->enp);
568 efx_nvram_fini(sc->enp);
569 efx_nic_unprobe(sc->enp);
571 /* Tear down MCDI. */
574 /* Destroy common code context. */
577 efx_nic_destroy(enp);
579 /* Free DMA memory. */
582 /* Free mapped BARs. */
585 (void) pci_disable_busmaster(sc->dev);
587 taskqueue_drain(taskqueue_thread, &sc->task_reset);
589 /* Destroy the softc lock. */
590 sx_destroy(&sc->softc_lock);
594 sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
596 struct sfxge_softc *sc = arg1;
597 efx_vpd_value_t value;
600 value.evv_tag = arg2 >> 16;
601 value.evv_keyword = arg2 & 0xffff;
602 if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
606 return (SYSCTL_OUT(req, value.evv_value, value.evv_length));
610 sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
611 efx_vpd_tag_t tag, const char *keyword)
613 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
614 efx_vpd_value_t value;
616 /* Check whether VPD tag/keyword is present */
618 value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
619 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
623 ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
624 sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
625 sfxge_vpd_handler, "A", "");
629 sfxge_vpd_init(struct sfxge_softc *sc)
631 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
632 struct sysctl_oid *vpd_node;
633 struct sysctl_oid_list *vpd_list;
635 efx_vpd_value_t value;
638 if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0)
640 sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
641 if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
644 /* Copy ID (product name) into device description, and log it. */
645 value.evv_tag = EFX_VPD_ID;
646 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
647 value.evv_value[value.evv_length] = 0;
648 device_set_desc_copy(sc->dev, value.evv_value);
649 device_printf(sc->dev, "%s\n", value.evv_value);
652 vpd_node = SYSCTL_ADD_NODE(
653 ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
654 OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
655 vpd_list = SYSCTL_CHILDREN(vpd_node);
657 /* Add sysctls for all expected and any vendor-defined keywords. */
658 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
659 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
660 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
663 for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
664 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
665 for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
666 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
671 free(sc->vpd_data, M_SFXGE);
677 sfxge_vpd_fini(struct sfxge_softc *sc)
679 free(sc->vpd_data, M_SFXGE);
683 sfxge_reset(void *arg, int npending)
685 struct sfxge_softc *sc;
690 sc = (struct sfxge_softc *)arg;
692 sx_xlock(&sc->softc_lock);
694 if (sc->init_state != SFXGE_STARTED)
698 efx_nic_reset(sc->enp);
699 if ((rc = sfxge_start(sc)) != 0)
700 device_printf(sc->dev,
701 "reset failed (%d); interface is now stopped\n",
705 sx_xunlock(&sc->softc_lock);
709 sfxge_schedule_reset(struct sfxge_softc *sc)
711 taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
715 sfxge_attach(device_t dev)
717 struct sfxge_softc *sc;
721 sc = device_get_softc(dev);
724 /* Allocate ifnet. */
725 ifp = if_alloc(IFT_ETHER);
727 device_printf(dev, "Couldn't allocate ifnet\n");
733 /* Initialize hardware. */
734 if ((error = sfxge_create(sc)) != 0)
737 /* Create the ifnet for the port. */
738 if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
741 if ((error = sfxge_vpd_init(sc)) != 0)
744 sc->init_state = SFXGE_REGISTERED;
749 sfxge_ifnet_fini(ifp);
761 sfxge_detach(device_t dev)
763 struct sfxge_softc *sc;
765 sc = device_get_softc(dev);
769 /* Destroy the ifnet. */
770 sfxge_ifnet_fini(sc->ifnet);
772 /* Tear down hardware. */
779 sfxge_probe(device_t dev)
781 uint16_t pci_vendor_id;
782 uint16_t pci_device_id;
786 pci_vendor_id = pci_get_vendor(dev);
787 pci_device_id = pci_get_device(dev);
789 rc = efx_family(pci_vendor_id, pci_device_id, &family);
793 KASSERT(family == EFX_FAMILY_SIENA, ("impossible controller family"));
794 device_set_desc(dev, "Solarflare SFC9000 family");
798 static device_method_t sfxge_methods[] = {
799 DEVMETHOD(device_probe, sfxge_probe),
800 DEVMETHOD(device_attach, sfxge_attach),
801 DEVMETHOD(device_detach, sfxge_detach),
806 static devclass_t sfxge_devclass;
808 static driver_t sfxge_driver = {
811 sizeof(struct sfxge_softc)
814 DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);