xref: /freebsd/sys/dev/xen/netfront/netfront.c (revision ff662b5c9814043f1fb93ab7b16eaa0d77515959)
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 
2889e0f4d2SKip Macy #include <sys/cdefs.h>
2989e0f4d2SKip Macy __FBSDID("$FreeBSD$");
3089e0f4d2SKip Macy 
3189e0f4d2SKip Macy #include <sys/param.h>
3289e0f4d2SKip Macy #include <sys/systm.h>
3389e0f4d2SKip Macy #include <sys/sockio.h>
3489e0f4d2SKip Macy #include <sys/mbuf.h>
3589e0f4d2SKip Macy #include <sys/malloc.h>
3623dc5621SKip Macy #include <sys/module.h>
3789e0f4d2SKip Macy #include <sys/kernel.h>
3889e0f4d2SKip Macy #include <sys/socket.h>
3912678024SDoug Rabson #include <sys/sysctl.h>
4089e0f4d2SKip Macy #include <sys/queue.h>
418cb07992SAdrian Chadd #include <sys/lock.h>
4289e0f4d2SKip Macy #include <sys/sx.h>
4389e0f4d2SKip Macy 
4489e0f4d2SKip Macy #include <net/if.h>
4589e0f4d2SKip Macy #include <net/if_arp.h>
4689e0f4d2SKip Macy #include <net/ethernet.h>
4789e0f4d2SKip Macy #include <net/if_dl.h>
4889e0f4d2SKip Macy #include <net/if_media.h>
4989e0f4d2SKip Macy 
5089e0f4d2SKip Macy #include <net/bpf.h>
5189e0f4d2SKip Macy 
5289e0f4d2SKip Macy #include <net/if_types.h>
5389e0f4d2SKip Macy #include <net/if.h>
5489e0f4d2SKip Macy 
5589e0f4d2SKip Macy #include <netinet/in_systm.h>
5689e0f4d2SKip Macy #include <netinet/in.h>
5789e0f4d2SKip Macy #include <netinet/ip.h>
5889e0f4d2SKip Macy #include <netinet/if_ether.h>
5912678024SDoug Rabson #if __FreeBSD_version >= 700000
6012678024SDoug Rabson #include <netinet/tcp.h>
6112678024SDoug Rabson #include <netinet/tcp_lro.h>
6212678024SDoug Rabson #endif
6389e0f4d2SKip Macy 
6489e0f4d2SKip Macy #include <vm/vm.h>
6589e0f4d2SKip Macy #include <vm/pmap.h>
6689e0f4d2SKip Macy 
6789e0f4d2SKip Macy #include <machine/clock.h>      /* for DELAY */
6889e0f4d2SKip Macy #include <machine/bus.h>
6989e0f4d2SKip Macy #include <machine/resource.h>
7089e0f4d2SKip Macy #include <machine/frame.h>
71980c7178SKip Macy #include <machine/vmparam.h>
7289e0f4d2SKip Macy 
7389e0f4d2SKip Macy #include <sys/bus.h>
7489e0f4d2SKip Macy #include <sys/rman.h>
7589e0f4d2SKip Macy 
7689e0f4d2SKip Macy #include <machine/intr_machdep.h>
7789e0f4d2SKip Macy 
7889e0f4d2SKip Macy #include <machine/xen/xen-os.h>
7912678024SDoug Rabson #include <machine/xen/xenfunc.h>
803a6d1fcfSKip Macy #include <xen/hypervisor.h>
813a6d1fcfSKip Macy #include <xen/xen_intr.h>
823a6d1fcfSKip Macy #include <xen/evtchn.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 
8812678024SDoug Rabson #include <dev/xen/netfront/mbufq.h>
8912678024SDoug Rabson 
9023dc5621SKip Macy #include "xenbus_if.h"
9189e0f4d2SKip Macy 
92931eeffaSKenneth D. Merry #define XN_CSUM_FEATURES	(CSUM_TCP | CSUM_UDP | CSUM_TSO)
9312678024SDoug Rabson 
9489e0f4d2SKip Macy #define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE)
9589e0f4d2SKip Macy #define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE)
9689e0f4d2SKip Macy 
9712678024SDoug Rabson #if __FreeBSD_version >= 700000
9812678024SDoug Rabson /*
9912678024SDoug Rabson  * Should the driver do LRO on the RX end
10012678024SDoug Rabson  *  this can be toggled on the fly, but the
10112678024SDoug Rabson  *  interface must be reset (down/up) for it
10212678024SDoug Rabson  *  to take effect.
10312678024SDoug Rabson  */
10412678024SDoug Rabson static int xn_enable_lro = 1;
10512678024SDoug Rabson TUNABLE_INT("hw.xn.enable_lro", &xn_enable_lro);
10612678024SDoug Rabson #else
10712678024SDoug Rabson 
10812678024SDoug Rabson #define IFCAP_TSO4	0
10912678024SDoug Rabson #define CSUM_TSO	0
11012678024SDoug Rabson 
11112678024SDoug Rabson #endif
11212678024SDoug Rabson 
11389e0f4d2SKip Macy #ifdef CONFIG_XEN
11489e0f4d2SKip Macy static int MODPARM_rx_copy = 0;
11589e0f4d2SKip Macy module_param_named(rx_copy, MODPARM_rx_copy, bool, 0);
11689e0f4d2SKip Macy MODULE_PARM_DESC(rx_copy, "Copy packets from network card (rather than flip)");
11789e0f4d2SKip Macy static int MODPARM_rx_flip = 0;
11889e0f4d2SKip Macy module_param_named(rx_flip, MODPARM_rx_flip, bool, 0);
11989e0f4d2SKip Macy MODULE_PARM_DESC(rx_flip, "Flip packets from network card (rather than copy)");
12089e0f4d2SKip Macy #else
12189e0f4d2SKip Macy static const int MODPARM_rx_copy = 1;
12289e0f4d2SKip Macy static const int MODPARM_rx_flip = 0;
12389e0f4d2SKip Macy #endif
12489e0f4d2SKip Macy 
125931eeffaSKenneth D. Merry /**
126931eeffaSKenneth D. Merry  * \brief The maximum allowed data fragments in a single transmit
127931eeffaSKenneth D. Merry  *        request.
128931eeffaSKenneth D. Merry  *
129931eeffaSKenneth D. Merry  * This limit is imposed by the backend driver.  We assume here that
130931eeffaSKenneth D. Merry  * we are dealing with a Linux driver domain and have set our limit
131931eeffaSKenneth D. Merry  * to mirror the Linux MAX_SKB_FRAGS constant.
132931eeffaSKenneth D. Merry  */
133931eeffaSKenneth D. Merry #define	MAX_TX_REQ_FRAGS (65536 / PAGE_SIZE + 2)
134931eeffaSKenneth D. Merry 
13589e0f4d2SKip Macy #define RX_COPY_THRESHOLD 256
13689e0f4d2SKip Macy 
13789e0f4d2SKip Macy #define net_ratelimit() 0
13889e0f4d2SKip Macy 
13989e0f4d2SKip Macy struct netfront_info;
14089e0f4d2SKip Macy struct netfront_rx_info;
14189e0f4d2SKip Macy 
14289e0f4d2SKip Macy static void xn_txeof(struct netfront_info *);
14389e0f4d2SKip Macy static void xn_rxeof(struct netfront_info *);
14489e0f4d2SKip Macy static void network_alloc_rx_buffers(struct netfront_info *);
14589e0f4d2SKip Macy 
14689e0f4d2SKip Macy static void xn_tick_locked(struct netfront_info *);
14789e0f4d2SKip Macy static void xn_tick(void *);
14889e0f4d2SKip Macy 
14989e0f4d2SKip Macy static void xn_intr(void *);
150931eeffaSKenneth D. Merry static inline int xn_count_frags(struct mbuf *m);
151931eeffaSKenneth D. Merry static int  xn_assemble_tx_request(struct netfront_info *sc,
152931eeffaSKenneth D. Merry 				   struct mbuf *m_head);
15389e0f4d2SKip Macy static void xn_start_locked(struct ifnet *);
15489e0f4d2SKip Macy static void xn_start(struct ifnet *);
15589e0f4d2SKip Macy static int  xn_ioctl(struct ifnet *, u_long, caddr_t);
15689e0f4d2SKip Macy static void xn_ifinit_locked(struct netfront_info *);
15789e0f4d2SKip Macy static void xn_ifinit(void *);
15889e0f4d2SKip Macy static void xn_stop(struct netfront_info *);
15989e0f4d2SKip Macy #ifdef notyet
16089e0f4d2SKip Macy static void xn_watchdog(struct ifnet *);
16189e0f4d2SKip Macy #endif
16289e0f4d2SKip Macy 
16389e0f4d2SKip Macy static void show_device(struct netfront_info *sc);
16489e0f4d2SKip Macy #ifdef notyet
16523dc5621SKip Macy static void netfront_closing(device_t dev);
16689e0f4d2SKip Macy #endif
16789e0f4d2SKip Macy static void netif_free(struct netfront_info *info);
16823dc5621SKip Macy static int netfront_detach(device_t dev);
16989e0f4d2SKip Macy 
17023dc5621SKip Macy static int talk_to_backend(device_t dev, struct netfront_info *info);
17123dc5621SKip Macy static int create_netdev(device_t dev);
17289e0f4d2SKip Macy static void netif_disconnect_backend(struct netfront_info *info);
17323dc5621SKip Macy static int setup_device(device_t dev, struct netfront_info *info);
17489e0f4d2SKip Macy static void end_access(int ref, void *page);
17589e0f4d2SKip Macy 
1760e509842SJustin T. Gibbs static int  xn_ifmedia_upd(struct ifnet *ifp);
1770e509842SJustin T. Gibbs static void xn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
1780e509842SJustin T. Gibbs 
17989e0f4d2SKip Macy /* Xenolinux helper functions */
18023dc5621SKip Macy int network_connect(struct netfront_info *);
18189e0f4d2SKip Macy 
18289e0f4d2SKip Macy static void xn_free_rx_ring(struct netfront_info *);
18389e0f4d2SKip Macy 
18489e0f4d2SKip Macy static void xn_free_tx_ring(struct netfront_info *);
18589e0f4d2SKip Macy 
18689e0f4d2SKip Macy static int xennet_get_responses(struct netfront_info *np,
187931eeffaSKenneth D. Merry 	struct netfront_rx_info *rinfo, RING_IDX rp, RING_IDX *cons,
188931eeffaSKenneth D. Merry 	struct mbuf **list, int *pages_flipped_p);
18989e0f4d2SKip Macy 
19089e0f4d2SKip Macy #define virt_to_mfn(x) (vtomach(x) >> PAGE_SHIFT)
19189e0f4d2SKip Macy 
19289e0f4d2SKip Macy #define INVALID_P2M_ENTRY (~0UL)
19389e0f4d2SKip Macy 
19489e0f4d2SKip Macy /*
19589e0f4d2SKip Macy  * Mbuf pointers. We need these to keep track of the virtual addresses
19689e0f4d2SKip Macy  * of our mbuf chains since we can only convert from virtual to physical,
19789e0f4d2SKip Macy  * not the other way around.  The size must track the free index arrays.
19889e0f4d2SKip Macy  */
19989e0f4d2SKip Macy struct xn_chain_data {
20089e0f4d2SKip Macy 	struct mbuf    *xn_tx_chain[NET_TX_RING_SIZE+1];
201a4ec37f5SAdrian Chadd 	int		xn_tx_chain_cnt;
20289e0f4d2SKip Macy 	struct mbuf    *xn_rx_chain[NET_RX_RING_SIZE+1];
20389e0f4d2SKip Macy };
20489e0f4d2SKip Macy 
205931eeffaSKenneth D. Merry #define NUM_ELEMENTS(x) (sizeof(x)/sizeof(*x))
20689e0f4d2SKip Macy 
20789e0f4d2SKip Macy struct net_device_stats
20889e0f4d2SKip Macy {
20989e0f4d2SKip Macy 	u_long	rx_packets;		/* total packets received	*/
21089e0f4d2SKip Macy 	u_long	tx_packets;		/* total packets transmitted	*/
21189e0f4d2SKip Macy 	u_long	rx_bytes;		/* total bytes received 	*/
21289e0f4d2SKip Macy 	u_long	tx_bytes;		/* total bytes transmitted	*/
21389e0f4d2SKip Macy 	u_long	rx_errors;		/* bad packets received		*/
21489e0f4d2SKip Macy 	u_long	tx_errors;		/* packet transmit problems	*/
21589e0f4d2SKip Macy 	u_long	rx_dropped;		/* no space in linux buffers	*/
21689e0f4d2SKip Macy 	u_long	tx_dropped;		/* no space available in linux	*/
21789e0f4d2SKip Macy 	u_long	multicast;		/* multicast packets received	*/
21889e0f4d2SKip Macy 	u_long	collisions;
21989e0f4d2SKip Macy 
22089e0f4d2SKip Macy 	/* detailed rx_errors: */
22189e0f4d2SKip Macy 	u_long	rx_length_errors;
22289e0f4d2SKip Macy 	u_long	rx_over_errors;		/* receiver ring buff overflow	*/
22389e0f4d2SKip Macy 	u_long	rx_crc_errors;		/* recved pkt with crc error	*/
22489e0f4d2SKip Macy 	u_long	rx_frame_errors;	/* recv'd frame alignment error */
22589e0f4d2SKip Macy 	u_long	rx_fifo_errors;		/* recv'r fifo overrun		*/
22689e0f4d2SKip Macy 	u_long	rx_missed_errors;	/* receiver missed packet	*/
22789e0f4d2SKip Macy 
22889e0f4d2SKip Macy 	/* detailed tx_errors */
22989e0f4d2SKip Macy 	u_long	tx_aborted_errors;
23089e0f4d2SKip Macy 	u_long	tx_carrier_errors;
23189e0f4d2SKip Macy 	u_long	tx_fifo_errors;
23289e0f4d2SKip Macy 	u_long	tx_heartbeat_errors;
23389e0f4d2SKip Macy 	u_long	tx_window_errors;
23489e0f4d2SKip Macy 
23589e0f4d2SKip Macy 	/* for cslip etc */
23689e0f4d2SKip Macy 	u_long	rx_compressed;
23789e0f4d2SKip Macy 	u_long	tx_compressed;
23889e0f4d2SKip Macy };
23989e0f4d2SKip Macy 
24089e0f4d2SKip Macy struct netfront_info {
24189e0f4d2SKip Macy 
24289e0f4d2SKip Macy 	struct ifnet *xn_ifp;
24312678024SDoug Rabson #if __FreeBSD_version >= 700000
24412678024SDoug Rabson 	struct lro_ctrl xn_lro;
24512678024SDoug Rabson #endif
24689e0f4d2SKip Macy 
24789e0f4d2SKip Macy 	struct net_device_stats stats;
24889e0f4d2SKip Macy 	u_int tx_full;
24989e0f4d2SKip Macy 
25089e0f4d2SKip Macy 	netif_tx_front_ring_t tx;
25189e0f4d2SKip Macy 	netif_rx_front_ring_t rx;
25289e0f4d2SKip Macy 
25389e0f4d2SKip Macy 	struct mtx   tx_lock;
25489e0f4d2SKip Macy 	struct mtx   rx_lock;
255227ca257SKip Macy 	struct mtx   sc_lock;
25689e0f4d2SKip Macy 
25789e0f4d2SKip Macy 	u_int handle;
25889e0f4d2SKip Macy 	u_int irq;
25989e0f4d2SKip Macy 	u_int copying_receiver;
26089e0f4d2SKip Macy 	u_int carrier;
26189e0f4d2SKip Macy 
26289e0f4d2SKip Macy 	/* Receive-ring batched refills. */
26389e0f4d2SKip Macy #define RX_MIN_TARGET 32
26489e0f4d2SKip Macy #define RX_MAX_TARGET NET_RX_RING_SIZE
2650e509842SJustin T. Gibbs 	int rx_min_target;
2660e509842SJustin T. Gibbs 	int rx_max_target;
2670e509842SJustin T. Gibbs 	int rx_target;
26889e0f4d2SKip Macy 
26989e0f4d2SKip Macy 	grant_ref_t gref_tx_head;
27089e0f4d2SKip Macy 	grant_ref_t grant_tx_ref[NET_TX_RING_SIZE + 1];
27189e0f4d2SKip Macy 	grant_ref_t gref_rx_head;
27289e0f4d2SKip Macy 	grant_ref_t grant_rx_ref[NET_TX_RING_SIZE + 1];
27389e0f4d2SKip Macy 
27423dc5621SKip Macy 	device_t		xbdev;
27589e0f4d2SKip Macy 	int			tx_ring_ref;
27689e0f4d2SKip Macy 	int			rx_ring_ref;
27789e0f4d2SKip Macy 	uint8_t			mac[ETHER_ADDR_LEN];
27889e0f4d2SKip Macy 	struct xn_chain_data	xn_cdata;	/* mbufs */
27989e0f4d2SKip Macy 	struct mbuf_head	xn_rx_batch;	/* head of the batch queue */
28089e0f4d2SKip Macy 
28189e0f4d2SKip Macy 	int			xn_if_flags;
28289e0f4d2SKip Macy 	struct callout	        xn_stat_ch;
28389e0f4d2SKip Macy 
28489e0f4d2SKip Macy 	u_long			rx_pfn_array[NET_RX_RING_SIZE];
28589e0f4d2SKip Macy 	multicall_entry_t	rx_mcl[NET_RX_RING_SIZE+1];
28689e0f4d2SKip Macy 	mmu_update_t		rx_mmu[NET_RX_RING_SIZE];
2870e509842SJustin T. Gibbs 	struct ifmedia		sc_media;
28889e0f4d2SKip Macy };
28989e0f4d2SKip Macy 
29089e0f4d2SKip Macy #define rx_mbufs xn_cdata.xn_rx_chain
29189e0f4d2SKip Macy #define tx_mbufs xn_cdata.xn_tx_chain
29289e0f4d2SKip Macy 
29389e0f4d2SKip Macy #define XN_LOCK_INIT(_sc, _name) \
29489e0f4d2SKip Macy         mtx_init(&(_sc)->tx_lock, #_name"_tx", "network transmit lock", MTX_DEF); \
29589e0f4d2SKip Macy         mtx_init(&(_sc)->rx_lock, #_name"_rx", "network receive lock", MTX_DEF);  \
296227ca257SKip Macy         mtx_init(&(_sc)->sc_lock, #_name"_sc", "netfront softc lock", MTX_DEF)
29789e0f4d2SKip Macy 
29889e0f4d2SKip Macy #define XN_RX_LOCK(_sc)           mtx_lock(&(_sc)->rx_lock)
29989e0f4d2SKip Macy #define XN_RX_UNLOCK(_sc)         mtx_unlock(&(_sc)->rx_lock)
30089e0f4d2SKip Macy 
30189e0f4d2SKip Macy #define XN_TX_LOCK(_sc)           mtx_lock(&(_sc)->tx_lock)
30289e0f4d2SKip Macy #define XN_TX_UNLOCK(_sc)         mtx_unlock(&(_sc)->tx_lock)
30389e0f4d2SKip Macy 
304227ca257SKip Macy #define XN_LOCK(_sc)           mtx_lock(&(_sc)->sc_lock);
305227ca257SKip Macy #define XN_UNLOCK(_sc)         mtx_unlock(&(_sc)->sc_lock);
30689e0f4d2SKip Macy 
307227ca257SKip Macy #define XN_LOCK_ASSERT(_sc)    mtx_assert(&(_sc)->sc_lock, MA_OWNED);
30889e0f4d2SKip Macy #define XN_RX_LOCK_ASSERT(_sc)    mtx_assert(&(_sc)->rx_lock, MA_OWNED);
30989e0f4d2SKip Macy #define XN_TX_LOCK_ASSERT(_sc)    mtx_assert(&(_sc)->tx_lock, MA_OWNED);
31089e0f4d2SKip Macy #define XN_LOCK_DESTROY(_sc)   mtx_destroy(&(_sc)->rx_lock); \
31189e0f4d2SKip Macy                                mtx_destroy(&(_sc)->tx_lock); \
312227ca257SKip Macy                                mtx_destroy(&(_sc)->sc_lock);
31389e0f4d2SKip Macy 
31489e0f4d2SKip Macy struct netfront_rx_info {
31589e0f4d2SKip Macy 	struct netif_rx_response rx;
31689e0f4d2SKip Macy 	struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
31789e0f4d2SKip Macy };
31889e0f4d2SKip Macy 
31989e0f4d2SKip Macy #define netfront_carrier_on(netif)	((netif)->carrier = 1)
32089e0f4d2SKip Macy #define netfront_carrier_off(netif)	((netif)->carrier = 0)
32189e0f4d2SKip Macy #define netfront_carrier_ok(netif)	((netif)->carrier)
32289e0f4d2SKip Macy 
32389e0f4d2SKip Macy /* Access macros for acquiring freeing slots in xn_free_{tx,rx}_idxs[]. */
32489e0f4d2SKip Macy 
32589e0f4d2SKip Macy 
32689e0f4d2SKip Macy 
32789e0f4d2SKip Macy /*
32889e0f4d2SKip Macy  * Access macros for acquiring freeing slots in tx_skbs[].
32989e0f4d2SKip Macy  */
33089e0f4d2SKip Macy 
33189e0f4d2SKip Macy static inline void
332931eeffaSKenneth D. Merry add_id_to_freelist(struct mbuf **list, uintptr_t id)
33389e0f4d2SKip Macy {
334931eeffaSKenneth D. Merry 	KASSERT(id != 0,
335931eeffaSKenneth D. Merry 		("%s: the head item (0) must always be free.", __func__));
33689e0f4d2SKip Macy 	list[id] = list[0];
337931eeffaSKenneth D. Merry 	list[0]  = (struct mbuf *)id;
33889e0f4d2SKip Macy }
33989e0f4d2SKip Macy 
34089e0f4d2SKip Macy static inline unsigned short
34189e0f4d2SKip Macy get_id_from_freelist(struct mbuf **list)
34289e0f4d2SKip Macy {
343931eeffaSKenneth D. Merry 	uintptr_t id;
344931eeffaSKenneth D. Merry 
345931eeffaSKenneth D. Merry 	id = (uintptr_t)list[0];
346931eeffaSKenneth D. Merry 	KASSERT(id != 0,
347931eeffaSKenneth D. Merry 		("%s: the head item (0) must always remain free.", __func__));
34889e0f4d2SKip Macy 	list[0] = list[id];
34989e0f4d2SKip Macy 	return (id);
35089e0f4d2SKip Macy }
35189e0f4d2SKip Macy 
35289e0f4d2SKip Macy static inline int
35389e0f4d2SKip Macy xennet_rxidx(RING_IDX idx)
35489e0f4d2SKip Macy {
35589e0f4d2SKip Macy 	return idx & (NET_RX_RING_SIZE - 1);
35689e0f4d2SKip Macy }
35789e0f4d2SKip Macy 
35889e0f4d2SKip Macy static inline struct mbuf *
359931eeffaSKenneth D. Merry xennet_get_rx_mbuf(struct netfront_info *np, RING_IDX ri)
36089e0f4d2SKip Macy {
36189e0f4d2SKip Macy 	int i = xennet_rxidx(ri);
36289e0f4d2SKip Macy 	struct mbuf *m;
36389e0f4d2SKip Macy 
36489e0f4d2SKip Macy 	m = np->rx_mbufs[i];
36589e0f4d2SKip Macy 	np->rx_mbufs[i] = NULL;
36689e0f4d2SKip Macy 	return (m);
36789e0f4d2SKip Macy }
36889e0f4d2SKip Macy 
36989e0f4d2SKip Macy static inline grant_ref_t
37089e0f4d2SKip Macy xennet_get_rx_ref(struct netfront_info *np, RING_IDX ri)
37189e0f4d2SKip Macy {
37289e0f4d2SKip Macy 	int i = xennet_rxidx(ri);
37389e0f4d2SKip Macy 	grant_ref_t ref = np->grant_rx_ref[i];
374*ff662b5cSJustin T. Gibbs 	KASSERT(ref != GRANT_REF_INVALID, ("Invalid grant reference!\n"));
375*ff662b5cSJustin T. Gibbs 	np->grant_rx_ref[i] = GRANT_REF_INVALID;
37689e0f4d2SKip Macy 	return ref;
37789e0f4d2SKip Macy }
37889e0f4d2SKip Macy 
37989e0f4d2SKip Macy #define IPRINTK(fmt, args...) \
38089e0f4d2SKip Macy     printf("[XEN] " fmt, ##args)
381227ca257SKip Macy #ifdef INVARIANTS
38289e0f4d2SKip Macy #define WPRINTK(fmt, args...) \
38389e0f4d2SKip Macy     printf("[XEN] " fmt, ##args)
384227ca257SKip Macy #else
385227ca257SKip Macy #define WPRINTK(fmt, args...)
386227ca257SKip Macy #endif
387227ca257SKip Macy #ifdef DEBUG
38889e0f4d2SKip Macy #define DPRINTK(fmt, args...) \
38923dc5621SKip Macy     printf("[XEN] %s: " fmt, __func__, ##args)
39012678024SDoug Rabson #else
39112678024SDoug Rabson #define DPRINTK(fmt, args...)
39212678024SDoug Rabson #endif
39389e0f4d2SKip Macy 
39489e0f4d2SKip Macy /**
39589e0f4d2SKip Macy  * Read the 'mac' node at the given device's node in the store, and parse that
39689e0f4d2SKip Macy  * as colon-separated octets, placing result the given mac array.  mac must be
39789e0f4d2SKip Macy  * a preallocated array of length ETH_ALEN (as declared in linux/if_ether.h).
39889e0f4d2SKip Macy  * Return 0 on success, or errno on error.
39989e0f4d2SKip Macy  */
40089e0f4d2SKip Macy static int
40123dc5621SKip Macy xen_net_read_mac(device_t dev, uint8_t mac[])
40289e0f4d2SKip Macy {
4033a6d1fcfSKip Macy 	int error, i;
4043a6d1fcfSKip Macy 	char *s, *e, *macstr;
4053a6d1fcfSKip Macy 
406*ff662b5cSJustin T. Gibbs 	error = xs_read(XST_NIL, xenbus_get_node(dev), "mac", NULL,
4073a6d1fcfSKip Macy 	    (void **) &macstr);
4083a6d1fcfSKip Macy 	if (error)
4093a6d1fcfSKip Macy 		return (error);
4103a6d1fcfSKip Macy 
41189e0f4d2SKip Macy 	s = macstr;
41289e0f4d2SKip Macy 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
41389e0f4d2SKip Macy 		mac[i] = strtoul(s, &e, 16);
41489e0f4d2SKip Macy 		if (s == e || (e[0] != ':' && e[0] != 0)) {
415*ff662b5cSJustin T. Gibbs 			free(macstr, M_XENBUS);
4163a6d1fcfSKip Macy 			return (ENOENT);
41789e0f4d2SKip Macy 		}
41889e0f4d2SKip Macy 		s = &e[1];
41989e0f4d2SKip Macy 	}
420*ff662b5cSJustin T. Gibbs 	free(macstr, M_XENBUS);
4213a6d1fcfSKip Macy 	return (0);
42289e0f4d2SKip Macy }
42389e0f4d2SKip Macy 
42489e0f4d2SKip Macy /**
42589e0f4d2SKip Macy  * Entry point to this code when a new device is created.  Allocate the basic
42689e0f4d2SKip Macy  * structures and the ring buffers for communication with the backend, and
42789e0f4d2SKip Macy  * inform the backend of the appropriate details for those.  Switch to
42889e0f4d2SKip Macy  * Connected state.
42989e0f4d2SKip Macy  */
43089e0f4d2SKip Macy static int
43123dc5621SKip Macy netfront_probe(device_t dev)
43223dc5621SKip Macy {
43323dc5621SKip Macy 
43423dc5621SKip Macy 	if (!strcmp(xenbus_get_type(dev), "vif")) {
43523dc5621SKip Macy 		device_set_desc(dev, "Virtual Network Interface");
43623dc5621SKip Macy 		return (0);
43723dc5621SKip Macy 	}
43823dc5621SKip Macy 
43923dc5621SKip Macy 	return (ENXIO);
44023dc5621SKip Macy }
44123dc5621SKip Macy 
44223dc5621SKip Macy static int
44323dc5621SKip Macy netfront_attach(device_t dev)
44489e0f4d2SKip Macy {
44589e0f4d2SKip Macy 	int err;
44689e0f4d2SKip Macy 
44723dc5621SKip Macy 	err = create_netdev(dev);
44889e0f4d2SKip Macy 	if (err) {
44989e0f4d2SKip Macy 		xenbus_dev_fatal(dev, err, "creating netdev");
45089e0f4d2SKip Macy 		return err;
45189e0f4d2SKip Macy 	}
45289e0f4d2SKip Macy 
45312678024SDoug Rabson #if __FreeBSD_version >= 700000
45412678024SDoug Rabson 	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
45512678024SDoug Rabson 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
45612678024SDoug Rabson 	    OID_AUTO, "enable_lro", CTLTYPE_INT|CTLFLAG_RW,
45712678024SDoug Rabson 	    &xn_enable_lro, 0, "Large Receive Offload");
45812678024SDoug Rabson #endif
45912678024SDoug Rabson 
46089e0f4d2SKip Macy 	return 0;
46189e0f4d2SKip Macy }
46289e0f4d2SKip Macy 
46389e0f4d2SKip Macy 
46489e0f4d2SKip Macy /**
46589e0f4d2SKip Macy  * We are reconnecting to the backend, due to a suspend/resume, or a backend
46689e0f4d2SKip Macy  * driver restart.  We tear down our netif structure and recreate it, but
46789e0f4d2SKip Macy  * leave the device-layer structures intact so that this is transparent to the
46889e0f4d2SKip Macy  * rest of the kernel.
46989e0f4d2SKip Macy  */
47089e0f4d2SKip Macy static int
47123dc5621SKip Macy netfront_resume(device_t dev)
47289e0f4d2SKip Macy {
47323dc5621SKip Macy 	struct netfront_info *info = device_get_softc(dev);
47489e0f4d2SKip Macy 
47589e0f4d2SKip Macy 	netif_disconnect_backend(info);
47689e0f4d2SKip Macy 	return (0);
47789e0f4d2SKip Macy }
47889e0f4d2SKip Macy 
47989e0f4d2SKip Macy 
48089e0f4d2SKip Macy /* Common code used when first setting up, and when resuming. */
48189e0f4d2SKip Macy static int
48223dc5621SKip Macy talk_to_backend(device_t dev, struct netfront_info *info)
48389e0f4d2SKip Macy {
48489e0f4d2SKip Macy 	const char *message;
485*ff662b5cSJustin T. Gibbs 	struct xs_transaction xst;
48623dc5621SKip Macy 	const char *node = xenbus_get_node(dev);
48789e0f4d2SKip Macy 	int err;
48889e0f4d2SKip Macy 
48989e0f4d2SKip Macy 	err = xen_net_read_mac(dev, info->mac);
49089e0f4d2SKip Macy 	if (err) {
49123dc5621SKip Macy 		xenbus_dev_fatal(dev, err, "parsing %s/mac", node);
49289e0f4d2SKip Macy 		goto out;
49389e0f4d2SKip Macy 	}
49489e0f4d2SKip Macy 
49589e0f4d2SKip Macy 	/* Create shared ring, alloc event channel. */
49689e0f4d2SKip Macy 	err = setup_device(dev, info);
49789e0f4d2SKip Macy 	if (err)
49889e0f4d2SKip Macy 		goto out;
49989e0f4d2SKip Macy 
50089e0f4d2SKip Macy  again:
501*ff662b5cSJustin T. Gibbs 	err = xs_transaction_start(&xst);
50289e0f4d2SKip Macy 	if (err) {
50389e0f4d2SKip Macy 		xenbus_dev_fatal(dev, err, "starting transaction");
50489e0f4d2SKip Macy 		goto destroy_ring;
50589e0f4d2SKip Macy 	}
506*ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "tx-ring-ref","%u",
50789e0f4d2SKip Macy 			info->tx_ring_ref);
50889e0f4d2SKip Macy 	if (err) {
50989e0f4d2SKip Macy 		message = "writing tx ring-ref";
51089e0f4d2SKip Macy 		goto abort_transaction;
51189e0f4d2SKip Macy 	}
512*ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "rx-ring-ref","%u",
51389e0f4d2SKip Macy 			info->rx_ring_ref);
51489e0f4d2SKip Macy 	if (err) {
51589e0f4d2SKip Macy 		message = "writing rx ring-ref";
51689e0f4d2SKip Macy 		goto abort_transaction;
51789e0f4d2SKip Macy 	}
518*ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node,
51989e0f4d2SKip Macy 			"event-channel", "%u", irq_to_evtchn_port(info->irq));
52089e0f4d2SKip Macy 	if (err) {
52189e0f4d2SKip Macy 		message = "writing event-channel";
52289e0f4d2SKip Macy 		goto abort_transaction;
52389e0f4d2SKip Macy 	}
524*ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "request-rx-copy", "%u",
52589e0f4d2SKip Macy 			info->copying_receiver);
52689e0f4d2SKip Macy 	if (err) {
52789e0f4d2SKip Macy 		message = "writing request-rx-copy";
52889e0f4d2SKip Macy 		goto abort_transaction;
52989e0f4d2SKip Macy 	}
530*ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "feature-rx-notify", "%d", 1);
53189e0f4d2SKip Macy 	if (err) {
53289e0f4d2SKip Macy 		message = "writing feature-rx-notify";
53389e0f4d2SKip Macy 		goto abort_transaction;
53489e0f4d2SKip Macy 	}
535*ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "feature-sg", "%d", 1);
53689e0f4d2SKip Macy 	if (err) {
53789e0f4d2SKip Macy 		message = "writing feature-sg";
53889e0f4d2SKip Macy 		goto abort_transaction;
53989e0f4d2SKip Macy 	}
54012678024SDoug Rabson #if __FreeBSD_version >= 700000
541*ff662b5cSJustin T. Gibbs 	err = xs_printf(xst, node, "feature-gso-tcpv4", "%d", 1);
54289e0f4d2SKip Macy 	if (err) {
54389e0f4d2SKip Macy 		message = "writing feature-gso-tcpv4";
54489e0f4d2SKip Macy 		goto abort_transaction;
54589e0f4d2SKip Macy 	}
54689e0f4d2SKip Macy #endif
54789e0f4d2SKip Macy 
548*ff662b5cSJustin T. Gibbs 	err = xs_transaction_end(xst, 0);
54989e0f4d2SKip Macy 	if (err) {
55089e0f4d2SKip Macy 		if (err == EAGAIN)
55189e0f4d2SKip Macy 			goto again;
55289e0f4d2SKip Macy 		xenbus_dev_fatal(dev, err, "completing transaction");
55389e0f4d2SKip Macy 		goto destroy_ring;
55489e0f4d2SKip Macy 	}
55589e0f4d2SKip Macy 
55689e0f4d2SKip Macy 	return 0;
55789e0f4d2SKip Macy 
55889e0f4d2SKip Macy  abort_transaction:
559*ff662b5cSJustin T. Gibbs 	xs_transaction_end(xst, 1);
56089e0f4d2SKip Macy 	xenbus_dev_fatal(dev, err, "%s", message);
56189e0f4d2SKip Macy  destroy_ring:
56289e0f4d2SKip Macy 	netif_free(info);
56389e0f4d2SKip Macy  out:
56489e0f4d2SKip Macy 	return err;
56589e0f4d2SKip Macy }
56689e0f4d2SKip Macy 
56789e0f4d2SKip Macy 
56889e0f4d2SKip Macy static int
56923dc5621SKip Macy setup_device(device_t dev, struct netfront_info *info)
57089e0f4d2SKip Macy {
57189e0f4d2SKip Macy 	netif_tx_sring_t *txs;
57289e0f4d2SKip Macy 	netif_rx_sring_t *rxs;
5733a6d1fcfSKip Macy 	int error;
57489e0f4d2SKip Macy 	struct ifnet *ifp;
57589e0f4d2SKip Macy 
57689e0f4d2SKip Macy 	ifp = info->xn_ifp;
57789e0f4d2SKip Macy 
578*ff662b5cSJustin T. Gibbs 	info->tx_ring_ref = GRANT_REF_INVALID;
579*ff662b5cSJustin T. Gibbs 	info->rx_ring_ref = GRANT_REF_INVALID;
58089e0f4d2SKip Macy 	info->rx.sring = NULL;
58189e0f4d2SKip Macy 	info->tx.sring = NULL;
58289e0f4d2SKip Macy 	info->irq = 0;
58389e0f4d2SKip Macy 
58489e0f4d2SKip Macy 	txs = (netif_tx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO);
58589e0f4d2SKip Macy 	if (!txs) {
5863a6d1fcfSKip Macy 		error = ENOMEM;
5873a6d1fcfSKip Macy 		xenbus_dev_fatal(dev, error, "allocating tx ring page");
58889e0f4d2SKip Macy 		goto fail;
58989e0f4d2SKip Macy 	}
59089e0f4d2SKip Macy 	SHARED_RING_INIT(txs);
59189e0f4d2SKip Macy 	FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
5923a6d1fcfSKip Macy 	error = xenbus_grant_ring(dev, virt_to_mfn(txs), &info->tx_ring_ref);
5933a6d1fcfSKip Macy 	if (error)
59489e0f4d2SKip Macy 		goto fail;
59589e0f4d2SKip Macy 
59689e0f4d2SKip Macy 	rxs = (netif_rx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO);
59789e0f4d2SKip Macy 	if (!rxs) {
5983a6d1fcfSKip Macy 		error = ENOMEM;
5993a6d1fcfSKip Macy 		xenbus_dev_fatal(dev, error, "allocating rx ring page");
60089e0f4d2SKip Macy 		goto fail;
60189e0f4d2SKip Macy 	}
60289e0f4d2SKip Macy 	SHARED_RING_INIT(rxs);
60389e0f4d2SKip Macy 	FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
60489e0f4d2SKip Macy 
6053a6d1fcfSKip Macy 	error = xenbus_grant_ring(dev, virt_to_mfn(rxs), &info->rx_ring_ref);
6063a6d1fcfSKip Macy 	if (error)
60789e0f4d2SKip Macy 		goto fail;
60889e0f4d2SKip Macy 
6093a6d1fcfSKip Macy 	error = bind_listening_port_to_irqhandler(xenbus_get_otherend_id(dev),
6103a6d1fcfSKip Macy 	    "xn", xn_intr, info, INTR_TYPE_NET | INTR_MPSAFE, &info->irq);
61189e0f4d2SKip Macy 
6123a6d1fcfSKip Macy 	if (error) {
6133a6d1fcfSKip Macy 		xenbus_dev_fatal(dev, error,
61489e0f4d2SKip Macy 				 "bind_evtchn_to_irqhandler failed");
61589e0f4d2SKip Macy 		goto fail;
61689e0f4d2SKip Macy 	}
61789e0f4d2SKip Macy 
61889e0f4d2SKip Macy 	show_device(info);
61989e0f4d2SKip Macy 
6203a6d1fcfSKip Macy 	return (0);
62189e0f4d2SKip Macy 
62289e0f4d2SKip Macy  fail:
62389e0f4d2SKip Macy 	netif_free(info);
6243a6d1fcfSKip Macy 	return (error);
62589e0f4d2SKip Macy }
62689e0f4d2SKip Macy 
62789e0f4d2SKip Macy /**
62812678024SDoug Rabson  * If this interface has an ipv4 address, send an arp for it. This
62912678024SDoug Rabson  * helps to get the network going again after migrating hosts.
63012678024SDoug Rabson  */
63112678024SDoug Rabson static void
63212678024SDoug Rabson netfront_send_fake_arp(device_t dev, struct netfront_info *info)
63312678024SDoug Rabson {
63412678024SDoug Rabson 	struct ifnet *ifp;
63512678024SDoug Rabson 	struct ifaddr *ifa;
63612678024SDoug Rabson 
63712678024SDoug Rabson 	ifp = info->xn_ifp;
63812678024SDoug Rabson 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
63912678024SDoug Rabson 		if (ifa->ifa_addr->sa_family == AF_INET) {
64012678024SDoug Rabson 			arp_ifinit(ifp, ifa);
64112678024SDoug Rabson 		}
64212678024SDoug Rabson 	}
64312678024SDoug Rabson }
64412678024SDoug Rabson 
64512678024SDoug Rabson /**
64689e0f4d2SKip Macy  * Callback received when the backend's state changes.
64789e0f4d2SKip Macy  */
648cfed3783SKip Macy static int
64923dc5621SKip Macy netfront_backend_changed(device_t dev, XenbusState newstate)
65089e0f4d2SKip Macy {
65123dc5621SKip Macy 	struct netfront_info *sc = device_get_softc(dev);
65289e0f4d2SKip Macy 
65323dc5621SKip Macy 	DPRINTK("newstate=%d\n", newstate);
65489e0f4d2SKip Macy 
65523dc5621SKip Macy 	switch (newstate) {
65689e0f4d2SKip Macy 	case XenbusStateInitialising:
65789e0f4d2SKip Macy 	case XenbusStateInitialised:
65889e0f4d2SKip Macy 	case XenbusStateConnected:
65989e0f4d2SKip Macy 	case XenbusStateUnknown:
66089e0f4d2SKip Macy 	case XenbusStateClosed:
661920ba15bSKip Macy 	case XenbusStateReconfigured:
662920ba15bSKip Macy 	case XenbusStateReconfiguring:
66389e0f4d2SKip Macy 		break;
66489e0f4d2SKip Macy 	case XenbusStateInitWait:
66523dc5621SKip Macy 		if (xenbus_get_state(dev) != XenbusStateInitialising)
66689e0f4d2SKip Macy 			break;
66723dc5621SKip Macy 		if (network_connect(sc) != 0)
66889e0f4d2SKip Macy 			break;
66923dc5621SKip Macy 		xenbus_set_state(dev, XenbusStateConnected);
67012678024SDoug Rabson 		netfront_send_fake_arp(dev, sc);
67123dc5621SKip Macy 		break;
67289e0f4d2SKip Macy 	case XenbusStateClosing:
67323dc5621SKip Macy 		xenbus_set_state(dev, XenbusStateClosed);
67489e0f4d2SKip Macy 		break;
67589e0f4d2SKip Macy 	}
676cfed3783SKip Macy 	return (0);
67789e0f4d2SKip Macy }
67889e0f4d2SKip Macy 
67989e0f4d2SKip Macy static void
68089e0f4d2SKip Macy xn_free_rx_ring(struct netfront_info *sc)
68189e0f4d2SKip Macy {
68289e0f4d2SKip Macy #if 0
68389e0f4d2SKip Macy 	int i;
68489e0f4d2SKip Macy 
68589e0f4d2SKip Macy 	for (i = 0; i < NET_RX_RING_SIZE; i++) {
686931eeffaSKenneth D. Merry 		if (sc->xn_cdata.rx_mbufs[i] != NULL) {
687931eeffaSKenneth D. Merry 			m_freem(sc->rx_mbufs[i]);
688931eeffaSKenneth D. Merry 			sc->rx_mbufs[i] = NULL;
68989e0f4d2SKip Macy 		}
69089e0f4d2SKip Macy 	}
69189e0f4d2SKip Macy 
69289e0f4d2SKip Macy 	sc->rx.rsp_cons = 0;
69389e0f4d2SKip Macy 	sc->xn_rx_if->req_prod = 0;
69489e0f4d2SKip Macy 	sc->xn_rx_if->event = sc->rx.rsp_cons ;
69589e0f4d2SKip Macy #endif
69689e0f4d2SKip Macy }
69789e0f4d2SKip Macy 
69889e0f4d2SKip Macy static void
69989e0f4d2SKip Macy xn_free_tx_ring(struct netfront_info *sc)
70089e0f4d2SKip Macy {
70189e0f4d2SKip Macy #if 0
70289e0f4d2SKip Macy 	int i;
70389e0f4d2SKip Macy 
70489e0f4d2SKip Macy 	for (i = 0; i < NET_TX_RING_SIZE; i++) {
705931eeffaSKenneth D. Merry 		if (sc->tx_mbufs[i] != NULL) {
706931eeffaSKenneth D. Merry 			m_freem(sc->tx_mbufs[i]);
70789e0f4d2SKip Macy 			sc->xn_cdata.xn_tx_chain[i] = NULL;
70889e0f4d2SKip Macy 		}
70989e0f4d2SKip Macy 	}
71089e0f4d2SKip Macy 
71189e0f4d2SKip Macy 	return;
71289e0f4d2SKip Macy #endif
71389e0f4d2SKip Macy }
71489e0f4d2SKip Macy 
715931eeffaSKenneth D. Merry /**
716931eeffaSKenneth D. Merry  * \brief Verify that there is sufficient space in the Tx ring
717931eeffaSKenneth D. Merry  *        buffer for a maximally sized request to be enqueued.
718c099cafaSAdrian Chadd  *
719931eeffaSKenneth D. Merry  * A transmit request requires a transmit descriptor for each packet
720931eeffaSKenneth D. Merry  * fragment, plus up to 2 entries for "options" (e.g. TSO).
721c099cafaSAdrian Chadd  */
72289e0f4d2SKip Macy static inline int
723931eeffaSKenneth D. Merry xn_tx_slot_available(struct netfront_info *np)
72489e0f4d2SKip Macy {
725931eeffaSKenneth D. Merry 	return (RING_FREE_REQUESTS(&np->tx) > (MAX_TX_REQ_FRAGS + 2));
72689e0f4d2SKip Macy }
727931eeffaSKenneth D. Merry 
72889e0f4d2SKip Macy static void
72989e0f4d2SKip Macy netif_release_tx_bufs(struct netfront_info *np)
73089e0f4d2SKip Macy {
73189e0f4d2SKip Macy 	int i;
73289e0f4d2SKip Macy 
73389e0f4d2SKip Macy 	for (i = 1; i <= NET_TX_RING_SIZE; i++) {
734931eeffaSKenneth D. Merry 		struct mbuf *m;
73589e0f4d2SKip Macy 
736931eeffaSKenneth D. Merry 		m = np->tx_mbufs[i];
737931eeffaSKenneth D. Merry 
738931eeffaSKenneth D. Merry 		/*
739931eeffaSKenneth D. Merry 		 * We assume that no kernel addresses are
740931eeffaSKenneth D. Merry 		 * less than NET_TX_RING_SIZE.  Any entry
741931eeffaSKenneth D. Merry 		 * in the table that is below this number
742931eeffaSKenneth D. Merry 		 * must be an index from free-list tracking.
743931eeffaSKenneth D. Merry 		 */
744931eeffaSKenneth D. Merry 		if (((uintptr_t)m) <= NET_TX_RING_SIZE)
74589e0f4d2SKip Macy 			continue;
74689e0f4d2SKip Macy 		gnttab_grant_foreign_access_ref(np->grant_tx_ref[i],
74723dc5621SKip Macy 		    xenbus_get_otherend_id(np->xbdev),
74823dc5621SKip Macy 		    virt_to_mfn(mtod(m, vm_offset_t)),
74989e0f4d2SKip Macy 		    GNTMAP_readonly);
75089e0f4d2SKip Macy 		gnttab_release_grant_reference(&np->gref_tx_head,
75189e0f4d2SKip Macy 		    np->grant_tx_ref[i]);
752*ff662b5cSJustin T. Gibbs 		np->grant_tx_ref[i] = GRANT_REF_INVALID;
75389e0f4d2SKip Macy 		add_id_to_freelist(np->tx_mbufs, i);
754a4ec37f5SAdrian Chadd 		np->xn_cdata.xn_tx_chain_cnt--;
755a4ec37f5SAdrian Chadd 		if (np->xn_cdata.xn_tx_chain_cnt < 0) {
756a4ec37f5SAdrian Chadd 			panic("netif_release_tx_bufs: tx_chain_cnt must be >= 0");
757a4ec37f5SAdrian Chadd 		}
75889e0f4d2SKip Macy 		m_freem(m);
75989e0f4d2SKip Macy 	}
76089e0f4d2SKip Macy }
76189e0f4d2SKip Macy 
76289e0f4d2SKip Macy static void
76389e0f4d2SKip Macy network_alloc_rx_buffers(struct netfront_info *sc)
76489e0f4d2SKip Macy {
76523dc5621SKip Macy 	int otherend_id = xenbus_get_otherend_id(sc->xbdev);
76689e0f4d2SKip Macy 	unsigned short id;
76789e0f4d2SKip Macy 	struct mbuf *m_new;
76889e0f4d2SKip Macy 	int i, batch_target, notify;
76989e0f4d2SKip Macy 	RING_IDX req_prod;
77089e0f4d2SKip Macy 	struct xen_memory_reservation reservation;
77189e0f4d2SKip Macy 	grant_ref_t ref;
77289e0f4d2SKip Macy 	int nr_flips;
77389e0f4d2SKip Macy 	netif_rx_request_t *req;
77489e0f4d2SKip Macy 	vm_offset_t vaddr;
77589e0f4d2SKip Macy 	u_long pfn;
77689e0f4d2SKip Macy 
77789e0f4d2SKip Macy 	req_prod = sc->rx.req_prod_pvt;
77889e0f4d2SKip Macy 
77989e0f4d2SKip Macy 	if (unlikely(sc->carrier == 0))
78089e0f4d2SKip Macy 		return;
78189e0f4d2SKip Macy 
78289e0f4d2SKip Macy 	/*
783931eeffaSKenneth D. Merry 	 * Allocate mbufs greedily, even though we batch updates to the
78489e0f4d2SKip Macy 	 * receive ring. This creates a less bursty demand on the memory
785931eeffaSKenneth D. Merry 	 * allocator, and so should reduce the chance of failed allocation
78689e0f4d2SKip Macy 	 * requests both for ourself and for other kernel subsystems.
787931eeffaSKenneth D. Merry 	 *
788931eeffaSKenneth D. Merry 	 * Here we attempt to maintain rx_target buffers in flight, counting
789931eeffaSKenneth D. Merry 	 * buffers that we have yet to process in the receive ring.
79089e0f4d2SKip Macy 	 */
79189e0f4d2SKip Macy 	batch_target = sc->rx_target - (req_prod - sc->rx.rsp_cons);
79289e0f4d2SKip Macy 	for (i = mbufq_len(&sc->xn_rx_batch); i < batch_target; i++) {
79389e0f4d2SKip Macy 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
794931eeffaSKenneth D. Merry 		if (m_new == NULL) {
795931eeffaSKenneth D. Merry 			printf("%s: MGETHDR failed\n", __func__);
79689e0f4d2SKip Macy 			goto no_mbuf;
797931eeffaSKenneth D. Merry 		}
79889e0f4d2SKip Macy 
79989e0f4d2SKip Macy 		m_cljget(m_new, M_DONTWAIT, MJUMPAGESIZE);
80089e0f4d2SKip Macy 		if ((m_new->m_flags & M_EXT) == 0) {
801931eeffaSKenneth D. Merry 			printf("%s: m_cljget failed\n", __func__);
80289e0f4d2SKip Macy 			m_freem(m_new);
80389e0f4d2SKip Macy 
80489e0f4d2SKip Macy no_mbuf:
80589e0f4d2SKip Macy 			if (i != 0)
80689e0f4d2SKip Macy 				goto refill;
80789e0f4d2SKip Macy 			/*
80889e0f4d2SKip Macy 			 * XXX set timer
80989e0f4d2SKip Macy 			 */
81089e0f4d2SKip Macy 			break;
81189e0f4d2SKip Macy 		}
81289e0f4d2SKip Macy 		m_new->m_len = m_new->m_pkthdr.len = MJUMPAGESIZE;
81389e0f4d2SKip Macy 
81489e0f4d2SKip Macy 		/* queue the mbufs allocated */
81589e0f4d2SKip Macy 		mbufq_tail(&sc->xn_rx_batch, m_new);
81689e0f4d2SKip Macy 	}
81789e0f4d2SKip Macy 
818931eeffaSKenneth D. Merry 	/*
819931eeffaSKenneth D. Merry 	 * If we've allocated at least half of our target number of entries,
820931eeffaSKenneth D. Merry 	 * submit them to the backend - we have enough to make the overhead
821931eeffaSKenneth D. Merry 	 * of submission worthwhile.  Otherwise wait for more mbufs and
822931eeffaSKenneth D. Merry 	 * request entries to become available.
823931eeffaSKenneth D. Merry 	 */
82489e0f4d2SKip Macy 	if (i < (sc->rx_target/2)) {
82589e0f4d2SKip Macy 		if (req_prod >sc->rx.sring->req_prod)
82689e0f4d2SKip Macy 			goto push;
82789e0f4d2SKip Macy 		return;
82889e0f4d2SKip Macy 	}
829931eeffaSKenneth D. Merry 
830931eeffaSKenneth D. Merry 	/*
831931eeffaSKenneth D. Merry 	 * Double floating fill target if we risked having the backend
832931eeffaSKenneth D. Merry 	 * run out of empty buffers for receive traffic.  We define "running
833931eeffaSKenneth D. Merry 	 * low" as having less than a fourth of our target buffers free
834931eeffaSKenneth D. Merry 	 * at the time we refilled the queue.
835931eeffaSKenneth D. Merry 	 */
836931eeffaSKenneth D. Merry 	if ((req_prod - sc->rx.sring->rsp_prod) < (sc->rx_target / 4)) {
837931eeffaSKenneth D. Merry 		sc->rx_target *= 2;
838931eeffaSKenneth D. Merry 		if (sc->rx_target > sc->rx_max_target)
83989e0f4d2SKip Macy 			sc->rx_target = sc->rx_max_target;
840931eeffaSKenneth D. Merry 	}
84189e0f4d2SKip Macy 
84289e0f4d2SKip Macy refill:
84389e0f4d2SKip Macy 	for (nr_flips = i = 0; ; i++) {
84489e0f4d2SKip Macy 		if ((m_new = mbufq_dequeue(&sc->xn_rx_batch)) == NULL)
84589e0f4d2SKip Macy 			break;
84689e0f4d2SKip Macy 
84789e0f4d2SKip Macy 		m_new->m_ext.ext_arg1 = (vm_paddr_t *)(uintptr_t)(
84889e0f4d2SKip Macy 				vtophys(m_new->m_ext.ext_buf) >> PAGE_SHIFT);
84989e0f4d2SKip Macy 
85089e0f4d2SKip Macy 		id = xennet_rxidx(req_prod + i);
85189e0f4d2SKip Macy 
852931eeffaSKenneth D. Merry 		KASSERT(sc->rx_mbufs[id] == NULL, ("non-NULL xm_rx_chain"));
853931eeffaSKenneth D. Merry 		sc->rx_mbufs[id] = m_new;
85489e0f4d2SKip Macy 
85589e0f4d2SKip Macy 		ref = gnttab_claim_grant_reference(&sc->gref_rx_head);
856*ff662b5cSJustin T. Gibbs 		KASSERT(ref != GNTTAB_LIST_END,
857*ff662b5cSJustin T. Gibbs 			("reserved grant references exhuasted"));
85889e0f4d2SKip Macy 		sc->grant_rx_ref[id] = ref;
85989e0f4d2SKip Macy 
86089e0f4d2SKip Macy 		vaddr = mtod(m_new, vm_offset_t);
86189e0f4d2SKip Macy 		pfn = vtophys(vaddr) >> PAGE_SHIFT;
86289e0f4d2SKip Macy 		req = RING_GET_REQUEST(&sc->rx, req_prod + i);
86389e0f4d2SKip Macy 
86489e0f4d2SKip Macy 		if (sc->copying_receiver == 0) {
86589e0f4d2SKip Macy 			gnttab_grant_foreign_transfer_ref(ref,
86623dc5621SKip Macy 			    otherend_id, pfn);
86789e0f4d2SKip Macy 			sc->rx_pfn_array[nr_flips] = PFNTOMFN(pfn);
86889e0f4d2SKip Macy 			if (!xen_feature(XENFEAT_auto_translated_physmap)) {
86989e0f4d2SKip Macy 				/* Remove this page before passing
87089e0f4d2SKip Macy 				 * back to Xen.
87189e0f4d2SKip Macy 				 */
87289e0f4d2SKip Macy 				set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
87389e0f4d2SKip Macy 				MULTI_update_va_mapping(&sc->rx_mcl[i],
87489e0f4d2SKip Macy 				    vaddr, 0, 0);
87589e0f4d2SKip Macy 			}
87689e0f4d2SKip Macy 			nr_flips++;
87789e0f4d2SKip Macy 		} else {
87889e0f4d2SKip Macy 			gnttab_grant_foreign_access_ref(ref,
87923dc5621SKip Macy 			    otherend_id,
88089e0f4d2SKip Macy 			    PFNTOMFN(pfn), 0);
88189e0f4d2SKip Macy 		}
88289e0f4d2SKip Macy 		req->id = id;
88389e0f4d2SKip Macy 		req->gref = ref;
88489e0f4d2SKip Macy 
88589e0f4d2SKip Macy 		sc->rx_pfn_array[i] =
88689e0f4d2SKip Macy 		    vtomach(mtod(m_new,vm_offset_t)) >> PAGE_SHIFT;
88789e0f4d2SKip Macy 	}
88889e0f4d2SKip Macy 
88989e0f4d2SKip Macy 	KASSERT(i, ("no mbufs processed")); /* should have returned earlier */
89089e0f4d2SKip Macy 	KASSERT(mbufq_len(&sc->xn_rx_batch) == 0, ("not all mbufs processed"));
89189e0f4d2SKip Macy 	/*
89289e0f4d2SKip Macy 	 * We may have allocated buffers which have entries outstanding
89389e0f4d2SKip Macy 	 * in the page * update queue -- make sure we flush those first!
89489e0f4d2SKip Macy 	 */
89589e0f4d2SKip Macy 	PT_UPDATES_FLUSH();
89689e0f4d2SKip Macy 	if (nr_flips != 0) {
89789e0f4d2SKip Macy #ifdef notyet
89889e0f4d2SKip Macy 		/* Tell the ballon driver what is going on. */
89989e0f4d2SKip Macy 		balloon_update_driver_allowance(i);
90089e0f4d2SKip Macy #endif
901920ba15bSKip Macy 		set_xen_guest_handle(reservation.extent_start, sc->rx_pfn_array);
90289e0f4d2SKip Macy 		reservation.nr_extents   = i;
90389e0f4d2SKip Macy 		reservation.extent_order = 0;
90489e0f4d2SKip Macy 		reservation.address_bits = 0;
90589e0f4d2SKip Macy 		reservation.domid        = DOMID_SELF;
90689e0f4d2SKip Macy 
90789e0f4d2SKip Macy 		if (!xen_feature(XENFEAT_auto_translated_physmap)) {
90889e0f4d2SKip Macy 
90989e0f4d2SKip Macy 			/* After all PTEs have been zapped, flush the TLB. */
91089e0f4d2SKip Macy 			sc->rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] =
91189e0f4d2SKip Macy 			    UVMF_TLB_FLUSH|UVMF_ALL;
91289e0f4d2SKip Macy 
91389e0f4d2SKip Macy 			/* Give away a batch of pages. */
91489e0f4d2SKip Macy 			sc->rx_mcl[i].op = __HYPERVISOR_memory_op;
91589e0f4d2SKip Macy 			sc->rx_mcl[i].args[0] = XENMEM_decrease_reservation;
91689e0f4d2SKip Macy 			sc->rx_mcl[i].args[1] =  (u_long)&reservation;
91789e0f4d2SKip Macy 			/* Zap PTEs and give away pages in one big multicall. */
91889e0f4d2SKip Macy 			(void)HYPERVISOR_multicall(sc->rx_mcl, i+1);
91989e0f4d2SKip Macy 
92089e0f4d2SKip Macy 			/* Check return status of HYPERVISOR_dom_mem_op(). */
92189e0f4d2SKip Macy 			if (unlikely(sc->rx_mcl[i].result != i))
92289e0f4d2SKip Macy 				panic("Unable to reduce memory reservation\n");
92389e0f4d2SKip Macy 			} else {
92489e0f4d2SKip Macy 				if (HYPERVISOR_memory_op(
92589e0f4d2SKip Macy 				    XENMEM_decrease_reservation, &reservation)
92689e0f4d2SKip Macy 				    != i)
92789e0f4d2SKip Macy 					panic("Unable to reduce memory "
92889e0f4d2SKip Macy 					    "reservation\n");
92989e0f4d2SKip Macy 		}
93089e0f4d2SKip Macy 	} else {
93189e0f4d2SKip Macy 		wmb();
93289e0f4d2SKip Macy 	}
93389e0f4d2SKip Macy 
93489e0f4d2SKip Macy 	/* Above is a suitable barrier to ensure backend will see requests. */
93589e0f4d2SKip Macy 	sc->rx.req_prod_pvt = req_prod + i;
93689e0f4d2SKip Macy push:
93789e0f4d2SKip Macy 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->rx, notify);
93889e0f4d2SKip Macy 	if (notify)
93989e0f4d2SKip Macy 		notify_remote_via_irq(sc->irq);
94089e0f4d2SKip Macy }
94189e0f4d2SKip Macy 
94289e0f4d2SKip Macy static void
94389e0f4d2SKip Macy xn_rxeof(struct netfront_info *np)
94489e0f4d2SKip Macy {
94589e0f4d2SKip Macy 	struct ifnet *ifp;
94612678024SDoug Rabson #if __FreeBSD_version >= 700000
94712678024SDoug Rabson 	struct lro_ctrl *lro = &np->xn_lro;
94812678024SDoug Rabson 	struct lro_entry *queued;
94912678024SDoug Rabson #endif
95089e0f4d2SKip Macy 	struct netfront_rx_info rinfo;
95189e0f4d2SKip Macy 	struct netif_rx_response *rx = &rinfo.rx;
95289e0f4d2SKip Macy 	struct netif_extra_info *extras = rinfo.extras;
95389e0f4d2SKip Macy 	RING_IDX i, rp;
95489e0f4d2SKip Macy 	multicall_entry_t *mcl;
95589e0f4d2SKip Macy 	struct mbuf *m;
95683b92f6eSKip Macy 	struct mbuf_head rxq, errq;
95749906218SDoug Rabson 	int err, pages_flipped = 0, work_to_do;
95889e0f4d2SKip Macy 
95949906218SDoug Rabson 	do {
96089e0f4d2SKip Macy 		XN_RX_LOCK_ASSERT(np);
96189e0f4d2SKip Macy 		if (!netfront_carrier_ok(np))
96289e0f4d2SKip Macy 			return;
96389e0f4d2SKip Macy 
96489e0f4d2SKip Macy 		mbufq_init(&errq);
96589e0f4d2SKip Macy 		mbufq_init(&rxq);
96689e0f4d2SKip Macy 
96789e0f4d2SKip Macy 		ifp = np->xn_ifp;
96889e0f4d2SKip Macy 
96989e0f4d2SKip Macy 		rp = np->rx.sring->rsp_prod;
97089e0f4d2SKip Macy 		rmb();	/* Ensure we see queued responses up to 'rp'. */
97189e0f4d2SKip Macy 
97289e0f4d2SKip Macy 		i = np->rx.rsp_cons;
97389e0f4d2SKip Macy 		while ((i != rp)) {
97489e0f4d2SKip Macy 			memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
97589e0f4d2SKip Macy 			memset(extras, 0, sizeof(rinfo.extras));
97689e0f4d2SKip Macy 
97783b92f6eSKip Macy 			m = NULL;
978931eeffaSKenneth D. Merry 			err = xennet_get_responses(np, &rinfo, rp, &i, &m,
97989e0f4d2SKip Macy 			    &pages_flipped);
98089e0f4d2SKip Macy 
98189e0f4d2SKip Macy 			if (unlikely(err)) {
98283b92f6eSKip Macy 				if (m)
98389e0f4d2SKip Macy 					mbufq_tail(&errq, m);
98489e0f4d2SKip Macy 				np->stats.rx_errors++;
98589e0f4d2SKip Macy 				continue;
98689e0f4d2SKip Macy 			}
98789e0f4d2SKip Macy 
98889e0f4d2SKip Macy 			m->m_pkthdr.rcvif = ifp;
98989e0f4d2SKip Macy 			if ( rx->flags & NETRXF_data_validated ) {
99089e0f4d2SKip Macy 				/* Tell the stack the checksums are okay */
99189e0f4d2SKip Macy 				/*
99289e0f4d2SKip Macy 				 * XXX this isn't necessarily the case - need to add
99389e0f4d2SKip Macy 				 * check
99489e0f4d2SKip Macy 				 */
99589e0f4d2SKip Macy 
99689e0f4d2SKip Macy 				m->m_pkthdr.csum_flags |=
99789e0f4d2SKip Macy 					(CSUM_IP_CHECKED | CSUM_IP_VALID | CSUM_DATA_VALID
99889e0f4d2SKip Macy 					    | CSUM_PSEUDO_HDR);
99989e0f4d2SKip Macy 				m->m_pkthdr.csum_data = 0xffff;
100089e0f4d2SKip Macy 			}
100189e0f4d2SKip Macy 
100289e0f4d2SKip Macy 			np->stats.rx_packets++;
100383b92f6eSKip Macy 			np->stats.rx_bytes += m->m_pkthdr.len;
100489e0f4d2SKip Macy 
100589e0f4d2SKip Macy 			mbufq_tail(&rxq, m);
1006931eeffaSKenneth D. Merry 			np->rx.rsp_cons = i;
100789e0f4d2SKip Macy 		}
100889e0f4d2SKip Macy 
100989e0f4d2SKip Macy 		if (pages_flipped) {
101089e0f4d2SKip Macy 			/* Some pages are no longer absent... */
101189e0f4d2SKip Macy #ifdef notyet
101289e0f4d2SKip Macy 			balloon_update_driver_allowance(-pages_flipped);
101389e0f4d2SKip Macy #endif
101489e0f4d2SKip Macy 			/* Do all the remapping work, and M->P updates, in one big
101589e0f4d2SKip Macy 			 * hypercall.
101689e0f4d2SKip Macy 			 */
101789e0f4d2SKip Macy 			if (!!xen_feature(XENFEAT_auto_translated_physmap)) {
101889e0f4d2SKip Macy 				mcl = np->rx_mcl + pages_flipped;
101989e0f4d2SKip Macy 				mcl->op = __HYPERVISOR_mmu_update;
102089e0f4d2SKip Macy 				mcl->args[0] = (u_long)np->rx_mmu;
102189e0f4d2SKip Macy 				mcl->args[1] = pages_flipped;
102289e0f4d2SKip Macy 				mcl->args[2] = 0;
102389e0f4d2SKip Macy 				mcl->args[3] = DOMID_SELF;
102489e0f4d2SKip Macy 				(void)HYPERVISOR_multicall(np->rx_mcl,
102589e0f4d2SKip Macy 				    pages_flipped + 1);
102689e0f4d2SKip Macy 			}
102789e0f4d2SKip Macy 		}
102889e0f4d2SKip Macy 
102989e0f4d2SKip Macy 		while ((m = mbufq_dequeue(&errq)))
103089e0f4d2SKip Macy 			m_freem(m);
103189e0f4d2SKip Macy 
103289e0f4d2SKip Macy 		/*
103389e0f4d2SKip Macy 		 * Process all the mbufs after the remapping is complete.
103489e0f4d2SKip Macy 		 * Break the mbuf chain first though.
103589e0f4d2SKip Macy 		 */
103689e0f4d2SKip Macy 		while ((m = mbufq_dequeue(&rxq)) != NULL) {
103789e0f4d2SKip Macy 			ifp->if_ipackets++;
103889e0f4d2SKip Macy 
103989e0f4d2SKip Macy 			/*
104089e0f4d2SKip Macy 			 * Do we really need to drop the rx lock?
104189e0f4d2SKip Macy 			 */
104289e0f4d2SKip Macy 			XN_RX_UNLOCK(np);
104312678024SDoug Rabson #if __FreeBSD_version >= 700000
104412678024SDoug Rabson 			/* Use LRO if possible */
104512678024SDoug Rabson 			if ((ifp->if_capenable & IFCAP_LRO) == 0 ||
104612678024SDoug Rabson 			    lro->lro_cnt == 0 || tcp_lro_rx(lro, m, 0)) {
104712678024SDoug Rabson 				/*
104812678024SDoug Rabson 				 * If LRO fails, pass up to the stack
104912678024SDoug Rabson 				 * directly.
105012678024SDoug Rabson 				 */
105189e0f4d2SKip Macy 				(*ifp->if_input)(ifp, m);
105212678024SDoug Rabson 			}
105312678024SDoug Rabson #else
105412678024SDoug Rabson 			(*ifp->if_input)(ifp, m);
105512678024SDoug Rabson #endif
105689e0f4d2SKip Macy 			XN_RX_LOCK(np);
105789e0f4d2SKip Macy 		}
105889e0f4d2SKip Macy 
105989e0f4d2SKip Macy 		np->rx.rsp_cons = i;
106089e0f4d2SKip Macy 
106112678024SDoug Rabson #if __FreeBSD_version >= 700000
106212678024SDoug Rabson 		/*
106312678024SDoug Rabson 		 * Flush any outstanding LRO work
106412678024SDoug Rabson 		 */
106512678024SDoug Rabson 		while (!SLIST_EMPTY(&lro->lro_active)) {
106612678024SDoug Rabson 			queued = SLIST_FIRST(&lro->lro_active);
106712678024SDoug Rabson 			SLIST_REMOVE_HEAD(&lro->lro_active, next);
106812678024SDoug Rabson 			tcp_lro_flush(lro, queued);
106912678024SDoug Rabson 		}
107012678024SDoug Rabson #endif
107112678024SDoug Rabson 
107289e0f4d2SKip Macy #if 0
107389e0f4d2SKip Macy 		/* If we get a callback with very few responses, reduce fill target. */
107489e0f4d2SKip Macy 		/* NB. Note exponential increase, linear decrease. */
107589e0f4d2SKip Macy 		if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
107689e0f4d2SKip Macy 			((3*np->rx_target) / 4)) && (--np->rx_target < np->rx_min_target))
107789e0f4d2SKip Macy 			np->rx_target = np->rx_min_target;
107889e0f4d2SKip Macy #endif
107989e0f4d2SKip Macy 
108089e0f4d2SKip Macy 		network_alloc_rx_buffers(np);
108189e0f4d2SKip Macy 
108249906218SDoug Rabson 		RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, work_to_do);
108349906218SDoug Rabson 	} while (work_to_do);
108489e0f4d2SKip Macy }
108589e0f4d2SKip Macy 
108689e0f4d2SKip Macy static void
108789e0f4d2SKip Macy xn_txeof(struct netfront_info *np)
108889e0f4d2SKip Macy {
108989e0f4d2SKip Macy 	RING_IDX i, prod;
109089e0f4d2SKip Macy 	unsigned short id;
109189e0f4d2SKip Macy 	struct ifnet *ifp;
109212678024SDoug Rabson 	netif_tx_response_t *txr;
109389e0f4d2SKip Macy 	struct mbuf *m;
109489e0f4d2SKip Macy 
109589e0f4d2SKip Macy 	XN_TX_LOCK_ASSERT(np);
109689e0f4d2SKip Macy 
109789e0f4d2SKip Macy 	if (!netfront_carrier_ok(np))
109889e0f4d2SKip Macy 		return;
109989e0f4d2SKip Macy 
110089e0f4d2SKip Macy 	ifp = np->xn_ifp;
110189e0f4d2SKip Macy 
110289e0f4d2SKip Macy 	do {
110389e0f4d2SKip Macy 		prod = np->tx.sring->rsp_prod;
110489e0f4d2SKip Macy 		rmb(); /* Ensure we see responses up to 'rp'. */
110589e0f4d2SKip Macy 
110689e0f4d2SKip Macy 		for (i = np->tx.rsp_cons; i != prod; i++) {
110712678024SDoug Rabson 			txr = RING_GET_RESPONSE(&np->tx, i);
110812678024SDoug Rabson 			if (txr->status == NETIF_RSP_NULL)
110912678024SDoug Rabson 				continue;
111012678024SDoug Rabson 
1111931eeffaSKenneth D. Merry 			if (txr->status != NETIF_RSP_OKAY) {
1112931eeffaSKenneth D. Merry 				printf("%s: WARNING: response is %d!\n",
1113931eeffaSKenneth D. Merry 				       __func__, txr->status);
1114931eeffaSKenneth D. Merry 			}
111512678024SDoug Rabson 			id = txr->id;
1116931eeffaSKenneth D. Merry 			m = np->tx_mbufs[id];
11172d8fae98SAdrian Chadd 			KASSERT(m != NULL, ("mbuf not found in xn_tx_chain"));
1118931eeffaSKenneth D. Merry 			KASSERT((uintptr_t)m > NET_TX_RING_SIZE,
1119931eeffaSKenneth D. Merry 				("mbuf already on the free list, but we're "
1120931eeffaSKenneth D. Merry 				"trying to free it again!"));
11212d8fae98SAdrian Chadd 			M_ASSERTVALID(m);
112289e0f4d2SKip Macy 
112312678024SDoug Rabson 			/*
112412678024SDoug Rabson 			 * Increment packet count if this is the last
112512678024SDoug Rabson 			 * mbuf of the chain.
112612678024SDoug Rabson 			 */
112712678024SDoug Rabson 			if (!m->m_next)
112889e0f4d2SKip Macy 				ifp->if_opackets++;
112989e0f4d2SKip Macy 			if (unlikely(gnttab_query_foreign_access(
113089e0f4d2SKip Macy 			    np->grant_tx_ref[id]) != 0)) {
1131931eeffaSKenneth D. Merry 				panic("grant id %u still in use by the backend",
1132931eeffaSKenneth D. Merry 				      id);
113389e0f4d2SKip Macy 			}
113489e0f4d2SKip Macy 			gnttab_end_foreign_access_ref(
1135920ba15bSKip Macy 				np->grant_tx_ref[id]);
113689e0f4d2SKip Macy 			gnttab_release_grant_reference(
113789e0f4d2SKip Macy 				&np->gref_tx_head, np->grant_tx_ref[id]);
1138*ff662b5cSJustin T. Gibbs 			np->grant_tx_ref[id] = GRANT_REF_INVALID;
113989e0f4d2SKip Macy 
1140931eeffaSKenneth D. Merry 			np->tx_mbufs[id] = NULL;
1141931eeffaSKenneth D. Merry 			add_id_to_freelist(np->tx_mbufs, id);
1142a4ec37f5SAdrian Chadd 			np->xn_cdata.xn_tx_chain_cnt--;
114312678024SDoug Rabson 			m_free(m);
1144d76e4550SAdrian Chadd 			/* Only mark the queue active if we've freed up at least one slot to try */
1145d76e4550SAdrian Chadd 			ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
114689e0f4d2SKip Macy 		}
114789e0f4d2SKip Macy 		np->tx.rsp_cons = prod;
114889e0f4d2SKip Macy 
114989e0f4d2SKip Macy 		/*
115089e0f4d2SKip Macy 		 * Set a new event, then check for race with update of
115189e0f4d2SKip Macy 		 * tx_cons. Note that it is essential to schedule a
115289e0f4d2SKip Macy 		 * callback, no matter how few buffers are pending. Even if
115389e0f4d2SKip Macy 		 * there is space in the transmit ring, higher layers may
115489e0f4d2SKip Macy 		 * be blocked because too much data is outstanding: in such
115589e0f4d2SKip Macy 		 * cases notification from Xen is likely to be the only kick
115689e0f4d2SKip Macy 		 * that we'll get.
115789e0f4d2SKip Macy 		 */
115889e0f4d2SKip Macy 		np->tx.sring->rsp_event =
115989e0f4d2SKip Macy 		    prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
116089e0f4d2SKip Macy 
116189e0f4d2SKip Macy 		mb();
116289e0f4d2SKip Macy 	} while (prod != np->tx.sring->rsp_prod);
116389e0f4d2SKip Macy 
116489e0f4d2SKip Macy 	if (np->tx_full &&
116589e0f4d2SKip Macy 	    ((np->tx.sring->req_prod - prod) < NET_TX_RING_SIZE)) {
116689e0f4d2SKip Macy 		np->tx_full = 0;
116789e0f4d2SKip Macy #if 0
116889e0f4d2SKip Macy 		if (np->user_state == UST_OPEN)
116989e0f4d2SKip Macy 			netif_wake_queue(dev);
117089e0f4d2SKip Macy #endif
117189e0f4d2SKip Macy 	}
117289e0f4d2SKip Macy 
117389e0f4d2SKip Macy }
117489e0f4d2SKip Macy 
117589e0f4d2SKip Macy static void
117689e0f4d2SKip Macy xn_intr(void *xsc)
117789e0f4d2SKip Macy {
117889e0f4d2SKip Macy 	struct netfront_info *np = xsc;
117989e0f4d2SKip Macy 	struct ifnet *ifp = np->xn_ifp;
118089e0f4d2SKip Macy 
118189e0f4d2SKip Macy #if 0
118289e0f4d2SKip Macy 	if (!(np->rx.rsp_cons != np->rx.sring->rsp_prod &&
118389e0f4d2SKip Macy 	    likely(netfront_carrier_ok(np)) &&
118489e0f4d2SKip Macy 	    ifp->if_drv_flags & IFF_DRV_RUNNING))
118589e0f4d2SKip Macy 		return;
118689e0f4d2SKip Macy #endif
1187931eeffaSKenneth D. Merry 	if (RING_HAS_UNCONSUMED_RESPONSES(&np->tx)) {
118889e0f4d2SKip Macy 		XN_TX_LOCK(np);
118989e0f4d2SKip Macy 		xn_txeof(np);
119089e0f4d2SKip Macy 		XN_TX_UNLOCK(np);
119189e0f4d2SKip Macy 	}
119289e0f4d2SKip Macy 
119389e0f4d2SKip Macy 	XN_RX_LOCK(np);
119489e0f4d2SKip Macy 	xn_rxeof(np);
119589e0f4d2SKip Macy 	XN_RX_UNLOCK(np);
119689e0f4d2SKip Macy 
119789e0f4d2SKip Macy 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
119889e0f4d2SKip Macy 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
119989e0f4d2SKip Macy 		xn_start(ifp);
120089e0f4d2SKip Macy }
120189e0f4d2SKip Macy 
120289e0f4d2SKip Macy 
120389e0f4d2SKip Macy static void
120489e0f4d2SKip Macy xennet_move_rx_slot(struct netfront_info *np, struct mbuf *m,
120589e0f4d2SKip Macy 	grant_ref_t ref)
120689e0f4d2SKip Macy {
120789e0f4d2SKip Macy 	int new = xennet_rxidx(np->rx.req_prod_pvt);
120889e0f4d2SKip Macy 
120989e0f4d2SKip Macy 	KASSERT(np->rx_mbufs[new] == NULL, ("rx_mbufs != NULL"));
121089e0f4d2SKip Macy 	np->rx_mbufs[new] = m;
121189e0f4d2SKip Macy 	np->grant_rx_ref[new] = ref;
121289e0f4d2SKip Macy 	RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
121389e0f4d2SKip Macy 	RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
121489e0f4d2SKip Macy 	np->rx.req_prod_pvt++;
121589e0f4d2SKip Macy }
121689e0f4d2SKip Macy 
121789e0f4d2SKip Macy static int
121889e0f4d2SKip Macy xennet_get_extras(struct netfront_info *np,
1219931eeffaSKenneth D. Merry     struct netif_extra_info *extras, RING_IDX rp, RING_IDX *cons)
122089e0f4d2SKip Macy {
122189e0f4d2SKip Macy 	struct netif_extra_info *extra;
122289e0f4d2SKip Macy 
122389e0f4d2SKip Macy 	int err = 0;
122489e0f4d2SKip Macy 
122589e0f4d2SKip Macy 	do {
122689e0f4d2SKip Macy 		struct mbuf *m;
122789e0f4d2SKip Macy 		grant_ref_t ref;
122889e0f4d2SKip Macy 
1229931eeffaSKenneth D. Merry 		if (unlikely(*cons + 1 == rp)) {
123089e0f4d2SKip Macy #if 0
123189e0f4d2SKip Macy 			if (net_ratelimit())
123289e0f4d2SKip Macy 				WPRINTK("Missing extra info\n");
123389e0f4d2SKip Macy #endif
1234931eeffaSKenneth D. Merry 			err = EINVAL;
123589e0f4d2SKip Macy 			break;
123689e0f4d2SKip Macy 		}
123789e0f4d2SKip Macy 
123889e0f4d2SKip Macy 		extra = (struct netif_extra_info *)
1239931eeffaSKenneth D. Merry 		RING_GET_RESPONSE(&np->rx, ++(*cons));
124089e0f4d2SKip Macy 
124189e0f4d2SKip Macy 		if (unlikely(!extra->type ||
124289e0f4d2SKip Macy 			extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
124389e0f4d2SKip Macy #if 0
124489e0f4d2SKip Macy 			if (net_ratelimit())
124589e0f4d2SKip Macy 				WPRINTK("Invalid extra type: %d\n",
124689e0f4d2SKip Macy 					extra->type);
124789e0f4d2SKip Macy #endif
1248931eeffaSKenneth D. Merry 			err = EINVAL;
124989e0f4d2SKip Macy 		} else {
125089e0f4d2SKip Macy 			memcpy(&extras[extra->type - 1], extra, sizeof(*extra));
125189e0f4d2SKip Macy 		}
125289e0f4d2SKip Macy 
1253931eeffaSKenneth D. Merry 		m = xennet_get_rx_mbuf(np, *cons);
1254931eeffaSKenneth D. Merry 		ref = xennet_get_rx_ref(np, *cons);
125589e0f4d2SKip Macy 		xennet_move_rx_slot(np, m, ref);
125689e0f4d2SKip Macy 	} while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
125789e0f4d2SKip Macy 
125889e0f4d2SKip Macy 	return err;
125989e0f4d2SKip Macy }
126089e0f4d2SKip Macy 
126189e0f4d2SKip Macy static int
126289e0f4d2SKip Macy xennet_get_responses(struct netfront_info *np,
1263931eeffaSKenneth D. Merry 	struct netfront_rx_info *rinfo, RING_IDX rp, RING_IDX *cons,
126483b92f6eSKip Macy 	struct mbuf  **list,
126589e0f4d2SKip Macy 	int *pages_flipped_p)
126689e0f4d2SKip Macy {
126789e0f4d2SKip Macy 	int pages_flipped = *pages_flipped_p;
126889e0f4d2SKip Macy 	struct mmu_update *mmu;
126989e0f4d2SKip Macy 	struct multicall_entry *mcl;
127089e0f4d2SKip Macy 	struct netif_rx_response *rx = &rinfo->rx;
127189e0f4d2SKip Macy 	struct netif_extra_info *extras = rinfo->extras;
127283b92f6eSKip Macy 	struct mbuf *m, *m0, *m_prev;
1273931eeffaSKenneth D. Merry 	grant_ref_t ref = xennet_get_rx_ref(np, *cons);
1274931eeffaSKenneth D. Merry 	RING_IDX ref_cons = *cons;
1275931eeffaSKenneth D. Merry 	int max = 5 /* MAX_TX_REQ_FRAGS + (rx->status <= RX_COPY_THRESHOLD) */;
127689e0f4d2SKip Macy 	int frags = 1;
127789e0f4d2SKip Macy 	int err = 0;
127889e0f4d2SKip Macy 	u_long ret;
127989e0f4d2SKip Macy 
1280931eeffaSKenneth D. Merry 	m0 = m = m_prev = xennet_get_rx_mbuf(np, *cons);
128183b92f6eSKip Macy 
128283b92f6eSKip Macy 
128389e0f4d2SKip Macy 	if (rx->flags & NETRXF_extra_info) {
1284931eeffaSKenneth D. Merry 		err = xennet_get_extras(np, extras, rp, cons);
128589e0f4d2SKip Macy 	}
128689e0f4d2SKip Macy 
128783b92f6eSKip Macy 
128883b92f6eSKip Macy 	if (m0 != NULL) {
128983b92f6eSKip Macy 		m0->m_pkthdr.len = 0;
129083b92f6eSKip Macy 		m0->m_next = NULL;
129183b92f6eSKip Macy 	}
129283b92f6eSKip Macy 
129389e0f4d2SKip Macy 	for (;;) {
129489e0f4d2SKip Macy 		u_long mfn;
129589e0f4d2SKip Macy 
129683b92f6eSKip Macy #if 0
1297227ca257SKip Macy 		DPRINTK("rx->status=%hd rx->offset=%hu frags=%u\n",
129883b92f6eSKip Macy 			rx->status, rx->offset, frags);
129983b92f6eSKip Macy #endif
130089e0f4d2SKip Macy 		if (unlikely(rx->status < 0 ||
130189e0f4d2SKip Macy 			rx->offset + rx->status > PAGE_SIZE)) {
1302931eeffaSKenneth D. Merry 
130389e0f4d2SKip Macy #if 0
130489e0f4d2SKip Macy 			if (net_ratelimit())
130589e0f4d2SKip Macy 				WPRINTK("rx->offset: %x, size: %u\n",
130689e0f4d2SKip Macy 					rx->offset, rx->status);
130789e0f4d2SKip Macy #endif
130889e0f4d2SKip Macy 			xennet_move_rx_slot(np, m, ref);
1309931eeffaSKenneth D. Merry 			if (m0 == m)
1310931eeffaSKenneth D. Merry 				m0 = NULL;
1311931eeffaSKenneth D. Merry 			m = NULL;
1312931eeffaSKenneth D. Merry 			err = EINVAL;
1313931eeffaSKenneth D. Merry 			goto next_skip_queue;
131489e0f4d2SKip Macy 		}
131589e0f4d2SKip Macy 
131689e0f4d2SKip Macy 		/*
131789e0f4d2SKip Macy 		 * This definitely indicates a bug, either in this driver or in
131889e0f4d2SKip Macy 		 * the backend driver. In future this should flag the bad
131989e0f4d2SKip Macy 		 * situation to the system controller to reboot the backed.
132089e0f4d2SKip Macy 		 */
1321*ff662b5cSJustin T. Gibbs 		if (ref == GRANT_REF_INVALID) {
1322931eeffaSKenneth D. Merry 
132389e0f4d2SKip Macy #if 0
132489e0f4d2SKip Macy 			if (net_ratelimit())
132589e0f4d2SKip Macy 				WPRINTK("Bad rx response id %d.\n", rx->id);
132689e0f4d2SKip Macy #endif
1327*ff662b5cSJustin T. Gibbs 			printf("%s: Bad rx response id %d.\n", __func__,rx->id);
1328931eeffaSKenneth D. Merry 			err = EINVAL;
132989e0f4d2SKip Macy 			goto next;
133089e0f4d2SKip Macy 		}
133189e0f4d2SKip Macy 
133289e0f4d2SKip Macy 		if (!np->copying_receiver) {
133389e0f4d2SKip Macy 			/* Memory pressure, insufficient buffer
133489e0f4d2SKip Macy 			 * headroom, ...
133589e0f4d2SKip Macy 			 */
133689e0f4d2SKip Macy 			if (!(mfn = gnttab_end_foreign_transfer_ref(ref))) {
1337931eeffaSKenneth D. Merry 				WPRINTK("Unfulfilled rx req (id=%d, st=%d).\n",
133889e0f4d2SKip Macy 					rx->id, rx->status);
133989e0f4d2SKip Macy 				xennet_move_rx_slot(np, m, ref);
1340931eeffaSKenneth D. Merry 				err = ENOMEM;
134189e0f4d2SKip Macy 				goto next;
134289e0f4d2SKip Macy 			}
134389e0f4d2SKip Macy 
134489e0f4d2SKip Macy 			if (!xen_feature( XENFEAT_auto_translated_physmap)) {
134589e0f4d2SKip Macy 				/* Remap the page. */
134689e0f4d2SKip Macy 				void *vaddr = mtod(m, void *);
134789e0f4d2SKip Macy 				uint32_t pfn;
134889e0f4d2SKip Macy 
134989e0f4d2SKip Macy 				mcl = np->rx_mcl + pages_flipped;
135089e0f4d2SKip Macy 				mmu = np->rx_mmu + pages_flipped;
135189e0f4d2SKip Macy 
135289e0f4d2SKip Macy 				MULTI_update_va_mapping(mcl, (u_long)vaddr,
13536ae0e31bSKip Macy 				    (((vm_paddr_t)mfn) << PAGE_SHIFT) | PG_RW |
135489e0f4d2SKip Macy 				    PG_V | PG_M | PG_A, 0);
13553a6d1fcfSKip Macy 				pfn = (uintptr_t)m->m_ext.ext_arg1;
135689e0f4d2SKip Macy 				mmu->ptr = ((vm_paddr_t)mfn << PAGE_SHIFT) |
135789e0f4d2SKip Macy 				    MMU_MACHPHYS_UPDATE;
135889e0f4d2SKip Macy 				mmu->val = pfn;
135989e0f4d2SKip Macy 
136089e0f4d2SKip Macy 				set_phys_to_machine(pfn, mfn);
136189e0f4d2SKip Macy 			}
136289e0f4d2SKip Macy 			pages_flipped++;
136389e0f4d2SKip Macy 		} else {
1364920ba15bSKip Macy 			ret = gnttab_end_foreign_access_ref(ref);
136589e0f4d2SKip Macy 			KASSERT(ret, ("ret != 0"));
136689e0f4d2SKip Macy 		}
136789e0f4d2SKip Macy 
136889e0f4d2SKip Macy 		gnttab_release_grant_reference(&np->gref_rx_head, ref);
136989e0f4d2SKip Macy 
137089e0f4d2SKip Macy next:
13713a539122SAdrian Chadd 		if (m == NULL)
13723a539122SAdrian Chadd 			break;
13733a539122SAdrian Chadd 
137483b92f6eSKip Macy 		m->m_len = rx->status;
137583b92f6eSKip Macy 		m->m_data += rx->offset;
137683b92f6eSKip Macy 		m0->m_pkthdr.len += rx->status;
137783b92f6eSKip Macy 
1378931eeffaSKenneth D. Merry next_skip_queue:
137989e0f4d2SKip Macy 		if (!(rx->flags & NETRXF_more_data))
138089e0f4d2SKip Macy 			break;
138189e0f4d2SKip Macy 
1382931eeffaSKenneth D. Merry 		if (*cons + frags == rp) {
138389e0f4d2SKip Macy 			if (net_ratelimit())
138489e0f4d2SKip Macy 				WPRINTK("Need more frags\n");
1385931eeffaSKenneth D. Merry 			err = ENOENT;
1386931eeffaSKenneth D. Merry 			printf("%s: cons %u frags %u rp %u, not enough frags\n",
1387931eeffaSKenneth D. Merry 			       __func__, *cons, frags, rp);
138889e0f4d2SKip Macy 			break;
138989e0f4d2SKip Macy 		}
1390931eeffaSKenneth D. Merry 		/*
1391931eeffaSKenneth D. Merry 		 * Note that m can be NULL, if rx->status < 0 or if
1392931eeffaSKenneth D. Merry 		 * rx->offset + rx->status > PAGE_SIZE above.
1393931eeffaSKenneth D. Merry 		 */
139483b92f6eSKip Macy 		m_prev = m;
139589e0f4d2SKip Macy 
1396931eeffaSKenneth D. Merry 		rx = RING_GET_RESPONSE(&np->rx, *cons + frags);
1397931eeffaSKenneth D. Merry 		m = xennet_get_rx_mbuf(np, *cons + frags);
139883b92f6eSKip Macy 
1399931eeffaSKenneth D. Merry 		/*
1400931eeffaSKenneth D. Merry 		 * m_prev == NULL can happen if rx->status < 0 or if
1401931eeffaSKenneth D. Merry 		 * rx->offset + * rx->status > PAGE_SIZE above.
1402931eeffaSKenneth D. Merry 		 */
1403931eeffaSKenneth D. Merry 		if (m_prev != NULL)
140483b92f6eSKip Macy 			m_prev->m_next = m;
1405931eeffaSKenneth D. Merry 
1406931eeffaSKenneth D. Merry 		/*
1407931eeffaSKenneth D. Merry 		 * m0 can be NULL if rx->status < 0 or if * rx->offset +
1408931eeffaSKenneth D. Merry 		 * rx->status > PAGE_SIZE above.
1409931eeffaSKenneth D. Merry 		 */
1410931eeffaSKenneth D. Merry 		if (m0 == NULL)
1411931eeffaSKenneth D. Merry 			m0 = m;
141283b92f6eSKip Macy 		m->m_next = NULL;
1413931eeffaSKenneth D. Merry 		ref = xennet_get_rx_ref(np, *cons + frags);
1414931eeffaSKenneth D. Merry 		ref_cons = *cons + frags;
141589e0f4d2SKip Macy 		frags++;
141689e0f4d2SKip Macy 	}
141783b92f6eSKip Macy 	*list = m0;
141889e0f4d2SKip Macy 
141989e0f4d2SKip Macy 	if (unlikely(frags > max)) {
142089e0f4d2SKip Macy 		if (net_ratelimit())
142189e0f4d2SKip Macy 			WPRINTK("Too many frags\n");
1422931eeffaSKenneth D. Merry 		printf("%s: too many frags %d > max %d\n", __func__, frags,
1423931eeffaSKenneth D. Merry 		       max);
1424931eeffaSKenneth D. Merry 		err = E2BIG;
142589e0f4d2SKip Macy 	}
142689e0f4d2SKip Macy 
1427931eeffaSKenneth D. Merry 	*cons += frags;
142889e0f4d2SKip Macy 
142989e0f4d2SKip Macy 	*pages_flipped_p = pages_flipped;
143089e0f4d2SKip Macy 
143189e0f4d2SKip Macy 	return err;
143289e0f4d2SKip Macy }
143389e0f4d2SKip Macy 
143489e0f4d2SKip Macy static void
143589e0f4d2SKip Macy xn_tick_locked(struct netfront_info *sc)
143689e0f4d2SKip Macy {
143789e0f4d2SKip Macy 	XN_RX_LOCK_ASSERT(sc);
143889e0f4d2SKip Macy 	callout_reset(&sc->xn_stat_ch, hz, xn_tick, sc);
143989e0f4d2SKip Macy 
144089e0f4d2SKip Macy 	/* XXX placeholder for printing debug information */
144189e0f4d2SKip Macy 
144289e0f4d2SKip Macy }
144389e0f4d2SKip Macy 
144489e0f4d2SKip Macy 
144589e0f4d2SKip Macy static void
144689e0f4d2SKip Macy xn_tick(void *xsc)
144789e0f4d2SKip Macy {
144889e0f4d2SKip Macy 	struct netfront_info *sc;
144989e0f4d2SKip Macy 
145089e0f4d2SKip Macy 	sc = xsc;
145189e0f4d2SKip Macy 	XN_RX_LOCK(sc);
145289e0f4d2SKip Macy 	xn_tick_locked(sc);
145389e0f4d2SKip Macy 	XN_RX_UNLOCK(sc);
145489e0f4d2SKip Macy 
145589e0f4d2SKip Macy }
145689e0f4d2SKip Macy 
1457931eeffaSKenneth D. Merry /**
1458931eeffaSKenneth D. Merry  * \brief Count the number of fragments in an mbuf chain.
1459931eeffaSKenneth D. Merry  *
1460931eeffaSKenneth D. Merry  * Surprisingly, there isn't an M* macro for this.
1461c099cafaSAdrian Chadd  */
1462931eeffaSKenneth D. Merry static inline int
1463931eeffaSKenneth D. Merry xn_count_frags(struct mbuf *m)
1464931eeffaSKenneth D. Merry {
1465931eeffaSKenneth D. Merry 	int nfrags;
1466931eeffaSKenneth D. Merry 
1467931eeffaSKenneth D. Merry 	for (nfrags = 0; m != NULL; m = m->m_next)
1468931eeffaSKenneth D. Merry 		nfrags++;
1469931eeffaSKenneth D. Merry 
1470931eeffaSKenneth D. Merry 	return (nfrags);
147189e0f4d2SKip Macy }
147289e0f4d2SKip Macy 
1473931eeffaSKenneth D. Merry /**
1474931eeffaSKenneth D. Merry  * Given an mbuf chain, make sure we have enough room and then push
1475931eeffaSKenneth D. Merry  * it onto the transmit ring.
1476931eeffaSKenneth D. Merry  */
1477931eeffaSKenneth D. Merry static int
1478931eeffaSKenneth D. Merry xn_assemble_tx_request(struct netfront_info *sc, struct mbuf *m_head)
1479931eeffaSKenneth D. Merry {
1480931eeffaSKenneth D. Merry 	struct ifnet *ifp;
1481931eeffaSKenneth D. Merry 	struct mbuf *m;
1482931eeffaSKenneth D. Merry 	u_int nfrags;
1483931eeffaSKenneth D. Merry 	netif_extra_info_t *extra;
1484931eeffaSKenneth D. Merry 	int otherend_id;
1485931eeffaSKenneth D. Merry 
1486931eeffaSKenneth D. Merry 	ifp = sc->xn_ifp;
1487931eeffaSKenneth D. Merry 
1488931eeffaSKenneth D. Merry 	/**
148912678024SDoug Rabson 	 * Defragment the mbuf if necessary.
149012678024SDoug Rabson 	 */
1491931eeffaSKenneth D. Merry 	nfrags = xn_count_frags(m_head);
1492931eeffaSKenneth D. Merry 
1493931eeffaSKenneth D. Merry 	/*
1494931eeffaSKenneth D. Merry 	 * Check to see whether this request is longer than netback
1495931eeffaSKenneth D. Merry 	 * can handle, and try to defrag it.
1496931eeffaSKenneth D. Merry 	 */
1497931eeffaSKenneth D. Merry 	/**
1498931eeffaSKenneth D. Merry 	 * It is a bit lame, but the netback driver in Linux can't
1499931eeffaSKenneth D. Merry 	 * deal with nfrags > MAX_TX_REQ_FRAGS, which is a quirk of
1500931eeffaSKenneth D. Merry 	 * the Linux network stack.
1501931eeffaSKenneth D. Merry 	 */
1502931eeffaSKenneth D. Merry 	if (nfrags > MAX_TX_REQ_FRAGS) {
150312678024SDoug Rabson 		m = m_defrag(m_head, M_DONTWAIT);
150412678024SDoug Rabson 		if (!m) {
1505931eeffaSKenneth D. Merry 			/*
1506931eeffaSKenneth D. Merry 			 * Defrag failed, so free the mbuf and
1507931eeffaSKenneth D. Merry 			 * therefore drop the packet.
1508931eeffaSKenneth D. Merry 			 */
150912678024SDoug Rabson 			m_freem(m_head);
1510931eeffaSKenneth D. Merry 			return (EMSGSIZE);
151112678024SDoug Rabson 		}
151212678024SDoug Rabson 		m_head = m;
151312678024SDoug Rabson 	}
151489e0f4d2SKip Macy 
1515a4ec37f5SAdrian Chadd 	/* Determine how many fragments now exist */
1516931eeffaSKenneth D. Merry 	nfrags = xn_count_frags(m_head);
1517a4ec37f5SAdrian Chadd 
1518a4ec37f5SAdrian Chadd 	/*
1519931eeffaSKenneth D. Merry 	 * Check to see whether the defragmented packet has too many
1520931eeffaSKenneth D. Merry 	 * segments for the Linux netback driver.
1521a4ec37f5SAdrian Chadd 	 */
1522931eeffaSKenneth D. Merry 	/**
1523931eeffaSKenneth D. Merry 	 * The FreeBSD TCP stack, with TSO enabled, can produce a chain
1524931eeffaSKenneth D. Merry 	 * of mbufs longer than Linux can handle.  Make sure we don't
1525931eeffaSKenneth D. Merry 	 * pass a too-long chain over to the other side by dropping the
1526931eeffaSKenneth D. Merry 	 * packet.  It doesn't look like there is currently a way to
1527931eeffaSKenneth D. Merry 	 * tell the TCP stack to generate a shorter chain of packets.
15283fb28bbbSAdrian Chadd 	 */
1529931eeffaSKenneth D. Merry 	if (nfrags > MAX_TX_REQ_FRAGS) {
1530*ff662b5cSJustin T. Gibbs #ifdef DEBUG
1531*ff662b5cSJustin T. Gibbs 		printf("%s: nfrags %d > MAX_TX_REQ_FRAGS %d, netback "
1532*ff662b5cSJustin T. Gibbs 		       "won't be able to handle it, dropping\n",
1533*ff662b5cSJustin T. Gibbs 		       __func__, nfrags, MAX_TX_REQ_FRAGS);
1534*ff662b5cSJustin T. Gibbs #endif
1535931eeffaSKenneth D. Merry 		m_freem(m_head);
1536931eeffaSKenneth D. Merry 		return (EMSGSIZE);
1537a4ec37f5SAdrian Chadd 	}
1538a4ec37f5SAdrian Chadd 
15393fb28bbbSAdrian Chadd 	/*
1540931eeffaSKenneth D. Merry 	 * This check should be redundant.  We've already verified that we
1541931eeffaSKenneth D. Merry 	 * have enough slots in the ring to handle a packet of maximum
1542931eeffaSKenneth D. Merry 	 * size, and that our packet is less than the maximum size.  Keep
1543931eeffaSKenneth D. Merry 	 * it in here as an assert for now just to make certain that
1544931eeffaSKenneth D. Merry 	 * xn_tx_chain_cnt is accurate.
15453fb28bbbSAdrian Chadd 	 */
1546931eeffaSKenneth D. Merry 	KASSERT((sc->xn_cdata.xn_tx_chain_cnt + nfrags) <= NET_TX_RING_SIZE,
1547931eeffaSKenneth D. Merry 		("%s: xn_tx_chain_cnt (%d) + nfrags (%d) > NET_TX_RING_SIZE "
1548931eeffaSKenneth D. Merry 		 "(%d)!", __func__, (int) sc->xn_cdata.xn_tx_chain_cnt,
1549931eeffaSKenneth D. Merry                     (int) nfrags, (int) NET_TX_RING_SIZE));
1550a4ec37f5SAdrian Chadd 
155189e0f4d2SKip Macy 	/*
155289e0f4d2SKip Macy 	 * Start packing the mbufs in this chain into
155389e0f4d2SKip Macy 	 * the fragment pointers. Stop when we run out
155489e0f4d2SKip Macy 	 * of fragments or hit the end of the mbuf chain.
155589e0f4d2SKip Macy 	 */
155612678024SDoug Rabson 	m = m_head;
155712678024SDoug Rabson 	extra = NULL;
1558931eeffaSKenneth D. Merry 	otherend_id = xenbus_get_otherend_id(sc->xbdev);
155912678024SDoug Rabson 	for (m = m_head; m; m = m->m_next) {
1560931eeffaSKenneth D. Merry 		netif_tx_request_t *tx;
1561931eeffaSKenneth D. Merry 		uintptr_t id;
1562931eeffaSKenneth D. Merry 		grant_ref_t ref;
1563931eeffaSKenneth D. Merry 		u_long mfn; /* XXX Wrong type? */
1564931eeffaSKenneth D. Merry 
1565931eeffaSKenneth D. Merry 		tx = RING_GET_REQUEST(&sc->tx, sc->tx.req_prod_pvt);
1566931eeffaSKenneth D. Merry 		id = get_id_from_freelist(sc->tx_mbufs);
1567a4ec37f5SAdrian Chadd 		if (id == 0)
1568a4ec37f5SAdrian Chadd 			panic("xn_start_locked: was allocated the freelist head!\n");
1569a4ec37f5SAdrian Chadd 		sc->xn_cdata.xn_tx_chain_cnt++;
1570931eeffaSKenneth D. Merry 		if (sc->xn_cdata.xn_tx_chain_cnt > NET_TX_RING_SIZE)
1571931eeffaSKenneth D. Merry 			panic("xn_start_locked: tx_chain_cnt must be <= NET_TX_RING_SIZE\n");
1572931eeffaSKenneth D. Merry 		sc->tx_mbufs[id] = m;
157389e0f4d2SKip Macy 		tx->id = id;
157489e0f4d2SKip Macy 		ref = gnttab_claim_grant_reference(&sc->gref_tx_head);
157589e0f4d2SKip Macy 		KASSERT((short)ref >= 0, ("Negative ref"));
157612678024SDoug Rabson 		mfn = virt_to_mfn(mtod(m, vm_offset_t));
157723dc5621SKip Macy 		gnttab_grant_foreign_access_ref(ref, otherend_id,
157889e0f4d2SKip Macy 		    mfn, GNTMAP_readonly);
157989e0f4d2SKip Macy 		tx->gref = sc->grant_tx_ref[id] = ref;
158012678024SDoug Rabson 		tx->offset = mtod(m, vm_offset_t) & (PAGE_SIZE - 1);
158189e0f4d2SKip Macy 		tx->flags = 0;
158212678024SDoug Rabson 		if (m == m_head) {
158312678024SDoug Rabson 			/*
158412678024SDoug Rabson 			 * The first fragment has the entire packet
158512678024SDoug Rabson 			 * size, subsequent fragments have just the
158612678024SDoug Rabson 			 * fragment size. The backend works out the
158712678024SDoug Rabson 			 * true size of the first fragment by
158812678024SDoug Rabson 			 * subtracting the sizes of the other
158912678024SDoug Rabson 			 * fragments.
159012678024SDoug Rabson 			 */
159112678024SDoug Rabson 			tx->size = m->m_pkthdr.len;
159289e0f4d2SKip Macy 
159312678024SDoug Rabson 			/*
1594931eeffaSKenneth D. Merry 			 * The first fragment contains the checksum flags
1595931eeffaSKenneth D. Merry 			 * and is optionally followed by extra data for
1596931eeffaSKenneth D. Merry 			 * TSO etc.
1597931eeffaSKenneth D. Merry 			 */
1598931eeffaSKenneth D. Merry 			/**
1599931eeffaSKenneth D. Merry 			 * CSUM_TSO requires checksum offloading.
1600931eeffaSKenneth D. Merry 			 * Some versions of FreeBSD fail to
1601931eeffaSKenneth D. Merry 			 * set CSUM_TCP in the CSUM_TSO case,
1602931eeffaSKenneth D. Merry 			 * so we have to test for CSUM_TSO
1603931eeffaSKenneth D. Merry 			 * explicitly.
160412678024SDoug Rabson 			 */
160512678024SDoug Rabson 			if (m->m_pkthdr.csum_flags
1606931eeffaSKenneth D. Merry 			    & (CSUM_DELAY_DATA | CSUM_TSO)) {
160712678024SDoug Rabson 				tx->flags |= (NETTXF_csum_blank
160812678024SDoug Rabson 				    | NETTXF_data_validated);
160912678024SDoug Rabson 			}
161012678024SDoug Rabson #if __FreeBSD_version >= 700000
161112678024SDoug Rabson 			if (m->m_pkthdr.csum_flags & CSUM_TSO) {
161212678024SDoug Rabson 				struct netif_extra_info *gso =
161312678024SDoug Rabson 					(struct netif_extra_info *)
1614931eeffaSKenneth D. Merry 					RING_GET_REQUEST(&sc->tx,
1615931eeffaSKenneth D. Merry 							 ++sc->tx.req_prod_pvt);
161689e0f4d2SKip Macy 
161712678024SDoug Rabson 				tx->flags |= NETTXF_extra_info;
161889e0f4d2SKip Macy 
161912678024SDoug Rabson 				gso->u.gso.size = m->m_pkthdr.tso_segsz;
162012678024SDoug Rabson 				gso->u.gso.type =
162112678024SDoug Rabson 					XEN_NETIF_GSO_TYPE_TCPV4;
162212678024SDoug Rabson 				gso->u.gso.pad = 0;
162312678024SDoug Rabson 				gso->u.gso.features = 0;
162412678024SDoug Rabson 
162512678024SDoug Rabson 				gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
162612678024SDoug Rabson 				gso->flags = 0;
162712678024SDoug Rabson 			}
162812678024SDoug Rabson #endif
162912678024SDoug Rabson 		} else {
163012678024SDoug Rabson 			tx->size = m->m_len;
163112678024SDoug Rabson 		}
1632931eeffaSKenneth D. Merry 		if (m->m_next)
163312678024SDoug Rabson 			tx->flags |= NETTXF_more_data;
163412678024SDoug Rabson 
1635931eeffaSKenneth D. Merry 		sc->tx.req_prod_pvt++;
1636931eeffaSKenneth D. Merry 	}
163712678024SDoug Rabson 	BPF_MTAP(ifp, m_head);
163812678024SDoug Rabson 
163912678024SDoug Rabson 	sc->stats.tx_bytes += m_head->m_pkthdr.len;
164089e0f4d2SKip Macy 	sc->stats.tx_packets++;
1641931eeffaSKenneth D. Merry 
1642931eeffaSKenneth D. Merry 	return (0);
164389e0f4d2SKip Macy }
164489e0f4d2SKip Macy 
1645931eeffaSKenneth D. Merry static void
1646931eeffaSKenneth D. Merry xn_start_locked(struct ifnet *ifp)
1647931eeffaSKenneth D. Merry {
1648931eeffaSKenneth D. Merry 	struct netfront_info *sc;
1649931eeffaSKenneth D. Merry 	struct mbuf *m_head;
1650931eeffaSKenneth D. Merry 	int notify;
1651931eeffaSKenneth D. Merry 
1652931eeffaSKenneth D. Merry 	sc = ifp->if_softc;
1653931eeffaSKenneth D. Merry 
1654931eeffaSKenneth D. Merry 	if (!netfront_carrier_ok(sc))
1655931eeffaSKenneth D. Merry 		return;
1656931eeffaSKenneth D. Merry 
1657931eeffaSKenneth D. Merry 	/*
1658931eeffaSKenneth D. Merry 	 * While we have enough transmit slots available for at least one
1659931eeffaSKenneth D. Merry 	 * maximum-sized packet, pull mbufs off the queue and put them on
1660931eeffaSKenneth D. Merry 	 * the transmit ring.
1661931eeffaSKenneth D. Merry 	 */
1662931eeffaSKenneth D. Merry 	while (xn_tx_slot_available(sc)) {
1663931eeffaSKenneth D. Merry 		IF_DEQUEUE(&ifp->if_snd, m_head);
1664931eeffaSKenneth D. Merry 		if (m_head == NULL)
1665931eeffaSKenneth D. Merry 			break;
1666931eeffaSKenneth D. Merry 
1667931eeffaSKenneth D. Merry 		if (xn_assemble_tx_request(sc, m_head) != 0)
1668931eeffaSKenneth D. Merry 			break;
1669931eeffaSKenneth D. Merry 	}
1670931eeffaSKenneth D. Merry 
167189e0f4d2SKip Macy 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->tx, notify);
167289e0f4d2SKip Macy 	if (notify)
167389e0f4d2SKip Macy 		notify_remote_via_irq(sc->irq);
167489e0f4d2SKip Macy 
167589e0f4d2SKip Macy 	if (RING_FULL(&sc->tx)) {
167689e0f4d2SKip Macy 		sc->tx_full = 1;
167789e0f4d2SKip Macy #if 0
167889e0f4d2SKip Macy 		netif_stop_queue(dev);
167989e0f4d2SKip Macy #endif
168089e0f4d2SKip Macy 	}
168189e0f4d2SKip Macy }
168289e0f4d2SKip Macy 
1683931eeffaSKenneth D. Merry 
168489e0f4d2SKip Macy static void
168589e0f4d2SKip Macy xn_start(struct ifnet *ifp)
168689e0f4d2SKip Macy {
168789e0f4d2SKip Macy 	struct netfront_info *sc;
168889e0f4d2SKip Macy 	sc = ifp->if_softc;
168989e0f4d2SKip Macy 	XN_TX_LOCK(sc);
169089e0f4d2SKip Macy 	xn_start_locked(ifp);
169189e0f4d2SKip Macy 	XN_TX_UNLOCK(sc);
169289e0f4d2SKip Macy }
169389e0f4d2SKip Macy 
169489e0f4d2SKip Macy /* equivalent of network_open() in Linux */
169589e0f4d2SKip Macy static void
169689e0f4d2SKip Macy xn_ifinit_locked(struct netfront_info *sc)
169789e0f4d2SKip Macy {
169889e0f4d2SKip Macy 	struct ifnet *ifp;
169989e0f4d2SKip Macy 
170089e0f4d2SKip Macy 	XN_LOCK_ASSERT(sc);
170189e0f4d2SKip Macy 
170289e0f4d2SKip Macy 	ifp = sc->xn_ifp;
170389e0f4d2SKip Macy 
170489e0f4d2SKip Macy 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
170589e0f4d2SKip Macy 		return;
170689e0f4d2SKip Macy 
170789e0f4d2SKip Macy 	xn_stop(sc);
170889e0f4d2SKip Macy 
170989e0f4d2SKip Macy 	network_alloc_rx_buffers(sc);
171089e0f4d2SKip Macy 	sc->rx.sring->rsp_event = sc->rx.rsp_cons + 1;
171189e0f4d2SKip Macy 
171289e0f4d2SKip Macy 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
171389e0f4d2SKip Macy 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
17140e509842SJustin T. Gibbs 	if_link_state_change(ifp, LINK_STATE_UP);
171589e0f4d2SKip Macy 
171689e0f4d2SKip Macy 	callout_reset(&sc->xn_stat_ch, hz, xn_tick, sc);
171789e0f4d2SKip Macy 
171889e0f4d2SKip Macy }
171989e0f4d2SKip Macy 
172089e0f4d2SKip Macy 
172189e0f4d2SKip Macy static void
172289e0f4d2SKip Macy xn_ifinit(void *xsc)
172389e0f4d2SKip Macy {
172489e0f4d2SKip Macy 	struct netfront_info *sc = xsc;
172589e0f4d2SKip Macy 
172689e0f4d2SKip Macy 	XN_LOCK(sc);
172789e0f4d2SKip Macy 	xn_ifinit_locked(sc);
172889e0f4d2SKip Macy 	XN_UNLOCK(sc);
172989e0f4d2SKip Macy 
173089e0f4d2SKip Macy }
173189e0f4d2SKip Macy 
173289e0f4d2SKip Macy 
173389e0f4d2SKip Macy static int
173489e0f4d2SKip Macy xn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
173589e0f4d2SKip Macy {
173689e0f4d2SKip Macy 	struct netfront_info *sc = ifp->if_softc;
173789e0f4d2SKip Macy 	struct ifreq *ifr = (struct ifreq *) data;
173889e0f4d2SKip Macy 	struct ifaddr *ifa = (struct ifaddr *)data;
173989e0f4d2SKip Macy 
174089e0f4d2SKip Macy 	int mask, error = 0;
174189e0f4d2SKip Macy 	switch(cmd) {
174289e0f4d2SKip Macy 	case SIOCSIFADDR:
174389e0f4d2SKip Macy 	case SIOCGIFADDR:
174489e0f4d2SKip Macy 		XN_LOCK(sc);
174589e0f4d2SKip Macy 		if (ifa->ifa_addr->sa_family == AF_INET) {
174689e0f4d2SKip Macy 			ifp->if_flags |= IFF_UP;
174789e0f4d2SKip Macy 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
174889e0f4d2SKip Macy 				xn_ifinit_locked(sc);
174989e0f4d2SKip Macy 			arp_ifinit(ifp, ifa);
175089e0f4d2SKip Macy 			XN_UNLOCK(sc);
175149906218SDoug Rabson 		} else {
175249906218SDoug Rabson 			XN_UNLOCK(sc);
175349906218SDoug Rabson 			error = ether_ioctl(ifp, cmd, data);
175449906218SDoug Rabson 		}
175589e0f4d2SKip Macy 		break;
175689e0f4d2SKip Macy 	case SIOCSIFMTU:
175789e0f4d2SKip Macy 		/* XXX can we alter the MTU on a VN ?*/
175889e0f4d2SKip Macy #ifdef notyet
175989e0f4d2SKip Macy 		if (ifr->ifr_mtu > XN_JUMBO_MTU)
176089e0f4d2SKip Macy 			error = EINVAL;
176189e0f4d2SKip Macy 		else
176289e0f4d2SKip Macy #endif
176389e0f4d2SKip Macy 		{
176489e0f4d2SKip Macy 			ifp->if_mtu = ifr->ifr_mtu;
176589e0f4d2SKip Macy 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
176689e0f4d2SKip Macy 			xn_ifinit(sc);
176789e0f4d2SKip Macy 		}
176889e0f4d2SKip Macy 		break;
176989e0f4d2SKip Macy 	case SIOCSIFFLAGS:
177089e0f4d2SKip Macy 		XN_LOCK(sc);
177189e0f4d2SKip Macy 		if (ifp->if_flags & IFF_UP) {
177289e0f4d2SKip Macy 			/*
177389e0f4d2SKip Macy 			 * If only the state of the PROMISC flag changed,
177489e0f4d2SKip Macy 			 * then just use the 'set promisc mode' command
177589e0f4d2SKip Macy 			 * instead of reinitializing the entire NIC. Doing
177689e0f4d2SKip Macy 			 * a full re-init means reloading the firmware and
177789e0f4d2SKip Macy 			 * waiting for it to start up, which may take a
177889e0f4d2SKip Macy 			 * second or two.
177989e0f4d2SKip Macy 			 */
178089e0f4d2SKip Macy #ifdef notyet
178189e0f4d2SKip Macy 			/* No promiscuous mode with Xen */
178289e0f4d2SKip Macy 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
178389e0f4d2SKip Macy 			    ifp->if_flags & IFF_PROMISC &&
178489e0f4d2SKip Macy 			    !(sc->xn_if_flags & IFF_PROMISC)) {
178589e0f4d2SKip Macy 				XN_SETBIT(sc, XN_RX_MODE,
178689e0f4d2SKip Macy 					  XN_RXMODE_RX_PROMISC);
178789e0f4d2SKip Macy 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
178889e0f4d2SKip Macy 				   !(ifp->if_flags & IFF_PROMISC) &&
178989e0f4d2SKip Macy 				   sc->xn_if_flags & IFF_PROMISC) {
179089e0f4d2SKip Macy 				XN_CLRBIT(sc, XN_RX_MODE,
179189e0f4d2SKip Macy 					  XN_RXMODE_RX_PROMISC);
179289e0f4d2SKip Macy 			} else
179389e0f4d2SKip Macy #endif
179489e0f4d2SKip Macy 				xn_ifinit_locked(sc);
179589e0f4d2SKip Macy 		} else {
179689e0f4d2SKip Macy 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
179789e0f4d2SKip Macy 				xn_stop(sc);
179889e0f4d2SKip Macy 			}
179989e0f4d2SKip Macy 		}
180089e0f4d2SKip Macy 		sc->xn_if_flags = ifp->if_flags;
180189e0f4d2SKip Macy 		XN_UNLOCK(sc);
180289e0f4d2SKip Macy 		error = 0;
180389e0f4d2SKip Macy 		break;
180489e0f4d2SKip Macy 	case SIOCSIFCAP:
180589e0f4d2SKip Macy 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
180612678024SDoug Rabson 		if (mask & IFCAP_TXCSUM) {
180712678024SDoug Rabson 			if (IFCAP_TXCSUM & ifp->if_capenable) {
180812678024SDoug Rabson 				ifp->if_capenable &= ~(IFCAP_TXCSUM|IFCAP_TSO4);
180912678024SDoug Rabson 				ifp->if_hwassist &= ~(CSUM_TCP | CSUM_UDP
181012678024SDoug Rabson 				    | CSUM_IP | CSUM_TSO);
181112678024SDoug Rabson 			} else {
181212678024SDoug Rabson 				ifp->if_capenable |= IFCAP_TXCSUM;
181312678024SDoug Rabson 				ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP
181412678024SDoug Rabson 				    | CSUM_IP);
181589e0f4d2SKip Macy 			}
181612678024SDoug Rabson 		}
181712678024SDoug Rabson 		if (mask & IFCAP_RXCSUM) {
181812678024SDoug Rabson 			ifp->if_capenable ^= IFCAP_RXCSUM;
181912678024SDoug Rabson 		}
182012678024SDoug Rabson #if __FreeBSD_version >= 700000
182112678024SDoug Rabson 		if (mask & IFCAP_TSO4) {
182212678024SDoug Rabson 			if (IFCAP_TSO4 & ifp->if_capenable) {
182312678024SDoug Rabson 				ifp->if_capenable &= ~IFCAP_TSO4;
182412678024SDoug Rabson 				ifp->if_hwassist &= ~CSUM_TSO;
182512678024SDoug Rabson 			} else if (IFCAP_TXCSUM & ifp->if_capenable) {
182612678024SDoug Rabson 				ifp->if_capenable |= IFCAP_TSO4;
182712678024SDoug Rabson 				ifp->if_hwassist |= CSUM_TSO;
182812678024SDoug Rabson 			} else {
18293552092bSAdrian Chadd 				IPRINTK("Xen requires tx checksum offload"
183012678024SDoug Rabson 				    " be enabled to use TSO\n");
183112678024SDoug Rabson 				error = EINVAL;
183212678024SDoug Rabson 			}
183312678024SDoug Rabson 		}
183412678024SDoug Rabson 		if (mask & IFCAP_LRO) {
183512678024SDoug Rabson 			ifp->if_capenable ^= IFCAP_LRO;
183612678024SDoug Rabson 
183712678024SDoug Rabson 		}
183812678024SDoug Rabson #endif
183989e0f4d2SKip Macy 		error = 0;
184089e0f4d2SKip Macy 		break;
184189e0f4d2SKip Macy 	case SIOCADDMULTI:
184289e0f4d2SKip Macy 	case SIOCDELMULTI:
184389e0f4d2SKip Macy #ifdef notyet
184489e0f4d2SKip Macy 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
184589e0f4d2SKip Macy 			XN_LOCK(sc);
184689e0f4d2SKip Macy 			xn_setmulti(sc);
184789e0f4d2SKip Macy 			XN_UNLOCK(sc);
184889e0f4d2SKip Macy 			error = 0;
184989e0f4d2SKip Macy 		}
185089e0f4d2SKip Macy #endif
185189e0f4d2SKip Macy 		/* FALLTHROUGH */
185289e0f4d2SKip Macy 	case SIOCSIFMEDIA:
185389e0f4d2SKip Macy 	case SIOCGIFMEDIA:
18540e509842SJustin T. Gibbs 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
185589e0f4d2SKip Macy 		break;
185689e0f4d2SKip Macy 	default:
185789e0f4d2SKip Macy 		error = ether_ioctl(ifp, cmd, data);
185889e0f4d2SKip Macy 	}
185989e0f4d2SKip Macy 
186089e0f4d2SKip Macy 	return (error);
186189e0f4d2SKip Macy }
186289e0f4d2SKip Macy 
186389e0f4d2SKip Macy static void
186489e0f4d2SKip Macy xn_stop(struct netfront_info *sc)
186589e0f4d2SKip Macy {
186689e0f4d2SKip Macy 	struct ifnet *ifp;
186789e0f4d2SKip Macy 
186889e0f4d2SKip Macy 	XN_LOCK_ASSERT(sc);
186989e0f4d2SKip Macy 
187089e0f4d2SKip Macy 	ifp = sc->xn_ifp;
187189e0f4d2SKip Macy 
187289e0f4d2SKip Macy 	callout_stop(&sc->xn_stat_ch);
187389e0f4d2SKip Macy 
187489e0f4d2SKip Macy 	xn_free_rx_ring(sc);
187589e0f4d2SKip Macy 	xn_free_tx_ring(sc);
187689e0f4d2SKip Macy 
187789e0f4d2SKip Macy 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
18780e509842SJustin T. Gibbs 	if_link_state_change(ifp, LINK_STATE_DOWN);
187989e0f4d2SKip Macy }
188089e0f4d2SKip Macy 
188189e0f4d2SKip Macy /* START of Xenolinux helper functions adapted to FreeBSD */
188223dc5621SKip Macy int
188323dc5621SKip Macy network_connect(struct netfront_info *np)
188489e0f4d2SKip Macy {
18853a6d1fcfSKip Macy 	int i, requeue_idx, error;
188689e0f4d2SKip Macy 	grant_ref_t ref;
188789e0f4d2SKip Macy 	netif_rx_request_t *req;
188889e0f4d2SKip Macy 	u_int feature_rx_copy, feature_rx_flip;
188989e0f4d2SKip Macy 
1890*ff662b5cSJustin T. Gibbs 	error = xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev),
18913a6d1fcfSKip Macy 	    "feature-rx-copy", NULL, "%u", &feature_rx_copy);
18923a6d1fcfSKip Macy 	if (error)
189389e0f4d2SKip Macy 		feature_rx_copy = 0;
1894*ff662b5cSJustin T. Gibbs 	error = xs_scanf(XST_NIL, xenbus_get_otherend_path(np->xbdev),
18953a6d1fcfSKip Macy 	    "feature-rx-flip", NULL, "%u", &feature_rx_flip);
18963a6d1fcfSKip Macy 	if (error)
189789e0f4d2SKip Macy 		feature_rx_flip = 1;
189889e0f4d2SKip Macy 
189989e0f4d2SKip Macy 	/*
190089e0f4d2SKip Macy 	 * Copy packets on receive path if:
190189e0f4d2SKip Macy 	 *  (a) This was requested by user, and the backend supports it; or
190289e0f4d2SKip Macy 	 *  (b) Flipping was requested, but this is unsupported by the backend.
190389e0f4d2SKip Macy 	 */
190489e0f4d2SKip Macy 	np->copying_receiver = ((MODPARM_rx_copy && feature_rx_copy) ||
190589e0f4d2SKip Macy 				(MODPARM_rx_flip && !feature_rx_flip));
190689e0f4d2SKip Macy 
190789e0f4d2SKip Macy 	/* Recovery procedure: */
19083a6d1fcfSKip Macy 	error = talk_to_backend(np->xbdev, np);
19093a6d1fcfSKip Macy 	if (error)
19103a6d1fcfSKip Macy 		return (error);
191189e0f4d2SKip Macy 
191289e0f4d2SKip Macy 	/* Step 1: Reinitialise variables. */
191389e0f4d2SKip Macy 	netif_release_tx_bufs(np);
191489e0f4d2SKip Macy 
191589e0f4d2SKip Macy 	/* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
191689e0f4d2SKip Macy 	for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
191789e0f4d2SKip Macy 		struct mbuf *m;
19183a6d1fcfSKip Macy 		u_long pfn;
191989e0f4d2SKip Macy 
192089e0f4d2SKip Macy 		if (np->rx_mbufs[i] == NULL)
192189e0f4d2SKip Macy 			continue;
192289e0f4d2SKip Macy 
192389e0f4d2SKip Macy 		m = np->rx_mbufs[requeue_idx] = xennet_get_rx_mbuf(np, i);
192489e0f4d2SKip Macy 		ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1925931eeffaSKenneth D. Merry 
192689e0f4d2SKip Macy 		req = RING_GET_REQUEST(&np->rx, requeue_idx);
19273a6d1fcfSKip Macy 		pfn = vtophys(mtod(m, vm_offset_t)) >> PAGE_SHIFT;
192889e0f4d2SKip Macy 
192989e0f4d2SKip Macy 		if (!np->copying_receiver) {
193089e0f4d2SKip Macy 			gnttab_grant_foreign_transfer_ref(ref,
193123dc5621SKip Macy 			    xenbus_get_otherend_id(np->xbdev),
19323a6d1fcfSKip Macy 			    pfn);
193389e0f4d2SKip Macy 		} else {
193489e0f4d2SKip Macy 			gnttab_grant_foreign_access_ref(ref,
193523dc5621SKip Macy 			    xenbus_get_otherend_id(np->xbdev),
19363a6d1fcfSKip Macy 			    PFNTOMFN(pfn), 0);
193789e0f4d2SKip Macy 		}
193889e0f4d2SKip Macy 		req->gref = ref;
193989e0f4d2SKip Macy 		req->id   = requeue_idx;
194089e0f4d2SKip Macy 
194189e0f4d2SKip Macy 		requeue_idx++;
194289e0f4d2SKip Macy 	}
194389e0f4d2SKip Macy 
194489e0f4d2SKip Macy 	np->rx.req_prod_pvt = requeue_idx;
194589e0f4d2SKip Macy 
194689e0f4d2SKip Macy 	/* Step 3: All public and private state should now be sane.  Get
194789e0f4d2SKip Macy 	 * ready to start sending and receiving packets and give the driver
194889e0f4d2SKip Macy 	 * domain a kick because we've probably just requeued some
194989e0f4d2SKip Macy 	 * packets.
195089e0f4d2SKip Macy 	 */
195189e0f4d2SKip Macy 	netfront_carrier_on(np);
195289e0f4d2SKip Macy 	notify_remote_via_irq(np->irq);
195389e0f4d2SKip Macy 	XN_TX_LOCK(np);
195489e0f4d2SKip Macy 	xn_txeof(np);
195589e0f4d2SKip Macy 	XN_TX_UNLOCK(np);
195689e0f4d2SKip Macy 	network_alloc_rx_buffers(np);
195789e0f4d2SKip Macy 
195889e0f4d2SKip Macy 	return (0);
195989e0f4d2SKip Macy }
196089e0f4d2SKip Macy 
196189e0f4d2SKip Macy static void
196289e0f4d2SKip Macy show_device(struct netfront_info *sc)
196389e0f4d2SKip Macy {
196489e0f4d2SKip Macy #ifdef DEBUG
196589e0f4d2SKip Macy 	if (sc) {
196689e0f4d2SKip Macy 		IPRINTK("<vif handle=%u %s(%s) evtchn=%u irq=%u tx=%p rx=%p>\n",
196789e0f4d2SKip Macy 			sc->xn_ifno,
196889e0f4d2SKip Macy 			be_state_name[sc->xn_backend_state],
196989e0f4d2SKip Macy 			sc->xn_user_state ? "open" : "closed",
197089e0f4d2SKip Macy 			sc->xn_evtchn,
197189e0f4d2SKip Macy 			sc->xn_irq,
197289e0f4d2SKip Macy 			sc->xn_tx_if,
197389e0f4d2SKip Macy 			sc->xn_rx_if);
197489e0f4d2SKip Macy 	} else {
197589e0f4d2SKip Macy 		IPRINTK("<vif NULL>\n");
197689e0f4d2SKip Macy 	}
197789e0f4d2SKip Macy #endif
197889e0f4d2SKip Macy }
197989e0f4d2SKip Macy 
198089e0f4d2SKip Macy /** Create a network device.
198189e0f4d2SKip Macy  * @param handle device handle
198289e0f4d2SKip Macy  */
198323dc5621SKip Macy int
198423dc5621SKip Macy create_netdev(device_t dev)
198589e0f4d2SKip Macy {
198689e0f4d2SKip Macy 	int i;
198789e0f4d2SKip Macy 	struct netfront_info *np;
198889e0f4d2SKip Macy 	int err;
198989e0f4d2SKip Macy 	struct ifnet *ifp;
199089e0f4d2SKip Macy 
199123dc5621SKip Macy 	np = device_get_softc(dev);
199289e0f4d2SKip Macy 
199389e0f4d2SKip Macy 	np->xbdev         = dev;
199489e0f4d2SKip Macy 
199589e0f4d2SKip Macy 	XN_LOCK_INIT(np, xennetif);
19960e509842SJustin T. Gibbs 
19970e509842SJustin T. Gibbs 	ifmedia_init(&np->sc_media, 0, xn_ifmedia_upd, xn_ifmedia_sts);
19980e509842SJustin T. Gibbs 	ifmedia_add(&np->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
19990e509842SJustin T. Gibbs 	ifmedia_set(&np->sc_media, IFM_ETHER|IFM_MANUAL);
20000e509842SJustin T. Gibbs 
200189e0f4d2SKip Macy 	np->rx_target     = RX_MIN_TARGET;
200289e0f4d2SKip Macy 	np->rx_min_target = RX_MIN_TARGET;
200389e0f4d2SKip Macy 	np->rx_max_target = RX_MAX_TARGET;
200489e0f4d2SKip Macy 
200589e0f4d2SKip Macy 	/* Initialise {tx,rx}_skbs to be a free chain containing every entry. */
200689e0f4d2SKip Macy 	for (i = 0; i <= NET_TX_RING_SIZE; i++) {
200789e0f4d2SKip Macy 		np->tx_mbufs[i] = (void *) ((u_long) i+1);
2008*ff662b5cSJustin T. Gibbs 		np->grant_tx_ref[i] = GRANT_REF_INVALID;
200989e0f4d2SKip Macy 	}
2010931eeffaSKenneth D. Merry 	np->tx_mbufs[NET_TX_RING_SIZE] = (void *)0;
2011931eeffaSKenneth D. Merry 
201289e0f4d2SKip Macy 	for (i = 0; i <= NET_RX_RING_SIZE; i++) {
2013931eeffaSKenneth D. Merry 
201489e0f4d2SKip Macy 		np->rx_mbufs[i] = NULL;
2015*ff662b5cSJustin T. Gibbs 		np->grant_rx_ref[i] = GRANT_REF_INVALID;
201689e0f4d2SKip Macy 	}
201789e0f4d2SKip Macy 	/* A grant for every tx ring slot */
2018931eeffaSKenneth D. Merry 	if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
2019931eeffaSKenneth D. Merry 					  &np->gref_tx_head) != 0) {
2020227ca257SKip Macy 		IPRINTK("#### netfront can't alloc tx grant refs\n");
202189e0f4d2SKip Macy 		err = ENOMEM;
202289e0f4d2SKip Macy 		goto exit;
202389e0f4d2SKip Macy 	}
202489e0f4d2SKip Macy 	/* A grant for every rx ring slot */
202589e0f4d2SKip Macy 	if (gnttab_alloc_grant_references(RX_MAX_TARGET,
2026931eeffaSKenneth D. Merry 					  &np->gref_rx_head) != 0) {
2027227ca257SKip Macy 		WPRINTK("#### netfront can't alloc rx grant refs\n");
202889e0f4d2SKip Macy 		gnttab_free_grant_references(np->gref_tx_head);
202989e0f4d2SKip Macy 		err = ENOMEM;
203089e0f4d2SKip Macy 		goto exit;
203189e0f4d2SKip Macy 	}
203289e0f4d2SKip Macy 
203389e0f4d2SKip Macy 	err = xen_net_read_mac(dev, np->mac);
203489e0f4d2SKip Macy 	if (err) {
203523dc5621SKip Macy 		xenbus_dev_fatal(dev, err, "parsing %s/mac",
203623dc5621SKip Macy 		    xenbus_get_node(dev));
203789e0f4d2SKip Macy 		goto out;
203889e0f4d2SKip Macy 	}
203989e0f4d2SKip Macy 
204089e0f4d2SKip Macy 	/* Set up ifnet structure */
204123dc5621SKip Macy 	ifp = np->xn_ifp = if_alloc(IFT_ETHER);
204289e0f4d2SKip Macy     	ifp->if_softc = np;
204323dc5621SKip Macy     	if_initname(ifp, "xn",  device_get_unit(dev));
20443a6d1fcfSKip Macy     	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
204589e0f4d2SKip Macy     	ifp->if_ioctl = xn_ioctl;
204689e0f4d2SKip Macy     	ifp->if_output = ether_output;
204789e0f4d2SKip Macy     	ifp->if_start = xn_start;
2048227ca257SKip Macy #ifdef notyet
2049227ca257SKip Macy     	ifp->if_watchdog = xn_watchdog;
2050227ca257SKip Macy #endif
205189e0f4d2SKip Macy     	ifp->if_init = xn_ifinit;
205289e0f4d2SKip Macy     	ifp->if_mtu = ETHERMTU;
205389e0f4d2SKip Macy     	ifp->if_snd.ifq_maxlen = NET_TX_RING_SIZE - 1;
205489e0f4d2SKip Macy 
205589e0f4d2SKip Macy     	ifp->if_hwassist = XN_CSUM_FEATURES;
205689e0f4d2SKip Macy     	ifp->if_capabilities = IFCAP_HWCSUM;
205712678024SDoug Rabson #if __FreeBSD_version >= 700000
2058931eeffaSKenneth D. Merry 	ifp->if_capabilities |= IFCAP_TSO4;
205912678024SDoug Rabson 	if (xn_enable_lro) {
206012678024SDoug Rabson 		int err = tcp_lro_init(&np->xn_lro);
206112678024SDoug Rabson 		if (err) {
206212678024SDoug Rabson 			device_printf(dev, "LRO initialization failed\n");
206312678024SDoug Rabson 			goto exit;
206412678024SDoug Rabson 		}
206512678024SDoug Rabson 		np->xn_lro.ifp = ifp;
206612678024SDoug Rabson 		ifp->if_capabilities |= IFCAP_LRO;
206712678024SDoug Rabson 	}
206889e0f4d2SKip Macy #endif
206912678024SDoug Rabson     	ifp->if_capenable = ifp->if_capabilities;
207089e0f4d2SKip Macy 
207189e0f4d2SKip Macy     	ether_ifattach(ifp, np->mac);
207289e0f4d2SKip Macy     	callout_init(&np->xn_stat_ch, CALLOUT_MPSAFE);
207389e0f4d2SKip Macy 	netfront_carrier_off(np);
207489e0f4d2SKip Macy 
207589e0f4d2SKip Macy 	return (0);
207689e0f4d2SKip Macy 
207789e0f4d2SKip Macy exit:
207889e0f4d2SKip Macy 	gnttab_free_grant_references(np->gref_tx_head);
207989e0f4d2SKip Macy out:
208089e0f4d2SKip Macy 	panic("do something smart");
208189e0f4d2SKip Macy 
208289e0f4d2SKip Macy }
208389e0f4d2SKip Macy 
208489e0f4d2SKip Macy /**
208589e0f4d2SKip Macy  * Handle the change of state of the backend to Closing.  We must delete our
208689e0f4d2SKip Macy  * device-layer structures now, to ensure that writes are flushed through to
208789e0f4d2SKip Macy  * the backend.  Once is this done, we can switch to Closed in
208889e0f4d2SKip Macy  * acknowledgement.
208989e0f4d2SKip Macy  */
209089e0f4d2SKip Macy #if 0
20910e509842SJustin T. Gibbs static void
20920e509842SJustin T. Gibbs netfront_closing(device_t dev)
209389e0f4d2SKip Macy {
209489e0f4d2SKip Macy #if 0
209589e0f4d2SKip Macy 	struct netfront_info *info = dev->dev_driver_data;
209689e0f4d2SKip Macy 
209789e0f4d2SKip Macy 	DPRINTK("netfront_closing: %s removed\n", dev->nodename);
209889e0f4d2SKip Macy 
209989e0f4d2SKip Macy 	close_netdev(info);
210089e0f4d2SKip Macy #endif
210189e0f4d2SKip Macy 	xenbus_switch_state(dev, XenbusStateClosed);
210289e0f4d2SKip Macy }
210389e0f4d2SKip Macy #endif
210489e0f4d2SKip Macy 
21050e509842SJustin T. Gibbs static int
21060e509842SJustin T. Gibbs netfront_detach(device_t dev)
210789e0f4d2SKip Macy {
210823dc5621SKip Macy 	struct netfront_info *info = device_get_softc(dev);
210989e0f4d2SKip Macy 
211023dc5621SKip Macy 	DPRINTK("%s\n", xenbus_get_node(dev));
211189e0f4d2SKip Macy 
211289e0f4d2SKip Macy 	netif_free(info);
211389e0f4d2SKip Macy 
211489e0f4d2SKip Macy 	return 0;
211589e0f4d2SKip Macy }
211689e0f4d2SKip Macy 
21170e509842SJustin T. Gibbs static void
21180e509842SJustin T. Gibbs netif_free(struct netfront_info *info)
211989e0f4d2SKip Macy {
212089e0f4d2SKip Macy 	netif_disconnect_backend(info);
212189e0f4d2SKip Macy #if 0
212289e0f4d2SKip Macy 	close_netdev(info);
212389e0f4d2SKip Macy #endif
212489e0f4d2SKip Macy }
212589e0f4d2SKip Macy 
21260e509842SJustin T. Gibbs static void
21270e509842SJustin T. Gibbs netif_disconnect_backend(struct netfront_info *info)
212889e0f4d2SKip Macy {
21293a6d1fcfSKip Macy 	XN_RX_LOCK(info);
21303a6d1fcfSKip Macy 	XN_TX_LOCK(info);
21313a6d1fcfSKip Macy 	netfront_carrier_off(info);
21323a6d1fcfSKip Macy 	XN_TX_UNLOCK(info);
21333a6d1fcfSKip Macy 	XN_RX_UNLOCK(info);
21343a6d1fcfSKip Macy 
213589e0f4d2SKip Macy 	end_access(info->tx_ring_ref, info->tx.sring);
213689e0f4d2SKip Macy 	end_access(info->rx_ring_ref, info->rx.sring);
2137*ff662b5cSJustin T. Gibbs 	info->tx_ring_ref = GRANT_REF_INVALID;
2138*ff662b5cSJustin T. Gibbs 	info->rx_ring_ref = GRANT_REF_INVALID;
213989e0f4d2SKip Macy 	info->tx.sring = NULL;
214089e0f4d2SKip Macy 	info->rx.sring = NULL;
214189e0f4d2SKip Macy 
214289e0f4d2SKip Macy 	if (info->irq)
21433a6d1fcfSKip Macy 		unbind_from_irqhandler(info->irq);
21443a6d1fcfSKip Macy 
214589e0f4d2SKip Macy 	info->irq = 0;
214689e0f4d2SKip Macy }
214789e0f4d2SKip Macy 
214889e0f4d2SKip Macy 
21490e509842SJustin T. Gibbs static void
21500e509842SJustin T. Gibbs end_access(int ref, void *page)
215189e0f4d2SKip Macy {
2152*ff662b5cSJustin T. Gibbs 	if (ref != GRANT_REF_INVALID)
2153920ba15bSKip Macy 		gnttab_end_foreign_access(ref, page);
215489e0f4d2SKip Macy }
215589e0f4d2SKip Macy 
21560e509842SJustin T. Gibbs static int
21570e509842SJustin T. Gibbs xn_ifmedia_upd(struct ifnet *ifp)
21580e509842SJustin T. Gibbs {
21590e509842SJustin T. Gibbs 	return (0);
21600e509842SJustin T. Gibbs }
21610e509842SJustin T. Gibbs 
21620e509842SJustin T. Gibbs static void
21630e509842SJustin T. Gibbs xn_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
21640e509842SJustin T. Gibbs {
21650e509842SJustin T. Gibbs 	ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE;
21660e509842SJustin T. Gibbs 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
21670e509842SJustin T. Gibbs }
21680e509842SJustin T. Gibbs 
216989e0f4d2SKip Macy /* ** Driver registration ** */
217023dc5621SKip Macy static device_method_t netfront_methods[] = {
217123dc5621SKip Macy 	/* Device interface */
217223dc5621SKip Macy 	DEVMETHOD(device_probe,         netfront_probe),
217323dc5621SKip Macy 	DEVMETHOD(device_attach,        netfront_attach),
217423dc5621SKip Macy 	DEVMETHOD(device_detach,        netfront_detach),
217523dc5621SKip Macy 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
217623dc5621SKip Macy 	DEVMETHOD(device_suspend,       bus_generic_suspend),
217723dc5621SKip Macy 	DEVMETHOD(device_resume,        netfront_resume),
217889e0f4d2SKip Macy 
217923dc5621SKip Macy 	/* Xenbus interface */
2180*ff662b5cSJustin T. Gibbs 	DEVMETHOD(xenbus_otherend_changed, netfront_backend_changed),
218189e0f4d2SKip Macy 
218223dc5621SKip Macy 	{ 0, 0 }
218389e0f4d2SKip Macy };
218489e0f4d2SKip Macy 
218523dc5621SKip Macy static driver_t netfront_driver = {
218623dc5621SKip Macy 	"xn",
218723dc5621SKip Macy 	netfront_methods,
218823dc5621SKip Macy 	sizeof(struct netfront_info),
218989e0f4d2SKip Macy };
219023dc5621SKip Macy devclass_t netfront_devclass;
219189e0f4d2SKip Macy 
2192*ff662b5cSJustin T. Gibbs DRIVER_MODULE(xe, xenbusb_front, netfront_driver, netfront_devclass, 0, 0);
2193