18e0ad55aSJoel Dahl /*- 289e0f4d2SKip Macy * Copyright (c) 2004-2006 Kip Macy 396375eacSRoger Pau Monné * Copyright (c) 2015 Wei Liu <wei.liu2@citrix.com> 489e0f4d2SKip Macy * All rights reserved. 589e0f4d2SKip Macy * 68e0ad55aSJoel Dahl * Redistribution and use in source and binary forms, with or without 78e0ad55aSJoel Dahl * modification, are permitted provided that the following conditions 88e0ad55aSJoel Dahl * are met: 98e0ad55aSJoel Dahl * 1. Redistributions of source code must retain the above copyright 108e0ad55aSJoel Dahl * notice, this list of conditions and the following disclaimer. 118e0ad55aSJoel Dahl * 2. Redistributions in binary form must reproduce the above copyright 128e0ad55aSJoel Dahl * notice, this list of conditions and the following disclaimer in the 138e0ad55aSJoel Dahl * documentation and/or other materials provided with the distribution. 1489e0f4d2SKip Macy * 158e0ad55aSJoel Dahl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 168e0ad55aSJoel Dahl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 178e0ad55aSJoel Dahl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 188e0ad55aSJoel Dahl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 198e0ad55aSJoel Dahl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 208e0ad55aSJoel Dahl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 218e0ad55aSJoel Dahl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 228e0ad55aSJoel Dahl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 238e0ad55aSJoel Dahl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 248e0ad55aSJoel Dahl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 258e0ad55aSJoel Dahl * SUCH DAMAGE. 2689e0f4d2SKip Macy */ 2789e0f4d2SKip Macy 2889e0f4d2SKip Macy #include <sys/cdefs.h> 2989e0f4d2SKip Macy __FBSDID("$FreeBSD$"); 3089e0f4d2SKip Macy 31a0ae8f04SBjoern A. Zeeb #include "opt_inet.h" 32f909bbb4SGleb Smirnoff #include "opt_inet6.h" 33a0ae8f04SBjoern A. Zeeb 3489e0f4d2SKip Macy #include <sys/param.h> 3589e0f4d2SKip Macy #include <sys/sockio.h> 36c578b6acSGleb Smirnoff #include <sys/limits.h> 3789e0f4d2SKip Macy #include <sys/mbuf.h> 3889e0f4d2SKip Macy #include <sys/malloc.h> 3923dc5621SKip Macy #include <sys/module.h> 4089e0f4d2SKip Macy #include <sys/kernel.h> 4189e0f4d2SKip Macy #include <sys/socket.h> 4212678024SDoug Rabson #include <sys/sysctl.h> 4396375eacSRoger Pau Monné #include <sys/taskqueue.h> 4489e0f4d2SKip Macy 4589e0f4d2SKip Macy #include <net/if.h> 4677374386SGleb Smirnoff #include <net/if_var.h> 4789e0f4d2SKip Macy #include <net/if_arp.h> 4889e0f4d2SKip Macy #include <net/ethernet.h> 4989e0f4d2SKip Macy #include <net/if_media.h> 5089e0f4d2SKip Macy #include <net/bpf.h> 5189e0f4d2SKip Macy #include <net/if_types.h> 5289e0f4d2SKip Macy 5389e0f4d2SKip Macy #include <netinet/in.h> 5489e0f4d2SKip Macy #include <netinet/ip.h> 5589e0f4d2SKip Macy #include <netinet/if_ether.h> 5612678024SDoug Rabson #include <netinet/tcp.h> 5712678024SDoug Rabson #include <netinet/tcp_lro.h> 5889e0f4d2SKip Macy 5989e0f4d2SKip Macy #include <vm/vm.h> 6089e0f4d2SKip Macy #include <vm/pmap.h> 6189e0f4d2SKip Macy 6289e0f4d2SKip Macy #include <sys/bus.h> 6389e0f4d2SKip Macy 6476acc41fSJustin T. Gibbs #include <xen/xen-os.h> 653a6d1fcfSKip Macy #include <xen/hypervisor.h> 663a6d1fcfSKip Macy #include <xen/xen_intr.h> 6789e0f4d2SKip Macy #include <xen/gnttab.h> 6889e0f4d2SKip Macy #include <xen/interface/memory.h> 6989e0f4d2SKip Macy #include <xen/interface/io/netif.h> 7023dc5621SKip Macy #include <xen/xenbus/xenbusvar.h> 7189e0f4d2SKip Macy 7223dc5621SKip Macy #include "xenbus_if.h" 7389e0f4d2SKip Macy 74578e4bf7SJustin T. Gibbs /* Features supported by all backends. TSO and LRO can be negotiated */ 75578e4bf7SJustin T. Gibbs #define XN_CSUM_FEATURES (CSUM_TCP | CSUM_UDP) 7612678024SDoug Rabson 7789e0f4d2SKip Macy #define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE) 7889e0f4d2SKip Macy #define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE) 7989e0f4d2SKip Macy 802568ee67SRoger Pau Monné #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1) 812568ee67SRoger Pau Monné 8212678024SDoug Rabson /* 8312678024SDoug Rabson * Should the driver do LRO on the RX end 8412678024SDoug Rabson * this can be toggled on the fly, but the 8512678024SDoug Rabson * interface must be reset (down/up) for it 8612678024SDoug Rabson * to take effect. 8712678024SDoug Rabson */ 8812678024SDoug Rabson static int xn_enable_lro = 1; 8912678024SDoug Rabson TUNABLE_INT("hw.xn.enable_lro", &xn_enable_lro); 9012678024SDoug Rabson 9196375eacSRoger Pau Monné /* 9296375eacSRoger Pau Monné * Number of pairs of queues. 9396375eacSRoger Pau Monné */ 9496375eacSRoger Pau Monné static unsigned long xn_num_queues = 4; 9596375eacSRoger Pau Monné TUNABLE_ULONG("hw.xn.num_queues", &xn_num_queues); 9696375eacSRoger Pau Monné 97931eeffaSKenneth D. Merry /** 98931eeffaSKenneth D. Merry * \brief The maximum allowed data fragments in a single transmit 99931eeffaSKenneth D. Merry * request. 100931eeffaSKenneth D. Merry * 101931eeffaSKenneth D. Merry * This limit is imposed by the backend driver. We assume here that 102931eeffaSKenneth D. Merry * we are dealing with a Linux driver domain and have set our limit 103931eeffaSKenneth D. Merry * to mirror the Linux MAX_SKB_FRAGS constant. 104931eeffaSKenneth D. Merry */ 105931eeffaSKenneth D. Merry #define MAX_TX_REQ_FRAGS (65536 / PAGE_SIZE + 2) 106931eeffaSKenneth D. Merry 10789e0f4d2SKip Macy #define RX_COPY_THRESHOLD 256 10889e0f4d2SKip Macy 10989e0f4d2SKip Macy #define net_ratelimit() 0 11089e0f4d2SKip Macy 11196375eacSRoger Pau Monné struct netfront_rxq; 11296375eacSRoger Pau Monné struct netfront_txq; 11389e0f4d2SKip Macy struct netfront_info; 11489e0f4d2SKip Macy struct netfront_rx_info; 11589e0f4d2SKip Macy 11696375eacSRoger Pau Monné static void xn_txeof(struct netfront_txq *); 11796375eacSRoger Pau Monné static void xn_rxeof(struct netfront_rxq *); 11896375eacSRoger Pau Monné static void xn_alloc_rx_buffers(struct netfront_rxq *); 1192568ee67SRoger Pau Monné static void xn_alloc_rx_buffers_callout(void *arg); 12089e0f4d2SKip Macy 12196375eacSRoger Pau Monné static void xn_release_rx_bufs(struct netfront_rxq *); 12296375eacSRoger Pau Monné static void xn_release_tx_bufs(struct netfront_txq *); 12389e0f4d2SKip Macy 124da695b05SRoger Pau Monné static void xn_rxq_intr(struct netfront_rxq *); 125da695b05SRoger Pau Monné static void xn_txq_intr(struct netfront_txq *); 126da695b05SRoger Pau Monné static void xn_intr(void *); 127931eeffaSKenneth D. Merry static inline int xn_count_frags(struct mbuf *m); 12896375eacSRoger Pau Monné static int xn_assemble_tx_request(struct netfront_txq *, struct mbuf *); 12989e0f4d2SKip Macy static int xn_ioctl(struct ifnet *, u_long, caddr_t); 13089e0f4d2SKip Macy static void xn_ifinit_locked(struct netfront_info *); 13189e0f4d2SKip Macy static void xn_ifinit(void *); 13289e0f4d2SKip Macy static void xn_stop(struct netfront_info *); 133578e4bf7SJustin T. Gibbs static void xn_query_features(struct netfront_info *np); 134578e4bf7SJustin T. Gibbs static int xn_configure_features(struct netfront_info *np); 13589e0f4d2SKip Macy static void netif_free(struct netfront_info *info); 13623dc5621SKip Macy static int netfront_detach(device_t dev); 13789e0f4d2SKip Macy 13896375eacSRoger Pau Monné static int xn_txq_mq_start_locked(struct netfront_txq *, struct mbuf *); 13996375eacSRoger Pau Monné static int xn_txq_mq_start(struct ifnet *, struct mbuf *); 14096375eacSRoger Pau Monné 14123dc5621SKip Macy static int talk_to_backend(device_t dev, struct netfront_info *info); 14223dc5621SKip Macy static int create_netdev(device_t dev); 14389e0f4d2SKip Macy static void netif_disconnect_backend(struct netfront_info *info); 14496375eacSRoger Pau Monné static int setup_device(device_t dev, struct netfront_info *info, 14596375eacSRoger Pau Monné unsigned long); 1460e509842SJustin T. Gibbs static int xn_ifmedia_upd(struct ifnet *ifp); 1470e509842SJustin T. Gibbs static void xn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr); 1480e509842SJustin T. Gibbs 14996375eacSRoger Pau Monné int xn_connect(struct netfront_info *); 15089e0f4d2SKip Macy 15196375eacSRoger Pau Monné static int xn_get_responses(struct netfront_rxq *, 15296375eacSRoger Pau Monné struct netfront_rx_info *, RING_IDX, RING_IDX *, 15396375eacSRoger Pau Monné struct mbuf **); 15489e0f4d2SKip Macy 1553c790178SJohn Baldwin #define virt_to_mfn(x) (vtophys(x) >> PAGE_SHIFT) 15689e0f4d2SKip Macy 15789e0f4d2SKip Macy #define INVALID_P2M_ENTRY (~0UL) 15889e0f4d2SKip Macy 15996375eacSRoger Pau Monné struct xn_rx_stats 16089e0f4d2SKip Macy { 16189e0f4d2SKip Macy u_long rx_packets; /* total packets received */ 16289e0f4d2SKip Macy u_long rx_bytes; /* total bytes received */ 16389e0f4d2SKip Macy u_long rx_errors; /* bad packets received */ 16496375eacSRoger Pau Monné }; 16596375eacSRoger Pau Monné 16696375eacSRoger Pau Monné struct xn_tx_stats 16796375eacSRoger Pau Monné { 16896375eacSRoger Pau Monné u_long tx_packets; /* total packets transmitted */ 16996375eacSRoger Pau Monné u_long tx_bytes; /* total bytes transmitted */ 17089e0f4d2SKip Macy u_long tx_errors; /* packet transmit problems */ 17189e0f4d2SKip Macy }; 17289e0f4d2SKip Macy 17396375eacSRoger Pau Monné #define XN_QUEUE_NAME_LEN 8 /* xn{t,r}x_%u, allow for two digits */ 17496375eacSRoger Pau Monné struct netfront_rxq { 17596375eacSRoger Pau Monné struct netfront_info *info; 17696375eacSRoger Pau Monné u_int id; 17796375eacSRoger Pau Monné char name[XN_QUEUE_NAME_LEN]; 17896375eacSRoger Pau Monné struct mtx lock; 17996375eacSRoger Pau Monné 18096375eacSRoger Pau Monné int ring_ref; 18196375eacSRoger Pau Monné netif_rx_front_ring_t ring; 18296375eacSRoger Pau Monné xen_intr_handle_t xen_intr_handle; 18396375eacSRoger Pau Monné 18496375eacSRoger Pau Monné grant_ref_t gref_head; 18596375eacSRoger Pau Monné grant_ref_t grant_ref[NET_TX_RING_SIZE + 1]; 18696375eacSRoger Pau Monné 18796375eacSRoger Pau Monné struct mbuf *mbufs[NET_RX_RING_SIZE + 1]; 18896375eacSRoger Pau Monné 18996375eacSRoger Pau Monné struct lro_ctrl lro; 19096375eacSRoger Pau Monné 1912568ee67SRoger Pau Monné struct callout rx_refill; 1922568ee67SRoger Pau Monné 19396375eacSRoger Pau Monné struct xn_rx_stats stats; 19496375eacSRoger Pau Monné }; 19596375eacSRoger Pau Monné 19696375eacSRoger Pau Monné struct netfront_txq { 19796375eacSRoger Pau Monné struct netfront_info *info; 19896375eacSRoger Pau Monné u_int id; 19996375eacSRoger Pau Monné char name[XN_QUEUE_NAME_LEN]; 20096375eacSRoger Pau Monné struct mtx lock; 20196375eacSRoger Pau Monné 20296375eacSRoger Pau Monné int ring_ref; 20396375eacSRoger Pau Monné netif_tx_front_ring_t ring; 20496375eacSRoger Pau Monné xen_intr_handle_t xen_intr_handle; 20596375eacSRoger Pau Monné 20696375eacSRoger Pau Monné grant_ref_t gref_head; 20796375eacSRoger Pau Monné grant_ref_t grant_ref[NET_TX_RING_SIZE + 1]; 20896375eacSRoger Pau Monné 20996375eacSRoger Pau Monné struct mbuf *mbufs[NET_TX_RING_SIZE + 1]; 21096375eacSRoger Pau Monné int mbufs_cnt; 21196375eacSRoger Pau Monné struct buf_ring *br; 21296375eacSRoger Pau Monné 21396375eacSRoger Pau Monné struct taskqueue *tq; 21496375eacSRoger Pau Monné struct task defrtask; 21596375eacSRoger Pau Monné 21696375eacSRoger Pau Monné bool full; 21796375eacSRoger Pau Monné 21896375eacSRoger Pau Monné struct xn_tx_stats stats; 21996375eacSRoger Pau Monné }; 22096375eacSRoger Pau Monné 22189e0f4d2SKip Macy struct netfront_info { 22289e0f4d2SKip Macy struct ifnet *xn_ifp; 22389e0f4d2SKip Macy 224227ca257SKip Macy struct mtx sc_lock; 22589e0f4d2SKip Macy 22696375eacSRoger Pau Monné u_int num_queues; 22796375eacSRoger Pau Monné struct netfront_rxq *rxq; 22896375eacSRoger Pau Monné struct netfront_txq *txq; 22996375eacSRoger Pau Monné 23089e0f4d2SKip Macy u_int carrier; 231578e4bf7SJustin T. Gibbs u_int maxfrags; 23289e0f4d2SKip Macy 23323dc5621SKip Macy device_t xbdev; 23489e0f4d2SKip Macy uint8_t mac[ETHER_ADDR_LEN]; 23589e0f4d2SKip Macy 23689e0f4d2SKip Macy int xn_if_flags; 23789e0f4d2SKip Macy 2380e509842SJustin T. Gibbs struct ifmedia sc_media; 2396a8e9695SRoger Pau Monné 240*c2d12e5eSRoger Pau Monné bool xn_reset; 24189e0f4d2SKip Macy }; 24289e0f4d2SKip Macy 24396375eacSRoger Pau Monné struct netfront_rx_info { 24496375eacSRoger Pau Monné struct netif_rx_response rx; 24596375eacSRoger Pau Monné struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1]; 24696375eacSRoger Pau Monné }; 24789e0f4d2SKip Macy 24896375eacSRoger Pau Monné #define XN_RX_LOCK(_q) mtx_lock(&(_q)->lock) 24996375eacSRoger Pau Monné #define XN_RX_UNLOCK(_q) mtx_unlock(&(_q)->lock) 25089e0f4d2SKip Macy 25196375eacSRoger Pau Monné #define XN_TX_LOCK(_q) mtx_lock(&(_q)->lock) 25296375eacSRoger Pau Monné #define XN_TX_TRYLOCK(_q) mtx_trylock(&(_q)->lock) 25396375eacSRoger Pau Monné #define XN_TX_UNLOCK(_q) mtx_unlock(&(_q)->lock) 25489e0f4d2SKip Macy 255227ca257SKip Macy #define XN_LOCK(_sc) mtx_lock(&(_sc)->sc_lock); 256227ca257SKip Macy #define XN_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_lock); 25789e0f4d2SKip Macy 258227ca257SKip Macy #define XN_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->sc_lock, MA_OWNED); 25996375eacSRoger Pau Monné #define XN_RX_LOCK_ASSERT(_q) mtx_assert(&(_q)->lock, MA_OWNED); 26096375eacSRoger Pau Monné #define XN_TX_LOCK_ASSERT(_q) mtx_assert(&(_q)->lock, MA_OWNED); 26189e0f4d2SKip Macy 26289e0f4d2SKip Macy #define netfront_carrier_on(netif) ((netif)->carrier = 1) 26389e0f4d2SKip Macy #define netfront_carrier_off(netif) ((netif)->carrier = 0) 26489e0f4d2SKip Macy #define netfront_carrier_ok(netif) ((netif)->carrier) 26589e0f4d2SKip Macy 26689e0f4d2SKip Macy /* Access macros for acquiring freeing slots in xn_free_{tx,rx}_idxs[]. */ 26789e0f4d2SKip Macy 26889e0f4d2SKip Macy static inline void 269931eeffaSKenneth D. Merry add_id_to_freelist(struct mbuf **list, uintptr_t id) 27089e0f4d2SKip Macy { 27196375eacSRoger Pau Monné 272931eeffaSKenneth D. Merry KASSERT(id != 0, 273931eeffaSKenneth D. Merry ("%s: the head item (0) must always be free.", __func__)); 27489e0f4d2SKip Macy list[id] = list[0]; 275931eeffaSKenneth D. Merry list[0] = (struct mbuf *)id; 27689e0f4d2SKip Macy } 27789e0f4d2SKip Macy 27889e0f4d2SKip Macy static inline unsigned short 27989e0f4d2SKip Macy get_id_from_freelist(struct mbuf **list) 28089e0f4d2SKip Macy { 281931eeffaSKenneth D. Merry uintptr_t id; 282931eeffaSKenneth D. Merry 283931eeffaSKenneth D. Merry id = (uintptr_t)list[0]; 284931eeffaSKenneth D. Merry KASSERT(id != 0, 285931eeffaSKenneth D. Merry ("%s: the head item (0) must always remain free.", __func__)); 28689e0f4d2SKip Macy list[0] = list[id]; 28789e0f4d2SKip Macy return (id); 28889e0f4d2SKip Macy } 28989e0f4d2SKip Macy 29089e0f4d2SKip Macy static inline int 29196375eacSRoger Pau Monné xn_rxidx(RING_IDX idx) 29289e0f4d2SKip Macy { 29396375eacSRoger Pau Monné 29489e0f4d2SKip Macy return idx & (NET_RX_RING_SIZE - 1); 29589e0f4d2SKip Macy } 29689e0f4d2SKip Macy 29789e0f4d2SKip Macy static inline struct mbuf * 29896375eacSRoger Pau Monné xn_get_rx_mbuf(struct netfront_rxq *rxq, RING_IDX ri) 29989e0f4d2SKip Macy { 30096375eacSRoger Pau Monné int i; 30189e0f4d2SKip Macy struct mbuf *m; 30289e0f4d2SKip Macy 30396375eacSRoger Pau Monné i = xn_rxidx(ri); 30496375eacSRoger Pau Monné m = rxq->mbufs[i]; 30596375eacSRoger Pau Monné rxq->mbufs[i] = NULL; 30689e0f4d2SKip Macy return (m); 30789e0f4d2SKip Macy } 30889e0f4d2SKip Macy 30989e0f4d2SKip Macy static inline grant_ref_t 31096375eacSRoger Pau Monné xn_get_rx_ref(struct netfront_rxq *rxq, RING_IDX ri) 31189e0f4d2SKip Macy { 31296375eacSRoger Pau Monné int i = xn_rxidx(ri); 31396375eacSRoger Pau Monné grant_ref_t ref = rxq->grant_ref[i]; 31496375eacSRoger Pau Monné 315ff662b5cSJustin T. Gibbs KASSERT(ref != GRANT_REF_INVALID, ("Invalid grant reference!\n")); 31696375eacSRoger Pau Monné rxq->grant_ref[i] = GRANT_REF_INVALID; 31796375eacSRoger Pau Monné return (ref); 31889e0f4d2SKip Macy } 31989e0f4d2SKip Macy 32089e0f4d2SKip Macy #define IPRINTK(fmt, args...) \ 32189e0f4d2SKip Macy printf("[XEN] " fmt, ##args) 322227ca257SKip Macy #ifdef INVARIANTS 32389e0f4d2SKip Macy #define WPRINTK(fmt, args...) \ 32489e0f4d2SKip Macy printf("[XEN] " fmt, ##args) 325227ca257SKip Macy #else 326227ca257SKip Macy #define WPRINTK(fmt, args...) 327227ca257SKip Macy #endif 328227ca257SKip Macy #ifdef DEBUG 32989e0f4d2SKip Macy #define DPRINTK(fmt, args...) \ 33023dc5621SKip Macy printf("[XEN] %s: " fmt, __func__, ##args) 33112678024SDoug Rabson #else 33212678024SDoug Rabson #define DPRINTK(fmt, args...) 33312678024SDoug Rabson #endif 33489e0f4d2SKip Macy 33589e0f4d2SKip Macy /** 33689e0f4d2SKip Macy * Read the 'mac' node at the given device's node in the store, and parse that 33789e0f4d2SKip Macy * as colon-separated octets, placing result the given mac array. mac must be 33889e0f4d2SKip Macy * a preallocated array of length ETH_ALEN (as declared in linux/if_ether.h). 33989e0f4d2SKip Macy * Return 0 on success, or errno on error. 34089e0f4d2SKip Macy */ 34189e0f4d2SKip Macy static int 34223dc5621SKip Macy xen_net_read_mac(device_t dev, uint8_t mac[]) 34389e0f4d2SKip Macy { 3443a6d1fcfSKip Macy int error, i; 3453a6d1fcfSKip Macy char *s, *e, *macstr; 346ffa06904SJustin T. Gibbs const char *path; 3473a6d1fcfSKip Macy 348ffa06904SJustin T. Gibbs path = xenbus_get_node(dev); 349ffa06904SJustin T. Gibbs error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr); 350ffa06904SJustin T. Gibbs if (error == ENOENT) { 351ffa06904SJustin T. Gibbs /* 352ffa06904SJustin T. Gibbs * Deal with missing mac XenStore nodes on devices with 353ffa06904SJustin T. Gibbs * HVM emulation (the 'ioemu' configuration attribute) 354ffa06904SJustin T. Gibbs * enabled. 355ffa06904SJustin T. Gibbs * 356ffa06904SJustin T. Gibbs * The HVM emulator may execute in a stub device model 357ffa06904SJustin T. Gibbs * domain which lacks the permission, only given to Dom0, 358ffa06904SJustin T. Gibbs * to update the guest's XenStore tree. For this reason, 359ffa06904SJustin T. Gibbs * the HVM emulator doesn't even attempt to write the 360ffa06904SJustin T. Gibbs * front-side mac node, even when operating in Dom0. 361ffa06904SJustin T. Gibbs * However, there should always be a mac listed in the 362ffa06904SJustin T. Gibbs * backend tree. Fallback to this version if our query 363ffa06904SJustin T. Gibbs * of the front side XenStore location doesn't find 364ffa06904SJustin T. Gibbs * anything. 365ffa06904SJustin T. Gibbs */ 366ffa06904SJustin T. Gibbs path = xenbus_get_otherend_path(dev); 367ffa06904SJustin T. Gibbs error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr); 368ffa06904SJustin T. Gibbs } 369ffa06904SJustin T. Gibbs if (error != 0) { 370ffa06904SJustin T. Gibbs xenbus_dev_fatal(dev, error, "parsing %s/mac", path); 3713a6d1fcfSKip Macy return (error); 372ffa06904SJustin T. Gibbs } 3733a6d1fcfSKip Macy 37489e0f4d2SKip Macy s = macstr; 37589e0f4d2SKip Macy for (i = 0; i < ETHER_ADDR_LEN; i++) { 37689e0f4d2SKip Macy mac[i] = strtoul(s, &e, 16); 37789e0f4d2SKip Macy if (s == e || (e[0] != ':' && e[0] != 0)) { 378ff662b5cSJustin T. Gibbs free(macstr, M_XENBUS); 3793a6d1fcfSKip Macy return (ENOENT); 38089e0f4d2SKip Macy } 38189e0f4d2SKip Macy s = &e[1]; 38289e0f4d2SKip Macy } 383ff662b5cSJustin T. Gibbs free(macstr, M_XENBUS); 3843a6d1fcfSKip Macy return (0); 38589e0f4d2SKip Macy } 38689e0f4d2SKip Macy 38789e0f4d2SKip Macy /** 38889e0f4d2SKip Macy * Entry point to this code when a new device is created. Allocate the basic 38989e0f4d2SKip Macy * structures and the ring buffers for communication with the backend, and 39089e0f4d2SKip Macy * inform the backend of the appropriate details for those. Switch to 39189e0f4d2SKip Macy * Connected state. 39289e0f4d2SKip Macy */ 39389e0f4d2SKip Macy static int 39423dc5621SKip Macy netfront_probe(device_t dev) 39523dc5621SKip Macy { 39623dc5621SKip Macy 397f8f1bb83SRoger Pau Monné if (xen_hvm_domain() && xen_disable_pv_nics != 0) 398f8f1bb83SRoger Pau Monné return (ENXIO); 399f8f1bb83SRoger Pau Monné 40023dc5621SKip Macy if (!strcmp(xenbus_get_type(dev), "vif")) { 40123dc5621SKip Macy device_set_desc(dev, "Virtual Network Interface"); 40223dc5621SKip Macy return (0); 40323dc5621SKip Macy } 40423dc5621SKip Macy 40523dc5621SKip Macy return (ENXIO); 40623dc5621SKip Macy } 40723dc5621SKip Macy 40823dc5621SKip Macy static int 40923dc5621SKip Macy netfront_attach(device_t dev) 41089e0f4d2SKip Macy { 41189e0f4d2SKip Macy int err; 41289e0f4d2SKip Macy 41323dc5621SKip Macy err = create_netdev(dev); 41496375eacSRoger Pau Monné if (err != 0) { 41589e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "creating netdev"); 416ffa06904SJustin T. Gibbs return (err); 41789e0f4d2SKip Macy } 41889e0f4d2SKip Macy 41912678024SDoug Rabson SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 42012678024SDoug Rabson SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 421f0188618SHans Petter Selasky OID_AUTO, "enable_lro", CTLFLAG_RW, 42212678024SDoug Rabson &xn_enable_lro, 0, "Large Receive Offload"); 42312678024SDoug Rabson 42496375eacSRoger Pau Monné SYSCTL_ADD_ULONG(device_get_sysctl_ctx(dev), 42596375eacSRoger Pau Monné SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 42696375eacSRoger Pau Monné OID_AUTO, "num_queues", CTLFLAG_RD, 42796375eacSRoger Pau Monné &xn_num_queues, "Number of pairs of queues"); 42896375eacSRoger Pau Monné 429ffa06904SJustin T. Gibbs return (0); 43089e0f4d2SKip Macy } 43189e0f4d2SKip Macy 432cf9c09e1SJustin T. Gibbs static int 433cf9c09e1SJustin T. Gibbs netfront_suspend(device_t dev) 434cf9c09e1SJustin T. Gibbs { 43596375eacSRoger Pau Monné struct netfront_info *np = device_get_softc(dev); 43696375eacSRoger Pau Monné u_int i; 437cf9c09e1SJustin T. Gibbs 43896375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 43996375eacSRoger Pau Monné XN_RX_LOCK(&np->rxq[i]); 44096375eacSRoger Pau Monné XN_TX_LOCK(&np->txq[i]); 44196375eacSRoger Pau Monné } 44296375eacSRoger Pau Monné netfront_carrier_off(np); 44396375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 44496375eacSRoger Pau Monné XN_RX_UNLOCK(&np->rxq[i]); 44596375eacSRoger Pau Monné XN_TX_UNLOCK(&np->txq[i]); 44696375eacSRoger Pau Monné } 447cf9c09e1SJustin T. Gibbs return (0); 448cf9c09e1SJustin T. Gibbs } 44989e0f4d2SKip Macy 45089e0f4d2SKip Macy /** 45189e0f4d2SKip Macy * We are reconnecting to the backend, due to a suspend/resume, or a backend 45289e0f4d2SKip Macy * driver restart. We tear down our netif structure and recreate it, but 45389e0f4d2SKip Macy * leave the device-layer structures intact so that this is transparent to the 45489e0f4d2SKip Macy * rest of the kernel. 45589e0f4d2SKip Macy */ 45689e0f4d2SKip Macy static int 45723dc5621SKip Macy netfront_resume(device_t dev) 45889e0f4d2SKip Macy { 45923dc5621SKip Macy struct netfront_info *info = device_get_softc(dev); 46089e0f4d2SKip Macy 46189e0f4d2SKip Macy netif_disconnect_backend(info); 46289e0f4d2SKip Macy return (0); 46389e0f4d2SKip Macy } 46489e0f4d2SKip Macy 46596375eacSRoger Pau Monné static int 46696375eacSRoger Pau Monné write_queue_xenstore_keys(device_t dev, 46796375eacSRoger Pau Monné struct netfront_rxq *rxq, 46896375eacSRoger Pau Monné struct netfront_txq *txq, 46996375eacSRoger Pau Monné struct xs_transaction *xst, bool hierarchy) 47096375eacSRoger Pau Monné { 47196375eacSRoger Pau Monné int err; 47296375eacSRoger Pau Monné const char *message; 47396375eacSRoger Pau Monné const char *node = xenbus_get_node(dev); 47496375eacSRoger Pau Monné char *path; 47596375eacSRoger Pau Monné size_t path_size; 47696375eacSRoger Pau Monné 47796375eacSRoger Pau Monné KASSERT(rxq->id == txq->id, ("Mismatch between RX and TX queue ids")); 47896375eacSRoger Pau Monné /* Split event channel support is not yet there. */ 47996375eacSRoger Pau Monné KASSERT(rxq->xen_intr_handle == txq->xen_intr_handle, 48096375eacSRoger Pau Monné ("Split event channels are not supported")); 48196375eacSRoger Pau Monné 48296375eacSRoger Pau Monné if (hierarchy) { 48396375eacSRoger Pau Monné path_size = strlen(node) + 10; 48496375eacSRoger Pau Monné path = malloc(path_size, M_DEVBUF, M_WAITOK|M_ZERO); 48596375eacSRoger Pau Monné snprintf(path, path_size, "%s/queue-%u", node, rxq->id); 48696375eacSRoger Pau Monné } else { 48796375eacSRoger Pau Monné path_size = strlen(node) + 1; 48896375eacSRoger Pau Monné path = malloc(path_size, M_DEVBUF, M_WAITOK|M_ZERO); 48996375eacSRoger Pau Monné snprintf(path, path_size, "%s", node); 49096375eacSRoger Pau Monné } 49196375eacSRoger Pau Monné 49296375eacSRoger Pau Monné err = xs_printf(*xst, path, "tx-ring-ref","%u", txq->ring_ref); 49396375eacSRoger Pau Monné if (err != 0) { 49496375eacSRoger Pau Monné message = "writing tx ring-ref"; 49596375eacSRoger Pau Monné goto error; 49696375eacSRoger Pau Monné } 49796375eacSRoger Pau Monné err = xs_printf(*xst, path, "rx-ring-ref","%u", rxq->ring_ref); 49896375eacSRoger Pau Monné if (err != 0) { 49996375eacSRoger Pau Monné message = "writing rx ring-ref"; 50096375eacSRoger Pau Monné goto error; 50196375eacSRoger Pau Monné } 50296375eacSRoger Pau Monné err = xs_printf(*xst, path, "event-channel", "%u", 50396375eacSRoger Pau Monné xen_intr_port(rxq->xen_intr_handle)); 50496375eacSRoger Pau Monné if (err != 0) { 50596375eacSRoger Pau Monné message = "writing event-channel"; 50696375eacSRoger Pau Monné goto error; 50796375eacSRoger Pau Monné } 50896375eacSRoger Pau Monné 50996375eacSRoger Pau Monné free(path, M_DEVBUF); 51096375eacSRoger Pau Monné 51196375eacSRoger Pau Monné return (0); 51296375eacSRoger Pau Monné 51396375eacSRoger Pau Monné error: 51496375eacSRoger Pau Monné free(path, M_DEVBUF); 51596375eacSRoger Pau Monné xenbus_dev_fatal(dev, err, "%s", message); 51696375eacSRoger Pau Monné 51796375eacSRoger Pau Monné return (err); 51896375eacSRoger Pau Monné } 51996375eacSRoger Pau Monné 52089e0f4d2SKip Macy /* Common code used when first setting up, and when resuming. */ 52189e0f4d2SKip Macy static int 52223dc5621SKip Macy talk_to_backend(device_t dev, struct netfront_info *info) 52389e0f4d2SKip Macy { 52489e0f4d2SKip Macy const char *message; 525ff662b5cSJustin T. Gibbs struct xs_transaction xst; 52623dc5621SKip Macy const char *node = xenbus_get_node(dev); 52789e0f4d2SKip Macy int err; 52896375eacSRoger Pau Monné unsigned long num_queues, max_queues = 0; 52996375eacSRoger Pau Monné unsigned int i; 53089e0f4d2SKip Macy 53189e0f4d2SKip Macy err = xen_net_read_mac(dev, info->mac); 53296375eacSRoger Pau Monné if (err != 0) { 53323dc5621SKip Macy xenbus_dev_fatal(dev, err, "parsing %s/mac", node); 53489e0f4d2SKip Macy goto out; 53589e0f4d2SKip Macy } 53689e0f4d2SKip Macy 53796375eacSRoger Pau Monné err = xs_scanf(XST_NIL, xenbus_get_otherend_path(info->xbdev), 53896375eacSRoger Pau Monné "multi-queue-max-queues", NULL, "%lu", &max_queues); 53996375eacSRoger Pau Monné if (err != 0) 54096375eacSRoger Pau Monné max_queues = 1; 54196375eacSRoger Pau Monné num_queues = xn_num_queues; 54296375eacSRoger Pau Monné if (num_queues > max_queues) 54396375eacSRoger Pau Monné num_queues = max_queues; 54496375eacSRoger Pau Monné 54596375eacSRoger Pau Monné err = setup_device(dev, info, num_queues); 54696375eacSRoger Pau Monné if (err != 0) 54789e0f4d2SKip Macy goto out; 54889e0f4d2SKip Macy 54989e0f4d2SKip Macy again: 550ff662b5cSJustin T. Gibbs err = xs_transaction_start(&xst); 55196375eacSRoger Pau Monné if (err != 0) { 55289e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "starting transaction"); 55396375eacSRoger Pau Monné goto free; 55489e0f4d2SKip Macy } 55596375eacSRoger Pau Monné 55696375eacSRoger Pau Monné if (info->num_queues == 1) { 55796375eacSRoger Pau Monné err = write_queue_xenstore_keys(dev, &info->rxq[0], 55896375eacSRoger Pau Monné &info->txq[0], &xst, false); 55996375eacSRoger Pau Monné if (err != 0) 56096375eacSRoger Pau Monné goto abort_transaction_no_def_error; 56196375eacSRoger Pau Monné } else { 56296375eacSRoger Pau Monné err = xs_printf(xst, node, "multi-queue-num-queues", 56396375eacSRoger Pau Monné "%u", info->num_queues); 56496375eacSRoger Pau Monné if (err != 0) { 56596375eacSRoger Pau Monné message = "writing multi-queue-num-queues"; 56689e0f4d2SKip Macy goto abort_transaction; 56789e0f4d2SKip Macy } 56896375eacSRoger Pau Monné 56996375eacSRoger Pau Monné for (i = 0; i < info->num_queues; i++) { 57096375eacSRoger Pau Monné err = write_queue_xenstore_keys(dev, &info->rxq[i], 57196375eacSRoger Pau Monné &info->txq[i], &xst, true); 57296375eacSRoger Pau Monné if (err != 0) 57396375eacSRoger Pau Monné goto abort_transaction_no_def_error; 57489e0f4d2SKip Macy } 57589e0f4d2SKip Macy } 57696375eacSRoger Pau Monné 577d0f3a8b9SRoger Pau Monné err = xs_printf(xst, node, "request-rx-copy", "%u", 1); 57896375eacSRoger Pau Monné if (err != 0) { 57989e0f4d2SKip Macy message = "writing request-rx-copy"; 58089e0f4d2SKip Macy goto abort_transaction; 58189e0f4d2SKip Macy } 582ff662b5cSJustin T. Gibbs err = xs_printf(xst, node, "feature-rx-notify", "%d", 1); 58396375eacSRoger Pau Monné if (err != 0) { 58489e0f4d2SKip Macy message = "writing feature-rx-notify"; 58589e0f4d2SKip Macy goto abort_transaction; 58689e0f4d2SKip Macy } 587ff662b5cSJustin T. Gibbs err = xs_printf(xst, node, "feature-sg", "%d", 1); 58896375eacSRoger Pau Monné if (err != 0) { 58989e0f4d2SKip Macy message = "writing feature-sg"; 59089e0f4d2SKip Macy goto abort_transaction; 59189e0f4d2SKip Macy } 592*c2d12e5eSRoger Pau Monné if ((info->xn_ifp->if_capenable & IFCAP_LRO) != 0) { 593ff662b5cSJustin T. Gibbs err = xs_printf(xst, node, "feature-gso-tcpv4", "%d", 1); 59496375eacSRoger Pau Monné if (err != 0) { 59589e0f4d2SKip Macy message = "writing feature-gso-tcpv4"; 59689e0f4d2SKip Macy goto abort_transaction; 59789e0f4d2SKip Macy } 598*c2d12e5eSRoger Pau Monné } 599*c2d12e5eSRoger Pau Monné if ((info->xn_ifp->if_capenable & IFCAP_RXCSUM) == 0) { 600*c2d12e5eSRoger Pau Monné err = xs_printf(xst, node, "feature-no-csum-offload", "%d", 1); 601*c2d12e5eSRoger Pau Monné if (err != 0) { 602*c2d12e5eSRoger Pau Monné message = "writing feature-no-csum-offload"; 603*c2d12e5eSRoger Pau Monné goto abort_transaction; 604*c2d12e5eSRoger Pau Monné } 605*c2d12e5eSRoger Pau Monné } 60689e0f4d2SKip Macy 607ff662b5cSJustin T. Gibbs err = xs_transaction_end(xst, 0); 60896375eacSRoger Pau Monné if (err != 0) { 60989e0f4d2SKip Macy if (err == EAGAIN) 61089e0f4d2SKip Macy goto again; 61189e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "completing transaction"); 61296375eacSRoger Pau Monné goto free; 61389e0f4d2SKip Macy } 61489e0f4d2SKip Macy 61589e0f4d2SKip Macy return 0; 61689e0f4d2SKip Macy 61789e0f4d2SKip Macy abort_transaction: 61889e0f4d2SKip Macy xenbus_dev_fatal(dev, err, "%s", message); 61996375eacSRoger Pau Monné abort_transaction_no_def_error: 62096375eacSRoger Pau Monné xs_transaction_end(xst, 1); 62196375eacSRoger Pau Monné free: 62289e0f4d2SKip Macy netif_free(info); 62389e0f4d2SKip Macy out: 62496375eacSRoger Pau Monné return (err); 62596375eacSRoger Pau Monné } 62696375eacSRoger Pau Monné 62796375eacSRoger Pau Monné static void 628da695b05SRoger Pau Monné xn_rxq_intr(struct netfront_rxq *rxq) 62996375eacSRoger Pau Monné { 63096375eacSRoger Pau Monné 63196375eacSRoger Pau Monné XN_RX_LOCK(rxq); 63296375eacSRoger Pau Monné xn_rxeof(rxq); 63396375eacSRoger Pau Monné XN_RX_UNLOCK(rxq); 63496375eacSRoger Pau Monné } 63596375eacSRoger Pau Monné 63696375eacSRoger Pau Monné static void 63796375eacSRoger Pau Monné xn_txq_start(struct netfront_txq *txq) 63896375eacSRoger Pau Monné { 63996375eacSRoger Pau Monné struct netfront_info *np = txq->info; 64096375eacSRoger Pau Monné struct ifnet *ifp = np->xn_ifp; 64196375eacSRoger Pau Monné 64296375eacSRoger Pau Monné XN_TX_LOCK_ASSERT(txq); 64396375eacSRoger Pau Monné if (!drbr_empty(ifp, txq->br)) 64496375eacSRoger Pau Monné xn_txq_mq_start_locked(txq, NULL); 64596375eacSRoger Pau Monné } 64696375eacSRoger Pau Monné 64796375eacSRoger Pau Monné static void 648da695b05SRoger Pau Monné xn_txq_intr(struct netfront_txq *txq) 64996375eacSRoger Pau Monné { 65096375eacSRoger Pau Monné 65196375eacSRoger Pau Monné XN_TX_LOCK(txq); 65296375eacSRoger Pau Monné if (RING_HAS_UNCONSUMED_RESPONSES(&txq->ring)) 65396375eacSRoger Pau Monné xn_txeof(txq); 65496375eacSRoger Pau Monné xn_txq_start(txq); 65596375eacSRoger Pau Monné XN_TX_UNLOCK(txq); 65696375eacSRoger Pau Monné } 65796375eacSRoger Pau Monné 65896375eacSRoger Pau Monné static void 65996375eacSRoger Pau Monné xn_txq_tq_deferred(void *xtxq, int pending) 66096375eacSRoger Pau Monné { 66196375eacSRoger Pau Monné struct netfront_txq *txq = xtxq; 66296375eacSRoger Pau Monné 66396375eacSRoger Pau Monné XN_TX_LOCK(txq); 66496375eacSRoger Pau Monné xn_txq_start(txq); 66596375eacSRoger Pau Monné XN_TX_UNLOCK(txq); 66696375eacSRoger Pau Monné } 66796375eacSRoger Pau Monné 66896375eacSRoger Pau Monné static void 66996375eacSRoger Pau Monné disconnect_rxq(struct netfront_rxq *rxq) 67096375eacSRoger Pau Monné { 67196375eacSRoger Pau Monné 67296375eacSRoger Pau Monné xn_release_rx_bufs(rxq); 67396375eacSRoger Pau Monné gnttab_free_grant_references(rxq->gref_head); 674d039b070SRoger Pau Monné gnttab_end_foreign_access(rxq->ring_ref, NULL); 67596375eacSRoger Pau Monné /* 67696375eacSRoger Pau Monné * No split event channel support at the moment, handle will 67796375eacSRoger Pau Monné * be unbound in tx. So no need to call xen_intr_unbind here, 67896375eacSRoger Pau Monné * but we do want to reset the handler to 0. 67996375eacSRoger Pau Monné */ 68096375eacSRoger Pau Monné rxq->xen_intr_handle = 0; 68196375eacSRoger Pau Monné } 68296375eacSRoger Pau Monné 68396375eacSRoger Pau Monné static void 68496375eacSRoger Pau Monné destroy_rxq(struct netfront_rxq *rxq) 68596375eacSRoger Pau Monné { 68696375eacSRoger Pau Monné 6872568ee67SRoger Pau Monné callout_drain(&rxq->rx_refill); 68896375eacSRoger Pau Monné free(rxq->ring.sring, M_DEVBUF); 68996375eacSRoger Pau Monné } 69096375eacSRoger Pau Monné 69196375eacSRoger Pau Monné static void 69296375eacSRoger Pau Monné destroy_rxqs(struct netfront_info *np) 69396375eacSRoger Pau Monné { 69496375eacSRoger Pau Monné int i; 69596375eacSRoger Pau Monné 69696375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) 69796375eacSRoger Pau Monné destroy_rxq(&np->rxq[i]); 69896375eacSRoger Pau Monné 69996375eacSRoger Pau Monné free(np->rxq, M_DEVBUF); 70096375eacSRoger Pau Monné np->rxq = NULL; 70189e0f4d2SKip Macy } 70289e0f4d2SKip Macy 70389e0f4d2SKip Macy static int 70496375eacSRoger Pau Monné setup_rxqs(device_t dev, struct netfront_info *info, 70596375eacSRoger Pau Monné unsigned long num_queues) 70689e0f4d2SKip Macy { 70796375eacSRoger Pau Monné int q, i; 7083a6d1fcfSKip Macy int error; 70996375eacSRoger Pau Monné netif_rx_sring_t *rxs; 71096375eacSRoger Pau Monné struct netfront_rxq *rxq; 71189e0f4d2SKip Macy 71296375eacSRoger Pau Monné info->rxq = malloc(sizeof(struct netfront_rxq) * num_queues, 71396375eacSRoger Pau Monné M_DEVBUF, M_WAITOK|M_ZERO); 71489e0f4d2SKip Macy 71596375eacSRoger Pau Monné for (q = 0; q < num_queues; q++) { 71696375eacSRoger Pau Monné rxq = &info->rxq[q]; 71796375eacSRoger Pau Monné 71896375eacSRoger Pau Monné rxq->id = q; 71996375eacSRoger Pau Monné rxq->info = info; 72096375eacSRoger Pau Monné rxq->ring_ref = GRANT_REF_INVALID; 72196375eacSRoger Pau Monné rxq->ring.sring = NULL; 72296375eacSRoger Pau Monné snprintf(rxq->name, XN_QUEUE_NAME_LEN, "xnrx_%u", q); 72396375eacSRoger Pau Monné mtx_init(&rxq->lock, rxq->name, "netfront receive lock", 72496375eacSRoger Pau Monné MTX_DEF); 72596375eacSRoger Pau Monné 72696375eacSRoger Pau Monné for (i = 0; i <= NET_RX_RING_SIZE; i++) { 72796375eacSRoger Pau Monné rxq->mbufs[i] = NULL; 72896375eacSRoger Pau Monné rxq->grant_ref[i] = GRANT_REF_INVALID; 72996375eacSRoger Pau Monné } 73096375eacSRoger Pau Monné 73196375eacSRoger Pau Monné /* Start resources allocation */ 73296375eacSRoger Pau Monné 7332568ee67SRoger Pau Monné if (gnttab_alloc_grant_references(NET_RX_RING_SIZE, 73496375eacSRoger Pau Monné &rxq->gref_head) != 0) { 73596375eacSRoger Pau Monné device_printf(dev, "allocating rx gref"); 7363a6d1fcfSKip Macy error = ENOMEM; 73789e0f4d2SKip Macy goto fail; 73889e0f4d2SKip Macy } 73989e0f4d2SKip Macy 74096375eacSRoger Pau Monné rxs = (netif_rx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, 74196375eacSRoger Pau Monné M_WAITOK|M_ZERO); 74289e0f4d2SKip Macy SHARED_RING_INIT(rxs); 74396375eacSRoger Pau Monné FRONT_RING_INIT(&rxq->ring, rxs, PAGE_SIZE); 74489e0f4d2SKip Macy 74596375eacSRoger Pau Monné error = xenbus_grant_ring(dev, virt_to_mfn(rxs), 74696375eacSRoger Pau Monné &rxq->ring_ref); 74796375eacSRoger Pau Monné if (error != 0) { 74896375eacSRoger Pau Monné device_printf(dev, "granting rx ring page"); 74996375eacSRoger Pau Monné goto fail_grant_ring; 75096375eacSRoger Pau Monné } 75189e0f4d2SKip Macy 7522568ee67SRoger Pau Monné callout_init(&rxq->rx_refill, 1); 75389e0f4d2SKip Macy } 75489e0f4d2SKip Macy 7553a6d1fcfSKip Macy return (0); 75689e0f4d2SKip Macy 75796375eacSRoger Pau Monné fail_grant_ring: 75896375eacSRoger Pau Monné gnttab_free_grant_references(rxq->gref_head); 75996375eacSRoger Pau Monné free(rxq->ring.sring, M_DEVBUF); 76089e0f4d2SKip Macy fail: 76196375eacSRoger Pau Monné for (; q >= 0; q--) { 76296375eacSRoger Pau Monné disconnect_rxq(&info->rxq[q]); 76396375eacSRoger Pau Monné destroy_rxq(&info->rxq[q]); 76496375eacSRoger Pau Monné } 76596375eacSRoger Pau Monné 76696375eacSRoger Pau Monné free(info->rxq, M_DEVBUF); 76796375eacSRoger Pau Monné return (error); 76896375eacSRoger Pau Monné } 76996375eacSRoger Pau Monné 77096375eacSRoger Pau Monné static void 77196375eacSRoger Pau Monné disconnect_txq(struct netfront_txq *txq) 77296375eacSRoger Pau Monné { 77396375eacSRoger Pau Monné 77496375eacSRoger Pau Monné xn_release_tx_bufs(txq); 77596375eacSRoger Pau Monné gnttab_free_grant_references(txq->gref_head); 776d039b070SRoger Pau Monné gnttab_end_foreign_access(txq->ring_ref, NULL); 77796375eacSRoger Pau Monné xen_intr_unbind(&txq->xen_intr_handle); 77896375eacSRoger Pau Monné } 77996375eacSRoger Pau Monné 78096375eacSRoger Pau Monné static void 78196375eacSRoger Pau Monné destroy_txq(struct netfront_txq *txq) 78296375eacSRoger Pau Monné { 78396375eacSRoger Pau Monné 78496375eacSRoger Pau Monné free(txq->ring.sring, M_DEVBUF); 78596375eacSRoger Pau Monné buf_ring_free(txq->br, M_DEVBUF); 78696375eacSRoger Pau Monné taskqueue_drain_all(txq->tq); 78796375eacSRoger Pau Monné taskqueue_free(txq->tq); 78896375eacSRoger Pau Monné } 78996375eacSRoger Pau Monné 79096375eacSRoger Pau Monné static void 79196375eacSRoger Pau Monné destroy_txqs(struct netfront_info *np) 79296375eacSRoger Pau Monné { 79396375eacSRoger Pau Monné int i; 79496375eacSRoger Pau Monné 79596375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) 79696375eacSRoger Pau Monné destroy_txq(&np->txq[i]); 79796375eacSRoger Pau Monné 79896375eacSRoger Pau Monné free(np->txq, M_DEVBUF); 79996375eacSRoger Pau Monné np->txq = NULL; 80096375eacSRoger Pau Monné } 80196375eacSRoger Pau Monné 80296375eacSRoger Pau Monné static int 80396375eacSRoger Pau Monné setup_txqs(device_t dev, struct netfront_info *info, 80496375eacSRoger Pau Monné unsigned long num_queues) 80596375eacSRoger Pau Monné { 80696375eacSRoger Pau Monné int q, i; 80796375eacSRoger Pau Monné int error; 80896375eacSRoger Pau Monné netif_tx_sring_t *txs; 80996375eacSRoger Pau Monné struct netfront_txq *txq; 81096375eacSRoger Pau Monné 81196375eacSRoger Pau Monné info->txq = malloc(sizeof(struct netfront_txq) * num_queues, 81296375eacSRoger Pau Monné M_DEVBUF, M_WAITOK|M_ZERO); 81396375eacSRoger Pau Monné 81496375eacSRoger Pau Monné for (q = 0; q < num_queues; q++) { 81596375eacSRoger Pau Monné txq = &info->txq[q]; 81696375eacSRoger Pau Monné 81796375eacSRoger Pau Monné txq->id = q; 81896375eacSRoger Pau Monné txq->info = info; 81996375eacSRoger Pau Monné 82096375eacSRoger Pau Monné txq->ring_ref = GRANT_REF_INVALID; 82196375eacSRoger Pau Monné txq->ring.sring = NULL; 82296375eacSRoger Pau Monné 82396375eacSRoger Pau Monné snprintf(txq->name, XN_QUEUE_NAME_LEN, "xntx_%u", q); 82496375eacSRoger Pau Monné 82596375eacSRoger Pau Monné mtx_init(&txq->lock, txq->name, "netfront transmit lock", 82696375eacSRoger Pau Monné MTX_DEF); 82796375eacSRoger Pau Monné 82896375eacSRoger Pau Monné for (i = 0; i <= NET_TX_RING_SIZE; i++) { 82996375eacSRoger Pau Monné txq->mbufs[i] = (void *) ((u_long) i+1); 83096375eacSRoger Pau Monné txq->grant_ref[i] = GRANT_REF_INVALID; 83196375eacSRoger Pau Monné } 83296375eacSRoger Pau Monné txq->mbufs[NET_TX_RING_SIZE] = (void *)0; 83396375eacSRoger Pau Monné 83496375eacSRoger Pau Monné /* Start resources allocation. */ 83596375eacSRoger Pau Monné 83696375eacSRoger Pau Monné if (gnttab_alloc_grant_references(NET_TX_RING_SIZE, 83796375eacSRoger Pau Monné &txq->gref_head) != 0) { 83896375eacSRoger Pau Monné device_printf(dev, "failed to allocate tx grant refs\n"); 83996375eacSRoger Pau Monné error = ENOMEM; 84096375eacSRoger Pau Monné goto fail; 84196375eacSRoger Pau Monné } 84296375eacSRoger Pau Monné 84396375eacSRoger Pau Monné txs = (netif_tx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, 84496375eacSRoger Pau Monné M_WAITOK|M_ZERO); 84596375eacSRoger Pau Monné SHARED_RING_INIT(txs); 84696375eacSRoger Pau Monné FRONT_RING_INIT(&txq->ring, txs, PAGE_SIZE); 84796375eacSRoger Pau Monné 84896375eacSRoger Pau Monné error = xenbus_grant_ring(dev, virt_to_mfn(txs), 84996375eacSRoger Pau Monné &txq->ring_ref); 85096375eacSRoger Pau Monné if (error != 0) { 85196375eacSRoger Pau Monné device_printf(dev, "failed to grant tx ring\n"); 85296375eacSRoger Pau Monné goto fail_grant_ring; 85396375eacSRoger Pau Monné } 85496375eacSRoger Pau Monné 85596375eacSRoger Pau Monné txq->br = buf_ring_alloc(NET_TX_RING_SIZE, M_DEVBUF, 85696375eacSRoger Pau Monné M_WAITOK, &txq->lock); 85796375eacSRoger Pau Monné TASK_INIT(&txq->defrtask, 0, xn_txq_tq_deferred, txq); 85896375eacSRoger Pau Monné 859da695b05SRoger Pau Monné txq->tq = taskqueue_create(txq->name, M_WAITOK, 86096375eacSRoger Pau Monné taskqueue_thread_enqueue, &txq->tq); 86196375eacSRoger Pau Monné 86296375eacSRoger Pau Monné error = taskqueue_start_threads(&txq->tq, 1, PI_NET, 86396375eacSRoger Pau Monné "%s txq %d", device_get_nameunit(dev), txq->id); 86496375eacSRoger Pau Monné if (error != 0) { 86596375eacSRoger Pau Monné device_printf(dev, "failed to start tx taskq %d\n", 86696375eacSRoger Pau Monné txq->id); 86796375eacSRoger Pau Monné goto fail_start_thread; 86896375eacSRoger Pau Monné } 86996375eacSRoger Pau Monné 87096375eacSRoger Pau Monné error = xen_intr_alloc_and_bind_local_port(dev, 871da695b05SRoger Pau Monné xenbus_get_otherend_id(dev), /* filter */ NULL, xn_intr, 872da695b05SRoger Pau Monné &info->txq[q], INTR_TYPE_NET | INTR_MPSAFE | INTR_ENTROPY, 87396375eacSRoger Pau Monné &txq->xen_intr_handle); 87496375eacSRoger Pau Monné 87596375eacSRoger Pau Monné if (error != 0) { 87696375eacSRoger Pau Monné device_printf(dev, "xen_intr_alloc_and_bind_local_port failed\n"); 87796375eacSRoger Pau Monné goto fail_bind_port; 87896375eacSRoger Pau Monné } 87996375eacSRoger Pau Monné } 88096375eacSRoger Pau Monné 88196375eacSRoger Pau Monné return (0); 88296375eacSRoger Pau Monné 88396375eacSRoger Pau Monné fail_bind_port: 88496375eacSRoger Pau Monné taskqueue_drain_all(txq->tq); 88596375eacSRoger Pau Monné fail_start_thread: 88696375eacSRoger Pau Monné buf_ring_free(txq->br, M_DEVBUF); 88796375eacSRoger Pau Monné taskqueue_free(txq->tq); 888d039b070SRoger Pau Monné gnttab_end_foreign_access(txq->ring_ref, NULL); 88996375eacSRoger Pau Monné fail_grant_ring: 89096375eacSRoger Pau Monné gnttab_free_grant_references(txq->gref_head); 89196375eacSRoger Pau Monné free(txq->ring.sring, M_DEVBUF); 89296375eacSRoger Pau Monné fail: 89396375eacSRoger Pau Monné for (; q >= 0; q--) { 89496375eacSRoger Pau Monné disconnect_txq(&info->txq[q]); 89596375eacSRoger Pau Monné destroy_txq(&info->txq[q]); 89696375eacSRoger Pau Monné } 89796375eacSRoger Pau Monné 89896375eacSRoger Pau Monné free(info->txq, M_DEVBUF); 89996375eacSRoger Pau Monné return (error); 90096375eacSRoger Pau Monné } 90196375eacSRoger Pau Monné 90296375eacSRoger Pau Monné static int 90396375eacSRoger Pau Monné setup_device(device_t dev, struct netfront_info *info, 90496375eacSRoger Pau Monné unsigned long num_queues) 90596375eacSRoger Pau Monné { 90696375eacSRoger Pau Monné int error; 90796375eacSRoger Pau Monné int q; 90896375eacSRoger Pau Monné 90996375eacSRoger Pau Monné if (info->txq) 91096375eacSRoger Pau Monné destroy_txqs(info); 91196375eacSRoger Pau Monné 91296375eacSRoger Pau Monné if (info->rxq) 91396375eacSRoger Pau Monné destroy_rxqs(info); 91496375eacSRoger Pau Monné 91596375eacSRoger Pau Monné info->num_queues = 0; 91696375eacSRoger Pau Monné 91796375eacSRoger Pau Monné error = setup_rxqs(dev, info, num_queues); 91896375eacSRoger Pau Monné if (error != 0) 91996375eacSRoger Pau Monné goto out; 92096375eacSRoger Pau Monné error = setup_txqs(dev, info, num_queues); 92196375eacSRoger Pau Monné if (error != 0) 92296375eacSRoger Pau Monné goto out; 92396375eacSRoger Pau Monné 92496375eacSRoger Pau Monné info->num_queues = num_queues; 92596375eacSRoger Pau Monné 92696375eacSRoger Pau Monné /* No split event channel at the moment. */ 92796375eacSRoger Pau Monné for (q = 0; q < num_queues; q++) 92896375eacSRoger Pau Monné info->rxq[q].xen_intr_handle = info->txq[q].xen_intr_handle; 92996375eacSRoger Pau Monné 93096375eacSRoger Pau Monné return (0); 93196375eacSRoger Pau Monné 93296375eacSRoger Pau Monné out: 93396375eacSRoger Pau Monné KASSERT(error != 0, ("Error path taken without providing an error code")); 9343a6d1fcfSKip Macy return (error); 93589e0f4d2SKip Macy } 93689e0f4d2SKip Macy 937a0ae8f04SBjoern A. Zeeb #ifdef INET 93889e0f4d2SKip Macy /** 93912678024SDoug Rabson * If this interface has an ipv4 address, send an arp for it. This 94012678024SDoug Rabson * helps to get the network going again after migrating hosts. 94112678024SDoug Rabson */ 94212678024SDoug Rabson static void 94312678024SDoug Rabson netfront_send_fake_arp(device_t dev, struct netfront_info *info) 94412678024SDoug Rabson { 94512678024SDoug Rabson struct ifnet *ifp; 94612678024SDoug Rabson struct ifaddr *ifa; 94712678024SDoug Rabson 94812678024SDoug Rabson ifp = info->xn_ifp; 94912678024SDoug Rabson TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 95012678024SDoug Rabson if (ifa->ifa_addr->sa_family == AF_INET) { 95112678024SDoug Rabson arp_ifinit(ifp, ifa); 95212678024SDoug Rabson } 95312678024SDoug Rabson } 95412678024SDoug Rabson } 955a0ae8f04SBjoern A. Zeeb #endif 95612678024SDoug Rabson 95712678024SDoug Rabson /** 95889e0f4d2SKip Macy * Callback received when the backend's state changes. 95989e0f4d2SKip Macy */ 960283d6f72SJustin T. Gibbs static void 96123dc5621SKip Macy netfront_backend_changed(device_t dev, XenbusState newstate) 96289e0f4d2SKip Macy { 96323dc5621SKip Macy struct netfront_info *sc = device_get_softc(dev); 96489e0f4d2SKip Macy 96523dc5621SKip Macy DPRINTK("newstate=%d\n", newstate); 96689e0f4d2SKip Macy 96723dc5621SKip Macy switch (newstate) { 96889e0f4d2SKip Macy case XenbusStateInitialising: 96989e0f4d2SKip Macy case XenbusStateInitialised: 97089e0f4d2SKip Macy case XenbusStateUnknown: 971920ba15bSKip Macy case XenbusStateReconfigured: 972920ba15bSKip Macy case XenbusStateReconfiguring: 97389e0f4d2SKip Macy break; 97489e0f4d2SKip Macy case XenbusStateInitWait: 97523dc5621SKip Macy if (xenbus_get_state(dev) != XenbusStateInitialising) 97689e0f4d2SKip Macy break; 97796375eacSRoger Pau Monné if (xn_connect(sc) != 0) 97889e0f4d2SKip Macy break; 97923dc5621SKip Macy xenbus_set_state(dev, XenbusStateConnected); 98023dc5621SKip Macy break; 98189e0f4d2SKip Macy case XenbusStateClosing: 98223dc5621SKip Macy xenbus_set_state(dev, XenbusStateClosed); 98389e0f4d2SKip Macy break; 984*c2d12e5eSRoger Pau Monné case XenbusStateClosed: 985*c2d12e5eSRoger Pau Monné if (sc->xn_reset) { 986*c2d12e5eSRoger Pau Monné netif_disconnect_backend(sc); 987*c2d12e5eSRoger Pau Monné xenbus_set_state(dev, XenbusStateInitialising); 988*c2d12e5eSRoger Pau Monné sc->xn_reset = false; 989*c2d12e5eSRoger Pau Monné } 990*c2d12e5eSRoger Pau Monné break; 991dbf82bdeSRoger Pau Monné case XenbusStateConnected: 992dbf82bdeSRoger Pau Monné #ifdef INET 993dbf82bdeSRoger Pau Monné netfront_send_fake_arp(dev, sc); 994dbf82bdeSRoger Pau Monné #endif 995dbf82bdeSRoger Pau Monné break; 99689e0f4d2SKip Macy } 99789e0f4d2SKip Macy } 99889e0f4d2SKip Macy 999931eeffaSKenneth D. Merry /** 1000931eeffaSKenneth D. Merry * \brief Verify that there is sufficient space in the Tx ring 1001931eeffaSKenneth D. Merry * buffer for a maximally sized request to be enqueued. 1002c099cafaSAdrian Chadd * 1003931eeffaSKenneth D. Merry * A transmit request requires a transmit descriptor for each packet 1004931eeffaSKenneth D. Merry * fragment, plus up to 2 entries for "options" (e.g. TSO). 1005c099cafaSAdrian Chadd */ 100689e0f4d2SKip Macy static inline int 100796375eacSRoger Pau Monné xn_tx_slot_available(struct netfront_txq *txq) 100889e0f4d2SKip Macy { 100996375eacSRoger Pau Monné 101096375eacSRoger Pau Monné return (RING_FREE_REQUESTS(&txq->ring) > (MAX_TX_REQ_FRAGS + 2)); 101189e0f4d2SKip Macy } 1012931eeffaSKenneth D. Merry 101389e0f4d2SKip Macy static void 101496375eacSRoger Pau Monné xn_release_tx_bufs(struct netfront_txq *txq) 101589e0f4d2SKip Macy { 101689e0f4d2SKip Macy int i; 101789e0f4d2SKip Macy 101889e0f4d2SKip Macy for (i = 1; i <= NET_TX_RING_SIZE; i++) { 1019931eeffaSKenneth D. Merry struct mbuf *m; 102089e0f4d2SKip Macy 102196375eacSRoger Pau Monné m = txq->mbufs[i]; 1022931eeffaSKenneth D. Merry 1023931eeffaSKenneth D. Merry /* 1024931eeffaSKenneth D. Merry * We assume that no kernel addresses are 1025931eeffaSKenneth D. Merry * less than NET_TX_RING_SIZE. Any entry 1026931eeffaSKenneth D. Merry * in the table that is below this number 1027931eeffaSKenneth D. Merry * must be an index from free-list tracking. 1028931eeffaSKenneth D. Merry */ 1029931eeffaSKenneth D. Merry if (((uintptr_t)m) <= NET_TX_RING_SIZE) 103089e0f4d2SKip Macy continue; 103196375eacSRoger Pau Monné gnttab_end_foreign_access_ref(txq->grant_ref[i]); 103296375eacSRoger Pau Monné gnttab_release_grant_reference(&txq->gref_head, 103396375eacSRoger Pau Monné txq->grant_ref[i]); 103496375eacSRoger Pau Monné txq->grant_ref[i] = GRANT_REF_INVALID; 103596375eacSRoger Pau Monné add_id_to_freelist(txq->mbufs, i); 103696375eacSRoger Pau Monné txq->mbufs_cnt--; 103796375eacSRoger Pau Monné if (txq->mbufs_cnt < 0) { 10386f9767acSMarius Strobl panic("%s: tx_chain_cnt must be >= 0", __func__); 1039a4ec37f5SAdrian Chadd } 1040cf9c09e1SJustin T. Gibbs m_free(m); 104189e0f4d2SKip Macy } 104289e0f4d2SKip Macy } 104389e0f4d2SKip Macy 10442568ee67SRoger Pau Monné static struct mbuf * 10452568ee67SRoger Pau Monné xn_alloc_one_rx_buffer(struct netfront_rxq *rxq) 10462568ee67SRoger Pau Monné { 10472568ee67SRoger Pau Monné struct mbuf *m; 10482568ee67SRoger Pau Monné 10492568ee67SRoger Pau Monné m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 10502568ee67SRoger Pau Monné if (m == NULL) 10512568ee67SRoger Pau Monné return NULL; 10522568ee67SRoger Pau Monné m->m_len = m->m_pkthdr.len = MJUMPAGESIZE; 10532568ee67SRoger Pau Monné 10542568ee67SRoger Pau Monné return (m); 10552568ee67SRoger Pau Monné } 10562568ee67SRoger Pau Monné 105789e0f4d2SKip Macy static void 105896375eacSRoger Pau Monné xn_alloc_rx_buffers(struct netfront_rxq *rxq) 105989e0f4d2SKip Macy { 106089e0f4d2SKip Macy RING_IDX req_prod; 10612568ee67SRoger Pau Monné int notify; 10622568ee67SRoger Pau Monné 10632568ee67SRoger Pau Monné XN_RX_LOCK_ASSERT(rxq); 10642568ee67SRoger Pau Monné 10652568ee67SRoger Pau Monné if (__predict_false(rxq->info->carrier == 0)) 10662568ee67SRoger Pau Monné return; 10672568ee67SRoger Pau Monné 10682568ee67SRoger Pau Monné for (req_prod = rxq->ring.req_prod_pvt; 10692568ee67SRoger Pau Monné req_prod - rxq->ring.rsp_cons < NET_RX_RING_SIZE; 10702568ee67SRoger Pau Monné req_prod++) { 10712568ee67SRoger Pau Monné struct mbuf *m; 10722568ee67SRoger Pau Monné unsigned short id; 107389e0f4d2SKip Macy grant_ref_t ref; 10742568ee67SRoger Pau Monné struct netif_rx_request *req; 10752568ee67SRoger Pau Monné unsigned long pfn; 107689e0f4d2SKip Macy 10772568ee67SRoger Pau Monné m = xn_alloc_one_rx_buffer(rxq); 10782568ee67SRoger Pau Monné if (m == NULL) 107989e0f4d2SKip Macy break; 108089e0f4d2SKip Macy 10812568ee67SRoger Pau Monné id = xn_rxidx(req_prod); 108289e0f4d2SKip Macy 108396375eacSRoger Pau Monné KASSERT(rxq->mbufs[id] == NULL, ("non-NULL xn_rx_chain")); 10842568ee67SRoger Pau Monné rxq->mbufs[id] = m; 108589e0f4d2SKip Macy 108696375eacSRoger Pau Monné ref = gnttab_claim_grant_reference(&rxq->gref_head); 1087ff662b5cSJustin T. Gibbs KASSERT(ref != GNTTAB_LIST_END, 1088ff662b5cSJustin T. Gibbs ("reserved grant references exhuasted")); 108996375eacSRoger Pau Monné rxq->grant_ref[id] = ref; 109089e0f4d2SKip Macy 10912568ee67SRoger Pau Monné pfn = atop(vtophys(mtod(m, vm_offset_t))); 10922568ee67SRoger Pau Monné req = RING_GET_REQUEST(&rxq->ring, req_prod); 109389e0f4d2SKip Macy 10942568ee67SRoger Pau Monné gnttab_grant_foreign_access_ref(ref, 10952568ee67SRoger Pau Monné xenbus_get_otherend_id(rxq->info->xbdev), pfn, 0); 109689e0f4d2SKip Macy req->id = id; 109789e0f4d2SKip Macy req->gref = ref; 109889e0f4d2SKip Macy } 109989e0f4d2SKip Macy 11002568ee67SRoger Pau Monné rxq->ring.req_prod_pvt = req_prod; 110189e0f4d2SKip Macy 11022568ee67SRoger Pau Monné /* Not enough requests? Try again later. */ 11032568ee67SRoger Pau Monné if (req_prod - rxq->ring.rsp_cons < NET_RX_SLOTS_MIN) { 11042568ee67SRoger Pau Monné callout_reset(&rxq->rx_refill, hz/10, xn_alloc_rx_buffers_callout, 11052568ee67SRoger Pau Monné rxq); 11062568ee67SRoger Pau Monné return; 11072568ee67SRoger Pau Monné } 11082568ee67SRoger Pau Monné 11092568ee67SRoger Pau Monné wmb(); /* barrier so backend seens requests */ 11102568ee67SRoger Pau Monné 111196375eacSRoger Pau Monné RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rxq->ring, notify); 111289e0f4d2SKip Macy if (notify) 111396375eacSRoger Pau Monné xen_intr_signal(rxq->xen_intr_handle); 111489e0f4d2SKip Macy } 111589e0f4d2SKip Macy 11162568ee67SRoger Pau Monné static void xn_alloc_rx_buffers_callout(void *arg) 11172568ee67SRoger Pau Monné { 11182568ee67SRoger Pau Monné struct netfront_rxq *rxq; 11192568ee67SRoger Pau Monné 11202568ee67SRoger Pau Monné rxq = (struct netfront_rxq *)arg; 11212568ee67SRoger Pau Monné XN_RX_LOCK(rxq); 11222568ee67SRoger Pau Monné xn_alloc_rx_buffers(rxq); 11232568ee67SRoger Pau Monné XN_RX_UNLOCK(rxq); 11242568ee67SRoger Pau Monné } 11252568ee67SRoger Pau Monné 112689e0f4d2SKip Macy static void 112796375eacSRoger Pau Monné xn_release_rx_bufs(struct netfront_rxq *rxq) 112896375eacSRoger Pau Monné { 112996375eacSRoger Pau Monné int i, ref; 113096375eacSRoger Pau Monné struct mbuf *m; 113196375eacSRoger Pau Monné 113296375eacSRoger Pau Monné for (i = 0; i < NET_RX_RING_SIZE; i++) { 113396375eacSRoger Pau Monné m = rxq->mbufs[i]; 113496375eacSRoger Pau Monné 113596375eacSRoger Pau Monné if (m == NULL) 113696375eacSRoger Pau Monné continue; 113796375eacSRoger Pau Monné 113896375eacSRoger Pau Monné ref = rxq->grant_ref[i]; 113996375eacSRoger Pau Monné if (ref == GRANT_REF_INVALID) 114096375eacSRoger Pau Monné continue; 114196375eacSRoger Pau Monné 114296375eacSRoger Pau Monné gnttab_end_foreign_access_ref(ref); 114396375eacSRoger Pau Monné gnttab_release_grant_reference(&rxq->gref_head, ref); 114496375eacSRoger Pau Monné rxq->mbufs[i] = NULL; 114596375eacSRoger Pau Monné rxq->grant_ref[i] = GRANT_REF_INVALID; 114696375eacSRoger Pau Monné m_freem(m); 114796375eacSRoger Pau Monné } 114896375eacSRoger Pau Monné } 114996375eacSRoger Pau Monné 115096375eacSRoger Pau Monné static void 115196375eacSRoger Pau Monné xn_rxeof(struct netfront_rxq *rxq) 115289e0f4d2SKip Macy { 115389e0f4d2SKip Macy struct ifnet *ifp; 115496375eacSRoger Pau Monné struct netfront_info *np = rxq->info; 11553778878dSRoger Pau Monné #if (defined(INET) || defined(INET6)) 115696375eacSRoger Pau Monné struct lro_ctrl *lro = &rxq->lro; 11573778878dSRoger Pau Monné #endif 115889e0f4d2SKip Macy struct netfront_rx_info rinfo; 115989e0f4d2SKip Macy struct netif_rx_response *rx = &rinfo.rx; 116089e0f4d2SKip Macy struct netif_extra_info *extras = rinfo.extras; 116189e0f4d2SKip Macy RING_IDX i, rp; 116289e0f4d2SKip Macy struct mbuf *m; 116396375eacSRoger Pau Monné struct mbufq mbufq_rxq, mbufq_errq; 1164d0f3a8b9SRoger Pau Monné int err, work_to_do; 116589e0f4d2SKip Macy 116649906218SDoug Rabson do { 116796375eacSRoger Pau Monné XN_RX_LOCK_ASSERT(rxq); 116889e0f4d2SKip Macy if (!netfront_carrier_ok(np)) 116989e0f4d2SKip Macy return; 117089e0f4d2SKip Macy 1171c578b6acSGleb Smirnoff /* XXX: there should be some sane limit. */ 117296375eacSRoger Pau Monné mbufq_init(&mbufq_errq, INT_MAX); 117396375eacSRoger Pau Monné mbufq_init(&mbufq_rxq, INT_MAX); 117489e0f4d2SKip Macy 117589e0f4d2SKip Macy ifp = np->xn_ifp; 117689e0f4d2SKip Macy 117796375eacSRoger Pau Monné rp = rxq->ring.sring->rsp_prod; 117889e0f4d2SKip Macy rmb(); /* Ensure we see queued responses up to 'rp'. */ 117989e0f4d2SKip Macy 118096375eacSRoger Pau Monné i = rxq->ring.rsp_cons; 118189e0f4d2SKip Macy while ((i != rp)) { 118296375eacSRoger Pau Monné memcpy(rx, RING_GET_RESPONSE(&rxq->ring, i), sizeof(*rx)); 118389e0f4d2SKip Macy memset(extras, 0, sizeof(rinfo.extras)); 118489e0f4d2SKip Macy 118583b92f6eSKip Macy m = NULL; 118696375eacSRoger Pau Monné err = xn_get_responses(rxq, &rinfo, rp, &i, &m); 118789e0f4d2SKip Macy 118876acc41fSJustin T. Gibbs if (__predict_false(err)) { 118983b92f6eSKip Macy if (m) 119096375eacSRoger Pau Monné (void )mbufq_enqueue(&mbufq_errq, m); 119196375eacSRoger Pau Monné rxq->stats.rx_errors++; 119289e0f4d2SKip Macy continue; 119389e0f4d2SKip Macy } 119489e0f4d2SKip Macy 119589e0f4d2SKip Macy m->m_pkthdr.rcvif = ifp; 119689e0f4d2SKip Macy if ( rx->flags & NETRXF_data_validated ) { 119789e0f4d2SKip Macy /* Tell the stack the checksums are okay */ 119889e0f4d2SKip Macy /* 119989e0f4d2SKip Macy * XXX this isn't necessarily the case - need to add 120089e0f4d2SKip Macy * check 120189e0f4d2SKip Macy */ 120289e0f4d2SKip Macy 120389e0f4d2SKip Macy m->m_pkthdr.csum_flags |= 120489e0f4d2SKip Macy (CSUM_IP_CHECKED | CSUM_IP_VALID | CSUM_DATA_VALID 120589e0f4d2SKip Macy | CSUM_PSEUDO_HDR); 120689e0f4d2SKip Macy m->m_pkthdr.csum_data = 0xffff; 120789e0f4d2SKip Macy } 1208d9a66b6dSRoger Pau Monné if ((rx->flags & NETRXF_extra_info) != 0 && 1209d9a66b6dSRoger Pau Monné (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type == 1210d9a66b6dSRoger Pau Monné XEN_NETIF_EXTRA_TYPE_GSO)) { 1211d9a66b6dSRoger Pau Monné m->m_pkthdr.tso_segsz = 1212d9a66b6dSRoger Pau Monné extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].u.gso.size; 1213d9a66b6dSRoger Pau Monné m->m_pkthdr.csum_flags |= CSUM_TSO; 1214d9a66b6dSRoger Pau Monné } 121589e0f4d2SKip Macy 121696375eacSRoger Pau Monné rxq->stats.rx_packets++; 121796375eacSRoger Pau Monné rxq->stats.rx_bytes += m->m_pkthdr.len; 121889e0f4d2SKip Macy 121996375eacSRoger Pau Monné (void )mbufq_enqueue(&mbufq_rxq, m); 122096375eacSRoger Pau Monné rxq->ring.rsp_cons = i; 122189e0f4d2SKip Macy } 122289e0f4d2SKip Macy 122396375eacSRoger Pau Monné mbufq_drain(&mbufq_errq); 122489e0f4d2SKip Macy 122589e0f4d2SKip Macy /* 122689e0f4d2SKip Macy * Process all the mbufs after the remapping is complete. 122789e0f4d2SKip Macy * Break the mbuf chain first though. 122889e0f4d2SKip Macy */ 122996375eacSRoger Pau Monné while ((m = mbufq_dequeue(&mbufq_rxq)) != NULL) { 1230c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1); 123189e0f4d2SKip Macy 123296375eacSRoger Pau Monné /* XXX: Do we really need to drop the rx lock? */ 123396375eacSRoger Pau Monné XN_RX_UNLOCK(rxq); 123408c9c2e0SRoger Pau Monné #if (defined(INET) || defined(INET6)) 123512678024SDoug Rabson /* Use LRO if possible */ 123612678024SDoug Rabson if ((ifp->if_capenable & IFCAP_LRO) == 0 || 123712678024SDoug Rabson lro->lro_cnt == 0 || tcp_lro_rx(lro, m, 0)) { 123812678024SDoug Rabson /* 123912678024SDoug Rabson * If LRO fails, pass up to the stack 124012678024SDoug Rabson * directly. 124112678024SDoug Rabson */ 124289e0f4d2SKip Macy (*ifp->if_input)(ifp, m); 124312678024SDoug Rabson } 124412678024SDoug Rabson #else 124512678024SDoug Rabson (*ifp->if_input)(ifp, m); 124612678024SDoug Rabson #endif 124796375eacSRoger Pau Monné 124896375eacSRoger Pau Monné XN_RX_LOCK(rxq); 124989e0f4d2SKip Macy } 125089e0f4d2SKip Macy 125196375eacSRoger Pau Monné rxq->ring.rsp_cons = i; 125289e0f4d2SKip Macy 125308c9c2e0SRoger Pau Monné #if (defined(INET) || defined(INET6)) 125412678024SDoug Rabson /* 125512678024SDoug Rabson * Flush any outstanding LRO work 125612678024SDoug Rabson */ 12576dd38b87SSepherosa Ziehau tcp_lro_flush_all(lro); 125812678024SDoug Rabson #endif 125912678024SDoug Rabson 126096375eacSRoger Pau Monné xn_alloc_rx_buffers(rxq); 126189e0f4d2SKip Macy 126296375eacSRoger Pau Monné RING_FINAL_CHECK_FOR_RESPONSES(&rxq->ring, work_to_do); 126349906218SDoug Rabson } while (work_to_do); 126489e0f4d2SKip Macy } 126589e0f4d2SKip Macy 126689e0f4d2SKip Macy static void 126796375eacSRoger Pau Monné xn_txeof(struct netfront_txq *txq) 126889e0f4d2SKip Macy { 126989e0f4d2SKip Macy RING_IDX i, prod; 127089e0f4d2SKip Macy unsigned short id; 127189e0f4d2SKip Macy struct ifnet *ifp; 127212678024SDoug Rabson netif_tx_response_t *txr; 127389e0f4d2SKip Macy struct mbuf *m; 127496375eacSRoger Pau Monné struct netfront_info *np = txq->info; 127589e0f4d2SKip Macy 127696375eacSRoger Pau Monné XN_TX_LOCK_ASSERT(txq); 127789e0f4d2SKip Macy 127889e0f4d2SKip Macy if (!netfront_carrier_ok(np)) 127989e0f4d2SKip Macy return; 128089e0f4d2SKip Macy 128189e0f4d2SKip Macy ifp = np->xn_ifp; 128289e0f4d2SKip Macy 128389e0f4d2SKip Macy do { 128496375eacSRoger Pau Monné prod = txq->ring.sring->rsp_prod; 128589e0f4d2SKip Macy rmb(); /* Ensure we see responses up to 'rp'. */ 128689e0f4d2SKip Macy 128796375eacSRoger Pau Monné for (i = txq->ring.rsp_cons; i != prod; i++) { 128896375eacSRoger Pau Monné txr = RING_GET_RESPONSE(&txq->ring, i); 128912678024SDoug Rabson if (txr->status == NETIF_RSP_NULL) 129012678024SDoug Rabson continue; 129112678024SDoug Rabson 1292931eeffaSKenneth D. Merry if (txr->status != NETIF_RSP_OKAY) { 1293931eeffaSKenneth D. Merry printf("%s: WARNING: response is %d!\n", 1294931eeffaSKenneth D. Merry __func__, txr->status); 1295931eeffaSKenneth D. Merry } 129612678024SDoug Rabson id = txr->id; 129796375eacSRoger Pau Monné m = txq->mbufs[id]; 129896375eacSRoger Pau Monné KASSERT(m != NULL, ("mbuf not found in chain")); 1299931eeffaSKenneth D. Merry KASSERT((uintptr_t)m > NET_TX_RING_SIZE, 1300931eeffaSKenneth D. Merry ("mbuf already on the free list, but we're " 1301931eeffaSKenneth D. Merry "trying to free it again!")); 13022d8fae98SAdrian Chadd M_ASSERTVALID(m); 130389e0f4d2SKip Macy 130412678024SDoug Rabson /* 130512678024SDoug Rabson * Increment packet count if this is the last 130612678024SDoug Rabson * mbuf of the chain. 130712678024SDoug Rabson */ 130812678024SDoug Rabson if (!m->m_next) 1309c8dfaf38SGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1); 131076acc41fSJustin T. Gibbs if (__predict_false(gnttab_query_foreign_access( 131196375eacSRoger Pau Monné txq->grant_ref[id]) != 0)) { 13126f9767acSMarius Strobl panic("%s: grant id %u still in use by the " 13136f9767acSMarius Strobl "backend", __func__, id); 131489e0f4d2SKip Macy } 131596375eacSRoger Pau Monné gnttab_end_foreign_access_ref(txq->grant_ref[id]); 131689e0f4d2SKip Macy gnttab_release_grant_reference( 131796375eacSRoger Pau Monné &txq->gref_head, txq->grant_ref[id]); 131896375eacSRoger Pau Monné txq->grant_ref[id] = GRANT_REF_INVALID; 131989e0f4d2SKip Macy 132096375eacSRoger Pau Monné txq->mbufs[id] = NULL; 132196375eacSRoger Pau Monné add_id_to_freelist(txq->mbufs, id); 132296375eacSRoger Pau Monné txq->mbufs_cnt--; 132312678024SDoug Rabson m_free(m); 132496375eacSRoger Pau Monné /* Only mark the txq active if we've freed up at least one slot to try */ 1325d76e4550SAdrian Chadd ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 132689e0f4d2SKip Macy } 132796375eacSRoger Pau Monné txq->ring.rsp_cons = prod; 132889e0f4d2SKip Macy 132989e0f4d2SKip Macy /* 133089e0f4d2SKip Macy * Set a new event, then check for race with update of 133189e0f4d2SKip Macy * tx_cons. Note that it is essential to schedule a 133289e0f4d2SKip Macy * callback, no matter how few buffers are pending. Even if 133389e0f4d2SKip Macy * there is space in the transmit ring, higher layers may 133489e0f4d2SKip Macy * be blocked because too much data is outstanding: in such 133589e0f4d2SKip Macy * cases notification from Xen is likely to be the only kick 133689e0f4d2SKip Macy * that we'll get. 133789e0f4d2SKip Macy */ 133896375eacSRoger Pau Monné txq->ring.sring->rsp_event = 133996375eacSRoger Pau Monné prod + ((txq->ring.sring->req_prod - prod) >> 1) + 1; 134089e0f4d2SKip Macy 134189e0f4d2SKip Macy mb(); 134296375eacSRoger Pau Monné } while (prod != txq->ring.sring->rsp_prod); 134389e0f4d2SKip Macy 134496375eacSRoger Pau Monné if (txq->full && 134596375eacSRoger Pau Monné ((txq->ring.sring->req_prod - prod) < NET_TX_RING_SIZE)) { 134696375eacSRoger Pau Monné txq->full = false; 1347da695b05SRoger Pau Monné xn_txq_start(txq); 134889e0f4d2SKip Macy } 134989e0f4d2SKip Macy } 135089e0f4d2SKip Macy 135189e0f4d2SKip Macy static void 135296375eacSRoger Pau Monné xn_intr(void *xsc) 135396375eacSRoger Pau Monné { 135496375eacSRoger Pau Monné struct netfront_txq *txq = xsc; 135596375eacSRoger Pau Monné struct netfront_info *np = txq->info; 135696375eacSRoger Pau Monné struct netfront_rxq *rxq = &np->rxq[txq->id]; 135796375eacSRoger Pau Monné 135896375eacSRoger Pau Monné /* kick both tx and rx */ 135996375eacSRoger Pau Monné xn_rxq_intr(rxq); 136096375eacSRoger Pau Monné xn_txq_intr(txq); 136196375eacSRoger Pau Monné } 136296375eacSRoger Pau Monné 136396375eacSRoger Pau Monné static void 136496375eacSRoger Pau Monné xn_move_rx_slot(struct netfront_rxq *rxq, struct mbuf *m, 136596375eacSRoger Pau Monné grant_ref_t ref) 136696375eacSRoger Pau Monné { 136796375eacSRoger Pau Monné int new = xn_rxidx(rxq->ring.req_prod_pvt); 136896375eacSRoger Pau Monné 136996375eacSRoger Pau Monné KASSERT(rxq->mbufs[new] == NULL, ("mbufs != NULL")); 137096375eacSRoger Pau Monné rxq->mbufs[new] = m; 137196375eacSRoger Pau Monné rxq->grant_ref[new] = ref; 137296375eacSRoger Pau Monné RING_GET_REQUEST(&rxq->ring, rxq->ring.req_prod_pvt)->id = new; 137396375eacSRoger Pau Monné RING_GET_REQUEST(&rxq->ring, rxq->ring.req_prod_pvt)->gref = ref; 137496375eacSRoger Pau Monné rxq->ring.req_prod_pvt++; 137596375eacSRoger Pau Monné } 137696375eacSRoger Pau Monné 137796375eacSRoger Pau Monné static int 137896375eacSRoger Pau Monné xn_get_extras(struct netfront_rxq *rxq, 1379931eeffaSKenneth D. Merry struct netif_extra_info *extras, RING_IDX rp, RING_IDX *cons) 138089e0f4d2SKip Macy { 138189e0f4d2SKip Macy struct netif_extra_info *extra; 138289e0f4d2SKip Macy 138389e0f4d2SKip Macy int err = 0; 138489e0f4d2SKip Macy 138589e0f4d2SKip Macy do { 138689e0f4d2SKip Macy struct mbuf *m; 138789e0f4d2SKip Macy grant_ref_t ref; 138889e0f4d2SKip Macy 138976acc41fSJustin T. Gibbs if (__predict_false(*cons + 1 == rp)) { 1390931eeffaSKenneth D. Merry err = EINVAL; 139189e0f4d2SKip Macy break; 139289e0f4d2SKip Macy } 139389e0f4d2SKip Macy 139489e0f4d2SKip Macy extra = (struct netif_extra_info *) 139596375eacSRoger Pau Monné RING_GET_RESPONSE(&rxq->ring, ++(*cons)); 139689e0f4d2SKip Macy 139776acc41fSJustin T. Gibbs if (__predict_false(!extra->type || 139889e0f4d2SKip Macy extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) { 1399931eeffaSKenneth D. Merry err = EINVAL; 140089e0f4d2SKip Macy } else { 140189e0f4d2SKip Macy memcpy(&extras[extra->type - 1], extra, sizeof(*extra)); 140289e0f4d2SKip Macy } 140389e0f4d2SKip Macy 140496375eacSRoger Pau Monné m = xn_get_rx_mbuf(rxq, *cons); 140596375eacSRoger Pau Monné ref = xn_get_rx_ref(rxq, *cons); 140696375eacSRoger Pau Monné xn_move_rx_slot(rxq, m, ref); 140789e0f4d2SKip Macy } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE); 140889e0f4d2SKip Macy 140989e0f4d2SKip Macy return err; 141089e0f4d2SKip Macy } 141189e0f4d2SKip Macy 141289e0f4d2SKip Macy static int 141396375eacSRoger Pau Monné xn_get_responses(struct netfront_rxq *rxq, 1414931eeffaSKenneth D. Merry struct netfront_rx_info *rinfo, RING_IDX rp, RING_IDX *cons, 1415d0f3a8b9SRoger Pau Monné struct mbuf **list) 141689e0f4d2SKip Macy { 141789e0f4d2SKip Macy struct netif_rx_response *rx = &rinfo->rx; 141889e0f4d2SKip Macy struct netif_extra_info *extras = rinfo->extras; 141983b92f6eSKip Macy struct mbuf *m, *m0, *m_prev; 142096375eacSRoger Pau Monné grant_ref_t ref = xn_get_rx_ref(rxq, *cons); 1421931eeffaSKenneth D. Merry RING_IDX ref_cons = *cons; 142289e0f4d2SKip Macy int frags = 1; 142389e0f4d2SKip Macy int err = 0; 142489e0f4d2SKip Macy u_long ret; 142589e0f4d2SKip Macy 142696375eacSRoger Pau Monné m0 = m = m_prev = xn_get_rx_mbuf(rxq, *cons); 142783b92f6eSKip Macy 142889e0f4d2SKip Macy if (rx->flags & NETRXF_extra_info) { 142996375eacSRoger Pau Monné err = xn_get_extras(rxq, extras, rp, cons); 143089e0f4d2SKip Macy } 143189e0f4d2SKip Macy 143283b92f6eSKip Macy if (m0 != NULL) { 143383b92f6eSKip Macy m0->m_pkthdr.len = 0; 143483b92f6eSKip Macy m0->m_next = NULL; 143583b92f6eSKip Macy } 143683b92f6eSKip Macy 143789e0f4d2SKip Macy for (;;) { 143883b92f6eSKip Macy #if 0 1439227ca257SKip Macy DPRINTK("rx->status=%hd rx->offset=%hu frags=%u\n", 144083b92f6eSKip Macy rx->status, rx->offset, frags); 144183b92f6eSKip Macy #endif 144276acc41fSJustin T. Gibbs if (__predict_false(rx->status < 0 || 144389e0f4d2SKip Macy rx->offset + rx->status > PAGE_SIZE)) { 1444931eeffaSKenneth D. Merry 144596375eacSRoger Pau Monné xn_move_rx_slot(rxq, m, ref); 1446931eeffaSKenneth D. Merry if (m0 == m) 1447931eeffaSKenneth D. Merry m0 = NULL; 1448931eeffaSKenneth D. Merry m = NULL; 1449931eeffaSKenneth D. Merry err = EINVAL; 1450931eeffaSKenneth D. Merry goto next_skip_queue; 145189e0f4d2SKip Macy } 145289e0f4d2SKip Macy 145389e0f4d2SKip Macy /* 145489e0f4d2SKip Macy * This definitely indicates a bug, either in this driver or in 145589e0f4d2SKip Macy * the backend driver. In future this should flag the bad 145689e0f4d2SKip Macy * situation to the system controller to reboot the backed. 145789e0f4d2SKip Macy */ 1458ff662b5cSJustin T. Gibbs if (ref == GRANT_REF_INVALID) { 1459ff662b5cSJustin T. Gibbs printf("%s: Bad rx response id %d.\n", __func__, rx->id); 1460931eeffaSKenneth D. Merry err = EINVAL; 146189e0f4d2SKip Macy goto next; 146289e0f4d2SKip Macy } 146389e0f4d2SKip Macy 1464920ba15bSKip Macy ret = gnttab_end_foreign_access_ref(ref); 1465d0f3a8b9SRoger Pau Monné KASSERT(ret, ("Unable to end access to grant references")); 146689e0f4d2SKip Macy 146796375eacSRoger Pau Monné gnttab_release_grant_reference(&rxq->gref_head, ref); 146889e0f4d2SKip Macy 146989e0f4d2SKip Macy next: 14703a539122SAdrian Chadd if (m == NULL) 14713a539122SAdrian Chadd break; 14723a539122SAdrian Chadd 147383b92f6eSKip Macy m->m_len = rx->status; 147483b92f6eSKip Macy m->m_data += rx->offset; 147583b92f6eSKip Macy m0->m_pkthdr.len += rx->status; 147683b92f6eSKip Macy 1477931eeffaSKenneth D. Merry next_skip_queue: 147889e0f4d2SKip Macy if (!(rx->flags & NETRXF_more_data)) 147989e0f4d2SKip Macy break; 148089e0f4d2SKip Macy 1481931eeffaSKenneth D. Merry if (*cons + frags == rp) { 148289e0f4d2SKip Macy if (net_ratelimit()) 148389e0f4d2SKip Macy WPRINTK("Need more frags\n"); 1484931eeffaSKenneth D. Merry err = ENOENT; 1485931eeffaSKenneth D. Merry printf("%s: cons %u frags %u rp %u, not enough frags\n", 1486931eeffaSKenneth D. Merry __func__, *cons, frags, rp); 148789e0f4d2SKip Macy break; 148889e0f4d2SKip Macy } 1489931eeffaSKenneth D. Merry /* 1490931eeffaSKenneth D. Merry * Note that m can be NULL, if rx->status < 0 or if 1491931eeffaSKenneth D. Merry * rx->offset + rx->status > PAGE_SIZE above. 1492931eeffaSKenneth D. Merry */ 149383b92f6eSKip Macy m_prev = m; 149489e0f4d2SKip Macy 149596375eacSRoger Pau Monné rx = RING_GET_RESPONSE(&rxq->ring, *cons + frags); 149696375eacSRoger Pau Monné m = xn_get_rx_mbuf(rxq, *cons + frags); 149783b92f6eSKip Macy 1498931eeffaSKenneth D. Merry /* 1499931eeffaSKenneth D. Merry * m_prev == NULL can happen if rx->status < 0 or if 1500931eeffaSKenneth D. Merry * rx->offset + * rx->status > PAGE_SIZE above. 1501931eeffaSKenneth D. Merry */ 1502931eeffaSKenneth D. Merry if (m_prev != NULL) 150383b92f6eSKip Macy m_prev->m_next = m; 1504931eeffaSKenneth D. Merry 1505931eeffaSKenneth D. Merry /* 1506931eeffaSKenneth D. Merry * m0 can be NULL if rx->status < 0 or if * rx->offset + 1507931eeffaSKenneth D. Merry * rx->status > PAGE_SIZE above. 1508931eeffaSKenneth D. Merry */ 1509931eeffaSKenneth D. Merry if (m0 == NULL) 1510931eeffaSKenneth D. Merry m0 = m; 151183b92f6eSKip Macy m->m_next = NULL; 151296375eacSRoger Pau Monné ref = xn_get_rx_ref(rxq, *cons + frags); 1513931eeffaSKenneth D. Merry ref_cons = *cons + frags; 151489e0f4d2SKip Macy frags++; 151589e0f4d2SKip Macy } 151683b92f6eSKip Macy *list = m0; 1517931eeffaSKenneth D. Merry *cons += frags; 151889e0f4d2SKip Macy 15198577146eSJustin T. Gibbs return (err); 152089e0f4d2SKip Macy } 152189e0f4d2SKip Macy 1522931eeffaSKenneth D. Merry /** 1523931eeffaSKenneth D. Merry * \brief Count the number of fragments in an mbuf chain. 1524931eeffaSKenneth D. Merry * 1525931eeffaSKenneth D. Merry * Surprisingly, there isn't an M* macro for this. 1526c099cafaSAdrian Chadd */ 1527931eeffaSKenneth D. Merry static inline int 1528931eeffaSKenneth D. Merry xn_count_frags(struct mbuf *m) 1529931eeffaSKenneth D. Merry { 1530931eeffaSKenneth D. Merry int nfrags; 1531931eeffaSKenneth D. Merry 1532931eeffaSKenneth D. Merry for (nfrags = 0; m != NULL; m = m->m_next) 1533931eeffaSKenneth D. Merry nfrags++; 1534931eeffaSKenneth D. Merry 1535931eeffaSKenneth D. Merry return (nfrags); 153689e0f4d2SKip Macy } 153789e0f4d2SKip Macy 1538931eeffaSKenneth D. Merry /** 1539931eeffaSKenneth D. Merry * Given an mbuf chain, make sure we have enough room and then push 1540931eeffaSKenneth D. Merry * it onto the transmit ring. 1541931eeffaSKenneth D. Merry */ 1542931eeffaSKenneth D. Merry static int 154396375eacSRoger Pau Monné xn_assemble_tx_request(struct netfront_txq *txq, struct mbuf *m_head) 1544931eeffaSKenneth D. Merry { 1545931eeffaSKenneth D. Merry struct mbuf *m; 154696375eacSRoger Pau Monné struct netfront_info *np = txq->info; 154796375eacSRoger Pau Monné struct ifnet *ifp = np->xn_ifp; 1548931eeffaSKenneth D. Merry u_int nfrags; 1549931eeffaSKenneth D. Merry int otherend_id; 1550931eeffaSKenneth D. Merry 1551931eeffaSKenneth D. Merry /** 155212678024SDoug Rabson * Defragment the mbuf if necessary. 155312678024SDoug Rabson */ 1554931eeffaSKenneth D. Merry nfrags = xn_count_frags(m_head); 1555931eeffaSKenneth D. Merry 1556931eeffaSKenneth D. Merry /* 1557931eeffaSKenneth D. Merry * Check to see whether this request is longer than netback 1558931eeffaSKenneth D. Merry * can handle, and try to defrag it. 1559931eeffaSKenneth D. Merry */ 1560931eeffaSKenneth D. Merry /** 1561931eeffaSKenneth D. Merry * It is a bit lame, but the netback driver in Linux can't 1562931eeffaSKenneth D. Merry * deal with nfrags > MAX_TX_REQ_FRAGS, which is a quirk of 1563931eeffaSKenneth D. Merry * the Linux network stack. 1564931eeffaSKenneth D. Merry */ 156596375eacSRoger Pau Monné if (nfrags > np->maxfrags) { 1566c6499eccSGleb Smirnoff m = m_defrag(m_head, M_NOWAIT); 156712678024SDoug Rabson if (!m) { 1568931eeffaSKenneth D. Merry /* 1569931eeffaSKenneth D. Merry * Defrag failed, so free the mbuf and 1570931eeffaSKenneth D. Merry * therefore drop the packet. 1571931eeffaSKenneth D. Merry */ 157212678024SDoug Rabson m_freem(m_head); 1573931eeffaSKenneth D. Merry return (EMSGSIZE); 157412678024SDoug Rabson } 157512678024SDoug Rabson m_head = m; 157612678024SDoug Rabson } 157789e0f4d2SKip Macy 1578a4ec37f5SAdrian Chadd /* Determine how many fragments now exist */ 1579931eeffaSKenneth D. Merry nfrags = xn_count_frags(m_head); 1580a4ec37f5SAdrian Chadd 1581a4ec37f5SAdrian Chadd /* 1582931eeffaSKenneth D. Merry * Check to see whether the defragmented packet has too many 1583931eeffaSKenneth D. Merry * segments for the Linux netback driver. 1584a4ec37f5SAdrian Chadd */ 1585931eeffaSKenneth D. Merry /** 1586931eeffaSKenneth D. Merry * The FreeBSD TCP stack, with TSO enabled, can produce a chain 1587931eeffaSKenneth D. Merry * of mbufs longer than Linux can handle. Make sure we don't 1588931eeffaSKenneth D. Merry * pass a too-long chain over to the other side by dropping the 1589931eeffaSKenneth D. Merry * packet. It doesn't look like there is currently a way to 1590931eeffaSKenneth D. Merry * tell the TCP stack to generate a shorter chain of packets. 15913fb28bbbSAdrian Chadd */ 1592931eeffaSKenneth D. Merry if (nfrags > MAX_TX_REQ_FRAGS) { 1593ff662b5cSJustin T. Gibbs #ifdef DEBUG 1594ff662b5cSJustin T. Gibbs printf("%s: nfrags %d > MAX_TX_REQ_FRAGS %d, netback " 1595ff662b5cSJustin T. Gibbs "won't be able to handle it, dropping\n", 1596ff662b5cSJustin T. Gibbs __func__, nfrags, MAX_TX_REQ_FRAGS); 1597ff662b5cSJustin T. Gibbs #endif 1598931eeffaSKenneth D. Merry m_freem(m_head); 1599931eeffaSKenneth D. Merry return (EMSGSIZE); 1600a4ec37f5SAdrian Chadd } 1601a4ec37f5SAdrian Chadd 16023fb28bbbSAdrian Chadd /* 1603931eeffaSKenneth D. Merry * This check should be redundant. We've already verified that we 1604931eeffaSKenneth D. Merry * have enough slots in the ring to handle a packet of maximum 1605931eeffaSKenneth D. Merry * size, and that our packet is less than the maximum size. Keep 1606931eeffaSKenneth D. Merry * it in here as an assert for now just to make certain that 160796375eacSRoger Pau Monné * chain_cnt is accurate. 16083fb28bbbSAdrian Chadd */ 160996375eacSRoger Pau Monné KASSERT((txq->mbufs_cnt + nfrags) <= NET_TX_RING_SIZE, 161096375eacSRoger Pau Monné ("%s: chain_cnt (%d) + nfrags (%d) > NET_TX_RING_SIZE " 161196375eacSRoger Pau Monné "(%d)!", __func__, (int) txq->mbufs_cnt, 1612931eeffaSKenneth D. Merry (int) nfrags, (int) NET_TX_RING_SIZE)); 1613a4ec37f5SAdrian Chadd 161489e0f4d2SKip Macy /* 161589e0f4d2SKip Macy * Start packing the mbufs in this chain into 161689e0f4d2SKip Macy * the fragment pointers. Stop when we run out 161789e0f4d2SKip Macy * of fragments or hit the end of the mbuf chain. 161889e0f4d2SKip Macy */ 161912678024SDoug Rabson m = m_head; 162096375eacSRoger Pau Monné otherend_id = xenbus_get_otherend_id(np->xbdev); 162112678024SDoug Rabson for (m = m_head; m; m = m->m_next) { 1622931eeffaSKenneth D. Merry netif_tx_request_t *tx; 1623931eeffaSKenneth D. Merry uintptr_t id; 1624931eeffaSKenneth D. Merry grant_ref_t ref; 1625931eeffaSKenneth D. Merry u_long mfn; /* XXX Wrong type? */ 1626931eeffaSKenneth D. Merry 162796375eacSRoger Pau Monné tx = RING_GET_REQUEST(&txq->ring, txq->ring.req_prod_pvt); 162896375eacSRoger Pau Monné id = get_id_from_freelist(txq->mbufs); 1629a4ec37f5SAdrian Chadd if (id == 0) 16306f9767acSMarius Strobl panic("%s: was allocated the freelist head!\n", 16316f9767acSMarius Strobl __func__); 163296375eacSRoger Pau Monné txq->mbufs_cnt++; 163396375eacSRoger Pau Monné if (txq->mbufs_cnt > NET_TX_RING_SIZE) 16346f9767acSMarius Strobl panic("%s: tx_chain_cnt must be <= NET_TX_RING_SIZE\n", 16356f9767acSMarius Strobl __func__); 163696375eacSRoger Pau Monné txq->mbufs[id] = m; 163789e0f4d2SKip Macy tx->id = id; 163896375eacSRoger Pau Monné ref = gnttab_claim_grant_reference(&txq->gref_head); 163989e0f4d2SKip Macy KASSERT((short)ref >= 0, ("Negative ref")); 164012678024SDoug Rabson mfn = virt_to_mfn(mtod(m, vm_offset_t)); 164123dc5621SKip Macy gnttab_grant_foreign_access_ref(ref, otherend_id, 164289e0f4d2SKip Macy mfn, GNTMAP_readonly); 164396375eacSRoger Pau Monné tx->gref = txq->grant_ref[id] = ref; 164412678024SDoug Rabson tx->offset = mtod(m, vm_offset_t) & (PAGE_SIZE - 1); 164589e0f4d2SKip Macy tx->flags = 0; 164612678024SDoug Rabson if (m == m_head) { 164712678024SDoug Rabson /* 164812678024SDoug Rabson * The first fragment has the entire packet 164912678024SDoug Rabson * size, subsequent fragments have just the 165012678024SDoug Rabson * fragment size. The backend works out the 165112678024SDoug Rabson * true size of the first fragment by 165212678024SDoug Rabson * subtracting the sizes of the other 165312678024SDoug Rabson * fragments. 165412678024SDoug Rabson */ 165512678024SDoug Rabson tx->size = m->m_pkthdr.len; 165689e0f4d2SKip Macy 165712678024SDoug Rabson /* 1658931eeffaSKenneth D. Merry * The first fragment contains the checksum flags 1659931eeffaSKenneth D. Merry * and is optionally followed by extra data for 1660931eeffaSKenneth D. Merry * TSO etc. 1661931eeffaSKenneth D. Merry */ 1662931eeffaSKenneth D. Merry /** 1663931eeffaSKenneth D. Merry * CSUM_TSO requires checksum offloading. 1664931eeffaSKenneth D. Merry * Some versions of FreeBSD fail to 1665931eeffaSKenneth D. Merry * set CSUM_TCP in the CSUM_TSO case, 1666931eeffaSKenneth D. Merry * so we have to test for CSUM_TSO 1667931eeffaSKenneth D. Merry * explicitly. 166812678024SDoug Rabson */ 166912678024SDoug Rabson if (m->m_pkthdr.csum_flags 1670931eeffaSKenneth D. Merry & (CSUM_DELAY_DATA | CSUM_TSO)) { 167112678024SDoug Rabson tx->flags |= (NETTXF_csum_blank 167212678024SDoug Rabson | NETTXF_data_validated); 167312678024SDoug Rabson } 167412678024SDoug Rabson if (m->m_pkthdr.csum_flags & CSUM_TSO) { 167512678024SDoug Rabson struct netif_extra_info *gso = 167612678024SDoug Rabson (struct netif_extra_info *) 167796375eacSRoger Pau Monné RING_GET_REQUEST(&txq->ring, 167896375eacSRoger Pau Monné ++txq->ring.req_prod_pvt); 167989e0f4d2SKip Macy 168012678024SDoug Rabson tx->flags |= NETTXF_extra_info; 168189e0f4d2SKip Macy 168212678024SDoug Rabson gso->u.gso.size = m->m_pkthdr.tso_segsz; 168312678024SDoug Rabson gso->u.gso.type = 168412678024SDoug Rabson XEN_NETIF_GSO_TYPE_TCPV4; 168512678024SDoug Rabson gso->u.gso.pad = 0; 168612678024SDoug Rabson gso->u.gso.features = 0; 168712678024SDoug Rabson 168812678024SDoug Rabson gso->type = XEN_NETIF_EXTRA_TYPE_GSO; 168912678024SDoug Rabson gso->flags = 0; 169012678024SDoug Rabson } 169112678024SDoug Rabson } else { 169212678024SDoug Rabson tx->size = m->m_len; 169312678024SDoug Rabson } 1694931eeffaSKenneth D. Merry if (m->m_next) 169512678024SDoug Rabson tx->flags |= NETTXF_more_data; 169612678024SDoug Rabson 169796375eacSRoger Pau Monné txq->ring.req_prod_pvt++; 1698931eeffaSKenneth D. Merry } 169912678024SDoug Rabson BPF_MTAP(ifp, m_head); 170012678024SDoug Rabson 170196375eacSRoger Pau Monné xn_txeof(txq); 170296375eacSRoger Pau Monné 170396375eacSRoger Pau Monné txq->stats.tx_bytes += m_head->m_pkthdr.len; 170496375eacSRoger Pau Monné txq->stats.tx_packets++; 1705931eeffaSKenneth D. Merry 1706931eeffaSKenneth D. Merry return (0); 170789e0f4d2SKip Macy } 170889e0f4d2SKip Macy 170989e0f4d2SKip Macy /* equivalent of network_open() in Linux */ 171089e0f4d2SKip Macy static void 171196375eacSRoger Pau Monné xn_ifinit_locked(struct netfront_info *np) 171289e0f4d2SKip Macy { 171389e0f4d2SKip Macy struct ifnet *ifp; 171496375eacSRoger Pau Monné int i; 171596375eacSRoger Pau Monné struct netfront_rxq *rxq; 171689e0f4d2SKip Macy 171796375eacSRoger Pau Monné XN_LOCK_ASSERT(np); 171889e0f4d2SKip Macy 171996375eacSRoger Pau Monné ifp = np->xn_ifp; 172089e0f4d2SKip Macy 1721c21b47d8SRoger Pau Monné if (ifp->if_drv_flags & IFF_DRV_RUNNING || !netfront_carrier_ok(np)) 172289e0f4d2SKip Macy return; 172389e0f4d2SKip Macy 172496375eacSRoger Pau Monné xn_stop(np); 172589e0f4d2SKip Macy 172696375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 172796375eacSRoger Pau Monné rxq = &np->rxq[i]; 17282568ee67SRoger Pau Monné XN_RX_LOCK(rxq); 172996375eacSRoger Pau Monné xn_alloc_rx_buffers(rxq); 173096375eacSRoger Pau Monné rxq->ring.sring->rsp_event = rxq->ring.rsp_cons + 1; 17312568ee67SRoger Pau Monné if (RING_HAS_UNCONSUMED_RESPONSES(&rxq->ring)) 1732da695b05SRoger Pau Monné xn_rxeof(rxq); 17332568ee67SRoger Pau Monné XN_RX_UNLOCK(rxq); 173496375eacSRoger Pau Monné } 173589e0f4d2SKip Macy 173689e0f4d2SKip Macy ifp->if_drv_flags |= IFF_DRV_RUNNING; 173789e0f4d2SKip Macy ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 17380e509842SJustin T. Gibbs if_link_state_change(ifp, LINK_STATE_UP); 173989e0f4d2SKip Macy } 174089e0f4d2SKip Macy 174189e0f4d2SKip Macy static void 174289e0f4d2SKip Macy xn_ifinit(void *xsc) 174389e0f4d2SKip Macy { 174489e0f4d2SKip Macy struct netfront_info *sc = xsc; 174589e0f4d2SKip Macy 174689e0f4d2SKip Macy XN_LOCK(sc); 174789e0f4d2SKip Macy xn_ifinit_locked(sc); 174889e0f4d2SKip Macy XN_UNLOCK(sc); 174989e0f4d2SKip Macy } 175089e0f4d2SKip Macy 175189e0f4d2SKip Macy static int 175289e0f4d2SKip Macy xn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 175389e0f4d2SKip Macy { 175489e0f4d2SKip Macy struct netfront_info *sc = ifp->if_softc; 175589e0f4d2SKip Macy struct ifreq *ifr = (struct ifreq *) data; 1756*c2d12e5eSRoger Pau Monné device_t dev; 1757a0ae8f04SBjoern A. Zeeb #ifdef INET 175889e0f4d2SKip Macy struct ifaddr *ifa = (struct ifaddr *)data; 1759a0ae8f04SBjoern A. Zeeb #endif 176089e0f4d2SKip Macy int mask, error = 0; 1761*c2d12e5eSRoger Pau Monné 1762*c2d12e5eSRoger Pau Monné dev = sc->xbdev; 1763*c2d12e5eSRoger Pau Monné 176489e0f4d2SKip Macy switch(cmd) { 176589e0f4d2SKip Macy case SIOCSIFADDR: 1766a0ae8f04SBjoern A. Zeeb #ifdef INET 176789e0f4d2SKip Macy XN_LOCK(sc); 176889e0f4d2SKip Macy if (ifa->ifa_addr->sa_family == AF_INET) { 176989e0f4d2SKip Macy ifp->if_flags |= IFF_UP; 177089e0f4d2SKip Macy if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 177189e0f4d2SKip Macy xn_ifinit_locked(sc); 177289e0f4d2SKip Macy arp_ifinit(ifp, ifa); 177389e0f4d2SKip Macy XN_UNLOCK(sc); 177449906218SDoug Rabson } else { 177549906218SDoug Rabson XN_UNLOCK(sc); 1776a0ae8f04SBjoern A. Zeeb #endif 177749906218SDoug Rabson error = ether_ioctl(ifp, cmd, data); 1778a0ae8f04SBjoern A. Zeeb #ifdef INET 177949906218SDoug Rabson } 1780a0ae8f04SBjoern A. Zeeb #endif 178189e0f4d2SKip Macy break; 178289e0f4d2SKip Macy case SIOCSIFMTU: 178389e0f4d2SKip Macy ifp->if_mtu = ifr->ifr_mtu; 178489e0f4d2SKip Macy ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 178589e0f4d2SKip Macy xn_ifinit(sc); 178689e0f4d2SKip Macy break; 178789e0f4d2SKip Macy case SIOCSIFFLAGS: 178889e0f4d2SKip Macy XN_LOCK(sc); 178989e0f4d2SKip Macy if (ifp->if_flags & IFF_UP) { 179089e0f4d2SKip Macy /* 179189e0f4d2SKip Macy * If only the state of the PROMISC flag changed, 179289e0f4d2SKip Macy * then just use the 'set promisc mode' command 179389e0f4d2SKip Macy * instead of reinitializing the entire NIC. Doing 179489e0f4d2SKip Macy * a full re-init means reloading the firmware and 179589e0f4d2SKip Macy * waiting for it to start up, which may take a 179689e0f4d2SKip Macy * second or two. 179789e0f4d2SKip Macy */ 179889e0f4d2SKip Macy xn_ifinit_locked(sc); 179989e0f4d2SKip Macy } else { 180089e0f4d2SKip Macy if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 180189e0f4d2SKip Macy xn_stop(sc); 180289e0f4d2SKip Macy } 180389e0f4d2SKip Macy } 180489e0f4d2SKip Macy sc->xn_if_flags = ifp->if_flags; 180589e0f4d2SKip Macy XN_UNLOCK(sc); 180689e0f4d2SKip Macy break; 180789e0f4d2SKip Macy case SIOCSIFCAP: 180889e0f4d2SKip Macy mask = ifr->ifr_reqcap ^ ifp->if_capenable; 180912678024SDoug Rabson if (mask & IFCAP_TXCSUM) { 181012678024SDoug Rabson if (IFCAP_TXCSUM & ifp->if_capenable) { 181112678024SDoug Rabson ifp->if_capenable &= ~(IFCAP_TXCSUM|IFCAP_TSO4); 181212678024SDoug Rabson ifp->if_hwassist &= ~(CSUM_TCP | CSUM_UDP 181312678024SDoug Rabson | CSUM_IP | CSUM_TSO); 181412678024SDoug Rabson } else { 181512678024SDoug Rabson ifp->if_capenable |= IFCAP_TXCSUM; 181612678024SDoug Rabson ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP 181712678024SDoug Rabson | CSUM_IP); 181889e0f4d2SKip Macy } 181912678024SDoug Rabson } 182012678024SDoug Rabson if (mask & IFCAP_RXCSUM) { 182112678024SDoug Rabson ifp->if_capenable ^= IFCAP_RXCSUM; 182212678024SDoug Rabson } 182312678024SDoug Rabson if (mask & IFCAP_TSO4) { 182412678024SDoug Rabson if (IFCAP_TSO4 & ifp->if_capenable) { 182512678024SDoug Rabson ifp->if_capenable &= ~IFCAP_TSO4; 182612678024SDoug Rabson ifp->if_hwassist &= ~CSUM_TSO; 182712678024SDoug Rabson } else if (IFCAP_TXCSUM & ifp->if_capenable) { 182812678024SDoug Rabson ifp->if_capenable |= IFCAP_TSO4; 182912678024SDoug Rabson ifp->if_hwassist |= CSUM_TSO; 183012678024SDoug Rabson } else { 18313552092bSAdrian Chadd IPRINTK("Xen requires tx checksum offload" 183212678024SDoug Rabson " be enabled to use TSO\n"); 183312678024SDoug Rabson error = EINVAL; 183412678024SDoug Rabson } 183512678024SDoug Rabson } 183612678024SDoug Rabson if (mask & IFCAP_LRO) { 183712678024SDoug Rabson ifp->if_capenable ^= IFCAP_LRO; 183812678024SDoug Rabson 183912678024SDoug Rabson } 1840*c2d12e5eSRoger Pau Monné /* 1841*c2d12e5eSRoger Pau Monné * We must reset the interface so the backend picks up the 1842*c2d12e5eSRoger Pau Monné * new features. 1843*c2d12e5eSRoger Pau Monné */ 1844*c2d12e5eSRoger Pau Monné XN_LOCK(sc); 1845*c2d12e5eSRoger Pau Monné netfront_carrier_off(sc); 1846*c2d12e5eSRoger Pau Monné sc->xn_reset = true; 1847*c2d12e5eSRoger Pau Monné /* 1848*c2d12e5eSRoger Pau Monné * NB: the pending packet queue is not flushed, since 1849*c2d12e5eSRoger Pau Monné * the interface should still support the old options. 1850*c2d12e5eSRoger Pau Monné */ 1851*c2d12e5eSRoger Pau Monné XN_UNLOCK(sc); 1852*c2d12e5eSRoger Pau Monné /* 1853*c2d12e5eSRoger Pau Monné * Delete the xenstore nodes that export features. 1854*c2d12e5eSRoger Pau Monné * 1855*c2d12e5eSRoger Pau Monné * NB: There's a xenbus state called 1856*c2d12e5eSRoger Pau Monné * "XenbusStateReconfiguring", which is what we should set 1857*c2d12e5eSRoger Pau Monné * here. Sadly none of the backends know how to handle it, 1858*c2d12e5eSRoger Pau Monné * and simply disconnect from the frontend, so we will just 1859*c2d12e5eSRoger Pau Monné * switch back to XenbusStateInitialising in order to force 1860*c2d12e5eSRoger Pau Monné * a reconnection. 1861*c2d12e5eSRoger Pau Monné */ 1862*c2d12e5eSRoger Pau Monné xs_rm(XST_NIL, xenbus_get_node(dev), "feature-gso-tcpv4"); 1863*c2d12e5eSRoger Pau Monné xs_rm(XST_NIL, xenbus_get_node(dev), "feature-no-csum-offload"); 1864*c2d12e5eSRoger Pau Monné xenbus_set_state(dev, XenbusStateClosing); 186589e0f4d2SKip Macy break; 186689e0f4d2SKip Macy case SIOCADDMULTI: 186789e0f4d2SKip Macy case SIOCDELMULTI: 1868ce8df48bSSimon J. Gerraty break; 186989e0f4d2SKip Macy case SIOCSIFMEDIA: 187089e0f4d2SKip Macy case SIOCGIFMEDIA: 18710e509842SJustin T. Gibbs error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 187289e0f4d2SKip Macy break; 187389e0f4d2SKip Macy default: 187489e0f4d2SKip Macy error = ether_ioctl(ifp, cmd, data); 187589e0f4d2SKip Macy } 187689e0f4d2SKip Macy 187789e0f4d2SKip Macy return (error); 187889e0f4d2SKip Macy } 187989e0f4d2SKip Macy 188089e0f4d2SKip Macy static void 188189e0f4d2SKip Macy xn_stop(struct netfront_info *sc) 188289e0f4d2SKip Macy { 188389e0f4d2SKip Macy struct ifnet *ifp; 188489e0f4d2SKip Macy 188589e0f4d2SKip Macy XN_LOCK_ASSERT(sc); 188689e0f4d2SKip Macy 188789e0f4d2SKip Macy ifp = sc->xn_ifp; 188889e0f4d2SKip Macy 188989e0f4d2SKip Macy ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 18900e509842SJustin T. Gibbs if_link_state_change(ifp, LINK_STATE_DOWN); 189189e0f4d2SKip Macy } 189289e0f4d2SKip Macy 189396375eacSRoger Pau Monné static void 189496375eacSRoger Pau Monné xn_rebuild_rx_bufs(struct netfront_rxq *rxq) 189589e0f4d2SKip Macy { 189696375eacSRoger Pau Monné int requeue_idx, i; 189789e0f4d2SKip Macy grant_ref_t ref; 189889e0f4d2SKip Macy netif_rx_request_t *req; 189989e0f4d2SKip Macy 190089e0f4d2SKip Macy for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) { 190189e0f4d2SKip Macy struct mbuf *m; 19023a6d1fcfSKip Macy u_long pfn; 190389e0f4d2SKip Macy 190496375eacSRoger Pau Monné if (rxq->mbufs[i] == NULL) 190589e0f4d2SKip Macy continue; 190689e0f4d2SKip Macy 190796375eacSRoger Pau Monné m = rxq->mbufs[requeue_idx] = xn_get_rx_mbuf(rxq, i); 190896375eacSRoger Pau Monné ref = rxq->grant_ref[requeue_idx] = xn_get_rx_ref(rxq, i); 1909931eeffaSKenneth D. Merry 191096375eacSRoger Pau Monné req = RING_GET_REQUEST(&rxq->ring, requeue_idx); 19113a6d1fcfSKip Macy pfn = vtophys(mtod(m, vm_offset_t)) >> PAGE_SHIFT; 191289e0f4d2SKip Macy 191389e0f4d2SKip Macy gnttab_grant_foreign_access_ref(ref, 191496375eacSRoger Pau Monné xenbus_get_otherend_id(rxq->info->xbdev), 1915ed95805eSJohn Baldwin pfn, 0); 1916d0f3a8b9SRoger Pau Monné 191789e0f4d2SKip Macy req->gref = ref; 191889e0f4d2SKip Macy req->id = requeue_idx; 191989e0f4d2SKip Macy 192089e0f4d2SKip Macy requeue_idx++; 192189e0f4d2SKip Macy } 192289e0f4d2SKip Macy 192396375eacSRoger Pau Monné rxq->ring.req_prod_pvt = requeue_idx; 192496375eacSRoger Pau Monné } 192589e0f4d2SKip Macy 192696375eacSRoger Pau Monné /* START of Xenolinux helper functions adapted to FreeBSD */ 192796375eacSRoger Pau Monné int 192896375eacSRoger Pau Monné xn_connect(struct netfront_info *np) 192996375eacSRoger Pau Monné { 193096375eacSRoger Pau Monné int i, error; 193196375eacSRoger Pau Monné u_int feature_rx_copy; 193296375eacSRoger Pau Monné struct netfront_rxq *rxq; 193396375eacSRoger Pau Monné struct netfront_txq *txq; 193496375eacSRoger Pau Monné 193596375eacSRoger Pau Monné error = xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 193696375eacSRoger Pau Monné "feature-rx-copy", NULL, "%u", &feature_rx_copy); 193796375eacSRoger Pau Monné if (error != 0) 193896375eacSRoger Pau Monné feature_rx_copy = 0; 193996375eacSRoger Pau Monné 194096375eacSRoger Pau Monné /* We only support rx copy. */ 194196375eacSRoger Pau Monné if (!feature_rx_copy) 194296375eacSRoger Pau Monné return (EPROTONOSUPPORT); 194396375eacSRoger Pau Monné 194496375eacSRoger Pau Monné /* Recovery procedure: */ 194596375eacSRoger Pau Monné error = talk_to_backend(np->xbdev, np); 194696375eacSRoger Pau Monné if (error != 0) 194796375eacSRoger Pau Monné return (error); 194896375eacSRoger Pau Monné 194996375eacSRoger Pau Monné /* Step 1: Reinitialise variables. */ 195096375eacSRoger Pau Monné xn_query_features(np); 195196375eacSRoger Pau Monné xn_configure_features(np); 195296375eacSRoger Pau Monné 195396375eacSRoger Pau Monné /* Step 2: Release TX buffer */ 195496375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 195596375eacSRoger Pau Monné txq = &np->txq[i]; 195696375eacSRoger Pau Monné xn_release_tx_bufs(txq); 195796375eacSRoger Pau Monné } 195896375eacSRoger Pau Monné 195996375eacSRoger Pau Monné /* Step 3: Rebuild the RX buffer freelist and the RX ring itself. */ 196096375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 196196375eacSRoger Pau Monné rxq = &np->rxq[i]; 196296375eacSRoger Pau Monné xn_rebuild_rx_bufs(rxq); 196396375eacSRoger Pau Monné } 196496375eacSRoger Pau Monné 196596375eacSRoger Pau Monné /* Step 4: All public and private state should now be sane. Get 196689e0f4d2SKip Macy * ready to start sending and receiving packets and give the driver 196789e0f4d2SKip Macy * domain a kick because we've probably just requeued some 196889e0f4d2SKip Macy * packets. 196989e0f4d2SKip Macy */ 197089e0f4d2SKip Macy netfront_carrier_on(np); 197196375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 197296375eacSRoger Pau Monné txq = &np->txq[i]; 197396375eacSRoger Pau Monné xen_intr_signal(txq->xen_intr_handle); 197496375eacSRoger Pau Monné XN_TX_LOCK(txq); 197596375eacSRoger Pau Monné xn_txeof(txq); 197696375eacSRoger Pau Monné XN_TX_UNLOCK(txq); 19772568ee67SRoger Pau Monné XN_RX_LOCK(rxq); 197896375eacSRoger Pau Monné xn_alloc_rx_buffers(rxq); 19792568ee67SRoger Pau Monné XN_RX_UNLOCK(rxq); 198096375eacSRoger Pau Monné } 198189e0f4d2SKip Macy 198289e0f4d2SKip Macy return (0); 198389e0f4d2SKip Macy } 198489e0f4d2SKip Macy 198589e0f4d2SKip Macy static void 1986578e4bf7SJustin T. Gibbs xn_query_features(struct netfront_info *np) 1987578e4bf7SJustin T. Gibbs { 1988578e4bf7SJustin T. Gibbs int val; 1989578e4bf7SJustin T. Gibbs 1990578e4bf7SJustin T. Gibbs device_printf(np->xbdev, "backend features:"); 1991578e4bf7SJustin T. Gibbs 1992578e4bf7SJustin T. Gibbs if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 1993107cfbb7SRoger Pau Monné "feature-sg", NULL, "%d", &val) != 0) 1994578e4bf7SJustin T. Gibbs val = 0; 1995578e4bf7SJustin T. Gibbs 1996578e4bf7SJustin T. Gibbs np->maxfrags = 1; 1997578e4bf7SJustin T. Gibbs if (val) { 1998578e4bf7SJustin T. Gibbs np->maxfrags = MAX_TX_REQ_FRAGS; 1999578e4bf7SJustin T. Gibbs printf(" feature-sg"); 2000578e4bf7SJustin T. Gibbs } 2001578e4bf7SJustin T. Gibbs 2002578e4bf7SJustin T. Gibbs if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 2003107cfbb7SRoger Pau Monné "feature-gso-tcpv4", NULL, "%d", &val) != 0) 2004578e4bf7SJustin T. Gibbs val = 0; 2005578e4bf7SJustin T. Gibbs 2006578e4bf7SJustin T. Gibbs np->xn_ifp->if_capabilities &= ~(IFCAP_TSO4|IFCAP_LRO); 2007578e4bf7SJustin T. Gibbs if (val) { 2008578e4bf7SJustin T. Gibbs np->xn_ifp->if_capabilities |= IFCAP_TSO4|IFCAP_LRO; 2009578e4bf7SJustin T. Gibbs printf(" feature-gso-tcp4"); 2010578e4bf7SJustin T. Gibbs } 2011578e4bf7SJustin T. Gibbs 2012*c2d12e5eSRoger Pau Monné /* 2013*c2d12e5eSRoger Pau Monné * HW CSUM offload is assumed to be available unless 2014*c2d12e5eSRoger Pau Monné * feature-no-csum-offload is set in xenstore. 2015*c2d12e5eSRoger Pau Monné */ 2016*c2d12e5eSRoger Pau Monné if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev), 2017*c2d12e5eSRoger Pau Monné "feature-no-csum-offload", NULL, "%d", &val) != 0) 2018*c2d12e5eSRoger Pau Monné val = 0; 2019*c2d12e5eSRoger Pau Monné 2020*c2d12e5eSRoger Pau Monné np->xn_ifp->if_capabilities |= IFCAP_HWCSUM; 2021*c2d12e5eSRoger Pau Monné if (val) { 2022*c2d12e5eSRoger Pau Monné np->xn_ifp->if_capabilities &= ~(IFCAP_HWCSUM); 2023*c2d12e5eSRoger Pau Monné printf(" feature-no-csum-offload"); 2024*c2d12e5eSRoger Pau Monné } 2025*c2d12e5eSRoger Pau Monné 2026578e4bf7SJustin T. Gibbs printf("\n"); 2027578e4bf7SJustin T. Gibbs } 2028578e4bf7SJustin T. Gibbs 2029cf9c09e1SJustin T. Gibbs static int 2030578e4bf7SJustin T. Gibbs xn_configure_features(struct netfront_info *np) 2031cf9c09e1SJustin T. Gibbs { 20326a8e9695SRoger Pau Monné int err, cap_enabled; 203396375eacSRoger Pau Monné #if (defined(INET) || defined(INET6)) 203496375eacSRoger Pau Monné int i; 203596375eacSRoger Pau Monné #endif 2036*c2d12e5eSRoger Pau Monné struct ifnet *ifp; 2037cf9c09e1SJustin T. Gibbs 2038*c2d12e5eSRoger Pau Monné ifp = np->xn_ifp; 2039cf9c09e1SJustin T. Gibbs err = 0; 20406a8e9695SRoger Pau Monné 2041*c2d12e5eSRoger Pau Monné if ((ifp->if_capenable & ifp->if_capabilities) == ifp->if_capenable) { 20426a8e9695SRoger Pau Monné /* Current options are available, no need to do anything. */ 20436a8e9695SRoger Pau Monné return (0); 20446a8e9695SRoger Pau Monné } 20456a8e9695SRoger Pau Monné 20466a8e9695SRoger Pau Monné /* Try to preserve as many options as possible. */ 2047*c2d12e5eSRoger Pau Monné cap_enabled = ifp->if_capenable; 2048*c2d12e5eSRoger Pau Monné ifp->if_capenable = ifp->if_hwassist = 0; 20496a8e9695SRoger Pau Monné 205008c9c2e0SRoger Pau Monné #if (defined(INET) || defined(INET6)) 2051*c2d12e5eSRoger Pau Monné if ((cap_enabled & IFCAP_LRO) != 0) 205296375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) 205396375eacSRoger Pau Monné tcp_lro_free(&np->rxq[i].lro); 2054*c2d12e5eSRoger Pau Monné if (xn_enable_lro && 2055*c2d12e5eSRoger Pau Monné (ifp->if_capabilities & cap_enabled & IFCAP_LRO) != 0) { 2056*c2d12e5eSRoger Pau Monné ifp->if_capenable |= IFCAP_LRO; 205796375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 205896375eacSRoger Pau Monné err = tcp_lro_init(&np->rxq[i].lro); 205996375eacSRoger Pau Monné if (err != 0) { 2060*c2d12e5eSRoger Pau Monné device_printf(np->xbdev, 2061*c2d12e5eSRoger Pau Monné "LRO initialization failed\n"); 2062*c2d12e5eSRoger Pau Monné ifp->if_capenable &= ~IFCAP_LRO; 2063*c2d12e5eSRoger Pau Monné break; 2064*c2d12e5eSRoger Pau Monné } 2065*c2d12e5eSRoger Pau Monné np->rxq[i].lro.ifp = ifp; 2066cf9c09e1SJustin T. Gibbs } 2067cf9c09e1SJustin T. Gibbs } 2068*c2d12e5eSRoger Pau Monné if ((ifp->if_capabilities & cap_enabled & IFCAP_TSO4) != 0) { 2069*c2d12e5eSRoger Pau Monné ifp->if_capenable |= IFCAP_TSO4; 2070*c2d12e5eSRoger Pau Monné ifp->if_hwassist |= CSUM_TSO; 2071578e4bf7SJustin T. Gibbs } 2072cf9c09e1SJustin T. Gibbs #endif 2073*c2d12e5eSRoger Pau Monné if ((ifp->if_capabilities & cap_enabled & IFCAP_TXCSUM) != 0) { 2074*c2d12e5eSRoger Pau Monné ifp->if_capenable |= IFCAP_TXCSUM; 2075*c2d12e5eSRoger Pau Monné ifp->if_hwassist |= CSUM_TCP|CSUM_UDP; 2076*c2d12e5eSRoger Pau Monné } 2077*c2d12e5eSRoger Pau Monné if ((ifp->if_capabilities & cap_enabled & IFCAP_RXCSUM) != 0) 2078*c2d12e5eSRoger Pau Monné ifp->if_capenable |= IFCAP_RXCSUM; 2079*c2d12e5eSRoger Pau Monné 2080cf9c09e1SJustin T. Gibbs return (err); 2081cf9c09e1SJustin T. Gibbs } 2082cf9c09e1SJustin T. Gibbs 208396375eacSRoger Pau Monné static int 208496375eacSRoger Pau Monné xn_txq_mq_start_locked(struct netfront_txq *txq, struct mbuf *m) 208596375eacSRoger Pau Monné { 208696375eacSRoger Pau Monné struct netfront_info *np; 208796375eacSRoger Pau Monné struct ifnet *ifp; 208896375eacSRoger Pau Monné struct buf_ring *br; 208996375eacSRoger Pau Monné int error, notify; 209096375eacSRoger Pau Monné 209196375eacSRoger Pau Monné np = txq->info; 209296375eacSRoger Pau Monné br = txq->br; 209396375eacSRoger Pau Monné ifp = np->xn_ifp; 209496375eacSRoger Pau Monné error = 0; 209596375eacSRoger Pau Monné 209696375eacSRoger Pau Monné XN_TX_LOCK_ASSERT(txq); 209796375eacSRoger Pau Monné 209896375eacSRoger Pau Monné if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 || 209996375eacSRoger Pau Monné !netfront_carrier_ok(np)) { 210096375eacSRoger Pau Monné if (m != NULL) 210196375eacSRoger Pau Monné error = drbr_enqueue(ifp, br, m); 210296375eacSRoger Pau Monné return (error); 210396375eacSRoger Pau Monné } 210496375eacSRoger Pau Monné 210596375eacSRoger Pau Monné if (m != NULL) { 210696375eacSRoger Pau Monné error = drbr_enqueue(ifp, br, m); 210796375eacSRoger Pau Monné if (error != 0) 210896375eacSRoger Pau Monné return (error); 210996375eacSRoger Pau Monné } 211096375eacSRoger Pau Monné 211196375eacSRoger Pau Monné while ((m = drbr_peek(ifp, br)) != NULL) { 211296375eacSRoger Pau Monné if (!xn_tx_slot_available(txq)) { 211396375eacSRoger Pau Monné drbr_putback(ifp, br, m); 211496375eacSRoger Pau Monné break; 211596375eacSRoger Pau Monné } 211696375eacSRoger Pau Monné 211796375eacSRoger Pau Monné error = xn_assemble_tx_request(txq, m); 211896375eacSRoger Pau Monné /* xn_assemble_tx_request always consumes the mbuf*/ 211996375eacSRoger Pau Monné if (error != 0) { 212096375eacSRoger Pau Monné drbr_advance(ifp, br); 212196375eacSRoger Pau Monné break; 212296375eacSRoger Pau Monné } 212396375eacSRoger Pau Monné 212496375eacSRoger Pau Monné RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&txq->ring, notify); 212596375eacSRoger Pau Monné if (notify) 212696375eacSRoger Pau Monné xen_intr_signal(txq->xen_intr_handle); 212796375eacSRoger Pau Monné 212896375eacSRoger Pau Monné drbr_advance(ifp, br); 212996375eacSRoger Pau Monné } 213096375eacSRoger Pau Monné 213196375eacSRoger Pau Monné if (RING_FULL(&txq->ring)) 213296375eacSRoger Pau Monné txq->full = true; 213396375eacSRoger Pau Monné 213496375eacSRoger Pau Monné return (0); 213596375eacSRoger Pau Monné } 213696375eacSRoger Pau Monné 213796375eacSRoger Pau Monné static int 213896375eacSRoger Pau Monné xn_txq_mq_start(struct ifnet *ifp, struct mbuf *m) 213996375eacSRoger Pau Monné { 214096375eacSRoger Pau Monné struct netfront_info *np; 214196375eacSRoger Pau Monné struct netfront_txq *txq; 214296375eacSRoger Pau Monné int i, npairs, error; 214396375eacSRoger Pau Monné 214496375eacSRoger Pau Monné np = ifp->if_softc; 214596375eacSRoger Pau Monné npairs = np->num_queues; 214696375eacSRoger Pau Monné 2147c21b47d8SRoger Pau Monné KASSERT(npairs != 0, ("called with 0 available queues")); 2148c21b47d8SRoger Pau Monné 214996375eacSRoger Pau Monné /* check if flowid is set */ 215096375eacSRoger Pau Monné if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE) 215196375eacSRoger Pau Monné i = m->m_pkthdr.flowid % npairs; 215296375eacSRoger Pau Monné else 215396375eacSRoger Pau Monné i = curcpu % npairs; 215496375eacSRoger Pau Monné 215596375eacSRoger Pau Monné txq = &np->txq[i]; 215696375eacSRoger Pau Monné 215796375eacSRoger Pau Monné if (XN_TX_TRYLOCK(txq) != 0) { 215896375eacSRoger Pau Monné error = xn_txq_mq_start_locked(txq, m); 215996375eacSRoger Pau Monné XN_TX_UNLOCK(txq); 216096375eacSRoger Pau Monné } else { 216196375eacSRoger Pau Monné error = drbr_enqueue(ifp, txq->br, m); 216296375eacSRoger Pau Monné taskqueue_enqueue(txq->tq, &txq->defrtask); 216396375eacSRoger Pau Monné } 216496375eacSRoger Pau Monné 216596375eacSRoger Pau Monné return (error); 216696375eacSRoger Pau Monné } 216796375eacSRoger Pau Monné 216896375eacSRoger Pau Monné static void 216996375eacSRoger Pau Monné xn_qflush(struct ifnet *ifp) 217096375eacSRoger Pau Monné { 217196375eacSRoger Pau Monné struct netfront_info *np; 217296375eacSRoger Pau Monné struct netfront_txq *txq; 217396375eacSRoger Pau Monné struct mbuf *m; 217496375eacSRoger Pau Monné int i; 217596375eacSRoger Pau Monné 217696375eacSRoger Pau Monné np = ifp->if_softc; 217796375eacSRoger Pau Monné 217896375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 217996375eacSRoger Pau Monné txq = &np->txq[i]; 218096375eacSRoger Pau Monné 218196375eacSRoger Pau Monné XN_TX_LOCK(txq); 218296375eacSRoger Pau Monné while ((m = buf_ring_dequeue_sc(txq->br)) != NULL) 218396375eacSRoger Pau Monné m_freem(m); 218496375eacSRoger Pau Monné XN_TX_UNLOCK(txq); 218596375eacSRoger Pau Monné } 218696375eacSRoger Pau Monné 218796375eacSRoger Pau Monné if_qflush(ifp); 218896375eacSRoger Pau Monné } 218996375eacSRoger Pau Monné 219076acc41fSJustin T. Gibbs /** 219176acc41fSJustin T. Gibbs * Create a network device. 219276acc41fSJustin T. Gibbs * @param dev Newbus device representing this virtual NIC. 219389e0f4d2SKip Macy */ 219423dc5621SKip Macy int 219523dc5621SKip Macy create_netdev(device_t dev) 219689e0f4d2SKip Macy { 219789e0f4d2SKip Macy struct netfront_info *np; 219889e0f4d2SKip Macy int err; 219989e0f4d2SKip Macy struct ifnet *ifp; 220089e0f4d2SKip Macy 220123dc5621SKip Macy np = device_get_softc(dev); 220289e0f4d2SKip Macy 220389e0f4d2SKip Macy np->xbdev = dev; 220489e0f4d2SKip Macy 2205177e3f13SRoger Pau Monné mtx_init(&np->sc_lock, "xnsc", "netfront softc lock", MTX_DEF); 22060e509842SJustin T. Gibbs 22070e509842SJustin T. Gibbs ifmedia_init(&np->sc_media, 0, xn_ifmedia_upd, xn_ifmedia_sts); 22080e509842SJustin T. Gibbs ifmedia_add(&np->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL); 22090e509842SJustin T. Gibbs ifmedia_set(&np->sc_media, IFM_ETHER|IFM_MANUAL); 22100e509842SJustin T. Gibbs 221189e0f4d2SKip Macy err = xen_net_read_mac(dev, np->mac); 221296375eacSRoger Pau Monné if (err != 0) 22131a2928b7SRoger Pau Monné goto error; 221489e0f4d2SKip Macy 221589e0f4d2SKip Macy /* Set up ifnet structure */ 221623dc5621SKip Macy ifp = np->xn_ifp = if_alloc(IFT_ETHER); 221789e0f4d2SKip Macy ifp->if_softc = np; 221823dc5621SKip Macy if_initname(ifp, "xn", device_get_unit(dev)); 22193a6d1fcfSKip Macy ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 222089e0f4d2SKip Macy ifp->if_ioctl = xn_ioctl; 222196375eacSRoger Pau Monné 222296375eacSRoger Pau Monné ifp->if_transmit = xn_txq_mq_start; 222396375eacSRoger Pau Monné ifp->if_qflush = xn_qflush; 222496375eacSRoger Pau Monné 222589e0f4d2SKip Macy ifp->if_init = xn_ifinit; 222689e0f4d2SKip Macy 222789e0f4d2SKip Macy ifp->if_hwassist = XN_CSUM_FEATURES; 2228*c2d12e5eSRoger Pau Monné /* Enable all supported features at device creation. */ 2229*c2d12e5eSRoger Pau Monné ifp->if_capenable = ifp->if_capabilities = 2230*c2d12e5eSRoger Pau Monné IFCAP_HWCSUM|IFCAP_TSO4|IFCAP_LRO; 22319fd573c3SHans Petter Selasky ifp->if_hw_tsomax = 65536 - (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN); 22329fd573c3SHans Petter Selasky ifp->if_hw_tsomaxsegcount = MAX_TX_REQ_FRAGS; 22339fd573c3SHans Petter Selasky ifp->if_hw_tsomaxsegsize = PAGE_SIZE; 223489e0f4d2SKip Macy 223589e0f4d2SKip Macy ether_ifattach(ifp, np->mac); 223689e0f4d2SKip Macy netfront_carrier_off(np); 223789e0f4d2SKip Macy 223889e0f4d2SKip Macy return (0); 223989e0f4d2SKip Macy 22401a2928b7SRoger Pau Monné error: 22411a2928b7SRoger Pau Monné KASSERT(err != 0, ("Error path with no error code specified")); 2242ffa06904SJustin T. Gibbs return (err); 224389e0f4d2SKip Macy } 224489e0f4d2SKip Macy 22450e509842SJustin T. Gibbs static int 22460e509842SJustin T. Gibbs netfront_detach(device_t dev) 224789e0f4d2SKip Macy { 224823dc5621SKip Macy struct netfront_info *info = device_get_softc(dev); 224989e0f4d2SKip Macy 225023dc5621SKip Macy DPRINTK("%s\n", xenbus_get_node(dev)); 225189e0f4d2SKip Macy 225289e0f4d2SKip Macy netif_free(info); 225389e0f4d2SKip Macy 225489e0f4d2SKip Macy return 0; 225589e0f4d2SKip Macy } 225689e0f4d2SKip Macy 22570e509842SJustin T. Gibbs static void 225896375eacSRoger Pau Monné netif_free(struct netfront_info *np) 225989e0f4d2SKip Macy { 226096375eacSRoger Pau Monné 226196375eacSRoger Pau Monné XN_LOCK(np); 226296375eacSRoger Pau Monné xn_stop(np); 226396375eacSRoger Pau Monné XN_UNLOCK(np); 226496375eacSRoger Pau Monné netif_disconnect_backend(np); 2265c21b47d8SRoger Pau Monné ether_ifdetach(np->xn_ifp); 226696375eacSRoger Pau Monné free(np->rxq, M_DEVBUF); 226796375eacSRoger Pau Monné free(np->txq, M_DEVBUF); 226896375eacSRoger Pau Monné if_free(np->xn_ifp); 226996375eacSRoger Pau Monné np->xn_ifp = NULL; 227096375eacSRoger Pau Monné ifmedia_removeall(&np->sc_media); 227189e0f4d2SKip Macy } 227289e0f4d2SKip Macy 22730e509842SJustin T. Gibbs static void 227496375eacSRoger Pau Monné netif_disconnect_backend(struct netfront_info *np) 227589e0f4d2SKip Macy { 227696375eacSRoger Pau Monné u_int i; 22773a6d1fcfSKip Macy 227896375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 227996375eacSRoger Pau Monné XN_RX_LOCK(&np->rxq[i]); 228096375eacSRoger Pau Monné XN_TX_LOCK(&np->txq[i]); 228196375eacSRoger Pau Monné } 228296375eacSRoger Pau Monné netfront_carrier_off(np); 228396375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 228496375eacSRoger Pau Monné XN_RX_UNLOCK(&np->rxq[i]); 228596375eacSRoger Pau Monné XN_TX_UNLOCK(&np->txq[i]); 228689e0f4d2SKip Macy } 228789e0f4d2SKip Macy 228896375eacSRoger Pau Monné for (i = 0; i < np->num_queues; i++) { 228996375eacSRoger Pau Monné disconnect_rxq(&np->rxq[i]); 229096375eacSRoger Pau Monné disconnect_txq(&np->txq[i]); 2291cf9c09e1SJustin T. Gibbs } 229289e0f4d2SKip Macy } 229389e0f4d2SKip Macy 22940e509842SJustin T. Gibbs static int 22950e509842SJustin T. Gibbs xn_ifmedia_upd(struct ifnet *ifp) 22960e509842SJustin T. Gibbs { 229796375eacSRoger Pau Monné 22980e509842SJustin T. Gibbs return (0); 22990e509842SJustin T. Gibbs } 23000e509842SJustin T. Gibbs 23010e509842SJustin T. Gibbs static void 23020e509842SJustin T. Gibbs xn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 23030e509842SJustin T. Gibbs { 230496375eacSRoger Pau Monné 23050e509842SJustin T. Gibbs ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE; 23060e509842SJustin T. Gibbs ifmr->ifm_active = IFM_ETHER|IFM_MANUAL; 23070e509842SJustin T. Gibbs } 23080e509842SJustin T. Gibbs 230989e0f4d2SKip Macy /* ** Driver registration ** */ 231023dc5621SKip Macy static device_method_t netfront_methods[] = { 231123dc5621SKip Macy /* Device interface */ 231223dc5621SKip Macy DEVMETHOD(device_probe, netfront_probe), 231323dc5621SKip Macy DEVMETHOD(device_attach, netfront_attach), 231423dc5621SKip Macy DEVMETHOD(device_detach, netfront_detach), 231523dc5621SKip Macy DEVMETHOD(device_shutdown, bus_generic_shutdown), 2316cf9c09e1SJustin T. Gibbs DEVMETHOD(device_suspend, netfront_suspend), 231723dc5621SKip Macy DEVMETHOD(device_resume, netfront_resume), 231889e0f4d2SKip Macy 231923dc5621SKip Macy /* Xenbus interface */ 2320ff662b5cSJustin T. Gibbs DEVMETHOD(xenbus_otherend_changed, netfront_backend_changed), 232189e0f4d2SKip Macy 23226f9767acSMarius Strobl DEVMETHOD_END 232389e0f4d2SKip Macy }; 232489e0f4d2SKip Macy 232523dc5621SKip Macy static driver_t netfront_driver = { 232623dc5621SKip Macy "xn", 232723dc5621SKip Macy netfront_methods, 232823dc5621SKip Macy sizeof(struct netfront_info), 232989e0f4d2SKip Macy }; 233023dc5621SKip Macy devclass_t netfront_devclass; 233189e0f4d2SKip Macy 23326f9767acSMarius Strobl DRIVER_MODULE(xe, xenbusb_front, netfront_driver, netfront_devclass, NULL, 23336f9767acSMarius Strobl NULL); 2334