18e0ad55aSJoel Dahl /*- 289e0f4d2SKip Macy * Copyright (c) 2004-2006 Kip Macy 389e0f4d2SKip Macy * All rights reserved. 489e0f4d2SKip Macy * 58e0ad55aSJoel Dahl * Redistribution and use in source and binary forms, with or without 68e0ad55aSJoel Dahl * modification, are permitted provided that the following conditions 78e0ad55aSJoel Dahl * are met: 88e0ad55aSJoel Dahl * 1. Redistributions of source code must retain the above copyright 98e0ad55aSJoel Dahl * notice, this list of conditions and the following disclaimer. 108e0ad55aSJoel Dahl * 2. Redistributions in binary form must reproduce the above copyright 118e0ad55aSJoel Dahl * notice, this list of conditions and the following disclaimer in the 128e0ad55aSJoel Dahl * documentation and/or other materials provided with the distribution. 1389e0f4d2SKip Macy * 148e0ad55aSJoel Dahl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 158e0ad55aSJoel Dahl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 168e0ad55aSJoel Dahl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 178e0ad55aSJoel Dahl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 188e0ad55aSJoel Dahl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 198e0ad55aSJoel Dahl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 208e0ad55aSJoel Dahl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 218e0ad55aSJoel Dahl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 228e0ad55aSJoel Dahl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 238e0ad55aSJoel Dahl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 248e0ad55aSJoel Dahl * SUCH DAMAGE. 2589e0f4d2SKip Macy */ 2689e0f4d2SKip Macy 2789e0f4d2SKip Macy #include <sys/cdefs.h> 2889e0f4d2SKip Macy __FBSDID("$FreeBSD$"); 2989e0f4d2SKip Macy 30a0ae8f04SBjoern A. Zeeb #include "opt_inet.h" 31f909bbb4SGleb Smirnoff #include "opt_inet6.h" 32a0ae8f04SBjoern A. Zeeb 3389e0f4d2SKip Macy #include <sys/param.h> 3489e0f4d2SKip Macy #include <sys/systm.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> 4389e0f4d2SKip Macy #include <sys/queue.h> 448cb07992SAdrian Chadd #include <sys/lock.h> 4589e0f4d2SKip Macy #include <sys/sx.h> 4689e0f4d2SKip Macy 4789e0f4d2SKip Macy #include <net/if.h> 4877374386SGleb Smirnoff #include <net/if_var.h> 4989e0f4d2SKip Macy #include <net/if_arp.h> 5089e0f4d2SKip Macy #include <net/ethernet.h> 5189e0f4d2SKip Macy #include <net/if_dl.h> 5289e0f4d2SKip Macy #include <net/if_media.h> 5389e0f4d2SKip Macy 5489e0f4d2SKip Macy #include <net/bpf.h> 5589e0f4d2SKip Macy 5689e0f4d2SKip Macy #include <net/if_types.h> 5789e0f4d2SKip Macy 5889e0f4d2SKip Macy #include <netinet/in_systm.h> 5989e0f4d2SKip Macy #include <netinet/in.h> 6089e0f4d2SKip Macy #include <netinet/ip.h> 6189e0f4d2SKip Macy #include <netinet/if_ether.h> 6212678024SDoug Rabson #if __FreeBSD_version >= 700000 6312678024SDoug Rabson #include <netinet/tcp.h> 6412678024SDoug Rabson #include <netinet/tcp_lro.h> 6512678024SDoug Rabson #endif 6689e0f4d2SKip Macy 6789e0f4d2SKip Macy #include <vm/vm.h> 6889e0f4d2SKip Macy #include <vm/pmap.h> 6989e0f4d2SKip Macy 7089e0f4d2SKip Macy #include <machine/clock.h> /* for DELAY */ 7189e0f4d2SKip Macy #include <machine/bus.h> 7289e0f4d2SKip Macy #include <machine/resource.h> 7389e0f4d2SKip Macy #include <machine/frame.h> 74980c7178SKip Macy #include <machine/vmparam.h> 7589e0f4d2SKip Macy 7689e0f4d2SKip Macy #include <sys/bus.h> 7789e0f4d2SKip Macy #include <sys/rman.h> 7889e0f4d2SKip Macy 7989e0f4d2SKip Macy #include <machine/intr_machdep.h> 8089e0f4d2SKip Macy 8176acc41fSJustin T. Gibbs #include <xen/xen-os.h> 823a6d1fcfSKip Macy #include <xen/hypervisor.h> 833a6d1fcfSKip Macy #include <xen/xen_intr.h> 8489e0f4d2SKip Macy #include <xen/gnttab.h> 8589e0f4d2SKip Macy #include <xen/interface/memory.h> 8689e0f4d2SKip Macy #include <xen/interface/io/netif.h> 8723dc5621SKip Macy #include <xen/xenbus/xenbusvar.h> 8889e0f4d2SKip Macy 8923dc5621SKip Macy #include "xenbus_if.h" 9089e0f4d2SKip Macy 91578e4bf7SJustin T. Gibbs /* Features supported by all backends. TSO and LRO can be negotiated */ 92578e4bf7SJustin T. Gibbs #define XN_CSUM_FEATURES (CSUM_TCP | CSUM_UDP) 9312678024SDoug Rabson 9489e0f4d2SKip Macy #define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE) 9589e0f4d2SKip Macy #define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE) 9689e0f4d2SKip Macy 9712678024SDoug Rabson #if __FreeBSD_version >= 700000 9812678024SDoug Rabson /* 9912678024SDoug Rabson * Should the driver do LRO on the RX end 10012678024SDoug Rabson * this can be toggled on the fly, but the 10112678024SDoug Rabson * interface must be reset (down/up) for it 10212678024SDoug Rabson * to take effect. 10312678024SDoug Rabson */ 10412678024SDoug Rabson static int xn_enable_lro = 1; 10512678024SDoug Rabson TUNABLE_INT("hw.xn.enable_lro", &xn_enable_lro); 10612678024SDoug Rabson #else 10712678024SDoug Rabson 10812678024SDoug Rabson #define IFCAP_TSO4 0 10912678024SDoug Rabson #define CSUM_TSO 0 11012678024SDoug Rabson 11112678024SDoug Rabson #endif 11212678024SDoug Rabson 113931eeffaSKenneth D. Merry /** 114931eeffaSKenneth D. Merry * \brief The maximum allowed data fragments in a single transmit 115931eeffaSKenneth D. Merry * request. 116931eeffaSKenneth D. Merry * 117931eeffaSKenneth D. Merry * This limit is imposed by the backend driver. We assume here that 118931eeffaSKenneth D. Merry * we are dealing with a Linux driver domain and have set our limit 119931eeffaSKenneth D. Merry * to mirror the Linux MAX_SKB_FRAGS constant. 120931eeffaSKenneth D. Merry */ 121931eeffaSKenneth D. Merry #define MAX_TX_REQ_FRAGS (65536 / PAGE_SIZE + 2) 122931eeffaSKenneth D. Merry 12389e0f4d2SKip Macy #define RX_COPY_THRESHOLD 256 12489e0f4d2SKip Macy 12589e0f4d2SKip Macy #define net_ratelimit() 0 12689e0f4d2SKip Macy 12789e0f4d2SKip Macy struct netfront_info; 12889e0f4d2SKip Macy struct netfront_rx_info; 12989e0f4d2SKip Macy 13089e0f4d2SKip Macy static void xn_txeof(struct netfront_info *); 13189e0f4d2SKip Macy static void xn_rxeof(struct netfront_info *); 13289e0f4d2SKip Macy static void network_alloc_rx_buffers(struct netfront_info *); 13389e0f4d2SKip Macy 13489e0f4d2SKip Macy static void xn_tick_locked(struct netfront_info *); 13589e0f4d2SKip Macy static void xn_tick(void *); 13689e0f4d2SKip Macy 13789e0f4d2SKip Macy static void xn_intr(void *); 138931eeffaSKenneth D. Merry static inline int xn_count_frags(struct mbuf *m); 139931eeffaSKenneth D. Merry static int xn_assemble_tx_request(struct netfront_info *sc, 140931eeffaSKenneth D. Merry struct mbuf *m_head); 14189e0f4d2SKip Macy static void xn_start_locked(struct ifnet *); 14289e0f4d2SKip Macy static void xn_start(struct ifnet *); 14389e0f4d2SKip Macy static int xn_ioctl(struct ifnet *, u_long, caddr_t); 14489e0f4d2SKip Macy static void xn_ifinit_locked(struct netfront_info *); 14589e0f4d2SKip Macy static void xn_ifinit(void *); 14689e0f4d2SKip Macy static void xn_stop(struct netfront_info *); 147578e4bf7SJustin T. Gibbs static void xn_query_features(struct netfront_info *np); 148578e4bf7SJustin T. Gibbs static int xn_configure_features(struct netfront_info *np); 14989e0f4d2SKip Macy #ifdef notyet 15089e0f4d2SKip Macy static void xn_watchdog(struct ifnet *); 15189e0f4d2SKip Macy #endif 15289e0f4d2SKip Macy 15389e0f4d2SKip Macy #ifdef notyet 15423dc5621SKip Macy static void netfront_closing(device_t dev); 15589e0f4d2SKip Macy #endif 15689e0f4d2SKip Macy static void netif_free(struct netfront_info *info); 15723dc5621SKip Macy static int netfront_detach(device_t dev); 15889e0f4d2SKip Macy 15923dc5621SKip Macy static int talk_to_backend(device_t dev, struct netfront_info *info); 16023dc5621SKip Macy static int create_netdev(device_t dev); 16189e0f4d2SKip Macy static void netif_disconnect_backend(struct netfront_info *info); 16223dc5621SKip Macy static int setup_device(device_t dev, struct netfront_info *info); 163cf9c09e1SJustin T. Gibbs static void free_ring(int *ref, void *ring_ptr_ref); 16489e0f4d2SKip Macy 1650e509842SJustin T. Gibbs static int xn_ifmedia_upd(struct ifnet *ifp); 1660e509842SJustin T. Gibbs static void xn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr); 1670e509842SJustin T. Gibbs 16889e0f4d2SKip Macy /* Xenolinux helper functions */ 16923dc5621SKip Macy int network_connect(struct netfront_info *); 17089e0f4d2SKip Macy 17189e0f4d2SKip Macy static void xn_free_rx_ring(struct netfront_info *); 17289e0f4d2SKip Macy 17389e0f4d2SKip Macy static void xn_free_tx_ring(struct netfront_info *); 17489e0f4d2SKip Macy 17589e0f4d2SKip Macy static int xennet_get_responses(struct netfront_info *np, 176931eeffaSKenneth D. Merry struct netfront_rx_info *rinfo, RING_IDX rp, RING_IDX *cons, 177*d0f3a8b9SRoger Pau Monné struct mbuf **list); 17889e0f4d2SKip Macy 1793c790178SJohn Baldwin #define virt_to_mfn(x) (vtophys(x) >> PAGE_SHIFT) 18089e0f4d2SKip Macy 18189e0f4d2SKip Macy #define INVALID_P2M_ENTRY (~0UL) 18289e0f4d2SKip Macy 18389e0f4d2SKip Macy /* 18489e0f4d2SKip Macy * Mbuf pointers. We need these to keep track of the virtual addresses 18589e0f4d2SKip Macy * of our mbuf chains since we can only convert from virtual to physical, 18689e0f4d2SKip Macy * not the other way around. The size must track the free index arrays. 18789e0f4d2SKip Macy */ 18889e0f4d2SKip Macy struct xn_chain_data { 18989e0f4d2SKip Macy struct mbuf *xn_tx_chain[NET_TX_RING_SIZE+1]; 190a4ec37f5SAdrian Chadd int xn_tx_chain_cnt; 19189e0f4d2SKip Macy struct mbuf *xn_rx_chain[NET_RX_RING_SIZE+1]; 19289e0f4d2SKip Macy }; 19389e0f4d2SKip Macy 19489e0f4d2SKip Macy struct net_device_stats 19589e0f4d2SKip Macy { 19689e0f4d2SKip Macy u_long rx_packets; /* total packets received */ 19789e0f4d2SKip Macy u_long tx_packets; /* total packets transmitted */ 19889e0f4d2SKip Macy u_long rx_bytes; /* total bytes received */ 19989e0f4d2SKip Macy u_long tx_bytes; /* total bytes transmitted */ 20089e0f4d2SKip Macy u_long rx_errors; /* bad packets received */ 20189e0f4d2SKip Macy u_long tx_errors; /* packet transmit problems */ 20289e0f4d2SKip Macy u_long rx_dropped; /* no space in linux buffers */ 20389e0f4d2SKip Macy u_long tx_dropped; /* no space available in linux */ 20489e0f4d2SKip Macy u_long multicast; /* multicast packets received */ 20589e0f4d2SKip Macy u_long collisions; 20689e0f4d2SKip Macy 20789e0f4d2SKip Macy /* detailed rx_errors: */ 20889e0f4d2SKip Macy u_long rx_length_errors; 20989e0f4d2SKip Macy u_long rx_over_errors; /* receiver ring buff overflow */ 21089e0f4d2SKip Macy u_long rx_crc_errors; /* recved pkt with crc error */ 21189e0f4d2SKip Macy u_long rx_frame_errors; /* recv'd frame alignment error */ 21289e0f4d2SKip Macy u_long rx_fifo_errors; /* recv'r fifo overrun */ 21389e0f4d2SKip Macy u_long rx_missed_errors; /* receiver missed packet */ 21489e0f4d2SKip Macy 21589e0f4d2SKip Macy /* detailed tx_errors */ 21689e0f4d2SKip Macy u_long tx_aborted_errors; 21789e0f4d2SKip Macy u_long tx_carrier_errors; 21889e0f4d2SKip Macy u_long tx_fifo_errors; 21989e0f4d2SKip Macy u_long tx_heartbeat_errors; 22089e0f4d2SKip Macy u_long tx_window_errors; 22189e0f4d2SKip Macy 22289e0f4d2SKip Macy /* for cslip etc */ 22389e0f4d2SKip Macy u_long rx_compressed; 22489e0f4d2SKip Macy u_long tx_compressed; 22589e0f4d2SKip Macy }; 22689e0f4d2SKip Macy 22789e0f4d2SKip Macy struct netfront_info { 22889e0f4d2SKip Macy struct ifnet *xn_ifp; 22912678024SDoug Rabson #if __FreeBSD_version >= 700000 23012678024SDoug Rabson struct lro_ctrl xn_lro; 23112678024SDoug Rabson #endif 23289e0f4d2SKip Macy 23389e0f4d2SKip Macy struct net_device_stats stats; 23489e0f4d2SKip Macy u_int tx_full; 23589e0f4d2SKip Macy 23689e0f4d2SKip Macy netif_tx_front_ring_t tx; 23789e0f4d2SKip Macy netif_rx_front_ring_t rx; 23889e0f4d2SKip Macy 23989e0f4d2SKip Macy struct mtx tx_lock; 24089e0f4d2SKip Macy struct mtx rx_lock; 241227ca257SKip Macy struct mtx sc_lock; 24289e0f4d2SKip Macy 24376acc41fSJustin T. Gibbs xen_intr_handle_t xen_intr_handle; 24489e0f4d2SKip Macy u_int carrier; 245578e4bf7SJustin T. Gibbs u_int maxfrags; 24689e0f4d2SKip Macy 24789e0f4d2SKip Macy /* Receive-ring batched refills. */ 24889e0f4d2SKip Macy #define RX_MIN_TARGET 32 24989e0f4d2SKip Macy #define RX_MAX_TARGET NET_RX_RING_SIZE 2500e509842SJustin T. Gibbs int rx_min_target; 2510e509842SJustin T. Gibbs int rx_max_target; 2520e509842SJustin T. Gibbs int rx_target; 25389e0f4d2SKip Macy 25489e0f4d2SKip Macy grant_ref_t gref_tx_head; 25589e0f4d2SKip Macy grant_ref_t grant_tx_ref[NET_TX_RING_SIZE + 1]; 25689e0f4d2SKip Macy grant_ref_t gref_rx_head; 25789e0f4d2SKip Macy grant_ref_t grant_rx_ref[NET_TX_RING_SIZE + 1]; 25889e0f4d2SKip Macy 25923dc5621SKip Macy device_t xbdev; 26089e0f4d2SKip Macy int tx_ring_ref; 26189e0f4d2SKip Macy int rx_ring_ref; 26289e0f4d2SKip Macy uint8_t mac[ETHER_ADDR_LEN]; 26389e0f4d2SKip Macy struct xn_chain_data xn_cdata; /* mbufs */ 264c578b6acSGleb Smirnoff struct mbufq xn_rx_batch; /* batch queue */ 26589e0f4d2SKip Macy 26689e0f4d2SKip Macy int xn_if_flags; 26789e0f4d2SKip Macy struct callout xn_stat_ch; 26889e0f4d2SKip Macy 26989e0f4d2SKip Macy u_long rx_pfn_array[NET_RX_RING_SIZE]; 2700e509842SJustin T. Gibbs struct ifmedia sc_media; 2716a8e9695SRoger Pau Monné 2726a8e9695SRoger Pau Monné bool xn_resume; 27389e0f4d2SKip Macy }; 27489e0f4d2SKip Macy 27589e0f4d2SKip Macy #define rx_mbufs xn_cdata.xn_rx_chain 27689e0f4d2SKip Macy #define tx_mbufs xn_cdata.xn_tx_chain 27789e0f4d2SKip Macy 27889e0f4d2SKip Macy #define XN_LOCK_INIT(_sc, _name) \ 27989e0f4d2SKip Macy mtx_init(&(_sc)->tx_lock, #_name"_tx", "network transmit lock", MTX_DEF); \ 28089e0f4d2SKip Macy mtx_init(&(_sc)->rx_lock, #_name"_rx", "network receive lock", MTX_DEF); \ 281227ca257SKip Macy mtx_init(&(_sc)->sc_lock, #_name"_sc", "netfront softc lock", MTX_DEF) 28289e0f4d2SKip Macy 28389e0f4d2SKip Macy #define XN_RX_LOCK(_sc) mtx_lock(&(_sc)->rx_lock) 28489e0f4d2SKip Macy #define XN_RX_UNLOCK(_sc) mtx_unlock(&(_sc)->rx_lock) 28589e0f4d2SKip Macy 28689e0f4d2SKip Macy #define XN_TX_LOCK(_sc) mtx_lock(&(_sc)->tx_lock) 28789e0f4d2SKip Macy #define XN_TX_UNLOCK(_sc) mtx_unlock(&(_sc)->tx_lock) 28889e0f4d2SKip Macy 289227ca257SKip Macy #define XN_LOCK(_sc) mtx_lock(&(_sc)->sc_lock); 290227ca257SKip Macy #define XN_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_lock); 29189e0f4d2SKip Macy 292227ca257SKip Macy #define XN_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->sc_lock, MA_OWNED); 29389e0f4d2SKip Macy #define XN_RX_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->rx_lock, MA_OWNED); 29489e0f4d2SKip Macy #define XN_TX_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->tx_lock, MA_OWNED); 29589e0f4d2SKip Macy #define XN_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->rx_lock); \ 29689e0f4d2SKip Macy mtx_destroy(&(_sc)->tx_lock); \ 297227ca257SKip Macy mtx_destroy(&(_sc)->sc_lock); 29889e0f4d2SKip Macy 29989e0f4d2SKip Macy struct netfront_rx_info { 30089e0f4d2SKip Macy struct netif_rx_response rx; 30189e0f4d2SKip Macy struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1]; 30289e0f4d2SKip Macy }; 30389e0f4d2SKip Macy 30489e0f4d2SKip Macy #define netfront_carrier_on(netif) ((netif)->carrier = 1) 30589e0f4d2SKip Macy #define netfront_carrier_off(netif) ((netif)->carrier = 0) 30689e0f4d2SKip Macy #define netfront_carrier_ok(netif) ((netif)->carrier) 30789e0f4d2SKip Macy 30889e0f4d2SKip Macy /* Access macros for acquiring freeing slots in xn_free_{tx,rx}_idxs[]. */ 30989e0f4d2SKip Macy 31089e0f4d2SKip Macy static inline void 311931eeffaSKenneth D. Merry add_id_to_freelist(struct mbuf **list, uintptr_t id) 31289e0f4d2SKip Macy { 313931eeffaSKenneth D. Merry KASSERT(id != 0, 314931eeffaSKenneth D. Merry ("%s: the head item (0) must always be free.", __func__)); 31589e0f4d2SKip Macy list[id] = list[0]; 316931eeffaSKenneth D. Merry list[0] = (struct mbuf *)id; 31789e0f4d2SKip Macy } 31889e0f4d2SKip Macy 31989e0f4d2SKip Macy static inline unsigned short 32089e0f4d2SKip Macy get_id_from_freelist(struct mbuf **list) 32189e0f4d2SKip Macy { 322931eeffaSKenneth D. Merry uintptr_t id; 323931eeffaSKenneth D. Merry 324931eeffaSKenneth D. Merry id = (uintptr_t)list[0]; 325931eeffaSKenneth D. Merry KASSERT(id != 0, 326931eeffaSKenneth D. Merry ("%s: the head item (0) must always remain free.", __func__)); 32789e0f4d2SKip Macy list[0] = list[id]; 32889e0f4d2SKip Macy return (id); 32989e0f4d2SKip Macy } 33089e0f4d2SKip Macy 33189e0f4d2SKip Macy static inline int 33289e0f4d2SKip Macy xennet_rxidx(RING_IDX idx) 33389e0f4d2SKip Macy { 33489e0f4d2SKip Macy return idx & (NET_RX_RING_SIZE - 1); 33589e0f4d2SKip Macy } 33689e0f4d2SKip Macy 33789e0f4d2SKip Macy static inline struct mbuf * 338931eeffaSKenneth D. Merry xennet_get_rx_mbuf(struct netfront_info *np, RING_IDX ri) 33989e0f4d2SKip Macy { 34089e0f4d2SKip Macy int i = xennet_rxidx(ri); 34189e0f4d2SKip Macy struct mbuf *m; 34289e0f4d2SKip Macy 34389e0f4d2SKip Macy m = np->rx_mbufs[i]; 34489e0f4d2SKip Macy np->rx_mbufs[i] = NULL; 34589e0f4d2SKip Macy return (m); 34689e0f4d2SKip Macy } 34789e0f4d2SKip Macy 34889e0f4d2SKip Macy static inline grant_ref_t 34989e0f4d2SKip Macy xennet_get_rx_ref(struct netfront_info *np, RING_IDX ri) 35089e0f4d2SKip Macy { 35189e0f4d2SKip Macy int i = xennet_rxidx(ri); 35289e0f4d2SKip Macy grant_ref_t ref = np->grant_rx_ref[i]; 353ff662b5cSJustin T. Gibbs KASSERT(ref != GRANT_REF_INVALID, ("Invalid grant reference!\n")); 354ff662b5cSJustin T. Gibbs np->grant_rx_ref[i] = GRANT_REF_INVALID; 35589e0f4d2SKip Macy return ref; 35689e0f4d2SKip Macy } 35789e0f4d2SKip Macy 35889e0f4d2SKip Macy #define IPRINTK(fmt, args...) \ 35989e0f4d2SKip Macy printf("[XEN] " fmt, ##args) 360227ca257SKip Macy #ifdef INVARIANTS 36189e0f4d2SKip Macy #define WPRINTK(fmt, args...) \ 36289e0f4d2SKip Macy printf("[XEN] " fmt, ##args) 363227ca257SKip Macy #else 364227ca257SKip Macy #define WPRINTK(fmt, args...) 365227ca257SKip Macy #endif 366227ca257SKip Macy #ifdef DEBUG 36789e0f4d2SKip Macy #define DPRINTK(fmt, args...) \ 36823dc5621SKip Macy printf("[XEN] %s: " fmt, __func__, ##args) 36912678024SDoug Rabson #else 37012678024SDoug Rabson #define DPRINTK(fmt, args...) 37112678024SDoug Rabson #endif 37289e0f4d2SKip Macy 37389e0f4d2SKip Macy /** 37489e0f4d2SKip Macy * Read the 'mac' node at the given device's node in the store, and parse that 37589e0f4d2SKip Macy * as colon-separated octets, placing result the given mac array. mac must be 37689e0f4d2SKip Macy * a preallocated array of length ETH_ALEN (as declared in linux/if_ether.h). 37789e0f4d2SKip Macy * Return 0 on success, or errno on error. 37889e0f4d2SKip Macy */ 37989e0f4d2SKip Macy static int 38023dc5621SKip Macy xen_net_read_mac(device_t dev, uint8_t mac[]) 38189e0f4d2SKip Macy { 3823a6d1fcfSKip Macy int error, i; 3833a6d1fcfSKip Macy char *s, *e, *macstr; 384ffa06904SJustin T. Gibbs const char *path; 3853a6d1fcfSKip Macy 386ffa06904SJustin T. Gibbs path = xenbus_get_node(dev); 387ffa06904SJustin T. Gibbs error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr); 388ffa06904SJustin T. Gibbs if (error == ENOENT) { 389ffa06904SJustin T. Gibbs /* 390ffa06904SJustin T. Gibbs * Deal with missing mac XenStore nodes on devices with 391ffa06904SJustin T. Gibbs * HVM emulation (the 'ioemu' configuration attribute) 392ffa06904SJustin T. Gibbs * enabled. 393ffa06904SJustin T. Gibbs * 394ffa06904SJustin T. Gibbs * The HVM emulator may execute in a stub device model 395ffa06904SJustin T. Gibbs * domain which lacks the permission, only given to Dom0, 396ffa06904SJustin T. Gibbs * to update the guest's XenStore tree. For this reason, 397ffa06904SJustin T. Gibbs * the HVM emulator doesn't even attempt to write the 398ffa06904SJustin T. Gibbs * front-side mac node, even when operating in Dom0. 399ffa06904SJustin T. Gibbs * However, there should always be a mac listed in the 400ffa06904SJustin T. Gibbs * backend tree. Fallback to this version if our query 401ffa06904SJustin T. Gibbs * of the front side XenStore location doesn't find 402ffa06904SJustin T. Gibbs * anything. 403ffa06904SJustin T. Gibbs */ 404ffa06904SJustin T. Gibbs path = xenbus_get_otherend_path(dev); 405ffa06904SJustin T. Gibbs error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr); 406ffa06904SJustin T. Gibbs } 407ffa06904SJustin T. Gibbs if (error != 0) { 408ffa06904SJustin T. Gibbs xenbus_dev_fatal(dev, error, "parsing %s/mac", path); 4093a6d1fcfSKip Macy return (error); 410ffa06904SJustin T. Gibbs } 4113a6d1fcfSKip Macy 41289e0f4d2SKip Macy s = macstr; 41389e0f4d2SKip Macy for (i = 0; i < ETHER_ADDR_LEN; i++) { 41489e0f4d2SKip Macy mac[i] = strtoul(s, &e, 16); 41589e0f4d2SKip Macy if (s == e || (e[0] != ':' && e[0] != 0)) { 416ff662b5cSJustin T. Gibbs free(macstr, M_XENBUS); 4173a6d1fcfSKip Macy return (ENOENT); 41889e0f4d2SKip Macy } 41989e0f4d2SKip Macy s = &e[1]; 42089e0f4d2SKip Macy } 421ff662b5cSJustin T. Gibbs free(macstr, M_XENBUS); 4223a6d1fcfSKip Macy return (0); 42389e0f4d2SKip Macy } 42489e0f4d2SKip Macy 42589e0f4d2SKip Macy /** 42689e0f4d2SKip Macy * Entry point to this code when a new device is created. Allocate the basic 42789e0f4d2SKip Macy * structures and the ring buffers for communication with the backend, and 42889e0f4d2SKip Macy * inform the backend of the appropriate details for those. Switch to 42989e0f4d2SKip Macy * Connected state. 43089e0f4d2SKip Macy */ 43189e0f4d2SKip Macy static int 43223dc5621SKip Macy netfront_probe(device_t dev) 43323dc5621SKip Macy { 43423dc5621SKip Macy 435f8f1bb83SRoger Pau Monné if (xen_hvm_domain() && xen_disable_pv_nics != 0) 436f8f1bb83SRoger Pau Monné return (ENXIO); 437f8f1bb83SRoger Pau Monné 43823dc5621SKip Macy if (!strcmp(xenbus_get_type(dev), "vif")) { 43923dc5621SKip Macy device_set_desc(dev, "Virtual Network Interface"); 44023dc5621SKip Macy return (0); 44123dc5621SKip Macy } 44223dc5621SKip Macy 44323dc5621SKip Macy return (ENXIO); 44423dc5621SKip Macy } 44523dc5621SKip Macy 44623dc5621SKip Macy static int 44723dc5621SKip Macy netfront_attach(device_t dev) 44889e0f4d2SKip Macy { 44989e0f4d2SKip Macy int err; 45089e0f4d2SKip Macy 45123dc5621SKip Macy err = create_netdev(dev); 45289e0f4d2SKip Macy if (err) { 45389e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "creating netdev"); 454ffa06904SJustin T. Gibbs return (err); 45589e0f4d2SKip Macy } 45689e0f4d2SKip Macy 45712678024SDoug Rabson #if __FreeBSD_version >= 700000 45812678024SDoug Rabson SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 45912678024SDoug Rabson SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 460f0188618SHans Petter Selasky OID_AUTO, "enable_lro", CTLFLAG_RW, 46112678024SDoug Rabson &xn_enable_lro, 0, "Large Receive Offload"); 46212678024SDoug Rabson #endif 46312678024SDoug Rabson 464ffa06904SJustin T. Gibbs return (0); 46589e0f4d2SKip Macy } 46689e0f4d2SKip Macy 467cf9c09e1SJustin T. Gibbs static int 468cf9c09e1SJustin T. Gibbs netfront_suspend(device_t dev) 469cf9c09e1SJustin T. Gibbs { 470cf9c09e1SJustin T. Gibbs struct netfront_info *info = device_get_softc(dev); 471cf9c09e1SJustin T. Gibbs 472cf9c09e1SJustin T. Gibbs XN_RX_LOCK(info); 473cf9c09e1SJustin T. Gibbs XN_TX_LOCK(info); 474cf9c09e1SJustin T. Gibbs netfront_carrier_off(info); 475cf9c09e1SJustin T. Gibbs XN_TX_UNLOCK(info); 476cf9c09e1SJustin T. Gibbs XN_RX_UNLOCK(info); 477cf9c09e1SJustin T. Gibbs return (0); 478cf9c09e1SJustin T. Gibbs } 47989e0f4d2SKip Macy 48089e0f4d2SKip Macy /** 48189e0f4d2SKip Macy * We are reconnecting to the backend, due to a suspend/resume, or a backend 48289e0f4d2SKip Macy * driver restart. We tear down our netif structure and recreate it, but 48389e0f4d2SKip Macy * leave the device-layer structures intact so that this is transparent to the 48489e0f4d2SKip Macy * rest of the kernel. 48589e0f4d2SKip Macy */ 48689e0f4d2SKip Macy static int 48723dc5621SKip Macy netfront_resume(device_t dev) 48889e0f4d2SKip Macy { 48923dc5621SKip Macy struct netfront_info *info = device_get_softc(dev); 49089e0f4d2SKip Macy 4916a8e9695SRoger Pau Monné info->xn_resume = true; 49289e0f4d2SKip Macy netif_disconnect_backend(info); 49389e0f4d2SKip Macy return (0); 49489e0f4d2SKip Macy } 49589e0f4d2SKip Macy 49689e0f4d2SKip Macy /* Common code used when first setting up, and when resuming. */ 49789e0f4d2SKip Macy static int 49823dc5621SKip Macy talk_to_backend(device_t dev, struct netfront_info *info) 49989e0f4d2SKip Macy { 50089e0f4d2SKip Macy const char *message; 501ff662b5cSJustin T. Gibbs struct xs_transaction xst; 50223dc5621SKip Macy const char *node = xenbus_get_node(dev); 50389e0f4d2SKip Macy int err; 50489e0f4d2SKip Macy 50589e0f4d2SKip Macy err = xen_net_read_mac(dev, info->mac); 50689e0f4d2SKip Macy if (err) { 50723dc5621SKip Macy xenbus_dev_fatal(dev, err, "parsing %s/mac", node); 50889e0f4d2SKip Macy goto out; 50989e0f4d2SKip Macy } 51089e0f4d2SKip Macy 51189e0f4d2SKip Macy /* Create shared ring, alloc event channel. */ 51289e0f4d2SKip Macy err = setup_device(dev, info); 51389e0f4d2SKip Macy if (err) 51489e0f4d2SKip Macy goto out; 51589e0f4d2SKip Macy 51689e0f4d2SKip Macy again: 517ff662b5cSJustin T. Gibbs err = xs_transaction_start(&xst); 51889e0f4d2SKip Macy if (err) { 51989e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "starting transaction"); 52089e0f4d2SKip Macy goto destroy_ring; 52189e0f4d2SKip Macy } 522ff662b5cSJustin T. Gibbs err = xs_printf(xst, node, "tx-ring-ref","%u", 52389e0f4d2SKip Macy info->tx_ring_ref); 52489e0f4d2SKip Macy if (err) { 52589e0f4d2SKip Macy message = "writing tx ring-ref"; 52689e0f4d2SKip Macy goto abort_transaction; 52789e0f4d2SKip Macy } 528ff662b5cSJustin T. Gibbs err = xs_printf(xst, node, "rx-ring-ref","%u", 52989e0f4d2SKip Macy info->rx_ring_ref); 53089e0f4d2SKip Macy if (err) { 53189e0f4d2SKip Macy message = "writing rx ring-ref"; 53289e0f4d2SKip Macy goto abort_transaction; 53389e0f4d2SKip Macy } 534ff662b5cSJustin T. Gibbs err = xs_printf(xst, node, 53576acc41fSJustin T. Gibbs "event-channel", "%u", 53676acc41fSJustin T. Gibbs xen_intr_port(info->xen_intr_handle)); 53789e0f4d2SKip Macy if (err) { 53889e0f4d2SKip Macy message = "writing event-channel"; 53989e0f4d2SKip Macy goto abort_transaction; 54089e0f4d2SKip Macy } 541*d0f3a8b9SRoger Pau Monné err = xs_printf(xst, node, "request-rx-copy", "%u", 1); 54289e0f4d2SKip Macy if (err) { 54389e0f4d2SKip Macy message = "writing request-rx-copy"; 54489e0f4d2SKip Macy goto abort_transaction; 54589e0f4d2SKip Macy } 546ff662b5cSJustin T. Gibbs err = xs_printf(xst, node, "feature-rx-notify", "%d", 1); 54789e0f4d2SKip Macy if (err) { 54889e0f4d2SKip Macy message = "writing feature-rx-notify"; 54989e0f4d2SKip Macy goto abort_transaction; 55089e0f4d2SKip Macy } 551ff662b5cSJustin T. Gibbs err = xs_printf(xst, node, "feature-sg", "%d", 1); 55289e0f4d2SKip Macy if (err) { 55389e0f4d2SKip Macy message = "writing feature-sg"; 55489e0f4d2SKip Macy goto abort_transaction; 55589e0f4d2SKip Macy } 55612678024SDoug Rabson #if __FreeBSD_version >= 700000 557ff662b5cSJustin T. Gibbs err = xs_printf(xst, node, "feature-gso-tcpv4", "%d", 1); 55889e0f4d2SKip Macy if (err) { 55989e0f4d2SKip Macy message = "writing feature-gso-tcpv4"; 56089e0f4d2SKip Macy goto abort_transaction; 56189e0f4d2SKip Macy } 56289e0f4d2SKip Macy #endif 56389e0f4d2SKip Macy 564ff662b5cSJustin T. Gibbs err = xs_transaction_end(xst, 0); 56589e0f4d2SKip Macy if (err) { 56689e0f4d2SKip Macy if (err == EAGAIN) 56789e0f4d2SKip Macy goto again; 56889e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "completing transaction"); 56989e0f4d2SKip Macy goto destroy_ring; 57089e0f4d2SKip Macy } 57189e0f4d2SKip Macy 57289e0f4d2SKip Macy return 0; 57389e0f4d2SKip Macy 57489e0f4d2SKip Macy abort_transaction: 575ff662b5cSJustin T. Gibbs xs_transaction_end(xst, 1); 57689e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "%s", message); 57789e0f4d2SKip Macy destroy_ring: 57889e0f4d2SKip Macy netif_free(info); 57989e0f4d2SKip Macy out: 58089e0f4d2SKip Macy return err; 58189e0f4d2SKip Macy } 58289e0f4d2SKip Macy 58389e0f4d2SKip Macy static int 58423dc5621SKip Macy setup_device(device_t dev, struct netfront_info *info) 58589e0f4d2SKip Macy { 58689e0f4d2SKip Macy netif_tx_sring_t *txs; 58789e0f4d2SKip Macy netif_rx_sring_t *rxs; 5883a6d1fcfSKip Macy int error; 58989e0f4d2SKip Macy 590ff662b5cSJustin T. Gibbs info->tx_ring_ref = GRANT_REF_INVALID; 591ff662b5cSJustin T. Gibbs info->rx_ring_ref = GRANT_REF_INVALID; 59289e0f4d2SKip Macy info->rx.sring = NULL; 59389e0f4d2SKip Macy info->tx.sring = NULL; 59489e0f4d2SKip Macy 59589e0f4d2SKip Macy txs = (netif_tx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO); 59689e0f4d2SKip Macy if (!txs) { 5973a6d1fcfSKip Macy error = ENOMEM; 5983a6d1fcfSKip Macy xenbus_dev_fatal(dev, error, "allocating tx ring page"); 59989e0f4d2SKip Macy goto fail; 60089e0f4d2SKip Macy } 60189e0f4d2SKip Macy SHARED_RING_INIT(txs); 60289e0f4d2SKip Macy FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE); 6033a6d1fcfSKip Macy error = xenbus_grant_ring(dev, virt_to_mfn(txs), &info->tx_ring_ref); 6043a6d1fcfSKip Macy if (error) 60589e0f4d2SKip Macy goto fail; 60689e0f4d2SKip Macy 60789e0f4d2SKip Macy rxs = (netif_rx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO); 60889e0f4d2SKip Macy if (!rxs) { 6093a6d1fcfSKip Macy error = ENOMEM; 6103a6d1fcfSKip Macy xenbus_dev_fatal(dev, error, "allocating rx ring page"); 61189e0f4d2SKip Macy goto fail; 61289e0f4d2SKip Macy } 61389e0f4d2SKip Macy SHARED_RING_INIT(rxs); 61489e0f4d2SKip Macy FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE); 61589e0f4d2SKip Macy 6163a6d1fcfSKip Macy error = xenbus_grant_ring(dev, virt_to_mfn(rxs), &info->rx_ring_ref); 6173a6d1fcfSKip Macy if (error) 61889e0f4d2SKip Macy goto fail; 61989e0f4d2SKip Macy 62076acc41fSJustin T. Gibbs error = xen_intr_alloc_and_bind_local_port(dev, 62176acc41fSJustin T. Gibbs xenbus_get_otherend_id(dev), /*filter*/NULL, xn_intr, info, 62276acc41fSJustin T. Gibbs INTR_TYPE_NET | INTR_MPSAFE | INTR_ENTROPY, &info->xen_intr_handle); 62389e0f4d2SKip Macy 6243a6d1fcfSKip Macy if (error) { 6253a6d1fcfSKip Macy xenbus_dev_fatal(dev, error, 62676acc41fSJustin T. Gibbs "xen_intr_alloc_and_bind_local_port failed"); 62789e0f4d2SKip Macy goto fail; 62889e0f4d2SKip Macy } 62989e0f4d2SKip Macy 6303a6d1fcfSKip Macy return (0); 63189e0f4d2SKip Macy 63289e0f4d2SKip Macy fail: 63389e0f4d2SKip Macy netif_free(info); 6343a6d1fcfSKip Macy return (error); 63589e0f4d2SKip Macy } 63689e0f4d2SKip Macy 637a0ae8f04SBjoern A. Zeeb #ifdef INET 63889e0f4d2SKip Macy /** 63912678024SDoug Rabson * If this interface has an ipv4 address, send an arp for it. This 64012678024SDoug Rabson * helps to get the network going again after migrating hosts. 64112678024SDoug Rabson */ 64212678024SDoug Rabson static void 64312678024SDoug Rabson netfront_send_fake_arp(device_t dev, struct netfront_info *info) 64412678024SDoug Rabson { 64512678024SDoug Rabson struct ifnet *ifp; 64612678024SDoug Rabson struct ifaddr *ifa; 64712678024SDoug Rabson 64812678024SDoug Rabson ifp = info->xn_ifp; 64912678024SDoug Rabson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 65012678024SDoug Rabson if (ifa->ifa_addr->sa_family == AF_INET) { 65112678024SDoug Rabson arp_ifinit(ifp, ifa); 65212678024SDoug Rabson } 65312678024SDoug Rabson } 65412678024SDoug Rabson } 655a0ae8f04SBjoern A. Zeeb #endif 65612678024SDoug Rabson 65712678024SDoug Rabson /** 65889e0f4d2SKip Macy * Callback received when the backend's state changes. 65989e0f4d2SKip Macy */ 660283d6f72SJustin T. Gibbs static void 66123dc5621SKip Macy netfront_backend_changed(device_t dev, XenbusState newstate) 66289e0f4d2SKip Macy { 66323dc5621SKip Macy struct netfront_info *sc = device_get_softc(dev); 66489e0f4d2SKip Macy 66523dc5621SKip Macy DPRINTK("newstate=%d\n", newstate); 66689e0f4d2SKip Macy 66723dc5621SKip Macy switch (newstate) { 66889e0f4d2SKip Macy case XenbusStateInitialising: 66989e0f4d2SKip Macy case XenbusStateInitialised: 67089e0f4d2SKip Macy case XenbusStateUnknown: 67189e0f4d2SKip Macy case XenbusStateClosed: 672920ba15bSKip Macy case XenbusStateReconfigured: 673920ba15bSKip Macy case XenbusStateReconfiguring: 67489e0f4d2SKip Macy break; 67589e0f4d2SKip Macy case XenbusStateInitWait: 67623dc5621SKip Macy if (xenbus_get_state(dev) != XenbusStateInitialising) 67789e0f4d2SKip Macy break; 67823dc5621SKip Macy if (network_connect(sc) != 0) 67989e0f4d2SKip Macy break; 68023dc5621SKip Macy xenbus_set_state(dev, XenbusStateConnected); 68123dc5621SKip Macy break; 68289e0f4d2SKip Macy case XenbusStateClosing: 68323dc5621SKip Macy xenbus_set_state(dev, XenbusStateClosed); 68489e0f4d2SKip Macy break; 685dbf82bdeSRoger Pau Monné case XenbusStateConnected: 686dbf82bdeSRoger Pau Monné #ifdef INET 687dbf82bdeSRoger Pau Monné netfront_send_fake_arp(dev, sc); 688dbf82bdeSRoger Pau Monné #endif 689dbf82bdeSRoger Pau Monné break; 69089e0f4d2SKip Macy } 69189e0f4d2SKip Macy } 69289e0f4d2SKip Macy 69389e0f4d2SKip Macy static void 69489e0f4d2SKip Macy xn_free_rx_ring(struct netfront_info *sc) 69589e0f4d2SKip Macy { 69689e0f4d2SKip Macy #if 0 69789e0f4d2SKip Macy int i; 69889e0f4d2SKip Macy 69989e0f4d2SKip Macy for (i = 0; i < NET_RX_RING_SIZE; i++) { 700931eeffaSKenneth D. Merry if (sc->xn_cdata.rx_mbufs[i] != NULL) { 701931eeffaSKenneth D. Merry m_freem(sc->rx_mbufs[i]); 702931eeffaSKenneth D. Merry sc->rx_mbufs[i] = NULL; 70389e0f4d2SKip Macy } 70489e0f4d2SKip Macy } 70589e0f4d2SKip Macy 70689e0f4d2SKip Macy sc->rx.rsp_cons = 0; 70789e0f4d2SKip Macy sc->xn_rx_if->req_prod = 0; 70889e0f4d2SKip Macy sc->xn_rx_if->event = sc->rx.rsp_cons ; 70989e0f4d2SKip Macy #endif 71089e0f4d2SKip Macy } 71189e0f4d2SKip Macy 71289e0f4d2SKip Macy static void 71389e0f4d2SKip Macy xn_free_tx_ring(struct netfront_info *sc) 71489e0f4d2SKip Macy { 71589e0f4d2SKip Macy #if 0 71689e0f4d2SKip Macy int i; 71789e0f4d2SKip Macy 71889e0f4d2SKip Macy for (i = 0; i < NET_TX_RING_SIZE; i++) { 719931eeffaSKenneth D. Merry if (sc->tx_mbufs[i] != NULL) { 720931eeffaSKenneth D. Merry m_freem(sc->tx_mbufs[i]); 72189e0f4d2SKip Macy sc->xn_cdata.xn_tx_chain[i] = NULL; 72289e0f4d2SKip Macy } 72389e0f4d2SKip Macy } 72489e0f4d2SKip Macy 72589e0f4d2SKip Macy return; 72689e0f4d2SKip Macy #endif 72789e0f4d2SKip Macy } 72889e0f4d2SKip Macy 729931eeffaSKenneth D. Merry /** 730931eeffaSKenneth D. Merry * \brief Verify that there is sufficient space in the Tx ring 731931eeffaSKenneth D. Merry * buffer for a maximally sized request to be enqueued. 732c099cafaSAdrian Chadd * 733931eeffaSKenneth D. Merry * A transmit request requires a transmit descriptor for each packet 734931eeffaSKenneth D. Merry * fragment, plus up to 2 entries for "options" (e.g. TSO). 735c099cafaSAdrian Chadd */ 73689e0f4d2SKip Macy static inline int 737931eeffaSKenneth D. Merry xn_tx_slot_available(struct netfront_info *np) 73889e0f4d2SKip Macy { 739931eeffaSKenneth D. Merry return (RING_FREE_REQUESTS(&np->tx) > (MAX_TX_REQ_FRAGS + 2)); 74089e0f4d2SKip Macy } 741931eeffaSKenneth D. Merry 74289e0f4d2SKip Macy static void 74389e0f4d2SKip Macy netif_release_tx_bufs(struct netfront_info *np) 74489e0f4d2SKip Macy { 74589e0f4d2SKip Macy int i; 74689e0f4d2SKip Macy 74789e0f4d2SKip Macy for (i = 1; i <= NET_TX_RING_SIZE; i++) { 748931eeffaSKenneth D. Merry struct mbuf *m; 74989e0f4d2SKip Macy 750931eeffaSKenneth D. Merry m = np->tx_mbufs[i]; 751931eeffaSKenneth D. Merry 752931eeffaSKenneth D. Merry /* 753931eeffaSKenneth D. Merry * We assume that no kernel addresses are 754931eeffaSKenneth D. Merry * less than NET_TX_RING_SIZE. Any entry 755931eeffaSKenneth D. Merry * in the table that is below this number 756931eeffaSKenneth D. Merry * must be an index from free-list tracking. 757931eeffaSKenneth D. Merry */ 758931eeffaSKenneth D. Merry if (((uintptr_t)m) <= NET_TX_RING_SIZE) 75989e0f4d2SKip Macy continue; 760cf9c09e1SJustin T. Gibbs gnttab_end_foreign_access_ref(np->grant_tx_ref[i]); 76189e0f4d2SKip Macy gnttab_release_grant_reference(&np->gref_tx_head, 76289e0f4d2SKip Macy np->grant_tx_ref[i]); 763ff662b5cSJustin T. Gibbs np->grant_tx_ref[i] = GRANT_REF_INVALID; 76489e0f4d2SKip Macy add_id_to_freelist(np->tx_mbufs, i); 765a4ec37f5SAdrian Chadd np->xn_cdata.xn_tx_chain_cnt--; 766a4ec37f5SAdrian Chadd if (np->xn_cdata.xn_tx_chain_cnt < 0) { 7676f9767acSMarius Strobl panic("%s: tx_chain_cnt must be >= 0", __func__); 768a4ec37f5SAdrian Chadd } 769cf9c09e1SJustin T. Gibbs m_free(m); 77089e0f4d2SKip Macy } 77189e0f4d2SKip Macy } 77289e0f4d2SKip Macy 77389e0f4d2SKip Macy static void 77489e0f4d2SKip Macy network_alloc_rx_buffers(struct netfront_info *sc) 77589e0f4d2SKip Macy { 77623dc5621SKip Macy int otherend_id = xenbus_get_otherend_id(sc->xbdev); 77789e0f4d2SKip Macy unsigned short id; 77889e0f4d2SKip Macy struct mbuf *m_new; 77989e0f4d2SKip Macy int i, batch_target, notify; 78089e0f4d2SKip Macy RING_IDX req_prod; 78189e0f4d2SKip Macy grant_ref_t ref; 78289e0f4d2SKip Macy netif_rx_request_t *req; 78389e0f4d2SKip Macy vm_offset_t vaddr; 78489e0f4d2SKip Macy u_long pfn; 78589e0f4d2SKip Macy 78689e0f4d2SKip Macy req_prod = sc->rx.req_prod_pvt; 78789e0f4d2SKip Macy 78876acc41fSJustin T. Gibbs if (__predict_false(sc->carrier == 0)) 78989e0f4d2SKip Macy return; 79089e0f4d2SKip Macy 79189e0f4d2SKip Macy /* 792931eeffaSKenneth D. Merry * Allocate mbufs greedily, even though we batch updates to the 79389e0f4d2SKip Macy * receive ring. This creates a less bursty demand on the memory 794931eeffaSKenneth D. Merry * allocator, and so should reduce the chance of failed allocation 79589e0f4d2SKip Macy * requests both for ourself and for other kernel subsystems. 796931eeffaSKenneth D. Merry * 797931eeffaSKenneth D. Merry * Here we attempt to maintain rx_target buffers in flight, counting 798931eeffaSKenneth D. Merry * buffers that we have yet to process in the receive ring. 79989e0f4d2SKip Macy */ 80089e0f4d2SKip Macy batch_target = sc->rx_target - (req_prod - sc->rx.rsp_cons); 80189e0f4d2SKip Macy for (i = mbufq_len(&sc->xn_rx_batch); i < batch_target; i++) { 802c2d9c6f0SGleb Smirnoff m_new = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 803931eeffaSKenneth D. Merry if (m_new == NULL) { 80489e0f4d2SKip Macy if (i != 0) 80589e0f4d2SKip Macy goto refill; 80689e0f4d2SKip Macy /* 80789e0f4d2SKip Macy * XXX set timer 80889e0f4d2SKip Macy */ 80989e0f4d2SKip Macy break; 81089e0f4d2SKip Macy } 81189e0f4d2SKip Macy m_new->m_len = m_new->m_pkthdr.len = MJUMPAGESIZE; 81289e0f4d2SKip Macy 81389e0f4d2SKip Macy /* queue the mbufs allocated */ 814c578b6acSGleb Smirnoff (void )mbufq_enqueue(&sc->xn_rx_batch, m_new); 81589e0f4d2SKip Macy } 81689e0f4d2SKip Macy 817931eeffaSKenneth D. Merry /* 818931eeffaSKenneth D. Merry * If we've allocated at least half of our target number of entries, 819931eeffaSKenneth D. Merry * submit them to the backend - we have enough to make the overhead 820931eeffaSKenneth D. Merry * of submission worthwhile. Otherwise wait for more mbufs and 821931eeffaSKenneth D. Merry * request entries to become available. 822931eeffaSKenneth D. Merry */ 82389e0f4d2SKip Macy if (i < (sc->rx_target/2)) { 82489e0f4d2SKip Macy if (req_prod >sc->rx.sring->req_prod) 82589e0f4d2SKip Macy goto push; 82689e0f4d2SKip Macy return; 82789e0f4d2SKip Macy } 828931eeffaSKenneth D. Merry 829931eeffaSKenneth D. Merry /* 830931eeffaSKenneth D. Merry * Double floating fill target if we risked having the backend 831931eeffaSKenneth D. Merry * run out of empty buffers for receive traffic. We define "running 832931eeffaSKenneth D. Merry * low" as having less than a fourth of our target buffers free 833931eeffaSKenneth D. Merry * at the time we refilled the queue. 834931eeffaSKenneth D. Merry */ 835931eeffaSKenneth D. Merry if ((req_prod - sc->rx.sring->rsp_prod) < (sc->rx_target / 4)) { 836931eeffaSKenneth D. Merry sc->rx_target *= 2; 837931eeffaSKenneth D. Merry if (sc->rx_target > sc->rx_max_target) 83889e0f4d2SKip Macy sc->rx_target = sc->rx_max_target; 839931eeffaSKenneth D. Merry } 84089e0f4d2SKip Macy 84189e0f4d2SKip Macy refill: 842*d0f3a8b9SRoger Pau Monné for (i = 0; ; i++) { 84389e0f4d2SKip Macy if ((m_new = mbufq_dequeue(&sc->xn_rx_batch)) == NULL) 84489e0f4d2SKip Macy break; 84589e0f4d2SKip Macy 84689e0f4d2SKip Macy m_new->m_ext.ext_arg1 = (vm_paddr_t *)(uintptr_t)( 84789e0f4d2SKip Macy vtophys(m_new->m_ext.ext_buf) >> PAGE_SHIFT); 84889e0f4d2SKip Macy 84989e0f4d2SKip Macy id = xennet_rxidx(req_prod + i); 85089e0f4d2SKip Macy 851931eeffaSKenneth D. Merry KASSERT(sc->rx_mbufs[id] == NULL, ("non-NULL xm_rx_chain")); 852931eeffaSKenneth D. Merry sc->rx_mbufs[id] = m_new; 85389e0f4d2SKip Macy 85489e0f4d2SKip Macy ref = gnttab_claim_grant_reference(&sc->gref_rx_head); 855ff662b5cSJustin T. Gibbs KASSERT(ref != GNTTAB_LIST_END, 856ff662b5cSJustin T. Gibbs ("reserved grant references exhuasted")); 85789e0f4d2SKip Macy sc->grant_rx_ref[id] = ref; 85889e0f4d2SKip Macy 85989e0f4d2SKip Macy vaddr = mtod(m_new, vm_offset_t); 86089e0f4d2SKip Macy pfn = vtophys(vaddr) >> PAGE_SHIFT; 86189e0f4d2SKip Macy req = RING_GET_REQUEST(&sc->rx, req_prod + i); 86289e0f4d2SKip Macy 863*d0f3a8b9SRoger Pau Monné gnttab_grant_foreign_access_ref(ref, otherend_id, pfn, 0); 86489e0f4d2SKip Macy req->id = id; 86589e0f4d2SKip Macy req->gref = ref; 86689e0f4d2SKip Macy 86789e0f4d2SKip Macy sc->rx_pfn_array[i] = 8683c790178SJohn Baldwin vtophys(mtod(m_new,vm_offset_t)) >> PAGE_SHIFT; 86989e0f4d2SKip Macy } 87089e0f4d2SKip Macy 87189e0f4d2SKip Macy KASSERT(i, ("no mbufs processed")); /* should have returned earlier */ 87289e0f4d2SKip Macy KASSERT(mbufq_len(&sc->xn_rx_batch) == 0, ("not all mbufs processed")); 87389e0f4d2SKip Macy /* 87489e0f4d2SKip Macy * We may have allocated buffers which have entries outstanding 87589e0f4d2SKip Macy * in the page * update queue -- make sure we flush those first! 87689e0f4d2SKip Macy */ 87789e0f4d2SKip Macy wmb(); 87889e0f4d2SKip Macy 87989e0f4d2SKip Macy /* Above is a suitable barrier to ensure backend will see requests. */ 88089e0f4d2SKip Macy sc->rx.req_prod_pvt = req_prod + i; 88189e0f4d2SKip Macy push: 88289e0f4d2SKip Macy RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->rx, notify); 88389e0f4d2SKip Macy if (notify) 88476acc41fSJustin T. Gibbs xen_intr_signal(sc->xen_intr_handle); 88589e0f4d2SKip Macy } 88689e0f4d2SKip Macy 88789e0f4d2SKip Macy static void 88889e0f4d2SKip Macy xn_rxeof(struct netfront_info *np) 88989e0f4d2SKip Macy { 89089e0f4d2SKip Macy struct ifnet *ifp; 891f909bbb4SGleb Smirnoff #if __FreeBSD_version >= 700000 && (defined(INET) || defined(INET6)) 89212678024SDoug Rabson struct lro_ctrl *lro = &np->xn_lro; 89312678024SDoug Rabson struct lro_entry *queued; 89412678024SDoug Rabson #endif 89589e0f4d2SKip Macy struct netfront_rx_info rinfo; 89689e0f4d2SKip Macy struct netif_rx_response *rx = &rinfo.rx; 89789e0f4d2SKip Macy struct netif_extra_info *extras = rinfo.extras; 89889e0f4d2SKip Macy RING_IDX i, rp; 89989e0f4d2SKip Macy struct mbuf *m; 900c578b6acSGleb Smirnoff struct mbufq rxq, errq; 901*d0f3a8b9SRoger Pau Monné int err, work_to_do; 90289e0f4d2SKip Macy 90349906218SDoug Rabson do { 90489e0f4d2SKip Macy XN_RX_LOCK_ASSERT(np); 90589e0f4d2SKip Macy if (!netfront_carrier_ok(np)) 90689e0f4d2SKip Macy return; 90789e0f4d2SKip Macy 908c578b6acSGleb Smirnoff /* XXX: there should be some sane limit. */ 909c578b6acSGleb Smirnoff mbufq_init(&errq, INT_MAX); 910c578b6acSGleb Smirnoff mbufq_init(&rxq, INT_MAX); 91189e0f4d2SKip Macy 91289e0f4d2SKip Macy ifp = np->xn_ifp; 91389e0f4d2SKip Macy 91489e0f4d2SKip Macy rp = np->rx.sring->rsp_prod; 91589e0f4d2SKip Macy rmb(); /* Ensure we see queued responses up to 'rp'. */ 91689e0f4d2SKip Macy 91789e0f4d2SKip Macy i = np->rx.rsp_cons; 91889e0f4d2SKip Macy while ((i != rp)) { 91989e0f4d2SKip Macy memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx)); 92089e0f4d2SKip Macy memset(extras, 0, sizeof(rinfo.extras)); 92189e0f4d2SKip Macy 92283b92f6eSKip Macy m = NULL; 923*d0f3a8b9SRoger Pau Monné err = xennet_get_responses(np, &rinfo, rp, &i, &m); 92489e0f4d2SKip Macy 92576acc41fSJustin T. Gibbs if (__predict_false(err)) { 92683b92f6eSKip Macy if (m) 927c578b6acSGleb Smirnoff (void )mbufq_enqueue(&errq, m); 92889e0f4d2SKip Macy np->stats.rx_errors++; 92989e0f4d2SKip Macy continue; 93089e0f4d2SKip Macy } 93189e0f4d2SKip Macy 93289e0f4d2SKip Macy m->m_pkthdr.rcvif = ifp; 93389e0f4d2SKip Macy if ( rx->flags & NETRXF_data_validated ) { 93489e0f4d2SKip Macy /* Tell the stack the checksums are okay */ 93589e0f4d2SKip Macy /* 93689e0f4d2SKip Macy * XXX this isn't necessarily the case - need to add 93789e0f4d2SKip Macy * check 93889e0f4d2SKip Macy */ 93989e0f4d2SKip Macy 94089e0f4d2SKip Macy m->m_pkthdr.csum_flags |= 94189e0f4d2SKip Macy (CSUM_IP_CHECKED | CSUM_IP_VALID | CSUM_DATA_VALID 94289e0f4d2SKip Macy | CSUM_PSEUDO_HDR); 94389e0f4d2SKip Macy m->m_pkthdr.csum_data = 0xffff; 94489e0f4d2SKip Macy } 94589e0f4d2SKip Macy 94689e0f4d2SKip Macy np->stats.rx_packets++; 94783b92f6eSKip Macy np->stats.rx_bytes += m->m_pkthdr.len; 94889e0f4d2SKip Macy 949c578b6acSGleb Smirnoff (void )mbufq_enqueue(&rxq, m); 950931eeffaSKenneth D. Merry np->rx.rsp_cons = i; 95189e0f4d2SKip Macy } 95289e0f4d2SKip Macy 953c578b6acSGleb Smirnoff mbufq_drain(&errq); 95489e0f4d2SKip Macy 95589e0f4d2SKip Macy /* 95689e0f4d2SKip Macy * Process all the mbufs after the remapping is complete. 95789e0f4d2SKip Macy * Break the mbuf chain first though. 95889e0f4d2SKip Macy */ 95989e0f4d2SKip Macy while ((m = mbufq_dequeue(&rxq)) != NULL) { 960c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 96189e0f4d2SKip Macy 96289e0f4d2SKip Macy /* 96389e0f4d2SKip Macy * Do we really need to drop the rx lock? 96489e0f4d2SKip Macy */ 96589e0f4d2SKip Macy XN_RX_UNLOCK(np); 966f909bbb4SGleb Smirnoff #if __FreeBSD_version >= 700000 && (defined(INET) || defined(INET6)) 96712678024SDoug Rabson /* Use LRO if possible */ 96812678024SDoug Rabson if ((ifp->if_capenable & IFCAP_LRO) == 0 || 96912678024SDoug Rabson lro->lro_cnt == 0 || tcp_lro_rx(lro, m, 0)) { 97012678024SDoug Rabson /* 97112678024SDoug Rabson * If LRO fails, pass up to the stack 97212678024SDoug Rabson * directly. 97312678024SDoug Rabson */ 97489e0f4d2SKip Macy (*ifp->if_input)(ifp, m); 97512678024SDoug Rabson } 97612678024SDoug Rabson #else 97712678024SDoug Rabson (*ifp->if_input)(ifp, m); 97812678024SDoug Rabson #endif 97989e0f4d2SKip Macy XN_RX_LOCK(np); 98089e0f4d2SKip Macy } 98189e0f4d2SKip Macy 98289e0f4d2SKip Macy np->rx.rsp_cons = i; 98389e0f4d2SKip Macy 984f909bbb4SGleb Smirnoff #if __FreeBSD_version >= 700000 && (defined(INET) || defined(INET6)) 98512678024SDoug Rabson /* 98612678024SDoug Rabson * Flush any outstanding LRO work 98712678024SDoug Rabson */ 98812678024SDoug Rabson while (!SLIST_EMPTY(&lro->lro_active)) { 98912678024SDoug Rabson queued = SLIST_FIRST(&lro->lro_active); 99012678024SDoug Rabson SLIST_REMOVE_HEAD(&lro->lro_active, next); 99112678024SDoug Rabson tcp_lro_flush(lro, queued); 99212678024SDoug Rabson } 99312678024SDoug Rabson #endif 99412678024SDoug Rabson 99589e0f4d2SKip Macy #if 0 99689e0f4d2SKip Macy /* If we get a callback with very few responses, reduce fill target. */ 99789e0f4d2SKip Macy /* NB. Note exponential increase, linear decrease. */ 99889e0f4d2SKip Macy if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) > 99989e0f4d2SKip Macy ((3*np->rx_target) / 4)) && (--np->rx_target < np->rx_min_target)) 100089e0f4d2SKip Macy np->rx_target = np->rx_min_target; 100189e0f4d2SKip Macy #endif 100289e0f4d2SKip Macy 100389e0f4d2SKip Macy network_alloc_rx_buffers(np); 100489e0f4d2SKip Macy 100549906218SDoug Rabson RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, work_to_do); 100649906218SDoug Rabson } while (work_to_do); 100789e0f4d2SKip Macy } 100889e0f4d2SKip Macy 100989e0f4d2SKip Macy static void 101089e0f4d2SKip Macy xn_txeof(struct netfront_info *np) 101189e0f4d2SKip Macy { 101289e0f4d2SKip Macy RING_IDX i, prod; 101389e0f4d2SKip Macy unsigned short id; 101489e0f4d2SKip Macy struct ifnet *ifp; 101512678024SDoug Rabson netif_tx_response_t *txr; 101689e0f4d2SKip Macy struct mbuf *m; 101789e0f4d2SKip Macy 101889e0f4d2SKip Macy XN_TX_LOCK_ASSERT(np); 101989e0f4d2SKip Macy 102089e0f4d2SKip Macy if (!netfront_carrier_ok(np)) 102189e0f4d2SKip Macy return; 102289e0f4d2SKip Macy 102389e0f4d2SKip Macy ifp = np->xn_ifp; 102489e0f4d2SKip Macy 102589e0f4d2SKip Macy do { 102689e0f4d2SKip Macy prod = np->tx.sring->rsp_prod; 102789e0f4d2SKip Macy rmb(); /* Ensure we see responses up to 'rp'. */ 102889e0f4d2SKip Macy 102989e0f4d2SKip Macy for (i = np->tx.rsp_cons; i != prod; i++) { 103012678024SDoug Rabson txr = RING_GET_RESPONSE(&np->tx, i); 103112678024SDoug Rabson if (txr->status == NETIF_RSP_NULL) 103212678024SDoug Rabson continue; 103312678024SDoug Rabson 1034931eeffaSKenneth D. Merry if (txr->status != NETIF_RSP_OKAY) { 1035931eeffaSKenneth D. Merry printf("%s: WARNING: response is %d!\n", 1036931eeffaSKenneth D. Merry __func__, txr->status); 1037931eeffaSKenneth D. Merry } 103812678024SDoug Rabson id = txr->id; 1039931eeffaSKenneth D. Merry m = np->tx_mbufs[id]; 10402d8fae98SAdrian Chadd KASSERT(m != NULL, ("mbuf not found in xn_tx_chain")); 1041931eeffaSKenneth D. Merry KASSERT((uintptr_t)m > NET_TX_RING_SIZE, 1042931eeffaSKenneth D. Merry ("mbuf already on the free list, but we're " 1043931eeffaSKenneth D. Merry "trying to free it again!")); 10442d8fae98SAdrian Chadd M_ASSERTVALID(m); 104589e0f4d2SKip Macy 104612678024SDoug Rabson /* 104712678024SDoug Rabson * Increment packet count if this is the last 104812678024SDoug Rabson * mbuf of the chain. 104912678024SDoug Rabson */ 105012678024SDoug Rabson if (!m->m_next) 1051c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 105276acc41fSJustin T. Gibbs if (__predict_false(gnttab_query_foreign_access( 105389e0f4d2SKip Macy np->grant_tx_ref[id]) != 0)) { 10546f9767acSMarius Strobl panic("%s: grant id %u still in use by the " 10556f9767acSMarius Strobl "backend", __func__, id); 105689e0f4d2SKip Macy } 105789e0f4d2SKip Macy gnttab_end_foreign_access_ref( 1058920ba15bSKip Macy np->grant_tx_ref[id]); 105989e0f4d2SKip Macy gnttab_release_grant_reference( 106089e0f4d2SKip Macy &np->gref_tx_head, np->grant_tx_ref[id]); 1061ff662b5cSJustin T. Gibbs np->grant_tx_ref[id] = GRANT_REF_INVALID; 106289e0f4d2SKip Macy 1063931eeffaSKenneth D. Merry np->tx_mbufs[id] = NULL; 1064931eeffaSKenneth D. Merry add_id_to_freelist(np->tx_mbufs, id); 1065a4ec37f5SAdrian Chadd np->xn_cdata.xn_tx_chain_cnt--; 106612678024SDoug Rabson m_free(m); 1067d76e4550SAdrian Chadd /* Only mark the queue active if we've freed up at least one slot to try */ 1068d76e4550SAdrian Chadd ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 106989e0f4d2SKip Macy } 107089e0f4d2SKip Macy np->tx.rsp_cons = prod; 107189e0f4d2SKip Macy 107289e0f4d2SKip Macy /* 107389e0f4d2SKip Macy * Set a new event, then check for race with update of 107489e0f4d2SKip Macy * tx_cons. Note that it is essential to schedule a 107589e0f4d2SKip Macy * callback, no matter how few buffers are pending. Even if 107689e0f4d2SKip Macy * there is space in the transmit ring, higher layers may 107789e0f4d2SKip Macy * be blocked because too much data is outstanding: in such 107889e0f4d2SKip Macy * cases notification from Xen is likely to be the only kick 107989e0f4d2SKip Macy * that we'll get. 108089e0f4d2SKip Macy */ 108189e0f4d2SKip Macy np->tx.sring->rsp_event = 108289e0f4d2SKip Macy prod + ((np->tx.sring->req_prod - prod) >> 1) + 1; 108389e0f4d2SKip Macy 108489e0f4d2SKip Macy mb(); 108589e0f4d2SKip Macy } while (prod != np->tx.sring->rsp_prod); 108689e0f4d2SKip Macy 108789e0f4d2SKip Macy if (np->tx_full && 108889e0f4d2SKip Macy ((np->tx.sring->req_prod - prod) < NET_TX_RING_SIZE)) { 108989e0f4d2SKip Macy np->tx_full = 0; 109089e0f4d2SKip Macy #if 0 109189e0f4d2SKip Macy if (np->user_state == UST_OPEN) 109289e0f4d2SKip Macy netif_wake_queue(dev); 109389e0f4d2SKip Macy #endif 109489e0f4d2SKip Macy } 109589e0f4d2SKip Macy } 109689e0f4d2SKip Macy 109789e0f4d2SKip Macy static void 109889e0f4d2SKip Macy xn_intr(void *xsc) 109989e0f4d2SKip Macy { 110089e0f4d2SKip Macy struct netfront_info *np = xsc; 110189e0f4d2SKip Macy struct ifnet *ifp = np->xn_ifp; 110289e0f4d2SKip Macy 110389e0f4d2SKip Macy #if 0 110489e0f4d2SKip Macy if (!(np->rx.rsp_cons != np->rx.sring->rsp_prod && 110589e0f4d2SKip Macy likely(netfront_carrier_ok(np)) && 110689e0f4d2SKip Macy ifp->if_drv_flags & IFF_DRV_RUNNING)) 110789e0f4d2SKip Macy return; 110889e0f4d2SKip Macy #endif 1109931eeffaSKenneth D. Merry if (RING_HAS_UNCONSUMED_RESPONSES(&np->tx)) { 111089e0f4d2SKip Macy XN_TX_LOCK(np); 111189e0f4d2SKip Macy xn_txeof(np); 111289e0f4d2SKip Macy XN_TX_UNLOCK(np); 111389e0f4d2SKip Macy } 111489e0f4d2SKip Macy 111589e0f4d2SKip Macy XN_RX_LOCK(np); 111689e0f4d2SKip Macy xn_rxeof(np); 111789e0f4d2SKip Macy XN_RX_UNLOCK(np); 111889e0f4d2SKip Macy 111989e0f4d2SKip Macy if (ifp->if_drv_flags & IFF_DRV_RUNNING && 112089e0f4d2SKip Macy !IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 112189e0f4d2SKip Macy xn_start(ifp); 112289e0f4d2SKip Macy } 112389e0f4d2SKip Macy 112489e0f4d2SKip Macy static void 112589e0f4d2SKip Macy xennet_move_rx_slot(struct netfront_info *np, struct mbuf *m, 112689e0f4d2SKip Macy grant_ref_t ref) 112789e0f4d2SKip Macy { 112889e0f4d2SKip Macy int new = xennet_rxidx(np->rx.req_prod_pvt); 112989e0f4d2SKip Macy 113089e0f4d2SKip Macy KASSERT(np->rx_mbufs[new] == NULL, ("rx_mbufs != NULL")); 113189e0f4d2SKip Macy np->rx_mbufs[new] = m; 113289e0f4d2SKip Macy np->grant_rx_ref[new] = ref; 113389e0f4d2SKip Macy RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new; 113489e0f4d2SKip Macy RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref; 113589e0f4d2SKip Macy np->rx.req_prod_pvt++; 113689e0f4d2SKip Macy } 113789e0f4d2SKip Macy 113889e0f4d2SKip Macy static int 113989e0f4d2SKip Macy xennet_get_extras(struct netfront_info *np, 1140931eeffaSKenneth D. Merry struct netif_extra_info *extras, RING_IDX rp, RING_IDX *cons) 114189e0f4d2SKip Macy { 114289e0f4d2SKip Macy struct netif_extra_info *extra; 114389e0f4d2SKip Macy 114489e0f4d2SKip Macy int err = 0; 114589e0f4d2SKip Macy 114689e0f4d2SKip Macy do { 114789e0f4d2SKip Macy struct mbuf *m; 114889e0f4d2SKip Macy grant_ref_t ref; 114989e0f4d2SKip Macy 115076acc41fSJustin T. Gibbs if (__predict_false(*cons + 1 == rp)) { 115189e0f4d2SKip Macy #if 0 115289e0f4d2SKip Macy if (net_ratelimit()) 115389e0f4d2SKip Macy WPRINTK("Missing extra info\n"); 115489e0f4d2SKip Macy #endif 1155931eeffaSKenneth D. Merry err = EINVAL; 115689e0f4d2SKip Macy break; 115789e0f4d2SKip Macy } 115889e0f4d2SKip Macy 115989e0f4d2SKip Macy extra = (struct netif_extra_info *) 1160931eeffaSKenneth D. Merry RING_GET_RESPONSE(&np->rx, ++(*cons)); 116189e0f4d2SKip Macy 116276acc41fSJustin T. Gibbs if (__predict_false(!extra->type || 116389e0f4d2SKip Macy extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) { 116489e0f4d2SKip Macy #if 0 116589e0f4d2SKip Macy if (net_ratelimit()) 116689e0f4d2SKip Macy WPRINTK("Invalid extra type: %d\n", 116789e0f4d2SKip Macy extra->type); 116889e0f4d2SKip Macy #endif 1169931eeffaSKenneth D. Merry err = EINVAL; 117089e0f4d2SKip Macy } else { 117189e0f4d2SKip Macy memcpy(&extras[extra->type - 1], extra, sizeof(*extra)); 117289e0f4d2SKip Macy } 117389e0f4d2SKip Macy 1174931eeffaSKenneth D. Merry m = xennet_get_rx_mbuf(np, *cons); 1175931eeffaSKenneth D. Merry ref = xennet_get_rx_ref(np, *cons); 117689e0f4d2SKip Macy xennet_move_rx_slot(np, m, ref); 117789e0f4d2SKip Macy } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE); 117889e0f4d2SKip Macy 117989e0f4d2SKip Macy return err; 118089e0f4d2SKip Macy } 118189e0f4d2SKip Macy 118289e0f4d2SKip Macy static int 118389e0f4d2SKip Macy xennet_get_responses(struct netfront_info *np, 1184931eeffaSKenneth D. Merry struct netfront_rx_info *rinfo, RING_IDX rp, RING_IDX *cons, 1185*d0f3a8b9SRoger Pau Monné struct mbuf **list) 118689e0f4d2SKip Macy { 118789e0f4d2SKip Macy struct netif_rx_response *rx = &rinfo->rx; 118889e0f4d2SKip Macy struct netif_extra_info *extras = rinfo->extras; 118983b92f6eSKip Macy struct mbuf *m, *m0, *m_prev; 1190931eeffaSKenneth D. Merry grant_ref_t ref = xennet_get_rx_ref(np, *cons); 1191931eeffaSKenneth D. Merry RING_IDX ref_cons = *cons; 119289e0f4d2SKip Macy int frags = 1; 119389e0f4d2SKip Macy int err = 0; 119489e0f4d2SKip Macy u_long ret; 119589e0f4d2SKip Macy 1196931eeffaSKenneth D. Merry m0 = m = m_prev = xennet_get_rx_mbuf(np, *cons); 119783b92f6eSKip Macy 119889e0f4d2SKip Macy if (rx->flags & NETRXF_extra_info) { 1199931eeffaSKenneth D. Merry err = xennet_get_extras(np, extras, rp, cons); 120089e0f4d2SKip Macy } 120189e0f4d2SKip Macy 120283b92f6eSKip Macy if (m0 != NULL) { 120383b92f6eSKip Macy m0->m_pkthdr.len = 0; 120483b92f6eSKip Macy m0->m_next = NULL; 120583b92f6eSKip Macy } 120683b92f6eSKip Macy 120789e0f4d2SKip Macy for (;;) { 120883b92f6eSKip Macy #if 0 1209227ca257SKip Macy DPRINTK("rx->status=%hd rx->offset=%hu frags=%u\n", 121083b92f6eSKip Macy rx->status, rx->offset, frags); 121183b92f6eSKip Macy #endif 121276acc41fSJustin T. Gibbs if (__predict_false(rx->status < 0 || 121389e0f4d2SKip Macy rx->offset + rx->status > PAGE_SIZE)) { 1214931eeffaSKenneth D. Merry 121589e0f4d2SKip Macy #if 0 121689e0f4d2SKip Macy if (net_ratelimit()) 121789e0f4d2SKip Macy WPRINTK("rx->offset: %x, size: %u\n", 121889e0f4d2SKip Macy rx->offset, rx->status); 121989e0f4d2SKip Macy #endif 122089e0f4d2SKip Macy xennet_move_rx_slot(np, m, ref); 1221931eeffaSKenneth D. Merry if (m0 == m) 1222931eeffaSKenneth D. Merry m0 = NULL; 1223931eeffaSKenneth D. Merry m = NULL; 1224931eeffaSKenneth D. Merry err = EINVAL; 1225931eeffaSKenneth D. Merry goto next_skip_queue; 122689e0f4d2SKip Macy } 122789e0f4d2SKip Macy 122889e0f4d2SKip Macy /* 122989e0f4d2SKip Macy * This definitely indicates a bug, either in this driver or in 123089e0f4d2SKip Macy * the backend driver. In future this should flag the bad 123189e0f4d2SKip Macy * situation to the system controller to reboot the backed. 123289e0f4d2SKip Macy */ 1233ff662b5cSJustin T. Gibbs if (ref == GRANT_REF_INVALID) { 1234931eeffaSKenneth D. Merry 123589e0f4d2SKip Macy #if 0 123689e0f4d2SKip Macy if (net_ratelimit()) 123789e0f4d2SKip Macy WPRINTK("Bad rx response id %d.\n", rx->id); 123889e0f4d2SKip Macy #endif 1239ff662b5cSJustin T. Gibbs printf("%s: Bad rx response id %d.\n", __func__,rx->id); 1240931eeffaSKenneth D. Merry err = EINVAL; 124189e0f4d2SKip Macy goto next; 124289e0f4d2SKip Macy } 124389e0f4d2SKip Macy 1244920ba15bSKip Macy ret = gnttab_end_foreign_access_ref(ref); 1245*d0f3a8b9SRoger Pau Monné KASSERT(ret, ("Unable to end access to grant references")); 124689e0f4d2SKip Macy 124789e0f4d2SKip Macy gnttab_release_grant_reference(&np->gref_rx_head, ref); 124889e0f4d2SKip Macy 124989e0f4d2SKip Macy next: 12503a539122SAdrian Chadd if (m == NULL) 12513a539122SAdrian Chadd break; 12523a539122SAdrian Chadd 125383b92f6eSKip Macy m->m_len = rx->status; 125483b92f6eSKip Macy m->m_data += rx->offset; 125583b92f6eSKip Macy m0->m_pkthdr.len += rx->status; 125683b92f6eSKip Macy 1257931eeffaSKenneth D. Merry next_skip_queue: 125889e0f4d2SKip Macy if (!(rx->flags & NETRXF_more_data)) 125989e0f4d2SKip Macy break; 126089e0f4d2SKip Macy 1261931eeffaSKenneth D. Merry if (*cons + frags == rp) { 126289e0f4d2SKip Macy if (net_ratelimit()) 126389e0f4d2SKip Macy WPRINTK("Need more frags\n"); 1264931eeffaSKenneth D. Merry err = ENOENT; 1265931eeffaSKenneth D. Merry printf("%s: cons %u frags %u rp %u, not enough frags\n", 1266931eeffaSKenneth D. Merry __func__, *cons, frags, rp); 126789e0f4d2SKip Macy break; 126889e0f4d2SKip Macy } 1269931eeffaSKenneth D. Merry /* 1270931eeffaSKenneth D. Merry * Note that m can be NULL, if rx->status < 0 or if 1271931eeffaSKenneth D. Merry * rx->offset + rx->status > PAGE_SIZE above. 1272931eeffaSKenneth D. Merry */ 127383b92f6eSKip Macy m_prev = m; 127489e0f4d2SKip Macy 1275931eeffaSKenneth D. Merry rx = RING_GET_RESPONSE(&np->rx, *cons + frags); 1276931eeffaSKenneth D. Merry m = xennet_get_rx_mbuf(np, *cons + frags); 127783b92f6eSKip Macy 1278931eeffaSKenneth D. Merry /* 1279931eeffaSKenneth D. Merry * m_prev == NULL can happen if rx->status < 0 or if 1280931eeffaSKenneth D. Merry * rx->offset + * rx->status > PAGE_SIZE above. 1281931eeffaSKenneth D. Merry */ 1282931eeffaSKenneth D. Merry if (m_prev != NULL) 128383b92f6eSKip Macy m_prev->m_next = m; 1284931eeffaSKenneth D. Merry 1285931eeffaSKenneth D. Merry /* 1286931eeffaSKenneth D. Merry * m0 can be NULL if rx->status < 0 or if * rx->offset + 1287931eeffaSKenneth D. Merry * rx->status > PAGE_SIZE above. 1288931eeffaSKenneth D. Merry */ 1289931eeffaSKenneth D. Merry if (m0 == NULL) 1290931eeffaSKenneth D. Merry m0 = m; 129183b92f6eSKip Macy m->m_next = NULL; 1292931eeffaSKenneth D. Merry ref = xennet_get_rx_ref(np, *cons + frags); 1293931eeffaSKenneth D. Merry ref_cons = *cons + frags; 129489e0f4d2SKip Macy frags++; 129589e0f4d2SKip Macy } 129683b92f6eSKip Macy *list = m0; 1297931eeffaSKenneth D. Merry *cons += frags; 129889e0f4d2SKip Macy 12998577146eSJustin T. Gibbs return (err); 130089e0f4d2SKip Macy } 130189e0f4d2SKip Macy 130289e0f4d2SKip Macy static void 130389e0f4d2SKip Macy xn_tick_locked(struct netfront_info *sc) 130489e0f4d2SKip Macy { 130589e0f4d2SKip Macy XN_RX_LOCK_ASSERT(sc); 130689e0f4d2SKip Macy callout_reset(&sc->xn_stat_ch, hz, xn_tick, sc); 130789e0f4d2SKip Macy 130889e0f4d2SKip Macy /* XXX placeholder for printing debug information */ 130989e0f4d2SKip Macy } 131089e0f4d2SKip Macy 131189e0f4d2SKip Macy static void 131289e0f4d2SKip Macy xn_tick(void *xsc) 131389e0f4d2SKip Macy { 131489e0f4d2SKip Macy struct netfront_info *sc; 131589e0f4d2SKip Macy 131689e0f4d2SKip Macy sc = xsc; 131789e0f4d2SKip Macy XN_RX_LOCK(sc); 131889e0f4d2SKip Macy xn_tick_locked(sc); 131989e0f4d2SKip Macy XN_RX_UNLOCK(sc); 132089e0f4d2SKip Macy } 132189e0f4d2SKip Macy 1322931eeffaSKenneth D. Merry /** 1323931eeffaSKenneth D. Merry * \brief Count the number of fragments in an mbuf chain. 1324931eeffaSKenneth D. Merry * 1325931eeffaSKenneth D. Merry * Surprisingly, there isn't an M* macro for this. 1326c099cafaSAdrian Chadd */ 1327931eeffaSKenneth D. Merry static inline int 1328931eeffaSKenneth D. Merry xn_count_frags(struct mbuf *m) 1329931eeffaSKenneth D. Merry { 1330931eeffaSKenneth D. Merry int nfrags; 1331931eeffaSKenneth D. Merry 1332931eeffaSKenneth D. Merry for (nfrags = 0; m != NULL; m = m->m_next) 1333931eeffaSKenneth D. Merry nfrags++; 1334931eeffaSKenneth D. Merry 1335931eeffaSKenneth D. Merry return (nfrags); 133689e0f4d2SKip Macy } 133789e0f4d2SKip Macy 1338931eeffaSKenneth D. Merry /** 1339931eeffaSKenneth D. Merry * Given an mbuf chain, make sure we have enough room and then push 1340931eeffaSKenneth D. Merry * it onto the transmit ring. 1341931eeffaSKenneth D. Merry */ 1342931eeffaSKenneth D. Merry static int 1343931eeffaSKenneth D. Merry xn_assemble_tx_request(struct netfront_info *sc, struct mbuf *m_head) 1344931eeffaSKenneth D. Merry { 1345931eeffaSKenneth D. Merry struct ifnet *ifp; 1346931eeffaSKenneth D. Merry struct mbuf *m; 1347931eeffaSKenneth D. Merry u_int nfrags; 1348931eeffaSKenneth D. Merry int otherend_id; 1349931eeffaSKenneth D. Merry 1350931eeffaSKenneth D. Merry ifp = sc->xn_ifp; 1351931eeffaSKenneth D. Merry 1352931eeffaSKenneth D. Merry /** 135312678024SDoug Rabson * Defragment the mbuf if necessary. 135412678024SDoug Rabson */ 1355931eeffaSKenneth D. Merry nfrags = xn_count_frags(m_head); 1356931eeffaSKenneth D. Merry 1357931eeffaSKenneth D. Merry /* 1358931eeffaSKenneth D. Merry * Check to see whether this request is longer than netback 1359931eeffaSKenneth D. Merry * can handle, and try to defrag it. 1360931eeffaSKenneth D. Merry */ 1361931eeffaSKenneth D. Merry /** 1362931eeffaSKenneth D. Merry * It is a bit lame, but the netback driver in Linux can't 1363931eeffaSKenneth D. Merry * deal with nfrags > MAX_TX_REQ_FRAGS, which is a quirk of 1364931eeffaSKenneth D. Merry * the Linux network stack. 1365931eeffaSKenneth D. Merry */ 1366578e4bf7SJustin T. Gibbs if (nfrags > sc->maxfrags) { 1367c6499eccSGleb Smirnoff m = m_defrag(m_head, M_NOWAIT); 136812678024SDoug Rabson if (!m) { 1369931eeffaSKenneth D. Merry /* 1370931eeffaSKenneth D. Merry * Defrag failed, so free the mbuf and 1371931eeffaSKenneth D. Merry * therefore drop the packet. 1372931eeffaSKenneth D. Merry */ 137312678024SDoug Rabson m_freem(m_head); 1374931eeffaSKenneth D. Merry return (EMSGSIZE); 137512678024SDoug Rabson } 137612678024SDoug Rabson m_head = m; 137712678024SDoug Rabson } 137889e0f4d2SKip Macy 1379a4ec37f5SAdrian Chadd /* Determine how many fragments now exist */ 1380931eeffaSKenneth D. Merry nfrags = xn_count_frags(m_head); 1381a4ec37f5SAdrian Chadd 1382a4ec37f5SAdrian Chadd /* 1383931eeffaSKenneth D. Merry * Check to see whether the defragmented packet has too many 1384931eeffaSKenneth D. Merry * segments for the Linux netback driver. 1385a4ec37f5SAdrian Chadd */ 1386931eeffaSKenneth D. Merry /** 1387931eeffaSKenneth D. Merry * The FreeBSD TCP stack, with TSO enabled, can produce a chain 1388931eeffaSKenneth D. Merry * of mbufs longer than Linux can handle. Make sure we don't 1389931eeffaSKenneth D. Merry * pass a too-long chain over to the other side by dropping the 1390931eeffaSKenneth D. Merry * packet. It doesn't look like there is currently a way to 1391931eeffaSKenneth D. Merry * tell the TCP stack to generate a shorter chain of packets. 13923fb28bbbSAdrian Chadd */ 1393931eeffaSKenneth D. Merry if (nfrags > MAX_TX_REQ_FRAGS) { 1394ff662b5cSJustin T. Gibbs #ifdef DEBUG 1395ff662b5cSJustin T. Gibbs printf("%s: nfrags %d > MAX_TX_REQ_FRAGS %d, netback " 1396ff662b5cSJustin T. Gibbs "won't be able to handle it, dropping\n", 1397ff662b5cSJustin T. Gibbs __func__, nfrags, MAX_TX_REQ_FRAGS); 1398ff662b5cSJustin T. Gibbs #endif 1399931eeffaSKenneth D. Merry m_freem(m_head); 1400931eeffaSKenneth D. Merry return (EMSGSIZE); 1401a4ec37f5SAdrian Chadd } 1402a4ec37f5SAdrian Chadd 14033fb28bbbSAdrian Chadd /* 1404931eeffaSKenneth D. Merry * This check should be redundant. We've already verified that we 1405931eeffaSKenneth D. Merry * have enough slots in the ring to handle a packet of maximum 1406931eeffaSKenneth D. Merry * size, and that our packet is less than the maximum size. Keep 1407931eeffaSKenneth D. Merry * it in here as an assert for now just to make certain that 1408931eeffaSKenneth D. Merry * xn_tx_chain_cnt is accurate. 14093fb28bbbSAdrian Chadd */ 1410931eeffaSKenneth D. Merry KASSERT((sc->xn_cdata.xn_tx_chain_cnt + nfrags) <= NET_TX_RING_SIZE, 1411931eeffaSKenneth D. Merry ("%s: xn_tx_chain_cnt (%d) + nfrags (%d) > NET_TX_RING_SIZE " 1412931eeffaSKenneth D. Merry "(%d)!", __func__, (int) sc->xn_cdata.xn_tx_chain_cnt, 1413931eeffaSKenneth D. Merry (int) nfrags, (int) NET_TX_RING_SIZE)); 1414a4ec37f5SAdrian Chadd 141589e0f4d2SKip Macy /* 141689e0f4d2SKip Macy * Start packing the mbufs in this chain into 141789e0f4d2SKip Macy * the fragment pointers. Stop when we run out 141889e0f4d2SKip Macy * of fragments or hit the end of the mbuf chain. 141989e0f4d2SKip Macy */ 142012678024SDoug Rabson m = m_head; 1421931eeffaSKenneth D. Merry otherend_id = xenbus_get_otherend_id(sc->xbdev); 142212678024SDoug Rabson for (m = m_head; m; m = m->m_next) { 1423931eeffaSKenneth D. Merry netif_tx_request_t *tx; 1424931eeffaSKenneth D. Merry uintptr_t id; 1425931eeffaSKenneth D. Merry grant_ref_t ref; 1426931eeffaSKenneth D. Merry u_long mfn; /* XXX Wrong type? */ 1427931eeffaSKenneth D. Merry 1428931eeffaSKenneth D. Merry tx = RING_GET_REQUEST(&sc->tx, sc->tx.req_prod_pvt); 1429931eeffaSKenneth D. Merry id = get_id_from_freelist(sc->tx_mbufs); 1430a4ec37f5SAdrian Chadd if (id == 0) 14316f9767acSMarius Strobl panic("%s: was allocated the freelist head!\n", 14326f9767acSMarius Strobl __func__); 1433a4ec37f5SAdrian Chadd sc->xn_cdata.xn_tx_chain_cnt++; 1434931eeffaSKenneth D. Merry if (sc->xn_cdata.xn_tx_chain_cnt > NET_TX_RING_SIZE) 14356f9767acSMarius Strobl panic("%s: tx_chain_cnt must be <= NET_TX_RING_SIZE\n", 14366f9767acSMarius Strobl __func__); 1437931eeffaSKenneth D. Merry sc->tx_mbufs[id] = m; 143889e0f4d2SKip Macy tx->id = id; 143989e0f4d2SKip Macy ref = gnttab_claim_grant_reference(&sc->gref_tx_head); 144089e0f4d2SKip Macy KASSERT((short)ref >= 0, ("Negative ref")); 144112678024SDoug Rabson mfn = virt_to_mfn(mtod(m, vm_offset_t)); 144223dc5621SKip Macy gnttab_grant_foreign_access_ref(ref, otherend_id, 144389e0f4d2SKip Macy mfn, GNTMAP_readonly); 144489e0f4d2SKip Macy tx->gref = sc->grant_tx_ref[id] = ref; 144512678024SDoug Rabson tx->offset = mtod(m, vm_offset_t) & (PAGE_SIZE - 1); 144689e0f4d2SKip Macy tx->flags = 0; 144712678024SDoug Rabson if (m == m_head) { 144812678024SDoug Rabson /* 144912678024SDoug Rabson * The first fragment has the entire packet 145012678024SDoug Rabson * size, subsequent fragments have just the 145112678024SDoug Rabson * fragment size. The backend works out the 145212678024SDoug Rabson * true size of the first fragment by 145312678024SDoug Rabson * subtracting the sizes of the other 145412678024SDoug Rabson * fragments. 145512678024SDoug Rabson */ 145612678024SDoug Rabson tx->size = m->m_pkthdr.len; 145789e0f4d2SKip Macy 145812678024SDoug Rabson /* 1459931eeffaSKenneth D. Merry * The first fragment contains the checksum flags 1460931eeffaSKenneth D. Merry * and is optionally followed by extra data for 1461931eeffaSKenneth D. Merry * TSO etc. 1462931eeffaSKenneth D. Merry */ 1463931eeffaSKenneth D. Merry /** 1464931eeffaSKenneth D. Merry * CSUM_TSO requires checksum offloading. 1465931eeffaSKenneth D. Merry * Some versions of FreeBSD fail to 1466931eeffaSKenneth D. Merry * set CSUM_TCP in the CSUM_TSO case, 1467931eeffaSKenneth D. Merry * so we have to test for CSUM_TSO 1468931eeffaSKenneth D. Merry * explicitly. 146912678024SDoug Rabson */ 147012678024SDoug Rabson if (m->m_pkthdr.csum_flags 1471931eeffaSKenneth D. Merry & (CSUM_DELAY_DATA | CSUM_TSO)) { 147212678024SDoug Rabson tx->flags |= (NETTXF_csum_blank 147312678024SDoug Rabson | NETTXF_data_validated); 147412678024SDoug Rabson } 147512678024SDoug Rabson #if __FreeBSD_version >= 700000 147612678024SDoug Rabson if (m->m_pkthdr.csum_flags & CSUM_TSO) { 147712678024SDoug Rabson struct netif_extra_info *gso = 147812678024SDoug Rabson (struct netif_extra_info *) 1479931eeffaSKenneth D. Merry RING_GET_REQUEST(&sc->tx, 1480931eeffaSKenneth D. Merry ++sc->tx.req_prod_pvt); 148189e0f4d2SKip Macy 148212678024SDoug Rabson tx->flags |= NETTXF_extra_info; 148389e0f4d2SKip Macy 148412678024SDoug Rabson gso->u.gso.size = m->m_pkthdr.tso_segsz; 148512678024SDoug Rabson gso->u.gso.type = 148612678024SDoug Rabson XEN_NETIF_GSO_TYPE_TCPV4; 148712678024SDoug Rabson gso->u.gso.pad = 0; 148812678024SDoug Rabson gso->u.gso.features = 0; 148912678024SDoug Rabson 149012678024SDoug Rabson gso->type = XEN_NETIF_EXTRA_TYPE_GSO; 149112678024SDoug Rabson gso->flags = 0; 149212678024SDoug Rabson } 149312678024SDoug Rabson #endif 149412678024SDoug Rabson } else { 149512678024SDoug Rabson tx->size = m->m_len; 149612678024SDoug Rabson } 1497931eeffaSKenneth D. Merry if (m->m_next) 149812678024SDoug Rabson tx->flags |= NETTXF_more_data; 149912678024SDoug Rabson 1500931eeffaSKenneth D. Merry sc->tx.req_prod_pvt++; 1501931eeffaSKenneth D. Merry } 150212678024SDoug Rabson BPF_MTAP(ifp, m_head); 150312678024SDoug Rabson 150412678024SDoug Rabson sc->stats.tx_bytes += m_head->m_pkthdr.len; 150589e0f4d2SKip Macy sc->stats.tx_packets++; 1506931eeffaSKenneth D. Merry 1507931eeffaSKenneth D. Merry return (0); 150889e0f4d2SKip Macy } 150989e0f4d2SKip Macy 1510931eeffaSKenneth D. Merry static void 1511931eeffaSKenneth D. Merry xn_start_locked(struct ifnet *ifp) 1512931eeffaSKenneth D. Merry { 1513931eeffaSKenneth D. Merry struct netfront_info *sc; 1514931eeffaSKenneth D. Merry struct mbuf *m_head; 1515931eeffaSKenneth D. Merry int notify; 1516931eeffaSKenneth D. Merry 1517931eeffaSKenneth D. Merry sc = ifp->if_softc; 1518931eeffaSKenneth D. Merry 1519931eeffaSKenneth D. Merry if (!netfront_carrier_ok(sc)) 1520931eeffaSKenneth D. Merry return; 1521931eeffaSKenneth D. Merry 1522931eeffaSKenneth D. Merry /* 1523931eeffaSKenneth D. Merry * While we have enough transmit slots available for at least one 1524931eeffaSKenneth D. Merry * maximum-sized packet, pull mbufs off the queue and put them on 1525931eeffaSKenneth D. Merry * the transmit ring. 1526931eeffaSKenneth D. Merry */ 1527931eeffaSKenneth D. Merry while (xn_tx_slot_available(sc)) { 1528931eeffaSKenneth D. Merry IF_DEQUEUE(&ifp->if_snd, m_head); 1529931eeffaSKenneth D. Merry if (m_head == NULL) 1530931eeffaSKenneth D. Merry break; 1531931eeffaSKenneth D. Merry 1532931eeffaSKenneth D. Merry if (xn_assemble_tx_request(sc, m_head) != 0) 1533931eeffaSKenneth D. Merry break; 1534931eeffaSKenneth D. Merry } 1535931eeffaSKenneth D. Merry 153689e0f4d2SKip Macy RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->tx, notify); 153789e0f4d2SKip Macy if (notify) 153876acc41fSJustin T. Gibbs xen_intr_signal(sc->xen_intr_handle); 153989e0f4d2SKip Macy 154089e0f4d2SKip Macy if (RING_FULL(&sc->tx)) { 154189e0f4d2SKip Macy sc->tx_full = 1; 154289e0f4d2SKip Macy #if 0 154389e0f4d2SKip Macy netif_stop_queue(dev); 154489e0f4d2SKip Macy #endif 154589e0f4d2SKip Macy } 154689e0f4d2SKip Macy } 154789e0f4d2SKip Macy 154889e0f4d2SKip Macy static void 154989e0f4d2SKip Macy xn_start(struct ifnet *ifp) 155089e0f4d2SKip Macy { 155189e0f4d2SKip Macy struct netfront_info *sc; 155289e0f4d2SKip Macy sc = ifp->if_softc; 155389e0f4d2SKip Macy XN_TX_LOCK(sc); 155489e0f4d2SKip Macy xn_start_locked(ifp); 155589e0f4d2SKip Macy XN_TX_UNLOCK(sc); 155689e0f4d2SKip Macy } 155789e0f4d2SKip Macy 155889e0f4d2SKip Macy /* equivalent of network_open() in Linux */ 155989e0f4d2SKip Macy static void 156089e0f4d2SKip Macy xn_ifinit_locked(struct netfront_info *sc) 156189e0f4d2SKip Macy { 156289e0f4d2SKip Macy struct ifnet *ifp; 156389e0f4d2SKip Macy 156489e0f4d2SKip Macy XN_LOCK_ASSERT(sc); 156589e0f4d2SKip Macy 156689e0f4d2SKip Macy ifp = sc->xn_ifp; 156789e0f4d2SKip Macy 156889e0f4d2SKip Macy if (ifp->if_drv_flags & IFF_DRV_RUNNING) 156989e0f4d2SKip Macy return; 157089e0f4d2SKip Macy 157189e0f4d2SKip Macy xn_stop(sc); 157289e0f4d2SKip Macy 157389e0f4d2SKip Macy network_alloc_rx_buffers(sc); 157489e0f4d2SKip Macy sc->rx.sring->rsp_event = sc->rx.rsp_cons + 1; 157589e0f4d2SKip Macy 157689e0f4d2SKip Macy ifp->if_drv_flags |= IFF_DRV_RUNNING; 157789e0f4d2SKip Macy ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 15780e509842SJustin T. Gibbs if_link_state_change(ifp, LINK_STATE_UP); 157989e0f4d2SKip Macy 158089e0f4d2SKip Macy callout_reset(&sc->xn_stat_ch, hz, xn_tick, sc); 158189e0f4d2SKip Macy } 158289e0f4d2SKip Macy 158389e0f4d2SKip Macy static void 158489e0f4d2SKip Macy xn_ifinit(void *xsc) 158589e0f4d2SKip Macy { 158689e0f4d2SKip Macy struct netfront_info *sc = xsc; 158789e0f4d2SKip Macy 158889e0f4d2SKip Macy XN_LOCK(sc); 158989e0f4d2SKip Macy xn_ifinit_locked(sc); 159089e0f4d2SKip Macy XN_UNLOCK(sc); 159189e0f4d2SKip Macy } 159289e0f4d2SKip Macy 159389e0f4d2SKip Macy static int 159489e0f4d2SKip Macy xn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 159589e0f4d2SKip Macy { 159689e0f4d2SKip Macy struct netfront_info *sc = ifp->if_softc; 159789e0f4d2SKip Macy struct ifreq *ifr = (struct ifreq *) data; 1598a0ae8f04SBjoern A. Zeeb #ifdef INET 159989e0f4d2SKip Macy struct ifaddr *ifa = (struct ifaddr *)data; 1600a0ae8f04SBjoern A. Zeeb #endif 160189e0f4d2SKip Macy 160289e0f4d2SKip Macy int mask, error = 0; 160389e0f4d2SKip Macy switch(cmd) { 160489e0f4d2SKip Macy case SIOCSIFADDR: 1605a0ae8f04SBjoern A. Zeeb #ifdef INET 160689e0f4d2SKip Macy XN_LOCK(sc); 160789e0f4d2SKip Macy if (ifa->ifa_addr->sa_family == AF_INET) { 160889e0f4d2SKip Macy ifp->if_flags |= IFF_UP; 160989e0f4d2SKip Macy if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 161089e0f4d2SKip Macy xn_ifinit_locked(sc); 161189e0f4d2SKip Macy arp_ifinit(ifp, ifa); 161289e0f4d2SKip Macy XN_UNLOCK(sc); 161349906218SDoug Rabson } else { 161449906218SDoug Rabson XN_UNLOCK(sc); 1615a0ae8f04SBjoern A. Zeeb #endif 161649906218SDoug Rabson error = ether_ioctl(ifp, cmd, data); 1617a0ae8f04SBjoern A. Zeeb #ifdef INET 161849906218SDoug Rabson } 1619a0ae8f04SBjoern A. Zeeb #endif 162089e0f4d2SKip Macy break; 162189e0f4d2SKip Macy case SIOCSIFMTU: 162289e0f4d2SKip Macy /* XXX can we alter the MTU on a VN ?*/ 162389e0f4d2SKip Macy #ifdef notyet 162489e0f4d2SKip Macy if (ifr->ifr_mtu > XN_JUMBO_MTU) 162589e0f4d2SKip Macy error = EINVAL; 162689e0f4d2SKip Macy else 162789e0f4d2SKip Macy #endif 162889e0f4d2SKip Macy { 162989e0f4d2SKip Macy ifp->if_mtu = ifr->ifr_mtu; 163089e0f4d2SKip Macy ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 163189e0f4d2SKip Macy xn_ifinit(sc); 163289e0f4d2SKip Macy } 163389e0f4d2SKip Macy break; 163489e0f4d2SKip Macy case SIOCSIFFLAGS: 163589e0f4d2SKip Macy XN_LOCK(sc); 163689e0f4d2SKip Macy if (ifp->if_flags & IFF_UP) { 163789e0f4d2SKip Macy /* 163889e0f4d2SKip Macy * If only the state of the PROMISC flag changed, 163989e0f4d2SKip Macy * then just use the 'set promisc mode' command 164089e0f4d2SKip Macy * instead of reinitializing the entire NIC. Doing 164189e0f4d2SKip Macy * a full re-init means reloading the firmware and 164289e0f4d2SKip Macy * waiting for it to start up, which may take a 164389e0f4d2SKip Macy * second or two. 164489e0f4d2SKip Macy */ 164589e0f4d2SKip Macy #ifdef notyet 164689e0f4d2SKip Macy /* No promiscuous mode with Xen */ 164789e0f4d2SKip Macy if (ifp->if_drv_flags & IFF_DRV_RUNNING && 164889e0f4d2SKip Macy ifp->if_flags & IFF_PROMISC && 164989e0f4d2SKip Macy !(sc->xn_if_flags & IFF_PROMISC)) { 165089e0f4d2SKip Macy XN_SETBIT(sc, XN_RX_MODE, 165189e0f4d2SKip Macy XN_RXMODE_RX_PROMISC); 165289e0f4d2SKip Macy } else if (ifp->if_drv_flags & IFF_DRV_RUNNING && 165389e0f4d2SKip Macy !(ifp->if_flags & IFF_PROMISC) && 165489e0f4d2SKip Macy sc->xn_if_flags & IFF_PROMISC) { 165589e0f4d2SKip Macy XN_CLRBIT(sc, XN_RX_MODE, 165689e0f4d2SKip Macy XN_RXMODE_RX_PROMISC); 165789e0f4d2SKip Macy } else 165889e0f4d2SKip Macy #endif 165989e0f4d2SKip Macy xn_ifinit_locked(sc); 166089e0f4d2SKip Macy } else { 166189e0f4d2SKip Macy if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 166289e0f4d2SKip Macy xn_stop(sc); 166389e0f4d2SKip Macy } 166489e0f4d2SKip Macy } 166589e0f4d2SKip Macy sc->xn_if_flags = ifp->if_flags; 166689e0f4d2SKip Macy XN_UNLOCK(sc); 166789e0f4d2SKip Macy error = 0; 166889e0f4d2SKip Macy break; 166989e0f4d2SKip Macy case SIOCSIFCAP: 167089e0f4d2SKip Macy mask = ifr->ifr_reqcap ^ ifp->if_capenable; 167112678024SDoug Rabson if (mask & IFCAP_TXCSUM) { 167212678024SDoug Rabson if (IFCAP_TXCSUM & ifp->if_capenable) { 167312678024SDoug Rabson ifp->if_capenable &= ~(IFCAP_TXCSUM|IFCAP_TSO4); 167412678024SDoug Rabson ifp->if_hwassist &= ~(CSUM_TCP | CSUM_UDP 167512678024SDoug Rabson | CSUM_IP | CSUM_TSO); 167612678024SDoug Rabson } else { 167712678024SDoug Rabson ifp->if_capenable |= IFCAP_TXCSUM; 167812678024SDoug Rabson ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP 167912678024SDoug Rabson | CSUM_IP); 168089e0f4d2SKip Macy } 168112678024SDoug Rabson } 168212678024SDoug Rabson if (mask & IFCAP_RXCSUM) { 168312678024SDoug Rabson ifp->if_capenable ^= IFCAP_RXCSUM; 168412678024SDoug Rabson } 168512678024SDoug Rabson #if __FreeBSD_version >= 700000 168612678024SDoug Rabson if (mask & IFCAP_TSO4) { 168712678024SDoug Rabson if (IFCAP_TSO4 & ifp->if_capenable) { 168812678024SDoug Rabson ifp->if_capenable &= ~IFCAP_TSO4; 168912678024SDoug Rabson ifp->if_hwassist &= ~CSUM_TSO; 169012678024SDoug Rabson } else if (IFCAP_TXCSUM & ifp->if_capenable) { 169112678024SDoug Rabson ifp->if_capenable |= IFCAP_TSO4; 169212678024SDoug Rabson ifp->if_hwassist |= CSUM_TSO; 169312678024SDoug Rabson } else { 16943552092bSAdrian Chadd IPRINTK("Xen requires tx checksum offload" 169512678024SDoug Rabson " be enabled to use TSO\n"); 169612678024SDoug Rabson error = EINVAL; 169712678024SDoug Rabson } 169812678024SDoug Rabson } 169912678024SDoug Rabson if (mask & IFCAP_LRO) { 170012678024SDoug Rabson ifp->if_capenable ^= IFCAP_LRO; 170112678024SDoug Rabson 170212678024SDoug Rabson } 170312678024SDoug Rabson #endif 170489e0f4d2SKip Macy error = 0; 170589e0f4d2SKip Macy break; 170689e0f4d2SKip Macy case SIOCADDMULTI: 170789e0f4d2SKip Macy case SIOCDELMULTI: 170889e0f4d2SKip Macy #ifdef notyet 170989e0f4d2SKip Macy if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 171089e0f4d2SKip Macy XN_LOCK(sc); 171189e0f4d2SKip Macy xn_setmulti(sc); 171289e0f4d2SKip Macy XN_UNLOCK(sc); 171389e0f4d2SKip Macy error = 0; 171489e0f4d2SKip Macy } 171589e0f4d2SKip Macy #endif 171689e0f4d2SKip Macy /* FALLTHROUGH */ 171789e0f4d2SKip Macy case SIOCSIFMEDIA: 171889e0f4d2SKip Macy case SIOCGIFMEDIA: 17190e509842SJustin T. Gibbs error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 172089e0f4d2SKip Macy break; 172189e0f4d2SKip Macy default: 172289e0f4d2SKip Macy error = ether_ioctl(ifp, cmd, data); 172389e0f4d2SKip Macy } 172489e0f4d2SKip Macy 172589e0f4d2SKip Macy return (error); 172689e0f4d2SKip Macy } 172789e0f4d2SKip Macy 172889e0f4d2SKip Macy static void 172989e0f4d2SKip Macy xn_stop(struct netfront_info *sc) 173089e0f4d2SKip Macy { 173189e0f4d2SKip Macy struct ifnet *ifp; 173289e0f4d2SKip Macy 173389e0f4d2SKip Macy XN_LOCK_ASSERT(sc); 173489e0f4d2SKip Macy 173589e0f4d2SKip Macy ifp = sc->xn_ifp; 173689e0f4d2SKip Macy 173789e0f4d2SKip Macy callout_stop(&sc->xn_stat_ch); 173889e0f4d2SKip Macy 173989e0f4d2SKip Macy xn_free_rx_ring(sc); 174089e0f4d2SKip Macy xn_free_tx_ring(sc); 174189e0f4d2SKip Macy 174289e0f4d2SKip Macy ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 17430e509842SJustin T. Gibbs if_link_state_change(ifp, LINK_STATE_DOWN); 174489e0f4d2SKip Macy } 174589e0f4d2SKip Macy 174689e0f4d2SKip Macy /* START of Xenolinux helper functions adapted to FreeBSD */ 174723dc5621SKip Macy int 174823dc5621SKip Macy network_connect(struct netfront_info *np) 174989e0f4d2SKip Macy { 17503a6d1fcfSKip Macy int i, requeue_idx, error; 175189e0f4d2SKip Macy grant_ref_t ref; 175289e0f4d2SKip Macy netif_rx_request_t *req; 1753*d0f3a8b9SRoger Pau Monné u_int feature_rx_copy; 175489e0f4d2SKip Macy 1755ff662b5cSJustin T. Gibbs error = xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 17563a6d1fcfSKip Macy "feature-rx-copy", NULL, "%u", &feature_rx_copy); 17573a6d1fcfSKip Macy if (error) 175889e0f4d2SKip Macy feature_rx_copy = 0; 175989e0f4d2SKip Macy 1760*d0f3a8b9SRoger Pau Monné /* We only support rx copy. */ 1761*d0f3a8b9SRoger Pau Monné if (!feature_rx_copy) 1762*d0f3a8b9SRoger Pau Monné return (EPROTONOSUPPORT); 176389e0f4d2SKip Macy 176489e0f4d2SKip Macy /* Recovery procedure: */ 17653a6d1fcfSKip Macy error = talk_to_backend(np->xbdev, np); 17663a6d1fcfSKip Macy if (error) 17673a6d1fcfSKip Macy return (error); 176889e0f4d2SKip Macy 176989e0f4d2SKip Macy /* Step 1: Reinitialise variables. */ 1770578e4bf7SJustin T. Gibbs xn_query_features(np); 1771578e4bf7SJustin T. Gibbs xn_configure_features(np); 177289e0f4d2SKip Macy netif_release_tx_bufs(np); 177389e0f4d2SKip Macy 177489e0f4d2SKip Macy /* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */ 177589e0f4d2SKip Macy for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) { 177689e0f4d2SKip Macy struct mbuf *m; 17773a6d1fcfSKip Macy u_long pfn; 177889e0f4d2SKip Macy 177989e0f4d2SKip Macy if (np->rx_mbufs[i] == NULL) 178089e0f4d2SKip Macy continue; 178189e0f4d2SKip Macy 178289e0f4d2SKip Macy m = np->rx_mbufs[requeue_idx] = xennet_get_rx_mbuf(np, i); 178389e0f4d2SKip Macy ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i); 1784931eeffaSKenneth D. Merry 178589e0f4d2SKip Macy req = RING_GET_REQUEST(&np->rx, requeue_idx); 17863a6d1fcfSKip Macy pfn = vtophys(mtod(m, vm_offset_t)) >> PAGE_SHIFT; 178789e0f4d2SKip Macy 178889e0f4d2SKip Macy gnttab_grant_foreign_access_ref(ref, 178923dc5621SKip Macy xenbus_get_otherend_id(np->xbdev), 1790ed95805eSJohn Baldwin pfn, 0); 1791*d0f3a8b9SRoger Pau Monné 179289e0f4d2SKip Macy req->gref = ref; 179389e0f4d2SKip Macy req->id = requeue_idx; 179489e0f4d2SKip Macy 179589e0f4d2SKip Macy requeue_idx++; 179689e0f4d2SKip Macy } 179789e0f4d2SKip Macy 179889e0f4d2SKip Macy np->rx.req_prod_pvt = requeue_idx; 179989e0f4d2SKip Macy 180089e0f4d2SKip Macy /* Step 3: All public and private state should now be sane. Get 180189e0f4d2SKip Macy * ready to start sending and receiving packets and give the driver 180289e0f4d2SKip Macy * domain a kick because we've probably just requeued some 180389e0f4d2SKip Macy * packets. 180489e0f4d2SKip Macy */ 180589e0f4d2SKip Macy netfront_carrier_on(np); 180676acc41fSJustin T. Gibbs xen_intr_signal(np->xen_intr_handle); 180789e0f4d2SKip Macy XN_TX_LOCK(np); 180889e0f4d2SKip Macy xn_txeof(np); 180989e0f4d2SKip Macy XN_TX_UNLOCK(np); 181089e0f4d2SKip Macy network_alloc_rx_buffers(np); 181189e0f4d2SKip Macy 181289e0f4d2SKip Macy return (0); 181389e0f4d2SKip Macy } 181489e0f4d2SKip Macy 181589e0f4d2SKip Macy static void 1816578e4bf7SJustin T. Gibbs xn_query_features(struct netfront_info *np) 1817578e4bf7SJustin T. Gibbs { 1818578e4bf7SJustin T. Gibbs int val; 1819578e4bf7SJustin T. Gibbs 1820578e4bf7SJustin T. Gibbs device_printf(np->xbdev, "backend features:"); 1821578e4bf7SJustin T. Gibbs 1822578e4bf7SJustin T. Gibbs if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 1823578e4bf7SJustin T. Gibbs "feature-sg", NULL, "%d", &val) < 0) 1824578e4bf7SJustin T. Gibbs val = 0; 1825578e4bf7SJustin T. Gibbs 1826578e4bf7SJustin T. Gibbs np->maxfrags = 1; 1827578e4bf7SJustin T. Gibbs if (val) { 1828578e4bf7SJustin T. Gibbs np->maxfrags = MAX_TX_REQ_FRAGS; 1829578e4bf7SJustin T. Gibbs printf(" feature-sg"); 1830578e4bf7SJustin T. Gibbs } 1831578e4bf7SJustin T. Gibbs 1832578e4bf7SJustin T. Gibbs if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 1833578e4bf7SJustin T. Gibbs "feature-gso-tcpv4", NULL, "%d", &val) < 0) 1834578e4bf7SJustin T. Gibbs val = 0; 1835578e4bf7SJustin T. Gibbs 1836578e4bf7SJustin T. Gibbs np->xn_ifp->if_capabilities &= ~(IFCAP_TSO4|IFCAP_LRO); 1837578e4bf7SJustin T. Gibbs if (val) { 1838578e4bf7SJustin T. Gibbs np->xn_ifp->if_capabilities |= IFCAP_TSO4|IFCAP_LRO; 1839578e4bf7SJustin T. Gibbs printf(" feature-gso-tcp4"); 1840578e4bf7SJustin T. Gibbs } 1841578e4bf7SJustin T. Gibbs 1842578e4bf7SJustin T. Gibbs printf("\n"); 1843578e4bf7SJustin T. Gibbs } 1844578e4bf7SJustin T. Gibbs 1845cf9c09e1SJustin T. Gibbs static int 1846578e4bf7SJustin T. Gibbs xn_configure_features(struct netfront_info *np) 1847cf9c09e1SJustin T. Gibbs { 18486a8e9695SRoger Pau Monné int err, cap_enabled; 1849cf9c09e1SJustin T. Gibbs 1850cf9c09e1SJustin T. Gibbs err = 0; 18516a8e9695SRoger Pau Monné 18526a8e9695SRoger Pau Monné if (np->xn_resume && 18536a8e9695SRoger Pau Monné ((np->xn_ifp->if_capenable & np->xn_ifp->if_capabilities) 18546a8e9695SRoger Pau Monné == np->xn_ifp->if_capenable)) { 18556a8e9695SRoger Pau Monné /* Current options are available, no need to do anything. */ 18566a8e9695SRoger Pau Monné return (0); 18576a8e9695SRoger Pau Monné } 18586a8e9695SRoger Pau Monné 18596a8e9695SRoger Pau Monné /* Try to preserve as many options as possible. */ 18606a8e9695SRoger Pau Monné if (np->xn_resume) 18616a8e9695SRoger Pau Monné cap_enabled = np->xn_ifp->if_capenable; 18626a8e9695SRoger Pau Monné else 18636a8e9695SRoger Pau Monné cap_enabled = UINT_MAX; 18646a8e9695SRoger Pau Monné 1865c6b15dd5SGleb Smirnoff #if __FreeBSD_version >= 700000 && (defined(INET) || defined(INET6)) 18666a8e9695SRoger Pau Monné if ((np->xn_ifp->if_capenable & IFCAP_LRO) == (cap_enabled & IFCAP_LRO)) 1867cf9c09e1SJustin T. Gibbs tcp_lro_free(&np->xn_lro); 1868578e4bf7SJustin T. Gibbs #endif 1869578e4bf7SJustin T. Gibbs np->xn_ifp->if_capenable = 18706a8e9695SRoger Pau Monné np->xn_ifp->if_capabilities & ~(IFCAP_LRO|IFCAP_TSO4) & cap_enabled; 1871578e4bf7SJustin T. Gibbs np->xn_ifp->if_hwassist &= ~CSUM_TSO; 1872f909bbb4SGleb Smirnoff #if __FreeBSD_version >= 700000 && (defined(INET) || defined(INET6)) 18736a8e9695SRoger Pau Monné if (xn_enable_lro && (np->xn_ifp->if_capabilities & IFCAP_LRO) == 18746a8e9695SRoger Pau Monné (cap_enabled & IFCAP_LRO)) { 1875cf9c09e1SJustin T. Gibbs err = tcp_lro_init(&np->xn_lro); 1876cf9c09e1SJustin T. Gibbs if (err) { 1877cf9c09e1SJustin T. Gibbs device_printf(np->xbdev, "LRO initialization failed\n"); 1878cf9c09e1SJustin T. Gibbs } else { 1879cf9c09e1SJustin T. Gibbs np->xn_lro.ifp = np->xn_ifp; 1880578e4bf7SJustin T. Gibbs np->xn_ifp->if_capenable |= IFCAP_LRO; 1881cf9c09e1SJustin T. Gibbs } 1882cf9c09e1SJustin T. Gibbs } 18836a8e9695SRoger Pau Monné if ((np->xn_ifp->if_capabilities & IFCAP_TSO4) == 18846a8e9695SRoger Pau Monné (cap_enabled & IFCAP_TSO4)) { 1885578e4bf7SJustin T. Gibbs np->xn_ifp->if_capenable |= IFCAP_TSO4; 1886578e4bf7SJustin T. Gibbs np->xn_ifp->if_hwassist |= CSUM_TSO; 1887578e4bf7SJustin T. Gibbs } 1888cf9c09e1SJustin T. Gibbs #endif 1889cf9c09e1SJustin T. Gibbs return (err); 1890cf9c09e1SJustin T. Gibbs } 1891cf9c09e1SJustin T. Gibbs 189276acc41fSJustin T. Gibbs /** 189376acc41fSJustin T. Gibbs * Create a network device. 189476acc41fSJustin T. Gibbs * @param dev Newbus device representing this virtual NIC. 189589e0f4d2SKip Macy */ 189623dc5621SKip Macy int 189723dc5621SKip Macy create_netdev(device_t dev) 189889e0f4d2SKip Macy { 189989e0f4d2SKip Macy int i; 190089e0f4d2SKip Macy struct netfront_info *np; 190189e0f4d2SKip Macy int err; 190289e0f4d2SKip Macy struct ifnet *ifp; 190389e0f4d2SKip Macy 190423dc5621SKip Macy np = device_get_softc(dev); 190589e0f4d2SKip Macy 190689e0f4d2SKip Macy np->xbdev = dev; 190789e0f4d2SKip Macy 190889e0f4d2SKip Macy XN_LOCK_INIT(np, xennetif); 19090e509842SJustin T. Gibbs 19100e509842SJustin T. Gibbs ifmedia_init(&np->sc_media, 0, xn_ifmedia_upd, xn_ifmedia_sts); 19110e509842SJustin T. Gibbs ifmedia_add(&np->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); 19120e509842SJustin T. Gibbs ifmedia_set(&np->sc_media, IFM_ETHER|IFM_MANUAL); 19130e509842SJustin T. Gibbs 191489e0f4d2SKip Macy np->rx_target = RX_MIN_TARGET; 191589e0f4d2SKip Macy np->rx_min_target = RX_MIN_TARGET; 191689e0f4d2SKip Macy np->rx_max_target = RX_MAX_TARGET; 191789e0f4d2SKip Macy 191889e0f4d2SKip Macy /* Initialise {tx,rx}_skbs to be a free chain containing every entry. */ 191989e0f4d2SKip Macy for (i = 0; i <= NET_TX_RING_SIZE; i++) { 192089e0f4d2SKip Macy np->tx_mbufs[i] = (void *) ((u_long) i+1); 1921ff662b5cSJustin T. Gibbs np->grant_tx_ref[i] = GRANT_REF_INVALID; 192289e0f4d2SKip Macy } 1923931eeffaSKenneth D. Merry np->tx_mbufs[NET_TX_RING_SIZE] = (void *)0; 1924931eeffaSKenneth D. Merry 192589e0f4d2SKip Macy for (i = 0; i <= NET_RX_RING_SIZE; i++) { 1926931eeffaSKenneth D. Merry 192789e0f4d2SKip Macy np->rx_mbufs[i] = NULL; 1928ff662b5cSJustin T. Gibbs np->grant_rx_ref[i] = GRANT_REF_INVALID; 192989e0f4d2SKip Macy } 193049e6be9cSGleb Smirnoff 193149e6be9cSGleb Smirnoff mbufq_init(&np->xn_rx_batch, INT_MAX); 193249e6be9cSGleb Smirnoff 193389e0f4d2SKip Macy /* A grant for every tx ring slot */ 1934931eeffaSKenneth D. Merry if (gnttab_alloc_grant_references(NET_TX_RING_SIZE, 1935931eeffaSKenneth D. Merry &np->gref_tx_head) != 0) { 1936227ca257SKip Macy IPRINTK("#### netfront can't alloc tx grant refs\n"); 193789e0f4d2SKip Macy err = ENOMEM; 193889e0f4d2SKip Macy goto exit; 193989e0f4d2SKip Macy } 194089e0f4d2SKip Macy /* A grant for every rx ring slot */ 194189e0f4d2SKip Macy if (gnttab_alloc_grant_references(RX_MAX_TARGET, 1942931eeffaSKenneth D. Merry &np->gref_rx_head) != 0) { 1943227ca257SKip Macy WPRINTK("#### netfront can't alloc rx grant refs\n"); 194489e0f4d2SKip Macy gnttab_free_grant_references(np->gref_tx_head); 194589e0f4d2SKip Macy err = ENOMEM; 194689e0f4d2SKip Macy goto exit; 194789e0f4d2SKip Macy } 194889e0f4d2SKip Macy 194989e0f4d2SKip Macy err = xen_net_read_mac(dev, np->mac); 1950ffa06904SJustin T. Gibbs if (err) 195189e0f4d2SKip Macy goto out; 195289e0f4d2SKip Macy 195389e0f4d2SKip Macy /* Set up ifnet structure */ 195423dc5621SKip Macy ifp = np->xn_ifp = if_alloc(IFT_ETHER); 195589e0f4d2SKip Macy ifp->if_softc = np; 195623dc5621SKip Macy if_initname(ifp, "xn", device_get_unit(dev)); 19573a6d1fcfSKip Macy ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 195889e0f4d2SKip Macy ifp->if_ioctl = xn_ioctl; 195989e0f4d2SKip Macy ifp->if_output = ether_output; 196089e0f4d2SKip Macy ifp->if_start = xn_start; 1961227ca257SKip Macy #ifdef notyet 1962227ca257SKip Macy ifp->if_watchdog = xn_watchdog; 1963227ca257SKip Macy #endif 196489e0f4d2SKip Macy ifp->if_init = xn_ifinit; 196589e0f4d2SKip Macy ifp->if_snd.ifq_maxlen = NET_TX_RING_SIZE - 1; 196689e0f4d2SKip Macy 196789e0f4d2SKip Macy ifp->if_hwassist = XN_CSUM_FEATURES; 196889e0f4d2SKip Macy ifp->if_capabilities = IFCAP_HWCSUM; 19699fd573c3SHans Petter Selasky ifp->if_hw_tsomax = 65536 - (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN); 19709fd573c3SHans Petter Selasky ifp->if_hw_tsomaxsegcount = MAX_TX_REQ_FRAGS; 19719fd573c3SHans Petter Selasky ifp->if_hw_tsomaxsegsize = PAGE_SIZE; 197289e0f4d2SKip Macy 197389e0f4d2SKip Macy ether_ifattach(ifp, np->mac); 1974fd90e2edSJung-uk Kim callout_init(&np->xn_stat_ch, 1); 197589e0f4d2SKip Macy netfront_carrier_off(np); 197689e0f4d2SKip Macy 197789e0f4d2SKip Macy return (0); 197889e0f4d2SKip Macy 197989e0f4d2SKip Macy exit: 198089e0f4d2SKip Macy gnttab_free_grant_references(np->gref_tx_head); 198189e0f4d2SKip Macy out: 1982ffa06904SJustin T. Gibbs return (err); 198389e0f4d2SKip Macy } 198489e0f4d2SKip Macy 198589e0f4d2SKip Macy /** 198689e0f4d2SKip Macy * Handle the change of state of the backend to Closing. We must delete our 198789e0f4d2SKip Macy * device-layer structures now, to ensure that writes are flushed through to 198889e0f4d2SKip Macy * the backend. Once is this done, we can switch to Closed in 198989e0f4d2SKip Macy * acknowledgement. 199089e0f4d2SKip Macy */ 199189e0f4d2SKip Macy #if 0 19920e509842SJustin T. Gibbs static void 19930e509842SJustin T. Gibbs netfront_closing(device_t dev) 199489e0f4d2SKip Macy { 199589e0f4d2SKip Macy #if 0 199689e0f4d2SKip Macy struct netfront_info *info = dev->dev_driver_data; 199789e0f4d2SKip Macy 199889e0f4d2SKip Macy DPRINTK("netfront_closing: %s removed\n", dev->nodename); 199989e0f4d2SKip Macy 200089e0f4d2SKip Macy close_netdev(info); 200189e0f4d2SKip Macy #endif 200289e0f4d2SKip Macy xenbus_switch_state(dev, XenbusStateClosed); 200389e0f4d2SKip Macy } 200489e0f4d2SKip Macy #endif 200589e0f4d2SKip Macy 20060e509842SJustin T. Gibbs static int 20070e509842SJustin T. Gibbs netfront_detach(device_t dev) 200889e0f4d2SKip Macy { 200923dc5621SKip Macy struct netfront_info *info = device_get_softc(dev); 201089e0f4d2SKip Macy 201123dc5621SKip Macy DPRINTK("%s\n", xenbus_get_node(dev)); 201289e0f4d2SKip Macy 201389e0f4d2SKip Macy netif_free(info); 201489e0f4d2SKip Macy 201589e0f4d2SKip Macy return 0; 201689e0f4d2SKip Macy } 201789e0f4d2SKip Macy 20180e509842SJustin T. Gibbs static void 20190e509842SJustin T. Gibbs netif_free(struct netfront_info *info) 202089e0f4d2SKip Macy { 2021818fe953SJustin T. Gibbs XN_LOCK(info); 2022818fe953SJustin T. Gibbs xn_stop(info); 2023818fe953SJustin T. Gibbs XN_UNLOCK(info); 2024818fe953SJustin T. Gibbs callout_drain(&info->xn_stat_ch); 202589e0f4d2SKip Macy netif_disconnect_backend(info); 2026e3242f9dSJustin T. Gibbs if (info->xn_ifp != NULL) { 2027818fe953SJustin T. Gibbs ether_ifdetach(info->xn_ifp); 2028818fe953SJustin T. Gibbs if_free(info->xn_ifp); 2029e3242f9dSJustin T. Gibbs info->xn_ifp = NULL; 2030e3242f9dSJustin T. Gibbs } 2031d5aeb779SJustin T. Gibbs ifmedia_removeall(&info->sc_media); 203289e0f4d2SKip Macy } 203389e0f4d2SKip Macy 20340e509842SJustin T. Gibbs static void 20350e509842SJustin T. Gibbs netif_disconnect_backend(struct netfront_info *info) 203689e0f4d2SKip Macy { 20373a6d1fcfSKip Macy XN_RX_LOCK(info); 20383a6d1fcfSKip Macy XN_TX_LOCK(info); 20393a6d1fcfSKip Macy netfront_carrier_off(info); 20403a6d1fcfSKip Macy XN_TX_UNLOCK(info); 20413a6d1fcfSKip Macy XN_RX_UNLOCK(info); 20423a6d1fcfSKip Macy 2043cf9c09e1SJustin T. Gibbs free_ring(&info->tx_ring_ref, &info->tx.sring); 2044cf9c09e1SJustin T. Gibbs free_ring(&info->rx_ring_ref, &info->rx.sring); 204589e0f4d2SKip Macy 204676acc41fSJustin T. Gibbs xen_intr_unbind(&info->xen_intr_handle); 204789e0f4d2SKip Macy } 204889e0f4d2SKip Macy 20490e509842SJustin T. Gibbs static void 2050cf9c09e1SJustin T. Gibbs free_ring(int *ref, void *ring_ptr_ref) 205189e0f4d2SKip Macy { 2052cf9c09e1SJustin T. Gibbs void **ring_ptr_ptr = ring_ptr_ref; 2053cf9c09e1SJustin T. Gibbs 2054cf9c09e1SJustin T. Gibbs if (*ref != GRANT_REF_INVALID) { 2055cf9c09e1SJustin T. Gibbs /* This API frees the associated storage. */ 2056cf9c09e1SJustin T. Gibbs gnttab_end_foreign_access(*ref, *ring_ptr_ptr); 2057cf9c09e1SJustin T. Gibbs *ref = GRANT_REF_INVALID; 2058cf9c09e1SJustin T. Gibbs } 2059cf9c09e1SJustin T. Gibbs *ring_ptr_ptr = NULL; 206089e0f4d2SKip Macy } 206189e0f4d2SKip Macy 20620e509842SJustin T. Gibbs static int 20630e509842SJustin T. Gibbs xn_ifmedia_upd(struct ifnet *ifp) 20640e509842SJustin T. Gibbs { 20650e509842SJustin T. Gibbs return (0); 20660e509842SJustin T. Gibbs } 20670e509842SJustin T. Gibbs 20680e509842SJustin T. Gibbs static void 20690e509842SJustin T. Gibbs xn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 20700e509842SJustin T. Gibbs { 20710e509842SJustin T. Gibbs ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE; 20720e509842SJustin T. Gibbs ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; 20730e509842SJustin T. Gibbs } 20740e509842SJustin T. Gibbs 207589e0f4d2SKip Macy /* ** Driver registration ** */ 207623dc5621SKip Macy static device_method_t netfront_methods[] = { 207723dc5621SKip Macy /* Device interface */ 207823dc5621SKip Macy DEVMETHOD(device_probe, netfront_probe), 207923dc5621SKip Macy DEVMETHOD(device_attach, netfront_attach), 208023dc5621SKip Macy DEVMETHOD(device_detach, netfront_detach), 208123dc5621SKip Macy DEVMETHOD(device_shutdown, bus_generic_shutdown), 2082cf9c09e1SJustin T. Gibbs DEVMETHOD(device_suspend, netfront_suspend), 208323dc5621SKip Macy DEVMETHOD(device_resume, netfront_resume), 208489e0f4d2SKip Macy 208523dc5621SKip Macy /* Xenbus interface */ 2086ff662b5cSJustin T. Gibbs DEVMETHOD(xenbus_otherend_changed, netfront_backend_changed), 208789e0f4d2SKip Macy 20886f9767acSMarius Strobl DEVMETHOD_END 208989e0f4d2SKip Macy }; 209089e0f4d2SKip Macy 209123dc5621SKip Macy static driver_t netfront_driver = { 209223dc5621SKip Macy "xn", 209323dc5621SKip Macy netfront_methods, 209423dc5621SKip Macy sizeof(struct netfront_info), 209589e0f4d2SKip Macy }; 209623dc5621SKip Macy devclass_t netfront_devclass; 209789e0f4d2SKip Macy 20986f9767acSMarius Strobl DRIVER_MODULE(xe, xenbusb_front, netfront_driver, netfront_devclass, NULL, 20996f9767acSMarius Strobl NULL); 2100