xref: /freebsd/sys/dev/xen/netfront/netfront.c (revision b2fd6999db269fd54228068dd0a719f86c4e002d)
18e0ad55aSJoel Dahl /*-
289e0f4d2SKip Macy  * Copyright (c) 2004-2006 Kip Macy
396375eacSRoger Pau Monné  * Copyright (c) 2015 Wei Liu <wei.liu2@citrix.com>
489e0f4d2SKip Macy  * All rights reserved.
589e0f4d2SKip Macy  *
68e0ad55aSJoel Dahl  * Redistribution and use in source and binary forms, with or without
78e0ad55aSJoel Dahl  * modification, are permitted provided that the following conditions
88e0ad55aSJoel Dahl  * are met:
98e0ad55aSJoel Dahl  * 1. Redistributions of source code must retain the above copyright
108e0ad55aSJoel Dahl  *    notice, this list of conditions and the following disclaimer.
118e0ad55aSJoel Dahl  * 2. Redistributions in binary form must reproduce the above copyright
128e0ad55aSJoel Dahl  *    notice, this list of conditions and the following disclaimer in the
138e0ad55aSJoel Dahl  *    documentation and/or other materials provided with the distribution.
1489e0f4d2SKip Macy  *
158e0ad55aSJoel Dahl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
168e0ad55aSJoel Dahl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
178e0ad55aSJoel Dahl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
188e0ad55aSJoel Dahl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
198e0ad55aSJoel Dahl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
208e0ad55aSJoel Dahl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
218e0ad55aSJoel Dahl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
228e0ad55aSJoel Dahl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
238e0ad55aSJoel Dahl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
248e0ad55aSJoel Dahl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
258e0ad55aSJoel Dahl  * SUCH DAMAGE.
2689e0f4d2SKip Macy  */
2789e0f4d2SKip Macy 
2889e0f4d2SKip Macy #include <sys/cdefs.h>
2989e0f4d2SKip Macy __FBSDID("$FreeBSD$");
3089e0f4d2SKip Macy 
31a0ae8f04SBjoern A. Zeeb #include "opt_inet.h"
32f909bbb4SGleb Smirnoff #include "opt_inet6.h"
33a0ae8f04SBjoern A. Zeeb 
3489e0f4d2SKip Macy #include <sys/param.h>
3589e0f4d2SKip Macy #include <sys/sockio.h>
36c578b6acSGleb Smirnoff #include <sys/limits.h>
3789e0f4d2SKip Macy #include <sys/mbuf.h>
3889e0f4d2SKip Macy #include <sys/malloc.h>
3923dc5621SKip Macy #include <sys/module.h>
4089e0f4d2SKip Macy #include <sys/kernel.h>
4189e0f4d2SKip Macy #include <sys/socket.h>
4212678024SDoug Rabson #include <sys/sysctl.h>
4396375eacSRoger Pau Monné #include <sys/taskqueue.h>
4489e0f4d2SKip Macy 
4589e0f4d2SKip Macy #include <net/if.h>
4677374386SGleb Smirnoff #include <net/if_var.h>
4789e0f4d2SKip Macy #include <net/if_arp.h>
4889e0f4d2SKip Macy #include <net/ethernet.h>
4989e0f4d2SKip Macy #include <net/if_media.h>
5089e0f4d2SKip Macy #include <net/bpf.h>
5189e0f4d2SKip Macy #include <net/if_types.h>
5289e0f4d2SKip Macy 
5389e0f4d2SKip Macy #include <netinet/in.h>
5489e0f4d2SKip Macy #include <netinet/ip.h>
5589e0f4d2SKip Macy #include <netinet/if_ether.h>
5612678024SDoug Rabson #include <netinet/tcp.h>
5712678024SDoug Rabson #include <netinet/tcp_lro.h>
5889e0f4d2SKip Macy 
5989e0f4d2SKip Macy #include <vm/vm.h>
6089e0f4d2SKip Macy #include <vm/pmap.h>
6189e0f4d2SKip Macy 
6289e0f4d2SKip Macy #include <sys/bus.h>
6389e0f4d2SKip Macy 
6476acc41fSJustin T. Gibbs #include <xen/xen-os.h>
653a6d1fcfSKip Macy #include <xen/hypervisor.h>
663a6d1fcfSKip Macy #include <xen/xen_intr.h>
6789e0f4d2SKip Macy #include <xen/gnttab.h>
6889e0f4d2SKip Macy #include <xen/interface/memory.h>
6989e0f4d2SKip Macy #include <xen/interface/io/netif.h>
7023dc5621SKip Macy #include <xen/xenbus/xenbusvar.h>
7189e0f4d2SKip Macy 
7223dc5621SKip Macy #include "xenbus_if.h"
7389e0f4d2SKip Macy 
74578e4bf7SJustin T. Gibbs /* Features supported by all backends.  TSO and LRO can be negotiated */
75578e4bf7SJustin T. Gibbs #define XN_CSUM_FEATURES	(CSUM_TCP | CSUM_UDP)
7612678024SDoug Rabson 
7789e0f4d2SKip Macy #define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE)
7889e0f4d2SKip Macy #define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE)
7989e0f4d2SKip Macy 
802568ee67SRoger Pau Monné #define NET_RX_SLOTS_MIN (XEN_NETIF_NR_SLOTS_MIN + 1)
812568ee67SRoger Pau Monné 
8212678024SDoug Rabson /*
8312678024SDoug Rabson  * Should the driver do LRO on the RX end
8412678024SDoug Rabson  *  this can be toggled on the fly, but the
8512678024SDoug Rabson  *  interface must be reset (down/up) for it
8612678024SDoug Rabson  *  to take effect.
8712678024SDoug Rabson  */
8812678024SDoug Rabson static int xn_enable_lro = 1;
8912678024SDoug Rabson TUNABLE_INT("hw.xn.enable_lro", &xn_enable_lro);
9012678024SDoug Rabson 
9196375eacSRoger Pau Monné /*
9296375eacSRoger Pau Monné  * Number of pairs of queues.
9396375eacSRoger Pau Monné  */
9496375eacSRoger Pau Monné static unsigned long xn_num_queues = 4;
9596375eacSRoger Pau Monné TUNABLE_ULONG("hw.xn.num_queues", &xn_num_queues);
9696375eacSRoger Pau Monné 
97931eeffaSKenneth D. Merry /**
98931eeffaSKenneth D. Merry  * \brief The maximum allowed data fragments in a single transmit
99931eeffaSKenneth D. Merry  *        request.
100931eeffaSKenneth D. Merry  *
101931eeffaSKenneth D. Merry  * This limit is imposed by the backend driver.  We assume here that
102931eeffaSKenneth D. Merry  * we are dealing with a Linux driver domain and have set our limit
103931eeffaSKenneth D. Merry  * to mirror the Linux MAX_SKB_FRAGS constant.
104931eeffaSKenneth D. Merry  */
105931eeffaSKenneth D. Merry #define	MAX_TX_REQ_FRAGS (65536 / PAGE_SIZE + 2)
106931eeffaSKenneth D. Merry 
10789e0f4d2SKip Macy #define RX_COPY_THRESHOLD 256
10889e0f4d2SKip Macy 
10989e0f4d2SKip Macy #define net_ratelimit() 0
11089e0f4d2SKip Macy 
11196375eacSRoger Pau Monné struct netfront_rxq;
11296375eacSRoger Pau Monné struct netfront_txq;
11389e0f4d2SKip Macy struct netfront_info;
11489e0f4d2SKip Macy struct netfront_rx_info;
11589e0f4d2SKip Macy 
11696375eacSRoger Pau Monné static void xn_txeof(struct netfront_txq *);
11796375eacSRoger Pau Monné static void xn_rxeof(struct netfront_rxq *);
11896375eacSRoger Pau Monné static void xn_alloc_rx_buffers(struct netfront_rxq *);
1192568ee67SRoger Pau Monné static void xn_alloc_rx_buffers_callout(void *arg);
12089e0f4d2SKip Macy 
12196375eacSRoger Pau Monné static void xn_release_rx_bufs(struct netfront_rxq *);
12296375eacSRoger Pau Monné static void xn_release_tx_bufs(struct netfront_txq *);
12389e0f4d2SKip Macy 
124da695b05SRoger Pau Monné static void xn_rxq_intr(struct netfront_rxq *);
125da695b05SRoger Pau Monné static void xn_txq_intr(struct netfront_txq *);
126da695b05SRoger Pau Monné static void xn_intr(void *);
127931eeffaSKenneth D. Merry static inline int xn_count_frags(struct mbuf *m);
12896375eacSRoger Pau Monné static int xn_assemble_tx_request(struct netfront_txq *, struct mbuf *);
12989e0f4d2SKip Macy static int xn_ioctl(struct ifnet *, u_long, caddr_t);
13089e0f4d2SKip Macy static void xn_ifinit_locked(struct netfront_info *);
13189e0f4d2SKip Macy static void xn_ifinit(void *);
13289e0f4d2SKip Macy static void xn_stop(struct netfront_info *);
133578e4bf7SJustin T. Gibbs static void xn_query_features(struct netfront_info *np);
134578e4bf7SJustin T. Gibbs static int xn_configure_features(struct netfront_info *np);
13589e0f4d2SKip Macy static void netif_free(struct netfront_info *info);
13623dc5621SKip Macy static int netfront_detach(device_t dev);
13789e0f4d2SKip Macy 
13896375eacSRoger Pau Monné static int xn_txq_mq_start_locked(struct netfront_txq *, struct mbuf *);
13996375eacSRoger Pau Monné static int xn_txq_mq_start(struct ifnet *, struct mbuf *);
14096375eacSRoger Pau Monné 
14123dc5621SKip Macy static int talk_to_backend(device_t dev, struct netfront_info *info);
14223dc5621SKip Macy static int create_netdev(device_t dev);
14389e0f4d2SKip Macy static void netif_disconnect_backend(struct netfront_info *info);
14496375eacSRoger Pau Monné static int setup_device(device_t dev, struct netfront_info *info,
14596375eacSRoger Pau Monné     unsigned long);
1460e509842SJustin T. Gibbs static int xn_ifmedia_upd(struct ifnet *ifp);
1470e509842SJustin T. Gibbs static void xn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
1480e509842SJustin T. Gibbs 
14965671253SRoger Pau Monné static int xn_connect(struct netfront_info *);
15065671253SRoger Pau Monné static void xn_kick_rings(struct netfront_info *);
15189e0f4d2SKip Macy 
15296375eacSRoger Pau Monné static int xn_get_responses(struct netfront_rxq *,
15396375eacSRoger Pau Monné     struct netfront_rx_info *, RING_IDX, RING_IDX *,
15496375eacSRoger Pau Monné     struct mbuf **);
15589e0f4d2SKip Macy 
1563c790178SJohn Baldwin #define virt_to_mfn(x) (vtophys(x) >> PAGE_SHIFT)
15789e0f4d2SKip Macy 
15889e0f4d2SKip Macy #define INVALID_P2M_ENTRY (~0UL)
15996375eacSRoger Pau Monné #define XN_QUEUE_NAME_LEN  8	/* xn{t,r}x_%u, allow for two digits */
16096375eacSRoger Pau Monné struct netfront_rxq {
16196375eacSRoger Pau Monné 	struct netfront_info 	*info;
16296375eacSRoger Pau Monné 	u_int			id;
16396375eacSRoger Pau Monné 	char			name[XN_QUEUE_NAME_LEN];
16496375eacSRoger Pau Monné 	struct mtx		lock;
16596375eacSRoger Pau Monné 
16696375eacSRoger Pau Monné 	int			ring_ref;
16796375eacSRoger Pau Monné 	netif_rx_front_ring_t 	ring;
16896375eacSRoger Pau Monné 	xen_intr_handle_t	xen_intr_handle;
16996375eacSRoger Pau Monné 
17096375eacSRoger Pau Monné 	grant_ref_t 		gref_head;
17196375eacSRoger Pau Monné 	grant_ref_t 		grant_ref[NET_TX_RING_SIZE + 1];
17296375eacSRoger Pau Monné 
17396375eacSRoger Pau Monné 	struct mbuf		*mbufs[NET_RX_RING_SIZE + 1];
17496375eacSRoger Pau Monné 
17596375eacSRoger Pau Monné 	struct lro_ctrl		lro;
17696375eacSRoger Pau Monné 
1772568ee67SRoger Pau Monné 	struct callout		rx_refill;
17896375eacSRoger Pau Monné };
17996375eacSRoger Pau Monné 
18096375eacSRoger Pau Monné struct netfront_txq {
18196375eacSRoger Pau Monné 	struct netfront_info 	*info;
18296375eacSRoger Pau Monné 	u_int 			id;
18396375eacSRoger Pau Monné 	char			name[XN_QUEUE_NAME_LEN];
18496375eacSRoger Pau Monné 	struct mtx		lock;
18596375eacSRoger Pau Monné 
18696375eacSRoger Pau Monné 	int			ring_ref;
18796375eacSRoger Pau Monné 	netif_tx_front_ring_t	ring;
18896375eacSRoger Pau Monné 	xen_intr_handle_t 	xen_intr_handle;
18996375eacSRoger Pau Monné 
19096375eacSRoger Pau Monné 	grant_ref_t		gref_head;
19196375eacSRoger Pau Monné 	grant_ref_t		grant_ref[NET_TX_RING_SIZE + 1];
19296375eacSRoger Pau Monné 
19396375eacSRoger Pau Monné 	struct mbuf		*mbufs[NET_TX_RING_SIZE + 1];
19496375eacSRoger Pau Monné 	int			mbufs_cnt;
19596375eacSRoger Pau Monné 	struct buf_ring		*br;
19696375eacSRoger Pau Monné 
19796375eacSRoger Pau Monné 	struct taskqueue 	*tq;
19896375eacSRoger Pau Monné 	struct task       	defrtask;
19996375eacSRoger Pau Monné 
20096375eacSRoger Pau Monné 	bool			full;
20196375eacSRoger Pau Monné };
20296375eacSRoger Pau Monné 
20389e0f4d2SKip Macy struct netfront_info {
20489e0f4d2SKip Macy 	struct ifnet 		*xn_ifp;
20589e0f4d2SKip Macy 
206227ca257SKip Macy 	struct mtx   		sc_lock;
20789e0f4d2SKip Macy 
20896375eacSRoger Pau Monné 	u_int  num_queues;
20996375eacSRoger Pau Monné 	struct netfront_rxq 	*rxq;
21096375eacSRoger Pau Monné 	struct netfront_txq 	*txq;
21196375eacSRoger Pau Monné 
21289e0f4d2SKip Macy 	u_int			carrier;
213578e4bf7SJustin T. Gibbs 	u_int			maxfrags;
21489e0f4d2SKip Macy 
21523dc5621SKip Macy 	device_t		xbdev;
21689e0f4d2SKip Macy 	uint8_t			mac[ETHER_ADDR_LEN];
21789e0f4d2SKip Macy 
21889e0f4d2SKip Macy 	int			xn_if_flags;
21989e0f4d2SKip Macy 
2200e509842SJustin T. Gibbs 	struct ifmedia		sc_media;
2216a8e9695SRoger Pau Monné 
222c2d12e5eSRoger Pau Monné 	bool			xn_reset;
22389e0f4d2SKip Macy };
22489e0f4d2SKip Macy 
22596375eacSRoger Pau Monné struct netfront_rx_info {
22696375eacSRoger Pau Monné 	struct netif_rx_response rx;
22796375eacSRoger Pau Monné 	struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
22896375eacSRoger Pau Monné };
22989e0f4d2SKip Macy 
23096375eacSRoger Pau Monné #define XN_RX_LOCK(_q)         mtx_lock(&(_q)->lock)
23196375eacSRoger Pau Monné #define XN_RX_UNLOCK(_q)       mtx_unlock(&(_q)->lock)
23289e0f4d2SKip Macy 
23396375eacSRoger Pau Monné #define XN_TX_LOCK(_q)         mtx_lock(&(_q)->lock)
23496375eacSRoger Pau Monné #define XN_TX_TRYLOCK(_q)      mtx_trylock(&(_q)->lock)
23596375eacSRoger Pau Monné #define XN_TX_UNLOCK(_q)       mtx_unlock(&(_q)->lock)
23689e0f4d2SKip Macy 
237227ca257SKip Macy #define XN_LOCK(_sc)           mtx_lock(&(_sc)->sc_lock);
238227ca257SKip Macy #define XN_UNLOCK(_sc)         mtx_unlock(&(_sc)->sc_lock);
23989e0f4d2SKip Macy 
240227ca257SKip Macy #define XN_LOCK_ASSERT(_sc)    mtx_assert(&(_sc)->sc_lock, MA_OWNED);
24196375eacSRoger Pau Monné #define XN_RX_LOCK_ASSERT(_q)  mtx_assert(&(_q)->lock, MA_OWNED);
24296375eacSRoger Pau Monné #define XN_TX_LOCK_ASSERT(_q)  mtx_assert(&(_q)->lock, MA_OWNED);
24389e0f4d2SKip Macy 
24489e0f4d2SKip Macy #define netfront_carrier_on(netif)	((netif)->carrier = 1)
24589e0f4d2SKip Macy #define netfront_carrier_off(netif)	((netif)->carrier = 0)
24689e0f4d2SKip Macy #define netfront_carrier_ok(netif)	((netif)->carrier)
24789e0f4d2SKip Macy 
24889e0f4d2SKip Macy /* Access macros for acquiring freeing slots in xn_free_{tx,rx}_idxs[]. */
24989e0f4d2SKip Macy 
25089e0f4d2SKip Macy static inline void
251931eeffaSKenneth D. Merry add_id_to_freelist(struct mbuf **list, uintptr_t id)
25289e0f4d2SKip Macy {
25396375eacSRoger Pau Monné 
254931eeffaSKenneth D. Merry 	KASSERT(id != 0,
255931eeffaSKenneth D. Merry 		("%s: the head item (0) must always be free.", __func__));
25689e0f4d2SKip Macy 	list[id] = list[0];
257931eeffaSKenneth D. Merry 	list[0]  = (struct mbuf *)id;
25889e0f4d2SKip Macy }
25989e0f4d2SKip Macy 
26089e0f4d2SKip Macy static inline unsigned short
26189e0f4d2SKip Macy get_id_from_freelist(struct mbuf **list)
26289e0f4d2SKip Macy {
263931eeffaSKenneth D. Merry 	uintptr_t id;
264931eeffaSKenneth D. Merry 
265931eeffaSKenneth D. Merry 	id = (uintptr_t)list[0];
266931eeffaSKenneth D. Merry 	KASSERT(id != 0,
267931eeffaSKenneth D. Merry 		("%s: the head item (0) must always remain free.", __func__));
26889e0f4d2SKip Macy 	list[0] = list[id];
26989e0f4d2SKip Macy 	return (id);
27089e0f4d2SKip Macy }
27189e0f4d2SKip Macy 
27289e0f4d2SKip Macy static inline int
27396375eacSRoger Pau Monné xn_rxidx(RING_IDX idx)
27489e0f4d2SKip Macy {
27596375eacSRoger Pau Monné 
27689e0f4d2SKip Macy 	return idx & (NET_RX_RING_SIZE - 1);
27789e0f4d2SKip Macy }
27889e0f4d2SKip Macy 
27989e0f4d2SKip Macy static inline struct mbuf *
28096375eacSRoger Pau Monné xn_get_rx_mbuf(struct netfront_rxq *rxq, RING_IDX ri)
28189e0f4d2SKip Macy {
28296375eacSRoger Pau Monné 	int i;
28389e0f4d2SKip Macy 	struct mbuf *m;
28489e0f4d2SKip Macy 
28596375eacSRoger Pau Monné 	i = xn_rxidx(ri);
28696375eacSRoger Pau Monné 	m = rxq->mbufs[i];
28796375eacSRoger Pau Monné 	rxq->mbufs[i] = NULL;
28889e0f4d2SKip Macy 	return (m);
28989e0f4d2SKip Macy }
29089e0f4d2SKip Macy 
29189e0f4d2SKip Macy static inline grant_ref_t
29296375eacSRoger Pau Monné xn_get_rx_ref(struct netfront_rxq *rxq, RING_IDX ri)
29389e0f4d2SKip Macy {
29496375eacSRoger Pau Monné 	int i = xn_rxidx(ri);
29596375eacSRoger Pau Monné 	grant_ref_t ref = rxq->grant_ref[i];
29696375eacSRoger Pau Monné 
297ff662b5cSJustin T. Gibbs 	KASSERT(ref != GRANT_REF_INVALID, ("Invalid grant reference!\n"));
29896375eacSRoger Pau Monné 	rxq->grant_ref[i] = GRANT_REF_INVALID;
29996375eacSRoger Pau Monné 	return (ref);
30089e0f4d2SKip Macy }
30189e0f4d2SKip Macy 
30289e0f4d2SKip Macy #define IPRINTK(fmt, args...) \
30389e0f4d2SKip Macy     printf("[XEN] " fmt, ##args)
304227ca257SKip Macy #ifdef INVARIANTS
30589e0f4d2SKip Macy #define WPRINTK(fmt, args...) \
30689e0f4d2SKip Macy     printf("[XEN] " fmt, ##args)
307227ca257SKip Macy #else
308227ca257SKip Macy #define WPRINTK(fmt, args...)
309227ca257SKip Macy #endif
310227ca257SKip Macy #ifdef DEBUG
31189e0f4d2SKip Macy #define DPRINTK(fmt, args...) \
31223dc5621SKip Macy     printf("[XEN] %s: " fmt, __func__, ##args)
31312678024SDoug Rabson #else
31412678024SDoug Rabson #define DPRINTK(fmt, args...)
31512678024SDoug Rabson #endif
31689e0f4d2SKip Macy 
31789e0f4d2SKip Macy /**
31889e0f4d2SKip Macy  * Read the 'mac' node at the given device's node in the store, and parse that
31989e0f4d2SKip Macy  * as colon-separated octets, placing result the given mac array.  mac must be
32089e0f4d2SKip Macy  * a preallocated array of length ETH_ALEN (as declared in linux/if_ether.h).
32189e0f4d2SKip Macy  * Return 0 on success, or errno on error.
32289e0f4d2SKip Macy  */
32389e0f4d2SKip Macy static int
32423dc5621SKip Macy xen_net_read_mac(device_t dev, uint8_t mac[])
32589e0f4d2SKip Macy {
3263a6d1fcfSKip Macy 	int error, i;
3273a6d1fcfSKip Macy 	char *s, *e, *macstr;
328ffa06904SJustin T. Gibbs 	const char *path;
3293a6d1fcfSKip Macy 
330ffa06904SJustin T. Gibbs 	path = xenbus_get_node(dev);
331ffa06904SJustin T. Gibbs 	error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr);
332ffa06904SJustin T. Gibbs 	if (error == ENOENT) {
333ffa06904SJustin T. Gibbs 		/*
334ffa06904SJustin T. Gibbs 		 * Deal with missing mac XenStore nodes on devices with
335ffa06904SJustin T. Gibbs 		 * HVM emulation (the 'ioemu' configuration attribute)
336ffa06904SJustin T. Gibbs 		 * enabled.
337ffa06904SJustin T. Gibbs 		 *
338ffa06904SJustin T. Gibbs 		 * The HVM emulator may execute in a stub device model
339ffa06904SJustin T. Gibbs 		 * domain which lacks the permission, only given to Dom0,
340ffa06904SJustin T. Gibbs 		 * to update the guest's XenStore tree.  For this reason,
341ffa06904SJustin T. Gibbs 		 * the HVM emulator doesn't even attempt to write the
342ffa06904SJustin T. Gibbs 		 * front-side mac node, even when operating in Dom0.
343ffa06904SJustin T. Gibbs 		 * However, there should always be a mac listed in the
344ffa06904SJustin T. Gibbs 		 * backend tree.  Fallback to this version if our query
345ffa06904SJustin T. Gibbs 		 * of the front side XenStore location doesn't find
346ffa06904SJustin T. Gibbs 		 * anything.
347ffa06904SJustin T. Gibbs 		 */
348ffa06904SJustin T. Gibbs 		path = xenbus_get_otherend_path(dev);
349ffa06904SJustin T. Gibbs 		error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr);
350ffa06904SJustin T. Gibbs 	}
351ffa06904SJustin T. Gibbs 	if (error != 0) {
352ffa06904SJustin T. Gibbs 		xenbus_dev_fatal(dev, error, "parsing %s/mac", path);
3533a6d1fcfSKip Macy 		return (error);
354ffa06904SJustin T. Gibbs 	}
3553a6d1fcfSKip Macy 
35689e0f4d2SKip Macy 	s = macstr;
35789e0f4d2SKip Macy 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
35889e0f4d2SKip Macy 		mac[i] = strtoul(s, &e, 16);
35989e0f4d2SKip Macy 		if (s == e || (e[0] != ':' && e[0] != 0)) {
360ff662b5cSJustin T. Gibbs 			free(macstr, M_XENBUS);
3613a6d1fcfSKip Macy 			return (ENOENT);
36289e0f4d2SKip Macy 		}
36389e0f4d2SKip Macy 		s = &e[1];
36489e0f4d2SKip Macy 	}
365ff662b5cSJustin T. Gibbs 	free(macstr, M_XENBUS);
3663a6d1fcfSKip Macy 	return (0);
36789e0f4d2SKip Macy }
36889e0f4d2SKip Macy 
36989e0f4d2SKip Macy /**
37089e0f4d2SKip Macy  * Entry point to this code when a new device is created.  Allocate the basic
37189e0f4d2SKip Macy  * structures and the ring buffers for communication with the backend, and
37289e0f4d2SKip Macy  * inform the backend of the appropriate details for those.  Switch to
37389e0f4d2SKip Macy  * Connected state.
37489e0f4d2SKip Macy  */
37589e0f4d2SKip Macy static int
37623dc5621SKip Macy netfront_probe(device_t dev)
37723dc5621SKip Macy {
37823dc5621SKip Macy 
379f8f1bb83SRoger Pau Monné 	if (xen_hvm_domain() && xen_disable_pv_nics != 0)
380f8f1bb83SRoger Pau Monné 		return (ENXIO);
381f8f1bb83SRoger Pau Monné 
38223dc5621SKip Macy 	if (!strcmp(xenbus_get_type(dev), "vif")) {
38323dc5621SKip Macy 		device_set_desc(dev, "Virtual Network Interface");
38423dc5621SKip Macy 		return (0);
38523dc5621SKip Macy 	}
38623dc5621SKip Macy 
38723dc5621SKip Macy 	return (ENXIO);
38823dc5621SKip Macy }
38923dc5621SKip Macy 
39023dc5621SKip Macy static int
39123dc5621SKip Macy netfront_attach(device_t dev)
39289e0f4d2SKip Macy {
39389e0f4d2SKip Macy 	int err;
39489e0f4d2SKip Macy 
39523dc5621SKip Macy 	err = create_netdev(dev);
39696375eacSRoger Pau Monné 	if (err != 0) {
39789e0f4d2SKip Macy 		xenbus_dev_fatal(dev, err, "creating netdev");
398ffa06904SJustin T. Gibbs 		return (err);
39989e0f4d2SKip Macy 	}
40089e0f4d2SKip Macy 
40112678024SDoug Rabson 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
40212678024SDoug Rabson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
403f0188618SHans Petter Selasky 	    OID_AUTO, "enable_lro", CTLFLAG_RW,
40412678024SDoug Rabson 	    &xn_enable_lro, 0, "Large Receive Offload");
40512678024SDoug Rabson 
40696375eacSRoger Pau Monné 	SYSCTL_ADD_ULONG(device_get_sysctl_ctx(dev),
40796375eacSRoger Pau Monné 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
40896375eacSRoger Pau Monné 	    OID_AUTO, "num_queues", CTLFLAG_RD,
40996375eacSRoger Pau Monné 	    &xn_num_queues, "Number of pairs of queues");
41096375eacSRoger Pau Monné 
411ffa06904SJustin T. Gibbs 	return (0);
41289e0f4d2SKip Macy }
41389e0f4d2SKip Macy 
414cf9c09e1SJustin T. Gibbs static int
415cf9c09e1SJustin T. Gibbs netfront_suspend(device_t dev)
416cf9c09e1SJustin T. Gibbs {
41796375eacSRoger Pau Monné 	struct netfront_info *np = device_get_softc(dev);
41896375eacSRoger Pau Monné 	u_int i;
419cf9c09e1SJustin T. Gibbs 
42096375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
42196375eacSRoger Pau Monné 		XN_RX_LOCK(&np->rxq[i]);
42296375eacSRoger Pau Monné 		XN_TX_LOCK(&np->txq[i]);
42396375eacSRoger Pau Monné 	}
42496375eacSRoger Pau Monné 	netfront_carrier_off(np);
42596375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
42696375eacSRoger Pau Monné 		XN_RX_UNLOCK(&np->rxq[i]);
42796375eacSRoger Pau Monné 		XN_TX_UNLOCK(&np->txq[i]);
42896375eacSRoger Pau Monné 	}
429cf9c09e1SJustin T. Gibbs 	return (0);
430cf9c09e1SJustin T. Gibbs }
43189e0f4d2SKip Macy 
43289e0f4d2SKip Macy /**
43389e0f4d2SKip Macy  * We are reconnecting to the backend, due to a suspend/resume, or a backend
43489e0f4d2SKip Macy  * driver restart.  We tear down our netif structure and recreate it, but
43589e0f4d2SKip Macy  * leave the device-layer structures intact so that this is transparent to the
43689e0f4d2SKip Macy  * rest of the kernel.
43789e0f4d2SKip Macy  */
43889e0f4d2SKip Macy static int
43923dc5621SKip Macy netfront_resume(device_t dev)
44089e0f4d2SKip Macy {
44123dc5621SKip Macy 	struct netfront_info *info = device_get_softc(dev);
44289e0f4d2SKip Macy 
44389e0f4d2SKip Macy 	netif_disconnect_backend(info);
44489e0f4d2SKip Macy 	return (0);
44589e0f4d2SKip Macy }
44689e0f4d2SKip Macy 
44796375eacSRoger Pau Monné static int
44896375eacSRoger Pau Monné write_queue_xenstore_keys(device_t dev,
44996375eacSRoger Pau Monné     struct netfront_rxq *rxq,
45096375eacSRoger Pau Monné     struct netfront_txq *txq,
45196375eacSRoger Pau Monné     struct xs_transaction *xst, bool hierarchy)
45296375eacSRoger Pau Monné {
45396375eacSRoger Pau Monné 	int err;
45496375eacSRoger Pau Monné 	const char *message;
45596375eacSRoger Pau Monné 	const char *node = xenbus_get_node(dev);
45696375eacSRoger Pau Monné 	char *path;
45796375eacSRoger Pau Monné 	size_t path_size;
45896375eacSRoger Pau Monné 
45996375eacSRoger Pau Monné 	KASSERT(rxq->id == txq->id, ("Mismatch between RX and TX queue ids"));
46096375eacSRoger Pau Monné 	/* Split event channel support is not yet there. */
46196375eacSRoger Pau Monné 	KASSERT(rxq->xen_intr_handle == txq->xen_intr_handle,
46296375eacSRoger Pau Monné 	    ("Split event channels are not supported"));
46396375eacSRoger Pau Monné 
46496375eacSRoger Pau Monné 	if (hierarchy) {
46596375eacSRoger Pau Monné 		path_size = strlen(node) + 10;
46696375eacSRoger Pau Monné 		path = malloc(path_size, M_DEVBUF, M_WAITOK|M_ZERO);
46796375eacSRoger Pau Monné 		snprintf(path, path_size, "%s/queue-%u", node, rxq->id);
46896375eacSRoger Pau Monné 	} else {
46996375eacSRoger Pau Monné 		path_size = strlen(node) + 1;
47096375eacSRoger Pau Monné 		path = malloc(path_size, M_DEVBUF, M_WAITOK|M_ZERO);
47196375eacSRoger Pau Monné 		snprintf(path, path_size, "%s", node);
47296375eacSRoger Pau Monné 	}
47396375eacSRoger Pau Monné 
47496375eacSRoger Pau Monné 	err = xs_printf(*xst, path, "tx-ring-ref","%u", txq->ring_ref);
47596375eacSRoger Pau Monné 	if (err != 0) {
47696375eacSRoger Pau Monné 		message = "writing tx ring-ref";
47796375eacSRoger Pau Monné 		goto error;
47896375eacSRoger Pau Monné 	}
47996375eacSRoger Pau Monné 	err = xs_printf(*xst, path, "rx-ring-ref","%u", rxq->ring_ref);
48096375eacSRoger Pau Monné 	if (err != 0) {
48196375eacSRoger Pau Monné 		message = "writing rx ring-ref";
48296375eacSRoger Pau Monné 		goto error;
48396375eacSRoger Pau Monné 	}
48496375eacSRoger Pau Monné 	err = xs_printf(*xst, path, "event-channel", "%u",
48596375eacSRoger Pau Monné 	    xen_intr_port(rxq->xen_intr_handle));
48696375eacSRoger Pau Monné 	if (err != 0) {
48796375eacSRoger Pau Monné 		message = "writing event-channel";
48896375eacSRoger Pau Monné 		goto error;
48996375eacSRoger Pau Monné 	}
49096375eacSRoger Pau Monné 
49196375eacSRoger Pau Monné 	free(path, M_DEVBUF);
49296375eacSRoger Pau Monné 
49396375eacSRoger Pau Monné 	return (0);
49496375eacSRoger Pau Monné 
49596375eacSRoger Pau Monné error:
49696375eacSRoger Pau Monné 	free(path, M_DEVBUF);
49796375eacSRoger Pau Monné 	xenbus_dev_fatal(dev, err, "%s", message);
49896375eacSRoger Pau Monné 
49996375eacSRoger Pau Monné 	return (err);
50096375eacSRoger Pau Monné }
50196375eacSRoger Pau Monné 
50289e0f4d2SKip Macy /* Common code used when first setting up, and when resuming. */
50389e0f4d2SKip Macy static int
50423dc5621SKip Macy talk_to_backend(device_t dev, struct netfront_info *info)
50589e0f4d2SKip Macy {
50689e0f4d2SKip Macy 	const char *message;
507ff662b5cSJustin T. Gibbs 	struct xs_transaction xst;
50823dc5621SKip Macy 	const char *node = xenbus_get_node(dev);
50989e0f4d2SKip Macy 	int err;
51096375eacSRoger Pau Monné 	unsigned long num_queues, max_queues = 0;
51196375eacSRoger Pau Monné 	unsigned int i;
51289e0f4d2SKip Macy 
51389e0f4d2SKip Macy 	err = xen_net_read_mac(dev, info->mac);
51496375eacSRoger Pau Monné 	if (err != 0) {
51523dc5621SKip Macy 		xenbus_dev_fatal(dev, err, "parsing %s/mac", node);
51689e0f4d2SKip Macy 		goto out;
51789e0f4d2SKip Macy 	}
51889e0f4d2SKip Macy 
51996375eacSRoger Pau Monné 	err = xs_scanf(XST_NIL, xenbus_get_otherend_path(info->xbdev),
52096375eacSRoger Pau Monné 	    "multi-queue-max-queues", NULL, "%lu", &max_queues);
52196375eacSRoger Pau Monné 	if (err != 0)
52296375eacSRoger Pau Monné 		max_queues = 1;
52396375eacSRoger Pau Monné 	num_queues = xn_num_queues;
52496375eacSRoger Pau Monné 	if (num_queues > max_queues)
52596375eacSRoger Pau Monné 		num_queues = max_queues;
52696375eacSRoger Pau Monné 
52796375eacSRoger Pau Monné 	err = setup_device(dev, info, num_queues);
52896375eacSRoger Pau Monné 	if (err != 0)
52989e0f4d2SKip Macy 		goto out;
53089e0f4d2SKip Macy 
53189e0f4d2SKip Macy  again:
532ff662b5cSJustin T. Gibbs 	err = xs_transaction_start(&xst);
53396375eacSRoger Pau Monné 	if (err != 0) {
53489e0f4d2SKip Macy 		xenbus_dev_fatal(dev, err, "starting transaction");
53596375eacSRoger Pau Monné 		goto free;
53689e0f4d2SKip Macy 	}
53796375eacSRoger Pau Monné 
53896375eacSRoger Pau Monné 	if (info->num_queues == 1) {
53996375eacSRoger Pau Monné 		err = write_queue_xenstore_keys(dev, &info->rxq[0],
54096375eacSRoger Pau Monné 		    &info->txq[0], &xst, false);
54196375eacSRoger Pau Monné 		if (err != 0)
54296375eacSRoger Pau Monné 			goto abort_transaction_no_def_error;
54396375eacSRoger Pau Monné 	} else {
54496375eacSRoger Pau Monné 		err = xs_printf(xst, node, "multi-queue-num-queues",
54596375eacSRoger Pau Monné 		    "%u", info->num_queues);
54696375eacSRoger Pau Monné 		if (err != 0) {
54796375eacSRoger Pau Monné 			message = "writing multi-queue-num-queues";
54889e0f4d2SKip Macy 			goto abort_transaction;
54989e0f4d2SKip Macy 		}
55096375eacSRoger Pau Monné 
55196375eacSRoger Pau Monné 		for (i = 0; i < info->num_queues; i++) {
55296375eacSRoger Pau Monné 			err = write_queue_xenstore_keys(dev, &info->rxq[i],
55396375eacSRoger Pau Monné 			    &info->txq[i], &xst, true);
55496375eacSRoger Pau Monné 			if (err != 0)
55596375eacSRoger Pau Monné 				goto abort_transaction_no_def_error;
55689e0f4d2SKip Macy 		}
55789e0f4d2SKip Macy 	}
55896375eacSRoger Pau Monné 
559d0f3a8b9SRoger Pau Monné 	err = xs_printf(xst, node, "request-rx-copy", "%u", 1);
56096375eacSRoger Pau Monné 	if (err != 0) {
56189e0f4d2SKip Macy 		message = "writing request-rx-copy";
56289e0f4d2SKip Macy 		goto abort_transaction;
56389e0f4d2SKip Macy 	}
564ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "feature-rx-notify", "%d", 1);
56596375eacSRoger Pau Monné 	if (err != 0) {
56689e0f4d2SKip Macy 		message = "writing feature-rx-notify";
56789e0f4d2SKip Macy 		goto abort_transaction;
56889e0f4d2SKip Macy 	}
569ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "feature-sg", "%d", 1);
57096375eacSRoger Pau Monné 	if (err != 0) {
57189e0f4d2SKip Macy 		message = "writing feature-sg";
57289e0f4d2SKip Macy 		goto abort_transaction;
57389e0f4d2SKip Macy 	}
574c2d12e5eSRoger Pau Monné 	if ((info->xn_ifp->if_capenable & IFCAP_LRO) != 0) {
575ff662b5cSJustin T. Gibbs 		err = xs_printf(xst, node, "feature-gso-tcpv4", "%d", 1);
57696375eacSRoger Pau Monné 		if (err != 0) {
57789e0f4d2SKip Macy 			message = "writing feature-gso-tcpv4";
57889e0f4d2SKip Macy 			goto abort_transaction;
57989e0f4d2SKip Macy 		}
580c2d12e5eSRoger Pau Monné 	}
581c2d12e5eSRoger Pau Monné 	if ((info->xn_ifp->if_capenable & IFCAP_RXCSUM) == 0) {
582c2d12e5eSRoger Pau Monné 		err = xs_printf(xst, node, "feature-no-csum-offload", "%d", 1);
583c2d12e5eSRoger Pau Monné 		if (err != 0) {
584c2d12e5eSRoger Pau Monné 			message = "writing feature-no-csum-offload";
585c2d12e5eSRoger Pau Monné 			goto abort_transaction;
586c2d12e5eSRoger Pau Monné 		}
587c2d12e5eSRoger Pau Monné 	}
58889e0f4d2SKip Macy 
589ff662b5cSJustin T. Gibbs 	err = xs_transaction_end(xst, 0);
59096375eacSRoger Pau Monné 	if (err != 0) {
59189e0f4d2SKip Macy 		if (err == EAGAIN)
59289e0f4d2SKip Macy 			goto again;
59389e0f4d2SKip Macy 		xenbus_dev_fatal(dev, err, "completing transaction");
59496375eacSRoger Pau Monné 		goto free;
59589e0f4d2SKip Macy 	}
59689e0f4d2SKip Macy 
59789e0f4d2SKip Macy 	return 0;
59889e0f4d2SKip Macy 
59989e0f4d2SKip Macy  abort_transaction:
60089e0f4d2SKip Macy 	xenbus_dev_fatal(dev, err, "%s", message);
60196375eacSRoger Pau Monné  abort_transaction_no_def_error:
60296375eacSRoger Pau Monné 	xs_transaction_end(xst, 1);
60396375eacSRoger Pau Monné  free:
60489e0f4d2SKip Macy 	netif_free(info);
60589e0f4d2SKip Macy  out:
60696375eacSRoger Pau Monné 	return (err);
60796375eacSRoger Pau Monné }
60896375eacSRoger Pau Monné 
60996375eacSRoger Pau Monné static void
610da695b05SRoger Pau Monné xn_rxq_intr(struct netfront_rxq *rxq)
61196375eacSRoger Pau Monné {
61296375eacSRoger Pau Monné 
61396375eacSRoger Pau Monné 	XN_RX_LOCK(rxq);
61496375eacSRoger Pau Monné 	xn_rxeof(rxq);
61596375eacSRoger Pau Monné 	XN_RX_UNLOCK(rxq);
61696375eacSRoger Pau Monné }
61796375eacSRoger Pau Monné 
61896375eacSRoger Pau Monné static void
61996375eacSRoger Pau Monné xn_txq_start(struct netfront_txq *txq)
62096375eacSRoger Pau Monné {
62196375eacSRoger Pau Monné 	struct netfront_info *np = txq->info;
62296375eacSRoger Pau Monné 	struct ifnet *ifp = np->xn_ifp;
62396375eacSRoger Pau Monné 
62496375eacSRoger Pau Monné 	XN_TX_LOCK_ASSERT(txq);
62596375eacSRoger Pau Monné 	if (!drbr_empty(ifp, txq->br))
62696375eacSRoger Pau Monné 		xn_txq_mq_start_locked(txq, NULL);
62796375eacSRoger Pau Monné }
62896375eacSRoger Pau Monné 
62996375eacSRoger Pau Monné static void
630da695b05SRoger Pau Monné xn_txq_intr(struct netfront_txq *txq)
63196375eacSRoger Pau Monné {
63296375eacSRoger Pau Monné 
63396375eacSRoger Pau Monné 	XN_TX_LOCK(txq);
63496375eacSRoger Pau Monné 	if (RING_HAS_UNCONSUMED_RESPONSES(&txq->ring))
63596375eacSRoger Pau Monné 		xn_txeof(txq);
63696375eacSRoger Pau Monné 	xn_txq_start(txq);
63796375eacSRoger Pau Monné 	XN_TX_UNLOCK(txq);
63896375eacSRoger Pau Monné }
63996375eacSRoger Pau Monné 
64096375eacSRoger Pau Monné static void
64196375eacSRoger Pau Monné xn_txq_tq_deferred(void *xtxq, int pending)
64296375eacSRoger Pau Monné {
64396375eacSRoger Pau Monné 	struct netfront_txq *txq = xtxq;
64496375eacSRoger Pau Monné 
64596375eacSRoger Pau Monné 	XN_TX_LOCK(txq);
64696375eacSRoger Pau Monné 	xn_txq_start(txq);
64796375eacSRoger Pau Monné 	XN_TX_UNLOCK(txq);
64896375eacSRoger Pau Monné }
64996375eacSRoger Pau Monné 
65096375eacSRoger Pau Monné static void
65196375eacSRoger Pau Monné disconnect_rxq(struct netfront_rxq *rxq)
65296375eacSRoger Pau Monné {
65396375eacSRoger Pau Monné 
65496375eacSRoger Pau Monné 	xn_release_rx_bufs(rxq);
65596375eacSRoger Pau Monné 	gnttab_free_grant_references(rxq->gref_head);
656d039b070SRoger Pau Monné 	gnttab_end_foreign_access(rxq->ring_ref, NULL);
65796375eacSRoger Pau Monné 	/*
65896375eacSRoger Pau Monné 	 * No split event channel support at the moment, handle will
65996375eacSRoger Pau Monné 	 * be unbound in tx. So no need to call xen_intr_unbind here,
66096375eacSRoger Pau Monné 	 * but we do want to reset the handler to 0.
66196375eacSRoger Pau Monné 	 */
66296375eacSRoger Pau Monné 	rxq->xen_intr_handle = 0;
66396375eacSRoger Pau Monné }
66496375eacSRoger Pau Monné 
66596375eacSRoger Pau Monné static void
66696375eacSRoger Pau Monné destroy_rxq(struct netfront_rxq *rxq)
66796375eacSRoger Pau Monné {
66896375eacSRoger Pau Monné 
6692568ee67SRoger Pau Monné 	callout_drain(&rxq->rx_refill);
67096375eacSRoger Pau Monné 	free(rxq->ring.sring, M_DEVBUF);
67196375eacSRoger Pau Monné }
67296375eacSRoger Pau Monné 
67396375eacSRoger Pau Monné static void
67496375eacSRoger Pau Monné destroy_rxqs(struct netfront_info *np)
67596375eacSRoger Pau Monné {
67696375eacSRoger Pau Monné 	int i;
67796375eacSRoger Pau Monné 
67896375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++)
67996375eacSRoger Pau Monné 		destroy_rxq(&np->rxq[i]);
68096375eacSRoger Pau Monné 
68196375eacSRoger Pau Monné 	free(np->rxq, M_DEVBUF);
68296375eacSRoger Pau Monné 	np->rxq = NULL;
68389e0f4d2SKip Macy }
68489e0f4d2SKip Macy 
68589e0f4d2SKip Macy static int
68696375eacSRoger Pau Monné setup_rxqs(device_t dev, struct netfront_info *info,
68796375eacSRoger Pau Monné 	   unsigned long num_queues)
68889e0f4d2SKip Macy {
68996375eacSRoger Pau Monné 	int q, i;
6903a6d1fcfSKip Macy 	int error;
69196375eacSRoger Pau Monné 	netif_rx_sring_t *rxs;
69296375eacSRoger Pau Monné 	struct netfront_rxq *rxq;
69389e0f4d2SKip Macy 
69496375eacSRoger Pau Monné 	info->rxq = malloc(sizeof(struct netfront_rxq) * num_queues,
69596375eacSRoger Pau Monné 	    M_DEVBUF, M_WAITOK|M_ZERO);
69689e0f4d2SKip Macy 
69796375eacSRoger Pau Monné 	for (q = 0; q < num_queues; q++) {
69896375eacSRoger Pau Monné 		rxq = &info->rxq[q];
69996375eacSRoger Pau Monné 
70096375eacSRoger Pau Monné 		rxq->id = q;
70196375eacSRoger Pau Monné 		rxq->info = info;
70296375eacSRoger Pau Monné 		rxq->ring_ref = GRANT_REF_INVALID;
70396375eacSRoger Pau Monné 		rxq->ring.sring = NULL;
70496375eacSRoger Pau Monné 		snprintf(rxq->name, XN_QUEUE_NAME_LEN, "xnrx_%u", q);
70596375eacSRoger Pau Monné 		mtx_init(&rxq->lock, rxq->name, "netfront receive lock",
70696375eacSRoger Pau Monné 		    MTX_DEF);
70796375eacSRoger Pau Monné 
70896375eacSRoger Pau Monné 		for (i = 0; i <= NET_RX_RING_SIZE; i++) {
70996375eacSRoger Pau Monné 			rxq->mbufs[i] = NULL;
71096375eacSRoger Pau Monné 			rxq->grant_ref[i] = GRANT_REF_INVALID;
71196375eacSRoger Pau Monné 		}
71296375eacSRoger Pau Monné 
71396375eacSRoger Pau Monné 		/* Start resources allocation */
71496375eacSRoger Pau Monné 
7152568ee67SRoger Pau Monné 		if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
71696375eacSRoger Pau Monné 		    &rxq->gref_head) != 0) {
71796375eacSRoger Pau Monné 			device_printf(dev, "allocating rx gref");
7183a6d1fcfSKip Macy 			error = ENOMEM;
71989e0f4d2SKip Macy 			goto fail;
72089e0f4d2SKip Macy 		}
72189e0f4d2SKip Macy 
72296375eacSRoger Pau Monné 		rxs = (netif_rx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF,
72396375eacSRoger Pau Monné 		    M_WAITOK|M_ZERO);
72489e0f4d2SKip Macy 		SHARED_RING_INIT(rxs);
72596375eacSRoger Pau Monné 		FRONT_RING_INIT(&rxq->ring, rxs, PAGE_SIZE);
72689e0f4d2SKip Macy 
72796375eacSRoger Pau Monné 		error = xenbus_grant_ring(dev, virt_to_mfn(rxs),
72896375eacSRoger Pau Monné 		    &rxq->ring_ref);
72996375eacSRoger Pau Monné 		if (error != 0) {
73096375eacSRoger Pau Monné 			device_printf(dev, "granting rx ring page");
73196375eacSRoger Pau Monné 			goto fail_grant_ring;
73296375eacSRoger Pau Monné 		}
73389e0f4d2SKip Macy 
7342568ee67SRoger Pau Monné 		callout_init(&rxq->rx_refill, 1);
73589e0f4d2SKip Macy 	}
73689e0f4d2SKip Macy 
7373a6d1fcfSKip Macy 	return (0);
73889e0f4d2SKip Macy 
73996375eacSRoger Pau Monné fail_grant_ring:
74096375eacSRoger Pau Monné 	gnttab_free_grant_references(rxq->gref_head);
74196375eacSRoger Pau Monné 	free(rxq->ring.sring, M_DEVBUF);
74289e0f4d2SKip Macy fail:
74396375eacSRoger Pau Monné 	for (; q >= 0; q--) {
74496375eacSRoger Pau Monné 		disconnect_rxq(&info->rxq[q]);
74596375eacSRoger Pau Monné 		destroy_rxq(&info->rxq[q]);
74696375eacSRoger Pau Monné 	}
74796375eacSRoger Pau Monné 
74896375eacSRoger Pau Monné 	free(info->rxq, M_DEVBUF);
74996375eacSRoger Pau Monné 	return (error);
75096375eacSRoger Pau Monné }
75196375eacSRoger Pau Monné 
75296375eacSRoger Pau Monné static void
75396375eacSRoger Pau Monné disconnect_txq(struct netfront_txq *txq)
75496375eacSRoger Pau Monné {
75596375eacSRoger Pau Monné 
75696375eacSRoger Pau Monné 	xn_release_tx_bufs(txq);
75796375eacSRoger Pau Monné 	gnttab_free_grant_references(txq->gref_head);
758d039b070SRoger Pau Monné 	gnttab_end_foreign_access(txq->ring_ref, NULL);
75996375eacSRoger Pau Monné 	xen_intr_unbind(&txq->xen_intr_handle);
76096375eacSRoger Pau Monné }
76196375eacSRoger Pau Monné 
76296375eacSRoger Pau Monné static void
76396375eacSRoger Pau Monné destroy_txq(struct netfront_txq *txq)
76496375eacSRoger Pau Monné {
76596375eacSRoger Pau Monné 
76696375eacSRoger Pau Monné 	free(txq->ring.sring, M_DEVBUF);
76796375eacSRoger Pau Monné 	buf_ring_free(txq->br, M_DEVBUF);
76896375eacSRoger Pau Monné 	taskqueue_drain_all(txq->tq);
76996375eacSRoger Pau Monné 	taskqueue_free(txq->tq);
77096375eacSRoger Pau Monné }
77196375eacSRoger Pau Monné 
77296375eacSRoger Pau Monné static void
77396375eacSRoger Pau Monné destroy_txqs(struct netfront_info *np)
77496375eacSRoger Pau Monné {
77596375eacSRoger Pau Monné 	int i;
77696375eacSRoger Pau Monné 
77796375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++)
77896375eacSRoger Pau Monné 		destroy_txq(&np->txq[i]);
77996375eacSRoger Pau Monné 
78096375eacSRoger Pau Monné 	free(np->txq, M_DEVBUF);
78196375eacSRoger Pau Monné 	np->txq = NULL;
78296375eacSRoger Pau Monné }
78396375eacSRoger Pau Monné 
78496375eacSRoger Pau Monné static int
78596375eacSRoger Pau Monné setup_txqs(device_t dev, struct netfront_info *info,
78696375eacSRoger Pau Monné 	   unsigned long num_queues)
78796375eacSRoger Pau Monné {
78896375eacSRoger Pau Monné 	int q, i;
78996375eacSRoger Pau Monné 	int error;
79096375eacSRoger Pau Monné 	netif_tx_sring_t *txs;
79196375eacSRoger Pau Monné 	struct netfront_txq *txq;
79296375eacSRoger Pau Monné 
79396375eacSRoger Pau Monné 	info->txq = malloc(sizeof(struct netfront_txq) * num_queues,
79496375eacSRoger Pau Monné 	    M_DEVBUF, M_WAITOK|M_ZERO);
79596375eacSRoger Pau Monné 
79696375eacSRoger Pau Monné 	for (q = 0; q < num_queues; q++) {
79796375eacSRoger Pau Monné 		txq = &info->txq[q];
79896375eacSRoger Pau Monné 
79996375eacSRoger Pau Monné 		txq->id = q;
80096375eacSRoger Pau Monné 		txq->info = info;
80196375eacSRoger Pau Monné 
80296375eacSRoger Pau Monné 		txq->ring_ref = GRANT_REF_INVALID;
80396375eacSRoger Pau Monné 		txq->ring.sring = NULL;
80496375eacSRoger Pau Monné 
80596375eacSRoger Pau Monné 		snprintf(txq->name, XN_QUEUE_NAME_LEN, "xntx_%u", q);
80696375eacSRoger Pau Monné 
80796375eacSRoger Pau Monné 		mtx_init(&txq->lock, txq->name, "netfront transmit lock",
80896375eacSRoger Pau Monné 		    MTX_DEF);
80996375eacSRoger Pau Monné 
81096375eacSRoger Pau Monné 		for (i = 0; i <= NET_TX_RING_SIZE; i++) {
81196375eacSRoger Pau Monné 			txq->mbufs[i] = (void *) ((u_long) i+1);
81296375eacSRoger Pau Monné 			txq->grant_ref[i] = GRANT_REF_INVALID;
81396375eacSRoger Pau Monné 		}
81496375eacSRoger Pau Monné 		txq->mbufs[NET_TX_RING_SIZE] = (void *)0;
81596375eacSRoger Pau Monné 
81696375eacSRoger Pau Monné 		/* Start resources allocation. */
81796375eacSRoger Pau Monné 
81896375eacSRoger Pau Monné 		if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
81996375eacSRoger Pau Monné 		    &txq->gref_head) != 0) {
82096375eacSRoger Pau Monné 			device_printf(dev, "failed to allocate tx grant refs\n");
82196375eacSRoger Pau Monné 			error = ENOMEM;
82296375eacSRoger Pau Monné 			goto fail;
82396375eacSRoger Pau Monné 		}
82496375eacSRoger Pau Monné 
82596375eacSRoger Pau Monné 		txs = (netif_tx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF,
82696375eacSRoger Pau Monné 		    M_WAITOK|M_ZERO);
82796375eacSRoger Pau Monné 		SHARED_RING_INIT(txs);
82896375eacSRoger Pau Monné 		FRONT_RING_INIT(&txq->ring, txs, PAGE_SIZE);
82996375eacSRoger Pau Monné 
83096375eacSRoger Pau Monné 		error = xenbus_grant_ring(dev, virt_to_mfn(txs),
83196375eacSRoger Pau Monné 		    &txq->ring_ref);
83296375eacSRoger Pau Monné 		if (error != 0) {
83396375eacSRoger Pau Monné 			device_printf(dev, "failed to grant tx ring\n");
83496375eacSRoger Pau Monné 			goto fail_grant_ring;
83596375eacSRoger Pau Monné 		}
83696375eacSRoger Pau Monné 
83796375eacSRoger Pau Monné 		txq->br = buf_ring_alloc(NET_TX_RING_SIZE, M_DEVBUF,
83896375eacSRoger Pau Monné 		    M_WAITOK, &txq->lock);
83996375eacSRoger Pau Monné 		TASK_INIT(&txq->defrtask, 0, xn_txq_tq_deferred, txq);
84096375eacSRoger Pau Monné 
841da695b05SRoger Pau Monné 		txq->tq = taskqueue_create(txq->name, M_WAITOK,
84296375eacSRoger Pau Monné 		    taskqueue_thread_enqueue, &txq->tq);
84396375eacSRoger Pau Monné 
84496375eacSRoger Pau Monné 		error = taskqueue_start_threads(&txq->tq, 1, PI_NET,
84596375eacSRoger Pau Monné 		    "%s txq %d", device_get_nameunit(dev), txq->id);
84696375eacSRoger Pau Monné 		if (error != 0) {
84796375eacSRoger Pau Monné 			device_printf(dev, "failed to start tx taskq %d\n",
84896375eacSRoger Pau Monné 			    txq->id);
84996375eacSRoger Pau Monné 			goto fail_start_thread;
85096375eacSRoger Pau Monné 		}
85196375eacSRoger Pau Monné 
85296375eacSRoger Pau Monné 		error = xen_intr_alloc_and_bind_local_port(dev,
853da695b05SRoger Pau Monné 		    xenbus_get_otherend_id(dev), /* filter */ NULL, xn_intr,
854da695b05SRoger Pau Monné 		    &info->txq[q], INTR_TYPE_NET | INTR_MPSAFE | INTR_ENTROPY,
85596375eacSRoger Pau Monné 		    &txq->xen_intr_handle);
85696375eacSRoger Pau Monné 
85796375eacSRoger Pau Monné 		if (error != 0) {
85896375eacSRoger Pau Monné 			device_printf(dev, "xen_intr_alloc_and_bind_local_port failed\n");
85996375eacSRoger Pau Monné 			goto fail_bind_port;
86096375eacSRoger Pau Monné 		}
86196375eacSRoger Pau Monné 	}
86296375eacSRoger Pau Monné 
86396375eacSRoger Pau Monné 	return (0);
86496375eacSRoger Pau Monné 
86596375eacSRoger Pau Monné fail_bind_port:
86696375eacSRoger Pau Monné 	taskqueue_drain_all(txq->tq);
86796375eacSRoger Pau Monné fail_start_thread:
86896375eacSRoger Pau Monné 	buf_ring_free(txq->br, M_DEVBUF);
86996375eacSRoger Pau Monné 	taskqueue_free(txq->tq);
870d039b070SRoger Pau Monné 	gnttab_end_foreign_access(txq->ring_ref, NULL);
87196375eacSRoger Pau Monné fail_grant_ring:
87296375eacSRoger Pau Monné 	gnttab_free_grant_references(txq->gref_head);
87396375eacSRoger Pau Monné 	free(txq->ring.sring, M_DEVBUF);
87496375eacSRoger Pau Monné fail:
87596375eacSRoger Pau Monné 	for (; q >= 0; q--) {
87696375eacSRoger Pau Monné 		disconnect_txq(&info->txq[q]);
87796375eacSRoger Pau Monné 		destroy_txq(&info->txq[q]);
87896375eacSRoger Pau Monné 	}
87996375eacSRoger Pau Monné 
88096375eacSRoger Pau Monné 	free(info->txq, M_DEVBUF);
88196375eacSRoger Pau Monné 	return (error);
88296375eacSRoger Pau Monné }
88396375eacSRoger Pau Monné 
88496375eacSRoger Pau Monné static int
88596375eacSRoger Pau Monné setup_device(device_t dev, struct netfront_info *info,
88696375eacSRoger Pau Monné     unsigned long num_queues)
88796375eacSRoger Pau Monné {
88896375eacSRoger Pau Monné 	int error;
88996375eacSRoger Pau Monné 	int q;
89096375eacSRoger Pau Monné 
89196375eacSRoger Pau Monné 	if (info->txq)
89296375eacSRoger Pau Monné 		destroy_txqs(info);
89396375eacSRoger Pau Monné 
89496375eacSRoger Pau Monné 	if (info->rxq)
89596375eacSRoger Pau Monné 		destroy_rxqs(info);
89696375eacSRoger Pau Monné 
89796375eacSRoger Pau Monné 	info->num_queues = 0;
89896375eacSRoger Pau Monné 
89996375eacSRoger Pau Monné 	error = setup_rxqs(dev, info, num_queues);
90096375eacSRoger Pau Monné 	if (error != 0)
90196375eacSRoger Pau Monné 		goto out;
90296375eacSRoger Pau Monné 	error = setup_txqs(dev, info, num_queues);
90396375eacSRoger Pau Monné 	if (error != 0)
90496375eacSRoger Pau Monné 		goto out;
90596375eacSRoger Pau Monné 
90696375eacSRoger Pau Monné 	info->num_queues = num_queues;
90796375eacSRoger Pau Monné 
90896375eacSRoger Pau Monné 	/* No split event channel at the moment. */
90996375eacSRoger Pau Monné 	for (q = 0; q < num_queues; q++)
91096375eacSRoger Pau Monné 		info->rxq[q].xen_intr_handle = info->txq[q].xen_intr_handle;
91196375eacSRoger Pau Monné 
91296375eacSRoger Pau Monné 	return (0);
91396375eacSRoger Pau Monné 
91496375eacSRoger Pau Monné out:
91596375eacSRoger Pau Monné 	KASSERT(error != 0, ("Error path taken without providing an error code"));
9163a6d1fcfSKip Macy 	return (error);
91789e0f4d2SKip Macy }
91889e0f4d2SKip Macy 
919a0ae8f04SBjoern A. Zeeb #ifdef INET
92089e0f4d2SKip Macy /**
92112678024SDoug Rabson  * If this interface has an ipv4 address, send an arp for it. This
92212678024SDoug Rabson  * helps to get the network going again after migrating hosts.
92312678024SDoug Rabson  */
92412678024SDoug Rabson static void
92512678024SDoug Rabson netfront_send_fake_arp(device_t dev, struct netfront_info *info)
92612678024SDoug Rabson {
92712678024SDoug Rabson 	struct ifnet *ifp;
92812678024SDoug Rabson 	struct ifaddr *ifa;
92912678024SDoug Rabson 
93012678024SDoug Rabson 	ifp = info->xn_ifp;
93112678024SDoug Rabson 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
93212678024SDoug Rabson 		if (ifa->ifa_addr->sa_family == AF_INET) {
93312678024SDoug Rabson 			arp_ifinit(ifp, ifa);
93412678024SDoug Rabson 		}
93512678024SDoug Rabson 	}
93612678024SDoug Rabson }
937a0ae8f04SBjoern A. Zeeb #endif
93812678024SDoug Rabson 
93912678024SDoug Rabson /**
94089e0f4d2SKip Macy  * Callback received when the backend's state changes.
94189e0f4d2SKip Macy  */
942283d6f72SJustin T. Gibbs static void
94323dc5621SKip Macy netfront_backend_changed(device_t dev, XenbusState newstate)
94489e0f4d2SKip Macy {
94523dc5621SKip Macy 	struct netfront_info *sc = device_get_softc(dev);
94689e0f4d2SKip Macy 
94723dc5621SKip Macy 	DPRINTK("newstate=%d\n", newstate);
94889e0f4d2SKip Macy 
94923dc5621SKip Macy 	switch (newstate) {
95089e0f4d2SKip Macy 	case XenbusStateInitialising:
95189e0f4d2SKip Macy 	case XenbusStateInitialised:
95289e0f4d2SKip Macy 	case XenbusStateUnknown:
953920ba15bSKip Macy 	case XenbusStateReconfigured:
954920ba15bSKip Macy 	case XenbusStateReconfiguring:
95589e0f4d2SKip Macy 		break;
95689e0f4d2SKip Macy 	case XenbusStateInitWait:
95723dc5621SKip Macy 		if (xenbus_get_state(dev) != XenbusStateInitialising)
95889e0f4d2SKip Macy 			break;
95996375eacSRoger Pau Monné 		if (xn_connect(sc) != 0)
96089e0f4d2SKip Macy 			break;
96165671253SRoger Pau Monné 		/* Switch to connected state before kicking the rings. */
96265671253SRoger Pau Monné 		xenbus_set_state(sc->xbdev, XenbusStateConnected);
96365671253SRoger Pau Monné 		xn_kick_rings(sc);
96423dc5621SKip Macy 		break;
96589e0f4d2SKip Macy 	case XenbusStateClosing:
96623dc5621SKip Macy 		xenbus_set_state(dev, XenbusStateClosed);
96789e0f4d2SKip Macy 		break;
968c2d12e5eSRoger Pau Monné 	case XenbusStateClosed:
969c2d12e5eSRoger Pau Monné 		if (sc->xn_reset) {
970c2d12e5eSRoger Pau Monné 			netif_disconnect_backend(sc);
971c2d12e5eSRoger Pau Monné 			xenbus_set_state(dev, XenbusStateInitialising);
972c2d12e5eSRoger Pau Monné 			sc->xn_reset = false;
973c2d12e5eSRoger Pau Monné 		}
974c2d12e5eSRoger Pau Monné 		break;
975dbf82bdeSRoger Pau Monné 	case XenbusStateConnected:
976dbf82bdeSRoger Pau Monné #ifdef INET
977dbf82bdeSRoger Pau Monné 		netfront_send_fake_arp(dev, sc);
978dbf82bdeSRoger Pau Monné #endif
979dbf82bdeSRoger Pau Monné 		break;
98089e0f4d2SKip Macy 	}
98189e0f4d2SKip Macy }
98289e0f4d2SKip Macy 
983931eeffaSKenneth D. Merry /**
984931eeffaSKenneth D. Merry  * \brief Verify that there is sufficient space in the Tx ring
985931eeffaSKenneth D. Merry  *        buffer for a maximally sized request to be enqueued.
986c099cafaSAdrian Chadd  *
987931eeffaSKenneth D. Merry  * A transmit request requires a transmit descriptor for each packet
988931eeffaSKenneth D. Merry  * fragment, plus up to 2 entries for "options" (e.g. TSO).
989c099cafaSAdrian Chadd  */
99089e0f4d2SKip Macy static inline int
99196375eacSRoger Pau Monné xn_tx_slot_available(struct netfront_txq *txq)
99289e0f4d2SKip Macy {
99396375eacSRoger Pau Monné 
99496375eacSRoger Pau Monné 	return (RING_FREE_REQUESTS(&txq->ring) > (MAX_TX_REQ_FRAGS + 2));
99589e0f4d2SKip Macy }
996931eeffaSKenneth D. Merry 
99789e0f4d2SKip Macy static void
99896375eacSRoger Pau Monné xn_release_tx_bufs(struct netfront_txq *txq)
99989e0f4d2SKip Macy {
100089e0f4d2SKip Macy 	int i;
100189e0f4d2SKip Macy 
100289e0f4d2SKip Macy 	for (i = 1; i <= NET_TX_RING_SIZE; i++) {
1003931eeffaSKenneth D. Merry 		struct mbuf *m;
100489e0f4d2SKip Macy 
100596375eacSRoger Pau Monné 		m = txq->mbufs[i];
1006931eeffaSKenneth D. Merry 
1007931eeffaSKenneth D. Merry 		/*
1008931eeffaSKenneth D. Merry 		 * We assume that no kernel addresses are
1009931eeffaSKenneth D. Merry 		 * less than NET_TX_RING_SIZE.  Any entry
1010931eeffaSKenneth D. Merry 		 * in the table that is below this number
1011931eeffaSKenneth D. Merry 		 * must be an index from free-list tracking.
1012931eeffaSKenneth D. Merry 		 */
1013931eeffaSKenneth D. Merry 		if (((uintptr_t)m) <= NET_TX_RING_SIZE)
101489e0f4d2SKip Macy 			continue;
101596375eacSRoger Pau Monné 		gnttab_end_foreign_access_ref(txq->grant_ref[i]);
101696375eacSRoger Pau Monné 		gnttab_release_grant_reference(&txq->gref_head,
101796375eacSRoger Pau Monné 		    txq->grant_ref[i]);
101896375eacSRoger Pau Monné 		txq->grant_ref[i] = GRANT_REF_INVALID;
101996375eacSRoger Pau Monné 		add_id_to_freelist(txq->mbufs, i);
102096375eacSRoger Pau Monné 		txq->mbufs_cnt--;
102196375eacSRoger Pau Monné 		if (txq->mbufs_cnt < 0) {
10226f9767acSMarius Strobl 			panic("%s: tx_chain_cnt must be >= 0", __func__);
1023a4ec37f5SAdrian Chadd 		}
1024cf9c09e1SJustin T. Gibbs 		m_free(m);
102589e0f4d2SKip Macy 	}
102689e0f4d2SKip Macy }
102789e0f4d2SKip Macy 
10282568ee67SRoger Pau Monné static struct mbuf *
10292568ee67SRoger Pau Monné xn_alloc_one_rx_buffer(struct netfront_rxq *rxq)
10302568ee67SRoger Pau Monné {
10312568ee67SRoger Pau Monné 	struct mbuf *m;
10322568ee67SRoger Pau Monné 
10332568ee67SRoger Pau Monné 	m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE);
10342568ee67SRoger Pau Monné 	if (m == NULL)
10352568ee67SRoger Pau Monné 		return NULL;
10362568ee67SRoger Pau Monné 	m->m_len = m->m_pkthdr.len = MJUMPAGESIZE;
10372568ee67SRoger Pau Monné 
10382568ee67SRoger Pau Monné 	return (m);
10392568ee67SRoger Pau Monné }
10402568ee67SRoger Pau Monné 
104189e0f4d2SKip Macy static void
104296375eacSRoger Pau Monné xn_alloc_rx_buffers(struct netfront_rxq *rxq)
104389e0f4d2SKip Macy {
104489e0f4d2SKip Macy 	RING_IDX req_prod;
10452568ee67SRoger Pau Monné 	int notify;
10462568ee67SRoger Pau Monné 
10472568ee67SRoger Pau Monné 	XN_RX_LOCK_ASSERT(rxq);
10482568ee67SRoger Pau Monné 
10492568ee67SRoger Pau Monné 	if (__predict_false(rxq->info->carrier == 0))
10502568ee67SRoger Pau Monné 		return;
10512568ee67SRoger Pau Monné 
10522568ee67SRoger Pau Monné 	for (req_prod = rxq->ring.req_prod_pvt;
10532568ee67SRoger Pau Monné 	     req_prod - rxq->ring.rsp_cons < NET_RX_RING_SIZE;
10542568ee67SRoger Pau Monné 	     req_prod++) {
10552568ee67SRoger Pau Monné 		struct mbuf *m;
10562568ee67SRoger Pau Monné 		unsigned short id;
105789e0f4d2SKip Macy 		grant_ref_t ref;
10582568ee67SRoger Pau Monné 		struct netif_rx_request *req;
10592568ee67SRoger Pau Monné 		unsigned long pfn;
106089e0f4d2SKip Macy 
10612568ee67SRoger Pau Monné 		m = xn_alloc_one_rx_buffer(rxq);
10622568ee67SRoger Pau Monné 		if (m == NULL)
106389e0f4d2SKip Macy 			break;
106489e0f4d2SKip Macy 
10652568ee67SRoger Pau Monné 		id = xn_rxidx(req_prod);
106689e0f4d2SKip Macy 
106796375eacSRoger Pau Monné 		KASSERT(rxq->mbufs[id] == NULL, ("non-NULL xn_rx_chain"));
10682568ee67SRoger Pau Monné 		rxq->mbufs[id] = m;
106989e0f4d2SKip Macy 
107096375eacSRoger Pau Monné 		ref = gnttab_claim_grant_reference(&rxq->gref_head);
1071ff662b5cSJustin T. Gibbs 		KASSERT(ref != GNTTAB_LIST_END,
1072ff662b5cSJustin T. Gibbs 		    ("reserved grant references exhuasted"));
107396375eacSRoger Pau Monné 		rxq->grant_ref[id] = ref;
107489e0f4d2SKip Macy 
10752568ee67SRoger Pau Monné 		pfn = atop(vtophys(mtod(m, vm_offset_t)));
10762568ee67SRoger Pau Monné 		req = RING_GET_REQUEST(&rxq->ring, req_prod);
107789e0f4d2SKip Macy 
10782568ee67SRoger Pau Monné 		gnttab_grant_foreign_access_ref(ref,
10792568ee67SRoger Pau Monné 		    xenbus_get_otherend_id(rxq->info->xbdev), pfn, 0);
108089e0f4d2SKip Macy 		req->id = id;
108189e0f4d2SKip Macy 		req->gref = ref;
108289e0f4d2SKip Macy 	}
108389e0f4d2SKip Macy 
10842568ee67SRoger Pau Monné 	rxq->ring.req_prod_pvt = req_prod;
108589e0f4d2SKip Macy 
10862568ee67SRoger Pau Monné 	/* Not enough requests? Try again later. */
10872568ee67SRoger Pau Monné 	if (req_prod - rxq->ring.rsp_cons < NET_RX_SLOTS_MIN) {
1088bf7b50dbSRoger Pau Monné 		callout_reset_curcpu(&rxq->rx_refill, hz/10,
1089bf7b50dbSRoger Pau Monné 		    xn_alloc_rx_buffers_callout, rxq);
10902568ee67SRoger Pau Monné 		return;
10912568ee67SRoger Pau Monné 	}
10922568ee67SRoger Pau Monné 
10932568ee67SRoger Pau Monné 	wmb();		/* barrier so backend seens requests */
10942568ee67SRoger Pau Monné 
109596375eacSRoger Pau Monné 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&rxq->ring, notify);
109689e0f4d2SKip Macy 	if (notify)
109796375eacSRoger Pau Monné 		xen_intr_signal(rxq->xen_intr_handle);
109889e0f4d2SKip Macy }
109989e0f4d2SKip Macy 
11002568ee67SRoger Pau Monné static void xn_alloc_rx_buffers_callout(void *arg)
11012568ee67SRoger Pau Monné {
11022568ee67SRoger Pau Monné 	struct netfront_rxq *rxq;
11032568ee67SRoger Pau Monné 
11042568ee67SRoger Pau Monné 	rxq = (struct netfront_rxq *)arg;
11052568ee67SRoger Pau Monné 	XN_RX_LOCK(rxq);
11062568ee67SRoger Pau Monné 	xn_alloc_rx_buffers(rxq);
11072568ee67SRoger Pau Monné 	XN_RX_UNLOCK(rxq);
11082568ee67SRoger Pau Monné }
11092568ee67SRoger Pau Monné 
111089e0f4d2SKip Macy static void
111196375eacSRoger Pau Monné xn_release_rx_bufs(struct netfront_rxq *rxq)
111296375eacSRoger Pau Monné {
111396375eacSRoger Pau Monné 	int i,  ref;
111496375eacSRoger Pau Monné 	struct mbuf *m;
111596375eacSRoger Pau Monné 
111696375eacSRoger Pau Monné 	for (i = 0; i < NET_RX_RING_SIZE; i++) {
111796375eacSRoger Pau Monné 		m = rxq->mbufs[i];
111896375eacSRoger Pau Monné 
111996375eacSRoger Pau Monné 		if (m == NULL)
112096375eacSRoger Pau Monné 			continue;
112196375eacSRoger Pau Monné 
112296375eacSRoger Pau Monné 		ref = rxq->grant_ref[i];
112396375eacSRoger Pau Monné 		if (ref == GRANT_REF_INVALID)
112496375eacSRoger Pau Monné 			continue;
112596375eacSRoger Pau Monné 
112696375eacSRoger Pau Monné 		gnttab_end_foreign_access_ref(ref);
112796375eacSRoger Pau Monné 		gnttab_release_grant_reference(&rxq->gref_head, ref);
112896375eacSRoger Pau Monné 		rxq->mbufs[i] = NULL;
112996375eacSRoger Pau Monné 		rxq->grant_ref[i] = GRANT_REF_INVALID;
113096375eacSRoger Pau Monné 		m_freem(m);
113196375eacSRoger Pau Monné 	}
113296375eacSRoger Pau Monné }
113396375eacSRoger Pau Monné 
113496375eacSRoger Pau Monné static void
113596375eacSRoger Pau Monné xn_rxeof(struct netfront_rxq *rxq)
113689e0f4d2SKip Macy {
113789e0f4d2SKip Macy 	struct ifnet *ifp;
113896375eacSRoger Pau Monné 	struct netfront_info *np = rxq->info;
11393778878dSRoger Pau Monné #if (defined(INET) || defined(INET6))
114096375eacSRoger Pau Monné 	struct lro_ctrl *lro = &rxq->lro;
11413778878dSRoger Pau Monné #endif
114289e0f4d2SKip Macy 	struct netfront_rx_info rinfo;
114389e0f4d2SKip Macy 	struct netif_rx_response *rx = &rinfo.rx;
114489e0f4d2SKip Macy 	struct netif_extra_info *extras = rinfo.extras;
114589e0f4d2SKip Macy 	RING_IDX i, rp;
114689e0f4d2SKip Macy 	struct mbuf *m;
114796375eacSRoger Pau Monné 	struct mbufq mbufq_rxq, mbufq_errq;
1148d0f3a8b9SRoger Pau Monné 	int err, work_to_do;
114989e0f4d2SKip Macy 
115049906218SDoug Rabson 	do {
115196375eacSRoger Pau Monné 		XN_RX_LOCK_ASSERT(rxq);
115289e0f4d2SKip Macy 		if (!netfront_carrier_ok(np))
115389e0f4d2SKip Macy 			return;
115489e0f4d2SKip Macy 
1155c578b6acSGleb Smirnoff 		/* XXX: there should be some sane limit. */
115696375eacSRoger Pau Monné 		mbufq_init(&mbufq_errq, INT_MAX);
115796375eacSRoger Pau Monné 		mbufq_init(&mbufq_rxq, INT_MAX);
115889e0f4d2SKip Macy 
115989e0f4d2SKip Macy 		ifp = np->xn_ifp;
116089e0f4d2SKip Macy 
116196375eacSRoger Pau Monné 		rp = rxq->ring.sring->rsp_prod;
116289e0f4d2SKip Macy 		rmb();	/* Ensure we see queued responses up to 'rp'. */
116389e0f4d2SKip Macy 
116496375eacSRoger Pau Monné 		i = rxq->ring.rsp_cons;
116589e0f4d2SKip Macy 		while ((i != rp)) {
116696375eacSRoger Pau Monné 			memcpy(rx, RING_GET_RESPONSE(&rxq->ring, i), sizeof(*rx));
116789e0f4d2SKip Macy 			memset(extras, 0, sizeof(rinfo.extras));
116889e0f4d2SKip Macy 
116983b92f6eSKip Macy 			m = NULL;
117096375eacSRoger Pau Monné 			err = xn_get_responses(rxq, &rinfo, rp, &i, &m);
117189e0f4d2SKip Macy 
117276acc41fSJustin T. Gibbs 			if (__predict_false(err)) {
117383b92f6eSKip Macy 				if (m)
117496375eacSRoger Pau Monné 					(void )mbufq_enqueue(&mbufq_errq, m);
1175*b2fd6999SRoger Pau Monné 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
117689e0f4d2SKip Macy 				continue;
117789e0f4d2SKip Macy 			}
117889e0f4d2SKip Macy 
117989e0f4d2SKip Macy 			m->m_pkthdr.rcvif = ifp;
118089e0f4d2SKip Macy 			if ( rx->flags & NETRXF_data_validated ) {
118189e0f4d2SKip Macy 				/* Tell the stack the checksums are okay */
118289e0f4d2SKip Macy 				/*
118389e0f4d2SKip Macy 				 * XXX this isn't necessarily the case - need to add
118489e0f4d2SKip Macy 				 * check
118589e0f4d2SKip Macy 				 */
118689e0f4d2SKip Macy 
118789e0f4d2SKip Macy 				m->m_pkthdr.csum_flags |=
118889e0f4d2SKip Macy 					(CSUM_IP_CHECKED | CSUM_IP_VALID | CSUM_DATA_VALID
118989e0f4d2SKip Macy 					    | CSUM_PSEUDO_HDR);
119089e0f4d2SKip Macy 				m->m_pkthdr.csum_data = 0xffff;
119189e0f4d2SKip Macy 			}
1192d9a66b6dSRoger Pau Monné 			if ((rx->flags & NETRXF_extra_info) != 0 &&
1193d9a66b6dSRoger Pau Monné 			    (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type ==
1194d9a66b6dSRoger Pau Monné 			    XEN_NETIF_EXTRA_TYPE_GSO)) {
1195d9a66b6dSRoger Pau Monné 				m->m_pkthdr.tso_segsz =
1196d9a66b6dSRoger Pau Monné 				extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].u.gso.size;
1197d9a66b6dSRoger Pau Monné 				m->m_pkthdr.csum_flags |= CSUM_TSO;
1198d9a66b6dSRoger Pau Monné 			}
119989e0f4d2SKip Macy 
120096375eacSRoger Pau Monné 			(void )mbufq_enqueue(&mbufq_rxq, m);
120196375eacSRoger Pau Monné 			rxq->ring.rsp_cons = i;
120289e0f4d2SKip Macy 		}
120389e0f4d2SKip Macy 
120496375eacSRoger Pau Monné 		mbufq_drain(&mbufq_errq);
120589e0f4d2SKip Macy 
120689e0f4d2SKip Macy 		/*
120789e0f4d2SKip Macy 		 * Process all the mbufs after the remapping is complete.
120889e0f4d2SKip Macy 		 * Break the mbuf chain first though.
120989e0f4d2SKip Macy 		 */
121096375eacSRoger Pau Monné 		while ((m = mbufq_dequeue(&mbufq_rxq)) != NULL) {
1211c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
121289e0f4d2SKip Macy 
121396375eacSRoger Pau Monné 			/* XXX: Do we really need to drop the rx lock? */
121496375eacSRoger Pau Monné 			XN_RX_UNLOCK(rxq);
121508c9c2e0SRoger Pau Monné #if (defined(INET) || defined(INET6))
121612678024SDoug Rabson 			/* Use LRO if possible */
121712678024SDoug Rabson 			if ((ifp->if_capenable & IFCAP_LRO) == 0 ||
121812678024SDoug Rabson 			    lro->lro_cnt == 0 || tcp_lro_rx(lro, m, 0)) {
121912678024SDoug Rabson 				/*
122012678024SDoug Rabson 				 * If LRO fails, pass up to the stack
122112678024SDoug Rabson 				 * directly.
122212678024SDoug Rabson 				 */
122389e0f4d2SKip Macy 				(*ifp->if_input)(ifp, m);
122412678024SDoug Rabson 			}
122512678024SDoug Rabson #else
122612678024SDoug Rabson 			(*ifp->if_input)(ifp, m);
122712678024SDoug Rabson #endif
122896375eacSRoger Pau Monné 
122996375eacSRoger Pau Monné 			XN_RX_LOCK(rxq);
123089e0f4d2SKip Macy 		}
123189e0f4d2SKip Macy 
123296375eacSRoger Pau Monné 		rxq->ring.rsp_cons = i;
123389e0f4d2SKip Macy 
123408c9c2e0SRoger Pau Monné #if (defined(INET) || defined(INET6))
123512678024SDoug Rabson 		/*
123612678024SDoug Rabson 		 * Flush any outstanding LRO work
123712678024SDoug Rabson 		 */
12386dd38b87SSepherosa Ziehau 		tcp_lro_flush_all(lro);
123912678024SDoug Rabson #endif
124012678024SDoug Rabson 
124196375eacSRoger Pau Monné 		xn_alloc_rx_buffers(rxq);
124289e0f4d2SKip Macy 
124396375eacSRoger Pau Monné 		RING_FINAL_CHECK_FOR_RESPONSES(&rxq->ring, work_to_do);
124449906218SDoug Rabson 	} while (work_to_do);
124589e0f4d2SKip Macy }
124689e0f4d2SKip Macy 
124789e0f4d2SKip Macy static void
124896375eacSRoger Pau Monné xn_txeof(struct netfront_txq *txq)
124989e0f4d2SKip Macy {
125089e0f4d2SKip Macy 	RING_IDX i, prod;
125189e0f4d2SKip Macy 	unsigned short id;
125289e0f4d2SKip Macy 	struct ifnet *ifp;
125312678024SDoug Rabson 	netif_tx_response_t *txr;
125489e0f4d2SKip Macy 	struct mbuf *m;
125596375eacSRoger Pau Monné 	struct netfront_info *np = txq->info;
125689e0f4d2SKip Macy 
125796375eacSRoger Pau Monné 	XN_TX_LOCK_ASSERT(txq);
125889e0f4d2SKip Macy 
125989e0f4d2SKip Macy 	if (!netfront_carrier_ok(np))
126089e0f4d2SKip Macy 		return;
126189e0f4d2SKip Macy 
126289e0f4d2SKip Macy 	ifp = np->xn_ifp;
126389e0f4d2SKip Macy 
126489e0f4d2SKip Macy 	do {
126596375eacSRoger Pau Monné 		prod = txq->ring.sring->rsp_prod;
126689e0f4d2SKip Macy 		rmb(); /* Ensure we see responses up to 'rp'. */
126789e0f4d2SKip Macy 
126896375eacSRoger Pau Monné 		for (i = txq->ring.rsp_cons; i != prod; i++) {
126996375eacSRoger Pau Monné 			txr = RING_GET_RESPONSE(&txq->ring, i);
127012678024SDoug Rabson 			if (txr->status == NETIF_RSP_NULL)
127112678024SDoug Rabson 				continue;
127212678024SDoug Rabson 
1273931eeffaSKenneth D. Merry 			if (txr->status != NETIF_RSP_OKAY) {
1274931eeffaSKenneth D. Merry 				printf("%s: WARNING: response is %d!\n",
1275931eeffaSKenneth D. Merry 				       __func__, txr->status);
1276931eeffaSKenneth D. Merry 			}
127712678024SDoug Rabson 			id = txr->id;
127896375eacSRoger Pau Monné 			m = txq->mbufs[id];
127996375eacSRoger Pau Monné 			KASSERT(m != NULL, ("mbuf not found in chain"));
1280931eeffaSKenneth D. Merry 			KASSERT((uintptr_t)m > NET_TX_RING_SIZE,
1281931eeffaSKenneth D. Merry 				("mbuf already on the free list, but we're "
1282931eeffaSKenneth D. Merry 				"trying to free it again!"));
12832d8fae98SAdrian Chadd 			M_ASSERTVALID(m);
128489e0f4d2SKip Macy 
128576acc41fSJustin T. Gibbs 			if (__predict_false(gnttab_query_foreign_access(
128696375eacSRoger Pau Monné 			    txq->grant_ref[id]) != 0)) {
12876f9767acSMarius Strobl 				panic("%s: grant id %u still in use by the "
12886f9767acSMarius Strobl 				    "backend", __func__, id);
128989e0f4d2SKip Macy 			}
129096375eacSRoger Pau Monné 			gnttab_end_foreign_access_ref(txq->grant_ref[id]);
129189e0f4d2SKip Macy 			gnttab_release_grant_reference(
129296375eacSRoger Pau Monné 				&txq->gref_head, txq->grant_ref[id]);
129396375eacSRoger Pau Monné 			txq->grant_ref[id] = GRANT_REF_INVALID;
129489e0f4d2SKip Macy 
129596375eacSRoger Pau Monné 			txq->mbufs[id] = NULL;
129696375eacSRoger Pau Monné 			add_id_to_freelist(txq->mbufs, id);
129796375eacSRoger Pau Monné 			txq->mbufs_cnt--;
129812678024SDoug Rabson 			m_free(m);
129996375eacSRoger Pau Monné 			/* Only mark the txq active if we've freed up at least one slot to try */
1300d76e4550SAdrian Chadd 			ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
130189e0f4d2SKip Macy 		}
130296375eacSRoger Pau Monné 		txq->ring.rsp_cons = prod;
130389e0f4d2SKip Macy 
130489e0f4d2SKip Macy 		/*
130589e0f4d2SKip Macy 		 * Set a new event, then check for race with update of
130689e0f4d2SKip Macy 		 * tx_cons. Note that it is essential to schedule a
130789e0f4d2SKip Macy 		 * callback, no matter how few buffers are pending. Even if
130889e0f4d2SKip Macy 		 * there is space in the transmit ring, higher layers may
130989e0f4d2SKip Macy 		 * be blocked because too much data is outstanding: in such
131089e0f4d2SKip Macy 		 * cases notification from Xen is likely to be the only kick
131189e0f4d2SKip Macy 		 * that we'll get.
131289e0f4d2SKip Macy 		 */
131396375eacSRoger Pau Monné 		txq->ring.sring->rsp_event =
131496375eacSRoger Pau Monné 		    prod + ((txq->ring.sring->req_prod - prod) >> 1) + 1;
131589e0f4d2SKip Macy 
131689e0f4d2SKip Macy 		mb();
131796375eacSRoger Pau Monné 	} while (prod != txq->ring.sring->rsp_prod);
131889e0f4d2SKip Macy 
131996375eacSRoger Pau Monné 	if (txq->full &&
132096375eacSRoger Pau Monné 	    ((txq->ring.sring->req_prod - prod) < NET_TX_RING_SIZE)) {
132196375eacSRoger Pau Monné 		txq->full = false;
1322da695b05SRoger Pau Monné 		xn_txq_start(txq);
132389e0f4d2SKip Macy 	}
132489e0f4d2SKip Macy }
132589e0f4d2SKip Macy 
132689e0f4d2SKip Macy static void
132796375eacSRoger Pau Monné xn_intr(void *xsc)
132896375eacSRoger Pau Monné {
132996375eacSRoger Pau Monné 	struct netfront_txq *txq = xsc;
133096375eacSRoger Pau Monné 	struct netfront_info *np = txq->info;
133196375eacSRoger Pau Monné 	struct netfront_rxq *rxq = &np->rxq[txq->id];
133296375eacSRoger Pau Monné 
133396375eacSRoger Pau Monné 	/* kick both tx and rx */
133496375eacSRoger Pau Monné 	xn_rxq_intr(rxq);
133596375eacSRoger Pau Monné 	xn_txq_intr(txq);
133696375eacSRoger Pau Monné }
133796375eacSRoger Pau Monné 
133896375eacSRoger Pau Monné static void
133996375eacSRoger Pau Monné xn_move_rx_slot(struct netfront_rxq *rxq, struct mbuf *m,
134096375eacSRoger Pau Monné     grant_ref_t ref)
134196375eacSRoger Pau Monné {
134296375eacSRoger Pau Monné 	int new = xn_rxidx(rxq->ring.req_prod_pvt);
134396375eacSRoger Pau Monné 
134496375eacSRoger Pau Monné 	KASSERT(rxq->mbufs[new] == NULL, ("mbufs != NULL"));
134596375eacSRoger Pau Monné 	rxq->mbufs[new] = m;
134696375eacSRoger Pau Monné 	rxq->grant_ref[new] = ref;
134796375eacSRoger Pau Monné 	RING_GET_REQUEST(&rxq->ring, rxq->ring.req_prod_pvt)->id = new;
134896375eacSRoger Pau Monné 	RING_GET_REQUEST(&rxq->ring, rxq->ring.req_prod_pvt)->gref = ref;
134996375eacSRoger Pau Monné 	rxq->ring.req_prod_pvt++;
135096375eacSRoger Pau Monné }
135196375eacSRoger Pau Monné 
135296375eacSRoger Pau Monné static int
135396375eacSRoger Pau Monné xn_get_extras(struct netfront_rxq *rxq,
1354931eeffaSKenneth D. Merry     struct netif_extra_info *extras, RING_IDX rp, RING_IDX *cons)
135589e0f4d2SKip Macy {
135689e0f4d2SKip Macy 	struct netif_extra_info *extra;
135789e0f4d2SKip Macy 
135889e0f4d2SKip Macy 	int err = 0;
135989e0f4d2SKip Macy 
136089e0f4d2SKip Macy 	do {
136189e0f4d2SKip Macy 		struct mbuf *m;
136289e0f4d2SKip Macy 		grant_ref_t ref;
136389e0f4d2SKip Macy 
136476acc41fSJustin T. Gibbs 		if (__predict_false(*cons + 1 == rp)) {
1365931eeffaSKenneth D. Merry 			err = EINVAL;
136689e0f4d2SKip Macy 			break;
136789e0f4d2SKip Macy 		}
136889e0f4d2SKip Macy 
136989e0f4d2SKip Macy 		extra = (struct netif_extra_info *)
137096375eacSRoger Pau Monné 		RING_GET_RESPONSE(&rxq->ring, ++(*cons));
137189e0f4d2SKip Macy 
137276acc41fSJustin T. Gibbs 		if (__predict_false(!extra->type ||
137389e0f4d2SKip Macy 			extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1374931eeffaSKenneth D. Merry 			err = EINVAL;
137589e0f4d2SKip Macy 		} else {
137689e0f4d2SKip Macy 			memcpy(&extras[extra->type - 1], extra, sizeof(*extra));
137789e0f4d2SKip Macy 		}
137889e0f4d2SKip Macy 
137996375eacSRoger Pau Monné 		m = xn_get_rx_mbuf(rxq, *cons);
138096375eacSRoger Pau Monné 		ref = xn_get_rx_ref(rxq,  *cons);
138196375eacSRoger Pau Monné 		xn_move_rx_slot(rxq, m, ref);
138289e0f4d2SKip Macy 	} while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
138389e0f4d2SKip Macy 
138489e0f4d2SKip Macy 	return err;
138589e0f4d2SKip Macy }
138689e0f4d2SKip Macy 
138789e0f4d2SKip Macy static int
138896375eacSRoger Pau Monné xn_get_responses(struct netfront_rxq *rxq,
1389931eeffaSKenneth D. Merry     struct netfront_rx_info *rinfo, RING_IDX rp, RING_IDX *cons,
1390d0f3a8b9SRoger Pau Monné     struct mbuf  **list)
139189e0f4d2SKip Macy {
139289e0f4d2SKip Macy 	struct netif_rx_response *rx = &rinfo->rx;
139389e0f4d2SKip Macy 	struct netif_extra_info *extras = rinfo->extras;
139483b92f6eSKip Macy 	struct mbuf *m, *m0, *m_prev;
139596375eacSRoger Pau Monné 	grant_ref_t ref = xn_get_rx_ref(rxq, *cons);
1396931eeffaSKenneth D. Merry 	RING_IDX ref_cons = *cons;
139789e0f4d2SKip Macy 	int frags = 1;
139889e0f4d2SKip Macy 	int err = 0;
139989e0f4d2SKip Macy 	u_long ret;
140089e0f4d2SKip Macy 
140196375eacSRoger Pau Monné 	m0 = m = m_prev = xn_get_rx_mbuf(rxq, *cons);
140283b92f6eSKip Macy 
140389e0f4d2SKip Macy 	if (rx->flags & NETRXF_extra_info) {
140496375eacSRoger Pau Monné 		err = xn_get_extras(rxq, extras, rp, cons);
140589e0f4d2SKip Macy 	}
140689e0f4d2SKip Macy 
140783b92f6eSKip Macy 	if (m0 != NULL) {
140883b92f6eSKip Macy 		m0->m_pkthdr.len = 0;
140983b92f6eSKip Macy 		m0->m_next = NULL;
141083b92f6eSKip Macy 	}
141183b92f6eSKip Macy 
141289e0f4d2SKip Macy 	for (;;) {
141383b92f6eSKip Macy #if 0
1414227ca257SKip Macy 		DPRINTK("rx->status=%hd rx->offset=%hu frags=%u\n",
141583b92f6eSKip Macy 			rx->status, rx->offset, frags);
141683b92f6eSKip Macy #endif
141776acc41fSJustin T. Gibbs 		if (__predict_false(rx->status < 0 ||
141889e0f4d2SKip Macy 			rx->offset + rx->status > PAGE_SIZE)) {
1419931eeffaSKenneth D. Merry 
142096375eacSRoger Pau Monné 			xn_move_rx_slot(rxq, m, ref);
1421931eeffaSKenneth D. Merry 			if (m0 == m)
1422931eeffaSKenneth D. Merry 				m0 = NULL;
1423931eeffaSKenneth D. Merry 			m = NULL;
1424931eeffaSKenneth D. Merry 			err = EINVAL;
1425931eeffaSKenneth D. Merry 			goto next_skip_queue;
142689e0f4d2SKip Macy 		}
142789e0f4d2SKip Macy 
142889e0f4d2SKip Macy 		/*
142989e0f4d2SKip Macy 		 * This definitely indicates a bug, either in this driver or in
143089e0f4d2SKip Macy 		 * the backend driver. In future this should flag the bad
143189e0f4d2SKip Macy 		 * situation to the system controller to reboot the backed.
143289e0f4d2SKip Macy 		 */
1433ff662b5cSJustin T. Gibbs 		if (ref == GRANT_REF_INVALID) {
1434ff662b5cSJustin T. Gibbs 			printf("%s: Bad rx response id %d.\n", __func__, rx->id);
1435931eeffaSKenneth D. Merry 			err = EINVAL;
143689e0f4d2SKip Macy 			goto next;
143789e0f4d2SKip Macy 		}
143889e0f4d2SKip Macy 
1439920ba15bSKip Macy 		ret = gnttab_end_foreign_access_ref(ref);
1440d0f3a8b9SRoger Pau Monné 		KASSERT(ret, ("Unable to end access to grant references"));
144189e0f4d2SKip Macy 
144296375eacSRoger Pau Monné 		gnttab_release_grant_reference(&rxq->gref_head, ref);
144389e0f4d2SKip Macy 
144489e0f4d2SKip Macy next:
14453a539122SAdrian Chadd 		if (m == NULL)
14463a539122SAdrian Chadd 			break;
14473a539122SAdrian Chadd 
144883b92f6eSKip Macy 		m->m_len = rx->status;
144983b92f6eSKip Macy 		m->m_data += rx->offset;
145083b92f6eSKip Macy 		m0->m_pkthdr.len += rx->status;
145183b92f6eSKip Macy 
1452931eeffaSKenneth D. Merry next_skip_queue:
145389e0f4d2SKip Macy 		if (!(rx->flags & NETRXF_more_data))
145489e0f4d2SKip Macy 			break;
145589e0f4d2SKip Macy 
1456931eeffaSKenneth D. Merry 		if (*cons + frags == rp) {
145789e0f4d2SKip Macy 			if (net_ratelimit())
145889e0f4d2SKip Macy 				WPRINTK("Need more frags\n");
1459931eeffaSKenneth D. Merry 			err = ENOENT;
1460931eeffaSKenneth D. Merry 			printf("%s: cons %u frags %u rp %u, not enough frags\n",
1461931eeffaSKenneth D. Merry 			       __func__, *cons, frags, rp);
146289e0f4d2SKip Macy 			break;
146389e0f4d2SKip Macy 		}
1464931eeffaSKenneth D. Merry 		/*
1465931eeffaSKenneth D. Merry 		 * Note that m can be NULL, if rx->status < 0 or if
1466931eeffaSKenneth D. Merry 		 * rx->offset + rx->status > PAGE_SIZE above.
1467931eeffaSKenneth D. Merry 		 */
146883b92f6eSKip Macy 		m_prev = m;
146989e0f4d2SKip Macy 
147096375eacSRoger Pau Monné 		rx = RING_GET_RESPONSE(&rxq->ring, *cons + frags);
147196375eacSRoger Pau Monné 		m = xn_get_rx_mbuf(rxq, *cons + frags);
147283b92f6eSKip Macy 
1473931eeffaSKenneth D. Merry 		/*
1474931eeffaSKenneth D. Merry 		 * m_prev == NULL can happen if rx->status < 0 or if
1475931eeffaSKenneth D. Merry 		 * rx->offset + * rx->status > PAGE_SIZE above.
1476931eeffaSKenneth D. Merry 		 */
1477931eeffaSKenneth D. Merry 		if (m_prev != NULL)
147883b92f6eSKip Macy 			m_prev->m_next = m;
1479931eeffaSKenneth D. Merry 
1480931eeffaSKenneth D. Merry 		/*
1481931eeffaSKenneth D. Merry 		 * m0 can be NULL if rx->status < 0 or if * rx->offset +
1482931eeffaSKenneth D. Merry 		 * rx->status > PAGE_SIZE above.
1483931eeffaSKenneth D. Merry 		 */
1484931eeffaSKenneth D. Merry 		if (m0 == NULL)
1485931eeffaSKenneth D. Merry 			m0 = m;
148683b92f6eSKip Macy 		m->m_next = NULL;
148796375eacSRoger Pau Monné 		ref = xn_get_rx_ref(rxq, *cons + frags);
1488931eeffaSKenneth D. Merry 		ref_cons = *cons + frags;
148989e0f4d2SKip Macy 		frags++;
149089e0f4d2SKip Macy 	}
149183b92f6eSKip Macy 	*list = m0;
1492931eeffaSKenneth D. Merry 	*cons += frags;
149389e0f4d2SKip Macy 
14948577146eSJustin T. Gibbs 	return (err);
149589e0f4d2SKip Macy }
149689e0f4d2SKip Macy 
1497931eeffaSKenneth D. Merry /**
1498931eeffaSKenneth D. Merry  * \brief Count the number of fragments in an mbuf chain.
1499931eeffaSKenneth D. Merry  *
1500931eeffaSKenneth D. Merry  * Surprisingly, there isn't an M* macro for this.
1501c099cafaSAdrian Chadd  */
1502931eeffaSKenneth D. Merry static inline int
1503931eeffaSKenneth D. Merry xn_count_frags(struct mbuf *m)
1504931eeffaSKenneth D. Merry {
1505931eeffaSKenneth D. Merry 	int nfrags;
1506931eeffaSKenneth D. Merry 
1507931eeffaSKenneth D. Merry 	for (nfrags = 0; m != NULL; m = m->m_next)
1508931eeffaSKenneth D. Merry 		nfrags++;
1509931eeffaSKenneth D. Merry 
1510931eeffaSKenneth D. Merry 	return (nfrags);
151189e0f4d2SKip Macy }
151289e0f4d2SKip Macy 
1513931eeffaSKenneth D. Merry /**
1514931eeffaSKenneth D. Merry  * Given an mbuf chain, make sure we have enough room and then push
1515931eeffaSKenneth D. Merry  * it onto the transmit ring.
1516931eeffaSKenneth D. Merry  */
1517931eeffaSKenneth D. Merry static int
151896375eacSRoger Pau Monné xn_assemble_tx_request(struct netfront_txq *txq, struct mbuf *m_head)
1519931eeffaSKenneth D. Merry {
1520931eeffaSKenneth D. Merry 	struct mbuf *m;
152196375eacSRoger Pau Monné 	struct netfront_info *np = txq->info;
152296375eacSRoger Pau Monné 	struct ifnet *ifp = np->xn_ifp;
1523931eeffaSKenneth D. Merry 	u_int nfrags;
1524931eeffaSKenneth D. Merry 	int otherend_id;
1525931eeffaSKenneth D. Merry 
1526931eeffaSKenneth D. Merry 	/**
152712678024SDoug Rabson 	 * Defragment the mbuf if necessary.
152812678024SDoug Rabson 	 */
1529931eeffaSKenneth D. Merry 	nfrags = xn_count_frags(m_head);
1530931eeffaSKenneth D. Merry 
1531931eeffaSKenneth D. Merry 	/*
1532931eeffaSKenneth D. Merry 	 * Check to see whether this request is longer than netback
1533931eeffaSKenneth D. Merry 	 * can handle, and try to defrag it.
1534931eeffaSKenneth D. Merry 	 */
1535931eeffaSKenneth D. Merry 	/**
1536931eeffaSKenneth D. Merry 	 * It is a bit lame, but the netback driver in Linux can't
1537931eeffaSKenneth D. Merry 	 * deal with nfrags > MAX_TX_REQ_FRAGS, which is a quirk of
1538931eeffaSKenneth D. Merry 	 * the Linux network stack.
1539931eeffaSKenneth D. Merry 	 */
154096375eacSRoger Pau Monné 	if (nfrags > np->maxfrags) {
1541c6499eccSGleb Smirnoff 		m = m_defrag(m_head, M_NOWAIT);
154212678024SDoug Rabson 		if (!m) {
1543931eeffaSKenneth D. Merry 			/*
1544931eeffaSKenneth D. Merry 			 * Defrag failed, so free the mbuf and
1545931eeffaSKenneth D. Merry 			 * therefore drop the packet.
1546931eeffaSKenneth D. Merry 			 */
154712678024SDoug Rabson 			m_freem(m_head);
1548931eeffaSKenneth D. Merry 			return (EMSGSIZE);
154912678024SDoug Rabson 		}
155012678024SDoug Rabson 		m_head = m;
155112678024SDoug Rabson 	}
155289e0f4d2SKip Macy 
1553a4ec37f5SAdrian Chadd 	/* Determine how many fragments now exist */
1554931eeffaSKenneth D. Merry 	nfrags = xn_count_frags(m_head);
1555a4ec37f5SAdrian Chadd 
1556a4ec37f5SAdrian Chadd 	/*
1557931eeffaSKenneth D. Merry 	 * Check to see whether the defragmented packet has too many
1558931eeffaSKenneth D. Merry 	 * segments for the Linux netback driver.
1559a4ec37f5SAdrian Chadd 	 */
1560931eeffaSKenneth D. Merry 	/**
1561931eeffaSKenneth D. Merry 	 * The FreeBSD TCP stack, with TSO enabled, can produce a chain
1562931eeffaSKenneth D. Merry 	 * of mbufs longer than Linux can handle.  Make sure we don't
1563931eeffaSKenneth D. Merry 	 * pass a too-long chain over to the other side by dropping the
1564931eeffaSKenneth D. Merry 	 * packet.  It doesn't look like there is currently a way to
1565931eeffaSKenneth D. Merry 	 * tell the TCP stack to generate a shorter chain of packets.
15663fb28bbbSAdrian Chadd 	 */
1567931eeffaSKenneth D. Merry 	if (nfrags > MAX_TX_REQ_FRAGS) {
1568ff662b5cSJustin T. Gibbs #ifdef DEBUG
1569ff662b5cSJustin T. Gibbs 		printf("%s: nfrags %d > MAX_TX_REQ_FRAGS %d, netback "
1570ff662b5cSJustin T. Gibbs 		       "won't be able to handle it, dropping\n",
1571ff662b5cSJustin T. Gibbs 		       __func__, nfrags, MAX_TX_REQ_FRAGS);
1572ff662b5cSJustin T. Gibbs #endif
1573931eeffaSKenneth D. Merry 		m_freem(m_head);
1574931eeffaSKenneth D. Merry 		return (EMSGSIZE);
1575a4ec37f5SAdrian Chadd 	}
1576a4ec37f5SAdrian Chadd 
15773fb28bbbSAdrian Chadd 	/*
1578931eeffaSKenneth D. Merry 	 * This check should be redundant.  We've already verified that we
1579931eeffaSKenneth D. Merry 	 * have enough slots in the ring to handle a packet of maximum
1580931eeffaSKenneth D. Merry 	 * size, and that our packet is less than the maximum size.  Keep
1581931eeffaSKenneth D. Merry 	 * it in here as an assert for now just to make certain that
158296375eacSRoger Pau Monné 	 * chain_cnt is accurate.
15833fb28bbbSAdrian Chadd 	 */
158496375eacSRoger Pau Monné 	KASSERT((txq->mbufs_cnt + nfrags) <= NET_TX_RING_SIZE,
158596375eacSRoger Pau Monné 		("%s: chain_cnt (%d) + nfrags (%d) > NET_TX_RING_SIZE "
158696375eacSRoger Pau Monné 		 "(%d)!", __func__, (int) txq->mbufs_cnt,
1587931eeffaSKenneth D. Merry                     (int) nfrags, (int) NET_TX_RING_SIZE));
1588a4ec37f5SAdrian Chadd 
158989e0f4d2SKip Macy 	/*
159089e0f4d2SKip Macy 	 * Start packing the mbufs in this chain into
159189e0f4d2SKip Macy 	 * the fragment pointers. Stop when we run out
159289e0f4d2SKip Macy 	 * of fragments or hit the end of the mbuf chain.
159389e0f4d2SKip Macy 	 */
159412678024SDoug Rabson 	m = m_head;
159596375eacSRoger Pau Monné 	otherend_id = xenbus_get_otherend_id(np->xbdev);
159612678024SDoug Rabson 	for (m = m_head; m; m = m->m_next) {
1597931eeffaSKenneth D. Merry 		netif_tx_request_t *tx;
1598931eeffaSKenneth D. Merry 		uintptr_t id;
1599931eeffaSKenneth D. Merry 		grant_ref_t ref;
1600931eeffaSKenneth D. Merry 		u_long mfn; /* XXX Wrong type? */
1601931eeffaSKenneth D. Merry 
160296375eacSRoger Pau Monné 		tx = RING_GET_REQUEST(&txq->ring, txq->ring.req_prod_pvt);
160396375eacSRoger Pau Monné 		id = get_id_from_freelist(txq->mbufs);
1604a4ec37f5SAdrian Chadd 		if (id == 0)
16056f9767acSMarius Strobl 			panic("%s: was allocated the freelist head!\n",
16066f9767acSMarius Strobl 			    __func__);
160796375eacSRoger Pau Monné 		txq->mbufs_cnt++;
160896375eacSRoger Pau Monné 		if (txq->mbufs_cnt > NET_TX_RING_SIZE)
16096f9767acSMarius Strobl 			panic("%s: tx_chain_cnt must be <= NET_TX_RING_SIZE\n",
16106f9767acSMarius Strobl 			    __func__);
161196375eacSRoger Pau Monné 		txq->mbufs[id] = m;
161289e0f4d2SKip Macy 		tx->id = id;
161396375eacSRoger Pau Monné 		ref = gnttab_claim_grant_reference(&txq->gref_head);
161489e0f4d2SKip Macy 		KASSERT((short)ref >= 0, ("Negative ref"));
161512678024SDoug Rabson 		mfn = virt_to_mfn(mtod(m, vm_offset_t));
161623dc5621SKip Macy 		gnttab_grant_foreign_access_ref(ref, otherend_id,
161789e0f4d2SKip Macy 		    mfn, GNTMAP_readonly);
161896375eacSRoger Pau Monné 		tx->gref = txq->grant_ref[id] = ref;
161912678024SDoug Rabson 		tx->offset = mtod(m, vm_offset_t) & (PAGE_SIZE - 1);
162089e0f4d2SKip Macy 		tx->flags = 0;
162112678024SDoug Rabson 		if (m == m_head) {
162212678024SDoug Rabson 			/*
162312678024SDoug Rabson 			 * The first fragment has the entire packet
162412678024SDoug Rabson 			 * size, subsequent fragments have just the
162512678024SDoug Rabson 			 * fragment size. The backend works out the
162612678024SDoug Rabson 			 * true size of the first fragment by
162712678024SDoug Rabson 			 * subtracting the sizes of the other
162812678024SDoug Rabson 			 * fragments.
162912678024SDoug Rabson 			 */
163012678024SDoug Rabson 			tx->size = m->m_pkthdr.len;
163189e0f4d2SKip Macy 
163212678024SDoug Rabson 			/*
1633931eeffaSKenneth D. Merry 			 * The first fragment contains the checksum flags
1634931eeffaSKenneth D. Merry 			 * and is optionally followed by extra data for
1635931eeffaSKenneth D. Merry 			 * TSO etc.
1636931eeffaSKenneth D. Merry 			 */
1637931eeffaSKenneth D. Merry 			/**
1638931eeffaSKenneth D. Merry 			 * CSUM_TSO requires checksum offloading.
1639931eeffaSKenneth D. Merry 			 * Some versions of FreeBSD fail to
1640931eeffaSKenneth D. Merry 			 * set CSUM_TCP in the CSUM_TSO case,
1641931eeffaSKenneth D. Merry 			 * so we have to test for CSUM_TSO
1642931eeffaSKenneth D. Merry 			 * explicitly.
164312678024SDoug Rabson 			 */
164412678024SDoug Rabson 			if (m->m_pkthdr.csum_flags
1645931eeffaSKenneth D. Merry 			    & (CSUM_DELAY_DATA | CSUM_TSO)) {
164612678024SDoug Rabson 				tx->flags |= (NETTXF_csum_blank
164712678024SDoug Rabson 				    | NETTXF_data_validated);
164812678024SDoug Rabson 			}
164912678024SDoug Rabson 			if (m->m_pkthdr.csum_flags & CSUM_TSO) {
165012678024SDoug Rabson 				struct netif_extra_info *gso =
165112678024SDoug Rabson 					(struct netif_extra_info *)
165296375eacSRoger Pau Monné 					RING_GET_REQUEST(&txq->ring,
165396375eacSRoger Pau Monné 							 ++txq->ring.req_prod_pvt);
165489e0f4d2SKip Macy 
165512678024SDoug Rabson 				tx->flags |= NETTXF_extra_info;
165689e0f4d2SKip Macy 
165712678024SDoug Rabson 				gso->u.gso.size = m->m_pkthdr.tso_segsz;
165812678024SDoug Rabson 				gso->u.gso.type =
165912678024SDoug Rabson 					XEN_NETIF_GSO_TYPE_TCPV4;
166012678024SDoug Rabson 				gso->u.gso.pad = 0;
166112678024SDoug Rabson 				gso->u.gso.features = 0;
166212678024SDoug Rabson 
166312678024SDoug Rabson 				gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
166412678024SDoug Rabson 				gso->flags = 0;
166512678024SDoug Rabson 			}
166612678024SDoug Rabson 		} else {
166712678024SDoug Rabson 			tx->size = m->m_len;
166812678024SDoug Rabson 		}
1669931eeffaSKenneth D. Merry 		if (m->m_next)
167012678024SDoug Rabson 			tx->flags |= NETTXF_more_data;
167112678024SDoug Rabson 
167296375eacSRoger Pau Monné 		txq->ring.req_prod_pvt++;
1673931eeffaSKenneth D. Merry 	}
167412678024SDoug Rabson 	BPF_MTAP(ifp, m_head);
167512678024SDoug Rabson 
1676*b2fd6999SRoger Pau Monné 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1677*b2fd6999SRoger Pau Monné 	if_inc_counter(ifp, IFCOUNTER_OBYTES, m_head->m_pkthdr.len);
1678*b2fd6999SRoger Pau Monné 	if (m_head->m_flags & M_MCAST)
1679*b2fd6999SRoger Pau Monné 		if_inc_counter(ifp, IFCOUNTER_OMCASTS, 1);
168096375eacSRoger Pau Monné 
1681*b2fd6999SRoger Pau Monné 	xn_txeof(txq);
1682931eeffaSKenneth D. Merry 
1683931eeffaSKenneth D. Merry 	return (0);
168489e0f4d2SKip Macy }
168589e0f4d2SKip Macy 
168689e0f4d2SKip Macy /* equivalent of network_open() in Linux */
168789e0f4d2SKip Macy static void
168896375eacSRoger Pau Monné xn_ifinit_locked(struct netfront_info *np)
168989e0f4d2SKip Macy {
169089e0f4d2SKip Macy 	struct ifnet *ifp;
169196375eacSRoger Pau Monné 	int i;
169296375eacSRoger Pau Monné 	struct netfront_rxq *rxq;
169389e0f4d2SKip Macy 
169496375eacSRoger Pau Monné 	XN_LOCK_ASSERT(np);
169589e0f4d2SKip Macy 
169696375eacSRoger Pau Monné 	ifp = np->xn_ifp;
169789e0f4d2SKip Macy 
1698c21b47d8SRoger Pau Monné 	if (ifp->if_drv_flags & IFF_DRV_RUNNING || !netfront_carrier_ok(np))
169989e0f4d2SKip Macy 		return;
170089e0f4d2SKip Macy 
170196375eacSRoger Pau Monné 	xn_stop(np);
170289e0f4d2SKip Macy 
170396375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
170496375eacSRoger Pau Monné 		rxq = &np->rxq[i];
17052568ee67SRoger Pau Monné 		XN_RX_LOCK(rxq);
170696375eacSRoger Pau Monné 		xn_alloc_rx_buffers(rxq);
170796375eacSRoger Pau Monné 		rxq->ring.sring->rsp_event = rxq->ring.rsp_cons + 1;
17082568ee67SRoger Pau Monné 		if (RING_HAS_UNCONSUMED_RESPONSES(&rxq->ring))
1709da695b05SRoger Pau Monné 			xn_rxeof(rxq);
17102568ee67SRoger Pau Monné 		XN_RX_UNLOCK(rxq);
171196375eacSRoger Pau Monné 	}
171289e0f4d2SKip Macy 
171389e0f4d2SKip Macy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
171489e0f4d2SKip Macy 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
17150e509842SJustin T. Gibbs 	if_link_state_change(ifp, LINK_STATE_UP);
171689e0f4d2SKip Macy }
171789e0f4d2SKip Macy 
171889e0f4d2SKip Macy static void
171989e0f4d2SKip Macy xn_ifinit(void *xsc)
172089e0f4d2SKip Macy {
172189e0f4d2SKip Macy 	struct netfront_info *sc = xsc;
172289e0f4d2SKip Macy 
172389e0f4d2SKip Macy 	XN_LOCK(sc);
172489e0f4d2SKip Macy 	xn_ifinit_locked(sc);
172589e0f4d2SKip Macy 	XN_UNLOCK(sc);
172689e0f4d2SKip Macy }
172789e0f4d2SKip Macy 
172889e0f4d2SKip Macy static int
172989e0f4d2SKip Macy xn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
173089e0f4d2SKip Macy {
173189e0f4d2SKip Macy 	struct netfront_info *sc = ifp->if_softc;
173289e0f4d2SKip Macy 	struct ifreq *ifr = (struct ifreq *) data;
1733c2d12e5eSRoger Pau Monné 	device_t dev;
1734a0ae8f04SBjoern A. Zeeb #ifdef INET
173589e0f4d2SKip Macy 	struct ifaddr *ifa = (struct ifaddr *)data;
1736a0ae8f04SBjoern A. Zeeb #endif
17373c9d5940SRoger Pau Monné 	int mask, error = 0, reinit;
1738c2d12e5eSRoger Pau Monné 
1739c2d12e5eSRoger Pau Monné 	dev = sc->xbdev;
1740c2d12e5eSRoger Pau Monné 
174189e0f4d2SKip Macy 	switch(cmd) {
174289e0f4d2SKip Macy 	case SIOCSIFADDR:
1743a0ae8f04SBjoern A. Zeeb #ifdef INET
174489e0f4d2SKip Macy 		XN_LOCK(sc);
174589e0f4d2SKip Macy 		if (ifa->ifa_addr->sa_family == AF_INET) {
174689e0f4d2SKip Macy 			ifp->if_flags |= IFF_UP;
174789e0f4d2SKip Macy 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
174889e0f4d2SKip Macy 				xn_ifinit_locked(sc);
174989e0f4d2SKip Macy 			arp_ifinit(ifp, ifa);
175089e0f4d2SKip Macy 			XN_UNLOCK(sc);
175149906218SDoug Rabson 		} else {
175249906218SDoug Rabson 			XN_UNLOCK(sc);
1753a0ae8f04SBjoern A. Zeeb #endif
175449906218SDoug Rabson 			error = ether_ioctl(ifp, cmd, data);
1755a0ae8f04SBjoern A. Zeeb #ifdef INET
175649906218SDoug Rabson 		}
1757a0ae8f04SBjoern A. Zeeb #endif
175889e0f4d2SKip Macy 		break;
175989e0f4d2SKip Macy 	case SIOCSIFMTU:
176089e0f4d2SKip Macy 		ifp->if_mtu = ifr->ifr_mtu;
176189e0f4d2SKip Macy 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
176289e0f4d2SKip Macy 		xn_ifinit(sc);
176389e0f4d2SKip Macy 		break;
176489e0f4d2SKip Macy 	case SIOCSIFFLAGS:
176589e0f4d2SKip Macy 		XN_LOCK(sc);
176689e0f4d2SKip Macy 		if (ifp->if_flags & IFF_UP) {
176789e0f4d2SKip Macy 			/*
176889e0f4d2SKip Macy 			 * If only the state of the PROMISC flag changed,
176989e0f4d2SKip Macy 			 * then just use the 'set promisc mode' command
177089e0f4d2SKip Macy 			 * instead of reinitializing the entire NIC. Doing
177189e0f4d2SKip Macy 			 * a full re-init means reloading the firmware and
177289e0f4d2SKip Macy 			 * waiting for it to start up, which may take a
177389e0f4d2SKip Macy 			 * second or two.
177489e0f4d2SKip Macy 			 */
177589e0f4d2SKip Macy 			xn_ifinit_locked(sc);
177689e0f4d2SKip Macy 		} else {
177789e0f4d2SKip Macy 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
177889e0f4d2SKip Macy 				xn_stop(sc);
177989e0f4d2SKip Macy 			}
178089e0f4d2SKip Macy 		}
178189e0f4d2SKip Macy 		sc->xn_if_flags = ifp->if_flags;
178289e0f4d2SKip Macy 		XN_UNLOCK(sc);
178389e0f4d2SKip Macy 		break;
178489e0f4d2SKip Macy 	case SIOCSIFCAP:
178589e0f4d2SKip Macy 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
17863c9d5940SRoger Pau Monné 		reinit = 0;
17873c9d5940SRoger Pau Monné 
178812678024SDoug Rabson 		if (mask & IFCAP_TXCSUM) {
17893c9d5940SRoger Pau Monné 			ifp->if_capenable ^= IFCAP_TXCSUM;
17903c9d5940SRoger Pau Monné 			ifp->if_hwassist ^= XN_CSUM_FEATURES;
179112678024SDoug Rabson 		}
179212678024SDoug Rabson 		if (mask & IFCAP_TSO4) {
17933c9d5940SRoger Pau Monné 			ifp->if_capenable ^= IFCAP_TSO4;
17943c9d5940SRoger Pau Monné 			ifp->if_hwassist ^= CSUM_TSO;
179512678024SDoug Rabson 		}
179612678024SDoug Rabson 
17973c9d5940SRoger Pau Monné 		if (mask & (IFCAP_RXCSUM | IFCAP_LRO)) {
17983c9d5940SRoger Pau Monné 			/* These Rx features require us to renegotiate. */
17993c9d5940SRoger Pau Monné 			reinit = 1;
18003c9d5940SRoger Pau Monné 
18013c9d5940SRoger Pau Monné 			if (mask & IFCAP_RXCSUM)
18023c9d5940SRoger Pau Monné 				ifp->if_capenable ^= IFCAP_RXCSUM;
18033c9d5940SRoger Pau Monné 			if (mask & IFCAP_LRO)
18043c9d5940SRoger Pau Monné 				ifp->if_capenable ^= IFCAP_LRO;
180512678024SDoug Rabson 		}
18063c9d5940SRoger Pau Monné 
18073c9d5940SRoger Pau Monné 		if (reinit == 0)
18083c9d5940SRoger Pau Monné 			break;
18093c9d5940SRoger Pau Monné 
1810c2d12e5eSRoger Pau Monné 		/*
1811c2d12e5eSRoger Pau Monné 		 * We must reset the interface so the backend picks up the
1812c2d12e5eSRoger Pau Monné 		 * new features.
1813c2d12e5eSRoger Pau Monné 		 */
18143c9d5940SRoger Pau Monné 		device_printf(sc->xbdev,
18153c9d5940SRoger Pau Monné 		    "performing interface reset due to feature change\n");
1816c2d12e5eSRoger Pau Monné 		XN_LOCK(sc);
1817c2d12e5eSRoger Pau Monné 		netfront_carrier_off(sc);
1818c2d12e5eSRoger Pau Monné 		sc->xn_reset = true;
1819c2d12e5eSRoger Pau Monné 		/*
1820c2d12e5eSRoger Pau Monné 		 * NB: the pending packet queue is not flushed, since
1821c2d12e5eSRoger Pau Monné 		 * the interface should still support the old options.
1822c2d12e5eSRoger Pau Monné 		 */
1823c2d12e5eSRoger Pau Monné 		XN_UNLOCK(sc);
1824c2d12e5eSRoger Pau Monné 		/*
1825c2d12e5eSRoger Pau Monné 		 * Delete the xenstore nodes that export features.
1826c2d12e5eSRoger Pau Monné 		 *
1827c2d12e5eSRoger Pau Monné 		 * NB: There's a xenbus state called
1828c2d12e5eSRoger Pau Monné 		 * "XenbusStateReconfiguring", which is what we should set
1829c2d12e5eSRoger Pau Monné 		 * here. Sadly none of the backends know how to handle it,
1830c2d12e5eSRoger Pau Monné 		 * and simply disconnect from the frontend, so we will just
1831c2d12e5eSRoger Pau Monné 		 * switch back to XenbusStateInitialising in order to force
1832c2d12e5eSRoger Pau Monné 		 * a reconnection.
1833c2d12e5eSRoger Pau Monné 		 */
1834c2d12e5eSRoger Pau Monné 		xs_rm(XST_NIL, xenbus_get_node(dev), "feature-gso-tcpv4");
1835c2d12e5eSRoger Pau Monné 		xs_rm(XST_NIL, xenbus_get_node(dev), "feature-no-csum-offload");
1836c2d12e5eSRoger Pau Monné 		xenbus_set_state(dev, XenbusStateClosing);
18373c9d5940SRoger Pau Monné 
18383c9d5940SRoger Pau Monné 		/*
18393c9d5940SRoger Pau Monné 		 * Wait for the frontend to reconnect before returning
18403c9d5940SRoger Pau Monné 		 * from the ioctl. 30s should be more than enough for any
18413c9d5940SRoger Pau Monné 		 * sane backend to reconnect.
18423c9d5940SRoger Pau Monné 		 */
18433c9d5940SRoger Pau Monné 		error = tsleep(sc, 0, "xn_rst", 30*hz);
184489e0f4d2SKip Macy 		break;
184589e0f4d2SKip Macy 	case SIOCADDMULTI:
184689e0f4d2SKip Macy 	case SIOCDELMULTI:
1847ce8df48bSSimon J. Gerraty 		break;
184889e0f4d2SKip Macy 	case SIOCSIFMEDIA:
184989e0f4d2SKip Macy 	case SIOCGIFMEDIA:
18500e509842SJustin T. Gibbs 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
185189e0f4d2SKip Macy 		break;
185289e0f4d2SKip Macy 	default:
185389e0f4d2SKip Macy 		error = ether_ioctl(ifp, cmd, data);
185489e0f4d2SKip Macy 	}
185589e0f4d2SKip Macy 
185689e0f4d2SKip Macy 	return (error);
185789e0f4d2SKip Macy }
185889e0f4d2SKip Macy 
185989e0f4d2SKip Macy static void
186089e0f4d2SKip Macy xn_stop(struct netfront_info *sc)
186189e0f4d2SKip Macy {
186289e0f4d2SKip Macy 	struct ifnet *ifp;
186389e0f4d2SKip Macy 
186489e0f4d2SKip Macy 	XN_LOCK_ASSERT(sc);
186589e0f4d2SKip Macy 
186689e0f4d2SKip Macy 	ifp = sc->xn_ifp;
186789e0f4d2SKip Macy 
186889e0f4d2SKip Macy 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
18690e509842SJustin T. Gibbs 	if_link_state_change(ifp, LINK_STATE_DOWN);
187089e0f4d2SKip Macy }
187189e0f4d2SKip Macy 
187296375eacSRoger Pau Monné static void
187396375eacSRoger Pau Monné xn_rebuild_rx_bufs(struct netfront_rxq *rxq)
187489e0f4d2SKip Macy {
187596375eacSRoger Pau Monné 	int requeue_idx, i;
187689e0f4d2SKip Macy 	grant_ref_t ref;
187789e0f4d2SKip Macy 	netif_rx_request_t *req;
187889e0f4d2SKip Macy 
187989e0f4d2SKip Macy 	for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
188089e0f4d2SKip Macy 		struct mbuf *m;
18813a6d1fcfSKip Macy 		u_long pfn;
188289e0f4d2SKip Macy 
188396375eacSRoger Pau Monné 		if (rxq->mbufs[i] == NULL)
188489e0f4d2SKip Macy 			continue;
188589e0f4d2SKip Macy 
188696375eacSRoger Pau Monné 		m = rxq->mbufs[requeue_idx] = xn_get_rx_mbuf(rxq, i);
188796375eacSRoger Pau Monné 		ref = rxq->grant_ref[requeue_idx] = xn_get_rx_ref(rxq, i);
1888931eeffaSKenneth D. Merry 
188996375eacSRoger Pau Monné 		req = RING_GET_REQUEST(&rxq->ring, requeue_idx);
18903a6d1fcfSKip Macy 		pfn = vtophys(mtod(m, vm_offset_t)) >> PAGE_SHIFT;
189189e0f4d2SKip Macy 
189289e0f4d2SKip Macy 		gnttab_grant_foreign_access_ref(ref,
189396375eacSRoger Pau Monné 		    xenbus_get_otherend_id(rxq->info->xbdev),
1894ed95805eSJohn Baldwin 		    pfn, 0);
1895d0f3a8b9SRoger Pau Monné 
189689e0f4d2SKip Macy 		req->gref = ref;
189789e0f4d2SKip Macy 		req->id   = requeue_idx;
189889e0f4d2SKip Macy 
189989e0f4d2SKip Macy 		requeue_idx++;
190089e0f4d2SKip Macy 	}
190189e0f4d2SKip Macy 
190296375eacSRoger Pau Monné 	rxq->ring.req_prod_pvt = requeue_idx;
190396375eacSRoger Pau Monné }
190489e0f4d2SKip Macy 
190596375eacSRoger Pau Monné /* START of Xenolinux helper functions adapted to FreeBSD */
190665671253SRoger Pau Monné static int
190796375eacSRoger Pau Monné xn_connect(struct netfront_info *np)
190896375eacSRoger Pau Monné {
190996375eacSRoger Pau Monné 	int i, error;
191096375eacSRoger Pau Monné 	u_int feature_rx_copy;
191196375eacSRoger Pau Monné 	struct netfront_rxq *rxq;
191296375eacSRoger Pau Monné 	struct netfront_txq *txq;
191396375eacSRoger Pau Monné 
191496375eacSRoger Pau Monné 	error = xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev),
191596375eacSRoger Pau Monné 	    "feature-rx-copy", NULL, "%u", &feature_rx_copy);
191696375eacSRoger Pau Monné 	if (error != 0)
191796375eacSRoger Pau Monné 		feature_rx_copy = 0;
191896375eacSRoger Pau Monné 
191996375eacSRoger Pau Monné 	/* We only support rx copy. */
192096375eacSRoger Pau Monné 	if (!feature_rx_copy)
192196375eacSRoger Pau Monné 		return (EPROTONOSUPPORT);
192296375eacSRoger Pau Monné 
192396375eacSRoger Pau Monné 	/* Recovery procedure: */
192496375eacSRoger Pau Monné 	error = talk_to_backend(np->xbdev, np);
192596375eacSRoger Pau Monné 	if (error != 0)
192696375eacSRoger Pau Monné 		return (error);
192796375eacSRoger Pau Monné 
192896375eacSRoger Pau Monné 	/* Step 1: Reinitialise variables. */
192996375eacSRoger Pau Monné 	xn_query_features(np);
193096375eacSRoger Pau Monné 	xn_configure_features(np);
193196375eacSRoger Pau Monné 
193296375eacSRoger Pau Monné 	/* Step 2: Release TX buffer */
193396375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
193496375eacSRoger Pau Monné 		txq = &np->txq[i];
193596375eacSRoger Pau Monné 		xn_release_tx_bufs(txq);
193696375eacSRoger Pau Monné 	}
193796375eacSRoger Pau Monné 
193896375eacSRoger Pau Monné 	/* Step 3: Rebuild the RX buffer freelist and the RX ring itself. */
193996375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
194096375eacSRoger Pau Monné 		rxq = &np->rxq[i];
194196375eacSRoger Pau Monné 		xn_rebuild_rx_bufs(rxq);
194296375eacSRoger Pau Monné 	}
194396375eacSRoger Pau Monné 
194496375eacSRoger Pau Monné 	/* Step 4: All public and private state should now be sane.  Get
194589e0f4d2SKip Macy 	 * ready to start sending and receiving packets and give the driver
194689e0f4d2SKip Macy 	 * domain a kick because we've probably just requeued some
194789e0f4d2SKip Macy 	 * packets.
194889e0f4d2SKip Macy 	 */
194989e0f4d2SKip Macy 	netfront_carrier_on(np);
19503c9d5940SRoger Pau Monné 	wakeup(np);
195165671253SRoger Pau Monné 
195265671253SRoger Pau Monné 	return (0);
195365671253SRoger Pau Monné }
195465671253SRoger Pau Monné 
195565671253SRoger Pau Monné static void
195665671253SRoger Pau Monné xn_kick_rings(struct netfront_info *np)
195765671253SRoger Pau Monné {
195865671253SRoger Pau Monné 	struct netfront_rxq *rxq;
195965671253SRoger Pau Monné 	struct netfront_txq *txq;
196065671253SRoger Pau Monné 	int i;
196165671253SRoger Pau Monné 
196296375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
196396375eacSRoger Pau Monné 		txq = &np->txq[i];
196465671253SRoger Pau Monné 		rxq = &np->rxq[i];
196596375eacSRoger Pau Monné 		xen_intr_signal(txq->xen_intr_handle);
196696375eacSRoger Pau Monné 		XN_TX_LOCK(txq);
196796375eacSRoger Pau Monné 		xn_txeof(txq);
196896375eacSRoger Pau Monné 		XN_TX_UNLOCK(txq);
19692568ee67SRoger Pau Monné 		XN_RX_LOCK(rxq);
197096375eacSRoger Pau Monné 		xn_alloc_rx_buffers(rxq);
19712568ee67SRoger Pau Monné 		XN_RX_UNLOCK(rxq);
197296375eacSRoger Pau Monné 	}
197389e0f4d2SKip Macy }
197489e0f4d2SKip Macy 
197589e0f4d2SKip Macy static void
1976578e4bf7SJustin T. Gibbs xn_query_features(struct netfront_info *np)
1977578e4bf7SJustin T. Gibbs {
1978578e4bf7SJustin T. Gibbs 	int val;
1979578e4bf7SJustin T. Gibbs 
1980578e4bf7SJustin T. Gibbs 	device_printf(np->xbdev, "backend features:");
1981578e4bf7SJustin T. Gibbs 
1982578e4bf7SJustin T. Gibbs 	if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev),
1983107cfbb7SRoger Pau Monné 		"feature-sg", NULL, "%d", &val) != 0)
1984578e4bf7SJustin T. Gibbs 		val = 0;
1985578e4bf7SJustin T. Gibbs 
1986578e4bf7SJustin T. Gibbs 	np->maxfrags = 1;
1987578e4bf7SJustin T. Gibbs 	if (val) {
1988578e4bf7SJustin T. Gibbs 		np->maxfrags = MAX_TX_REQ_FRAGS;
1989578e4bf7SJustin T. Gibbs 		printf(" feature-sg");
1990578e4bf7SJustin T. Gibbs 	}
1991578e4bf7SJustin T. Gibbs 
1992578e4bf7SJustin T. Gibbs 	if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev),
1993107cfbb7SRoger Pau Monné 		"feature-gso-tcpv4", NULL, "%d", &val) != 0)
1994578e4bf7SJustin T. Gibbs 		val = 0;
1995578e4bf7SJustin T. Gibbs 
1996578e4bf7SJustin T. Gibbs 	np->xn_ifp->if_capabilities &= ~(IFCAP_TSO4|IFCAP_LRO);
1997578e4bf7SJustin T. Gibbs 	if (val) {
1998578e4bf7SJustin T. Gibbs 		np->xn_ifp->if_capabilities |= IFCAP_TSO4|IFCAP_LRO;
1999578e4bf7SJustin T. Gibbs 		printf(" feature-gso-tcp4");
2000578e4bf7SJustin T. Gibbs 	}
2001578e4bf7SJustin T. Gibbs 
2002c2d12e5eSRoger Pau Monné 	/*
2003c2d12e5eSRoger Pau Monné 	 * HW CSUM offload is assumed to be available unless
2004c2d12e5eSRoger Pau Monné 	 * feature-no-csum-offload is set in xenstore.
2005c2d12e5eSRoger Pau Monné 	 */
2006c2d12e5eSRoger Pau Monné 	if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev),
2007c2d12e5eSRoger Pau Monné 		"feature-no-csum-offload", NULL, "%d", &val) != 0)
2008c2d12e5eSRoger Pau Monné 		val = 0;
2009c2d12e5eSRoger Pau Monné 
2010c2d12e5eSRoger Pau Monné 	np->xn_ifp->if_capabilities |= IFCAP_HWCSUM;
2011c2d12e5eSRoger Pau Monné 	if (val) {
2012c2d12e5eSRoger Pau Monné 		np->xn_ifp->if_capabilities &= ~(IFCAP_HWCSUM);
2013c2d12e5eSRoger Pau Monné 		printf(" feature-no-csum-offload");
2014c2d12e5eSRoger Pau Monné 	}
2015c2d12e5eSRoger Pau Monné 
2016578e4bf7SJustin T. Gibbs 	printf("\n");
2017578e4bf7SJustin T. Gibbs }
2018578e4bf7SJustin T. Gibbs 
2019cf9c09e1SJustin T. Gibbs static int
2020578e4bf7SJustin T. Gibbs xn_configure_features(struct netfront_info *np)
2021cf9c09e1SJustin T. Gibbs {
20226a8e9695SRoger Pau Monné 	int err, cap_enabled;
202396375eacSRoger Pau Monné #if (defined(INET) || defined(INET6))
202496375eacSRoger Pau Monné 	int i;
202596375eacSRoger Pau Monné #endif
2026c2d12e5eSRoger Pau Monné 	struct ifnet *ifp;
2027cf9c09e1SJustin T. Gibbs 
2028c2d12e5eSRoger Pau Monné 	ifp = np->xn_ifp;
2029cf9c09e1SJustin T. Gibbs 	err = 0;
20306a8e9695SRoger Pau Monné 
2031c2d12e5eSRoger Pau Monné 	if ((ifp->if_capenable & ifp->if_capabilities) == ifp->if_capenable) {
20326a8e9695SRoger Pau Monné 		/* Current options are available, no need to do anything. */
20336a8e9695SRoger Pau Monné 		return (0);
20346a8e9695SRoger Pau Monné 	}
20356a8e9695SRoger Pau Monné 
20366a8e9695SRoger Pau Monné 	/* Try to preserve as many options as possible. */
2037c2d12e5eSRoger Pau Monné 	cap_enabled = ifp->if_capenable;
2038c2d12e5eSRoger Pau Monné 	ifp->if_capenable = ifp->if_hwassist = 0;
20396a8e9695SRoger Pau Monné 
204008c9c2e0SRoger Pau Monné #if (defined(INET) || defined(INET6))
2041c2d12e5eSRoger Pau Monné 	if ((cap_enabled & IFCAP_LRO) != 0)
204296375eacSRoger Pau Monné 		for (i = 0; i < np->num_queues; i++)
204396375eacSRoger Pau Monné 			tcp_lro_free(&np->rxq[i].lro);
2044c2d12e5eSRoger Pau Monné 	if (xn_enable_lro &&
2045c2d12e5eSRoger Pau Monné 	    (ifp->if_capabilities & cap_enabled & IFCAP_LRO) != 0) {
2046c2d12e5eSRoger Pau Monné 	    	ifp->if_capenable |= IFCAP_LRO;
204796375eacSRoger Pau Monné 		for (i = 0; i < np->num_queues; i++) {
204896375eacSRoger Pau Monné 			err = tcp_lro_init(&np->rxq[i].lro);
204996375eacSRoger Pau Monné 			if (err != 0) {
2050c2d12e5eSRoger Pau Monné 				device_printf(np->xbdev,
2051c2d12e5eSRoger Pau Monné 				    "LRO initialization failed\n");
2052c2d12e5eSRoger Pau Monné 				ifp->if_capenable &= ~IFCAP_LRO;
2053c2d12e5eSRoger Pau Monné 				break;
2054c2d12e5eSRoger Pau Monné 			}
2055c2d12e5eSRoger Pau Monné 			np->rxq[i].lro.ifp = ifp;
2056cf9c09e1SJustin T. Gibbs 		}
2057cf9c09e1SJustin T. Gibbs 	}
2058c2d12e5eSRoger Pau Monné 	if ((ifp->if_capabilities & cap_enabled & IFCAP_TSO4) != 0) {
2059c2d12e5eSRoger Pau Monné 		ifp->if_capenable |= IFCAP_TSO4;
2060c2d12e5eSRoger Pau Monné 		ifp->if_hwassist |= CSUM_TSO;
2061578e4bf7SJustin T. Gibbs 	}
2062cf9c09e1SJustin T. Gibbs #endif
2063c2d12e5eSRoger Pau Monné 	if ((ifp->if_capabilities & cap_enabled & IFCAP_TXCSUM) != 0) {
2064c2d12e5eSRoger Pau Monné 		ifp->if_capenable |= IFCAP_TXCSUM;
20653c9d5940SRoger Pau Monné 		ifp->if_hwassist |= XN_CSUM_FEATURES;
2066c2d12e5eSRoger Pau Monné 	}
2067c2d12e5eSRoger Pau Monné 	if ((ifp->if_capabilities & cap_enabled & IFCAP_RXCSUM) != 0)
2068c2d12e5eSRoger Pau Monné 		ifp->if_capenable |= IFCAP_RXCSUM;
2069c2d12e5eSRoger Pau Monné 
2070cf9c09e1SJustin T. Gibbs 	return (err);
2071cf9c09e1SJustin T. Gibbs }
2072cf9c09e1SJustin T. Gibbs 
207396375eacSRoger Pau Monné static int
207496375eacSRoger Pau Monné xn_txq_mq_start_locked(struct netfront_txq *txq, struct mbuf *m)
207596375eacSRoger Pau Monné {
207696375eacSRoger Pau Monné 	struct netfront_info *np;
207796375eacSRoger Pau Monné 	struct ifnet *ifp;
207896375eacSRoger Pau Monné 	struct buf_ring *br;
207996375eacSRoger Pau Monné 	int error, notify;
208096375eacSRoger Pau Monné 
208196375eacSRoger Pau Monné 	np = txq->info;
208296375eacSRoger Pau Monné 	br = txq->br;
208396375eacSRoger Pau Monné 	ifp = np->xn_ifp;
208496375eacSRoger Pau Monné 	error = 0;
208596375eacSRoger Pau Monné 
208696375eacSRoger Pau Monné 	XN_TX_LOCK_ASSERT(txq);
208796375eacSRoger Pau Monné 
208896375eacSRoger Pau Monné 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
208996375eacSRoger Pau Monné 	    !netfront_carrier_ok(np)) {
209096375eacSRoger Pau Monné 		if (m != NULL)
209196375eacSRoger Pau Monné 			error = drbr_enqueue(ifp, br, m);
209296375eacSRoger Pau Monné 		return (error);
209396375eacSRoger Pau Monné 	}
209496375eacSRoger Pau Monné 
209596375eacSRoger Pau Monné 	if (m != NULL) {
209696375eacSRoger Pau Monné 		error = drbr_enqueue(ifp, br, m);
209796375eacSRoger Pau Monné 		if (error != 0)
209896375eacSRoger Pau Monné 			return (error);
209996375eacSRoger Pau Monné 	}
210096375eacSRoger Pau Monné 
210196375eacSRoger Pau Monné 	while ((m = drbr_peek(ifp, br)) != NULL) {
210296375eacSRoger Pau Monné 		if (!xn_tx_slot_available(txq)) {
210396375eacSRoger Pau Monné 			drbr_putback(ifp, br, m);
210496375eacSRoger Pau Monné 			break;
210596375eacSRoger Pau Monné 		}
210696375eacSRoger Pau Monné 
210796375eacSRoger Pau Monné 		error = xn_assemble_tx_request(txq, m);
210896375eacSRoger Pau Monné 		/* xn_assemble_tx_request always consumes the mbuf*/
210996375eacSRoger Pau Monné 		if (error != 0) {
211096375eacSRoger Pau Monné 			drbr_advance(ifp, br);
211196375eacSRoger Pau Monné 			break;
211296375eacSRoger Pau Monné 		}
211396375eacSRoger Pau Monné 
211496375eacSRoger Pau Monné 		RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&txq->ring, notify);
211596375eacSRoger Pau Monné 		if (notify)
211696375eacSRoger Pau Monné 			xen_intr_signal(txq->xen_intr_handle);
211796375eacSRoger Pau Monné 
211896375eacSRoger Pau Monné 		drbr_advance(ifp, br);
211996375eacSRoger Pau Monné 	}
212096375eacSRoger Pau Monné 
212196375eacSRoger Pau Monné 	if (RING_FULL(&txq->ring))
212296375eacSRoger Pau Monné 		txq->full = true;
212396375eacSRoger Pau Monné 
212496375eacSRoger Pau Monné 	return (0);
212596375eacSRoger Pau Monné }
212696375eacSRoger Pau Monné 
212796375eacSRoger Pau Monné static int
212896375eacSRoger Pau Monné xn_txq_mq_start(struct ifnet *ifp, struct mbuf *m)
212996375eacSRoger Pau Monné {
213096375eacSRoger Pau Monné 	struct netfront_info *np;
213196375eacSRoger Pau Monné 	struct netfront_txq *txq;
213296375eacSRoger Pau Monné 	int i, npairs, error;
213396375eacSRoger Pau Monné 
213496375eacSRoger Pau Monné 	np = ifp->if_softc;
213596375eacSRoger Pau Monné 	npairs = np->num_queues;
213696375eacSRoger Pau Monné 
2137339690b5SRoger Pau Monné 	if (!netfront_carrier_ok(np))
2138339690b5SRoger Pau Monné 		return (ENOBUFS);
2139339690b5SRoger Pau Monné 
2140c21b47d8SRoger Pau Monné 	KASSERT(npairs != 0, ("called with 0 available queues"));
2141c21b47d8SRoger Pau Monné 
214296375eacSRoger Pau Monné 	/* check if flowid is set */
214396375eacSRoger Pau Monné 	if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
214496375eacSRoger Pau Monné 		i = m->m_pkthdr.flowid % npairs;
214596375eacSRoger Pau Monné 	else
214696375eacSRoger Pau Monné 		i = curcpu % npairs;
214796375eacSRoger Pau Monné 
214896375eacSRoger Pau Monné 	txq = &np->txq[i];
214996375eacSRoger Pau Monné 
215096375eacSRoger Pau Monné 	if (XN_TX_TRYLOCK(txq) != 0) {
215196375eacSRoger Pau Monné 		error = xn_txq_mq_start_locked(txq, m);
215296375eacSRoger Pau Monné 		XN_TX_UNLOCK(txq);
215396375eacSRoger Pau Monné 	} else {
215496375eacSRoger Pau Monné 		error = drbr_enqueue(ifp, txq->br, m);
215596375eacSRoger Pau Monné 		taskqueue_enqueue(txq->tq, &txq->defrtask);
215696375eacSRoger Pau Monné 	}
215796375eacSRoger Pau Monné 
215896375eacSRoger Pau Monné 	return (error);
215996375eacSRoger Pau Monné }
216096375eacSRoger Pau Monné 
216196375eacSRoger Pau Monné static void
216296375eacSRoger Pau Monné xn_qflush(struct ifnet *ifp)
216396375eacSRoger Pau Monné {
216496375eacSRoger Pau Monné 	struct netfront_info *np;
216596375eacSRoger Pau Monné 	struct netfront_txq *txq;
216696375eacSRoger Pau Monné 	struct mbuf *m;
216796375eacSRoger Pau Monné 	int i;
216896375eacSRoger Pau Monné 
216996375eacSRoger Pau Monné 	np = ifp->if_softc;
217096375eacSRoger Pau Monné 
217196375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
217296375eacSRoger Pau Monné 		txq = &np->txq[i];
217396375eacSRoger Pau Monné 
217496375eacSRoger Pau Monné 		XN_TX_LOCK(txq);
217596375eacSRoger Pau Monné 		while ((m = buf_ring_dequeue_sc(txq->br)) != NULL)
217696375eacSRoger Pau Monné 			m_freem(m);
217796375eacSRoger Pau Monné 		XN_TX_UNLOCK(txq);
217896375eacSRoger Pau Monné 	}
217996375eacSRoger Pau Monné 
218096375eacSRoger Pau Monné 	if_qflush(ifp);
218196375eacSRoger Pau Monné }
218296375eacSRoger Pau Monné 
218376acc41fSJustin T. Gibbs /**
218476acc41fSJustin T. Gibbs  * Create a network device.
218576acc41fSJustin T. Gibbs  * @param dev  Newbus device representing this virtual NIC.
218689e0f4d2SKip Macy  */
218723dc5621SKip Macy int
218823dc5621SKip Macy create_netdev(device_t dev)
218989e0f4d2SKip Macy {
219089e0f4d2SKip Macy 	struct netfront_info *np;
219189e0f4d2SKip Macy 	int err;
219289e0f4d2SKip Macy 	struct ifnet *ifp;
219389e0f4d2SKip Macy 
219423dc5621SKip Macy 	np = device_get_softc(dev);
219589e0f4d2SKip Macy 
219689e0f4d2SKip Macy 	np->xbdev         = dev;
219789e0f4d2SKip Macy 
2198177e3f13SRoger Pau Monné 	mtx_init(&np->sc_lock, "xnsc", "netfront softc lock", MTX_DEF);
21990e509842SJustin T. Gibbs 
22000e509842SJustin T. Gibbs 	ifmedia_init(&np->sc_media, 0, xn_ifmedia_upd, xn_ifmedia_sts);
22010e509842SJustin T. Gibbs 	ifmedia_add(&np->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
22020e509842SJustin T. Gibbs 	ifmedia_set(&np->sc_media, IFM_ETHER|IFM_MANUAL);
22030e509842SJustin T. Gibbs 
220489e0f4d2SKip Macy 	err = xen_net_read_mac(dev, np->mac);
220596375eacSRoger Pau Monné 	if (err != 0)
22061a2928b7SRoger Pau Monné 		goto error;
220789e0f4d2SKip Macy 
220889e0f4d2SKip Macy 	/* Set up ifnet structure */
220923dc5621SKip Macy 	ifp = np->xn_ifp = if_alloc(IFT_ETHER);
221089e0f4d2SKip Macy     	ifp->if_softc = np;
221123dc5621SKip Macy     	if_initname(ifp, "xn",  device_get_unit(dev));
22123a6d1fcfSKip Macy     	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
221389e0f4d2SKip Macy     	ifp->if_ioctl = xn_ioctl;
221496375eacSRoger Pau Monné 
221596375eacSRoger Pau Monné 	ifp->if_transmit = xn_txq_mq_start;
221696375eacSRoger Pau Monné 	ifp->if_qflush = xn_qflush;
221796375eacSRoger Pau Monné 
221889e0f4d2SKip Macy     	ifp->if_init = xn_ifinit;
221989e0f4d2SKip Macy 
222089e0f4d2SKip Macy     	ifp->if_hwassist = XN_CSUM_FEATURES;
2221c2d12e5eSRoger Pau Monné 	/* Enable all supported features at device creation. */
2222c2d12e5eSRoger Pau Monné 	ifp->if_capenable = ifp->if_capabilities =
2223c2d12e5eSRoger Pau Monné 	    IFCAP_HWCSUM|IFCAP_TSO4|IFCAP_LRO;
22249fd573c3SHans Petter Selasky 	ifp->if_hw_tsomax = 65536 - (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
22259fd573c3SHans Petter Selasky 	ifp->if_hw_tsomaxsegcount = MAX_TX_REQ_FRAGS;
22269fd573c3SHans Petter Selasky 	ifp->if_hw_tsomaxsegsize = PAGE_SIZE;
222789e0f4d2SKip Macy 
222889e0f4d2SKip Macy     	ether_ifattach(ifp, np->mac);
222989e0f4d2SKip Macy 	netfront_carrier_off(np);
223089e0f4d2SKip Macy 
223189e0f4d2SKip Macy 	return (0);
223289e0f4d2SKip Macy 
22331a2928b7SRoger Pau Monné error:
22341a2928b7SRoger Pau Monné 	KASSERT(err != 0, ("Error path with no error code specified"));
2235ffa06904SJustin T. Gibbs 	return (err);
223689e0f4d2SKip Macy }
223789e0f4d2SKip Macy 
22380e509842SJustin T. Gibbs static int
22390e509842SJustin T. Gibbs netfront_detach(device_t dev)
224089e0f4d2SKip Macy {
224123dc5621SKip Macy 	struct netfront_info *info = device_get_softc(dev);
224289e0f4d2SKip Macy 
224323dc5621SKip Macy 	DPRINTK("%s\n", xenbus_get_node(dev));
224489e0f4d2SKip Macy 
224589e0f4d2SKip Macy 	netif_free(info);
224689e0f4d2SKip Macy 
224789e0f4d2SKip Macy 	return 0;
224889e0f4d2SKip Macy }
224989e0f4d2SKip Macy 
22500e509842SJustin T. Gibbs static void
225196375eacSRoger Pau Monné netif_free(struct netfront_info *np)
225289e0f4d2SKip Macy {
225396375eacSRoger Pau Monné 
225496375eacSRoger Pau Monné 	XN_LOCK(np);
225596375eacSRoger Pau Monné 	xn_stop(np);
225696375eacSRoger Pau Monné 	XN_UNLOCK(np);
225796375eacSRoger Pau Monné 	netif_disconnect_backend(np);
2258c21b47d8SRoger Pau Monné 	ether_ifdetach(np->xn_ifp);
225996375eacSRoger Pau Monné 	free(np->rxq, M_DEVBUF);
226096375eacSRoger Pau Monné 	free(np->txq, M_DEVBUF);
226196375eacSRoger Pau Monné 	if_free(np->xn_ifp);
226296375eacSRoger Pau Monné 	np->xn_ifp = NULL;
226396375eacSRoger Pau Monné 	ifmedia_removeall(&np->sc_media);
226489e0f4d2SKip Macy }
226589e0f4d2SKip Macy 
22660e509842SJustin T. Gibbs static void
226796375eacSRoger Pau Monné netif_disconnect_backend(struct netfront_info *np)
226889e0f4d2SKip Macy {
226996375eacSRoger Pau Monné 	u_int i;
22703a6d1fcfSKip Macy 
227196375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
227296375eacSRoger Pau Monné 		XN_RX_LOCK(&np->rxq[i]);
227396375eacSRoger Pau Monné 		XN_TX_LOCK(&np->txq[i]);
227496375eacSRoger Pau Monné 	}
227596375eacSRoger Pau Monné 	netfront_carrier_off(np);
227696375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
227796375eacSRoger Pau Monné 		XN_RX_UNLOCK(&np->rxq[i]);
227896375eacSRoger Pau Monné 		XN_TX_UNLOCK(&np->txq[i]);
227989e0f4d2SKip Macy 	}
228089e0f4d2SKip Macy 
228196375eacSRoger Pau Monné 	for (i = 0; i < np->num_queues; i++) {
228296375eacSRoger Pau Monné 		disconnect_rxq(&np->rxq[i]);
228396375eacSRoger Pau Monné 		disconnect_txq(&np->txq[i]);
2284cf9c09e1SJustin T. Gibbs 	}
228589e0f4d2SKip Macy }
228689e0f4d2SKip Macy 
22870e509842SJustin T. Gibbs static int
22880e509842SJustin T. Gibbs xn_ifmedia_upd(struct ifnet *ifp)
22890e509842SJustin T. Gibbs {
229096375eacSRoger Pau Monné 
22910e509842SJustin T. Gibbs 	return (0);
22920e509842SJustin T. Gibbs }
22930e509842SJustin T. Gibbs 
22940e509842SJustin T. Gibbs static void
22950e509842SJustin T. Gibbs xn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
22960e509842SJustin T. Gibbs {
229796375eacSRoger Pau Monné 
22980e509842SJustin T. Gibbs 	ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE;
22990e509842SJustin T. Gibbs 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
23000e509842SJustin T. Gibbs }
23010e509842SJustin T. Gibbs 
230289e0f4d2SKip Macy /* ** Driver registration ** */
230323dc5621SKip Macy static device_method_t netfront_methods[] = {
230423dc5621SKip Macy 	/* Device interface */
230523dc5621SKip Macy 	DEVMETHOD(device_probe,         netfront_probe),
230623dc5621SKip Macy 	DEVMETHOD(device_attach,        netfront_attach),
230723dc5621SKip Macy 	DEVMETHOD(device_detach,        netfront_detach),
230823dc5621SKip Macy 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
2309cf9c09e1SJustin T. Gibbs 	DEVMETHOD(device_suspend,       netfront_suspend),
231023dc5621SKip Macy 	DEVMETHOD(device_resume,        netfront_resume),
231189e0f4d2SKip Macy 
231223dc5621SKip Macy 	/* Xenbus interface */
2313ff662b5cSJustin T. Gibbs 	DEVMETHOD(xenbus_otherend_changed, netfront_backend_changed),
231489e0f4d2SKip Macy 
23156f9767acSMarius Strobl 	DEVMETHOD_END
231689e0f4d2SKip Macy };
231789e0f4d2SKip Macy 
231823dc5621SKip Macy static driver_t netfront_driver = {
231923dc5621SKip Macy 	"xn",
232023dc5621SKip Macy 	netfront_methods,
232123dc5621SKip Macy 	sizeof(struct netfront_info),
232289e0f4d2SKip Macy };
232323dc5621SKip Macy devclass_t netfront_devclass;
232489e0f4d2SKip Macy 
23256f9767acSMarius Strobl DRIVER_MODULE(xe, xenbusb_front, netfront_driver, netfront_devclass, NULL,
23266f9767acSMarius Strobl     NULL);
2327