xref: /freebsd/sys/dev/xen/netfront/netfront.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
18e0ad55aSJoel Dahl /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
489e0f4d2SKip Macy  * Copyright (c) 2004-2006 Kip Macy
596375eacSRoger Pau Monné  * Copyright (c) 2015 Wei Liu <wei.liu2@citrix.com>
689e0f4d2SKip Macy  * All rights reserved.
789e0f4d2SKip Macy  *
88e0ad55aSJoel Dahl  * Redistribution and use in source and binary forms, with or without
98e0ad55aSJoel Dahl  * modification, are permitted provided that the following conditions
108e0ad55aSJoel Dahl  * are met:
118e0ad55aSJoel Dahl  * 1. Redistributions of source code must retain the above copyright
128e0ad55aSJoel Dahl  *    notice, this list of conditions and the following disclaimer.
138e0ad55aSJoel Dahl  * 2. Redistributions in binary form must reproduce the above copyright
148e0ad55aSJoel Dahl  *    notice, this list of conditions and the following disclaimer in the
158e0ad55aSJoel Dahl  *    documentation and/or other materials provided with the distribution.
1689e0f4d2SKip Macy  *
178e0ad55aSJoel Dahl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
188e0ad55aSJoel Dahl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198e0ad55aSJoel Dahl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
208e0ad55aSJoel Dahl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
218e0ad55aSJoel Dahl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
228e0ad55aSJoel Dahl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
238e0ad55aSJoel Dahl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
248e0ad55aSJoel Dahl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
258e0ad55aSJoel Dahl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
268e0ad55aSJoel Dahl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
278e0ad55aSJoel Dahl  * SUCH DAMAGE.
2889e0f4d2SKip Macy  */
2989e0f4d2SKip Macy 
3089e0f4d2SKip Macy #include <sys/cdefs.h>
3189e0f4d2SKip Macy __FBSDID("$FreeBSD$");
3289e0f4d2SKip Macy 
33a0ae8f04SBjoern A. Zeeb #include "opt_inet.h"
34f909bbb4SGleb Smirnoff #include "opt_inet6.h"
35a0ae8f04SBjoern A. Zeeb 
3689e0f4d2SKip Macy #include <sys/param.h>
3789e0f4d2SKip Macy #include <sys/sockio.h>
38c578b6acSGleb Smirnoff #include <sys/limits.h>
3989e0f4d2SKip Macy #include <sys/mbuf.h>
4089e0f4d2SKip Macy #include <sys/malloc.h>
4123dc5621SKip Macy #include <sys/module.h>
4289e0f4d2SKip Macy #include <sys/kernel.h>
4389e0f4d2SKip Macy #include <sys/socket.h>
4412678024SDoug Rabson #include <sys/sysctl.h>
4596375eacSRoger Pau Monné #include <sys/taskqueue.h>
4689e0f4d2SKip Macy 
4789e0f4d2SKip Macy #include <net/if.h>
4877374386SGleb Smirnoff #include <net/if_var.h>
4989e0f4d2SKip Macy #include <net/if_arp.h>
5089e0f4d2SKip Macy #include <net/ethernet.h>
5189e0f4d2SKip Macy #include <net/if_media.h>
5289e0f4d2SKip Macy #include <net/bpf.h>
5389e0f4d2SKip Macy #include <net/if_types.h>
5489e0f4d2SKip Macy 
5589e0f4d2SKip Macy #include <netinet/in.h>
5689e0f4d2SKip Macy #include <netinet/ip.h>
5789e0f4d2SKip Macy #include <netinet/if_ether.h>
5812678024SDoug Rabson #include <netinet/tcp.h>
5912678024SDoug Rabson #include <netinet/tcp_lro.h>
6089e0f4d2SKip Macy 
6189e0f4d2SKip Macy #include <vm/vm.h>
6289e0f4d2SKip Macy #include <vm/pmap.h>
6389e0f4d2SKip Macy 
6489e0f4d2SKip Macy #include <sys/bus.h>
6589e0f4d2SKip Macy 
6676acc41fSJustin T. Gibbs #include <xen/xen-os.h>
673a6d1fcfSKip Macy #include <xen/hypervisor.h>
683a6d1fcfSKip Macy #include <xen/xen_intr.h>
6989e0f4d2SKip Macy #include <xen/gnttab.h>
70ad7dd514SElliott Mitchell #include <contrib/xen/memory.h>
71ad7dd514SElliott Mitchell #include <contrib/xen/io/netif.h>
7223dc5621SKip Macy #include <xen/xenbus/xenbusvar.h>
7389e0f4d2SKip Macy 
74dabb3db7SRoger Pau Monné #include <machine/bus.h>
75dabb3db7SRoger Pau Monné 
7623dc5621SKip Macy #include "xenbus_if.h"
7789e0f4d2SKip Macy 
78578e4bf7SJustin T. Gibbs /* Features supported by all backends.  TSO and LRO can be negotiated */
79578e4bf7SJustin T. Gibbs #define XN_CSUM_FEATURES	(CSUM_TCP | CSUM_UDP)
8012678024SDoug Rabson 
8198018db4SRyan Libby #define NET_TX_RING_SIZE __CONST_RING_SIZE(netif_tx, PAGE_SIZE)
8298018db4SRyan Libby #define NET_RX_RING_SIZE __CONST_RING_SIZE(netif_rx, PAGE_SIZE)
8389e0f4d2SKip Macy 
842568ee67SRoger Pau Monné #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
852568ee67SRoger Pau Monné 
8612678024SDoug Rabson /*
8712678024SDoug Rabson  * Should the driver do LRO on the RX end
8812678024SDoug Rabson  *  this can be toggled on the fly, but the
8912678024SDoug Rabson  *  interface must be reset (down/up) for it
9012678024SDoug Rabson  *  to take effect.
9112678024SDoug Rabson  */
9212678024SDoug Rabson static int xn_enable_lro = 1;
9312678024SDoug Rabson TUNABLE_INT("hw.xn.enable_lro", &xn_enable_lro);
9412678024SDoug Rabson 
9596375eacSRoger Pau Monné /*
9696375eacSRoger Pau Monné  * Number of pairs of queues.
9796375eacSRoger Pau Monné  */
9896375eacSRoger Pau Monné static unsigned long xn_num_queues = 4;
9996375eacSRoger Pau Monné TUNABLE_ULONG("hw.xn.num_queues", &xn_num_queues);
10096375eacSRoger Pau Monné 
101931eeffaSKenneth D. Merry /**
102931eeffaSKenneth D. Merry  * \brief The maximum allowed data fragments in a single transmit
103931eeffaSKenneth D. Merry  *        request.
104931eeffaSKenneth D. Merry  *
105931eeffaSKenneth D. Merry  * This limit is imposed by the backend driver.  We assume here that
106931eeffaSKenneth D. Merry  * we are dealing with a Linux driver domain and have set our limit
107931eeffaSKenneth D. Merry  * to mirror the Linux MAX_SKB_FRAGS constant.
108931eeffaSKenneth D. Merry  */
109931eeffaSKenneth D. Merry #define	MAX_TX_REQ_FRAGS (65536 / PAGE_SIZE + 2)
110931eeffaSKenneth D. Merry 
11189e0f4d2SKip Macy #define RX_COPY_THRESHOLD 256
11289e0f4d2SKip Macy 
11389e0f4d2SKip Macy #define net_ratelimit() 0
11489e0f4d2SKip Macy 
11596375eacSRoger Pau Monné struct netfront_rxq;
11696375eacSRoger Pau Monné struct netfront_txq;
11789e0f4d2SKip Macy struct netfront_info;
11889e0f4d2SKip Macy struct netfront_rx_info;
11989e0f4d2SKip Macy 
12096375eacSRoger Pau Monné static void xn_txeof(struct netfront_txq *);
12196375eacSRoger Pau Monné static void xn_rxeof(struct netfront_rxq *);
12296375eacSRoger Pau Monné static void xn_alloc_rx_buffers(struct netfront_rxq *);
1232568ee67SRoger Pau Monné static void xn_alloc_rx_buffers_callout(void *arg);
12489e0f4d2SKip Macy 
12596375eacSRoger Pau Monné static void xn_release_rx_bufs(struct netfront_rxq *);
12696375eacSRoger Pau Monné static void xn_release_tx_bufs(struct netfront_txq *);
12789e0f4d2SKip Macy 
128da695b05SRoger Pau Monné static void xn_rxq_intr(struct netfront_rxq *);
129da695b05SRoger Pau Monné static void xn_txq_intr(struct netfront_txq *);
130da695b05SRoger Pau Monné static void xn_intr(void *);
13196375eacSRoger Pau Monné static int xn_assemble_tx_request(struct netfront_txq *, struct mbuf *);
13202f3b17fSJustin Hibbits static int xn_ioctl(if_t, u_long, caddr_t);
13389e0f4d2SKip Macy static void xn_ifinit_locked(struct netfront_info *);
13489e0f4d2SKip Macy static void xn_ifinit(void *);
13589e0f4d2SKip Macy static void xn_stop(struct netfront_info *);
136578e4bf7SJustin T. Gibbs static void xn_query_features(struct netfront_info *np);
137578e4bf7SJustin T. Gibbs static int xn_configure_features(struct netfront_info *np);
13889e0f4d2SKip Macy static void netif_free(struct netfront_info *info);
13923dc5621SKip Macy static int netfront_detach(device_t dev);
14089e0f4d2SKip Macy 
14196375eacSRoger Pau Monné static int xn_txq_mq_start_locked(struct netfront_txq *, struct mbuf *);
14202f3b17fSJustin Hibbits static int xn_txq_mq_start(if_t, struct mbuf *);
14396375eacSRoger Pau Monné 
14423dc5621SKip Macy static int talk_to_backend(device_t dev, struct netfront_info *info);
14523dc5621SKip Macy static int create_netdev(device_t dev);
14689e0f4d2SKip Macy static void netif_disconnect_backend(struct netfront_info *info);
14796375eacSRoger Pau Monné static int setup_device(device_t dev, struct netfront_info *info,
14896375eacSRoger Pau Monné     unsigned long);
14902f3b17fSJustin Hibbits static int xn_ifmedia_upd(if_t ifp);
15002f3b17fSJustin Hibbits static void xn_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr);
1510e509842SJustin T. Gibbs 
15265671253SRoger Pau Monné static int xn_connect(struct netfront_info *);
15365671253SRoger Pau Monné static void xn_kick_rings(struct netfront_info *);
15489e0f4d2SKip Macy 
15596375eacSRoger Pau Monné static int xn_get_responses(struct netfront_rxq *,
15696375eacSRoger Pau Monné     struct netfront_rx_info *, RING_IDX, RING_IDX *,
15796375eacSRoger Pau Monné     struct mbuf **);
15889e0f4d2SKip Macy 
1593c790178SJohn Baldwin #define virt_to_mfn(x) (vtophys(x) >> PAGE_SHIFT)
16089e0f4d2SKip Macy 
16189e0f4d2SKip Macy #define INVALID_P2M_ENTRY (~0UL)
16296375eacSRoger Pau Monné #define XN_QUEUE_NAME_LEN  8	/* xn{t,r}x_%u, allow for two digits */
16396375eacSRoger Pau Monné struct netfront_rxq {
16496375eacSRoger Pau Monné 	struct netfront_info 	*info;
16596375eacSRoger Pau Monné 	u_int			id;
16696375eacSRoger Pau Monné 	char			name[XN_QUEUE_NAME_LEN];
16796375eacSRoger Pau Monné 	struct mtx		lock;
16896375eacSRoger Pau Monné 
16996375eacSRoger Pau Monné 	int			ring_ref;
17096375eacSRoger Pau Monné 	netif_rx_front_ring_t 	ring;
17196375eacSRoger Pau Monné 	xen_intr_handle_t	xen_intr_handle;
17296375eacSRoger Pau Monné 
17396375eacSRoger Pau Monné 	grant_ref_t 		gref_head;
17436ea5721SOlivier Houchard 	grant_ref_t 		grant_ref[NET_RX_RING_SIZE + 1];
17596375eacSRoger Pau Monné 
17696375eacSRoger Pau Monné 	struct mbuf		*mbufs[NET_RX_RING_SIZE + 1];
17796375eacSRoger Pau Monné 
17896375eacSRoger Pau Monné 	struct lro_ctrl		lro;
17996375eacSRoger Pau Monné 
1802568ee67SRoger Pau Monné 	struct callout		rx_refill;
18196375eacSRoger Pau Monné };
18296375eacSRoger Pau Monné 
18396375eacSRoger Pau Monné struct netfront_txq {
18496375eacSRoger Pau Monné 	struct netfront_info 	*info;
18596375eacSRoger Pau Monné 	u_int 			id;
18696375eacSRoger Pau Monné 	char			name[XN_QUEUE_NAME_LEN];
18796375eacSRoger Pau Monné 	struct mtx		lock;
18896375eacSRoger Pau Monné 
18996375eacSRoger Pau Monné 	int			ring_ref;
19096375eacSRoger Pau Monné 	netif_tx_front_ring_t	ring;
19196375eacSRoger Pau Monné 	xen_intr_handle_t 	xen_intr_handle;
19296375eacSRoger Pau Monné 
19396375eacSRoger Pau Monné 	grant_ref_t		gref_head;
19496375eacSRoger Pau Monné 	grant_ref_t		grant_ref[NET_TX_RING_SIZE + 1];
19596375eacSRoger Pau Monné 
19696375eacSRoger Pau Monné 	struct mbuf		*mbufs[NET_TX_RING_SIZE + 1];
19796375eacSRoger Pau Monné 	int			mbufs_cnt;
19896375eacSRoger Pau Monné 	struct buf_ring		*br;
19996375eacSRoger Pau Monné 
20096375eacSRoger Pau Monné 	struct taskqueue 	*tq;
20196375eacSRoger Pau Monné 	struct task       	defrtask;
20296375eacSRoger Pau Monné 
203dabb3db7SRoger Pau Monné 	bus_dma_segment_t	segs[MAX_TX_REQ_FRAGS];
204dabb3db7SRoger Pau Monné 	struct mbuf_xennet {
205dabb3db7SRoger Pau Monné 		struct m_tag 	tag;
206dabb3db7SRoger Pau Monné 		bus_dma_tag_t	dma_tag;
207dabb3db7SRoger Pau Monné 		bus_dmamap_t	dma_map;
208dabb3db7SRoger Pau Monné 		struct netfront_txq *txq;
209dabb3db7SRoger Pau Monné 		SLIST_ENTRY(mbuf_xennet) next;
210dabb3db7SRoger Pau Monné 		u_int 		count;
211dabb3db7SRoger Pau Monné 	}			xennet_tag[NET_TX_RING_SIZE + 1];
212dabb3db7SRoger Pau Monné 	SLIST_HEAD(, mbuf_xennet) tags;
213dabb3db7SRoger Pau Monné 
21496375eacSRoger Pau Monné 	bool			full;
21596375eacSRoger Pau Monné };
21696375eacSRoger Pau Monné 
21789e0f4d2SKip Macy struct netfront_info {
21802f3b17fSJustin Hibbits 	if_t			xn_ifp;
21989e0f4d2SKip Macy 
220227ca257SKip Macy 	struct mtx   		sc_lock;
22189e0f4d2SKip Macy 
22296375eacSRoger Pau Monné 	u_int  num_queues;
22396375eacSRoger Pau Monné 	struct netfront_rxq 	*rxq;
22496375eacSRoger Pau Monné 	struct netfront_txq 	*txq;
22596375eacSRoger Pau Monné 
22689e0f4d2SKip Macy 	u_int			carrier;
227578e4bf7SJustin T. Gibbs 	u_int			maxfrags;
22889e0f4d2SKip Macy 
22923dc5621SKip Macy 	device_t		xbdev;
23089e0f4d2SKip Macy 	uint8_t			mac[ETHER_ADDR_LEN];
23189e0f4d2SKip Macy 
23289e0f4d2SKip Macy 	int			xn_if_flags;
23389e0f4d2SKip Macy 
2340e509842SJustin T. Gibbs 	struct ifmedia		sc_media;
2356a8e9695SRoger Pau Monné 
236dabb3db7SRoger Pau Monné 	bus_dma_tag_t		dma_tag;
237dabb3db7SRoger Pau Monné 
238c2d12e5eSRoger Pau Monné 	bool			xn_reset;
23989e0f4d2SKip Macy };
24089e0f4d2SKip Macy 
24196375eacSRoger Pau Monné struct netfront_rx_info {
24296375eacSRoger Pau Monné 	struct netif_rx_response rx;
24396375eacSRoger Pau Monné 	struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
24496375eacSRoger Pau Monné };
24589e0f4d2SKip Macy 
24696375eacSRoger Pau Monné #define XN_RX_LOCK(_q)         mtx_lock(&(_q)->lock)
24796375eacSRoger Pau Monné #define XN_RX_UNLOCK(_q)       mtx_unlock(&(_q)->lock)
24889e0f4d2SKip Macy 
24996375eacSRoger Pau Monné #define XN_TX_LOCK(_q)         mtx_lock(&(_q)->lock)
25096375eacSRoger Pau Monné #define XN_TX_TRYLOCK(_q)      mtx_trylock(&(_q)->lock)
25196375eacSRoger Pau Monné #define XN_TX_UNLOCK(_q)       mtx_unlock(&(_q)->lock)
25289e0f4d2SKip Macy 
253227ca257SKip Macy #define XN_LOCK(_sc)           mtx_lock(&(_sc)->sc_lock);
254227ca257SKip Macy #define XN_UNLOCK(_sc)         mtx_unlock(&(_sc)->sc_lock);
25589e0f4d2SKip Macy 
256227ca257SKip Macy #define XN_LOCK_ASSERT(_sc)    mtx_assert(&(_sc)->sc_lock, MA_OWNED);
25796375eacSRoger Pau Monné #define XN_RX_LOCK_ASSERT(_q)  mtx_assert(&(_q)->lock, MA_OWNED);
25896375eacSRoger Pau Monné #define XN_TX_LOCK_ASSERT(_q)  mtx_assert(&(_q)->lock, MA_OWNED);
25989e0f4d2SKip Macy 
26089e0f4d2SKip Macy #define netfront_carrier_on(netif)	((netif)->carrier = 1)
26189e0f4d2SKip Macy #define netfront_carrier_off(netif)	((netif)->carrier = 0)
26289e0f4d2SKip Macy #define netfront_carrier_ok(netif)	((netif)->carrier)
26389e0f4d2SKip Macy 
26489e0f4d2SKip Macy /* Access macros for acquiring freeing slots in xn_free_{tx,rx}_idxs[]. */
26589e0f4d2SKip Macy 
26689e0f4d2SKip Macy static inline void
267931eeffaSKenneth D. Merry add_id_to_freelist(struct mbuf **list, uintptr_t id)
26889e0f4d2SKip Macy {
26996375eacSRoger Pau Monné 
270931eeffaSKenneth D. Merry 	KASSERT(id != 0,
271931eeffaSKenneth D. Merry 		("%s: the head item (0) must always be free.", __func__));
27289e0f4d2SKip Macy 	list[id] = list[0];
273931eeffaSKenneth D. Merry 	list[0]  = (struct mbuf *)id;
27489e0f4d2SKip Macy }
27589e0f4d2SKip Macy 
27689e0f4d2SKip Macy static inline unsigned short
27789e0f4d2SKip Macy get_id_from_freelist(struct mbuf **list)
27889e0f4d2SKip Macy {
279931eeffaSKenneth D. Merry 	uintptr_t id;
280931eeffaSKenneth D. Merry 
281931eeffaSKenneth D. Merry 	id = (uintptr_t)list[0];
282931eeffaSKenneth D. Merry 	KASSERT(id != 0,
283931eeffaSKenneth D. Merry 		("%s: the head item (0) must always remain free.", __func__));
28489e0f4d2SKip Macy 	list[0] = list[id];
28589e0f4d2SKip Macy 	return (id);
28689e0f4d2SKip Macy }
28789e0f4d2SKip Macy 
28889e0f4d2SKip Macy static inline int
28996375eacSRoger Pau Monné xn_rxidx(RING_IDX idx)
29089e0f4d2SKip Macy {
29196375eacSRoger Pau Monné 
29289e0f4d2SKip Macy 	return idx & (NET_RX_RING_SIZE - 1);
29389e0f4d2SKip Macy }
29489e0f4d2SKip Macy 
29589e0f4d2SKip Macy static inline struct mbuf *
29696375eacSRoger Pau Monné xn_get_rx_mbuf(struct netfront_rxq *rxq, RING_IDX ri)
29789e0f4d2SKip Macy {
29896375eacSRoger Pau Monné 	int i;
29989e0f4d2SKip Macy 	struct mbuf *m;
30089e0f4d2SKip Macy 
30196375eacSRoger Pau Monné 	i = xn_rxidx(ri);
30296375eacSRoger Pau Monné 	m = rxq->mbufs[i];
30396375eacSRoger Pau Monné 	rxq->mbufs[i] = NULL;
30489e0f4d2SKip Macy 	return (m);
30589e0f4d2SKip Macy }
30689e0f4d2SKip Macy 
30789e0f4d2SKip Macy static inline grant_ref_t
30896375eacSRoger Pau Monné xn_get_rx_ref(struct netfront_rxq *rxq, RING_IDX ri)
30989e0f4d2SKip Macy {
31096375eacSRoger Pau Monné 	int i = xn_rxidx(ri);
31196375eacSRoger Pau Monné 	grant_ref_t ref = rxq->grant_ref[i];
31296375eacSRoger Pau Monné 
313ff662b5cSJustin T. Gibbs 	KASSERT(ref != GRANT_REF_INVALID, ("Invalid grant reference!\n"));
31496375eacSRoger Pau Monné 	rxq->grant_ref[i] = GRANT_REF_INVALID;
31596375eacSRoger Pau Monné 	return (ref);
31689e0f4d2SKip Macy }
31789e0f4d2SKip Macy 
318dabb3db7SRoger Pau Monné #define MTAG_COOKIE 1218492000
319dabb3db7SRoger Pau Monné #define MTAG_XENNET 0
320dabb3db7SRoger Pau Monné 
321dabb3db7SRoger Pau Monné static void mbuf_grab(struct mbuf *m)
322dabb3db7SRoger Pau Monné {
323dabb3db7SRoger Pau Monné 	struct mbuf_xennet *ref;
324dabb3db7SRoger Pau Monné 
325dabb3db7SRoger Pau Monné 	ref = (struct mbuf_xennet *)m_tag_locate(m, MTAG_COOKIE,
326dabb3db7SRoger Pau Monné 	    MTAG_XENNET, NULL);
327dabb3db7SRoger Pau Monné 	KASSERT(ref != NULL, ("Cannot find refcount"));
328dabb3db7SRoger Pau Monné 	ref->count++;
329dabb3db7SRoger Pau Monné }
330dabb3db7SRoger Pau Monné 
331dabb3db7SRoger Pau Monné static void mbuf_release(struct mbuf *m)
332dabb3db7SRoger Pau Monné {
333dabb3db7SRoger Pau Monné 	struct mbuf_xennet *ref;
334dabb3db7SRoger Pau Monné 
335dabb3db7SRoger Pau Monné 	ref = (struct mbuf_xennet *)m_tag_locate(m, MTAG_COOKIE,
336dabb3db7SRoger Pau Monné 	    MTAG_XENNET, NULL);
337dabb3db7SRoger Pau Monné 	KASSERT(ref != NULL, ("Cannot find refcount"));
338dabb3db7SRoger Pau Monné 	KASSERT(ref->count > 0, ("Invalid reference count"));
339dabb3db7SRoger Pau Monné 
340dabb3db7SRoger Pau Monné 	if (--ref->count == 0)
341dabb3db7SRoger Pau Monné 		m_freem(m);
342dabb3db7SRoger Pau Monné }
343dabb3db7SRoger Pau Monné 
344dabb3db7SRoger Pau Monné static void tag_free(struct m_tag *t)
345dabb3db7SRoger Pau Monné {
346dabb3db7SRoger Pau Monné 	struct mbuf_xennet *ref = (struct mbuf_xennet *)t;
347dabb3db7SRoger Pau Monné 
348dabb3db7SRoger Pau Monné 	KASSERT(ref->count == 0, ("Free mbuf tag with pending refcnt"));
349dabb3db7SRoger Pau Monné 	bus_dmamap_sync(ref->dma_tag, ref->dma_map, BUS_DMASYNC_POSTWRITE);
350dabb3db7SRoger Pau Monné 	bus_dmamap_destroy(ref->dma_tag, ref->dma_map);
351dabb3db7SRoger Pau Monné 	SLIST_INSERT_HEAD(&ref->txq->tags, ref, next);
352dabb3db7SRoger Pau Monné }
353dabb3db7SRoger Pau Monné 
35489e0f4d2SKip Macy #define IPRINTK(fmt, args...) \
35589e0f4d2SKip Macy     printf("[XEN] " fmt, ##args)
356227ca257SKip Macy #ifdef INVARIANTS
35789e0f4d2SKip Macy #define WPRINTK(fmt, args...) \
35889e0f4d2SKip Macy     printf("[XEN] " fmt, ##args)
359227ca257SKip Macy #else
360227ca257SKip Macy #define WPRINTK(fmt, args...)
361227ca257SKip Macy #endif
362227ca257SKip Macy #ifdef DEBUG
36389e0f4d2SKip Macy #define DPRINTK(fmt, args...) \
36423dc5621SKip Macy     printf("[XEN] %s: " fmt, __func__, ##args)
36512678024SDoug Rabson #else
36612678024SDoug Rabson #define DPRINTK(fmt, args...)
36712678024SDoug Rabson #endif
36889e0f4d2SKip Macy 
36989e0f4d2SKip Macy /**
37089e0f4d2SKip Macy  * Read the 'mac' node at the given device's node in the store, and parse that
37189e0f4d2SKip Macy  * as colon-separated octets, placing result the given mac array.  mac must be
37289e0f4d2SKip Macy  * a preallocated array of length ETH_ALEN (as declared in linux/if_ether.h).
37389e0f4d2SKip Macy  * Return 0 on success, or errno on error.
37489e0f4d2SKip Macy  */
37589e0f4d2SKip Macy static int
37623dc5621SKip Macy xen_net_read_mac(device_t dev, uint8_t mac[])
37789e0f4d2SKip Macy {
3783a6d1fcfSKip Macy 	int error, i;
3793a6d1fcfSKip Macy 	char *s, *e, *macstr;
380ffa06904SJustin T. Gibbs 	const char *path;
3813a6d1fcfSKip Macy 
382ffa06904SJustin T. Gibbs 	path = xenbus_get_node(dev);
383ffa06904SJustin T. Gibbs 	error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr);
384ffa06904SJustin T. Gibbs 	if (error == ENOENT) {
385ffa06904SJustin T. Gibbs 		/*
386ffa06904SJustin T. Gibbs 		 * Deal with missing mac XenStore nodes on devices with
387ffa06904SJustin T. Gibbs 		 * HVM emulation (the 'ioemu' configuration attribute)
388ffa06904SJustin T. Gibbs 		 * enabled.
389ffa06904SJustin T. Gibbs 		 *
390ffa06904SJustin T. Gibbs 		 * The HVM emulator may execute in a stub device model
391ffa06904SJustin T. Gibbs 		 * domain which lacks the permission, only given to Dom0,
392ffa06904SJustin T. Gibbs 		 * to update the guest's XenStore tree.  For this reason,
393ffa06904SJustin T. Gibbs 		 * the HVM emulator doesn't even attempt to write the
394ffa06904SJustin T. Gibbs 		 * front-side mac node, even when operating in Dom0.
395ffa06904SJustin T. Gibbs 		 * However, there should always be a mac listed in the
396ffa06904SJustin T. Gibbs 		 * backend tree.  Fallback to this version if our query
397ffa06904SJustin T. Gibbs 		 * of the front side XenStore location doesn't find
398ffa06904SJustin T. Gibbs 		 * anything.
399ffa06904SJustin T. Gibbs 		 */
400ffa06904SJustin T. Gibbs 		path = xenbus_get_otherend_path(dev);
401ffa06904SJustin T. Gibbs 		error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr);
402ffa06904SJustin T. Gibbs 	}
403ffa06904SJustin T. Gibbs 	if (error != 0) {
404ffa06904SJustin T. Gibbs 		xenbus_dev_fatal(dev, error, "parsing %s/mac", path);
4053a6d1fcfSKip Macy 		return (error);
406ffa06904SJustin T. Gibbs 	}
4073a6d1fcfSKip Macy 
40889e0f4d2SKip Macy 	s = macstr;
40989e0f4d2SKip Macy 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
41089e0f4d2SKip Macy 		mac[i] = strtoul(s, &e, 16);
41189e0f4d2SKip Macy 		if (s == e || (e[0] != ':' && e[0] != 0)) {
412ff662b5cSJustin T. Gibbs 			free(macstr, M_XENBUS);
4133a6d1fcfSKip Macy 			return (ENOENT);
41489e0f4d2SKip Macy 		}
41589e0f4d2SKip Macy 		s = &e[1];
41689e0f4d2SKip Macy 	}
417ff662b5cSJustin T. Gibbs 	free(macstr, M_XENBUS);
4183a6d1fcfSKip Macy 	return (0);
41989e0f4d2SKip Macy }
42089e0f4d2SKip Macy 
42189e0f4d2SKip Macy /**
42289e0f4d2SKip Macy  * Entry point to this code when a new device is created.  Allocate the basic
42389e0f4d2SKip Macy  * structures and the ring buffers for communication with the backend, and
42489e0f4d2SKip Macy  * inform the backend of the appropriate details for those.  Switch to
42589e0f4d2SKip Macy  * Connected state.
42689e0f4d2SKip Macy  */
42789e0f4d2SKip Macy static int
42823dc5621SKip Macy netfront_probe(device_t dev)
42923dc5621SKip Macy {
43023dc5621SKip Macy 
4315f700083SJulien Grall 	if (xen_pv_nics_disabled())
432f8f1bb83SRoger Pau Monné 		return (ENXIO);
433f8f1bb83SRoger Pau Monné 
43423dc5621SKip Macy 	if (!strcmp(xenbus_get_type(dev), "vif")) {
43523dc5621SKip Macy 		device_set_desc(dev, "Virtual Network Interface");
43623dc5621SKip Macy 		return (0);
43723dc5621SKip Macy 	}
43823dc5621SKip Macy 
43923dc5621SKip Macy 	return (ENXIO);
44023dc5621SKip Macy }
44123dc5621SKip Macy 
44223dc5621SKip Macy static int
44323dc5621SKip Macy netfront_attach(device_t dev)
44489e0f4d2SKip Macy {
44589e0f4d2SKip Macy 	int err;
44689e0f4d2SKip Macy 
44723dc5621SKip Macy 	err = create_netdev(dev);
44896375eacSRoger Pau Monné 	if (err != 0) {
44989e0f4d2SKip Macy 		xenbus_dev_fatal(dev, err, "creating netdev");
450ffa06904SJustin T. Gibbs 		return (err);
45189e0f4d2SKip Macy 	}
45289e0f4d2SKip Macy 
45312678024SDoug Rabson 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
45412678024SDoug Rabson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
455f0188618SHans Petter Selasky 	    OID_AUTO, "enable_lro", CTLFLAG_RW,
45612678024SDoug Rabson 	    &xn_enable_lro, 0, "Large Receive Offload");
45712678024SDoug Rabson 
45896375eacSRoger Pau Monné 	SYSCTL_ADD_ULONG(device_get_sysctl_ctx(dev),
45996375eacSRoger Pau Monné 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
46096375eacSRoger Pau Monné 	    OID_AUTO, "num_queues", CTLFLAG_RD,
46196375eacSRoger Pau Monné 	    &xn_num_queues, "Number of pairs of queues");
46296375eacSRoger Pau Monné 
463ffa06904SJustin T. Gibbs 	return (0);
46489e0f4d2SKip Macy }
46589e0f4d2SKip Macy 
466cf9c09e1SJustin T. Gibbs static int
467cf9c09e1SJustin T. Gibbs netfront_suspend(device_t dev)
468cf9c09e1SJustin T. Gibbs {
46996375eacSRoger Pau Monné 	struct netfront_info *np = device_get_softc(dev);
47096375eacSRoger Pau Monné 	u_int i;
471cf9c09e1SJustin T. Gibbs 
47296375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
47396375eacSRoger Pau Monné 		XN_RX_LOCK(&np->rxq[i]);
47496375eacSRoger Pau Monné 		XN_TX_LOCK(&np->txq[i]);
47596375eacSRoger Pau Monné 	}
47696375eacSRoger Pau Monné 	netfront_carrier_off(np);
47796375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
47896375eacSRoger Pau Monné 		XN_RX_UNLOCK(&np->rxq[i]);
47996375eacSRoger Pau Monné 		XN_TX_UNLOCK(&np->txq[i]);
48096375eacSRoger Pau Monné 	}
481cf9c09e1SJustin T. Gibbs 	return (0);
482cf9c09e1SJustin T. Gibbs }
48389e0f4d2SKip Macy 
48489e0f4d2SKip Macy /**
48589e0f4d2SKip Macy  * We are reconnecting to the backend, due to a suspend/resume, or a backend
48689e0f4d2SKip Macy  * driver restart.  We tear down our netif structure and recreate it, but
48789e0f4d2SKip Macy  * leave the device-layer structures intact so that this is transparent to the
48889e0f4d2SKip Macy  * rest of the kernel.
48989e0f4d2SKip Macy  */
49089e0f4d2SKip Macy static int
49123dc5621SKip Macy netfront_resume(device_t dev)
49289e0f4d2SKip Macy {
49323dc5621SKip Macy 	struct netfront_info *info = device_get_softc(dev);
4948dee0e9bSRoger Pau Monné 	u_int i;
4958dee0e9bSRoger Pau Monné 
4968dee0e9bSRoger Pau Monné 	if (xen_suspend_cancelled) {
4978dee0e9bSRoger Pau Monné 		for (i = 0; i < info->num_queues; i++) {
4988dee0e9bSRoger Pau Monné 			XN_RX_LOCK(&info->rxq[i]);
4998dee0e9bSRoger Pau Monné 			XN_TX_LOCK(&info->txq[i]);
5008dee0e9bSRoger Pau Monné 		}
5018dee0e9bSRoger Pau Monné 		netfront_carrier_on(info);
5028dee0e9bSRoger Pau Monné 		for (i = 0; i < info->num_queues; i++) {
5038dee0e9bSRoger Pau Monné 			XN_RX_UNLOCK(&info->rxq[i]);
5048dee0e9bSRoger Pau Monné 			XN_TX_UNLOCK(&info->txq[i]);
5058dee0e9bSRoger Pau Monné 		}
5068dee0e9bSRoger Pau Monné 		return (0);
5078dee0e9bSRoger Pau Monné 	}
50889e0f4d2SKip Macy 
50989e0f4d2SKip Macy 	netif_disconnect_backend(info);
51089e0f4d2SKip Macy 	return (0);
51189e0f4d2SKip Macy }
51289e0f4d2SKip Macy 
51396375eacSRoger Pau Monné static int
51496375eacSRoger Pau Monné write_queue_xenstore_keys(device_t dev,
51596375eacSRoger Pau Monné     struct netfront_rxq *rxq,
51696375eacSRoger Pau Monné     struct netfront_txq *txq,
51796375eacSRoger Pau Monné     struct xs_transaction *xst, bool hierarchy)
51896375eacSRoger Pau Monné {
51996375eacSRoger Pau Monné 	int err;
52096375eacSRoger Pau Monné 	const char *message;
52196375eacSRoger Pau Monné 	const char *node = xenbus_get_node(dev);
52296375eacSRoger Pau Monné 	char *path;
52396375eacSRoger Pau Monné 	size_t path_size;
52496375eacSRoger Pau Monné 
52596375eacSRoger Pau Monné 	KASSERT(rxq->id == txq->id, ("Mismatch between RX and TX queue ids"));
52696375eacSRoger Pau Monné 	/* Split event channel support is not yet there. */
52796375eacSRoger Pau Monné 	KASSERT(rxq->xen_intr_handle == txq->xen_intr_handle,
52896375eacSRoger Pau Monné 	    ("Split event channels are not supported"));
52996375eacSRoger Pau Monné 
53096375eacSRoger Pau Monné 	if (hierarchy) {
53196375eacSRoger Pau Monné 		path_size = strlen(node) + 10;
53296375eacSRoger Pau Monné 		path = malloc(path_size, M_DEVBUF, M_WAITOK|M_ZERO);
53396375eacSRoger Pau Monné 		snprintf(path, path_size, "%s/queue-%u", node, rxq->id);
53496375eacSRoger Pau Monné 	} else {
53596375eacSRoger Pau Monné 		path_size = strlen(node) + 1;
53696375eacSRoger Pau Monné 		path = malloc(path_size, M_DEVBUF, M_WAITOK|M_ZERO);
53796375eacSRoger Pau Monné 		snprintf(path, path_size, "%s", node);
53896375eacSRoger Pau Monné 	}
53996375eacSRoger Pau Monné 
54096375eacSRoger Pau Monné 	err = xs_printf(*xst, path, "tx-ring-ref","%u", txq->ring_ref);
54196375eacSRoger Pau Monné 	if (err != 0) {
54296375eacSRoger Pau Monné 		message = "writing tx ring-ref";
54396375eacSRoger Pau Monné 		goto error;
54496375eacSRoger Pau Monné 	}
54596375eacSRoger Pau Monné 	err = xs_printf(*xst, path, "rx-ring-ref","%u", rxq->ring_ref);
54696375eacSRoger Pau Monné 	if (err != 0) {
54796375eacSRoger Pau Monné 		message = "writing rx ring-ref";
54896375eacSRoger Pau Monné 		goto error;
54996375eacSRoger Pau Monné 	}
55096375eacSRoger Pau Monné 	err = xs_printf(*xst, path, "event-channel", "%u",
55196375eacSRoger Pau Monné 	    xen_intr_port(rxq->xen_intr_handle));
55296375eacSRoger Pau Monné 	if (err != 0) {
55396375eacSRoger Pau Monné 		message = "writing event-channel";
55496375eacSRoger Pau Monné 		goto error;
55596375eacSRoger Pau Monné 	}
55696375eacSRoger Pau Monné 
55796375eacSRoger Pau Monné 	free(path, M_DEVBUF);
55896375eacSRoger Pau Monné 
55996375eacSRoger Pau Monné 	return (0);
56096375eacSRoger Pau Monné 
56196375eacSRoger Pau Monné error:
56296375eacSRoger Pau Monné 	free(path, M_DEVBUF);
56396375eacSRoger Pau Monné 	xenbus_dev_fatal(dev, err, "%s", message);
56496375eacSRoger Pau Monné 
56596375eacSRoger Pau Monné 	return (err);
56696375eacSRoger Pau Monné }
56796375eacSRoger Pau Monné 
56889e0f4d2SKip Macy /* Common code used when first setting up, and when resuming. */
56989e0f4d2SKip Macy static int
57023dc5621SKip Macy talk_to_backend(device_t dev, struct netfront_info *info)
57189e0f4d2SKip Macy {
57289e0f4d2SKip Macy 	const char *message;
573ff662b5cSJustin T. Gibbs 	struct xs_transaction xst;
57423dc5621SKip Macy 	const char *node = xenbus_get_node(dev);
57589e0f4d2SKip Macy 	int err;
57696375eacSRoger Pau Monné 	unsigned long num_queues, max_queues = 0;
57796375eacSRoger Pau Monné 	unsigned int i;
57889e0f4d2SKip Macy 
57989e0f4d2SKip Macy 	err = xen_net_read_mac(dev, info->mac);
58096375eacSRoger Pau Monné 	if (err != 0) {
58123dc5621SKip Macy 		xenbus_dev_fatal(dev, err, "parsing %s/mac", node);
58289e0f4d2SKip Macy 		goto out;
58389e0f4d2SKip Macy 	}
58489e0f4d2SKip Macy 
58596375eacSRoger Pau Monné 	err = xs_scanf(XST_NIL, xenbus_get_otherend_path(info->xbdev),
58696375eacSRoger Pau Monné 	    "multi-queue-max-queues", NULL, "%lu", &max_queues);
58796375eacSRoger Pau Monné 	if (err != 0)
58896375eacSRoger Pau Monné 		max_queues = 1;
58996375eacSRoger Pau Monné 	num_queues = xn_num_queues;
59096375eacSRoger Pau Monné 	if (num_queues > max_queues)
59196375eacSRoger Pau Monné 		num_queues = max_queues;
59296375eacSRoger Pau Monné 
59396375eacSRoger Pau Monné 	err = setup_device(dev, info, num_queues);
59496375eacSRoger Pau Monné 	if (err != 0)
59589e0f4d2SKip Macy 		goto out;
59689e0f4d2SKip Macy 
59789e0f4d2SKip Macy  again:
598ff662b5cSJustin T. Gibbs 	err = xs_transaction_start(&xst);
59996375eacSRoger Pau Monné 	if (err != 0) {
60089e0f4d2SKip Macy 		xenbus_dev_fatal(dev, err, "starting transaction");
60196375eacSRoger Pau Monné 		goto free;
60289e0f4d2SKip Macy 	}
60396375eacSRoger Pau Monné 
60496375eacSRoger Pau Monné 	if (info->num_queues == 1) {
60596375eacSRoger Pau Monné 		err = write_queue_xenstore_keys(dev, &info->rxq[0],
60696375eacSRoger Pau Monné 		    &info->txq[0], &xst, false);
60796375eacSRoger Pau Monné 		if (err != 0)
60896375eacSRoger Pau Monné 			goto abort_transaction_no_def_error;
60996375eacSRoger Pau Monné 	} else {
61096375eacSRoger Pau Monné 		err = xs_printf(xst, node, "multi-queue-num-queues",
61196375eacSRoger Pau Monné 		    "%u", info->num_queues);
61296375eacSRoger Pau Monné 		if (err != 0) {
61396375eacSRoger Pau Monné 			message = "writing multi-queue-num-queues";
61489e0f4d2SKip Macy 			goto abort_transaction;
61589e0f4d2SKip Macy 		}
61696375eacSRoger Pau Monné 
61796375eacSRoger Pau Monné 		for (i = 0; i < info->num_queues; i++) {
61896375eacSRoger Pau Monné 			err = write_queue_xenstore_keys(dev, &info->rxq[i],
61996375eacSRoger Pau Monné 			    &info->txq[i], &xst, true);
62096375eacSRoger Pau Monné 			if (err != 0)
62196375eacSRoger Pau Monné 				goto abort_transaction_no_def_error;
62289e0f4d2SKip Macy 		}
62389e0f4d2SKip Macy 	}
62496375eacSRoger Pau Monné 
625d0f3a8b9SRoger Pau Monné 	err = xs_printf(xst, node, "request-rx-copy", "%u", 1);
62696375eacSRoger Pau Monné 	if (err != 0) {
62789e0f4d2SKip Macy 		message = "writing request-rx-copy";
62889e0f4d2SKip Macy 		goto abort_transaction;
62989e0f4d2SKip Macy 	}
630ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "feature-rx-notify", "%d", 1);
63196375eacSRoger Pau Monné 	if (err != 0) {
63289e0f4d2SKip Macy 		message = "writing feature-rx-notify";
63389e0f4d2SKip Macy 		goto abort_transaction;
63489e0f4d2SKip Macy 	}
635ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "feature-sg", "%d", 1);
63696375eacSRoger Pau Monné 	if (err != 0) {
63789e0f4d2SKip Macy 		message = "writing feature-sg";
63889e0f4d2SKip Macy 		goto abort_transaction;
63989e0f4d2SKip Macy 	}
64002f3b17fSJustin Hibbits 	if ((if_getcapenable(info->xn_ifp) & IFCAP_LRO) != 0) {
641ff662b5cSJustin T. Gibbs 		err = xs_printf(xst, node, "feature-gso-tcpv4", "%d", 1);
64296375eacSRoger Pau Monné 		if (err != 0) {
64389e0f4d2SKip Macy 			message = "writing feature-gso-tcpv4";
64489e0f4d2SKip Macy 			goto abort_transaction;
64589e0f4d2SKip Macy 		}
646c2d12e5eSRoger Pau Monné 	}
64702f3b17fSJustin Hibbits 	if ((if_getcapenable(info->xn_ifp) & IFCAP_RXCSUM) == 0) {
648c2d12e5eSRoger Pau Monné 		err = xs_printf(xst, node, "feature-no-csum-offload", "%d", 1);
649c2d12e5eSRoger Pau Monné 		if (err != 0) {
650c2d12e5eSRoger Pau Monné 			message = "writing feature-no-csum-offload";
651c2d12e5eSRoger Pau Monné 			goto abort_transaction;
652c2d12e5eSRoger Pau Monné 		}
653c2d12e5eSRoger Pau Monné 	}
65489e0f4d2SKip Macy 
655ff662b5cSJustin T. Gibbs 	err = xs_transaction_end(xst, 0);
65696375eacSRoger Pau Monné 	if (err != 0) {
65789e0f4d2SKip Macy 		if (err == EAGAIN)
65889e0f4d2SKip Macy 			goto again;
65989e0f4d2SKip Macy 		xenbus_dev_fatal(dev, err, "completing transaction");
66096375eacSRoger Pau Monné 		goto free;
66189e0f4d2SKip Macy 	}
66289e0f4d2SKip Macy 
66389e0f4d2SKip Macy 	return 0;
66489e0f4d2SKip Macy 
66589e0f4d2SKip Macy  abort_transaction:
66689e0f4d2SKip Macy 	xenbus_dev_fatal(dev, err, "%s", message);
66796375eacSRoger Pau Monné  abort_transaction_no_def_error:
66896375eacSRoger Pau Monné 	xs_transaction_end(xst, 1);
66996375eacSRoger Pau Monné  free:
67089e0f4d2SKip Macy 	netif_free(info);
67189e0f4d2SKip Macy  out:
67296375eacSRoger Pau Monné 	return (err);
67396375eacSRoger Pau Monné }
67496375eacSRoger Pau Monné 
67596375eacSRoger Pau Monné static void
676da695b05SRoger Pau Monné xn_rxq_intr(struct netfront_rxq *rxq)
67796375eacSRoger Pau Monné {
67896375eacSRoger Pau Monné 
67996375eacSRoger Pau Monné 	XN_RX_LOCK(rxq);
68096375eacSRoger Pau Monné 	xn_rxeof(rxq);
68196375eacSRoger Pau Monné 	XN_RX_UNLOCK(rxq);
68296375eacSRoger Pau Monné }
68396375eacSRoger Pau Monné 
68496375eacSRoger Pau Monné static void
68596375eacSRoger Pau Monné xn_txq_start(struct netfront_txq *txq)
68696375eacSRoger Pau Monné {
68796375eacSRoger Pau Monné 	struct netfront_info *np = txq->info;
68802f3b17fSJustin Hibbits 	if_t ifp = np->xn_ifp;
68996375eacSRoger Pau Monné 
69096375eacSRoger Pau Monné 	XN_TX_LOCK_ASSERT(txq);
69196375eacSRoger Pau Monné 	if (!drbr_empty(ifp, txq->br))
69296375eacSRoger Pau Monné 		xn_txq_mq_start_locked(txq, NULL);
69396375eacSRoger Pau Monné }
69496375eacSRoger Pau Monné 
69596375eacSRoger Pau Monné static void
696da695b05SRoger Pau Monné xn_txq_intr(struct netfront_txq *txq)
69796375eacSRoger Pau Monné {
69896375eacSRoger Pau Monné 
69996375eacSRoger Pau Monné 	XN_TX_LOCK(txq);
70096375eacSRoger Pau Monné 	if (RING_HAS_UNCONSUMED_RESPONSES(&txq->ring))
70196375eacSRoger Pau Monné 		xn_txeof(txq);
70296375eacSRoger Pau Monné 	xn_txq_start(txq);
70396375eacSRoger Pau Monné 	XN_TX_UNLOCK(txq);
70496375eacSRoger Pau Monné }
70596375eacSRoger Pau Monné 
70696375eacSRoger Pau Monné static void
70796375eacSRoger Pau Monné xn_txq_tq_deferred(void *xtxq, int pending)
70896375eacSRoger Pau Monné {
70996375eacSRoger Pau Monné 	struct netfront_txq *txq = xtxq;
71096375eacSRoger Pau Monné 
71196375eacSRoger Pau Monné 	XN_TX_LOCK(txq);
71296375eacSRoger Pau Monné 	xn_txq_start(txq);
71396375eacSRoger Pau Monné 	XN_TX_UNLOCK(txq);
71496375eacSRoger Pau Monné }
71596375eacSRoger Pau Monné 
71696375eacSRoger Pau Monné static void
71796375eacSRoger Pau Monné disconnect_rxq(struct netfront_rxq *rxq)
71896375eacSRoger Pau Monné {
71996375eacSRoger Pau Monné 
72096375eacSRoger Pau Monné 	xn_release_rx_bufs(rxq);
72196375eacSRoger Pau Monné 	gnttab_free_grant_references(rxq->gref_head);
722d039b070SRoger Pau Monné 	gnttab_end_foreign_access(rxq->ring_ref, NULL);
72396375eacSRoger Pau Monné 	/*
72496375eacSRoger Pau Monné 	 * No split event channel support at the moment, handle will
72596375eacSRoger Pau Monné 	 * be unbound in tx. So no need to call xen_intr_unbind here,
72696375eacSRoger Pau Monné 	 * but we do want to reset the handler to 0.
72796375eacSRoger Pau Monné 	 */
72896375eacSRoger Pau Monné 	rxq->xen_intr_handle = 0;
72996375eacSRoger Pau Monné }
73096375eacSRoger Pau Monné 
73196375eacSRoger Pau Monné static void
73296375eacSRoger Pau Monné destroy_rxq(struct netfront_rxq *rxq)
73396375eacSRoger Pau Monné {
73496375eacSRoger Pau Monné 
7352568ee67SRoger Pau Monné 	callout_drain(&rxq->rx_refill);
73696375eacSRoger Pau Monné 	free(rxq->ring.sring, M_DEVBUF);
73796375eacSRoger Pau Monné }
73896375eacSRoger Pau Monné 
73996375eacSRoger Pau Monné static void
74096375eacSRoger Pau Monné destroy_rxqs(struct netfront_info *np)
74196375eacSRoger Pau Monné {
74296375eacSRoger Pau Monné 	int i;
74396375eacSRoger Pau Monné 
74496375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++)
74596375eacSRoger Pau Monné 		destroy_rxq(&np->rxq[i]);
74696375eacSRoger Pau Monné 
74796375eacSRoger Pau Monné 	free(np->rxq, M_DEVBUF);
74896375eacSRoger Pau Monné 	np->rxq = NULL;
74989e0f4d2SKip Macy }
75089e0f4d2SKip Macy 
75189e0f4d2SKip Macy static int
75296375eacSRoger Pau Monné setup_rxqs(device_t dev, struct netfront_info *info,
75396375eacSRoger Pau Monné 	   unsigned long num_queues)
75489e0f4d2SKip Macy {
75596375eacSRoger Pau Monné 	int q, i;
7563a6d1fcfSKip Macy 	int error;
75796375eacSRoger Pau Monné 	netif_rx_sring_t *rxs;
75896375eacSRoger Pau Monné 	struct netfront_rxq *rxq;
75989e0f4d2SKip Macy 
76096375eacSRoger Pau Monné 	info->rxq = malloc(sizeof(struct netfront_rxq) * num_queues,
76196375eacSRoger Pau Monné 	    M_DEVBUF, M_WAITOK|M_ZERO);
76289e0f4d2SKip Macy 
76396375eacSRoger Pau Monné 	for (q = 0; q < num_queues; q++) {
76496375eacSRoger Pau Monné 		rxq = &info->rxq[q];
76596375eacSRoger Pau Monné 
76696375eacSRoger Pau Monné 		rxq->id = q;
76796375eacSRoger Pau Monné 		rxq->info = info;
76896375eacSRoger Pau Monné 		rxq->ring_ref = GRANT_REF_INVALID;
76996375eacSRoger Pau Monné 		rxq->ring.sring = NULL;
77096375eacSRoger Pau Monné 		snprintf(rxq->name, XN_QUEUE_NAME_LEN, "xnrx_%u", q);
77196375eacSRoger Pau Monné 		mtx_init(&rxq->lock, rxq->name, "netfront receive lock",
77296375eacSRoger Pau Monné 		    MTX_DEF);
77396375eacSRoger Pau Monné 
77496375eacSRoger Pau Monné 		for (i = 0; i <= NET_RX_RING_SIZE; i++) {
77596375eacSRoger Pau Monné 			rxq->mbufs[i] = NULL;
77696375eacSRoger Pau Monné 			rxq->grant_ref[i] = GRANT_REF_INVALID;
77796375eacSRoger Pau Monné 		}
77896375eacSRoger Pau Monné 
77996375eacSRoger Pau Monné 		/* Start resources allocation */
78096375eacSRoger Pau Monné 
7812568ee67SRoger Pau Monné 		if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
78296375eacSRoger Pau Monné 		    &rxq->gref_head) != 0) {
78396375eacSRoger Pau Monné 			device_printf(dev, "allocating rx gref");
7843a6d1fcfSKip Macy 			error = ENOMEM;
78589e0f4d2SKip Macy 			goto fail;
78689e0f4d2SKip Macy 		}
78789e0f4d2SKip Macy 
78896375eacSRoger Pau Monné 		rxs = (netif_rx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF,
78996375eacSRoger Pau Monné 		    M_WAITOK|M_ZERO);
79089e0f4d2SKip Macy 		SHARED_RING_INIT(rxs);
79196375eacSRoger Pau Monné 		FRONT_RING_INIT(&rxq->ring, rxs, PAGE_SIZE);
79289e0f4d2SKip Macy 
79396375eacSRoger Pau Monné 		error = xenbus_grant_ring(dev, virt_to_mfn(rxs),
79496375eacSRoger Pau Monné 		    &rxq->ring_ref);
79596375eacSRoger Pau Monné 		if (error != 0) {
79696375eacSRoger Pau Monné 			device_printf(dev, "granting rx ring page");
79796375eacSRoger Pau Monné 			goto fail_grant_ring;
79896375eacSRoger Pau Monné 		}
79989e0f4d2SKip Macy 
8002568ee67SRoger Pau Monné 		callout_init(&rxq->rx_refill, 1);
80189e0f4d2SKip Macy 	}
80289e0f4d2SKip Macy 
8033a6d1fcfSKip Macy 	return (0);
80489e0f4d2SKip Macy 
80596375eacSRoger Pau Monné fail_grant_ring:
80696375eacSRoger Pau Monné 	gnttab_free_grant_references(rxq->gref_head);
80796375eacSRoger Pau Monné 	free(rxq->ring.sring, M_DEVBUF);
80889e0f4d2SKip Macy fail:
80996375eacSRoger Pau Monné 	for (; q >= 0; q--) {
81096375eacSRoger Pau Monné 		disconnect_rxq(&info->rxq[q]);
81196375eacSRoger Pau Monné 		destroy_rxq(&info->rxq[q]);
81296375eacSRoger Pau Monné 	}
81396375eacSRoger Pau Monné 
81496375eacSRoger Pau Monné 	free(info->rxq, M_DEVBUF);
81596375eacSRoger Pau Monné 	return (error);
81696375eacSRoger Pau Monné }
81796375eacSRoger Pau Monné 
81896375eacSRoger Pau Monné static void
81996375eacSRoger Pau Monné disconnect_txq(struct netfront_txq *txq)
82096375eacSRoger Pau Monné {
82196375eacSRoger Pau Monné 
82296375eacSRoger Pau Monné 	xn_release_tx_bufs(txq);
82396375eacSRoger Pau Monné 	gnttab_free_grant_references(txq->gref_head);
824d039b070SRoger Pau Monné 	gnttab_end_foreign_access(txq->ring_ref, NULL);
82596375eacSRoger Pau Monné 	xen_intr_unbind(&txq->xen_intr_handle);
82696375eacSRoger Pau Monné }
82796375eacSRoger Pau Monné 
82896375eacSRoger Pau Monné static void
82996375eacSRoger Pau Monné destroy_txq(struct netfront_txq *txq)
83096375eacSRoger Pau Monné {
831dabb3db7SRoger Pau Monné 	unsigned int i;
83296375eacSRoger Pau Monné 
83396375eacSRoger Pau Monné 	free(txq->ring.sring, M_DEVBUF);
83496375eacSRoger Pau Monné 	buf_ring_free(txq->br, M_DEVBUF);
83596375eacSRoger Pau Monné 	taskqueue_drain_all(txq->tq);
83696375eacSRoger Pau Monné 	taskqueue_free(txq->tq);
837dabb3db7SRoger Pau Monné 
838dabb3db7SRoger Pau Monné 	for (i = 0; i <= NET_TX_RING_SIZE; i++) {
839dabb3db7SRoger Pau Monné 		bus_dmamap_destroy(txq->info->dma_tag,
840dabb3db7SRoger Pau Monné 		    txq->xennet_tag[i].dma_map);
841dabb3db7SRoger Pau Monné 		txq->xennet_tag[i].dma_map = NULL;
842dabb3db7SRoger Pau Monné 	}
84396375eacSRoger Pau Monné }
84496375eacSRoger Pau Monné 
84596375eacSRoger Pau Monné static void
84696375eacSRoger Pau Monné destroy_txqs(struct netfront_info *np)
84796375eacSRoger Pau Monné {
84896375eacSRoger Pau Monné 	int i;
84996375eacSRoger Pau Monné 
85096375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++)
85196375eacSRoger Pau Monné 		destroy_txq(&np->txq[i]);
85296375eacSRoger Pau Monné 
85396375eacSRoger Pau Monné 	free(np->txq, M_DEVBUF);
85496375eacSRoger Pau Monné 	np->txq = NULL;
85596375eacSRoger Pau Monné }
85696375eacSRoger Pau Monné 
85796375eacSRoger Pau Monné static int
85896375eacSRoger Pau Monné setup_txqs(device_t dev, struct netfront_info *info,
85996375eacSRoger Pau Monné 	   unsigned long num_queues)
86096375eacSRoger Pau Monné {
86196375eacSRoger Pau Monné 	int q, i;
86296375eacSRoger Pau Monné 	int error;
86396375eacSRoger Pau Monné 	netif_tx_sring_t *txs;
86496375eacSRoger Pau Monné 	struct netfront_txq *txq;
86596375eacSRoger Pau Monné 
86696375eacSRoger Pau Monné 	info->txq = malloc(sizeof(struct netfront_txq) * num_queues,
86796375eacSRoger Pau Monné 	    M_DEVBUF, M_WAITOK|M_ZERO);
86896375eacSRoger Pau Monné 
86996375eacSRoger Pau Monné 	for (q = 0; q < num_queues; q++) {
87096375eacSRoger Pau Monné 		txq = &info->txq[q];
87196375eacSRoger Pau Monné 
87296375eacSRoger Pau Monné 		txq->id = q;
87396375eacSRoger Pau Monné 		txq->info = info;
87496375eacSRoger Pau Monné 
87596375eacSRoger Pau Monné 		txq->ring_ref = GRANT_REF_INVALID;
87696375eacSRoger Pau Monné 		txq->ring.sring = NULL;
87796375eacSRoger Pau Monné 
87896375eacSRoger Pau Monné 		snprintf(txq->name, XN_QUEUE_NAME_LEN, "xntx_%u", q);
87996375eacSRoger Pau Monné 
88096375eacSRoger Pau Monné 		mtx_init(&txq->lock, txq->name, "netfront transmit lock",
88196375eacSRoger Pau Monné 		    MTX_DEF);
882dabb3db7SRoger Pau Monné 		SLIST_INIT(&txq->tags);
88396375eacSRoger Pau Monné 
88496375eacSRoger Pau Monné 		for (i = 0; i <= NET_TX_RING_SIZE; i++) {
88596375eacSRoger Pau Monné 			txq->mbufs[i] = (void *) ((u_long) i+1);
88696375eacSRoger Pau Monné 			txq->grant_ref[i] = GRANT_REF_INVALID;
887dabb3db7SRoger Pau Monné 			txq->xennet_tag[i].txq = txq;
888dabb3db7SRoger Pau Monné 			txq->xennet_tag[i].dma_tag = info->dma_tag;
889dabb3db7SRoger Pau Monné 			error = bus_dmamap_create(info->dma_tag, 0,
890dabb3db7SRoger Pau Monné 			    &txq->xennet_tag[i].dma_map);
891dabb3db7SRoger Pau Monné 			if (error != 0) {
892dabb3db7SRoger Pau Monné 				device_printf(dev,
893dabb3db7SRoger Pau Monné 				    "failed to allocate dma map\n");
894dabb3db7SRoger Pau Monné 				goto fail;
895dabb3db7SRoger Pau Monné 			}
896dabb3db7SRoger Pau Monné 			m_tag_setup(&txq->xennet_tag[i].tag,
897dabb3db7SRoger Pau Monné 			    MTAG_COOKIE, MTAG_XENNET,
898dabb3db7SRoger Pau Monné 			    sizeof(txq->xennet_tag[i]) -
899dabb3db7SRoger Pau Monné 			    sizeof(txq->xennet_tag[i].tag));
900dabb3db7SRoger Pau Monné 			txq->xennet_tag[i].tag.m_tag_free = &tag_free;
901dabb3db7SRoger Pau Monné 			SLIST_INSERT_HEAD(&txq->tags, &txq->xennet_tag[i],
902dabb3db7SRoger Pau Monné 			    next);
90396375eacSRoger Pau Monné 		}
90496375eacSRoger Pau Monné 		txq->mbufs[NET_TX_RING_SIZE] = (void *)0;
90596375eacSRoger Pau Monné 
90696375eacSRoger Pau Monné 		/* Start resources allocation. */
90796375eacSRoger Pau Monné 
90896375eacSRoger Pau Monné 		if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
90996375eacSRoger Pau Monné 		    &txq->gref_head) != 0) {
91096375eacSRoger Pau Monné 			device_printf(dev, "failed to allocate tx grant refs\n");
91196375eacSRoger Pau Monné 			error = ENOMEM;
91296375eacSRoger Pau Monné 			goto fail;
91396375eacSRoger Pau Monné 		}
91496375eacSRoger Pau Monné 
91596375eacSRoger Pau Monné 		txs = (netif_tx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF,
91696375eacSRoger Pau Monné 		    M_WAITOK|M_ZERO);
91796375eacSRoger Pau Monné 		SHARED_RING_INIT(txs);
91896375eacSRoger Pau Monné 		FRONT_RING_INIT(&txq->ring, txs, PAGE_SIZE);
91996375eacSRoger Pau Monné 
92096375eacSRoger Pau Monné 		error = xenbus_grant_ring(dev, virt_to_mfn(txs),
92196375eacSRoger Pau Monné 		    &txq->ring_ref);
92296375eacSRoger Pau Monné 		if (error != 0) {
92396375eacSRoger Pau Monné 			device_printf(dev, "failed to grant tx ring\n");
92496375eacSRoger Pau Monné 			goto fail_grant_ring;
92596375eacSRoger Pau Monné 		}
92696375eacSRoger Pau Monné 
92796375eacSRoger Pau Monné 		txq->br = buf_ring_alloc(NET_TX_RING_SIZE, M_DEVBUF,
92896375eacSRoger Pau Monné 		    M_WAITOK, &txq->lock);
92996375eacSRoger Pau Monné 		TASK_INIT(&txq->defrtask, 0, xn_txq_tq_deferred, txq);
93096375eacSRoger Pau Monné 
931da695b05SRoger Pau Monné 		txq->tq = taskqueue_create(txq->name, M_WAITOK,
93296375eacSRoger Pau Monné 		    taskqueue_thread_enqueue, &txq->tq);
93396375eacSRoger Pau Monné 
93496375eacSRoger Pau Monné 		error = taskqueue_start_threads(&txq->tq, 1, PI_NET,
93596375eacSRoger Pau Monné 		    "%s txq %d", device_get_nameunit(dev), txq->id);
93696375eacSRoger Pau Monné 		if (error != 0) {
93796375eacSRoger Pau Monné 			device_printf(dev, "failed to start tx taskq %d\n",
93896375eacSRoger Pau Monné 			    txq->id);
93996375eacSRoger Pau Monné 			goto fail_start_thread;
94096375eacSRoger Pau Monné 		}
94196375eacSRoger Pau Monné 
94296375eacSRoger Pau Monné 		error = xen_intr_alloc_and_bind_local_port(dev,
943da695b05SRoger Pau Monné 		    xenbus_get_otherend_id(dev), /* filter */ NULL, xn_intr,
944da695b05SRoger Pau Monné 		    &info->txq[q], INTR_TYPE_NET | INTR_MPSAFE | INTR_ENTROPY,
94596375eacSRoger Pau Monné 		    &txq->xen_intr_handle);
94696375eacSRoger Pau Monné 
94796375eacSRoger Pau Monné 		if (error != 0) {
94896375eacSRoger Pau Monné 			device_printf(dev, "xen_intr_alloc_and_bind_local_port failed\n");
94996375eacSRoger Pau Monné 			goto fail_bind_port;
95096375eacSRoger Pau Monné 		}
95196375eacSRoger Pau Monné 	}
95296375eacSRoger Pau Monné 
95396375eacSRoger Pau Monné 	return (0);
95496375eacSRoger Pau Monné 
95596375eacSRoger Pau Monné fail_bind_port:
95696375eacSRoger Pau Monné 	taskqueue_drain_all(txq->tq);
95796375eacSRoger Pau Monné fail_start_thread:
95896375eacSRoger Pau Monné 	buf_ring_free(txq->br, M_DEVBUF);
95996375eacSRoger Pau Monné 	taskqueue_free(txq->tq);
960d039b070SRoger Pau Monné 	gnttab_end_foreign_access(txq->ring_ref, NULL);
96196375eacSRoger Pau Monné fail_grant_ring:
96296375eacSRoger Pau Monné 	gnttab_free_grant_references(txq->gref_head);
96396375eacSRoger Pau Monné 	free(txq->ring.sring, M_DEVBUF);
96496375eacSRoger Pau Monné fail:
96596375eacSRoger Pau Monné 	for (; q >= 0; q--) {
96696375eacSRoger Pau Monné 		disconnect_txq(&info->txq[q]);
96796375eacSRoger Pau Monné 		destroy_txq(&info->txq[q]);
96896375eacSRoger Pau Monné 	}
96996375eacSRoger Pau Monné 
97096375eacSRoger Pau Monné 	free(info->txq, M_DEVBUF);
97196375eacSRoger Pau Monné 	return (error);
97296375eacSRoger Pau Monné }
97396375eacSRoger Pau Monné 
97496375eacSRoger Pau Monné static int
97596375eacSRoger Pau Monné setup_device(device_t dev, struct netfront_info *info,
97696375eacSRoger Pau Monné     unsigned long num_queues)
97796375eacSRoger Pau Monné {
97896375eacSRoger Pau Monné 	int error;
97996375eacSRoger Pau Monné 	int q;
98096375eacSRoger Pau Monné 
98196375eacSRoger Pau Monné 	if (info->txq)
98296375eacSRoger Pau Monné 		destroy_txqs(info);
98396375eacSRoger Pau Monné 
98496375eacSRoger Pau Monné 	if (info->rxq)
98596375eacSRoger Pau Monné 		destroy_rxqs(info);
98696375eacSRoger Pau Monné 
98796375eacSRoger Pau Monné 	info->num_queues = 0;
98896375eacSRoger Pau Monné 
98996375eacSRoger Pau Monné 	error = setup_rxqs(dev, info, num_queues);
99096375eacSRoger Pau Monné 	if (error != 0)
99196375eacSRoger Pau Monné 		goto out;
99296375eacSRoger Pau Monné 	error = setup_txqs(dev, info, num_queues);
99396375eacSRoger Pau Monné 	if (error != 0)
99496375eacSRoger Pau Monné 		goto out;
99596375eacSRoger Pau Monné 
99696375eacSRoger Pau Monné 	info->num_queues = num_queues;
99796375eacSRoger Pau Monné 
99896375eacSRoger Pau Monné 	/* No split event channel at the moment. */
99996375eacSRoger Pau Monné 	for (q = 0; q < num_queues; q++)
100096375eacSRoger Pau Monné 		info->rxq[q].xen_intr_handle = info->txq[q].xen_intr_handle;
100196375eacSRoger Pau Monné 
100296375eacSRoger Pau Monné 	return (0);
100396375eacSRoger Pau Monné 
100496375eacSRoger Pau Monné out:
100596375eacSRoger Pau Monné 	KASSERT(error != 0, ("Error path taken without providing an error code"));
10063a6d1fcfSKip Macy 	return (error);
100789e0f4d2SKip Macy }
100889e0f4d2SKip Macy 
1009a0ae8f04SBjoern A. Zeeb #ifdef INET
101002f3b17fSJustin Hibbits static u_int
101102f3b17fSJustin Hibbits netfront_addr_cb(void *arg, struct ifaddr *a, u_int count)
101202f3b17fSJustin Hibbits {
101302f3b17fSJustin Hibbits 	arp_ifinit((if_t)arg, a);
101402f3b17fSJustin Hibbits 	return (1);
101502f3b17fSJustin Hibbits }
101689e0f4d2SKip Macy /**
101712678024SDoug Rabson  * If this interface has an ipv4 address, send an arp for it. This
101812678024SDoug Rabson  * helps to get the network going again after migrating hosts.
101912678024SDoug Rabson  */
102012678024SDoug Rabson static void
102112678024SDoug Rabson netfront_send_fake_arp(device_t dev, struct netfront_info *info)
102212678024SDoug Rabson {
102302f3b17fSJustin Hibbits 	if_t ifp;
102412678024SDoug Rabson 
102512678024SDoug Rabson 	ifp = info->xn_ifp;
102602f3b17fSJustin Hibbits 	if_foreach_addr_type(ifp, AF_INET, netfront_addr_cb, ifp);
102712678024SDoug Rabson }
1028a0ae8f04SBjoern A. Zeeb #endif
102912678024SDoug Rabson 
103012678024SDoug Rabson /**
103189e0f4d2SKip Macy  * Callback received when the backend's state changes.
103289e0f4d2SKip Macy  */
1033283d6f72SJustin T. Gibbs static void
103423dc5621SKip Macy netfront_backend_changed(device_t dev, XenbusState newstate)
103589e0f4d2SKip Macy {
103623dc5621SKip Macy 	struct netfront_info *sc = device_get_softc(dev);
103789e0f4d2SKip Macy 
103823dc5621SKip Macy 	DPRINTK("newstate=%d\n", newstate);
103989e0f4d2SKip Macy 
104002f3b17fSJustin Hibbits 	CURVNET_SET(if_getvnet(sc->xn_ifp));
1041903eaa68SKristof Provost 
104223dc5621SKip Macy 	switch (newstate) {
104389e0f4d2SKip Macy 	case XenbusStateInitialising:
104489e0f4d2SKip Macy 	case XenbusStateInitialised:
104589e0f4d2SKip Macy 	case XenbusStateUnknown:
1046920ba15bSKip Macy 	case XenbusStateReconfigured:
1047920ba15bSKip Macy 	case XenbusStateReconfiguring:
104889e0f4d2SKip Macy 		break;
104989e0f4d2SKip Macy 	case XenbusStateInitWait:
105023dc5621SKip Macy 		if (xenbus_get_state(dev) != XenbusStateInitialising)
105189e0f4d2SKip Macy 			break;
105296375eacSRoger Pau Monné 		if (xn_connect(sc) != 0)
105389e0f4d2SKip Macy 			break;
105465671253SRoger Pau Monné 		/* Switch to connected state before kicking the rings. */
105565671253SRoger Pau Monné 		xenbus_set_state(sc->xbdev, XenbusStateConnected);
105665671253SRoger Pau Monné 		xn_kick_rings(sc);
105723dc5621SKip Macy 		break;
105889e0f4d2SKip Macy 	case XenbusStateClosing:
105923dc5621SKip Macy 		xenbus_set_state(dev, XenbusStateClosed);
106089e0f4d2SKip Macy 		break;
1061c2d12e5eSRoger Pau Monné 	case XenbusStateClosed:
1062c2d12e5eSRoger Pau Monné 		if (sc->xn_reset) {
1063c2d12e5eSRoger Pau Monné 			netif_disconnect_backend(sc);
1064c2d12e5eSRoger Pau Monné 			xenbus_set_state(dev, XenbusStateInitialising);
1065c2d12e5eSRoger Pau Monné 			sc->xn_reset = false;
1066c2d12e5eSRoger Pau Monné 		}
1067c2d12e5eSRoger Pau Monné 		break;
1068dbf82bdeSRoger Pau Monné 	case XenbusStateConnected:
1069dbf82bdeSRoger Pau Monné #ifdef INET
1070dbf82bdeSRoger Pau Monné 		netfront_send_fake_arp(dev, sc);
1071dbf82bdeSRoger Pau Monné #endif
1072dbf82bdeSRoger Pau Monné 		break;
107389e0f4d2SKip Macy 	}
1074903eaa68SKristof Provost 
1075903eaa68SKristof Provost 	CURVNET_RESTORE();
107689e0f4d2SKip Macy }
107789e0f4d2SKip Macy 
1078931eeffaSKenneth D. Merry /**
1079931eeffaSKenneth D. Merry  * \brief Verify that there is sufficient space in the Tx ring
1080931eeffaSKenneth D. Merry  *        buffer for a maximally sized request to be enqueued.
1081c099cafaSAdrian Chadd  *
1082931eeffaSKenneth D. Merry  * A transmit request requires a transmit descriptor for each packet
1083931eeffaSKenneth D. Merry  * fragment, plus up to 2 entries for "options" (e.g. TSO).
1084c099cafaSAdrian Chadd  */
108589e0f4d2SKip Macy static inline int
108696375eacSRoger Pau Monné xn_tx_slot_available(struct netfront_txq *txq)
108789e0f4d2SKip Macy {
108896375eacSRoger Pau Monné 
108996375eacSRoger Pau Monné 	return (RING_FREE_REQUESTS(&txq->ring) > (MAX_TX_REQ_FRAGS + 2));
109089e0f4d2SKip Macy }
1091931eeffaSKenneth D. Merry 
109289e0f4d2SKip Macy static void
109396375eacSRoger Pau Monné xn_release_tx_bufs(struct netfront_txq *txq)
109489e0f4d2SKip Macy {
109589e0f4d2SKip Macy 	int i;
109689e0f4d2SKip Macy 
109789e0f4d2SKip Macy 	for (i = 1; i <= NET_TX_RING_SIZE; i++) {
1098931eeffaSKenneth D. Merry 		struct mbuf *m;
109989e0f4d2SKip Macy 
110096375eacSRoger Pau Monné 		m = txq->mbufs[i];
1101931eeffaSKenneth D. Merry 
1102931eeffaSKenneth D. Merry 		/*
1103931eeffaSKenneth D. Merry 		 * We assume that no kernel addresses are
1104931eeffaSKenneth D. Merry 		 * less than NET_TX_RING_SIZE.  Any entry
1105931eeffaSKenneth D. Merry 		 * in the table that is below this number
1106931eeffaSKenneth D. Merry 		 * must be an index from free-list tracking.
1107931eeffaSKenneth D. Merry 		 */
1108931eeffaSKenneth D. Merry 		if (((uintptr_t)m) <= NET_TX_RING_SIZE)
110989e0f4d2SKip Macy 			continue;
111096375eacSRoger Pau Monné 		gnttab_end_foreign_access_ref(txq->grant_ref[i]);
111196375eacSRoger Pau Monné 		gnttab_release_grant_reference(&txq->gref_head,
111296375eacSRoger Pau Monné 		    txq->grant_ref[i]);
111396375eacSRoger Pau Monné 		txq->grant_ref[i] = GRANT_REF_INVALID;
111496375eacSRoger Pau Monné 		add_id_to_freelist(txq->mbufs, i);
111596375eacSRoger Pau Monné 		txq->mbufs_cnt--;
111696375eacSRoger Pau Monné 		if (txq->mbufs_cnt < 0) {
11176f9767acSMarius Strobl 			panic("%s: tx_chain_cnt must be >= 0", __func__);
1118a4ec37f5SAdrian Chadd 		}
1119dabb3db7SRoger Pau Monné 		mbuf_release(m);
112089e0f4d2SKip Macy 	}
112189e0f4d2SKip Macy }
112289e0f4d2SKip Macy 
11232568ee67SRoger Pau Monné static struct mbuf *
11242568ee67SRoger Pau Monné xn_alloc_one_rx_buffer(struct netfront_rxq *rxq)
11252568ee67SRoger Pau Monné {
11262568ee67SRoger Pau Monné 	struct mbuf *m;
11272568ee67SRoger Pau Monné 
11282568ee67SRoger Pau Monné 	m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
11292568ee67SRoger Pau Monné 	if (m == NULL)
11302568ee67SRoger Pau Monné 		return NULL;
11312568ee67SRoger Pau Monné 	m->m_len = m->m_pkthdr.len = MJUMPAGESIZE;
11322568ee67SRoger Pau Monné 
11332568ee67SRoger Pau Monné 	return (m);
11342568ee67SRoger Pau Monné }
11352568ee67SRoger Pau Monné 
113689e0f4d2SKip Macy static void
113796375eacSRoger Pau Monné xn_alloc_rx_buffers(struct netfront_rxq *rxq)
113889e0f4d2SKip Macy {
113989e0f4d2SKip Macy 	RING_IDX req_prod;
11402568ee67SRoger Pau Monné 	int notify;
11412568ee67SRoger Pau Monné 
11422568ee67SRoger Pau Monné 	XN_RX_LOCK_ASSERT(rxq);
11432568ee67SRoger Pau Monné 
11442568ee67SRoger Pau Monné 	if (__predict_false(rxq->info->carrier == 0))
11452568ee67SRoger Pau Monné 		return;
11462568ee67SRoger Pau Monné 
11472568ee67SRoger Pau Monné 	for (req_prod = rxq->ring.req_prod_pvt;
11482568ee67SRoger Pau Monné 	     req_prod - rxq->ring.rsp_cons < NET_RX_RING_SIZE;
11492568ee67SRoger Pau Monné 	     req_prod++) {
11502568ee67SRoger Pau Monné 		struct mbuf *m;
11512568ee67SRoger Pau Monné 		unsigned short id;
115289e0f4d2SKip Macy 		grant_ref_t ref;
11532568ee67SRoger Pau Monné 		struct netif_rx_request *req;
11542568ee67SRoger Pau Monné 		unsigned long pfn;
115589e0f4d2SKip Macy 
11562568ee67SRoger Pau Monné 		m = xn_alloc_one_rx_buffer(rxq);
11572568ee67SRoger Pau Monné 		if (m == NULL)
115889e0f4d2SKip Macy 			break;
115989e0f4d2SKip Macy 
11602568ee67SRoger Pau Monné 		id = xn_rxidx(req_prod);
116189e0f4d2SKip Macy 
116296375eacSRoger Pau Monné 		KASSERT(rxq->mbufs[id] == NULL, ("non-NULL xn_rx_chain"));
11632568ee67SRoger Pau Monné 		rxq->mbufs[id] = m;
116489e0f4d2SKip Macy 
116596375eacSRoger Pau Monné 		ref = gnttab_claim_grant_reference(&rxq->gref_head);
1166ff662b5cSJustin T. Gibbs 		KASSERT(ref != GNTTAB_LIST_END,
1167ff662b5cSJustin T. Gibbs 		    ("reserved grant references exhuasted"));
116896375eacSRoger Pau Monné 		rxq->grant_ref[id] = ref;
116989e0f4d2SKip Macy 
11702568ee67SRoger Pau Monné 		pfn = atop(vtophys(mtod(m, vm_offset_t)));
11712568ee67SRoger Pau Monné 		req = RING_GET_REQUEST(&rxq->ring, req_prod);
117289e0f4d2SKip Macy 
11732568ee67SRoger Pau Monné 		gnttab_grant_foreign_access_ref(ref,
11742568ee67SRoger Pau Monné 		    xenbus_get_otherend_id(rxq->info->xbdev), pfn, 0);
117589e0f4d2SKip Macy 		req->id = id;
117689e0f4d2SKip Macy 		req->gref = ref;
117789e0f4d2SKip Macy 	}
117889e0f4d2SKip Macy 
11792568ee67SRoger Pau Monné 	rxq->ring.req_prod_pvt = req_prod;
118089e0f4d2SKip Macy 
11812568ee67SRoger Pau Monné 	/* Not enough requests? Try again later. */
11822568ee67SRoger Pau Monné 	if (req_prod - rxq->ring.rsp_cons < NET_RX_SLOTS_MIN) {
1183bf7b50dbSRoger Pau Monné 		callout_reset_curcpu(&rxq->rx_refill, hz/10,
1184bf7b50dbSRoger Pau Monné 		    xn_alloc_rx_buffers_callout, rxq);
11852568ee67SRoger Pau Monné 		return;
11862568ee67SRoger Pau Monné 	}
11872568ee67SRoger Pau Monné 
11882568ee67SRoger Pau Monné 	wmb();		/* barrier so backend seens requests */
11892568ee67SRoger Pau Monné 
119096375eacSRoger Pau Monné 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rxq->ring, notify);
119189e0f4d2SKip Macy 	if (notify)
119296375eacSRoger Pau Monné 		xen_intr_signal(rxq->xen_intr_handle);
119389e0f4d2SKip Macy }
119489e0f4d2SKip Macy 
11952568ee67SRoger Pau Monné static void xn_alloc_rx_buffers_callout(void *arg)
11962568ee67SRoger Pau Monné {
11972568ee67SRoger Pau Monné 	struct netfront_rxq *rxq;
11982568ee67SRoger Pau Monné 
11992568ee67SRoger Pau Monné 	rxq = (struct netfront_rxq *)arg;
12002568ee67SRoger Pau Monné 	XN_RX_LOCK(rxq);
12012568ee67SRoger Pau Monné 	xn_alloc_rx_buffers(rxq);
12022568ee67SRoger Pau Monné 	XN_RX_UNLOCK(rxq);
12032568ee67SRoger Pau Monné }
12042568ee67SRoger Pau Monné 
120589e0f4d2SKip Macy static void
120696375eacSRoger Pau Monné xn_release_rx_bufs(struct netfront_rxq *rxq)
120796375eacSRoger Pau Monné {
120896375eacSRoger Pau Monné 	int i,  ref;
120996375eacSRoger Pau Monné 	struct mbuf *m;
121096375eacSRoger Pau Monné 
121196375eacSRoger Pau Monné 	for (i = 0; i < NET_RX_RING_SIZE; i++) {
121296375eacSRoger Pau Monné 		m = rxq->mbufs[i];
121396375eacSRoger Pau Monné 
121496375eacSRoger Pau Monné 		if (m == NULL)
121596375eacSRoger Pau Monné 			continue;
121696375eacSRoger Pau Monné 
121796375eacSRoger Pau Monné 		ref = rxq->grant_ref[i];
121896375eacSRoger Pau Monné 		if (ref == GRANT_REF_INVALID)
121996375eacSRoger Pau Monné 			continue;
122096375eacSRoger Pau Monné 
122196375eacSRoger Pau Monné 		gnttab_end_foreign_access_ref(ref);
122296375eacSRoger Pau Monné 		gnttab_release_grant_reference(&rxq->gref_head, ref);
122396375eacSRoger Pau Monné 		rxq->mbufs[i] = NULL;
122496375eacSRoger Pau Monné 		rxq->grant_ref[i] = GRANT_REF_INVALID;
122596375eacSRoger Pau Monné 		m_freem(m);
122696375eacSRoger Pau Monné 	}
122796375eacSRoger Pau Monné }
122896375eacSRoger Pau Monné 
122996375eacSRoger Pau Monné static void
123096375eacSRoger Pau Monné xn_rxeof(struct netfront_rxq *rxq)
123189e0f4d2SKip Macy {
123202f3b17fSJustin Hibbits 	if_t ifp;
123396375eacSRoger Pau Monné 	struct netfront_info *np = rxq->info;
12343778878dSRoger Pau Monné #if (defined(INET) || defined(INET6))
123596375eacSRoger Pau Monné 	struct lro_ctrl *lro = &rxq->lro;
12363778878dSRoger Pau Monné #endif
123789e0f4d2SKip Macy 	struct netfront_rx_info rinfo;
123889e0f4d2SKip Macy 	struct netif_rx_response *rx = &rinfo.rx;
123989e0f4d2SKip Macy 	struct netif_extra_info *extras = rinfo.extras;
124089e0f4d2SKip Macy 	RING_IDX i, rp;
124189e0f4d2SKip Macy 	struct mbuf *m;
124296375eacSRoger Pau Monné 	struct mbufq mbufq_rxq, mbufq_errq;
1243d0f3a8b9SRoger Pau Monné 	int err, work_to_do;
124489e0f4d2SKip Macy 
124596375eacSRoger Pau Monné 	XN_RX_LOCK_ASSERT(rxq);
1246bf319173SRoger Pau Monné 
124789e0f4d2SKip Macy 	if (!netfront_carrier_ok(np))
124889e0f4d2SKip Macy 		return;
124989e0f4d2SKip Macy 
1250c578b6acSGleb Smirnoff 	/* XXX: there should be some sane limit. */
125196375eacSRoger Pau Monné 	mbufq_init(&mbufq_errq, INT_MAX);
125296375eacSRoger Pau Monné 	mbufq_init(&mbufq_rxq, INT_MAX);
125389e0f4d2SKip Macy 
125489e0f4d2SKip Macy 	ifp = np->xn_ifp;
125589e0f4d2SKip Macy 
1256bf319173SRoger Pau Monné 	do {
125796375eacSRoger Pau Monné 		rp = rxq->ring.sring->rsp_prod;
125889e0f4d2SKip Macy 		rmb();	/* Ensure we see queued responses up to 'rp'. */
125989e0f4d2SKip Macy 
126096375eacSRoger Pau Monné 		i = rxq->ring.rsp_cons;
126189e0f4d2SKip Macy 		while ((i != rp)) {
126296375eacSRoger Pau Monné 			memcpy(rx, RING_GET_RESPONSE(&rxq->ring, i), sizeof(*rx));
126389e0f4d2SKip Macy 			memset(extras, 0, sizeof(rinfo.extras));
126489e0f4d2SKip Macy 
126583b92f6eSKip Macy 			m = NULL;
126696375eacSRoger Pau Monné 			err = xn_get_responses(rxq, &rinfo, rp, &i, &m);
126789e0f4d2SKip Macy 
126876acc41fSJustin T. Gibbs 			if (__predict_false(err)) {
126983b92f6eSKip Macy 				if (m)
127096375eacSRoger Pau Monné 					(void )mbufq_enqueue(&mbufq_errq, m);
1271b2fd6999SRoger Pau Monné 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
127289e0f4d2SKip Macy 				continue;
127389e0f4d2SKip Macy 			}
127489e0f4d2SKip Macy 
127589e0f4d2SKip Macy 			m->m_pkthdr.rcvif = ifp;
127689e0f4d2SKip Macy 			if (rx->flags & NETRXF_data_validated) {
127789e0f4d2SKip Macy 				/*
1278a81683c3SRoger Pau Monné 				 * According to mbuf(9) the correct way to tell
1279a81683c3SRoger Pau Monné 				 * the stack that the checksum of an inbound
1280a81683c3SRoger Pau Monné 				 * packet is correct, without it actually being
1281a81683c3SRoger Pau Monné 				 * present (because the underlying interface
1282a81683c3SRoger Pau Monné 				 * doesn't provide it), is to set the
1283a81683c3SRoger Pau Monné 				 * CSUM_DATA_VALID and CSUM_PSEUDO_HDR flags,
1284a81683c3SRoger Pau Monné 				 * and the csum_data field to 0xffff.
128589e0f4d2SKip Macy 				 */
1286a81683c3SRoger Pau Monné 				m->m_pkthdr.csum_flags |= (CSUM_DATA_VALID
128789e0f4d2SKip Macy 				    | CSUM_PSEUDO_HDR);
128889e0f4d2SKip Macy 				m->m_pkthdr.csum_data = 0xffff;
128989e0f4d2SKip Macy 			}
1290d9a66b6dSRoger Pau Monné 			if ((rx->flags & NETRXF_extra_info) != 0 &&
1291d9a66b6dSRoger Pau Monné 			    (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type ==
1292d9a66b6dSRoger Pau Monné 			    XEN_NETIF_EXTRA_TYPE_GSO)) {
1293d9a66b6dSRoger Pau Monné 				m->m_pkthdr.tso_segsz =
1294d9a66b6dSRoger Pau Monné 				extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].u.gso.size;
1295d9a66b6dSRoger Pau Monné 				m->m_pkthdr.csum_flags |= CSUM_TSO;
1296d9a66b6dSRoger Pau Monné 			}
129789e0f4d2SKip Macy 
129896375eacSRoger Pau Monné 			(void )mbufq_enqueue(&mbufq_rxq, m);
129989e0f4d2SKip Macy 		}
130089e0f4d2SKip Macy 
1301bf319173SRoger Pau Monné 		rxq->ring.rsp_cons = i;
130289e0f4d2SKip Macy 
1303bf319173SRoger Pau Monné 		xn_alloc_rx_buffers(rxq);
1304bf319173SRoger Pau Monné 
1305bf319173SRoger Pau Monné 		RING_FINAL_CHECK_FOR_RESPONSES(&rxq->ring, work_to_do);
1306bf319173SRoger Pau Monné 	} while (work_to_do);
1307bf319173SRoger Pau Monné 
1308bf319173SRoger Pau Monné 	mbufq_drain(&mbufq_errq);
130989e0f4d2SKip Macy 	/*
131089e0f4d2SKip Macy 	 * Process all the mbufs after the remapping is complete.
131189e0f4d2SKip Macy 	 * Break the mbuf chain first though.
131289e0f4d2SKip Macy 	 */
131396375eacSRoger Pau Monné 	while ((m = mbufq_dequeue(&mbufq_rxq)) != NULL) {
1314c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
131508c9c2e0SRoger Pau Monné #if (defined(INET) || defined(INET6))
131612678024SDoug Rabson 		/* Use LRO if possible */
131702f3b17fSJustin Hibbits 		if ((if_getcapenable(ifp) & IFCAP_LRO) == 0 ||
131812678024SDoug Rabson 		    lro->lro_cnt == 0 || tcp_lro_rx(lro, m, 0)) {
131912678024SDoug Rabson 			/*
132012678024SDoug Rabson 			 * If LRO fails, pass up to the stack
132112678024SDoug Rabson 			 * directly.
132212678024SDoug Rabson 			 */
132302f3b17fSJustin Hibbits 			if_input(ifp, m);
132412678024SDoug Rabson 		}
132512678024SDoug Rabson #else
132602f3b17fSJustin Hibbits 		if_input(ifp, m);
132712678024SDoug Rabson #endif
132889e0f4d2SKip Macy 	}
132989e0f4d2SKip Macy 
133008c9c2e0SRoger Pau Monné #if (defined(INET) || defined(INET6))
133112678024SDoug Rabson 	/*
133212678024SDoug Rabson 	 * Flush any outstanding LRO work
133312678024SDoug Rabson 	 */
13346dd38b87SSepherosa Ziehau 	tcp_lro_flush_all(lro);
133512678024SDoug Rabson #endif
133689e0f4d2SKip Macy }
133789e0f4d2SKip Macy 
133889e0f4d2SKip Macy static void
133996375eacSRoger Pau Monné xn_txeof(struct netfront_txq *txq)
134089e0f4d2SKip Macy {
134189e0f4d2SKip Macy 	RING_IDX i, prod;
134289e0f4d2SKip Macy 	unsigned short id;
134302f3b17fSJustin Hibbits 	if_t ifp;
134412678024SDoug Rabson 	netif_tx_response_t *txr;
134589e0f4d2SKip Macy 	struct mbuf *m;
134696375eacSRoger Pau Monné 	struct netfront_info *np = txq->info;
134789e0f4d2SKip Macy 
134896375eacSRoger Pau Monné 	XN_TX_LOCK_ASSERT(txq);
134989e0f4d2SKip Macy 
135089e0f4d2SKip Macy 	if (!netfront_carrier_ok(np))
135189e0f4d2SKip Macy 		return;
135289e0f4d2SKip Macy 
135389e0f4d2SKip Macy 	ifp = np->xn_ifp;
135489e0f4d2SKip Macy 
135589e0f4d2SKip Macy 	do {
135696375eacSRoger Pau Monné 		prod = txq->ring.sring->rsp_prod;
135789e0f4d2SKip Macy 		rmb(); /* Ensure we see responses up to 'rp'. */
135889e0f4d2SKip Macy 
135996375eacSRoger Pau Monné 		for (i = txq->ring.rsp_cons; i != prod; i++) {
136096375eacSRoger Pau Monné 			txr = RING_GET_RESPONSE(&txq->ring, i);
136112678024SDoug Rabson 			if (txr->status == NETIF_RSP_NULL)
136212678024SDoug Rabson 				continue;
136312678024SDoug Rabson 
1364931eeffaSKenneth D. Merry 			if (txr->status != NETIF_RSP_OKAY) {
1365931eeffaSKenneth D. Merry 				printf("%s: WARNING: response is %d!\n",
1366931eeffaSKenneth D. Merry 				       __func__, txr->status);
1367931eeffaSKenneth D. Merry 			}
136812678024SDoug Rabson 			id = txr->id;
136996375eacSRoger Pau Monné 			m = txq->mbufs[id];
137096375eacSRoger Pau Monné 			KASSERT(m != NULL, ("mbuf not found in chain"));
1371931eeffaSKenneth D. Merry 			KASSERT((uintptr_t)m > NET_TX_RING_SIZE,
1372931eeffaSKenneth D. Merry 				("mbuf already on the free list, but we're "
1373931eeffaSKenneth D. Merry 				"trying to free it again!"));
13742d8fae98SAdrian Chadd 			M_ASSERTVALID(m);
137589e0f4d2SKip Macy 
137676acc41fSJustin T. Gibbs 			if (__predict_false(gnttab_query_foreign_access(
137796375eacSRoger Pau Monné 			    txq->grant_ref[id]) != 0)) {
13786f9767acSMarius Strobl 				panic("%s: grant id %u still in use by the "
13796f9767acSMarius Strobl 				    "backend", __func__, id);
138089e0f4d2SKip Macy 			}
138196375eacSRoger Pau Monné 			gnttab_end_foreign_access_ref(txq->grant_ref[id]);
138289e0f4d2SKip Macy 			gnttab_release_grant_reference(
138396375eacSRoger Pau Monné 				&txq->gref_head, txq->grant_ref[id]);
138496375eacSRoger Pau Monné 			txq->grant_ref[id] = GRANT_REF_INVALID;
138589e0f4d2SKip Macy 
138696375eacSRoger Pau Monné 			txq->mbufs[id] = NULL;
138796375eacSRoger Pau Monné 			add_id_to_freelist(txq->mbufs, id);
138896375eacSRoger Pau Monné 			txq->mbufs_cnt--;
1389dabb3db7SRoger Pau Monné 			mbuf_release(m);
139096375eacSRoger Pau Monné 			/* Only mark the txq active if we've freed up at least one slot to try */
139102f3b17fSJustin Hibbits 			if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
139289e0f4d2SKip Macy 		}
139396375eacSRoger Pau Monné 		txq->ring.rsp_cons = prod;
139489e0f4d2SKip Macy 
139589e0f4d2SKip Macy 		/*
139689e0f4d2SKip Macy 		 * Set a new event, then check for race with update of
139789e0f4d2SKip Macy 		 * tx_cons. Note that it is essential to schedule a
139889e0f4d2SKip Macy 		 * callback, no matter how few buffers are pending. Even if
139989e0f4d2SKip Macy 		 * there is space in the transmit ring, higher layers may
140089e0f4d2SKip Macy 		 * be blocked because too much data is outstanding: in such
140189e0f4d2SKip Macy 		 * cases notification from Xen is likely to be the only kick
140289e0f4d2SKip Macy 		 * that we'll get.
140389e0f4d2SKip Macy 		 */
140496375eacSRoger Pau Monné 		txq->ring.sring->rsp_event =
140596375eacSRoger Pau Monné 		    prod + ((txq->ring.sring->req_prod - prod) >> 1) + 1;
140689e0f4d2SKip Macy 
140789e0f4d2SKip Macy 		mb();
140896375eacSRoger Pau Monné 	} while (prod != txq->ring.sring->rsp_prod);
140989e0f4d2SKip Macy 
141096375eacSRoger Pau Monné 	if (txq->full &&
141196375eacSRoger Pau Monné 	    ((txq->ring.sring->req_prod - prod) < NET_TX_RING_SIZE)) {
141296375eacSRoger Pau Monné 		txq->full = false;
1413da695b05SRoger Pau Monné 		xn_txq_start(txq);
141489e0f4d2SKip Macy 	}
141589e0f4d2SKip Macy }
141689e0f4d2SKip Macy 
141789e0f4d2SKip Macy static void
141896375eacSRoger Pau Monné xn_intr(void *xsc)
141996375eacSRoger Pau Monné {
142096375eacSRoger Pau Monné 	struct netfront_txq *txq = xsc;
142196375eacSRoger Pau Monné 	struct netfront_info *np = txq->info;
142296375eacSRoger Pau Monné 	struct netfront_rxq *rxq = &np->rxq[txq->id];
142396375eacSRoger Pau Monné 
142496375eacSRoger Pau Monné 	/* kick both tx and rx */
142596375eacSRoger Pau Monné 	xn_rxq_intr(rxq);
142696375eacSRoger Pau Monné 	xn_txq_intr(txq);
142796375eacSRoger Pau Monné }
142896375eacSRoger Pau Monné 
142996375eacSRoger Pau Monné static void
143096375eacSRoger Pau Monné xn_move_rx_slot(struct netfront_rxq *rxq, struct mbuf *m,
143196375eacSRoger Pau Monné     grant_ref_t ref)
143296375eacSRoger Pau Monné {
143396375eacSRoger Pau Monné 	int new = xn_rxidx(rxq->ring.req_prod_pvt);
143496375eacSRoger Pau Monné 
143596375eacSRoger Pau Monné 	KASSERT(rxq->mbufs[new] == NULL, ("mbufs != NULL"));
143696375eacSRoger Pau Monné 	rxq->mbufs[new] = m;
143796375eacSRoger Pau Monné 	rxq->grant_ref[new] = ref;
143896375eacSRoger Pau Monné 	RING_GET_REQUEST(&rxq->ring, rxq->ring.req_prod_pvt)->id = new;
143996375eacSRoger Pau Monné 	RING_GET_REQUEST(&rxq->ring, rxq->ring.req_prod_pvt)->gref = ref;
144096375eacSRoger Pau Monné 	rxq->ring.req_prod_pvt++;
144196375eacSRoger Pau Monné }
144296375eacSRoger Pau Monné 
144396375eacSRoger Pau Monné static int
144496375eacSRoger Pau Monné xn_get_extras(struct netfront_rxq *rxq,
1445931eeffaSKenneth D. Merry     struct netif_extra_info *extras, RING_IDX rp, RING_IDX *cons)
144689e0f4d2SKip Macy {
144789e0f4d2SKip Macy 	struct netif_extra_info *extra;
144889e0f4d2SKip Macy 
144989e0f4d2SKip Macy 	int err = 0;
145089e0f4d2SKip Macy 
145189e0f4d2SKip Macy 	do {
145289e0f4d2SKip Macy 		struct mbuf *m;
145389e0f4d2SKip Macy 		grant_ref_t ref;
145489e0f4d2SKip Macy 
145576acc41fSJustin T. Gibbs 		if (__predict_false(*cons + 1 == rp)) {
1456931eeffaSKenneth D. Merry 			err = EINVAL;
145789e0f4d2SKip Macy 			break;
145889e0f4d2SKip Macy 		}
145989e0f4d2SKip Macy 
146089e0f4d2SKip Macy 		extra = (struct netif_extra_info *)
146196375eacSRoger Pau Monné 		RING_GET_RESPONSE(&rxq->ring, ++(*cons));
146289e0f4d2SKip Macy 
146376acc41fSJustin T. Gibbs 		if (__predict_false(!extra->type ||
146489e0f4d2SKip Macy 			extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1465931eeffaSKenneth D. Merry 			err = EINVAL;
146689e0f4d2SKip Macy 		} else {
146789e0f4d2SKip Macy 			memcpy(&extras[extra->type - 1], extra, sizeof(*extra));
146889e0f4d2SKip Macy 		}
146989e0f4d2SKip Macy 
147096375eacSRoger Pau Monné 		m = xn_get_rx_mbuf(rxq, *cons);
147196375eacSRoger Pau Monné 		ref = xn_get_rx_ref(rxq,  *cons);
147296375eacSRoger Pau Monné 		xn_move_rx_slot(rxq, m, ref);
147389e0f4d2SKip Macy 	} while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
147489e0f4d2SKip Macy 
147589e0f4d2SKip Macy 	return err;
147689e0f4d2SKip Macy }
147789e0f4d2SKip Macy 
147889e0f4d2SKip Macy static int
147996375eacSRoger Pau Monné xn_get_responses(struct netfront_rxq *rxq,
1480931eeffaSKenneth D. Merry     struct netfront_rx_info *rinfo, RING_IDX rp, RING_IDX *cons,
1481d0f3a8b9SRoger Pau Monné     struct mbuf  **list)
148289e0f4d2SKip Macy {
148389e0f4d2SKip Macy 	struct netif_rx_response *rx = &rinfo->rx;
148489e0f4d2SKip Macy 	struct netif_extra_info *extras = rinfo->extras;
148583b92f6eSKip Macy 	struct mbuf *m, *m0, *m_prev;
148696375eacSRoger Pau Monné 	grant_ref_t ref = xn_get_rx_ref(rxq, *cons);
148789e0f4d2SKip Macy 	int frags = 1;
148889e0f4d2SKip Macy 	int err = 0;
1489e7236a7dSMateusz Guzik 	u_long ret __diagused;
149089e0f4d2SKip Macy 
149196375eacSRoger Pau Monné 	m0 = m = m_prev = xn_get_rx_mbuf(rxq, *cons);
149283b92f6eSKip Macy 
149389e0f4d2SKip Macy 	if (rx->flags & NETRXF_extra_info) {
149496375eacSRoger Pau Monné 		err = xn_get_extras(rxq, extras, rp, cons);
149589e0f4d2SKip Macy 	}
149689e0f4d2SKip Macy 
149783b92f6eSKip Macy 	if (m0 != NULL) {
149883b92f6eSKip Macy 		m0->m_pkthdr.len = 0;
149983b92f6eSKip Macy 		m0->m_next = NULL;
150083b92f6eSKip Macy 	}
150183b92f6eSKip Macy 
150289e0f4d2SKip Macy 	for (;;) {
150383b92f6eSKip Macy #if 0
1504227ca257SKip Macy 		DPRINTK("rx->status=%hd rx->offset=%hu frags=%u\n",
150583b92f6eSKip Macy 			rx->status, rx->offset, frags);
150683b92f6eSKip Macy #endif
150776acc41fSJustin T. Gibbs 		if (__predict_false(rx->status < 0 ||
150889e0f4d2SKip Macy 			rx->offset + rx->status > PAGE_SIZE)) {
150996375eacSRoger Pau Monné 			xn_move_rx_slot(rxq, m, ref);
1510931eeffaSKenneth D. Merry 			if (m0 == m)
1511931eeffaSKenneth D. Merry 				m0 = NULL;
1512931eeffaSKenneth D. Merry 			m = NULL;
1513931eeffaSKenneth D. Merry 			err = EINVAL;
1514931eeffaSKenneth D. Merry 			goto next_skip_queue;
151589e0f4d2SKip Macy 		}
151689e0f4d2SKip Macy 
151789e0f4d2SKip Macy 		/*
151889e0f4d2SKip Macy 		 * This definitely indicates a bug, either in this driver or in
151989e0f4d2SKip Macy 		 * the backend driver. In future this should flag the bad
152089e0f4d2SKip Macy 		 * situation to the system controller to reboot the backed.
152189e0f4d2SKip Macy 		 */
1522ff662b5cSJustin T. Gibbs 		if (ref == GRANT_REF_INVALID) {
1523ff662b5cSJustin T. Gibbs 			printf("%s: Bad rx response id %d.\n", __func__, rx->id);
1524931eeffaSKenneth D. Merry 			err = EINVAL;
152589e0f4d2SKip Macy 			goto next;
152689e0f4d2SKip Macy 		}
152789e0f4d2SKip Macy 
1528920ba15bSKip Macy 		ret = gnttab_end_foreign_access_ref(ref);
1529d0f3a8b9SRoger Pau Monné 		KASSERT(ret, ("Unable to end access to grant references"));
153089e0f4d2SKip Macy 
153196375eacSRoger Pau Monné 		gnttab_release_grant_reference(&rxq->gref_head, ref);
153289e0f4d2SKip Macy 
153389e0f4d2SKip Macy next:
15343a539122SAdrian Chadd 		if (m == NULL)
15353a539122SAdrian Chadd 			break;
15363a539122SAdrian Chadd 
153783b92f6eSKip Macy 		m->m_len = rx->status;
153883b92f6eSKip Macy 		m->m_data += rx->offset;
153983b92f6eSKip Macy 		m0->m_pkthdr.len += rx->status;
154083b92f6eSKip Macy 
1541931eeffaSKenneth D. Merry next_skip_queue:
154289e0f4d2SKip Macy 		if (!(rx->flags & NETRXF_more_data))
154389e0f4d2SKip Macy 			break;
154489e0f4d2SKip Macy 
1545931eeffaSKenneth D. Merry 		if (*cons + frags == rp) {
154689e0f4d2SKip Macy 			if (net_ratelimit())
154789e0f4d2SKip Macy 				WPRINTK("Need more frags\n");
1548931eeffaSKenneth D. Merry 			err = ENOENT;
1549931eeffaSKenneth D. Merry 			printf("%s: cons %u frags %u rp %u, not enough frags\n",
1550931eeffaSKenneth D. Merry 			       __func__, *cons, frags, rp);
155189e0f4d2SKip Macy 			break;
155289e0f4d2SKip Macy 		}
1553931eeffaSKenneth D. Merry 		/*
1554931eeffaSKenneth D. Merry 		 * Note that m can be NULL, if rx->status < 0 or if
1555931eeffaSKenneth D. Merry 		 * rx->offset + rx->status > PAGE_SIZE above.
1556931eeffaSKenneth D. Merry 		 */
155783b92f6eSKip Macy 		m_prev = m;
155889e0f4d2SKip Macy 
155996375eacSRoger Pau Monné 		rx = RING_GET_RESPONSE(&rxq->ring, *cons + frags);
156096375eacSRoger Pau Monné 		m = xn_get_rx_mbuf(rxq, *cons + frags);
156183b92f6eSKip Macy 
1562931eeffaSKenneth D. Merry 		/*
1563931eeffaSKenneth D. Merry 		 * m_prev == NULL can happen if rx->status < 0 or if
1564931eeffaSKenneth D. Merry 		 * rx->offset + * rx->status > PAGE_SIZE above.
1565931eeffaSKenneth D. Merry 		 */
1566931eeffaSKenneth D. Merry 		if (m_prev != NULL)
156783b92f6eSKip Macy 			m_prev->m_next = m;
1568931eeffaSKenneth D. Merry 
1569931eeffaSKenneth D. Merry 		/*
1570931eeffaSKenneth D. Merry 		 * m0 can be NULL if rx->status < 0 or if * rx->offset +
1571931eeffaSKenneth D. Merry 		 * rx->status > PAGE_SIZE above.
1572931eeffaSKenneth D. Merry 		 */
1573931eeffaSKenneth D. Merry 		if (m0 == NULL)
1574931eeffaSKenneth D. Merry 			m0 = m;
157583b92f6eSKip Macy 		m->m_next = NULL;
157696375eacSRoger Pau Monné 		ref = xn_get_rx_ref(rxq, *cons + frags);
157789e0f4d2SKip Macy 		frags++;
157889e0f4d2SKip Macy 	}
157983b92f6eSKip Macy 	*list = m0;
1580931eeffaSKenneth D. Merry 	*cons += frags;
158189e0f4d2SKip Macy 
15828577146eSJustin T. Gibbs 	return (err);
158389e0f4d2SKip Macy }
158489e0f4d2SKip Macy 
1585931eeffaSKenneth D. Merry /**
1586931eeffaSKenneth D. Merry  * Given an mbuf chain, make sure we have enough room and then push
1587931eeffaSKenneth D. Merry  * it onto the transmit ring.
1588931eeffaSKenneth D. Merry  */
1589931eeffaSKenneth D. Merry static int
159096375eacSRoger Pau Monné xn_assemble_tx_request(struct netfront_txq *txq, struct mbuf *m_head)
1591931eeffaSKenneth D. Merry {
159296375eacSRoger Pau Monné 	struct netfront_info *np = txq->info;
159302f3b17fSJustin Hibbits 	if_t ifp = np->xn_ifp;
1594dabb3db7SRoger Pau Monné 	int otherend_id, error, nfrags;
1595dabb3db7SRoger Pau Monné 	bus_dma_segment_t *segs = txq->segs;
1596dabb3db7SRoger Pau Monné 	struct mbuf_xennet *tag;
1597dabb3db7SRoger Pau Monné 	bus_dmamap_t map;
1598dabb3db7SRoger Pau Monné 	unsigned int i;
1599931eeffaSKenneth D. Merry 
1600dabb3db7SRoger Pau Monné 	KASSERT(!SLIST_EMPTY(&txq->tags), ("no tags available"));
1601dabb3db7SRoger Pau Monné 	tag = SLIST_FIRST(&txq->tags);
1602dabb3db7SRoger Pau Monné 	SLIST_REMOVE_HEAD(&txq->tags, next);
1603dabb3db7SRoger Pau Monné 	KASSERT(tag->count == 0, ("tag already in-use"));
1604dabb3db7SRoger Pau Monné 	map = tag->dma_map;
1605dabb3db7SRoger Pau Monné 	error = bus_dmamap_load_mbuf_sg(np->dma_tag, map, m_head, segs,
1606dabb3db7SRoger Pau Monné 	    &nfrags, 0);
1607dabb3db7SRoger Pau Monné 	if (error == EFBIG || nfrags > np->maxfrags) {
1608dabb3db7SRoger Pau Monné 		struct mbuf *m;
1609931eeffaSKenneth D. Merry 
1610dabb3db7SRoger Pau Monné 		bus_dmamap_unload(np->dma_tag, map);
1611c6499eccSGleb Smirnoff 		m = m_defrag(m_head, M_NOWAIT);
161212678024SDoug Rabson 		if (!m) {
1613931eeffaSKenneth D. Merry 			/*
1614931eeffaSKenneth D. Merry 			 * Defrag failed, so free the mbuf and
1615931eeffaSKenneth D. Merry 			 * therefore drop the packet.
1616931eeffaSKenneth D. Merry 			 */
1617dabb3db7SRoger Pau Monné 			SLIST_INSERT_HEAD(&txq->tags, tag, next);
161812678024SDoug Rabson 			m_freem(m_head);
1619931eeffaSKenneth D. Merry 			return (EMSGSIZE);
162012678024SDoug Rabson 		}
162112678024SDoug Rabson 		m_head = m;
1622dabb3db7SRoger Pau Monné 		error = bus_dmamap_load_mbuf_sg(np->dma_tag, map, m_head, segs,
1623dabb3db7SRoger Pau Monné 		    &nfrags, 0);
1624dabb3db7SRoger Pau Monné 		if (error != 0 || nfrags > np->maxfrags) {
1625dabb3db7SRoger Pau Monné 			bus_dmamap_unload(np->dma_tag, map);
1626dabb3db7SRoger Pau Monné 			SLIST_INSERT_HEAD(&txq->tags, tag, next);
1627dabb3db7SRoger Pau Monné 			m_freem(m_head);
1628dabb3db7SRoger Pau Monné 			return (error ?: EFBIG);
1629dabb3db7SRoger Pau Monné 		}
1630dabb3db7SRoger Pau Monné 	} else if (error != 0) {
1631dabb3db7SRoger Pau Monné 		SLIST_INSERT_HEAD(&txq->tags, tag, next);
1632dabb3db7SRoger Pau Monné 		m_freem(m_head);
1633dabb3db7SRoger Pau Monné 		return (error);
163412678024SDoug Rabson 	}
163589e0f4d2SKip Macy 
1636931eeffaSKenneth D. Merry 	/**
1637931eeffaSKenneth D. Merry 	 * The FreeBSD TCP stack, with TSO enabled, can produce a chain
1638931eeffaSKenneth D. Merry 	 * of mbufs longer than Linux can handle.  Make sure we don't
1639931eeffaSKenneth D. Merry 	 * pass a too-long chain over to the other side by dropping the
1640931eeffaSKenneth D. Merry 	 * packet.  It doesn't look like there is currently a way to
1641931eeffaSKenneth D. Merry 	 * tell the TCP stack to generate a shorter chain of packets.
16423fb28bbbSAdrian Chadd 	 */
1643931eeffaSKenneth D. Merry 	if (nfrags > MAX_TX_REQ_FRAGS) {
1644ff662b5cSJustin T. Gibbs #ifdef DEBUG
1645ff662b5cSJustin T. Gibbs 		printf("%s: nfrags %d > MAX_TX_REQ_FRAGS %d, netback "
1646ff662b5cSJustin T. Gibbs 		       "won't be able to handle it, dropping\n",
1647ff662b5cSJustin T. Gibbs 		       __func__, nfrags, MAX_TX_REQ_FRAGS);
1648ff662b5cSJustin T. Gibbs #endif
1649dabb3db7SRoger Pau Monné 		SLIST_INSERT_HEAD(&txq->tags, tag, next);
1650dabb3db7SRoger Pau Monné 		bus_dmamap_unload(np->dma_tag, map);
1651931eeffaSKenneth D. Merry 		m_freem(m_head);
1652931eeffaSKenneth D. Merry 		return (EMSGSIZE);
1653a4ec37f5SAdrian Chadd 	}
1654a4ec37f5SAdrian Chadd 
16553fb28bbbSAdrian Chadd 	/*
1656931eeffaSKenneth D. Merry 	 * This check should be redundant.  We've already verified that we
1657931eeffaSKenneth D. Merry 	 * have enough slots in the ring to handle a packet of maximum
1658931eeffaSKenneth D. Merry 	 * size, and that our packet is less than the maximum size.  Keep
1659931eeffaSKenneth D. Merry 	 * it in here as an assert for now just to make certain that
166096375eacSRoger Pau Monné 	 * chain_cnt is accurate.
16613fb28bbbSAdrian Chadd 	 */
166296375eacSRoger Pau Monné 	KASSERT((txq->mbufs_cnt + nfrags) <= NET_TX_RING_SIZE,
166396375eacSRoger Pau Monné 		("%s: chain_cnt (%d) + nfrags (%d) > NET_TX_RING_SIZE "
166496375eacSRoger Pau Monné 		 "(%d)!", __func__, (int) txq->mbufs_cnt,
1665931eeffaSKenneth D. Merry                     (int) nfrags, (int) NET_TX_RING_SIZE));
1666a4ec37f5SAdrian Chadd 
166789e0f4d2SKip Macy 	/*
166889e0f4d2SKip Macy 	 * Start packing the mbufs in this chain into
166989e0f4d2SKip Macy 	 * the fragment pointers. Stop when we run out
167089e0f4d2SKip Macy 	 * of fragments or hit the end of the mbuf chain.
167189e0f4d2SKip Macy 	 */
167296375eacSRoger Pau Monné 	otherend_id = xenbus_get_otherend_id(np->xbdev);
1673dabb3db7SRoger Pau Monné 	m_tag_prepend(m_head, &tag->tag);
1674dabb3db7SRoger Pau Monné 	for (i = 0; i < nfrags; i++) {
1675931eeffaSKenneth D. Merry 		netif_tx_request_t *tx;
1676931eeffaSKenneth D. Merry 		uintptr_t id;
1677931eeffaSKenneth D. Merry 		grant_ref_t ref;
1678931eeffaSKenneth D. Merry 		u_long mfn; /* XXX Wrong type? */
1679931eeffaSKenneth D. Merry 
168096375eacSRoger Pau Monné 		tx = RING_GET_REQUEST(&txq->ring, txq->ring.req_prod_pvt);
168196375eacSRoger Pau Monné 		id = get_id_from_freelist(txq->mbufs);
1682a4ec37f5SAdrian Chadd 		if (id == 0)
16836f9767acSMarius Strobl 			panic("%s: was allocated the freelist head!\n",
16846f9767acSMarius Strobl 			    __func__);
168596375eacSRoger Pau Monné 		txq->mbufs_cnt++;
168696375eacSRoger Pau Monné 		if (txq->mbufs_cnt > NET_TX_RING_SIZE)
16876f9767acSMarius Strobl 			panic("%s: tx_chain_cnt must be <= NET_TX_RING_SIZE\n",
16886f9767acSMarius Strobl 			    __func__);
1689dabb3db7SRoger Pau Monné 		mbuf_grab(m_head);
1690dabb3db7SRoger Pau Monné 		txq->mbufs[id] = m_head;
169189e0f4d2SKip Macy 		tx->id = id;
169296375eacSRoger Pau Monné 		ref = gnttab_claim_grant_reference(&txq->gref_head);
169389e0f4d2SKip Macy 		KASSERT((short)ref >= 0, ("Negative ref"));
1694dabb3db7SRoger Pau Monné 		mfn = atop(segs[i].ds_addr);
169523dc5621SKip Macy 		gnttab_grant_foreign_access_ref(ref, otherend_id,
169689e0f4d2SKip Macy 		    mfn, GNTMAP_readonly);
169796375eacSRoger Pau Monné 		tx->gref = txq->grant_ref[id] = ref;
1698dabb3db7SRoger Pau Monné 		tx->offset = segs[i].ds_addr & PAGE_MASK;
1699dabb3db7SRoger Pau Monné 		KASSERT(tx->offset + segs[i].ds_len <= PAGE_SIZE,
1700dabb3db7SRoger Pau Monné 		    ("mbuf segment crosses a page boundary"));
170189e0f4d2SKip Macy 		tx->flags = 0;
1702dabb3db7SRoger Pau Monné 		if (i == 0) {
170312678024SDoug Rabson 			/*
170412678024SDoug Rabson 			 * The first fragment has the entire packet
170512678024SDoug Rabson 			 * size, subsequent fragments have just the
170612678024SDoug Rabson 			 * fragment size. The backend works out the
170712678024SDoug Rabson 			 * true size of the first fragment by
170812678024SDoug Rabson 			 * subtracting the sizes of the other
170912678024SDoug Rabson 			 * fragments.
171012678024SDoug Rabson 			 */
1711dabb3db7SRoger Pau Monné 			tx->size = m_head->m_pkthdr.len;
171289e0f4d2SKip Macy 
171312678024SDoug Rabson 			/*
1714931eeffaSKenneth D. Merry 			 * The first fragment contains the checksum flags
1715931eeffaSKenneth D. Merry 			 * and is optionally followed by extra data for
1716931eeffaSKenneth D. Merry 			 * TSO etc.
1717931eeffaSKenneth D. Merry 			 */
1718931eeffaSKenneth D. Merry 			/**
1719931eeffaSKenneth D. Merry 			 * CSUM_TSO requires checksum offloading.
1720931eeffaSKenneth D. Merry 			 * Some versions of FreeBSD fail to
1721931eeffaSKenneth D. Merry 			 * set CSUM_TCP in the CSUM_TSO case,
1722931eeffaSKenneth D. Merry 			 * so we have to test for CSUM_TSO
1723931eeffaSKenneth D. Merry 			 * explicitly.
172412678024SDoug Rabson 			 */
1725dabb3db7SRoger Pau Monné 			if (m_head->m_pkthdr.csum_flags
1726931eeffaSKenneth D. Merry 			    & (CSUM_DELAY_DATA | CSUM_TSO)) {
172712678024SDoug Rabson 				tx->flags |= (NETTXF_csum_blank
172812678024SDoug Rabson 				    | NETTXF_data_validated);
172912678024SDoug Rabson 			}
1730dabb3db7SRoger Pau Monné 			if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
173112678024SDoug Rabson 				struct netif_extra_info *gso =
173212678024SDoug Rabson 					(struct netif_extra_info *)
173396375eacSRoger Pau Monné 					RING_GET_REQUEST(&txq->ring,
173496375eacSRoger Pau Monné 							 ++txq->ring.req_prod_pvt);
173589e0f4d2SKip Macy 
173612678024SDoug Rabson 				tx->flags |= NETTXF_extra_info;
173789e0f4d2SKip Macy 
1738dabb3db7SRoger Pau Monné 				gso->u.gso.size = m_head->m_pkthdr.tso_segsz;
173912678024SDoug Rabson 				gso->u.gso.type =
174012678024SDoug Rabson 					XEN_NETIF_GSO_TYPE_TCPV4;
174112678024SDoug Rabson 				gso->u.gso.pad = 0;
174212678024SDoug Rabson 				gso->u.gso.features = 0;
174312678024SDoug Rabson 
174412678024SDoug Rabson 				gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
174512678024SDoug Rabson 				gso->flags = 0;
174612678024SDoug Rabson 			}
174712678024SDoug Rabson 		} else {
1748dabb3db7SRoger Pau Monné 			tx->size = segs[i].ds_len;
174912678024SDoug Rabson 		}
1750dabb3db7SRoger Pau Monné 		if (i != nfrags - 1)
175112678024SDoug Rabson 			tx->flags |= NETTXF_more_data;
175212678024SDoug Rabson 
175396375eacSRoger Pau Monné 		txq->ring.req_prod_pvt++;
1754931eeffaSKenneth D. Merry 	}
1755dabb3db7SRoger Pau Monné 	bus_dmamap_sync(np->dma_tag, map, BUS_DMASYNC_PREWRITE);
175612678024SDoug Rabson 	BPF_MTAP(ifp, m_head);
175712678024SDoug Rabson 
1758b2fd6999SRoger Pau Monné 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1759b2fd6999SRoger Pau Monné 	if_inc_counter(ifp, IFCOUNTER_OBYTES, m_head->m_pkthdr.len);
1760b2fd6999SRoger Pau Monné 	if (m_head->m_flags & M_MCAST)
1761b2fd6999SRoger Pau Monné 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
176296375eacSRoger Pau Monné 
1763b2fd6999SRoger Pau Monné 	xn_txeof(txq);
1764931eeffaSKenneth D. Merry 
1765931eeffaSKenneth D. Merry 	return (0);
176689e0f4d2SKip Macy }
176789e0f4d2SKip Macy 
176889e0f4d2SKip Macy /* equivalent of network_open() in Linux */
176989e0f4d2SKip Macy static void
177096375eacSRoger Pau Monné xn_ifinit_locked(struct netfront_info *np)
177189e0f4d2SKip Macy {
177202f3b17fSJustin Hibbits 	if_t ifp;
177396375eacSRoger Pau Monné 	int i;
177496375eacSRoger Pau Monné 	struct netfront_rxq *rxq;
177589e0f4d2SKip Macy 
177696375eacSRoger Pau Monné 	XN_LOCK_ASSERT(np);
177789e0f4d2SKip Macy 
177896375eacSRoger Pau Monné 	ifp = np->xn_ifp;
177989e0f4d2SKip Macy 
178002f3b17fSJustin Hibbits 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING || !netfront_carrier_ok(np))
178189e0f4d2SKip Macy 		return;
178289e0f4d2SKip Macy 
178396375eacSRoger Pau Monné 	xn_stop(np);
178489e0f4d2SKip Macy 
178596375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
178696375eacSRoger Pau Monné 		rxq = &np->rxq[i];
17872568ee67SRoger Pau Monné 		XN_RX_LOCK(rxq);
178896375eacSRoger Pau Monné 		xn_alloc_rx_buffers(rxq);
178996375eacSRoger Pau Monné 		rxq->ring.sring->rsp_event = rxq->ring.rsp_cons + 1;
17902568ee67SRoger Pau Monné 		if (RING_HAS_UNCONSUMED_RESPONSES(&rxq->ring))
1791da695b05SRoger Pau Monné 			xn_rxeof(rxq);
17922568ee67SRoger Pau Monné 		XN_RX_UNLOCK(rxq);
179396375eacSRoger Pau Monné 	}
179489e0f4d2SKip Macy 
179502f3b17fSJustin Hibbits 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
179602f3b17fSJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
17970e509842SJustin T. Gibbs 	if_link_state_change(ifp, LINK_STATE_UP);
179889e0f4d2SKip Macy }
179989e0f4d2SKip Macy 
180089e0f4d2SKip Macy static void
180189e0f4d2SKip Macy xn_ifinit(void *xsc)
180289e0f4d2SKip Macy {
180389e0f4d2SKip Macy 	struct netfront_info *sc = xsc;
180489e0f4d2SKip Macy 
180589e0f4d2SKip Macy 	XN_LOCK(sc);
180689e0f4d2SKip Macy 	xn_ifinit_locked(sc);
180789e0f4d2SKip Macy 	XN_UNLOCK(sc);
180889e0f4d2SKip Macy }
180989e0f4d2SKip Macy 
181089e0f4d2SKip Macy static int
181102f3b17fSJustin Hibbits xn_ioctl(if_t ifp, u_long cmd, caddr_t data)
181289e0f4d2SKip Macy {
181302f3b17fSJustin Hibbits 	struct netfront_info *sc = if_getsoftc(ifp);
181489e0f4d2SKip Macy 	struct ifreq *ifr = (struct ifreq *) data;
1815c2d12e5eSRoger Pau Monné 	device_t dev;
1816a0ae8f04SBjoern A. Zeeb #ifdef INET
181789e0f4d2SKip Macy 	struct ifaddr *ifa = (struct ifaddr *)data;
1818a0ae8f04SBjoern A. Zeeb #endif
18193c9d5940SRoger Pau Monné 	int mask, error = 0, reinit;
1820c2d12e5eSRoger Pau Monné 
1821c2d12e5eSRoger Pau Monné 	dev = sc->xbdev;
1822c2d12e5eSRoger Pau Monné 
182389e0f4d2SKip Macy 	switch(cmd) {
182489e0f4d2SKip Macy 	case SIOCSIFADDR:
1825a0ae8f04SBjoern A. Zeeb #ifdef INET
182689e0f4d2SKip Macy 		XN_LOCK(sc);
182789e0f4d2SKip Macy 		if (ifa->ifa_addr->sa_family == AF_INET) {
182802f3b17fSJustin Hibbits 			if_setflagbits(ifp, IFF_UP, 0);
182902f3b17fSJustin Hibbits 			if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
183089e0f4d2SKip Macy 				xn_ifinit_locked(sc);
183189e0f4d2SKip Macy 			arp_ifinit(ifp, ifa);
183289e0f4d2SKip Macy 			XN_UNLOCK(sc);
183349906218SDoug Rabson 		} else {
183449906218SDoug Rabson 			XN_UNLOCK(sc);
1835a0ae8f04SBjoern A. Zeeb #endif
183649906218SDoug Rabson 			error = ether_ioctl(ifp, cmd, data);
1837a0ae8f04SBjoern A. Zeeb #ifdef INET
183849906218SDoug Rabson 		}
1839a0ae8f04SBjoern A. Zeeb #endif
184089e0f4d2SKip Macy 		break;
184189e0f4d2SKip Macy 	case SIOCSIFMTU:
184202f3b17fSJustin Hibbits 		if (if_getmtu(ifp) == ifr->ifr_mtu)
1843c74415edSColin Percival 			break;
1844c74415edSColin Percival 
184502f3b17fSJustin Hibbits 		if_setmtu(ifp, ifr->ifr_mtu);
184602f3b17fSJustin Hibbits 		if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
184789e0f4d2SKip Macy 		xn_ifinit(sc);
184889e0f4d2SKip Macy 		break;
184989e0f4d2SKip Macy 	case SIOCSIFFLAGS:
185089e0f4d2SKip Macy 		XN_LOCK(sc);
185102f3b17fSJustin Hibbits 		if (if_getflags(ifp) & IFF_UP) {
185289e0f4d2SKip Macy 			/*
185389e0f4d2SKip Macy 			 * If only the state of the PROMISC flag changed,
185489e0f4d2SKip Macy 			 * then just use the 'set promisc mode' command
185589e0f4d2SKip Macy 			 * instead of reinitializing the entire NIC. Doing
185689e0f4d2SKip Macy 			 * a full re-init means reloading the firmware and
185789e0f4d2SKip Macy 			 * waiting for it to start up, which may take a
185889e0f4d2SKip Macy 			 * second or two.
185989e0f4d2SKip Macy 			 */
186089e0f4d2SKip Macy 			xn_ifinit_locked(sc);
186189e0f4d2SKip Macy 		} else {
186202f3b17fSJustin Hibbits 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
186389e0f4d2SKip Macy 				xn_stop(sc);
186489e0f4d2SKip Macy 			}
186589e0f4d2SKip Macy 		}
186602f3b17fSJustin Hibbits 		sc->xn_if_flags = if_getflags(ifp);
186789e0f4d2SKip Macy 		XN_UNLOCK(sc);
186889e0f4d2SKip Macy 		break;
186989e0f4d2SKip Macy 	case SIOCSIFCAP:
187002f3b17fSJustin Hibbits 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
18713c9d5940SRoger Pau Monné 		reinit = 0;
18723c9d5940SRoger Pau Monné 
187312678024SDoug Rabson 		if (mask & IFCAP_TXCSUM) {
187402f3b17fSJustin Hibbits 			if_togglecapenable(ifp, IFCAP_TXCSUM);
187502f3b17fSJustin Hibbits 			if_togglehwassist(ifp, XN_CSUM_FEATURES);
187612678024SDoug Rabson 		}
187712678024SDoug Rabson 		if (mask & IFCAP_TSO4) {
187802f3b17fSJustin Hibbits 			if_togglecapenable(ifp, IFCAP_TSO4);
187902f3b17fSJustin Hibbits 			if_togglehwassist(ifp, CSUM_TSO);
188012678024SDoug Rabson 		}
188112678024SDoug Rabson 
18823c9d5940SRoger Pau Monné 		if (mask & (IFCAP_RXCSUM | IFCAP_LRO)) {
18833c9d5940SRoger Pau Monné 			/* These Rx features require us to renegotiate. */
18843c9d5940SRoger Pau Monné 			reinit = 1;
18853c9d5940SRoger Pau Monné 
18863c9d5940SRoger Pau Monné 			if (mask & IFCAP_RXCSUM)
188702f3b17fSJustin Hibbits 				if_togglecapenable(ifp, IFCAP_RXCSUM);
18883c9d5940SRoger Pau Monné 			if (mask & IFCAP_LRO)
188902f3b17fSJustin Hibbits 				if_togglecapenable(ifp, IFCAP_LRO);
189012678024SDoug Rabson 		}
18913c9d5940SRoger Pau Monné 
18923c9d5940SRoger Pau Monné 		if (reinit == 0)
18933c9d5940SRoger Pau Monné 			break;
18943c9d5940SRoger Pau Monné 
1895c2d12e5eSRoger Pau Monné 		/*
1896c2d12e5eSRoger Pau Monné 		 * We must reset the interface so the backend picks up the
1897c2d12e5eSRoger Pau Monné 		 * new features.
1898c2d12e5eSRoger Pau Monné 		 */
18993c9d5940SRoger Pau Monné 		device_printf(sc->xbdev,
19003c9d5940SRoger Pau Monné 		    "performing interface reset due to feature change\n");
1901c2d12e5eSRoger Pau Monné 		XN_LOCK(sc);
1902c2d12e5eSRoger Pau Monné 		netfront_carrier_off(sc);
1903c2d12e5eSRoger Pau Monné 		sc->xn_reset = true;
1904c2d12e5eSRoger Pau Monné 		/*
1905c2d12e5eSRoger Pau Monné 		 * NB: the pending packet queue is not flushed, since
1906c2d12e5eSRoger Pau Monné 		 * the interface should still support the old options.
1907c2d12e5eSRoger Pau Monné 		 */
1908c2d12e5eSRoger Pau Monné 		XN_UNLOCK(sc);
1909c2d12e5eSRoger Pau Monné 		/*
1910c2d12e5eSRoger Pau Monné 		 * Delete the xenstore nodes that export features.
1911c2d12e5eSRoger Pau Monné 		 *
1912c2d12e5eSRoger Pau Monné 		 * NB: There's a xenbus state called
1913c2d12e5eSRoger Pau Monné 		 * "XenbusStateReconfiguring", which is what we should set
1914c2d12e5eSRoger Pau Monné 		 * here. Sadly none of the backends know how to handle it,
1915c2d12e5eSRoger Pau Monné 		 * and simply disconnect from the frontend, so we will just
1916c2d12e5eSRoger Pau Monné 		 * switch back to XenbusStateInitialising in order to force
1917c2d12e5eSRoger Pau Monné 		 * a reconnection.
1918c2d12e5eSRoger Pau Monné 		 */
1919c2d12e5eSRoger Pau Monné 		xs_rm(XST_NIL, xenbus_get_node(dev), "feature-gso-tcpv4");
1920c2d12e5eSRoger Pau Monné 		xs_rm(XST_NIL, xenbus_get_node(dev), "feature-no-csum-offload");
1921c2d12e5eSRoger Pau Monné 		xenbus_set_state(dev, XenbusStateClosing);
19223c9d5940SRoger Pau Monné 
19233c9d5940SRoger Pau Monné 		/*
19243c9d5940SRoger Pau Monné 		 * Wait for the frontend to reconnect before returning
19253c9d5940SRoger Pau Monné 		 * from the ioctl. 30s should be more than enough for any
19263c9d5940SRoger Pau Monné 		 * sane backend to reconnect.
19273c9d5940SRoger Pau Monné 		 */
19283c9d5940SRoger Pau Monné 		error = tsleep(sc, 0, "xn_rst", 30*hz);
192989e0f4d2SKip Macy 		break;
193089e0f4d2SKip Macy 	case SIOCADDMULTI:
193189e0f4d2SKip Macy 	case SIOCDELMULTI:
1932ce8df48bSSimon J. Gerraty 		break;
193389e0f4d2SKip Macy 	case SIOCSIFMEDIA:
193489e0f4d2SKip Macy 	case SIOCGIFMEDIA:
19350e509842SJustin T. Gibbs 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
193689e0f4d2SKip Macy 		break;
193789e0f4d2SKip Macy 	default:
193889e0f4d2SKip Macy 		error = ether_ioctl(ifp, cmd, data);
193989e0f4d2SKip Macy 	}
194089e0f4d2SKip Macy 
194189e0f4d2SKip Macy 	return (error);
194289e0f4d2SKip Macy }
194389e0f4d2SKip Macy 
194489e0f4d2SKip Macy static void
194589e0f4d2SKip Macy xn_stop(struct netfront_info *sc)
194689e0f4d2SKip Macy {
194702f3b17fSJustin Hibbits 	if_t ifp;
194889e0f4d2SKip Macy 
194989e0f4d2SKip Macy 	XN_LOCK_ASSERT(sc);
195089e0f4d2SKip Macy 
195189e0f4d2SKip Macy 	ifp = sc->xn_ifp;
195289e0f4d2SKip Macy 
195302f3b17fSJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
19540e509842SJustin T. Gibbs 	if_link_state_change(ifp, LINK_STATE_DOWN);
195589e0f4d2SKip Macy }
195689e0f4d2SKip Macy 
195796375eacSRoger Pau Monné static void
195896375eacSRoger Pau Monné xn_rebuild_rx_bufs(struct netfront_rxq *rxq)
195989e0f4d2SKip Macy {
196096375eacSRoger Pau Monné 	int requeue_idx, i;
196189e0f4d2SKip Macy 	grant_ref_t ref;
196289e0f4d2SKip Macy 	netif_rx_request_t *req;
196389e0f4d2SKip Macy 
196489e0f4d2SKip Macy 	for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
196589e0f4d2SKip Macy 		struct mbuf *m;
19663a6d1fcfSKip Macy 		u_long pfn;
196789e0f4d2SKip Macy 
196896375eacSRoger Pau Monné 		if (rxq->mbufs[i] == NULL)
196989e0f4d2SKip Macy 			continue;
197089e0f4d2SKip Macy 
197196375eacSRoger Pau Monné 		m = rxq->mbufs[requeue_idx] = xn_get_rx_mbuf(rxq, i);
197296375eacSRoger Pau Monné 		ref = rxq->grant_ref[requeue_idx] = xn_get_rx_ref(rxq, i);
1973931eeffaSKenneth D. Merry 
197496375eacSRoger Pau Monné 		req = RING_GET_REQUEST(&rxq->ring, requeue_idx);
19753a6d1fcfSKip Macy 		pfn = vtophys(mtod(m, vm_offset_t)) >> PAGE_SHIFT;
197689e0f4d2SKip Macy 
197789e0f4d2SKip Macy 		gnttab_grant_foreign_access_ref(ref,
197896375eacSRoger Pau Monné 		    xenbus_get_otherend_id(rxq->info->xbdev),
1979ed95805eSJohn Baldwin 		    pfn, 0);
1980d0f3a8b9SRoger Pau Monné 
198189e0f4d2SKip Macy 		req->gref = ref;
198289e0f4d2SKip Macy 		req->id   = requeue_idx;
198389e0f4d2SKip Macy 
198489e0f4d2SKip Macy 		requeue_idx++;
198589e0f4d2SKip Macy 	}
198689e0f4d2SKip Macy 
198796375eacSRoger Pau Monné 	rxq->ring.req_prod_pvt = requeue_idx;
198896375eacSRoger Pau Monné }
198989e0f4d2SKip Macy 
199096375eacSRoger Pau Monné /* START of Xenolinux helper functions adapted to FreeBSD */
199165671253SRoger Pau Monné static int
199296375eacSRoger Pau Monné xn_connect(struct netfront_info *np)
199396375eacSRoger Pau Monné {
199496375eacSRoger Pau Monné 	int i, error;
199596375eacSRoger Pau Monné 	u_int feature_rx_copy;
199696375eacSRoger Pau Monné 	struct netfront_rxq *rxq;
199796375eacSRoger Pau Monné 	struct netfront_txq *txq;
199896375eacSRoger Pau Monné 
199996375eacSRoger Pau Monné 	error = xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev),
200096375eacSRoger Pau Monné 	    "feature-rx-copy", NULL, "%u", &feature_rx_copy);
200196375eacSRoger Pau Monné 	if (error != 0)
200296375eacSRoger Pau Monné 		feature_rx_copy = 0;
200396375eacSRoger Pau Monné 
200496375eacSRoger Pau Monné 	/* We only support rx copy. */
200596375eacSRoger Pau Monné 	if (!feature_rx_copy)
200696375eacSRoger Pau Monné 		return (EPROTONOSUPPORT);
200796375eacSRoger Pau Monné 
200896375eacSRoger Pau Monné 	/* Recovery procedure: */
200996375eacSRoger Pau Monné 	error = talk_to_backend(np->xbdev, np);
201096375eacSRoger Pau Monné 	if (error != 0)
201196375eacSRoger Pau Monné 		return (error);
201296375eacSRoger Pau Monné 
201396375eacSRoger Pau Monné 	/* Step 1: Reinitialise variables. */
201496375eacSRoger Pau Monné 	xn_query_features(np);
201596375eacSRoger Pau Monné 	xn_configure_features(np);
201696375eacSRoger Pau Monné 
201796375eacSRoger Pau Monné 	/* Step 2: Release TX buffer */
201896375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
201996375eacSRoger Pau Monné 		txq = &np->txq[i];
202096375eacSRoger Pau Monné 		xn_release_tx_bufs(txq);
202196375eacSRoger Pau Monné 	}
202296375eacSRoger Pau Monné 
202396375eacSRoger Pau Monné 	/* Step 3: Rebuild the RX buffer freelist and the RX ring itself. */
202496375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
202596375eacSRoger Pau Monné 		rxq = &np->rxq[i];
202696375eacSRoger Pau Monné 		xn_rebuild_rx_bufs(rxq);
202796375eacSRoger Pau Monné 	}
202896375eacSRoger Pau Monné 
202996375eacSRoger Pau Monné 	/* Step 4: All public and private state should now be sane.  Get
203089e0f4d2SKip Macy 	 * ready to start sending and receiving packets and give the driver
203189e0f4d2SKip Macy 	 * domain a kick because we've probably just requeued some
203289e0f4d2SKip Macy 	 * packets.
203389e0f4d2SKip Macy 	 */
203489e0f4d2SKip Macy 	netfront_carrier_on(np);
20353c9d5940SRoger Pau Monné 	wakeup(np);
203665671253SRoger Pau Monné 
203765671253SRoger Pau Monné 	return (0);
203865671253SRoger Pau Monné }
203965671253SRoger Pau Monné 
204065671253SRoger Pau Monné static void
204165671253SRoger Pau Monné xn_kick_rings(struct netfront_info *np)
204265671253SRoger Pau Monné {
204365671253SRoger Pau Monné 	struct netfront_rxq *rxq;
204465671253SRoger Pau Monné 	struct netfront_txq *txq;
204565671253SRoger Pau Monné 	int i;
204665671253SRoger Pau Monné 
204796375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
204896375eacSRoger Pau Monné 		txq = &np->txq[i];
204965671253SRoger Pau Monné 		rxq = &np->rxq[i];
205096375eacSRoger Pau Monné 		xen_intr_signal(txq->xen_intr_handle);
205196375eacSRoger Pau Monné 		XN_TX_LOCK(txq);
205296375eacSRoger Pau Monné 		xn_txeof(txq);
205396375eacSRoger Pau Monné 		XN_TX_UNLOCK(txq);
20542568ee67SRoger Pau Monné 		XN_RX_LOCK(rxq);
205596375eacSRoger Pau Monné 		xn_alloc_rx_buffers(rxq);
20562568ee67SRoger Pau Monné 		XN_RX_UNLOCK(rxq);
205796375eacSRoger Pau Monné 	}
205889e0f4d2SKip Macy }
205989e0f4d2SKip Macy 
206089e0f4d2SKip Macy static void
2061578e4bf7SJustin T. Gibbs xn_query_features(struct netfront_info *np)
2062578e4bf7SJustin T. Gibbs {
2063578e4bf7SJustin T. Gibbs 	int val;
2064578e4bf7SJustin T. Gibbs 
2065578e4bf7SJustin T. Gibbs 	device_printf(np->xbdev, "backend features:");
2066578e4bf7SJustin T. Gibbs 
2067578e4bf7SJustin T. Gibbs 	if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev),
2068107cfbb7SRoger Pau Monné 		"feature-sg", NULL, "%d", &val) != 0)
2069578e4bf7SJustin T. Gibbs 		val = 0;
2070578e4bf7SJustin T. Gibbs 
2071578e4bf7SJustin T. Gibbs 	np->maxfrags = 1;
2072578e4bf7SJustin T. Gibbs 	if (val) {
2073578e4bf7SJustin T. Gibbs 		np->maxfrags = MAX_TX_REQ_FRAGS;
2074578e4bf7SJustin T. Gibbs 		printf(" feature-sg");
2075578e4bf7SJustin T. Gibbs 	}
2076578e4bf7SJustin T. Gibbs 
2077578e4bf7SJustin T. Gibbs 	if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev),
2078107cfbb7SRoger Pau Monné 		"feature-gso-tcpv4", NULL, "%d", &val) != 0)
2079578e4bf7SJustin T. Gibbs 		val = 0;
2080578e4bf7SJustin T. Gibbs 
208102f3b17fSJustin Hibbits 	if_setcapabilitiesbit(np->xn_ifp, 0, IFCAP_TSO4 | IFCAP_LRO);
2082578e4bf7SJustin T. Gibbs 	if (val) {
208302f3b17fSJustin Hibbits 		if_setcapabilitiesbit(np->xn_ifp, IFCAP_TSO4 | IFCAP_LRO, 0);
2084578e4bf7SJustin T. Gibbs 		printf(" feature-gso-tcp4");
2085578e4bf7SJustin T. Gibbs 	}
2086578e4bf7SJustin T. Gibbs 
2087c2d12e5eSRoger Pau Monné 	/*
2088c2d12e5eSRoger Pau Monné 	 * HW CSUM offload is assumed to be available unless
2089c2d12e5eSRoger Pau Monné 	 * feature-no-csum-offload is set in xenstore.
2090c2d12e5eSRoger Pau Monné 	 */
2091c2d12e5eSRoger Pau Monné 	if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev),
2092c2d12e5eSRoger Pau Monné 		"feature-no-csum-offload", NULL, "%d", &val) != 0)
2093c2d12e5eSRoger Pau Monné 		val = 0;
2094c2d12e5eSRoger Pau Monné 
209502f3b17fSJustin Hibbits 	if_setcapabilitiesbit(np->xn_ifp, IFCAP_HWCSUM, 0);
2096c2d12e5eSRoger Pau Monné 	if (val) {
209702f3b17fSJustin Hibbits 		if_setcapabilitiesbit(np->xn_ifp, 0, IFCAP_HWCSUM);
2098c2d12e5eSRoger Pau Monné 		printf(" feature-no-csum-offload");
2099c2d12e5eSRoger Pau Monné 	}
2100c2d12e5eSRoger Pau Monné 
2101578e4bf7SJustin T. Gibbs 	printf("\n");
2102578e4bf7SJustin T. Gibbs }
2103578e4bf7SJustin T. Gibbs 
2104cf9c09e1SJustin T. Gibbs static int
2105578e4bf7SJustin T. Gibbs xn_configure_features(struct netfront_info *np)
2106cf9c09e1SJustin T. Gibbs {
21076a8e9695SRoger Pau Monné 	int err, cap_enabled;
210896375eacSRoger Pau Monné #if (defined(INET) || defined(INET6))
210996375eacSRoger Pau Monné 	int i;
211096375eacSRoger Pau Monné #endif
211102f3b17fSJustin Hibbits 	if_t ifp;
2112cf9c09e1SJustin T. Gibbs 
2113c2d12e5eSRoger Pau Monné 	ifp = np->xn_ifp;
2114cf9c09e1SJustin T. Gibbs 	err = 0;
21156a8e9695SRoger Pau Monné 
211602f3b17fSJustin Hibbits 	if ((if_getcapenable(ifp) & if_getcapabilities(ifp)) == if_getcapenable(ifp)) {
21176a8e9695SRoger Pau Monné 		/* Current options are available, no need to do anything. */
21186a8e9695SRoger Pau Monné 		return (0);
21196a8e9695SRoger Pau Monné 	}
21206a8e9695SRoger Pau Monné 
21216a8e9695SRoger Pau Monné 	/* Try to preserve as many options as possible. */
212202f3b17fSJustin Hibbits 	cap_enabled = if_getcapenable(ifp);
212302f3b17fSJustin Hibbits 	if_setcapenable(ifp, 0);
212402f3b17fSJustin Hibbits 	if_sethwassist(ifp, 0);
21256a8e9695SRoger Pau Monné 
212608c9c2e0SRoger Pau Monné #if (defined(INET) || defined(INET6))
2127c2d12e5eSRoger Pau Monné 	if ((cap_enabled & IFCAP_LRO) != 0)
212896375eacSRoger Pau Monné 		for (i = 0; i < np->num_queues; i++)
212996375eacSRoger Pau Monné 			tcp_lro_free(&np->rxq[i].lro);
2130c2d12e5eSRoger Pau Monné 	if (xn_enable_lro &&
213102f3b17fSJustin Hibbits 	    (if_getcapabilities(ifp) & cap_enabled & IFCAP_LRO) != 0) {
213202f3b17fSJustin Hibbits 	    	if_setcapenablebit(ifp, IFCAP_LRO, 0);
213396375eacSRoger Pau Monné 		for (i = 0; i < np->num_queues; i++) {
213496375eacSRoger Pau Monné 			err = tcp_lro_init(&np->rxq[i].lro);
213596375eacSRoger Pau Monné 			if (err != 0) {
2136c2d12e5eSRoger Pau Monné 				device_printf(np->xbdev,
2137c2d12e5eSRoger Pau Monné 				    "LRO initialization failed\n");
213802f3b17fSJustin Hibbits 				if_setcapenablebit(ifp, 0, IFCAP_LRO);
2139c2d12e5eSRoger Pau Monné 				break;
2140c2d12e5eSRoger Pau Monné 			}
2141c2d12e5eSRoger Pau Monné 			np->rxq[i].lro.ifp = ifp;
2142cf9c09e1SJustin T. Gibbs 		}
2143cf9c09e1SJustin T. Gibbs 	}
214402f3b17fSJustin Hibbits 	if ((if_getcapabilities(ifp) & cap_enabled & IFCAP_TSO4) != 0) {
214502f3b17fSJustin Hibbits 		if_setcapenablebit(ifp, IFCAP_TSO4, 0);
214602f3b17fSJustin Hibbits 		if_sethwassistbits(ifp, CSUM_TSO, 0);
2147578e4bf7SJustin T. Gibbs 	}
2148cf9c09e1SJustin T. Gibbs #endif
214902f3b17fSJustin Hibbits 	if ((if_getcapabilities(ifp) & cap_enabled & IFCAP_TXCSUM) != 0) {
215002f3b17fSJustin Hibbits 		if_setcapenablebit(ifp, IFCAP_TXCSUM, 0);
215102f3b17fSJustin Hibbits 		if_sethwassistbits(ifp, XN_CSUM_FEATURES, 0);
2152c2d12e5eSRoger Pau Monné 	}
215302f3b17fSJustin Hibbits 	if ((if_getcapabilities(ifp) & cap_enabled & IFCAP_RXCSUM) != 0)
215402f3b17fSJustin Hibbits 		if_setcapenablebit(ifp, IFCAP_RXCSUM, 0);
2155c2d12e5eSRoger Pau Monné 
2156cf9c09e1SJustin T. Gibbs 	return (err);
2157cf9c09e1SJustin T. Gibbs }
2158cf9c09e1SJustin T. Gibbs 
215996375eacSRoger Pau Monné static int
216096375eacSRoger Pau Monné xn_txq_mq_start_locked(struct netfront_txq *txq, struct mbuf *m)
216196375eacSRoger Pau Monné {
216296375eacSRoger Pau Monné 	struct netfront_info *np;
216302f3b17fSJustin Hibbits 	if_t ifp;
216496375eacSRoger Pau Monné 	struct buf_ring *br;
216596375eacSRoger Pau Monné 	int error, notify;
216696375eacSRoger Pau Monné 
216796375eacSRoger Pau Monné 	np = txq->info;
216896375eacSRoger Pau Monné 	br = txq->br;
216996375eacSRoger Pau Monné 	ifp = np->xn_ifp;
217096375eacSRoger Pau Monné 	error = 0;
217196375eacSRoger Pau Monné 
217296375eacSRoger Pau Monné 	XN_TX_LOCK_ASSERT(txq);
217396375eacSRoger Pau Monné 
217402f3b17fSJustin Hibbits 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0 ||
217596375eacSRoger Pau Monné 	    !netfront_carrier_ok(np)) {
217696375eacSRoger Pau Monné 		if (m != NULL)
217796375eacSRoger Pau Monné 			error = drbr_enqueue(ifp, br, m);
217896375eacSRoger Pau Monné 		return (error);
217996375eacSRoger Pau Monné 	}
218096375eacSRoger Pau Monné 
218196375eacSRoger Pau Monné 	if (m != NULL) {
218296375eacSRoger Pau Monné 		error = drbr_enqueue(ifp, br, m);
218396375eacSRoger Pau Monné 		if (error != 0)
218496375eacSRoger Pau Monné 			return (error);
218596375eacSRoger Pau Monné 	}
218696375eacSRoger Pau Monné 
218796375eacSRoger Pau Monné 	while ((m = drbr_peek(ifp, br)) != NULL) {
218896375eacSRoger Pau Monné 		if (!xn_tx_slot_available(txq)) {
218996375eacSRoger Pau Monné 			drbr_putback(ifp, br, m);
219096375eacSRoger Pau Monné 			break;
219196375eacSRoger Pau Monné 		}
219296375eacSRoger Pau Monné 
219396375eacSRoger Pau Monné 		error = xn_assemble_tx_request(txq, m);
219496375eacSRoger Pau Monné 		/* xn_assemble_tx_request always consumes the mbuf*/
219596375eacSRoger Pau Monné 		if (error != 0) {
219696375eacSRoger Pau Monné 			drbr_advance(ifp, br);
219796375eacSRoger Pau Monné 			break;
219896375eacSRoger Pau Monné 		}
219996375eacSRoger Pau Monné 
220096375eacSRoger Pau Monné 		RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&txq->ring, notify);
220196375eacSRoger Pau Monné 		if (notify)
220296375eacSRoger Pau Monné 			xen_intr_signal(txq->xen_intr_handle);
220396375eacSRoger Pau Monné 
220496375eacSRoger Pau Monné 		drbr_advance(ifp, br);
220596375eacSRoger Pau Monné 	}
220696375eacSRoger Pau Monné 
220796375eacSRoger Pau Monné 	if (RING_FULL(&txq->ring))
220896375eacSRoger Pau Monné 		txq->full = true;
220996375eacSRoger Pau Monné 
221096375eacSRoger Pau Monné 	return (0);
221196375eacSRoger Pau Monné }
221296375eacSRoger Pau Monné 
221396375eacSRoger Pau Monné static int
221402f3b17fSJustin Hibbits xn_txq_mq_start(if_t ifp, struct mbuf *m)
221596375eacSRoger Pau Monné {
221696375eacSRoger Pau Monné 	struct netfront_info *np;
221796375eacSRoger Pau Monné 	struct netfront_txq *txq;
221896375eacSRoger Pau Monné 	int i, npairs, error;
221996375eacSRoger Pau Monné 
222002f3b17fSJustin Hibbits 	np = if_getsoftc(ifp);
222196375eacSRoger Pau Monné 	npairs = np->num_queues;
222296375eacSRoger Pau Monné 
2223339690b5SRoger Pau Monné 	if (!netfront_carrier_ok(np))
2224339690b5SRoger Pau Monné 		return (ENOBUFS);
2225339690b5SRoger Pau Monné 
2226c21b47d8SRoger Pau Monné 	KASSERT(npairs != 0, ("called with 0 available queues"));
2227c21b47d8SRoger Pau Monné 
222896375eacSRoger Pau Monné 	/* check if flowid is set */
222996375eacSRoger Pau Monné 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
223096375eacSRoger Pau Monné 		i = m->m_pkthdr.flowid % npairs;
223196375eacSRoger Pau Monné 	else
223296375eacSRoger Pau Monné 		i = curcpu % npairs;
223396375eacSRoger Pau Monné 
223496375eacSRoger Pau Monné 	txq = &np->txq[i];
223596375eacSRoger Pau Monné 
223696375eacSRoger Pau Monné 	if (XN_TX_TRYLOCK(txq) != 0) {
223796375eacSRoger Pau Monné 		error = xn_txq_mq_start_locked(txq, m);
223896375eacSRoger Pau Monné 		XN_TX_UNLOCK(txq);
223996375eacSRoger Pau Monné 	} else {
224096375eacSRoger Pau Monné 		error = drbr_enqueue(ifp, txq->br, m);
224196375eacSRoger Pau Monné 		taskqueue_enqueue(txq->tq, &txq->defrtask);
224296375eacSRoger Pau Monné 	}
224396375eacSRoger Pau Monné 
224496375eacSRoger Pau Monné 	return (error);
224596375eacSRoger Pau Monné }
224696375eacSRoger Pau Monné 
224796375eacSRoger Pau Monné static void
224802f3b17fSJustin Hibbits xn_qflush(if_t ifp)
224996375eacSRoger Pau Monné {
225096375eacSRoger Pau Monné 	struct netfront_info *np;
225196375eacSRoger Pau Monné 	struct netfront_txq *txq;
225296375eacSRoger Pau Monné 	struct mbuf *m;
225396375eacSRoger Pau Monné 	int i;
225496375eacSRoger Pau Monné 
225502f3b17fSJustin Hibbits 	np = if_getsoftc(ifp);
225696375eacSRoger Pau Monné 
225796375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
225896375eacSRoger Pau Monné 		txq = &np->txq[i];
225996375eacSRoger Pau Monné 
226096375eacSRoger Pau Monné 		XN_TX_LOCK(txq);
226196375eacSRoger Pau Monné 		while ((m = buf_ring_dequeue_sc(txq->br)) != NULL)
226296375eacSRoger Pau Monné 			m_freem(m);
226396375eacSRoger Pau Monné 		XN_TX_UNLOCK(txq);
226496375eacSRoger Pau Monné 	}
226596375eacSRoger Pau Monné 
226696375eacSRoger Pau Monné 	if_qflush(ifp);
226796375eacSRoger Pau Monné }
226896375eacSRoger Pau Monné 
226976acc41fSJustin T. Gibbs /**
227076acc41fSJustin T. Gibbs  * Create a network device.
227176acc41fSJustin T. Gibbs  * @param dev  Newbus device representing this virtual NIC.
227289e0f4d2SKip Macy  */
227323dc5621SKip Macy int
227423dc5621SKip Macy create_netdev(device_t dev)
227589e0f4d2SKip Macy {
227689e0f4d2SKip Macy 	struct netfront_info *np;
227789e0f4d2SKip Macy 	int err;
227802f3b17fSJustin Hibbits 	if_t ifp;
227989e0f4d2SKip Macy 
228023dc5621SKip Macy 	np = device_get_softc(dev);
228189e0f4d2SKip Macy 
228289e0f4d2SKip Macy 	np->xbdev         = dev;
228389e0f4d2SKip Macy 
2284177e3f13SRoger Pau Monné 	mtx_init(&np->sc_lock, "xnsc", "netfront softc lock", MTX_DEF);
22850e509842SJustin T. Gibbs 
22860e509842SJustin T. Gibbs 	ifmedia_init(&np->sc_media, 0, xn_ifmedia_upd, xn_ifmedia_sts);
22870e509842SJustin T. Gibbs 	ifmedia_add(&np->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
22880e509842SJustin T. Gibbs 	ifmedia_set(&np->sc_media, IFM_ETHER|IFM_MANUAL);
22890e509842SJustin T. Gibbs 
229089e0f4d2SKip Macy 	err = xen_net_read_mac(dev, np->mac);
229196375eacSRoger Pau Monné 	if (err != 0)
22921a2928b7SRoger Pau Monné 		goto error;
229389e0f4d2SKip Macy 
229489e0f4d2SKip Macy 	/* Set up ifnet structure */
229523dc5621SKip Macy 	ifp = np->xn_ifp = if_alloc(IFT_ETHER);
229602f3b17fSJustin Hibbits 	if_setsoftc(ifp, np);
229723dc5621SKip Macy 	if_initname(ifp, "xn",  device_get_unit(dev));
229802f3b17fSJustin Hibbits 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
229902f3b17fSJustin Hibbits 	if_setioctlfn(ifp, xn_ioctl);
230096375eacSRoger Pau Monné 
230102f3b17fSJustin Hibbits 	if_settransmitfn(ifp, xn_txq_mq_start);
230202f3b17fSJustin Hibbits 	if_setqflushfn(ifp, xn_qflush);
230396375eacSRoger Pau Monné 
230402f3b17fSJustin Hibbits 	if_setinitfn(ifp, xn_ifinit);
230589e0f4d2SKip Macy 
230602f3b17fSJustin Hibbits 	if_sethwassist(ifp, XN_CSUM_FEATURES);
2307c2d12e5eSRoger Pau Monné 	/* Enable all supported features at device creation. */
230802f3b17fSJustin Hibbits 	if_setcapabilities(ifp, IFCAP_HWCSUM|IFCAP_TSO4|IFCAP_LRO);
230902f3b17fSJustin Hibbits 	if_setcapenable(ifp, if_getcapabilities(ifp));
231002f3b17fSJustin Hibbits 
231102f3b17fSJustin Hibbits 	if_sethwtsomax(ifp, 65536 - (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN));
231202f3b17fSJustin Hibbits 	if_sethwtsomaxsegcount(ifp, MAX_TX_REQ_FRAGS);
231302f3b17fSJustin Hibbits 	if_sethwtsomaxsegsize(ifp, PAGE_SIZE);
231489e0f4d2SKip Macy 
231589e0f4d2SKip Macy 	ether_ifattach(ifp, np->mac);
231689e0f4d2SKip Macy 	netfront_carrier_off(np);
231789e0f4d2SKip Macy 
2318dabb3db7SRoger Pau Monné 	err = bus_dma_tag_create(
2319dabb3db7SRoger Pau Monné 	    bus_get_dma_tag(dev),		/* parent */
2320dabb3db7SRoger Pau Monné 	    1, PAGE_SIZE,			/* algnmnt, boundary */
2321dabb3db7SRoger Pau Monné 	    BUS_SPACE_MAXADDR,			/* lowaddr */
2322dabb3db7SRoger Pau Monné 	    BUS_SPACE_MAXADDR,			/* highaddr */
2323dabb3db7SRoger Pau Monné 	    NULL, NULL,				/* filter, filterarg */
2324dabb3db7SRoger Pau Monné 	    PAGE_SIZE * MAX_TX_REQ_FRAGS,	/* max request size */
2325dabb3db7SRoger Pau Monné 	    MAX_TX_REQ_FRAGS,			/* max segments */
2326dabb3db7SRoger Pau Monné 	    PAGE_SIZE,				/* maxsegsize */
2327dabb3db7SRoger Pau Monné 	    BUS_DMA_ALLOCNOW,			/* flags */
2328dabb3db7SRoger Pau Monné 	    NULL, NULL,				/* lockfunc, lockarg */
2329dabb3db7SRoger Pau Monné 	    &np->dma_tag);
2330dabb3db7SRoger Pau Monné 
2331dabb3db7SRoger Pau Monné 	return (err);
233289e0f4d2SKip Macy 
23331a2928b7SRoger Pau Monné error:
23341a2928b7SRoger Pau Monné 	KASSERT(err != 0, ("Error path with no error code specified"));
2335ffa06904SJustin T. Gibbs 	return (err);
233689e0f4d2SKip Macy }
233789e0f4d2SKip Macy 
23380e509842SJustin T. Gibbs static int
23390e509842SJustin T. Gibbs netfront_detach(device_t dev)
234089e0f4d2SKip Macy {
234123dc5621SKip Macy 	struct netfront_info *info = device_get_softc(dev);
234289e0f4d2SKip Macy 
234323dc5621SKip Macy 	DPRINTK("%s\n", xenbus_get_node(dev));
234489e0f4d2SKip Macy 
234589e0f4d2SKip Macy 	netif_free(info);
234689e0f4d2SKip Macy 
234789e0f4d2SKip Macy 	return 0;
234889e0f4d2SKip Macy }
234989e0f4d2SKip Macy 
23500e509842SJustin T. Gibbs static void
235196375eacSRoger Pau Monné netif_free(struct netfront_info *np)
235289e0f4d2SKip Macy {
235396375eacSRoger Pau Monné 
235496375eacSRoger Pau Monné 	XN_LOCK(np);
235596375eacSRoger Pau Monné 	xn_stop(np);
235696375eacSRoger Pau Monné 	XN_UNLOCK(np);
235796375eacSRoger Pau Monné 	netif_disconnect_backend(np);
2358c21b47d8SRoger Pau Monné 	ether_ifdetach(np->xn_ifp);
235996375eacSRoger Pau Monné 	free(np->rxq, M_DEVBUF);
236096375eacSRoger Pau Monné 	free(np->txq, M_DEVBUF);
236196375eacSRoger Pau Monné 	if_free(np->xn_ifp);
236296375eacSRoger Pau Monné 	np->xn_ifp = NULL;
236396375eacSRoger Pau Monné 	ifmedia_removeall(&np->sc_media);
2364dabb3db7SRoger Pau Monné 	bus_dma_tag_destroy(np->dma_tag);
236589e0f4d2SKip Macy }
236689e0f4d2SKip Macy 
23670e509842SJustin T. Gibbs static void
236896375eacSRoger Pau Monné netif_disconnect_backend(struct netfront_info *np)
236989e0f4d2SKip Macy {
237096375eacSRoger Pau Monné 	u_int i;
23713a6d1fcfSKip Macy 
237296375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
237396375eacSRoger Pau Monné 		XN_RX_LOCK(&np->rxq[i]);
237496375eacSRoger Pau Monné 		XN_TX_LOCK(&np->txq[i]);
237596375eacSRoger Pau Monné 	}
237696375eacSRoger Pau Monné 	netfront_carrier_off(np);
237796375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
237896375eacSRoger Pau Monné 		XN_RX_UNLOCK(&np->rxq[i]);
237996375eacSRoger Pau Monné 		XN_TX_UNLOCK(&np->txq[i]);
238089e0f4d2SKip Macy 	}
238189e0f4d2SKip Macy 
238296375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
238396375eacSRoger Pau Monné 		disconnect_rxq(&np->rxq[i]);
238496375eacSRoger Pau Monné 		disconnect_txq(&np->txq[i]);
2385cf9c09e1SJustin T. Gibbs 	}
238689e0f4d2SKip Macy }
238789e0f4d2SKip Macy 
23880e509842SJustin T. Gibbs static int
238902f3b17fSJustin Hibbits xn_ifmedia_upd(if_t ifp)
23900e509842SJustin T. Gibbs {
239196375eacSRoger Pau Monné 
23920e509842SJustin T. Gibbs 	return (0);
23930e509842SJustin T. Gibbs }
23940e509842SJustin T. Gibbs 
23950e509842SJustin T. Gibbs static void
239602f3b17fSJustin Hibbits xn_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
23970e509842SJustin T. Gibbs {
239896375eacSRoger Pau Monné 
23990e509842SJustin T. Gibbs 	ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE;
24000e509842SJustin T. Gibbs 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
24010e509842SJustin T. Gibbs }
24020e509842SJustin T. Gibbs 
240389e0f4d2SKip Macy /* ** Driver registration ** */
240423dc5621SKip Macy static device_method_t netfront_methods[] = {
240523dc5621SKip Macy 	/* Device interface */
240623dc5621SKip Macy 	DEVMETHOD(device_probe,         netfront_probe),
240723dc5621SKip Macy 	DEVMETHOD(device_attach,        netfront_attach),
240823dc5621SKip Macy 	DEVMETHOD(device_detach,        netfront_detach),
240923dc5621SKip Macy 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
2410cf9c09e1SJustin T. Gibbs 	DEVMETHOD(device_suspend,       netfront_suspend),
241123dc5621SKip Macy 	DEVMETHOD(device_resume,        netfront_resume),
241289e0f4d2SKip Macy 
241323dc5621SKip Macy 	/* Xenbus interface */
2414ff662b5cSJustin T. Gibbs 	DEVMETHOD(xenbus_otherend_changed, netfront_backend_changed),
241589e0f4d2SKip Macy 
24166f9767acSMarius Strobl 	DEVMETHOD_END
241789e0f4d2SKip Macy };
241889e0f4d2SKip Macy 
241923dc5621SKip Macy static driver_t netfront_driver = {
242023dc5621SKip Macy 	"xn",
242123dc5621SKip Macy 	netfront_methods,
242223dc5621SKip Macy 	sizeof(struct netfront_info),
242389e0f4d2SKip Macy };
242489e0f4d2SKip Macy 
2425f929eb1eSJohn Baldwin DRIVER_MODULE(xe, xenbusb_front, netfront_driver, NULL, NULL);
2426