xref: /freebsd/sys/dev/xen/netfront/netfront.c (revision f909bbb4dbfa4b4c4c68375e10b3722fd6d2b258)
18e0ad55aSJoel Dahl /*-
289e0f4d2SKip Macy  * Copyright (c) 2004-2006 Kip Macy
389e0f4d2SKip Macy  * All rights reserved.
489e0f4d2SKip Macy  *
58e0ad55aSJoel Dahl  * Redistribution and use in source and binary forms, with or without
68e0ad55aSJoel Dahl  * modification, are permitted provided that the following conditions
78e0ad55aSJoel Dahl  * are met:
88e0ad55aSJoel Dahl  * 1. Redistributions of source code must retain the above copyright
98e0ad55aSJoel Dahl  *    notice, this list of conditions and the following disclaimer.
108e0ad55aSJoel Dahl  * 2. Redistributions in binary form must reproduce the above copyright
118e0ad55aSJoel Dahl  *    notice, this list of conditions and the following disclaimer in the
128e0ad55aSJoel Dahl  *    documentation and/or other materials provided with the distribution.
1389e0f4d2SKip Macy  *
148e0ad55aSJoel Dahl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
158e0ad55aSJoel Dahl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
168e0ad55aSJoel Dahl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
178e0ad55aSJoel Dahl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
188e0ad55aSJoel Dahl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
198e0ad55aSJoel Dahl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
208e0ad55aSJoel Dahl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
218e0ad55aSJoel Dahl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
228e0ad55aSJoel Dahl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
238e0ad55aSJoel Dahl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
248e0ad55aSJoel Dahl  * SUCH DAMAGE.
2589e0f4d2SKip Macy  */
2689e0f4d2SKip Macy 
2789e0f4d2SKip Macy #include <sys/cdefs.h>
2889e0f4d2SKip Macy __FBSDID("$FreeBSD$");
2989e0f4d2SKip Macy 
30a0ae8f04SBjoern A. Zeeb #include "opt_inet.h"
31*f909bbb4SGleb Smirnoff #include "opt_inet6.h"
32a0ae8f04SBjoern A. Zeeb 
3389e0f4d2SKip Macy #include <sys/param.h>
3489e0f4d2SKip Macy #include <sys/systm.h>
3589e0f4d2SKip Macy #include <sys/sockio.h>
3689e0f4d2SKip Macy #include <sys/mbuf.h>
3789e0f4d2SKip Macy #include <sys/malloc.h>
3823dc5621SKip Macy #include <sys/module.h>
3989e0f4d2SKip Macy #include <sys/kernel.h>
4089e0f4d2SKip Macy #include <sys/socket.h>
4112678024SDoug Rabson #include <sys/sysctl.h>
4289e0f4d2SKip Macy #include <sys/queue.h>
438cb07992SAdrian Chadd #include <sys/lock.h>
4489e0f4d2SKip Macy #include <sys/sx.h>
4589e0f4d2SKip Macy 
4689e0f4d2SKip Macy #include <net/if.h>
4777374386SGleb Smirnoff #include <net/if_var.h>
4889e0f4d2SKip Macy #include <net/if_arp.h>
4989e0f4d2SKip Macy #include <net/ethernet.h>
5089e0f4d2SKip Macy #include <net/if_dl.h>
5189e0f4d2SKip Macy #include <net/if_media.h>
5289e0f4d2SKip Macy 
5389e0f4d2SKip Macy #include <net/bpf.h>
5489e0f4d2SKip Macy 
5589e0f4d2SKip Macy #include <net/if_types.h>
5689e0f4d2SKip Macy 
5789e0f4d2SKip Macy #include <netinet/in_systm.h>
5889e0f4d2SKip Macy #include <netinet/in.h>
5989e0f4d2SKip Macy #include <netinet/ip.h>
6089e0f4d2SKip Macy #include <netinet/if_ether.h>
6112678024SDoug Rabson #if __FreeBSD_version >= 700000
6212678024SDoug Rabson #include <netinet/tcp.h>
6312678024SDoug Rabson #include <netinet/tcp_lro.h>
6412678024SDoug Rabson #endif
6589e0f4d2SKip Macy 
6689e0f4d2SKip Macy #include <vm/vm.h>
6789e0f4d2SKip Macy #include <vm/pmap.h>
6889e0f4d2SKip Macy 
6989e0f4d2SKip Macy #include <machine/clock.h>      /* for DELAY */
7089e0f4d2SKip Macy #include <machine/bus.h>
7189e0f4d2SKip Macy #include <machine/resource.h>
7289e0f4d2SKip Macy #include <machine/frame.h>
73980c7178SKip Macy #include <machine/vmparam.h>
7489e0f4d2SKip Macy 
7589e0f4d2SKip Macy #include <sys/bus.h>
7689e0f4d2SKip Macy #include <sys/rman.h>
7789e0f4d2SKip Macy 
7889e0f4d2SKip Macy #include <machine/intr_machdep.h>
7989e0f4d2SKip Macy 
8076acc41fSJustin T. Gibbs #include <xen/xen-os.h>
813a6d1fcfSKip Macy #include <xen/hypervisor.h>
823a6d1fcfSKip Macy #include <xen/xen_intr.h>
8389e0f4d2SKip Macy #include <xen/gnttab.h>
8489e0f4d2SKip Macy #include <xen/interface/memory.h>
8589e0f4d2SKip Macy #include <xen/interface/io/netif.h>
8623dc5621SKip Macy #include <xen/xenbus/xenbusvar.h>
8789e0f4d2SKip Macy 
8876acc41fSJustin T. Gibbs #include <machine/xen/xenvar.h>
8976acc41fSJustin T. Gibbs 
9012678024SDoug Rabson #include <dev/xen/netfront/mbufq.h>
9112678024SDoug Rabson 
9223dc5621SKip Macy #include "xenbus_if.h"
9389e0f4d2SKip Macy 
94578e4bf7SJustin T. Gibbs /* Features supported by all backends.  TSO and LRO can be negotiated */
95578e4bf7SJustin T. Gibbs #define XN_CSUM_FEATURES	(CSUM_TCP | CSUM_UDP)
9612678024SDoug Rabson 
9789e0f4d2SKip Macy #define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE)
9889e0f4d2SKip Macy #define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE)
9989e0f4d2SKip Macy 
10012678024SDoug Rabson #if __FreeBSD_version >= 700000
10112678024SDoug Rabson /*
10212678024SDoug Rabson  * Should the driver do LRO on the RX end
10312678024SDoug Rabson  *  this can be toggled on the fly, but the
10412678024SDoug Rabson  *  interface must be reset (down/up) for it
10512678024SDoug Rabson  *  to take effect.
10612678024SDoug Rabson  */
10712678024SDoug Rabson static int xn_enable_lro = 1;
10812678024SDoug Rabson TUNABLE_INT("hw.xn.enable_lro", &xn_enable_lro);
10912678024SDoug Rabson #else
11012678024SDoug Rabson 
11112678024SDoug Rabson #define IFCAP_TSO4	0
11212678024SDoug Rabson #define CSUM_TSO	0
11312678024SDoug Rabson 
11412678024SDoug Rabson #endif
11512678024SDoug Rabson 
11689e0f4d2SKip Macy #ifdef CONFIG_XEN
11789e0f4d2SKip Macy static int MODPARM_rx_copy = 0;
11889e0f4d2SKip Macy module_param_named(rx_copy, MODPARM_rx_copy, bool, 0);
11989e0f4d2SKip Macy MODULE_PARM_DESC(rx_copy, "Copy packets from network card (rather than flip)");
12089e0f4d2SKip Macy static int MODPARM_rx_flip = 0;
12189e0f4d2SKip Macy module_param_named(rx_flip, MODPARM_rx_flip, bool, 0);
12289e0f4d2SKip Macy MODULE_PARM_DESC(rx_flip, "Flip packets from network card (rather than copy)");
12389e0f4d2SKip Macy #else
12489e0f4d2SKip Macy static const int MODPARM_rx_copy = 1;
12589e0f4d2SKip Macy static const int MODPARM_rx_flip = 0;
12689e0f4d2SKip Macy #endif
12789e0f4d2SKip Macy 
128931eeffaSKenneth D. Merry /**
129931eeffaSKenneth D. Merry  * \brief The maximum allowed data fragments in a single transmit
130931eeffaSKenneth D. Merry  *        request.
131931eeffaSKenneth D. Merry  *
132931eeffaSKenneth D. Merry  * This limit is imposed by the backend driver.  We assume here that
133931eeffaSKenneth D. Merry  * we are dealing with a Linux driver domain and have set our limit
134931eeffaSKenneth D. Merry  * to mirror the Linux MAX_SKB_FRAGS constant.
135931eeffaSKenneth D. Merry  */
136931eeffaSKenneth D. Merry #define	MAX_TX_REQ_FRAGS (65536 / PAGE_SIZE + 2)
13733e0730eSAndre Oppermann #define	NF_TSO_MAXBURST ((IP_MAXPACKET / PAGE_SIZE) * MCLBYTES)
138931eeffaSKenneth D. Merry 
13989e0f4d2SKip Macy #define RX_COPY_THRESHOLD 256
14089e0f4d2SKip Macy 
14189e0f4d2SKip Macy #define net_ratelimit() 0
14289e0f4d2SKip Macy 
14389e0f4d2SKip Macy struct netfront_info;
14489e0f4d2SKip Macy struct netfront_rx_info;
14589e0f4d2SKip Macy 
14689e0f4d2SKip Macy static void xn_txeof(struct netfront_info *);
14789e0f4d2SKip Macy static void xn_rxeof(struct netfront_info *);
14889e0f4d2SKip Macy static void network_alloc_rx_buffers(struct netfront_info *);
14989e0f4d2SKip Macy 
15089e0f4d2SKip Macy static void xn_tick_locked(struct netfront_info *);
15189e0f4d2SKip Macy static void xn_tick(void *);
15289e0f4d2SKip Macy 
15389e0f4d2SKip Macy static void xn_intr(void *);
154931eeffaSKenneth D. Merry static inline int xn_count_frags(struct mbuf *m);
155931eeffaSKenneth D. Merry static int  xn_assemble_tx_request(struct netfront_info *sc,
156931eeffaSKenneth D. Merry 				   struct mbuf *m_head);
15789e0f4d2SKip Macy static void xn_start_locked(struct ifnet *);
15889e0f4d2SKip Macy static void xn_start(struct ifnet *);
15989e0f4d2SKip Macy static int  xn_ioctl(struct ifnet *, u_long, caddr_t);
16089e0f4d2SKip Macy static void xn_ifinit_locked(struct netfront_info *);
16189e0f4d2SKip Macy static void xn_ifinit(void *);
16289e0f4d2SKip Macy static void xn_stop(struct netfront_info *);
163578e4bf7SJustin T. Gibbs static void xn_query_features(struct netfront_info *np);
164578e4bf7SJustin T. Gibbs static int  xn_configure_features(struct netfront_info *np);
16589e0f4d2SKip Macy #ifdef notyet
16689e0f4d2SKip Macy static void xn_watchdog(struct ifnet *);
16789e0f4d2SKip Macy #endif
16889e0f4d2SKip Macy 
16989e0f4d2SKip Macy #ifdef notyet
17023dc5621SKip Macy static void netfront_closing(device_t dev);
17189e0f4d2SKip Macy #endif
17289e0f4d2SKip Macy static void netif_free(struct netfront_info *info);
17323dc5621SKip Macy static int netfront_detach(device_t dev);
17489e0f4d2SKip Macy 
17523dc5621SKip Macy static int talk_to_backend(device_t dev, struct netfront_info *info);
17623dc5621SKip Macy static int create_netdev(device_t dev);
17789e0f4d2SKip Macy static void netif_disconnect_backend(struct netfront_info *info);
17823dc5621SKip Macy static int setup_device(device_t dev, struct netfront_info *info);
179cf9c09e1SJustin T. Gibbs static void free_ring(int *ref, void *ring_ptr_ref);
18089e0f4d2SKip Macy 
1810e509842SJustin T. Gibbs static int  xn_ifmedia_upd(struct ifnet *ifp);
1820e509842SJustin T. Gibbs static void xn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
1830e509842SJustin T. Gibbs 
18489e0f4d2SKip Macy /* Xenolinux helper functions */
18523dc5621SKip Macy int network_connect(struct netfront_info *);
18689e0f4d2SKip Macy 
18789e0f4d2SKip Macy static void xn_free_rx_ring(struct netfront_info *);
18889e0f4d2SKip Macy 
18989e0f4d2SKip Macy static void xn_free_tx_ring(struct netfront_info *);
19089e0f4d2SKip Macy 
19189e0f4d2SKip Macy static int xennet_get_responses(struct netfront_info *np,
192931eeffaSKenneth D. Merry 	struct netfront_rx_info *rinfo, RING_IDX rp, RING_IDX *cons,
193931eeffaSKenneth D. Merry 	struct mbuf **list, int *pages_flipped_p);
19489e0f4d2SKip Macy 
19589e0f4d2SKip Macy #define virt_to_mfn(x) (vtomach(x) >> PAGE_SHIFT)
19689e0f4d2SKip Macy 
19789e0f4d2SKip Macy #define INVALID_P2M_ENTRY (~0UL)
19889e0f4d2SKip Macy 
19989e0f4d2SKip Macy /*
20089e0f4d2SKip Macy  * Mbuf pointers. We need these to keep track of the virtual addresses
20189e0f4d2SKip Macy  * of our mbuf chains since we can only convert from virtual to physical,
20289e0f4d2SKip Macy  * not the other way around.  The size must track the free index arrays.
20389e0f4d2SKip Macy  */
20489e0f4d2SKip Macy struct xn_chain_data {
20589e0f4d2SKip Macy 	struct mbuf    *xn_tx_chain[NET_TX_RING_SIZE+1];
206a4ec37f5SAdrian Chadd 	int		xn_tx_chain_cnt;
20789e0f4d2SKip Macy 	struct mbuf    *xn_rx_chain[NET_RX_RING_SIZE+1];
20889e0f4d2SKip Macy };
20989e0f4d2SKip Macy 
21089e0f4d2SKip Macy struct net_device_stats
21189e0f4d2SKip Macy {
21289e0f4d2SKip Macy 	u_long	rx_packets;		/* total packets received	*/
21389e0f4d2SKip Macy 	u_long	tx_packets;		/* total packets transmitted	*/
21489e0f4d2SKip Macy 	u_long	rx_bytes;		/* total bytes received 	*/
21589e0f4d2SKip Macy 	u_long	tx_bytes;		/* total bytes transmitted	*/
21689e0f4d2SKip Macy 	u_long	rx_errors;		/* bad packets received		*/
21789e0f4d2SKip Macy 	u_long	tx_errors;		/* packet transmit problems	*/
21889e0f4d2SKip Macy 	u_long	rx_dropped;		/* no space in linux buffers	*/
21989e0f4d2SKip Macy 	u_long	tx_dropped;		/* no space available in linux	*/
22089e0f4d2SKip Macy 	u_long	multicast;		/* multicast packets received	*/
22189e0f4d2SKip Macy 	u_long	collisions;
22289e0f4d2SKip Macy 
22389e0f4d2SKip Macy 	/* detailed rx_errors: */
22489e0f4d2SKip Macy 	u_long	rx_length_errors;
22589e0f4d2SKip Macy 	u_long	rx_over_errors;		/* receiver ring buff overflow	*/
22689e0f4d2SKip Macy 	u_long	rx_crc_errors;		/* recved pkt with crc error	*/
22789e0f4d2SKip Macy 	u_long	rx_frame_errors;	/* recv'd frame alignment error */
22889e0f4d2SKip Macy 	u_long	rx_fifo_errors;		/* recv'r fifo overrun		*/
22989e0f4d2SKip Macy 	u_long	rx_missed_errors;	/* receiver missed packet	*/
23089e0f4d2SKip Macy 
23189e0f4d2SKip Macy 	/* detailed tx_errors */
23289e0f4d2SKip Macy 	u_long	tx_aborted_errors;
23389e0f4d2SKip Macy 	u_long	tx_carrier_errors;
23489e0f4d2SKip Macy 	u_long	tx_fifo_errors;
23589e0f4d2SKip Macy 	u_long	tx_heartbeat_errors;
23689e0f4d2SKip Macy 	u_long	tx_window_errors;
23789e0f4d2SKip Macy 
23889e0f4d2SKip Macy 	/* for cslip etc */
23989e0f4d2SKip Macy 	u_long	rx_compressed;
24089e0f4d2SKip Macy 	u_long	tx_compressed;
24189e0f4d2SKip Macy };
24289e0f4d2SKip Macy 
24389e0f4d2SKip Macy struct netfront_info {
24489e0f4d2SKip Macy 	struct ifnet *xn_ifp;
24512678024SDoug Rabson #if __FreeBSD_version >= 700000
24612678024SDoug Rabson 	struct lro_ctrl xn_lro;
24712678024SDoug Rabson #endif
24889e0f4d2SKip Macy 
24989e0f4d2SKip Macy 	struct net_device_stats stats;
25089e0f4d2SKip Macy 	u_int tx_full;
25189e0f4d2SKip Macy 
25289e0f4d2SKip Macy 	netif_tx_front_ring_t tx;
25389e0f4d2SKip Macy 	netif_rx_front_ring_t rx;
25489e0f4d2SKip Macy 
25589e0f4d2SKip Macy 	struct mtx   tx_lock;
25689e0f4d2SKip Macy 	struct mtx   rx_lock;
257227ca257SKip Macy 	struct mtx   sc_lock;
25889e0f4d2SKip Macy 
25976acc41fSJustin T. Gibbs 	xen_intr_handle_t xen_intr_handle;
26089e0f4d2SKip Macy 	u_int copying_receiver;
26189e0f4d2SKip Macy 	u_int carrier;
262578e4bf7SJustin T. Gibbs 	u_int maxfrags;
26389e0f4d2SKip Macy 
26489e0f4d2SKip Macy 	/* Receive-ring batched refills. */
26589e0f4d2SKip Macy #define RX_MIN_TARGET 32
26689e0f4d2SKip Macy #define RX_MAX_TARGET NET_RX_RING_SIZE
2670e509842SJustin T. Gibbs 	int rx_min_target;
2680e509842SJustin T. Gibbs 	int rx_max_target;
2690e509842SJustin T. Gibbs 	int rx_target;
27089e0f4d2SKip Macy 
27189e0f4d2SKip Macy 	grant_ref_t gref_tx_head;
27289e0f4d2SKip Macy 	grant_ref_t grant_tx_ref[NET_TX_RING_SIZE + 1];
27389e0f4d2SKip Macy 	grant_ref_t gref_rx_head;
27489e0f4d2SKip Macy 	grant_ref_t grant_rx_ref[NET_TX_RING_SIZE + 1];
27589e0f4d2SKip Macy 
27623dc5621SKip Macy 	device_t		xbdev;
27789e0f4d2SKip Macy 	int			tx_ring_ref;
27889e0f4d2SKip Macy 	int			rx_ring_ref;
27989e0f4d2SKip Macy 	uint8_t			mac[ETHER_ADDR_LEN];
28089e0f4d2SKip Macy 	struct xn_chain_data	xn_cdata;	/* mbufs */
28189e0f4d2SKip Macy 	struct mbuf_head	xn_rx_batch;	/* head of the batch queue */
28289e0f4d2SKip Macy 
28389e0f4d2SKip Macy 	int			xn_if_flags;
28489e0f4d2SKip Macy 	struct callout	        xn_stat_ch;
28589e0f4d2SKip Macy 
28689e0f4d2SKip Macy 	u_long			rx_pfn_array[NET_RX_RING_SIZE];
28789e0f4d2SKip Macy 	multicall_entry_t	rx_mcl[NET_RX_RING_SIZE+1];
28889e0f4d2SKip Macy 	mmu_update_t		rx_mmu[NET_RX_RING_SIZE];
2890e509842SJustin T. Gibbs 	struct ifmedia		sc_media;
29089e0f4d2SKip Macy };
29189e0f4d2SKip Macy 
29289e0f4d2SKip Macy #define rx_mbufs xn_cdata.xn_rx_chain
29389e0f4d2SKip Macy #define tx_mbufs xn_cdata.xn_tx_chain
29489e0f4d2SKip Macy 
29589e0f4d2SKip Macy #define XN_LOCK_INIT(_sc, _name) \
29689e0f4d2SKip Macy         mtx_init(&(_sc)->tx_lock, #_name"_tx", "network transmit lock", MTX_DEF); \
29789e0f4d2SKip Macy         mtx_init(&(_sc)->rx_lock, #_name"_rx", "network receive lock", MTX_DEF);  \
298227ca257SKip Macy         mtx_init(&(_sc)->sc_lock, #_name"_sc", "netfront softc lock", MTX_DEF)
29989e0f4d2SKip Macy 
30089e0f4d2SKip Macy #define XN_RX_LOCK(_sc)           mtx_lock(&(_sc)->rx_lock)
30189e0f4d2SKip Macy #define XN_RX_UNLOCK(_sc)         mtx_unlock(&(_sc)->rx_lock)
30289e0f4d2SKip Macy 
30389e0f4d2SKip Macy #define XN_TX_LOCK(_sc)           mtx_lock(&(_sc)->tx_lock)
30489e0f4d2SKip Macy #define XN_TX_UNLOCK(_sc)         mtx_unlock(&(_sc)->tx_lock)
30589e0f4d2SKip Macy 
306227ca257SKip Macy #define XN_LOCK(_sc)           mtx_lock(&(_sc)->sc_lock);
307227ca257SKip Macy #define XN_UNLOCK(_sc)         mtx_unlock(&(_sc)->sc_lock);
30889e0f4d2SKip Macy 
309227ca257SKip Macy #define XN_LOCK_ASSERT(_sc)    mtx_assert(&(_sc)->sc_lock, MA_OWNED);
31089e0f4d2SKip Macy #define XN_RX_LOCK_ASSERT(_sc)    mtx_assert(&(_sc)->rx_lock, MA_OWNED);
31189e0f4d2SKip Macy #define XN_TX_LOCK_ASSERT(_sc)    mtx_assert(&(_sc)->tx_lock, MA_OWNED);
31289e0f4d2SKip Macy #define XN_LOCK_DESTROY(_sc)   mtx_destroy(&(_sc)->rx_lock); \
31389e0f4d2SKip Macy                                mtx_destroy(&(_sc)->tx_lock); \
314227ca257SKip Macy                                mtx_destroy(&(_sc)->sc_lock);
31589e0f4d2SKip Macy 
31689e0f4d2SKip Macy struct netfront_rx_info {
31789e0f4d2SKip Macy 	struct netif_rx_response rx;
31889e0f4d2SKip Macy 	struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
31989e0f4d2SKip Macy };
32089e0f4d2SKip Macy 
32189e0f4d2SKip Macy #define netfront_carrier_on(netif)	((netif)->carrier = 1)
32289e0f4d2SKip Macy #define netfront_carrier_off(netif)	((netif)->carrier = 0)
32389e0f4d2SKip Macy #define netfront_carrier_ok(netif)	((netif)->carrier)
32489e0f4d2SKip Macy 
32589e0f4d2SKip Macy /* Access macros for acquiring freeing slots in xn_free_{tx,rx}_idxs[]. */
32689e0f4d2SKip Macy 
32789e0f4d2SKip Macy static inline void
328931eeffaSKenneth D. Merry add_id_to_freelist(struct mbuf **list, uintptr_t id)
32989e0f4d2SKip Macy {
330931eeffaSKenneth D. Merry 	KASSERT(id != 0,
331931eeffaSKenneth D. Merry 		("%s: the head item (0) must always be free.", __func__));
33289e0f4d2SKip Macy 	list[id] = list[0];
333931eeffaSKenneth D. Merry 	list[0]  = (struct mbuf *)id;
33489e0f4d2SKip Macy }
33589e0f4d2SKip Macy 
33689e0f4d2SKip Macy static inline unsigned short
33789e0f4d2SKip Macy get_id_from_freelist(struct mbuf **list)
33889e0f4d2SKip Macy {
339931eeffaSKenneth D. Merry 	uintptr_t id;
340931eeffaSKenneth D. Merry 
341931eeffaSKenneth D. Merry 	id = (uintptr_t)list[0];
342931eeffaSKenneth D. Merry 	KASSERT(id != 0,
343931eeffaSKenneth D. Merry 		("%s: the head item (0) must always remain free.", __func__));
34489e0f4d2SKip Macy 	list[0] = list[id];
34589e0f4d2SKip Macy 	return (id);
34689e0f4d2SKip Macy }
34789e0f4d2SKip Macy 
34889e0f4d2SKip Macy static inline int
34989e0f4d2SKip Macy xennet_rxidx(RING_IDX idx)
35089e0f4d2SKip Macy {
35189e0f4d2SKip Macy 	return idx & (NET_RX_RING_SIZE - 1);
35289e0f4d2SKip Macy }
35389e0f4d2SKip Macy 
35489e0f4d2SKip Macy static inline struct mbuf *
355931eeffaSKenneth D. Merry xennet_get_rx_mbuf(struct netfront_info *np, RING_IDX ri)
35689e0f4d2SKip Macy {
35789e0f4d2SKip Macy 	int i = xennet_rxidx(ri);
35889e0f4d2SKip Macy 	struct mbuf *m;
35989e0f4d2SKip Macy 
36089e0f4d2SKip Macy 	m = np->rx_mbufs[i];
36189e0f4d2SKip Macy 	np->rx_mbufs[i] = NULL;
36289e0f4d2SKip Macy 	return (m);
36389e0f4d2SKip Macy }
36489e0f4d2SKip Macy 
36589e0f4d2SKip Macy static inline grant_ref_t
36689e0f4d2SKip Macy xennet_get_rx_ref(struct netfront_info *np, RING_IDX ri)
36789e0f4d2SKip Macy {
36889e0f4d2SKip Macy 	int i = xennet_rxidx(ri);
36989e0f4d2SKip Macy 	grant_ref_t ref = np->grant_rx_ref[i];
370ff662b5cSJustin T. Gibbs 	KASSERT(ref != GRANT_REF_INVALID, ("Invalid grant reference!\n"));
371ff662b5cSJustin T. Gibbs 	np->grant_rx_ref[i] = GRANT_REF_INVALID;
37289e0f4d2SKip Macy 	return ref;
37389e0f4d2SKip Macy }
37489e0f4d2SKip Macy 
37589e0f4d2SKip Macy #define IPRINTK(fmt, args...) \
37689e0f4d2SKip Macy     printf("[XEN] " fmt, ##args)
377227ca257SKip Macy #ifdef INVARIANTS
37889e0f4d2SKip Macy #define WPRINTK(fmt, args...) \
37989e0f4d2SKip Macy     printf("[XEN] " fmt, ##args)
380227ca257SKip Macy #else
381227ca257SKip Macy #define WPRINTK(fmt, args...)
382227ca257SKip Macy #endif
383227ca257SKip Macy #ifdef DEBUG
38489e0f4d2SKip Macy #define DPRINTK(fmt, args...) \
38523dc5621SKip Macy     printf("[XEN] %s: " fmt, __func__, ##args)
38612678024SDoug Rabson #else
38712678024SDoug Rabson #define DPRINTK(fmt, args...)
38812678024SDoug Rabson #endif
38989e0f4d2SKip Macy 
39089e0f4d2SKip Macy /**
39189e0f4d2SKip Macy  * Read the 'mac' node at the given device's node in the store, and parse that
39289e0f4d2SKip Macy  * as colon-separated octets, placing result the given mac array.  mac must be
39389e0f4d2SKip Macy  * a preallocated array of length ETH_ALEN (as declared in linux/if_ether.h).
39489e0f4d2SKip Macy  * Return 0 on success, or errno on error.
39589e0f4d2SKip Macy  */
39689e0f4d2SKip Macy static int
39723dc5621SKip Macy xen_net_read_mac(device_t dev, uint8_t mac[])
39889e0f4d2SKip Macy {
3993a6d1fcfSKip Macy 	int error, i;
4003a6d1fcfSKip Macy 	char *s, *e, *macstr;
401ffa06904SJustin T. Gibbs 	const char *path;
4023a6d1fcfSKip Macy 
403ffa06904SJustin T. Gibbs 	path = xenbus_get_node(dev);
404ffa06904SJustin T. Gibbs 	error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr);
405ffa06904SJustin T. Gibbs 	if (error == ENOENT) {
406ffa06904SJustin T. Gibbs 		/*
407ffa06904SJustin T. Gibbs 		 * Deal with missing mac XenStore nodes on devices with
408ffa06904SJustin T. Gibbs 		 * HVM emulation (the 'ioemu' configuration attribute)
409ffa06904SJustin T. Gibbs 		 * enabled.
410ffa06904SJustin T. Gibbs 		 *
411ffa06904SJustin T. Gibbs 		 * The HVM emulator may execute in a stub device model
412ffa06904SJustin T. Gibbs 		 * domain which lacks the permission, only given to Dom0,
413ffa06904SJustin T. Gibbs 		 * to update the guest's XenStore tree.  For this reason,
414ffa06904SJustin T. Gibbs 		 * the HVM emulator doesn't even attempt to write the
415ffa06904SJustin T. Gibbs 		 * front-side mac node, even when operating in Dom0.
416ffa06904SJustin T. Gibbs 		 * However, there should always be a mac listed in the
417ffa06904SJustin T. Gibbs 		 * backend tree.  Fallback to this version if our query
418ffa06904SJustin T. Gibbs 		 * of the front side XenStore location doesn't find
419ffa06904SJustin T. Gibbs 		 * anything.
420ffa06904SJustin T. Gibbs 		 */
421ffa06904SJustin T. Gibbs 		path = xenbus_get_otherend_path(dev);
422ffa06904SJustin T. Gibbs 		error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr);
423ffa06904SJustin T. Gibbs 	}
424ffa06904SJustin T. Gibbs 	if (error != 0) {
425ffa06904SJustin T. Gibbs 		xenbus_dev_fatal(dev, error, "parsing %s/mac", path);
4263a6d1fcfSKip Macy 		return (error);
427ffa06904SJustin T. Gibbs 	}
4283a6d1fcfSKip Macy 
42989e0f4d2SKip Macy 	s = macstr;
43089e0f4d2SKip Macy 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
43189e0f4d2SKip Macy 		mac[i] = strtoul(s, &e, 16);
43289e0f4d2SKip Macy 		if (s == e || (e[0] != ':' && e[0] != 0)) {
433ff662b5cSJustin T. Gibbs 			free(macstr, M_XENBUS);
4343a6d1fcfSKip Macy 			return (ENOENT);
43589e0f4d2SKip Macy 		}
43689e0f4d2SKip Macy 		s = &e[1];
43789e0f4d2SKip Macy 	}
438ff662b5cSJustin T. Gibbs 	free(macstr, M_XENBUS);
4393a6d1fcfSKip Macy 	return (0);
44089e0f4d2SKip Macy }
44189e0f4d2SKip Macy 
44289e0f4d2SKip Macy /**
44389e0f4d2SKip Macy  * Entry point to this code when a new device is created.  Allocate the basic
44489e0f4d2SKip Macy  * structures and the ring buffers for communication with the backend, and
44589e0f4d2SKip Macy  * inform the backend of the appropriate details for those.  Switch to
44689e0f4d2SKip Macy  * Connected state.
44789e0f4d2SKip Macy  */
44889e0f4d2SKip Macy static int
44923dc5621SKip Macy netfront_probe(device_t dev)
45023dc5621SKip Macy {
45123dc5621SKip Macy 
45223dc5621SKip Macy 	if (!strcmp(xenbus_get_type(dev), "vif")) {
45323dc5621SKip Macy 		device_set_desc(dev, "Virtual Network Interface");
45423dc5621SKip Macy 		return (0);
45523dc5621SKip Macy 	}
45623dc5621SKip Macy 
45723dc5621SKip Macy 	return (ENXIO);
45823dc5621SKip Macy }
45923dc5621SKip Macy 
46023dc5621SKip Macy static int
46123dc5621SKip Macy netfront_attach(device_t dev)
46289e0f4d2SKip Macy {
46389e0f4d2SKip Macy 	int err;
46489e0f4d2SKip Macy 
46523dc5621SKip Macy 	err = create_netdev(dev);
46689e0f4d2SKip Macy 	if (err) {
46789e0f4d2SKip Macy 		xenbus_dev_fatal(dev, err, "creating netdev");
468ffa06904SJustin T. Gibbs 		return (err);
46989e0f4d2SKip Macy 	}
47089e0f4d2SKip Macy 
47112678024SDoug Rabson #if __FreeBSD_version >= 700000
47212678024SDoug Rabson 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
47312678024SDoug Rabson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
47412678024SDoug Rabson 	    OID_AUTO, "enable_lro", CTLTYPE_INT|CTLFLAG_RW,
47512678024SDoug Rabson 	    &xn_enable_lro, 0, "Large Receive Offload");
47612678024SDoug Rabson #endif
47712678024SDoug Rabson 
478ffa06904SJustin T. Gibbs 	return (0);
47989e0f4d2SKip Macy }
48089e0f4d2SKip Macy 
481cf9c09e1SJustin T. Gibbs static int
482cf9c09e1SJustin T. Gibbs netfront_suspend(device_t dev)
483cf9c09e1SJustin T. Gibbs {
484cf9c09e1SJustin T. Gibbs 	struct netfront_info *info = device_get_softc(dev);
485cf9c09e1SJustin T. Gibbs 
486cf9c09e1SJustin T. Gibbs 	XN_RX_LOCK(info);
487cf9c09e1SJustin T. Gibbs 	XN_TX_LOCK(info);
488cf9c09e1SJustin T. Gibbs 	netfront_carrier_off(info);
489cf9c09e1SJustin T. Gibbs 	XN_TX_UNLOCK(info);
490cf9c09e1SJustin T. Gibbs 	XN_RX_UNLOCK(info);
491cf9c09e1SJustin T. Gibbs 	return (0);
492cf9c09e1SJustin T. Gibbs }
49389e0f4d2SKip Macy 
49489e0f4d2SKip Macy /**
49589e0f4d2SKip Macy  * We are reconnecting to the backend, due to a suspend/resume, or a backend
49689e0f4d2SKip Macy  * driver restart.  We tear down our netif structure and recreate it, but
49789e0f4d2SKip Macy  * leave the device-layer structures intact so that this is transparent to the
49889e0f4d2SKip Macy  * rest of the kernel.
49989e0f4d2SKip Macy  */
50089e0f4d2SKip Macy static int
50123dc5621SKip Macy netfront_resume(device_t dev)
50289e0f4d2SKip Macy {
50323dc5621SKip Macy 	struct netfront_info *info = device_get_softc(dev);
50489e0f4d2SKip Macy 
50589e0f4d2SKip Macy 	netif_disconnect_backend(info);
50689e0f4d2SKip Macy 	return (0);
50789e0f4d2SKip Macy }
50889e0f4d2SKip Macy 
50989e0f4d2SKip Macy /* Common code used when first setting up, and when resuming. */
51089e0f4d2SKip Macy static int
51123dc5621SKip Macy talk_to_backend(device_t dev, struct netfront_info *info)
51289e0f4d2SKip Macy {
51389e0f4d2SKip Macy 	const char *message;
514ff662b5cSJustin T. Gibbs 	struct xs_transaction xst;
51523dc5621SKip Macy 	const char *node = xenbus_get_node(dev);
51689e0f4d2SKip Macy 	int err;
51789e0f4d2SKip Macy 
51889e0f4d2SKip Macy 	err = xen_net_read_mac(dev, info->mac);
51989e0f4d2SKip Macy 	if (err) {
52023dc5621SKip Macy 		xenbus_dev_fatal(dev, err, "parsing %s/mac", node);
52189e0f4d2SKip Macy 		goto out;
52289e0f4d2SKip Macy 	}
52389e0f4d2SKip Macy 
52489e0f4d2SKip Macy 	/* Create shared ring, alloc event channel. */
52589e0f4d2SKip Macy 	err = setup_device(dev, info);
52689e0f4d2SKip Macy 	if (err)
52789e0f4d2SKip Macy 		goto out;
52889e0f4d2SKip Macy 
52989e0f4d2SKip Macy  again:
530ff662b5cSJustin T. Gibbs 	err = xs_transaction_start(&xst);
53189e0f4d2SKip Macy 	if (err) {
53289e0f4d2SKip Macy 		xenbus_dev_fatal(dev, err, "starting transaction");
53389e0f4d2SKip Macy 		goto destroy_ring;
53489e0f4d2SKip Macy 	}
535ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "tx-ring-ref","%u",
53689e0f4d2SKip Macy 			info->tx_ring_ref);
53789e0f4d2SKip Macy 	if (err) {
53889e0f4d2SKip Macy 		message = "writing tx ring-ref";
53989e0f4d2SKip Macy 		goto abort_transaction;
54089e0f4d2SKip Macy 	}
541ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "rx-ring-ref","%u",
54289e0f4d2SKip Macy 			info->rx_ring_ref);
54389e0f4d2SKip Macy 	if (err) {
54489e0f4d2SKip Macy 		message = "writing rx ring-ref";
54589e0f4d2SKip Macy 		goto abort_transaction;
54689e0f4d2SKip Macy 	}
547ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node,
54876acc41fSJustin T. Gibbs 			"event-channel", "%u",
54976acc41fSJustin T. Gibbs 			xen_intr_port(info->xen_intr_handle));
55089e0f4d2SKip Macy 	if (err) {
55189e0f4d2SKip Macy 		message = "writing event-channel";
55289e0f4d2SKip Macy 		goto abort_transaction;
55389e0f4d2SKip Macy 	}
554ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "request-rx-copy", "%u",
55589e0f4d2SKip Macy 			info->copying_receiver);
55689e0f4d2SKip Macy 	if (err) {
55789e0f4d2SKip Macy 		message = "writing request-rx-copy";
55889e0f4d2SKip Macy 		goto abort_transaction;
55989e0f4d2SKip Macy 	}
560ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "feature-rx-notify", "%d", 1);
56189e0f4d2SKip Macy 	if (err) {
56289e0f4d2SKip Macy 		message = "writing feature-rx-notify";
56389e0f4d2SKip Macy 		goto abort_transaction;
56489e0f4d2SKip Macy 	}
565ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "feature-sg", "%d", 1);
56689e0f4d2SKip Macy 	if (err) {
56789e0f4d2SKip Macy 		message = "writing feature-sg";
56889e0f4d2SKip Macy 		goto abort_transaction;
56989e0f4d2SKip Macy 	}
57012678024SDoug Rabson #if __FreeBSD_version >= 700000
571ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "feature-gso-tcpv4", "%d", 1);
57289e0f4d2SKip Macy 	if (err) {
57389e0f4d2SKip Macy 		message = "writing feature-gso-tcpv4";
57489e0f4d2SKip Macy 		goto abort_transaction;
57589e0f4d2SKip Macy 	}
57689e0f4d2SKip Macy #endif
57789e0f4d2SKip Macy 
578ff662b5cSJustin T. Gibbs 	err = xs_transaction_end(xst, 0);
57989e0f4d2SKip Macy 	if (err) {
58089e0f4d2SKip Macy 		if (err == EAGAIN)
58189e0f4d2SKip Macy 			goto again;
58289e0f4d2SKip Macy 		xenbus_dev_fatal(dev, err, "completing transaction");
58389e0f4d2SKip Macy 		goto destroy_ring;
58489e0f4d2SKip Macy 	}
58589e0f4d2SKip Macy 
58689e0f4d2SKip Macy 	return 0;
58789e0f4d2SKip Macy 
58889e0f4d2SKip Macy  abort_transaction:
589ff662b5cSJustin T. Gibbs 	xs_transaction_end(xst, 1);
59089e0f4d2SKip Macy 	xenbus_dev_fatal(dev, err, "%s", message);
59189e0f4d2SKip Macy  destroy_ring:
59289e0f4d2SKip Macy 	netif_free(info);
59389e0f4d2SKip Macy  out:
59489e0f4d2SKip Macy 	return err;
59589e0f4d2SKip Macy }
59689e0f4d2SKip Macy 
59789e0f4d2SKip Macy static int
59823dc5621SKip Macy setup_device(device_t dev, struct netfront_info *info)
59989e0f4d2SKip Macy {
60089e0f4d2SKip Macy 	netif_tx_sring_t *txs;
60189e0f4d2SKip Macy 	netif_rx_sring_t *rxs;
6023a6d1fcfSKip Macy 	int error;
60389e0f4d2SKip Macy 	struct ifnet *ifp;
60489e0f4d2SKip Macy 
60589e0f4d2SKip Macy 	ifp = info->xn_ifp;
60689e0f4d2SKip Macy 
607ff662b5cSJustin T. Gibbs 	info->tx_ring_ref = GRANT_REF_INVALID;
608ff662b5cSJustin T. Gibbs 	info->rx_ring_ref = GRANT_REF_INVALID;
60989e0f4d2SKip Macy 	info->rx.sring = NULL;
61089e0f4d2SKip Macy 	info->tx.sring = NULL;
61189e0f4d2SKip Macy 
61289e0f4d2SKip Macy 	txs = (netif_tx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO);
61389e0f4d2SKip Macy 	if (!txs) {
6143a6d1fcfSKip Macy 		error = ENOMEM;
6153a6d1fcfSKip Macy 		xenbus_dev_fatal(dev, error, "allocating tx ring page");
61689e0f4d2SKip Macy 		goto fail;
61789e0f4d2SKip Macy 	}
61889e0f4d2SKip Macy 	SHARED_RING_INIT(txs);
61989e0f4d2SKip Macy 	FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
6203a6d1fcfSKip Macy 	error = xenbus_grant_ring(dev, virt_to_mfn(txs), &info->tx_ring_ref);
6213a6d1fcfSKip Macy 	if (error)
62289e0f4d2SKip Macy 		goto fail;
62389e0f4d2SKip Macy 
62489e0f4d2SKip Macy 	rxs = (netif_rx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO);
62589e0f4d2SKip Macy 	if (!rxs) {
6263a6d1fcfSKip Macy 		error = ENOMEM;
6273a6d1fcfSKip Macy 		xenbus_dev_fatal(dev, error, "allocating rx ring page");
62889e0f4d2SKip Macy 		goto fail;
62989e0f4d2SKip Macy 	}
63089e0f4d2SKip Macy 	SHARED_RING_INIT(rxs);
63189e0f4d2SKip Macy 	FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
63289e0f4d2SKip Macy 
6333a6d1fcfSKip Macy 	error = xenbus_grant_ring(dev, virt_to_mfn(rxs), &info->rx_ring_ref);
6343a6d1fcfSKip Macy 	if (error)
63589e0f4d2SKip Macy 		goto fail;
63689e0f4d2SKip Macy 
63776acc41fSJustin T. Gibbs 	error = xen_intr_alloc_and_bind_local_port(dev,
63876acc41fSJustin T. Gibbs 	    xenbus_get_otherend_id(dev), /*filter*/NULL, xn_intr, info,
63976acc41fSJustin T. Gibbs 	    INTR_TYPE_NET | INTR_MPSAFE | INTR_ENTROPY, &info->xen_intr_handle);
64089e0f4d2SKip Macy 
6413a6d1fcfSKip Macy 	if (error) {
6423a6d1fcfSKip Macy 		xenbus_dev_fatal(dev, error,
64376acc41fSJustin T. Gibbs 				 "xen_intr_alloc_and_bind_local_port failed");
64489e0f4d2SKip Macy 		goto fail;
64589e0f4d2SKip Macy 	}
64689e0f4d2SKip Macy 
6473a6d1fcfSKip Macy 	return (0);
64889e0f4d2SKip Macy 
64989e0f4d2SKip Macy  fail:
65089e0f4d2SKip Macy 	netif_free(info);
6513a6d1fcfSKip Macy 	return (error);
65289e0f4d2SKip Macy }
65389e0f4d2SKip Macy 
654a0ae8f04SBjoern A. Zeeb #ifdef INET
65589e0f4d2SKip Macy /**
65612678024SDoug Rabson  * If this interface has an ipv4 address, send an arp for it. This
65712678024SDoug Rabson  * helps to get the network going again after migrating hosts.
65812678024SDoug Rabson  */
65912678024SDoug Rabson static void
66012678024SDoug Rabson netfront_send_fake_arp(device_t dev, struct netfront_info *info)
66112678024SDoug Rabson {
66212678024SDoug Rabson 	struct ifnet *ifp;
66312678024SDoug Rabson 	struct ifaddr *ifa;
66412678024SDoug Rabson 
66512678024SDoug Rabson 	ifp = info->xn_ifp;
66612678024SDoug Rabson 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
66712678024SDoug Rabson 		if (ifa->ifa_addr->sa_family == AF_INET) {
66812678024SDoug Rabson 			arp_ifinit(ifp, ifa);
66912678024SDoug Rabson 		}
67012678024SDoug Rabson 	}
67112678024SDoug Rabson }
672a0ae8f04SBjoern A. Zeeb #endif
67312678024SDoug Rabson 
67412678024SDoug Rabson /**
67589e0f4d2SKip Macy  * Callback received when the backend's state changes.
67689e0f4d2SKip Macy  */
677283d6f72SJustin T. Gibbs static void
67823dc5621SKip Macy netfront_backend_changed(device_t dev, XenbusState newstate)
67989e0f4d2SKip Macy {
68023dc5621SKip Macy 	struct netfront_info *sc = device_get_softc(dev);
68189e0f4d2SKip Macy 
68223dc5621SKip Macy 	DPRINTK("newstate=%d\n", newstate);
68389e0f4d2SKip Macy 
68423dc5621SKip Macy 	switch (newstate) {
68589e0f4d2SKip Macy 	case XenbusStateInitialising:
68689e0f4d2SKip Macy 	case XenbusStateInitialised:
68789e0f4d2SKip Macy 	case XenbusStateConnected:
68889e0f4d2SKip Macy 	case XenbusStateUnknown:
68989e0f4d2SKip Macy 	case XenbusStateClosed:
690920ba15bSKip Macy 	case XenbusStateReconfigured:
691920ba15bSKip Macy 	case XenbusStateReconfiguring:
69289e0f4d2SKip Macy 		break;
69389e0f4d2SKip Macy 	case XenbusStateInitWait:
69423dc5621SKip Macy 		if (xenbus_get_state(dev) != XenbusStateInitialising)
69589e0f4d2SKip Macy 			break;
69623dc5621SKip Macy 		if (network_connect(sc) != 0)
69789e0f4d2SKip Macy 			break;
69823dc5621SKip Macy 		xenbus_set_state(dev, XenbusStateConnected);
699a0ae8f04SBjoern A. Zeeb #ifdef INET
70012678024SDoug Rabson 		netfront_send_fake_arp(dev, sc);
701a0ae8f04SBjoern A. Zeeb #endif
70223dc5621SKip Macy 		break;
70389e0f4d2SKip Macy 	case XenbusStateClosing:
70423dc5621SKip Macy 		xenbus_set_state(dev, XenbusStateClosed);
70589e0f4d2SKip Macy 		break;
70689e0f4d2SKip Macy 	}
70789e0f4d2SKip Macy }
70889e0f4d2SKip Macy 
70989e0f4d2SKip Macy static void
71089e0f4d2SKip Macy xn_free_rx_ring(struct netfront_info *sc)
71189e0f4d2SKip Macy {
71289e0f4d2SKip Macy #if 0
71389e0f4d2SKip Macy 	int i;
71489e0f4d2SKip Macy 
71589e0f4d2SKip Macy 	for (i = 0; i < NET_RX_RING_SIZE; i++) {
716931eeffaSKenneth D. Merry 		if (sc->xn_cdata.rx_mbufs[i] != NULL) {
717931eeffaSKenneth D. Merry 			m_freem(sc->rx_mbufs[i]);
718931eeffaSKenneth D. Merry 			sc->rx_mbufs[i] = NULL;
71989e0f4d2SKip Macy 		}
72089e0f4d2SKip Macy 	}
72189e0f4d2SKip Macy 
72289e0f4d2SKip Macy 	sc->rx.rsp_cons = 0;
72389e0f4d2SKip Macy 	sc->xn_rx_if->req_prod = 0;
72489e0f4d2SKip Macy 	sc->xn_rx_if->event = sc->rx.rsp_cons ;
72589e0f4d2SKip Macy #endif
72689e0f4d2SKip Macy }
72789e0f4d2SKip Macy 
72889e0f4d2SKip Macy static void
72989e0f4d2SKip Macy xn_free_tx_ring(struct netfront_info *sc)
73089e0f4d2SKip Macy {
73189e0f4d2SKip Macy #if 0
73289e0f4d2SKip Macy 	int i;
73389e0f4d2SKip Macy 
73489e0f4d2SKip Macy 	for (i = 0; i < NET_TX_RING_SIZE; i++) {
735931eeffaSKenneth D. Merry 		if (sc->tx_mbufs[i] != NULL) {
736931eeffaSKenneth D. Merry 			m_freem(sc->tx_mbufs[i]);
73789e0f4d2SKip Macy 			sc->xn_cdata.xn_tx_chain[i] = NULL;
73889e0f4d2SKip Macy 		}
73989e0f4d2SKip Macy 	}
74089e0f4d2SKip Macy 
74189e0f4d2SKip Macy 	return;
74289e0f4d2SKip Macy #endif
74389e0f4d2SKip Macy }
74489e0f4d2SKip Macy 
745931eeffaSKenneth D. Merry /**
746931eeffaSKenneth D. Merry  * \brief Verify that there is sufficient space in the Tx ring
747931eeffaSKenneth D. Merry  *        buffer for a maximally sized request to be enqueued.
748c099cafaSAdrian Chadd  *
749931eeffaSKenneth D. Merry  * A transmit request requires a transmit descriptor for each packet
750931eeffaSKenneth D. Merry  * fragment, plus up to 2 entries for "options" (e.g. TSO).
751c099cafaSAdrian Chadd  */
75289e0f4d2SKip Macy static inline int
753931eeffaSKenneth D. Merry xn_tx_slot_available(struct netfront_info *np)
75489e0f4d2SKip Macy {
755931eeffaSKenneth D. Merry 	return (RING_FREE_REQUESTS(&np->tx) > (MAX_TX_REQ_FRAGS + 2));
75689e0f4d2SKip Macy }
757931eeffaSKenneth D. Merry 
75889e0f4d2SKip Macy static void
75989e0f4d2SKip Macy netif_release_tx_bufs(struct netfront_info *np)
76089e0f4d2SKip Macy {
76189e0f4d2SKip Macy 	int i;
76289e0f4d2SKip Macy 
76389e0f4d2SKip Macy 	for (i = 1; i <= NET_TX_RING_SIZE; i++) {
764931eeffaSKenneth D. Merry 		struct mbuf *m;
76589e0f4d2SKip Macy 
766931eeffaSKenneth D. Merry 		m = np->tx_mbufs[i];
767931eeffaSKenneth D. Merry 
768931eeffaSKenneth D. Merry 		/*
769931eeffaSKenneth D. Merry 		 * We assume that no kernel addresses are
770931eeffaSKenneth D. Merry 		 * less than NET_TX_RING_SIZE.  Any entry
771931eeffaSKenneth D. Merry 		 * in the table that is below this number
772931eeffaSKenneth D. Merry 		 * must be an index from free-list tracking.
773931eeffaSKenneth D. Merry 		 */
774931eeffaSKenneth D. Merry 		if (((uintptr_t)m) <= NET_TX_RING_SIZE)
77589e0f4d2SKip Macy 			continue;
776cf9c09e1SJustin T. Gibbs 		gnttab_end_foreign_access_ref(np->grant_tx_ref[i]);
77789e0f4d2SKip Macy 		gnttab_release_grant_reference(&np->gref_tx_head,
77889e0f4d2SKip Macy 		    np->grant_tx_ref[i]);
779ff662b5cSJustin T. Gibbs 		np->grant_tx_ref[i] = GRANT_REF_INVALID;
78089e0f4d2SKip Macy 		add_id_to_freelist(np->tx_mbufs, i);
781a4ec37f5SAdrian Chadd 		np->xn_cdata.xn_tx_chain_cnt--;
782a4ec37f5SAdrian Chadd 		if (np->xn_cdata.xn_tx_chain_cnt < 0) {
7836f9767acSMarius Strobl 			panic("%s: tx_chain_cnt must be >= 0", __func__);
784a4ec37f5SAdrian Chadd 		}
785cf9c09e1SJustin T. Gibbs 		m_free(m);
78689e0f4d2SKip Macy 	}
78789e0f4d2SKip Macy }
78889e0f4d2SKip Macy 
78989e0f4d2SKip Macy static void
79089e0f4d2SKip Macy network_alloc_rx_buffers(struct netfront_info *sc)
79189e0f4d2SKip Macy {
79223dc5621SKip Macy 	int otherend_id = xenbus_get_otherend_id(sc->xbdev);
79389e0f4d2SKip Macy 	unsigned short id;
79489e0f4d2SKip Macy 	struct mbuf *m_new;
79589e0f4d2SKip Macy 	int i, batch_target, notify;
79689e0f4d2SKip Macy 	RING_IDX req_prod;
79789e0f4d2SKip Macy 	struct xen_memory_reservation reservation;
79889e0f4d2SKip Macy 	grant_ref_t ref;
79989e0f4d2SKip Macy 	int nr_flips;
80089e0f4d2SKip Macy 	netif_rx_request_t *req;
80189e0f4d2SKip Macy 	vm_offset_t vaddr;
80289e0f4d2SKip Macy 	u_long pfn;
80389e0f4d2SKip Macy 
80489e0f4d2SKip Macy 	req_prod = sc->rx.req_prod_pvt;
80589e0f4d2SKip Macy 
80676acc41fSJustin T. Gibbs 	if (__predict_false(sc->carrier == 0))
80789e0f4d2SKip Macy 		return;
80889e0f4d2SKip Macy 
80989e0f4d2SKip Macy 	/*
810931eeffaSKenneth D. Merry 	 * Allocate mbufs greedily, even though we batch updates to the
81189e0f4d2SKip Macy 	 * receive ring. This creates a less bursty demand on the memory
812931eeffaSKenneth D. Merry 	 * allocator, and so should reduce the chance of failed allocation
81389e0f4d2SKip Macy 	 * requests both for ourself and for other kernel subsystems.
814931eeffaSKenneth D. Merry 	 *
815931eeffaSKenneth D. Merry 	 * Here we attempt to maintain rx_target buffers in flight, counting
816931eeffaSKenneth D. Merry 	 * buffers that we have yet to process in the receive ring.
81789e0f4d2SKip Macy 	 */
81889e0f4d2SKip Macy 	batch_target = sc->rx_target - (req_prod - sc->rx.rsp_cons);
81989e0f4d2SKip Macy 	for (i = mbufq_len(&sc->xn_rx_batch); i < batch_target; i++) {
820c6499eccSGleb Smirnoff 		MGETHDR(m_new, M_NOWAIT, MT_DATA);
821931eeffaSKenneth D. Merry 		if (m_new == NULL) {
822931eeffaSKenneth D. Merry 			printf("%s: MGETHDR failed\n", __func__);
82389e0f4d2SKip Macy 			goto no_mbuf;
824931eeffaSKenneth D. Merry 		}
82589e0f4d2SKip Macy 
826c6499eccSGleb Smirnoff 		m_cljget(m_new, M_NOWAIT, MJUMPAGESIZE);
82789e0f4d2SKip Macy 		if ((m_new->m_flags & M_EXT) == 0) {
828931eeffaSKenneth D. Merry 			printf("%s: m_cljget failed\n", __func__);
82989e0f4d2SKip Macy 			m_freem(m_new);
83089e0f4d2SKip Macy 
83189e0f4d2SKip Macy no_mbuf:
83289e0f4d2SKip Macy 			if (i != 0)
83389e0f4d2SKip Macy 				goto refill;
83489e0f4d2SKip Macy 			/*
83589e0f4d2SKip Macy 			 * XXX set timer
83689e0f4d2SKip Macy 			 */
83789e0f4d2SKip Macy 			break;
83889e0f4d2SKip Macy 		}
83989e0f4d2SKip Macy 		m_new->m_len = m_new->m_pkthdr.len = MJUMPAGESIZE;
84089e0f4d2SKip Macy 
84189e0f4d2SKip Macy 		/* queue the mbufs allocated */
84289e0f4d2SKip Macy 		mbufq_tail(&sc->xn_rx_batch, m_new);
84389e0f4d2SKip Macy 	}
84489e0f4d2SKip Macy 
845931eeffaSKenneth D. Merry 	/*
846931eeffaSKenneth D. Merry 	 * If we've allocated at least half of our target number of entries,
847931eeffaSKenneth D. Merry 	 * submit them to the backend - we have enough to make the overhead
848931eeffaSKenneth D. Merry 	 * of submission worthwhile.  Otherwise wait for more mbufs and
849931eeffaSKenneth D. Merry 	 * request entries to become available.
850931eeffaSKenneth D. Merry 	 */
85189e0f4d2SKip Macy 	if (i < (sc->rx_target/2)) {
85289e0f4d2SKip Macy 		if (req_prod >sc->rx.sring->req_prod)
85389e0f4d2SKip Macy 			goto push;
85489e0f4d2SKip Macy 		return;
85589e0f4d2SKip Macy 	}
856931eeffaSKenneth D. Merry 
857931eeffaSKenneth D. Merry 	/*
858931eeffaSKenneth D. Merry 	 * Double floating fill target if we risked having the backend
859931eeffaSKenneth D. Merry 	 * run out of empty buffers for receive traffic.  We define "running
860931eeffaSKenneth D. Merry 	 * low" as having less than a fourth of our target buffers free
861931eeffaSKenneth D. Merry 	 * at the time we refilled the queue.
862931eeffaSKenneth D. Merry 	 */
863931eeffaSKenneth D. Merry 	if ((req_prod - sc->rx.sring->rsp_prod) < (sc->rx_target / 4)) {
864931eeffaSKenneth D. Merry 		sc->rx_target *= 2;
865931eeffaSKenneth D. Merry 		if (sc->rx_target > sc->rx_max_target)
86689e0f4d2SKip Macy 			sc->rx_target = sc->rx_max_target;
867931eeffaSKenneth D. Merry 	}
86889e0f4d2SKip Macy 
86989e0f4d2SKip Macy refill:
87089e0f4d2SKip Macy 	for (nr_flips = i = 0; ; i++) {
87189e0f4d2SKip Macy 		if ((m_new = mbufq_dequeue(&sc->xn_rx_batch)) == NULL)
87289e0f4d2SKip Macy 			break;
87389e0f4d2SKip Macy 
87489e0f4d2SKip Macy 		m_new->m_ext.ext_arg1 = (vm_paddr_t *)(uintptr_t)(
87589e0f4d2SKip Macy 				vtophys(m_new->m_ext.ext_buf) >> PAGE_SHIFT);
87689e0f4d2SKip Macy 
87789e0f4d2SKip Macy 		id = xennet_rxidx(req_prod + i);
87889e0f4d2SKip Macy 
879931eeffaSKenneth D. Merry 		KASSERT(sc->rx_mbufs[id] == NULL, ("non-NULL xm_rx_chain"));
880931eeffaSKenneth D. Merry 		sc->rx_mbufs[id] = m_new;
88189e0f4d2SKip Macy 
88289e0f4d2SKip Macy 		ref = gnttab_claim_grant_reference(&sc->gref_rx_head);
883ff662b5cSJustin T. Gibbs 		KASSERT(ref != GNTTAB_LIST_END,
884ff662b5cSJustin T. Gibbs 			("reserved grant references exhuasted"));
88589e0f4d2SKip Macy 		sc->grant_rx_ref[id] = ref;
88689e0f4d2SKip Macy 
88789e0f4d2SKip Macy 		vaddr = mtod(m_new, vm_offset_t);
88889e0f4d2SKip Macy 		pfn = vtophys(vaddr) >> PAGE_SHIFT;
88989e0f4d2SKip Macy 		req = RING_GET_REQUEST(&sc->rx, req_prod + i);
89089e0f4d2SKip Macy 
89189e0f4d2SKip Macy 		if (sc->copying_receiver == 0) {
89289e0f4d2SKip Macy 			gnttab_grant_foreign_transfer_ref(ref,
89323dc5621SKip Macy 			    otherend_id, pfn);
89489e0f4d2SKip Macy 			sc->rx_pfn_array[nr_flips] = PFNTOMFN(pfn);
89589e0f4d2SKip Macy 			if (!xen_feature(XENFEAT_auto_translated_physmap)) {
89689e0f4d2SKip Macy 				/* Remove this page before passing
89789e0f4d2SKip Macy 				 * back to Xen.
89889e0f4d2SKip Macy 				 */
89989e0f4d2SKip Macy 				set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
90089e0f4d2SKip Macy 				MULTI_update_va_mapping(&sc->rx_mcl[i],
90189e0f4d2SKip Macy 				    vaddr, 0, 0);
90289e0f4d2SKip Macy 			}
90389e0f4d2SKip Macy 			nr_flips++;
90489e0f4d2SKip Macy 		} else {
90589e0f4d2SKip Macy 			gnttab_grant_foreign_access_ref(ref,
90623dc5621SKip Macy 			    otherend_id,
90789e0f4d2SKip Macy 			    PFNTOMFN(pfn), 0);
90889e0f4d2SKip Macy 		}
90989e0f4d2SKip Macy 		req->id = id;
91089e0f4d2SKip Macy 		req->gref = ref;
91189e0f4d2SKip Macy 
91289e0f4d2SKip Macy 		sc->rx_pfn_array[i] =
91389e0f4d2SKip Macy 		    vtomach(mtod(m_new,vm_offset_t)) >> PAGE_SHIFT;
91489e0f4d2SKip Macy 	}
91589e0f4d2SKip Macy 
91689e0f4d2SKip Macy 	KASSERT(i, ("no mbufs processed")); /* should have returned earlier */
91789e0f4d2SKip Macy 	KASSERT(mbufq_len(&sc->xn_rx_batch) == 0, ("not all mbufs processed"));
91889e0f4d2SKip Macy 	/*
91989e0f4d2SKip Macy 	 * We may have allocated buffers which have entries outstanding
92089e0f4d2SKip Macy 	 * in the page * update queue -- make sure we flush those first!
92189e0f4d2SKip Macy 	 */
92289e0f4d2SKip Macy 	PT_UPDATES_FLUSH();
92389e0f4d2SKip Macy 	if (nr_flips != 0) {
92489e0f4d2SKip Macy #ifdef notyet
92589e0f4d2SKip Macy 		/* Tell the ballon driver what is going on. */
92689e0f4d2SKip Macy 		balloon_update_driver_allowance(i);
92789e0f4d2SKip Macy #endif
928920ba15bSKip Macy 		set_xen_guest_handle(reservation.extent_start, sc->rx_pfn_array);
92989e0f4d2SKip Macy 		reservation.nr_extents   = i;
93089e0f4d2SKip Macy 		reservation.extent_order = 0;
93189e0f4d2SKip Macy 		reservation.address_bits = 0;
93289e0f4d2SKip Macy 		reservation.domid        = DOMID_SELF;
93389e0f4d2SKip Macy 
93489e0f4d2SKip Macy 		if (!xen_feature(XENFEAT_auto_translated_physmap)) {
93589e0f4d2SKip Macy 			/* After all PTEs have been zapped, flush the TLB. */
93689e0f4d2SKip Macy 			sc->rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] =
93789e0f4d2SKip Macy 			    UVMF_TLB_FLUSH|UVMF_ALL;
93889e0f4d2SKip Macy 
93989e0f4d2SKip Macy 			/* Give away a batch of pages. */
94089e0f4d2SKip Macy 			sc->rx_mcl[i].op = __HYPERVISOR_memory_op;
94189e0f4d2SKip Macy 			sc->rx_mcl[i].args[0] = XENMEM_decrease_reservation;
94289e0f4d2SKip Macy 			sc->rx_mcl[i].args[1] =  (u_long)&reservation;
94389e0f4d2SKip Macy 			/* Zap PTEs and give away pages in one big multicall. */
94489e0f4d2SKip Macy 			(void)HYPERVISOR_multicall(sc->rx_mcl, i+1);
94589e0f4d2SKip Macy 
94676acc41fSJustin T. Gibbs 			if (__predict_false(sc->rx_mcl[i].result != i ||
9476f9767acSMarius Strobl 			    HYPERVISOR_memory_op(XENMEM_decrease_reservation,
9486f9767acSMarius Strobl 			    &reservation) != i))
9496f9767acSMarius Strobl 				panic("%s: unable to reduce memory "
9506f9767acSMarius Strobl 				    "reservation\n", __func__);
95189e0f4d2SKip Macy 		}
95289e0f4d2SKip Macy 	} else {
95389e0f4d2SKip Macy 		wmb();
95489e0f4d2SKip Macy 	}
95589e0f4d2SKip Macy 
95689e0f4d2SKip Macy 	/* Above is a suitable barrier to ensure backend will see requests. */
95789e0f4d2SKip Macy 	sc->rx.req_prod_pvt = req_prod + i;
95889e0f4d2SKip Macy push:
95989e0f4d2SKip Macy 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->rx, notify);
96089e0f4d2SKip Macy 	if (notify)
96176acc41fSJustin T. Gibbs 		xen_intr_signal(sc->xen_intr_handle);
96289e0f4d2SKip Macy }
96389e0f4d2SKip Macy 
96489e0f4d2SKip Macy static void
96589e0f4d2SKip Macy xn_rxeof(struct netfront_info *np)
96689e0f4d2SKip Macy {
96789e0f4d2SKip Macy 	struct ifnet *ifp;
968*f909bbb4SGleb Smirnoff #if __FreeBSD_version >= 700000 && (defined(INET) || defined(INET6))
96912678024SDoug Rabson 	struct lro_ctrl *lro = &np->xn_lro;
97012678024SDoug Rabson 	struct lro_entry *queued;
97112678024SDoug Rabson #endif
97289e0f4d2SKip Macy 	struct netfront_rx_info rinfo;
97389e0f4d2SKip Macy 	struct netif_rx_response *rx = &rinfo.rx;
97489e0f4d2SKip Macy 	struct netif_extra_info *extras = rinfo.extras;
97589e0f4d2SKip Macy 	RING_IDX i, rp;
97689e0f4d2SKip Macy 	multicall_entry_t *mcl;
97789e0f4d2SKip Macy 	struct mbuf *m;
97883b92f6eSKip Macy 	struct mbuf_head rxq, errq;
97949906218SDoug Rabson 	int err, pages_flipped = 0, work_to_do;
98089e0f4d2SKip Macy 
98149906218SDoug Rabson 	do {
98289e0f4d2SKip Macy 		XN_RX_LOCK_ASSERT(np);
98389e0f4d2SKip Macy 		if (!netfront_carrier_ok(np))
98489e0f4d2SKip Macy 			return;
98589e0f4d2SKip Macy 
98689e0f4d2SKip Macy 		mbufq_init(&errq);
98789e0f4d2SKip Macy 		mbufq_init(&rxq);
98889e0f4d2SKip Macy 
98989e0f4d2SKip Macy 		ifp = np->xn_ifp;
99089e0f4d2SKip Macy 
99189e0f4d2SKip Macy 		rp = np->rx.sring->rsp_prod;
99289e0f4d2SKip Macy 		rmb();	/* Ensure we see queued responses up to 'rp'. */
99389e0f4d2SKip Macy 
99489e0f4d2SKip Macy 		i = np->rx.rsp_cons;
99589e0f4d2SKip Macy 		while ((i != rp)) {
99689e0f4d2SKip Macy 			memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
99789e0f4d2SKip Macy 			memset(extras, 0, sizeof(rinfo.extras));
99889e0f4d2SKip Macy 
99983b92f6eSKip Macy 			m = NULL;
1000931eeffaSKenneth D. Merry 			err = xennet_get_responses(np, &rinfo, rp, &i, &m,
100189e0f4d2SKip Macy 			    &pages_flipped);
100289e0f4d2SKip Macy 
100376acc41fSJustin T. Gibbs 			if (__predict_false(err)) {
100483b92f6eSKip Macy 				if (m)
100589e0f4d2SKip Macy 					mbufq_tail(&errq, m);
100689e0f4d2SKip Macy 				np->stats.rx_errors++;
100789e0f4d2SKip Macy 				continue;
100889e0f4d2SKip Macy 			}
100989e0f4d2SKip Macy 
101089e0f4d2SKip Macy 			m->m_pkthdr.rcvif = ifp;
101189e0f4d2SKip Macy 			if ( rx->flags & NETRXF_data_validated ) {
101289e0f4d2SKip Macy 				/* Tell the stack the checksums are okay */
101389e0f4d2SKip Macy 				/*
101489e0f4d2SKip Macy 				 * XXX this isn't necessarily the case - need to add
101589e0f4d2SKip Macy 				 * check
101689e0f4d2SKip Macy 				 */
101789e0f4d2SKip Macy 
101889e0f4d2SKip Macy 				m->m_pkthdr.csum_flags |=
101989e0f4d2SKip Macy 					(CSUM_IP_CHECKED | CSUM_IP_VALID | CSUM_DATA_VALID
102089e0f4d2SKip Macy 					    | CSUM_PSEUDO_HDR);
102189e0f4d2SKip Macy 				m->m_pkthdr.csum_data = 0xffff;
102289e0f4d2SKip Macy 			}
102389e0f4d2SKip Macy 
102489e0f4d2SKip Macy 			np->stats.rx_packets++;
102583b92f6eSKip Macy 			np->stats.rx_bytes += m->m_pkthdr.len;
102689e0f4d2SKip Macy 
102789e0f4d2SKip Macy 			mbufq_tail(&rxq, m);
1028931eeffaSKenneth D. Merry 			np->rx.rsp_cons = i;
102989e0f4d2SKip Macy 		}
103089e0f4d2SKip Macy 
103189e0f4d2SKip Macy 		if (pages_flipped) {
103289e0f4d2SKip Macy 			/* Some pages are no longer absent... */
103389e0f4d2SKip Macy #ifdef notyet
103489e0f4d2SKip Macy 			balloon_update_driver_allowance(-pages_flipped);
103589e0f4d2SKip Macy #endif
103689e0f4d2SKip Macy 			/* Do all the remapping work, and M->P updates, in one big
103789e0f4d2SKip Macy 			 * hypercall.
103889e0f4d2SKip Macy 			 */
103989e0f4d2SKip Macy 			if (!!xen_feature(XENFEAT_auto_translated_physmap)) {
104089e0f4d2SKip Macy 				mcl = np->rx_mcl + pages_flipped;
104189e0f4d2SKip Macy 				mcl->op = __HYPERVISOR_mmu_update;
104289e0f4d2SKip Macy 				mcl->args[0] = (u_long)np->rx_mmu;
104389e0f4d2SKip Macy 				mcl->args[1] = pages_flipped;
104489e0f4d2SKip Macy 				mcl->args[2] = 0;
104589e0f4d2SKip Macy 				mcl->args[3] = DOMID_SELF;
104689e0f4d2SKip Macy 				(void)HYPERVISOR_multicall(np->rx_mcl,
104789e0f4d2SKip Macy 				    pages_flipped + 1);
104889e0f4d2SKip Macy 			}
104989e0f4d2SKip Macy 		}
105089e0f4d2SKip Macy 
105189e0f4d2SKip Macy 		while ((m = mbufq_dequeue(&errq)))
105289e0f4d2SKip Macy 			m_freem(m);
105389e0f4d2SKip Macy 
105489e0f4d2SKip Macy 		/*
105589e0f4d2SKip Macy 		 * Process all the mbufs after the remapping is complete.
105689e0f4d2SKip Macy 		 * Break the mbuf chain first though.
105789e0f4d2SKip Macy 		 */
105889e0f4d2SKip Macy 		while ((m = mbufq_dequeue(&rxq)) != NULL) {
105989e0f4d2SKip Macy 			ifp->if_ipackets++;
106089e0f4d2SKip Macy 
106189e0f4d2SKip Macy 			/*
106289e0f4d2SKip Macy 			 * Do we really need to drop the rx lock?
106389e0f4d2SKip Macy 			 */
106489e0f4d2SKip Macy 			XN_RX_UNLOCK(np);
1065*f909bbb4SGleb Smirnoff #if __FreeBSD_version >= 700000 && (defined(INET) || defined(INET6))
106612678024SDoug Rabson 			/* Use LRO if possible */
106712678024SDoug Rabson 			if ((ifp->if_capenable & IFCAP_LRO) == 0 ||
106812678024SDoug Rabson 			    lro->lro_cnt == 0 || tcp_lro_rx(lro, m, 0)) {
106912678024SDoug Rabson 				/*
107012678024SDoug Rabson 				 * If LRO fails, pass up to the stack
107112678024SDoug Rabson 				 * directly.
107212678024SDoug Rabson 				 */
107389e0f4d2SKip Macy 				(*ifp->if_input)(ifp, m);
107412678024SDoug Rabson 			}
107512678024SDoug Rabson #else
107612678024SDoug Rabson 			(*ifp->if_input)(ifp, m);
107712678024SDoug Rabson #endif
107889e0f4d2SKip Macy 			XN_RX_LOCK(np);
107989e0f4d2SKip Macy 		}
108089e0f4d2SKip Macy 
108189e0f4d2SKip Macy 		np->rx.rsp_cons = i;
108289e0f4d2SKip Macy 
1083*f909bbb4SGleb Smirnoff #if __FreeBSD_version >= 700000 && (defined(INET) || defined(INET6))
108412678024SDoug Rabson 		/*
108512678024SDoug Rabson 		 * Flush any outstanding LRO work
108612678024SDoug Rabson 		 */
108712678024SDoug Rabson 		while (!SLIST_EMPTY(&lro->lro_active)) {
108812678024SDoug Rabson 			queued = SLIST_FIRST(&lro->lro_active);
108912678024SDoug Rabson 			SLIST_REMOVE_HEAD(&lro->lro_active, next);
109012678024SDoug Rabson 			tcp_lro_flush(lro, queued);
109112678024SDoug Rabson 		}
109212678024SDoug Rabson #endif
109312678024SDoug Rabson 
109489e0f4d2SKip Macy #if 0
109589e0f4d2SKip Macy 		/* If we get a callback with very few responses, reduce fill target. */
109689e0f4d2SKip Macy 		/* NB. Note exponential increase, linear decrease. */
109789e0f4d2SKip Macy 		if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
109889e0f4d2SKip Macy 			((3*np->rx_target) / 4)) && (--np->rx_target < np->rx_min_target))
109989e0f4d2SKip Macy 			np->rx_target = np->rx_min_target;
110089e0f4d2SKip Macy #endif
110189e0f4d2SKip Macy 
110289e0f4d2SKip Macy 		network_alloc_rx_buffers(np);
110389e0f4d2SKip Macy 
110449906218SDoug Rabson 		RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, work_to_do);
110549906218SDoug Rabson 	} while (work_to_do);
110689e0f4d2SKip Macy }
110789e0f4d2SKip Macy 
110889e0f4d2SKip Macy static void
110989e0f4d2SKip Macy xn_txeof(struct netfront_info *np)
111089e0f4d2SKip Macy {
111189e0f4d2SKip Macy 	RING_IDX i, prod;
111289e0f4d2SKip Macy 	unsigned short id;
111389e0f4d2SKip Macy 	struct ifnet *ifp;
111412678024SDoug Rabson 	netif_tx_response_t *txr;
111589e0f4d2SKip Macy 	struct mbuf *m;
111689e0f4d2SKip Macy 
111789e0f4d2SKip Macy 	XN_TX_LOCK_ASSERT(np);
111889e0f4d2SKip Macy 
111989e0f4d2SKip Macy 	if (!netfront_carrier_ok(np))
112089e0f4d2SKip Macy 		return;
112189e0f4d2SKip Macy 
112289e0f4d2SKip Macy 	ifp = np->xn_ifp;
112389e0f4d2SKip Macy 
112489e0f4d2SKip Macy 	do {
112589e0f4d2SKip Macy 		prod = np->tx.sring->rsp_prod;
112689e0f4d2SKip Macy 		rmb(); /* Ensure we see responses up to 'rp'. */
112789e0f4d2SKip Macy 
112889e0f4d2SKip Macy 		for (i = np->tx.rsp_cons; i != prod; i++) {
112912678024SDoug Rabson 			txr = RING_GET_RESPONSE(&np->tx, i);
113012678024SDoug Rabson 			if (txr->status == NETIF_RSP_NULL)
113112678024SDoug Rabson 				continue;
113212678024SDoug Rabson 
1133931eeffaSKenneth D. Merry 			if (txr->status != NETIF_RSP_OKAY) {
1134931eeffaSKenneth D. Merry 				printf("%s: WARNING: response is %d!\n",
1135931eeffaSKenneth D. Merry 				       __func__, txr->status);
1136931eeffaSKenneth D. Merry 			}
113712678024SDoug Rabson 			id = txr->id;
1138931eeffaSKenneth D. Merry 			m = np->tx_mbufs[id];
11392d8fae98SAdrian Chadd 			KASSERT(m != NULL, ("mbuf not found in xn_tx_chain"));
1140931eeffaSKenneth D. Merry 			KASSERT((uintptr_t)m > NET_TX_RING_SIZE,
1141931eeffaSKenneth D. Merry 				("mbuf already on the free list, but we're "
1142931eeffaSKenneth D. Merry 				"trying to free it again!"));
11432d8fae98SAdrian Chadd 			M_ASSERTVALID(m);
114489e0f4d2SKip Macy 
114512678024SDoug Rabson 			/*
114612678024SDoug Rabson 			 * Increment packet count if this is the last
114712678024SDoug Rabson 			 * mbuf of the chain.
114812678024SDoug Rabson 			 */
114912678024SDoug Rabson 			if (!m->m_next)
115089e0f4d2SKip Macy 				ifp->if_opackets++;
115176acc41fSJustin T. Gibbs 			if (__predict_false(gnttab_query_foreign_access(
115289e0f4d2SKip Macy 			    np->grant_tx_ref[id]) != 0)) {
11536f9767acSMarius Strobl 				panic("%s: grant id %u still in use by the "
11546f9767acSMarius Strobl 				    "backend", __func__, id);
115589e0f4d2SKip Macy 			}
115689e0f4d2SKip Macy 			gnttab_end_foreign_access_ref(
1157920ba15bSKip Macy 				np->grant_tx_ref[id]);
115889e0f4d2SKip Macy 			gnttab_release_grant_reference(
115989e0f4d2SKip Macy 				&np->gref_tx_head, np->grant_tx_ref[id]);
1160ff662b5cSJustin T. Gibbs 			np->grant_tx_ref[id] = GRANT_REF_INVALID;
116189e0f4d2SKip Macy 
1162931eeffaSKenneth D. Merry 			np->tx_mbufs[id] = NULL;
1163931eeffaSKenneth D. Merry 			add_id_to_freelist(np->tx_mbufs, id);
1164a4ec37f5SAdrian Chadd 			np->xn_cdata.xn_tx_chain_cnt--;
116512678024SDoug Rabson 			m_free(m);
1166d76e4550SAdrian Chadd 			/* Only mark the queue active if we've freed up at least one slot to try */
1167d76e4550SAdrian Chadd 			ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
116889e0f4d2SKip Macy 		}
116989e0f4d2SKip Macy 		np->tx.rsp_cons = prod;
117089e0f4d2SKip Macy 
117189e0f4d2SKip Macy 		/*
117289e0f4d2SKip Macy 		 * Set a new event, then check for race with update of
117389e0f4d2SKip Macy 		 * tx_cons. Note that it is essential to schedule a
117489e0f4d2SKip Macy 		 * callback, no matter how few buffers are pending. Even if
117589e0f4d2SKip Macy 		 * there is space in the transmit ring, higher layers may
117689e0f4d2SKip Macy 		 * be blocked because too much data is outstanding: in such
117789e0f4d2SKip Macy 		 * cases notification from Xen is likely to be the only kick
117889e0f4d2SKip Macy 		 * that we'll get.
117989e0f4d2SKip Macy 		 */
118089e0f4d2SKip Macy 		np->tx.sring->rsp_event =
118189e0f4d2SKip Macy 		    prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
118289e0f4d2SKip Macy 
118389e0f4d2SKip Macy 		mb();
118489e0f4d2SKip Macy 	} while (prod != np->tx.sring->rsp_prod);
118589e0f4d2SKip Macy 
118689e0f4d2SKip Macy 	if (np->tx_full &&
118789e0f4d2SKip Macy 	    ((np->tx.sring->req_prod - prod) < NET_TX_RING_SIZE)) {
118889e0f4d2SKip Macy 		np->tx_full = 0;
118989e0f4d2SKip Macy #if 0
119089e0f4d2SKip Macy 		if (np->user_state == UST_OPEN)
119189e0f4d2SKip Macy 			netif_wake_queue(dev);
119289e0f4d2SKip Macy #endif
119389e0f4d2SKip Macy 	}
119489e0f4d2SKip Macy }
119589e0f4d2SKip Macy 
119689e0f4d2SKip Macy static void
119789e0f4d2SKip Macy xn_intr(void *xsc)
119889e0f4d2SKip Macy {
119989e0f4d2SKip Macy 	struct netfront_info *np = xsc;
120089e0f4d2SKip Macy 	struct ifnet *ifp = np->xn_ifp;
120189e0f4d2SKip Macy 
120289e0f4d2SKip Macy #if 0
120389e0f4d2SKip Macy 	if (!(np->rx.rsp_cons != np->rx.sring->rsp_prod &&
120489e0f4d2SKip Macy 	    likely(netfront_carrier_ok(np)) &&
120589e0f4d2SKip Macy 	    ifp->if_drv_flags & IFF_DRV_RUNNING))
120689e0f4d2SKip Macy 		return;
120789e0f4d2SKip Macy #endif
1208931eeffaSKenneth D. Merry 	if (RING_HAS_UNCONSUMED_RESPONSES(&np->tx)) {
120989e0f4d2SKip Macy 		XN_TX_LOCK(np);
121089e0f4d2SKip Macy 		xn_txeof(np);
121189e0f4d2SKip Macy 		XN_TX_UNLOCK(np);
121289e0f4d2SKip Macy 	}
121389e0f4d2SKip Macy 
121489e0f4d2SKip Macy 	XN_RX_LOCK(np);
121589e0f4d2SKip Macy 	xn_rxeof(np);
121689e0f4d2SKip Macy 	XN_RX_UNLOCK(np);
121789e0f4d2SKip Macy 
121889e0f4d2SKip Macy 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
121989e0f4d2SKip Macy 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
122089e0f4d2SKip Macy 		xn_start(ifp);
122189e0f4d2SKip Macy }
122289e0f4d2SKip Macy 
122389e0f4d2SKip Macy static void
122489e0f4d2SKip Macy xennet_move_rx_slot(struct netfront_info *np, struct mbuf *m,
122589e0f4d2SKip Macy 	grant_ref_t ref)
122689e0f4d2SKip Macy {
122789e0f4d2SKip Macy 	int new = xennet_rxidx(np->rx.req_prod_pvt);
122889e0f4d2SKip Macy 
122989e0f4d2SKip Macy 	KASSERT(np->rx_mbufs[new] == NULL, ("rx_mbufs != NULL"));
123089e0f4d2SKip Macy 	np->rx_mbufs[new] = m;
123189e0f4d2SKip Macy 	np->grant_rx_ref[new] = ref;
123289e0f4d2SKip Macy 	RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
123389e0f4d2SKip Macy 	RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
123489e0f4d2SKip Macy 	np->rx.req_prod_pvt++;
123589e0f4d2SKip Macy }
123689e0f4d2SKip Macy 
123789e0f4d2SKip Macy static int
123889e0f4d2SKip Macy xennet_get_extras(struct netfront_info *np,
1239931eeffaSKenneth D. Merry     struct netif_extra_info *extras, RING_IDX rp, RING_IDX *cons)
124089e0f4d2SKip Macy {
124189e0f4d2SKip Macy 	struct netif_extra_info *extra;
124289e0f4d2SKip Macy 
124389e0f4d2SKip Macy 	int err = 0;
124489e0f4d2SKip Macy 
124589e0f4d2SKip Macy 	do {
124689e0f4d2SKip Macy 		struct mbuf *m;
124789e0f4d2SKip Macy 		grant_ref_t ref;
124889e0f4d2SKip Macy 
124976acc41fSJustin T. Gibbs 		if (__predict_false(*cons + 1 == rp)) {
125089e0f4d2SKip Macy #if 0
125189e0f4d2SKip Macy 			if (net_ratelimit())
125289e0f4d2SKip Macy 				WPRINTK("Missing extra info\n");
125389e0f4d2SKip Macy #endif
1254931eeffaSKenneth D. Merry 			err = EINVAL;
125589e0f4d2SKip Macy 			break;
125689e0f4d2SKip Macy 		}
125789e0f4d2SKip Macy 
125889e0f4d2SKip Macy 		extra = (struct netif_extra_info *)
1259931eeffaSKenneth D. Merry 		RING_GET_RESPONSE(&np->rx, ++(*cons));
126089e0f4d2SKip Macy 
126176acc41fSJustin T. Gibbs 		if (__predict_false(!extra->type ||
126289e0f4d2SKip Macy 			extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
126389e0f4d2SKip Macy #if 0
126489e0f4d2SKip Macy 			if (net_ratelimit())
126589e0f4d2SKip Macy 				WPRINTK("Invalid extra type: %d\n",
126689e0f4d2SKip Macy 					extra->type);
126789e0f4d2SKip Macy #endif
1268931eeffaSKenneth D. Merry 			err = EINVAL;
126989e0f4d2SKip Macy 		} else {
127089e0f4d2SKip Macy 			memcpy(&extras[extra->type - 1], extra, sizeof(*extra));
127189e0f4d2SKip Macy 		}
127289e0f4d2SKip Macy 
1273931eeffaSKenneth D. Merry 		m = xennet_get_rx_mbuf(np, *cons);
1274931eeffaSKenneth D. Merry 		ref = xennet_get_rx_ref(np, *cons);
127589e0f4d2SKip Macy 		xennet_move_rx_slot(np, m, ref);
127689e0f4d2SKip Macy 	} while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
127789e0f4d2SKip Macy 
127889e0f4d2SKip Macy 	return err;
127989e0f4d2SKip Macy }
128089e0f4d2SKip Macy 
128189e0f4d2SKip Macy static int
128289e0f4d2SKip Macy xennet_get_responses(struct netfront_info *np,
1283931eeffaSKenneth D. Merry 	struct netfront_rx_info *rinfo, RING_IDX rp, RING_IDX *cons,
128483b92f6eSKip Macy 	struct mbuf  **list,
128589e0f4d2SKip Macy 	int *pages_flipped_p)
128689e0f4d2SKip Macy {
128789e0f4d2SKip Macy 	int pages_flipped = *pages_flipped_p;
128889e0f4d2SKip Macy 	struct mmu_update *mmu;
128989e0f4d2SKip Macy 	struct multicall_entry *mcl;
129089e0f4d2SKip Macy 	struct netif_rx_response *rx = &rinfo->rx;
129189e0f4d2SKip Macy 	struct netif_extra_info *extras = rinfo->extras;
129283b92f6eSKip Macy 	struct mbuf *m, *m0, *m_prev;
1293931eeffaSKenneth D. Merry 	grant_ref_t ref = xennet_get_rx_ref(np, *cons);
1294931eeffaSKenneth D. Merry 	RING_IDX ref_cons = *cons;
129589e0f4d2SKip Macy 	int frags = 1;
129689e0f4d2SKip Macy 	int err = 0;
129789e0f4d2SKip Macy 	u_long ret;
129889e0f4d2SKip Macy 
1299931eeffaSKenneth D. Merry 	m0 = m = m_prev = xennet_get_rx_mbuf(np, *cons);
130083b92f6eSKip Macy 
130189e0f4d2SKip Macy 	if (rx->flags & NETRXF_extra_info) {
1302931eeffaSKenneth D. Merry 		err = xennet_get_extras(np, extras, rp, cons);
130389e0f4d2SKip Macy 	}
130489e0f4d2SKip Macy 
130583b92f6eSKip Macy 	if (m0 != NULL) {
130683b92f6eSKip Macy 		m0->m_pkthdr.len = 0;
130783b92f6eSKip Macy 		m0->m_next = NULL;
130883b92f6eSKip Macy 	}
130983b92f6eSKip Macy 
131089e0f4d2SKip Macy 	for (;;) {
131189e0f4d2SKip Macy 		u_long mfn;
131289e0f4d2SKip Macy 
131383b92f6eSKip Macy #if 0
1314227ca257SKip Macy 		DPRINTK("rx->status=%hd rx->offset=%hu frags=%u\n",
131583b92f6eSKip Macy 			rx->status, rx->offset, frags);
131683b92f6eSKip Macy #endif
131776acc41fSJustin T. Gibbs 		if (__predict_false(rx->status < 0 ||
131889e0f4d2SKip Macy 			rx->offset + rx->status > PAGE_SIZE)) {
1319931eeffaSKenneth D. Merry 
132089e0f4d2SKip Macy #if 0
132189e0f4d2SKip Macy 			if (net_ratelimit())
132289e0f4d2SKip Macy 				WPRINTK("rx->offset: %x, size: %u\n",
132389e0f4d2SKip Macy 					rx->offset, rx->status);
132489e0f4d2SKip Macy #endif
132589e0f4d2SKip Macy 			xennet_move_rx_slot(np, m, ref);
1326931eeffaSKenneth D. Merry 			if (m0 == m)
1327931eeffaSKenneth D. Merry 				m0 = NULL;
1328931eeffaSKenneth D. Merry 			m = NULL;
1329931eeffaSKenneth D. Merry 			err = EINVAL;
1330931eeffaSKenneth D. Merry 			goto next_skip_queue;
133189e0f4d2SKip Macy 		}
133289e0f4d2SKip Macy 
133389e0f4d2SKip Macy 		/*
133489e0f4d2SKip Macy 		 * This definitely indicates a bug, either in this driver or in
133589e0f4d2SKip Macy 		 * the backend driver. In future this should flag the bad
133689e0f4d2SKip Macy 		 * situation to the system controller to reboot the backed.
133789e0f4d2SKip Macy 		 */
1338ff662b5cSJustin T. Gibbs 		if (ref == GRANT_REF_INVALID) {
1339931eeffaSKenneth D. Merry 
134089e0f4d2SKip Macy #if 0
134189e0f4d2SKip Macy 			if (net_ratelimit())
134289e0f4d2SKip Macy 				WPRINTK("Bad rx response id %d.\n", rx->id);
134389e0f4d2SKip Macy #endif
1344ff662b5cSJustin T. Gibbs 			printf("%s: Bad rx response id %d.\n", __func__,rx->id);
1345931eeffaSKenneth D. Merry 			err = EINVAL;
134689e0f4d2SKip Macy 			goto next;
134789e0f4d2SKip Macy 		}
134889e0f4d2SKip Macy 
134989e0f4d2SKip Macy 		if (!np->copying_receiver) {
135089e0f4d2SKip Macy 			/* Memory pressure, insufficient buffer
135189e0f4d2SKip Macy 			 * headroom, ...
135289e0f4d2SKip Macy 			 */
135389e0f4d2SKip Macy 			if (!(mfn = gnttab_end_foreign_transfer_ref(ref))) {
1354931eeffaSKenneth D. Merry 				WPRINTK("Unfulfilled rx req (id=%d, st=%d).\n",
135589e0f4d2SKip Macy 					rx->id, rx->status);
135689e0f4d2SKip Macy 				xennet_move_rx_slot(np, m, ref);
1357931eeffaSKenneth D. Merry 				err = ENOMEM;
135889e0f4d2SKip Macy 				goto next;
135989e0f4d2SKip Macy 			}
136089e0f4d2SKip Macy 
136189e0f4d2SKip Macy 			if (!xen_feature( XENFEAT_auto_translated_physmap)) {
136289e0f4d2SKip Macy 				/* Remap the page. */
136389e0f4d2SKip Macy 				void *vaddr = mtod(m, void *);
136489e0f4d2SKip Macy 				uint32_t pfn;
136589e0f4d2SKip Macy 
136689e0f4d2SKip Macy 				mcl = np->rx_mcl + pages_flipped;
136789e0f4d2SKip Macy 				mmu = np->rx_mmu + pages_flipped;
136889e0f4d2SKip Macy 
136989e0f4d2SKip Macy 				MULTI_update_va_mapping(mcl, (u_long)vaddr,
13706ae0e31bSKip Macy 				    (((vm_paddr_t)mfn) << PAGE_SHIFT) | PG_RW |
137189e0f4d2SKip Macy 				    PG_V | PG_M | PG_A, 0);
13723a6d1fcfSKip Macy 				pfn = (uintptr_t)m->m_ext.ext_arg1;
137389e0f4d2SKip Macy 				mmu->ptr = ((vm_paddr_t)mfn << PAGE_SHIFT) |
137489e0f4d2SKip Macy 				    MMU_MACHPHYS_UPDATE;
137589e0f4d2SKip Macy 				mmu->val = pfn;
137689e0f4d2SKip Macy 
137789e0f4d2SKip Macy 				set_phys_to_machine(pfn, mfn);
137889e0f4d2SKip Macy 			}
137989e0f4d2SKip Macy 			pages_flipped++;
138089e0f4d2SKip Macy 		} else {
1381920ba15bSKip Macy 			ret = gnttab_end_foreign_access_ref(ref);
138289e0f4d2SKip Macy 			KASSERT(ret, ("ret != 0"));
138389e0f4d2SKip Macy 		}
138489e0f4d2SKip Macy 
138589e0f4d2SKip Macy 		gnttab_release_grant_reference(&np->gref_rx_head, ref);
138689e0f4d2SKip Macy 
138789e0f4d2SKip Macy next:
13883a539122SAdrian Chadd 		if (m == NULL)
13893a539122SAdrian Chadd 			break;
13903a539122SAdrian Chadd 
139183b92f6eSKip Macy 		m->m_len = rx->status;
139283b92f6eSKip Macy 		m->m_data += rx->offset;
139383b92f6eSKip Macy 		m0->m_pkthdr.len += rx->status;
139483b92f6eSKip Macy 
1395931eeffaSKenneth D. Merry next_skip_queue:
139689e0f4d2SKip Macy 		if (!(rx->flags & NETRXF_more_data))
139789e0f4d2SKip Macy 			break;
139889e0f4d2SKip Macy 
1399931eeffaSKenneth D. Merry 		if (*cons + frags == rp) {
140089e0f4d2SKip Macy 			if (net_ratelimit())
140189e0f4d2SKip Macy 				WPRINTK("Need more frags\n");
1402931eeffaSKenneth D. Merry 			err = ENOENT;
1403931eeffaSKenneth D. Merry 			printf("%s: cons %u frags %u rp %u, not enough frags\n",
1404931eeffaSKenneth D. Merry 			       __func__, *cons, frags, rp);
140589e0f4d2SKip Macy 			break;
140689e0f4d2SKip Macy 		}
1407931eeffaSKenneth D. Merry 		/*
1408931eeffaSKenneth D. Merry 		 * Note that m can be NULL, if rx->status < 0 or if
1409931eeffaSKenneth D. Merry 		 * rx->offset + rx->status > PAGE_SIZE above.
1410931eeffaSKenneth D. Merry 		 */
141183b92f6eSKip Macy 		m_prev = m;
141289e0f4d2SKip Macy 
1413931eeffaSKenneth D. Merry 		rx = RING_GET_RESPONSE(&np->rx, *cons + frags);
1414931eeffaSKenneth D. Merry 		m = xennet_get_rx_mbuf(np, *cons + frags);
141583b92f6eSKip Macy 
1416931eeffaSKenneth D. Merry 		/*
1417931eeffaSKenneth D. Merry 		 * m_prev == NULL can happen if rx->status < 0 or if
1418931eeffaSKenneth D. Merry 		 * rx->offset + * rx->status > PAGE_SIZE above.
1419931eeffaSKenneth D. Merry 		 */
1420931eeffaSKenneth D. Merry 		if (m_prev != NULL)
142183b92f6eSKip Macy 			m_prev->m_next = m;
1422931eeffaSKenneth D. Merry 
1423931eeffaSKenneth D. Merry 		/*
1424931eeffaSKenneth D. Merry 		 * m0 can be NULL if rx->status < 0 or if * rx->offset +
1425931eeffaSKenneth D. Merry 		 * rx->status > PAGE_SIZE above.
1426931eeffaSKenneth D. Merry 		 */
1427931eeffaSKenneth D. Merry 		if (m0 == NULL)
1428931eeffaSKenneth D. Merry 			m0 = m;
142983b92f6eSKip Macy 		m->m_next = NULL;
1430931eeffaSKenneth D. Merry 		ref = xennet_get_rx_ref(np, *cons + frags);
1431931eeffaSKenneth D. Merry 		ref_cons = *cons + frags;
143289e0f4d2SKip Macy 		frags++;
143389e0f4d2SKip Macy 	}
143483b92f6eSKip Macy 	*list = m0;
1435931eeffaSKenneth D. Merry 	*cons += frags;
143689e0f4d2SKip Macy 	*pages_flipped_p = pages_flipped;
143789e0f4d2SKip Macy 
14388577146eSJustin T. Gibbs 	return (err);
143989e0f4d2SKip Macy }
144089e0f4d2SKip Macy 
144189e0f4d2SKip Macy static void
144289e0f4d2SKip Macy xn_tick_locked(struct netfront_info *sc)
144389e0f4d2SKip Macy {
144489e0f4d2SKip Macy 	XN_RX_LOCK_ASSERT(sc);
144589e0f4d2SKip Macy 	callout_reset(&sc->xn_stat_ch, hz, xn_tick, sc);
144689e0f4d2SKip Macy 
144789e0f4d2SKip Macy 	/* XXX placeholder for printing debug information */
144889e0f4d2SKip Macy }
144989e0f4d2SKip Macy 
145089e0f4d2SKip Macy static void
145189e0f4d2SKip Macy xn_tick(void *xsc)
145289e0f4d2SKip Macy {
145389e0f4d2SKip Macy 	struct netfront_info *sc;
145489e0f4d2SKip Macy 
145589e0f4d2SKip Macy 	sc = xsc;
145689e0f4d2SKip Macy 	XN_RX_LOCK(sc);
145789e0f4d2SKip Macy 	xn_tick_locked(sc);
145889e0f4d2SKip Macy 	XN_RX_UNLOCK(sc);
145989e0f4d2SKip Macy }
146089e0f4d2SKip Macy 
1461931eeffaSKenneth D. Merry /**
1462931eeffaSKenneth D. Merry  * \brief Count the number of fragments in an mbuf chain.
1463931eeffaSKenneth D. Merry  *
1464931eeffaSKenneth D. Merry  * Surprisingly, there isn't an M* macro for this.
1465c099cafaSAdrian Chadd  */
1466931eeffaSKenneth D. Merry static inline int
1467931eeffaSKenneth D. Merry xn_count_frags(struct mbuf *m)
1468931eeffaSKenneth D. Merry {
1469931eeffaSKenneth D. Merry 	int nfrags;
1470931eeffaSKenneth D. Merry 
1471931eeffaSKenneth D. Merry 	for (nfrags = 0; m != NULL; m = m->m_next)
1472931eeffaSKenneth D. Merry 		nfrags++;
1473931eeffaSKenneth D. Merry 
1474931eeffaSKenneth D. Merry 	return (nfrags);
147589e0f4d2SKip Macy }
147689e0f4d2SKip Macy 
1477931eeffaSKenneth D. Merry /**
1478931eeffaSKenneth D. Merry  * Given an mbuf chain, make sure we have enough room and then push
1479931eeffaSKenneth D. Merry  * it onto the transmit ring.
1480931eeffaSKenneth D. Merry  */
1481931eeffaSKenneth D. Merry static int
1482931eeffaSKenneth D. Merry xn_assemble_tx_request(struct netfront_info *sc, struct mbuf *m_head)
1483931eeffaSKenneth D. Merry {
1484931eeffaSKenneth D. Merry 	struct ifnet *ifp;
1485931eeffaSKenneth D. Merry 	struct mbuf *m;
1486931eeffaSKenneth D. Merry 	u_int nfrags;
1487931eeffaSKenneth D. Merry 	netif_extra_info_t *extra;
1488931eeffaSKenneth D. Merry 	int otherend_id;
1489931eeffaSKenneth D. Merry 
1490931eeffaSKenneth D. Merry 	ifp = sc->xn_ifp;
1491931eeffaSKenneth D. Merry 
1492931eeffaSKenneth D. Merry 	/**
149312678024SDoug Rabson 	 * Defragment the mbuf if necessary.
149412678024SDoug Rabson 	 */
1495931eeffaSKenneth D. Merry 	nfrags = xn_count_frags(m_head);
1496931eeffaSKenneth D. Merry 
1497931eeffaSKenneth D. Merry 	/*
1498931eeffaSKenneth D. Merry 	 * Check to see whether this request is longer than netback
1499931eeffaSKenneth D. Merry 	 * can handle, and try to defrag it.
1500931eeffaSKenneth D. Merry 	 */
1501931eeffaSKenneth D. Merry 	/**
1502931eeffaSKenneth D. Merry 	 * It is a bit lame, but the netback driver in Linux can't
1503931eeffaSKenneth D. Merry 	 * deal with nfrags > MAX_TX_REQ_FRAGS, which is a quirk of
1504931eeffaSKenneth D. Merry 	 * the Linux network stack.
1505931eeffaSKenneth D. Merry 	 */
1506578e4bf7SJustin T. Gibbs 	if (nfrags > sc->maxfrags) {
1507c6499eccSGleb Smirnoff 		m = m_defrag(m_head, M_NOWAIT);
150812678024SDoug Rabson 		if (!m) {
1509931eeffaSKenneth D. Merry 			/*
1510931eeffaSKenneth D. Merry 			 * Defrag failed, so free the mbuf and
1511931eeffaSKenneth D. Merry 			 * therefore drop the packet.
1512931eeffaSKenneth D. Merry 			 */
151312678024SDoug Rabson 			m_freem(m_head);
1514931eeffaSKenneth D. Merry 			return (EMSGSIZE);
151512678024SDoug Rabson 		}
151612678024SDoug Rabson 		m_head = m;
151712678024SDoug Rabson 	}
151889e0f4d2SKip Macy 
1519a4ec37f5SAdrian Chadd 	/* Determine how many fragments now exist */
1520931eeffaSKenneth D. Merry 	nfrags = xn_count_frags(m_head);
1521a4ec37f5SAdrian Chadd 
1522a4ec37f5SAdrian Chadd 	/*
1523931eeffaSKenneth D. Merry 	 * Check to see whether the defragmented packet has too many
1524931eeffaSKenneth D. Merry 	 * segments for the Linux netback driver.
1525a4ec37f5SAdrian Chadd 	 */
1526931eeffaSKenneth D. Merry 	/**
1527931eeffaSKenneth D. Merry 	 * The FreeBSD TCP stack, with TSO enabled, can produce a chain
1528931eeffaSKenneth D. Merry 	 * of mbufs longer than Linux can handle.  Make sure we don't
1529931eeffaSKenneth D. Merry 	 * pass a too-long chain over to the other side by dropping the
1530931eeffaSKenneth D. Merry 	 * packet.  It doesn't look like there is currently a way to
1531931eeffaSKenneth D. Merry 	 * tell the TCP stack to generate a shorter chain of packets.
15323fb28bbbSAdrian Chadd 	 */
1533931eeffaSKenneth D. Merry 	if (nfrags > MAX_TX_REQ_FRAGS) {
1534ff662b5cSJustin T. Gibbs #ifdef DEBUG
1535ff662b5cSJustin T. Gibbs 		printf("%s: nfrags %d > MAX_TX_REQ_FRAGS %d, netback "
1536ff662b5cSJustin T. Gibbs 		       "won't be able to handle it, dropping\n",
1537ff662b5cSJustin T. Gibbs 		       __func__, nfrags, MAX_TX_REQ_FRAGS);
1538ff662b5cSJustin T. Gibbs #endif
1539931eeffaSKenneth D. Merry 		m_freem(m_head);
1540931eeffaSKenneth D. Merry 		return (EMSGSIZE);
1541a4ec37f5SAdrian Chadd 	}
1542a4ec37f5SAdrian Chadd 
15433fb28bbbSAdrian Chadd 	/*
1544931eeffaSKenneth D. Merry 	 * This check should be redundant.  We've already verified that we
1545931eeffaSKenneth D. Merry 	 * have enough slots in the ring to handle a packet of maximum
1546931eeffaSKenneth D. Merry 	 * size, and that our packet is less than the maximum size.  Keep
1547931eeffaSKenneth D. Merry 	 * it in here as an assert for now just to make certain that
1548931eeffaSKenneth D. Merry 	 * xn_tx_chain_cnt is accurate.
15493fb28bbbSAdrian Chadd 	 */
1550931eeffaSKenneth D. Merry 	KASSERT((sc->xn_cdata.xn_tx_chain_cnt + nfrags) <= NET_TX_RING_SIZE,
1551931eeffaSKenneth D. Merry 		("%s: xn_tx_chain_cnt (%d) + nfrags (%d) > NET_TX_RING_SIZE "
1552931eeffaSKenneth D. Merry 		 "(%d)!", __func__, (int) sc->xn_cdata.xn_tx_chain_cnt,
1553931eeffaSKenneth D. Merry                     (int) nfrags, (int) NET_TX_RING_SIZE));
1554a4ec37f5SAdrian Chadd 
155589e0f4d2SKip Macy 	/*
155689e0f4d2SKip Macy 	 * Start packing the mbufs in this chain into
155789e0f4d2SKip Macy 	 * the fragment pointers. Stop when we run out
155889e0f4d2SKip Macy 	 * of fragments or hit the end of the mbuf chain.
155989e0f4d2SKip Macy 	 */
156012678024SDoug Rabson 	m = m_head;
156112678024SDoug Rabson 	extra = NULL;
1562931eeffaSKenneth D. Merry 	otherend_id = xenbus_get_otherend_id(sc->xbdev);
156312678024SDoug Rabson 	for (m = m_head; m; m = m->m_next) {
1564931eeffaSKenneth D. Merry 		netif_tx_request_t *tx;
1565931eeffaSKenneth D. Merry 		uintptr_t id;
1566931eeffaSKenneth D. Merry 		grant_ref_t ref;
1567931eeffaSKenneth D. Merry 		u_long mfn; /* XXX Wrong type? */
1568931eeffaSKenneth D. Merry 
1569931eeffaSKenneth D. Merry 		tx = RING_GET_REQUEST(&sc->tx, sc->tx.req_prod_pvt);
1570931eeffaSKenneth D. Merry 		id = get_id_from_freelist(sc->tx_mbufs);
1571a4ec37f5SAdrian Chadd 		if (id == 0)
15726f9767acSMarius Strobl 			panic("%s: was allocated the freelist head!\n",
15736f9767acSMarius Strobl 			    __func__);
1574a4ec37f5SAdrian Chadd 		sc->xn_cdata.xn_tx_chain_cnt++;
1575931eeffaSKenneth D. Merry 		if (sc->xn_cdata.xn_tx_chain_cnt > NET_TX_RING_SIZE)
15766f9767acSMarius Strobl 			panic("%s: tx_chain_cnt must be <= NET_TX_RING_SIZE\n",
15776f9767acSMarius Strobl 			    __func__);
1578931eeffaSKenneth D. Merry 		sc->tx_mbufs[id] = m;
157989e0f4d2SKip Macy 		tx->id = id;
158089e0f4d2SKip Macy 		ref = gnttab_claim_grant_reference(&sc->gref_tx_head);
158189e0f4d2SKip Macy 		KASSERT((short)ref >= 0, ("Negative ref"));
158212678024SDoug Rabson 		mfn = virt_to_mfn(mtod(m, vm_offset_t));
158323dc5621SKip Macy 		gnttab_grant_foreign_access_ref(ref, otherend_id,
158489e0f4d2SKip Macy 		    mfn, GNTMAP_readonly);
158589e0f4d2SKip Macy 		tx->gref = sc->grant_tx_ref[id] = ref;
158612678024SDoug Rabson 		tx->offset = mtod(m, vm_offset_t) & (PAGE_SIZE - 1);
158789e0f4d2SKip Macy 		tx->flags = 0;
158812678024SDoug Rabson 		if (m == m_head) {
158912678024SDoug Rabson 			/*
159012678024SDoug Rabson 			 * The first fragment has the entire packet
159112678024SDoug Rabson 			 * size, subsequent fragments have just the
159212678024SDoug Rabson 			 * fragment size. The backend works out the
159312678024SDoug Rabson 			 * true size of the first fragment by
159412678024SDoug Rabson 			 * subtracting the sizes of the other
159512678024SDoug Rabson 			 * fragments.
159612678024SDoug Rabson 			 */
159712678024SDoug Rabson 			tx->size = m->m_pkthdr.len;
159889e0f4d2SKip Macy 
159912678024SDoug Rabson 			/*
1600931eeffaSKenneth D. Merry 			 * The first fragment contains the checksum flags
1601931eeffaSKenneth D. Merry 			 * and is optionally followed by extra data for
1602931eeffaSKenneth D. Merry 			 * TSO etc.
1603931eeffaSKenneth D. Merry 			 */
1604931eeffaSKenneth D. Merry 			/**
1605931eeffaSKenneth D. Merry 			 * CSUM_TSO requires checksum offloading.
1606931eeffaSKenneth D. Merry 			 * Some versions of FreeBSD fail to
1607931eeffaSKenneth D. Merry 			 * set CSUM_TCP in the CSUM_TSO case,
1608931eeffaSKenneth D. Merry 			 * so we have to test for CSUM_TSO
1609931eeffaSKenneth D. Merry 			 * explicitly.
161012678024SDoug Rabson 			 */
161112678024SDoug Rabson 			if (m->m_pkthdr.csum_flags
1612931eeffaSKenneth D. Merry 			    & (CSUM_DELAY_DATA | CSUM_TSO)) {
161312678024SDoug Rabson 				tx->flags |= (NETTXF_csum_blank
161412678024SDoug Rabson 				    | NETTXF_data_validated);
161512678024SDoug Rabson 			}
161612678024SDoug Rabson #if __FreeBSD_version >= 700000
161712678024SDoug Rabson 			if (m->m_pkthdr.csum_flags & CSUM_TSO) {
161812678024SDoug Rabson 				struct netif_extra_info *gso =
161912678024SDoug Rabson 					(struct netif_extra_info *)
1620931eeffaSKenneth D. Merry 					RING_GET_REQUEST(&sc->tx,
1621931eeffaSKenneth D. Merry 							 ++sc->tx.req_prod_pvt);
162289e0f4d2SKip Macy 
162312678024SDoug Rabson 				tx->flags |= NETTXF_extra_info;
162489e0f4d2SKip Macy 
162512678024SDoug Rabson 				gso->u.gso.size = m->m_pkthdr.tso_segsz;
162612678024SDoug Rabson 				gso->u.gso.type =
162712678024SDoug Rabson 					XEN_NETIF_GSO_TYPE_TCPV4;
162812678024SDoug Rabson 				gso->u.gso.pad = 0;
162912678024SDoug Rabson 				gso->u.gso.features = 0;
163012678024SDoug Rabson 
163112678024SDoug Rabson 				gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
163212678024SDoug Rabson 				gso->flags = 0;
163312678024SDoug Rabson 			}
163412678024SDoug Rabson #endif
163512678024SDoug Rabson 		} else {
163612678024SDoug Rabson 			tx->size = m->m_len;
163712678024SDoug Rabson 		}
1638931eeffaSKenneth D. Merry 		if (m->m_next)
163912678024SDoug Rabson 			tx->flags |= NETTXF_more_data;
164012678024SDoug Rabson 
1641931eeffaSKenneth D. Merry 		sc->tx.req_prod_pvt++;
1642931eeffaSKenneth D. Merry 	}
164312678024SDoug Rabson 	BPF_MTAP(ifp, m_head);
164412678024SDoug Rabson 
164512678024SDoug Rabson 	sc->stats.tx_bytes += m_head->m_pkthdr.len;
164689e0f4d2SKip Macy 	sc->stats.tx_packets++;
1647931eeffaSKenneth D. Merry 
1648931eeffaSKenneth D. Merry 	return (0);
164989e0f4d2SKip Macy }
165089e0f4d2SKip Macy 
1651931eeffaSKenneth D. Merry static void
1652931eeffaSKenneth D. Merry xn_start_locked(struct ifnet *ifp)
1653931eeffaSKenneth D. Merry {
1654931eeffaSKenneth D. Merry 	struct netfront_info *sc;
1655931eeffaSKenneth D. Merry 	struct mbuf *m_head;
1656931eeffaSKenneth D. Merry 	int notify;
1657931eeffaSKenneth D. Merry 
1658931eeffaSKenneth D. Merry 	sc = ifp->if_softc;
1659931eeffaSKenneth D. Merry 
1660931eeffaSKenneth D. Merry 	if (!netfront_carrier_ok(sc))
1661931eeffaSKenneth D. Merry 		return;
1662931eeffaSKenneth D. Merry 
1663931eeffaSKenneth D. Merry 	/*
1664931eeffaSKenneth D. Merry 	 * While we have enough transmit slots available for at least one
1665931eeffaSKenneth D. Merry 	 * maximum-sized packet, pull mbufs off the queue and put them on
1666931eeffaSKenneth D. Merry 	 * the transmit ring.
1667931eeffaSKenneth D. Merry 	 */
1668931eeffaSKenneth D. Merry 	while (xn_tx_slot_available(sc)) {
1669931eeffaSKenneth D. Merry 		IF_DEQUEUE(&ifp->if_snd, m_head);
1670931eeffaSKenneth D. Merry 		if (m_head == NULL)
1671931eeffaSKenneth D. Merry 			break;
1672931eeffaSKenneth D. Merry 
1673931eeffaSKenneth D. Merry 		if (xn_assemble_tx_request(sc, m_head) != 0)
1674931eeffaSKenneth D. Merry 			break;
1675931eeffaSKenneth D. Merry 	}
1676931eeffaSKenneth D. Merry 
167789e0f4d2SKip Macy 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->tx, notify);
167889e0f4d2SKip Macy 	if (notify)
167976acc41fSJustin T. Gibbs 		xen_intr_signal(sc->xen_intr_handle);
168089e0f4d2SKip Macy 
168189e0f4d2SKip Macy 	if (RING_FULL(&sc->tx)) {
168289e0f4d2SKip Macy 		sc->tx_full = 1;
168389e0f4d2SKip Macy #if 0
168489e0f4d2SKip Macy 		netif_stop_queue(dev);
168589e0f4d2SKip Macy #endif
168689e0f4d2SKip Macy 	}
168789e0f4d2SKip Macy }
168889e0f4d2SKip Macy 
168989e0f4d2SKip Macy static void
169089e0f4d2SKip Macy xn_start(struct ifnet *ifp)
169189e0f4d2SKip Macy {
169289e0f4d2SKip Macy 	struct netfront_info *sc;
169389e0f4d2SKip Macy 	sc = ifp->if_softc;
169489e0f4d2SKip Macy 	XN_TX_LOCK(sc);
169589e0f4d2SKip Macy 	xn_start_locked(ifp);
169689e0f4d2SKip Macy 	XN_TX_UNLOCK(sc);
169789e0f4d2SKip Macy }
169889e0f4d2SKip Macy 
169989e0f4d2SKip Macy /* equivalent of network_open() in Linux */
170089e0f4d2SKip Macy static void
170189e0f4d2SKip Macy xn_ifinit_locked(struct netfront_info *sc)
170289e0f4d2SKip Macy {
170389e0f4d2SKip Macy 	struct ifnet *ifp;
170489e0f4d2SKip Macy 
170589e0f4d2SKip Macy 	XN_LOCK_ASSERT(sc);
170689e0f4d2SKip Macy 
170789e0f4d2SKip Macy 	ifp = sc->xn_ifp;
170889e0f4d2SKip Macy 
170989e0f4d2SKip Macy 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
171089e0f4d2SKip Macy 		return;
171189e0f4d2SKip Macy 
171289e0f4d2SKip Macy 	xn_stop(sc);
171389e0f4d2SKip Macy 
171489e0f4d2SKip Macy 	network_alloc_rx_buffers(sc);
171589e0f4d2SKip Macy 	sc->rx.sring->rsp_event = sc->rx.rsp_cons + 1;
171689e0f4d2SKip Macy 
171789e0f4d2SKip Macy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
171889e0f4d2SKip Macy 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
17190e509842SJustin T. Gibbs 	if_link_state_change(ifp, LINK_STATE_UP);
172089e0f4d2SKip Macy 
172189e0f4d2SKip Macy 	callout_reset(&sc->xn_stat_ch, hz, xn_tick, sc);
172289e0f4d2SKip Macy }
172389e0f4d2SKip Macy 
172489e0f4d2SKip Macy static void
172589e0f4d2SKip Macy xn_ifinit(void *xsc)
172689e0f4d2SKip Macy {
172789e0f4d2SKip Macy 	struct netfront_info *sc = xsc;
172889e0f4d2SKip Macy 
172989e0f4d2SKip Macy 	XN_LOCK(sc);
173089e0f4d2SKip Macy 	xn_ifinit_locked(sc);
173189e0f4d2SKip Macy 	XN_UNLOCK(sc);
173289e0f4d2SKip Macy }
173389e0f4d2SKip Macy 
173489e0f4d2SKip Macy static int
173589e0f4d2SKip Macy xn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
173689e0f4d2SKip Macy {
173789e0f4d2SKip Macy 	struct netfront_info *sc = ifp->if_softc;
173889e0f4d2SKip Macy 	struct ifreq *ifr = (struct ifreq *) data;
1739a0ae8f04SBjoern A. Zeeb #ifdef INET
174089e0f4d2SKip Macy 	struct ifaddr *ifa = (struct ifaddr *)data;
1741a0ae8f04SBjoern A. Zeeb #endif
174289e0f4d2SKip Macy 
174389e0f4d2SKip Macy 	int mask, error = 0;
174489e0f4d2SKip Macy 	switch(cmd) {
174589e0f4d2SKip Macy 	case SIOCSIFADDR:
174689e0f4d2SKip Macy 	case SIOCGIFADDR:
1747a0ae8f04SBjoern A. Zeeb #ifdef INET
174889e0f4d2SKip Macy 		XN_LOCK(sc);
174989e0f4d2SKip Macy 		if (ifa->ifa_addr->sa_family == AF_INET) {
175089e0f4d2SKip Macy 			ifp->if_flags |= IFF_UP;
175189e0f4d2SKip Macy 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
175289e0f4d2SKip Macy 				xn_ifinit_locked(sc);
175389e0f4d2SKip Macy 			arp_ifinit(ifp, ifa);
175489e0f4d2SKip Macy 			XN_UNLOCK(sc);
175549906218SDoug Rabson 		} else {
175649906218SDoug Rabson 			XN_UNLOCK(sc);
1757a0ae8f04SBjoern A. Zeeb #endif
175849906218SDoug Rabson 			error = ether_ioctl(ifp, cmd, data);
1759a0ae8f04SBjoern A. Zeeb #ifdef INET
176049906218SDoug Rabson 		}
1761a0ae8f04SBjoern A. Zeeb #endif
176289e0f4d2SKip Macy 		break;
176389e0f4d2SKip Macy 	case SIOCSIFMTU:
176489e0f4d2SKip Macy 		/* XXX can we alter the MTU on a VN ?*/
176589e0f4d2SKip Macy #ifdef notyet
176689e0f4d2SKip Macy 		if (ifr->ifr_mtu > XN_JUMBO_MTU)
176789e0f4d2SKip Macy 			error = EINVAL;
176889e0f4d2SKip Macy 		else
176989e0f4d2SKip Macy #endif
177089e0f4d2SKip Macy 		{
177189e0f4d2SKip Macy 			ifp->if_mtu = ifr->ifr_mtu;
177289e0f4d2SKip Macy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
177389e0f4d2SKip Macy 			xn_ifinit(sc);
177489e0f4d2SKip Macy 		}
177589e0f4d2SKip Macy 		break;
177689e0f4d2SKip Macy 	case SIOCSIFFLAGS:
177789e0f4d2SKip Macy 		XN_LOCK(sc);
177889e0f4d2SKip Macy 		if (ifp->if_flags & IFF_UP) {
177989e0f4d2SKip Macy 			/*
178089e0f4d2SKip Macy 			 * If only the state of the PROMISC flag changed,
178189e0f4d2SKip Macy 			 * then just use the 'set promisc mode' command
178289e0f4d2SKip Macy 			 * instead of reinitializing the entire NIC. Doing
178389e0f4d2SKip Macy 			 * a full re-init means reloading the firmware and
178489e0f4d2SKip Macy 			 * waiting for it to start up, which may take a
178589e0f4d2SKip Macy 			 * second or two.
178689e0f4d2SKip Macy 			 */
178789e0f4d2SKip Macy #ifdef notyet
178889e0f4d2SKip Macy 			/* No promiscuous mode with Xen */
178989e0f4d2SKip Macy 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
179089e0f4d2SKip Macy 			    ifp->if_flags & IFF_PROMISC &&
179189e0f4d2SKip Macy 			    !(sc->xn_if_flags & IFF_PROMISC)) {
179289e0f4d2SKip Macy 				XN_SETBIT(sc, XN_RX_MODE,
179389e0f4d2SKip Macy 					  XN_RXMODE_RX_PROMISC);
179489e0f4d2SKip Macy 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
179589e0f4d2SKip Macy 				   !(ifp->if_flags & IFF_PROMISC) &&
179689e0f4d2SKip Macy 				   sc->xn_if_flags & IFF_PROMISC) {
179789e0f4d2SKip Macy 				XN_CLRBIT(sc, XN_RX_MODE,
179889e0f4d2SKip Macy 					  XN_RXMODE_RX_PROMISC);
179989e0f4d2SKip Macy 			} else
180089e0f4d2SKip Macy #endif
180189e0f4d2SKip Macy 				xn_ifinit_locked(sc);
180289e0f4d2SKip Macy 		} else {
180389e0f4d2SKip Macy 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
180489e0f4d2SKip Macy 				xn_stop(sc);
180589e0f4d2SKip Macy 			}
180689e0f4d2SKip Macy 		}
180789e0f4d2SKip Macy 		sc->xn_if_flags = ifp->if_flags;
180889e0f4d2SKip Macy 		XN_UNLOCK(sc);
180989e0f4d2SKip Macy 		error = 0;
181089e0f4d2SKip Macy 		break;
181189e0f4d2SKip Macy 	case SIOCSIFCAP:
181289e0f4d2SKip Macy 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
181312678024SDoug Rabson 		if (mask & IFCAP_TXCSUM) {
181412678024SDoug Rabson 			if (IFCAP_TXCSUM & ifp->if_capenable) {
181512678024SDoug Rabson 				ifp->if_capenable &= ~(IFCAP_TXCSUM|IFCAP_TSO4);
181612678024SDoug Rabson 				ifp->if_hwassist &= ~(CSUM_TCP | CSUM_UDP
181712678024SDoug Rabson 				    | CSUM_IP | CSUM_TSO);
181812678024SDoug Rabson 			} else {
181912678024SDoug Rabson 				ifp->if_capenable |= IFCAP_TXCSUM;
182012678024SDoug Rabson 				ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP
182112678024SDoug Rabson 				    | CSUM_IP);
182289e0f4d2SKip Macy 			}
182312678024SDoug Rabson 		}
182412678024SDoug Rabson 		if (mask & IFCAP_RXCSUM) {
182512678024SDoug Rabson 			ifp->if_capenable ^= IFCAP_RXCSUM;
182612678024SDoug Rabson 		}
182712678024SDoug Rabson #if __FreeBSD_version >= 700000
182812678024SDoug Rabson 		if (mask & IFCAP_TSO4) {
182912678024SDoug Rabson 			if (IFCAP_TSO4 & ifp->if_capenable) {
183012678024SDoug Rabson 				ifp->if_capenable &= ~IFCAP_TSO4;
183112678024SDoug Rabson 				ifp->if_hwassist &= ~CSUM_TSO;
183212678024SDoug Rabson 			} else if (IFCAP_TXCSUM & ifp->if_capenable) {
183312678024SDoug Rabson 				ifp->if_capenable |= IFCAP_TSO4;
183412678024SDoug Rabson 				ifp->if_hwassist |= CSUM_TSO;
183512678024SDoug Rabson 			} else {
18363552092bSAdrian Chadd 				IPRINTK("Xen requires tx checksum offload"
183712678024SDoug Rabson 				    " be enabled to use TSO\n");
183812678024SDoug Rabson 				error = EINVAL;
183912678024SDoug Rabson 			}
184012678024SDoug Rabson 		}
184112678024SDoug Rabson 		if (mask & IFCAP_LRO) {
184212678024SDoug Rabson 			ifp->if_capenable ^= IFCAP_LRO;
184312678024SDoug Rabson 
184412678024SDoug Rabson 		}
184512678024SDoug Rabson #endif
184689e0f4d2SKip Macy 		error = 0;
184789e0f4d2SKip Macy 		break;
184889e0f4d2SKip Macy 	case SIOCADDMULTI:
184989e0f4d2SKip Macy 	case SIOCDELMULTI:
185089e0f4d2SKip Macy #ifdef notyet
185189e0f4d2SKip Macy 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
185289e0f4d2SKip Macy 			XN_LOCK(sc);
185389e0f4d2SKip Macy 			xn_setmulti(sc);
185489e0f4d2SKip Macy 			XN_UNLOCK(sc);
185589e0f4d2SKip Macy 			error = 0;
185689e0f4d2SKip Macy 		}
185789e0f4d2SKip Macy #endif
185889e0f4d2SKip Macy 		/* FALLTHROUGH */
185989e0f4d2SKip Macy 	case SIOCSIFMEDIA:
186089e0f4d2SKip Macy 	case SIOCGIFMEDIA:
18610e509842SJustin T. Gibbs 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
186289e0f4d2SKip Macy 		break;
186389e0f4d2SKip Macy 	default:
186489e0f4d2SKip Macy 		error = ether_ioctl(ifp, cmd, data);
186589e0f4d2SKip Macy 	}
186689e0f4d2SKip Macy 
186789e0f4d2SKip Macy 	return (error);
186889e0f4d2SKip Macy }
186989e0f4d2SKip Macy 
187089e0f4d2SKip Macy static void
187189e0f4d2SKip Macy xn_stop(struct netfront_info *sc)
187289e0f4d2SKip Macy {
187389e0f4d2SKip Macy 	struct ifnet *ifp;
187489e0f4d2SKip Macy 
187589e0f4d2SKip Macy 	XN_LOCK_ASSERT(sc);
187689e0f4d2SKip Macy 
187789e0f4d2SKip Macy 	ifp = sc->xn_ifp;
187889e0f4d2SKip Macy 
187989e0f4d2SKip Macy 	callout_stop(&sc->xn_stat_ch);
188089e0f4d2SKip Macy 
188189e0f4d2SKip Macy 	xn_free_rx_ring(sc);
188289e0f4d2SKip Macy 	xn_free_tx_ring(sc);
188389e0f4d2SKip Macy 
188489e0f4d2SKip Macy 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
18850e509842SJustin T. Gibbs 	if_link_state_change(ifp, LINK_STATE_DOWN);
188689e0f4d2SKip Macy }
188789e0f4d2SKip Macy 
188889e0f4d2SKip Macy /* START of Xenolinux helper functions adapted to FreeBSD */
188923dc5621SKip Macy int
189023dc5621SKip Macy network_connect(struct netfront_info *np)
189189e0f4d2SKip Macy {
18923a6d1fcfSKip Macy 	int i, requeue_idx, error;
189389e0f4d2SKip Macy 	grant_ref_t ref;
189489e0f4d2SKip Macy 	netif_rx_request_t *req;
189589e0f4d2SKip Macy 	u_int feature_rx_copy, feature_rx_flip;
189689e0f4d2SKip Macy 
1897ff662b5cSJustin T. Gibbs 	error = xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev),
18983a6d1fcfSKip Macy 	    "feature-rx-copy", NULL, "%u", &feature_rx_copy);
18993a6d1fcfSKip Macy 	if (error)
190089e0f4d2SKip Macy 		feature_rx_copy = 0;
1901ff662b5cSJustin T. Gibbs 	error = xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev),
19023a6d1fcfSKip Macy 	    "feature-rx-flip", NULL, "%u", &feature_rx_flip);
19033a6d1fcfSKip Macy 	if (error)
190489e0f4d2SKip Macy 		feature_rx_flip = 1;
190589e0f4d2SKip Macy 
190689e0f4d2SKip Macy 	/*
190789e0f4d2SKip Macy 	 * Copy packets on receive path if:
190889e0f4d2SKip Macy 	 *  (a) This was requested by user, and the backend supports it; or
190989e0f4d2SKip Macy 	 *  (b) Flipping was requested, but this is unsupported by the backend.
191089e0f4d2SKip Macy 	 */
191189e0f4d2SKip Macy 	np->copying_receiver = ((MODPARM_rx_copy && feature_rx_copy) ||
191289e0f4d2SKip Macy 				(MODPARM_rx_flip && !feature_rx_flip));
191389e0f4d2SKip Macy 
191489e0f4d2SKip Macy 	/* Recovery procedure: */
19153a6d1fcfSKip Macy 	error = talk_to_backend(np->xbdev, np);
19163a6d1fcfSKip Macy 	if (error)
19173a6d1fcfSKip Macy 		return (error);
191889e0f4d2SKip Macy 
191989e0f4d2SKip Macy 	/* Step 1: Reinitialise variables. */
1920578e4bf7SJustin T. Gibbs 	xn_query_features(np);
1921578e4bf7SJustin T. Gibbs 	xn_configure_features(np);
192289e0f4d2SKip Macy 	netif_release_tx_bufs(np);
192389e0f4d2SKip Macy 
192489e0f4d2SKip Macy 	/* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
192589e0f4d2SKip Macy 	for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
192689e0f4d2SKip Macy 		struct mbuf *m;
19273a6d1fcfSKip Macy 		u_long pfn;
192889e0f4d2SKip Macy 
192989e0f4d2SKip Macy 		if (np->rx_mbufs[i] == NULL)
193089e0f4d2SKip Macy 			continue;
193189e0f4d2SKip Macy 
193289e0f4d2SKip Macy 		m = np->rx_mbufs[requeue_idx] = xennet_get_rx_mbuf(np, i);
193389e0f4d2SKip Macy 		ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1934931eeffaSKenneth D. Merry 
193589e0f4d2SKip Macy 		req = RING_GET_REQUEST(&np->rx, requeue_idx);
19363a6d1fcfSKip Macy 		pfn = vtophys(mtod(m, vm_offset_t)) >> PAGE_SHIFT;
193789e0f4d2SKip Macy 
193889e0f4d2SKip Macy 		if (!np->copying_receiver) {
193989e0f4d2SKip Macy 			gnttab_grant_foreign_transfer_ref(ref,
194023dc5621SKip Macy 			    xenbus_get_otherend_id(np->xbdev),
19413a6d1fcfSKip Macy 			    pfn);
194289e0f4d2SKip Macy 		} else {
194389e0f4d2SKip Macy 			gnttab_grant_foreign_access_ref(ref,
194423dc5621SKip Macy 			    xenbus_get_otherend_id(np->xbdev),
19453a6d1fcfSKip Macy 			    PFNTOMFN(pfn), 0);
194689e0f4d2SKip Macy 		}
194789e0f4d2SKip Macy 		req->gref = ref;
194889e0f4d2SKip Macy 		req->id   = requeue_idx;
194989e0f4d2SKip Macy 
195089e0f4d2SKip Macy 		requeue_idx++;
195189e0f4d2SKip Macy 	}
195289e0f4d2SKip Macy 
195389e0f4d2SKip Macy 	np->rx.req_prod_pvt = requeue_idx;
195489e0f4d2SKip Macy 
195589e0f4d2SKip Macy 	/* Step 3: All public and private state should now be sane.  Get
195689e0f4d2SKip Macy 	 * ready to start sending and receiving packets and give the driver
195789e0f4d2SKip Macy 	 * domain a kick because we've probably just requeued some
195889e0f4d2SKip Macy 	 * packets.
195989e0f4d2SKip Macy 	 */
196089e0f4d2SKip Macy 	netfront_carrier_on(np);
196176acc41fSJustin T. Gibbs 	xen_intr_signal(np->xen_intr_handle);
196289e0f4d2SKip Macy 	XN_TX_LOCK(np);
196389e0f4d2SKip Macy 	xn_txeof(np);
196489e0f4d2SKip Macy 	XN_TX_UNLOCK(np);
196589e0f4d2SKip Macy 	network_alloc_rx_buffers(np);
196689e0f4d2SKip Macy 
196789e0f4d2SKip Macy 	return (0);
196889e0f4d2SKip Macy }
196989e0f4d2SKip Macy 
197089e0f4d2SKip Macy static void
1971578e4bf7SJustin T. Gibbs xn_query_features(struct netfront_info *np)
1972578e4bf7SJustin T. Gibbs {
1973578e4bf7SJustin T. Gibbs 	int val;
1974578e4bf7SJustin T. Gibbs 
1975578e4bf7SJustin T. Gibbs 	device_printf(np->xbdev, "backend features:");
1976578e4bf7SJustin T. Gibbs 
1977578e4bf7SJustin T. Gibbs 	if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev),
1978578e4bf7SJustin T. Gibbs 		"feature-sg", NULL, "%d", &val) < 0)
1979578e4bf7SJustin T. Gibbs 		val = 0;
1980578e4bf7SJustin T. Gibbs 
1981578e4bf7SJustin T. Gibbs 	np->maxfrags = 1;
1982578e4bf7SJustin T. Gibbs 	if (val) {
1983578e4bf7SJustin T. Gibbs 		np->maxfrags = MAX_TX_REQ_FRAGS;
1984578e4bf7SJustin T. Gibbs 		printf(" feature-sg");
1985578e4bf7SJustin T. Gibbs 	}
1986578e4bf7SJustin T. Gibbs 
1987578e4bf7SJustin T. Gibbs 	if (xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev),
1988578e4bf7SJustin T. Gibbs 		"feature-gso-tcpv4", NULL, "%d", &val) < 0)
1989578e4bf7SJustin T. Gibbs 		val = 0;
1990578e4bf7SJustin T. Gibbs 
1991578e4bf7SJustin T. Gibbs 	np->xn_ifp->if_capabilities &= ~(IFCAP_TSO4|IFCAP_LRO);
1992578e4bf7SJustin T. Gibbs 	if (val) {
1993578e4bf7SJustin T. Gibbs 		np->xn_ifp->if_capabilities |= IFCAP_TSO4|IFCAP_LRO;
1994578e4bf7SJustin T. Gibbs 		printf(" feature-gso-tcp4");
1995578e4bf7SJustin T. Gibbs 	}
1996578e4bf7SJustin T. Gibbs 
1997578e4bf7SJustin T. Gibbs 	printf("\n");
1998578e4bf7SJustin T. Gibbs }
1999578e4bf7SJustin T. Gibbs 
2000cf9c09e1SJustin T. Gibbs static int
2001578e4bf7SJustin T. Gibbs xn_configure_features(struct netfront_info *np)
2002cf9c09e1SJustin T. Gibbs {
2003cf9c09e1SJustin T. Gibbs 	int err;
2004cf9c09e1SJustin T. Gibbs 
2005cf9c09e1SJustin T. Gibbs 	err = 0;
2006*f909bbb4SGleb Smirnoff #if __FreeBSD_version >= 70000 && (defined(INET) || defined(INET6))
2007578e4bf7SJustin T. Gibbs 	if ((np->xn_ifp->if_capenable & IFCAP_LRO) != 0)
2008cf9c09e1SJustin T. Gibbs 		tcp_lro_free(&np->xn_lro);
2009578e4bf7SJustin T. Gibbs #endif
2010578e4bf7SJustin T. Gibbs     	np->xn_ifp->if_capenable =
2011578e4bf7SJustin T. Gibbs 	    np->xn_ifp->if_capabilities & ~(IFCAP_LRO|IFCAP_TSO4);
2012578e4bf7SJustin T. Gibbs 	np->xn_ifp->if_hwassist &= ~CSUM_TSO;
2013*f909bbb4SGleb Smirnoff #if __FreeBSD_version >= 700000 && (defined(INET) || defined(INET6))
2014578e4bf7SJustin T. Gibbs 	if (xn_enable_lro && (np->xn_ifp->if_capabilities & IFCAP_LRO) != 0) {
2015cf9c09e1SJustin T. Gibbs 		err = tcp_lro_init(&np->xn_lro);
2016cf9c09e1SJustin T. Gibbs 		if (err) {
2017cf9c09e1SJustin T. Gibbs 			device_printf(np->xbdev, "LRO initialization failed\n");
2018cf9c09e1SJustin T. Gibbs 		} else {
2019cf9c09e1SJustin T. Gibbs 			np->xn_lro.ifp = np->xn_ifp;
2020578e4bf7SJustin T. Gibbs 			np->xn_ifp->if_capenable |= IFCAP_LRO;
2021cf9c09e1SJustin T. Gibbs 		}
2022cf9c09e1SJustin T. Gibbs 	}
2023578e4bf7SJustin T. Gibbs 	if ((np->xn_ifp->if_capabilities & IFCAP_TSO4) != 0) {
2024578e4bf7SJustin T. Gibbs 		np->xn_ifp->if_capenable |= IFCAP_TSO4;
2025578e4bf7SJustin T. Gibbs 		np->xn_ifp->if_hwassist |= CSUM_TSO;
2026578e4bf7SJustin T. Gibbs 	}
2027cf9c09e1SJustin T. Gibbs #endif
2028cf9c09e1SJustin T. Gibbs 	return (err);
2029cf9c09e1SJustin T. Gibbs }
2030cf9c09e1SJustin T. Gibbs 
203176acc41fSJustin T. Gibbs /**
203276acc41fSJustin T. Gibbs  * Create a network device.
203376acc41fSJustin T. Gibbs  * @param dev  Newbus device representing this virtual NIC.
203489e0f4d2SKip Macy  */
203523dc5621SKip Macy int
203623dc5621SKip Macy create_netdev(device_t dev)
203789e0f4d2SKip Macy {
203889e0f4d2SKip Macy 	int i;
203989e0f4d2SKip Macy 	struct netfront_info *np;
204089e0f4d2SKip Macy 	int err;
204189e0f4d2SKip Macy 	struct ifnet *ifp;
204289e0f4d2SKip Macy 
204323dc5621SKip Macy 	np = device_get_softc(dev);
204489e0f4d2SKip Macy 
204589e0f4d2SKip Macy 	np->xbdev         = dev;
204689e0f4d2SKip Macy 
204789e0f4d2SKip Macy 	XN_LOCK_INIT(np, xennetif);
20480e509842SJustin T. Gibbs 
20490e509842SJustin T. Gibbs 	ifmedia_init(&np->sc_media, 0, xn_ifmedia_upd, xn_ifmedia_sts);
20500e509842SJustin T. Gibbs 	ifmedia_add(&np->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
20510e509842SJustin T. Gibbs 	ifmedia_set(&np->sc_media, IFM_ETHER|IFM_MANUAL);
20520e509842SJustin T. Gibbs 
205389e0f4d2SKip Macy 	np->rx_target     = RX_MIN_TARGET;
205489e0f4d2SKip Macy 	np->rx_min_target = RX_MIN_TARGET;
205589e0f4d2SKip Macy 	np->rx_max_target = RX_MAX_TARGET;
205689e0f4d2SKip Macy 
205789e0f4d2SKip Macy 	/* Initialise {tx,rx}_skbs to be a free chain containing every entry. */
205889e0f4d2SKip Macy 	for (i = 0; i <= NET_TX_RING_SIZE; i++) {
205989e0f4d2SKip Macy 		np->tx_mbufs[i] = (void *) ((u_long) i+1);
2060ff662b5cSJustin T. Gibbs 		np->grant_tx_ref[i] = GRANT_REF_INVALID;
206189e0f4d2SKip Macy 	}
2062931eeffaSKenneth D. Merry 	np->tx_mbufs[NET_TX_RING_SIZE] = (void *)0;
2063931eeffaSKenneth D. Merry 
206489e0f4d2SKip Macy 	for (i = 0; i <= NET_RX_RING_SIZE; i++) {
2065931eeffaSKenneth D. Merry 
206689e0f4d2SKip Macy 		np->rx_mbufs[i] = NULL;
2067ff662b5cSJustin T. Gibbs 		np->grant_rx_ref[i] = GRANT_REF_INVALID;
206889e0f4d2SKip Macy 	}
206989e0f4d2SKip Macy 	/* A grant for every tx ring slot */
2070931eeffaSKenneth D. Merry 	if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
2071931eeffaSKenneth D. Merry 					  &np->gref_tx_head) != 0) {
2072227ca257SKip Macy 		IPRINTK("#### netfront can't alloc tx grant refs\n");
207389e0f4d2SKip Macy 		err = ENOMEM;
207489e0f4d2SKip Macy 		goto exit;
207589e0f4d2SKip Macy 	}
207689e0f4d2SKip Macy 	/* A grant for every rx ring slot */
207789e0f4d2SKip Macy 	if (gnttab_alloc_grant_references(RX_MAX_TARGET,
2078931eeffaSKenneth D. Merry 					  &np->gref_rx_head) != 0) {
2079227ca257SKip Macy 		WPRINTK("#### netfront can't alloc rx grant refs\n");
208089e0f4d2SKip Macy 		gnttab_free_grant_references(np->gref_tx_head);
208189e0f4d2SKip Macy 		err = ENOMEM;
208289e0f4d2SKip Macy 		goto exit;
208389e0f4d2SKip Macy 	}
208489e0f4d2SKip Macy 
208589e0f4d2SKip Macy 	err = xen_net_read_mac(dev, np->mac);
2086ffa06904SJustin T. Gibbs 	if (err)
208789e0f4d2SKip Macy 		goto out;
208889e0f4d2SKip Macy 
208989e0f4d2SKip Macy 	/* Set up ifnet structure */
209023dc5621SKip Macy 	ifp = np->xn_ifp = if_alloc(IFT_ETHER);
209189e0f4d2SKip Macy     	ifp->if_softc = np;
209223dc5621SKip Macy     	if_initname(ifp, "xn",  device_get_unit(dev));
20933a6d1fcfSKip Macy     	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
209489e0f4d2SKip Macy     	ifp->if_ioctl = xn_ioctl;
209589e0f4d2SKip Macy     	ifp->if_output = ether_output;
209689e0f4d2SKip Macy     	ifp->if_start = xn_start;
2097227ca257SKip Macy #ifdef notyet
2098227ca257SKip Macy     	ifp->if_watchdog = xn_watchdog;
2099227ca257SKip Macy #endif
210089e0f4d2SKip Macy     	ifp->if_init = xn_ifinit;
210189e0f4d2SKip Macy     	ifp->if_snd.ifq_maxlen = NET_TX_RING_SIZE - 1;
210289e0f4d2SKip Macy 
210389e0f4d2SKip Macy     	ifp->if_hwassist = XN_CSUM_FEATURES;
210489e0f4d2SKip Macy     	ifp->if_capabilities = IFCAP_HWCSUM;
210533e0730eSAndre Oppermann 	ifp->if_hw_tsomax = NF_TSO_MAXBURST;
210689e0f4d2SKip Macy 
210789e0f4d2SKip Macy     	ether_ifattach(ifp, np->mac);
210889e0f4d2SKip Macy     	callout_init(&np->xn_stat_ch, CALLOUT_MPSAFE);
210989e0f4d2SKip Macy 	netfront_carrier_off(np);
211089e0f4d2SKip Macy 
211189e0f4d2SKip Macy 	return (0);
211289e0f4d2SKip Macy 
211389e0f4d2SKip Macy exit:
211489e0f4d2SKip Macy 	gnttab_free_grant_references(np->gref_tx_head);
211589e0f4d2SKip Macy out:
2116ffa06904SJustin T. Gibbs 	return (err);
211789e0f4d2SKip Macy }
211889e0f4d2SKip Macy 
211989e0f4d2SKip Macy /**
212089e0f4d2SKip Macy  * Handle the change of state of the backend to Closing.  We must delete our
212189e0f4d2SKip Macy  * device-layer structures now, to ensure that writes are flushed through to
212289e0f4d2SKip Macy  * the backend.  Once is this done, we can switch to Closed in
212389e0f4d2SKip Macy  * acknowledgement.
212489e0f4d2SKip Macy  */
212589e0f4d2SKip Macy #if 0
21260e509842SJustin T. Gibbs static void
21270e509842SJustin T. Gibbs netfront_closing(device_t dev)
212889e0f4d2SKip Macy {
212989e0f4d2SKip Macy #if 0
213089e0f4d2SKip Macy 	struct netfront_info *info = dev->dev_driver_data;
213189e0f4d2SKip Macy 
213289e0f4d2SKip Macy 	DPRINTK("netfront_closing: %s removed\n", dev->nodename);
213389e0f4d2SKip Macy 
213489e0f4d2SKip Macy 	close_netdev(info);
213589e0f4d2SKip Macy #endif
213689e0f4d2SKip Macy 	xenbus_switch_state(dev, XenbusStateClosed);
213789e0f4d2SKip Macy }
213889e0f4d2SKip Macy #endif
213989e0f4d2SKip Macy 
21400e509842SJustin T. Gibbs static int
21410e509842SJustin T. Gibbs netfront_detach(device_t dev)
214289e0f4d2SKip Macy {
214323dc5621SKip Macy 	struct netfront_info *info = device_get_softc(dev);
214489e0f4d2SKip Macy 
214523dc5621SKip Macy 	DPRINTK("%s\n", xenbus_get_node(dev));
214689e0f4d2SKip Macy 
214789e0f4d2SKip Macy 	netif_free(info);
214889e0f4d2SKip Macy 
214989e0f4d2SKip Macy 	return 0;
215089e0f4d2SKip Macy }
215189e0f4d2SKip Macy 
21520e509842SJustin T. Gibbs static void
21530e509842SJustin T. Gibbs netif_free(struct netfront_info *info)
215489e0f4d2SKip Macy {
2155818fe953SJustin T. Gibbs 	XN_LOCK(info);
2156818fe953SJustin T. Gibbs 	xn_stop(info);
2157818fe953SJustin T. Gibbs 	XN_UNLOCK(info);
2158818fe953SJustin T. Gibbs 	callout_drain(&info->xn_stat_ch);
215989e0f4d2SKip Macy 	netif_disconnect_backend(info);
2160e3242f9dSJustin T. Gibbs 	if (info->xn_ifp != NULL) {
2161818fe953SJustin T. Gibbs 		ether_ifdetach(info->xn_ifp);
2162818fe953SJustin T. Gibbs 		if_free(info->xn_ifp);
2163e3242f9dSJustin T. Gibbs 		info->xn_ifp = NULL;
2164e3242f9dSJustin T. Gibbs 	}
2165d5aeb779SJustin T. Gibbs 	ifmedia_removeall(&info->sc_media);
216689e0f4d2SKip Macy }
216789e0f4d2SKip Macy 
21680e509842SJustin T. Gibbs static void
21690e509842SJustin T. Gibbs netif_disconnect_backend(struct netfront_info *info)
217089e0f4d2SKip Macy {
21713a6d1fcfSKip Macy 	XN_RX_LOCK(info);
21723a6d1fcfSKip Macy 	XN_TX_LOCK(info);
21733a6d1fcfSKip Macy 	netfront_carrier_off(info);
21743a6d1fcfSKip Macy 	XN_TX_UNLOCK(info);
21753a6d1fcfSKip Macy 	XN_RX_UNLOCK(info);
21763a6d1fcfSKip Macy 
2177cf9c09e1SJustin T. Gibbs 	free_ring(&info->tx_ring_ref, &info->tx.sring);
2178cf9c09e1SJustin T. Gibbs 	free_ring(&info->rx_ring_ref, &info->rx.sring);
217989e0f4d2SKip Macy 
218076acc41fSJustin T. Gibbs 	xen_intr_unbind(&info->xen_intr_handle);
218189e0f4d2SKip Macy }
218289e0f4d2SKip Macy 
21830e509842SJustin T. Gibbs static void
2184cf9c09e1SJustin T. Gibbs free_ring(int *ref, void *ring_ptr_ref)
218589e0f4d2SKip Macy {
2186cf9c09e1SJustin T. Gibbs 	void **ring_ptr_ptr = ring_ptr_ref;
2187cf9c09e1SJustin T. Gibbs 
2188cf9c09e1SJustin T. Gibbs 	if (*ref != GRANT_REF_INVALID) {
2189cf9c09e1SJustin T. Gibbs 		/* This API frees the associated storage. */
2190cf9c09e1SJustin T. Gibbs 		gnttab_end_foreign_access(*ref, *ring_ptr_ptr);
2191cf9c09e1SJustin T. Gibbs 		*ref = GRANT_REF_INVALID;
2192cf9c09e1SJustin T. Gibbs 	}
2193cf9c09e1SJustin T. Gibbs 	*ring_ptr_ptr = NULL;
219489e0f4d2SKip Macy }
219589e0f4d2SKip Macy 
21960e509842SJustin T. Gibbs static int
21970e509842SJustin T. Gibbs xn_ifmedia_upd(struct ifnet *ifp)
21980e509842SJustin T. Gibbs {
21990e509842SJustin T. Gibbs 	return (0);
22000e509842SJustin T. Gibbs }
22010e509842SJustin T. Gibbs 
22020e509842SJustin T. Gibbs static void
22030e509842SJustin T. Gibbs xn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
22040e509842SJustin T. Gibbs {
22050e509842SJustin T. Gibbs 	ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE;
22060e509842SJustin T. Gibbs 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
22070e509842SJustin T. Gibbs }
22080e509842SJustin T. Gibbs 
220989e0f4d2SKip Macy /* ** Driver registration ** */
221023dc5621SKip Macy static device_method_t netfront_methods[] = {
221123dc5621SKip Macy 	/* Device interface */
221223dc5621SKip Macy 	DEVMETHOD(device_probe,         netfront_probe),
221323dc5621SKip Macy 	DEVMETHOD(device_attach,        netfront_attach),
221423dc5621SKip Macy 	DEVMETHOD(device_detach,        netfront_detach),
221523dc5621SKip Macy 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
2216cf9c09e1SJustin T. Gibbs 	DEVMETHOD(device_suspend,       netfront_suspend),
221723dc5621SKip Macy 	DEVMETHOD(device_resume,        netfront_resume),
221889e0f4d2SKip Macy 
221923dc5621SKip Macy 	/* Xenbus interface */
2220ff662b5cSJustin T. Gibbs 	DEVMETHOD(xenbus_otherend_changed, netfront_backend_changed),
222189e0f4d2SKip Macy 
22226f9767acSMarius Strobl 	DEVMETHOD_END
222389e0f4d2SKip Macy };
222489e0f4d2SKip Macy 
222523dc5621SKip Macy static driver_t netfront_driver = {
222623dc5621SKip Macy 	"xn",
222723dc5621SKip Macy 	netfront_methods,
222823dc5621SKip Macy 	sizeof(struct netfront_info),
222989e0f4d2SKip Macy };
223023dc5621SKip Macy devclass_t netfront_devclass;
223189e0f4d2SKip Macy 
22326f9767acSMarius Strobl DRIVER_MODULE(xe, xenbusb_front, netfront_driver, netfront_devclass, NULL,
22336f9767acSMarius Strobl     NULL);
2234