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_VLAN_HWCSUM | \
68 IFCAP_JUMBO_MTU | IFCAP_LINKSTATE)
70 MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver");
73 SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD, 0,
74 "SFXGE driver parameters");
76 #define SFXGE_PARAM_RX_RING SFXGE_PARAM(rx_ring)
77 static int sfxge_rx_ring_entries = SFXGE_NDESCS;
78 TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries);
79 SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN,
80 &sfxge_rx_ring_entries, 0,
81 "Maximum number of descriptors in a receive ring");
83 #define SFXGE_PARAM_TX_RING SFXGE_PARAM(tx_ring)
84 static int sfxge_tx_ring_entries = SFXGE_NDESCS;
85 TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries);
86 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN,
87 &sfxge_tx_ring_entries, 0,
88 "Maximum number of descriptors in a transmit ring");
92 sfxge_reset(void *arg, int npending);
95 sfxge_start(struct sfxge_softc *sc)
99 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
101 if (sc->init_state == SFXGE_STARTED)
104 if (sc->init_state != SFXGE_REGISTERED) {
109 if ((rc = efx_nic_init(sc->enp)) != 0)
112 /* Start processing interrupts. */
113 if ((rc = sfxge_intr_start(sc)) != 0)
116 /* Start processing events. */
117 if ((rc = sfxge_ev_start(sc)) != 0)
120 /* Start the receiver side. */
121 if ((rc = sfxge_rx_start(sc)) != 0)
124 /* Start the transmitter side. */
125 if ((rc = sfxge_tx_start(sc)) != 0)
128 /* Fire up the port. */
129 if ((rc = sfxge_port_start(sc)) != 0)
132 sc->init_state = SFXGE_STARTED;
134 /* Tell the stack we're running. */
135 sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
136 sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
153 efx_nic_fini(sc->enp);
156 device_printf(sc->dev, "sfxge_start: %d\n", rc);
162 sfxge_if_init(void *arg)
164 struct sfxge_softc *sc;
166 sc = (struct sfxge_softc *)arg;
168 SFXGE_ADAPTER_LOCK(sc);
169 (void)sfxge_start(sc);
170 SFXGE_ADAPTER_UNLOCK(sc);
174 sfxge_stop(struct sfxge_softc *sc)
176 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
178 if (sc->init_state != SFXGE_STARTED)
181 sc->init_state = SFXGE_REGISTERED;
186 /* Stop the transmitter. */
189 /* Stop the receiver. */
192 /* Stop processing events. */
195 /* Stop processing interrupts. */
198 efx_nic_fini(sc->enp);
200 sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
204 sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
206 struct sfxge_softc *sc;
210 ifr = (struct ifreq *)data;
216 SFXGE_ADAPTER_LOCK(sc);
217 if (ifp->if_flags & IFF_UP) {
218 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
219 if ((ifp->if_flags ^ sc->if_flags) &
220 (IFF_PROMISC | IFF_ALLMULTI)) {
221 sfxge_mac_filter_set(sc);
226 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
228 sc->if_flags = ifp->if_flags;
229 SFXGE_ADAPTER_UNLOCK(sc);
232 if (ifr->ifr_mtu == ifp->if_mtu) {
235 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
237 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
238 ifp->if_mtu = ifr->ifr_mtu;
241 /* Restart required */
242 SFXGE_ADAPTER_LOCK(sc);
244 ifp->if_mtu = ifr->ifr_mtu;
245 error = sfxge_start(sc);
246 SFXGE_ADAPTER_UNLOCK(sc);
248 ifp->if_flags &= ~IFF_UP;
249 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
256 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
257 sfxge_mac_filter_set(sc);
261 int reqcap = ifr->ifr_reqcap;
264 SFXGE_ADAPTER_LOCK(sc);
266 /* Capabilities to be changed in accordance with request */
267 capchg_mask = ifp->if_capenable ^ reqcap;
270 * The networking core already rejects attempts to
271 * enable capabilities we don't have. We still have
272 * to reject attempts to disable capabilities that we
273 * can't (yet) disable.
275 KASSERT((reqcap & ~ifp->if_capabilities) == 0,
276 ("Unsupported capabilities %x requested %x vs %x",
277 reqcap & ~ifp->if_capabilities,
278 reqcap , ifp->if_capabilities));
279 if (capchg_mask & SFXGE_CAP_FIXED) {
281 SFXGE_ADAPTER_UNLOCK(sc);
285 if (reqcap & IFCAP_TXCSUM)
286 ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
288 ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
289 if (reqcap & IFCAP_TXCSUM_IPV6)
290 ifp->if_hwassist |= (CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
292 ifp->if_hwassist &= ~(CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
295 * The kernel takes both IFCAP_TSOx and CSUM_TSO into
296 * account before using TSO. So, we do not touch
297 * checksum flags when IFCAP_TSOx is modified.
298 * Note that CSUM_TSO is (CSUM_IP_TSO|CSUM_IP6_TSO),
299 * but both bits are set in IPv4 and IPv6 mbufs.
302 ifp->if_capenable = reqcap;
304 SFXGE_ADAPTER_UNLOCK(sc);
309 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
312 error = ether_ioctl(ifp, command, data);
319 sfxge_ifnet_fini(struct ifnet *ifp)
321 struct sfxge_softc *sc = ifp->if_softc;
323 SFXGE_ADAPTER_LOCK(sc);
325 SFXGE_ADAPTER_UNLOCK(sc);
327 ifmedia_removeall(&sc->media);
333 sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
335 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
342 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
343 ifp->if_init = sfxge_if_init;
345 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
346 ifp->if_ioctl = sfxge_if_ioctl;
348 ifp->if_capabilities = SFXGE_CAP;
349 ifp->if_capenable = SFXGE_CAP_ENABLE;
350 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
351 CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
353 ether_ifattach(ifp, encp->enc_mac_addr);
355 ifp->if_transmit = sfxge_if_transmit;
356 ifp->if_qflush = sfxge_if_qflush;
358 if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
364 ether_ifdetach(sc->ifnet);
369 sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
371 KASSERT(sc->buffer_table_next + n <=
372 efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
373 ("buffer table full"));
375 *idp = sc->buffer_table_next;
376 sc->buffer_table_next += n;
380 sfxge_bar_init(struct sfxge_softc *sc)
382 efsys_bar_t *esbp = &sc->bar;
384 esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR);
385 if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
386 &esbp->esb_rid, RF_ACTIVE)) == NULL) {
387 device_printf(sc->dev, "Cannot allocate BAR region %d\n",
391 esbp->esb_tag = rman_get_bustag(esbp->esb_res);
392 esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
394 SFXGE_BAR_LOCK_INIT(esbp, device_get_nameunit(sc->dev));
400 sfxge_bar_fini(struct sfxge_softc *sc)
402 efsys_bar_t *esbp = &sc->bar;
404 bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
406 SFXGE_BAR_LOCK_DESTROY(esbp);
410 sfxge_create(struct sfxge_softc *sc)
415 char rss_param_name[sizeof(SFXGE_PARAM(%d.max_rss_channels))];
419 SFXGE_ADAPTER_LOCK_INIT(sc, device_get_nameunit(sc->dev));
421 sc->max_rss_channels = 0;
422 snprintf(rss_param_name, sizeof(rss_param_name),
423 SFXGE_PARAM(%d.max_rss_channels),
424 (int)device_get_unit(dev));
425 TUNABLE_INT_FETCH(rss_param_name, &sc->max_rss_channels);
427 sc->stats_node = SYSCTL_ADD_NODE(
428 device_get_sysctl_ctx(dev),
429 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
430 OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics");
431 if (sc->stats_node == NULL) {
436 TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
438 (void) pci_enable_busmaster(dev);
440 /* Initialize DMA mappings. */
441 if ((error = sfxge_dma_init(sc)) != 0)
444 /* Map the device registers. */
445 if ((error = sfxge_bar_init(sc)) != 0)
448 error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
450 KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
452 /* Create the common code nic object. */
453 SFXGE_EFSYS_LOCK_INIT(&sc->enp_lock,
454 device_get_nameunit(sc->dev), "nic");
455 if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
456 &sc->bar, &sc->enp_lock, &enp)) != 0)
460 if (!ISP2(sfxge_rx_ring_entries) ||
461 !(sfxge_rx_ring_entries & EFX_RXQ_NDESCS_MASK)) {
462 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
463 SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries,
464 EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS);
466 goto fail_rx_ring_entries;
468 sc->rxq_entries = sfxge_rx_ring_entries;
470 if (!ISP2(sfxge_tx_ring_entries) ||
471 !(sfxge_tx_ring_entries & EFX_TXQ_NDESCS_MASK)) {
472 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
473 SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries,
474 EFX_TXQ_MINNDESCS, EFX_TXQ_MAXNDESCS);
476 goto fail_tx_ring_entries;
478 sc->txq_entries = sfxge_tx_ring_entries;
480 /* Initialize MCDI to talk to the microcontroller. */
481 if ((error = sfxge_mcdi_init(sc)) != 0)
484 /* Probe the NIC and build the configuration data area. */
485 if ((error = efx_nic_probe(enp)) != 0)
488 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
489 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
490 OID_AUTO, "version", CTLFLAG_RD,
491 SFXGE_VERSION_STRING, 0,
494 /* Initialize the NVRAM. */
495 if ((error = efx_nvram_init(enp)) != 0)
498 /* Initialize the VPD. */
499 if ((error = efx_vpd_init(enp)) != 0)
503 if ((error = efx_nic_reset(enp)) != 0)
506 /* Initialize buffer table allocation. */
507 sc->buffer_table_next = 0;
509 /* Set up interrupts. */
510 if ((error = sfxge_intr_init(sc)) != 0)
513 /* Initialize event processing state. */
514 if ((error = sfxge_ev_init(sc)) != 0)
517 /* Initialize receive state. */
518 if ((error = sfxge_rx_init(sc)) != 0)
521 /* Initialize transmit state. */
522 if ((error = sfxge_tx_init(sc)) != 0)
525 /* Initialize port state. */
526 if ((error = sfxge_port_init(sc)) != 0)
529 sc->init_state = SFXGE_INITIALIZED;
552 efx_nic_unprobe(enp);
558 fail_tx_ring_entries:
559 fail_rx_ring_entries:
561 efx_nic_destroy(enp);
562 SFXGE_EFSYS_LOCK_DESTROY(&sc->enp_lock);
566 (void) pci_disable_busmaster(sc->dev);
570 SFXGE_ADAPTER_LOCK_DESTROY(sc);
575 sfxge_destroy(struct sfxge_softc *sc)
579 /* Clean up port state. */
582 /* Clean up transmit state. */
585 /* Clean up receive state. */
588 /* Clean up event processing state. */
591 /* Clean up interrupts. */
594 /* Tear down common code subsystems. */
595 efx_nic_reset(sc->enp);
596 efx_vpd_fini(sc->enp);
597 efx_nvram_fini(sc->enp);
598 efx_nic_unprobe(sc->enp);
600 /* Tear down MCDI. */
603 /* Destroy common code context. */
606 efx_nic_destroy(enp);
608 /* Free DMA memory. */
611 /* Free mapped BARs. */
614 (void) pci_disable_busmaster(sc->dev);
616 taskqueue_drain(taskqueue_thread, &sc->task_reset);
618 /* Destroy the softc lock. */
619 SFXGE_ADAPTER_LOCK_DESTROY(sc);
623 sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
625 struct sfxge_softc *sc = arg1;
626 efx_vpd_value_t value;
629 value.evv_tag = arg2 >> 16;
630 value.evv_keyword = arg2 & 0xffff;
631 if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
635 return (SYSCTL_OUT(req, value.evv_value, value.evv_length));
639 sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
640 efx_vpd_tag_t tag, const char *keyword)
642 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
643 efx_vpd_value_t value;
645 /* Check whether VPD tag/keyword is present */
647 value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
648 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
652 ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
653 sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
654 sfxge_vpd_handler, "A", "");
658 sfxge_vpd_init(struct sfxge_softc *sc)
660 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
661 struct sysctl_oid *vpd_node;
662 struct sysctl_oid_list *vpd_list;
664 efx_vpd_value_t value;
667 if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0)
669 sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
670 if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
673 /* Copy ID (product name) into device description, and log it. */
674 value.evv_tag = EFX_VPD_ID;
675 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
676 value.evv_value[value.evv_length] = 0;
677 device_set_desc_copy(sc->dev, value.evv_value);
678 device_printf(sc->dev, "%s\n", value.evv_value);
681 vpd_node = SYSCTL_ADD_NODE(
682 ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
683 OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
684 vpd_list = SYSCTL_CHILDREN(vpd_node);
686 /* Add sysctls for all expected and any vendor-defined keywords. */
687 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
688 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
689 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
692 for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
693 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
694 for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
695 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
700 free(sc->vpd_data, M_SFXGE);
706 sfxge_vpd_fini(struct sfxge_softc *sc)
708 free(sc->vpd_data, M_SFXGE);
712 sfxge_reset(void *arg, int npending)
714 struct sfxge_softc *sc;
719 sc = (struct sfxge_softc *)arg;
721 SFXGE_ADAPTER_LOCK(sc);
723 if (sc->init_state != SFXGE_STARTED)
727 efx_nic_reset(sc->enp);
728 if ((rc = sfxge_start(sc)) != 0)
729 device_printf(sc->dev,
730 "reset failed (%d); interface is now stopped\n",
734 SFXGE_ADAPTER_UNLOCK(sc);
738 sfxge_schedule_reset(struct sfxge_softc *sc)
740 taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
744 sfxge_attach(device_t dev)
746 struct sfxge_softc *sc;
750 sc = device_get_softc(dev);
753 /* Allocate ifnet. */
754 ifp = if_alloc(IFT_ETHER);
756 device_printf(dev, "Couldn't allocate ifnet\n");
762 /* Initialize hardware. */
763 if ((error = sfxge_create(sc)) != 0)
766 /* Create the ifnet for the port. */
767 if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
770 if ((error = sfxge_vpd_init(sc)) != 0)
773 sc->init_state = SFXGE_REGISTERED;
778 sfxge_ifnet_fini(ifp);
790 sfxge_detach(device_t dev)
792 struct sfxge_softc *sc;
794 sc = device_get_softc(dev);
798 /* Destroy the ifnet. */
799 sfxge_ifnet_fini(sc->ifnet);
801 /* Tear down hardware. */
808 sfxge_probe(device_t dev)
810 uint16_t pci_vendor_id;
811 uint16_t pci_device_id;
815 pci_vendor_id = pci_get_vendor(dev);
816 pci_device_id = pci_get_device(dev);
818 rc = efx_family(pci_vendor_id, pci_device_id, &family);
822 KASSERT(family == EFX_FAMILY_SIENA, ("impossible controller family"));
823 device_set_desc(dev, "Solarflare SFC9000 family");
827 static device_method_t sfxge_methods[] = {
828 DEVMETHOD(device_probe, sfxge_probe),
829 DEVMETHOD(device_attach, sfxge_attach),
830 DEVMETHOD(device_detach, sfxge_detach),
835 static devclass_t sfxge_devclass;
837 static driver_t sfxge_driver = {
840 sizeof(struct sfxge_softc)
843 DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);