xref: /freebsd/sys/dev/xen/netback/netback.c (revision 7e949c467cda56d24b24fb4b5d23d4299862ab71)
1*7e949c46SKenneth D. Merry /*-
2*7e949c46SKenneth D. Merry  * Copyright (c) 2009-2011 Spectra Logic Corporation
389e0f4d2SKip Macy  * All rights reserved.
489e0f4d2SKip Macy  *
589e0f4d2SKip Macy  * Redistribution and use in source and binary forms, with or without
689e0f4d2SKip Macy  * modification, are permitted provided that the following conditions
789e0f4d2SKip Macy  * are met:
889e0f4d2SKip Macy  * 1. Redistributions of source code must retain the above copyright
9*7e949c46SKenneth D. Merry  *    notice, this list of conditions, and the following disclaimer,
10*7e949c46SKenneth D. Merry  *    without modification.
11*7e949c46SKenneth D. Merry  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12*7e949c46SKenneth D. Merry  *    substantially similar to the "NO WARRANTY" disclaimer below
13*7e949c46SKenneth D. Merry  *    ("Disclaimer") and any redistribution must be conditioned upon
14*7e949c46SKenneth D. Merry  *    including a substantially similar Disclaimer requirement for further
15*7e949c46SKenneth D. Merry  *    binary redistribution.
1689e0f4d2SKip Macy  *
17*7e949c46SKenneth D. Merry  * NO WARRANTY
18*7e949c46SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*7e949c46SKenneth D. Merry  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*7e949c46SKenneth D. Merry  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21*7e949c46SKenneth D. Merry  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*7e949c46SKenneth D. Merry  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*7e949c46SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*7e949c46SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*7e949c46SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26*7e949c46SKenneth D. Merry  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27*7e949c46SKenneth D. Merry  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*7e949c46SKenneth D. Merry  * POSSIBILITY OF SUCH DAMAGES.
29*7e949c46SKenneth D. Merry  *
30*7e949c46SKenneth D. Merry  * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
31*7e949c46SKenneth D. Merry  *          Alan Somers         (Spectra Logic Corporation)
32*7e949c46SKenneth D. Merry  *          John Suykerbuyk     (Spectra Logic Corporation)
3389e0f4d2SKip Macy  */
3489e0f4d2SKip Macy 
3589e0f4d2SKip Macy #include <sys/cdefs.h>
3689e0f4d2SKip Macy __FBSDID("$FreeBSD$");
37*7e949c46SKenneth D. Merry 
38*7e949c46SKenneth D. Merry /**
39*7e949c46SKenneth D. Merry  * \file netback.c
40*7e949c46SKenneth D. Merry  *
41*7e949c46SKenneth D. Merry  * \brief Device driver supporting the vending of network access
42*7e949c46SKenneth D. Merry  * 	  from this FreeBSD domain to other domains.
43*7e949c46SKenneth D. Merry  */
44*7e949c46SKenneth D. Merry #include "opt_inet.h"
45*7e949c46SKenneth D. Merry #include "opt_global.h"
46*7e949c46SKenneth D. Merry 
472f4afd21SRandall Stewart #include "opt_sctp.h"
4889e0f4d2SKip Macy 
4989e0f4d2SKip Macy #include <sys/param.h>
5089e0f4d2SKip Macy #include <sys/kernel.h>
5189e0f4d2SKip Macy 
5289e0f4d2SKip Macy #include <sys/bus.h>
53*7e949c46SKenneth D. Merry #include <sys/module.h>
54*7e949c46SKenneth D. Merry #include <sys/rman.h>
55*7e949c46SKenneth D. Merry #include <sys/socket.h>
56*7e949c46SKenneth D. Merry #include <sys/sockio.h>
5789e0f4d2SKip Macy #include <sys/sysctl.h>
5889e0f4d2SKip Macy 
5989e0f4d2SKip Macy #include <net/if.h>
6089e0f4d2SKip Macy #include <net/if_arp.h>
6189e0f4d2SKip Macy #include <net/ethernet.h>
62*7e949c46SKenneth D. Merry #include <net/if_dl.h>
63*7e949c46SKenneth D. Merry #include <net/if_media.h>
64*7e949c46SKenneth D. Merry #include <net/if_types.h>
6589e0f4d2SKip Macy 
6689e0f4d2SKip Macy #include <netinet/in.h>
6789e0f4d2SKip Macy #include <netinet/ip.h>
68*7e949c46SKenneth D. Merry #include <netinet/if_ether.h>
69*7e949c46SKenneth D. Merry #if __FreeBSD_version >= 700000
7089e0f4d2SKip Macy #include <netinet/tcp.h>
71*7e949c46SKenneth D. Merry #endif
72*7e949c46SKenneth D. Merry #include <netinet/ip_icmp.h>
7389e0f4d2SKip Macy #include <netinet/udp.h>
7489e0f4d2SKip Macy #include <machine/in_cksum.h>
7589e0f4d2SKip Macy 
76*7e949c46SKenneth D. Merry #include <vm/vm.h>
77*7e949c46SKenneth D. Merry #include <vm/pmap.h>
7889e0f4d2SKip Macy 
79*7e949c46SKenneth D. Merry #include <machine/_inttypes.h>
80*7e949c46SKenneth D. Merry #include <machine/xen/xen-os.h>
81*7e949c46SKenneth D. Merry #include <machine/xen/xenvar.h>
82*7e949c46SKenneth D. Merry 
83*7e949c46SKenneth D. Merry #include <xen/evtchn.h>
84*7e949c46SKenneth D. Merry #include <xen/xen_intr.h>
85*7e949c46SKenneth D. Merry #include <xen/interface/io/netif.h>
86*7e949c46SKenneth D. Merry #include <xen/xenbus/xenbusvar.h>
87*7e949c46SKenneth D. Merry 
88*7e949c46SKenneth D. Merry /*--------------------------- Compile-time Tunables --------------------------*/
89*7e949c46SKenneth D. Merry 
90*7e949c46SKenneth D. Merry /*---------------------------------- Macros ----------------------------------*/
91*7e949c46SKenneth D. Merry /**
92*7e949c46SKenneth D. Merry  * Custom malloc type for all driver allocations.
93*7e949c46SKenneth D. Merry  */
94*7e949c46SKenneth D. Merry static MALLOC_DEFINE(M_XENNETBACK, "xnb", "Xen Net Back Driver Data");
95*7e949c46SKenneth D. Merry 
96*7e949c46SKenneth D. Merry #define	XNB_SG	1	/* netback driver supports feature-sg */
97*7e949c46SKenneth D. Merry #define	XNB_GSO_TCPV4 1	/* netback driver supports feature-gso-tcpv4 */
98*7e949c46SKenneth D. Merry #define	XNB_RX_COPY 1	/* netback driver supports feature-rx-copy */
99*7e949c46SKenneth D. Merry #define	XNB_RX_FLIP 0	/* netback driver does not support feature-rx-flip */
100*7e949c46SKenneth D. Merry 
101*7e949c46SKenneth D. Merry #undef XNB_DEBUG
102*7e949c46SKenneth D. Merry #define	XNB_DEBUG /* hardcode on during development */
103*7e949c46SKenneth D. Merry 
104*7e949c46SKenneth D. Merry #ifdef XNB_DEBUG
10589e0f4d2SKip Macy #define	DPRINTF(fmt, args...) \
106*7e949c46SKenneth D. Merry 	printf("xnb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
10789e0f4d2SKip Macy #else
108*7e949c46SKenneth D. Merry #define	DPRINTF(fmt, args...) do {} while (0)
10989e0f4d2SKip Macy #endif
11089e0f4d2SKip Macy 
111*7e949c46SKenneth D. Merry /* Default length for stack-allocated grant tables */
112*7e949c46SKenneth D. Merry #define	GNTTAB_LEN	(64)
11389e0f4d2SKip Macy 
114*7e949c46SKenneth D. Merry /* Features supported by all backends.  TSO and LRO can be negotiated */
115*7e949c46SKenneth D. Merry #define	XNB_CSUM_FEATURES	(CSUM_TCP | CSUM_UDP)
11689e0f4d2SKip Macy 
11789e0f4d2SKip Macy #define	NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE)
11889e0f4d2SKip Macy #define	NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE)
11989e0f4d2SKip Macy 
120*7e949c46SKenneth D. Merry /**
121*7e949c46SKenneth D. Merry  * Two argument version of the standard macro.  Second argument is a tentative
122*7e949c46SKenneth D. Merry  * value of req_cons
123*7e949c46SKenneth D. Merry  */
124*7e949c46SKenneth D. Merry #define	RING_HAS_UNCONSUMED_REQUESTS_2(_r, cons) ({                     \
125*7e949c46SKenneth D. Merry 	unsigned int req = (_r)->sring->req_prod - cons;          	\
126*7e949c46SKenneth D. Merry 	unsigned int rsp = RING_SIZE(_r) -                              \
127*7e949c46SKenneth D. Merry 	(cons - (_r)->rsp_prod_pvt);                          		\
128*7e949c46SKenneth D. Merry 	req < rsp ? req : rsp;                                          \
129*7e949c46SKenneth D. Merry })
130*7e949c46SKenneth D. Merry 
131*7e949c46SKenneth D. Merry #define	virt_to_mfn(x) (vtomach(x) >> PAGE_SHIFT)
132*7e949c46SKenneth D. Merry #define	virt_to_offset(x) ((x) & (PAGE_SIZE - 1))
133*7e949c46SKenneth D. Merry 
134*7e949c46SKenneth D. Merry /**
135*7e949c46SKenneth D. Merry  * Predefined array type of grant table copy descriptors.  Used to pass around
136*7e949c46SKenneth D. Merry  * statically allocated memory structures.
137*7e949c46SKenneth D. Merry  */
138*7e949c46SKenneth D. Merry typedef struct gnttab_copy gnttab_copy_table[GNTTAB_LEN];
139*7e949c46SKenneth D. Merry 
140*7e949c46SKenneth D. Merry /*--------------------------- Forward Declarations ---------------------------*/
141*7e949c46SKenneth D. Merry struct xnb_softc;
142*7e949c46SKenneth D. Merry struct xnb_pkt;
143*7e949c46SKenneth D. Merry 
144*7e949c46SKenneth D. Merry static void	xnb_attach_failed(struct xnb_softc *xnb,
145*7e949c46SKenneth D. Merry 				  int err, const char *fmt, ...)
146*7e949c46SKenneth D. Merry 				  __printflike(3,4);
147*7e949c46SKenneth D. Merry static int	xnb_shutdown(struct xnb_softc *xnb);
148*7e949c46SKenneth D. Merry static int	create_netdev(device_t dev);
149*7e949c46SKenneth D. Merry static int	xnb_detach(device_t dev);
150*7e949c46SKenneth D. Merry static int	xen_net_read_mac(device_t dev, uint8_t mac[]);
151*7e949c46SKenneth D. Merry static int	xnb_ifmedia_upd(struct ifnet *ifp);
152*7e949c46SKenneth D. Merry static void	xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
153*7e949c46SKenneth D. Merry static void 	xnb_intr(void *arg);
154*7e949c46SKenneth D. Merry static int	xnb_send(netif_rx_back_ring_t *rxb, domid_t otherend,
155*7e949c46SKenneth D. Merry 			 const struct mbuf *mbufc, gnttab_copy_table gnttab);
156*7e949c46SKenneth D. Merry static int	xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend,
157*7e949c46SKenneth D. Merry 			 struct mbuf **mbufc, struct ifnet *ifnet,
158*7e949c46SKenneth D. Merry 			 gnttab_copy_table gnttab);
159*7e949c46SKenneth D. Merry static int	xnb_ring2pkt(struct xnb_pkt *pkt,
160*7e949c46SKenneth D. Merry 			     const netif_tx_back_ring_t *tx_ring,
161*7e949c46SKenneth D. Merry 			     RING_IDX start);
162*7e949c46SKenneth D. Merry static void	xnb_txpkt2rsp(const struct xnb_pkt *pkt,
163*7e949c46SKenneth D. Merry 			      netif_tx_back_ring_t *ring, int error);
164*7e949c46SKenneth D. Merry static struct mbuf *xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp);
165*7e949c46SKenneth D. Merry static int	xnb_txpkt2gnttab(const struct xnb_pkt *pkt,
166*7e949c46SKenneth D. Merry 				 const struct mbuf *mbufc,
167*7e949c46SKenneth D. Merry 				 gnttab_copy_table gnttab,
168*7e949c46SKenneth D. Merry 				 const netif_tx_back_ring_t *txb,
169*7e949c46SKenneth D. Merry 				 domid_t otherend_id);
170*7e949c46SKenneth D. Merry static void	xnb_update_mbufc(struct mbuf *mbufc,
171*7e949c46SKenneth D. Merry 				 const gnttab_copy_table gnttab, int n_entries);
172*7e949c46SKenneth D. Merry static int	xnb_mbufc2pkt(const struct mbuf *mbufc,
173*7e949c46SKenneth D. Merry 			      struct xnb_pkt *pkt,
174*7e949c46SKenneth D. Merry 			      RING_IDX start, int space);
175*7e949c46SKenneth D. Merry static int	xnb_rxpkt2gnttab(const struct xnb_pkt *pkt,
176*7e949c46SKenneth D. Merry 				 const struct mbuf *mbufc,
177*7e949c46SKenneth D. Merry 				 gnttab_copy_table gnttab,
178*7e949c46SKenneth D. Merry 				 const netif_rx_back_ring_t *rxb,
179*7e949c46SKenneth D. Merry 				 domid_t otherend_id);
180*7e949c46SKenneth D. Merry static int	xnb_rxpkt2rsp(const struct xnb_pkt *pkt,
181*7e949c46SKenneth D. Merry 			      const gnttab_copy_table gnttab, int n_entries,
182*7e949c46SKenneth D. Merry 			      netif_rx_back_ring_t *ring);
183*7e949c46SKenneth D. Merry static void	xnb_add_mbuf_cksum(struct mbuf *mbufc);
184*7e949c46SKenneth D. Merry static void	xnb_stop(struct xnb_softc*);
185*7e949c46SKenneth D. Merry static int	xnb_ioctl(struct ifnet*, u_long, caddr_t);
186*7e949c46SKenneth D. Merry static void	xnb_start_locked(struct ifnet*);
187*7e949c46SKenneth D. Merry static void	xnb_start(struct ifnet*);
188*7e949c46SKenneth D. Merry static void	xnb_ifinit_locked(struct xnb_softc*);
189*7e949c46SKenneth D. Merry static void	xnb_ifinit(void*);
190*7e949c46SKenneth D. Merry #ifdef XNB_DEBUG
191*7e949c46SKenneth D. Merry static int	xnb_unit_test_main(SYSCTL_HANDLER_ARGS);
192*7e949c46SKenneth D. Merry static int	xnb_dump_rings(SYSCTL_HANDLER_ARGS);
193*7e949c46SKenneth D. Merry #endif
194*7e949c46SKenneth D. Merry /*------------------------------ Data Structures -----------------------------*/
195*7e949c46SKenneth D. Merry 
196*7e949c46SKenneth D. Merry 
197*7e949c46SKenneth D. Merry /**
198*7e949c46SKenneth D. Merry  * Representation of a xennet packet.  Simplified version of a packet as
199*7e949c46SKenneth D. Merry  * stored in the Xen tx ring.  Applicable to both RX and TX packets
200*7e949c46SKenneth D. Merry  */
201*7e949c46SKenneth D. Merry struct xnb_pkt{
202*7e949c46SKenneth D. Merry 	/**
203*7e949c46SKenneth D. Merry 	 * Array index of the first data-bearing (eg, not extra info) entry
204*7e949c46SKenneth D. Merry 	 * for this packet
205*7e949c46SKenneth D. Merry 	 */
206*7e949c46SKenneth D. Merry 	RING_IDX	car;
207*7e949c46SKenneth D. Merry 
208*7e949c46SKenneth D. Merry 	/**
209*7e949c46SKenneth D. Merry 	 * Array index of the second data-bearing entry for this packet.
210*7e949c46SKenneth D. Merry 	 * Invalid if the packet has only one data-bearing entry.  If the
211*7e949c46SKenneth D. Merry 	 * packet has more than two data-bearing entries, then the second
212*7e949c46SKenneth D. Merry 	 * through the last will be sequential modulo the ring size
213*7e949c46SKenneth D. Merry 	 */
214*7e949c46SKenneth D. Merry 	RING_IDX	cdr;
215*7e949c46SKenneth D. Merry 
216*7e949c46SKenneth D. Merry 	/**
217*7e949c46SKenneth D. Merry 	 * Optional extra info.  Only valid if flags contains
218*7e949c46SKenneth D. Merry 	 * NETTXF_extra_info.  Note that extra.type will always be
219*7e949c46SKenneth D. Merry 	 * XEN_NETIF_EXTRA_TYPE_GSO.  Currently, no known netfront or netback
220*7e949c46SKenneth D. Merry 	 * driver will ever set XEN_NETIF_EXTRA_TYPE_MCAST_*
221*7e949c46SKenneth D. Merry 	 */
222*7e949c46SKenneth D. Merry 	netif_extra_info_t extra;
223*7e949c46SKenneth D. Merry 
224*7e949c46SKenneth D. Merry 	/** Size of entire packet in bytes.       */
225*7e949c46SKenneth D. Merry 	uint16_t	size;
226*7e949c46SKenneth D. Merry 
227*7e949c46SKenneth D. Merry 	/** The size of the first entry's data in bytes */
228*7e949c46SKenneth D. Merry 	uint16_t	car_size;
229*7e949c46SKenneth D. Merry 
230*7e949c46SKenneth D. Merry 	/**
231*7e949c46SKenneth D. Merry 	 * Either NETTXF_ or NETRXF_ flags.  Note that the flag values are
232*7e949c46SKenneth D. Merry 	 * not the same for TX and RX packets
233*7e949c46SKenneth D. Merry 	 */
234*7e949c46SKenneth D. Merry 	uint16_t	flags;
235*7e949c46SKenneth D. Merry 
236*7e949c46SKenneth D. Merry 	/**
237*7e949c46SKenneth D. Merry 	 * The number of valid data-bearing entries (either netif_tx_request's
238*7e949c46SKenneth D. Merry 	 * or netif_rx_response's) in the packet.  If this is 0, it means the
239*7e949c46SKenneth D. Merry 	 * entire packet is invalid.
240*7e949c46SKenneth D. Merry 	 */
241*7e949c46SKenneth D. Merry 	uint16_t	list_len;
242*7e949c46SKenneth D. Merry 
243*7e949c46SKenneth D. Merry 	/** There was an error processing the packet */
244*7e949c46SKenneth D. Merry 	uint8_t		error;
24589e0f4d2SKip Macy };
24689e0f4d2SKip Macy 
247*7e949c46SKenneth D. Merry /** xnb_pkt method: initialize it */
248*7e949c46SKenneth D. Merry static inline void
249*7e949c46SKenneth D. Merry xnb_pkt_initialize(struct xnb_pkt *pxnb)
250*7e949c46SKenneth D. Merry {
251*7e949c46SKenneth D. Merry 	bzero(pxnb, sizeof(*pxnb));
252*7e949c46SKenneth D. Merry }
25389e0f4d2SKip Macy 
254*7e949c46SKenneth D. Merry /** xnb_pkt method: mark the packet as valid */
255*7e949c46SKenneth D. Merry static inline void
256*7e949c46SKenneth D. Merry xnb_pkt_validate(struct xnb_pkt *pxnb)
257*7e949c46SKenneth D. Merry {
258*7e949c46SKenneth D. Merry 	pxnb->error = 0;
259*7e949c46SKenneth D. Merry };
26089e0f4d2SKip Macy 
261*7e949c46SKenneth D. Merry /** xnb_pkt method: mark the packet as invalid */
262*7e949c46SKenneth D. Merry static inline void
263*7e949c46SKenneth D. Merry xnb_pkt_invalidate(struct xnb_pkt *pxnb)
264*7e949c46SKenneth D. Merry {
265*7e949c46SKenneth D. Merry 	pxnb->error = 1;
266*7e949c46SKenneth D. Merry };
26789e0f4d2SKip Macy 
268*7e949c46SKenneth D. Merry /** xnb_pkt method: Check whether the packet is valid */
269*7e949c46SKenneth D. Merry static inline int
270*7e949c46SKenneth D. Merry xnb_pkt_is_valid(const struct xnb_pkt *pxnb)
271*7e949c46SKenneth D. Merry {
272*7e949c46SKenneth D. Merry 	return (! pxnb->error);
273*7e949c46SKenneth D. Merry }
274*7e949c46SKenneth D. Merry 
275*7e949c46SKenneth D. Merry #ifdef XNB_DEBUG
276*7e949c46SKenneth D. Merry /** xnb_pkt method: print the packet's contents in human-readable format*/
277*7e949c46SKenneth D. Merry static void __unused
278*7e949c46SKenneth D. Merry xnb_dump_pkt(const struct xnb_pkt *pkt) {
279*7e949c46SKenneth D. Merry 	if (pkt == NULL) {
280*7e949c46SKenneth D. Merry 	  DPRINTF("Was passed a null pointer.\n");
281*7e949c46SKenneth D. Merry 	  return;
282*7e949c46SKenneth D. Merry 	}
283*7e949c46SKenneth D. Merry 	DPRINTF("pkt address= %p\n", pkt);
284*7e949c46SKenneth D. Merry 	DPRINTF("pkt->size=%d\n", pkt->size);
285*7e949c46SKenneth D. Merry 	DPRINTF("pkt->car_size=%d\n", pkt->car_size);
286*7e949c46SKenneth D. Merry 	DPRINTF("pkt->flags=0x%04x\n", pkt->flags);
287*7e949c46SKenneth D. Merry 	DPRINTF("pkt->list_len=%d\n", pkt->list_len);
288*7e949c46SKenneth D. Merry 	/* DPRINTF("pkt->extra");	TODO */
289*7e949c46SKenneth D. Merry 	DPRINTF("pkt->car=%d\n", pkt->car);
290*7e949c46SKenneth D. Merry 	DPRINTF("pkt->cdr=%d\n", pkt->cdr);
291*7e949c46SKenneth D. Merry 	DPRINTF("pkt->error=%d\n", pkt->error);
292*7e949c46SKenneth D. Merry }
293*7e949c46SKenneth D. Merry #endif /* XNB_DEBUG */
294*7e949c46SKenneth D. Merry 
295*7e949c46SKenneth D. Merry static void
296*7e949c46SKenneth D. Merry xnb_dump_txreq(RING_IDX idx, const struct netif_tx_request *txreq)
297*7e949c46SKenneth D. Merry {
298*7e949c46SKenneth D. Merry 	if (txreq != NULL) {
299*7e949c46SKenneth D. Merry 		DPRINTF("netif_tx_request index =%u\n", idx);
300*7e949c46SKenneth D. Merry 		DPRINTF("netif_tx_request.gref  =%u\n", txreq->gref);
301*7e949c46SKenneth D. Merry 		DPRINTF("netif_tx_request.offset=%hu\n", txreq->offset);
302*7e949c46SKenneth D. Merry 		DPRINTF("netif_tx_request.flags =%hu\n", txreq->flags);
303*7e949c46SKenneth D. Merry 		DPRINTF("netif_tx_request.id    =%hu\n", txreq->id);
304*7e949c46SKenneth D. Merry 		DPRINTF("netif_tx_request.size  =%hu\n", txreq->size);
305*7e949c46SKenneth D. Merry 	}
306*7e949c46SKenneth D. Merry }
307*7e949c46SKenneth D. Merry 
308*7e949c46SKenneth D. Merry 
309*7e949c46SKenneth D. Merry /**
310*7e949c46SKenneth D. Merry  * \brief Configuration data for a shared memory request ring
311*7e949c46SKenneth D. Merry  *        used to communicate with the front-end client of this
312*7e949c46SKenneth D. Merry  *        this driver.
313*7e949c46SKenneth D. Merry  */
314*7e949c46SKenneth D. Merry struct xnb_ring_config {
315*7e949c46SKenneth D. Merry 	/**
316*7e949c46SKenneth D. Merry 	 * Runtime structures for ring access.  Unfortunately, TX and RX rings
317*7e949c46SKenneth D. Merry 	 * use different data structures, and that cannot be changed since it
318*7e949c46SKenneth D. Merry 	 * is part of the interdomain protocol.
319*7e949c46SKenneth D. Merry 	 */
320*7e949c46SKenneth D. Merry 	union{
321*7e949c46SKenneth D. Merry 		netif_rx_back_ring_t	  rx_ring;
322*7e949c46SKenneth D. Merry 		netif_tx_back_ring_t	  tx_ring;
323*7e949c46SKenneth D. Merry 	} back_ring;
324*7e949c46SKenneth D. Merry 
325*7e949c46SKenneth D. Merry 	/**
326*7e949c46SKenneth D. Merry 	 * The device bus address returned by the hypervisor when
327*7e949c46SKenneth D. Merry 	 * mapping the ring and required to unmap it when a connection
328*7e949c46SKenneth D. Merry 	 * is torn down.
329*7e949c46SKenneth D. Merry 	 */
330*7e949c46SKenneth D. Merry 	uint64_t	bus_addr;
331*7e949c46SKenneth D. Merry 
332*7e949c46SKenneth D. Merry 	/** The pseudo-physical address where ring memory is mapped.*/
333*7e949c46SKenneth D. Merry 	uint64_t	gnt_addr;
334*7e949c46SKenneth D. Merry 
335*7e949c46SKenneth D. Merry 	/** KVA address where ring memory is mapped. */
336*7e949c46SKenneth D. Merry 	vm_offset_t	va;
337*7e949c46SKenneth D. Merry 
338*7e949c46SKenneth D. Merry 	/**
339*7e949c46SKenneth D. Merry 	 * Grant table handles, one per-ring page, returned by the
340*7e949c46SKenneth D. Merry 	 * hyperpervisor upon mapping of the ring and required to
341*7e949c46SKenneth D. Merry 	 * unmap it when a connection is torn down.
342*7e949c46SKenneth D. Merry 	 */
343*7e949c46SKenneth D. Merry 	grant_handle_t	handle;
344*7e949c46SKenneth D. Merry 
345*7e949c46SKenneth D. Merry 	/** The number of ring pages mapped for the current connection. */
346*7e949c46SKenneth D. Merry 	unsigned	ring_pages;
347*7e949c46SKenneth D. Merry 
348*7e949c46SKenneth D. Merry 	/**
349*7e949c46SKenneth D. Merry 	 * The grant references, one per-ring page, supplied by the
350*7e949c46SKenneth D. Merry 	 * front-end, allowing us to reference the ring pages in the
351*7e949c46SKenneth D. Merry 	 * front-end's domain and to map these pages into our own domain.
352*7e949c46SKenneth D. Merry 	 */
353*7e949c46SKenneth D. Merry 	grant_ref_t	ring_ref;
354*7e949c46SKenneth D. Merry };
355*7e949c46SKenneth D. Merry 
356*7e949c46SKenneth D. Merry /**
357*7e949c46SKenneth D. Merry  * Per-instance connection state flags.
358*7e949c46SKenneth D. Merry  */
359*7e949c46SKenneth D. Merry typedef enum
360*7e949c46SKenneth D. Merry {
361*7e949c46SKenneth D. Merry 	/** Communication with the front-end has been established. */
362*7e949c46SKenneth D. Merry 	XNBF_RING_CONNECTED    = 0x01,
363*7e949c46SKenneth D. Merry 
364*7e949c46SKenneth D. Merry 	/**
365*7e949c46SKenneth D. Merry 	 * Front-end requests exist in the ring and are waiting for
366*7e949c46SKenneth D. Merry 	 * xnb_xen_req objects to free up.
367*7e949c46SKenneth D. Merry 	 */
368*7e949c46SKenneth D. Merry 	XNBF_RESOURCE_SHORTAGE = 0x02,
369*7e949c46SKenneth D. Merry 
370*7e949c46SKenneth D. Merry 	/** Connection teardown has started. */
371*7e949c46SKenneth D. Merry 	XNBF_SHUTDOWN          = 0x04,
372*7e949c46SKenneth D. Merry 
373*7e949c46SKenneth D. Merry 	/** A thread is already performing shutdown processing. */
374*7e949c46SKenneth D. Merry 	XNBF_IN_SHUTDOWN       = 0x08
375*7e949c46SKenneth D. Merry } xnb_flag_t;
376*7e949c46SKenneth D. Merry 
377*7e949c46SKenneth D. Merry /**
378*7e949c46SKenneth D. Merry  * Types of rings.  Used for array indices and to identify a ring's control
379*7e949c46SKenneth D. Merry  * data structure type
380*7e949c46SKenneth D. Merry  */
381*7e949c46SKenneth D. Merry typedef enum{
382*7e949c46SKenneth D. Merry 	XNB_RING_TYPE_TX = 0,	/* ID of TX rings, used for array indices */
383*7e949c46SKenneth D. Merry 	XNB_RING_TYPE_RX = 1,	/* ID of RX rings, used for array indices */
384*7e949c46SKenneth D. Merry 	XNB_NUM_RING_TYPES
385*7e949c46SKenneth D. Merry } xnb_ring_type_t;
386*7e949c46SKenneth D. Merry 
387*7e949c46SKenneth D. Merry /**
388*7e949c46SKenneth D. Merry  * Per-instance configuration data.
389*7e949c46SKenneth D. Merry  */
390*7e949c46SKenneth D. Merry struct xnb_softc {
391*7e949c46SKenneth D. Merry 	/** NewBus device corresponding to this instance. */
392*7e949c46SKenneth D. Merry 	device_t		dev;
393*7e949c46SKenneth D. Merry 
394*7e949c46SKenneth D. Merry 	/* Media related fields */
395*7e949c46SKenneth D. Merry 
396*7e949c46SKenneth D. Merry 	/** Generic network media state */
397*7e949c46SKenneth D. Merry 	struct ifmedia		sc_media;
398*7e949c46SKenneth D. Merry 
399*7e949c46SKenneth D. Merry 	/** Media carrier info */
400*7e949c46SKenneth D. Merry 	struct ifnet 		*xnb_ifp;
401*7e949c46SKenneth D. Merry 
402*7e949c46SKenneth D. Merry 	/** Our own private carrier state */
403*7e949c46SKenneth D. Merry 	unsigned carrier;
404*7e949c46SKenneth D. Merry 
405*7e949c46SKenneth D. Merry 	/** Device MAC Address */
406*7e949c46SKenneth D. Merry 	uint8_t			mac[ETHER_ADDR_LEN];
407*7e949c46SKenneth D. Merry 
408*7e949c46SKenneth D. Merry 	/* Xen related fields */
409*7e949c46SKenneth D. Merry 
410*7e949c46SKenneth D. Merry 	/**
411*7e949c46SKenneth D. Merry 	 * \brief The netif protocol abi in effect.
412*7e949c46SKenneth D. Merry 	 *
413*7e949c46SKenneth D. Merry 	 * There are situations where the back and front ends can
414*7e949c46SKenneth D. Merry 	 * have a different, native abi (e.g. intel x86_64 and
415*7e949c46SKenneth D. Merry 	 * 32bit x86 domains on the same machine).  The back-end
416*7e949c46SKenneth D. Merry 	 * always accomodates the front-end's native abi.  That
417*7e949c46SKenneth D. Merry 	 * value is pulled from the XenStore and recorded here.
418*7e949c46SKenneth D. Merry 	 */
419*7e949c46SKenneth D. Merry 	int			abi;
420*7e949c46SKenneth D. Merry 
421*7e949c46SKenneth D. Merry 	/**
422*7e949c46SKenneth D. Merry 	 * Name of the bridge to which this VIF is connected, if any
423*7e949c46SKenneth D. Merry 	 * This field is dynamically allocated by xenbus and must be free()ed
424*7e949c46SKenneth D. Merry 	 * when no longer needed
425*7e949c46SKenneth D. Merry 	 */
42689e0f4d2SKip Macy 	char			*bridge;
42789e0f4d2SKip Macy 
428*7e949c46SKenneth D. Merry 	/** The interrupt driven even channel used to signal ring events. */
42989e0f4d2SKip Macy 	evtchn_port_t		evtchn;
430*7e949c46SKenneth D. Merry 
431*7e949c46SKenneth D. Merry 	/** Xen device handle.*/
432*7e949c46SKenneth D. Merry 	long 			handle;
433*7e949c46SKenneth D. Merry 
434*7e949c46SKenneth D. Merry 	/** IRQ mapping for the communication ring event channel. */
43589e0f4d2SKip Macy 	int			irq;
43689e0f4d2SKip Macy 
437*7e949c46SKenneth D. Merry 	/**
438*7e949c46SKenneth D. Merry 	 * \brief Cached value of the front-end's domain id.
439*7e949c46SKenneth D. Merry 	 *
440*7e949c46SKenneth D. Merry 	 * This value is used at once for each mapped page in
441*7e949c46SKenneth D. Merry 	 * a transaction.  We cache it to avoid incuring the
442*7e949c46SKenneth D. Merry 	 * cost of an ivar access every time this is needed.
443*7e949c46SKenneth D. Merry 	 */
444*7e949c46SKenneth D. Merry 	domid_t			otherend_id;
44589e0f4d2SKip Macy 
446*7e949c46SKenneth D. Merry 	/**
447*7e949c46SKenneth D. Merry 	 * Undocumented frontend feature.  Has something to do with
448*7e949c46SKenneth D. Merry 	 * scatter/gather IO
449*7e949c46SKenneth D. Merry 	 */
450*7e949c46SKenneth D. Merry 	uint8_t			can_sg;
451*7e949c46SKenneth D. Merry 	/** Undocumented frontend feature */
452*7e949c46SKenneth D. Merry 	uint8_t			gso;
453*7e949c46SKenneth D. Merry 	/** Undocumented frontend feature */
454*7e949c46SKenneth D. Merry 	uint8_t			gso_prefix;
455*7e949c46SKenneth D. Merry 	/** Can checksum TCP/UDP over IPv4 */
456*7e949c46SKenneth D. Merry 	uint8_t			ip_csum;
45789e0f4d2SKip Macy 
458*7e949c46SKenneth D. Merry 	/* Implementation related fields */
459*7e949c46SKenneth D. Merry 	/**
460*7e949c46SKenneth D. Merry 	 * Preallocated grant table copy descriptor for RX operations.
461*7e949c46SKenneth D. Merry 	 * Access must be protected by rx_lock
462*7e949c46SKenneth D. Merry 	 */
463*7e949c46SKenneth D. Merry 	gnttab_copy_table	rx_gnttab;
46489e0f4d2SKip Macy 
465*7e949c46SKenneth D. Merry 	/**
466*7e949c46SKenneth D. Merry 	 * Preallocated grant table copy descriptor for TX operations.
467*7e949c46SKenneth D. Merry 	 * Access must be protected by tx_lock
468*7e949c46SKenneth D. Merry 	 */
469*7e949c46SKenneth D. Merry 	gnttab_copy_table	tx_gnttab;
47089e0f4d2SKip Macy 
471*7e949c46SKenneth D. Merry #ifdef XENHVM
472*7e949c46SKenneth D. Merry 	/**
473*7e949c46SKenneth D. Merry 	 * Resource representing allocated physical address space
474*7e949c46SKenneth D. Merry 	 * associated with our per-instance kva region.
475*7e949c46SKenneth D. Merry 	 */
476*7e949c46SKenneth D. Merry 	struct resource		*pseudo_phys_res;
47789e0f4d2SKip Macy 
478*7e949c46SKenneth D. Merry 	/** Resource id for allocated physical address space. */
479*7e949c46SKenneth D. Merry 	int			pseudo_phys_res_id;
480*7e949c46SKenneth D. Merry #endif
48189e0f4d2SKip Macy 
482*7e949c46SKenneth D. Merry 	/** Ring mapping and interrupt configuration data. */
483*7e949c46SKenneth D. Merry 	struct xnb_ring_config	ring_configs[XNB_NUM_RING_TYPES];
48489e0f4d2SKip Macy 
485*7e949c46SKenneth D. Merry 	/**
486*7e949c46SKenneth D. Merry 	 * Global pool of kva used for mapping remote domain ring
487*7e949c46SKenneth D. Merry 	 * and I/O transaction data.
488*7e949c46SKenneth D. Merry 	 */
489*7e949c46SKenneth D. Merry 	vm_offset_t		kva;
49089e0f4d2SKip Macy 
491*7e949c46SKenneth D. Merry 	/** Psuedo-physical address corresponding to kva. */
492*7e949c46SKenneth D. Merry 	uint64_t		gnt_base_addr;
49389e0f4d2SKip Macy 
494*7e949c46SKenneth D. Merry 	/** Various configuration and state bit flags. */
495*7e949c46SKenneth D. Merry 	xnb_flag_t		flags;
49689e0f4d2SKip Macy 
497*7e949c46SKenneth D. Merry 	/** Mutex protecting per-instance data in the receive path. */
498*7e949c46SKenneth D. Merry 	struct mtx		rx_lock;
49989e0f4d2SKip Macy 
500*7e949c46SKenneth D. Merry 	/** Mutex protecting per-instance data in the softc structure. */
501*7e949c46SKenneth D. Merry 	struct mtx		sc_lock;
50289e0f4d2SKip Macy 
503*7e949c46SKenneth D. Merry 	/** Mutex protecting per-instance data in the transmit path. */
504*7e949c46SKenneth D. Merry 	struct mtx		tx_lock;
50589e0f4d2SKip Macy 
506*7e949c46SKenneth D. Merry 	/** The size of the global kva pool. */
507*7e949c46SKenneth D. Merry 	int			kva_size;
508*7e949c46SKenneth D. Merry };
509*7e949c46SKenneth D. Merry 
510*7e949c46SKenneth D. Merry /*---------------------------- Debugging functions ---------------------------*/
511*7e949c46SKenneth D. Merry #ifdef XNB_DEBUG
512*7e949c46SKenneth D. Merry static void __unused
513*7e949c46SKenneth D. Merry xnb_dump_gnttab_copy(const struct gnttab_copy *entry)
51489e0f4d2SKip Macy {
515*7e949c46SKenneth D. Merry 	if (entry == NULL) {
516*7e949c46SKenneth D. Merry 		printf("NULL grant table pointer\n");
517*7e949c46SKenneth D. Merry 		return;
51889e0f4d2SKip Macy 	}
519*7e949c46SKenneth D. Merry 
520*7e949c46SKenneth D. Merry 	if (entry->flags & GNTCOPY_dest_gref)
521*7e949c46SKenneth D. Merry 		printf("gnttab dest ref=\t%u\n", entry->dest.u.ref);
522*7e949c46SKenneth D. Merry 	else
523*7e949c46SKenneth D. Merry 		printf("gnttab dest gmfn=\t%lu\n", entry->dest.u.gmfn);
524*7e949c46SKenneth D. Merry 	printf("gnttab dest offset=\t%hu\n", entry->dest.offset);
525*7e949c46SKenneth D. Merry 	printf("gnttab dest domid=\t%hu\n", entry->dest.domid);
526*7e949c46SKenneth D. Merry 	if (entry->flags & GNTCOPY_source_gref)
527*7e949c46SKenneth D. Merry 		printf("gnttab source ref=\t%u\n", entry->source.u.ref);
528*7e949c46SKenneth D. Merry 	else
529*7e949c46SKenneth D. Merry 		printf("gnttab source gmfn=\t%lu\n", entry->source.u.gmfn);
530*7e949c46SKenneth D. Merry 	printf("gnttab source offset=\t%hu\n", entry->source.offset);
531*7e949c46SKenneth D. Merry 	printf("gnttab source domid=\t%hu\n", entry->source.domid);
532*7e949c46SKenneth D. Merry 	printf("gnttab len=\t%hu\n", entry->len);
533*7e949c46SKenneth D. Merry 	printf("gnttab flags=\t%hu\n", entry->flags);
534*7e949c46SKenneth D. Merry 	printf("gnttab status=\t%hd\n", entry->status);
535*7e949c46SKenneth D. Merry }
536*7e949c46SKenneth D. Merry 
537*7e949c46SKenneth D. Merry static int
538*7e949c46SKenneth D. Merry xnb_dump_rings(SYSCTL_HANDLER_ARGS)
539*7e949c46SKenneth D. Merry {
540*7e949c46SKenneth D. Merry 	static char results[720];
541*7e949c46SKenneth D. Merry 	struct xnb_softc const* xnb = (struct xnb_softc*)arg1;
542*7e949c46SKenneth D. Merry 	netif_rx_back_ring_t const* rxb =
543*7e949c46SKenneth D. Merry 		&xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring;
544*7e949c46SKenneth D. Merry 	netif_tx_back_ring_t const* txb =
545*7e949c46SKenneth D. Merry 		&xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring;
546*7e949c46SKenneth D. Merry 
547*7e949c46SKenneth D. Merry 	/* empty the result strings */
548*7e949c46SKenneth D. Merry 	results[0] = 0;
549*7e949c46SKenneth D. Merry 
550*7e949c46SKenneth D. Merry 	if ( !txb || !txb->sring || !rxb || !rxb->sring )
551*7e949c46SKenneth D. Merry 		return (SYSCTL_OUT(req, results, strnlen(results, 720)));
552*7e949c46SKenneth D. Merry 
553*7e949c46SKenneth D. Merry 	snprintf(results, 720,
554*7e949c46SKenneth D. Merry 	    "\n\t%35s %18s\n"	/* TX, RX */
555*7e949c46SKenneth D. Merry 	    "\t%16s %18d %18d\n"	/* req_cons */
556*7e949c46SKenneth D. Merry 	    "\t%16s %18d %18d\n"	/* nr_ents */
557*7e949c46SKenneth D. Merry 	    "\t%16s %18d %18d\n"	/* rsp_prod_pvt */
558*7e949c46SKenneth D. Merry 	    "\t%16s %18p %18p\n"	/* sring */
559*7e949c46SKenneth D. Merry 	    "\t%16s %18d %18d\n"	/* req_prod */
560*7e949c46SKenneth D. Merry 	    "\t%16s %18d %18d\n"	/* req_event */
561*7e949c46SKenneth D. Merry 	    "\t%16s %18d %18d\n"	/* rsp_prod */
562*7e949c46SKenneth D. Merry 	    "\t%16s %18d %18d\n",	/* rsp_event */
563*7e949c46SKenneth D. Merry 	    "TX", "RX",
564*7e949c46SKenneth D. Merry 	    "req_cons", txb->req_cons, rxb->req_cons,
565*7e949c46SKenneth D. Merry 	    "nr_ents", txb->nr_ents, rxb->nr_ents,
566*7e949c46SKenneth D. Merry 	    "rsp_prod_pvt", txb->rsp_prod_pvt, rxb->rsp_prod_pvt,
567*7e949c46SKenneth D. Merry 	    "sring", txb->sring, rxb->sring,
568*7e949c46SKenneth D. Merry 	    "sring->req_prod", txb->sring->req_prod, rxb->sring->req_prod,
569*7e949c46SKenneth D. Merry 	    "sring->req_event", txb->sring->req_event, rxb->sring->req_event,
570*7e949c46SKenneth D. Merry 	    "sring->rsp_prod", txb->sring->rsp_prod, rxb->sring->rsp_prod,
571*7e949c46SKenneth D. Merry 	    "sring->rsp_event", txb->sring->rsp_event, rxb->sring->rsp_event);
572*7e949c46SKenneth D. Merry 
573*7e949c46SKenneth D. Merry 	return (SYSCTL_OUT(req, results, strnlen(results, 720)));
574*7e949c46SKenneth D. Merry }
575*7e949c46SKenneth D. Merry 
576*7e949c46SKenneth D. Merry static void __unused
577*7e949c46SKenneth D. Merry xnb_dump_mbuf(const struct mbuf *m)
578*7e949c46SKenneth D. Merry {
579*7e949c46SKenneth D. Merry 	int len;
580*7e949c46SKenneth D. Merry 	uint8_t *d;
581*7e949c46SKenneth D. Merry 	if (m == NULL)
582*7e949c46SKenneth D. Merry 		return;
583*7e949c46SKenneth D. Merry 
584*7e949c46SKenneth D. Merry 	printf("xnb_dump_mbuf:\n");
585*7e949c46SKenneth D. Merry 	if (m->m_flags & M_PKTHDR) {
586*7e949c46SKenneth D. Merry 		printf("    flowid=%10d, csum_flags=%#8x, csum_data=%#8x, "
587*7e949c46SKenneth D. Merry 		       "tso_segsz=%5hd\n",
588*7e949c46SKenneth D. Merry 		       m->m_pkthdr.flowid, m->m_pkthdr.csum_flags,
589*7e949c46SKenneth D. Merry 		       m->m_pkthdr.csum_data, m->m_pkthdr.tso_segsz);
590*7e949c46SKenneth D. Merry 		printf("    rcvif=%16p,  header=%18p, len=%19d\n",
591*7e949c46SKenneth D. Merry 		       m->m_pkthdr.rcvif, m->m_pkthdr.header, m->m_pkthdr.len);
592*7e949c46SKenneth D. Merry 	}
593*7e949c46SKenneth D. Merry 	printf("    m_next=%16p, m_nextpk=%16p, m_data=%16p\n",
594*7e949c46SKenneth D. Merry 	       m->m_next, m->m_nextpkt, m->m_data);
595*7e949c46SKenneth D. Merry 	printf("    m_len=%17d, m_flags=%#15x, m_type=%18hd\n",
596*7e949c46SKenneth D. Merry 	       m->m_len, m->m_flags, m->m_type);
597*7e949c46SKenneth D. Merry 
598*7e949c46SKenneth D. Merry 	len = m->m_len;
599*7e949c46SKenneth D. Merry 	d = mtod(m, uint8_t*);
600*7e949c46SKenneth D. Merry 	while (len > 0) {
601*7e949c46SKenneth D. Merry 		int i;
602*7e949c46SKenneth D. Merry 		printf("                ");
603*7e949c46SKenneth D. Merry 		for (i = 0; (i < 16) && (len > 0); i++, len--) {
604*7e949c46SKenneth D. Merry 			printf("%02hhx ", *(d++));
60589e0f4d2SKip Macy 		}
60689e0f4d2SKip Macy 		printf("\n");
60789e0f4d2SKip Macy 	}
60889e0f4d2SKip Macy }
609*7e949c46SKenneth D. Merry #endif /* XNB_DEBUG */
61089e0f4d2SKip Macy 
611*7e949c46SKenneth D. Merry /*------------------------ Inter-Domain Communication ------------------------*/
612*7e949c46SKenneth D. Merry /**
613*7e949c46SKenneth D. Merry  * Free dynamically allocated KVA or pseudo-physical address allocations.
614*7e949c46SKenneth D. Merry  *
615*7e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
61689e0f4d2SKip Macy  */
61789e0f4d2SKip Macy static void
618*7e949c46SKenneth D. Merry xnb_free_communication_mem(struct xnb_softc *xnb)
61989e0f4d2SKip Macy {
620*7e949c46SKenneth D. Merry 	if (xnb->kva != 0) {
621*7e949c46SKenneth D. Merry #ifndef XENHVM
622*7e949c46SKenneth D. Merry 		kmem_free(kernel_map, xnb->kva, xnb->kva_size);
62389e0f4d2SKip Macy #else
624*7e949c46SKenneth D. Merry 		if (xnb->pseudo_phys_res != NULL) {
625*7e949c46SKenneth D. Merry 			bus_release_resource(xnb->dev, SYS_RES_MEMORY,
626*7e949c46SKenneth D. Merry 			    xnb->pseudo_phys_res_id,
627*7e949c46SKenneth D. Merry 			    xnb->pseudo_phys_res);
628*7e949c46SKenneth D. Merry 			xnb->pseudo_phys_res = NULL;
62989e0f4d2SKip Macy 		}
630*7e949c46SKenneth D. Merry #endif /* XENHVM */
631*7e949c46SKenneth D. Merry 	}
632*7e949c46SKenneth D. Merry 	xnb->kva = 0;
633*7e949c46SKenneth D. Merry 	xnb->gnt_base_addr = 0;
63489e0f4d2SKip Macy }
63589e0f4d2SKip Macy 
636*7e949c46SKenneth D. Merry /**
637*7e949c46SKenneth D. Merry  * Cleanup all inter-domain communication mechanisms.
638*7e949c46SKenneth D. Merry  *
639*7e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
640*7e949c46SKenneth D. Merry  */
641*7e949c46SKenneth D. Merry static int
642*7e949c46SKenneth D. Merry xnb_disconnect(struct xnb_softc *xnb)
64389e0f4d2SKip Macy {
644*7e949c46SKenneth D. Merry 	struct gnttab_unmap_grant_ref gnts[XNB_NUM_RING_TYPES];
645*7e949c46SKenneth D. Merry 	int error;
646*7e949c46SKenneth D. Merry 	int i;
64789e0f4d2SKip Macy 
648*7e949c46SKenneth D. Merry 	if (xnb->irq != 0) {
649*7e949c46SKenneth D. Merry 		unbind_from_irqhandler(xnb->irq);
650*7e949c46SKenneth D. Merry 		xnb->irq = 0;
65189e0f4d2SKip Macy 	}
65289e0f4d2SKip Macy 
65389e0f4d2SKip Macy 	/*
654*7e949c46SKenneth D. Merry 	 * We may still have another thread currently processing requests.  We
655*7e949c46SKenneth D. Merry 	 * must acquire the rx and tx locks to make sure those threads are done,
656*7e949c46SKenneth D. Merry 	 * but we can release those locks as soon as we acquire them, because no
657*7e949c46SKenneth D. Merry 	 * more interrupts will be arriving.
65889e0f4d2SKip Macy 	 */
659*7e949c46SKenneth D. Merry 	mtx_lock(&xnb->tx_lock);
660*7e949c46SKenneth D. Merry 	mtx_unlock(&xnb->tx_lock);
661*7e949c46SKenneth D. Merry 	mtx_lock(&xnb->rx_lock);
662*7e949c46SKenneth D. Merry 	mtx_unlock(&xnb->rx_lock);
663*7e949c46SKenneth D. Merry 
664*7e949c46SKenneth D. Merry 	/* Free malloc'd softc member variables */
665*7e949c46SKenneth D. Merry 	if (xnb->bridge != NULL)
666*7e949c46SKenneth D. Merry 		free(xnb->bridge, M_XENSTORE);
667*7e949c46SKenneth D. Merry 
668*7e949c46SKenneth D. Merry 	/* All request processing has stopped, so unmap the rings */
669*7e949c46SKenneth D. Merry 	for (i=0; i < XNB_NUM_RING_TYPES; i++) {
670*7e949c46SKenneth D. Merry 		gnts[i].host_addr = xnb->ring_configs[i].gnt_addr;
671*7e949c46SKenneth D. Merry 		gnts[i].dev_bus_addr = xnb->ring_configs[i].bus_addr;
672*7e949c46SKenneth D. Merry 		gnts[i].handle = xnb->ring_configs[i].handle;
67389e0f4d2SKip Macy 	}
674*7e949c46SKenneth D. Merry 	error = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, gnts,
675*7e949c46SKenneth D. Merry 					  XNB_NUM_RING_TYPES);
676*7e949c46SKenneth D. Merry 	KASSERT(error == 0, ("Grant table unmap op failed (%d)", error));
67789e0f4d2SKip Macy 
678*7e949c46SKenneth D. Merry 	xnb_free_communication_mem(xnb);
679*7e949c46SKenneth D. Merry 	/*
680*7e949c46SKenneth D. Merry 	 * Zero the ring config structs because the pointers, handles, and
681*7e949c46SKenneth D. Merry 	 * grant refs contained therein are no longer valid.
682*7e949c46SKenneth D. Merry 	 */
683*7e949c46SKenneth D. Merry 	bzero(&xnb->ring_configs[XNB_RING_TYPE_TX],
684*7e949c46SKenneth D. Merry 	    sizeof(struct xnb_ring_config));
685*7e949c46SKenneth D. Merry 	bzero(&xnb->ring_configs[XNB_RING_TYPE_RX],
686*7e949c46SKenneth D. Merry 	    sizeof(struct xnb_ring_config));
68789e0f4d2SKip Macy 
688*7e949c46SKenneth D. Merry 	xnb->flags &= ~XNBF_RING_CONNECTED;
689*7e949c46SKenneth D. Merry 	return (0);
69089e0f4d2SKip Macy }
69189e0f4d2SKip Macy 
692*7e949c46SKenneth D. Merry /**
693*7e949c46SKenneth D. Merry  * Map a single shared memory ring into domain local address space and
694*7e949c46SKenneth D. Merry  * initialize its control structure
695*7e949c46SKenneth D. Merry  *
696*7e949c46SKenneth D. Merry  * \param xnb	Per-instance xnb configuration structure
697*7e949c46SKenneth D. Merry  * \param ring_type	Array index of this ring in the xnb's array of rings
698*7e949c46SKenneth D. Merry  * \return 	An errno
699*7e949c46SKenneth D. Merry  */
700*7e949c46SKenneth D. Merry static int
701*7e949c46SKenneth D. Merry xnb_connect_ring(struct xnb_softc *xnb, xnb_ring_type_t ring_type)
70289e0f4d2SKip Macy {
703*7e949c46SKenneth D. Merry 	struct gnttab_map_grant_ref gnt;
704*7e949c46SKenneth D. Merry 	struct xnb_ring_config *ring = &xnb->ring_configs[ring_type];
705*7e949c46SKenneth D. Merry 	int error;
70689e0f4d2SKip Macy 
707*7e949c46SKenneth D. Merry 	/* TX ring type = 0, RX =1 */
708*7e949c46SKenneth D. Merry 	ring->va = xnb->kva + ring_type * PAGE_SIZE;
709*7e949c46SKenneth D. Merry 	ring->gnt_addr = xnb->gnt_base_addr + ring_type * PAGE_SIZE;
71089e0f4d2SKip Macy 
711*7e949c46SKenneth D. Merry 	gnt.host_addr = ring->gnt_addr;
712*7e949c46SKenneth D. Merry 	gnt.flags     = GNTMAP_host_map;
713*7e949c46SKenneth D. Merry 	gnt.ref       = ring->ring_ref;
714*7e949c46SKenneth D. Merry 	gnt.dom       = xnb->otherend_id;
71589e0f4d2SKip Macy 
716*7e949c46SKenneth D. Merry 	error = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &gnt, 1);
717*7e949c46SKenneth D. Merry 	if (error != 0)
718*7e949c46SKenneth D. Merry 		panic("netback: Ring page grant table op failed (%d)", error);
71989e0f4d2SKip Macy 
720*7e949c46SKenneth D. Merry 	if (gnt.status != 0) {
721*7e949c46SKenneth D. Merry 		ring->va = 0;
722*7e949c46SKenneth D. Merry 		error = EACCES;
723*7e949c46SKenneth D. Merry 		xenbus_dev_fatal(xnb->dev, error,
724*7e949c46SKenneth D. Merry 				 "Ring shared page mapping failed. "
725*7e949c46SKenneth D. Merry 				 "Status %d.", gnt.status);
72689e0f4d2SKip Macy 	} else {
727*7e949c46SKenneth D. Merry 		ring->handle = gnt.handle;
728*7e949c46SKenneth D. Merry 		ring->bus_addr = gnt.dev_bus_addr;
72989e0f4d2SKip Macy 
730*7e949c46SKenneth D. Merry 		if (ring_type == XNB_RING_TYPE_TX) {
731*7e949c46SKenneth D. Merry 			BACK_RING_INIT(&ring->back_ring.tx_ring,
732*7e949c46SKenneth D. Merry 			    (netif_tx_sring_t*)ring->va,
733*7e949c46SKenneth D. Merry 			    ring->ring_pages * PAGE_SIZE);
734*7e949c46SKenneth D. Merry 		} else if (ring_type == XNB_RING_TYPE_RX) {
735*7e949c46SKenneth D. Merry 			BACK_RING_INIT(&ring->back_ring.rx_ring,
736*7e949c46SKenneth D. Merry 			    (netif_rx_sring_t*)ring->va,
737*7e949c46SKenneth D. Merry 			    ring->ring_pages * PAGE_SIZE);
738*7e949c46SKenneth D. Merry 		} else {
739*7e949c46SKenneth D. Merry 			xenbus_dev_fatal(xnb->dev, error,
740*7e949c46SKenneth D. Merry 				 "Unknown ring type %d", ring_type);
74189e0f4d2SKip Macy 		}
74289e0f4d2SKip Macy 	}
74389e0f4d2SKip Macy 
744*7e949c46SKenneth D. Merry 	return error;
74589e0f4d2SKip Macy }
74689e0f4d2SKip Macy 
747*7e949c46SKenneth D. Merry /**
748*7e949c46SKenneth D. Merry  * Setup the shared memory rings and bind an interrupt to the event channel
749*7e949c46SKenneth D. Merry  * used to notify us of ring changes.
750*7e949c46SKenneth D. Merry  *
751*7e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
752*7e949c46SKenneth D. Merry  */
753*7e949c46SKenneth D. Merry static int
754*7e949c46SKenneth D. Merry xnb_connect_comms(struct xnb_softc *xnb)
755*7e949c46SKenneth D. Merry {
756*7e949c46SKenneth D. Merry 	int	error;
757*7e949c46SKenneth D. Merry 	xnb_ring_type_t i;
758*7e949c46SKenneth D. Merry 
759*7e949c46SKenneth D. Merry 	if ((xnb->flags & XNBF_RING_CONNECTED) != 0)
760*7e949c46SKenneth D. Merry 		return (0);
761*7e949c46SKenneth D. Merry 
762*7e949c46SKenneth D. Merry 	/*
763*7e949c46SKenneth D. Merry 	 * Kva for our rings are at the tail of the region of kva allocated
764*7e949c46SKenneth D. Merry 	 * by xnb_alloc_communication_mem().
765*7e949c46SKenneth D. Merry 	 */
766*7e949c46SKenneth D. Merry 	for (i=0; i < XNB_NUM_RING_TYPES; i++) {
767*7e949c46SKenneth D. Merry 		error = xnb_connect_ring(xnb, i);
768*7e949c46SKenneth D. Merry 		if (error != 0)
769*7e949c46SKenneth D. Merry 	  		return error;
770*7e949c46SKenneth D. Merry 	}
771*7e949c46SKenneth D. Merry 
772*7e949c46SKenneth D. Merry 	xnb->flags |= XNBF_RING_CONNECTED;
773*7e949c46SKenneth D. Merry 
774*7e949c46SKenneth D. Merry 	error =
775*7e949c46SKenneth D. Merry 	    bind_interdomain_evtchn_to_irqhandler(xnb->otherend_id,
776*7e949c46SKenneth D. Merry 						  xnb->evtchn,
777*7e949c46SKenneth D. Merry 						  device_get_nameunit(xnb->dev),
778*7e949c46SKenneth D. Merry 						  xnb_intr, /*arg*/xnb,
779*7e949c46SKenneth D. Merry 						  INTR_TYPE_BIO | INTR_MPSAFE,
780*7e949c46SKenneth D. Merry 						  &xnb->irq);
781*7e949c46SKenneth D. Merry 	if (error != 0) {
782*7e949c46SKenneth D. Merry 		(void)xnb_disconnect(xnb);
783*7e949c46SKenneth D. Merry 		xenbus_dev_fatal(xnb->dev, error, "binding event channel");
784*7e949c46SKenneth D. Merry 		return (error);
785*7e949c46SKenneth D. Merry 	}
786*7e949c46SKenneth D. Merry 
787*7e949c46SKenneth D. Merry 	DPRINTF("rings connected!\n");
788*7e949c46SKenneth D. Merry 
789*7e949c46SKenneth D. Merry 	return (0);
790*7e949c46SKenneth D. Merry }
791*7e949c46SKenneth D. Merry 
792*7e949c46SKenneth D. Merry /**
793*7e949c46SKenneth D. Merry  * Size KVA and pseudo-physical address allocations based on negotiated
794*7e949c46SKenneth D. Merry  * values for the size and number of I/O requests, and the size of our
795*7e949c46SKenneth D. Merry  * communication ring.
796*7e949c46SKenneth D. Merry  *
797*7e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
798*7e949c46SKenneth D. Merry  *
799*7e949c46SKenneth D. Merry  * These address spaces are used to dynamically map pages in the
800*7e949c46SKenneth D. Merry  * front-end's domain into our own.
801*7e949c46SKenneth D. Merry  */
802*7e949c46SKenneth D. Merry static int
803*7e949c46SKenneth D. Merry xnb_alloc_communication_mem(struct xnb_softc *xnb)
804*7e949c46SKenneth D. Merry {
805*7e949c46SKenneth D. Merry 	xnb_ring_type_t i;
806*7e949c46SKenneth D. Merry 
807*7e949c46SKenneth D. Merry 	xnb->kva_size = 0;
808*7e949c46SKenneth D. Merry 	for (i=0; i < XNB_NUM_RING_TYPES; i++) {
809*7e949c46SKenneth D. Merry 		xnb->kva_size += xnb->ring_configs[i].ring_pages * PAGE_SIZE;
810*7e949c46SKenneth D. Merry 	}
811*7e949c46SKenneth D. Merry #ifndef XENHVM
812*7e949c46SKenneth D. Merry 	xnb->kva = kmem_alloc_nofault(kernel_map, xnb->kva_size);
813*7e949c46SKenneth D. Merry 	if (xnb->kva == 0)
814*7e949c46SKenneth D. Merry 		return (ENOMEM);
815*7e949c46SKenneth D. Merry 	xnb->gnt_base_addr = xnb->kva;
816*7e949c46SKenneth D. Merry #else /* defined XENHVM */
817*7e949c46SKenneth D. Merry 	/*
818*7e949c46SKenneth D. Merry 	 * Reserve a range of pseudo physical memory that we can map
819*7e949c46SKenneth D. Merry 	 * into kva.  These pages will only be backed by machine
820*7e949c46SKenneth D. Merry 	 * pages ("real memory") during the lifetime of front-end requests
821*7e949c46SKenneth D. Merry 	 * via grant table operations.  We will map the netif tx and rx rings
822*7e949c46SKenneth D. Merry 	 * into this space.
823*7e949c46SKenneth D. Merry 	 */
824*7e949c46SKenneth D. Merry 	xnb->pseudo_phys_res_id = 0;
825*7e949c46SKenneth D. Merry 	xnb->pseudo_phys_res = bus_alloc_resource(xnb->dev, SYS_RES_MEMORY,
826*7e949c46SKenneth D. Merry 						  &xnb->pseudo_phys_res_id,
827*7e949c46SKenneth D. Merry 						  0, ~0, xnb->kva_size,
828*7e949c46SKenneth D. Merry 						  RF_ACTIVE);
829*7e949c46SKenneth D. Merry 	if (xnb->pseudo_phys_res == NULL) {
830*7e949c46SKenneth D. Merry 		xnb->kva = 0;
831*7e949c46SKenneth D. Merry 		return (ENOMEM);
832*7e949c46SKenneth D. Merry 	}
833*7e949c46SKenneth D. Merry 	xnb->kva = (vm_offset_t)rman_get_virtual(xnb->pseudo_phys_res);
834*7e949c46SKenneth D. Merry 	xnb->gnt_base_addr = rman_get_start(xnb->pseudo_phys_res);
835*7e949c46SKenneth D. Merry #endif /* !defined XENHVM */
836*7e949c46SKenneth D. Merry 	return (0);
837*7e949c46SKenneth D. Merry }
838*7e949c46SKenneth D. Merry 
839*7e949c46SKenneth D. Merry /**
840*7e949c46SKenneth D. Merry  * Collect information from the XenStore related to our device and its frontend
841*7e949c46SKenneth D. Merry  *
842*7e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
843*7e949c46SKenneth D. Merry  */
844*7e949c46SKenneth D. Merry static int
845*7e949c46SKenneth D. Merry xnb_collect_xenstore_info(struct xnb_softc *xnb)
846*7e949c46SKenneth D. Merry {
847*7e949c46SKenneth D. Merry 	/**
848*7e949c46SKenneth D. Merry 	 * \todo Linux collects the following info.  We should collect most
849*7e949c46SKenneth D. Merry 	 * of this, too:
850*7e949c46SKenneth D. Merry 	 * "feature-rx-notify"
851*7e949c46SKenneth D. Merry 	 */
852*7e949c46SKenneth D. Merry 	const char *otherend_path;
853*7e949c46SKenneth D. Merry 	const char *our_path;
854*7e949c46SKenneth D. Merry 	int err;
855*7e949c46SKenneth D. Merry 	unsigned int rx_copy, bridge_len;
856*7e949c46SKenneth D. Merry 	uint8_t no_csum_offload;
857*7e949c46SKenneth D. Merry 
858*7e949c46SKenneth D. Merry 	otherend_path = xenbus_get_otherend_path(xnb->dev);
859*7e949c46SKenneth D. Merry 	our_path = xenbus_get_node(xnb->dev);
860*7e949c46SKenneth D. Merry 
861*7e949c46SKenneth D. Merry 	/* Collect the critical communication parameters */
862*7e949c46SKenneth D. Merry 	err = xs_gather(XST_NIL, otherend_path,
863*7e949c46SKenneth D. Merry 	    "tx-ring-ref", "%l" PRIu32,
864*7e949c46SKenneth D. Merry 	    	&xnb->ring_configs[XNB_RING_TYPE_TX].ring_ref,
865*7e949c46SKenneth D. Merry 	    "rx-ring-ref", "%l" PRIu32,
866*7e949c46SKenneth D. Merry 	    	&xnb->ring_configs[XNB_RING_TYPE_RX].ring_ref,
867*7e949c46SKenneth D. Merry 	    "event-channel", "%" PRIu32, &xnb->evtchn,
868*7e949c46SKenneth D. Merry 	    NULL);
869*7e949c46SKenneth D. Merry 	if (err != 0) {
870*7e949c46SKenneth D. Merry 		xenbus_dev_fatal(xnb->dev, err,
871*7e949c46SKenneth D. Merry 				 "Unable to retrieve ring information from "
872*7e949c46SKenneth D. Merry 				 "frontend %s.  Unable to connect.",
873*7e949c46SKenneth D. Merry 				 otherend_path);
874*7e949c46SKenneth D. Merry 		return (err);
875*7e949c46SKenneth D. Merry 	}
876*7e949c46SKenneth D. Merry 
877*7e949c46SKenneth D. Merry 	/* Collect the handle from xenstore */
878*7e949c46SKenneth D. Merry 	err = xs_scanf(XST_NIL, our_path, "handle", NULL, "%li", &xnb->handle);
879*7e949c46SKenneth D. Merry 	if (err != 0) {
880*7e949c46SKenneth D. Merry 		xenbus_dev_fatal(xnb->dev, err,
881*7e949c46SKenneth D. Merry 		    "Error reading handle from frontend %s.  "
882*7e949c46SKenneth D. Merry 		    "Unable to connect.", otherend_path);
883*7e949c46SKenneth D. Merry 	}
884*7e949c46SKenneth D. Merry 
885*7e949c46SKenneth D. Merry 	/*
886*7e949c46SKenneth D. Merry 	 * Collect the bridgename, if any.  We do not need bridge_len; we just
887*7e949c46SKenneth D. Merry 	 * throw it away
888*7e949c46SKenneth D. Merry 	 */
889*7e949c46SKenneth D. Merry 	err = xs_read(XST_NIL, our_path, "bridge", &bridge_len,
890*7e949c46SKenneth D. Merry 		      (void**)&xnb->bridge);
891*7e949c46SKenneth D. Merry 	if (err != 0)
892*7e949c46SKenneth D. Merry 		xnb->bridge = NULL;
893*7e949c46SKenneth D. Merry 
894*7e949c46SKenneth D. Merry 	/*
895*7e949c46SKenneth D. Merry 	 * Does the frontend request that we use rx copy?  If not, return an
896*7e949c46SKenneth D. Merry 	 * error because this driver only supports rx copy.
897*7e949c46SKenneth D. Merry 	 */
898*7e949c46SKenneth D. Merry 	err = xs_scanf(XST_NIL, otherend_path, "request-rx-copy", NULL,
899*7e949c46SKenneth D. Merry 		       "%" PRIu32, &rx_copy);
900*7e949c46SKenneth D. Merry 	if (err == ENOENT) {
901*7e949c46SKenneth D. Merry 		err = 0;
902*7e949c46SKenneth D. Merry 	 	rx_copy = 0;
903*7e949c46SKenneth D. Merry 	}
904*7e949c46SKenneth D. Merry 	if (err < 0) {
905*7e949c46SKenneth D. Merry 		xenbus_dev_fatal(xnb->dev, err, "reading %s/request-rx-copy",
906*7e949c46SKenneth D. Merry 				 otherend_path);
907*7e949c46SKenneth D. Merry 		return err;
908*7e949c46SKenneth D. Merry 	}
909*7e949c46SKenneth D. Merry 	/**
910*7e949c46SKenneth D. Merry 	 * \todo: figure out the exact meaning of this feature, and when
911*7e949c46SKenneth D. Merry 	 * the frontend will set it to true.  It should be set to true
912*7e949c46SKenneth D. Merry 	 * at some point
913*7e949c46SKenneth D. Merry 	 */
914*7e949c46SKenneth D. Merry /*        if (!rx_copy)*/
915*7e949c46SKenneth D. Merry /*          return EOPNOTSUPP;*/
916*7e949c46SKenneth D. Merry 
917*7e949c46SKenneth D. Merry 	/** \todo Collect the rx notify feature */
918*7e949c46SKenneth D. Merry 
919*7e949c46SKenneth D. Merry 	/*  Collect the feature-sg. */
920*7e949c46SKenneth D. Merry 	if (xs_scanf(XST_NIL, otherend_path, "feature-sg", NULL,
921*7e949c46SKenneth D. Merry 		     "%hhu", &xnb->can_sg) < 0)
922*7e949c46SKenneth D. Merry 		xnb->can_sg = 0;
923*7e949c46SKenneth D. Merry 
924*7e949c46SKenneth D. Merry 	/* Collect remaining frontend features */
925*7e949c46SKenneth D. Merry 	if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4", NULL,
926*7e949c46SKenneth D. Merry 		     "%hhu", &xnb->gso) < 0)
927*7e949c46SKenneth D. Merry 		xnb->gso = 0;
928*7e949c46SKenneth D. Merry 
929*7e949c46SKenneth D. Merry 	if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4-prefix", NULL,
930*7e949c46SKenneth D. Merry 		     "%hhu", &xnb->gso_prefix) < 0)
931*7e949c46SKenneth D. Merry 		xnb->gso_prefix = 0;
932*7e949c46SKenneth D. Merry 
933*7e949c46SKenneth D. Merry 	if (xs_scanf(XST_NIL, otherend_path, "feature-no-csum-offload", NULL,
934*7e949c46SKenneth D. Merry 		     "%hhu", &no_csum_offload) < 0)
935*7e949c46SKenneth D. Merry 		no_csum_offload = 0;
936*7e949c46SKenneth D. Merry 	xnb->ip_csum = (no_csum_offload == 0);
937*7e949c46SKenneth D. Merry 
938*7e949c46SKenneth D. Merry 	return (0);
939*7e949c46SKenneth D. Merry }
940*7e949c46SKenneth D. Merry 
941*7e949c46SKenneth D. Merry /**
942*7e949c46SKenneth D. Merry  * Supply information about the physical device to the frontend
943*7e949c46SKenneth D. Merry  * via XenBus.
944*7e949c46SKenneth D. Merry  *
945*7e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
946*7e949c46SKenneth D. Merry  */
947*7e949c46SKenneth D. Merry static int
948*7e949c46SKenneth D. Merry xnb_publish_backend_info(struct xnb_softc *xnb)
949*7e949c46SKenneth D. Merry {
950*7e949c46SKenneth D. Merry 	struct xs_transaction xst;
951*7e949c46SKenneth D. Merry 	const char *our_path;
952*7e949c46SKenneth D. Merry 	int error;
953*7e949c46SKenneth D. Merry 
954*7e949c46SKenneth D. Merry 	our_path = xenbus_get_node(xnb->dev);
955*7e949c46SKenneth D. Merry 
956*7e949c46SKenneth D. Merry 	do {
957*7e949c46SKenneth D. Merry 		error = xs_transaction_start(&xst);
958*7e949c46SKenneth D. Merry 		if (error != 0) {
959*7e949c46SKenneth D. Merry 			xenbus_dev_fatal(xnb->dev, error,
960*7e949c46SKenneth D. Merry 					 "Error publishing backend info "
961*7e949c46SKenneth D. Merry 					 "(start transaction)");
962*7e949c46SKenneth D. Merry 			break;
963*7e949c46SKenneth D. Merry 		}
964*7e949c46SKenneth D. Merry 
965*7e949c46SKenneth D. Merry 		error = xs_printf(xst, our_path, "feature-sg",
966*7e949c46SKenneth D. Merry 				  "%d", XNB_SG);
967*7e949c46SKenneth D. Merry 		if (error != 0)
968*7e949c46SKenneth D. Merry 			break;
969*7e949c46SKenneth D. Merry 
970*7e949c46SKenneth D. Merry 		error = xs_printf(xst, our_path, "feature-gso-tcpv4",
971*7e949c46SKenneth D. Merry 				  "%d", XNB_GSO_TCPV4);
972*7e949c46SKenneth D. Merry 		if (error != 0)
973*7e949c46SKenneth D. Merry 			break;
974*7e949c46SKenneth D. Merry 
975*7e949c46SKenneth D. Merry 		error = xs_printf(xst, our_path, "feature-rx-copy",
976*7e949c46SKenneth D. Merry 				  "%d", XNB_RX_COPY);
977*7e949c46SKenneth D. Merry 		if (error != 0)
978*7e949c46SKenneth D. Merry 			break;
979*7e949c46SKenneth D. Merry 
980*7e949c46SKenneth D. Merry 		error = xs_printf(xst, our_path, "feature-rx-flip",
981*7e949c46SKenneth D. Merry 				  "%d", XNB_RX_FLIP);
982*7e949c46SKenneth D. Merry 		if (error != 0)
983*7e949c46SKenneth D. Merry 			break;
984*7e949c46SKenneth D. Merry 
985*7e949c46SKenneth D. Merry 		error = xs_transaction_end(xst, 0);
986*7e949c46SKenneth D. Merry 		if (error != 0 && error != EAGAIN) {
987*7e949c46SKenneth D. Merry 			xenbus_dev_fatal(xnb->dev, error, "ending transaction");
988*7e949c46SKenneth D. Merry 			break;
989*7e949c46SKenneth D. Merry 		}
990*7e949c46SKenneth D. Merry 
991*7e949c46SKenneth D. Merry 	} while (error == EAGAIN);
992*7e949c46SKenneth D. Merry 
993*7e949c46SKenneth D. Merry 	return (error);
994*7e949c46SKenneth D. Merry }
995*7e949c46SKenneth D. Merry 
996*7e949c46SKenneth D. Merry /**
997*7e949c46SKenneth D. Merry  * Connect to our netfront peer now that it has completed publishing
998*7e949c46SKenneth D. Merry  * its configuration into the XenStore.
999*7e949c46SKenneth D. Merry  *
1000*7e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
1001*7e949c46SKenneth D. Merry  */
100289e0f4d2SKip Macy static void
1003*7e949c46SKenneth D. Merry xnb_connect(struct xnb_softc *xnb)
100489e0f4d2SKip Macy {
1005*7e949c46SKenneth D. Merry 	int	error;
100689e0f4d2SKip Macy 
1007*7e949c46SKenneth D. Merry 	if (xenbus_get_state(xnb->dev) == XenbusStateConnected)
100889e0f4d2SKip Macy 		return;
100989e0f4d2SKip Macy 
1010*7e949c46SKenneth D. Merry 	if (xnb_collect_xenstore_info(xnb) != 0)
1011*7e949c46SKenneth D. Merry 		return;
1012*7e949c46SKenneth D. Merry 
1013*7e949c46SKenneth D. Merry 	xnb->flags &= ~XNBF_SHUTDOWN;
1014*7e949c46SKenneth D. Merry 
1015*7e949c46SKenneth D. Merry 	/* Read front end configuration. */
1016*7e949c46SKenneth D. Merry 
1017*7e949c46SKenneth D. Merry 	/* Allocate resources whose size depends on front-end configuration. */
1018*7e949c46SKenneth D. Merry 	error = xnb_alloc_communication_mem(xnb);
1019*7e949c46SKenneth D. Merry 	if (error != 0) {
1020*7e949c46SKenneth D. Merry 		xenbus_dev_fatal(xnb->dev, error,
1021*7e949c46SKenneth D. Merry 				 "Unable to allocate communication memory");
1022*7e949c46SKenneth D. Merry 		return;
102389e0f4d2SKip Macy 	}
102489e0f4d2SKip Macy 
102589e0f4d2SKip Macy 	/*
1026*7e949c46SKenneth D. Merry 	 * Connect communication channel.
102789e0f4d2SKip Macy 	 */
1028*7e949c46SKenneth D. Merry 	error = xnb_connect_comms(xnb);
1029*7e949c46SKenneth D. Merry 	if (error != 0) {
1030*7e949c46SKenneth D. Merry 		/* Specific errors are reported by xnb_connect_comms(). */
1031*7e949c46SKenneth D. Merry 		return;
103289e0f4d2SKip Macy 	}
1033*7e949c46SKenneth D. Merry 	xnb->carrier = 1;
103489e0f4d2SKip Macy 
1035*7e949c46SKenneth D. Merry 	/* Ready for I/O. */
1036*7e949c46SKenneth D. Merry 	xenbus_set_state(xnb->dev, XenbusStateConnected);
103789e0f4d2SKip Macy }
103889e0f4d2SKip Macy 
1039*7e949c46SKenneth D. Merry /*-------------------------- Device Teardown Support -------------------------*/
1040*7e949c46SKenneth D. Merry /**
1041*7e949c46SKenneth D. Merry  * Perform device shutdown functions.
1042*7e949c46SKenneth D. Merry  *
1043*7e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
1044*7e949c46SKenneth D. Merry  *
1045*7e949c46SKenneth D. Merry  * Mark this instance as shutting down, wait for any active requests
1046*7e949c46SKenneth D. Merry  * to drain, disconnect from the front-end, and notify any waiters (e.g.
1047*7e949c46SKenneth D. Merry  * a thread invoking our detach method) that detach can now proceed.
1048*7e949c46SKenneth D. Merry  */
104989e0f4d2SKip Macy static int
1050*7e949c46SKenneth D. Merry xnb_shutdown(struct xnb_softc *xnb)
105189e0f4d2SKip Macy {
1052*7e949c46SKenneth D. Merry 	/*
1053*7e949c46SKenneth D. Merry 	 * Due to the need to drop our mutex during some
1054*7e949c46SKenneth D. Merry 	 * xenbus operations, it is possible for two threads
1055*7e949c46SKenneth D. Merry 	 * to attempt to close out shutdown processing at
1056*7e949c46SKenneth D. Merry 	 * the same time.  Tell the caller that hits this
1057*7e949c46SKenneth D. Merry 	 * race to try back later.
1058*7e949c46SKenneth D. Merry 	 */
1059*7e949c46SKenneth D. Merry 	if ((xnb->flags & XNBF_IN_SHUTDOWN) != 0)
1060*7e949c46SKenneth D. Merry 		return (EAGAIN);
106189e0f4d2SKip Macy 
1062*7e949c46SKenneth D. Merry 	xnb->flags |= XNBF_SHUTDOWN;
106389e0f4d2SKip Macy 
1064*7e949c46SKenneth D. Merry 	xnb->flags |= XNBF_IN_SHUTDOWN;
1065*7e949c46SKenneth D. Merry 
1066*7e949c46SKenneth D. Merry 	mtx_unlock(&xnb->sc_lock);
1067*7e949c46SKenneth D. Merry 	/* Free the network interface */
1068*7e949c46SKenneth D. Merry 	xnb->carrier = 0;
1069*7e949c46SKenneth D. Merry 	if (xnb->xnb_ifp != NULL) {
1070*7e949c46SKenneth D. Merry 		ether_ifdetach(xnb->xnb_ifp);
1071*7e949c46SKenneth D. Merry 		if_free(xnb->xnb_ifp);
1072*7e949c46SKenneth D. Merry 		xnb->xnb_ifp = NULL;
1073*7e949c46SKenneth D. Merry 	}
1074*7e949c46SKenneth D. Merry 	mtx_lock(&xnb->sc_lock);
1075*7e949c46SKenneth D. Merry 
1076*7e949c46SKenneth D. Merry 	xnb_disconnect(xnb);
1077*7e949c46SKenneth D. Merry 
1078*7e949c46SKenneth D. Merry 	mtx_unlock(&xnb->sc_lock);
1079*7e949c46SKenneth D. Merry 	if (xenbus_get_state(xnb->dev) < XenbusStateClosing)
1080*7e949c46SKenneth D. Merry 		xenbus_set_state(xnb->dev, XenbusStateClosing);
1081*7e949c46SKenneth D. Merry 	mtx_lock(&xnb->sc_lock);
1082*7e949c46SKenneth D. Merry 
1083*7e949c46SKenneth D. Merry 	xnb->flags &= ~XNBF_IN_SHUTDOWN;
1084*7e949c46SKenneth D. Merry 
1085*7e949c46SKenneth D. Merry 
1086*7e949c46SKenneth D. Merry 	/* Indicate to xnb_detach() that is it safe to proceed. */
1087*7e949c46SKenneth D. Merry 	wakeup(xnb);
1088*7e949c46SKenneth D. Merry 
1089*7e949c46SKenneth D. Merry 	return (0);
109089e0f4d2SKip Macy }
109189e0f4d2SKip Macy 
1092*7e949c46SKenneth D. Merry /**
1093*7e949c46SKenneth D. Merry  * Report an attach time error to the console and Xen, and cleanup
1094*7e949c46SKenneth D. Merry  * this instance by forcing immediate detach processing.
1095*7e949c46SKenneth D. Merry  *
1096*7e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
1097*7e949c46SKenneth D. Merry  * \param err  Errno describing the error.
1098*7e949c46SKenneth D. Merry  * \param fmt  Printf style format and arguments
1099*7e949c46SKenneth D. Merry  */
110089e0f4d2SKip Macy static void
1101*7e949c46SKenneth D. Merry xnb_attach_failed(struct xnb_softc *xnb, int err, const char *fmt, ...)
110289e0f4d2SKip Macy {
1103*7e949c46SKenneth D. Merry 	va_list ap;
1104*7e949c46SKenneth D. Merry 	va_list ap_hotplug;
110589e0f4d2SKip Macy 
1106*7e949c46SKenneth D. Merry 	va_start(ap, fmt);
1107*7e949c46SKenneth D. Merry 	va_copy(ap_hotplug, ap);
1108*7e949c46SKenneth D. Merry 	xs_vprintf(XST_NIL, xenbus_get_node(xnb->dev),
1109*7e949c46SKenneth D. Merry 		  "hotplug-error", fmt, ap_hotplug);
1110*7e949c46SKenneth D. Merry 	va_end(ap_hotplug);
1111*7e949c46SKenneth D. Merry 	xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
1112*7e949c46SKenneth D. Merry 		  "hotplug-status", "error");
111389e0f4d2SKip Macy 
1114*7e949c46SKenneth D. Merry 	xenbus_dev_vfatal(xnb->dev, err, fmt, ap);
1115*7e949c46SKenneth D. Merry 	va_end(ap);
1116*7e949c46SKenneth D. Merry 
1117*7e949c46SKenneth D. Merry 	xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
1118*7e949c46SKenneth D. Merry 		  "online", "0");
1119*7e949c46SKenneth D. Merry 	xnb_detach(xnb->dev);
112089e0f4d2SKip Macy }
112189e0f4d2SKip Macy 
1122*7e949c46SKenneth D. Merry /*---------------------------- NewBus Entrypoints ----------------------------*/
1123*7e949c46SKenneth D. Merry /**
1124*7e949c46SKenneth D. Merry  * Inspect a XenBus device and claim it if is of the appropriate type.
1125*7e949c46SKenneth D. Merry  *
1126*7e949c46SKenneth D. Merry  * \param dev  NewBus device object representing a candidate XenBus device.
1127*7e949c46SKenneth D. Merry  *
1128*7e949c46SKenneth D. Merry  * \return  0 for success, errno codes for failure.
1129*7e949c46SKenneth D. Merry  */
113089e0f4d2SKip Macy static int
1131*7e949c46SKenneth D. Merry xnb_probe(device_t dev)
113289e0f4d2SKip Macy {
1133*7e949c46SKenneth D. Merry 	 if (!strcmp(xenbus_get_type(dev), "vif")) {
1134*7e949c46SKenneth D. Merry 		DPRINTF("Claiming device %d, %s\n", device_get_unit(dev),
1135*7e949c46SKenneth D. Merry 		    devclass_get_name(device_get_devclass(dev)));
1136*7e949c46SKenneth D. Merry 		device_set_desc(dev, "Backend Virtual Network Device");
1137*7e949c46SKenneth D. Merry 		device_quiet(dev);
1138*7e949c46SKenneth D. Merry 		return (0);
1139*7e949c46SKenneth D. Merry 	}
1140*7e949c46SKenneth D. Merry 	return (ENXIO);
114189e0f4d2SKip Macy }
114289e0f4d2SKip Macy 
1143*7e949c46SKenneth D. Merry /**
1144*7e949c46SKenneth D. Merry  * Setup sysctl variables to control various Network Back parameters.
1145*7e949c46SKenneth D. Merry  *
1146*7e949c46SKenneth D. Merry  * \param xnb  Xen Net Back softc.
1147*7e949c46SKenneth D. Merry  *
1148*7e949c46SKenneth D. Merry  */
114989e0f4d2SKip Macy static void
1150*7e949c46SKenneth D. Merry xnb_setup_sysctl(struct xnb_softc *xnb)
115189e0f4d2SKip Macy {
1152*7e949c46SKenneth D. Merry 	struct sysctl_ctx_list *sysctl_ctx = NULL;
1153*7e949c46SKenneth D. Merry 	struct sysctl_oid      *sysctl_tree = NULL;
1154*7e949c46SKenneth D. Merry 
1155*7e949c46SKenneth D. Merry 	sysctl_ctx = device_get_sysctl_ctx(xnb->dev);
1156*7e949c46SKenneth D. Merry 	if (sysctl_ctx == NULL)
1157*7e949c46SKenneth D. Merry 		return;
1158*7e949c46SKenneth D. Merry 
1159*7e949c46SKenneth D. Merry 	sysctl_tree = device_get_sysctl_tree(xnb->dev);
1160*7e949c46SKenneth D. Merry 	if (sysctl_tree == NULL)
1161*7e949c46SKenneth D. Merry 		return;
1162*7e949c46SKenneth D. Merry 
1163*7e949c46SKenneth D. Merry #ifdef XNB_DEBUG
1164*7e949c46SKenneth D. Merry 	SYSCTL_ADD_PROC(sysctl_ctx,
1165*7e949c46SKenneth D. Merry 			SYSCTL_CHILDREN(sysctl_tree),
1166*7e949c46SKenneth D. Merry 			OID_AUTO,
1167*7e949c46SKenneth D. Merry 			"unit_test_results",
1168*7e949c46SKenneth D. Merry 			CTLTYPE_STRING | CTLFLAG_RD,
1169*7e949c46SKenneth D. Merry 			xnb,
1170*7e949c46SKenneth D. Merry 			0,
1171*7e949c46SKenneth D. Merry 			xnb_unit_test_main,
1172*7e949c46SKenneth D. Merry 			"A",
1173*7e949c46SKenneth D. Merry 			"Results of builtin unit tests");
1174*7e949c46SKenneth D. Merry 
1175*7e949c46SKenneth D. Merry 	SYSCTL_ADD_PROC(sysctl_ctx,
1176*7e949c46SKenneth D. Merry 			SYSCTL_CHILDREN(sysctl_tree),
1177*7e949c46SKenneth D. Merry 			OID_AUTO,
1178*7e949c46SKenneth D. Merry 			"dump_rings",
1179*7e949c46SKenneth D. Merry 			CTLTYPE_STRING | CTLFLAG_RD,
1180*7e949c46SKenneth D. Merry 			xnb,
1181*7e949c46SKenneth D. Merry 			0,
1182*7e949c46SKenneth D. Merry 			xnb_dump_rings,
1183*7e949c46SKenneth D. Merry 			"A",
1184*7e949c46SKenneth D. Merry 			"Xennet Back Rings");
1185*7e949c46SKenneth D. Merry #endif /* XNB_DEBUG */
1186*7e949c46SKenneth D. Merry }
1187*7e949c46SKenneth D. Merry 
1188*7e949c46SKenneth D. Merry /**
1189*7e949c46SKenneth D. Merry  * Create a network device.
1190*7e949c46SKenneth D. Merry  * @param handle device handle
1191*7e949c46SKenneth D. Merry  */
1192*7e949c46SKenneth D. Merry int
1193*7e949c46SKenneth D. Merry create_netdev(device_t dev)
1194*7e949c46SKenneth D. Merry {
1195*7e949c46SKenneth D. Merry 	struct ifnet *ifp;
1196*7e949c46SKenneth D. Merry 	struct xnb_softc *xnb;
1197*7e949c46SKenneth D. Merry 	int err = 0;
1198*7e949c46SKenneth D. Merry 
1199*7e949c46SKenneth D. Merry 	xnb = device_get_softc(dev);
1200*7e949c46SKenneth D. Merry 	mtx_init(&xnb->sc_lock, "xnb_softc", "xen netback softc lock", MTX_DEF);
1201*7e949c46SKenneth D. Merry 	mtx_init(&xnb->tx_lock, "xnb_tx", "xen netback tx lock", MTX_DEF);
1202*7e949c46SKenneth D. Merry 	mtx_init(&xnb->rx_lock, "xnb_rx", "xen netback rx lock", MTX_DEF);
1203*7e949c46SKenneth D. Merry 
1204*7e949c46SKenneth D. Merry 	xnb->dev = dev;
1205*7e949c46SKenneth D. Merry 
1206*7e949c46SKenneth D. Merry 	ifmedia_init(&xnb->sc_media, 0, xnb_ifmedia_upd, xnb_ifmedia_sts);
1207*7e949c46SKenneth D. Merry 	ifmedia_add(&xnb->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
1208*7e949c46SKenneth D. Merry 	ifmedia_set(&xnb->sc_media, IFM_ETHER|IFM_MANUAL);
1209*7e949c46SKenneth D. Merry 
1210*7e949c46SKenneth D. Merry 	err = xen_net_read_mac(dev, xnb->mac);
1211*7e949c46SKenneth D. Merry 	if (err == 0) {
1212*7e949c46SKenneth D. Merry 		/* Set up ifnet structure */
1213*7e949c46SKenneth D. Merry 		ifp = xnb->xnb_ifp = if_alloc(IFT_ETHER);
1214*7e949c46SKenneth D. Merry 		ifp->if_softc = xnb;
1215*7e949c46SKenneth D. Merry 		if_initname(ifp, "xnb",  device_get_unit(dev));
1216*7e949c46SKenneth D. Merry 		ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1217*7e949c46SKenneth D. Merry 		ifp->if_ioctl = xnb_ioctl;
1218*7e949c46SKenneth D. Merry 		ifp->if_output = ether_output;
1219*7e949c46SKenneth D. Merry 		ifp->if_start = xnb_start;
1220*7e949c46SKenneth D. Merry #ifdef notyet
1221*7e949c46SKenneth D. Merry 		ifp->if_watchdog = xnb_watchdog;
1222*7e949c46SKenneth D. Merry #endif
1223*7e949c46SKenneth D. Merry 		ifp->if_init = xnb_ifinit;
1224*7e949c46SKenneth D. Merry 		ifp->if_mtu = ETHERMTU;
1225*7e949c46SKenneth D. Merry 		ifp->if_snd.ifq_maxlen = NET_RX_RING_SIZE - 1;
1226*7e949c46SKenneth D. Merry 
1227*7e949c46SKenneth D. Merry 		ifp->if_hwassist = XNB_CSUM_FEATURES;
1228*7e949c46SKenneth D. Merry 		ifp->if_capabilities = IFCAP_HWCSUM;
1229*7e949c46SKenneth D. Merry 		ifp->if_capenable = IFCAP_HWCSUM;
1230*7e949c46SKenneth D. Merry 
1231*7e949c46SKenneth D. Merry 		ether_ifattach(ifp, xnb->mac);
1232*7e949c46SKenneth D. Merry 		xnb->carrier = 0;
1233*7e949c46SKenneth D. Merry 	}
1234*7e949c46SKenneth D. Merry 
1235*7e949c46SKenneth D. Merry 	return err;
1236*7e949c46SKenneth D. Merry }
1237*7e949c46SKenneth D. Merry 
1238*7e949c46SKenneth D. Merry /**
1239*7e949c46SKenneth D. Merry  * Attach to a XenBus device that has been claimed by our probe routine.
1240*7e949c46SKenneth D. Merry  *
1241*7e949c46SKenneth D. Merry  * \param dev  NewBus device object representing this Xen Net Back instance.
1242*7e949c46SKenneth D. Merry  *
1243*7e949c46SKenneth D. Merry  * \return  0 for success, errno codes for failure.
1244*7e949c46SKenneth D. Merry  */
1245*7e949c46SKenneth D. Merry static int
1246*7e949c46SKenneth D. Merry xnb_attach(device_t dev)
1247*7e949c46SKenneth D. Merry {
1248*7e949c46SKenneth D. Merry 	struct xnb_softc *xnb;
1249*7e949c46SKenneth D. Merry 	int	error;
1250*7e949c46SKenneth D. Merry 	xnb_ring_type_t	i;
1251*7e949c46SKenneth D. Merry 
1252*7e949c46SKenneth D. Merry 	error = create_netdev(dev);
1253*7e949c46SKenneth D. Merry 	if (error != 0) {
1254*7e949c46SKenneth D. Merry 		xenbus_dev_fatal(dev, error, "creating netdev");
1255*7e949c46SKenneth D. Merry 		return (error);
1256*7e949c46SKenneth D. Merry 	}
1257*7e949c46SKenneth D. Merry 
1258*7e949c46SKenneth D. Merry 	DPRINTF("Attaching to %s\n", xenbus_get_node(dev));
1259*7e949c46SKenneth D. Merry 
1260*7e949c46SKenneth D. Merry 	/*
1261*7e949c46SKenneth D. Merry 	 * Basic initialization.
1262*7e949c46SKenneth D. Merry 	 * After this block it is safe to call xnb_detach()
1263*7e949c46SKenneth D. Merry 	 * to clean up any allocated data for this instance.
1264*7e949c46SKenneth D. Merry 	 */
1265*7e949c46SKenneth D. Merry 	xnb = device_get_softc(dev);
1266*7e949c46SKenneth D. Merry 	xnb->otherend_id = xenbus_get_otherend_id(dev);
1267*7e949c46SKenneth D. Merry 	for (i=0; i < XNB_NUM_RING_TYPES; i++) {
1268*7e949c46SKenneth D. Merry 		xnb->ring_configs[i].ring_pages = 1;
1269*7e949c46SKenneth D. Merry 	}
1270*7e949c46SKenneth D. Merry 
1271*7e949c46SKenneth D. Merry 	/*
1272*7e949c46SKenneth D. Merry 	 * Setup sysctl variables.
1273*7e949c46SKenneth D. Merry 	 */
1274*7e949c46SKenneth D. Merry 	xnb_setup_sysctl(xnb);
1275*7e949c46SKenneth D. Merry 
1276*7e949c46SKenneth D. Merry 	/* Update hot-plug status to satisfy xend. */
1277*7e949c46SKenneth D. Merry 	error = xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
1278*7e949c46SKenneth D. Merry 			  "hotplug-status", "connected");
1279*7e949c46SKenneth D. Merry 	if (error != 0) {
1280*7e949c46SKenneth D. Merry 		xnb_attach_failed(xnb, error, "writing %s/hotplug-status",
1281*7e949c46SKenneth D. Merry 				  xenbus_get_node(xnb->dev));
1282*7e949c46SKenneth D. Merry 		return (error);
1283*7e949c46SKenneth D. Merry 	}
1284*7e949c46SKenneth D. Merry 
1285*7e949c46SKenneth D. Merry 	if ((error = xnb_publish_backend_info(xnb)) != 0) {
1286*7e949c46SKenneth D. Merry 		/*
1287*7e949c46SKenneth D. Merry 		 * If we can't publish our data, we cannot participate
1288*7e949c46SKenneth D. Merry 		 * in this connection, and waiting for a front-end state
1289*7e949c46SKenneth D. Merry 		 * change will not help the situation.
1290*7e949c46SKenneth D. Merry 		 */
1291*7e949c46SKenneth D. Merry 		xnb_attach_failed(xnb, error,
1292*7e949c46SKenneth D. Merry 		    "Publishing backend status for %s",
1293*7e949c46SKenneth D. Merry 				  xenbus_get_node(xnb->dev));
1294*7e949c46SKenneth D. Merry 		return error;
1295*7e949c46SKenneth D. Merry 	}
1296*7e949c46SKenneth D. Merry 
1297*7e949c46SKenneth D. Merry 	/* Tell the front end that we are ready to connect. */
1298*7e949c46SKenneth D. Merry 	xenbus_set_state(dev, XenbusStateInitWait);
1299*7e949c46SKenneth D. Merry 
1300*7e949c46SKenneth D. Merry 	return (0);
1301*7e949c46SKenneth D. Merry }
1302*7e949c46SKenneth D. Merry 
1303*7e949c46SKenneth D. Merry /**
1304*7e949c46SKenneth D. Merry  * Detach from a net back device instance.
1305*7e949c46SKenneth D. Merry  *
1306*7e949c46SKenneth D. Merry  * \param dev  NewBus device object representing this Xen Net Back instance.
1307*7e949c46SKenneth D. Merry  *
1308*7e949c46SKenneth D. Merry  * \return  0 for success, errno codes for failure.
1309*7e949c46SKenneth D. Merry  *
1310*7e949c46SKenneth D. Merry  * \note A net back device may be detached at any time in its life-cycle,
1311*7e949c46SKenneth D. Merry  *       including part way through the attach process.  For this reason,
1312*7e949c46SKenneth D. Merry  *       initialization order and the intialization state checks in this
1313*7e949c46SKenneth D. Merry  *       routine must be carefully coupled so that attach time failures
1314*7e949c46SKenneth D. Merry  *       are gracefully handled.
1315*7e949c46SKenneth D. Merry  */
1316*7e949c46SKenneth D. Merry static int
1317*7e949c46SKenneth D. Merry xnb_detach(device_t dev)
1318*7e949c46SKenneth D. Merry {
1319*7e949c46SKenneth D. Merry 	struct xnb_softc *xnb;
1320*7e949c46SKenneth D. Merry 
132189e0f4d2SKip Macy 	DPRINTF("\n");
132289e0f4d2SKip Macy 
1323*7e949c46SKenneth D. Merry 	xnb = device_get_softc(dev);
1324*7e949c46SKenneth D. Merry 	mtx_lock(&xnb->sc_lock);
1325*7e949c46SKenneth D. Merry 	while (xnb_shutdown(xnb) == EAGAIN) {
1326*7e949c46SKenneth D. Merry 		msleep(xnb, &xnb->sc_lock, /*wakeup prio unchanged*/0,
1327*7e949c46SKenneth D. Merry 		       "xnb_shutdown", 0);
132889e0f4d2SKip Macy 	}
1329*7e949c46SKenneth D. Merry 	mtx_unlock(&xnb->sc_lock);
1330*7e949c46SKenneth D. Merry 	DPRINTF("\n");
1331*7e949c46SKenneth D. Merry 
1332*7e949c46SKenneth D. Merry 	mtx_destroy(&xnb->tx_lock);
1333*7e949c46SKenneth D. Merry 	mtx_destroy(&xnb->rx_lock);
1334*7e949c46SKenneth D. Merry 	mtx_destroy(&xnb->sc_lock);
1335*7e949c46SKenneth D. Merry 	return (0);
133689e0f4d2SKip Macy }
133789e0f4d2SKip Macy 
1338*7e949c46SKenneth D. Merry /**
1339*7e949c46SKenneth D. Merry  * Prepare this net back device for suspension of this VM.
1340*7e949c46SKenneth D. Merry  *
1341*7e949c46SKenneth D. Merry  * \param dev  NewBus device object representing this Xen net Back instance.
1342*7e949c46SKenneth D. Merry  *
1343*7e949c46SKenneth D. Merry  * \return  0 for success, errno codes for failure.
1344*7e949c46SKenneth D. Merry  */
1345*7e949c46SKenneth D. Merry static int
1346*7e949c46SKenneth D. Merry xnb_suspend(device_t dev)
1347*7e949c46SKenneth D. Merry {
1348*7e949c46SKenneth D. Merry 	return (0);
1349*7e949c46SKenneth D. Merry }
1350*7e949c46SKenneth D. Merry 
1351*7e949c46SKenneth D. Merry /**
1352*7e949c46SKenneth D. Merry  * Perform any processing required to recover from a suspended state.
1353*7e949c46SKenneth D. Merry  *
1354*7e949c46SKenneth D. Merry  * \param dev  NewBus device object representing this Xen Net Back instance.
1355*7e949c46SKenneth D. Merry  *
1356*7e949c46SKenneth D. Merry  * \return  0 for success, errno codes for failure.
1357*7e949c46SKenneth D. Merry  */
1358*7e949c46SKenneth D. Merry static int
1359*7e949c46SKenneth D. Merry xnb_resume(device_t dev)
1360*7e949c46SKenneth D. Merry {
1361*7e949c46SKenneth D. Merry 	return (0);
1362*7e949c46SKenneth D. Merry }
1363*7e949c46SKenneth D. Merry 
1364*7e949c46SKenneth D. Merry /**
1365*7e949c46SKenneth D. Merry  * Handle state changes expressed via the XenStore by our front-end peer.
1366*7e949c46SKenneth D. Merry  *
1367*7e949c46SKenneth D. Merry  * \param dev             NewBus device object representing this Xen
1368*7e949c46SKenneth D. Merry  *                        Net Back instance.
1369*7e949c46SKenneth D. Merry  * \param frontend_state  The new state of the front-end.
1370*7e949c46SKenneth D. Merry  *
1371*7e949c46SKenneth D. Merry  * \return  0 for success, errno codes for failure.
1372*7e949c46SKenneth D. Merry  */
137389e0f4d2SKip Macy static void
1374*7e949c46SKenneth D. Merry xnb_frontend_changed(device_t dev, XenbusState frontend_state)
137589e0f4d2SKip Macy {
1376*7e949c46SKenneth D. Merry 	struct xnb_softc *xnb;
137789e0f4d2SKip Macy 
1378*7e949c46SKenneth D. Merry 	xnb = device_get_softc(dev);
137989e0f4d2SKip Macy 
1380*7e949c46SKenneth D. Merry 	DPRINTF("frontend_state=%s, xnb_state=%s\n",
1381*7e949c46SKenneth D. Merry 	        xenbus_strstate(frontend_state),
1382*7e949c46SKenneth D. Merry 		xenbus_strstate(xenbus_get_state(xnb->dev)));
138389e0f4d2SKip Macy 
138489e0f4d2SKip Macy 	switch (frontend_state) {
138589e0f4d2SKip Macy 	case XenbusStateInitialising:
138689e0f4d2SKip Macy 		break;
1387*7e949c46SKenneth D. Merry 	case XenbusStateInitialised:
138889e0f4d2SKip Macy 	case XenbusStateConnected:
1389*7e949c46SKenneth D. Merry 		xnb_connect(xnb);
139089e0f4d2SKip Macy 		break;
139189e0f4d2SKip Macy 	case XenbusStateClosing:
139289e0f4d2SKip Macy 	case XenbusStateClosed:
1393*7e949c46SKenneth D. Merry 		mtx_lock(&xnb->sc_lock);
1394*7e949c46SKenneth D. Merry 		xnb_shutdown(xnb);
1395*7e949c46SKenneth D. Merry 		mtx_unlock(&xnb->sc_lock);
1396*7e949c46SKenneth D. Merry 		if (frontend_state == XenbusStateClosed)
1397*7e949c46SKenneth D. Merry 			xenbus_set_state(xnb->dev, XenbusStateClosed);
139889e0f4d2SKip Macy 		break;
1399*7e949c46SKenneth D. Merry 	default:
1400*7e949c46SKenneth D. Merry 		xenbus_dev_fatal(xnb->dev, EINVAL, "saw state %d at frontend",
140189e0f4d2SKip Macy 				 frontend_state);
140289e0f4d2SKip Macy 		break;
140389e0f4d2SKip Macy 	}
140489e0f4d2SKip Macy }
140589e0f4d2SKip Macy 
140689e0f4d2SKip Macy 
1407*7e949c46SKenneth D. Merry /*---------------------------- Request Processing ----------------------------*/
1408*7e949c46SKenneth D. Merry /**
1409*7e949c46SKenneth D. Merry  * Interrupt handler bound to the shared ring's event channel.
1410*7e949c46SKenneth D. Merry  * Entry point for the xennet transmit path in netback
1411*7e949c46SKenneth D. Merry  * Transfers packets from the Xen ring to the host's generic networking stack
1412*7e949c46SKenneth D. Merry  *
1413*7e949c46SKenneth D. Merry  * \param arg  Callback argument registerd during event channel
1414*7e949c46SKenneth D. Merry  *             binding - the xnb_softc for this instance.
1415*7e949c46SKenneth D. Merry  */
141689e0f4d2SKip Macy static void
1417*7e949c46SKenneth D. Merry xnb_intr(void *arg)
141889e0f4d2SKip Macy {
1419*7e949c46SKenneth D. Merry 	struct xnb_softc *xnb;
1420*7e949c46SKenneth D. Merry 	struct ifnet *ifp;
1421*7e949c46SKenneth D. Merry 	netif_tx_back_ring_t *txb;
1422*7e949c46SKenneth D. Merry 	RING_IDX req_prod_local;
142389e0f4d2SKip Macy 
1424*7e949c46SKenneth D. Merry 	xnb = (struct xnb_softc *)arg;
1425*7e949c46SKenneth D. Merry 	ifp = xnb->xnb_ifp;
1426*7e949c46SKenneth D. Merry 	txb = &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring;
142789e0f4d2SKip Macy 
1428*7e949c46SKenneth D. Merry 	mtx_lock(&xnb->tx_lock);
1429*7e949c46SKenneth D. Merry 	do {
1430*7e949c46SKenneth D. Merry 		int notify;
1431*7e949c46SKenneth D. Merry 		req_prod_local = txb->sring->req_prod;
1432*7e949c46SKenneth D. Merry 		xen_rmb();
143389e0f4d2SKip Macy 
1434*7e949c46SKenneth D. Merry 		for (;;) {
1435*7e949c46SKenneth D. Merry 			struct mbuf *mbufc;
143689e0f4d2SKip Macy 			int err;
143789e0f4d2SKip Macy 
1438*7e949c46SKenneth D. Merry 			err = xnb_recv(txb, xnb->otherend_id, &mbufc, ifp,
1439*7e949c46SKenneth D. Merry 			    	       xnb->tx_gnttab);
1440*7e949c46SKenneth D. Merry 			if (err || (mbufc == NULL))
1441*7e949c46SKenneth D. Merry 				break;
1442*7e949c46SKenneth D. Merry 
1443*7e949c46SKenneth D. Merry 			/* Send the packet to the generic network stack */
1444*7e949c46SKenneth D. Merry 			(*xnb->xnb_ifp->if_input)(xnb->xnb_ifp, mbufc);
1445*7e949c46SKenneth D. Merry 		}
1446*7e949c46SKenneth D. Merry 
1447*7e949c46SKenneth D. Merry 		RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(txb, notify);
1448*7e949c46SKenneth D. Merry 		if (notify != 0)
1449*7e949c46SKenneth D. Merry 			notify_remote_via_irq(xnb->irq);
1450*7e949c46SKenneth D. Merry 
1451*7e949c46SKenneth D. Merry 		txb->sring->req_event = txb->req_cons + 1;
1452*7e949c46SKenneth D. Merry 		xen_mb();
1453*7e949c46SKenneth D. Merry 	} while (txb->sring->req_prod != req_prod_local) ;
1454*7e949c46SKenneth D. Merry 	mtx_unlock(&xnb->tx_lock);
1455*7e949c46SKenneth D. Merry 
1456*7e949c46SKenneth D. Merry 	xnb_start(ifp);
1457*7e949c46SKenneth D. Merry }
1458*7e949c46SKenneth D. Merry 
1459*7e949c46SKenneth D. Merry 
1460*7e949c46SKenneth D. Merry /**
1461*7e949c46SKenneth D. Merry  * Build a struct xnb_pkt based on netif_tx_request's from a netif tx ring.
1462*7e949c46SKenneth D. Merry  * Will read exactly 0 or 1 packets from the ring; never a partial packet.
1463*7e949c46SKenneth D. Merry  * \param[out]	pkt	The returned packet.  If there is an error building
1464*7e949c46SKenneth D. Merry  * 			the packet, pkt.list_len will be set to 0.
1465*7e949c46SKenneth D. Merry  * \param[in]	tx_ring	Pointer to the Ring that is the input to this function
1466*7e949c46SKenneth D. Merry  * \param[in]	start	The ring index of the first potential request
1467*7e949c46SKenneth D. Merry  * \return		The number of requests consumed to build this packet
1468*7e949c46SKenneth D. Merry  */
1469*7e949c46SKenneth D. Merry static int
1470*7e949c46SKenneth D. Merry xnb_ring2pkt(struct xnb_pkt *pkt, const netif_tx_back_ring_t *tx_ring,
1471*7e949c46SKenneth D. Merry 	     RING_IDX start)
1472*7e949c46SKenneth D. Merry {
1473*7e949c46SKenneth D. Merry 	/*
1474*7e949c46SKenneth D. Merry 	 * Outline:
1475*7e949c46SKenneth D. Merry 	 * 1) Initialize pkt
1476*7e949c46SKenneth D. Merry 	 * 2) Read the first request of the packet
1477*7e949c46SKenneth D. Merry 	 * 3) Read the extras
1478*7e949c46SKenneth D. Merry 	 * 4) Set cdr
1479*7e949c46SKenneth D. Merry 	 * 5) Loop on the remainder of the packet
1480*7e949c46SKenneth D. Merry 	 * 6) Finalize pkt (stuff like car_size and list_len)
1481*7e949c46SKenneth D. Merry 	 */
1482*7e949c46SKenneth D. Merry 	int idx = start;
1483*7e949c46SKenneth D. Merry 	int discard = 0;	/* whether to discard the packet */
1484*7e949c46SKenneth D. Merry 	int more_data = 0;	/* there are more request past the last one */
1485*7e949c46SKenneth D. Merry 	uint16_t cdr_size = 0;	/* accumulated size of requests 2 through n */
1486*7e949c46SKenneth D. Merry 
1487*7e949c46SKenneth D. Merry 	xnb_pkt_initialize(pkt);
1488*7e949c46SKenneth D. Merry 
1489*7e949c46SKenneth D. Merry 	/* Read the first request */
1490*7e949c46SKenneth D. Merry 	if (RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1491*7e949c46SKenneth D. Merry 		netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx);
1492*7e949c46SKenneth D. Merry 		pkt->size = tx->size;
1493*7e949c46SKenneth D. Merry 		pkt->flags = tx->flags & ~NETTXF_more_data;
1494*7e949c46SKenneth D. Merry 		more_data = tx->flags & NETTXF_more_data;
1495*7e949c46SKenneth D. Merry 		pkt->list_len++;
1496*7e949c46SKenneth D. Merry 		pkt->car = idx;
1497*7e949c46SKenneth D. Merry 		idx++;
1498*7e949c46SKenneth D. Merry 	}
1499*7e949c46SKenneth D. Merry 
1500*7e949c46SKenneth D. Merry 	/* Read the extra info */
1501*7e949c46SKenneth D. Merry 	if ((pkt->flags & NETTXF_extra_info) &&
1502*7e949c46SKenneth D. Merry 	    RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1503*7e949c46SKenneth D. Merry 		netif_extra_info_t *ext =
1504*7e949c46SKenneth D. Merry 		    (netif_extra_info_t*) RING_GET_REQUEST(tx_ring, idx);
1505*7e949c46SKenneth D. Merry 		pkt->extra.type = ext->type;
1506*7e949c46SKenneth D. Merry 		switch (pkt->extra.type) {
1507*7e949c46SKenneth D. Merry 			case XEN_NETIF_EXTRA_TYPE_GSO:
1508*7e949c46SKenneth D. Merry 				pkt->extra.u.gso = ext->u.gso;
150989e0f4d2SKip Macy 				break;
151089e0f4d2SKip Macy 			default:
1511*7e949c46SKenneth D. Merry 				/*
1512*7e949c46SKenneth D. Merry 				 * The reference Linux netfront driver will
1513*7e949c46SKenneth D. Merry 				 * never set any other extra.type.  So we don't
1514*7e949c46SKenneth D. Merry 				 * know what to do with it.  Let's print an
1515*7e949c46SKenneth D. Merry 				 * error, then consume and discard the packet
1516*7e949c46SKenneth D. Merry 				 */
1517*7e949c46SKenneth D. Merry 				printf("xnb(%s:%d): Unknown extra info type %d."
1518*7e949c46SKenneth D. Merry 				       "  Discarding packet\n",
1519*7e949c46SKenneth D. Merry 				       __func__, __LINE__, pkt->extra.type);
1520*7e949c46SKenneth D. Merry 				xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring,
1521*7e949c46SKenneth D. Merry 				    start));
1522*7e949c46SKenneth D. Merry 				xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring,
1523*7e949c46SKenneth D. Merry 				    idx));
1524*7e949c46SKenneth D. Merry 				discard = 1;
1525*7e949c46SKenneth D. Merry 				break;
152689e0f4d2SKip Macy 		}
152789e0f4d2SKip Macy 
1528*7e949c46SKenneth D. Merry 		pkt->extra.flags = ext->flags;
1529*7e949c46SKenneth D. Merry 		if (ext->flags & XEN_NETIF_EXTRA_FLAG_MORE) {
1530*7e949c46SKenneth D. Merry 			/*
1531*7e949c46SKenneth D. Merry 			 * The reference linux netfront driver never sets this
1532*7e949c46SKenneth D. Merry 			 * flag (nor does any other known netfront).  So we
1533*7e949c46SKenneth D. Merry 			 * will discard the packet.
1534*7e949c46SKenneth D. Merry 			 */
1535*7e949c46SKenneth D. Merry 			printf("xnb(%s:%d): Request sets "
1536*7e949c46SKenneth D. Merry 			    "XEN_NETIF_EXTRA_FLAG_MORE, but we can't handle "
1537*7e949c46SKenneth D. Merry 			    "that\n", __func__, __LINE__);
1538*7e949c46SKenneth D. Merry 			xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start));
1539*7e949c46SKenneth D. Merry 			xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx));
1540*7e949c46SKenneth D. Merry 			discard = 1;
154189e0f4d2SKip Macy 		}
154289e0f4d2SKip Macy 
1543*7e949c46SKenneth D. Merry 		idx++;
1544*7e949c46SKenneth D. Merry 	}
1545*7e949c46SKenneth D. Merry 
1546*7e949c46SKenneth D. Merry 	/* Set cdr.  If there is not more data, cdr is invalid */
1547*7e949c46SKenneth D. Merry 	pkt->cdr = idx;
1548*7e949c46SKenneth D. Merry 
1549*7e949c46SKenneth D. Merry 	/* Loop on remainder of packet */
1550*7e949c46SKenneth D. Merry 	while (more_data && RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1551*7e949c46SKenneth D. Merry 		netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx);
1552*7e949c46SKenneth D. Merry 		pkt->list_len++;
1553*7e949c46SKenneth D. Merry 		cdr_size += tx->size;
1554*7e949c46SKenneth D. Merry 		if (tx->flags & ~NETTXF_more_data) {
1555*7e949c46SKenneth D. Merry 			/* There should be no other flags set at this point */
1556*7e949c46SKenneth D. Merry 			printf("xnb(%s:%d): Request sets unknown flags %d "
1557*7e949c46SKenneth D. Merry 			    "after the 1st request in the packet.\n",
1558*7e949c46SKenneth D. Merry 			    __func__, __LINE__, tx->flags);
1559*7e949c46SKenneth D. Merry 			xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start));
1560*7e949c46SKenneth D. Merry 			xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx));
1561*7e949c46SKenneth D. Merry 		}
1562*7e949c46SKenneth D. Merry 
1563*7e949c46SKenneth D. Merry 		more_data = tx->flags & NETTXF_more_data;
1564*7e949c46SKenneth D. Merry 		idx++;
1565*7e949c46SKenneth D. Merry 	}
1566*7e949c46SKenneth D. Merry 
1567*7e949c46SKenneth D. Merry 	/* Finalize packet */
1568*7e949c46SKenneth D. Merry 	if (more_data != 0) {
1569*7e949c46SKenneth D. Merry 		/* The ring ran out of requests before finishing the packet */
1570*7e949c46SKenneth D. Merry 		xnb_pkt_invalidate(pkt);
1571*7e949c46SKenneth D. Merry 		idx = start;	/* tell caller that we consumed no requests */
1572*7e949c46SKenneth D. Merry 	} else {
1573*7e949c46SKenneth D. Merry 		/* Calculate car_size */
1574*7e949c46SKenneth D. Merry 		pkt->car_size = pkt->size - cdr_size;
1575*7e949c46SKenneth D. Merry 	}
1576*7e949c46SKenneth D. Merry 	if (discard != 0) {
1577*7e949c46SKenneth D. Merry 		xnb_pkt_invalidate(pkt);
1578*7e949c46SKenneth D. Merry 	}
1579*7e949c46SKenneth D. Merry 
1580*7e949c46SKenneth D. Merry 	return idx - start;
1581*7e949c46SKenneth D. Merry }
1582*7e949c46SKenneth D. Merry 
1583*7e949c46SKenneth D. Merry 
1584*7e949c46SKenneth D. Merry /**
1585*7e949c46SKenneth D. Merry  * Respond to all the requests that constituted pkt.  Builds the responses and
1586*7e949c46SKenneth D. Merry  * writes them to the ring, but doesn't push them to the shared ring.
1587*7e949c46SKenneth D. Merry  * \param[in] pkt	the packet that needs a response
1588*7e949c46SKenneth D. Merry  * \param[in] error	true if there was an error handling the packet, such
1589*7e949c46SKenneth D. Merry  * 			as in the hypervisor copy op or mbuf allocation
1590*7e949c46SKenneth D. Merry  * \param[out] ring	Responses go here
1591*7e949c46SKenneth D. Merry  */
1592*7e949c46SKenneth D. Merry static void
1593*7e949c46SKenneth D. Merry xnb_txpkt2rsp(const struct xnb_pkt *pkt, netif_tx_back_ring_t *ring,
1594*7e949c46SKenneth D. Merry 	      int error)
159589e0f4d2SKip Macy {
1596*7e949c46SKenneth D. Merry 	/*
1597*7e949c46SKenneth D. Merry 	 * Outline:
1598*7e949c46SKenneth D. Merry 	 * 1) Respond to the first request
1599*7e949c46SKenneth D. Merry 	 * 2) Respond to the extra info reques
1600*7e949c46SKenneth D. Merry 	 * Loop through every remaining request in the packet, generating
1601*7e949c46SKenneth D. Merry 	 * responses that copy those requests' ids and sets the status
1602*7e949c46SKenneth D. Merry 	 * appropriately.
1603*7e949c46SKenneth D. Merry 	 */
1604*7e949c46SKenneth D. Merry 	netif_tx_request_t *tx;
1605*7e949c46SKenneth D. Merry 	netif_tx_response_t *rsp;
1606*7e949c46SKenneth D. Merry 	int i;
1607*7e949c46SKenneth D. Merry 	uint16_t status;
1608*7e949c46SKenneth D. Merry 
1609*7e949c46SKenneth D. Merry 	status = (xnb_pkt_is_valid(pkt) == 0) || error ?
1610*7e949c46SKenneth D. Merry 		NETIF_RSP_ERROR : NETIF_RSP_OKAY;
1611*7e949c46SKenneth D. Merry 	KASSERT((pkt->list_len == 0) || (ring->rsp_prod_pvt == pkt->car),
1612*7e949c46SKenneth D. Merry 	    ("Cannot respond to ring requests out of order"));
1613*7e949c46SKenneth D. Merry 
1614*7e949c46SKenneth D. Merry 	if (pkt->list_len >= 1) {
1615*7e949c46SKenneth D. Merry 		uint16_t id;
1616*7e949c46SKenneth D. Merry 		tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt);
1617*7e949c46SKenneth D. Merry 		id = tx->id;
1618*7e949c46SKenneth D. Merry 		rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1619*7e949c46SKenneth D. Merry 		rsp->id = id;
1620*7e949c46SKenneth D. Merry 		rsp->status = status;
1621*7e949c46SKenneth D. Merry 		ring->rsp_prod_pvt++;
1622*7e949c46SKenneth D. Merry 
1623*7e949c46SKenneth D. Merry 		if (pkt->flags & NETRXF_extra_info) {
1624*7e949c46SKenneth D. Merry 			rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1625*7e949c46SKenneth D. Merry 			rsp->status = NETIF_RSP_NULL;
1626*7e949c46SKenneth D. Merry 			ring->rsp_prod_pvt++;
1627*7e949c46SKenneth D. Merry 		}
1628*7e949c46SKenneth D. Merry 	}
1629*7e949c46SKenneth D. Merry 
1630*7e949c46SKenneth D. Merry 	for (i=0; i < pkt->list_len - 1; i++) {
1631*7e949c46SKenneth D. Merry 		uint16_t id;
1632*7e949c46SKenneth D. Merry 		tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt);
1633*7e949c46SKenneth D. Merry 		id = tx->id;
1634*7e949c46SKenneth D. Merry 		rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1635*7e949c46SKenneth D. Merry 		rsp->id = id;
1636*7e949c46SKenneth D. Merry 		rsp->status = status;
1637*7e949c46SKenneth D. Merry 		ring->rsp_prod_pvt++;
1638*7e949c46SKenneth D. Merry 	}
1639*7e949c46SKenneth D. Merry }
1640*7e949c46SKenneth D. Merry 
1641*7e949c46SKenneth D. Merry /**
1642*7e949c46SKenneth D. Merry  * Create an mbuf chain to represent a packet.  Initializes all of the headers
1643*7e949c46SKenneth D. Merry  * in the mbuf chain, but does not copy the data.  The returned chain must be
1644*7e949c46SKenneth D. Merry  * free()'d when no longer needed
1645*7e949c46SKenneth D. Merry  * \param[in]	pkt	A packet to model the mbuf chain after
1646*7e949c46SKenneth D. Merry  * \return	A newly allocated mbuf chain, possibly with clusters attached.
1647*7e949c46SKenneth D. Merry  * 		NULL on failure
1648*7e949c46SKenneth D. Merry  */
1649*7e949c46SKenneth D. Merry static struct mbuf*
1650*7e949c46SKenneth D. Merry xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp)
1651*7e949c46SKenneth D. Merry {
1652*7e949c46SKenneth D. Merry 	/**
1653*7e949c46SKenneth D. Merry 	 * \todo consider using a memory pool for mbufs instead of
1654*7e949c46SKenneth D. Merry 	 * reallocating them for every packet
1655*7e949c46SKenneth D. Merry 	 */
1656*7e949c46SKenneth D. Merry 	/** \todo handle extra data */
1657*7e949c46SKenneth D. Merry 	struct mbuf *m;
1658*7e949c46SKenneth D. Merry 
1659*7e949c46SKenneth D. Merry 	m = m_getm(NULL, pkt->size, M_NOWAIT, MT_DATA);
1660*7e949c46SKenneth D. Merry 
1661*7e949c46SKenneth D. Merry 	if (m != NULL) {
1662*7e949c46SKenneth D. Merry 		m->m_pkthdr.rcvif = ifp;
1663*7e949c46SKenneth D. Merry 		if (pkt->flags & NETTXF_data_validated) {
1664*7e949c46SKenneth D. Merry 			/*
1665*7e949c46SKenneth D. Merry 			 * We lie to the host OS and always tell it that the
1666*7e949c46SKenneth D. Merry 			 * checksums are ok, because the packet is unlikely to
1667*7e949c46SKenneth D. Merry 			 * get corrupted going across domains.
1668*7e949c46SKenneth D. Merry 			 */
1669*7e949c46SKenneth D. Merry 			m->m_pkthdr.csum_flags = (
1670*7e949c46SKenneth D. Merry 				CSUM_IP_CHECKED |
1671*7e949c46SKenneth D. Merry 				CSUM_IP_VALID   |
1672*7e949c46SKenneth D. Merry 				CSUM_DATA_VALID |
1673*7e949c46SKenneth D. Merry 				CSUM_PSEUDO_HDR
1674*7e949c46SKenneth D. Merry 				);
1675*7e949c46SKenneth D. Merry 			m->m_pkthdr.csum_data = 0xffff;
1676*7e949c46SKenneth D. Merry 		}
1677*7e949c46SKenneth D. Merry 	}
1678*7e949c46SKenneth D. Merry 	return m;
1679*7e949c46SKenneth D. Merry }
1680*7e949c46SKenneth D. Merry 
1681*7e949c46SKenneth D. Merry /**
1682*7e949c46SKenneth D. Merry  * Build a gnttab_copy table that can be used to copy data from a pkt
1683*7e949c46SKenneth D. Merry  * to an mbufc.  Does not actually perform the copy.  Always uses gref's on
1684*7e949c46SKenneth D. Merry  * the packet side.
1685*7e949c46SKenneth D. Merry  * \param[in]	pkt	pkt's associated requests form the src for
1686*7e949c46SKenneth D. Merry  * 			the copy operation
1687*7e949c46SKenneth D. Merry  * \param[in]	mbufc	mbufc's storage forms the dest for the copy operation
1688*7e949c46SKenneth D. Merry  * \param[out]  gnttab	Storage for the returned grant table
1689*7e949c46SKenneth D. Merry  * \param[in]	txb	Pointer to the backend ring structure
1690*7e949c46SKenneth D. Merry  * \param[in]	otherend_id	The domain ID of the other end of the copy
1691*7e949c46SKenneth D. Merry  * \return 		The number of gnttab entries filled
1692*7e949c46SKenneth D. Merry  */
1693*7e949c46SKenneth D. Merry static int
1694*7e949c46SKenneth D. Merry xnb_txpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc,
1695*7e949c46SKenneth D. Merry 		 gnttab_copy_table gnttab, const netif_tx_back_ring_t *txb,
1696*7e949c46SKenneth D. Merry 		 domid_t otherend_id)
1697*7e949c46SKenneth D. Merry {
1698*7e949c46SKenneth D. Merry 
1699*7e949c46SKenneth D. Merry 	const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */
1700*7e949c46SKenneth D. Merry 	int gnt_idx = 0;		/* index into grant table */
1701*7e949c46SKenneth D. Merry 	RING_IDX r_idx = pkt->car;	/* index into tx ring buffer */
1702*7e949c46SKenneth D. Merry 	int r_ofs = 0;	/* offset of next data within tx request's data area */
1703*7e949c46SKenneth D. Merry 	int m_ofs = 0;	/* offset of next data within mbuf's data area */
1704*7e949c46SKenneth D. Merry 	/* size in bytes that still needs to be represented in the table */
1705*7e949c46SKenneth D. Merry 	uint16_t size_remaining = pkt->size;
1706*7e949c46SKenneth D. Merry 
1707*7e949c46SKenneth D. Merry 	while (size_remaining > 0) {
1708*7e949c46SKenneth D. Merry 		const netif_tx_request_t *txq = RING_GET_REQUEST(txb, r_idx);
1709*7e949c46SKenneth D. Merry 		const size_t mbuf_space = M_TRAILINGSPACE(mbuf) - m_ofs;
1710*7e949c46SKenneth D. Merry 		const size_t req_size =
1711*7e949c46SKenneth D. Merry 			r_idx == pkt->car ? pkt->car_size : txq->size;
1712*7e949c46SKenneth D. Merry 		const size_t pkt_space = req_size - r_ofs;
1713*7e949c46SKenneth D. Merry 		/*
1714*7e949c46SKenneth D. Merry 		 * space is the largest amount of data that can be copied in the
1715*7e949c46SKenneth D. Merry 		 * grant table's next entry
1716*7e949c46SKenneth D. Merry 		 */
1717*7e949c46SKenneth D. Merry 		const size_t space = MIN(pkt_space, mbuf_space);
1718*7e949c46SKenneth D. Merry 
1719*7e949c46SKenneth D. Merry 		/* TODO: handle this error condition without panicking */
1720*7e949c46SKenneth D. Merry 		KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short"));
1721*7e949c46SKenneth D. Merry 
1722*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].source.u.ref = txq->gref;
1723*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].source.domid = otherend_id;
1724*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].source.offset = txq->offset + r_ofs;
1725*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].dest.u.gmfn = virt_to_mfn(
1726*7e949c46SKenneth D. Merry 		    mtod(mbuf, vm_offset_t) + m_ofs);
1727*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].dest.offset = virt_to_offset(
1728*7e949c46SKenneth D. Merry 		    mtod(mbuf, vm_offset_t) + m_ofs);
1729*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].dest.domid = DOMID_SELF;
1730*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].len = space;
1731*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].flags = GNTCOPY_source_gref;
1732*7e949c46SKenneth D. Merry 
1733*7e949c46SKenneth D. Merry 		gnt_idx++;
1734*7e949c46SKenneth D. Merry 		r_ofs += space;
1735*7e949c46SKenneth D. Merry 		m_ofs += space;
1736*7e949c46SKenneth D. Merry 		size_remaining -= space;
1737*7e949c46SKenneth D. Merry 		if (req_size - r_ofs <= 0) {
1738*7e949c46SKenneth D. Merry 			/* Must move to the next tx request */
1739*7e949c46SKenneth D. Merry 			r_ofs = 0;
1740*7e949c46SKenneth D. Merry 			r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1;
1741*7e949c46SKenneth D. Merry 		}
1742*7e949c46SKenneth D. Merry 		if (M_TRAILINGSPACE(mbuf) - m_ofs <= 0) {
1743*7e949c46SKenneth D. Merry 			/* Must move to the next mbuf */
1744*7e949c46SKenneth D. Merry 			m_ofs = 0;
1745*7e949c46SKenneth D. Merry 			mbuf = mbuf->m_next;
1746*7e949c46SKenneth D. Merry 		}
1747*7e949c46SKenneth D. Merry 	}
1748*7e949c46SKenneth D. Merry 
1749*7e949c46SKenneth D. Merry 	return gnt_idx;
1750*7e949c46SKenneth D. Merry }
1751*7e949c46SKenneth D. Merry 
1752*7e949c46SKenneth D. Merry /**
1753*7e949c46SKenneth D. Merry  * Check the status of the grant copy operations, and update mbufs various
1754*7e949c46SKenneth D. Merry  * non-data fields to reflect the data present.
1755*7e949c46SKenneth D. Merry  * \param[in,out] mbufc	mbuf chain to update.  The chain must be valid and of
1756*7e949c46SKenneth D. Merry  * 			the correct length, and data should already be present
1757*7e949c46SKenneth D. Merry  * \param[in] gnttab	A grant table for a just completed copy op
1758*7e949c46SKenneth D. Merry  * \param[in] n_entries The number of valid entries in the grant table
1759*7e949c46SKenneth D. Merry  */
1760*7e949c46SKenneth D. Merry static void
1761*7e949c46SKenneth D. Merry xnb_update_mbufc(struct mbuf *mbufc, const gnttab_copy_table gnttab,
1762*7e949c46SKenneth D. Merry     		 int n_entries)
1763*7e949c46SKenneth D. Merry {
1764*7e949c46SKenneth D. Merry 	struct mbuf *mbuf = mbufc;
1765*7e949c46SKenneth D. Merry 	int i;
1766*7e949c46SKenneth D. Merry 	size_t total_size = 0;
1767*7e949c46SKenneth D. Merry 
1768*7e949c46SKenneth D. Merry 	for (i = 0; i < n_entries; i++) {
1769*7e949c46SKenneth D. Merry 		KASSERT(gnttab[i].status == GNTST_okay,
1770*7e949c46SKenneth D. Merry 		    ("Some gnttab_copy entry had error status %hd\n",
1771*7e949c46SKenneth D. Merry 		    gnttab[i].status));
1772*7e949c46SKenneth D. Merry 
1773*7e949c46SKenneth D. Merry 		mbuf->m_len += gnttab[i].len;
1774*7e949c46SKenneth D. Merry 		total_size += gnttab[i].len;
1775*7e949c46SKenneth D. Merry 		if (M_TRAILINGSPACE(mbuf) <= 0) {
1776*7e949c46SKenneth D. Merry 			mbuf = mbuf->m_next;
1777*7e949c46SKenneth D. Merry 		}
1778*7e949c46SKenneth D. Merry 	}
1779*7e949c46SKenneth D. Merry 	mbufc->m_pkthdr.len = total_size;
1780*7e949c46SKenneth D. Merry 
1781*7e949c46SKenneth D. Merry 	xnb_add_mbuf_cksum(mbufc);
1782*7e949c46SKenneth D. Merry }
1783*7e949c46SKenneth D. Merry 
1784*7e949c46SKenneth D. Merry /**
1785*7e949c46SKenneth D. Merry  * Dequeue at most one packet from the shared ring
1786*7e949c46SKenneth D. Merry  * \param[in,out] txb	Netif tx ring.  A packet will be removed from it, and
1787*7e949c46SKenneth D. Merry  * 			its private indices will be updated.  But the indices
1788*7e949c46SKenneth D. Merry  * 			will not be pushed to the shared ring.
1789*7e949c46SKenneth D. Merry  * \param[in] ifnet	Interface to which the packet will be sent
1790*7e949c46SKenneth D. Merry  * \param[in] otherend	Domain ID of the other end of the ring
1791*7e949c46SKenneth D. Merry  * \param[out] mbufc	The assembled mbuf chain, ready to send to the generic
1792*7e949c46SKenneth D. Merry  * 			networking stack
1793*7e949c46SKenneth D. Merry  * \param[in,out] gnttab Pointer to enough memory for a grant table.  We make
1794*7e949c46SKenneth D. Merry  * 			this a function parameter so that we will take less
1795*7e949c46SKenneth D. Merry  * 			stack space.
1796*7e949c46SKenneth D. Merry  * \return		An error code
1797*7e949c46SKenneth D. Merry  */
1798*7e949c46SKenneth D. Merry static int
1799*7e949c46SKenneth D. Merry xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, struct mbuf **mbufc,
1800*7e949c46SKenneth D. Merry 	 struct ifnet *ifnet, gnttab_copy_table gnttab)
1801*7e949c46SKenneth D. Merry {
1802*7e949c46SKenneth D. Merry 	struct xnb_pkt pkt;
1803*7e949c46SKenneth D. Merry 	/* number of tx requests consumed to build the last packet */
1804*7e949c46SKenneth D. Merry 	int num_consumed;
1805*7e949c46SKenneth D. Merry 	int nr_ents;
1806*7e949c46SKenneth D. Merry 
1807*7e949c46SKenneth D. Merry 	*mbufc = NULL;
1808*7e949c46SKenneth D. Merry 	num_consumed = xnb_ring2pkt(&pkt, txb, txb->req_cons);
1809*7e949c46SKenneth D. Merry 	if (num_consumed == 0)
1810*7e949c46SKenneth D. Merry 		return 0;	/* Nothing to receive */
1811*7e949c46SKenneth D. Merry 
1812*7e949c46SKenneth D. Merry 	/* update statistics indepdent of errors */
1813*7e949c46SKenneth D. Merry 	ifnet->if_ipackets++;
1814*7e949c46SKenneth D. Merry 
1815*7e949c46SKenneth D. Merry 	/*
1816*7e949c46SKenneth D. Merry 	 * if we got here, then 1 or more requests was consumed, but the packet
1817*7e949c46SKenneth D. Merry 	 * is not necesarily valid.
1818*7e949c46SKenneth D. Merry 	 */
1819*7e949c46SKenneth D. Merry 	if (xnb_pkt_is_valid(&pkt) == 0) {
1820*7e949c46SKenneth D. Merry 		/* got a garbage packet, respond and drop it */
1821*7e949c46SKenneth D. Merry 		xnb_txpkt2rsp(&pkt, txb, 1);
1822*7e949c46SKenneth D. Merry 		txb->req_cons += num_consumed;
1823*7e949c46SKenneth D. Merry 		DPRINTF("xnb_intr: garbage packet, num_consumed=%d\n",
1824*7e949c46SKenneth D. Merry 				num_consumed);
1825*7e949c46SKenneth D. Merry 		ifnet->if_ierrors++;
1826*7e949c46SKenneth D. Merry 		return EINVAL;
1827*7e949c46SKenneth D. Merry 	}
1828*7e949c46SKenneth D. Merry 
1829*7e949c46SKenneth D. Merry 	*mbufc = xnb_pkt2mbufc(&pkt, ifnet);
1830*7e949c46SKenneth D. Merry 
1831*7e949c46SKenneth D. Merry 	if (*mbufc == NULL) {
1832*7e949c46SKenneth D. Merry 		/*
1833*7e949c46SKenneth D. Merry 		 * Couldn't allocate mbufs.  Respond and drop the packet.  Do
1834*7e949c46SKenneth D. Merry 		 * not consume the requests
1835*7e949c46SKenneth D. Merry 		 */
1836*7e949c46SKenneth D. Merry 		xnb_txpkt2rsp(&pkt, txb, 1);
1837*7e949c46SKenneth D. Merry 		DPRINTF("xnb_intr: Couldn't allocate mbufs, num_consumed=%d\n",
1838*7e949c46SKenneth D. Merry 		    num_consumed);
1839*7e949c46SKenneth D. Merry 		ifnet->if_iqdrops++;
1840*7e949c46SKenneth D. Merry 		return ENOMEM;
1841*7e949c46SKenneth D. Merry 	}
1842*7e949c46SKenneth D. Merry 
1843*7e949c46SKenneth D. Merry 	nr_ents = xnb_txpkt2gnttab(&pkt, *mbufc, gnttab, txb, otherend);
1844*7e949c46SKenneth D. Merry 
1845*7e949c46SKenneth D. Merry 	if (nr_ents > 0) {
1846*7e949c46SKenneth D. Merry 		int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
1847*7e949c46SKenneth D. Merry 		    gnttab, nr_ents);
1848*7e949c46SKenneth D. Merry 		KASSERT(hv_ret == 0,
1849*7e949c46SKenneth D. Merry 		    ("HYPERVISOR_grant_table_op returned %d\n", hv_ret));
1850*7e949c46SKenneth D. Merry 		xnb_update_mbufc(*mbufc, gnttab, nr_ents);
1851*7e949c46SKenneth D. Merry 	}
1852*7e949c46SKenneth D. Merry 
1853*7e949c46SKenneth D. Merry 	xnb_txpkt2rsp(&pkt, txb, 0);
1854*7e949c46SKenneth D. Merry 	txb->req_cons += num_consumed;
185589e0f4d2SKip Macy 	return 0;
185689e0f4d2SKip Macy }
185789e0f4d2SKip Macy 
1858*7e949c46SKenneth D. Merry /**
1859*7e949c46SKenneth D. Merry  * Create an xnb_pkt based on the contents of an mbuf chain.
1860*7e949c46SKenneth D. Merry  * \param[in] mbufc	mbuf chain to transform into a packet
1861*7e949c46SKenneth D. Merry  * \param[out] pkt	Storage for the newly generated xnb_pkt
1862*7e949c46SKenneth D. Merry  * \param[in] start	The ring index of the first available slot in the rx
1863*7e949c46SKenneth D. Merry  * 			ring
1864*7e949c46SKenneth D. Merry  * \param[in] space	The number of free slots in the rx ring
1865*7e949c46SKenneth D. Merry  * \retval 0		Success
1866*7e949c46SKenneth D. Merry  * \retval EINVAL	mbufc was corrupt or not convertible into a pkt
1867*7e949c46SKenneth D. Merry  * \retval EAGAIN	There was not enough space in the ring to queue the
1868*7e949c46SKenneth D. Merry  * 			packet
1869*7e949c46SKenneth D. Merry  */
187089e0f4d2SKip Macy static int
1871*7e949c46SKenneth D. Merry xnb_mbufc2pkt(const struct mbuf *mbufc, struct xnb_pkt *pkt,
1872*7e949c46SKenneth D. Merry 	      RING_IDX start, int space)
187389e0f4d2SKip Macy {
187489e0f4d2SKip Macy 
1875*7e949c46SKenneth D. Merry 	int retval = 0;
187689e0f4d2SKip Macy 
1877*7e949c46SKenneth D. Merry 	if ((mbufc == NULL) ||
1878*7e949c46SKenneth D. Merry 	     ( (mbufc->m_flags & M_PKTHDR) == 0) ||
1879*7e949c46SKenneth D. Merry 	     (mbufc->m_pkthdr.len == 0)) {
1880*7e949c46SKenneth D. Merry 		xnb_pkt_invalidate(pkt);
1881*7e949c46SKenneth D. Merry 		retval = EINVAL;
1882*7e949c46SKenneth D. Merry 	} else {
1883*7e949c46SKenneth D. Merry 		int slots_required;
188489e0f4d2SKip Macy 
1885*7e949c46SKenneth D. Merry 		xnb_pkt_validate(pkt);
1886*7e949c46SKenneth D. Merry 		pkt->flags = 0;
1887*7e949c46SKenneth D. Merry 		pkt->size = mbufc->m_pkthdr.len;
1888*7e949c46SKenneth D. Merry 		pkt->car = start;
1889*7e949c46SKenneth D. Merry 		pkt->car_size = mbufc->m_len;
189089e0f4d2SKip Macy 
1891*7e949c46SKenneth D. Merry 		if (mbufc->m_pkthdr.csum_flags & CSUM_TSO) {
1892*7e949c46SKenneth D. Merry 			pkt->flags |= NETRXF_extra_info;
1893*7e949c46SKenneth D. Merry 			pkt->extra.u.gso.size = mbufc->m_pkthdr.tso_segsz;
1894*7e949c46SKenneth D. Merry 			pkt->extra.u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
1895*7e949c46SKenneth D. Merry 			pkt->extra.u.gso.pad = 0;
1896*7e949c46SKenneth D. Merry 			pkt->extra.u.gso.features = 0;
1897*7e949c46SKenneth D. Merry 			pkt->extra.type = XEN_NETIF_EXTRA_TYPE_GSO;
1898*7e949c46SKenneth D. Merry 			pkt->extra.flags = 0;
1899*7e949c46SKenneth D. Merry 			pkt->cdr = start + 2;
1900*7e949c46SKenneth D. Merry 		} else {
1901*7e949c46SKenneth D. Merry 			pkt->cdr = start + 1;
1902*7e949c46SKenneth D. Merry 		}
1903*7e949c46SKenneth D. Merry 		if (mbufc->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_DELAY_DATA)) {
1904*7e949c46SKenneth D. Merry 			pkt->flags |=
1905*7e949c46SKenneth D. Merry 			    (NETRXF_csum_blank | NETRXF_data_validated);
1906*7e949c46SKenneth D. Merry 		}
190789e0f4d2SKip Macy 
1908*7e949c46SKenneth D. Merry 		/*
1909*7e949c46SKenneth D. Merry 		 * Each ring response can have up to PAGE_SIZE of data.
1910*7e949c46SKenneth D. Merry 		 * Assume that we can defragment the mbuf chain efficiently
1911*7e949c46SKenneth D. Merry 		 * into responses so that each response but the last uses all
1912*7e949c46SKenneth D. Merry 		 * PAGE_SIZE bytes.
1913*7e949c46SKenneth D. Merry 		 */
1914*7e949c46SKenneth D. Merry 		pkt->list_len = (pkt->size + PAGE_SIZE - 1) / PAGE_SIZE;
191589e0f4d2SKip Macy 
1916*7e949c46SKenneth D. Merry 		if (pkt->list_len > 1) {
1917*7e949c46SKenneth D. Merry 			pkt->flags |= NETRXF_more_data;
1918*7e949c46SKenneth D. Merry 		}
1919*7e949c46SKenneth D. Merry 
1920*7e949c46SKenneth D. Merry 		slots_required = pkt->list_len +
1921*7e949c46SKenneth D. Merry 			(pkt->flags & NETRXF_extra_info ? 1 : 0);
1922*7e949c46SKenneth D. Merry 		if (slots_required > space) {
1923*7e949c46SKenneth D. Merry 			xnb_pkt_invalidate(pkt);
1924*7e949c46SKenneth D. Merry 			retval = EAGAIN;
192589e0f4d2SKip Macy 		}
192689e0f4d2SKip Macy 	}
192789e0f4d2SKip Macy 
1928*7e949c46SKenneth D. Merry 	return retval;
192989e0f4d2SKip Macy }
193089e0f4d2SKip Macy 
1931*7e949c46SKenneth D. Merry /**
1932*7e949c46SKenneth D. Merry  * Build a gnttab_copy table that can be used to copy data from an mbuf chain
1933*7e949c46SKenneth D. Merry  * to the frontend's shared buffers.  Does not actually perform the copy.
1934*7e949c46SKenneth D. Merry  * Always uses gref's on the other end's side.
1935*7e949c46SKenneth D. Merry  * \param[in]	pkt	pkt's associated responses form the dest for the copy
1936*7e949c46SKenneth D. Merry  * 			operatoin
1937*7e949c46SKenneth D. Merry  * \param[in]	mbufc	The source for the copy operation
1938*7e949c46SKenneth D. Merry  * \param[out]	gnttab	Storage for the returned grant table
1939*7e949c46SKenneth D. Merry  * \param[in]	rxb	Pointer to the backend ring structure
1940*7e949c46SKenneth D. Merry  * \param[in]	otherend_id	The domain ID of the other end of the copy
1941*7e949c46SKenneth D. Merry  * \return 		The number of gnttab entries filled
1942*7e949c46SKenneth D. Merry  */
194389e0f4d2SKip Macy static int
1944*7e949c46SKenneth D. Merry xnb_rxpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc,
1945*7e949c46SKenneth D. Merry 		 gnttab_copy_table gnttab, const netif_rx_back_ring_t *rxb,
1946*7e949c46SKenneth D. Merry 		 domid_t otherend_id)
194789e0f4d2SKip Macy {
194889e0f4d2SKip Macy 
1949*7e949c46SKenneth D. Merry 	const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */
1950*7e949c46SKenneth D. Merry 	int gnt_idx = 0;		/* index into grant table */
1951*7e949c46SKenneth D. Merry 	RING_IDX r_idx = pkt->car;	/* index into rx ring buffer */
1952*7e949c46SKenneth D. Merry 	int r_ofs = 0;	/* offset of next data within rx request's data area */
1953*7e949c46SKenneth D. Merry 	int m_ofs = 0;	/* offset of next data within mbuf's data area */
1954*7e949c46SKenneth D. Merry 	/* size in bytes that still needs to be represented in the table */
1955*7e949c46SKenneth D. Merry 	uint16_t size_remaining;
195689e0f4d2SKip Macy 
1957*7e949c46SKenneth D. Merry 	size_remaining = (xnb_pkt_is_valid(pkt) != 0) ? pkt->size : 0;
1958*7e949c46SKenneth D. Merry 
1959*7e949c46SKenneth D. Merry 	while (size_remaining > 0) {
1960*7e949c46SKenneth D. Merry 		const netif_rx_request_t *rxq = RING_GET_REQUEST(rxb, r_idx);
1961*7e949c46SKenneth D. Merry 		const size_t mbuf_space = mbuf->m_len - m_ofs;
1962*7e949c46SKenneth D. Merry 		/* Xen shared pages have an implied size of PAGE_SIZE */
1963*7e949c46SKenneth D. Merry 		const size_t req_size = PAGE_SIZE;
1964*7e949c46SKenneth D. Merry 		const size_t pkt_space = req_size - r_ofs;
1965*7e949c46SKenneth D. Merry 		/*
1966*7e949c46SKenneth D. Merry 		 * space is the largest amount of data that can be copied in the
1967*7e949c46SKenneth D. Merry 		 * grant table's next entry
1968*7e949c46SKenneth D. Merry 		 */
1969*7e949c46SKenneth D. Merry 		const size_t space = MIN(pkt_space, mbuf_space);
1970*7e949c46SKenneth D. Merry 
1971*7e949c46SKenneth D. Merry 		/* TODO: handle this error condition without panicing */
1972*7e949c46SKenneth D. Merry 		KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short"));
1973*7e949c46SKenneth D. Merry 
1974*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].dest.u.ref = rxq->gref;
1975*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].dest.domid = otherend_id;
1976*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].dest.offset = r_ofs;
1977*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].source.u.gmfn = virt_to_mfn(
1978*7e949c46SKenneth D. Merry 		    mtod(mbuf, vm_offset_t) + m_ofs);
1979*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].source.offset = virt_to_offset(
1980*7e949c46SKenneth D. Merry 		    mtod(mbuf, vm_offset_t) + m_ofs);
1981*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].source.domid = DOMID_SELF;
1982*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].len = space;
1983*7e949c46SKenneth D. Merry 		gnttab[gnt_idx].flags = GNTCOPY_dest_gref;
1984*7e949c46SKenneth D. Merry 
1985*7e949c46SKenneth D. Merry 		gnt_idx++;
1986*7e949c46SKenneth D. Merry 
1987*7e949c46SKenneth D. Merry 		r_ofs += space;
1988*7e949c46SKenneth D. Merry 		m_ofs += space;
1989*7e949c46SKenneth D. Merry 		size_remaining -= space;
1990*7e949c46SKenneth D. Merry 		if (req_size - r_ofs <= 0) {
1991*7e949c46SKenneth D. Merry 			/* Must move to the next rx request */
1992*7e949c46SKenneth D. Merry 			r_ofs = 0;
1993*7e949c46SKenneth D. Merry 			r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1;
1994*7e949c46SKenneth D. Merry 		}
1995*7e949c46SKenneth D. Merry 		if (mbuf->m_len - m_ofs <= 0) {
1996*7e949c46SKenneth D. Merry 			/* Must move to the next mbuf */
1997*7e949c46SKenneth D. Merry 			m_ofs = 0;
1998*7e949c46SKenneth D. Merry 			mbuf = mbuf->m_next;
1999*7e949c46SKenneth D. Merry 		}
2000*7e949c46SKenneth D. Merry 	}
2001*7e949c46SKenneth D. Merry 
2002*7e949c46SKenneth D. Merry 	return gnt_idx;
2003*7e949c46SKenneth D. Merry }
2004*7e949c46SKenneth D. Merry 
2005*7e949c46SKenneth D. Merry /**
2006*7e949c46SKenneth D. Merry  * Generates responses for all the requests that constituted pkt.  Builds
2007*7e949c46SKenneth D. Merry  * responses and writes them to the ring, but doesn't push the shared ring
2008*7e949c46SKenneth D. Merry  * indices.
2009*7e949c46SKenneth D. Merry  * \param[in] pkt	the packet that needs a response
2010*7e949c46SKenneth D. Merry  * \param[in] gnttab	The grant copy table corresponding to this packet.
2011*7e949c46SKenneth D. Merry  * 			Used to determine how many rsp->netif_rx_response_t's to
2012*7e949c46SKenneth D. Merry  * 			generate.
2013*7e949c46SKenneth D. Merry  * \param[in] n_entries	Number of relevant entries in the grant table
2014*7e949c46SKenneth D. Merry  * \param[out] ring	Responses go here
2015*7e949c46SKenneth D. Merry  * \return		The number of RX requests that were consumed to generate
2016*7e949c46SKenneth D. Merry  * 			the responses
2017*7e949c46SKenneth D. Merry  */
2018*7e949c46SKenneth D. Merry static int
2019*7e949c46SKenneth D. Merry xnb_rxpkt2rsp(const struct xnb_pkt *pkt, const gnttab_copy_table gnttab,
2020*7e949c46SKenneth D. Merry     	      int n_entries, netif_rx_back_ring_t *ring)
2021*7e949c46SKenneth D. Merry {
2022*7e949c46SKenneth D. Merry 	/*
2023*7e949c46SKenneth D. Merry 	 * This code makes the following assumptions:
2024*7e949c46SKenneth D. Merry 	 *	* All entries in gnttab set GNTCOPY_dest_gref
2025*7e949c46SKenneth D. Merry 	 *	* The entries in gnttab are grouped by their grefs: any two
2026*7e949c46SKenneth D. Merry 	 *	   entries with the same gref must be adjacent
2027*7e949c46SKenneth D. Merry 	 */
2028*7e949c46SKenneth D. Merry 	int error = 0;
2029*7e949c46SKenneth D. Merry 	int gnt_idx, i;
2030*7e949c46SKenneth D. Merry 	int n_responses = 0;
2031*7e949c46SKenneth D. Merry 	grant_ref_t last_gref = GRANT_REF_INVALID;
2032*7e949c46SKenneth D. Merry 	RING_IDX r_idx;
2033*7e949c46SKenneth D. Merry 
2034*7e949c46SKenneth D. Merry 	KASSERT(gnttab != NULL, ("Received a null granttable copy"));
2035*7e949c46SKenneth D. Merry 
2036*7e949c46SKenneth D. Merry 	/*
2037*7e949c46SKenneth D. Merry 	 * In the event of an error, we only need to send one response to the
2038*7e949c46SKenneth D. Merry 	 * netfront.  In that case, we musn't write any data to the responses
2039*7e949c46SKenneth D. Merry 	 * after the one we send.  So we must loop all the way through gnttab
2040*7e949c46SKenneth D. Merry 	 * looking for errors before we generate any responses
2041*7e949c46SKenneth D. Merry 	 *
2042*7e949c46SKenneth D. Merry 	 * Since we're looping through the grant table anyway, we'll count the
2043*7e949c46SKenneth D. Merry 	 * number of different gref's in it, which will tell us how many
2044*7e949c46SKenneth D. Merry 	 * responses to generate
2045*7e949c46SKenneth D. Merry 	 */
2046*7e949c46SKenneth D. Merry 	for (gnt_idx = 0; gnt_idx < n_entries; gnt_idx++) {
2047*7e949c46SKenneth D. Merry 		int16_t status = gnttab[gnt_idx].status;
2048*7e949c46SKenneth D. Merry 		if (status != GNTST_okay) {
2049*7e949c46SKenneth D. Merry 			DPRINTF(
2050*7e949c46SKenneth D. Merry 			    "Got error %d for hypervisor gnttab_copy status\n",
2051*7e949c46SKenneth D. Merry 			    status);
2052*7e949c46SKenneth D. Merry 			error = 1;
2053*7e949c46SKenneth D. Merry 			break;
2054*7e949c46SKenneth D. Merry 		}
2055*7e949c46SKenneth D. Merry 		if (gnttab[gnt_idx].dest.u.ref != last_gref) {
2056*7e949c46SKenneth D. Merry 			n_responses++;
2057*7e949c46SKenneth D. Merry 			last_gref = gnttab[gnt_idx].dest.u.ref;
2058*7e949c46SKenneth D. Merry 		}
2059*7e949c46SKenneth D. Merry 	}
2060*7e949c46SKenneth D. Merry 
2061*7e949c46SKenneth D. Merry 	if (error != 0) {
2062*7e949c46SKenneth D. Merry 		uint16_t id;
2063*7e949c46SKenneth D. Merry 		netif_rx_response_t *rsp;
2064*7e949c46SKenneth D. Merry 
2065*7e949c46SKenneth D. Merry 		id = RING_GET_REQUEST(ring, ring->rsp_prod_pvt)->id;
2066*7e949c46SKenneth D. Merry 		rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
2067*7e949c46SKenneth D. Merry 		rsp->id = id;
2068*7e949c46SKenneth D. Merry 		rsp->status = NETIF_RSP_ERROR;
2069*7e949c46SKenneth D. Merry 		n_responses = 1;
2070*7e949c46SKenneth D. Merry 	} else {
2071*7e949c46SKenneth D. Merry 		gnt_idx = 0;
2072*7e949c46SKenneth D. Merry 		const int has_extra = pkt->flags & NETRXF_extra_info;
2073*7e949c46SKenneth D. Merry 		if (has_extra != 0)
2074*7e949c46SKenneth D. Merry 			n_responses++;
2075*7e949c46SKenneth D. Merry 
2076*7e949c46SKenneth D. Merry 		for (i = 0; i < n_responses; i++) {
2077*7e949c46SKenneth D. Merry 			netif_rx_request_t rxq;
2078*7e949c46SKenneth D. Merry 			netif_rx_response_t *rsp;
2079*7e949c46SKenneth D. Merry 
2080*7e949c46SKenneth D. Merry 			r_idx = ring->rsp_prod_pvt + i;
2081*7e949c46SKenneth D. Merry 			/*
2082*7e949c46SKenneth D. Merry 			 * We copy the structure of rxq instead of making a
2083*7e949c46SKenneth D. Merry 			 * pointer because it shares the same memory as rsp.
2084*7e949c46SKenneth D. Merry 			 */
2085*7e949c46SKenneth D. Merry 			rxq = *(RING_GET_REQUEST(ring, r_idx));
2086*7e949c46SKenneth D. Merry 			rsp = RING_GET_RESPONSE(ring, r_idx);
2087*7e949c46SKenneth D. Merry 			if (has_extra && (i == 1)) {
2088*7e949c46SKenneth D. Merry 				netif_extra_info_t *ext =
2089*7e949c46SKenneth D. Merry 					(netif_extra_info_t*)rsp;
2090*7e949c46SKenneth D. Merry 				ext->type = XEN_NETIF_EXTRA_TYPE_GSO;
2091*7e949c46SKenneth D. Merry 				ext->flags = 0;
2092*7e949c46SKenneth D. Merry 				ext->u.gso.size = pkt->extra.u.gso.size;
2093*7e949c46SKenneth D. Merry 				ext->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
2094*7e949c46SKenneth D. Merry 				ext->u.gso.pad = 0;
2095*7e949c46SKenneth D. Merry 				ext->u.gso.features = 0;
2096*7e949c46SKenneth D. Merry 			} else {
2097*7e949c46SKenneth D. Merry 				rsp->id = rxq.id;
2098*7e949c46SKenneth D. Merry 				rsp->status = GNTST_okay;
2099*7e949c46SKenneth D. Merry 				rsp->offset = 0;
2100*7e949c46SKenneth D. Merry 				rsp->flags = 0;
2101*7e949c46SKenneth D. Merry 				if (i < pkt->list_len - 1)
2102*7e949c46SKenneth D. Merry 					rsp->flags |= NETRXF_more_data;
2103*7e949c46SKenneth D. Merry 				if ((i == 0) && has_extra)
2104*7e949c46SKenneth D. Merry 					rsp->flags |= NETRXF_extra_info;
2105*7e949c46SKenneth D. Merry 				if ((i == 0) &&
2106*7e949c46SKenneth D. Merry 					(pkt->flags & NETRXF_data_validated)) {
2107*7e949c46SKenneth D. Merry 					rsp->flags |= NETRXF_data_validated;
2108*7e949c46SKenneth D. Merry 					rsp->flags |= NETRXF_csum_blank;
2109*7e949c46SKenneth D. Merry 				}
2110*7e949c46SKenneth D. Merry 				rsp->status = 0;
2111*7e949c46SKenneth D. Merry 				for (; gnttab[gnt_idx].dest.u.ref == rxq.gref;
2112*7e949c46SKenneth D. Merry 				    gnt_idx++) {
2113*7e949c46SKenneth D. Merry 					rsp->status += gnttab[gnt_idx].len;
2114*7e949c46SKenneth D. Merry 				}
2115*7e949c46SKenneth D. Merry 			}
2116*7e949c46SKenneth D. Merry 		}
2117*7e949c46SKenneth D. Merry 	}
2118*7e949c46SKenneth D. Merry 
2119*7e949c46SKenneth D. Merry 	ring->req_cons += n_responses;
2120*7e949c46SKenneth D. Merry 	ring->rsp_prod_pvt += n_responses;
2121*7e949c46SKenneth D. Merry 	return n_responses;
2122*7e949c46SKenneth D. Merry }
2123*7e949c46SKenneth D. Merry 
2124*7e949c46SKenneth D. Merry /**
2125*7e949c46SKenneth D. Merry  * Add IP, TCP, and/or UDP checksums to every mbuf in a chain.  The first mbuf
2126*7e949c46SKenneth D. Merry  * in the chain must start with a struct ether_header.
2127*7e949c46SKenneth D. Merry  *
2128*7e949c46SKenneth D. Merry  * XXX This function will perform incorrectly on UDP packets that are split up
2129*7e949c46SKenneth D. Merry  * into multiple ethernet frames.
2130*7e949c46SKenneth D. Merry  */
2131*7e949c46SKenneth D. Merry static void
2132*7e949c46SKenneth D. Merry xnb_add_mbuf_cksum(struct mbuf *mbufc)
2133*7e949c46SKenneth D. Merry {
2134*7e949c46SKenneth D. Merry 	struct ether_header *eh;
2135*7e949c46SKenneth D. Merry 	struct ip *iph;
2136*7e949c46SKenneth D. Merry 	uint16_t ether_type;
2137*7e949c46SKenneth D. Merry 
2138*7e949c46SKenneth D. Merry 	eh = mtod(mbufc, struct ether_header*);
2139*7e949c46SKenneth D. Merry 	ether_type = ntohs(eh->ether_type);
2140*7e949c46SKenneth D. Merry 	if (ether_type != ETHERTYPE_IP) {
2141*7e949c46SKenneth D. Merry 		/* Nothing to calculate */
2142*7e949c46SKenneth D. Merry 		return;
2143*7e949c46SKenneth D. Merry 	}
2144*7e949c46SKenneth D. Merry 
2145*7e949c46SKenneth D. Merry 	iph = (struct ip*)(eh + 1);
2146*7e949c46SKenneth D. Merry 	if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2147*7e949c46SKenneth D. Merry 		iph->ip_sum = 0;
2148*7e949c46SKenneth D. Merry 		iph->ip_sum = in_cksum_hdr(iph);
2149*7e949c46SKenneth D. Merry 	}
2150*7e949c46SKenneth D. Merry 
2151*7e949c46SKenneth D. Merry 	switch (iph->ip_p) {
2152*7e949c46SKenneth D. Merry 	case IPPROTO_TCP:
2153*7e949c46SKenneth D. Merry 		if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2154*7e949c46SKenneth D. Merry 			size_t tcplen = ntohs(iph->ip_len) - sizeof(struct ip);
2155*7e949c46SKenneth D. Merry 			struct tcphdr *th = (struct tcphdr*)(iph + 1);
2156*7e949c46SKenneth D. Merry 			th->th_sum = in_pseudo(iph->ip_src.s_addr,
2157*7e949c46SKenneth D. Merry 			    iph->ip_dst.s_addr, htons(IPPROTO_TCP + tcplen));
2158*7e949c46SKenneth D. Merry 			th->th_sum = in_cksum_skip(mbufc,
2159*7e949c46SKenneth D. Merry 			    sizeof(struct ether_header) + ntohs(iph->ip_len),
2160*7e949c46SKenneth D. Merry 			    sizeof(struct ether_header) + (iph->ip_hl << 2));
2161*7e949c46SKenneth D. Merry 		}
2162*7e949c46SKenneth D. Merry 		break;
2163*7e949c46SKenneth D. Merry 	case IPPROTO_UDP:
2164*7e949c46SKenneth D. Merry 		if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2165*7e949c46SKenneth D. Merry 			size_t udplen = ntohs(iph->ip_len) - sizeof(struct ip);
2166*7e949c46SKenneth D. Merry 			struct udphdr *uh = (struct udphdr*)(iph + 1);
2167*7e949c46SKenneth D. Merry 			uh->uh_sum = in_pseudo(iph->ip_src.s_addr,
2168*7e949c46SKenneth D. Merry 			    iph->ip_dst.s_addr, htons(IPPROTO_UDP + udplen));
2169*7e949c46SKenneth D. Merry 			uh->uh_sum = in_cksum_skip(mbufc,
2170*7e949c46SKenneth D. Merry 			    sizeof(struct ether_header) + ntohs(iph->ip_len),
2171*7e949c46SKenneth D. Merry 			    sizeof(struct ether_header) + (iph->ip_hl << 2));
2172*7e949c46SKenneth D. Merry 		}
2173*7e949c46SKenneth D. Merry 		break;
2174*7e949c46SKenneth D. Merry 	default:
2175*7e949c46SKenneth D. Merry 		break;
2176*7e949c46SKenneth D. Merry 	}
2177*7e949c46SKenneth D. Merry }
2178*7e949c46SKenneth D. Merry 
2179*7e949c46SKenneth D. Merry static void
2180*7e949c46SKenneth D. Merry xnb_stop(struct xnb_softc *xnb)
2181*7e949c46SKenneth D. Merry {
2182*7e949c46SKenneth D. Merry 	struct ifnet *ifp;
2183*7e949c46SKenneth D. Merry 
2184*7e949c46SKenneth D. Merry 	mtx_assert(&xnb->sc_lock, MA_OWNED);
2185*7e949c46SKenneth D. Merry 	ifp = xnb->xnb_ifp;
218689e0f4d2SKip Macy 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2187*7e949c46SKenneth D. Merry 	if_link_state_change(ifp, LINK_STATE_DOWN);
2188*7e949c46SKenneth D. Merry }
218989e0f4d2SKip Macy 
2190*7e949c46SKenneth D. Merry static int
2191*7e949c46SKenneth D. Merry xnb_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2192*7e949c46SKenneth D. Merry {
2193*7e949c46SKenneth D. Merry 	struct xnb_softc *xnb = ifp->if_softc;
2194*7e949c46SKenneth D. Merry #ifdef INET
2195*7e949c46SKenneth D. Merry 	struct ifreq *ifr = (struct ifreq*) data;
2196*7e949c46SKenneth D. Merry 	struct ifaddr *ifa = (struct ifaddr*)data;
2197*7e949c46SKenneth D. Merry #endif
2198*7e949c46SKenneth D. Merry 	int error = 0;
219989e0f4d2SKip Macy 
2200*7e949c46SKenneth D. Merry 	switch (cmd) {
2201*7e949c46SKenneth D. Merry 		case SIOCSIFFLAGS:
2202*7e949c46SKenneth D. Merry 			mtx_lock(&xnb->sc_lock);
2203*7e949c46SKenneth D. Merry 			if (ifp->if_flags & IFF_UP) {
2204*7e949c46SKenneth D. Merry 				xnb_ifinit_locked(xnb);
2205*7e949c46SKenneth D. Merry 			} else {
2206*7e949c46SKenneth D. Merry 				if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2207*7e949c46SKenneth D. Merry 					xnb_stop(xnb);
2208*7e949c46SKenneth D. Merry 				}
2209*7e949c46SKenneth D. Merry 			}
2210*7e949c46SKenneth D. Merry 			/*
2211*7e949c46SKenneth D. Merry 			 * Note: netfront sets a variable named xn_if_flags
2212*7e949c46SKenneth D. Merry 			 * here, but that variable is never read
2213*7e949c46SKenneth D. Merry 			 */
2214*7e949c46SKenneth D. Merry 			mtx_unlock(&xnb->sc_lock);
2215*7e949c46SKenneth D. Merry 			break;
2216*7e949c46SKenneth D. Merry 		case SIOCSIFADDR:
2217*7e949c46SKenneth D. Merry 		case SIOCGIFADDR:
2218*7e949c46SKenneth D. Merry #ifdef INET
2219*7e949c46SKenneth D. Merry 			mtx_lock(&xnb->sc_lock);
2220*7e949c46SKenneth D. Merry 			if (ifa->ifa_addr->sa_family == AF_INET) {
2221*7e949c46SKenneth D. Merry 				ifp->if_flags |= IFF_UP;
2222*7e949c46SKenneth D. Merry 				if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2223*7e949c46SKenneth D. Merry 					ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
2224*7e949c46SKenneth D. Merry 							IFF_DRV_OACTIVE);
2225*7e949c46SKenneth D. Merry 					if_link_state_change(ifp,
2226*7e949c46SKenneth D. Merry 							LINK_STATE_DOWN);
2227*7e949c46SKenneth D. Merry 					ifp->if_drv_flags |= IFF_DRV_RUNNING;
2228*7e949c46SKenneth D. Merry 					ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2229*7e949c46SKenneth D. Merry 					if_link_state_change(ifp,
2230*7e949c46SKenneth D. Merry 					    LINK_STATE_UP);
2231*7e949c46SKenneth D. Merry 				}
2232*7e949c46SKenneth D. Merry 				arp_ifinit(ifp, ifa);
2233*7e949c46SKenneth D. Merry 				mtx_unlock(&xnb->sc_lock);
2234*7e949c46SKenneth D. Merry 			} else {
2235*7e949c46SKenneth D. Merry 				mtx_unlock(&xnb->sc_lock);
2236*7e949c46SKenneth D. Merry #endif
2237*7e949c46SKenneth D. Merry 				error = ether_ioctl(ifp, cmd, data);
2238*7e949c46SKenneth D. Merry #ifdef INET
2239*7e949c46SKenneth D. Merry 			}
2240*7e949c46SKenneth D. Merry #endif
2241*7e949c46SKenneth D. Merry 			break;
2242*7e949c46SKenneth D. Merry 		case SIOCSIFCAP:
2243*7e949c46SKenneth D. Merry 			mtx_lock(&xnb->sc_lock);
2244*7e949c46SKenneth D. Merry 			if (ifr->ifr_reqcap & IFCAP_TXCSUM) {
2245*7e949c46SKenneth D. Merry 				ifp->if_capenable |= IFCAP_TXCSUM;
2246*7e949c46SKenneth D. Merry 				ifp->if_hwassist |= XNB_CSUM_FEATURES;
2247*7e949c46SKenneth D. Merry 			} else {
2248*7e949c46SKenneth D. Merry 				ifp->if_capenable &= ~(IFCAP_TXCSUM);
2249*7e949c46SKenneth D. Merry 				ifp->if_hwassist &= ~(XNB_CSUM_FEATURES);
2250*7e949c46SKenneth D. Merry 			}
2251*7e949c46SKenneth D. Merry 			if ((ifr->ifr_reqcap & IFCAP_RXCSUM)) {
2252*7e949c46SKenneth D. Merry 				ifp->if_capenable |= IFCAP_RXCSUM;
2253*7e949c46SKenneth D. Merry 			} else {
2254*7e949c46SKenneth D. Merry 				ifp->if_capenable &= ~(IFCAP_RXCSUM);
2255*7e949c46SKenneth D. Merry 			}
2256*7e949c46SKenneth D. Merry 			/*
2257*7e949c46SKenneth D. Merry 			 * TODO enable TSO4 and LRO once we no longer need
2258*7e949c46SKenneth D. Merry 			 * to calculate checksums in software
2259*7e949c46SKenneth D. Merry 			 */
2260*7e949c46SKenneth D. Merry #if 0
2261*7e949c46SKenneth D. Merry 			if (ifr->if_reqcap |= IFCAP_TSO4) {
2262*7e949c46SKenneth D. Merry 				if (IFCAP_TXCSUM & ifp->if_capenable) {
2263*7e949c46SKenneth D. Merry 					printf("xnb: Xen netif requires that "
2264*7e949c46SKenneth D. Merry 						"TXCSUM be enabled in order "
2265*7e949c46SKenneth D. Merry 						"to use TSO4\n");
2266*7e949c46SKenneth D. Merry 					error = EINVAL;
2267*7e949c46SKenneth D. Merry 				} else {
2268*7e949c46SKenneth D. Merry 					ifp->if_capenable |= IFCAP_TSO4;
2269*7e949c46SKenneth D. Merry 					ifp->if_hwassist |= CSUM_TSO;
2270*7e949c46SKenneth D. Merry 				}
2271*7e949c46SKenneth D. Merry 			} else {
2272*7e949c46SKenneth D. Merry 				ifp->if_capenable &= ~(IFCAP_TSO4);
2273*7e949c46SKenneth D. Merry 				ifp->if_hwassist &= ~(CSUM_TSO);
2274*7e949c46SKenneth D. Merry 			}
2275*7e949c46SKenneth D. Merry 			if (ifr->ifreqcap |= IFCAP_LRO) {
2276*7e949c46SKenneth D. Merry 				ifp->if_capenable |= IFCAP_LRO;
2277*7e949c46SKenneth D. Merry 			} else {
2278*7e949c46SKenneth D. Merry 				ifp->if_capenable &= ~(IFCAP_LRO);
2279*7e949c46SKenneth D. Merry 			}
2280*7e949c46SKenneth D. Merry #endif
2281*7e949c46SKenneth D. Merry 			mtx_unlock(&xnb->sc_lock);
2282*7e949c46SKenneth D. Merry 			break;
2283*7e949c46SKenneth D. Merry 		case SIOCSIFMTU:
2284*7e949c46SKenneth D. Merry 			ifp->if_mtu = ifr->ifr_mtu;
2285*7e949c46SKenneth D. Merry 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2286*7e949c46SKenneth D. Merry 			xnb_ifinit(xnb);
2287*7e949c46SKenneth D. Merry 			break;
2288*7e949c46SKenneth D. Merry 		case SIOCADDMULTI:
2289*7e949c46SKenneth D. Merry 		case SIOCDELMULTI:
2290*7e949c46SKenneth D. Merry 		case SIOCSIFMEDIA:
2291*7e949c46SKenneth D. Merry 		case SIOCGIFMEDIA:
2292*7e949c46SKenneth D. Merry 			error = ifmedia_ioctl(ifp, ifr, &xnb->sc_media, cmd);
2293*7e949c46SKenneth D. Merry 			break;
2294*7e949c46SKenneth D. Merry 		default:
2295*7e949c46SKenneth D. Merry 			error = ether_ioctl(ifp, cmd, data);
2296*7e949c46SKenneth D. Merry 			break;
2297*7e949c46SKenneth D. Merry 	}
2298*7e949c46SKenneth D. Merry 	return (error);
2299*7e949c46SKenneth D. Merry }
230089e0f4d2SKip Macy 
2301*7e949c46SKenneth D. Merry static void
2302*7e949c46SKenneth D. Merry xnb_start_locked(struct ifnet *ifp)
2303*7e949c46SKenneth D. Merry {
2304*7e949c46SKenneth D. Merry 	netif_rx_back_ring_t *rxb;
2305*7e949c46SKenneth D. Merry 	struct xnb_softc *xnb;
2306*7e949c46SKenneth D. Merry 	struct mbuf *mbufc;
2307*7e949c46SKenneth D. Merry 	RING_IDX req_prod_local;
230889e0f4d2SKip Macy 
2309*7e949c46SKenneth D. Merry 	xnb = ifp->if_softc;
2310*7e949c46SKenneth D. Merry 	rxb = &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring;
2311*7e949c46SKenneth D. Merry 
2312*7e949c46SKenneth D. Merry 	if (!xnb->carrier)
2313*7e949c46SKenneth D. Merry 		return;
2314*7e949c46SKenneth D. Merry 
2315*7e949c46SKenneth D. Merry 	do {
2316*7e949c46SKenneth D. Merry 		int out_of_space = 0;
2317*7e949c46SKenneth D. Merry 		int notify;
2318*7e949c46SKenneth D. Merry 		req_prod_local = rxb->sring->req_prod;
2319*7e949c46SKenneth D. Merry 		xen_rmb();
2320*7e949c46SKenneth D. Merry 		for (;;) {
2321*7e949c46SKenneth D. Merry 			int error;
2322*7e949c46SKenneth D. Merry 
2323*7e949c46SKenneth D. Merry 			IF_DEQUEUE(&ifp->if_snd, mbufc);
2324*7e949c46SKenneth D. Merry 			if (mbufc == NULL)
2325*7e949c46SKenneth D. Merry 				break;
2326*7e949c46SKenneth D. Merry 			error = xnb_send(rxb, xnb->otherend_id, mbufc,
2327*7e949c46SKenneth D. Merry 			    		 xnb->rx_gnttab);
2328*7e949c46SKenneth D. Merry 			switch (error) {
2329*7e949c46SKenneth D. Merry 				case EAGAIN:
2330*7e949c46SKenneth D. Merry 					/*
2331*7e949c46SKenneth D. Merry 					 * Insufficient space in the ring.
2332*7e949c46SKenneth D. Merry 					 * Requeue pkt and send when space is
2333*7e949c46SKenneth D. Merry 					 * available.
2334*7e949c46SKenneth D. Merry 					 */
2335*7e949c46SKenneth D. Merry 					IF_PREPEND(&ifp->if_snd, mbufc);
2336*7e949c46SKenneth D. Merry 					/*
2337*7e949c46SKenneth D. Merry 					 * Perhaps the frontend missed an IRQ
2338*7e949c46SKenneth D. Merry 					 * and went to sleep.  Notify it to wake
2339*7e949c46SKenneth D. Merry 					 * it up.
2340*7e949c46SKenneth D. Merry 					 */
2341*7e949c46SKenneth D. Merry 					out_of_space = 1;
2342*7e949c46SKenneth D. Merry 					break;
2343*7e949c46SKenneth D. Merry 
2344*7e949c46SKenneth D. Merry 				case EINVAL:
2345*7e949c46SKenneth D. Merry 					/* OS gave a corrupt packet.  Drop it.*/
2346*7e949c46SKenneth D. Merry 					ifp->if_oerrors++;
2347*7e949c46SKenneth D. Merry 					/* FALLTHROUGH */
2348*7e949c46SKenneth D. Merry 				default:
2349*7e949c46SKenneth D. Merry 					/* Send succeeded, or packet had error.
2350*7e949c46SKenneth D. Merry 					 * Free the packet */
2351*7e949c46SKenneth D. Merry 					ifp->if_opackets++;
2352*7e949c46SKenneth D. Merry 					if (mbufc)
2353*7e949c46SKenneth D. Merry 						m_freem(mbufc);
2354*7e949c46SKenneth D. Merry 					break;
2355*7e949c46SKenneth D. Merry 			}
2356*7e949c46SKenneth D. Merry 			if (out_of_space != 0)
2357*7e949c46SKenneth D. Merry 				break;
2358*7e949c46SKenneth D. Merry 		}
2359*7e949c46SKenneth D. Merry 
2360*7e949c46SKenneth D. Merry 		RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(rxb, notify);
2361*7e949c46SKenneth D. Merry 		if ((notify != 0) || (out_of_space != 0))
2362*7e949c46SKenneth D. Merry 			notify_remote_via_irq(xnb->irq);
2363*7e949c46SKenneth D. Merry 		rxb->sring->req_event = req_prod_local + 1;
2364*7e949c46SKenneth D. Merry 		xen_mb();
2365*7e949c46SKenneth D. Merry 	} while (rxb->sring->req_prod != req_prod_local) ;
2366*7e949c46SKenneth D. Merry }
2367*7e949c46SKenneth D. Merry 
2368*7e949c46SKenneth D. Merry /**
2369*7e949c46SKenneth D. Merry  * Sends one packet to the ring.  Blocks until the packet is on the ring
2370*7e949c46SKenneth D. Merry  * \param[in]	mbufc	Contains one packet to send.  Caller must free
2371*7e949c46SKenneth D. Merry  * \param[in,out] rxb	The packet will be pushed onto this ring, but the
2372*7e949c46SKenneth D. Merry  * 			otherend will not be notified.
2373*7e949c46SKenneth D. Merry  * \param[in]	otherend The domain ID of the other end of the connection
2374*7e949c46SKenneth D. Merry  * \retval	EAGAIN	The ring did not have enough space for the packet.
2375*7e949c46SKenneth D. Merry  * 			The ring has not been modified
2376*7e949c46SKenneth D. Merry  * \param[in,out] gnttab Pointer to enough memory for a grant table.  We make
2377*7e949c46SKenneth D. Merry  * 			this a function parameter so that we will take less
2378*7e949c46SKenneth D. Merry  * 			stack space.
2379*7e949c46SKenneth D. Merry  * \retval EINVAL	mbufc was corrupt or not convertible into a pkt
2380*7e949c46SKenneth D. Merry  */
2381*7e949c46SKenneth D. Merry static int
2382*7e949c46SKenneth D. Merry xnb_send(netif_rx_back_ring_t *ring, domid_t otherend, const struct mbuf *mbufc,
2383*7e949c46SKenneth D. Merry 	 gnttab_copy_table gnttab)
2384*7e949c46SKenneth D. Merry {
2385*7e949c46SKenneth D. Merry 	struct xnb_pkt pkt;
2386*7e949c46SKenneth D. Merry 	int error, n_entries, n_reqs;
2387*7e949c46SKenneth D. Merry 	RING_IDX space;
2388*7e949c46SKenneth D. Merry 
2389*7e949c46SKenneth D. Merry 	space = ring->sring->req_prod - ring->req_cons;
2390*7e949c46SKenneth D. Merry 	error = xnb_mbufc2pkt(mbufc, &pkt, ring->rsp_prod_pvt, space);
2391*7e949c46SKenneth D. Merry 	if (error != 0)
2392*7e949c46SKenneth D. Merry 		return error;
2393*7e949c46SKenneth D. Merry 	n_entries = xnb_rxpkt2gnttab(&pkt, mbufc, gnttab, ring, otherend);
2394*7e949c46SKenneth D. Merry 	if (n_entries != 0) {
2395*7e949c46SKenneth D. Merry 		int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
2396*7e949c46SKenneth D. Merry 		    gnttab, n_entries);
2397*7e949c46SKenneth D. Merry 		KASSERT(hv_ret == 0, ("HYPERVISOR_grant_table_op returned %d\n",
2398*7e949c46SKenneth D. Merry 		    hv_ret));
2399*7e949c46SKenneth D. Merry 	}
2400*7e949c46SKenneth D. Merry 
2401*7e949c46SKenneth D. Merry 	n_reqs = xnb_rxpkt2rsp(&pkt, gnttab, n_entries, ring);
240289e0f4d2SKip Macy 
240389e0f4d2SKip Macy 	return 0;
240489e0f4d2SKip Macy }
240589e0f4d2SKip Macy 
2406*7e949c46SKenneth D. Merry static void
2407*7e949c46SKenneth D. Merry xnb_start(struct ifnet *ifp)
2408*7e949c46SKenneth D. Merry {
2409*7e949c46SKenneth D. Merry 	struct xnb_softc *xnb;
2410*7e949c46SKenneth D. Merry 
2411*7e949c46SKenneth D. Merry 	xnb = ifp->if_softc;
2412*7e949c46SKenneth D. Merry 	mtx_lock(&xnb->rx_lock);
2413*7e949c46SKenneth D. Merry 	xnb_start_locked(ifp);
2414*7e949c46SKenneth D. Merry 	mtx_unlock(&xnb->rx_lock);
2415*7e949c46SKenneth D. Merry }
2416*7e949c46SKenneth D. Merry 
2417*7e949c46SKenneth D. Merry /* equivalent of network_open() in Linux */
2418*7e949c46SKenneth D. Merry static void
2419*7e949c46SKenneth D. Merry xnb_ifinit_locked(struct xnb_softc *xnb)
2420*7e949c46SKenneth D. Merry {
2421*7e949c46SKenneth D. Merry 	struct ifnet *ifp;
2422*7e949c46SKenneth D. Merry 
2423*7e949c46SKenneth D. Merry 	ifp = xnb->xnb_ifp;
2424*7e949c46SKenneth D. Merry 
2425*7e949c46SKenneth D. Merry 	mtx_assert(&xnb->sc_lock, MA_OWNED);
2426*7e949c46SKenneth D. Merry 
2427*7e949c46SKenneth D. Merry 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2428*7e949c46SKenneth D. Merry 		return;
2429*7e949c46SKenneth D. Merry 
2430*7e949c46SKenneth D. Merry 	xnb_stop(xnb);
2431*7e949c46SKenneth D. Merry 
2432*7e949c46SKenneth D. Merry 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2433*7e949c46SKenneth D. Merry 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2434*7e949c46SKenneth D. Merry 	if_link_state_change(ifp, LINK_STATE_UP);
2435*7e949c46SKenneth D. Merry }
2436*7e949c46SKenneth D. Merry 
2437*7e949c46SKenneth D. Merry 
2438*7e949c46SKenneth D. Merry static void
2439*7e949c46SKenneth D. Merry xnb_ifinit(void *xsc)
2440*7e949c46SKenneth D. Merry {
2441*7e949c46SKenneth D. Merry 	struct xnb_softc *xnb = xsc;
2442*7e949c46SKenneth D. Merry 
2443*7e949c46SKenneth D. Merry 	mtx_lock(&xnb->sc_lock);
2444*7e949c46SKenneth D. Merry 	xnb_ifinit_locked(xnb);
2445*7e949c46SKenneth D. Merry 	mtx_unlock(&xnb->sc_lock);
2446*7e949c46SKenneth D. Merry }
2447*7e949c46SKenneth D. Merry 
2448*7e949c46SKenneth D. Merry 
2449*7e949c46SKenneth D. Merry /**
2450*7e949c46SKenneth D. Merry  * Read the 'mac' node at the given device's node in the store, and parse that
2451*7e949c46SKenneth D. Merry  * as colon-separated octets, placing result the given mac array.  mac must be
2452*7e949c46SKenneth D. Merry  * a preallocated array of length ETHER_ADDR_LEN ETH_ALEN (as declared in
2453*7e949c46SKenneth D. Merry  * net/ethernet.h).
2454*7e949c46SKenneth D. Merry  * Return 0 on success, or errno on error.
2455*7e949c46SKenneth D. Merry  */
2456*7e949c46SKenneth D. Merry static int
2457*7e949c46SKenneth D. Merry xen_net_read_mac(device_t dev, uint8_t mac[])
2458*7e949c46SKenneth D. Merry {
2459*7e949c46SKenneth D. Merry 	char *s, *e, *macstr;
2460*7e949c46SKenneth D. Merry 	const char *path;
2461*7e949c46SKenneth D. Merry 	int error = 0;
2462*7e949c46SKenneth D. Merry 	int i;
2463*7e949c46SKenneth D. Merry 
2464*7e949c46SKenneth D. Merry 	path = xenbus_get_node(dev);
2465*7e949c46SKenneth D. Merry 	error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr);
2466*7e949c46SKenneth D. Merry 	if (error != 0) {
2467*7e949c46SKenneth D. Merry 		xenbus_dev_fatal(dev, error, "parsing %s/mac", path);
2468*7e949c46SKenneth D. Merry 	} else {
2469*7e949c46SKenneth D. Merry 	        s = macstr;
2470*7e949c46SKenneth D. Merry 	        for (i = 0; i < ETHER_ADDR_LEN; i++) {
2471*7e949c46SKenneth D. Merry 		        mac[i] = strtoul(s, &e, 16);
2472*7e949c46SKenneth D. Merry 		        if (s == e || (e[0] != ':' && e[0] != 0)) {
2473*7e949c46SKenneth D. Merry 				error = ENOENT;
2474*7e949c46SKenneth D. Merry 				break;
2475*7e949c46SKenneth D. Merry 		        }
2476*7e949c46SKenneth D. Merry 		        s = &e[1];
2477*7e949c46SKenneth D. Merry 	        }
2478*7e949c46SKenneth D. Merry 	        free(macstr, M_XENBUS);
2479*7e949c46SKenneth D. Merry 	}
2480*7e949c46SKenneth D. Merry 	return error;
2481*7e949c46SKenneth D. Merry }
2482*7e949c46SKenneth D. Merry 
2483*7e949c46SKenneth D. Merry 
2484*7e949c46SKenneth D. Merry /**
2485*7e949c46SKenneth D. Merry  * Callback used by the generic networking code to tell us when our carrier
2486*7e949c46SKenneth D. Merry  * state has changed.  Since we don't have a physical carrier, we don't care
2487*7e949c46SKenneth D. Merry  */
2488*7e949c46SKenneth D. Merry static int
2489*7e949c46SKenneth D. Merry xnb_ifmedia_upd(struct ifnet *ifp)
2490*7e949c46SKenneth D. Merry {
2491*7e949c46SKenneth D. Merry 	return (0);
2492*7e949c46SKenneth D. Merry }
2493*7e949c46SKenneth D. Merry 
2494*7e949c46SKenneth D. Merry /**
2495*7e949c46SKenneth D. Merry  * Callback used by the generic networking code to ask us what our carrier
2496*7e949c46SKenneth D. Merry  * state is.  Since we don't have a physical carrier, this is very simple
2497*7e949c46SKenneth D. Merry  */
2498*7e949c46SKenneth D. Merry static void
2499*7e949c46SKenneth D. Merry xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2500*7e949c46SKenneth D. Merry {
2501*7e949c46SKenneth D. Merry 	ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE;
2502*7e949c46SKenneth D. Merry 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2503*7e949c46SKenneth D. Merry }
2504*7e949c46SKenneth D. Merry 
2505*7e949c46SKenneth D. Merry 
2506*7e949c46SKenneth D. Merry /*---------------------------- NewBus Registration ---------------------------*/
2507*7e949c46SKenneth D. Merry static device_method_t xnb_methods[] = {
250889e0f4d2SKip Macy 	/* Device interface */
2509*7e949c46SKenneth D. Merry 	DEVMETHOD(device_probe,		xnb_probe),
2510*7e949c46SKenneth D. Merry 	DEVMETHOD(device_attach,	xnb_attach),
2511*7e949c46SKenneth D. Merry 	DEVMETHOD(device_detach,	xnb_detach),
251289e0f4d2SKip Macy 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
2513*7e949c46SKenneth D. Merry 	DEVMETHOD(device_suspend,	xnb_suspend),
2514*7e949c46SKenneth D. Merry 	DEVMETHOD(device_resume,	xnb_resume),
2515*7e949c46SKenneth D. Merry 
2516*7e949c46SKenneth D. Merry 	/* Xenbus interface */
2517*7e949c46SKenneth D. Merry 	DEVMETHOD(xenbus_otherend_changed, xnb_frontend_changed),
2518*7e949c46SKenneth D. Merry 
251989e0f4d2SKip Macy 	{ 0, 0 }
252089e0f4d2SKip Macy };
252189e0f4d2SKip Macy 
2522*7e949c46SKenneth D. Merry static driver_t xnb_driver = {
2523*7e949c46SKenneth D. Merry 	"xnb",
2524*7e949c46SKenneth D. Merry 	xnb_methods,
2525*7e949c46SKenneth D. Merry 	sizeof(struct xnb_softc),
252689e0f4d2SKip Macy };
2527*7e949c46SKenneth D. Merry devclass_t xnb_devclass;
252889e0f4d2SKip Macy 
2529*7e949c46SKenneth D. Merry DRIVER_MODULE(xnb, xenbusb_back, xnb_driver, xnb_devclass, 0, 0);
253089e0f4d2SKip Macy 
253189e0f4d2SKip Macy 
2532*7e949c46SKenneth D. Merry /*-------------------------- Unit Tests -------------------------------------*/
2533*7e949c46SKenneth D. Merry #ifdef XNB_DEBUG
2534*7e949c46SKenneth D. Merry #include "netback_unit_tests.c"
2535*7e949c46SKenneth D. Merry #endif
2536