1e47937d1SCarl Delsey /*- 29a5325c2SAlexander Motin * Copyright (c) 2016 Alexander Motin <mav@FreeBSD.org> 3e47937d1SCarl Delsey * Copyright (C) 2013 Intel Corporation 4763fa8aeSConrad Meyer * Copyright (C) 2015 EMC Corporation 5e47937d1SCarl Delsey * All rights reserved. 6e47937d1SCarl Delsey * 7e47937d1SCarl Delsey * Redistribution and use in source and binary forms, with or without 8e47937d1SCarl Delsey * modification, are permitted provided that the following conditions 9e47937d1SCarl Delsey * are met: 10e47937d1SCarl Delsey * 1. Redistributions of source code must retain the above copyright 11e47937d1SCarl Delsey * notice, this list of conditions and the following disclaimer. 12e47937d1SCarl Delsey * 2. Redistributions in binary form must reproduce the above copyright 13e47937d1SCarl Delsey * notice, this list of conditions and the following disclaimer in the 14e47937d1SCarl Delsey * documentation and/or other materials provided with the distribution. 15e47937d1SCarl Delsey * 16e47937d1SCarl Delsey * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17e47937d1SCarl Delsey * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18e47937d1SCarl Delsey * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19e47937d1SCarl Delsey * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20e47937d1SCarl Delsey * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21e47937d1SCarl Delsey * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22e47937d1SCarl Delsey * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23e47937d1SCarl Delsey * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24e47937d1SCarl Delsey * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25e47937d1SCarl Delsey * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26e47937d1SCarl Delsey * SUCH DAMAGE. 27e47937d1SCarl Delsey */ 28e47937d1SCarl Delsey 299a5325c2SAlexander Motin /* 309a5325c2SAlexander Motin * The Non-Transparent Bridge (NTB) is a device that allows you to connect 319a5325c2SAlexander Motin * two or more systems using a PCI-e links, providing remote memory access. 329a5325c2SAlexander Motin * 339a5325c2SAlexander Motin * This module contains a driver for simulated Ethernet device, using 349a5325c2SAlexander Motin * underlying NTB Transport device. 359a5325c2SAlexander Motin * 369a5325c2SAlexander Motin * NOTE: Much of the code in this module is shared with Linux. Any patches may 379a5325c2SAlexander Motin * be picked up and redistributed in Linux with a dual GPL/BSD license. 389a5325c2SAlexander Motin */ 399a5325c2SAlexander Motin 40e47937d1SCarl Delsey #include <sys/cdefs.h> 41e47937d1SCarl Delsey __FBSDID("$FreeBSD$"); 42e47937d1SCarl Delsey 43e47937d1SCarl Delsey #include <sys/param.h> 44e47937d1SCarl Delsey #include <sys/kernel.h> 45e47937d1SCarl Delsey #include <sys/systm.h> 468795de77SAlexander Motin #include <sys/buf_ring.h> 47e47937d1SCarl Delsey #include <sys/bus.h> 48737bc501SConrad Meyer #include <sys/limits.h> 49e47937d1SCarl Delsey #include <sys/module.h> 50e47937d1SCarl Delsey #include <sys/socket.h> 51e47937d1SCarl Delsey #include <sys/sockio.h> 528795de77SAlexander Motin #include <sys/sysctl.h> 538795de77SAlexander Motin #include <sys/taskqueue.h> 54b67ddac2SConrad Meyer 55e47937d1SCarl Delsey #include <net/if.h> 56e47937d1SCarl Delsey #include <net/if_media.h> 57e47937d1SCarl Delsey #include <net/if_types.h> 588795de77SAlexander Motin #include <net/if_media.h> 59e47937d1SCarl Delsey #include <net/if_var.h> 60e47937d1SCarl Delsey #include <net/bpf.h> 61e47937d1SCarl Delsey #include <net/ethernet.h> 62b67ddac2SConrad Meyer 63e47937d1SCarl Delsey #include <machine/bus.h> 64e47937d1SCarl Delsey 659a5325c2SAlexander Motin #include "../ntb_transport.h" 66e47937d1SCarl Delsey 67e47937d1SCarl Delsey #define KTR_NTB KTR_SPARE3 688795de77SAlexander Motin #define NTB_MEDIATYPE (IFM_ETHER | IFM_AUTO | IFM_FDX) 69e47937d1SCarl Delsey 708795de77SAlexander Motin static SYSCTL_NODE(_hw, OID_AUTO, if_ntb, CTLFLAG_RW, 0, "if_ntb"); 718795de77SAlexander Motin 728795de77SAlexander Motin static unsigned g_if_ntb_num_queues = 1; 738795de77SAlexander Motin SYSCTL_UINT(_hw_if_ntb, OID_AUTO, num_queues, CTLFLAG_RWTUN, 748795de77SAlexander Motin &g_if_ntb_num_queues, 0, "Number of queues per interface"); 758795de77SAlexander Motin 768795de77SAlexander Motin struct ntb_net_queue { 778795de77SAlexander Motin struct ntb_net_ctx *sc; 788795de77SAlexander Motin if_t ifp; 799a5325c2SAlexander Motin struct ntb_transport_qp *qp; 808795de77SAlexander Motin struct buf_ring *br; 818795de77SAlexander Motin struct task tx_task; 828795de77SAlexander Motin struct taskqueue *tx_tq; 83e47937d1SCarl Delsey struct mtx tx_lock; 849a5325c2SAlexander Motin struct callout queue_full; 85e47937d1SCarl Delsey }; 86e47937d1SCarl Delsey 878795de77SAlexander Motin struct ntb_net_ctx { 888795de77SAlexander Motin if_t ifp; 898795de77SAlexander Motin struct ifmedia media; 908795de77SAlexander Motin u_char eaddr[ETHER_ADDR_LEN]; 918795de77SAlexander Motin int num_queues; 928795de77SAlexander Motin struct ntb_net_queue *queues; 938795de77SAlexander Motin int mtu; 948795de77SAlexander Motin }; 958795de77SAlexander Motin 969a5325c2SAlexander Motin static int ntb_net_probe(device_t dev); 979a5325c2SAlexander Motin static int ntb_net_attach(device_t dev); 989a5325c2SAlexander Motin static int ntb_net_detach(device_t dev); 99e47937d1SCarl Delsey static void ntb_net_init(void *arg); 1008795de77SAlexander Motin static int ntb_ifmedia_upd(struct ifnet *); 1018795de77SAlexander Motin static void ntb_ifmedia_sts(struct ifnet *, struct ifmediareq *); 1028795de77SAlexander Motin static int ntb_ioctl(if_t ifp, u_long command, caddr_t data); 1038795de77SAlexander Motin static int ntb_transmit(if_t ifp, struct mbuf *m); 104e47937d1SCarl Delsey static void ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data, 105e47937d1SCarl Delsey void *data, int len); 106e47937d1SCarl Delsey static void ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data, 107e47937d1SCarl Delsey void *data, int len); 108531c7b99SConrad Meyer static void ntb_net_event_handler(void *data, enum ntb_link_event status); 1098795de77SAlexander Motin static void ntb_handle_tx(void *arg, int pending); 110e47937d1SCarl Delsey static void ntb_qp_full(void *arg); 1118795de77SAlexander Motin static void ntb_qflush(if_t ifp); 112e47937d1SCarl Delsey static void create_random_local_eui48(u_char *eaddr); 113e47937d1SCarl Delsey 114e47937d1SCarl Delsey static int 1159a5325c2SAlexander Motin ntb_net_probe(device_t dev) 116e47937d1SCarl Delsey { 1179a5325c2SAlexander Motin 1189a5325c2SAlexander Motin device_set_desc(dev, "NTB Network Interface"); 1199a5325c2SAlexander Motin return (0); 1209a5325c2SAlexander Motin } 1219a5325c2SAlexander Motin 1229a5325c2SAlexander Motin static int 1239a5325c2SAlexander Motin ntb_net_attach(device_t dev) 1249a5325c2SAlexander Motin { 1259a5325c2SAlexander Motin struct ntb_net_ctx *sc = device_get_softc(dev); 1268795de77SAlexander Motin struct ntb_net_queue *q; 1278795de77SAlexander Motin if_t ifp; 128e47937d1SCarl Delsey struct ntb_queue_handlers handlers = { ntb_net_rx_handler, 129e47937d1SCarl Delsey ntb_net_tx_handler, ntb_net_event_handler }; 1308795de77SAlexander Motin int i; 131e47937d1SCarl Delsey 1328795de77SAlexander Motin ifp = sc->ifp = if_gethandle(IFT_ETHER); 133e47937d1SCarl Delsey if (ifp == NULL) { 13495a3f7fbSConrad Meyer printf("ntb: Cannot allocate ifnet structure\n"); 135e47937d1SCarl Delsey return (ENOMEM); 136e47937d1SCarl Delsey } 1379a5325c2SAlexander Motin if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 1388795de77SAlexander Motin if_setdev(ifp, dev); 13998bdb1ceSConrad Meyer 1408795de77SAlexander Motin sc->num_queues = g_if_ntb_num_queues; 1418795de77SAlexander Motin sc->queues = malloc(sc->num_queues * sizeof(struct ntb_net_queue), 1428795de77SAlexander Motin M_DEVBUF, M_WAITOK | M_ZERO); 1438795de77SAlexander Motin sc->mtu = INT_MAX; 1448795de77SAlexander Motin for (i = 0; i < sc->num_queues; i++) { 1458795de77SAlexander Motin q = &sc->queues[i]; 1468795de77SAlexander Motin q->sc = sc; 1478795de77SAlexander Motin q->ifp = ifp; 1488795de77SAlexander Motin q->qp = ntb_transport_create_queue(q, 1498795de77SAlexander Motin device_get_parent(dev), &handlers); 1508795de77SAlexander Motin if (q->qp == NULL) 1518795de77SAlexander Motin break; 1528795de77SAlexander Motin sc->mtu = imin(sc->mtu, ntb_transport_max_size(q->qp)); 1538795de77SAlexander Motin mtx_init(&q->tx_lock, "ntb tx", NULL, MTX_DEF); 1548795de77SAlexander Motin q->br = buf_ring_alloc(4096, M_DEVBUF, M_WAITOK, &q->tx_lock); 1558795de77SAlexander Motin TASK_INIT(&q->tx_task, 0, ntb_handle_tx, q); 1568795de77SAlexander Motin q->tx_tq = taskqueue_create_fast("ntb_txq", M_NOWAIT, 1578795de77SAlexander Motin taskqueue_thread_enqueue, &q->tx_tq); 1588795de77SAlexander Motin taskqueue_start_threads(&q->tx_tq, 1, PI_NET, "%s txq%d", 1598795de77SAlexander Motin device_get_nameunit(dev), i); 1608795de77SAlexander Motin callout_init(&q->queue_full, 1); 1618795de77SAlexander Motin } 1628795de77SAlexander Motin sc->num_queues = i; 163e47937d1SCarl Delsey 1648795de77SAlexander Motin if_setinitfn(ifp, ntb_net_init); 1658795de77SAlexander Motin if_setsoftc(ifp, sc); 1668795de77SAlexander Motin if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 1678795de77SAlexander Motin if_setioctlfn(ifp, ntb_ioctl); 1688795de77SAlexander Motin if_settransmitfn(ifp, ntb_transmit); 1698795de77SAlexander Motin if_setqflushfn(ifp, ntb_qflush); 1709a5325c2SAlexander Motin create_random_local_eui48(sc->eaddr); 1719a5325c2SAlexander Motin ether_ifattach(ifp, sc->eaddr); 1728795de77SAlexander Motin if_setcapabilities(ifp, IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 | 1738795de77SAlexander Motin IFCAP_JUMBO_MTU | IFCAP_LINKSTATE); 1748795de77SAlexander Motin if_setcapenable(ifp, if_getcapabilities(ifp)); 1758795de77SAlexander Motin if_setmtu(ifp, sc->mtu - ETHER_HDR_LEN); 176e47937d1SCarl Delsey 1778795de77SAlexander Motin ifmedia_init(&sc->media, IFM_IMASK, ntb_ifmedia_upd, 1788795de77SAlexander Motin ntb_ifmedia_sts); 1798795de77SAlexander Motin ifmedia_add(&sc->media, NTB_MEDIATYPE, 0, NULL); 1808795de77SAlexander Motin ifmedia_set(&sc->media, NTB_MEDIATYPE); 1818795de77SAlexander Motin 1828795de77SAlexander Motin for (i = 0; i < sc->num_queues; i++) 1838795de77SAlexander Motin ntb_transport_link_up(sc->queues[i].qp); 184e47937d1SCarl Delsey return (0); 185e47937d1SCarl Delsey } 186e47937d1SCarl Delsey 187e47937d1SCarl Delsey static int 1889a5325c2SAlexander Motin ntb_net_detach(device_t dev) 189e47937d1SCarl Delsey { 1909a5325c2SAlexander Motin struct ntb_net_ctx *sc = device_get_softc(dev); 1918795de77SAlexander Motin struct ntb_net_queue *q; 1928795de77SAlexander Motin int i; 193e47937d1SCarl Delsey 1948795de77SAlexander Motin for (i = 0; i < sc->num_queues; i++) 1958795de77SAlexander Motin ntb_transport_link_down(sc->queues[i].qp); 1969a5325c2SAlexander Motin ether_ifdetach(sc->ifp); 1979a5325c2SAlexander Motin if_free(sc->ifp); 1988795de77SAlexander Motin ifmedia_removeall(&sc->media); 1998795de77SAlexander Motin for (i = 0; i < sc->num_queues; i++) { 2008795de77SAlexander Motin q = &sc->queues[i]; 2018795de77SAlexander Motin ntb_transport_free_queue(q->qp); 2028795de77SAlexander Motin buf_ring_free(q->br, M_DEVBUF); 2038795de77SAlexander Motin callout_drain(&q->queue_full); 2048795de77SAlexander Motin taskqueue_drain_all(q->tx_tq); 2058795de77SAlexander Motin mtx_destroy(&q->tx_lock); 206538779c1SCarl Delsey } 2078795de77SAlexander Motin free(sc->queues, M_DEVBUF); 208e47937d1SCarl Delsey return (0); 209e47937d1SCarl Delsey } 210e47937d1SCarl Delsey 211e47937d1SCarl Delsey /* Network device interface */ 212e47937d1SCarl Delsey 213e47937d1SCarl Delsey static void 214e47937d1SCarl Delsey ntb_net_init(void *arg) 215e47937d1SCarl Delsey { 2169a5325c2SAlexander Motin struct ntb_net_ctx *sc = arg; 2178795de77SAlexander Motin if_t ifp = sc->ifp; 218e47937d1SCarl Delsey 2198795de77SAlexander Motin if_setdrvflagbits(ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE); 2208795de77SAlexander Motin if_link_state_change(ifp, ntb_transport_link_query(sc->queues[0].qp) ? 2218795de77SAlexander Motin LINK_STATE_UP : LINK_STATE_DOWN); 222e47937d1SCarl Delsey } 223e47937d1SCarl Delsey 224e47937d1SCarl Delsey static int 2258795de77SAlexander Motin ntb_ioctl(if_t ifp, u_long command, caddr_t data) 226e47937d1SCarl Delsey { 2278795de77SAlexander Motin struct ntb_net_ctx *sc = if_getsoftc(ifp); 228e47937d1SCarl Delsey struct ifreq *ifr = (struct ifreq *)data; 229e47937d1SCarl Delsey int error = 0; 230e47937d1SCarl Delsey 231e47937d1SCarl Delsey switch (command) { 232e47937d1SCarl Delsey case SIOCSIFMTU: 233e47937d1SCarl Delsey { 2348795de77SAlexander Motin if (ifr->ifr_mtu > sc->mtu - ETHER_HDR_LEN) { 235e47937d1SCarl Delsey error = EINVAL; 236e47937d1SCarl Delsey break; 237e47937d1SCarl Delsey } 238e47937d1SCarl Delsey 2398795de77SAlexander Motin if_setmtu(ifp, ifr->ifr_mtu); 240e47937d1SCarl Delsey break; 241e47937d1SCarl Delsey } 2428795de77SAlexander Motin 2438795de77SAlexander Motin case SIOCSIFMEDIA: 2448795de77SAlexander Motin case SIOCGIFMEDIA: 2458795de77SAlexander Motin error = ifmedia_ioctl(ifp, ifr, &sc->media, command); 2468795de77SAlexander Motin break; 2478795de77SAlexander Motin 248e47937d1SCarl Delsey default: 249e47937d1SCarl Delsey error = ether_ioctl(ifp, command, data); 250e47937d1SCarl Delsey break; 251e47937d1SCarl Delsey } 252e47937d1SCarl Delsey 253e47937d1SCarl Delsey return (error); 254e47937d1SCarl Delsey } 255e47937d1SCarl Delsey 2568795de77SAlexander Motin static int 2578795de77SAlexander Motin ntb_ifmedia_upd(struct ifnet *ifp) 2588795de77SAlexander Motin { 2598795de77SAlexander Motin struct ntb_net_ctx *sc = if_getsoftc(ifp); 2608795de77SAlexander Motin struct ifmedia *ifm = &sc->media; 2618795de77SAlexander Motin 2628795de77SAlexander Motin if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 2638795de77SAlexander Motin return (EINVAL); 2648795de77SAlexander Motin 2658795de77SAlexander Motin return (0); 2668795de77SAlexander Motin } 267e47937d1SCarl Delsey 268e47937d1SCarl Delsey static void 2698795de77SAlexander Motin ntb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 270e47937d1SCarl Delsey { 2718795de77SAlexander Motin struct ntb_net_ctx *sc = if_getsoftc(ifp); 272e47937d1SCarl Delsey 2738795de77SAlexander Motin ifmr->ifm_status = IFM_AVALID; 2748795de77SAlexander Motin ifmr->ifm_active = NTB_MEDIATYPE; 2758795de77SAlexander Motin if (ntb_transport_link_query(sc->queues[0].qp)) 2768795de77SAlexander Motin ifmr->ifm_status |= IFM_ACTIVE; 2778795de77SAlexander Motin } 2788795de77SAlexander Motin 2798795de77SAlexander Motin static void 2808795de77SAlexander Motin ntb_transmit_locked(struct ntb_net_queue *q) 2818795de77SAlexander Motin { 2828795de77SAlexander Motin if_t ifp = q->ifp; 2838795de77SAlexander Motin struct mbuf *m; 2848795de77SAlexander Motin int rc, len; 2858795de77SAlexander Motin short mflags; 2868795de77SAlexander Motin 2878795de77SAlexander Motin CTR0(KTR_NTB, "TX: ntb_transmit_locked"); 2888795de77SAlexander Motin while ((m = drbr_peek(ifp, q->br)) != NULL) { 2898795de77SAlexander Motin CTR1(KTR_NTB, "TX: start mbuf %p", m); 2908795de77SAlexander Motin if_etherbpfmtap(ifp, m); 2918795de77SAlexander Motin len = m->m_pkthdr.len; 2928795de77SAlexander Motin mflags = m->m_flags; 2938795de77SAlexander Motin rc = ntb_transport_tx_enqueue(q->qp, m, m, len); 294e47937d1SCarl Delsey if (rc != 0) { 2958795de77SAlexander Motin CTR2(KTR_NTB, "TX: could not tx mbuf %p: %d", m, rc); 296e47937d1SCarl Delsey if (rc == EAGAIN) { 2978795de77SAlexander Motin drbr_putback(ifp, q->br, m); 2988795de77SAlexander Motin callout_reset_sbt(&q->queue_full, 2998795de77SAlexander Motin SBT_1MS / 4, SBT_1MS / 4, 3008795de77SAlexander Motin ntb_qp_full, q, 0); 3018795de77SAlexander Motin } else { 3028795de77SAlexander Motin m_freem(m); 3038795de77SAlexander Motin drbr_advance(ifp, q->br); 3048795de77SAlexander Motin if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 305e47937d1SCarl Delsey } 306e47937d1SCarl Delsey break; 307e47937d1SCarl Delsey } 3088795de77SAlexander Motin drbr_advance(ifp, q->br); 3098795de77SAlexander Motin if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 3108795de77SAlexander Motin if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 3118795de77SAlexander Motin if (mflags & M_MCAST) 3128795de77SAlexander Motin if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 313e47937d1SCarl Delsey } 3148795de77SAlexander Motin } 3158795de77SAlexander Motin 3168795de77SAlexander Motin static int 3178795de77SAlexander Motin ntb_transmit(if_t ifp, struct mbuf *m) 3188795de77SAlexander Motin { 3198795de77SAlexander Motin struct ntb_net_ctx *sc = if_getsoftc(ifp); 3208795de77SAlexander Motin struct ntb_net_queue *q; 3218795de77SAlexander Motin int error, i; 3228795de77SAlexander Motin 3238795de77SAlexander Motin CTR0(KTR_NTB, "TX: ntb_transmit"); 3248795de77SAlexander Motin if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) 3258795de77SAlexander Motin i = m->m_pkthdr.flowid % sc->num_queues; 3268795de77SAlexander Motin else 3278795de77SAlexander Motin i = curcpu % sc->num_queues; 3288795de77SAlexander Motin q = &sc->queues[i]; 3298795de77SAlexander Motin 3308795de77SAlexander Motin error = drbr_enqueue(ifp, q->br, m); 3318795de77SAlexander Motin if (error) 3328795de77SAlexander Motin return (error); 3338795de77SAlexander Motin 3348795de77SAlexander Motin if (mtx_trylock(&q->tx_lock)) { 3358795de77SAlexander Motin ntb_transmit_locked(q); 3368795de77SAlexander Motin mtx_unlock(&q->tx_lock); 3378795de77SAlexander Motin } else 3388795de77SAlexander Motin taskqueue_enqueue(q->tx_tq, &q->tx_task); 3398795de77SAlexander Motin return (0); 3408795de77SAlexander Motin } 3418795de77SAlexander Motin 3428795de77SAlexander Motin static void 3438795de77SAlexander Motin ntb_handle_tx(void *arg, int pending) 3448795de77SAlexander Motin { 3458795de77SAlexander Motin struct ntb_net_queue *q = arg; 3468795de77SAlexander Motin 3478795de77SAlexander Motin mtx_lock(&q->tx_lock); 3488795de77SAlexander Motin ntb_transmit_locked(q); 3498795de77SAlexander Motin mtx_unlock(&q->tx_lock); 3508795de77SAlexander Motin } 3518795de77SAlexander Motin 3528795de77SAlexander Motin static void 3538795de77SAlexander Motin ntb_qp_full(void *arg) 3548795de77SAlexander Motin { 3558795de77SAlexander Motin struct ntb_net_queue *q = arg; 3568795de77SAlexander Motin 3578795de77SAlexander Motin CTR0(KTR_NTB, "TX: qp_full callout"); 3588795de77SAlexander Motin if (ntb_transport_tx_free_entry(q->qp) > 0) 3598795de77SAlexander Motin taskqueue_enqueue(q->tx_tq, &q->tx_task); 3608795de77SAlexander Motin else 3618795de77SAlexander Motin callout_schedule_sbt(&q->queue_full, 3628795de77SAlexander Motin SBT_1MS / 4, SBT_1MS / 4, 0); 3638795de77SAlexander Motin } 3648795de77SAlexander Motin 3658795de77SAlexander Motin static void 3668795de77SAlexander Motin ntb_qflush(if_t ifp) 3678795de77SAlexander Motin { 3688795de77SAlexander Motin struct ntb_net_ctx *sc = if_getsoftc(ifp); 3698795de77SAlexander Motin struct ntb_net_queue *q; 3708795de77SAlexander Motin struct mbuf *m; 3718795de77SAlexander Motin int i; 3728795de77SAlexander Motin 3738795de77SAlexander Motin for (i = 0; i < sc->num_queues; i++) { 3748795de77SAlexander Motin q = &sc->queues[i]; 3758795de77SAlexander Motin mtx_lock(&q->tx_lock); 3768795de77SAlexander Motin while ((m = buf_ring_dequeue_sc(q->br)) != NULL) 3778795de77SAlexander Motin m_freem(m); 3788795de77SAlexander Motin mtx_unlock(&q->tx_lock); 3798795de77SAlexander Motin } 3808795de77SAlexander Motin if_qflush(ifp); 381e47937d1SCarl Delsey } 382e47937d1SCarl Delsey 383e47937d1SCarl Delsey /* Network Device Callbacks */ 384e47937d1SCarl Delsey static void 385e47937d1SCarl Delsey ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data, 386e47937d1SCarl Delsey int len) 387e47937d1SCarl Delsey { 388e47937d1SCarl Delsey 389e47937d1SCarl Delsey m_freem(data); 390e47937d1SCarl Delsey CTR1(KTR_NTB, "TX: tx_handler freeing mbuf %p", data); 391e47937d1SCarl Delsey } 392e47937d1SCarl Delsey 393e47937d1SCarl Delsey static void 394e47937d1SCarl Delsey ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data, 395e47937d1SCarl Delsey int len) 396e47937d1SCarl Delsey { 3978795de77SAlexander Motin struct ntb_net_queue *q = qp_data; 3988795de77SAlexander Motin struct ntb_net_ctx *sc = q->sc; 399e47937d1SCarl Delsey struct mbuf *m = data; 4008795de77SAlexander Motin if_t ifp = q->ifp; 401e47937d1SCarl Delsey 402b42e3ec6SAlexander Motin CTR1(KTR_NTB, "RX: rx handler (%d)", len); 403b42e3ec6SAlexander Motin if (len < 0) { 404b42e3ec6SAlexander Motin if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 405b42e3ec6SAlexander Motin return; 406b42e3ec6SAlexander Motin } 407b42e3ec6SAlexander Motin 4088795de77SAlexander Motin m->m_pkthdr.rcvif = ifp; 4098795de77SAlexander Motin if (sc->num_queues > 1) { 4108795de77SAlexander Motin m->m_pkthdr.flowid = q - sc->queues; 4118795de77SAlexander Motin M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE); 4128795de77SAlexander Motin } 4138795de77SAlexander Motin if ((if_getcapenable(ifp) & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) == 4148795de77SAlexander Motin (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) { 4159a5325c2SAlexander Motin m->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID; 4168795de77SAlexander Motin } 4178795de77SAlexander Motin if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 4188795de77SAlexander Motin if_input(ifp, m); 419e47937d1SCarl Delsey } 420e47937d1SCarl Delsey 421e47937d1SCarl Delsey static void 422531c7b99SConrad Meyer ntb_net_event_handler(void *data, enum ntb_link_event status) 423e47937d1SCarl Delsey { 4248795de77SAlexander Motin struct ntb_net_queue *q = data; 4258795de77SAlexander Motin int new_state; 426531c7b99SConrad Meyer 427531c7b99SConrad Meyer switch (status) { 428531c7b99SConrad Meyer case NTB_LINK_DOWN: 4298795de77SAlexander Motin new_state = LINK_STATE_DOWN; 430531c7b99SConrad Meyer break; 431531c7b99SConrad Meyer case NTB_LINK_UP: 4328795de77SAlexander Motin new_state = LINK_STATE_UP; 433531c7b99SConrad Meyer break; 434531c7b99SConrad Meyer default: 4358795de77SAlexander Motin new_state = LINK_STATE_UNKNOWN; 4368795de77SAlexander Motin break; 437531c7b99SConrad Meyer } 4388795de77SAlexander Motin if_link_state_change(q->ifp, new_state); 439e47937d1SCarl Delsey } 440e47937d1SCarl Delsey 441e47937d1SCarl Delsey /* Helper functions */ 442e47937d1SCarl Delsey /* TODO: This too should really be part of the kernel */ 443e47937d1SCarl Delsey #define EUI48_MULTICAST 1 << 0 444e47937d1SCarl Delsey #define EUI48_LOCALLY_ADMINISTERED 1 << 1 445e47937d1SCarl Delsey static void 446e47937d1SCarl Delsey create_random_local_eui48(u_char *eaddr) 447e47937d1SCarl Delsey { 448e47937d1SCarl Delsey static uint8_t counter = 0; 449e47937d1SCarl Delsey uint32_t seed = ticks; 450e47937d1SCarl Delsey 451e47937d1SCarl Delsey eaddr[0] = EUI48_LOCALLY_ADMINISTERED; 452e47937d1SCarl Delsey memcpy(&eaddr[1], &seed, sizeof(uint32_t)); 453e47937d1SCarl Delsey eaddr[5] = counter++; 454e47937d1SCarl Delsey } 455e47937d1SCarl Delsey 4569a5325c2SAlexander Motin static device_method_t ntb_net_methods[] = { 4579a5325c2SAlexander Motin /* Device interface */ 4589a5325c2SAlexander Motin DEVMETHOD(device_probe, ntb_net_probe), 4599a5325c2SAlexander Motin DEVMETHOD(device_attach, ntb_net_attach), 4609a5325c2SAlexander Motin DEVMETHOD(device_detach, ntb_net_detach), 4619a5325c2SAlexander Motin DEVMETHOD_END 4629a5325c2SAlexander Motin }; 463e47937d1SCarl Delsey 4649a5325c2SAlexander Motin devclass_t ntb_net_devclass; 4659a5325c2SAlexander Motin static DEFINE_CLASS_0(ntb, ntb_net_driver, ntb_net_methods, 4669a5325c2SAlexander Motin sizeof(struct ntb_net_ctx)); 4679a5325c2SAlexander Motin DRIVER_MODULE(if_ntb, ntb_transport, ntb_net_driver, ntb_net_devclass, 4689a5325c2SAlexander Motin NULL, NULL); 4699a5325c2SAlexander Motin MODULE_DEPEND(if_ntb, ntb_transport, 1, 1, 1); 4709a5325c2SAlexander Motin MODULE_VERSION(if_ntb, 1); 471