18e0ad55aSJoel Dahl /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3718cf2ccSPedro F. Giffuni * 489e0f4d2SKip Macy * Copyright (c) 2004-2006 Kip Macy 596375eacSRoger Pau Monné * Copyright (c) 2015 Wei Liu <wei.liu2@citrix.com> 689e0f4d2SKip Macy * All rights reserved. 789e0f4d2SKip Macy * 88e0ad55aSJoel Dahl * Redistribution and use in source and binary forms, with or without 98e0ad55aSJoel Dahl * modification, are permitted provided that the following conditions 108e0ad55aSJoel Dahl * are met: 118e0ad55aSJoel Dahl * 1. Redistributions of source code must retain the above copyright 128e0ad55aSJoel Dahl * notice, this list of conditions and the following disclaimer. 138e0ad55aSJoel Dahl * 2. Redistributions in binary form must reproduce the above copyright 148e0ad55aSJoel Dahl * notice, this list of conditions and the following disclaimer in the 158e0ad55aSJoel Dahl * documentation and/or other materials provided with the distribution. 1689e0f4d2SKip Macy * 178e0ad55aSJoel Dahl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 188e0ad55aSJoel Dahl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 198e0ad55aSJoel Dahl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 208e0ad55aSJoel Dahl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 218e0ad55aSJoel Dahl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 228e0ad55aSJoel Dahl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 238e0ad55aSJoel Dahl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 248e0ad55aSJoel Dahl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 258e0ad55aSJoel Dahl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 268e0ad55aSJoel Dahl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 278e0ad55aSJoel Dahl * SUCH DAMAGE. 2889e0f4d2SKip Macy */ 2989e0f4d2SKip Macy 3089e0f4d2SKip Macy #include <sys/cdefs.h> 31a0ae8f04SBjoern A. Zeeb #include "opt_inet.h" 32f909bbb4SGleb Smirnoff #include "opt_inet6.h" 33a0ae8f04SBjoern A. Zeeb 3489e0f4d2SKip Macy #include <sys/param.h> 3589e0f4d2SKip Macy #include <sys/sockio.h> 36c578b6acSGleb Smirnoff #include <sys/limits.h> 3789e0f4d2SKip Macy #include <sys/mbuf.h> 3889e0f4d2SKip Macy #include <sys/malloc.h> 3923dc5621SKip Macy #include <sys/module.h> 4089e0f4d2SKip Macy #include <sys/kernel.h> 4189e0f4d2SKip Macy #include <sys/socket.h> 4212678024SDoug Rabson #include <sys/sysctl.h> 4396375eacSRoger Pau Monné #include <sys/taskqueue.h> 4489e0f4d2SKip Macy 4589e0f4d2SKip Macy #include <net/if.h> 4677374386SGleb Smirnoff #include <net/if_var.h> 4789e0f4d2SKip Macy #include <net/if_arp.h> 4889e0f4d2SKip Macy #include <net/ethernet.h> 4989e0f4d2SKip Macy #include <net/if_media.h> 5089e0f4d2SKip Macy #include <net/bpf.h> 5189e0f4d2SKip Macy #include <net/if_types.h> 5289e0f4d2SKip Macy 5389e0f4d2SKip Macy #include <netinet/in.h> 5489e0f4d2SKip Macy #include <netinet/ip.h> 5589e0f4d2SKip Macy #include <netinet/if_ether.h> 5612678024SDoug Rabson #include <netinet/tcp.h> 5712678024SDoug Rabson #include <netinet/tcp_lro.h> 5889e0f4d2SKip Macy 5989e0f4d2SKip Macy #include <vm/vm.h> 6089e0f4d2SKip Macy #include <vm/pmap.h> 6189e0f4d2SKip Macy 6289e0f4d2SKip Macy #include <sys/bus.h> 6389e0f4d2SKip Macy 6476acc41fSJustin T. Gibbs #include <xen/xen-os.h> 653a6d1fcfSKip Macy #include <xen/hypervisor.h> 663a6d1fcfSKip Macy #include <xen/xen_intr.h> 6789e0f4d2SKip Macy #include <xen/gnttab.h> 68ad7dd514SElliott Mitchell #include <contrib/xen/memory.h> 69ad7dd514SElliott Mitchell #include <contrib/xen/io/netif.h> 7023dc5621SKip Macy #include <xen/xenbus/xenbusvar.h> 7189e0f4d2SKip Macy 72dabb3db7SRoger Pau Monné #include <machine/bus.h> 73dabb3db7SRoger Pau Monné 7423dc5621SKip Macy #include "xenbus_if.h" 7589e0f4d2SKip Macy 76578e4bf7SJustin T. Gibbs /* Features supported by all backends. TSO and LRO can be negotiated */ 77578e4bf7SJustin T. Gibbs #define XN_CSUM_FEATURES (CSUM_TCP | CSUM_UDP) 7812678024SDoug Rabson 7998018db4SRyan Libby #define NET_TX_RING_SIZE __CONST_RING_SIZE(netif_tx, PAGE_SIZE) 8098018db4SRyan Libby #define NET_RX_RING_SIZE __CONST_RING_SIZE(netif_rx, PAGE_SIZE) 8189e0f4d2SKip Macy 822568ee67SRoger Pau Monné #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1) 832568ee67SRoger Pau Monné 8412678024SDoug Rabson /* 8512678024SDoug Rabson * Should the driver do LRO on the RX end 8612678024SDoug Rabson * this can be toggled on the fly, but the 8712678024SDoug Rabson * interface must be reset (down/up) for it 8812678024SDoug Rabson * to take effect. 8912678024SDoug Rabson */ 9012678024SDoug Rabson static int xn_enable_lro = 1; 9112678024SDoug Rabson TUNABLE_INT("hw.xn.enable_lro", &xn_enable_lro); 9212678024SDoug Rabson 9396375eacSRoger Pau Monné /* 9496375eacSRoger Pau Monné * Number of pairs of queues. 9596375eacSRoger Pau Monné */ 9696375eacSRoger Pau Monné static unsigned long xn_num_queues = 4; 9796375eacSRoger Pau Monné TUNABLE_ULONG("hw.xn.num_queues", &xn_num_queues); 9896375eacSRoger Pau Monné 99931eeffaSKenneth D. Merry /** 100931eeffaSKenneth D. Merry * \brief The maximum allowed data fragments in a single transmit 101931eeffaSKenneth D. Merry * request. 102931eeffaSKenneth D. Merry * 103931eeffaSKenneth D. Merry * This limit is imposed by the backend driver. We assume here that 104931eeffaSKenneth D. Merry * we are dealing with a Linux driver domain and have set our limit 105931eeffaSKenneth D. Merry * to mirror the Linux MAX_SKB_FRAGS constant. 106931eeffaSKenneth D. Merry */ 107931eeffaSKenneth D. Merry #define MAX_TX_REQ_FRAGS (65536 / PAGE_SIZE + 2) 108931eeffaSKenneth D. Merry 10989e0f4d2SKip Macy #define RX_COPY_THRESHOLD 256 11089e0f4d2SKip Macy 11189e0f4d2SKip Macy #define net_ratelimit() 0 11289e0f4d2SKip Macy 11396375eacSRoger Pau Monné struct netfront_rxq; 11496375eacSRoger Pau Monné struct netfront_txq; 11589e0f4d2SKip Macy struct netfront_info; 11689e0f4d2SKip Macy struct netfront_rx_info; 11789e0f4d2SKip Macy 11896375eacSRoger Pau Monné static void xn_txeof(struct netfront_txq *); 11996375eacSRoger Pau Monné static void xn_rxeof(struct netfront_rxq *); 12096375eacSRoger Pau Monné static void xn_alloc_rx_buffers(struct netfront_rxq *); 1212568ee67SRoger Pau Monné static void xn_alloc_rx_buffers_callout(void *arg); 12289e0f4d2SKip Macy 12396375eacSRoger Pau Monné static void xn_release_rx_bufs(struct netfront_rxq *); 12496375eacSRoger Pau Monné static void xn_release_tx_bufs(struct netfront_txq *); 12589e0f4d2SKip Macy 126da695b05SRoger Pau Monné static void xn_rxq_intr(struct netfront_rxq *); 127da695b05SRoger Pau Monné static void xn_txq_intr(struct netfront_txq *); 128da695b05SRoger Pau Monné static void xn_intr(void *); 12996375eacSRoger Pau Monné static int xn_assemble_tx_request(struct netfront_txq *, struct mbuf *); 13002f3b17fSJustin Hibbits static int xn_ioctl(if_t, u_long, caddr_t); 13189e0f4d2SKip Macy static void xn_ifinit_locked(struct netfront_info *); 13289e0f4d2SKip Macy static void xn_ifinit(void *); 13389e0f4d2SKip Macy static void xn_stop(struct netfront_info *); 134578e4bf7SJustin T. Gibbs static void xn_query_features(struct netfront_info *np); 135578e4bf7SJustin T. Gibbs static int xn_configure_features(struct netfront_info *np); 13689e0f4d2SKip Macy static void netif_free(struct netfront_info *info); 13723dc5621SKip Macy static int netfront_detach(device_t dev); 13889e0f4d2SKip Macy 13996375eacSRoger Pau Monné static int xn_txq_mq_start_locked(struct netfront_txq *, struct mbuf *); 14002f3b17fSJustin Hibbits static int xn_txq_mq_start(if_t, struct mbuf *); 14196375eacSRoger Pau Monné 14223dc5621SKip Macy static int talk_to_backend(device_t dev, struct netfront_info *info); 14323dc5621SKip Macy static int create_netdev(device_t dev); 14489e0f4d2SKip Macy static void netif_disconnect_backend(struct netfront_info *info); 14596375eacSRoger Pau Monné static int setup_device(device_t dev, struct netfront_info *info, 14696375eacSRoger Pau Monné unsigned long); 14702f3b17fSJustin Hibbits static int xn_ifmedia_upd(if_t ifp); 14802f3b17fSJustin Hibbits static void xn_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr); 1490e509842SJustin T. Gibbs 15065671253SRoger Pau Monné static int xn_connect(struct netfront_info *); 15165671253SRoger Pau Monné static void xn_kick_rings(struct netfront_info *); 15289e0f4d2SKip Macy 15396375eacSRoger Pau Monné static int xn_get_responses(struct netfront_rxq *, 15496375eacSRoger Pau Monné struct netfront_rx_info *, RING_IDX, RING_IDX *, 15596375eacSRoger Pau Monné struct mbuf **); 15689e0f4d2SKip Macy 1573c790178SJohn Baldwin #define virt_to_mfn(x) (vtophys(x) >> PAGE_SHIFT) 15889e0f4d2SKip Macy 15989e0f4d2SKip Macy #define INVALID_P2M_ENTRY (~0UL) 16096375eacSRoger Pau Monné #define XN_QUEUE_NAME_LEN 8 /* xn{t,r}x_%u, allow for two digits */ 16196375eacSRoger Pau Monné struct netfront_rxq { 16296375eacSRoger Pau Monné struct netfront_info *info; 16396375eacSRoger Pau Monné u_int id; 16496375eacSRoger Pau Monné char name[XN_QUEUE_NAME_LEN]; 16596375eacSRoger Pau Monné struct mtx lock; 16696375eacSRoger Pau Monné 16796375eacSRoger Pau Monné int ring_ref; 16896375eacSRoger Pau Monné netif_rx_front_ring_t ring; 16996375eacSRoger Pau Monné xen_intr_handle_t xen_intr_handle; 17096375eacSRoger Pau Monné 17196375eacSRoger Pau Monné grant_ref_t gref_head; 17236ea5721SOlivier Houchard grant_ref_t grant_ref[NET_RX_RING_SIZE + 1]; 17396375eacSRoger Pau Monné 17496375eacSRoger Pau Monné struct mbuf *mbufs[NET_RX_RING_SIZE + 1]; 17596375eacSRoger Pau Monné 17696375eacSRoger Pau Monné struct lro_ctrl lro; 17796375eacSRoger Pau Monné 1782568ee67SRoger Pau Monné struct callout rx_refill; 17996375eacSRoger Pau Monné }; 18096375eacSRoger Pau Monné 18196375eacSRoger Pau Monné struct netfront_txq { 18296375eacSRoger Pau Monné struct netfront_info *info; 18396375eacSRoger Pau Monné u_int id; 18496375eacSRoger Pau Monné char name[XN_QUEUE_NAME_LEN]; 18596375eacSRoger Pau Monné struct mtx lock; 18696375eacSRoger Pau Monné 18796375eacSRoger Pau Monné int ring_ref; 18896375eacSRoger Pau Monné netif_tx_front_ring_t ring; 18996375eacSRoger Pau Monné xen_intr_handle_t xen_intr_handle; 19096375eacSRoger Pau Monné 19196375eacSRoger Pau Monné grant_ref_t gref_head; 19296375eacSRoger Pau Monné grant_ref_t grant_ref[NET_TX_RING_SIZE + 1]; 19396375eacSRoger Pau Monné 19496375eacSRoger Pau Monné struct mbuf *mbufs[NET_TX_RING_SIZE + 1]; 19596375eacSRoger Pau Monné int mbufs_cnt; 19696375eacSRoger Pau Monné struct buf_ring *br; 19796375eacSRoger Pau Monné 19896375eacSRoger Pau Monné struct taskqueue *tq; 19996375eacSRoger Pau Monné struct task defrtask; 20096375eacSRoger Pau Monné 201dabb3db7SRoger Pau Monné bus_dma_segment_t segs[MAX_TX_REQ_FRAGS]; 202dabb3db7SRoger Pau Monné struct mbuf_xennet { 203dabb3db7SRoger Pau Monné struct m_tag tag; 204dabb3db7SRoger Pau Monné bus_dma_tag_t dma_tag; 205dabb3db7SRoger Pau Monné bus_dmamap_t dma_map; 206dabb3db7SRoger Pau Monné struct netfront_txq *txq; 207dabb3db7SRoger Pau Monné SLIST_ENTRY(mbuf_xennet) next; 208dabb3db7SRoger Pau Monné u_int count; 209dabb3db7SRoger Pau Monné } xennet_tag[NET_TX_RING_SIZE + 1]; 210dabb3db7SRoger Pau Monné SLIST_HEAD(, mbuf_xennet) tags; 211dabb3db7SRoger Pau Monné 21296375eacSRoger Pau Monné bool full; 21396375eacSRoger Pau Monné }; 21496375eacSRoger Pau Monné 21589e0f4d2SKip Macy struct netfront_info { 21602f3b17fSJustin Hibbits if_t xn_ifp; 21789e0f4d2SKip Macy 218227ca257SKip Macy struct mtx sc_lock; 21989e0f4d2SKip Macy 22096375eacSRoger Pau Monné u_int num_queues; 22196375eacSRoger Pau Monné struct netfront_rxq *rxq; 22296375eacSRoger Pau Monné struct netfront_txq *txq; 22396375eacSRoger Pau Monné 22489e0f4d2SKip Macy u_int carrier; 225578e4bf7SJustin T. Gibbs u_int maxfrags; 22689e0f4d2SKip Macy 22723dc5621SKip Macy device_t xbdev; 22889e0f4d2SKip Macy uint8_t mac[ETHER_ADDR_LEN]; 22989e0f4d2SKip Macy 23089e0f4d2SKip Macy int xn_if_flags; 23189e0f4d2SKip Macy 2320e509842SJustin T. Gibbs struct ifmedia sc_media; 2336a8e9695SRoger Pau Monné 234dabb3db7SRoger Pau Monné bus_dma_tag_t dma_tag; 235dabb3db7SRoger Pau Monné 236c2d12e5eSRoger Pau Monné bool xn_reset; 23789e0f4d2SKip Macy }; 23889e0f4d2SKip Macy 23996375eacSRoger Pau Monné struct netfront_rx_info { 24096375eacSRoger Pau Monné struct netif_rx_response rx; 24196375eacSRoger Pau Monné struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1]; 24296375eacSRoger Pau Monné }; 24389e0f4d2SKip Macy 24496375eacSRoger Pau Monné #define XN_RX_LOCK(_q) mtx_lock(&(_q)->lock) 24596375eacSRoger Pau Monné #define XN_RX_UNLOCK(_q) mtx_unlock(&(_q)->lock) 24689e0f4d2SKip Macy 24796375eacSRoger Pau Monné #define XN_TX_LOCK(_q) mtx_lock(&(_q)->lock) 24896375eacSRoger Pau Monné #define XN_TX_TRYLOCK(_q) mtx_trylock(&(_q)->lock) 24996375eacSRoger Pau Monné #define XN_TX_UNLOCK(_q) mtx_unlock(&(_q)->lock) 25089e0f4d2SKip Macy 251227ca257SKip Macy #define XN_LOCK(_sc) mtx_lock(&(_sc)->sc_lock); 252227ca257SKip Macy #define XN_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_lock); 25389e0f4d2SKip Macy 254227ca257SKip Macy #define XN_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->sc_lock, MA_OWNED); 25596375eacSRoger Pau Monné #define XN_RX_LOCK_ASSERT(_q) mtx_assert(&(_q)->lock, MA_OWNED); 25696375eacSRoger Pau Monné #define XN_TX_LOCK_ASSERT(_q) mtx_assert(&(_q)->lock, MA_OWNED); 25789e0f4d2SKip Macy 25889e0f4d2SKip Macy #define netfront_carrier_on(netif) ((netif)->carrier = 1) 25989e0f4d2SKip Macy #define netfront_carrier_off(netif) ((netif)->carrier = 0) 26089e0f4d2SKip Macy #define netfront_carrier_ok(netif) ((netif)->carrier) 26189e0f4d2SKip Macy 26289e0f4d2SKip Macy /* Access macros for acquiring freeing slots in xn_free_{tx,rx}_idxs[]. */ 26389e0f4d2SKip Macy 26489e0f4d2SKip Macy static inline void 265931eeffaSKenneth D. Merry add_id_to_freelist(struct mbuf **list, uintptr_t id) 26689e0f4d2SKip Macy { 26796375eacSRoger Pau Monné 268931eeffaSKenneth D. Merry KASSERT(id != 0, 269931eeffaSKenneth D. Merry ("%s: the head item (0) must always be free.", __func__)); 27089e0f4d2SKip Macy list[id] = list[0]; 271931eeffaSKenneth D. Merry list[0] = (struct mbuf *)id; 27289e0f4d2SKip Macy } 27389e0f4d2SKip Macy 27489e0f4d2SKip Macy static inline unsigned short 27589e0f4d2SKip Macy get_id_from_freelist(struct mbuf **list) 27689e0f4d2SKip Macy { 277931eeffaSKenneth D. Merry uintptr_t id; 278931eeffaSKenneth D. Merry 279931eeffaSKenneth D. Merry id = (uintptr_t)list[0]; 280931eeffaSKenneth D. Merry KASSERT(id != 0, 281931eeffaSKenneth D. Merry ("%s: the head item (0) must always remain free.", __func__)); 28289e0f4d2SKip Macy list[0] = list[id]; 28389e0f4d2SKip Macy return (id); 28489e0f4d2SKip Macy } 28589e0f4d2SKip Macy 28689e0f4d2SKip Macy static inline int 28796375eacSRoger Pau Monné xn_rxidx(RING_IDX idx) 28889e0f4d2SKip Macy { 28996375eacSRoger Pau Monné 29089e0f4d2SKip Macy return idx & (NET_RX_RING_SIZE - 1); 29189e0f4d2SKip Macy } 29289e0f4d2SKip Macy 29389e0f4d2SKip Macy static inline struct mbuf * 29496375eacSRoger Pau Monné xn_get_rx_mbuf(struct netfront_rxq *rxq, RING_IDX ri) 29589e0f4d2SKip Macy { 29696375eacSRoger Pau Monné int i; 29789e0f4d2SKip Macy struct mbuf *m; 29889e0f4d2SKip Macy 29996375eacSRoger Pau Monné i = xn_rxidx(ri); 30096375eacSRoger Pau Monné m = rxq->mbufs[i]; 30196375eacSRoger Pau Monné rxq->mbufs[i] = NULL; 30289e0f4d2SKip Macy return (m); 30389e0f4d2SKip Macy } 30489e0f4d2SKip Macy 30589e0f4d2SKip Macy static inline grant_ref_t 30696375eacSRoger Pau Monné xn_get_rx_ref(struct netfront_rxq *rxq, RING_IDX ri) 30789e0f4d2SKip Macy { 30896375eacSRoger Pau Monné int i = xn_rxidx(ri); 30996375eacSRoger Pau Monné grant_ref_t ref = rxq->grant_ref[i]; 31096375eacSRoger Pau Monné 311ff662b5cSJustin T. Gibbs KASSERT(ref != GRANT_REF_INVALID, ("Invalid grant reference!\n")); 31296375eacSRoger Pau Monné rxq->grant_ref[i] = GRANT_REF_INVALID; 31396375eacSRoger Pau Monné return (ref); 31489e0f4d2SKip Macy } 31589e0f4d2SKip Macy 316dabb3db7SRoger Pau Monné #define MTAG_COOKIE 1218492000 317dabb3db7SRoger Pau Monné #define MTAG_XENNET 0 318dabb3db7SRoger Pau Monné 319dabb3db7SRoger Pau Monné static void mbuf_grab(struct mbuf *m) 320dabb3db7SRoger Pau Monné { 321dabb3db7SRoger Pau Monné struct mbuf_xennet *ref; 322dabb3db7SRoger Pau Monné 323dabb3db7SRoger Pau Monné ref = (struct mbuf_xennet *)m_tag_locate(m, MTAG_COOKIE, 324dabb3db7SRoger Pau Monné MTAG_XENNET, NULL); 325dabb3db7SRoger Pau Monné KASSERT(ref != NULL, ("Cannot find refcount")); 326dabb3db7SRoger Pau Monné ref->count++; 327dabb3db7SRoger Pau Monné } 328dabb3db7SRoger Pau Monné 329dabb3db7SRoger Pau Monné static void mbuf_release(struct mbuf *m) 330dabb3db7SRoger Pau Monné { 331dabb3db7SRoger Pau Monné struct mbuf_xennet *ref; 332dabb3db7SRoger Pau Monné 333dabb3db7SRoger Pau Monné ref = (struct mbuf_xennet *)m_tag_locate(m, MTAG_COOKIE, 334dabb3db7SRoger Pau Monné MTAG_XENNET, NULL); 335dabb3db7SRoger Pau Monné KASSERT(ref != NULL, ("Cannot find refcount")); 336dabb3db7SRoger Pau Monné KASSERT(ref->count > 0, ("Invalid reference count")); 337dabb3db7SRoger Pau Monné 338dabb3db7SRoger Pau Monné if (--ref->count == 0) 339dabb3db7SRoger Pau Monné m_freem(m); 340dabb3db7SRoger Pau Monné } 341dabb3db7SRoger Pau Monné 342dabb3db7SRoger Pau Monné static void tag_free(struct m_tag *t) 343dabb3db7SRoger Pau Monné { 344dabb3db7SRoger Pau Monné struct mbuf_xennet *ref = (struct mbuf_xennet *)t; 345dabb3db7SRoger Pau Monné 346dabb3db7SRoger Pau Monné KASSERT(ref->count == 0, ("Free mbuf tag with pending refcnt")); 347dabb3db7SRoger Pau Monné bus_dmamap_sync(ref->dma_tag, ref->dma_map, BUS_DMASYNC_POSTWRITE); 348dabb3db7SRoger Pau Monné bus_dmamap_destroy(ref->dma_tag, ref->dma_map); 349dabb3db7SRoger Pau Monné SLIST_INSERT_HEAD(&ref->txq->tags, ref, next); 350dabb3db7SRoger Pau Monné } 351dabb3db7SRoger Pau Monné 35289e0f4d2SKip Macy #define IPRINTK(fmt, args...) \ 35389e0f4d2SKip Macy printf("[XEN] " fmt, ##args) 354227ca257SKip Macy #ifdef INVARIANTS 35589e0f4d2SKip Macy #define WPRINTK(fmt, args...) \ 35689e0f4d2SKip Macy printf("[XEN] " fmt, ##args) 357227ca257SKip Macy #else 358227ca257SKip Macy #define WPRINTK(fmt, args...) 359227ca257SKip Macy #endif 360227ca257SKip Macy #ifdef DEBUG 36189e0f4d2SKip Macy #define DPRINTK(fmt, args...) \ 36223dc5621SKip Macy printf("[XEN] %s: " fmt, __func__, ##args) 36312678024SDoug Rabson #else 36412678024SDoug Rabson #define DPRINTK(fmt, args...) 36512678024SDoug Rabson #endif 36689e0f4d2SKip Macy 36789e0f4d2SKip Macy /** 36889e0f4d2SKip Macy * Read the 'mac' node at the given device's node in the store, and parse that 36989e0f4d2SKip Macy * as colon-separated octets, placing result the given mac array. mac must be 37089e0f4d2SKip Macy * a preallocated array of length ETH_ALEN (as declared in linux/if_ether.h). 37189e0f4d2SKip Macy * Return 0 on success, or errno on error. 37289e0f4d2SKip Macy */ 37389e0f4d2SKip Macy static int 37423dc5621SKip Macy xen_net_read_mac(device_t dev, uint8_t mac[]) 37589e0f4d2SKip Macy { 3763a6d1fcfSKip Macy int error, i; 3773a6d1fcfSKip Macy char *s, *e, *macstr; 378ffa06904SJustin T. Gibbs const char *path; 3793a6d1fcfSKip Macy 380ffa06904SJustin T. Gibbs path = xenbus_get_node(dev); 381ffa06904SJustin T. Gibbs error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr); 382ffa06904SJustin T. Gibbs if (error == ENOENT) { 383ffa06904SJustin T. Gibbs /* 384ffa06904SJustin T. Gibbs * Deal with missing mac XenStore nodes on devices with 385ffa06904SJustin T. Gibbs * HVM emulation (the 'ioemu' configuration attribute) 386ffa06904SJustin T. Gibbs * enabled. 387ffa06904SJustin T. Gibbs * 388ffa06904SJustin T. Gibbs * The HVM emulator may execute in a stub device model 389ffa06904SJustin T. Gibbs * domain which lacks the permission, only given to Dom0, 390ffa06904SJustin T. Gibbs * to update the guest's XenStore tree. For this reason, 391ffa06904SJustin T. Gibbs * the HVM emulator doesn't even attempt to write the 392ffa06904SJustin T. Gibbs * front-side mac node, even when operating in Dom0. 393ffa06904SJustin T. Gibbs * However, there should always be a mac listed in the 394ffa06904SJustin T. Gibbs * backend tree. Fallback to this version if our query 395ffa06904SJustin T. Gibbs * of the front side XenStore location doesn't find 396ffa06904SJustin T. Gibbs * anything. 397ffa06904SJustin T. Gibbs */ 398ffa06904SJustin T. Gibbs path = xenbus_get_otherend_path(dev); 399ffa06904SJustin T. Gibbs error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr); 400ffa06904SJustin T. Gibbs } 401ffa06904SJustin T. Gibbs if (error != 0) { 402ffa06904SJustin T. Gibbs xenbus_dev_fatal(dev, error, "parsing %s/mac", path); 4033a6d1fcfSKip Macy return (error); 404ffa06904SJustin T. Gibbs } 4053a6d1fcfSKip Macy 40689e0f4d2SKip Macy s = macstr; 40789e0f4d2SKip Macy for (i = 0; i < ETHER_ADDR_LEN; i++) { 40889e0f4d2SKip Macy mac[i] = strtoul(s, &e, 16); 40989e0f4d2SKip Macy if (s == e || (e[0] != ':' && e[0] != 0)) { 410ff662b5cSJustin T. Gibbs free(macstr, M_XENBUS); 4113a6d1fcfSKip Macy return (ENOENT); 41289e0f4d2SKip Macy } 41389e0f4d2SKip Macy s = &e[1]; 41489e0f4d2SKip Macy } 415ff662b5cSJustin T. Gibbs free(macstr, M_XENBUS); 4163a6d1fcfSKip Macy return (0); 41789e0f4d2SKip Macy } 41889e0f4d2SKip Macy 41989e0f4d2SKip Macy /** 42089e0f4d2SKip Macy * Entry point to this code when a new device is created. Allocate the basic 42189e0f4d2SKip Macy * structures and the ring buffers for communication with the backend, and 42289e0f4d2SKip Macy * inform the backend of the appropriate details for those. Switch to 42389e0f4d2SKip Macy * Connected state. 42489e0f4d2SKip Macy */ 42589e0f4d2SKip Macy static int 42623dc5621SKip Macy netfront_probe(device_t dev) 42723dc5621SKip Macy { 42823dc5621SKip Macy 4295f700083SJulien Grall if (xen_pv_nics_disabled()) 430f8f1bb83SRoger Pau Monné return (ENXIO); 431f8f1bb83SRoger Pau Monné 43223dc5621SKip Macy if (!strcmp(xenbus_get_type(dev), "vif")) { 43323dc5621SKip Macy device_set_desc(dev, "Virtual Network Interface"); 43423dc5621SKip Macy return (0); 43523dc5621SKip Macy } 43623dc5621SKip Macy 43723dc5621SKip Macy return (ENXIO); 43823dc5621SKip Macy } 43923dc5621SKip Macy 44023dc5621SKip Macy static int 44123dc5621SKip Macy netfront_attach(device_t dev) 44289e0f4d2SKip Macy { 44389e0f4d2SKip Macy int err; 44489e0f4d2SKip Macy 44523dc5621SKip Macy err = create_netdev(dev); 44696375eacSRoger Pau Monné if (err != 0) { 44789e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "creating netdev"); 448ffa06904SJustin T. Gibbs return (err); 44989e0f4d2SKip Macy } 45089e0f4d2SKip Macy 45112678024SDoug Rabson SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 45212678024SDoug Rabson SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 453f0188618SHans Petter Selasky OID_AUTO, "enable_lro", CTLFLAG_RW, 45412678024SDoug Rabson &xn_enable_lro, 0, "Large Receive Offload"); 45512678024SDoug Rabson 45696375eacSRoger Pau Monné SYSCTL_ADD_ULONG(device_get_sysctl_ctx(dev), 45796375eacSRoger Pau Monné SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 45896375eacSRoger Pau Monné OID_AUTO, "num_queues", CTLFLAG_RD, 45996375eacSRoger Pau Monné &xn_num_queues, "Number of pairs of queues"); 46096375eacSRoger Pau Monné 461ffa06904SJustin T. Gibbs return (0); 46289e0f4d2SKip Macy } 46389e0f4d2SKip Macy 464cf9c09e1SJustin T. Gibbs static int 465cf9c09e1SJustin T. Gibbs netfront_suspend(device_t dev) 466cf9c09e1SJustin T. Gibbs { 46796375eacSRoger Pau Monné struct netfront_info *np = device_get_softc(dev); 46896375eacSRoger Pau Monné u_int i; 469cf9c09e1SJustin T. Gibbs 47096375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 47196375eacSRoger Pau Monné XN_RX_LOCK(&np->rxq[i]); 47296375eacSRoger Pau Monné XN_TX_LOCK(&np->txq[i]); 47396375eacSRoger Pau Monné } 47496375eacSRoger Pau Monné netfront_carrier_off(np); 47596375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 47696375eacSRoger Pau Monné XN_RX_UNLOCK(&np->rxq[i]); 47796375eacSRoger Pau Monné XN_TX_UNLOCK(&np->txq[i]); 47896375eacSRoger Pau Monné } 479cf9c09e1SJustin T. Gibbs return (0); 480cf9c09e1SJustin T. Gibbs } 48189e0f4d2SKip Macy 48289e0f4d2SKip Macy /** 48389e0f4d2SKip Macy * We are reconnecting to the backend, due to a suspend/resume, or a backend 48489e0f4d2SKip Macy * driver restart. We tear down our netif structure and recreate it, but 48589e0f4d2SKip Macy * leave the device-layer structures intact so that this is transparent to the 48689e0f4d2SKip Macy * rest of the kernel. 48789e0f4d2SKip Macy */ 48889e0f4d2SKip Macy static int 48923dc5621SKip Macy netfront_resume(device_t dev) 49089e0f4d2SKip Macy { 49123dc5621SKip Macy struct netfront_info *info = device_get_softc(dev); 4928dee0e9bSRoger Pau Monné u_int i; 4938dee0e9bSRoger Pau Monné 4948dee0e9bSRoger Pau Monné if (xen_suspend_cancelled) { 4958dee0e9bSRoger Pau Monné for (i = 0; i < info->num_queues; i++) { 4968dee0e9bSRoger Pau Monné XN_RX_LOCK(&info->rxq[i]); 4978dee0e9bSRoger Pau Monné XN_TX_LOCK(&info->txq[i]); 4988dee0e9bSRoger Pau Monné } 4998dee0e9bSRoger Pau Monné netfront_carrier_on(info); 5008dee0e9bSRoger Pau Monné for (i = 0; i < info->num_queues; i++) { 5018dee0e9bSRoger Pau Monné XN_RX_UNLOCK(&info->rxq[i]); 5028dee0e9bSRoger Pau Monné XN_TX_UNLOCK(&info->txq[i]); 5038dee0e9bSRoger Pau Monné } 5048dee0e9bSRoger Pau Monné return (0); 5058dee0e9bSRoger Pau Monné } 50689e0f4d2SKip Macy 50789e0f4d2SKip Macy netif_disconnect_backend(info); 50889e0f4d2SKip Macy return (0); 50989e0f4d2SKip Macy } 51089e0f4d2SKip Macy 51196375eacSRoger Pau Monné static int 51296375eacSRoger Pau Monné write_queue_xenstore_keys(device_t dev, 51396375eacSRoger Pau Monné struct netfront_rxq *rxq, 51496375eacSRoger Pau Monné struct netfront_txq *txq, 51596375eacSRoger Pau Monné struct xs_transaction *xst, bool hierarchy) 51696375eacSRoger Pau Monné { 51796375eacSRoger Pau Monné int err; 51896375eacSRoger Pau Monné const char *message; 51996375eacSRoger Pau Monné const char *node = xenbus_get_node(dev); 52096375eacSRoger Pau Monné char *path; 52196375eacSRoger Pau Monné size_t path_size; 52296375eacSRoger Pau Monné 52396375eacSRoger Pau Monné KASSERT(rxq->id == txq->id, ("Mismatch between RX and TX queue ids")); 52496375eacSRoger Pau Monné /* Split event channel support is not yet there. */ 52596375eacSRoger Pau Monné KASSERT(rxq->xen_intr_handle == txq->xen_intr_handle, 52696375eacSRoger Pau Monné ("Split event channels are not supported")); 52796375eacSRoger Pau Monné 52896375eacSRoger Pau Monné if (hierarchy) { 52996375eacSRoger Pau Monné path_size = strlen(node) + 10; 53096375eacSRoger Pau Monné path = malloc(path_size, M_DEVBUF, M_WAITOK|M_ZERO); 53196375eacSRoger Pau Monné snprintf(path, path_size, "%s/queue-%u", node, rxq->id); 53296375eacSRoger Pau Monné } else { 53396375eacSRoger Pau Monné path_size = strlen(node) + 1; 53496375eacSRoger Pau Monné path = malloc(path_size, M_DEVBUF, M_WAITOK|M_ZERO); 53596375eacSRoger Pau Monné snprintf(path, path_size, "%s", node); 53696375eacSRoger Pau Monné } 53796375eacSRoger Pau Monné 53896375eacSRoger Pau Monné err = xs_printf(*xst, path, "tx-ring-ref","%u", txq->ring_ref); 53996375eacSRoger Pau Monné if (err != 0) { 54096375eacSRoger Pau Monné message = "writing tx ring-ref"; 54196375eacSRoger Pau Monné goto error; 54296375eacSRoger Pau Monné } 54396375eacSRoger Pau Monné err = xs_printf(*xst, path, "rx-ring-ref","%u", rxq->ring_ref); 54496375eacSRoger Pau Monné if (err != 0) { 54596375eacSRoger Pau Monné message = "writing rx ring-ref"; 54696375eacSRoger Pau Monné goto error; 54796375eacSRoger Pau Monné } 54896375eacSRoger Pau Monné err = xs_printf(*xst, path, "event-channel", "%u", 54996375eacSRoger Pau Monné xen_intr_port(rxq->xen_intr_handle)); 55096375eacSRoger Pau Monné if (err != 0) { 55196375eacSRoger Pau Monné message = "writing event-channel"; 55296375eacSRoger Pau Monné goto error; 55396375eacSRoger Pau Monné } 55496375eacSRoger Pau Monné 55596375eacSRoger Pau Monné free(path, M_DEVBUF); 55696375eacSRoger Pau Monné 55796375eacSRoger Pau Monné return (0); 55896375eacSRoger Pau Monné 55996375eacSRoger Pau Monné error: 56096375eacSRoger Pau Monné free(path, M_DEVBUF); 56196375eacSRoger Pau Monné xenbus_dev_fatal(dev, err, "%s", message); 56296375eacSRoger Pau Monné 56396375eacSRoger Pau Monné return (err); 56496375eacSRoger Pau Monné } 56596375eacSRoger Pau Monné 56689e0f4d2SKip Macy /* Common code used when first setting up, and when resuming. */ 56789e0f4d2SKip Macy static int 56823dc5621SKip Macy talk_to_backend(device_t dev, struct netfront_info *info) 56989e0f4d2SKip Macy { 57089e0f4d2SKip Macy const char *message; 571ff662b5cSJustin T. Gibbs struct xs_transaction xst; 57223dc5621SKip Macy const char *node = xenbus_get_node(dev); 57389e0f4d2SKip Macy int err; 57496375eacSRoger Pau Monné unsigned long num_queues, max_queues = 0; 57596375eacSRoger Pau Monné unsigned int i; 57689e0f4d2SKip Macy 57789e0f4d2SKip Macy err = xen_net_read_mac(dev, info->mac); 57896375eacSRoger Pau Monné if (err != 0) { 57923dc5621SKip Macy xenbus_dev_fatal(dev, err, "parsing %s/mac", node); 58089e0f4d2SKip Macy goto out; 58189e0f4d2SKip Macy } 58289e0f4d2SKip Macy 58396375eacSRoger Pau Monné err = xs_scanf(XST_NIL, xenbus_get_otherend_path(info->xbdev), 58496375eacSRoger Pau Monné "multi-queue-max-queues", NULL, "%lu", &max_queues); 58596375eacSRoger Pau Monné if (err != 0) 58696375eacSRoger Pau Monné max_queues = 1; 58796375eacSRoger Pau Monné num_queues = xn_num_queues; 58896375eacSRoger Pau Monné if (num_queues > max_queues) 58996375eacSRoger Pau Monné num_queues = max_queues; 59096375eacSRoger Pau Monné 59196375eacSRoger Pau Monné err = setup_device(dev, info, num_queues); 592*318bbb6dSRoger Pau Monné if (err != 0) { 593*318bbb6dSRoger Pau Monné xenbus_dev_fatal(dev, err, "setup device"); 59489e0f4d2SKip Macy goto out; 595*318bbb6dSRoger Pau Monné } 59689e0f4d2SKip Macy 59789e0f4d2SKip Macy again: 598ff662b5cSJustin T. Gibbs err = xs_transaction_start(&xst); 59996375eacSRoger Pau Monné if (err != 0) { 60089e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "starting transaction"); 60196375eacSRoger Pau Monné goto free; 60289e0f4d2SKip Macy } 60396375eacSRoger Pau Monné 60496375eacSRoger Pau Monné if (info->num_queues == 1) { 60596375eacSRoger Pau Monné err = write_queue_xenstore_keys(dev, &info->rxq[0], 60696375eacSRoger Pau Monné &info->txq[0], &xst, false); 60796375eacSRoger Pau Monné if (err != 0) 60896375eacSRoger Pau Monné goto abort_transaction_no_def_error; 60996375eacSRoger Pau Monné } else { 61096375eacSRoger Pau Monné err = xs_printf(xst, node, "multi-queue-num-queues", 61196375eacSRoger Pau Monné "%u", info->num_queues); 61296375eacSRoger Pau Monné if (err != 0) { 61396375eacSRoger Pau Monné message = "writing multi-queue-num-queues"; 61489e0f4d2SKip Macy goto abort_transaction; 61589e0f4d2SKip Macy } 61696375eacSRoger Pau Monné 61796375eacSRoger Pau Monné for (i = 0; i < info->num_queues; i++) { 61896375eacSRoger Pau Monné err = write_queue_xenstore_keys(dev, &info->rxq[i], 61996375eacSRoger Pau Monné &info->txq[i], &xst, true); 62096375eacSRoger Pau Monné if (err != 0) 62196375eacSRoger Pau Monné goto abort_transaction_no_def_error; 62289e0f4d2SKip Macy } 62389e0f4d2SKip Macy } 62496375eacSRoger Pau Monné 625d0f3a8b9SRoger Pau Monné err = xs_printf(xst, node, "request-rx-copy", "%u", 1); 62696375eacSRoger Pau Monné if (err != 0) { 62789e0f4d2SKip Macy message = "writing request-rx-copy"; 62889e0f4d2SKip Macy goto abort_transaction; 62989e0f4d2SKip Macy } 630ff662b5cSJustin T. Gibbs err = xs_printf(xst, node, "feature-rx-notify", "%d", 1); 63196375eacSRoger Pau Monné if (err != 0) { 63289e0f4d2SKip Macy message = "writing feature-rx-notify"; 63389e0f4d2SKip Macy goto abort_transaction; 63489e0f4d2SKip Macy } 635ff662b5cSJustin T. Gibbs err = xs_printf(xst, node, "feature-sg", "%d", 1); 63696375eacSRoger Pau Monné if (err != 0) { 63789e0f4d2SKip Macy message = "writing feature-sg"; 63889e0f4d2SKip Macy goto abort_transaction; 63989e0f4d2SKip Macy } 64002f3b17fSJustin Hibbits if ((if_getcapenable(info->xn_ifp) & IFCAP_LRO) != 0) { 641ff662b5cSJustin T. Gibbs err = xs_printf(xst, node, "feature-gso-tcpv4", "%d", 1); 64296375eacSRoger Pau Monné if (err != 0) { 64389e0f4d2SKip Macy message = "writing feature-gso-tcpv4"; 64489e0f4d2SKip Macy goto abort_transaction; 64589e0f4d2SKip Macy } 646c2d12e5eSRoger Pau Monné } 64702f3b17fSJustin Hibbits if ((if_getcapenable(info->xn_ifp) & IFCAP_RXCSUM) == 0) { 648c2d12e5eSRoger Pau Monné err = xs_printf(xst, node, "feature-no-csum-offload", "%d", 1); 649c2d12e5eSRoger Pau Monné if (err != 0) { 650c2d12e5eSRoger Pau Monné message = "writing feature-no-csum-offload"; 651c2d12e5eSRoger Pau Monné goto abort_transaction; 652c2d12e5eSRoger Pau Monné } 653c2d12e5eSRoger Pau Monné } 65489e0f4d2SKip Macy 655ff662b5cSJustin T. Gibbs err = xs_transaction_end(xst, 0); 65696375eacSRoger Pau Monné if (err != 0) { 65789e0f4d2SKip Macy if (err == EAGAIN) 65889e0f4d2SKip Macy goto again; 65989e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "completing transaction"); 66096375eacSRoger Pau Monné goto free; 66189e0f4d2SKip Macy } 66289e0f4d2SKip Macy 66389e0f4d2SKip Macy return 0; 66489e0f4d2SKip Macy 66589e0f4d2SKip Macy abort_transaction: 66689e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "%s", message); 66796375eacSRoger Pau Monné abort_transaction_no_def_error: 66896375eacSRoger Pau Monné xs_transaction_end(xst, 1); 66996375eacSRoger Pau Monné free: 67089e0f4d2SKip Macy netif_free(info); 67189e0f4d2SKip Macy out: 67296375eacSRoger Pau Monné return (err); 67396375eacSRoger Pau Monné } 67496375eacSRoger Pau Monné 67596375eacSRoger Pau Monné static void 676da695b05SRoger Pau Monné xn_rxq_intr(struct netfront_rxq *rxq) 67796375eacSRoger Pau Monné { 67896375eacSRoger Pau Monné 67996375eacSRoger Pau Monné XN_RX_LOCK(rxq); 68096375eacSRoger Pau Monné xn_rxeof(rxq); 68196375eacSRoger Pau Monné XN_RX_UNLOCK(rxq); 68296375eacSRoger Pau Monné } 68396375eacSRoger Pau Monné 68496375eacSRoger Pau Monné static void 68596375eacSRoger Pau Monné xn_txq_start(struct netfront_txq *txq) 68696375eacSRoger Pau Monné { 68796375eacSRoger Pau Monné struct netfront_info *np = txq->info; 68802f3b17fSJustin Hibbits if_t ifp = np->xn_ifp; 68996375eacSRoger Pau Monné 69096375eacSRoger Pau Monné XN_TX_LOCK_ASSERT(txq); 69196375eacSRoger Pau Monné if (!drbr_empty(ifp, txq->br)) 69296375eacSRoger Pau Monné xn_txq_mq_start_locked(txq, NULL); 69396375eacSRoger Pau Monné } 69496375eacSRoger Pau Monné 69596375eacSRoger Pau Monné static void 696da695b05SRoger Pau Monné xn_txq_intr(struct netfront_txq *txq) 69796375eacSRoger Pau Monné { 69896375eacSRoger Pau Monné 69996375eacSRoger Pau Monné XN_TX_LOCK(txq); 70096375eacSRoger Pau Monné if (RING_HAS_UNCONSUMED_RESPONSES(&txq->ring)) 70196375eacSRoger Pau Monné xn_txeof(txq); 70296375eacSRoger Pau Monné xn_txq_start(txq); 70396375eacSRoger Pau Monné XN_TX_UNLOCK(txq); 70496375eacSRoger Pau Monné } 70596375eacSRoger Pau Monné 70696375eacSRoger Pau Monné static void 70796375eacSRoger Pau Monné xn_txq_tq_deferred(void *xtxq, int pending) 70896375eacSRoger Pau Monné { 70996375eacSRoger Pau Monné struct netfront_txq *txq = xtxq; 71096375eacSRoger Pau Monné 71196375eacSRoger Pau Monné XN_TX_LOCK(txq); 71296375eacSRoger Pau Monné xn_txq_start(txq); 71396375eacSRoger Pau Monné XN_TX_UNLOCK(txq); 71496375eacSRoger Pau Monné } 71596375eacSRoger Pau Monné 71696375eacSRoger Pau Monné static void 71796375eacSRoger Pau Monné disconnect_rxq(struct netfront_rxq *rxq) 71896375eacSRoger Pau Monné { 71996375eacSRoger Pau Monné 72096375eacSRoger Pau Monné xn_release_rx_bufs(rxq); 72196375eacSRoger Pau Monné gnttab_free_grant_references(rxq->gref_head); 722*318bbb6dSRoger Pau Monné if (rxq->ring_ref != GRANT_REF_INVALID) { 723d039b070SRoger Pau Monné gnttab_end_foreign_access(rxq->ring_ref, NULL); 724*318bbb6dSRoger Pau Monné rxq->ring_ref = GRANT_REF_INVALID; 725*318bbb6dSRoger Pau Monné } 72696375eacSRoger Pau Monné /* 72796375eacSRoger Pau Monné * No split event channel support at the moment, handle will 72896375eacSRoger Pau Monné * be unbound in tx. So no need to call xen_intr_unbind here, 72996375eacSRoger Pau Monné * but we do want to reset the handler to 0. 73096375eacSRoger Pau Monné */ 73196375eacSRoger Pau Monné rxq->xen_intr_handle = 0; 73296375eacSRoger Pau Monné } 73396375eacSRoger Pau Monné 73496375eacSRoger Pau Monné static void 73596375eacSRoger Pau Monné destroy_rxq(struct netfront_rxq *rxq) 73696375eacSRoger Pau Monné { 73796375eacSRoger Pau Monné 7382568ee67SRoger Pau Monné callout_drain(&rxq->rx_refill); 73996375eacSRoger Pau Monné free(rxq->ring.sring, M_DEVBUF); 740*318bbb6dSRoger Pau Monné rxq->ring.sring = NULL; 74196375eacSRoger Pau Monné } 74296375eacSRoger Pau Monné 74396375eacSRoger Pau Monné static void 74496375eacSRoger Pau Monné destroy_rxqs(struct netfront_info *np) 74596375eacSRoger Pau Monné { 74696375eacSRoger Pau Monné int i; 74796375eacSRoger Pau Monné 74896375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) 74996375eacSRoger Pau Monné destroy_rxq(&np->rxq[i]); 75096375eacSRoger Pau Monné 75196375eacSRoger Pau Monné free(np->rxq, M_DEVBUF); 75296375eacSRoger Pau Monné np->rxq = NULL; 75389e0f4d2SKip Macy } 75489e0f4d2SKip Macy 75589e0f4d2SKip Macy static int 75696375eacSRoger Pau Monné setup_rxqs(device_t dev, struct netfront_info *info, 75796375eacSRoger Pau Monné unsigned long num_queues) 75889e0f4d2SKip Macy { 75996375eacSRoger Pau Monné int q, i; 7603a6d1fcfSKip Macy int error; 76196375eacSRoger Pau Monné netif_rx_sring_t *rxs; 76296375eacSRoger Pau Monné struct netfront_rxq *rxq; 76389e0f4d2SKip Macy 76496375eacSRoger Pau Monné info->rxq = malloc(sizeof(struct netfront_rxq) * num_queues, 76596375eacSRoger Pau Monné M_DEVBUF, M_WAITOK|M_ZERO); 76689e0f4d2SKip Macy 76796375eacSRoger Pau Monné for (q = 0; q < num_queues; q++) { 76896375eacSRoger Pau Monné rxq = &info->rxq[q]; 76996375eacSRoger Pau Monné 77096375eacSRoger Pau Monné rxq->id = q; 77196375eacSRoger Pau Monné rxq->info = info; 772*318bbb6dSRoger Pau Monné 773*318bbb6dSRoger Pau Monné rxq->gref_head = GNTTAB_LIST_END; 77496375eacSRoger Pau Monné rxq->ring_ref = GRANT_REF_INVALID; 77596375eacSRoger Pau Monné rxq->ring.sring = NULL; 77696375eacSRoger Pau Monné snprintf(rxq->name, XN_QUEUE_NAME_LEN, "xnrx_%u", q); 77796375eacSRoger Pau Monné mtx_init(&rxq->lock, rxq->name, "netfront receive lock", 77896375eacSRoger Pau Monné MTX_DEF); 77996375eacSRoger Pau Monné 78096375eacSRoger Pau Monné for (i = 0; i <= NET_RX_RING_SIZE; i++) { 78196375eacSRoger Pau Monné rxq->mbufs[i] = NULL; 78296375eacSRoger Pau Monné rxq->grant_ref[i] = GRANT_REF_INVALID; 78396375eacSRoger Pau Monné } 78496375eacSRoger Pau Monné 78596375eacSRoger Pau Monné /* Start resources allocation */ 78696375eacSRoger Pau Monné 7872568ee67SRoger Pau Monné if (gnttab_alloc_grant_references(NET_RX_RING_SIZE, 78896375eacSRoger Pau Monné &rxq->gref_head) != 0) { 78996375eacSRoger Pau Monné device_printf(dev, "allocating rx gref"); 7903a6d1fcfSKip Macy error = ENOMEM; 79189e0f4d2SKip Macy goto fail; 79289e0f4d2SKip Macy } 79389e0f4d2SKip Macy 79496375eacSRoger Pau Monné rxs = (netif_rx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, 79596375eacSRoger Pau Monné M_WAITOK|M_ZERO); 79689e0f4d2SKip Macy SHARED_RING_INIT(rxs); 79796375eacSRoger Pau Monné FRONT_RING_INIT(&rxq->ring, rxs, PAGE_SIZE); 79889e0f4d2SKip Macy 79996375eacSRoger Pau Monné error = xenbus_grant_ring(dev, virt_to_mfn(rxs), 80096375eacSRoger Pau Monné &rxq->ring_ref); 80196375eacSRoger Pau Monné if (error != 0) { 80296375eacSRoger Pau Monné device_printf(dev, "granting rx ring page"); 80396375eacSRoger Pau Monné goto fail_grant_ring; 80496375eacSRoger Pau Monné } 80589e0f4d2SKip Macy 8062568ee67SRoger Pau Monné callout_init(&rxq->rx_refill, 1); 80789e0f4d2SKip Macy } 80889e0f4d2SKip Macy 8093a6d1fcfSKip Macy return (0); 81089e0f4d2SKip Macy 81196375eacSRoger Pau Monné fail_grant_ring: 81296375eacSRoger Pau Monné gnttab_free_grant_references(rxq->gref_head); 81396375eacSRoger Pau Monné free(rxq->ring.sring, M_DEVBUF); 81489e0f4d2SKip Macy fail: 81596375eacSRoger Pau Monné for (; q >= 0; q--) { 81696375eacSRoger Pau Monné disconnect_rxq(&info->rxq[q]); 81796375eacSRoger Pau Monné destroy_rxq(&info->rxq[q]); 81896375eacSRoger Pau Monné } 81996375eacSRoger Pau Monné 82096375eacSRoger Pau Monné free(info->rxq, M_DEVBUF); 82196375eacSRoger Pau Monné return (error); 82296375eacSRoger Pau Monné } 82396375eacSRoger Pau Monné 82496375eacSRoger Pau Monné static void 82596375eacSRoger Pau Monné disconnect_txq(struct netfront_txq *txq) 82696375eacSRoger Pau Monné { 82796375eacSRoger Pau Monné 82896375eacSRoger Pau Monné xn_release_tx_bufs(txq); 82996375eacSRoger Pau Monné gnttab_free_grant_references(txq->gref_head); 830*318bbb6dSRoger Pau Monné if (txq->ring_ref != GRANT_REF_INVALID) { 831d039b070SRoger Pau Monné gnttab_end_foreign_access(txq->ring_ref, NULL); 832*318bbb6dSRoger Pau Monné txq->ring_ref = GRANT_REF_INVALID; 833*318bbb6dSRoger Pau Monné } 83496375eacSRoger Pau Monné xen_intr_unbind(&txq->xen_intr_handle); 83596375eacSRoger Pau Monné } 83696375eacSRoger Pau Monné 83796375eacSRoger Pau Monné static void 83896375eacSRoger Pau Monné destroy_txq(struct netfront_txq *txq) 83996375eacSRoger Pau Monné { 840dabb3db7SRoger Pau Monné unsigned int i; 84196375eacSRoger Pau Monné 84296375eacSRoger Pau Monné free(txq->ring.sring, M_DEVBUF); 843*318bbb6dSRoger Pau Monné txq->ring.sring = NULL; 84496375eacSRoger Pau Monné buf_ring_free(txq->br, M_DEVBUF); 845*318bbb6dSRoger Pau Monné txq->br = NULL; 846*318bbb6dSRoger Pau Monné if (txq->tq) { 84796375eacSRoger Pau Monné taskqueue_drain_all(txq->tq); 84896375eacSRoger Pau Monné taskqueue_free(txq->tq); 849*318bbb6dSRoger Pau Monné txq->tq = NULL; 850*318bbb6dSRoger Pau Monné } 851dabb3db7SRoger Pau Monné 852dabb3db7SRoger Pau Monné for (i = 0; i <= NET_TX_RING_SIZE; i++) { 853dabb3db7SRoger Pau Monné bus_dmamap_destroy(txq->info->dma_tag, 854dabb3db7SRoger Pau Monné txq->xennet_tag[i].dma_map); 855dabb3db7SRoger Pau Monné txq->xennet_tag[i].dma_map = NULL; 856dabb3db7SRoger Pau Monné } 85796375eacSRoger Pau Monné } 85896375eacSRoger Pau Monné 85996375eacSRoger Pau Monné static void 86096375eacSRoger Pau Monné destroy_txqs(struct netfront_info *np) 86196375eacSRoger Pau Monné { 86296375eacSRoger Pau Monné int i; 86396375eacSRoger Pau Monné 86496375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) 86596375eacSRoger Pau Monné destroy_txq(&np->txq[i]); 86696375eacSRoger Pau Monné 86796375eacSRoger Pau Monné free(np->txq, M_DEVBUF); 86896375eacSRoger Pau Monné np->txq = NULL; 86996375eacSRoger Pau Monné } 87096375eacSRoger Pau Monné 87196375eacSRoger Pau Monné static int 87296375eacSRoger Pau Monné setup_txqs(device_t dev, struct netfront_info *info, 87396375eacSRoger Pau Monné unsigned long num_queues) 87496375eacSRoger Pau Monné { 87596375eacSRoger Pau Monné int q, i; 87696375eacSRoger Pau Monné int error; 87796375eacSRoger Pau Monné netif_tx_sring_t *txs; 87896375eacSRoger Pau Monné struct netfront_txq *txq; 87996375eacSRoger Pau Monné 88096375eacSRoger Pau Monné info->txq = malloc(sizeof(struct netfront_txq) * num_queues, 88196375eacSRoger Pau Monné M_DEVBUF, M_WAITOK|M_ZERO); 88296375eacSRoger Pau Monné 88396375eacSRoger Pau Monné for (q = 0; q < num_queues; q++) { 88496375eacSRoger Pau Monné txq = &info->txq[q]; 88596375eacSRoger Pau Monné 88696375eacSRoger Pau Monné txq->id = q; 88796375eacSRoger Pau Monné txq->info = info; 88896375eacSRoger Pau Monné 889*318bbb6dSRoger Pau Monné txq->gref_head = GNTTAB_LIST_END; 89096375eacSRoger Pau Monné txq->ring_ref = GRANT_REF_INVALID; 89196375eacSRoger Pau Monné txq->ring.sring = NULL; 89296375eacSRoger Pau Monné 89396375eacSRoger Pau Monné snprintf(txq->name, XN_QUEUE_NAME_LEN, "xntx_%u", q); 89496375eacSRoger Pau Monné 89596375eacSRoger Pau Monné mtx_init(&txq->lock, txq->name, "netfront transmit lock", 89696375eacSRoger Pau Monné MTX_DEF); 897dabb3db7SRoger Pau Monné SLIST_INIT(&txq->tags); 89896375eacSRoger Pau Monné 89996375eacSRoger Pau Monné for (i = 0; i <= NET_TX_RING_SIZE; i++) { 90096375eacSRoger Pau Monné txq->mbufs[i] = (void *) ((u_long) i+1); 90196375eacSRoger Pau Monné txq->grant_ref[i] = GRANT_REF_INVALID; 902dabb3db7SRoger Pau Monné txq->xennet_tag[i].txq = txq; 903dabb3db7SRoger Pau Monné txq->xennet_tag[i].dma_tag = info->dma_tag; 904dabb3db7SRoger Pau Monné error = bus_dmamap_create(info->dma_tag, 0, 905dabb3db7SRoger Pau Monné &txq->xennet_tag[i].dma_map); 906dabb3db7SRoger Pau Monné if (error != 0) { 907dabb3db7SRoger Pau Monné device_printf(dev, 908dabb3db7SRoger Pau Monné "failed to allocate dma map\n"); 909dabb3db7SRoger Pau Monné goto fail; 910dabb3db7SRoger Pau Monné } 911dabb3db7SRoger Pau Monné m_tag_setup(&txq->xennet_tag[i].tag, 912dabb3db7SRoger Pau Monné MTAG_COOKIE, MTAG_XENNET, 913dabb3db7SRoger Pau Monné sizeof(txq->xennet_tag[i]) - 914dabb3db7SRoger Pau Monné sizeof(txq->xennet_tag[i].tag)); 915dabb3db7SRoger Pau Monné txq->xennet_tag[i].tag.m_tag_free = &tag_free; 916dabb3db7SRoger Pau Monné SLIST_INSERT_HEAD(&txq->tags, &txq->xennet_tag[i], 917dabb3db7SRoger Pau Monné next); 91896375eacSRoger Pau Monné } 91996375eacSRoger Pau Monné txq->mbufs[NET_TX_RING_SIZE] = (void *)0; 92096375eacSRoger Pau Monné 92196375eacSRoger Pau Monné /* Start resources allocation. */ 92296375eacSRoger Pau Monné 92396375eacSRoger Pau Monné if (gnttab_alloc_grant_references(NET_TX_RING_SIZE, 92496375eacSRoger Pau Monné &txq->gref_head) != 0) { 92596375eacSRoger Pau Monné device_printf(dev, "failed to allocate tx grant refs\n"); 92696375eacSRoger Pau Monné error = ENOMEM; 92796375eacSRoger Pau Monné goto fail; 92896375eacSRoger Pau Monné } 92996375eacSRoger Pau Monné 93096375eacSRoger Pau Monné txs = (netif_tx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, 93196375eacSRoger Pau Monné M_WAITOK|M_ZERO); 93296375eacSRoger Pau Monné SHARED_RING_INIT(txs); 93396375eacSRoger Pau Monné FRONT_RING_INIT(&txq->ring, txs, PAGE_SIZE); 93496375eacSRoger Pau Monné 93596375eacSRoger Pau Monné error = xenbus_grant_ring(dev, virt_to_mfn(txs), 93696375eacSRoger Pau Monné &txq->ring_ref); 93796375eacSRoger Pau Monné if (error != 0) { 93896375eacSRoger Pau Monné device_printf(dev, "failed to grant tx ring\n"); 93996375eacSRoger Pau Monné goto fail_grant_ring; 94096375eacSRoger Pau Monné } 94196375eacSRoger Pau Monné 94296375eacSRoger Pau Monné txq->br = buf_ring_alloc(NET_TX_RING_SIZE, M_DEVBUF, 94396375eacSRoger Pau Monné M_WAITOK, &txq->lock); 94496375eacSRoger Pau Monné TASK_INIT(&txq->defrtask, 0, xn_txq_tq_deferred, txq); 94596375eacSRoger Pau Monné 946da695b05SRoger Pau Monné txq->tq = taskqueue_create(txq->name, M_WAITOK, 94796375eacSRoger Pau Monné taskqueue_thread_enqueue, &txq->tq); 94896375eacSRoger Pau Monné 94996375eacSRoger Pau Monné error = taskqueue_start_threads(&txq->tq, 1, PI_NET, 95096375eacSRoger Pau Monné "%s txq %d", device_get_nameunit(dev), txq->id); 95196375eacSRoger Pau Monné if (error != 0) { 95296375eacSRoger Pau Monné device_printf(dev, "failed to start tx taskq %d\n", 95396375eacSRoger Pau Monné txq->id); 95496375eacSRoger Pau Monné goto fail_start_thread; 95596375eacSRoger Pau Monné } 95696375eacSRoger Pau Monné 95796375eacSRoger Pau Monné error = xen_intr_alloc_and_bind_local_port(dev, 958da695b05SRoger Pau Monné xenbus_get_otherend_id(dev), /* filter */ NULL, xn_intr, 959da695b05SRoger Pau Monné &info->txq[q], INTR_TYPE_NET | INTR_MPSAFE | INTR_ENTROPY, 96096375eacSRoger Pau Monné &txq->xen_intr_handle); 96196375eacSRoger Pau Monné 96296375eacSRoger Pau Monné if (error != 0) { 96396375eacSRoger Pau Monné device_printf(dev, "xen_intr_alloc_and_bind_local_port failed\n"); 96496375eacSRoger Pau Monné goto fail_bind_port; 96596375eacSRoger Pau Monné } 96696375eacSRoger Pau Monné } 96796375eacSRoger Pau Monné 96896375eacSRoger Pau Monné return (0); 96996375eacSRoger Pau Monné 97096375eacSRoger Pau Monné fail_bind_port: 97196375eacSRoger Pau Monné taskqueue_drain_all(txq->tq); 97296375eacSRoger Pau Monné fail_start_thread: 97396375eacSRoger Pau Monné buf_ring_free(txq->br, M_DEVBUF); 97496375eacSRoger Pau Monné taskqueue_free(txq->tq); 975d039b070SRoger Pau Monné gnttab_end_foreign_access(txq->ring_ref, NULL); 97696375eacSRoger Pau Monné fail_grant_ring: 97796375eacSRoger Pau Monné gnttab_free_grant_references(txq->gref_head); 97896375eacSRoger Pau Monné free(txq->ring.sring, M_DEVBUF); 97996375eacSRoger Pau Monné fail: 98096375eacSRoger Pau Monné for (; q >= 0; q--) { 98196375eacSRoger Pau Monné disconnect_txq(&info->txq[q]); 98296375eacSRoger Pau Monné destroy_txq(&info->txq[q]); 98396375eacSRoger Pau Monné } 98496375eacSRoger Pau Monné 98596375eacSRoger Pau Monné free(info->txq, M_DEVBUF); 98696375eacSRoger Pau Monné return (error); 98796375eacSRoger Pau Monné } 98896375eacSRoger Pau Monné 98996375eacSRoger Pau Monné static int 99096375eacSRoger Pau Monné setup_device(device_t dev, struct netfront_info *info, 99196375eacSRoger Pau Monné unsigned long num_queues) 99296375eacSRoger Pau Monné { 99396375eacSRoger Pau Monné int error; 99496375eacSRoger Pau Monné int q; 99596375eacSRoger Pau Monné 99696375eacSRoger Pau Monné if (info->txq) 99796375eacSRoger Pau Monné destroy_txqs(info); 99896375eacSRoger Pau Monné 99996375eacSRoger Pau Monné if (info->rxq) 100096375eacSRoger Pau Monné destroy_rxqs(info); 100196375eacSRoger Pau Monné 100296375eacSRoger Pau Monné info->num_queues = 0; 100396375eacSRoger Pau Monné 100496375eacSRoger Pau Monné error = setup_rxqs(dev, info, num_queues); 100596375eacSRoger Pau Monné if (error != 0) 100696375eacSRoger Pau Monné goto out; 100796375eacSRoger Pau Monné error = setup_txqs(dev, info, num_queues); 100896375eacSRoger Pau Monné if (error != 0) 100996375eacSRoger Pau Monné goto out; 101096375eacSRoger Pau Monné 101196375eacSRoger Pau Monné info->num_queues = num_queues; 101296375eacSRoger Pau Monné 101396375eacSRoger Pau Monné /* No split event channel at the moment. */ 101496375eacSRoger Pau Monné for (q = 0; q < num_queues; q++) 101596375eacSRoger Pau Monné info->rxq[q].xen_intr_handle = info->txq[q].xen_intr_handle; 101696375eacSRoger Pau Monné 101796375eacSRoger Pau Monné return (0); 101896375eacSRoger Pau Monné 101996375eacSRoger Pau Monné out: 102096375eacSRoger Pau Monné KASSERT(error != 0, ("Error path taken without providing an error code")); 10213a6d1fcfSKip Macy return (error); 102289e0f4d2SKip Macy } 102389e0f4d2SKip Macy 1024a0ae8f04SBjoern A. Zeeb #ifdef INET 102502f3b17fSJustin Hibbits static u_int 102602f3b17fSJustin Hibbits netfront_addr_cb(void *arg, struct ifaddr *a, u_int count) 102702f3b17fSJustin Hibbits { 102802f3b17fSJustin Hibbits arp_ifinit((if_t)arg, a); 102902f3b17fSJustin Hibbits return (1); 103002f3b17fSJustin Hibbits } 103189e0f4d2SKip Macy /** 103212678024SDoug Rabson * If this interface has an ipv4 address, send an arp for it. This 103312678024SDoug Rabson * helps to get the network going again after migrating hosts. 103412678024SDoug Rabson */ 103512678024SDoug Rabson static void 103612678024SDoug Rabson netfront_send_fake_arp(device_t dev, struct netfront_info *info) 103712678024SDoug Rabson { 103802f3b17fSJustin Hibbits if_t ifp; 103912678024SDoug Rabson 104012678024SDoug Rabson ifp = info->xn_ifp; 104102f3b17fSJustin Hibbits if_foreach_addr_type(ifp, AF_INET, netfront_addr_cb, ifp); 104212678024SDoug Rabson } 1043a0ae8f04SBjoern A. Zeeb #endif 104412678024SDoug Rabson 104512678024SDoug Rabson /** 104689e0f4d2SKip Macy * Callback received when the backend's state changes. 104789e0f4d2SKip Macy */ 1048283d6f72SJustin T. Gibbs static void 104923dc5621SKip Macy netfront_backend_changed(device_t dev, XenbusState newstate) 105089e0f4d2SKip Macy { 105123dc5621SKip Macy struct netfront_info *sc = device_get_softc(dev); 105289e0f4d2SKip Macy 105323dc5621SKip Macy DPRINTK("newstate=%d\n", newstate); 105489e0f4d2SKip Macy 105502f3b17fSJustin Hibbits CURVNET_SET(if_getvnet(sc->xn_ifp)); 1056903eaa68SKristof Provost 105723dc5621SKip Macy switch (newstate) { 105889e0f4d2SKip Macy case XenbusStateInitialising: 105989e0f4d2SKip Macy case XenbusStateInitialised: 106089e0f4d2SKip Macy case XenbusStateUnknown: 1061920ba15bSKip Macy case XenbusStateReconfigured: 1062920ba15bSKip Macy case XenbusStateReconfiguring: 106389e0f4d2SKip Macy break; 106489e0f4d2SKip Macy case XenbusStateInitWait: 106523dc5621SKip Macy if (xenbus_get_state(dev) != XenbusStateInitialising) 106689e0f4d2SKip Macy break; 106796375eacSRoger Pau Monné if (xn_connect(sc) != 0) 106889e0f4d2SKip Macy break; 106965671253SRoger Pau Monné /* Switch to connected state before kicking the rings. */ 107065671253SRoger Pau Monné xenbus_set_state(sc->xbdev, XenbusStateConnected); 107165671253SRoger Pau Monné xn_kick_rings(sc); 107223dc5621SKip Macy break; 107389e0f4d2SKip Macy case XenbusStateClosing: 107423dc5621SKip Macy xenbus_set_state(dev, XenbusStateClosed); 107589e0f4d2SKip Macy break; 1076c2d12e5eSRoger Pau Monné case XenbusStateClosed: 1077c2d12e5eSRoger Pau Monné if (sc->xn_reset) { 1078c2d12e5eSRoger Pau Monné netif_disconnect_backend(sc); 1079c2d12e5eSRoger Pau Monné xenbus_set_state(dev, XenbusStateInitialising); 1080c2d12e5eSRoger Pau Monné sc->xn_reset = false; 1081c2d12e5eSRoger Pau Monné } 1082c2d12e5eSRoger Pau Monné break; 1083dbf82bdeSRoger Pau Monné case XenbusStateConnected: 1084dbf82bdeSRoger Pau Monné #ifdef INET 1085dbf82bdeSRoger Pau Monné netfront_send_fake_arp(dev, sc); 1086dbf82bdeSRoger Pau Monné #endif 1087dbf82bdeSRoger Pau Monné break; 108889e0f4d2SKip Macy } 1089903eaa68SKristof Provost 1090903eaa68SKristof Provost CURVNET_RESTORE(); 109189e0f4d2SKip Macy } 109289e0f4d2SKip Macy 1093931eeffaSKenneth D. Merry /** 1094931eeffaSKenneth D. Merry * \brief Verify that there is sufficient space in the Tx ring 1095931eeffaSKenneth D. Merry * buffer for a maximally sized request to be enqueued. 1096c099cafaSAdrian Chadd * 1097931eeffaSKenneth D. Merry * A transmit request requires a transmit descriptor for each packet 1098931eeffaSKenneth D. Merry * fragment, plus up to 2 entries for "options" (e.g. TSO). 1099c099cafaSAdrian Chadd */ 110089e0f4d2SKip Macy static inline int 110196375eacSRoger Pau Monné xn_tx_slot_available(struct netfront_txq *txq) 110289e0f4d2SKip Macy { 110396375eacSRoger Pau Monné 110496375eacSRoger Pau Monné return (RING_FREE_REQUESTS(&txq->ring) > (MAX_TX_REQ_FRAGS + 2)); 110589e0f4d2SKip Macy } 1106931eeffaSKenneth D. Merry 110789e0f4d2SKip Macy static void 110896375eacSRoger Pau Monné xn_release_tx_bufs(struct netfront_txq *txq) 110989e0f4d2SKip Macy { 111089e0f4d2SKip Macy int i; 111189e0f4d2SKip Macy 111289e0f4d2SKip Macy for (i = 1; i <= NET_TX_RING_SIZE; i++) { 1113931eeffaSKenneth D. Merry struct mbuf *m; 111489e0f4d2SKip Macy 111596375eacSRoger Pau Monné m = txq->mbufs[i]; 1116931eeffaSKenneth D. Merry 1117931eeffaSKenneth D. Merry /* 1118931eeffaSKenneth D. Merry * We assume that no kernel addresses are 1119931eeffaSKenneth D. Merry * less than NET_TX_RING_SIZE. Any entry 1120931eeffaSKenneth D. Merry * in the table that is below this number 1121931eeffaSKenneth D. Merry * must be an index from free-list tracking. 1122931eeffaSKenneth D. Merry */ 1123931eeffaSKenneth D. Merry if (((uintptr_t)m) <= NET_TX_RING_SIZE) 112489e0f4d2SKip Macy continue; 112596375eacSRoger Pau Monné gnttab_end_foreign_access_ref(txq->grant_ref[i]); 112696375eacSRoger Pau Monné gnttab_release_grant_reference(&txq->gref_head, 112796375eacSRoger Pau Monné txq->grant_ref[i]); 112896375eacSRoger Pau Monné txq->grant_ref[i] = GRANT_REF_INVALID; 112996375eacSRoger Pau Monné add_id_to_freelist(txq->mbufs, i); 113096375eacSRoger Pau Monné txq->mbufs_cnt--; 113196375eacSRoger Pau Monné if (txq->mbufs_cnt < 0) { 11326f9767acSMarius Strobl panic("%s: tx_chain_cnt must be >= 0", __func__); 1133a4ec37f5SAdrian Chadd } 1134dabb3db7SRoger Pau Monné mbuf_release(m); 113589e0f4d2SKip Macy } 113689e0f4d2SKip Macy } 113789e0f4d2SKip Macy 11382568ee67SRoger Pau Monné static struct mbuf * 11392568ee67SRoger Pau Monné xn_alloc_one_rx_buffer(struct netfront_rxq *rxq) 11402568ee67SRoger Pau Monné { 11412568ee67SRoger Pau Monné struct mbuf *m; 11422568ee67SRoger Pau Monné 11432568ee67SRoger Pau Monné m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 11442568ee67SRoger Pau Monné if (m == NULL) 11452568ee67SRoger Pau Monné return NULL; 11462568ee67SRoger Pau Monné m->m_len = m->m_pkthdr.len = MJUMPAGESIZE; 11472568ee67SRoger Pau Monné 11482568ee67SRoger Pau Monné return (m); 11492568ee67SRoger Pau Monné } 11502568ee67SRoger Pau Monné 115189e0f4d2SKip Macy static void 115296375eacSRoger Pau Monné xn_alloc_rx_buffers(struct netfront_rxq *rxq) 115389e0f4d2SKip Macy { 115489e0f4d2SKip Macy RING_IDX req_prod; 11552568ee67SRoger Pau Monné int notify; 11562568ee67SRoger Pau Monné 11572568ee67SRoger Pau Monné XN_RX_LOCK_ASSERT(rxq); 11582568ee67SRoger Pau Monné 11592568ee67SRoger Pau Monné if (__predict_false(rxq->info->carrier == 0)) 11602568ee67SRoger Pau Monné return; 11612568ee67SRoger Pau Monné 11622568ee67SRoger Pau Monné for (req_prod = rxq->ring.req_prod_pvt; 11632568ee67SRoger Pau Monné req_prod - rxq->ring.rsp_cons < NET_RX_RING_SIZE; 11642568ee67SRoger Pau Monné req_prod++) { 11652568ee67SRoger Pau Monné struct mbuf *m; 11662568ee67SRoger Pau Monné unsigned short id; 116789e0f4d2SKip Macy grant_ref_t ref; 11682568ee67SRoger Pau Monné struct netif_rx_request *req; 11692568ee67SRoger Pau Monné unsigned long pfn; 117089e0f4d2SKip Macy 11712568ee67SRoger Pau Monné m = xn_alloc_one_rx_buffer(rxq); 11722568ee67SRoger Pau Monné if (m == NULL) 117389e0f4d2SKip Macy break; 117489e0f4d2SKip Macy 11752568ee67SRoger Pau Monné id = xn_rxidx(req_prod); 117689e0f4d2SKip Macy 117796375eacSRoger Pau Monné KASSERT(rxq->mbufs[id] == NULL, ("non-NULL xn_rx_chain")); 11782568ee67SRoger Pau Monné rxq->mbufs[id] = m; 117989e0f4d2SKip Macy 118096375eacSRoger Pau Monné ref = gnttab_claim_grant_reference(&rxq->gref_head); 1181ff662b5cSJustin T. Gibbs KASSERT(ref != GNTTAB_LIST_END, 1182ff662b5cSJustin T. Gibbs ("reserved grant references exhuasted")); 118396375eacSRoger Pau Monné rxq->grant_ref[id] = ref; 118489e0f4d2SKip Macy 11852568ee67SRoger Pau Monné pfn = atop(vtophys(mtod(m, vm_offset_t))); 11862568ee67SRoger Pau Monné req = RING_GET_REQUEST(&rxq->ring, req_prod); 118789e0f4d2SKip Macy 11882568ee67SRoger Pau Monné gnttab_grant_foreign_access_ref(ref, 11892568ee67SRoger Pau Monné xenbus_get_otherend_id(rxq->info->xbdev), pfn, 0); 119089e0f4d2SKip Macy req->id = id; 119189e0f4d2SKip Macy req->gref = ref; 119289e0f4d2SKip Macy } 119389e0f4d2SKip Macy 11942568ee67SRoger Pau Monné rxq->ring.req_prod_pvt = req_prod; 119589e0f4d2SKip Macy 11962568ee67SRoger Pau Monné /* Not enough requests? Try again later. */ 11972568ee67SRoger Pau Monné if (req_prod - rxq->ring.rsp_cons < NET_RX_SLOTS_MIN) { 1198bf7b50dbSRoger Pau Monné callout_reset_curcpu(&rxq->rx_refill, hz/10, 1199bf7b50dbSRoger Pau Monné xn_alloc_rx_buffers_callout, rxq); 12002568ee67SRoger Pau Monné return; 12012568ee67SRoger Pau Monné } 12022568ee67SRoger Pau Monné 12032568ee67SRoger Pau Monné wmb(); /* barrier so backend seens requests */ 12042568ee67SRoger Pau Monné 120596375eacSRoger Pau Monné RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rxq->ring, notify); 120689e0f4d2SKip Macy if (notify) 120796375eacSRoger Pau Monné xen_intr_signal(rxq->xen_intr_handle); 120889e0f4d2SKip Macy } 120989e0f4d2SKip Macy 12102568ee67SRoger Pau Monné static void xn_alloc_rx_buffers_callout(void *arg) 12112568ee67SRoger Pau Monné { 12122568ee67SRoger Pau Monné struct netfront_rxq *rxq; 12132568ee67SRoger Pau Monné 12142568ee67SRoger Pau Monné rxq = (struct netfront_rxq *)arg; 12152568ee67SRoger Pau Monné XN_RX_LOCK(rxq); 12162568ee67SRoger Pau Monné xn_alloc_rx_buffers(rxq); 12172568ee67SRoger Pau Monné XN_RX_UNLOCK(rxq); 12182568ee67SRoger Pau Monné } 12192568ee67SRoger Pau Monné 122089e0f4d2SKip Macy static void 122196375eacSRoger Pau Monné xn_release_rx_bufs(struct netfront_rxq *rxq) 122296375eacSRoger Pau Monné { 122396375eacSRoger Pau Monné int i, ref; 122496375eacSRoger Pau Monné struct mbuf *m; 122596375eacSRoger Pau Monné 122696375eacSRoger Pau Monné for (i = 0; i < NET_RX_RING_SIZE; i++) { 122796375eacSRoger Pau Monné m = rxq->mbufs[i]; 122896375eacSRoger Pau Monné 122996375eacSRoger Pau Monné if (m == NULL) 123096375eacSRoger Pau Monné continue; 123196375eacSRoger Pau Monné 123296375eacSRoger Pau Monné ref = rxq->grant_ref[i]; 123396375eacSRoger Pau Monné if (ref == GRANT_REF_INVALID) 123496375eacSRoger Pau Monné continue; 123596375eacSRoger Pau Monné 123696375eacSRoger Pau Monné gnttab_end_foreign_access_ref(ref); 123796375eacSRoger Pau Monné gnttab_release_grant_reference(&rxq->gref_head, ref); 123896375eacSRoger Pau Monné rxq->mbufs[i] = NULL; 123996375eacSRoger Pau Monné rxq->grant_ref[i] = GRANT_REF_INVALID; 124096375eacSRoger Pau Monné m_freem(m); 124196375eacSRoger Pau Monné } 124296375eacSRoger Pau Monné } 124396375eacSRoger Pau Monné 124496375eacSRoger Pau Monné static void 124596375eacSRoger Pau Monné xn_rxeof(struct netfront_rxq *rxq) 124689e0f4d2SKip Macy { 124702f3b17fSJustin Hibbits if_t ifp; 124896375eacSRoger Pau Monné struct netfront_info *np = rxq->info; 12493778878dSRoger Pau Monné #if (defined(INET) || defined(INET6)) 125096375eacSRoger Pau Monné struct lro_ctrl *lro = &rxq->lro; 12513778878dSRoger Pau Monné #endif 125289e0f4d2SKip Macy struct netfront_rx_info rinfo; 125389e0f4d2SKip Macy struct netif_rx_response *rx = &rinfo.rx; 125489e0f4d2SKip Macy struct netif_extra_info *extras = rinfo.extras; 125589e0f4d2SKip Macy RING_IDX i, rp; 125689e0f4d2SKip Macy struct mbuf *m; 125796375eacSRoger Pau Monné struct mbufq mbufq_rxq, mbufq_errq; 1258d0f3a8b9SRoger Pau Monné int err, work_to_do; 125989e0f4d2SKip Macy 126096375eacSRoger Pau Monné XN_RX_LOCK_ASSERT(rxq); 1261bf319173SRoger Pau Monné 126289e0f4d2SKip Macy if (!netfront_carrier_ok(np)) 126389e0f4d2SKip Macy return; 126489e0f4d2SKip Macy 1265c578b6acSGleb Smirnoff /* XXX: there should be some sane limit. */ 126696375eacSRoger Pau Monné mbufq_init(&mbufq_errq, INT_MAX); 126796375eacSRoger Pau Monné mbufq_init(&mbufq_rxq, INT_MAX); 126889e0f4d2SKip Macy 126989e0f4d2SKip Macy ifp = np->xn_ifp; 127089e0f4d2SKip Macy 1271bf319173SRoger Pau Monné do { 127296375eacSRoger Pau Monné rp = rxq->ring.sring->rsp_prod; 127389e0f4d2SKip Macy rmb(); /* Ensure we see queued responses up to 'rp'. */ 127489e0f4d2SKip Macy 127596375eacSRoger Pau Monné i = rxq->ring.rsp_cons; 127689e0f4d2SKip Macy while ((i != rp)) { 127796375eacSRoger Pau Monné memcpy(rx, RING_GET_RESPONSE(&rxq->ring, i), sizeof(*rx)); 127889e0f4d2SKip Macy memset(extras, 0, sizeof(rinfo.extras)); 127989e0f4d2SKip Macy 128083b92f6eSKip Macy m = NULL; 128196375eacSRoger Pau Monné err = xn_get_responses(rxq, &rinfo, rp, &i, &m); 128289e0f4d2SKip Macy 128376acc41fSJustin T. Gibbs if (__predict_false(err)) { 128483b92f6eSKip Macy if (m) 128596375eacSRoger Pau Monné (void )mbufq_enqueue(&mbufq_errq, m); 1286b2fd6999SRoger Pau Monné if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1); 128789e0f4d2SKip Macy continue; 128889e0f4d2SKip Macy } 128989e0f4d2SKip Macy 129089e0f4d2SKip Macy m->m_pkthdr.rcvif = ifp; 129189e0f4d2SKip Macy if (rx->flags & NETRXF_data_validated) { 129289e0f4d2SKip Macy /* 1293a81683c3SRoger Pau Monné * According to mbuf(9) the correct way to tell 1294a81683c3SRoger Pau Monné * the stack that the checksum of an inbound 1295a81683c3SRoger Pau Monné * packet is correct, without it actually being 1296a81683c3SRoger Pau Monné * present (because the underlying interface 1297a81683c3SRoger Pau Monné * doesn't provide it), is to set the 1298a81683c3SRoger Pau Monné * CSUM_DATA_VALID and CSUM_PSEUDO_HDR flags, 1299a81683c3SRoger Pau Monné * and the csum_data field to 0xffff. 130089e0f4d2SKip Macy */ 1301a81683c3SRoger Pau Monné m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID 130289e0f4d2SKip Macy | CSUM_PSEUDO_HDR); 130389e0f4d2SKip Macy m->m_pkthdr.csum_data = 0xffff; 130489e0f4d2SKip Macy } 1305d9a66b6dSRoger Pau Monné if ((rx->flags & NETRXF_extra_info) != 0 && 1306d9a66b6dSRoger Pau Monné (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type == 1307d9a66b6dSRoger Pau Monné XEN_NETIF_EXTRA_TYPE_GSO)) { 1308d9a66b6dSRoger Pau Monné m->m_pkthdr.tso_segsz = 1309d9a66b6dSRoger Pau Monné extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].u.gso.size; 1310d9a66b6dSRoger Pau Monné m->m_pkthdr.csum_flags |= CSUM_TSO; 1311d9a66b6dSRoger Pau Monné } 131289e0f4d2SKip Macy 131396375eacSRoger Pau Monné (void )mbufq_enqueue(&mbufq_rxq, m); 131489e0f4d2SKip Macy } 131589e0f4d2SKip Macy 1316bf319173SRoger Pau Monné rxq->ring.rsp_cons = i; 131789e0f4d2SKip Macy 1318bf319173SRoger Pau Monné xn_alloc_rx_buffers(rxq); 1319bf319173SRoger Pau Monné 1320bf319173SRoger Pau Monné RING_FINAL_CHECK_FOR_RESPONSES(&rxq->ring, work_to_do); 1321bf319173SRoger Pau Monné } while (work_to_do); 1322bf319173SRoger Pau Monné 1323bf319173SRoger Pau Monné mbufq_drain(&mbufq_errq); 132489e0f4d2SKip Macy /* 132589e0f4d2SKip Macy * Process all the mbufs after the remapping is complete. 132689e0f4d2SKip Macy * Break the mbuf chain first though. 132789e0f4d2SKip Macy */ 132896375eacSRoger Pau Monné while ((m = mbufq_dequeue(&mbufq_rxq)) != NULL) { 1329c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 133008c9c2e0SRoger Pau Monné #if (defined(INET) || defined(INET6)) 133112678024SDoug Rabson /* Use LRO if possible */ 133202f3b17fSJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_LRO) == 0 || 133312678024SDoug Rabson lro->lro_cnt == 0 || tcp_lro_rx(lro, m, 0)) { 133412678024SDoug Rabson /* 133512678024SDoug Rabson * If LRO fails, pass up to the stack 133612678024SDoug Rabson * directly. 133712678024SDoug Rabson */ 133802f3b17fSJustin Hibbits if_input(ifp, m); 133912678024SDoug Rabson } 134012678024SDoug Rabson #else 134102f3b17fSJustin Hibbits if_input(ifp, m); 134212678024SDoug Rabson #endif 134389e0f4d2SKip Macy } 134489e0f4d2SKip Macy 134508c9c2e0SRoger Pau Monné #if (defined(INET) || defined(INET6)) 134612678024SDoug Rabson /* 134712678024SDoug Rabson * Flush any outstanding LRO work 134812678024SDoug Rabson */ 13496dd38b87SSepherosa Ziehau tcp_lro_flush_all(lro); 135012678024SDoug Rabson #endif 135189e0f4d2SKip Macy } 135289e0f4d2SKip Macy 135389e0f4d2SKip Macy static void 135496375eacSRoger Pau Monné xn_txeof(struct netfront_txq *txq) 135589e0f4d2SKip Macy { 135689e0f4d2SKip Macy RING_IDX i, prod; 135789e0f4d2SKip Macy unsigned short id; 135802f3b17fSJustin Hibbits if_t ifp; 135912678024SDoug Rabson netif_tx_response_t *txr; 136089e0f4d2SKip Macy struct mbuf *m; 136196375eacSRoger Pau Monné struct netfront_info *np = txq->info; 136289e0f4d2SKip Macy 136396375eacSRoger Pau Monné XN_TX_LOCK_ASSERT(txq); 136489e0f4d2SKip Macy 136589e0f4d2SKip Macy if (!netfront_carrier_ok(np)) 136689e0f4d2SKip Macy return; 136789e0f4d2SKip Macy 136889e0f4d2SKip Macy ifp = np->xn_ifp; 136989e0f4d2SKip Macy 137089e0f4d2SKip Macy do { 137196375eacSRoger Pau Monné prod = txq->ring.sring->rsp_prod; 137289e0f4d2SKip Macy rmb(); /* Ensure we see responses up to 'rp'. */ 137389e0f4d2SKip Macy 137496375eacSRoger Pau Monné for (i = txq->ring.rsp_cons; i != prod; i++) { 137596375eacSRoger Pau Monné txr = RING_GET_RESPONSE(&txq->ring, i); 137612678024SDoug Rabson if (txr->status == NETIF_RSP_NULL) 137712678024SDoug Rabson continue; 137812678024SDoug Rabson 1379931eeffaSKenneth D. Merry if (txr->status != NETIF_RSP_OKAY) { 1380931eeffaSKenneth D. Merry printf("%s: WARNING: response is %d!\n", 1381931eeffaSKenneth D. Merry __func__, txr->status); 1382931eeffaSKenneth D. Merry } 138312678024SDoug Rabson id = txr->id; 138496375eacSRoger Pau Monné m = txq->mbufs[id]; 138596375eacSRoger Pau Monné KASSERT(m != NULL, ("mbuf not found in chain")); 1386931eeffaSKenneth D. Merry KASSERT((uintptr_t)m > NET_TX_RING_SIZE, 1387931eeffaSKenneth D. Merry ("mbuf already on the free list, but we're " 1388931eeffaSKenneth D. Merry "trying to free it again!")); 13892d8fae98SAdrian Chadd M_ASSERTVALID(m); 139089e0f4d2SKip Macy 139176acc41fSJustin T. Gibbs if (__predict_false(gnttab_query_foreign_access( 139296375eacSRoger Pau Monné txq->grant_ref[id]) != 0)) { 13936f9767acSMarius Strobl panic("%s: grant id %u still in use by the " 13946f9767acSMarius Strobl "backend", __func__, id); 139589e0f4d2SKip Macy } 139696375eacSRoger Pau Monné gnttab_end_foreign_access_ref(txq->grant_ref[id]); 139789e0f4d2SKip Macy gnttab_release_grant_reference( 139896375eacSRoger Pau Monné &txq->gref_head, txq->grant_ref[id]); 139996375eacSRoger Pau Monné txq->grant_ref[id] = GRANT_REF_INVALID; 140089e0f4d2SKip Macy 140196375eacSRoger Pau Monné txq->mbufs[id] = NULL; 140296375eacSRoger Pau Monné add_id_to_freelist(txq->mbufs, id); 140396375eacSRoger Pau Monné txq->mbufs_cnt--; 1404dabb3db7SRoger Pau Monné mbuf_release(m); 140596375eacSRoger Pau Monné /* Only mark the txq active if we've freed up at least one slot to try */ 140602f3b17fSJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE); 140789e0f4d2SKip Macy } 140896375eacSRoger Pau Monné txq->ring.rsp_cons = prod; 140989e0f4d2SKip Macy 141089e0f4d2SKip Macy /* 141189e0f4d2SKip Macy * Set a new event, then check for race with update of 141289e0f4d2SKip Macy * tx_cons. Note that it is essential to schedule a 141389e0f4d2SKip Macy * callback, no matter how few buffers are pending. Even if 141489e0f4d2SKip Macy * there is space in the transmit ring, higher layers may 141589e0f4d2SKip Macy * be blocked because too much data is outstanding: in such 141689e0f4d2SKip Macy * cases notification from Xen is likely to be the only kick 141789e0f4d2SKip Macy * that we'll get. 141889e0f4d2SKip Macy */ 141996375eacSRoger Pau Monné txq->ring.sring->rsp_event = 142096375eacSRoger Pau Monné prod + ((txq->ring.sring->req_prod - prod) >> 1) + 1; 142189e0f4d2SKip Macy 142289e0f4d2SKip Macy mb(); 142396375eacSRoger Pau Monné } while (prod != txq->ring.sring->rsp_prod); 142489e0f4d2SKip Macy 142596375eacSRoger Pau Monné if (txq->full && 142696375eacSRoger Pau Monné ((txq->ring.sring->req_prod - prod) < NET_TX_RING_SIZE)) { 142796375eacSRoger Pau Monné txq->full = false; 1428da695b05SRoger Pau Monné xn_txq_start(txq); 142989e0f4d2SKip Macy } 143089e0f4d2SKip Macy } 143189e0f4d2SKip Macy 143289e0f4d2SKip Macy static void 143396375eacSRoger Pau Monné xn_intr(void *xsc) 143496375eacSRoger Pau Monné { 143596375eacSRoger Pau Monné struct netfront_txq *txq = xsc; 143696375eacSRoger Pau Monné struct netfront_info *np = txq->info; 143796375eacSRoger Pau Monné struct netfront_rxq *rxq = &np->rxq[txq->id]; 143896375eacSRoger Pau Monné 143996375eacSRoger Pau Monné /* kick both tx and rx */ 144096375eacSRoger Pau Monné xn_rxq_intr(rxq); 144196375eacSRoger Pau Monné xn_txq_intr(txq); 144296375eacSRoger Pau Monné } 144396375eacSRoger Pau Monné 144496375eacSRoger Pau Monné static void 144596375eacSRoger Pau Monné xn_move_rx_slot(struct netfront_rxq *rxq, struct mbuf *m, 144696375eacSRoger Pau Monné grant_ref_t ref) 144796375eacSRoger Pau Monné { 144896375eacSRoger Pau Monné int new = xn_rxidx(rxq->ring.req_prod_pvt); 144996375eacSRoger Pau Monné 145096375eacSRoger Pau Monné KASSERT(rxq->mbufs[new] == NULL, ("mbufs != NULL")); 145196375eacSRoger Pau Monné rxq->mbufs[new] = m; 145296375eacSRoger Pau Monné rxq->grant_ref[new] = ref; 145396375eacSRoger Pau Monné RING_GET_REQUEST(&rxq->ring, rxq->ring.req_prod_pvt)->id = new; 145496375eacSRoger Pau Monné RING_GET_REQUEST(&rxq->ring, rxq->ring.req_prod_pvt)->gref = ref; 145596375eacSRoger Pau Monné rxq->ring.req_prod_pvt++; 145696375eacSRoger Pau Monné } 145796375eacSRoger Pau Monné 145896375eacSRoger Pau Monné static int 145996375eacSRoger Pau Monné xn_get_extras(struct netfront_rxq *rxq, 1460931eeffaSKenneth D. Merry struct netif_extra_info *extras, RING_IDX rp, RING_IDX *cons) 146189e0f4d2SKip Macy { 146289e0f4d2SKip Macy struct netif_extra_info *extra; 146389e0f4d2SKip Macy 146489e0f4d2SKip Macy int err = 0; 146589e0f4d2SKip Macy 146689e0f4d2SKip Macy do { 146789e0f4d2SKip Macy struct mbuf *m; 146889e0f4d2SKip Macy grant_ref_t ref; 146989e0f4d2SKip Macy 147076acc41fSJustin T. Gibbs if (__predict_false(*cons + 1 == rp)) { 1471931eeffaSKenneth D. Merry err = EINVAL; 147289e0f4d2SKip Macy break; 147389e0f4d2SKip Macy } 147489e0f4d2SKip Macy 147589e0f4d2SKip Macy extra = (struct netif_extra_info *) 147696375eacSRoger Pau Monné RING_GET_RESPONSE(&rxq->ring, ++(*cons)); 147789e0f4d2SKip Macy 147876acc41fSJustin T. Gibbs if (__predict_false(!extra->type || 147989e0f4d2SKip Macy extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) { 1480931eeffaSKenneth D. Merry err = EINVAL; 148189e0f4d2SKip Macy } else { 148289e0f4d2SKip Macy memcpy(&extras[extra->type - 1], extra, sizeof(*extra)); 148389e0f4d2SKip Macy } 148489e0f4d2SKip Macy 148596375eacSRoger Pau Monné m = xn_get_rx_mbuf(rxq, *cons); 148696375eacSRoger Pau Monné ref = xn_get_rx_ref(rxq, *cons); 148796375eacSRoger Pau Monné xn_move_rx_slot(rxq, m, ref); 148889e0f4d2SKip Macy } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE); 148989e0f4d2SKip Macy 149089e0f4d2SKip Macy return err; 149189e0f4d2SKip Macy } 149289e0f4d2SKip Macy 149389e0f4d2SKip Macy static int 149496375eacSRoger Pau Monné xn_get_responses(struct netfront_rxq *rxq, 1495931eeffaSKenneth D. Merry struct netfront_rx_info *rinfo, RING_IDX rp, RING_IDX *cons, 1496d0f3a8b9SRoger Pau Monné struct mbuf **list) 149789e0f4d2SKip Macy { 149889e0f4d2SKip Macy struct netif_rx_response *rx = &rinfo->rx; 149989e0f4d2SKip Macy struct netif_extra_info *extras = rinfo->extras; 150083b92f6eSKip Macy struct mbuf *m, *m0, *m_prev; 150196375eacSRoger Pau Monné grant_ref_t ref = xn_get_rx_ref(rxq, *cons); 150289e0f4d2SKip Macy int frags = 1; 150389e0f4d2SKip Macy int err = 0; 1504e7236a7dSMateusz Guzik u_long ret __diagused; 150589e0f4d2SKip Macy 150696375eacSRoger Pau Monné m0 = m = m_prev = xn_get_rx_mbuf(rxq, *cons); 150783b92f6eSKip Macy 150889e0f4d2SKip Macy if (rx->flags & NETRXF_extra_info) { 150996375eacSRoger Pau Monné err = xn_get_extras(rxq, extras, rp, cons); 151089e0f4d2SKip Macy } 151189e0f4d2SKip Macy 151283b92f6eSKip Macy if (m0 != NULL) { 151383b92f6eSKip Macy m0->m_pkthdr.len = 0; 151483b92f6eSKip Macy m0->m_next = NULL; 151583b92f6eSKip Macy } 151683b92f6eSKip Macy 151789e0f4d2SKip Macy for (;;) { 151883b92f6eSKip Macy #if 0 1519227ca257SKip Macy DPRINTK("rx->status=%hd rx->offset=%hu frags=%u\n", 152083b92f6eSKip Macy rx->status, rx->offset, frags); 152183b92f6eSKip Macy #endif 152276acc41fSJustin T. Gibbs if (__predict_false(rx->status < 0 || 152389e0f4d2SKip Macy rx->offset + rx->status > PAGE_SIZE)) { 152496375eacSRoger Pau Monné xn_move_rx_slot(rxq, m, ref); 1525931eeffaSKenneth D. Merry if (m0 == m) 1526931eeffaSKenneth D. Merry m0 = NULL; 1527931eeffaSKenneth D. Merry m = NULL; 1528931eeffaSKenneth D. Merry err = EINVAL; 1529931eeffaSKenneth D. Merry goto next_skip_queue; 153089e0f4d2SKip Macy } 153189e0f4d2SKip Macy 153289e0f4d2SKip Macy /* 153389e0f4d2SKip Macy * This definitely indicates a bug, either in this driver or in 153489e0f4d2SKip Macy * the backend driver. In future this should flag the bad 153589e0f4d2SKip Macy * situation to the system controller to reboot the backed. 153689e0f4d2SKip Macy */ 1537ff662b5cSJustin T. Gibbs if (ref == GRANT_REF_INVALID) { 1538ff662b5cSJustin T. Gibbs printf("%s: Bad rx response id %d.\n", __func__, rx->id); 1539931eeffaSKenneth D. Merry err = EINVAL; 154089e0f4d2SKip Macy goto next; 154189e0f4d2SKip Macy } 154289e0f4d2SKip Macy 1543920ba15bSKip Macy ret = gnttab_end_foreign_access_ref(ref); 1544d0f3a8b9SRoger Pau Monné KASSERT(ret, ("Unable to end access to grant references")); 154589e0f4d2SKip Macy 154696375eacSRoger Pau Monné gnttab_release_grant_reference(&rxq->gref_head, ref); 154789e0f4d2SKip Macy 154889e0f4d2SKip Macy next: 15493a539122SAdrian Chadd if (m == NULL) 15503a539122SAdrian Chadd break; 15513a539122SAdrian Chadd 155283b92f6eSKip Macy m->m_len = rx->status; 155383b92f6eSKip Macy m->m_data += rx->offset; 155483b92f6eSKip Macy m0->m_pkthdr.len += rx->status; 155583b92f6eSKip Macy 1556931eeffaSKenneth D. Merry next_skip_queue: 155789e0f4d2SKip Macy if (!(rx->flags & NETRXF_more_data)) 155889e0f4d2SKip Macy break; 155989e0f4d2SKip Macy 1560931eeffaSKenneth D. Merry if (*cons + frags == rp) { 156189e0f4d2SKip Macy if (net_ratelimit()) 156289e0f4d2SKip Macy WPRINTK("Need more frags\n"); 1563931eeffaSKenneth D. Merry err = ENOENT; 1564931eeffaSKenneth D. Merry printf("%s: cons %u frags %u rp %u, not enough frags\n", 1565931eeffaSKenneth D. Merry __func__, *cons, frags, rp); 156689e0f4d2SKip Macy break; 156789e0f4d2SKip Macy } 1568931eeffaSKenneth D. Merry /* 1569931eeffaSKenneth D. Merry * Note that m can be NULL, if rx->status < 0 or if 1570931eeffaSKenneth D. Merry * rx->offset + rx->status > PAGE_SIZE above. 1571931eeffaSKenneth D. Merry */ 157283b92f6eSKip Macy m_prev = m; 157389e0f4d2SKip Macy 157496375eacSRoger Pau Monné rx = RING_GET_RESPONSE(&rxq->ring, *cons + frags); 157596375eacSRoger Pau Monné m = xn_get_rx_mbuf(rxq, *cons + frags); 157683b92f6eSKip Macy 1577931eeffaSKenneth D. Merry /* 1578931eeffaSKenneth D. Merry * m_prev == NULL can happen if rx->status < 0 or if 1579931eeffaSKenneth D. Merry * rx->offset + * rx->status > PAGE_SIZE above. 1580931eeffaSKenneth D. Merry */ 1581931eeffaSKenneth D. Merry if (m_prev != NULL) 158283b92f6eSKip Macy m_prev->m_next = m; 1583931eeffaSKenneth D. Merry 1584931eeffaSKenneth D. Merry /* 1585931eeffaSKenneth D. Merry * m0 can be NULL if rx->status < 0 or if * rx->offset + 1586931eeffaSKenneth D. Merry * rx->status > PAGE_SIZE above. 1587931eeffaSKenneth D. Merry */ 1588931eeffaSKenneth D. Merry if (m0 == NULL) 1589931eeffaSKenneth D. Merry m0 = m; 159083b92f6eSKip Macy m->m_next = NULL; 159196375eacSRoger Pau Monné ref = xn_get_rx_ref(rxq, *cons + frags); 159289e0f4d2SKip Macy frags++; 159389e0f4d2SKip Macy } 159483b92f6eSKip Macy *list = m0; 1595931eeffaSKenneth D. Merry *cons += frags; 159689e0f4d2SKip Macy 15978577146eSJustin T. Gibbs return (err); 159889e0f4d2SKip Macy } 159989e0f4d2SKip Macy 1600931eeffaSKenneth D. Merry /** 1601931eeffaSKenneth D. Merry * Given an mbuf chain, make sure we have enough room and then push 1602931eeffaSKenneth D. Merry * it onto the transmit ring. 1603931eeffaSKenneth D. Merry */ 1604931eeffaSKenneth D. Merry static int 160596375eacSRoger Pau Monné xn_assemble_tx_request(struct netfront_txq *txq, struct mbuf *m_head) 1606931eeffaSKenneth D. Merry { 160796375eacSRoger Pau Monné struct netfront_info *np = txq->info; 160802f3b17fSJustin Hibbits if_t ifp = np->xn_ifp; 1609dabb3db7SRoger Pau Monné int otherend_id, error, nfrags; 1610dabb3db7SRoger Pau Monné bus_dma_segment_t *segs = txq->segs; 1611dabb3db7SRoger Pau Monné struct mbuf_xennet *tag; 1612dabb3db7SRoger Pau Monné bus_dmamap_t map; 1613dabb3db7SRoger Pau Monné unsigned int i; 1614931eeffaSKenneth D. Merry 1615dabb3db7SRoger Pau Monné KASSERT(!SLIST_EMPTY(&txq->tags), ("no tags available")); 1616dabb3db7SRoger Pau Monné tag = SLIST_FIRST(&txq->tags); 1617dabb3db7SRoger Pau Monné SLIST_REMOVE_HEAD(&txq->tags, next); 1618dabb3db7SRoger Pau Monné KASSERT(tag->count == 0, ("tag already in-use")); 1619dabb3db7SRoger Pau Monné map = tag->dma_map; 1620dabb3db7SRoger Pau Monné error = bus_dmamap_load_mbuf_sg(np->dma_tag, map, m_head, segs, 1621dabb3db7SRoger Pau Monné &nfrags, 0); 1622dabb3db7SRoger Pau Monné if (error == EFBIG || nfrags > np->maxfrags) { 1623dabb3db7SRoger Pau Monné struct mbuf *m; 1624931eeffaSKenneth D. Merry 1625dabb3db7SRoger Pau Monné bus_dmamap_unload(np->dma_tag, map); 1626c6499eccSGleb Smirnoff m = m_defrag(m_head, M_NOWAIT); 162712678024SDoug Rabson if (!m) { 1628931eeffaSKenneth D. Merry /* 1629931eeffaSKenneth D. Merry * Defrag failed, so free the mbuf and 1630931eeffaSKenneth D. Merry * therefore drop the packet. 1631931eeffaSKenneth D. Merry */ 1632dabb3db7SRoger Pau Monné SLIST_INSERT_HEAD(&txq->tags, tag, next); 163312678024SDoug Rabson m_freem(m_head); 1634931eeffaSKenneth D. Merry return (EMSGSIZE); 163512678024SDoug Rabson } 163612678024SDoug Rabson m_head = m; 1637dabb3db7SRoger Pau Monné error = bus_dmamap_load_mbuf_sg(np->dma_tag, map, m_head, segs, 1638dabb3db7SRoger Pau Monné &nfrags, 0); 1639dabb3db7SRoger Pau Monné if (error != 0 || nfrags > np->maxfrags) { 1640dabb3db7SRoger Pau Monné bus_dmamap_unload(np->dma_tag, map); 1641dabb3db7SRoger Pau Monné SLIST_INSERT_HEAD(&txq->tags, tag, next); 1642dabb3db7SRoger Pau Monné m_freem(m_head); 1643dabb3db7SRoger Pau Monné return (error ?: EFBIG); 1644dabb3db7SRoger Pau Monné } 1645dabb3db7SRoger Pau Monné } else if (error != 0) { 1646dabb3db7SRoger Pau Monné SLIST_INSERT_HEAD(&txq->tags, tag, next); 1647dabb3db7SRoger Pau Monné m_freem(m_head); 1648dabb3db7SRoger Pau Monné return (error); 164912678024SDoug Rabson } 165089e0f4d2SKip Macy 1651931eeffaSKenneth D. Merry /** 1652931eeffaSKenneth D. Merry * The FreeBSD TCP stack, with TSO enabled, can produce a chain 1653931eeffaSKenneth D. Merry * of mbufs longer than Linux can handle. Make sure we don't 1654931eeffaSKenneth D. Merry * pass a too-long chain over to the other side by dropping the 1655931eeffaSKenneth D. Merry * packet. It doesn't look like there is currently a way to 1656931eeffaSKenneth D. Merry * tell the TCP stack to generate a shorter chain of packets. 16573fb28bbbSAdrian Chadd */ 1658931eeffaSKenneth D. Merry if (nfrags > MAX_TX_REQ_FRAGS) { 1659ff662b5cSJustin T. Gibbs #ifdef DEBUG 1660ff662b5cSJustin T. Gibbs printf("%s: nfrags %d > MAX_TX_REQ_FRAGS %d, netback " 1661ff662b5cSJustin T. Gibbs "won't be able to handle it, dropping\n", 1662ff662b5cSJustin T. Gibbs __func__, nfrags, MAX_TX_REQ_FRAGS); 1663ff662b5cSJustin T. Gibbs #endif 1664dabb3db7SRoger Pau Monné SLIST_INSERT_HEAD(&txq->tags, tag, next); 1665dabb3db7SRoger Pau Monné bus_dmamap_unload(np->dma_tag, map); 1666931eeffaSKenneth D. Merry m_freem(m_head); 1667931eeffaSKenneth D. Merry return (EMSGSIZE); 1668a4ec37f5SAdrian Chadd } 1669a4ec37f5SAdrian Chadd 16703fb28bbbSAdrian Chadd /* 1671931eeffaSKenneth D. Merry * This check should be redundant. We've already verified that we 1672931eeffaSKenneth D. Merry * have enough slots in the ring to handle a packet of maximum 1673931eeffaSKenneth D. Merry * size, and that our packet is less than the maximum size. Keep 1674931eeffaSKenneth D. Merry * it in here as an assert for now just to make certain that 167596375eacSRoger Pau Monné * chain_cnt is accurate. 16763fb28bbbSAdrian Chadd */ 167796375eacSRoger Pau Monné KASSERT((txq->mbufs_cnt + nfrags) <= NET_TX_RING_SIZE, 167896375eacSRoger Pau Monné ("%s: chain_cnt (%d) + nfrags (%d) > NET_TX_RING_SIZE " 167996375eacSRoger Pau Monné "(%d)!", __func__, (int) txq->mbufs_cnt, 1680931eeffaSKenneth D. Merry (int) nfrags, (int) NET_TX_RING_SIZE)); 1681a4ec37f5SAdrian Chadd 168289e0f4d2SKip Macy /* 168389e0f4d2SKip Macy * Start packing the mbufs in this chain into 168489e0f4d2SKip Macy * the fragment pointers. Stop when we run out 168589e0f4d2SKip Macy * of fragments or hit the end of the mbuf chain. 168689e0f4d2SKip Macy */ 168796375eacSRoger Pau Monné otherend_id = xenbus_get_otherend_id(np->xbdev); 1688dabb3db7SRoger Pau Monné m_tag_prepend(m_head, &tag->tag); 1689dabb3db7SRoger Pau Monné for (i = 0; i < nfrags; i++) { 1690931eeffaSKenneth D. Merry netif_tx_request_t *tx; 1691931eeffaSKenneth D. Merry uintptr_t id; 1692931eeffaSKenneth D. Merry grant_ref_t ref; 1693931eeffaSKenneth D. Merry u_long mfn; /* XXX Wrong type? */ 1694931eeffaSKenneth D. Merry 169596375eacSRoger Pau Monné tx = RING_GET_REQUEST(&txq->ring, txq->ring.req_prod_pvt); 169696375eacSRoger Pau Monné id = get_id_from_freelist(txq->mbufs); 1697a4ec37f5SAdrian Chadd if (id == 0) 16986f9767acSMarius Strobl panic("%s: was allocated the freelist head!\n", 16996f9767acSMarius Strobl __func__); 170096375eacSRoger Pau Monné txq->mbufs_cnt++; 170196375eacSRoger Pau Monné if (txq->mbufs_cnt > NET_TX_RING_SIZE) 17026f9767acSMarius Strobl panic("%s: tx_chain_cnt must be <= NET_TX_RING_SIZE\n", 17036f9767acSMarius Strobl __func__); 1704dabb3db7SRoger Pau Monné mbuf_grab(m_head); 1705dabb3db7SRoger Pau Monné txq->mbufs[id] = m_head; 170689e0f4d2SKip Macy tx->id = id; 170796375eacSRoger Pau Monné ref = gnttab_claim_grant_reference(&txq->gref_head); 170889e0f4d2SKip Macy KASSERT((short)ref >= 0, ("Negative ref")); 1709dabb3db7SRoger Pau Monné mfn = atop(segs[i].ds_addr); 171023dc5621SKip Macy gnttab_grant_foreign_access_ref(ref, otherend_id, 171189e0f4d2SKip Macy mfn, GNTMAP_readonly); 171296375eacSRoger Pau Monné tx->gref = txq->grant_ref[id] = ref; 1713dabb3db7SRoger Pau Monné tx->offset = segs[i].ds_addr & PAGE_MASK; 1714dabb3db7SRoger Pau Monné KASSERT(tx->offset + segs[i].ds_len <= PAGE_SIZE, 1715dabb3db7SRoger Pau Monné ("mbuf segment crosses a page boundary")); 171689e0f4d2SKip Macy tx->flags = 0; 1717dabb3db7SRoger Pau Monné if (i == 0) { 171812678024SDoug Rabson /* 171912678024SDoug Rabson * The first fragment has the entire packet 172012678024SDoug Rabson * size, subsequent fragments have just the 172112678024SDoug Rabson * fragment size. The backend works out the 172212678024SDoug Rabson * true size of the first fragment by 172312678024SDoug Rabson * subtracting the sizes of the other 172412678024SDoug Rabson * fragments. 172512678024SDoug Rabson */ 1726dabb3db7SRoger Pau Monné tx->size = m_head->m_pkthdr.len; 172789e0f4d2SKip Macy 172812678024SDoug Rabson /* 1729931eeffaSKenneth D. Merry * The first fragment contains the checksum flags 1730931eeffaSKenneth D. Merry * and is optionally followed by extra data for 1731931eeffaSKenneth D. Merry * TSO etc. 1732931eeffaSKenneth D. Merry */ 1733931eeffaSKenneth D. Merry /** 1734931eeffaSKenneth D. Merry * CSUM_TSO requires checksum offloading. 1735931eeffaSKenneth D. Merry * Some versions of FreeBSD fail to 1736931eeffaSKenneth D. Merry * set CSUM_TCP in the CSUM_TSO case, 1737931eeffaSKenneth D. Merry * so we have to test for CSUM_TSO 1738931eeffaSKenneth D. Merry * explicitly. 173912678024SDoug Rabson */ 1740dabb3db7SRoger Pau Monné if (m_head->m_pkthdr.csum_flags 1741931eeffaSKenneth D. Merry & (CSUM_DELAY_DATA | CSUM_TSO)) { 174212678024SDoug Rabson tx->flags |= (NETTXF_csum_blank 174312678024SDoug Rabson | NETTXF_data_validated); 174412678024SDoug Rabson } 1745dabb3db7SRoger Pau Monné if (m_head->m_pkthdr.csum_flags & CSUM_TSO) { 174612678024SDoug Rabson struct netif_extra_info *gso = 174712678024SDoug Rabson (struct netif_extra_info *) 174896375eacSRoger Pau Monné RING_GET_REQUEST(&txq->ring, 174996375eacSRoger Pau Monné ++txq->ring.req_prod_pvt); 175089e0f4d2SKip Macy 175112678024SDoug Rabson tx->flags |= NETTXF_extra_info; 175289e0f4d2SKip Macy 1753dabb3db7SRoger Pau Monné gso->u.gso.size = m_head->m_pkthdr.tso_segsz; 175412678024SDoug Rabson gso->u.gso.type = 175512678024SDoug Rabson XEN_NETIF_GSO_TYPE_TCPV4; 175612678024SDoug Rabson gso->u.gso.pad = 0; 175712678024SDoug Rabson gso->u.gso.features = 0; 175812678024SDoug Rabson 175912678024SDoug Rabson gso->type = XEN_NETIF_EXTRA_TYPE_GSO; 176012678024SDoug Rabson gso->flags = 0; 176112678024SDoug Rabson } 176212678024SDoug Rabson } else { 1763dabb3db7SRoger Pau Monné tx->size = segs[i].ds_len; 176412678024SDoug Rabson } 1765dabb3db7SRoger Pau Monné if (i != nfrags - 1) 176612678024SDoug Rabson tx->flags |= NETTXF_more_data; 176712678024SDoug Rabson 176896375eacSRoger Pau Monné txq->ring.req_prod_pvt++; 1769931eeffaSKenneth D. Merry } 1770dabb3db7SRoger Pau Monné bus_dmamap_sync(np->dma_tag, map, BUS_DMASYNC_PREWRITE); 177112678024SDoug Rabson BPF_MTAP(ifp, m_head); 177212678024SDoug Rabson 1773b2fd6999SRoger Pau Monné if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 1774b2fd6999SRoger Pau Monné if_inc_counter(ifp, IFCOUNTER_OBYTES, m_head->m_pkthdr.len); 1775b2fd6999SRoger Pau Monné if (m_head->m_flags & M_MCAST) 1776b2fd6999SRoger Pau Monné if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1); 177796375eacSRoger Pau Monné 1778b2fd6999SRoger Pau Monné xn_txeof(txq); 1779931eeffaSKenneth D. Merry 1780931eeffaSKenneth D. Merry return (0); 178189e0f4d2SKip Macy } 178289e0f4d2SKip Macy 178389e0f4d2SKip Macy /* equivalent of network_open() in Linux */ 178489e0f4d2SKip Macy static void 178596375eacSRoger Pau Monné xn_ifinit_locked(struct netfront_info *np) 178689e0f4d2SKip Macy { 178702f3b17fSJustin Hibbits if_t ifp; 178896375eacSRoger Pau Monné int i; 178996375eacSRoger Pau Monné struct netfront_rxq *rxq; 179089e0f4d2SKip Macy 179196375eacSRoger Pau Monné XN_LOCK_ASSERT(np); 179289e0f4d2SKip Macy 179396375eacSRoger Pau Monné ifp = np->xn_ifp; 179489e0f4d2SKip Macy 179502f3b17fSJustin Hibbits if (if_getdrvflags(ifp) & IFF_DRV_RUNNING || !netfront_carrier_ok(np)) 179689e0f4d2SKip Macy return; 179789e0f4d2SKip Macy 179896375eacSRoger Pau Monné xn_stop(np); 179989e0f4d2SKip Macy 180096375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 180196375eacSRoger Pau Monné rxq = &np->rxq[i]; 18022568ee67SRoger Pau Monné XN_RX_LOCK(rxq); 180396375eacSRoger Pau Monné xn_alloc_rx_buffers(rxq); 180496375eacSRoger Pau Monné rxq->ring.sring->rsp_event = rxq->ring.rsp_cons + 1; 18052568ee67SRoger Pau Monné if (RING_HAS_UNCONSUMED_RESPONSES(&rxq->ring)) 1806da695b05SRoger Pau Monné xn_rxeof(rxq); 18072568ee67SRoger Pau Monné XN_RX_UNLOCK(rxq); 180896375eacSRoger Pau Monné } 180989e0f4d2SKip Macy 181002f3b17fSJustin Hibbits if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0); 181102f3b17fSJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE); 18120e509842SJustin T. Gibbs if_link_state_change(ifp, LINK_STATE_UP); 181389e0f4d2SKip Macy } 181489e0f4d2SKip Macy 181589e0f4d2SKip Macy static void 181689e0f4d2SKip Macy xn_ifinit(void *xsc) 181789e0f4d2SKip Macy { 181889e0f4d2SKip Macy struct netfront_info *sc = xsc; 181989e0f4d2SKip Macy 182089e0f4d2SKip Macy XN_LOCK(sc); 182189e0f4d2SKip Macy xn_ifinit_locked(sc); 182289e0f4d2SKip Macy XN_UNLOCK(sc); 182389e0f4d2SKip Macy } 182489e0f4d2SKip Macy 182589e0f4d2SKip Macy static int 182602f3b17fSJustin Hibbits xn_ioctl(if_t ifp, u_long cmd, caddr_t data) 182789e0f4d2SKip Macy { 182802f3b17fSJustin Hibbits struct netfront_info *sc = if_getsoftc(ifp); 182989e0f4d2SKip Macy struct ifreq *ifr = (struct ifreq *) data; 1830c2d12e5eSRoger Pau Monné device_t dev; 1831a0ae8f04SBjoern A. Zeeb #ifdef INET 183289e0f4d2SKip Macy struct ifaddr *ifa = (struct ifaddr *)data; 1833a0ae8f04SBjoern A. Zeeb #endif 18343c9d5940SRoger Pau Monné int mask, error = 0, reinit; 1835c2d12e5eSRoger Pau Monné 1836c2d12e5eSRoger Pau Monné dev = sc->xbdev; 1837c2d12e5eSRoger Pau Monné 183889e0f4d2SKip Macy switch(cmd) { 183989e0f4d2SKip Macy case SIOCSIFADDR: 1840a0ae8f04SBjoern A. Zeeb #ifdef INET 184189e0f4d2SKip Macy XN_LOCK(sc); 184289e0f4d2SKip Macy if (ifa->ifa_addr->sa_family == AF_INET) { 184302f3b17fSJustin Hibbits if_setflagbits(ifp, IFF_UP, 0); 184402f3b17fSJustin Hibbits if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) 184589e0f4d2SKip Macy xn_ifinit_locked(sc); 184689e0f4d2SKip Macy arp_ifinit(ifp, ifa); 184789e0f4d2SKip Macy XN_UNLOCK(sc); 184849906218SDoug Rabson } else { 184949906218SDoug Rabson XN_UNLOCK(sc); 1850a0ae8f04SBjoern A. Zeeb #endif 185149906218SDoug Rabson error = ether_ioctl(ifp, cmd, data); 1852a0ae8f04SBjoern A. Zeeb #ifdef INET 185349906218SDoug Rabson } 1854a0ae8f04SBjoern A. Zeeb #endif 185589e0f4d2SKip Macy break; 185689e0f4d2SKip Macy case SIOCSIFMTU: 185702f3b17fSJustin Hibbits if (if_getmtu(ifp) == ifr->ifr_mtu) 1858c74415edSColin Percival break; 1859c74415edSColin Percival 186002f3b17fSJustin Hibbits if_setmtu(ifp, ifr->ifr_mtu); 186102f3b17fSJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING); 186289e0f4d2SKip Macy xn_ifinit(sc); 186389e0f4d2SKip Macy break; 186489e0f4d2SKip Macy case SIOCSIFFLAGS: 186589e0f4d2SKip Macy XN_LOCK(sc); 186602f3b17fSJustin Hibbits if (if_getflags(ifp) & IFF_UP) { 186789e0f4d2SKip Macy /* 186889e0f4d2SKip Macy * If only the state of the PROMISC flag changed, 186989e0f4d2SKip Macy * then just use the 'set promisc mode' command 187089e0f4d2SKip Macy * instead of reinitializing the entire NIC. Doing 187189e0f4d2SKip Macy * a full re-init means reloading the firmware and 187289e0f4d2SKip Macy * waiting for it to start up, which may take a 187389e0f4d2SKip Macy * second or two. 187489e0f4d2SKip Macy */ 187589e0f4d2SKip Macy xn_ifinit_locked(sc); 187689e0f4d2SKip Macy } else { 187702f3b17fSJustin Hibbits if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 187889e0f4d2SKip Macy xn_stop(sc); 187989e0f4d2SKip Macy } 188089e0f4d2SKip Macy } 188102f3b17fSJustin Hibbits sc->xn_if_flags = if_getflags(ifp); 188289e0f4d2SKip Macy XN_UNLOCK(sc); 188389e0f4d2SKip Macy break; 188489e0f4d2SKip Macy case SIOCSIFCAP: 188502f3b17fSJustin Hibbits mask = ifr->ifr_reqcap ^ if_getcapenable(ifp); 18863c9d5940SRoger Pau Monné reinit = 0; 18873c9d5940SRoger Pau Monné 188812678024SDoug Rabson if (mask & IFCAP_TXCSUM) { 188902f3b17fSJustin Hibbits if_togglecapenable(ifp, IFCAP_TXCSUM); 189002f3b17fSJustin Hibbits if_togglehwassist(ifp, XN_CSUM_FEATURES); 189112678024SDoug Rabson } 189212678024SDoug Rabson if (mask & IFCAP_TSO4) { 189302f3b17fSJustin Hibbits if_togglecapenable(ifp, IFCAP_TSO4); 189402f3b17fSJustin Hibbits if_togglehwassist(ifp, CSUM_TSO); 189512678024SDoug Rabson } 189612678024SDoug Rabson 18973c9d5940SRoger Pau Monné if (mask & (IFCAP_RXCSUM | IFCAP_LRO)) { 18983c9d5940SRoger Pau Monné /* These Rx features require us to renegotiate. */ 18993c9d5940SRoger Pau Monné reinit = 1; 19003c9d5940SRoger Pau Monné 19013c9d5940SRoger Pau Monné if (mask & IFCAP_RXCSUM) 190202f3b17fSJustin Hibbits if_togglecapenable(ifp, IFCAP_RXCSUM); 19033c9d5940SRoger Pau Monné if (mask & IFCAP_LRO) 190402f3b17fSJustin Hibbits if_togglecapenable(ifp, IFCAP_LRO); 190512678024SDoug Rabson } 19063c9d5940SRoger Pau Monné 19073c9d5940SRoger Pau Monné if (reinit == 0) 19083c9d5940SRoger Pau Monné break; 19093c9d5940SRoger Pau Monné 1910c2d12e5eSRoger Pau Monné /* 1911c2d12e5eSRoger Pau Monné * We must reset the interface so the backend picks up the 1912c2d12e5eSRoger Pau Monné * new features. 1913c2d12e5eSRoger Pau Monné */ 19143c9d5940SRoger Pau Monné device_printf(sc->xbdev, 19153c9d5940SRoger Pau Monné "performing interface reset due to feature change\n"); 1916c2d12e5eSRoger Pau Monné XN_LOCK(sc); 1917c2d12e5eSRoger Pau Monné netfront_carrier_off(sc); 1918c2d12e5eSRoger Pau Monné sc->xn_reset = true; 1919c2d12e5eSRoger Pau Monné /* 1920c2d12e5eSRoger Pau Monné * NB: the pending packet queue is not flushed, since 1921c2d12e5eSRoger Pau Monné * the interface should still support the old options. 1922c2d12e5eSRoger Pau Monné */ 1923c2d12e5eSRoger Pau Monné XN_UNLOCK(sc); 1924c2d12e5eSRoger Pau Monné /* 1925c2d12e5eSRoger Pau Monné * Delete the xenstore nodes that export features. 1926c2d12e5eSRoger Pau Monné * 1927c2d12e5eSRoger Pau Monné * NB: There's a xenbus state called 1928c2d12e5eSRoger Pau Monné * "XenbusStateReconfiguring", which is what we should set 1929c2d12e5eSRoger Pau Monné * here. Sadly none of the backends know how to handle it, 1930c2d12e5eSRoger Pau Monné * and simply disconnect from the frontend, so we will just 1931c2d12e5eSRoger Pau Monné * switch back to XenbusStateInitialising in order to force 1932c2d12e5eSRoger Pau Monné * a reconnection. 1933c2d12e5eSRoger Pau Monné */ 1934c2d12e5eSRoger Pau Monné xs_rm(XST_NIL, xenbus_get_node(dev), "feature-gso-tcpv4"); 1935c2d12e5eSRoger Pau Monné xs_rm(XST_NIL, xenbus_get_node(dev), "feature-no-csum-offload"); 1936c2d12e5eSRoger Pau Monné xenbus_set_state(dev, XenbusStateClosing); 19373c9d5940SRoger Pau Monné 19383c9d5940SRoger Pau Monné /* 19393c9d5940SRoger Pau Monné * Wait for the frontend to reconnect before returning 19403c9d5940SRoger Pau Monné * from the ioctl. 30s should be more than enough for any 19413c9d5940SRoger Pau Monné * sane backend to reconnect. 19423c9d5940SRoger Pau Monné */ 19433c9d5940SRoger Pau Monné error = tsleep(sc, 0, "xn_rst", 30*hz); 194489e0f4d2SKip Macy break; 194589e0f4d2SKip Macy case SIOCADDMULTI: 194689e0f4d2SKip Macy case SIOCDELMULTI: 1947ce8df48bSSimon J. Gerraty break; 194889e0f4d2SKip Macy case SIOCSIFMEDIA: 194989e0f4d2SKip Macy case SIOCGIFMEDIA: 19500e509842SJustin T. Gibbs error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 195189e0f4d2SKip Macy break; 195289e0f4d2SKip Macy default: 195389e0f4d2SKip Macy error = ether_ioctl(ifp, cmd, data); 195489e0f4d2SKip Macy } 195589e0f4d2SKip Macy 195689e0f4d2SKip Macy return (error); 195789e0f4d2SKip Macy } 195889e0f4d2SKip Macy 195989e0f4d2SKip Macy static void 196089e0f4d2SKip Macy xn_stop(struct netfront_info *sc) 196189e0f4d2SKip Macy { 196202f3b17fSJustin Hibbits if_t ifp; 196389e0f4d2SKip Macy 196489e0f4d2SKip Macy XN_LOCK_ASSERT(sc); 196589e0f4d2SKip Macy 196689e0f4d2SKip Macy ifp = sc->xn_ifp; 196789e0f4d2SKip Macy 196802f3b17fSJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 19690e509842SJustin T. Gibbs if_link_state_change(ifp, LINK_STATE_DOWN); 197089e0f4d2SKip Macy } 197189e0f4d2SKip Macy 197296375eacSRoger Pau Monné static void 197396375eacSRoger Pau Monné xn_rebuild_rx_bufs(struct netfront_rxq *rxq) 197489e0f4d2SKip Macy { 197596375eacSRoger Pau Monné int requeue_idx, i; 197689e0f4d2SKip Macy grant_ref_t ref; 197789e0f4d2SKip Macy netif_rx_request_t *req; 197889e0f4d2SKip Macy 197989e0f4d2SKip Macy for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) { 198089e0f4d2SKip Macy struct mbuf *m; 19813a6d1fcfSKip Macy u_long pfn; 198289e0f4d2SKip Macy 198396375eacSRoger Pau Monné if (rxq->mbufs[i] == NULL) 198489e0f4d2SKip Macy continue; 198589e0f4d2SKip Macy 198696375eacSRoger Pau Monné m = rxq->mbufs[requeue_idx] = xn_get_rx_mbuf(rxq, i); 198796375eacSRoger Pau Monné ref = rxq->grant_ref[requeue_idx] = xn_get_rx_ref(rxq, i); 1988931eeffaSKenneth D. Merry 198996375eacSRoger Pau Monné req = RING_GET_REQUEST(&rxq->ring, requeue_idx); 19903a6d1fcfSKip Macy pfn = vtophys(mtod(m, vm_offset_t)) >> PAGE_SHIFT; 199189e0f4d2SKip Macy 199289e0f4d2SKip Macy gnttab_grant_foreign_access_ref(ref, 199396375eacSRoger Pau Monné xenbus_get_otherend_id(rxq->info->xbdev), 1994ed95805eSJohn Baldwin pfn, 0); 1995d0f3a8b9SRoger Pau Monné 199689e0f4d2SKip Macy req->gref = ref; 199789e0f4d2SKip Macy req->id = requeue_idx; 199889e0f4d2SKip Macy 199989e0f4d2SKip Macy requeue_idx++; 200089e0f4d2SKip Macy } 200189e0f4d2SKip Macy 200296375eacSRoger Pau Monné rxq->ring.req_prod_pvt = requeue_idx; 200396375eacSRoger Pau Monné } 200489e0f4d2SKip Macy 200596375eacSRoger Pau Monné /* START of Xenolinux helper functions adapted to FreeBSD */ 200665671253SRoger Pau Monné static int 200796375eacSRoger Pau Monné xn_connect(struct netfront_info *np) 200896375eacSRoger Pau Monné { 200996375eacSRoger Pau Monné int i, error; 201096375eacSRoger Pau Monné u_int feature_rx_copy; 201196375eacSRoger Pau Monné struct netfront_rxq *rxq; 201296375eacSRoger Pau Monné struct netfront_txq *txq; 201396375eacSRoger Pau Monné 201496375eacSRoger Pau Monné error = xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 201596375eacSRoger Pau Monné "feature-rx-copy", NULL, "%u", &feature_rx_copy); 201696375eacSRoger Pau Monné if (error != 0) 201796375eacSRoger Pau Monné feature_rx_copy = 0; 201896375eacSRoger Pau Monné 201996375eacSRoger Pau Monné /* We only support rx copy. */ 202096375eacSRoger Pau Monné if (!feature_rx_copy) 202196375eacSRoger Pau Monné return (EPROTONOSUPPORT); 202296375eacSRoger Pau Monné 202396375eacSRoger Pau Monné /* Recovery procedure: */ 202496375eacSRoger Pau Monné error = talk_to_backend(np->xbdev, np); 202596375eacSRoger Pau Monné if (error != 0) 202696375eacSRoger Pau Monné return (error); 202796375eacSRoger Pau Monné 202896375eacSRoger Pau Monné /* Step 1: Reinitialise variables. */ 202996375eacSRoger Pau Monné xn_query_features(np); 203096375eacSRoger Pau Monné xn_configure_features(np); 203196375eacSRoger Pau Monné 203296375eacSRoger Pau Monné /* Step 2: Release TX buffer */ 203396375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 203496375eacSRoger Pau Monné txq = &np->txq[i]; 203596375eacSRoger Pau Monné xn_release_tx_bufs(txq); 203696375eacSRoger Pau Monné } 203796375eacSRoger Pau Monné 203896375eacSRoger Pau Monné /* Step 3: Rebuild the RX buffer freelist and the RX ring itself. */ 203996375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 204096375eacSRoger Pau Monné rxq = &np->rxq[i]; 204196375eacSRoger Pau Monné xn_rebuild_rx_bufs(rxq); 204296375eacSRoger Pau Monné } 204396375eacSRoger Pau Monné 204496375eacSRoger Pau Monné /* Step 4: All public and private state should now be sane. Get 204589e0f4d2SKip Macy * ready to start sending and receiving packets and give the driver 204689e0f4d2SKip Macy * domain a kick because we've probably just requeued some 204789e0f4d2SKip Macy * packets. 204889e0f4d2SKip Macy */ 204989e0f4d2SKip Macy netfront_carrier_on(np); 20503c9d5940SRoger Pau Monné wakeup(np); 205165671253SRoger Pau Monné 205265671253SRoger Pau Monné return (0); 205365671253SRoger Pau Monné } 205465671253SRoger Pau Monné 205565671253SRoger Pau Monné static void 205665671253SRoger Pau Monné xn_kick_rings(struct netfront_info *np) 205765671253SRoger Pau Monné { 205865671253SRoger Pau Monné struct netfront_rxq *rxq; 205965671253SRoger Pau Monné struct netfront_txq *txq; 206065671253SRoger Pau Monné int i; 206165671253SRoger Pau Monné 206296375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 206396375eacSRoger Pau Monné txq = &np->txq[i]; 206465671253SRoger Pau Monné rxq = &np->rxq[i]; 206596375eacSRoger Pau Monné xen_intr_signal(txq->xen_intr_handle); 206696375eacSRoger Pau Monné XN_TX_LOCK(txq); 206796375eacSRoger Pau Monné xn_txeof(txq); 206896375eacSRoger Pau Monné XN_TX_UNLOCK(txq); 20692568ee67SRoger Pau Monné XN_RX_LOCK(rxq); 207096375eacSRoger Pau Monné xn_alloc_rx_buffers(rxq); 20712568ee67SRoger Pau Monné XN_RX_UNLOCK(rxq); 207296375eacSRoger Pau Monné } 207389e0f4d2SKip Macy } 207489e0f4d2SKip Macy 207589e0f4d2SKip Macy static void 2076578e4bf7SJustin T. Gibbs xn_query_features(struct netfront_info *np) 2077578e4bf7SJustin T. Gibbs { 2078578e4bf7SJustin T. Gibbs int val; 2079578e4bf7SJustin T. Gibbs 2080578e4bf7SJustin T. Gibbs device_printf(np->xbdev, "backend features:"); 2081578e4bf7SJustin T. Gibbs 2082578e4bf7SJustin T. Gibbs if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 2083107cfbb7SRoger Pau Monné "feature-sg", NULL, "%d", &val) != 0) 2084578e4bf7SJustin T. Gibbs val = 0; 2085578e4bf7SJustin T. Gibbs 2086578e4bf7SJustin T. Gibbs np->maxfrags = 1; 2087578e4bf7SJustin T. Gibbs if (val) { 2088578e4bf7SJustin T. Gibbs np->maxfrags = MAX_TX_REQ_FRAGS; 2089578e4bf7SJustin T. Gibbs printf(" feature-sg"); 2090578e4bf7SJustin T. Gibbs } 2091578e4bf7SJustin T. Gibbs 2092578e4bf7SJustin T. Gibbs if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 2093107cfbb7SRoger Pau Monné "feature-gso-tcpv4", NULL, "%d", &val) != 0) 2094578e4bf7SJustin T. Gibbs val = 0; 2095578e4bf7SJustin T. Gibbs 209602f3b17fSJustin Hibbits if_setcapabilitiesbit(np->xn_ifp, 0, IFCAP_TSO4 | IFCAP_LRO); 2097578e4bf7SJustin T. Gibbs if (val) { 209802f3b17fSJustin Hibbits if_setcapabilitiesbit(np->xn_ifp, IFCAP_TSO4 | IFCAP_LRO, 0); 2099578e4bf7SJustin T. Gibbs printf(" feature-gso-tcp4"); 2100578e4bf7SJustin T. Gibbs } 2101578e4bf7SJustin T. Gibbs 2102c2d12e5eSRoger Pau Monné /* 2103c2d12e5eSRoger Pau Monné * HW CSUM offload is assumed to be available unless 2104c2d12e5eSRoger Pau Monné * feature-no-csum-offload is set in xenstore. 2105c2d12e5eSRoger Pau Monné */ 2106c2d12e5eSRoger Pau Monné if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 2107c2d12e5eSRoger Pau Monné "feature-no-csum-offload", NULL, "%d", &val) != 0) 2108c2d12e5eSRoger Pau Monné val = 0; 2109c2d12e5eSRoger Pau Monné 211002f3b17fSJustin Hibbits if_setcapabilitiesbit(np->xn_ifp, IFCAP_HWCSUM, 0); 2111c2d12e5eSRoger Pau Monné if (val) { 211202f3b17fSJustin Hibbits if_setcapabilitiesbit(np->xn_ifp, 0, IFCAP_HWCSUM); 2113c2d12e5eSRoger Pau Monné printf(" feature-no-csum-offload"); 2114c2d12e5eSRoger Pau Monné } 2115c2d12e5eSRoger Pau Monné 2116578e4bf7SJustin T. Gibbs printf("\n"); 2117578e4bf7SJustin T. Gibbs } 2118578e4bf7SJustin T. Gibbs 2119cf9c09e1SJustin T. Gibbs static int 2120578e4bf7SJustin T. Gibbs xn_configure_features(struct netfront_info *np) 2121cf9c09e1SJustin T. Gibbs { 21226a8e9695SRoger Pau Monné int err, cap_enabled; 212396375eacSRoger Pau Monné #if (defined(INET) || defined(INET6)) 212496375eacSRoger Pau Monné int i; 212596375eacSRoger Pau Monné #endif 212602f3b17fSJustin Hibbits if_t ifp; 2127cf9c09e1SJustin T. Gibbs 2128c2d12e5eSRoger Pau Monné ifp = np->xn_ifp; 2129cf9c09e1SJustin T. Gibbs err = 0; 21306a8e9695SRoger Pau Monné 213102f3b17fSJustin Hibbits if ((if_getcapenable(ifp) & if_getcapabilities(ifp)) == if_getcapenable(ifp)) { 21326a8e9695SRoger Pau Monné /* Current options are available, no need to do anything. */ 21336a8e9695SRoger Pau Monné return (0); 21346a8e9695SRoger Pau Monné } 21356a8e9695SRoger Pau Monné 21366a8e9695SRoger Pau Monné /* Try to preserve as many options as possible. */ 213702f3b17fSJustin Hibbits cap_enabled = if_getcapenable(ifp); 213802f3b17fSJustin Hibbits if_setcapenable(ifp, 0); 213902f3b17fSJustin Hibbits if_sethwassist(ifp, 0); 21406a8e9695SRoger Pau Monné 214108c9c2e0SRoger Pau Monné #if (defined(INET) || defined(INET6)) 2142c2d12e5eSRoger Pau Monné if ((cap_enabled & IFCAP_LRO) != 0) 214396375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) 214496375eacSRoger Pau Monné tcp_lro_free(&np->rxq[i].lro); 2145c2d12e5eSRoger Pau Monné if (xn_enable_lro && 214602f3b17fSJustin Hibbits (if_getcapabilities(ifp) & cap_enabled & IFCAP_LRO) != 0) { 214702f3b17fSJustin Hibbits if_setcapenablebit(ifp, IFCAP_LRO, 0); 214896375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 214996375eacSRoger Pau Monné err = tcp_lro_init(&np->rxq[i].lro); 215096375eacSRoger Pau Monné if (err != 0) { 2151c2d12e5eSRoger Pau Monné device_printf(np->xbdev, 2152c2d12e5eSRoger Pau Monné "LRO initialization failed\n"); 215302f3b17fSJustin Hibbits if_setcapenablebit(ifp, 0, IFCAP_LRO); 2154c2d12e5eSRoger Pau Monné break; 2155c2d12e5eSRoger Pau Monné } 2156c2d12e5eSRoger Pau Monné np->rxq[i].lro.ifp = ifp; 2157cf9c09e1SJustin T. Gibbs } 2158cf9c09e1SJustin T. Gibbs } 215902f3b17fSJustin Hibbits if ((if_getcapabilities(ifp) & cap_enabled & IFCAP_TSO4) != 0) { 216002f3b17fSJustin Hibbits if_setcapenablebit(ifp, IFCAP_TSO4, 0); 216102f3b17fSJustin Hibbits if_sethwassistbits(ifp, CSUM_TSO, 0); 2162578e4bf7SJustin T. Gibbs } 2163cf9c09e1SJustin T. Gibbs #endif 216402f3b17fSJustin Hibbits if ((if_getcapabilities(ifp) & cap_enabled & IFCAP_TXCSUM) != 0) { 216502f3b17fSJustin Hibbits if_setcapenablebit(ifp, IFCAP_TXCSUM, 0); 216602f3b17fSJustin Hibbits if_sethwassistbits(ifp, XN_CSUM_FEATURES, 0); 2167c2d12e5eSRoger Pau Monné } 216802f3b17fSJustin Hibbits if ((if_getcapabilities(ifp) & cap_enabled & IFCAP_RXCSUM) != 0) 216902f3b17fSJustin Hibbits if_setcapenablebit(ifp, IFCAP_RXCSUM, 0); 2170c2d12e5eSRoger Pau Monné 2171cf9c09e1SJustin T. Gibbs return (err); 2172cf9c09e1SJustin T. Gibbs } 2173cf9c09e1SJustin T. Gibbs 217496375eacSRoger Pau Monné static int 217596375eacSRoger Pau Monné xn_txq_mq_start_locked(struct netfront_txq *txq, struct mbuf *m) 217696375eacSRoger Pau Monné { 217796375eacSRoger Pau Monné struct netfront_info *np; 217802f3b17fSJustin Hibbits if_t ifp; 217996375eacSRoger Pau Monné struct buf_ring *br; 218096375eacSRoger Pau Monné int error, notify; 218196375eacSRoger Pau Monné 218296375eacSRoger Pau Monné np = txq->info; 218396375eacSRoger Pau Monné br = txq->br; 218496375eacSRoger Pau Monné ifp = np->xn_ifp; 218596375eacSRoger Pau Monné error = 0; 218696375eacSRoger Pau Monné 218796375eacSRoger Pau Monné XN_TX_LOCK_ASSERT(txq); 218896375eacSRoger Pau Monné 218902f3b17fSJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0 || 219096375eacSRoger Pau Monné !netfront_carrier_ok(np)) { 219196375eacSRoger Pau Monné if (m != NULL) 219296375eacSRoger Pau Monné error = drbr_enqueue(ifp, br, m); 219396375eacSRoger Pau Monné return (error); 219496375eacSRoger Pau Monné } 219596375eacSRoger Pau Monné 219696375eacSRoger Pau Monné if (m != NULL) { 219796375eacSRoger Pau Monné error = drbr_enqueue(ifp, br, m); 219896375eacSRoger Pau Monné if (error != 0) 219996375eacSRoger Pau Monné return (error); 220096375eacSRoger Pau Monné } 220196375eacSRoger Pau Monné 220296375eacSRoger Pau Monné while ((m = drbr_peek(ifp, br)) != NULL) { 220396375eacSRoger Pau Monné if (!xn_tx_slot_available(txq)) { 220496375eacSRoger Pau Monné drbr_putback(ifp, br, m); 220596375eacSRoger Pau Monné break; 220696375eacSRoger Pau Monné } 220796375eacSRoger Pau Monné 220896375eacSRoger Pau Monné error = xn_assemble_tx_request(txq, m); 220996375eacSRoger Pau Monné /* xn_assemble_tx_request always consumes the mbuf*/ 221096375eacSRoger Pau Monné if (error != 0) { 221196375eacSRoger Pau Monné drbr_advance(ifp, br); 221296375eacSRoger Pau Monné break; 221396375eacSRoger Pau Monné } 221496375eacSRoger Pau Monné 221596375eacSRoger Pau Monné RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&txq->ring, notify); 221696375eacSRoger Pau Monné if (notify) 221796375eacSRoger Pau Monné xen_intr_signal(txq->xen_intr_handle); 221896375eacSRoger Pau Monné 221996375eacSRoger Pau Monné drbr_advance(ifp, br); 222096375eacSRoger Pau Monné } 222196375eacSRoger Pau Monné 222296375eacSRoger Pau Monné if (RING_FULL(&txq->ring)) 222396375eacSRoger Pau Monné txq->full = true; 222496375eacSRoger Pau Monné 222596375eacSRoger Pau Monné return (0); 222696375eacSRoger Pau Monné } 222796375eacSRoger Pau Monné 222896375eacSRoger Pau Monné static int 222902f3b17fSJustin Hibbits xn_txq_mq_start(if_t ifp, struct mbuf *m) 223096375eacSRoger Pau Monné { 223196375eacSRoger Pau Monné struct netfront_info *np; 223296375eacSRoger Pau Monné struct netfront_txq *txq; 223396375eacSRoger Pau Monné int i, npairs, error; 223496375eacSRoger Pau Monné 223502f3b17fSJustin Hibbits np = if_getsoftc(ifp); 223696375eacSRoger Pau Monné npairs = np->num_queues; 223796375eacSRoger Pau Monné 2238339690b5SRoger Pau Monné if (!netfront_carrier_ok(np)) 2239339690b5SRoger Pau Monné return (ENOBUFS); 2240339690b5SRoger Pau Monné 2241c21b47d8SRoger Pau Monné KASSERT(npairs != 0, ("called with 0 available queues")); 2242c21b47d8SRoger Pau Monné 224396375eacSRoger Pau Monné /* check if flowid is set */ 224496375eacSRoger Pau Monné if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) 224596375eacSRoger Pau Monné i = m->m_pkthdr.flowid % npairs; 224696375eacSRoger Pau Monné else 224796375eacSRoger Pau Monné i = curcpu % npairs; 224896375eacSRoger Pau Monné 224996375eacSRoger Pau Monné txq = &np->txq[i]; 225096375eacSRoger Pau Monné 225196375eacSRoger Pau Monné if (XN_TX_TRYLOCK(txq) != 0) { 225296375eacSRoger Pau Monné error = xn_txq_mq_start_locked(txq, m); 225396375eacSRoger Pau Monné XN_TX_UNLOCK(txq); 225496375eacSRoger Pau Monné } else { 225596375eacSRoger Pau Monné error = drbr_enqueue(ifp, txq->br, m); 225696375eacSRoger Pau Monné taskqueue_enqueue(txq->tq, &txq->defrtask); 225796375eacSRoger Pau Monné } 225896375eacSRoger Pau Monné 225996375eacSRoger Pau Monné return (error); 226096375eacSRoger Pau Monné } 226196375eacSRoger Pau Monné 226296375eacSRoger Pau Monné static void 226302f3b17fSJustin Hibbits xn_qflush(if_t ifp) 226496375eacSRoger Pau Monné { 226596375eacSRoger Pau Monné struct netfront_info *np; 226696375eacSRoger Pau Monné struct netfront_txq *txq; 226796375eacSRoger Pau Monné struct mbuf *m; 226896375eacSRoger Pau Monné int i; 226996375eacSRoger Pau Monné 227002f3b17fSJustin Hibbits np = if_getsoftc(ifp); 227196375eacSRoger Pau Monné 227296375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 227396375eacSRoger Pau Monné txq = &np->txq[i]; 227496375eacSRoger Pau Monné 227596375eacSRoger Pau Monné XN_TX_LOCK(txq); 227696375eacSRoger Pau Monné while ((m = buf_ring_dequeue_sc(txq->br)) != NULL) 227796375eacSRoger Pau Monné m_freem(m); 227896375eacSRoger Pau Monné XN_TX_UNLOCK(txq); 227996375eacSRoger Pau Monné } 228096375eacSRoger Pau Monné 228196375eacSRoger Pau Monné if_qflush(ifp); 228296375eacSRoger Pau Monné } 228396375eacSRoger Pau Monné 228476acc41fSJustin T. Gibbs /** 228576acc41fSJustin T. Gibbs * Create a network device. 228676acc41fSJustin T. Gibbs * @param dev Newbus device representing this virtual NIC. 228789e0f4d2SKip Macy */ 228823dc5621SKip Macy int 228923dc5621SKip Macy create_netdev(device_t dev) 229089e0f4d2SKip Macy { 229189e0f4d2SKip Macy struct netfront_info *np; 2292da4b0d6eSDoug Rabson int err, cap_enabled; 229302f3b17fSJustin Hibbits if_t ifp; 229489e0f4d2SKip Macy 229523dc5621SKip Macy np = device_get_softc(dev); 229689e0f4d2SKip Macy 229789e0f4d2SKip Macy np->xbdev = dev; 229889e0f4d2SKip Macy 2299177e3f13SRoger Pau Monné mtx_init(&np->sc_lock, "xnsc", "netfront softc lock", MTX_DEF); 23000e509842SJustin T. Gibbs 23010e509842SJustin T. Gibbs ifmedia_init(&np->sc_media, 0, xn_ifmedia_upd, xn_ifmedia_sts); 23020e509842SJustin T. Gibbs ifmedia_add(&np->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); 23030e509842SJustin T. Gibbs ifmedia_set(&np->sc_media, IFM_ETHER|IFM_MANUAL); 23040e509842SJustin T. Gibbs 230589e0f4d2SKip Macy err = xen_net_read_mac(dev, np->mac); 230696375eacSRoger Pau Monné if (err != 0) 23071a2928b7SRoger Pau Monné goto error; 230889e0f4d2SKip Macy 230989e0f4d2SKip Macy /* Set up ifnet structure */ 231023dc5621SKip Macy ifp = np->xn_ifp = if_alloc(IFT_ETHER); 231102f3b17fSJustin Hibbits if_setsoftc(ifp, np); 231223dc5621SKip Macy if_initname(ifp, "xn", device_get_unit(dev)); 231302f3b17fSJustin Hibbits if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 231402f3b17fSJustin Hibbits if_setioctlfn(ifp, xn_ioctl); 231596375eacSRoger Pau Monné 231602f3b17fSJustin Hibbits if_settransmitfn(ifp, xn_txq_mq_start); 231702f3b17fSJustin Hibbits if_setqflushfn(ifp, xn_qflush); 231896375eacSRoger Pau Monné 231902f3b17fSJustin Hibbits if_setinitfn(ifp, xn_ifinit); 232089e0f4d2SKip Macy 232102f3b17fSJustin Hibbits if_sethwassist(ifp, XN_CSUM_FEATURES); 2322c2d12e5eSRoger Pau Monné /* Enable all supported features at device creation. */ 232302f3b17fSJustin Hibbits if_setcapabilities(ifp, IFCAP_HWCSUM|IFCAP_TSO4|IFCAP_LRO); 2324da4b0d6eSDoug Rabson cap_enabled = if_getcapabilities(ifp); 2325da4b0d6eSDoug Rabson if (!xn_enable_lro) { 2326da4b0d6eSDoug Rabson cap_enabled &= ~IFCAP_LRO; 2327da4b0d6eSDoug Rabson } 2328da4b0d6eSDoug Rabson if_setcapenable(ifp, cap_enabled); 232902f3b17fSJustin Hibbits 233002f3b17fSJustin Hibbits if_sethwtsomax(ifp, 65536 - (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN)); 233102f3b17fSJustin Hibbits if_sethwtsomaxsegcount(ifp, MAX_TX_REQ_FRAGS); 233202f3b17fSJustin Hibbits if_sethwtsomaxsegsize(ifp, PAGE_SIZE); 233389e0f4d2SKip Macy 233489e0f4d2SKip Macy ether_ifattach(ifp, np->mac); 233589e0f4d2SKip Macy netfront_carrier_off(np); 233689e0f4d2SKip Macy 2337dabb3db7SRoger Pau Monné err = bus_dma_tag_create( 2338dabb3db7SRoger Pau Monné bus_get_dma_tag(dev), /* parent */ 2339dabb3db7SRoger Pau Monné 1, PAGE_SIZE, /* algnmnt, boundary */ 2340dabb3db7SRoger Pau Monné BUS_SPACE_MAXADDR, /* lowaddr */ 2341dabb3db7SRoger Pau Monné BUS_SPACE_MAXADDR, /* highaddr */ 2342dabb3db7SRoger Pau Monné NULL, NULL, /* filter, filterarg */ 2343dabb3db7SRoger Pau Monné PAGE_SIZE * MAX_TX_REQ_FRAGS, /* max request size */ 2344dabb3db7SRoger Pau Monné MAX_TX_REQ_FRAGS, /* max segments */ 2345dabb3db7SRoger Pau Monné PAGE_SIZE, /* maxsegsize */ 2346dabb3db7SRoger Pau Monné BUS_DMA_ALLOCNOW, /* flags */ 2347dabb3db7SRoger Pau Monné NULL, NULL, /* lockfunc, lockarg */ 2348dabb3db7SRoger Pau Monné &np->dma_tag); 2349dabb3db7SRoger Pau Monné 2350dabb3db7SRoger Pau Monné return (err); 235189e0f4d2SKip Macy 23521a2928b7SRoger Pau Monné error: 23531a2928b7SRoger Pau Monné KASSERT(err != 0, ("Error path with no error code specified")); 2354ffa06904SJustin T. Gibbs return (err); 235589e0f4d2SKip Macy } 235689e0f4d2SKip Macy 23570e509842SJustin T. Gibbs static int 23580e509842SJustin T. Gibbs netfront_detach(device_t dev) 235989e0f4d2SKip Macy { 236023dc5621SKip Macy struct netfront_info *info = device_get_softc(dev); 236189e0f4d2SKip Macy 236223dc5621SKip Macy DPRINTK("%s\n", xenbus_get_node(dev)); 236389e0f4d2SKip Macy 236489e0f4d2SKip Macy netif_free(info); 236589e0f4d2SKip Macy 236689e0f4d2SKip Macy return 0; 236789e0f4d2SKip Macy } 236889e0f4d2SKip Macy 23690e509842SJustin T. Gibbs static void 237096375eacSRoger Pau Monné netif_free(struct netfront_info *np) 237189e0f4d2SKip Macy { 237296375eacSRoger Pau Monné 237396375eacSRoger Pau Monné XN_LOCK(np); 237496375eacSRoger Pau Monné xn_stop(np); 237596375eacSRoger Pau Monné XN_UNLOCK(np); 237696375eacSRoger Pau Monné netif_disconnect_backend(np); 2377c21b47d8SRoger Pau Monné ether_ifdetach(np->xn_ifp); 237896375eacSRoger Pau Monné free(np->rxq, M_DEVBUF); 237996375eacSRoger Pau Monné free(np->txq, M_DEVBUF); 238096375eacSRoger Pau Monné if_free(np->xn_ifp); 238196375eacSRoger Pau Monné np->xn_ifp = NULL; 238296375eacSRoger Pau Monné ifmedia_removeall(&np->sc_media); 2383dabb3db7SRoger Pau Monné bus_dma_tag_destroy(np->dma_tag); 238489e0f4d2SKip Macy } 238589e0f4d2SKip Macy 23860e509842SJustin T. Gibbs static void 238796375eacSRoger Pau Monné netif_disconnect_backend(struct netfront_info *np) 238889e0f4d2SKip Macy { 238996375eacSRoger Pau Monné u_int i; 23903a6d1fcfSKip Macy 239196375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 239296375eacSRoger Pau Monné XN_RX_LOCK(&np->rxq[i]); 239396375eacSRoger Pau Monné XN_TX_LOCK(&np->txq[i]); 239496375eacSRoger Pau Monné } 239596375eacSRoger Pau Monné netfront_carrier_off(np); 239696375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 239796375eacSRoger Pau Monné XN_RX_UNLOCK(&np->rxq[i]); 239896375eacSRoger Pau Monné XN_TX_UNLOCK(&np->txq[i]); 239989e0f4d2SKip Macy } 240089e0f4d2SKip Macy 240196375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 240296375eacSRoger Pau Monné disconnect_rxq(&np->rxq[i]); 240396375eacSRoger Pau Monné disconnect_txq(&np->txq[i]); 2404cf9c09e1SJustin T. Gibbs } 240589e0f4d2SKip Macy } 240689e0f4d2SKip Macy 24070e509842SJustin T. Gibbs static int 240802f3b17fSJustin Hibbits xn_ifmedia_upd(if_t ifp) 24090e509842SJustin T. Gibbs { 241096375eacSRoger Pau Monné 24110e509842SJustin T. Gibbs return (0); 24120e509842SJustin T. Gibbs } 24130e509842SJustin T. Gibbs 24140e509842SJustin T. Gibbs static void 241502f3b17fSJustin Hibbits xn_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr) 24160e509842SJustin T. Gibbs { 241796375eacSRoger Pau Monné 24180e509842SJustin T. Gibbs ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE; 24190e509842SJustin T. Gibbs ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; 24200e509842SJustin T. Gibbs } 24210e509842SJustin T. Gibbs 242289e0f4d2SKip Macy /* ** Driver registration ** */ 242323dc5621SKip Macy static device_method_t netfront_methods[] = { 242423dc5621SKip Macy /* Device interface */ 242523dc5621SKip Macy DEVMETHOD(device_probe, netfront_probe), 242623dc5621SKip Macy DEVMETHOD(device_attach, netfront_attach), 242723dc5621SKip Macy DEVMETHOD(device_detach, netfront_detach), 242823dc5621SKip Macy DEVMETHOD(device_shutdown, bus_generic_shutdown), 2429cf9c09e1SJustin T. Gibbs DEVMETHOD(device_suspend, netfront_suspend), 243023dc5621SKip Macy DEVMETHOD(device_resume, netfront_resume), 243189e0f4d2SKip Macy 243223dc5621SKip Macy /* Xenbus interface */ 2433ff662b5cSJustin T. Gibbs DEVMETHOD(xenbus_otherend_changed, netfront_backend_changed), 243489e0f4d2SKip Macy 24356f9767acSMarius Strobl DEVMETHOD_END 243689e0f4d2SKip Macy }; 243789e0f4d2SKip Macy 243823dc5621SKip Macy static driver_t netfront_driver = { 243923dc5621SKip Macy "xn", 244023dc5621SKip Macy netfront_methods, 244123dc5621SKip Macy sizeof(struct netfront_info), 244289e0f4d2SKip Macy }; 244389e0f4d2SKip Macy 2444f929eb1eSJohn Baldwin DRIVER_MODULE(xe, xenbusb_front, netfront_driver, NULL, NULL); 2445