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>
46 #include <dev/pci/pcireg.h>
47 #include <dev/pci/pcivar.h>
49 #include <net/ethernet.h>
51 #include <net/if_media.h>
52 #include <net/if_types.h>
54 #include "common/efx.h"
59 #define SFXGE_CAP (IFCAP_VLAN_MTU | \
60 IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM | IFCAP_TSO | \
61 IFCAP_JUMBO_MTU | IFCAP_LRO | \
62 IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE)
63 #define SFXGE_CAP_ENABLE SFXGE_CAP
64 #define SFXGE_CAP_FIXED (IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM | \
65 IFCAP_JUMBO_MTU | IFCAP_LINKSTATE)
67 MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver");
70 sfxge_reset(void *arg, int npending);
73 sfxge_start(struct sfxge_softc *sc)
77 sx_assert(&sc->softc_lock, LA_XLOCKED);
79 if (sc->init_state == SFXGE_STARTED)
82 if (sc->init_state != SFXGE_REGISTERED) {
87 if ((rc = efx_nic_init(sc->enp)) != 0)
90 /* Start processing interrupts. */
91 if ((rc = sfxge_intr_start(sc)) != 0)
94 /* Start processing events. */
95 if ((rc = sfxge_ev_start(sc)) != 0)
98 /* Start the receiver side. */
99 if ((rc = sfxge_rx_start(sc)) != 0)
102 /* Start the transmitter side. */
103 if ((rc = sfxge_tx_start(sc)) != 0)
106 /* Fire up the port. */
107 if ((rc = sfxge_port_start(sc)) != 0)
110 sc->init_state = SFXGE_STARTED;
112 /* Tell the stack we're running. */
113 sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
114 sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
131 efx_nic_fini(sc->enp);
134 device_printf(sc->dev, "sfxge_start: %d\n", rc);
140 sfxge_if_init(void *arg)
142 struct sfxge_softc *sc;
144 sc = (struct sfxge_softc *)arg;
146 sx_xlock(&sc->softc_lock);
147 (void)sfxge_start(sc);
148 sx_xunlock(&sc->softc_lock);
152 sfxge_stop(struct sfxge_softc *sc)
154 sx_assert(&sc->softc_lock, LA_XLOCKED);
156 if (sc->init_state != SFXGE_STARTED)
159 sc->init_state = SFXGE_REGISTERED;
164 /* Stop the transmitter. */
167 /* Stop the receiver. */
170 /* Stop processing events. */
173 /* Stop processing interrupts. */
176 efx_nic_fini(sc->enp);
178 sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
182 sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
184 struct sfxge_softc *sc;
188 ifr = (struct ifreq *)data;
194 sx_xlock(&sc->softc_lock);
195 if (ifp->if_flags & IFF_UP) {
196 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
197 if ((ifp->if_flags ^ sc->if_flags) &
198 (IFF_PROMISC | IFF_ALLMULTI)) {
199 sfxge_mac_filter_set(sc);
204 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
206 sc->if_flags = ifp->if_flags;
207 sx_xunlock(&sc->softc_lock);
210 if (ifr->ifr_mtu == ifp->if_mtu) {
213 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
215 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
216 ifp->if_mtu = ifr->ifr_mtu;
219 /* Restart required */
220 sx_xlock(&sc->softc_lock);
222 ifp->if_mtu = ifr->ifr_mtu;
223 error = sfxge_start(sc);
224 sx_xunlock(&sc->softc_lock);
226 ifp->if_flags &= ~IFF_UP;
227 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
234 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
235 sfxge_mac_filter_set(sc);
238 sx_xlock(&sc->softc_lock);
241 * The networking core already rejects attempts to
242 * enable capabilities we don't have. We still have
243 * to reject attempts to disable capabilities that we
244 * can't (yet) disable.
246 if (~ifr->ifr_reqcap & SFXGE_CAP_FIXED) {
248 sx_xunlock(&sc->softc_lock);
252 ifp->if_capenable = ifr->ifr_reqcap;
253 if (ifp->if_capenable & IFCAP_TXCSUM)
254 ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
256 ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
257 if (ifp->if_capenable & IFCAP_TSO)
258 ifp->if_hwassist |= CSUM_TSO;
260 ifp->if_hwassist &= ~CSUM_TSO;
262 sx_xunlock(&sc->softc_lock);
266 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
269 error = ether_ioctl(ifp, command, data);
276 sfxge_ifnet_fini(struct ifnet *ifp)
278 struct sfxge_softc *sc = ifp->if_softc;
280 sx_xlock(&sc->softc_lock);
282 sx_xunlock(&sc->softc_lock);
284 ifmedia_removeall(&sc->media);
290 sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
292 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
299 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
300 ifp->if_init = sfxge_if_init;
302 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
303 ifp->if_ioctl = sfxge_if_ioctl;
305 ifp->if_capabilities = SFXGE_CAP;
306 ifp->if_capenable = SFXGE_CAP_ENABLE;
307 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO;
309 ether_ifattach(ifp, encp->enc_mac_addr);
312 ifp->if_transmit = sfxge_if_transmit;
313 ifp->if_qflush = sfxge_if_qflush;
315 ifp->if_start = sfxge_if_start;
316 IFQ_SET_MAXLEN(&ifp->if_snd, SFXGE_NDESCS - 1);
317 ifp->if_snd.ifq_drv_maxlen = SFXGE_NDESCS - 1;
318 IFQ_SET_READY(&ifp->if_snd);
320 mtx_init(&sc->tx_lock, "txq", NULL, MTX_DEF);
323 if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
329 ether_ifdetach(sc->ifnet);
334 sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
336 KASSERT(sc->buffer_table_next + n <=
337 efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
338 ("buffer table full"));
340 *idp = sc->buffer_table_next;
341 sc->buffer_table_next += n;
345 sfxge_bar_init(struct sfxge_softc *sc)
347 efsys_bar_t *esbp = &sc->bar;
349 esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR);
350 if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
351 &esbp->esb_rid, RF_ACTIVE)) == NULL) {
352 device_printf(sc->dev, "Cannot allocate BAR region %d\n",
356 esbp->esb_tag = rman_get_bustag(esbp->esb_res);
357 esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
358 mtx_init(&esbp->esb_lock, "sfxge_efsys_bar", NULL, MTX_DEF);
364 sfxge_bar_fini(struct sfxge_softc *sc)
366 efsys_bar_t *esbp = &sc->bar;
368 bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
370 mtx_destroy(&esbp->esb_lock);
374 sfxge_create(struct sfxge_softc *sc)
382 sx_init(&sc->softc_lock, "sfxge_softc");
384 sc->stats_node = SYSCTL_ADD_NODE(
385 device_get_sysctl_ctx(dev),
386 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
387 OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics");
388 if (!sc->stats_node) {
393 TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
395 (void) pci_enable_busmaster(dev);
397 /* Initialize DMA mappings. */
398 if ((error = sfxge_dma_init(sc)) != 0)
401 /* Map the device registers. */
402 if ((error = sfxge_bar_init(sc)) != 0)
405 error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
407 KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
409 /* Create the common code nic object. */
410 mtx_init(&sc->enp_lock, "sfxge_nic", NULL, MTX_DEF);
411 if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
412 &sc->bar, &sc->enp_lock, &enp)) != 0)
416 /* Initialize MCDI to talk to the microcontroller. */
417 if ((error = sfxge_mcdi_init(sc)) != 0)
420 /* Probe the NIC and build the configuration data area. */
421 if ((error = efx_nic_probe(enp)) != 0)
424 /* Initialize the NVRAM. */
425 if ((error = efx_nvram_init(enp)) != 0)
428 /* Initialize the VPD. */
429 if ((error = efx_vpd_init(enp)) != 0)
433 if ((error = efx_nic_reset(enp)) != 0)
436 /* Initialize buffer table allocation. */
437 sc->buffer_table_next = 0;
439 /* Set up interrupts. */
440 if ((error = sfxge_intr_init(sc)) != 0)
443 /* Initialize event processing state. */
444 if ((error = sfxge_ev_init(sc)) != 0)
447 /* Initialize receive state. */
448 if ((error = sfxge_rx_init(sc)) != 0)
451 /* Initialize transmit state. */
452 if ((error = sfxge_tx_init(sc)) != 0)
455 /* Initialize port state. */
456 if ((error = sfxge_port_init(sc)) != 0)
459 sc->init_state = SFXGE_INITIALIZED;
482 efx_nic_unprobe(enp);
489 efx_nic_destroy(enp);
490 mtx_destroy(&sc->enp_lock);
494 (void) pci_disable_busmaster(sc->dev);
498 sx_destroy(&sc->softc_lock);
503 sfxge_destroy(struct sfxge_softc *sc)
507 /* Clean up port state. */
510 /* Clean up transmit state. */
513 /* Clean up receive state. */
516 /* Clean up event processing state. */
519 /* Clean up interrupts. */
522 /* Tear down common code subsystems. */
523 efx_nic_reset(sc->enp);
524 efx_vpd_fini(sc->enp);
525 efx_nvram_fini(sc->enp);
526 efx_nic_unprobe(sc->enp);
528 /* Tear down MCDI. */
531 /* Destroy common code context. */
534 efx_nic_destroy(enp);
536 /* Free DMA memory. */
539 /* Free mapped BARs. */
542 (void) pci_disable_busmaster(sc->dev);
544 taskqueue_drain(taskqueue_thread, &sc->task_reset);
546 /* Destroy the softc lock. */
547 sx_destroy(&sc->softc_lock);
551 sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
553 struct sfxge_softc *sc = arg1;
554 efx_vpd_value_t value;
557 value.evv_tag = arg2 >> 16;
558 value.evv_keyword = arg2 & 0xffff;
559 if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
563 return SYSCTL_OUT(req, value.evv_value, value.evv_length);
567 sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
568 efx_vpd_tag_t tag, const char *keyword)
570 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
571 efx_vpd_value_t value;
573 /* Check whether VPD tag/keyword is present */
575 value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
576 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
580 ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
581 sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
582 sfxge_vpd_handler, "A", "");
586 sfxge_vpd_init(struct sfxge_softc *sc)
588 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
589 struct sysctl_oid *vpd_node;
590 struct sysctl_oid_list *vpd_list;
592 efx_vpd_value_t value;
595 if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0)
597 sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
598 if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
601 /* Copy ID (product name) into device description, and log it. */
602 value.evv_tag = EFX_VPD_ID;
603 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
604 value.evv_value[value.evv_length] = 0;
605 device_set_desc_copy(sc->dev, value.evv_value);
606 device_printf(sc->dev, "%s\n", value.evv_value);
609 vpd_node = SYSCTL_ADD_NODE(
610 ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
611 OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
612 vpd_list = SYSCTL_CHILDREN(vpd_node);
614 /* Add sysctls for all expected and any vendor-defined keywords. */
615 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
616 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
617 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
620 for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
621 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
622 for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
623 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
628 free(sc->vpd_data, M_SFXGE);
634 sfxge_vpd_fini(struct sfxge_softc *sc)
636 free(sc->vpd_data, M_SFXGE);
640 sfxge_reset(void *arg, int npending)
642 struct sfxge_softc *sc;
647 sc = (struct sfxge_softc *)arg;
649 sx_xlock(&sc->softc_lock);
651 if (sc->init_state != SFXGE_STARTED)
655 efx_nic_reset(sc->enp);
656 if ((rc = sfxge_start(sc)) != 0)
657 device_printf(sc->dev,
658 "reset failed (%d); interface is now stopped\n",
662 sx_xunlock(&sc->softc_lock);
666 sfxge_schedule_reset(struct sfxge_softc *sc)
668 taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
672 sfxge_attach(device_t dev)
674 struct sfxge_softc *sc;
678 sc = device_get_softc(dev);
681 /* Allocate ifnet. */
682 ifp = if_alloc(IFT_ETHER);
684 device_printf(dev, "Couldn't allocate ifnet\n");
690 /* Initialize hardware. */
691 if ((error = sfxge_create(sc)) != 0)
694 /* Create the ifnet for the port. */
695 if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
698 if ((error = sfxge_vpd_init(sc)) != 0)
701 sc->init_state = SFXGE_REGISTERED;
706 sfxge_ifnet_fini(ifp);
718 sfxge_detach(device_t dev)
720 struct sfxge_softc *sc;
722 sc = device_get_softc(dev);
726 /* Destroy the ifnet. */
727 sfxge_ifnet_fini(sc->ifnet);
729 /* Tear down hardware. */
736 sfxge_probe(device_t dev)
738 uint16_t pci_vendor_id;
739 uint16_t pci_device_id;
743 pci_vendor_id = pci_get_vendor(dev);
744 pci_device_id = pci_get_device(dev);
746 rc = efx_family(pci_vendor_id, pci_device_id, &family);
750 KASSERT(family == EFX_FAMILY_SIENA, ("impossible controller family"));
751 device_set_desc(dev, "Solarflare SFC9000 family");
755 static device_method_t sfxge_methods[] = {
756 DEVMETHOD(device_probe, sfxge_probe),
757 DEVMETHOD(device_attach, sfxge_attach),
758 DEVMETHOD(device_detach, sfxge_detach),
763 static devclass_t sfxge_devclass;
765 static driver_t sfxge_driver = {
768 sizeof(struct sfxge_softc)
771 DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);