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"
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 SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD, 0,
72 "SFXGE driver parameters");
74 #define SFXGE_PARAM_RX_RING SFXGE_PARAM(rx_ring)
75 static int sfxge_rx_ring_entries = SFXGE_NDESCS;
76 TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries);
77 SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN,
78 &sfxge_rx_ring_entries, 0,
79 "Maximum number of descriptors in a receive ring");
81 #define SFXGE_PARAM_TX_RING SFXGE_PARAM(tx_ring)
82 static int sfxge_tx_ring_entries = SFXGE_NDESCS;
83 TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries);
84 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN,
85 &sfxge_tx_ring_entries, 0,
86 "Maximum number of descriptors in a transmit ring");
90 sfxge_reset(void *arg, int npending);
93 sfxge_start(struct sfxge_softc *sc)
97 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
99 if (sc->init_state == SFXGE_STARTED)
102 if (sc->init_state != SFXGE_REGISTERED) {
107 if ((rc = efx_nic_init(sc->enp)) != 0)
110 /* Start processing interrupts. */
111 if ((rc = sfxge_intr_start(sc)) != 0)
114 /* Start processing events. */
115 if ((rc = sfxge_ev_start(sc)) != 0)
118 /* Start the receiver side. */
119 if ((rc = sfxge_rx_start(sc)) != 0)
122 /* Start the transmitter side. */
123 if ((rc = sfxge_tx_start(sc)) != 0)
126 /* Fire up the port. */
127 if ((rc = sfxge_port_start(sc)) != 0)
130 sc->init_state = SFXGE_STARTED;
132 /* Tell the stack we're running. */
133 sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
134 sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
151 efx_nic_fini(sc->enp);
154 device_printf(sc->dev, "sfxge_start: %d\n", rc);
160 sfxge_if_init(void *arg)
162 struct sfxge_softc *sc;
164 sc = (struct sfxge_softc *)arg;
166 SFXGE_ADAPTER_LOCK(sc);
167 (void)sfxge_start(sc);
168 SFXGE_ADAPTER_UNLOCK(sc);
172 sfxge_stop(struct sfxge_softc *sc)
174 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
176 if (sc->init_state != SFXGE_STARTED)
179 sc->init_state = SFXGE_REGISTERED;
184 /* Stop the transmitter. */
187 /* Stop the receiver. */
190 /* Stop processing events. */
193 /* Stop processing interrupts. */
196 efx_nic_fini(sc->enp);
198 sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
202 sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
204 struct sfxge_softc *sc;
208 ifr = (struct ifreq *)data;
214 SFXGE_ADAPTER_LOCK(sc);
215 if (ifp->if_flags & IFF_UP) {
216 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
217 if ((ifp->if_flags ^ sc->if_flags) &
218 (IFF_PROMISC | IFF_ALLMULTI)) {
219 sfxge_mac_filter_set(sc);
224 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
226 sc->if_flags = ifp->if_flags;
227 SFXGE_ADAPTER_UNLOCK(sc);
230 if (ifr->ifr_mtu == ifp->if_mtu) {
233 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
235 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
236 ifp->if_mtu = ifr->ifr_mtu;
239 /* Restart required */
240 SFXGE_ADAPTER_LOCK(sc);
242 ifp->if_mtu = ifr->ifr_mtu;
243 error = sfxge_start(sc);
244 SFXGE_ADAPTER_UNLOCK(sc);
246 ifp->if_flags &= ~IFF_UP;
247 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
254 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
255 sfxge_mac_filter_set(sc);
258 SFXGE_ADAPTER_LOCK(sc);
261 * The networking core already rejects attempts to
262 * enable capabilities we don't have. We still have
263 * to reject attempts to disable capabilities that we
264 * can't (yet) disable.
266 if (~ifr->ifr_reqcap & SFXGE_CAP_FIXED) {
268 SFXGE_ADAPTER_UNLOCK(sc);
272 ifp->if_capenable = ifr->ifr_reqcap;
273 if (ifp->if_capenable & IFCAP_TXCSUM)
274 ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
276 ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
277 if (ifp->if_capenable & IFCAP_TSO)
278 ifp->if_hwassist |= CSUM_TSO;
280 ifp->if_hwassist &= ~CSUM_TSO;
282 SFXGE_ADAPTER_UNLOCK(sc);
286 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
289 error = ether_ioctl(ifp, command, data);
296 sfxge_ifnet_fini(struct ifnet *ifp)
298 struct sfxge_softc *sc = ifp->if_softc;
300 SFXGE_ADAPTER_LOCK(sc);
302 SFXGE_ADAPTER_UNLOCK(sc);
304 ifmedia_removeall(&sc->media);
310 sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
312 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
319 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
320 ifp->if_init = sfxge_if_init;
322 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
323 ifp->if_ioctl = sfxge_if_ioctl;
325 ifp->if_capabilities = SFXGE_CAP;
326 ifp->if_capenable = SFXGE_CAP_ENABLE;
327 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO;
329 ether_ifattach(ifp, encp->enc_mac_addr);
332 ifp->if_transmit = sfxge_if_transmit;
333 ifp->if_qflush = sfxge_if_qflush;
335 ifp->if_start = sfxge_if_start;
336 IFQ_SET_MAXLEN(&ifp->if_snd, sc->txq_entries - 1);
337 ifp->if_snd.ifq_drv_maxlen = sc->txq_entries - 1;
338 IFQ_SET_READY(&ifp->if_snd);
340 snprintf(sc->tx_lock_name, sizeof(sc->tx_lock_name),
341 "%s:tx", device_get_nameunit(sc->dev));
342 mtx_init(&sc->tx_lock, sc->tx_lock_name, NULL, MTX_DEF);
345 if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
351 ether_ifdetach(sc->ifnet);
356 sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
358 KASSERT(sc->buffer_table_next + n <=
359 efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
360 ("buffer table full"));
362 *idp = sc->buffer_table_next;
363 sc->buffer_table_next += n;
367 sfxge_bar_init(struct sfxge_softc *sc)
369 efsys_bar_t *esbp = &sc->bar;
371 esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR);
372 if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
373 &esbp->esb_rid, RF_ACTIVE)) == NULL) {
374 device_printf(sc->dev, "Cannot allocate BAR region %d\n",
378 esbp->esb_tag = rman_get_bustag(esbp->esb_res);
379 esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
381 SFXGE_BAR_LOCK_INIT(esbp, device_get_nameunit(sc->dev));
387 sfxge_bar_fini(struct sfxge_softc *sc)
389 efsys_bar_t *esbp = &sc->bar;
391 bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
393 SFXGE_BAR_LOCK_DESTROY(esbp);
397 sfxge_create(struct sfxge_softc *sc)
402 char rss_param_name[sizeof(SFXGE_PARAM(%d.max_rss_channels))];
406 SFXGE_ADAPTER_LOCK_INIT(sc, device_get_nameunit(sc->dev));
408 sc->max_rss_channels = 0;
409 snprintf(rss_param_name, sizeof(rss_param_name),
410 SFXGE_PARAM(%d.max_rss_channels),
411 (int)device_get_unit(dev));
412 TUNABLE_INT_FETCH(rss_param_name, &sc->max_rss_channels);
414 sc->stats_node = SYSCTL_ADD_NODE(
415 device_get_sysctl_ctx(dev),
416 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
417 OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics");
418 if (sc->stats_node == NULL) {
423 TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
425 (void) pci_enable_busmaster(dev);
427 /* Initialize DMA mappings. */
428 if ((error = sfxge_dma_init(sc)) != 0)
431 /* Map the device registers. */
432 if ((error = sfxge_bar_init(sc)) != 0)
435 error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
437 KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
439 /* Create the common code nic object. */
440 SFXGE_EFSYS_LOCK_INIT(&sc->enp_lock,
441 device_get_nameunit(sc->dev), "nic");
442 if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
443 &sc->bar, &sc->enp_lock, &enp)) != 0)
447 if (!ISP2(sfxge_rx_ring_entries) ||
448 !(sfxge_rx_ring_entries & EFX_RXQ_NDESCS_MASK)) {
449 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
450 SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries,
451 EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS);
453 goto fail_rx_ring_entries;
455 sc->rxq_entries = sfxge_rx_ring_entries;
457 if (!ISP2(sfxge_tx_ring_entries) ||
458 !(sfxge_tx_ring_entries & EFX_TXQ_NDESCS_MASK)) {
459 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
460 SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries,
461 EFX_TXQ_MINNDESCS, EFX_TXQ_MAXNDESCS);
463 goto fail_tx_ring_entries;
465 sc->txq_entries = sfxge_tx_ring_entries;
467 /* Initialize MCDI to talk to the microcontroller. */
468 if ((error = sfxge_mcdi_init(sc)) != 0)
471 /* Probe the NIC and build the configuration data area. */
472 if ((error = efx_nic_probe(enp)) != 0)
475 /* Initialize the NVRAM. */
476 if ((error = efx_nvram_init(enp)) != 0)
479 /* Initialize the VPD. */
480 if ((error = efx_vpd_init(enp)) != 0)
484 if ((error = efx_nic_reset(enp)) != 0)
487 /* Initialize buffer table allocation. */
488 sc->buffer_table_next = 0;
490 /* Set up interrupts. */
491 if ((error = sfxge_intr_init(sc)) != 0)
494 /* Initialize event processing state. */
495 if ((error = sfxge_ev_init(sc)) != 0)
498 /* Initialize receive state. */
499 if ((error = sfxge_rx_init(sc)) != 0)
502 /* Initialize transmit state. */
503 if ((error = sfxge_tx_init(sc)) != 0)
506 /* Initialize port state. */
507 if ((error = sfxge_port_init(sc)) != 0)
510 sc->init_state = SFXGE_INITIALIZED;
533 efx_nic_unprobe(enp);
539 fail_tx_ring_entries:
540 fail_rx_ring_entries:
542 efx_nic_destroy(enp);
543 SFXGE_EFSYS_LOCK_DESTROY(&sc->enp_lock);
547 (void) pci_disable_busmaster(sc->dev);
551 SFXGE_ADAPTER_LOCK_DESTROY(sc);
556 sfxge_destroy(struct sfxge_softc *sc)
560 /* Clean up port state. */
563 /* Clean up transmit state. */
566 /* Clean up receive state. */
569 /* Clean up event processing state. */
572 /* Clean up interrupts. */
575 /* Tear down common code subsystems. */
576 efx_nic_reset(sc->enp);
577 efx_vpd_fini(sc->enp);
578 efx_nvram_fini(sc->enp);
579 efx_nic_unprobe(sc->enp);
581 /* Tear down MCDI. */
584 /* Destroy common code context. */
587 efx_nic_destroy(enp);
589 /* Free DMA memory. */
592 /* Free mapped BARs. */
595 (void) pci_disable_busmaster(sc->dev);
597 taskqueue_drain(taskqueue_thread, &sc->task_reset);
599 /* Destroy the softc lock. */
600 SFXGE_ADAPTER_LOCK_DESTROY(sc);
604 sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
606 struct sfxge_softc *sc = arg1;
607 efx_vpd_value_t value;
610 value.evv_tag = arg2 >> 16;
611 value.evv_keyword = arg2 & 0xffff;
612 if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
616 return (SYSCTL_OUT(req, value.evv_value, value.evv_length));
620 sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
621 efx_vpd_tag_t tag, const char *keyword)
623 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
624 efx_vpd_value_t value;
626 /* Check whether VPD tag/keyword is present */
628 value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
629 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
633 ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
634 sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
635 sfxge_vpd_handler, "A", "");
639 sfxge_vpd_init(struct sfxge_softc *sc)
641 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
642 struct sysctl_oid *vpd_node;
643 struct sysctl_oid_list *vpd_list;
645 efx_vpd_value_t value;
648 if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0)
650 sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
651 if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
654 /* Copy ID (product name) into device description, and log it. */
655 value.evv_tag = EFX_VPD_ID;
656 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
657 value.evv_value[value.evv_length] = 0;
658 device_set_desc_copy(sc->dev, value.evv_value);
659 device_printf(sc->dev, "%s\n", value.evv_value);
662 vpd_node = SYSCTL_ADD_NODE(
663 ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
664 OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
665 vpd_list = SYSCTL_CHILDREN(vpd_node);
667 /* Add sysctls for all expected and any vendor-defined keywords. */
668 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
669 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
670 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
673 for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
674 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
675 for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
676 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
681 free(sc->vpd_data, M_SFXGE);
687 sfxge_vpd_fini(struct sfxge_softc *sc)
689 free(sc->vpd_data, M_SFXGE);
693 sfxge_reset(void *arg, int npending)
695 struct sfxge_softc *sc;
700 sc = (struct sfxge_softc *)arg;
702 SFXGE_ADAPTER_LOCK(sc);
704 if (sc->init_state != SFXGE_STARTED)
708 efx_nic_reset(sc->enp);
709 if ((rc = sfxge_start(sc)) != 0)
710 device_printf(sc->dev,
711 "reset failed (%d); interface is now stopped\n",
715 SFXGE_ADAPTER_UNLOCK(sc);
719 sfxge_schedule_reset(struct sfxge_softc *sc)
721 taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
725 sfxge_attach(device_t dev)
727 struct sfxge_softc *sc;
731 sc = device_get_softc(dev);
734 /* Allocate ifnet. */
735 ifp = if_alloc(IFT_ETHER);
737 device_printf(dev, "Couldn't allocate ifnet\n");
743 /* Initialize hardware. */
744 if ((error = sfxge_create(sc)) != 0)
747 /* Create the ifnet for the port. */
748 if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
751 if ((error = sfxge_vpd_init(sc)) != 0)
754 sc->init_state = SFXGE_REGISTERED;
759 sfxge_ifnet_fini(ifp);
771 sfxge_detach(device_t dev)
773 struct sfxge_softc *sc;
775 sc = device_get_softc(dev);
779 /* Destroy the ifnet. */
780 sfxge_ifnet_fini(sc->ifnet);
782 /* Tear down hardware. */
789 sfxge_probe(device_t dev)
791 uint16_t pci_vendor_id;
792 uint16_t pci_device_id;
796 pci_vendor_id = pci_get_vendor(dev);
797 pci_device_id = pci_get_device(dev);
799 rc = efx_family(pci_vendor_id, pci_device_id, &family);
803 KASSERT(family == EFX_FAMILY_SIENA, ("impossible controller family"));
804 device_set_desc(dev, "Solarflare SFC9000 family");
808 static device_method_t sfxge_methods[] = {
809 DEVMETHOD(device_probe, sfxge_probe),
810 DEVMETHOD(device_attach, sfxge_attach),
811 DEVMETHOD(device_detach, sfxge_detach),
816 static devclass_t sfxge_devclass;
818 static driver_t sfxge_driver = {
821 sizeof(struct sfxge_softc)
824 DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);