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> 48e2e050c8SConrad Meyer #include <sys/ktr.h> 49737bc501SConrad Meyer #include <sys/limits.h> 50e47937d1SCarl Delsey #include <sys/module.h> 51e47937d1SCarl Delsey #include <sys/socket.h> 52e47937d1SCarl Delsey #include <sys/sockio.h> 538795de77SAlexander Motin #include <sys/sysctl.h> 548795de77SAlexander Motin #include <sys/taskqueue.h> 55b67ddac2SConrad Meyer 56e47937d1SCarl Delsey #include <net/if.h> 57e47937d1SCarl Delsey #include <net/if_media.h> 58e47937d1SCarl Delsey #include <net/if_types.h> 598795de77SAlexander Motin #include <net/if_media.h> 60e47937d1SCarl Delsey #include <net/if_var.h> 61e47937d1SCarl Delsey #include <net/bpf.h> 62e47937d1SCarl Delsey #include <net/ethernet.h> 63b67ddac2SConrad Meyer 64e47937d1SCarl Delsey #include <machine/bus.h> 65e47937d1SCarl Delsey 669a5325c2SAlexander Motin #include "../ntb_transport.h" 67e47937d1SCarl Delsey 68e47937d1SCarl Delsey #define KTR_NTB KTR_SPARE3 698795de77SAlexander Motin #define NTB_MEDIATYPE (IFM_ETHER | IFM_AUTO | IFM_FDX) 70e47937d1SCarl Delsey 71c266ab38SAlexander Motin #define NTB_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP) 72c266ab38SAlexander Motin #define NTB_CSUM_FEATURES6 (CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_SCTP_IPV6) 73c266ab38SAlexander Motin #define NTB_CSUM_SET (CSUM_DATA_VALID | CSUM_DATA_VALID_IPV6 | \ 74c266ab38SAlexander Motin CSUM_PSEUDO_HDR | \ 75c266ab38SAlexander Motin CSUM_IP_CHECKED | CSUM_IP_VALID | \ 76c266ab38SAlexander Motin CSUM_SCTP_VALID) 77c266ab38SAlexander Motin 788795de77SAlexander Motin static SYSCTL_NODE(_hw, OID_AUTO, if_ntb, CTLFLAG_RW, 0, "if_ntb"); 798795de77SAlexander Motin 806bd57d14SAlexander Motin static unsigned g_if_ntb_num_queues = UINT_MAX; 818795de77SAlexander Motin SYSCTL_UINT(_hw_if_ntb, OID_AUTO, num_queues, CTLFLAG_RWTUN, 828795de77SAlexander Motin &g_if_ntb_num_queues, 0, "Number of queues per interface"); 838795de77SAlexander Motin 848795de77SAlexander Motin struct ntb_net_queue { 858795de77SAlexander Motin struct ntb_net_ctx *sc; 868795de77SAlexander Motin if_t ifp; 879a5325c2SAlexander Motin struct ntb_transport_qp *qp; 888795de77SAlexander Motin struct buf_ring *br; 898795de77SAlexander Motin struct task tx_task; 908795de77SAlexander Motin struct taskqueue *tx_tq; 91e47937d1SCarl Delsey struct mtx tx_lock; 929a5325c2SAlexander Motin struct callout queue_full; 93e47937d1SCarl Delsey }; 94e47937d1SCarl Delsey 958795de77SAlexander Motin struct ntb_net_ctx { 968795de77SAlexander Motin if_t ifp; 978795de77SAlexander Motin struct ifmedia media; 988795de77SAlexander Motin u_char eaddr[ETHER_ADDR_LEN]; 998795de77SAlexander Motin int num_queues; 1008795de77SAlexander Motin struct ntb_net_queue *queues; 1018795de77SAlexander Motin int mtu; 1028795de77SAlexander Motin }; 1038795de77SAlexander Motin 1049a5325c2SAlexander Motin static int ntb_net_probe(device_t dev); 1059a5325c2SAlexander Motin static int ntb_net_attach(device_t dev); 1069a5325c2SAlexander Motin static int ntb_net_detach(device_t dev); 107e47937d1SCarl Delsey static void ntb_net_init(void *arg); 1088795de77SAlexander Motin static int ntb_ifmedia_upd(struct ifnet *); 1098795de77SAlexander Motin static void ntb_ifmedia_sts(struct ifnet *, struct ifmediareq *); 1108795de77SAlexander Motin static int ntb_ioctl(if_t ifp, u_long command, caddr_t data); 1118795de77SAlexander Motin static int ntb_transmit(if_t ifp, struct mbuf *m); 112e47937d1SCarl Delsey static void ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data, 113e47937d1SCarl Delsey void *data, int len); 114e47937d1SCarl Delsey static void ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data, 115e47937d1SCarl Delsey void *data, int len); 116531c7b99SConrad Meyer static void ntb_net_event_handler(void *data, enum ntb_link_event status); 1178795de77SAlexander Motin static void ntb_handle_tx(void *arg, int pending); 118e47937d1SCarl Delsey static void ntb_qp_full(void *arg); 1198795de77SAlexander Motin static void ntb_qflush(if_t ifp); 120e47937d1SCarl Delsey static void create_random_local_eui48(u_char *eaddr); 121e47937d1SCarl Delsey 122e47937d1SCarl Delsey static int 1239a5325c2SAlexander Motin ntb_net_probe(device_t dev) 124e47937d1SCarl Delsey { 1259a5325c2SAlexander Motin 1269a5325c2SAlexander Motin device_set_desc(dev, "NTB Network Interface"); 1279a5325c2SAlexander Motin return (0); 1289a5325c2SAlexander Motin } 1299a5325c2SAlexander Motin 1309a5325c2SAlexander Motin static int 1319a5325c2SAlexander Motin ntb_net_attach(device_t dev) 1329a5325c2SAlexander Motin { 1339a5325c2SAlexander Motin struct ntb_net_ctx *sc = device_get_softc(dev); 1348795de77SAlexander Motin struct ntb_net_queue *q; 1358795de77SAlexander Motin if_t ifp; 136e47937d1SCarl Delsey struct ntb_queue_handlers handlers = { ntb_net_rx_handler, 137e47937d1SCarl Delsey ntb_net_tx_handler, ntb_net_event_handler }; 1388795de77SAlexander Motin int i; 139e47937d1SCarl Delsey 1408795de77SAlexander Motin ifp = sc->ifp = if_gethandle(IFT_ETHER); 141e47937d1SCarl Delsey if (ifp == NULL) { 14295a3f7fbSConrad Meyer printf("ntb: Cannot allocate ifnet structure\n"); 143e47937d1SCarl Delsey return (ENOMEM); 144e47937d1SCarl Delsey } 1459a5325c2SAlexander Motin if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 1468795de77SAlexander Motin if_setdev(ifp, dev); 14798bdb1ceSConrad Meyer 1486bd57d14SAlexander Motin sc->num_queues = min(g_if_ntb_num_queues, 1496bd57d14SAlexander Motin ntb_transport_queue_count(dev)); 1508795de77SAlexander Motin sc->queues = malloc(sc->num_queues * sizeof(struct ntb_net_queue), 1518795de77SAlexander Motin M_DEVBUF, M_WAITOK | M_ZERO); 1528795de77SAlexander Motin sc->mtu = INT_MAX; 1538795de77SAlexander Motin for (i = 0; i < sc->num_queues; i++) { 1548795de77SAlexander Motin q = &sc->queues[i]; 1558795de77SAlexander Motin q->sc = sc; 1568795de77SAlexander Motin q->ifp = ifp; 1576bd57d14SAlexander Motin q->qp = ntb_transport_create_queue(dev, i, &handlers, q); 1588795de77SAlexander Motin if (q->qp == NULL) 1598795de77SAlexander Motin break; 1608795de77SAlexander Motin sc->mtu = imin(sc->mtu, ntb_transport_max_size(q->qp)); 1618795de77SAlexander Motin mtx_init(&q->tx_lock, "ntb tx", NULL, MTX_DEF); 1628795de77SAlexander Motin q->br = buf_ring_alloc(4096, M_DEVBUF, M_WAITOK, &q->tx_lock); 1638795de77SAlexander Motin TASK_INIT(&q->tx_task, 0, ntb_handle_tx, q); 1648795de77SAlexander Motin q->tx_tq = taskqueue_create_fast("ntb_txq", M_NOWAIT, 1658795de77SAlexander Motin taskqueue_thread_enqueue, &q->tx_tq); 1668795de77SAlexander Motin taskqueue_start_threads(&q->tx_tq, 1, PI_NET, "%s txq%d", 1678795de77SAlexander Motin device_get_nameunit(dev), i); 1688795de77SAlexander Motin callout_init(&q->queue_full, 1); 1698795de77SAlexander Motin } 1708795de77SAlexander Motin sc->num_queues = i; 1716bd57d14SAlexander Motin device_printf(dev, "%d queue(s)\n", sc->num_queues); 172e47937d1SCarl Delsey 1738795de77SAlexander Motin if_setinitfn(ifp, ntb_net_init); 1748795de77SAlexander Motin if_setsoftc(ifp, sc); 1758795de77SAlexander Motin if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 1768795de77SAlexander Motin if_setioctlfn(ifp, ntb_ioctl); 1778795de77SAlexander Motin if_settransmitfn(ifp, ntb_transmit); 1788795de77SAlexander Motin if_setqflushfn(ifp, ntb_qflush); 1799a5325c2SAlexander Motin create_random_local_eui48(sc->eaddr); 1809a5325c2SAlexander Motin ether_ifattach(ifp, sc->eaddr); 1818795de77SAlexander Motin if_setcapabilities(ifp, IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 | 1828795de77SAlexander Motin IFCAP_JUMBO_MTU | IFCAP_LINKSTATE); 183c266ab38SAlexander Motin if_setcapenable(ifp, IFCAP_JUMBO_MTU | IFCAP_LINKSTATE); 1848795de77SAlexander Motin if_setmtu(ifp, sc->mtu - ETHER_HDR_LEN); 185e47937d1SCarl Delsey 1868795de77SAlexander Motin ifmedia_init(&sc->media, IFM_IMASK, ntb_ifmedia_upd, 1878795de77SAlexander Motin ntb_ifmedia_sts); 1888795de77SAlexander Motin ifmedia_add(&sc->media, NTB_MEDIATYPE, 0, NULL); 1898795de77SAlexander Motin ifmedia_set(&sc->media, NTB_MEDIATYPE); 1908795de77SAlexander Motin 1918795de77SAlexander Motin for (i = 0; i < sc->num_queues; i++) 1928795de77SAlexander Motin ntb_transport_link_up(sc->queues[i].qp); 193e47937d1SCarl Delsey return (0); 194e47937d1SCarl Delsey } 195e47937d1SCarl Delsey 196e47937d1SCarl Delsey static int 1979a5325c2SAlexander Motin ntb_net_detach(device_t dev) 198e47937d1SCarl Delsey { 1999a5325c2SAlexander Motin struct ntb_net_ctx *sc = device_get_softc(dev); 2008795de77SAlexander Motin struct ntb_net_queue *q; 2018795de77SAlexander Motin int i; 202e47937d1SCarl Delsey 2038795de77SAlexander Motin for (i = 0; i < sc->num_queues; i++) 2048795de77SAlexander Motin ntb_transport_link_down(sc->queues[i].qp); 2059a5325c2SAlexander Motin ether_ifdetach(sc->ifp); 2069a5325c2SAlexander Motin if_free(sc->ifp); 2078795de77SAlexander Motin ifmedia_removeall(&sc->media); 2088795de77SAlexander Motin for (i = 0; i < sc->num_queues; i++) { 2098795de77SAlexander Motin q = &sc->queues[i]; 2108795de77SAlexander Motin ntb_transport_free_queue(q->qp); 2118795de77SAlexander Motin buf_ring_free(q->br, M_DEVBUF); 2128795de77SAlexander Motin callout_drain(&q->queue_full); 2138795de77SAlexander Motin taskqueue_drain_all(q->tx_tq); 2148795de77SAlexander Motin mtx_destroy(&q->tx_lock); 215538779c1SCarl Delsey } 2168795de77SAlexander Motin free(sc->queues, M_DEVBUF); 217e47937d1SCarl Delsey return (0); 218e47937d1SCarl Delsey } 219e47937d1SCarl Delsey 220e47937d1SCarl Delsey /* Network device interface */ 221e47937d1SCarl Delsey 222e47937d1SCarl Delsey static void 223e47937d1SCarl Delsey ntb_net_init(void *arg) 224e47937d1SCarl Delsey { 2259a5325c2SAlexander Motin struct ntb_net_ctx *sc = arg; 2268795de77SAlexander Motin if_t ifp = sc->ifp; 227e47937d1SCarl Delsey 2288795de77SAlexander Motin if_setdrvflagbits(ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE); 22948c47677SAlexander Motin if_setbaudrate(ifp, ntb_transport_link_speed(sc->queues[0].qp)); 2308795de77SAlexander Motin if_link_state_change(ifp, ntb_transport_link_query(sc->queues[0].qp) ? 2318795de77SAlexander Motin LINK_STATE_UP : LINK_STATE_DOWN); 232e47937d1SCarl Delsey } 233e47937d1SCarl Delsey 234e47937d1SCarl Delsey static int 2358795de77SAlexander Motin ntb_ioctl(if_t ifp, u_long command, caddr_t data) 236e47937d1SCarl Delsey { 2378795de77SAlexander Motin struct ntb_net_ctx *sc = if_getsoftc(ifp); 238e47937d1SCarl Delsey struct ifreq *ifr = (struct ifreq *)data; 239e47937d1SCarl Delsey int error = 0; 240e47937d1SCarl Delsey 241e47937d1SCarl Delsey switch (command) { 24247967ab7SAlexander Motin case SIOCSIFFLAGS: 24347967ab7SAlexander Motin case SIOCADDMULTI: 24447967ab7SAlexander Motin case SIOCDELMULTI: 24547967ab7SAlexander Motin break; 24647967ab7SAlexander Motin 247e47937d1SCarl Delsey case SIOCSIFMTU: 248e47937d1SCarl Delsey { 2498795de77SAlexander Motin if (ifr->ifr_mtu > sc->mtu - ETHER_HDR_LEN) { 250e47937d1SCarl Delsey error = EINVAL; 251e47937d1SCarl Delsey break; 252e47937d1SCarl Delsey } 253e47937d1SCarl Delsey 2548795de77SAlexander Motin if_setmtu(ifp, ifr->ifr_mtu); 255e47937d1SCarl Delsey break; 256e47937d1SCarl Delsey } 2578795de77SAlexander Motin 2588795de77SAlexander Motin case SIOCSIFMEDIA: 2598795de77SAlexander Motin case SIOCGIFMEDIA: 2608795de77SAlexander Motin error = ifmedia_ioctl(ifp, ifr, &sc->media, command); 2618795de77SAlexander Motin break; 2628795de77SAlexander Motin 263c266ab38SAlexander Motin case SIOCSIFCAP: 264c266ab38SAlexander Motin if (ifr->ifr_reqcap & IFCAP_RXCSUM) 265c266ab38SAlexander Motin if_setcapenablebit(ifp, IFCAP_RXCSUM, 0); 266c266ab38SAlexander Motin else 267c266ab38SAlexander Motin if_setcapenablebit(ifp, 0, IFCAP_RXCSUM); 268c266ab38SAlexander Motin if (ifr->ifr_reqcap & IFCAP_TXCSUM) { 269c266ab38SAlexander Motin if_setcapenablebit(ifp, IFCAP_TXCSUM, 0); 270c266ab38SAlexander Motin if_sethwassistbits(ifp, NTB_CSUM_FEATURES, 0); 271c266ab38SAlexander Motin } else { 272c266ab38SAlexander Motin if_setcapenablebit(ifp, 0, IFCAP_TXCSUM); 273c266ab38SAlexander Motin if_sethwassistbits(ifp, 0, NTB_CSUM_FEATURES); 274c266ab38SAlexander Motin } 275c266ab38SAlexander Motin if (ifr->ifr_reqcap & IFCAP_RXCSUM_IPV6) 276c266ab38SAlexander Motin if_setcapenablebit(ifp, IFCAP_RXCSUM_IPV6, 0); 277c266ab38SAlexander Motin else 278c266ab38SAlexander Motin if_setcapenablebit(ifp, 0, IFCAP_RXCSUM_IPV6); 279c266ab38SAlexander Motin if (ifr->ifr_reqcap & IFCAP_TXCSUM_IPV6) { 280c266ab38SAlexander Motin if_setcapenablebit(ifp, IFCAP_TXCSUM_IPV6, 0); 281c266ab38SAlexander Motin if_sethwassistbits(ifp, NTB_CSUM_FEATURES6, 0); 282c266ab38SAlexander Motin } else { 283c266ab38SAlexander Motin if_setcapenablebit(ifp, 0, IFCAP_TXCSUM_IPV6); 284c266ab38SAlexander Motin if_sethwassistbits(ifp, 0, NTB_CSUM_FEATURES6); 285c266ab38SAlexander Motin } 286c266ab38SAlexander Motin break; 287c266ab38SAlexander Motin 288e47937d1SCarl Delsey default: 289e47937d1SCarl Delsey error = ether_ioctl(ifp, command, data); 290e47937d1SCarl Delsey break; 291e47937d1SCarl Delsey } 292e47937d1SCarl Delsey 293e47937d1SCarl Delsey return (error); 294e47937d1SCarl Delsey } 295e47937d1SCarl Delsey 2968795de77SAlexander Motin static int 2978795de77SAlexander Motin ntb_ifmedia_upd(struct ifnet *ifp) 2988795de77SAlexander Motin { 2998795de77SAlexander Motin struct ntb_net_ctx *sc = if_getsoftc(ifp); 3008795de77SAlexander Motin struct ifmedia *ifm = &sc->media; 3018795de77SAlexander Motin 3028795de77SAlexander Motin if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) 3038795de77SAlexander Motin return (EINVAL); 3048795de77SAlexander Motin 3058795de77SAlexander Motin return (0); 3068795de77SAlexander Motin } 307e47937d1SCarl Delsey 308e47937d1SCarl Delsey static void 3098795de77SAlexander Motin ntb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 310e47937d1SCarl Delsey { 3118795de77SAlexander Motin struct ntb_net_ctx *sc = if_getsoftc(ifp); 312e47937d1SCarl Delsey 3138795de77SAlexander Motin ifmr->ifm_status = IFM_AVALID; 3148795de77SAlexander Motin ifmr->ifm_active = NTB_MEDIATYPE; 3158795de77SAlexander Motin if (ntb_transport_link_query(sc->queues[0].qp)) 3168795de77SAlexander Motin ifmr->ifm_status |= IFM_ACTIVE; 3178795de77SAlexander Motin } 3188795de77SAlexander Motin 3198795de77SAlexander Motin static void 3208795de77SAlexander Motin ntb_transmit_locked(struct ntb_net_queue *q) 3218795de77SAlexander Motin { 3228795de77SAlexander Motin if_t ifp = q->ifp; 3238795de77SAlexander Motin struct mbuf *m; 3248795de77SAlexander Motin int rc, len; 3258795de77SAlexander Motin short mflags; 3268795de77SAlexander Motin 3278795de77SAlexander Motin CTR0(KTR_NTB, "TX: ntb_transmit_locked"); 3288795de77SAlexander Motin while ((m = drbr_peek(ifp, q->br)) != NULL) { 3298795de77SAlexander Motin CTR1(KTR_NTB, "TX: start mbuf %p", m); 3308795de77SAlexander Motin if_etherbpfmtap(ifp, m); 3318795de77SAlexander Motin len = m->m_pkthdr.len; 3328795de77SAlexander Motin mflags = m->m_flags; 3338795de77SAlexander Motin rc = ntb_transport_tx_enqueue(q->qp, m, m, len); 334e47937d1SCarl Delsey if (rc != 0) { 3358795de77SAlexander Motin CTR2(KTR_NTB, "TX: could not tx mbuf %p: %d", m, rc); 336e47937d1SCarl Delsey if (rc == EAGAIN) { 3378795de77SAlexander Motin drbr_putback(ifp, q->br, m); 3388795de77SAlexander Motin callout_reset_sbt(&q->queue_full, 3398795de77SAlexander Motin SBT_1MS / 4, SBT_1MS / 4, 3408795de77SAlexander Motin ntb_qp_full, q, 0); 3418795de77SAlexander Motin } else { 3428795de77SAlexander Motin m_freem(m); 3438795de77SAlexander Motin drbr_advance(ifp, q->br); 3448795de77SAlexander Motin if_inc_counter(ifp, IFCOUNTER_OERRORS, 1); 345e47937d1SCarl Delsey } 346e47937d1SCarl Delsey break; 347e47937d1SCarl Delsey } 3488795de77SAlexander Motin drbr_advance(ifp, q->br); 3498795de77SAlexander Motin if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 3508795de77SAlexander Motin if_inc_counter(ifp, IFCOUNTER_OBYTES, len); 3518795de77SAlexander Motin if (mflags & M_MCAST) 3528795de77SAlexander Motin if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 353e47937d1SCarl Delsey } 3548795de77SAlexander Motin } 3558795de77SAlexander Motin 3568795de77SAlexander Motin static int 3578795de77SAlexander Motin ntb_transmit(if_t ifp, struct mbuf *m) 3588795de77SAlexander Motin { 3598795de77SAlexander Motin struct ntb_net_ctx *sc = if_getsoftc(ifp); 3608795de77SAlexander Motin struct ntb_net_queue *q; 3618795de77SAlexander Motin int error, i; 3628795de77SAlexander Motin 3638795de77SAlexander Motin CTR0(KTR_NTB, "TX: ntb_transmit"); 3648795de77SAlexander Motin if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) 3658795de77SAlexander Motin i = m->m_pkthdr.flowid % sc->num_queues; 3668795de77SAlexander Motin else 3678795de77SAlexander Motin i = curcpu % sc->num_queues; 3688795de77SAlexander Motin q = &sc->queues[i]; 3698795de77SAlexander Motin 3708795de77SAlexander Motin error = drbr_enqueue(ifp, q->br, m); 3718795de77SAlexander Motin if (error) 3728795de77SAlexander Motin return (error); 3738795de77SAlexander Motin 3748795de77SAlexander Motin if (mtx_trylock(&q->tx_lock)) { 3758795de77SAlexander Motin ntb_transmit_locked(q); 3768795de77SAlexander Motin mtx_unlock(&q->tx_lock); 3778795de77SAlexander Motin } else 3788795de77SAlexander Motin taskqueue_enqueue(q->tx_tq, &q->tx_task); 3798795de77SAlexander Motin return (0); 3808795de77SAlexander Motin } 3818795de77SAlexander Motin 3828795de77SAlexander Motin static void 3838795de77SAlexander Motin ntb_handle_tx(void *arg, int pending) 3848795de77SAlexander Motin { 3858795de77SAlexander Motin struct ntb_net_queue *q = arg; 3868795de77SAlexander Motin 3878795de77SAlexander Motin mtx_lock(&q->tx_lock); 3888795de77SAlexander Motin ntb_transmit_locked(q); 3898795de77SAlexander Motin mtx_unlock(&q->tx_lock); 3908795de77SAlexander Motin } 3918795de77SAlexander Motin 3928795de77SAlexander Motin static void 3938795de77SAlexander Motin ntb_qp_full(void *arg) 3948795de77SAlexander Motin { 3958795de77SAlexander Motin struct ntb_net_queue *q = arg; 3968795de77SAlexander Motin 3978795de77SAlexander Motin CTR0(KTR_NTB, "TX: qp_full callout"); 3988795de77SAlexander Motin if (ntb_transport_tx_free_entry(q->qp) > 0) 3998795de77SAlexander Motin taskqueue_enqueue(q->tx_tq, &q->tx_task); 4008795de77SAlexander Motin else 4018795de77SAlexander Motin callout_schedule_sbt(&q->queue_full, 4028795de77SAlexander Motin SBT_1MS / 4, SBT_1MS / 4, 0); 4038795de77SAlexander Motin } 4048795de77SAlexander Motin 4058795de77SAlexander Motin static void 4068795de77SAlexander Motin ntb_qflush(if_t ifp) 4078795de77SAlexander Motin { 4088795de77SAlexander Motin struct ntb_net_ctx *sc = if_getsoftc(ifp); 4098795de77SAlexander Motin struct ntb_net_queue *q; 4108795de77SAlexander Motin struct mbuf *m; 4118795de77SAlexander Motin int i; 4128795de77SAlexander Motin 4138795de77SAlexander Motin for (i = 0; i < sc->num_queues; i++) { 4148795de77SAlexander Motin q = &sc->queues[i]; 4158795de77SAlexander Motin mtx_lock(&q->tx_lock); 4168795de77SAlexander Motin while ((m = buf_ring_dequeue_sc(q->br)) != NULL) 4178795de77SAlexander Motin m_freem(m); 4188795de77SAlexander Motin mtx_unlock(&q->tx_lock); 4198795de77SAlexander Motin } 4208795de77SAlexander Motin if_qflush(ifp); 421e47937d1SCarl Delsey } 422e47937d1SCarl Delsey 423e47937d1SCarl Delsey /* Network Device Callbacks */ 424e47937d1SCarl Delsey static void 425e47937d1SCarl Delsey ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data, 426e47937d1SCarl Delsey int len) 427e47937d1SCarl Delsey { 428e47937d1SCarl Delsey 429e47937d1SCarl Delsey m_freem(data); 430e47937d1SCarl Delsey CTR1(KTR_NTB, "TX: tx_handler freeing mbuf %p", data); 431e47937d1SCarl Delsey } 432e47937d1SCarl Delsey 433e47937d1SCarl Delsey static void 434e47937d1SCarl Delsey ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data, 435e47937d1SCarl Delsey int len) 436e47937d1SCarl Delsey { 4378795de77SAlexander Motin struct ntb_net_queue *q = qp_data; 4388795de77SAlexander Motin struct ntb_net_ctx *sc = q->sc; 439e47937d1SCarl Delsey struct mbuf *m = data; 4408795de77SAlexander Motin if_t ifp = q->ifp; 441c266ab38SAlexander Motin uint16_t proto; 442e47937d1SCarl Delsey 443b42e3ec6SAlexander Motin CTR1(KTR_NTB, "RX: rx handler (%d)", len); 444b42e3ec6SAlexander Motin if (len < 0) { 445b42e3ec6SAlexander Motin if_inc_counter(ifp, IFCOUNTER_IERRORS, 1); 446b42e3ec6SAlexander Motin return; 447b42e3ec6SAlexander Motin } 448b42e3ec6SAlexander Motin 4498795de77SAlexander Motin m->m_pkthdr.rcvif = ifp; 4508795de77SAlexander Motin if (sc->num_queues > 1) { 4518795de77SAlexander Motin m->m_pkthdr.flowid = q - sc->queues; 4528795de77SAlexander Motin M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE); 4538795de77SAlexander Motin } 454c266ab38SAlexander Motin if (if_getcapenable(ifp) & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) { 455c266ab38SAlexander Motin m_copydata(m, 12, 2, (void *)&proto); 456c266ab38SAlexander Motin switch (ntohs(proto)) { 457c266ab38SAlexander Motin case ETHERTYPE_IP: 458c266ab38SAlexander Motin if (if_getcapenable(ifp) & IFCAP_RXCSUM) { 459c266ab38SAlexander Motin m->m_pkthdr.csum_data = 0xffff; 460c266ab38SAlexander Motin m->m_pkthdr.csum_flags = NTB_CSUM_SET; 461c266ab38SAlexander Motin } 462c266ab38SAlexander Motin break; 463c266ab38SAlexander Motin case ETHERTYPE_IPV6: 464c266ab38SAlexander Motin if (if_getcapenable(ifp) & IFCAP_RXCSUM_IPV6) { 465c266ab38SAlexander Motin m->m_pkthdr.csum_data = 0xffff; 466c266ab38SAlexander Motin m->m_pkthdr.csum_flags = NTB_CSUM_SET; 467c266ab38SAlexander Motin } 468c266ab38SAlexander Motin break; 469c266ab38SAlexander Motin } 4708795de77SAlexander Motin } 4718795de77SAlexander Motin if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 4728795de77SAlexander Motin if_input(ifp, m); 473e47937d1SCarl Delsey } 474e47937d1SCarl Delsey 475e47937d1SCarl Delsey static void 476531c7b99SConrad Meyer ntb_net_event_handler(void *data, enum ntb_link_event status) 477e47937d1SCarl Delsey { 4788795de77SAlexander Motin struct ntb_net_queue *q = data; 479531c7b99SConrad Meyer 48048c47677SAlexander Motin if_setbaudrate(q->ifp, ntb_transport_link_speed(q->qp)); 48148c47677SAlexander Motin if_link_state_change(q->ifp, (status == NTB_LINK_UP) ? LINK_STATE_UP : 48248c47677SAlexander Motin LINK_STATE_DOWN); 483e47937d1SCarl Delsey } 484e47937d1SCarl Delsey 485e47937d1SCarl Delsey /* Helper functions */ 486e47937d1SCarl Delsey /* TODO: This too should really be part of the kernel */ 487e47937d1SCarl Delsey #define EUI48_MULTICAST 1 << 0 488e47937d1SCarl Delsey #define EUI48_LOCALLY_ADMINISTERED 1 << 1 489e47937d1SCarl Delsey static void 490e47937d1SCarl Delsey create_random_local_eui48(u_char *eaddr) 491e47937d1SCarl Delsey { 492e47937d1SCarl Delsey static uint8_t counter = 0; 493e47937d1SCarl Delsey 494e47937d1SCarl Delsey eaddr[0] = EUI48_LOCALLY_ADMINISTERED; 4958260941cSAlexander Motin arc4rand(&eaddr[1], 4, 0); 496e47937d1SCarl Delsey eaddr[5] = counter++; 497e47937d1SCarl Delsey } 498e47937d1SCarl Delsey 4999a5325c2SAlexander Motin static device_method_t ntb_net_methods[] = { 5009a5325c2SAlexander Motin /* Device interface */ 5019a5325c2SAlexander Motin DEVMETHOD(device_probe, ntb_net_probe), 5029a5325c2SAlexander Motin DEVMETHOD(device_attach, ntb_net_attach), 5039a5325c2SAlexander Motin DEVMETHOD(device_detach, ntb_net_detach), 5049a5325c2SAlexander Motin DEVMETHOD_END 5059a5325c2SAlexander Motin }; 506e47937d1SCarl Delsey 5079a5325c2SAlexander Motin devclass_t ntb_net_devclass; 5089a5325c2SAlexander Motin static DEFINE_CLASS_0(ntb, ntb_net_driver, ntb_net_methods, 5099a5325c2SAlexander Motin sizeof(struct ntb_net_ctx)); 5109a5325c2SAlexander Motin DRIVER_MODULE(if_ntb, ntb_transport, ntb_net_driver, ntb_net_devclass, 5119a5325c2SAlexander Motin NULL, NULL); 5129a5325c2SAlexander Motin MODULE_DEPEND(if_ntb, ntb_transport, 1, 1, 1); 5139a5325c2SAlexander Motin MODULE_VERSION(if_ntb, 1); 514