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_var.h>
52 #include <net/if_media.h>
53 #include <net/if_types.h>
55 #include "common/efx.h"
60 #define SFXGE_CAP (IFCAP_VLAN_MTU | \
61 IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM | IFCAP_TSO | \
62 IFCAP_JUMBO_MTU | IFCAP_LRO | \
63 IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE)
64 #define SFXGE_CAP_ENABLE SFXGE_CAP
65 #define SFXGE_CAP_FIXED (IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM | \
66 IFCAP_JUMBO_MTU | IFCAP_LINKSTATE)
68 MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver");
71 sfxge_reset(void *arg, int npending);
74 sfxge_start(struct sfxge_softc *sc)
78 sx_assert(&sc->softc_lock, LA_XLOCKED);
80 if (sc->init_state == SFXGE_STARTED)
83 if (sc->init_state != SFXGE_REGISTERED) {
88 if ((rc = efx_nic_init(sc->enp)) != 0)
91 /* Start processing interrupts. */
92 if ((rc = sfxge_intr_start(sc)) != 0)
95 /* Start processing events. */
96 if ((rc = sfxge_ev_start(sc)) != 0)
99 /* Start the receiver side. */
100 if ((rc = sfxge_rx_start(sc)) != 0)
103 /* Start the transmitter side. */
104 if ((rc = sfxge_tx_start(sc)) != 0)
107 /* Fire up the port. */
108 if ((rc = sfxge_port_start(sc)) != 0)
111 sc->init_state = SFXGE_STARTED;
113 /* Tell the stack we're running. */
114 sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
115 sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
132 efx_nic_fini(sc->enp);
135 device_printf(sc->dev, "sfxge_start: %d\n", rc);
141 sfxge_if_init(void *arg)
143 struct sfxge_softc *sc;
145 sc = (struct sfxge_softc *)arg;
147 sx_xlock(&sc->softc_lock);
148 (void)sfxge_start(sc);
149 sx_xunlock(&sc->softc_lock);
153 sfxge_stop(struct sfxge_softc *sc)
155 sx_assert(&sc->softc_lock, LA_XLOCKED);
157 if (sc->init_state != SFXGE_STARTED)
160 sc->init_state = SFXGE_REGISTERED;
165 /* Stop the transmitter. */
168 /* Stop the receiver. */
171 /* Stop processing events. */
174 /* Stop processing interrupts. */
177 efx_nic_fini(sc->enp);
179 sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
183 sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
185 struct sfxge_softc *sc;
189 ifr = (struct ifreq *)data;
195 sx_xlock(&sc->softc_lock);
196 if (ifp->if_flags & IFF_UP) {
197 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
198 if ((ifp->if_flags ^ sc->if_flags) &
199 (IFF_PROMISC | IFF_ALLMULTI)) {
200 sfxge_mac_filter_set(sc);
205 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
207 sc->if_flags = ifp->if_flags;
208 sx_xunlock(&sc->softc_lock);
211 if (ifr->ifr_mtu == ifp->if_mtu) {
214 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
216 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
217 ifp->if_mtu = ifr->ifr_mtu;
220 /* Restart required */
221 sx_xlock(&sc->softc_lock);
223 ifp->if_mtu = ifr->ifr_mtu;
224 error = sfxge_start(sc);
225 sx_xunlock(&sc->softc_lock);
227 ifp->if_flags &= ~IFF_UP;
228 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
235 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
236 sfxge_mac_filter_set(sc);
239 sx_xlock(&sc->softc_lock);
242 * The networking core already rejects attempts to
243 * enable capabilities we don't have. We still have
244 * to reject attempts to disable capabilities that we
245 * can't (yet) disable.
247 if (~ifr->ifr_reqcap & SFXGE_CAP_FIXED) {
249 sx_xunlock(&sc->softc_lock);
253 ifp->if_capenable = ifr->ifr_reqcap;
254 if (ifp->if_capenable & IFCAP_TXCSUM)
255 ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
257 ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
258 if (ifp->if_capenable & IFCAP_TSO)
259 ifp->if_hwassist |= CSUM_TSO;
261 ifp->if_hwassist &= ~CSUM_TSO;
263 sx_xunlock(&sc->softc_lock);
267 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
270 error = ether_ioctl(ifp, command, data);
277 sfxge_ifnet_fini(struct ifnet *ifp)
279 struct sfxge_softc *sc = ifp->if_softc;
281 sx_xlock(&sc->softc_lock);
283 sx_xunlock(&sc->softc_lock);
285 ifmedia_removeall(&sc->media);
291 sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
293 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
300 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
301 ifp->if_init = sfxge_if_init;
303 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
304 ifp->if_ioctl = sfxge_if_ioctl;
306 ifp->if_capabilities = SFXGE_CAP;
307 ifp->if_capenable = SFXGE_CAP_ENABLE;
308 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO;
310 ether_ifattach(ifp, encp->enc_mac_addr);
313 ifp->if_transmit = sfxge_if_transmit;
314 ifp->if_qflush = sfxge_if_qflush;
316 ifp->if_start = sfxge_if_start;
317 IFQ_SET_MAXLEN(&ifp->if_snd, SFXGE_NDESCS - 1);
318 ifp->if_snd.ifq_drv_maxlen = SFXGE_NDESCS - 1;
319 IFQ_SET_READY(&ifp->if_snd);
321 mtx_init(&sc->tx_lock, "txq", NULL, MTX_DEF);
324 if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
330 ether_ifdetach(sc->ifnet);
335 sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
337 KASSERT(sc->buffer_table_next + n <=
338 efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
339 ("buffer table full"));
341 *idp = sc->buffer_table_next;
342 sc->buffer_table_next += n;
346 sfxge_bar_init(struct sfxge_softc *sc)
348 efsys_bar_t *esbp = &sc->bar;
350 esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR);
351 if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
352 &esbp->esb_rid, RF_ACTIVE)) == NULL) {
353 device_printf(sc->dev, "Cannot allocate BAR region %d\n",
357 esbp->esb_tag = rman_get_bustag(esbp->esb_res);
358 esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
359 mtx_init(&esbp->esb_lock, "sfxge_efsys_bar", NULL, MTX_DEF);
365 sfxge_bar_fini(struct sfxge_softc *sc)
367 efsys_bar_t *esbp = &sc->bar;
369 bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
371 mtx_destroy(&esbp->esb_lock);
375 sfxge_create(struct sfxge_softc *sc)
383 sx_init(&sc->softc_lock, "sfxge_softc");
385 sc->stats_node = SYSCTL_ADD_NODE(
386 device_get_sysctl_ctx(dev),
387 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
388 OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics");
389 if (!sc->stats_node) {
394 TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
396 (void) pci_enable_busmaster(dev);
398 /* Initialize DMA mappings. */
399 if ((error = sfxge_dma_init(sc)) != 0)
402 /* Map the device registers. */
403 if ((error = sfxge_bar_init(sc)) != 0)
406 error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
408 KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
410 /* Create the common code nic object. */
411 mtx_init(&sc->enp_lock, "sfxge_nic", NULL, MTX_DEF);
412 if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
413 &sc->bar, &sc->enp_lock, &enp)) != 0)
417 /* Initialize MCDI to talk to the microcontroller. */
418 if ((error = sfxge_mcdi_init(sc)) != 0)
421 /* Probe the NIC and build the configuration data area. */
422 if ((error = efx_nic_probe(enp)) != 0)
425 /* Initialize the NVRAM. */
426 if ((error = efx_nvram_init(enp)) != 0)
429 /* Initialize the VPD. */
430 if ((error = efx_vpd_init(enp)) != 0)
434 if ((error = efx_nic_reset(enp)) != 0)
437 /* Initialize buffer table allocation. */
438 sc->buffer_table_next = 0;
440 /* Set up interrupts. */
441 if ((error = sfxge_intr_init(sc)) != 0)
444 /* Initialize event processing state. */
445 if ((error = sfxge_ev_init(sc)) != 0)
448 /* Initialize receive state. */
449 if ((error = sfxge_rx_init(sc)) != 0)
452 /* Initialize transmit state. */
453 if ((error = sfxge_tx_init(sc)) != 0)
456 /* Initialize port state. */
457 if ((error = sfxge_port_init(sc)) != 0)
460 sc->init_state = SFXGE_INITIALIZED;
483 efx_nic_unprobe(enp);
490 efx_nic_destroy(enp);
491 mtx_destroy(&sc->enp_lock);
495 (void) pci_disable_busmaster(sc->dev);
499 sx_destroy(&sc->softc_lock);
504 sfxge_destroy(struct sfxge_softc *sc)
508 /* Clean up port state. */
511 /* Clean up transmit state. */
514 /* Clean up receive state. */
517 /* Clean up event processing state. */
520 /* Clean up interrupts. */
523 /* Tear down common code subsystems. */
524 efx_nic_reset(sc->enp);
525 efx_vpd_fini(sc->enp);
526 efx_nvram_fini(sc->enp);
527 efx_nic_unprobe(sc->enp);
529 /* Tear down MCDI. */
532 /* Destroy common code context. */
535 efx_nic_destroy(enp);
537 /* Free DMA memory. */
540 /* Free mapped BARs. */
543 (void) pci_disable_busmaster(sc->dev);
545 taskqueue_drain(taskqueue_thread, &sc->task_reset);
547 /* Destroy the softc lock. */
548 sx_destroy(&sc->softc_lock);
552 sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
554 struct sfxge_softc *sc = arg1;
555 efx_vpd_value_t value;
558 value.evv_tag = arg2 >> 16;
559 value.evv_keyword = arg2 & 0xffff;
560 if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
564 return SYSCTL_OUT(req, value.evv_value, value.evv_length);
568 sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
569 efx_vpd_tag_t tag, const char *keyword)
571 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
572 efx_vpd_value_t value;
574 /* Check whether VPD tag/keyword is present */
576 value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
577 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
581 ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
582 sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
583 sfxge_vpd_handler, "A", "");
587 sfxge_vpd_init(struct sfxge_softc *sc)
589 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
590 struct sysctl_oid *vpd_node;
591 struct sysctl_oid_list *vpd_list;
593 efx_vpd_value_t value;
596 if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0)
598 sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
599 if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
602 /* Copy ID (product name) into device description, and log it. */
603 value.evv_tag = EFX_VPD_ID;
604 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
605 value.evv_value[value.evv_length] = 0;
606 device_set_desc_copy(sc->dev, value.evv_value);
607 device_printf(sc->dev, "%s\n", value.evv_value);
610 vpd_node = SYSCTL_ADD_NODE(
611 ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
612 OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
613 vpd_list = SYSCTL_CHILDREN(vpd_node);
615 /* Add sysctls for all expected and any vendor-defined keywords. */
616 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
617 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
618 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
621 for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
622 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
623 for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
624 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
629 free(sc->vpd_data, M_SFXGE);
635 sfxge_vpd_fini(struct sfxge_softc *sc)
637 free(sc->vpd_data, M_SFXGE);
641 sfxge_reset(void *arg, int npending)
643 struct sfxge_softc *sc;
648 sc = (struct sfxge_softc *)arg;
650 sx_xlock(&sc->softc_lock);
652 if (sc->init_state != SFXGE_STARTED)
656 efx_nic_reset(sc->enp);
657 if ((rc = sfxge_start(sc)) != 0)
658 device_printf(sc->dev,
659 "reset failed (%d); interface is now stopped\n",
663 sx_xunlock(&sc->softc_lock);
667 sfxge_schedule_reset(struct sfxge_softc *sc)
669 taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
673 sfxge_attach(device_t dev)
675 struct sfxge_softc *sc;
679 sc = device_get_softc(dev);
682 /* Allocate ifnet. */
683 ifp = if_alloc(IFT_ETHER);
685 device_printf(dev, "Couldn't allocate ifnet\n");
691 /* Initialize hardware. */
692 if ((error = sfxge_create(sc)) != 0)
695 /* Create the ifnet for the port. */
696 if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
699 if ((error = sfxge_vpd_init(sc)) != 0)
702 sc->init_state = SFXGE_REGISTERED;
707 sfxge_ifnet_fini(ifp);
719 sfxge_detach(device_t dev)
721 struct sfxge_softc *sc;
723 sc = device_get_softc(dev);
727 /* Destroy the ifnet. */
728 sfxge_ifnet_fini(sc->ifnet);
730 /* Tear down hardware. */
737 sfxge_probe(device_t dev)
739 uint16_t pci_vendor_id;
740 uint16_t pci_device_id;
744 pci_vendor_id = pci_get_vendor(dev);
745 pci_device_id = pci_get_device(dev);
747 rc = efx_family(pci_vendor_id, pci_device_id, &family);
751 KASSERT(family == EFX_FAMILY_SIENA, ("impossible controller family"));
752 device_set_desc(dev, "Solarflare SFC9000 family");
756 static device_method_t sfxge_methods[] = {
757 DEVMETHOD(device_probe, sfxge_probe),
758 DEVMETHOD(device_attach, sfxge_attach),
759 DEVMETHOD(device_detach, sfxge_detach),
764 static devclass_t sfxge_devclass;
766 static driver_t sfxge_driver = {
769 sizeof(struct sfxge_softc)
772 DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);