xref: /freebsd/sys/dev/xen/netback/netback.c (revision 691a22f94eedd8c0cda9bd7c21b92216153e909a)
17e949c46SKenneth D. Merry /*-
27e949c46SKenneth 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
97e949c46SKenneth D. Merry  *    notice, this list of conditions, and the following disclaimer,
107e949c46SKenneth D. Merry  *    without modification.
117e949c46SKenneth D. Merry  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
127e949c46SKenneth D. Merry  *    substantially similar to the "NO WARRANTY" disclaimer below
137e949c46SKenneth D. Merry  *    ("Disclaimer") and any redistribution must be conditioned upon
147e949c46SKenneth D. Merry  *    including a substantially similar Disclaimer requirement for further
157e949c46SKenneth D. Merry  *    binary redistribution.
1689e0f4d2SKip Macy  *
177e949c46SKenneth D. Merry  * NO WARRANTY
187e949c46SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
197e949c46SKenneth D. Merry  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
207e949c46SKenneth D. Merry  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
217e949c46SKenneth D. Merry  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
227e949c46SKenneth D. Merry  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
237e949c46SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
247e949c46SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
257e949c46SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
267e949c46SKenneth D. Merry  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
277e949c46SKenneth D. Merry  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
287e949c46SKenneth D. Merry  * POSSIBILITY OF SUCH DAMAGES.
297e949c46SKenneth D. Merry  *
307e949c46SKenneth D. Merry  * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
317e949c46SKenneth D. Merry  *          Alan Somers         (Spectra Logic Corporation)
327e949c46SKenneth D. Merry  *          John Suykerbuyk     (Spectra Logic Corporation)
3389e0f4d2SKip Macy  */
3489e0f4d2SKip Macy 
3589e0f4d2SKip Macy #include <sys/cdefs.h>
3689e0f4d2SKip Macy __FBSDID("$FreeBSD$");
377e949c46SKenneth D. Merry 
387e949c46SKenneth D. Merry /**
397e949c46SKenneth D. Merry  * \file netback.c
407e949c46SKenneth D. Merry  *
417e949c46SKenneth D. Merry  * \brief Device driver supporting the vending of network access
427e949c46SKenneth D. Merry  * 	  from this FreeBSD domain to other domains.
437e949c46SKenneth D. Merry  */
447e949c46SKenneth D. Merry #include "opt_inet.h"
45f909bbb4SGleb Smirnoff #include "opt_inet6.h"
467e949c46SKenneth 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>
537e949c46SKenneth D. Merry #include <sys/module.h>
547e949c46SKenneth D. Merry #include <sys/rman.h>
557e949c46SKenneth D. Merry #include <sys/socket.h>
567e949c46SKenneth D. Merry #include <sys/sockio.h>
5789e0f4d2SKip Macy #include <sys/sysctl.h>
5889e0f4d2SKip Macy 
5989e0f4d2SKip Macy #include <net/if.h>
6077374386SGleb Smirnoff #include <net/if_var.h>
6189e0f4d2SKip Macy #include <net/if_arp.h>
6289e0f4d2SKip Macy #include <net/ethernet.h>
637e949c46SKenneth D. Merry #include <net/if_dl.h>
647e949c46SKenneth D. Merry #include <net/if_media.h>
657e949c46SKenneth D. Merry #include <net/if_types.h>
6689e0f4d2SKip Macy 
6789e0f4d2SKip Macy #include <netinet/in.h>
6889e0f4d2SKip Macy #include <netinet/ip.h>
697e949c46SKenneth D. Merry #include <netinet/if_ether.h>
707e949c46SKenneth D. Merry #if __FreeBSD_version >= 700000
7189e0f4d2SKip Macy #include <netinet/tcp.h>
727e949c46SKenneth D. Merry #endif
737e949c46SKenneth D. Merry #include <netinet/ip_icmp.h>
7489e0f4d2SKip Macy #include <netinet/udp.h>
7589e0f4d2SKip Macy #include <machine/in_cksum.h>
7689e0f4d2SKip Macy 
777e949c46SKenneth D. Merry #include <vm/vm.h>
787e949c46SKenneth D. Merry #include <vm/pmap.h>
79048a50f3SKenneth D. Merry #include <vm/vm_extern.h>
80048a50f3SKenneth D. Merry #include <vm/vm_kern.h>
8189e0f4d2SKip Macy 
827e949c46SKenneth D. Merry #include <machine/_inttypes.h>
837e949c46SKenneth D. Merry 
8476acc41fSJustin T. Gibbs #include <xen/xen-os.h>
8576acc41fSJustin T. Gibbs #include <xen/hypervisor.h>
867e949c46SKenneth D. Merry #include <xen/xen_intr.h>
877e949c46SKenneth D. Merry #include <xen/interface/io/netif.h>
887e949c46SKenneth D. Merry #include <xen/xenbus/xenbusvar.h>
897e949c46SKenneth D. Merry 
9076acc41fSJustin T. Gibbs #include <machine/xen/xenvar.h>
9176acc41fSJustin T. Gibbs 
927e949c46SKenneth D. Merry /*--------------------------- Compile-time Tunables --------------------------*/
937e949c46SKenneth D. Merry 
947e949c46SKenneth D. Merry /*---------------------------------- Macros ----------------------------------*/
957e949c46SKenneth D. Merry /**
967e949c46SKenneth D. Merry  * Custom malloc type for all driver allocations.
977e949c46SKenneth D. Merry  */
987e949c46SKenneth D. Merry static MALLOC_DEFINE(M_XENNETBACK, "xnb", "Xen Net Back Driver Data");
997e949c46SKenneth D. Merry 
1007e949c46SKenneth D. Merry #define	XNB_SG	1	/* netback driver supports feature-sg */
101*691a22f9SRoger Pau Monné #define	XNB_GSO_TCPV4 0	/* netback driver supports feature-gso-tcpv4 */
1027e949c46SKenneth D. Merry #define	XNB_RX_COPY 1	/* netback driver supports feature-rx-copy */
1037e949c46SKenneth D. Merry #define	XNB_RX_FLIP 0	/* netback driver does not support feature-rx-flip */
1047e949c46SKenneth D. Merry 
1057e949c46SKenneth D. Merry #undef XNB_DEBUG
1067e949c46SKenneth D. Merry #define	XNB_DEBUG /* hardcode on during development */
1077e949c46SKenneth D. Merry 
1087e949c46SKenneth D. Merry #ifdef XNB_DEBUG
10989e0f4d2SKip Macy #define	DPRINTF(fmt, args...) \
1107e949c46SKenneth D. Merry 	printf("xnb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
11189e0f4d2SKip Macy #else
1127e949c46SKenneth D. Merry #define	DPRINTF(fmt, args...) do {} while (0)
11389e0f4d2SKip Macy #endif
11489e0f4d2SKip Macy 
1157e949c46SKenneth D. Merry /* Default length for stack-allocated grant tables */
1167e949c46SKenneth D. Merry #define	GNTTAB_LEN	(64)
11789e0f4d2SKip Macy 
1187e949c46SKenneth D. Merry /* Features supported by all backends.  TSO and LRO can be negotiated */
1197e949c46SKenneth D. Merry #define	XNB_CSUM_FEATURES	(CSUM_TCP | CSUM_UDP)
12089e0f4d2SKip Macy 
12189e0f4d2SKip Macy #define	NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE)
12289e0f4d2SKip Macy #define	NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE)
12389e0f4d2SKip Macy 
1247e949c46SKenneth D. Merry /**
1257e949c46SKenneth D. Merry  * Two argument version of the standard macro.  Second argument is a tentative
1267e949c46SKenneth D. Merry  * value of req_cons
1277e949c46SKenneth D. Merry  */
1287e949c46SKenneth D. Merry #define	RING_HAS_UNCONSUMED_REQUESTS_2(_r, cons) ({                     \
1297e949c46SKenneth D. Merry 	unsigned int req = (_r)->sring->req_prod - cons;          	\
1307e949c46SKenneth D. Merry 	unsigned int rsp = RING_SIZE(_r) -                              \
1317e949c46SKenneth D. Merry 	(cons - (_r)->rsp_prod_pvt);                          		\
1327e949c46SKenneth D. Merry 	req < rsp ? req : rsp;                                          \
1337e949c46SKenneth D. Merry })
1347e949c46SKenneth D. Merry 
1357e949c46SKenneth D. Merry #define	virt_to_mfn(x) (vtomach(x) >> PAGE_SHIFT)
1367e949c46SKenneth D. Merry #define	virt_to_offset(x) ((x) & (PAGE_SIZE - 1))
1377e949c46SKenneth D. Merry 
1387e949c46SKenneth D. Merry /**
1397e949c46SKenneth D. Merry  * Predefined array type of grant table copy descriptors.  Used to pass around
1407e949c46SKenneth D. Merry  * statically allocated memory structures.
1417e949c46SKenneth D. Merry  */
1427e949c46SKenneth D. Merry typedef struct gnttab_copy gnttab_copy_table[GNTTAB_LEN];
1437e949c46SKenneth D. Merry 
1447e949c46SKenneth D. Merry /*--------------------------- Forward Declarations ---------------------------*/
1457e949c46SKenneth D. Merry struct xnb_softc;
1467e949c46SKenneth D. Merry struct xnb_pkt;
1477e949c46SKenneth D. Merry 
1487e949c46SKenneth D. Merry static void	xnb_attach_failed(struct xnb_softc *xnb,
1497e949c46SKenneth D. Merry 				  int err, const char *fmt, ...)
1507e949c46SKenneth D. Merry 				  __printflike(3,4);
1517e949c46SKenneth D. Merry static int	xnb_shutdown(struct xnb_softc *xnb);
1527e949c46SKenneth D. Merry static int	create_netdev(device_t dev);
1537e949c46SKenneth D. Merry static int	xnb_detach(device_t dev);
1547e949c46SKenneth D. Merry static int	xnb_ifmedia_upd(struct ifnet *ifp);
1557e949c46SKenneth D. Merry static void	xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
1567e949c46SKenneth D. Merry static void 	xnb_intr(void *arg);
1577e949c46SKenneth D. Merry static int	xnb_send(netif_rx_back_ring_t *rxb, domid_t otherend,
1587e949c46SKenneth D. Merry 			 const struct mbuf *mbufc, gnttab_copy_table gnttab);
1597e949c46SKenneth D. Merry static int	xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend,
1607e949c46SKenneth D. Merry 			 struct mbuf **mbufc, struct ifnet *ifnet,
1617e949c46SKenneth D. Merry 			 gnttab_copy_table gnttab);
1627e949c46SKenneth D. Merry static int	xnb_ring2pkt(struct xnb_pkt *pkt,
1637e949c46SKenneth D. Merry 			     const netif_tx_back_ring_t *tx_ring,
1647e949c46SKenneth D. Merry 			     RING_IDX start);
1657e949c46SKenneth D. Merry static void	xnb_txpkt2rsp(const struct xnb_pkt *pkt,
1667e949c46SKenneth D. Merry 			      netif_tx_back_ring_t *ring, int error);
1677e949c46SKenneth D. Merry static struct mbuf *xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp);
1687e949c46SKenneth D. Merry static int	xnb_txpkt2gnttab(const struct xnb_pkt *pkt,
1697e949c46SKenneth D. Merry 				 const struct mbuf *mbufc,
1707e949c46SKenneth D. Merry 				 gnttab_copy_table gnttab,
1717e949c46SKenneth D. Merry 				 const netif_tx_back_ring_t *txb,
1727e949c46SKenneth D. Merry 				 domid_t otherend_id);
1737e949c46SKenneth D. Merry static void	xnb_update_mbufc(struct mbuf *mbufc,
1747e949c46SKenneth D. Merry 				 const gnttab_copy_table gnttab, int n_entries);
1757e949c46SKenneth D. Merry static int	xnb_mbufc2pkt(const struct mbuf *mbufc,
1767e949c46SKenneth D. Merry 			      struct xnb_pkt *pkt,
1777e949c46SKenneth D. Merry 			      RING_IDX start, int space);
1787e949c46SKenneth D. Merry static int	xnb_rxpkt2gnttab(const struct xnb_pkt *pkt,
1797e949c46SKenneth D. Merry 				 const struct mbuf *mbufc,
1807e949c46SKenneth D. Merry 				 gnttab_copy_table gnttab,
1817e949c46SKenneth D. Merry 				 const netif_rx_back_ring_t *rxb,
1827e949c46SKenneth D. Merry 				 domid_t otherend_id);
1837e949c46SKenneth D. Merry static int	xnb_rxpkt2rsp(const struct xnb_pkt *pkt,
1847e949c46SKenneth D. Merry 			      const gnttab_copy_table gnttab, int n_entries,
1857e949c46SKenneth D. Merry 			      netif_rx_back_ring_t *ring);
1867e949c46SKenneth D. Merry static void	xnb_stop(struct xnb_softc*);
1877e949c46SKenneth D. Merry static int	xnb_ioctl(struct ifnet*, u_long, caddr_t);
1887e949c46SKenneth D. Merry static void	xnb_start_locked(struct ifnet*);
1897e949c46SKenneth D. Merry static void	xnb_start(struct ifnet*);
1907e949c46SKenneth D. Merry static void	xnb_ifinit_locked(struct xnb_softc*);
1917e949c46SKenneth D. Merry static void	xnb_ifinit(void*);
1927e949c46SKenneth D. Merry #ifdef XNB_DEBUG
1937e949c46SKenneth D. Merry static int	xnb_unit_test_main(SYSCTL_HANDLER_ARGS);
1947e949c46SKenneth D. Merry static int	xnb_dump_rings(SYSCTL_HANDLER_ARGS);
1957e949c46SKenneth D. Merry #endif
196f909bbb4SGleb Smirnoff #if defined(INET) || defined(INET6)
197f909bbb4SGleb Smirnoff static void	xnb_add_mbuf_cksum(struct mbuf *mbufc);
198f909bbb4SGleb Smirnoff #endif
1997e949c46SKenneth D. Merry /*------------------------------ Data Structures -----------------------------*/
2007e949c46SKenneth D. Merry 
2017e949c46SKenneth D. Merry 
2027e949c46SKenneth D. Merry /**
2037e949c46SKenneth D. Merry  * Representation of a xennet packet.  Simplified version of a packet as
2047e949c46SKenneth D. Merry  * stored in the Xen tx ring.  Applicable to both RX and TX packets
2057e949c46SKenneth D. Merry  */
2067e949c46SKenneth D. Merry struct xnb_pkt{
2077e949c46SKenneth D. Merry 	/**
2087e949c46SKenneth D. Merry 	 * Array index of the first data-bearing (eg, not extra info) entry
2097e949c46SKenneth D. Merry 	 * for this packet
2107e949c46SKenneth D. Merry 	 */
2117e949c46SKenneth D. Merry 	RING_IDX	car;
2127e949c46SKenneth D. Merry 
2137e949c46SKenneth D. Merry 	/**
2147e949c46SKenneth D. Merry 	 * Array index of the second data-bearing entry for this packet.
2157e949c46SKenneth D. Merry 	 * Invalid if the packet has only one data-bearing entry.  If the
2167e949c46SKenneth D. Merry 	 * packet has more than two data-bearing entries, then the second
2177e949c46SKenneth D. Merry 	 * through the last will be sequential modulo the ring size
2187e949c46SKenneth D. Merry 	 */
2197e949c46SKenneth D. Merry 	RING_IDX	cdr;
2207e949c46SKenneth D. Merry 
2217e949c46SKenneth D. Merry 	/**
2227e949c46SKenneth D. Merry 	 * Optional extra info.  Only valid if flags contains
2237e949c46SKenneth D. Merry 	 * NETTXF_extra_info.  Note that extra.type will always be
2247e949c46SKenneth D. Merry 	 * XEN_NETIF_EXTRA_TYPE_GSO.  Currently, no known netfront or netback
2257e949c46SKenneth D. Merry 	 * driver will ever set XEN_NETIF_EXTRA_TYPE_MCAST_*
2267e949c46SKenneth D. Merry 	 */
2277e949c46SKenneth D. Merry 	netif_extra_info_t extra;
2287e949c46SKenneth D. Merry 
2297e949c46SKenneth D. Merry 	/** Size of entire packet in bytes.       */
2307e949c46SKenneth D. Merry 	uint16_t	size;
2317e949c46SKenneth D. Merry 
2327e949c46SKenneth D. Merry 	/** The size of the first entry's data in bytes */
2337e949c46SKenneth D. Merry 	uint16_t	car_size;
2347e949c46SKenneth D. Merry 
2357e949c46SKenneth D. Merry 	/**
2367e949c46SKenneth D. Merry 	 * Either NETTXF_ or NETRXF_ flags.  Note that the flag values are
2377e949c46SKenneth D. Merry 	 * not the same for TX and RX packets
2387e949c46SKenneth D. Merry 	 */
2397e949c46SKenneth D. Merry 	uint16_t	flags;
2407e949c46SKenneth D. Merry 
2417e949c46SKenneth D. Merry 	/**
2427e949c46SKenneth D. Merry 	 * The number of valid data-bearing entries (either netif_tx_request's
2437e949c46SKenneth D. Merry 	 * or netif_rx_response's) in the packet.  If this is 0, it means the
2447e949c46SKenneth D. Merry 	 * entire packet is invalid.
2457e949c46SKenneth D. Merry 	 */
2467e949c46SKenneth D. Merry 	uint16_t	list_len;
2477e949c46SKenneth D. Merry 
2487e949c46SKenneth D. Merry 	/** There was an error processing the packet */
2497e949c46SKenneth D. Merry 	uint8_t		error;
25089e0f4d2SKip Macy };
25189e0f4d2SKip Macy 
2527e949c46SKenneth D. Merry /** xnb_pkt method: initialize it */
2537e949c46SKenneth D. Merry static inline void
2547e949c46SKenneth D. Merry xnb_pkt_initialize(struct xnb_pkt *pxnb)
2557e949c46SKenneth D. Merry {
2567e949c46SKenneth D. Merry 	bzero(pxnb, sizeof(*pxnb));
2577e949c46SKenneth D. Merry }
25889e0f4d2SKip Macy 
2597e949c46SKenneth D. Merry /** xnb_pkt method: mark the packet as valid */
2607e949c46SKenneth D. Merry static inline void
2617e949c46SKenneth D. Merry xnb_pkt_validate(struct xnb_pkt *pxnb)
2627e949c46SKenneth D. Merry {
2637e949c46SKenneth D. Merry 	pxnb->error = 0;
2647e949c46SKenneth D. Merry };
26589e0f4d2SKip Macy 
2667e949c46SKenneth D. Merry /** xnb_pkt method: mark the packet as invalid */
2677e949c46SKenneth D. Merry static inline void
2687e949c46SKenneth D. Merry xnb_pkt_invalidate(struct xnb_pkt *pxnb)
2697e949c46SKenneth D. Merry {
2707e949c46SKenneth D. Merry 	pxnb->error = 1;
2717e949c46SKenneth D. Merry };
27289e0f4d2SKip Macy 
2737e949c46SKenneth D. Merry /** xnb_pkt method: Check whether the packet is valid */
2747e949c46SKenneth D. Merry static inline int
2757e949c46SKenneth D. Merry xnb_pkt_is_valid(const struct xnb_pkt *pxnb)
2767e949c46SKenneth D. Merry {
2777e949c46SKenneth D. Merry 	return (! pxnb->error);
2787e949c46SKenneth D. Merry }
2797e949c46SKenneth D. Merry 
2807e949c46SKenneth D. Merry #ifdef XNB_DEBUG
2817e949c46SKenneth D. Merry /** xnb_pkt method: print the packet's contents in human-readable format*/
2827e949c46SKenneth D. Merry static void __unused
2837e949c46SKenneth D. Merry xnb_dump_pkt(const struct xnb_pkt *pkt) {
2847e949c46SKenneth D. Merry 	if (pkt == NULL) {
2857e949c46SKenneth D. Merry 	  DPRINTF("Was passed a null pointer.\n");
2867e949c46SKenneth D. Merry 	  return;
2877e949c46SKenneth D. Merry 	}
2887e949c46SKenneth D. Merry 	DPRINTF("pkt address= %p\n", pkt);
2897e949c46SKenneth D. Merry 	DPRINTF("pkt->size=%d\n", pkt->size);
2907e949c46SKenneth D. Merry 	DPRINTF("pkt->car_size=%d\n", pkt->car_size);
2917e949c46SKenneth D. Merry 	DPRINTF("pkt->flags=0x%04x\n", pkt->flags);
2927e949c46SKenneth D. Merry 	DPRINTF("pkt->list_len=%d\n", pkt->list_len);
2937e949c46SKenneth D. Merry 	/* DPRINTF("pkt->extra");	TODO */
2947e949c46SKenneth D. Merry 	DPRINTF("pkt->car=%d\n", pkt->car);
2957e949c46SKenneth D. Merry 	DPRINTF("pkt->cdr=%d\n", pkt->cdr);
2967e949c46SKenneth D. Merry 	DPRINTF("pkt->error=%d\n", pkt->error);
2977e949c46SKenneth D. Merry }
2987e949c46SKenneth D. Merry #endif /* XNB_DEBUG */
2997e949c46SKenneth D. Merry 
3007e949c46SKenneth D. Merry static void
3017e949c46SKenneth D. Merry xnb_dump_txreq(RING_IDX idx, const struct netif_tx_request *txreq)
3027e949c46SKenneth D. Merry {
3037e949c46SKenneth D. Merry 	if (txreq != NULL) {
3047e949c46SKenneth D. Merry 		DPRINTF("netif_tx_request index =%u\n", idx);
3057e949c46SKenneth D. Merry 		DPRINTF("netif_tx_request.gref  =%u\n", txreq->gref);
3067e949c46SKenneth D. Merry 		DPRINTF("netif_tx_request.offset=%hu\n", txreq->offset);
3077e949c46SKenneth D. Merry 		DPRINTF("netif_tx_request.flags =%hu\n", txreq->flags);
3087e949c46SKenneth D. Merry 		DPRINTF("netif_tx_request.id    =%hu\n", txreq->id);
3097e949c46SKenneth D. Merry 		DPRINTF("netif_tx_request.size  =%hu\n", txreq->size);
3107e949c46SKenneth D. Merry 	}
3117e949c46SKenneth D. Merry }
3127e949c46SKenneth D. Merry 
3137e949c46SKenneth D. Merry 
3147e949c46SKenneth D. Merry /**
3157e949c46SKenneth D. Merry  * \brief Configuration data for a shared memory request ring
3167e949c46SKenneth D. Merry  *        used to communicate with the front-end client of this
3177e949c46SKenneth D. Merry  *        this driver.
3187e949c46SKenneth D. Merry  */
3197e949c46SKenneth D. Merry struct xnb_ring_config {
3207e949c46SKenneth D. Merry 	/**
3217e949c46SKenneth D. Merry 	 * Runtime structures for ring access.  Unfortunately, TX and RX rings
3227e949c46SKenneth D. Merry 	 * use different data structures, and that cannot be changed since it
3237e949c46SKenneth D. Merry 	 * is part of the interdomain protocol.
3247e949c46SKenneth D. Merry 	 */
3257e949c46SKenneth D. Merry 	union{
3267e949c46SKenneth D. Merry 		netif_rx_back_ring_t	  rx_ring;
3277e949c46SKenneth D. Merry 		netif_tx_back_ring_t	  tx_ring;
3287e949c46SKenneth D. Merry 	} back_ring;
3297e949c46SKenneth D. Merry 
3307e949c46SKenneth D. Merry 	/**
3317e949c46SKenneth D. Merry 	 * The device bus address returned by the hypervisor when
3327e949c46SKenneth D. Merry 	 * mapping the ring and required to unmap it when a connection
3337e949c46SKenneth D. Merry 	 * is torn down.
3347e949c46SKenneth D. Merry 	 */
3357e949c46SKenneth D. Merry 	uint64_t	bus_addr;
3367e949c46SKenneth D. Merry 
3377e949c46SKenneth D. Merry 	/** The pseudo-physical address where ring memory is mapped.*/
3387e949c46SKenneth D. Merry 	uint64_t	gnt_addr;
3397e949c46SKenneth D. Merry 
3407e949c46SKenneth D. Merry 	/** KVA address where ring memory is mapped. */
3417e949c46SKenneth D. Merry 	vm_offset_t	va;
3427e949c46SKenneth D. Merry 
3437e949c46SKenneth D. Merry 	/**
3447e949c46SKenneth D. Merry 	 * Grant table handles, one per-ring page, returned by the
3457e949c46SKenneth D. Merry 	 * hyperpervisor upon mapping of the ring and required to
3467e949c46SKenneth D. Merry 	 * unmap it when a connection is torn down.
3477e949c46SKenneth D. Merry 	 */
3487e949c46SKenneth D. Merry 	grant_handle_t	handle;
3497e949c46SKenneth D. Merry 
3507e949c46SKenneth D. Merry 	/** The number of ring pages mapped for the current connection. */
3517e949c46SKenneth D. Merry 	unsigned	ring_pages;
3527e949c46SKenneth D. Merry 
3537e949c46SKenneth D. Merry 	/**
3547e949c46SKenneth D. Merry 	 * The grant references, one per-ring page, supplied by the
3557e949c46SKenneth D. Merry 	 * front-end, allowing us to reference the ring pages in the
3567e949c46SKenneth D. Merry 	 * front-end's domain and to map these pages into our own domain.
3577e949c46SKenneth D. Merry 	 */
3587e949c46SKenneth D. Merry 	grant_ref_t	ring_ref;
3597e949c46SKenneth D. Merry };
3607e949c46SKenneth D. Merry 
3617e949c46SKenneth D. Merry /**
3627e949c46SKenneth D. Merry  * Per-instance connection state flags.
3637e949c46SKenneth D. Merry  */
3647e949c46SKenneth D. Merry typedef enum
3657e949c46SKenneth D. Merry {
3667e949c46SKenneth D. Merry 	/** Communication with the front-end has been established. */
3677e949c46SKenneth D. Merry 	XNBF_RING_CONNECTED    = 0x01,
3687e949c46SKenneth D. Merry 
3697e949c46SKenneth D. Merry 	/**
3707e949c46SKenneth D. Merry 	 * Front-end requests exist in the ring and are waiting for
3717e949c46SKenneth D. Merry 	 * xnb_xen_req objects to free up.
3727e949c46SKenneth D. Merry 	 */
3737e949c46SKenneth D. Merry 	XNBF_RESOURCE_SHORTAGE = 0x02,
3747e949c46SKenneth D. Merry 
3757e949c46SKenneth D. Merry 	/** Connection teardown has started. */
3767e949c46SKenneth D. Merry 	XNBF_SHUTDOWN          = 0x04,
3777e949c46SKenneth D. Merry 
3787e949c46SKenneth D. Merry 	/** A thread is already performing shutdown processing. */
3797e949c46SKenneth D. Merry 	XNBF_IN_SHUTDOWN       = 0x08
3807e949c46SKenneth D. Merry } xnb_flag_t;
3817e949c46SKenneth D. Merry 
3827e949c46SKenneth D. Merry /**
3837e949c46SKenneth D. Merry  * Types of rings.  Used for array indices and to identify a ring's control
3847e949c46SKenneth D. Merry  * data structure type
3857e949c46SKenneth D. Merry  */
3867e949c46SKenneth D. Merry typedef enum{
3877e949c46SKenneth D. Merry 	XNB_RING_TYPE_TX = 0,	/* ID of TX rings, used for array indices */
3887e949c46SKenneth D. Merry 	XNB_RING_TYPE_RX = 1,	/* ID of RX rings, used for array indices */
3897e949c46SKenneth D. Merry 	XNB_NUM_RING_TYPES
3907e949c46SKenneth D. Merry } xnb_ring_type_t;
3917e949c46SKenneth D. Merry 
3927e949c46SKenneth D. Merry /**
3937e949c46SKenneth D. Merry  * Per-instance configuration data.
3947e949c46SKenneth D. Merry  */
3957e949c46SKenneth D. Merry struct xnb_softc {
3967e949c46SKenneth D. Merry 	/** NewBus device corresponding to this instance. */
3977e949c46SKenneth D. Merry 	device_t		dev;
3987e949c46SKenneth D. Merry 
3997e949c46SKenneth D. Merry 	/* Media related fields */
4007e949c46SKenneth D. Merry 
4017e949c46SKenneth D. Merry 	/** Generic network media state */
4027e949c46SKenneth D. Merry 	struct ifmedia		sc_media;
4037e949c46SKenneth D. Merry 
4047e949c46SKenneth D. Merry 	/** Media carrier info */
4057e949c46SKenneth D. Merry 	struct ifnet 		*xnb_ifp;
4067e949c46SKenneth D. Merry 
4077e949c46SKenneth D. Merry 	/** Our own private carrier state */
4087e949c46SKenneth D. Merry 	unsigned carrier;
4097e949c46SKenneth D. Merry 
4107e949c46SKenneth D. Merry 	/** Device MAC Address */
4117e949c46SKenneth D. Merry 	uint8_t			mac[ETHER_ADDR_LEN];
4127e949c46SKenneth D. Merry 
4137e949c46SKenneth D. Merry 	/* Xen related fields */
4147e949c46SKenneth D. Merry 
4157e949c46SKenneth D. Merry 	/**
4167e949c46SKenneth D. Merry 	 * \brief The netif protocol abi in effect.
4177e949c46SKenneth D. Merry 	 *
4187e949c46SKenneth D. Merry 	 * There are situations where the back and front ends can
4197e949c46SKenneth D. Merry 	 * have a different, native abi (e.g. intel x86_64 and
4207e949c46SKenneth D. Merry 	 * 32bit x86 domains on the same machine).  The back-end
4217e949c46SKenneth D. Merry 	 * always accomodates the front-end's native abi.  That
4227e949c46SKenneth D. Merry 	 * value is pulled from the XenStore and recorded here.
4237e949c46SKenneth D. Merry 	 */
4247e949c46SKenneth D. Merry 	int			abi;
4257e949c46SKenneth D. Merry 
4267e949c46SKenneth D. Merry 	/**
4277e949c46SKenneth D. Merry 	 * Name of the bridge to which this VIF is connected, if any
4287e949c46SKenneth D. Merry 	 * This field is dynamically allocated by xenbus and must be free()ed
4297e949c46SKenneth D. Merry 	 * when no longer needed
4307e949c46SKenneth D. Merry 	 */
43189e0f4d2SKip Macy 	char			*bridge;
43289e0f4d2SKip Macy 
4337e949c46SKenneth D. Merry 	/** The interrupt driven even channel used to signal ring events. */
43489e0f4d2SKip Macy 	evtchn_port_t		evtchn;
4357e949c46SKenneth D. Merry 
4367e949c46SKenneth D. Merry 	/** Xen device handle.*/
4377e949c46SKenneth D. Merry 	long 			handle;
4387e949c46SKenneth D. Merry 
43976acc41fSJustin T. Gibbs 	/** Handle to the communication ring event channel. */
44076acc41fSJustin T. Gibbs 	xen_intr_handle_t	xen_intr_handle;
44189e0f4d2SKip Macy 
4427e949c46SKenneth D. Merry 	/**
4437e949c46SKenneth D. Merry 	 * \brief Cached value of the front-end's domain id.
4447e949c46SKenneth D. Merry 	 *
4457e949c46SKenneth D. Merry 	 * This value is used at once for each mapped page in
4467e949c46SKenneth D. Merry 	 * a transaction.  We cache it to avoid incuring the
4477e949c46SKenneth D. Merry 	 * cost of an ivar access every time this is needed.
4487e949c46SKenneth D. Merry 	 */
4497e949c46SKenneth D. Merry 	domid_t			otherend_id;
45089e0f4d2SKip Macy 
4517e949c46SKenneth D. Merry 	/**
4527e949c46SKenneth D. Merry 	 * Undocumented frontend feature.  Has something to do with
4537e949c46SKenneth D. Merry 	 * scatter/gather IO
4547e949c46SKenneth D. Merry 	 */
4557e949c46SKenneth D. Merry 	uint8_t			can_sg;
4567e949c46SKenneth D. Merry 	/** Undocumented frontend feature */
4577e949c46SKenneth D. Merry 	uint8_t			gso;
4587e949c46SKenneth D. Merry 	/** Undocumented frontend feature */
4597e949c46SKenneth D. Merry 	uint8_t			gso_prefix;
4607e949c46SKenneth D. Merry 	/** Can checksum TCP/UDP over IPv4 */
4617e949c46SKenneth D. Merry 	uint8_t			ip_csum;
46289e0f4d2SKip Macy 
4637e949c46SKenneth D. Merry 	/* Implementation related fields */
4647e949c46SKenneth D. Merry 	/**
4657e949c46SKenneth D. Merry 	 * Preallocated grant table copy descriptor for RX operations.
4667e949c46SKenneth D. Merry 	 * Access must be protected by rx_lock
4677e949c46SKenneth D. Merry 	 */
4687e949c46SKenneth D. Merry 	gnttab_copy_table	rx_gnttab;
46989e0f4d2SKip Macy 
4707e949c46SKenneth D. Merry 	/**
4717e949c46SKenneth D. Merry 	 * Preallocated grant table copy descriptor for TX operations.
4727e949c46SKenneth D. Merry 	 * Access must be protected by tx_lock
4737e949c46SKenneth D. Merry 	 */
4747e949c46SKenneth D. Merry 	gnttab_copy_table	tx_gnttab;
47589e0f4d2SKip Macy 
4767e949c46SKenneth D. Merry #ifdef XENHVM
4777e949c46SKenneth D. Merry 	/**
4787e949c46SKenneth D. Merry 	 * Resource representing allocated physical address space
4797e949c46SKenneth D. Merry 	 * associated with our per-instance kva region.
4807e949c46SKenneth D. Merry 	 */
4817e949c46SKenneth D. Merry 	struct resource		*pseudo_phys_res;
48289e0f4d2SKip Macy 
4837e949c46SKenneth D. Merry 	/** Resource id for allocated physical address space. */
4847e949c46SKenneth D. Merry 	int			pseudo_phys_res_id;
4857e949c46SKenneth D. Merry #endif
48689e0f4d2SKip Macy 
4877e949c46SKenneth D. Merry 	/** Ring mapping and interrupt configuration data. */
4887e949c46SKenneth D. Merry 	struct xnb_ring_config	ring_configs[XNB_NUM_RING_TYPES];
48989e0f4d2SKip Macy 
4907e949c46SKenneth D. Merry 	/**
4917e949c46SKenneth D. Merry 	 * Global pool of kva used for mapping remote domain ring
4927e949c46SKenneth D. Merry 	 * and I/O transaction data.
4937e949c46SKenneth D. Merry 	 */
4947e949c46SKenneth D. Merry 	vm_offset_t		kva;
49589e0f4d2SKip Macy 
4967e949c46SKenneth D. Merry 	/** Psuedo-physical address corresponding to kva. */
4977e949c46SKenneth D. Merry 	uint64_t		gnt_base_addr;
49889e0f4d2SKip Macy 
4997e949c46SKenneth D. Merry 	/** Various configuration and state bit flags. */
5007e949c46SKenneth D. Merry 	xnb_flag_t		flags;
50189e0f4d2SKip Macy 
5027e949c46SKenneth D. Merry 	/** Mutex protecting per-instance data in the receive path. */
5037e949c46SKenneth D. Merry 	struct mtx		rx_lock;
50489e0f4d2SKip Macy 
5057e949c46SKenneth D. Merry 	/** Mutex protecting per-instance data in the softc structure. */
5067e949c46SKenneth D. Merry 	struct mtx		sc_lock;
50789e0f4d2SKip Macy 
5087e949c46SKenneth D. Merry 	/** Mutex protecting per-instance data in the transmit path. */
5097e949c46SKenneth D. Merry 	struct mtx		tx_lock;
51089e0f4d2SKip Macy 
5117e949c46SKenneth D. Merry 	/** The size of the global kva pool. */
5127e949c46SKenneth D. Merry 	int			kva_size;
513ddcc16cfSRoger Pau Monné 
514ddcc16cfSRoger Pau Monné 	/** Name of the interface */
515ddcc16cfSRoger Pau Monné 	char			 if_name[IFNAMSIZ];
5167e949c46SKenneth D. Merry };
5177e949c46SKenneth D. Merry 
5187e949c46SKenneth D. Merry /*---------------------------- Debugging functions ---------------------------*/
5197e949c46SKenneth D. Merry #ifdef XNB_DEBUG
5207e949c46SKenneth D. Merry static void __unused
5217e949c46SKenneth D. Merry xnb_dump_gnttab_copy(const struct gnttab_copy *entry)
52289e0f4d2SKip Macy {
5237e949c46SKenneth D. Merry 	if (entry == NULL) {
5247e949c46SKenneth D. Merry 		printf("NULL grant table pointer\n");
5257e949c46SKenneth D. Merry 		return;
52689e0f4d2SKip Macy 	}
5277e949c46SKenneth D. Merry 
5287e949c46SKenneth D. Merry 	if (entry->flags & GNTCOPY_dest_gref)
5297e949c46SKenneth D. Merry 		printf("gnttab dest ref=\t%u\n", entry->dest.u.ref);
5307e949c46SKenneth D. Merry 	else
5317e949c46SKenneth D. Merry 		printf("gnttab dest gmfn=\t%lu\n", entry->dest.u.gmfn);
5327e949c46SKenneth D. Merry 	printf("gnttab dest offset=\t%hu\n", entry->dest.offset);
5337e949c46SKenneth D. Merry 	printf("gnttab dest domid=\t%hu\n", entry->dest.domid);
5347e949c46SKenneth D. Merry 	if (entry->flags & GNTCOPY_source_gref)
5357e949c46SKenneth D. Merry 		printf("gnttab source ref=\t%u\n", entry->source.u.ref);
5367e949c46SKenneth D. Merry 	else
5377e949c46SKenneth D. Merry 		printf("gnttab source gmfn=\t%lu\n", entry->source.u.gmfn);
5387e949c46SKenneth D. Merry 	printf("gnttab source offset=\t%hu\n", entry->source.offset);
5397e949c46SKenneth D. Merry 	printf("gnttab source domid=\t%hu\n", entry->source.domid);
5407e949c46SKenneth D. Merry 	printf("gnttab len=\t%hu\n", entry->len);
5417e949c46SKenneth D. Merry 	printf("gnttab flags=\t%hu\n", entry->flags);
5427e949c46SKenneth D. Merry 	printf("gnttab status=\t%hd\n", entry->status);
5437e949c46SKenneth D. Merry }
5447e949c46SKenneth D. Merry 
5457e949c46SKenneth D. Merry static int
5467e949c46SKenneth D. Merry xnb_dump_rings(SYSCTL_HANDLER_ARGS)
5477e949c46SKenneth D. Merry {
5487e949c46SKenneth D. Merry 	static char results[720];
5497e949c46SKenneth D. Merry 	struct xnb_softc const* xnb = (struct xnb_softc*)arg1;
5507e949c46SKenneth D. Merry 	netif_rx_back_ring_t const* rxb =
5517e949c46SKenneth D. Merry 		&xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring;
5527e949c46SKenneth D. Merry 	netif_tx_back_ring_t const* txb =
5537e949c46SKenneth D. Merry 		&xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring;
5547e949c46SKenneth D. Merry 
5557e949c46SKenneth D. Merry 	/* empty the result strings */
5567e949c46SKenneth D. Merry 	results[0] = 0;
5577e949c46SKenneth D. Merry 
5587e949c46SKenneth D. Merry 	if ( !txb || !txb->sring || !rxb || !rxb->sring )
5597e949c46SKenneth D. Merry 		return (SYSCTL_OUT(req, results, strnlen(results, 720)));
5607e949c46SKenneth D. Merry 
5617e949c46SKenneth D. Merry 	snprintf(results, 720,
5627e949c46SKenneth D. Merry 	    "\n\t%35s %18s\n"	/* TX, RX */
5637e949c46SKenneth D. Merry 	    "\t%16s %18d %18d\n"	/* req_cons */
5647e949c46SKenneth D. Merry 	    "\t%16s %18d %18d\n"	/* nr_ents */
5657e949c46SKenneth D. Merry 	    "\t%16s %18d %18d\n"	/* rsp_prod_pvt */
5667e949c46SKenneth D. Merry 	    "\t%16s %18p %18p\n"	/* sring */
5677e949c46SKenneth D. Merry 	    "\t%16s %18d %18d\n"	/* req_prod */
5687e949c46SKenneth D. Merry 	    "\t%16s %18d %18d\n"	/* req_event */
5697e949c46SKenneth D. Merry 	    "\t%16s %18d %18d\n"	/* rsp_prod */
5707e949c46SKenneth D. Merry 	    "\t%16s %18d %18d\n",	/* rsp_event */
5717e949c46SKenneth D. Merry 	    "TX", "RX",
5727e949c46SKenneth D. Merry 	    "req_cons", txb->req_cons, rxb->req_cons,
5737e949c46SKenneth D. Merry 	    "nr_ents", txb->nr_ents, rxb->nr_ents,
5747e949c46SKenneth D. Merry 	    "rsp_prod_pvt", txb->rsp_prod_pvt, rxb->rsp_prod_pvt,
5757e949c46SKenneth D. Merry 	    "sring", txb->sring, rxb->sring,
5767e949c46SKenneth D. Merry 	    "sring->req_prod", txb->sring->req_prod, rxb->sring->req_prod,
5777e949c46SKenneth D. Merry 	    "sring->req_event", txb->sring->req_event, rxb->sring->req_event,
5787e949c46SKenneth D. Merry 	    "sring->rsp_prod", txb->sring->rsp_prod, rxb->sring->rsp_prod,
5797e949c46SKenneth D. Merry 	    "sring->rsp_event", txb->sring->rsp_event, rxb->sring->rsp_event);
5807e949c46SKenneth D. Merry 
5817e949c46SKenneth D. Merry 	return (SYSCTL_OUT(req, results, strnlen(results, 720)));
5827e949c46SKenneth D. Merry }
5837e949c46SKenneth D. Merry 
5847e949c46SKenneth D. Merry static void __unused
5857e949c46SKenneth D. Merry xnb_dump_mbuf(const struct mbuf *m)
5867e949c46SKenneth D. Merry {
5877e949c46SKenneth D. Merry 	int len;
5887e949c46SKenneth D. Merry 	uint8_t *d;
5897e949c46SKenneth D. Merry 	if (m == NULL)
5907e949c46SKenneth D. Merry 		return;
5917e949c46SKenneth D. Merry 
5927e949c46SKenneth D. Merry 	printf("xnb_dump_mbuf:\n");
5937e949c46SKenneth D. Merry 	if (m->m_flags & M_PKTHDR) {
5947e949c46SKenneth D. Merry 		printf("    flowid=%10d, csum_flags=%#8x, csum_data=%#8x, "
5957e949c46SKenneth D. Merry 		       "tso_segsz=%5hd\n",
596b072bdc1SAndre Oppermann 		       m->m_pkthdr.flowid, (int)m->m_pkthdr.csum_flags,
5977e949c46SKenneth D. Merry 		       m->m_pkthdr.csum_data, m->m_pkthdr.tso_segsz);
598b072bdc1SAndre Oppermann 		printf("    rcvif=%16p,  len=%19d\n",
599b072bdc1SAndre Oppermann 		       m->m_pkthdr.rcvif, m->m_pkthdr.len);
6007e949c46SKenneth D. Merry 	}
6017e949c46SKenneth D. Merry 	printf("    m_next=%16p, m_nextpk=%16p, m_data=%16p\n",
6027e949c46SKenneth D. Merry 	       m->m_next, m->m_nextpkt, m->m_data);
603b072bdc1SAndre Oppermann 	printf("    m_len=%17d, m_flags=%#15x, m_type=%18u\n",
6047e949c46SKenneth D. Merry 	       m->m_len, m->m_flags, m->m_type);
6057e949c46SKenneth D. Merry 
6067e949c46SKenneth D. Merry 	len = m->m_len;
6077e949c46SKenneth D. Merry 	d = mtod(m, uint8_t*);
6087e949c46SKenneth D. Merry 	while (len > 0) {
6097e949c46SKenneth D. Merry 		int i;
6107e949c46SKenneth D. Merry 		printf("                ");
6117e949c46SKenneth D. Merry 		for (i = 0; (i < 16) && (len > 0); i++, len--) {
6127e949c46SKenneth D. Merry 			printf("%02hhx ", *(d++));
61389e0f4d2SKip Macy 		}
61489e0f4d2SKip Macy 		printf("\n");
61589e0f4d2SKip Macy 	}
61689e0f4d2SKip Macy }
6177e949c46SKenneth D. Merry #endif /* XNB_DEBUG */
61889e0f4d2SKip Macy 
6197e949c46SKenneth D. Merry /*------------------------ Inter-Domain Communication ------------------------*/
6207e949c46SKenneth D. Merry /**
6217e949c46SKenneth D. Merry  * Free dynamically allocated KVA or pseudo-physical address allocations.
6227e949c46SKenneth D. Merry  *
6237e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
62489e0f4d2SKip Macy  */
62589e0f4d2SKip Macy static void
6267e949c46SKenneth D. Merry xnb_free_communication_mem(struct xnb_softc *xnb)
62789e0f4d2SKip Macy {
6287e949c46SKenneth D. Merry 	if (xnb->kva != 0) {
6297e949c46SKenneth D. Merry #ifndef XENHVM
6305df87b21SJeff Roberson 		kva_free(xnb->kva, xnb->kva_size);
63189e0f4d2SKip Macy #else
6327e949c46SKenneth D. Merry 		if (xnb->pseudo_phys_res != NULL) {
6337e949c46SKenneth D. Merry 			bus_release_resource(xnb->dev, SYS_RES_MEMORY,
6347e949c46SKenneth D. Merry 			    xnb->pseudo_phys_res_id,
6357e949c46SKenneth D. Merry 			    xnb->pseudo_phys_res);
6367e949c46SKenneth D. Merry 			xnb->pseudo_phys_res = NULL;
63789e0f4d2SKip Macy 		}
6387e949c46SKenneth D. Merry #endif /* XENHVM */
6397e949c46SKenneth D. Merry 	}
6407e949c46SKenneth D. Merry 	xnb->kva = 0;
6417e949c46SKenneth D. Merry 	xnb->gnt_base_addr = 0;
64289e0f4d2SKip Macy }
64389e0f4d2SKip Macy 
6447e949c46SKenneth D. Merry /**
6457e949c46SKenneth D. Merry  * Cleanup all inter-domain communication mechanisms.
6467e949c46SKenneth D. Merry  *
6477e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
6487e949c46SKenneth D. Merry  */
6497e949c46SKenneth D. Merry static int
6507e949c46SKenneth D. Merry xnb_disconnect(struct xnb_softc *xnb)
65189e0f4d2SKip Macy {
6527e949c46SKenneth D. Merry 	struct gnttab_unmap_grant_ref gnts[XNB_NUM_RING_TYPES];
6537e949c46SKenneth D. Merry 	int error;
6547e949c46SKenneth D. Merry 	int i;
65589e0f4d2SKip Macy 
6561b386f62SRoger Pau Monné 	if (xnb->xen_intr_handle != NULL)
6571b386f62SRoger Pau Monné 		xen_intr_unbind(&xnb->xen_intr_handle);
65889e0f4d2SKip Macy 
65989e0f4d2SKip Macy 	/*
6607e949c46SKenneth D. Merry 	 * We may still have another thread currently processing requests.  We
6617e949c46SKenneth D. Merry 	 * must acquire the rx and tx locks to make sure those threads are done,
6627e949c46SKenneth D. Merry 	 * but we can release those locks as soon as we acquire them, because no
6637e949c46SKenneth D. Merry 	 * more interrupts will be arriving.
66489e0f4d2SKip Macy 	 */
6657e949c46SKenneth D. Merry 	mtx_lock(&xnb->tx_lock);
6667e949c46SKenneth D. Merry 	mtx_unlock(&xnb->tx_lock);
6677e949c46SKenneth D. Merry 	mtx_lock(&xnb->rx_lock);
6687e949c46SKenneth D. Merry 	mtx_unlock(&xnb->rx_lock);
6697e949c46SKenneth D. Merry 
6707e949c46SKenneth D. Merry 	/* Free malloc'd softc member variables */
6711b386f62SRoger Pau Monné 	if (xnb->bridge != NULL) {
6727e949c46SKenneth D. Merry 		free(xnb->bridge, M_XENSTORE);
6731b386f62SRoger Pau Monné 		xnb->bridge = NULL;
6741b386f62SRoger Pau Monné 	}
6757e949c46SKenneth D. Merry 
6767e949c46SKenneth D. Merry 	/* All request processing has stopped, so unmap the rings */
6777e949c46SKenneth D. Merry 	for (i=0; i < XNB_NUM_RING_TYPES; i++) {
6787e949c46SKenneth D. Merry 		gnts[i].host_addr = xnb->ring_configs[i].gnt_addr;
6797e949c46SKenneth D. Merry 		gnts[i].dev_bus_addr = xnb->ring_configs[i].bus_addr;
6807e949c46SKenneth D. Merry 		gnts[i].handle = xnb->ring_configs[i].handle;
68189e0f4d2SKip Macy 	}
6827e949c46SKenneth D. Merry 	error = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, gnts,
6837e949c46SKenneth D. Merry 					  XNB_NUM_RING_TYPES);
6847e949c46SKenneth D. Merry 	KASSERT(error == 0, ("Grant table unmap op failed (%d)", error));
68589e0f4d2SKip Macy 
6867e949c46SKenneth D. Merry 	xnb_free_communication_mem(xnb);
6877e949c46SKenneth D. Merry 	/*
6887e949c46SKenneth D. Merry 	 * Zero the ring config structs because the pointers, handles, and
6897e949c46SKenneth D. Merry 	 * grant refs contained therein are no longer valid.
6907e949c46SKenneth D. Merry 	 */
6917e949c46SKenneth D. Merry 	bzero(&xnb->ring_configs[XNB_RING_TYPE_TX],
6927e949c46SKenneth D. Merry 	    sizeof(struct xnb_ring_config));
6937e949c46SKenneth D. Merry 	bzero(&xnb->ring_configs[XNB_RING_TYPE_RX],
6947e949c46SKenneth D. Merry 	    sizeof(struct xnb_ring_config));
69589e0f4d2SKip Macy 
6967e949c46SKenneth D. Merry 	xnb->flags &= ~XNBF_RING_CONNECTED;
6977e949c46SKenneth D. Merry 	return (0);
69889e0f4d2SKip Macy }
69989e0f4d2SKip Macy 
7007e949c46SKenneth D. Merry /**
7017e949c46SKenneth D. Merry  * Map a single shared memory ring into domain local address space and
7027e949c46SKenneth D. Merry  * initialize its control structure
7037e949c46SKenneth D. Merry  *
7047e949c46SKenneth D. Merry  * \param xnb	Per-instance xnb configuration structure
7057e949c46SKenneth D. Merry  * \param ring_type	Array index of this ring in the xnb's array of rings
7067e949c46SKenneth D. Merry  * \return 	An errno
7077e949c46SKenneth D. Merry  */
7087e949c46SKenneth D. Merry static int
7097e949c46SKenneth D. Merry xnb_connect_ring(struct xnb_softc *xnb, xnb_ring_type_t ring_type)
71089e0f4d2SKip Macy {
7117e949c46SKenneth D. Merry 	struct gnttab_map_grant_ref gnt;
7127e949c46SKenneth D. Merry 	struct xnb_ring_config *ring = &xnb->ring_configs[ring_type];
7137e949c46SKenneth D. Merry 	int error;
71489e0f4d2SKip Macy 
7157e949c46SKenneth D. Merry 	/* TX ring type = 0, RX =1 */
7167e949c46SKenneth D. Merry 	ring->va = xnb->kva + ring_type * PAGE_SIZE;
7177e949c46SKenneth D. Merry 	ring->gnt_addr = xnb->gnt_base_addr + ring_type * PAGE_SIZE;
71889e0f4d2SKip Macy 
7197e949c46SKenneth D. Merry 	gnt.host_addr = ring->gnt_addr;
7207e949c46SKenneth D. Merry 	gnt.flags     = GNTMAP_host_map;
7217e949c46SKenneth D. Merry 	gnt.ref       = ring->ring_ref;
7227e949c46SKenneth D. Merry 	gnt.dom       = xnb->otherend_id;
72389e0f4d2SKip Macy 
7247e949c46SKenneth D. Merry 	error = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &gnt, 1);
7257e949c46SKenneth D. Merry 	if (error != 0)
7267e949c46SKenneth D. Merry 		panic("netback: Ring page grant table op failed (%d)", error);
72789e0f4d2SKip Macy 
7287e949c46SKenneth D. Merry 	if (gnt.status != 0) {
7297e949c46SKenneth D. Merry 		ring->va = 0;
7307e949c46SKenneth D. Merry 		error = EACCES;
7317e949c46SKenneth D. Merry 		xenbus_dev_fatal(xnb->dev, error,
7327e949c46SKenneth D. Merry 				 "Ring shared page mapping failed. "
7337e949c46SKenneth D. Merry 				 "Status %d.", gnt.status);
73489e0f4d2SKip Macy 	} else {
7357e949c46SKenneth D. Merry 		ring->handle = gnt.handle;
7367e949c46SKenneth D. Merry 		ring->bus_addr = gnt.dev_bus_addr;
73789e0f4d2SKip Macy 
7387e949c46SKenneth D. Merry 		if (ring_type == XNB_RING_TYPE_TX) {
7397e949c46SKenneth D. Merry 			BACK_RING_INIT(&ring->back_ring.tx_ring,
7407e949c46SKenneth D. Merry 			    (netif_tx_sring_t*)ring->va,
7417e949c46SKenneth D. Merry 			    ring->ring_pages * PAGE_SIZE);
7427e949c46SKenneth D. Merry 		} else if (ring_type == XNB_RING_TYPE_RX) {
7437e949c46SKenneth D. Merry 			BACK_RING_INIT(&ring->back_ring.rx_ring,
7447e949c46SKenneth D. Merry 			    (netif_rx_sring_t*)ring->va,
7457e949c46SKenneth D. Merry 			    ring->ring_pages * PAGE_SIZE);
7467e949c46SKenneth D. Merry 		} else {
7477e949c46SKenneth D. Merry 			xenbus_dev_fatal(xnb->dev, error,
7487e949c46SKenneth D. Merry 				 "Unknown ring type %d", ring_type);
74989e0f4d2SKip Macy 		}
75089e0f4d2SKip Macy 	}
75189e0f4d2SKip Macy 
7527e949c46SKenneth D. Merry 	return error;
75389e0f4d2SKip Macy }
75489e0f4d2SKip Macy 
7557e949c46SKenneth D. Merry /**
7567e949c46SKenneth D. Merry  * Setup the shared memory rings and bind an interrupt to the event channel
7577e949c46SKenneth D. Merry  * used to notify us of ring changes.
7587e949c46SKenneth D. Merry  *
7597e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
7607e949c46SKenneth D. Merry  */
7617e949c46SKenneth D. Merry static int
7627e949c46SKenneth D. Merry xnb_connect_comms(struct xnb_softc *xnb)
7637e949c46SKenneth D. Merry {
7647e949c46SKenneth D. Merry 	int	error;
7657e949c46SKenneth D. Merry 	xnb_ring_type_t i;
7667e949c46SKenneth D. Merry 
7677e949c46SKenneth D. Merry 	if ((xnb->flags & XNBF_RING_CONNECTED) != 0)
7687e949c46SKenneth D. Merry 		return (0);
7697e949c46SKenneth D. Merry 
7707e949c46SKenneth D. Merry 	/*
7717e949c46SKenneth D. Merry 	 * Kva for our rings are at the tail of the region of kva allocated
7727e949c46SKenneth D. Merry 	 * by xnb_alloc_communication_mem().
7737e949c46SKenneth D. Merry 	 */
7747e949c46SKenneth D. Merry 	for (i=0; i < XNB_NUM_RING_TYPES; i++) {
7757e949c46SKenneth D. Merry 		error = xnb_connect_ring(xnb, i);
7767e949c46SKenneth D. Merry 		if (error != 0)
7777e949c46SKenneth D. Merry 	  		return error;
7787e949c46SKenneth D. Merry 	}
7797e949c46SKenneth D. Merry 
7807e949c46SKenneth D. Merry 	xnb->flags |= XNBF_RING_CONNECTED;
7817e949c46SKenneth D. Merry 
78276acc41fSJustin T. Gibbs 	error = xen_intr_bind_remote_port(xnb->dev,
78376acc41fSJustin T. Gibbs 					  xnb->otherend_id,
7847e949c46SKenneth D. Merry 					  xnb->evtchn,
78576acc41fSJustin T. Gibbs 					  /*filter*/NULL,
7867e949c46SKenneth D. Merry 					  xnb_intr, /*arg*/xnb,
7877e949c46SKenneth D. Merry 					  INTR_TYPE_BIO | INTR_MPSAFE,
78876acc41fSJustin T. Gibbs 					  &xnb->xen_intr_handle);
7897e949c46SKenneth D. Merry 	if (error != 0) {
7907e949c46SKenneth D. Merry 		(void)xnb_disconnect(xnb);
7917e949c46SKenneth D. Merry 		xenbus_dev_fatal(xnb->dev, error, "binding event channel");
7927e949c46SKenneth D. Merry 		return (error);
7937e949c46SKenneth D. Merry 	}
7947e949c46SKenneth D. Merry 
7957e949c46SKenneth D. Merry 	DPRINTF("rings connected!\n");
7967e949c46SKenneth D. Merry 
7977e949c46SKenneth D. Merry 	return (0);
7987e949c46SKenneth D. Merry }
7997e949c46SKenneth D. Merry 
8007e949c46SKenneth D. Merry /**
8017e949c46SKenneth D. Merry  * Size KVA and pseudo-physical address allocations based on negotiated
8027e949c46SKenneth D. Merry  * values for the size and number of I/O requests, and the size of our
8037e949c46SKenneth D. Merry  * communication ring.
8047e949c46SKenneth D. Merry  *
8057e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
8067e949c46SKenneth D. Merry  *
8077e949c46SKenneth D. Merry  * These address spaces are used to dynamically map pages in the
8087e949c46SKenneth D. Merry  * front-end's domain into our own.
8097e949c46SKenneth D. Merry  */
8107e949c46SKenneth D. Merry static int
8117e949c46SKenneth D. Merry xnb_alloc_communication_mem(struct xnb_softc *xnb)
8127e949c46SKenneth D. Merry {
8137e949c46SKenneth D. Merry 	xnb_ring_type_t i;
8147e949c46SKenneth D. Merry 
8157e949c46SKenneth D. Merry 	xnb->kva_size = 0;
8167e949c46SKenneth D. Merry 	for (i=0; i < XNB_NUM_RING_TYPES; i++) {
8177e949c46SKenneth D. Merry 		xnb->kva_size += xnb->ring_configs[i].ring_pages * PAGE_SIZE;
8187e949c46SKenneth D. Merry 	}
8197e949c46SKenneth D. Merry #ifndef XENHVM
8205df87b21SJeff Roberson 	xnb->kva = kva_alloc(xnb->kva_size);
8217e949c46SKenneth D. Merry 	if (xnb->kva == 0)
8227e949c46SKenneth D. Merry 		return (ENOMEM);
8237e949c46SKenneth D. Merry 	xnb->gnt_base_addr = xnb->kva;
8247e949c46SKenneth D. Merry #else /* defined XENHVM */
8257e949c46SKenneth D. Merry 	/*
8267e949c46SKenneth D. Merry 	 * Reserve a range of pseudo physical memory that we can map
8277e949c46SKenneth D. Merry 	 * into kva.  These pages will only be backed by machine
8287e949c46SKenneth D. Merry 	 * pages ("real memory") during the lifetime of front-end requests
8297e949c46SKenneth D. Merry 	 * via grant table operations.  We will map the netif tx and rx rings
8307e949c46SKenneth D. Merry 	 * into this space.
8317e949c46SKenneth D. Merry 	 */
8327e949c46SKenneth D. Merry 	xnb->pseudo_phys_res_id = 0;
8337e949c46SKenneth D. Merry 	xnb->pseudo_phys_res = bus_alloc_resource(xnb->dev, SYS_RES_MEMORY,
8347e949c46SKenneth D. Merry 						  &xnb->pseudo_phys_res_id,
8357e949c46SKenneth D. Merry 						  0, ~0, xnb->kva_size,
8367e949c46SKenneth D. Merry 						  RF_ACTIVE);
8377e949c46SKenneth D. Merry 	if (xnb->pseudo_phys_res == NULL) {
8387e949c46SKenneth D. Merry 		xnb->kva = 0;
8397e949c46SKenneth D. Merry 		return (ENOMEM);
8407e949c46SKenneth D. Merry 	}
8417e949c46SKenneth D. Merry 	xnb->kva = (vm_offset_t)rman_get_virtual(xnb->pseudo_phys_res);
8427e949c46SKenneth D. Merry 	xnb->gnt_base_addr = rman_get_start(xnb->pseudo_phys_res);
8437e949c46SKenneth D. Merry #endif /* !defined XENHVM */
8447e949c46SKenneth D. Merry 	return (0);
8457e949c46SKenneth D. Merry }
8467e949c46SKenneth D. Merry 
8477e949c46SKenneth D. Merry /**
8487e949c46SKenneth D. Merry  * Collect information from the XenStore related to our device and its frontend
8497e949c46SKenneth D. Merry  *
8507e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
8517e949c46SKenneth D. Merry  */
8527e949c46SKenneth D. Merry static int
8537e949c46SKenneth D. Merry xnb_collect_xenstore_info(struct xnb_softc *xnb)
8547e949c46SKenneth D. Merry {
8557e949c46SKenneth D. Merry 	/**
8567e949c46SKenneth D. Merry 	 * \todo Linux collects the following info.  We should collect most
8577e949c46SKenneth D. Merry 	 * of this, too:
8587e949c46SKenneth D. Merry 	 * "feature-rx-notify"
8597e949c46SKenneth D. Merry 	 */
8607e949c46SKenneth D. Merry 	const char *otherend_path;
8617e949c46SKenneth D. Merry 	const char *our_path;
8627e949c46SKenneth D. Merry 	int err;
8637e949c46SKenneth D. Merry 	unsigned int rx_copy, bridge_len;
8647e949c46SKenneth D. Merry 	uint8_t no_csum_offload;
8657e949c46SKenneth D. Merry 
8667e949c46SKenneth D. Merry 	otherend_path = xenbus_get_otherend_path(xnb->dev);
8677e949c46SKenneth D. Merry 	our_path = xenbus_get_node(xnb->dev);
8687e949c46SKenneth D. Merry 
8697e949c46SKenneth D. Merry 	/* Collect the critical communication parameters */
8707e949c46SKenneth D. Merry 	err = xs_gather(XST_NIL, otherend_path,
8717e949c46SKenneth D. Merry 	    "tx-ring-ref", "%l" PRIu32,
8727e949c46SKenneth D. Merry 	    	&xnb->ring_configs[XNB_RING_TYPE_TX].ring_ref,
8737e949c46SKenneth D. Merry 	    "rx-ring-ref", "%l" PRIu32,
8747e949c46SKenneth D. Merry 	    	&xnb->ring_configs[XNB_RING_TYPE_RX].ring_ref,
8757e949c46SKenneth D. Merry 	    "event-channel", "%" PRIu32, &xnb->evtchn,
8767e949c46SKenneth D. Merry 	    NULL);
8777e949c46SKenneth D. Merry 	if (err != 0) {
8787e949c46SKenneth D. Merry 		xenbus_dev_fatal(xnb->dev, err,
8797e949c46SKenneth D. Merry 				 "Unable to retrieve ring information from "
8807e949c46SKenneth D. Merry 				 "frontend %s.  Unable to connect.",
8817e949c46SKenneth D. Merry 				 otherend_path);
8827e949c46SKenneth D. Merry 		return (err);
8837e949c46SKenneth D. Merry 	}
8847e949c46SKenneth D. Merry 
8857e949c46SKenneth D. Merry 	/* Collect the handle from xenstore */
8867e949c46SKenneth D. Merry 	err = xs_scanf(XST_NIL, our_path, "handle", NULL, "%li", &xnb->handle);
8877e949c46SKenneth D. Merry 	if (err != 0) {
8887e949c46SKenneth D. Merry 		xenbus_dev_fatal(xnb->dev, err,
8897e949c46SKenneth D. Merry 		    "Error reading handle from frontend %s.  "
8907e949c46SKenneth D. Merry 		    "Unable to connect.", otherend_path);
8917e949c46SKenneth D. Merry 	}
8927e949c46SKenneth D. Merry 
8937e949c46SKenneth D. Merry 	/*
8947e949c46SKenneth D. Merry 	 * Collect the bridgename, if any.  We do not need bridge_len; we just
8957e949c46SKenneth D. Merry 	 * throw it away
8967e949c46SKenneth D. Merry 	 */
8977e949c46SKenneth D. Merry 	err = xs_read(XST_NIL, our_path, "bridge", &bridge_len,
8987e949c46SKenneth D. Merry 		      (void**)&xnb->bridge);
8997e949c46SKenneth D. Merry 	if (err != 0)
9007e949c46SKenneth D. Merry 		xnb->bridge = NULL;
9017e949c46SKenneth D. Merry 
9027e949c46SKenneth D. Merry 	/*
9037e949c46SKenneth D. Merry 	 * Does the frontend request that we use rx copy?  If not, return an
9047e949c46SKenneth D. Merry 	 * error because this driver only supports rx copy.
9057e949c46SKenneth D. Merry 	 */
9067e949c46SKenneth D. Merry 	err = xs_scanf(XST_NIL, otherend_path, "request-rx-copy", NULL,
9077e949c46SKenneth D. Merry 		       "%" PRIu32, &rx_copy);
9087e949c46SKenneth D. Merry 	if (err == ENOENT) {
9097e949c46SKenneth D. Merry 		err = 0;
9107e949c46SKenneth D. Merry 	 	rx_copy = 0;
9117e949c46SKenneth D. Merry 	}
9127e949c46SKenneth D. Merry 	if (err < 0) {
9137e949c46SKenneth D. Merry 		xenbus_dev_fatal(xnb->dev, err, "reading %s/request-rx-copy",
9147e949c46SKenneth D. Merry 				 otherend_path);
9157e949c46SKenneth D. Merry 		return err;
9167e949c46SKenneth D. Merry 	}
9177e949c46SKenneth D. Merry 	/**
9187e949c46SKenneth D. Merry 	 * \todo: figure out the exact meaning of this feature, and when
9197e949c46SKenneth D. Merry 	 * the frontend will set it to true.  It should be set to true
9207e949c46SKenneth D. Merry 	 * at some point
9217e949c46SKenneth D. Merry 	 */
9227e949c46SKenneth D. Merry /*        if (!rx_copy)*/
9237e949c46SKenneth D. Merry /*          return EOPNOTSUPP;*/
9247e949c46SKenneth D. Merry 
9257e949c46SKenneth D. Merry 	/** \todo Collect the rx notify feature */
9267e949c46SKenneth D. Merry 
9277e949c46SKenneth D. Merry 	/*  Collect the feature-sg. */
9287e949c46SKenneth D. Merry 	if (xs_scanf(XST_NIL, otherend_path, "feature-sg", NULL,
9297e949c46SKenneth D. Merry 		     "%hhu", &xnb->can_sg) < 0)
9307e949c46SKenneth D. Merry 		xnb->can_sg = 0;
9317e949c46SKenneth D. Merry 
9327e949c46SKenneth D. Merry 	/* Collect remaining frontend features */
9337e949c46SKenneth D. Merry 	if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4", NULL,
9347e949c46SKenneth D. Merry 		     "%hhu", &xnb->gso) < 0)
9357e949c46SKenneth D. Merry 		xnb->gso = 0;
9367e949c46SKenneth D. Merry 
9377e949c46SKenneth D. Merry 	if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4-prefix", NULL,
9387e949c46SKenneth D. Merry 		     "%hhu", &xnb->gso_prefix) < 0)
9397e949c46SKenneth D. Merry 		xnb->gso_prefix = 0;
9407e949c46SKenneth D. Merry 
9417e949c46SKenneth D. Merry 	if (xs_scanf(XST_NIL, otherend_path, "feature-no-csum-offload", NULL,
9427e949c46SKenneth D. Merry 		     "%hhu", &no_csum_offload) < 0)
9437e949c46SKenneth D. Merry 		no_csum_offload = 0;
9447e949c46SKenneth D. Merry 	xnb->ip_csum = (no_csum_offload == 0);
9457e949c46SKenneth D. Merry 
9467e949c46SKenneth D. Merry 	return (0);
9477e949c46SKenneth D. Merry }
9487e949c46SKenneth D. Merry 
9497e949c46SKenneth D. Merry /**
9507e949c46SKenneth D. Merry  * Supply information about the physical device to the frontend
9517e949c46SKenneth D. Merry  * via XenBus.
9527e949c46SKenneth D. Merry  *
9537e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
9547e949c46SKenneth D. Merry  */
9557e949c46SKenneth D. Merry static int
9567e949c46SKenneth D. Merry xnb_publish_backend_info(struct xnb_softc *xnb)
9577e949c46SKenneth D. Merry {
9587e949c46SKenneth D. Merry 	struct xs_transaction xst;
9597e949c46SKenneth D. Merry 	const char *our_path;
9607e949c46SKenneth D. Merry 	int error;
9617e949c46SKenneth D. Merry 
9627e949c46SKenneth D. Merry 	our_path = xenbus_get_node(xnb->dev);
9637e949c46SKenneth D. Merry 
9647e949c46SKenneth D. Merry 	do {
9657e949c46SKenneth D. Merry 		error = xs_transaction_start(&xst);
9667e949c46SKenneth D. Merry 		if (error != 0) {
9677e949c46SKenneth D. Merry 			xenbus_dev_fatal(xnb->dev, error,
9687e949c46SKenneth D. Merry 					 "Error publishing backend info "
9697e949c46SKenneth D. Merry 					 "(start transaction)");
9707e949c46SKenneth D. Merry 			break;
9717e949c46SKenneth D. Merry 		}
9727e949c46SKenneth D. Merry 
9737e949c46SKenneth D. Merry 		error = xs_printf(xst, our_path, "feature-sg",
9747e949c46SKenneth D. Merry 				  "%d", XNB_SG);
9757e949c46SKenneth D. Merry 		if (error != 0)
9767e949c46SKenneth D. Merry 			break;
9777e949c46SKenneth D. Merry 
9787e949c46SKenneth D. Merry 		error = xs_printf(xst, our_path, "feature-gso-tcpv4",
9797e949c46SKenneth D. Merry 				  "%d", XNB_GSO_TCPV4);
9807e949c46SKenneth D. Merry 		if (error != 0)
9817e949c46SKenneth D. Merry 			break;
9827e949c46SKenneth D. Merry 
9837e949c46SKenneth D. Merry 		error = xs_printf(xst, our_path, "feature-rx-copy",
9847e949c46SKenneth D. Merry 				  "%d", XNB_RX_COPY);
9857e949c46SKenneth D. Merry 		if (error != 0)
9867e949c46SKenneth D. Merry 			break;
9877e949c46SKenneth D. Merry 
9887e949c46SKenneth D. Merry 		error = xs_printf(xst, our_path, "feature-rx-flip",
9897e949c46SKenneth D. Merry 				  "%d", XNB_RX_FLIP);
9907e949c46SKenneth D. Merry 		if (error != 0)
9917e949c46SKenneth D. Merry 			break;
9927e949c46SKenneth D. Merry 
9937e949c46SKenneth D. Merry 		error = xs_transaction_end(xst, 0);
9947e949c46SKenneth D. Merry 		if (error != 0 && error != EAGAIN) {
9957e949c46SKenneth D. Merry 			xenbus_dev_fatal(xnb->dev, error, "ending transaction");
9967e949c46SKenneth D. Merry 			break;
9977e949c46SKenneth D. Merry 		}
9987e949c46SKenneth D. Merry 
9997e949c46SKenneth D. Merry 	} while (error == EAGAIN);
10007e949c46SKenneth D. Merry 
10017e949c46SKenneth D. Merry 	return (error);
10027e949c46SKenneth D. Merry }
10037e949c46SKenneth D. Merry 
10047e949c46SKenneth D. Merry /**
10057e949c46SKenneth D. Merry  * Connect to our netfront peer now that it has completed publishing
10067e949c46SKenneth D. Merry  * its configuration into the XenStore.
10077e949c46SKenneth D. Merry  *
10087e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
10097e949c46SKenneth D. Merry  */
101089e0f4d2SKip Macy static void
10117e949c46SKenneth D. Merry xnb_connect(struct xnb_softc *xnb)
101289e0f4d2SKip Macy {
10137e949c46SKenneth D. Merry 	int	error;
101489e0f4d2SKip Macy 
10157e949c46SKenneth D. Merry 	if (xenbus_get_state(xnb->dev) == XenbusStateConnected)
101689e0f4d2SKip Macy 		return;
101789e0f4d2SKip Macy 
10187e949c46SKenneth D. Merry 	if (xnb_collect_xenstore_info(xnb) != 0)
10197e949c46SKenneth D. Merry 		return;
10207e949c46SKenneth D. Merry 
10217e949c46SKenneth D. Merry 	xnb->flags &= ~XNBF_SHUTDOWN;
10227e949c46SKenneth D. Merry 
10237e949c46SKenneth D. Merry 	/* Read front end configuration. */
10247e949c46SKenneth D. Merry 
10257e949c46SKenneth D. Merry 	/* Allocate resources whose size depends on front-end configuration. */
10267e949c46SKenneth D. Merry 	error = xnb_alloc_communication_mem(xnb);
10277e949c46SKenneth D. Merry 	if (error != 0) {
10287e949c46SKenneth D. Merry 		xenbus_dev_fatal(xnb->dev, error,
10297e949c46SKenneth D. Merry 				 "Unable to allocate communication memory");
10307e949c46SKenneth D. Merry 		return;
103189e0f4d2SKip Macy 	}
103289e0f4d2SKip Macy 
103389e0f4d2SKip Macy 	/*
10347e949c46SKenneth D. Merry 	 * Connect communication channel.
103589e0f4d2SKip Macy 	 */
10367e949c46SKenneth D. Merry 	error = xnb_connect_comms(xnb);
10377e949c46SKenneth D. Merry 	if (error != 0) {
10387e949c46SKenneth D. Merry 		/* Specific errors are reported by xnb_connect_comms(). */
10397e949c46SKenneth D. Merry 		return;
104089e0f4d2SKip Macy 	}
10417e949c46SKenneth D. Merry 	xnb->carrier = 1;
104289e0f4d2SKip Macy 
10437e949c46SKenneth D. Merry 	/* Ready for I/O. */
10447e949c46SKenneth D. Merry 	xenbus_set_state(xnb->dev, XenbusStateConnected);
104589e0f4d2SKip Macy }
104689e0f4d2SKip Macy 
10477e949c46SKenneth D. Merry /*-------------------------- Device Teardown Support -------------------------*/
10487e949c46SKenneth D. Merry /**
10497e949c46SKenneth D. Merry  * Perform device shutdown functions.
10507e949c46SKenneth D. Merry  *
10517e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
10527e949c46SKenneth D. Merry  *
10537e949c46SKenneth D. Merry  * Mark this instance as shutting down, wait for any active requests
10547e949c46SKenneth D. Merry  * to drain, disconnect from the front-end, and notify any waiters (e.g.
10557e949c46SKenneth D. Merry  * a thread invoking our detach method) that detach can now proceed.
10567e949c46SKenneth D. Merry  */
105789e0f4d2SKip Macy static int
10587e949c46SKenneth D. Merry xnb_shutdown(struct xnb_softc *xnb)
105989e0f4d2SKip Macy {
10607e949c46SKenneth D. Merry 	/*
10617e949c46SKenneth D. Merry 	 * Due to the need to drop our mutex during some
10627e949c46SKenneth D. Merry 	 * xenbus operations, it is possible for two threads
10637e949c46SKenneth D. Merry 	 * to attempt to close out shutdown processing at
10647e949c46SKenneth D. Merry 	 * the same time.  Tell the caller that hits this
10657e949c46SKenneth D. Merry 	 * race to try back later.
10667e949c46SKenneth D. Merry 	 */
10677e949c46SKenneth D. Merry 	if ((xnb->flags & XNBF_IN_SHUTDOWN) != 0)
10687e949c46SKenneth D. Merry 		return (EAGAIN);
106989e0f4d2SKip Macy 
10707e949c46SKenneth D. Merry 	xnb->flags |= XNBF_SHUTDOWN;
107189e0f4d2SKip Macy 
10727e949c46SKenneth D. Merry 	xnb->flags |= XNBF_IN_SHUTDOWN;
10737e949c46SKenneth D. Merry 
10747e949c46SKenneth D. Merry 	mtx_unlock(&xnb->sc_lock);
10757e949c46SKenneth D. Merry 	/* Free the network interface */
10767e949c46SKenneth D. Merry 	xnb->carrier = 0;
10777e949c46SKenneth D. Merry 	if (xnb->xnb_ifp != NULL) {
10787e949c46SKenneth D. Merry 		ether_ifdetach(xnb->xnb_ifp);
10797e949c46SKenneth D. Merry 		if_free(xnb->xnb_ifp);
10807e949c46SKenneth D. Merry 		xnb->xnb_ifp = NULL;
10817e949c46SKenneth D. Merry 	}
10827e949c46SKenneth D. Merry 	mtx_lock(&xnb->sc_lock);
10837e949c46SKenneth D. Merry 
10847e949c46SKenneth D. Merry 	xnb_disconnect(xnb);
10857e949c46SKenneth D. Merry 
10867e949c46SKenneth D. Merry 	mtx_unlock(&xnb->sc_lock);
10877e949c46SKenneth D. Merry 	if (xenbus_get_state(xnb->dev) < XenbusStateClosing)
10887e949c46SKenneth D. Merry 		xenbus_set_state(xnb->dev, XenbusStateClosing);
10897e949c46SKenneth D. Merry 	mtx_lock(&xnb->sc_lock);
10907e949c46SKenneth D. Merry 
10917e949c46SKenneth D. Merry 	xnb->flags &= ~XNBF_IN_SHUTDOWN;
10927e949c46SKenneth D. Merry 
10937e949c46SKenneth D. Merry 
10947e949c46SKenneth D. Merry 	/* Indicate to xnb_detach() that is it safe to proceed. */
10957e949c46SKenneth D. Merry 	wakeup(xnb);
10967e949c46SKenneth D. Merry 
10977e949c46SKenneth D. Merry 	return (0);
109889e0f4d2SKip Macy }
109989e0f4d2SKip Macy 
11007e949c46SKenneth D. Merry /**
11017e949c46SKenneth D. Merry  * Report an attach time error to the console and Xen, and cleanup
11027e949c46SKenneth D. Merry  * this instance by forcing immediate detach processing.
11037e949c46SKenneth D. Merry  *
11047e949c46SKenneth D. Merry  * \param xnb  Per-instance xnb configuration structure.
11057e949c46SKenneth D. Merry  * \param err  Errno describing the error.
11067e949c46SKenneth D. Merry  * \param fmt  Printf style format and arguments
11077e949c46SKenneth D. Merry  */
110889e0f4d2SKip Macy static void
11097e949c46SKenneth D. Merry xnb_attach_failed(struct xnb_softc *xnb, int err, const char *fmt, ...)
111089e0f4d2SKip Macy {
11117e949c46SKenneth D. Merry 	va_list ap;
11127e949c46SKenneth D. Merry 	va_list ap_hotplug;
111389e0f4d2SKip Macy 
11147e949c46SKenneth D. Merry 	va_start(ap, fmt);
11157e949c46SKenneth D. Merry 	va_copy(ap_hotplug, ap);
11167e949c46SKenneth D. Merry 	xs_vprintf(XST_NIL, xenbus_get_node(xnb->dev),
11177e949c46SKenneth D. Merry 		  "hotplug-error", fmt, ap_hotplug);
11187e949c46SKenneth D. Merry 	va_end(ap_hotplug);
11197e949c46SKenneth D. Merry 	xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
11207e949c46SKenneth D. Merry 		  "hotplug-status", "error");
112189e0f4d2SKip Macy 
11227e949c46SKenneth D. Merry 	xenbus_dev_vfatal(xnb->dev, err, fmt, ap);
11237e949c46SKenneth D. Merry 	va_end(ap);
11247e949c46SKenneth D. Merry 
11257e949c46SKenneth D. Merry 	xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
11267e949c46SKenneth D. Merry 		  "online", "0");
11277e949c46SKenneth D. Merry 	xnb_detach(xnb->dev);
112889e0f4d2SKip Macy }
112989e0f4d2SKip Macy 
11307e949c46SKenneth D. Merry /*---------------------------- NewBus Entrypoints ----------------------------*/
11317e949c46SKenneth D. Merry /**
11327e949c46SKenneth D. Merry  * Inspect a XenBus device and claim it if is of the appropriate type.
11337e949c46SKenneth D. Merry  *
11347e949c46SKenneth D. Merry  * \param dev  NewBus device object representing a candidate XenBus device.
11357e949c46SKenneth D. Merry  *
11367e949c46SKenneth D. Merry  * \return  0 for success, errno codes for failure.
11377e949c46SKenneth D. Merry  */
113889e0f4d2SKip Macy static int
11397e949c46SKenneth D. Merry xnb_probe(device_t dev)
114089e0f4d2SKip Macy {
11417e949c46SKenneth D. Merry 	 if (!strcmp(xenbus_get_type(dev), "vif")) {
11427e949c46SKenneth D. Merry 		DPRINTF("Claiming device %d, %s\n", device_get_unit(dev),
11437e949c46SKenneth D. Merry 		    devclass_get_name(device_get_devclass(dev)));
11447e949c46SKenneth D. Merry 		device_set_desc(dev, "Backend Virtual Network Device");
11457e949c46SKenneth D. Merry 		device_quiet(dev);
11467e949c46SKenneth D. Merry 		return (0);
11477e949c46SKenneth D. Merry 	}
11487e949c46SKenneth D. Merry 	return (ENXIO);
114989e0f4d2SKip Macy }
115089e0f4d2SKip Macy 
11517e949c46SKenneth D. Merry /**
11527e949c46SKenneth D. Merry  * Setup sysctl variables to control various Network Back parameters.
11537e949c46SKenneth D. Merry  *
11547e949c46SKenneth D. Merry  * \param xnb  Xen Net Back softc.
11557e949c46SKenneth D. Merry  *
11567e949c46SKenneth D. Merry  */
115789e0f4d2SKip Macy static void
11587e949c46SKenneth D. Merry xnb_setup_sysctl(struct xnb_softc *xnb)
115989e0f4d2SKip Macy {
11607e949c46SKenneth D. Merry 	struct sysctl_ctx_list *sysctl_ctx = NULL;
11617e949c46SKenneth D. Merry 	struct sysctl_oid      *sysctl_tree = NULL;
11627e949c46SKenneth D. Merry 
11637e949c46SKenneth D. Merry 	sysctl_ctx = device_get_sysctl_ctx(xnb->dev);
11647e949c46SKenneth D. Merry 	if (sysctl_ctx == NULL)
11657e949c46SKenneth D. Merry 		return;
11667e949c46SKenneth D. Merry 
11677e949c46SKenneth D. Merry 	sysctl_tree = device_get_sysctl_tree(xnb->dev);
11687e949c46SKenneth D. Merry 	if (sysctl_tree == NULL)
11697e949c46SKenneth D. Merry 		return;
11707e949c46SKenneth D. Merry 
11717e949c46SKenneth D. Merry #ifdef XNB_DEBUG
11727e949c46SKenneth D. Merry 	SYSCTL_ADD_PROC(sysctl_ctx,
11737e949c46SKenneth D. Merry 			SYSCTL_CHILDREN(sysctl_tree),
11747e949c46SKenneth D. Merry 			OID_AUTO,
11757e949c46SKenneth D. Merry 			"unit_test_results",
11767e949c46SKenneth D. Merry 			CTLTYPE_STRING | CTLFLAG_RD,
11777e949c46SKenneth D. Merry 			xnb,
11787e949c46SKenneth D. Merry 			0,
11797e949c46SKenneth D. Merry 			xnb_unit_test_main,
11807e949c46SKenneth D. Merry 			"A",
11817e949c46SKenneth D. Merry 			"Results of builtin unit tests");
11827e949c46SKenneth D. Merry 
11837e949c46SKenneth D. Merry 	SYSCTL_ADD_PROC(sysctl_ctx,
11847e949c46SKenneth D. Merry 			SYSCTL_CHILDREN(sysctl_tree),
11857e949c46SKenneth D. Merry 			OID_AUTO,
11867e949c46SKenneth D. Merry 			"dump_rings",
11877e949c46SKenneth D. Merry 			CTLTYPE_STRING | CTLFLAG_RD,
11887e949c46SKenneth D. Merry 			xnb,
11897e949c46SKenneth D. Merry 			0,
11907e949c46SKenneth D. Merry 			xnb_dump_rings,
11917e949c46SKenneth D. Merry 			"A",
11927e949c46SKenneth D. Merry 			"Xennet Back Rings");
11937e949c46SKenneth D. Merry #endif /* XNB_DEBUG */
11947e949c46SKenneth D. Merry }
11957e949c46SKenneth D. Merry 
11967e949c46SKenneth D. Merry /**
11977e949c46SKenneth D. Merry  * Create a network device.
11987e949c46SKenneth D. Merry  * @param handle device handle
11997e949c46SKenneth D. Merry  */
12007e949c46SKenneth D. Merry int
12017e949c46SKenneth D. Merry create_netdev(device_t dev)
12027e949c46SKenneth D. Merry {
12037e949c46SKenneth D. Merry 	struct ifnet *ifp;
12047e949c46SKenneth D. Merry 	struct xnb_softc *xnb;
12057e949c46SKenneth D. Merry 	int err = 0;
1206ddcc16cfSRoger Pau Monné 	uint32_t handle;
12077e949c46SKenneth D. Merry 
12087e949c46SKenneth D. Merry 	xnb = device_get_softc(dev);
12097e949c46SKenneth D. Merry 	mtx_init(&xnb->sc_lock, "xnb_softc", "xen netback softc lock", MTX_DEF);
12107e949c46SKenneth D. Merry 	mtx_init(&xnb->tx_lock, "xnb_tx", "xen netback tx lock", MTX_DEF);
12117e949c46SKenneth D. Merry 	mtx_init(&xnb->rx_lock, "xnb_rx", "xen netback rx lock", MTX_DEF);
12127e949c46SKenneth D. Merry 
12137e949c46SKenneth D. Merry 	xnb->dev = dev;
12147e949c46SKenneth D. Merry 
12157e949c46SKenneth D. Merry 	ifmedia_init(&xnb->sc_media, 0, xnb_ifmedia_upd, xnb_ifmedia_sts);
12167e949c46SKenneth D. Merry 	ifmedia_add(&xnb->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
12177e949c46SKenneth D. Merry 	ifmedia_set(&xnb->sc_media, IFM_ETHER|IFM_MANUAL);
12187e949c46SKenneth D. Merry 
12191b386f62SRoger Pau Monné 	/*
12201b386f62SRoger Pau Monné 	 * Set the MAC address to a dummy value (00:00:00:00:00),
12211b386f62SRoger Pau Monné 	 * if the MAC address of the host-facing interface is set
12221b386f62SRoger Pau Monné 	 * to the same as the guest-facing one (the value found in
12231b386f62SRoger Pau Monné 	 * xenstore), the bridge would stop delivering packets to
12241b386f62SRoger Pau Monné 	 * us because it would see that the destination address of
12251b386f62SRoger Pau Monné 	 * the packet is the same as the interface, and so the bridge
12261b386f62SRoger Pau Monné 	 * would expect the packet has already been delivered locally
12271b386f62SRoger Pau Monné 	 * (and just drop it).
12281b386f62SRoger Pau Monné 	 */
12291b386f62SRoger Pau Monné 	bzero(&xnb->mac[0], sizeof(xnb->mac));
12301b386f62SRoger Pau Monné 
1231ddcc16cfSRoger Pau Monné 	/* The interface will be named using the following nomenclature:
1232ddcc16cfSRoger Pau Monné 	 *
1233ddcc16cfSRoger Pau Monné 	 * xnb<domid>.<handle>
1234ddcc16cfSRoger Pau Monné 	 *
1235ddcc16cfSRoger Pau Monné 	 * Where handle is the oder of the interface referred to the guest.
1236ddcc16cfSRoger Pau Monné 	 */
1237ddcc16cfSRoger Pau Monné 	err = xs_scanf(XST_NIL, xenbus_get_node(xnb->dev), "handle", NULL,
1238ddcc16cfSRoger Pau Monné 		       "%" PRIu32, &handle);
1239ddcc16cfSRoger Pau Monné 	if (err != 0)
1240ddcc16cfSRoger Pau Monné 		return (err);
1241ddcc16cfSRoger Pau Monné 	snprintf(xnb->if_name, IFNAMSIZ, "xnb%" PRIu16 ".%" PRIu32,
1242ddcc16cfSRoger Pau Monné 	    xenbus_get_otherend_id(dev), handle);
1243ddcc16cfSRoger Pau Monné 
12447e949c46SKenneth D. Merry 	if (err == 0) {
12457e949c46SKenneth D. Merry 		/* Set up ifnet structure */
12467e949c46SKenneth D. Merry 		ifp = xnb->xnb_ifp = if_alloc(IFT_ETHER);
12477e949c46SKenneth D. Merry 		ifp->if_softc = xnb;
1248ddcc16cfSRoger Pau Monné 		if_initname(ifp, xnb->if_name,  IF_DUNIT_NONE);
12497e949c46SKenneth D. Merry 		ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
12507e949c46SKenneth D. Merry 		ifp->if_ioctl = xnb_ioctl;
12517e949c46SKenneth D. Merry 		ifp->if_output = ether_output;
12527e949c46SKenneth D. Merry 		ifp->if_start = xnb_start;
12537e949c46SKenneth D. Merry #ifdef notyet
12547e949c46SKenneth D. Merry 		ifp->if_watchdog = xnb_watchdog;
12557e949c46SKenneth D. Merry #endif
12567e949c46SKenneth D. Merry 		ifp->if_init = xnb_ifinit;
12577e949c46SKenneth D. Merry 		ifp->if_mtu = ETHERMTU;
12587e949c46SKenneth D. Merry 		ifp->if_snd.ifq_maxlen = NET_RX_RING_SIZE - 1;
12597e949c46SKenneth D. Merry 
12607e949c46SKenneth D. Merry 		ifp->if_hwassist = XNB_CSUM_FEATURES;
12617e949c46SKenneth D. Merry 		ifp->if_capabilities = IFCAP_HWCSUM;
12627e949c46SKenneth D. Merry 		ifp->if_capenable = IFCAP_HWCSUM;
12637e949c46SKenneth D. Merry 
12647e949c46SKenneth D. Merry 		ether_ifattach(ifp, xnb->mac);
12657e949c46SKenneth D. Merry 		xnb->carrier = 0;
12667e949c46SKenneth D. Merry 	}
12677e949c46SKenneth D. Merry 
12687e949c46SKenneth D. Merry 	return err;
12697e949c46SKenneth D. Merry }
12707e949c46SKenneth D. Merry 
12717e949c46SKenneth D. Merry /**
12727e949c46SKenneth D. Merry  * Attach to a XenBus device that has been claimed by our probe routine.
12737e949c46SKenneth D. Merry  *
12747e949c46SKenneth D. Merry  * \param dev  NewBus device object representing this Xen Net Back instance.
12757e949c46SKenneth D. Merry  *
12767e949c46SKenneth D. Merry  * \return  0 for success, errno codes for failure.
12777e949c46SKenneth D. Merry  */
12787e949c46SKenneth D. Merry static int
12797e949c46SKenneth D. Merry xnb_attach(device_t dev)
12807e949c46SKenneth D. Merry {
12817e949c46SKenneth D. Merry 	struct xnb_softc *xnb;
12827e949c46SKenneth D. Merry 	int	error;
12837e949c46SKenneth D. Merry 	xnb_ring_type_t	i;
12847e949c46SKenneth D. Merry 
12857e949c46SKenneth D. Merry 	error = create_netdev(dev);
12867e949c46SKenneth D. Merry 	if (error != 0) {
12877e949c46SKenneth D. Merry 		xenbus_dev_fatal(dev, error, "creating netdev");
12887e949c46SKenneth D. Merry 		return (error);
12897e949c46SKenneth D. Merry 	}
12907e949c46SKenneth D. Merry 
12917e949c46SKenneth D. Merry 	DPRINTF("Attaching to %s\n", xenbus_get_node(dev));
12927e949c46SKenneth D. Merry 
12937e949c46SKenneth D. Merry 	/*
12947e949c46SKenneth D. Merry 	 * Basic initialization.
12957e949c46SKenneth D. Merry 	 * After this block it is safe to call xnb_detach()
12967e949c46SKenneth D. Merry 	 * to clean up any allocated data for this instance.
12977e949c46SKenneth D. Merry 	 */
12987e949c46SKenneth D. Merry 	xnb = device_get_softc(dev);
12997e949c46SKenneth D. Merry 	xnb->otherend_id = xenbus_get_otherend_id(dev);
13007e949c46SKenneth D. Merry 	for (i=0; i < XNB_NUM_RING_TYPES; i++) {
13017e949c46SKenneth D. Merry 		xnb->ring_configs[i].ring_pages = 1;
13027e949c46SKenneth D. Merry 	}
13037e949c46SKenneth D. Merry 
13047e949c46SKenneth D. Merry 	/*
13057e949c46SKenneth D. Merry 	 * Setup sysctl variables.
13067e949c46SKenneth D. Merry 	 */
13077e949c46SKenneth D. Merry 	xnb_setup_sysctl(xnb);
13087e949c46SKenneth D. Merry 
13097e949c46SKenneth D. Merry 	/* Update hot-plug status to satisfy xend. */
13107e949c46SKenneth D. Merry 	error = xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
13117e949c46SKenneth D. Merry 			  "hotplug-status", "connected");
13127e949c46SKenneth D. Merry 	if (error != 0) {
13137e949c46SKenneth D. Merry 		xnb_attach_failed(xnb, error, "writing %s/hotplug-status",
13147e949c46SKenneth D. Merry 				  xenbus_get_node(xnb->dev));
13157e949c46SKenneth D. Merry 		return (error);
13167e949c46SKenneth D. Merry 	}
13177e949c46SKenneth D. Merry 
13187e949c46SKenneth D. Merry 	if ((error = xnb_publish_backend_info(xnb)) != 0) {
13197e949c46SKenneth D. Merry 		/*
13207e949c46SKenneth D. Merry 		 * If we can't publish our data, we cannot participate
13217e949c46SKenneth D. Merry 		 * in this connection, and waiting for a front-end state
13227e949c46SKenneth D. Merry 		 * change will not help the situation.
13237e949c46SKenneth D. Merry 		 */
13247e949c46SKenneth D. Merry 		xnb_attach_failed(xnb, error,
13257e949c46SKenneth D. Merry 		    "Publishing backend status for %s",
13267e949c46SKenneth D. Merry 				  xenbus_get_node(xnb->dev));
13277e949c46SKenneth D. Merry 		return error;
13287e949c46SKenneth D. Merry 	}
13297e949c46SKenneth D. Merry 
13307e949c46SKenneth D. Merry 	/* Tell the front end that we are ready to connect. */
13317e949c46SKenneth D. Merry 	xenbus_set_state(dev, XenbusStateInitWait);
13327e949c46SKenneth D. Merry 
13337e949c46SKenneth D. Merry 	return (0);
13347e949c46SKenneth D. Merry }
13357e949c46SKenneth D. Merry 
13367e949c46SKenneth D. Merry /**
13377e949c46SKenneth D. Merry  * Detach from a net back device instance.
13387e949c46SKenneth D. Merry  *
13397e949c46SKenneth D. Merry  * \param dev  NewBus device object representing this Xen Net Back instance.
13407e949c46SKenneth D. Merry  *
13417e949c46SKenneth D. Merry  * \return  0 for success, errno codes for failure.
13427e949c46SKenneth D. Merry  *
13437e949c46SKenneth D. Merry  * \note A net back device may be detached at any time in its life-cycle,
13447e949c46SKenneth D. Merry  *       including part way through the attach process.  For this reason,
13457e949c46SKenneth D. Merry  *       initialization order and the intialization state checks in this
13467e949c46SKenneth D. Merry  *       routine must be carefully coupled so that attach time failures
13477e949c46SKenneth D. Merry  *       are gracefully handled.
13487e949c46SKenneth D. Merry  */
13497e949c46SKenneth D. Merry static int
13507e949c46SKenneth D. Merry xnb_detach(device_t dev)
13517e949c46SKenneth D. Merry {
13527e949c46SKenneth D. Merry 	struct xnb_softc *xnb;
13537e949c46SKenneth D. Merry 
135489e0f4d2SKip Macy 	DPRINTF("\n");
135589e0f4d2SKip Macy 
13567e949c46SKenneth D. Merry 	xnb = device_get_softc(dev);
13577e949c46SKenneth D. Merry 	mtx_lock(&xnb->sc_lock);
13587e949c46SKenneth D. Merry 	while (xnb_shutdown(xnb) == EAGAIN) {
13597e949c46SKenneth D. Merry 		msleep(xnb, &xnb->sc_lock, /*wakeup prio unchanged*/0,
13607e949c46SKenneth D. Merry 		       "xnb_shutdown", 0);
136189e0f4d2SKip Macy 	}
13627e949c46SKenneth D. Merry 	mtx_unlock(&xnb->sc_lock);
13637e949c46SKenneth D. Merry 	DPRINTF("\n");
13647e949c46SKenneth D. Merry 
13657e949c46SKenneth D. Merry 	mtx_destroy(&xnb->tx_lock);
13667e949c46SKenneth D. Merry 	mtx_destroy(&xnb->rx_lock);
13677e949c46SKenneth D. Merry 	mtx_destroy(&xnb->sc_lock);
13687e949c46SKenneth D. Merry 	return (0);
136989e0f4d2SKip Macy }
137089e0f4d2SKip Macy 
13717e949c46SKenneth D. Merry /**
13727e949c46SKenneth D. Merry  * Prepare this net back device for suspension of this VM.
13737e949c46SKenneth D. Merry  *
13747e949c46SKenneth D. Merry  * \param dev  NewBus device object representing this Xen net Back instance.
13757e949c46SKenneth D. Merry  *
13767e949c46SKenneth D. Merry  * \return  0 for success, errno codes for failure.
13777e949c46SKenneth D. Merry  */
13787e949c46SKenneth D. Merry static int
13797e949c46SKenneth D. Merry xnb_suspend(device_t dev)
13807e949c46SKenneth D. Merry {
13817e949c46SKenneth D. Merry 	return (0);
13827e949c46SKenneth D. Merry }
13837e949c46SKenneth D. Merry 
13847e949c46SKenneth D. Merry /**
13857e949c46SKenneth D. Merry  * Perform any processing required to recover from a suspended state.
13867e949c46SKenneth D. Merry  *
13877e949c46SKenneth D. Merry  * \param dev  NewBus device object representing this Xen Net Back instance.
13887e949c46SKenneth D. Merry  *
13897e949c46SKenneth D. Merry  * \return  0 for success, errno codes for failure.
13907e949c46SKenneth D. Merry  */
13917e949c46SKenneth D. Merry static int
13927e949c46SKenneth D. Merry xnb_resume(device_t dev)
13937e949c46SKenneth D. Merry {
13947e949c46SKenneth D. Merry 	return (0);
13957e949c46SKenneth D. Merry }
13967e949c46SKenneth D. Merry 
13977e949c46SKenneth D. Merry /**
13987e949c46SKenneth D. Merry  * Handle state changes expressed via the XenStore by our front-end peer.
13997e949c46SKenneth D. Merry  *
14007e949c46SKenneth D. Merry  * \param dev             NewBus device object representing this Xen
14017e949c46SKenneth D. Merry  *                        Net Back instance.
14027e949c46SKenneth D. Merry  * \param frontend_state  The new state of the front-end.
14037e949c46SKenneth D. Merry  *
14047e949c46SKenneth D. Merry  * \return  0 for success, errno codes for failure.
14057e949c46SKenneth D. Merry  */
140689e0f4d2SKip Macy static void
14077e949c46SKenneth D. Merry xnb_frontend_changed(device_t dev, XenbusState frontend_state)
140889e0f4d2SKip Macy {
14097e949c46SKenneth D. Merry 	struct xnb_softc *xnb;
141089e0f4d2SKip Macy 
14117e949c46SKenneth D. Merry 	xnb = device_get_softc(dev);
141289e0f4d2SKip Macy 
14137e949c46SKenneth D. Merry 	DPRINTF("frontend_state=%s, xnb_state=%s\n",
14147e949c46SKenneth D. Merry 	        xenbus_strstate(frontend_state),
14157e949c46SKenneth D. Merry 		xenbus_strstate(xenbus_get_state(xnb->dev)));
141689e0f4d2SKip Macy 
141789e0f4d2SKip Macy 	switch (frontend_state) {
141889e0f4d2SKip Macy 	case XenbusStateInitialising:
141989e0f4d2SKip Macy 		break;
14207e949c46SKenneth D. Merry 	case XenbusStateInitialised:
142189e0f4d2SKip Macy 	case XenbusStateConnected:
14227e949c46SKenneth D. Merry 		xnb_connect(xnb);
142389e0f4d2SKip Macy 		break;
142489e0f4d2SKip Macy 	case XenbusStateClosing:
142589e0f4d2SKip Macy 	case XenbusStateClosed:
14267e949c46SKenneth D. Merry 		mtx_lock(&xnb->sc_lock);
14277e949c46SKenneth D. Merry 		xnb_shutdown(xnb);
14287e949c46SKenneth D. Merry 		mtx_unlock(&xnb->sc_lock);
14297e949c46SKenneth D. Merry 		if (frontend_state == XenbusStateClosed)
14307e949c46SKenneth D. Merry 			xenbus_set_state(xnb->dev, XenbusStateClosed);
143189e0f4d2SKip Macy 		break;
14327e949c46SKenneth D. Merry 	default:
14337e949c46SKenneth D. Merry 		xenbus_dev_fatal(xnb->dev, EINVAL, "saw state %d at frontend",
143489e0f4d2SKip Macy 				 frontend_state);
143589e0f4d2SKip Macy 		break;
143689e0f4d2SKip Macy 	}
143789e0f4d2SKip Macy }
143889e0f4d2SKip Macy 
143989e0f4d2SKip Macy 
14407e949c46SKenneth D. Merry /*---------------------------- Request Processing ----------------------------*/
14417e949c46SKenneth D. Merry /**
14427e949c46SKenneth D. Merry  * Interrupt handler bound to the shared ring's event channel.
14437e949c46SKenneth D. Merry  * Entry point for the xennet transmit path in netback
14447e949c46SKenneth D. Merry  * Transfers packets from the Xen ring to the host's generic networking stack
14457e949c46SKenneth D. Merry  *
14467e949c46SKenneth D. Merry  * \param arg  Callback argument registerd during event channel
14477e949c46SKenneth D. Merry  *             binding - the xnb_softc for this instance.
14487e949c46SKenneth D. Merry  */
144989e0f4d2SKip Macy static void
14507e949c46SKenneth D. Merry xnb_intr(void *arg)
145189e0f4d2SKip Macy {
14527e949c46SKenneth D. Merry 	struct xnb_softc *xnb;
14537e949c46SKenneth D. Merry 	struct ifnet *ifp;
14547e949c46SKenneth D. Merry 	netif_tx_back_ring_t *txb;
14557e949c46SKenneth D. Merry 	RING_IDX req_prod_local;
145689e0f4d2SKip Macy 
14577e949c46SKenneth D. Merry 	xnb = (struct xnb_softc *)arg;
14587e949c46SKenneth D. Merry 	ifp = xnb->xnb_ifp;
14597e949c46SKenneth D. Merry 	txb = &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring;
146089e0f4d2SKip Macy 
14617e949c46SKenneth D. Merry 	mtx_lock(&xnb->tx_lock);
14627e949c46SKenneth D. Merry 	do {
14637e949c46SKenneth D. Merry 		int notify;
14647e949c46SKenneth D. Merry 		req_prod_local = txb->sring->req_prod;
14657e949c46SKenneth D. Merry 		xen_rmb();
146689e0f4d2SKip Macy 
14677e949c46SKenneth D. Merry 		for (;;) {
14687e949c46SKenneth D. Merry 			struct mbuf *mbufc;
146989e0f4d2SKip Macy 			int err;
147089e0f4d2SKip Macy 
14717e949c46SKenneth D. Merry 			err = xnb_recv(txb, xnb->otherend_id, &mbufc, ifp,
14727e949c46SKenneth D. Merry 			    	       xnb->tx_gnttab);
14737e949c46SKenneth D. Merry 			if (err || (mbufc == NULL))
14747e949c46SKenneth D. Merry 				break;
14757e949c46SKenneth D. Merry 
14767e949c46SKenneth D. Merry 			/* Send the packet to the generic network stack */
14777e949c46SKenneth D. Merry 			(*xnb->xnb_ifp->if_input)(xnb->xnb_ifp, mbufc);
14787e949c46SKenneth D. Merry 		}
14797e949c46SKenneth D. Merry 
14807e949c46SKenneth D. Merry 		RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(txb, notify);
14817e949c46SKenneth D. Merry 		if (notify != 0)
148276acc41fSJustin T. Gibbs 			xen_intr_signal(xnb->xen_intr_handle);
14837e949c46SKenneth D. Merry 
14847e949c46SKenneth D. Merry 		txb->sring->req_event = txb->req_cons + 1;
14857e949c46SKenneth D. Merry 		xen_mb();
14867e949c46SKenneth D. Merry 	} while (txb->sring->req_prod != req_prod_local) ;
14877e949c46SKenneth D. Merry 	mtx_unlock(&xnb->tx_lock);
14887e949c46SKenneth D. Merry 
14897e949c46SKenneth D. Merry 	xnb_start(ifp);
14907e949c46SKenneth D. Merry }
14917e949c46SKenneth D. Merry 
14927e949c46SKenneth D. Merry 
14937e949c46SKenneth D. Merry /**
14947e949c46SKenneth D. Merry  * Build a struct xnb_pkt based on netif_tx_request's from a netif tx ring.
14957e949c46SKenneth D. Merry  * Will read exactly 0 or 1 packets from the ring; never a partial packet.
14967e949c46SKenneth D. Merry  * \param[out]	pkt	The returned packet.  If there is an error building
14977e949c46SKenneth D. Merry  * 			the packet, pkt.list_len will be set to 0.
14987e949c46SKenneth D. Merry  * \param[in]	tx_ring	Pointer to the Ring that is the input to this function
14997e949c46SKenneth D. Merry  * \param[in]	start	The ring index of the first potential request
15007e949c46SKenneth D. Merry  * \return		The number of requests consumed to build this packet
15017e949c46SKenneth D. Merry  */
15027e949c46SKenneth D. Merry static int
15037e949c46SKenneth D. Merry xnb_ring2pkt(struct xnb_pkt *pkt, const netif_tx_back_ring_t *tx_ring,
15047e949c46SKenneth D. Merry 	     RING_IDX start)
15057e949c46SKenneth D. Merry {
15067e949c46SKenneth D. Merry 	/*
15077e949c46SKenneth D. Merry 	 * Outline:
15087e949c46SKenneth D. Merry 	 * 1) Initialize pkt
15097e949c46SKenneth D. Merry 	 * 2) Read the first request of the packet
15107e949c46SKenneth D. Merry 	 * 3) Read the extras
15117e949c46SKenneth D. Merry 	 * 4) Set cdr
15127e949c46SKenneth D. Merry 	 * 5) Loop on the remainder of the packet
15137e949c46SKenneth D. Merry 	 * 6) Finalize pkt (stuff like car_size and list_len)
15147e949c46SKenneth D. Merry 	 */
15157e949c46SKenneth D. Merry 	int idx = start;
15167e949c46SKenneth D. Merry 	int discard = 0;	/* whether to discard the packet */
15177e949c46SKenneth D. Merry 	int more_data = 0;	/* there are more request past the last one */
15187e949c46SKenneth D. Merry 	uint16_t cdr_size = 0;	/* accumulated size of requests 2 through n */
15197e949c46SKenneth D. Merry 
15207e949c46SKenneth D. Merry 	xnb_pkt_initialize(pkt);
15217e949c46SKenneth D. Merry 
15227e949c46SKenneth D. Merry 	/* Read the first request */
15237e949c46SKenneth D. Merry 	if (RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
15247e949c46SKenneth D. Merry 		netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx);
15257e949c46SKenneth D. Merry 		pkt->size = tx->size;
15267e949c46SKenneth D. Merry 		pkt->flags = tx->flags & ~NETTXF_more_data;
15277e949c46SKenneth D. Merry 		more_data = tx->flags & NETTXF_more_data;
15287e949c46SKenneth D. Merry 		pkt->list_len++;
15297e949c46SKenneth D. Merry 		pkt->car = idx;
15307e949c46SKenneth D. Merry 		idx++;
15317e949c46SKenneth D. Merry 	}
15327e949c46SKenneth D. Merry 
15337e949c46SKenneth D. Merry 	/* Read the extra info */
15347e949c46SKenneth D. Merry 	if ((pkt->flags & NETTXF_extra_info) &&
15357e949c46SKenneth D. Merry 	    RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
15367e949c46SKenneth D. Merry 		netif_extra_info_t *ext =
15377e949c46SKenneth D. Merry 		    (netif_extra_info_t*) RING_GET_REQUEST(tx_ring, idx);
15387e949c46SKenneth D. Merry 		pkt->extra.type = ext->type;
15397e949c46SKenneth D. Merry 		switch (pkt->extra.type) {
15407e949c46SKenneth D. Merry 			case XEN_NETIF_EXTRA_TYPE_GSO:
15417e949c46SKenneth D. Merry 				pkt->extra.u.gso = ext->u.gso;
154289e0f4d2SKip Macy 				break;
154389e0f4d2SKip Macy 			default:
15447e949c46SKenneth D. Merry 				/*
15457e949c46SKenneth D. Merry 				 * The reference Linux netfront driver will
15467e949c46SKenneth D. Merry 				 * never set any other extra.type.  So we don't
15477e949c46SKenneth D. Merry 				 * know what to do with it.  Let's print an
15487e949c46SKenneth D. Merry 				 * error, then consume and discard the packet
15497e949c46SKenneth D. Merry 				 */
15507e949c46SKenneth D. Merry 				printf("xnb(%s:%d): Unknown extra info type %d."
15517e949c46SKenneth D. Merry 				       "  Discarding packet\n",
15527e949c46SKenneth D. Merry 				       __func__, __LINE__, pkt->extra.type);
15537e949c46SKenneth D. Merry 				xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring,
15547e949c46SKenneth D. Merry 				    start));
15557e949c46SKenneth D. Merry 				xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring,
15567e949c46SKenneth D. Merry 				    idx));
15577e949c46SKenneth D. Merry 				discard = 1;
15587e949c46SKenneth D. Merry 				break;
155989e0f4d2SKip Macy 		}
156089e0f4d2SKip Macy 
15617e949c46SKenneth D. Merry 		pkt->extra.flags = ext->flags;
15627e949c46SKenneth D. Merry 		if (ext->flags & XEN_NETIF_EXTRA_FLAG_MORE) {
15637e949c46SKenneth D. Merry 			/*
15647e949c46SKenneth D. Merry 			 * The reference linux netfront driver never sets this
15657e949c46SKenneth D. Merry 			 * flag (nor does any other known netfront).  So we
15667e949c46SKenneth D. Merry 			 * will discard the packet.
15677e949c46SKenneth D. Merry 			 */
15687e949c46SKenneth D. Merry 			printf("xnb(%s:%d): Request sets "
15697e949c46SKenneth D. Merry 			    "XEN_NETIF_EXTRA_FLAG_MORE, but we can't handle "
15707e949c46SKenneth D. Merry 			    "that\n", __func__, __LINE__);
15717e949c46SKenneth D. Merry 			xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start));
15727e949c46SKenneth D. Merry 			xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx));
15737e949c46SKenneth D. Merry 			discard = 1;
157489e0f4d2SKip Macy 		}
157589e0f4d2SKip Macy 
15767e949c46SKenneth D. Merry 		idx++;
15777e949c46SKenneth D. Merry 	}
15787e949c46SKenneth D. Merry 
15797e949c46SKenneth D. Merry 	/* Set cdr.  If there is not more data, cdr is invalid */
15807e949c46SKenneth D. Merry 	pkt->cdr = idx;
15817e949c46SKenneth D. Merry 
15827e949c46SKenneth D. Merry 	/* Loop on remainder of packet */
15837e949c46SKenneth D. Merry 	while (more_data && RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
15847e949c46SKenneth D. Merry 		netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx);
15857e949c46SKenneth D. Merry 		pkt->list_len++;
15867e949c46SKenneth D. Merry 		cdr_size += tx->size;
15877e949c46SKenneth D. Merry 		if (tx->flags & ~NETTXF_more_data) {
15887e949c46SKenneth D. Merry 			/* There should be no other flags set at this point */
15897e949c46SKenneth D. Merry 			printf("xnb(%s:%d): Request sets unknown flags %d "
15907e949c46SKenneth D. Merry 			    "after the 1st request in the packet.\n",
15917e949c46SKenneth D. Merry 			    __func__, __LINE__, tx->flags);
15927e949c46SKenneth D. Merry 			xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start));
15937e949c46SKenneth D. Merry 			xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx));
15947e949c46SKenneth D. Merry 		}
15957e949c46SKenneth D. Merry 
15967e949c46SKenneth D. Merry 		more_data = tx->flags & NETTXF_more_data;
15977e949c46SKenneth D. Merry 		idx++;
15987e949c46SKenneth D. Merry 	}
15997e949c46SKenneth D. Merry 
16007e949c46SKenneth D. Merry 	/* Finalize packet */
16017e949c46SKenneth D. Merry 	if (more_data != 0) {
16027e949c46SKenneth D. Merry 		/* The ring ran out of requests before finishing the packet */
16037e949c46SKenneth D. Merry 		xnb_pkt_invalidate(pkt);
16047e949c46SKenneth D. Merry 		idx = start;	/* tell caller that we consumed no requests */
16057e949c46SKenneth D. Merry 	} else {
16067e949c46SKenneth D. Merry 		/* Calculate car_size */
16077e949c46SKenneth D. Merry 		pkt->car_size = pkt->size - cdr_size;
16087e949c46SKenneth D. Merry 	}
16097e949c46SKenneth D. Merry 	if (discard != 0) {
16107e949c46SKenneth D. Merry 		xnb_pkt_invalidate(pkt);
16117e949c46SKenneth D. Merry 	}
16127e949c46SKenneth D. Merry 
16137e949c46SKenneth D. Merry 	return idx - start;
16147e949c46SKenneth D. Merry }
16157e949c46SKenneth D. Merry 
16167e949c46SKenneth D. Merry 
16177e949c46SKenneth D. Merry /**
16187e949c46SKenneth D. Merry  * Respond to all the requests that constituted pkt.  Builds the responses and
16197e949c46SKenneth D. Merry  * writes them to the ring, but doesn't push them to the shared ring.
16207e949c46SKenneth D. Merry  * \param[in] pkt	the packet that needs a response
16217e949c46SKenneth D. Merry  * \param[in] error	true if there was an error handling the packet, such
16227e949c46SKenneth D. Merry  * 			as in the hypervisor copy op or mbuf allocation
16237e949c46SKenneth D. Merry  * \param[out] ring	Responses go here
16247e949c46SKenneth D. Merry  */
16257e949c46SKenneth D. Merry static void
16267e949c46SKenneth D. Merry xnb_txpkt2rsp(const struct xnb_pkt *pkt, netif_tx_back_ring_t *ring,
16277e949c46SKenneth D. Merry 	      int error)
162889e0f4d2SKip Macy {
16297e949c46SKenneth D. Merry 	/*
16307e949c46SKenneth D. Merry 	 * Outline:
16317e949c46SKenneth D. Merry 	 * 1) Respond to the first request
16327e949c46SKenneth D. Merry 	 * 2) Respond to the extra info reques
16337e949c46SKenneth D. Merry 	 * Loop through every remaining request in the packet, generating
16347e949c46SKenneth D. Merry 	 * responses that copy those requests' ids and sets the status
16357e949c46SKenneth D. Merry 	 * appropriately.
16367e949c46SKenneth D. Merry 	 */
16377e949c46SKenneth D. Merry 	netif_tx_request_t *tx;
16387e949c46SKenneth D. Merry 	netif_tx_response_t *rsp;
16397e949c46SKenneth D. Merry 	int i;
16407e949c46SKenneth D. Merry 	uint16_t status;
16417e949c46SKenneth D. Merry 
16427e949c46SKenneth D. Merry 	status = (xnb_pkt_is_valid(pkt) == 0) || error ?
16437e949c46SKenneth D. Merry 		NETIF_RSP_ERROR : NETIF_RSP_OKAY;
16447e949c46SKenneth D. Merry 	KASSERT((pkt->list_len == 0) || (ring->rsp_prod_pvt == pkt->car),
16457e949c46SKenneth D. Merry 	    ("Cannot respond to ring requests out of order"));
16467e949c46SKenneth D. Merry 
16477e949c46SKenneth D. Merry 	if (pkt->list_len >= 1) {
16487e949c46SKenneth D. Merry 		uint16_t id;
16497e949c46SKenneth D. Merry 		tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt);
16507e949c46SKenneth D. Merry 		id = tx->id;
16517e949c46SKenneth D. Merry 		rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
16527e949c46SKenneth D. Merry 		rsp->id = id;
16537e949c46SKenneth D. Merry 		rsp->status = status;
16547e949c46SKenneth D. Merry 		ring->rsp_prod_pvt++;
16557e949c46SKenneth D. Merry 
16567e949c46SKenneth D. Merry 		if (pkt->flags & NETRXF_extra_info) {
16577e949c46SKenneth D. Merry 			rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
16587e949c46SKenneth D. Merry 			rsp->status = NETIF_RSP_NULL;
16597e949c46SKenneth D. Merry 			ring->rsp_prod_pvt++;
16607e949c46SKenneth D. Merry 		}
16617e949c46SKenneth D. Merry 	}
16627e949c46SKenneth D. Merry 
16637e949c46SKenneth D. Merry 	for (i=0; i < pkt->list_len - 1; i++) {
16647e949c46SKenneth D. Merry 		uint16_t id;
16657e949c46SKenneth D. Merry 		tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt);
16667e949c46SKenneth D. Merry 		id = tx->id;
16677e949c46SKenneth D. Merry 		rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
16687e949c46SKenneth D. Merry 		rsp->id = id;
16697e949c46SKenneth D. Merry 		rsp->status = status;
16707e949c46SKenneth D. Merry 		ring->rsp_prod_pvt++;
16717e949c46SKenneth D. Merry 	}
16727e949c46SKenneth D. Merry }
16737e949c46SKenneth D. Merry 
16747e949c46SKenneth D. Merry /**
16757e949c46SKenneth D. Merry  * Create an mbuf chain to represent a packet.  Initializes all of the headers
16767e949c46SKenneth D. Merry  * in the mbuf chain, but does not copy the data.  The returned chain must be
16777e949c46SKenneth D. Merry  * free()'d when no longer needed
16787e949c46SKenneth D. Merry  * \param[in]	pkt	A packet to model the mbuf chain after
16797e949c46SKenneth D. Merry  * \return	A newly allocated mbuf chain, possibly with clusters attached.
16807e949c46SKenneth D. Merry  * 		NULL on failure
16817e949c46SKenneth D. Merry  */
16827e949c46SKenneth D. Merry static struct mbuf*
16837e949c46SKenneth D. Merry xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp)
16847e949c46SKenneth D. Merry {
16857e949c46SKenneth D. Merry 	/**
16867e949c46SKenneth D. Merry 	 * \todo consider using a memory pool for mbufs instead of
16877e949c46SKenneth D. Merry 	 * reallocating them for every packet
16887e949c46SKenneth D. Merry 	 */
16897e949c46SKenneth D. Merry 	/** \todo handle extra data */
16907e949c46SKenneth D. Merry 	struct mbuf *m;
16917e949c46SKenneth D. Merry 
16927e949c46SKenneth D. Merry 	m = m_getm(NULL, pkt->size, M_NOWAIT, MT_DATA);
16937e949c46SKenneth D. Merry 
16947e949c46SKenneth D. Merry 	if (m != NULL) {
16957e949c46SKenneth D. Merry 		m->m_pkthdr.rcvif = ifp;
16967e949c46SKenneth D. Merry 		if (pkt->flags & NETTXF_data_validated) {
16977e949c46SKenneth D. Merry 			/*
16987e949c46SKenneth D. Merry 			 * We lie to the host OS and always tell it that the
16997e949c46SKenneth D. Merry 			 * checksums are ok, because the packet is unlikely to
17007e949c46SKenneth D. Merry 			 * get corrupted going across domains.
17017e949c46SKenneth D. Merry 			 */
17027e949c46SKenneth D. Merry 			m->m_pkthdr.csum_flags = (
17037e949c46SKenneth D. Merry 				CSUM_IP_CHECKED |
17047e949c46SKenneth D. Merry 				CSUM_IP_VALID   |
17057e949c46SKenneth D. Merry 				CSUM_DATA_VALID |
17067e949c46SKenneth D. Merry 				CSUM_PSEUDO_HDR
17077e949c46SKenneth D. Merry 				);
17087e949c46SKenneth D. Merry 			m->m_pkthdr.csum_data = 0xffff;
17097e949c46SKenneth D. Merry 		}
17107e949c46SKenneth D. Merry 	}
17117e949c46SKenneth D. Merry 	return m;
17127e949c46SKenneth D. Merry }
17137e949c46SKenneth D. Merry 
17147e949c46SKenneth D. Merry /**
17157e949c46SKenneth D. Merry  * Build a gnttab_copy table that can be used to copy data from a pkt
17167e949c46SKenneth D. Merry  * to an mbufc.  Does not actually perform the copy.  Always uses gref's on
17177e949c46SKenneth D. Merry  * the packet side.
17187e949c46SKenneth D. Merry  * \param[in]	pkt	pkt's associated requests form the src for
17197e949c46SKenneth D. Merry  * 			the copy operation
17207e949c46SKenneth D. Merry  * \param[in]	mbufc	mbufc's storage forms the dest for the copy operation
17217e949c46SKenneth D. Merry  * \param[out]  gnttab	Storage for the returned grant table
17227e949c46SKenneth D. Merry  * \param[in]	txb	Pointer to the backend ring structure
17237e949c46SKenneth D. Merry  * \param[in]	otherend_id	The domain ID of the other end of the copy
17247e949c46SKenneth D. Merry  * \return 		The number of gnttab entries filled
17257e949c46SKenneth D. Merry  */
17267e949c46SKenneth D. Merry static int
17277e949c46SKenneth D. Merry xnb_txpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc,
17287e949c46SKenneth D. Merry 		 gnttab_copy_table gnttab, const netif_tx_back_ring_t *txb,
17297e949c46SKenneth D. Merry 		 domid_t otherend_id)
17307e949c46SKenneth D. Merry {
17317e949c46SKenneth D. Merry 
17327e949c46SKenneth D. Merry 	const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */
17337e949c46SKenneth D. Merry 	int gnt_idx = 0;		/* index into grant table */
17347e949c46SKenneth D. Merry 	RING_IDX r_idx = pkt->car;	/* index into tx ring buffer */
17357e949c46SKenneth D. Merry 	int r_ofs = 0;	/* offset of next data within tx request's data area */
17367e949c46SKenneth D. Merry 	int m_ofs = 0;	/* offset of next data within mbuf's data area */
17377e949c46SKenneth D. Merry 	/* size in bytes that still needs to be represented in the table */
17387e949c46SKenneth D. Merry 	uint16_t size_remaining = pkt->size;
17397e949c46SKenneth D. Merry 
17407e949c46SKenneth D. Merry 	while (size_remaining > 0) {
17417e949c46SKenneth D. Merry 		const netif_tx_request_t *txq = RING_GET_REQUEST(txb, r_idx);
17427e949c46SKenneth D. Merry 		const size_t mbuf_space = M_TRAILINGSPACE(mbuf) - m_ofs;
17437e949c46SKenneth D. Merry 		const size_t req_size =
17447e949c46SKenneth D. Merry 			r_idx == pkt->car ? pkt->car_size : txq->size;
17457e949c46SKenneth D. Merry 		const size_t pkt_space = req_size - r_ofs;
17467e949c46SKenneth D. Merry 		/*
17477e949c46SKenneth D. Merry 		 * space is the largest amount of data that can be copied in the
17487e949c46SKenneth D. Merry 		 * grant table's next entry
17497e949c46SKenneth D. Merry 		 */
17507e949c46SKenneth D. Merry 		const size_t space = MIN(pkt_space, mbuf_space);
17517e949c46SKenneth D. Merry 
17527e949c46SKenneth D. Merry 		/* TODO: handle this error condition without panicking */
17537e949c46SKenneth D. Merry 		KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short"));
17547e949c46SKenneth D. Merry 
17557e949c46SKenneth D. Merry 		gnttab[gnt_idx].source.u.ref = txq->gref;
17567e949c46SKenneth D. Merry 		gnttab[gnt_idx].source.domid = otherend_id;
17577e949c46SKenneth D. Merry 		gnttab[gnt_idx].source.offset = txq->offset + r_ofs;
17587e949c46SKenneth D. Merry 		gnttab[gnt_idx].dest.u.gmfn = virt_to_mfn(
17597e949c46SKenneth D. Merry 		    mtod(mbuf, vm_offset_t) + m_ofs);
17607e949c46SKenneth D. Merry 		gnttab[gnt_idx].dest.offset = virt_to_offset(
17617e949c46SKenneth D. Merry 		    mtod(mbuf, vm_offset_t) + m_ofs);
17627e949c46SKenneth D. Merry 		gnttab[gnt_idx].dest.domid = DOMID_SELF;
17637e949c46SKenneth D. Merry 		gnttab[gnt_idx].len = space;
17647e949c46SKenneth D. Merry 		gnttab[gnt_idx].flags = GNTCOPY_source_gref;
17657e949c46SKenneth D. Merry 
17667e949c46SKenneth D. Merry 		gnt_idx++;
17677e949c46SKenneth D. Merry 		r_ofs += space;
17687e949c46SKenneth D. Merry 		m_ofs += space;
17697e949c46SKenneth D. Merry 		size_remaining -= space;
17707e949c46SKenneth D. Merry 		if (req_size - r_ofs <= 0) {
17717e949c46SKenneth D. Merry 			/* Must move to the next tx request */
17727e949c46SKenneth D. Merry 			r_ofs = 0;
17737e949c46SKenneth D. Merry 			r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1;
17747e949c46SKenneth D. Merry 		}
17757e949c46SKenneth D. Merry 		if (M_TRAILINGSPACE(mbuf) - m_ofs <= 0) {
17767e949c46SKenneth D. Merry 			/* Must move to the next mbuf */
17777e949c46SKenneth D. Merry 			m_ofs = 0;
17787e949c46SKenneth D. Merry 			mbuf = mbuf->m_next;
17797e949c46SKenneth D. Merry 		}
17807e949c46SKenneth D. Merry 	}
17817e949c46SKenneth D. Merry 
17827e949c46SKenneth D. Merry 	return gnt_idx;
17837e949c46SKenneth D. Merry }
17847e949c46SKenneth D. Merry 
17857e949c46SKenneth D. Merry /**
17867e949c46SKenneth D. Merry  * Check the status of the grant copy operations, and update mbufs various
17877e949c46SKenneth D. Merry  * non-data fields to reflect the data present.
17887e949c46SKenneth D. Merry  * \param[in,out] mbufc	mbuf chain to update.  The chain must be valid and of
17897e949c46SKenneth D. Merry  * 			the correct length, and data should already be present
17907e949c46SKenneth D. Merry  * \param[in] gnttab	A grant table for a just completed copy op
17917e949c46SKenneth D. Merry  * \param[in] n_entries The number of valid entries in the grant table
17927e949c46SKenneth D. Merry  */
17937e949c46SKenneth D. Merry static void
17947e949c46SKenneth D. Merry xnb_update_mbufc(struct mbuf *mbufc, const gnttab_copy_table gnttab,
17957e949c46SKenneth D. Merry     		 int n_entries)
17967e949c46SKenneth D. Merry {
17977e949c46SKenneth D. Merry 	struct mbuf *mbuf = mbufc;
17987e949c46SKenneth D. Merry 	int i;
17997e949c46SKenneth D. Merry 	size_t total_size = 0;
18007e949c46SKenneth D. Merry 
18017e949c46SKenneth D. Merry 	for (i = 0; i < n_entries; i++) {
18027e949c46SKenneth D. Merry 		KASSERT(gnttab[i].status == GNTST_okay,
18037e949c46SKenneth D. Merry 		    ("Some gnttab_copy entry had error status %hd\n",
18047e949c46SKenneth D. Merry 		    gnttab[i].status));
18057e949c46SKenneth D. Merry 
18067e949c46SKenneth D. Merry 		mbuf->m_len += gnttab[i].len;
18077e949c46SKenneth D. Merry 		total_size += gnttab[i].len;
18087e949c46SKenneth D. Merry 		if (M_TRAILINGSPACE(mbuf) <= 0) {
18097e949c46SKenneth D. Merry 			mbuf = mbuf->m_next;
18107e949c46SKenneth D. Merry 		}
18117e949c46SKenneth D. Merry 	}
18127e949c46SKenneth D. Merry 	mbufc->m_pkthdr.len = total_size;
18137e949c46SKenneth D. Merry 
1814f909bbb4SGleb Smirnoff #if defined(INET) || defined(INET6)
18157e949c46SKenneth D. Merry 	xnb_add_mbuf_cksum(mbufc);
1816f909bbb4SGleb Smirnoff #endif
18177e949c46SKenneth D. Merry }
18187e949c46SKenneth D. Merry 
18197e949c46SKenneth D. Merry /**
18207e949c46SKenneth D. Merry  * Dequeue at most one packet from the shared ring
18217e949c46SKenneth D. Merry  * \param[in,out] txb	Netif tx ring.  A packet will be removed from it, and
18227e949c46SKenneth D. Merry  * 			its private indices will be updated.  But the indices
18237e949c46SKenneth D. Merry  * 			will not be pushed to the shared ring.
18247e949c46SKenneth D. Merry  * \param[in] ifnet	Interface to which the packet will be sent
18257e949c46SKenneth D. Merry  * \param[in] otherend	Domain ID of the other end of the ring
18267e949c46SKenneth D. Merry  * \param[out] mbufc	The assembled mbuf chain, ready to send to the generic
18277e949c46SKenneth D. Merry  * 			networking stack
18287e949c46SKenneth D. Merry  * \param[in,out] gnttab Pointer to enough memory for a grant table.  We make
18297e949c46SKenneth D. Merry  * 			this a function parameter so that we will take less
18307e949c46SKenneth D. Merry  * 			stack space.
18317e949c46SKenneth D. Merry  * \return		An error code
18327e949c46SKenneth D. Merry  */
18337e949c46SKenneth D. Merry static int
18347e949c46SKenneth D. Merry xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, struct mbuf **mbufc,
18357e949c46SKenneth D. Merry 	 struct ifnet *ifnet, gnttab_copy_table gnttab)
18367e949c46SKenneth D. Merry {
18377e949c46SKenneth D. Merry 	struct xnb_pkt pkt;
18387e949c46SKenneth D. Merry 	/* number of tx requests consumed to build the last packet */
18397e949c46SKenneth D. Merry 	int num_consumed;
18407e949c46SKenneth D. Merry 	int nr_ents;
18417e949c46SKenneth D. Merry 
18427e949c46SKenneth D. Merry 	*mbufc = NULL;
18437e949c46SKenneth D. Merry 	num_consumed = xnb_ring2pkt(&pkt, txb, txb->req_cons);
18447e949c46SKenneth D. Merry 	if (num_consumed == 0)
18457e949c46SKenneth D. Merry 		return 0;	/* Nothing to receive */
18467e949c46SKenneth D. Merry 
1847a8b5c2a0SGabor Kovesdan 	/* update statistics independent of errors */
1848c8dfaf38SGleb Smirnoff 	if_inc_counter(ifnet, IFCOUNTER_IPACKETS, 1);
18497e949c46SKenneth D. Merry 
18507e949c46SKenneth D. Merry 	/*
18517e949c46SKenneth D. Merry 	 * if we got here, then 1 or more requests was consumed, but the packet
1852a2098feaSGabor Kovesdan 	 * is not necessarily valid.
18537e949c46SKenneth D. Merry 	 */
18547e949c46SKenneth D. Merry 	if (xnb_pkt_is_valid(&pkt) == 0) {
18557e949c46SKenneth D. Merry 		/* got a garbage packet, respond and drop it */
18567e949c46SKenneth D. Merry 		xnb_txpkt2rsp(&pkt, txb, 1);
18577e949c46SKenneth D. Merry 		txb->req_cons += num_consumed;
18587e949c46SKenneth D. Merry 		DPRINTF("xnb_intr: garbage packet, num_consumed=%d\n",
18597e949c46SKenneth D. Merry 				num_consumed);
1860c8dfaf38SGleb Smirnoff 		if_inc_counter(ifnet, IFCOUNTER_IERRORS, 1);
18617e949c46SKenneth D. Merry 		return EINVAL;
18627e949c46SKenneth D. Merry 	}
18637e949c46SKenneth D. Merry 
18647e949c46SKenneth D. Merry 	*mbufc = xnb_pkt2mbufc(&pkt, ifnet);
18657e949c46SKenneth D. Merry 
18667e949c46SKenneth D. Merry 	if (*mbufc == NULL) {
18677e949c46SKenneth D. Merry 		/*
18687e949c46SKenneth D. Merry 		 * Couldn't allocate mbufs.  Respond and drop the packet.  Do
18697e949c46SKenneth D. Merry 		 * not consume the requests
18707e949c46SKenneth D. Merry 		 */
18717e949c46SKenneth D. Merry 		xnb_txpkt2rsp(&pkt, txb, 1);
18727e949c46SKenneth D. Merry 		DPRINTF("xnb_intr: Couldn't allocate mbufs, num_consumed=%d\n",
18737e949c46SKenneth D. Merry 		    num_consumed);
1874c8dfaf38SGleb Smirnoff 		if_inc_counter(ifnet, IFCOUNTER_IQDROPS, 1);
18757e949c46SKenneth D. Merry 		return ENOMEM;
18767e949c46SKenneth D. Merry 	}
18777e949c46SKenneth D. Merry 
18787e949c46SKenneth D. Merry 	nr_ents = xnb_txpkt2gnttab(&pkt, *mbufc, gnttab, txb, otherend);
18797e949c46SKenneth D. Merry 
18807e949c46SKenneth D. Merry 	if (nr_ents > 0) {
18817e949c46SKenneth D. Merry 		int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
18827e949c46SKenneth D. Merry 		    gnttab, nr_ents);
18837e949c46SKenneth D. Merry 		KASSERT(hv_ret == 0,
18847e949c46SKenneth D. Merry 		    ("HYPERVISOR_grant_table_op returned %d\n", hv_ret));
18857e949c46SKenneth D. Merry 		xnb_update_mbufc(*mbufc, gnttab, nr_ents);
18867e949c46SKenneth D. Merry 	}
18877e949c46SKenneth D. Merry 
18887e949c46SKenneth D. Merry 	xnb_txpkt2rsp(&pkt, txb, 0);
18897e949c46SKenneth D. Merry 	txb->req_cons += num_consumed;
189089e0f4d2SKip Macy 	return 0;
189189e0f4d2SKip Macy }
189289e0f4d2SKip Macy 
18937e949c46SKenneth D. Merry /**
18947e949c46SKenneth D. Merry  * Create an xnb_pkt based on the contents of an mbuf chain.
18957e949c46SKenneth D. Merry  * \param[in] mbufc	mbuf chain to transform into a packet
18967e949c46SKenneth D. Merry  * \param[out] pkt	Storage for the newly generated xnb_pkt
18977e949c46SKenneth D. Merry  * \param[in] start	The ring index of the first available slot in the rx
18987e949c46SKenneth D. Merry  * 			ring
18997e949c46SKenneth D. Merry  * \param[in] space	The number of free slots in the rx ring
19007e949c46SKenneth D. Merry  * \retval 0		Success
19017e949c46SKenneth D. Merry  * \retval EINVAL	mbufc was corrupt or not convertible into a pkt
19027e949c46SKenneth D. Merry  * \retval EAGAIN	There was not enough space in the ring to queue the
19037e949c46SKenneth D. Merry  * 			packet
19047e949c46SKenneth D. Merry  */
190589e0f4d2SKip Macy static int
19067e949c46SKenneth D. Merry xnb_mbufc2pkt(const struct mbuf *mbufc, struct xnb_pkt *pkt,
19077e949c46SKenneth D. Merry 	      RING_IDX start, int space)
190889e0f4d2SKip Macy {
190989e0f4d2SKip Macy 
19107e949c46SKenneth D. Merry 	int retval = 0;
191189e0f4d2SKip Macy 
19127e949c46SKenneth D. Merry 	if ((mbufc == NULL) ||
19137e949c46SKenneth D. Merry 	     ( (mbufc->m_flags & M_PKTHDR) == 0) ||
19147e949c46SKenneth D. Merry 	     (mbufc->m_pkthdr.len == 0)) {
19157e949c46SKenneth D. Merry 		xnb_pkt_invalidate(pkt);
19167e949c46SKenneth D. Merry 		retval = EINVAL;
19177e949c46SKenneth D. Merry 	} else {
19187e949c46SKenneth D. Merry 		int slots_required;
191989e0f4d2SKip Macy 
19207e949c46SKenneth D. Merry 		xnb_pkt_validate(pkt);
19217e949c46SKenneth D. Merry 		pkt->flags = 0;
19227e949c46SKenneth D. Merry 		pkt->size = mbufc->m_pkthdr.len;
19237e949c46SKenneth D. Merry 		pkt->car = start;
19247e949c46SKenneth D. Merry 		pkt->car_size = mbufc->m_len;
192589e0f4d2SKip Macy 
19267e949c46SKenneth D. Merry 		if (mbufc->m_pkthdr.csum_flags & CSUM_TSO) {
19277e949c46SKenneth D. Merry 			pkt->flags |= NETRXF_extra_info;
19287e949c46SKenneth D. Merry 			pkt->extra.u.gso.size = mbufc->m_pkthdr.tso_segsz;
19297e949c46SKenneth D. Merry 			pkt->extra.u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
19307e949c46SKenneth D. Merry 			pkt->extra.u.gso.pad = 0;
19317e949c46SKenneth D. Merry 			pkt->extra.u.gso.features = 0;
19327e949c46SKenneth D. Merry 			pkt->extra.type = XEN_NETIF_EXTRA_TYPE_GSO;
19337e949c46SKenneth D. Merry 			pkt->extra.flags = 0;
19347e949c46SKenneth D. Merry 			pkt->cdr = start + 2;
19357e949c46SKenneth D. Merry 		} else {
19367e949c46SKenneth D. Merry 			pkt->cdr = start + 1;
19377e949c46SKenneth D. Merry 		}
19387e949c46SKenneth D. Merry 		if (mbufc->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_DELAY_DATA)) {
19397e949c46SKenneth D. Merry 			pkt->flags |=
19407e949c46SKenneth D. Merry 			    (NETRXF_csum_blank | NETRXF_data_validated);
19417e949c46SKenneth D. Merry 		}
194289e0f4d2SKip Macy 
19437e949c46SKenneth D. Merry 		/*
19447e949c46SKenneth D. Merry 		 * Each ring response can have up to PAGE_SIZE of data.
19457e949c46SKenneth D. Merry 		 * Assume that we can defragment the mbuf chain efficiently
19467e949c46SKenneth D. Merry 		 * into responses so that each response but the last uses all
19477e949c46SKenneth D. Merry 		 * PAGE_SIZE bytes.
19487e949c46SKenneth D. Merry 		 */
19497e949c46SKenneth D. Merry 		pkt->list_len = (pkt->size + PAGE_SIZE - 1) / PAGE_SIZE;
195089e0f4d2SKip Macy 
19517e949c46SKenneth D. Merry 		if (pkt->list_len > 1) {
19527e949c46SKenneth D. Merry 			pkt->flags |= NETRXF_more_data;
19537e949c46SKenneth D. Merry 		}
19547e949c46SKenneth D. Merry 
19557e949c46SKenneth D. Merry 		slots_required = pkt->list_len +
19567e949c46SKenneth D. Merry 			(pkt->flags & NETRXF_extra_info ? 1 : 0);
19577e949c46SKenneth D. Merry 		if (slots_required > space) {
19587e949c46SKenneth D. Merry 			xnb_pkt_invalidate(pkt);
19597e949c46SKenneth D. Merry 			retval = EAGAIN;
196089e0f4d2SKip Macy 		}
196189e0f4d2SKip Macy 	}
196289e0f4d2SKip Macy 
19637e949c46SKenneth D. Merry 	return retval;
196489e0f4d2SKip Macy }
196589e0f4d2SKip Macy 
19667e949c46SKenneth D. Merry /**
19677e949c46SKenneth D. Merry  * Build a gnttab_copy table that can be used to copy data from an mbuf chain
19687e949c46SKenneth D. Merry  * to the frontend's shared buffers.  Does not actually perform the copy.
19697e949c46SKenneth D. Merry  * Always uses gref's on the other end's side.
19707e949c46SKenneth D. Merry  * \param[in]	pkt	pkt's associated responses form the dest for the copy
19717e949c46SKenneth D. Merry  * 			operatoin
19727e949c46SKenneth D. Merry  * \param[in]	mbufc	The source for the copy operation
19737e949c46SKenneth D. Merry  * \param[out]	gnttab	Storage for the returned grant table
19747e949c46SKenneth D. Merry  * \param[in]	rxb	Pointer to the backend ring structure
19757e949c46SKenneth D. Merry  * \param[in]	otherend_id	The domain ID of the other end of the copy
19767e949c46SKenneth D. Merry  * \return 		The number of gnttab entries filled
19777e949c46SKenneth D. Merry  */
197889e0f4d2SKip Macy static int
19797e949c46SKenneth D. Merry xnb_rxpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc,
19807e949c46SKenneth D. Merry 		 gnttab_copy_table gnttab, const netif_rx_back_ring_t *rxb,
19817e949c46SKenneth D. Merry 		 domid_t otherend_id)
198289e0f4d2SKip Macy {
198389e0f4d2SKip Macy 
19847e949c46SKenneth D. Merry 	const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */
19857e949c46SKenneth D. Merry 	int gnt_idx = 0;		/* index into grant table */
19867e949c46SKenneth D. Merry 	RING_IDX r_idx = pkt->car;	/* index into rx ring buffer */
19877e949c46SKenneth D. Merry 	int r_ofs = 0;	/* offset of next data within rx request's data area */
19887e949c46SKenneth D. Merry 	int m_ofs = 0;	/* offset of next data within mbuf's data area */
19897e949c46SKenneth D. Merry 	/* size in bytes that still needs to be represented in the table */
19907e949c46SKenneth D. Merry 	uint16_t size_remaining;
199189e0f4d2SKip Macy 
19927e949c46SKenneth D. Merry 	size_remaining = (xnb_pkt_is_valid(pkt) != 0) ? pkt->size : 0;
19937e949c46SKenneth D. Merry 
19947e949c46SKenneth D. Merry 	while (size_remaining > 0) {
19957e949c46SKenneth D. Merry 		const netif_rx_request_t *rxq = RING_GET_REQUEST(rxb, r_idx);
19967e949c46SKenneth D. Merry 		const size_t mbuf_space = mbuf->m_len - m_ofs;
19977e949c46SKenneth D. Merry 		/* Xen shared pages have an implied size of PAGE_SIZE */
19987e949c46SKenneth D. Merry 		const size_t req_size = PAGE_SIZE;
19997e949c46SKenneth D. Merry 		const size_t pkt_space = req_size - r_ofs;
20007e949c46SKenneth D. Merry 		/*
20017e949c46SKenneth D. Merry 		 * space is the largest amount of data that can be copied in the
20027e949c46SKenneth D. Merry 		 * grant table's next entry
20037e949c46SKenneth D. Merry 		 */
20047e949c46SKenneth D. Merry 		const size_t space = MIN(pkt_space, mbuf_space);
20057e949c46SKenneth D. Merry 
20067e949c46SKenneth D. Merry 		/* TODO: handle this error condition without panicing */
20077e949c46SKenneth D. Merry 		KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short"));
20087e949c46SKenneth D. Merry 
20097e949c46SKenneth D. Merry 		gnttab[gnt_idx].dest.u.ref = rxq->gref;
20107e949c46SKenneth D. Merry 		gnttab[gnt_idx].dest.domid = otherend_id;
20117e949c46SKenneth D. Merry 		gnttab[gnt_idx].dest.offset = r_ofs;
20127e949c46SKenneth D. Merry 		gnttab[gnt_idx].source.u.gmfn = virt_to_mfn(
20137e949c46SKenneth D. Merry 		    mtod(mbuf, vm_offset_t) + m_ofs);
20147e949c46SKenneth D. Merry 		gnttab[gnt_idx].source.offset = virt_to_offset(
20157e949c46SKenneth D. Merry 		    mtod(mbuf, vm_offset_t) + m_ofs);
20167e949c46SKenneth D. Merry 		gnttab[gnt_idx].source.domid = DOMID_SELF;
20177e949c46SKenneth D. Merry 		gnttab[gnt_idx].len = space;
20187e949c46SKenneth D. Merry 		gnttab[gnt_idx].flags = GNTCOPY_dest_gref;
20197e949c46SKenneth D. Merry 
20207e949c46SKenneth D. Merry 		gnt_idx++;
20217e949c46SKenneth D. Merry 
20227e949c46SKenneth D. Merry 		r_ofs += space;
20237e949c46SKenneth D. Merry 		m_ofs += space;
20247e949c46SKenneth D. Merry 		size_remaining -= space;
20257e949c46SKenneth D. Merry 		if (req_size - r_ofs <= 0) {
20267e949c46SKenneth D. Merry 			/* Must move to the next rx request */
20277e949c46SKenneth D. Merry 			r_ofs = 0;
20287e949c46SKenneth D. Merry 			r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1;
20297e949c46SKenneth D. Merry 		}
20307e949c46SKenneth D. Merry 		if (mbuf->m_len - m_ofs <= 0) {
20317e949c46SKenneth D. Merry 			/* Must move to the next mbuf */
20327e949c46SKenneth D. Merry 			m_ofs = 0;
20337e949c46SKenneth D. Merry 			mbuf = mbuf->m_next;
20347e949c46SKenneth D. Merry 		}
20357e949c46SKenneth D. Merry 	}
20367e949c46SKenneth D. Merry 
20377e949c46SKenneth D. Merry 	return gnt_idx;
20387e949c46SKenneth D. Merry }
20397e949c46SKenneth D. Merry 
20407e949c46SKenneth D. Merry /**
20417e949c46SKenneth D. Merry  * Generates responses for all the requests that constituted pkt.  Builds
20427e949c46SKenneth D. Merry  * responses and writes them to the ring, but doesn't push the shared ring
20437e949c46SKenneth D. Merry  * indices.
20447e949c46SKenneth D. Merry  * \param[in] pkt	the packet that needs a response
20457e949c46SKenneth D. Merry  * \param[in] gnttab	The grant copy table corresponding to this packet.
20467e949c46SKenneth D. Merry  * 			Used to determine how many rsp->netif_rx_response_t's to
20477e949c46SKenneth D. Merry  * 			generate.
20487e949c46SKenneth D. Merry  * \param[in] n_entries	Number of relevant entries in the grant table
20497e949c46SKenneth D. Merry  * \param[out] ring	Responses go here
20507e949c46SKenneth D. Merry  * \return		The number of RX requests that were consumed to generate
20517e949c46SKenneth D. Merry  * 			the responses
20527e949c46SKenneth D. Merry  */
20537e949c46SKenneth D. Merry static int
20547e949c46SKenneth D. Merry xnb_rxpkt2rsp(const struct xnb_pkt *pkt, const gnttab_copy_table gnttab,
20557e949c46SKenneth D. Merry     	      int n_entries, netif_rx_back_ring_t *ring)
20567e949c46SKenneth D. Merry {
20577e949c46SKenneth D. Merry 	/*
20587e949c46SKenneth D. Merry 	 * This code makes the following assumptions:
20597e949c46SKenneth D. Merry 	 *	* All entries in gnttab set GNTCOPY_dest_gref
20607e949c46SKenneth D. Merry 	 *	* The entries in gnttab are grouped by their grefs: any two
20617e949c46SKenneth D. Merry 	 *	   entries with the same gref must be adjacent
20627e949c46SKenneth D. Merry 	 */
20637e949c46SKenneth D. Merry 	int error = 0;
20647e949c46SKenneth D. Merry 	int gnt_idx, i;
20657e949c46SKenneth D. Merry 	int n_responses = 0;
20667e949c46SKenneth D. Merry 	grant_ref_t last_gref = GRANT_REF_INVALID;
20677e949c46SKenneth D. Merry 	RING_IDX r_idx;
20687e949c46SKenneth D. Merry 
20697e949c46SKenneth D. Merry 	KASSERT(gnttab != NULL, ("Received a null granttable copy"));
20707e949c46SKenneth D. Merry 
20717e949c46SKenneth D. Merry 	/*
20727e949c46SKenneth D. Merry 	 * In the event of an error, we only need to send one response to the
20737e949c46SKenneth D. Merry 	 * netfront.  In that case, we musn't write any data to the responses
20747e949c46SKenneth D. Merry 	 * after the one we send.  So we must loop all the way through gnttab
20757e949c46SKenneth D. Merry 	 * looking for errors before we generate any responses
20767e949c46SKenneth D. Merry 	 *
20777e949c46SKenneth D. Merry 	 * Since we're looping through the grant table anyway, we'll count the
20787e949c46SKenneth D. Merry 	 * number of different gref's in it, which will tell us how many
20797e949c46SKenneth D. Merry 	 * responses to generate
20807e949c46SKenneth D. Merry 	 */
20817e949c46SKenneth D. Merry 	for (gnt_idx = 0; gnt_idx < n_entries; gnt_idx++) {
20827e949c46SKenneth D. Merry 		int16_t status = gnttab[gnt_idx].status;
20837e949c46SKenneth D. Merry 		if (status != GNTST_okay) {
20847e949c46SKenneth D. Merry 			DPRINTF(
20857e949c46SKenneth D. Merry 			    "Got error %d for hypervisor gnttab_copy status\n",
20867e949c46SKenneth D. Merry 			    status);
20877e949c46SKenneth D. Merry 			error = 1;
20887e949c46SKenneth D. Merry 			break;
20897e949c46SKenneth D. Merry 		}
20907e949c46SKenneth D. Merry 		if (gnttab[gnt_idx].dest.u.ref != last_gref) {
20917e949c46SKenneth D. Merry 			n_responses++;
20927e949c46SKenneth D. Merry 			last_gref = gnttab[gnt_idx].dest.u.ref;
20937e949c46SKenneth D. Merry 		}
20947e949c46SKenneth D. Merry 	}
20957e949c46SKenneth D. Merry 
20967e949c46SKenneth D. Merry 	if (error != 0) {
20977e949c46SKenneth D. Merry 		uint16_t id;
20987e949c46SKenneth D. Merry 		netif_rx_response_t *rsp;
20997e949c46SKenneth D. Merry 
21007e949c46SKenneth D. Merry 		id = RING_GET_REQUEST(ring, ring->rsp_prod_pvt)->id;
21017e949c46SKenneth D. Merry 		rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
21027e949c46SKenneth D. Merry 		rsp->id = id;
21037e949c46SKenneth D. Merry 		rsp->status = NETIF_RSP_ERROR;
21047e949c46SKenneth D. Merry 		n_responses = 1;
21057e949c46SKenneth D. Merry 	} else {
21067e949c46SKenneth D. Merry 		gnt_idx = 0;
21077e949c46SKenneth D. Merry 		const int has_extra = pkt->flags & NETRXF_extra_info;
21087e949c46SKenneth D. Merry 		if (has_extra != 0)
21097e949c46SKenneth D. Merry 			n_responses++;
21107e949c46SKenneth D. Merry 
21117e949c46SKenneth D. Merry 		for (i = 0; i < n_responses; i++) {
21127e949c46SKenneth D. Merry 			netif_rx_request_t rxq;
21137e949c46SKenneth D. Merry 			netif_rx_response_t *rsp;
21147e949c46SKenneth D. Merry 
21157e949c46SKenneth D. Merry 			r_idx = ring->rsp_prod_pvt + i;
21167e949c46SKenneth D. Merry 			/*
21177e949c46SKenneth D. Merry 			 * We copy the structure of rxq instead of making a
21187e949c46SKenneth D. Merry 			 * pointer because it shares the same memory as rsp.
21197e949c46SKenneth D. Merry 			 */
21207e949c46SKenneth D. Merry 			rxq = *(RING_GET_REQUEST(ring, r_idx));
21217e949c46SKenneth D. Merry 			rsp = RING_GET_RESPONSE(ring, r_idx);
21227e949c46SKenneth D. Merry 			if (has_extra && (i == 1)) {
21237e949c46SKenneth D. Merry 				netif_extra_info_t *ext =
21247e949c46SKenneth D. Merry 					(netif_extra_info_t*)rsp;
21257e949c46SKenneth D. Merry 				ext->type = XEN_NETIF_EXTRA_TYPE_GSO;
21267e949c46SKenneth D. Merry 				ext->flags = 0;
21277e949c46SKenneth D. Merry 				ext->u.gso.size = pkt->extra.u.gso.size;
21287e949c46SKenneth D. Merry 				ext->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
21297e949c46SKenneth D. Merry 				ext->u.gso.pad = 0;
21307e949c46SKenneth D. Merry 				ext->u.gso.features = 0;
21317e949c46SKenneth D. Merry 			} else {
21327e949c46SKenneth D. Merry 				rsp->id = rxq.id;
21337e949c46SKenneth D. Merry 				rsp->status = GNTST_okay;
21347e949c46SKenneth D. Merry 				rsp->offset = 0;
21357e949c46SKenneth D. Merry 				rsp->flags = 0;
21367e949c46SKenneth D. Merry 				if (i < pkt->list_len - 1)
21377e949c46SKenneth D. Merry 					rsp->flags |= NETRXF_more_data;
21387e949c46SKenneth D. Merry 				if ((i == 0) && has_extra)
21397e949c46SKenneth D. Merry 					rsp->flags |= NETRXF_extra_info;
21407e949c46SKenneth D. Merry 				if ((i == 0) &&
21417e949c46SKenneth D. Merry 					(pkt->flags & NETRXF_data_validated)) {
21427e949c46SKenneth D. Merry 					rsp->flags |= NETRXF_data_validated;
21437e949c46SKenneth D. Merry 					rsp->flags |= NETRXF_csum_blank;
21447e949c46SKenneth D. Merry 				}
21457e949c46SKenneth D. Merry 				rsp->status = 0;
21467e949c46SKenneth D. Merry 				for (; gnttab[gnt_idx].dest.u.ref == rxq.gref;
21477e949c46SKenneth D. Merry 				    gnt_idx++) {
21487e949c46SKenneth D. Merry 					rsp->status += gnttab[gnt_idx].len;
21497e949c46SKenneth D. Merry 				}
21507e949c46SKenneth D. Merry 			}
21517e949c46SKenneth D. Merry 		}
21527e949c46SKenneth D. Merry 	}
21537e949c46SKenneth D. Merry 
21547e949c46SKenneth D. Merry 	ring->req_cons += n_responses;
21557e949c46SKenneth D. Merry 	ring->rsp_prod_pvt += n_responses;
21567e949c46SKenneth D. Merry 	return n_responses;
21577e949c46SKenneth D. Merry }
21587e949c46SKenneth D. Merry 
2159f909bbb4SGleb Smirnoff #if defined(INET) || defined(INET6)
21607e949c46SKenneth D. Merry /**
21617e949c46SKenneth D. Merry  * Add IP, TCP, and/or UDP checksums to every mbuf in a chain.  The first mbuf
21627e949c46SKenneth D. Merry  * in the chain must start with a struct ether_header.
21637e949c46SKenneth D. Merry  *
21647e949c46SKenneth D. Merry  * XXX This function will perform incorrectly on UDP packets that are split up
21657e949c46SKenneth D. Merry  * into multiple ethernet frames.
21667e949c46SKenneth D. Merry  */
21677e949c46SKenneth D. Merry static void
21687e949c46SKenneth D. Merry xnb_add_mbuf_cksum(struct mbuf *mbufc)
21697e949c46SKenneth D. Merry {
21707e949c46SKenneth D. Merry 	struct ether_header *eh;
21717e949c46SKenneth D. Merry 	struct ip *iph;
21727e949c46SKenneth D. Merry 	uint16_t ether_type;
21737e949c46SKenneth D. Merry 
21747e949c46SKenneth D. Merry 	eh = mtod(mbufc, struct ether_header*);
21757e949c46SKenneth D. Merry 	ether_type = ntohs(eh->ether_type);
21767e949c46SKenneth D. Merry 	if (ether_type != ETHERTYPE_IP) {
21777e949c46SKenneth D. Merry 		/* Nothing to calculate */
21787e949c46SKenneth D. Merry 		return;
21797e949c46SKenneth D. Merry 	}
21807e949c46SKenneth D. Merry 
21817e949c46SKenneth D. Merry 	iph = (struct ip*)(eh + 1);
21827e949c46SKenneth D. Merry 	if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
21837e949c46SKenneth D. Merry 		iph->ip_sum = 0;
21847e949c46SKenneth D. Merry 		iph->ip_sum = in_cksum_hdr(iph);
21857e949c46SKenneth D. Merry 	}
21867e949c46SKenneth D. Merry 
21877e949c46SKenneth D. Merry 	switch (iph->ip_p) {
21887e949c46SKenneth D. Merry 	case IPPROTO_TCP:
21897e949c46SKenneth D. Merry 		if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
21907e949c46SKenneth D. Merry 			size_t tcplen = ntohs(iph->ip_len) - sizeof(struct ip);
21917e949c46SKenneth D. Merry 			struct tcphdr *th = (struct tcphdr*)(iph + 1);
21927e949c46SKenneth D. Merry 			th->th_sum = in_pseudo(iph->ip_src.s_addr,
21937e949c46SKenneth D. Merry 			    iph->ip_dst.s_addr, htons(IPPROTO_TCP + tcplen));
21947e949c46SKenneth D. Merry 			th->th_sum = in_cksum_skip(mbufc,
21957e949c46SKenneth D. Merry 			    sizeof(struct ether_header) + ntohs(iph->ip_len),
21967e949c46SKenneth D. Merry 			    sizeof(struct ether_header) + (iph->ip_hl << 2));
21977e949c46SKenneth D. Merry 		}
21987e949c46SKenneth D. Merry 		break;
21997e949c46SKenneth D. Merry 	case IPPROTO_UDP:
22007e949c46SKenneth D. Merry 		if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
22017e949c46SKenneth D. Merry 			size_t udplen = ntohs(iph->ip_len) - sizeof(struct ip);
22027e949c46SKenneth D. Merry 			struct udphdr *uh = (struct udphdr*)(iph + 1);
22037e949c46SKenneth D. Merry 			uh->uh_sum = in_pseudo(iph->ip_src.s_addr,
22047e949c46SKenneth D. Merry 			    iph->ip_dst.s_addr, htons(IPPROTO_UDP + udplen));
22057e949c46SKenneth D. Merry 			uh->uh_sum = in_cksum_skip(mbufc,
22067e949c46SKenneth D. Merry 			    sizeof(struct ether_header) + ntohs(iph->ip_len),
22077e949c46SKenneth D. Merry 			    sizeof(struct ether_header) + (iph->ip_hl << 2));
22087e949c46SKenneth D. Merry 		}
22097e949c46SKenneth D. Merry 		break;
22107e949c46SKenneth D. Merry 	default:
22117e949c46SKenneth D. Merry 		break;
22127e949c46SKenneth D. Merry 	}
22137e949c46SKenneth D. Merry }
2214f909bbb4SGleb Smirnoff #endif /* INET || INET6 */
22157e949c46SKenneth D. Merry 
22167e949c46SKenneth D. Merry static void
22177e949c46SKenneth D. Merry xnb_stop(struct xnb_softc *xnb)
22187e949c46SKenneth D. Merry {
22197e949c46SKenneth D. Merry 	struct ifnet *ifp;
22207e949c46SKenneth D. Merry 
22217e949c46SKenneth D. Merry 	mtx_assert(&xnb->sc_lock, MA_OWNED);
22227e949c46SKenneth D. Merry 	ifp = xnb->xnb_ifp;
222389e0f4d2SKip Macy 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
22247e949c46SKenneth D. Merry 	if_link_state_change(ifp, LINK_STATE_DOWN);
22257e949c46SKenneth D. Merry }
222689e0f4d2SKip Macy 
22277e949c46SKenneth D. Merry static int
22287e949c46SKenneth D. Merry xnb_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
22297e949c46SKenneth D. Merry {
22307e949c46SKenneth D. Merry 	struct xnb_softc *xnb = ifp->if_softc;
22317e949c46SKenneth D. Merry 	struct ifreq *ifr = (struct ifreq*) data;
22325f8dfdceSBjoern A. Zeeb #ifdef INET
22337e949c46SKenneth D. Merry 	struct ifaddr *ifa = (struct ifaddr*)data;
22347e949c46SKenneth D. Merry #endif
22357e949c46SKenneth D. Merry 	int error = 0;
223689e0f4d2SKip Macy 
22377e949c46SKenneth D. Merry 	switch (cmd) {
22387e949c46SKenneth D. Merry 		case SIOCSIFFLAGS:
22397e949c46SKenneth D. Merry 			mtx_lock(&xnb->sc_lock);
22407e949c46SKenneth D. Merry 			if (ifp->if_flags & IFF_UP) {
22417e949c46SKenneth D. Merry 				xnb_ifinit_locked(xnb);
22427e949c46SKenneth D. Merry 			} else {
22437e949c46SKenneth D. Merry 				if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
22447e949c46SKenneth D. Merry 					xnb_stop(xnb);
22457e949c46SKenneth D. Merry 				}
22467e949c46SKenneth D. Merry 			}
22477e949c46SKenneth D. Merry 			/*
22487e949c46SKenneth D. Merry 			 * Note: netfront sets a variable named xn_if_flags
22497e949c46SKenneth D. Merry 			 * here, but that variable is never read
22507e949c46SKenneth D. Merry 			 */
22517e949c46SKenneth D. Merry 			mtx_unlock(&xnb->sc_lock);
22527e949c46SKenneth D. Merry 			break;
22537e949c46SKenneth D. Merry 		case SIOCSIFADDR:
22547e949c46SKenneth D. Merry #ifdef INET
22557e949c46SKenneth D. Merry 			mtx_lock(&xnb->sc_lock);
22567e949c46SKenneth D. Merry 			if (ifa->ifa_addr->sa_family == AF_INET) {
22577e949c46SKenneth D. Merry 				ifp->if_flags |= IFF_UP;
22587e949c46SKenneth D. Merry 				if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
22597e949c46SKenneth D. Merry 					ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
22607e949c46SKenneth D. Merry 							IFF_DRV_OACTIVE);
22617e949c46SKenneth D. Merry 					if_link_state_change(ifp,
22627e949c46SKenneth D. Merry 							LINK_STATE_DOWN);
22637e949c46SKenneth D. Merry 					ifp->if_drv_flags |= IFF_DRV_RUNNING;
22647e949c46SKenneth D. Merry 					ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
22657e949c46SKenneth D. Merry 					if_link_state_change(ifp,
22667e949c46SKenneth D. Merry 					    LINK_STATE_UP);
22677e949c46SKenneth D. Merry 				}
22687e949c46SKenneth D. Merry 				arp_ifinit(ifp, ifa);
22697e949c46SKenneth D. Merry 				mtx_unlock(&xnb->sc_lock);
22707e949c46SKenneth D. Merry 			} else {
22717e949c46SKenneth D. Merry 				mtx_unlock(&xnb->sc_lock);
22727e949c46SKenneth D. Merry #endif
22737e949c46SKenneth D. Merry 				error = ether_ioctl(ifp, cmd, data);
22747e949c46SKenneth D. Merry #ifdef INET
22757e949c46SKenneth D. Merry 			}
22767e949c46SKenneth D. Merry #endif
22777e949c46SKenneth D. Merry 			break;
22787e949c46SKenneth D. Merry 		case SIOCSIFCAP:
22797e949c46SKenneth D. Merry 			mtx_lock(&xnb->sc_lock);
22807e949c46SKenneth D. Merry 			if (ifr->ifr_reqcap & IFCAP_TXCSUM) {
22817e949c46SKenneth D. Merry 				ifp->if_capenable |= IFCAP_TXCSUM;
22827e949c46SKenneth D. Merry 				ifp->if_hwassist |= XNB_CSUM_FEATURES;
22837e949c46SKenneth D. Merry 			} else {
22847e949c46SKenneth D. Merry 				ifp->if_capenable &= ~(IFCAP_TXCSUM);
22857e949c46SKenneth D. Merry 				ifp->if_hwassist &= ~(XNB_CSUM_FEATURES);
22867e949c46SKenneth D. Merry 			}
22877e949c46SKenneth D. Merry 			if ((ifr->ifr_reqcap & IFCAP_RXCSUM)) {
22887e949c46SKenneth D. Merry 				ifp->if_capenable |= IFCAP_RXCSUM;
22897e949c46SKenneth D. Merry 			} else {
22907e949c46SKenneth D. Merry 				ifp->if_capenable &= ~(IFCAP_RXCSUM);
22917e949c46SKenneth D. Merry 			}
22927e949c46SKenneth D. Merry 			/*
22937e949c46SKenneth D. Merry 			 * TODO enable TSO4 and LRO once we no longer need
22947e949c46SKenneth D. Merry 			 * to calculate checksums in software
22957e949c46SKenneth D. Merry 			 */
22967e949c46SKenneth D. Merry #if 0
22977e949c46SKenneth D. Merry 			if (ifr->if_reqcap |= IFCAP_TSO4) {
22987e949c46SKenneth D. Merry 				if (IFCAP_TXCSUM & ifp->if_capenable) {
22997e949c46SKenneth D. Merry 					printf("xnb: Xen netif requires that "
23007e949c46SKenneth D. Merry 						"TXCSUM be enabled in order "
23017e949c46SKenneth D. Merry 						"to use TSO4\n");
23027e949c46SKenneth D. Merry 					error = EINVAL;
23037e949c46SKenneth D. Merry 				} else {
23047e949c46SKenneth D. Merry 					ifp->if_capenable |= IFCAP_TSO4;
23057e949c46SKenneth D. Merry 					ifp->if_hwassist |= CSUM_TSO;
23067e949c46SKenneth D. Merry 				}
23077e949c46SKenneth D. Merry 			} else {
23087e949c46SKenneth D. Merry 				ifp->if_capenable &= ~(IFCAP_TSO4);
23097e949c46SKenneth D. Merry 				ifp->if_hwassist &= ~(CSUM_TSO);
23107e949c46SKenneth D. Merry 			}
23117e949c46SKenneth D. Merry 			if (ifr->ifreqcap |= IFCAP_LRO) {
23127e949c46SKenneth D. Merry 				ifp->if_capenable |= IFCAP_LRO;
23137e949c46SKenneth D. Merry 			} else {
23147e949c46SKenneth D. Merry 				ifp->if_capenable &= ~(IFCAP_LRO);
23157e949c46SKenneth D. Merry 			}
23167e949c46SKenneth D. Merry #endif
23177e949c46SKenneth D. Merry 			mtx_unlock(&xnb->sc_lock);
23187e949c46SKenneth D. Merry 			break;
23197e949c46SKenneth D. Merry 		case SIOCSIFMTU:
23207e949c46SKenneth D. Merry 			ifp->if_mtu = ifr->ifr_mtu;
23217e949c46SKenneth D. Merry 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
23227e949c46SKenneth D. Merry 			xnb_ifinit(xnb);
23237e949c46SKenneth D. Merry 			break;
23247e949c46SKenneth D. Merry 		case SIOCADDMULTI:
23257e949c46SKenneth D. Merry 		case SIOCDELMULTI:
23267e949c46SKenneth D. Merry 		case SIOCSIFMEDIA:
23277e949c46SKenneth D. Merry 		case SIOCGIFMEDIA:
23287e949c46SKenneth D. Merry 			error = ifmedia_ioctl(ifp, ifr, &xnb->sc_media, cmd);
23297e949c46SKenneth D. Merry 			break;
23307e949c46SKenneth D. Merry 		default:
23317e949c46SKenneth D. Merry 			error = ether_ioctl(ifp, cmd, data);
23327e949c46SKenneth D. Merry 			break;
23337e949c46SKenneth D. Merry 	}
23347e949c46SKenneth D. Merry 	return (error);
23357e949c46SKenneth D. Merry }
233689e0f4d2SKip Macy 
23377e949c46SKenneth D. Merry static void
23387e949c46SKenneth D. Merry xnb_start_locked(struct ifnet *ifp)
23397e949c46SKenneth D. Merry {
23407e949c46SKenneth D. Merry 	netif_rx_back_ring_t *rxb;
23417e949c46SKenneth D. Merry 	struct xnb_softc *xnb;
23427e949c46SKenneth D. Merry 	struct mbuf *mbufc;
23437e949c46SKenneth D. Merry 	RING_IDX req_prod_local;
234489e0f4d2SKip Macy 
23457e949c46SKenneth D. Merry 	xnb = ifp->if_softc;
23467e949c46SKenneth D. Merry 	rxb = &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring;
23477e949c46SKenneth D. Merry 
23487e949c46SKenneth D. Merry 	if (!xnb->carrier)
23497e949c46SKenneth D. Merry 		return;
23507e949c46SKenneth D. Merry 
23517e949c46SKenneth D. Merry 	do {
23527e949c46SKenneth D. Merry 		int out_of_space = 0;
23537e949c46SKenneth D. Merry 		int notify;
23547e949c46SKenneth D. Merry 		req_prod_local = rxb->sring->req_prod;
23557e949c46SKenneth D. Merry 		xen_rmb();
23567e949c46SKenneth D. Merry 		for (;;) {
23577e949c46SKenneth D. Merry 			int error;
23587e949c46SKenneth D. Merry 
23597e949c46SKenneth D. Merry 			IF_DEQUEUE(&ifp->if_snd, mbufc);
23607e949c46SKenneth D. Merry 			if (mbufc == NULL)
23617e949c46SKenneth D. Merry 				break;
23627e949c46SKenneth D. Merry 			error = xnb_send(rxb, xnb->otherend_id, mbufc,
23637e949c46SKenneth D. Merry 			    		 xnb->rx_gnttab);
23647e949c46SKenneth D. Merry 			switch (error) {
23657e949c46SKenneth D. Merry 				case EAGAIN:
23667e949c46SKenneth D. Merry 					/*
23677e949c46SKenneth D. Merry 					 * Insufficient space in the ring.
23687e949c46SKenneth D. Merry 					 * Requeue pkt and send when space is
23697e949c46SKenneth D. Merry 					 * available.
23707e949c46SKenneth D. Merry 					 */
23717e949c46SKenneth D. Merry 					IF_PREPEND(&ifp->if_snd, mbufc);
23727e949c46SKenneth D. Merry 					/*
23737e949c46SKenneth D. Merry 					 * Perhaps the frontend missed an IRQ
23747e949c46SKenneth D. Merry 					 * and went to sleep.  Notify it to wake
23757e949c46SKenneth D. Merry 					 * it up.
23767e949c46SKenneth D. Merry 					 */
23777e949c46SKenneth D. Merry 					out_of_space = 1;
23787e949c46SKenneth D. Merry 					break;
23797e949c46SKenneth D. Merry 
23807e949c46SKenneth D. Merry 				case EINVAL:
23817e949c46SKenneth D. Merry 					/* OS gave a corrupt packet.  Drop it.*/
2382c8dfaf38SGleb Smirnoff 					if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
23837e949c46SKenneth D. Merry 					/* FALLTHROUGH */
23847e949c46SKenneth D. Merry 				default:
23857e949c46SKenneth D. Merry 					/* Send succeeded, or packet had error.
23867e949c46SKenneth D. Merry 					 * Free the packet */
2387c8dfaf38SGleb Smirnoff 					if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
23887e949c46SKenneth D. Merry 					if (mbufc)
23897e949c46SKenneth D. Merry 						m_freem(mbufc);
23907e949c46SKenneth D. Merry 					break;
23917e949c46SKenneth D. Merry 			}
23927e949c46SKenneth D. Merry 			if (out_of_space != 0)
23937e949c46SKenneth D. Merry 				break;
23947e949c46SKenneth D. Merry 		}
23957e949c46SKenneth D. Merry 
23967e949c46SKenneth D. Merry 		RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(rxb, notify);
23977e949c46SKenneth D. Merry 		if ((notify != 0) || (out_of_space != 0))
239876acc41fSJustin T. Gibbs 			xen_intr_signal(xnb->xen_intr_handle);
23997e949c46SKenneth D. Merry 		rxb->sring->req_event = req_prod_local + 1;
24007e949c46SKenneth D. Merry 		xen_mb();
24017e949c46SKenneth D. Merry 	} while (rxb->sring->req_prod != req_prod_local) ;
24027e949c46SKenneth D. Merry }
24037e949c46SKenneth D. Merry 
24047e949c46SKenneth D. Merry /**
24057e949c46SKenneth D. Merry  * Sends one packet to the ring.  Blocks until the packet is on the ring
24067e949c46SKenneth D. Merry  * \param[in]	mbufc	Contains one packet to send.  Caller must free
24077e949c46SKenneth D. Merry  * \param[in,out] rxb	The packet will be pushed onto this ring, but the
24087e949c46SKenneth D. Merry  * 			otherend will not be notified.
24097e949c46SKenneth D. Merry  * \param[in]	otherend The domain ID of the other end of the connection
24107e949c46SKenneth D. Merry  * \retval	EAGAIN	The ring did not have enough space for the packet.
24117e949c46SKenneth D. Merry  * 			The ring has not been modified
24127e949c46SKenneth D. Merry  * \param[in,out] gnttab Pointer to enough memory for a grant table.  We make
24137e949c46SKenneth D. Merry  * 			this a function parameter so that we will take less
24147e949c46SKenneth D. Merry  * 			stack space.
24157e949c46SKenneth D. Merry  * \retval EINVAL	mbufc was corrupt or not convertible into a pkt
24167e949c46SKenneth D. Merry  */
24177e949c46SKenneth D. Merry static int
24187e949c46SKenneth D. Merry xnb_send(netif_rx_back_ring_t *ring, domid_t otherend, const struct mbuf *mbufc,
24197e949c46SKenneth D. Merry 	 gnttab_copy_table gnttab)
24207e949c46SKenneth D. Merry {
24217e949c46SKenneth D. Merry 	struct xnb_pkt pkt;
24227e949c46SKenneth D. Merry 	int error, n_entries, n_reqs;
24237e949c46SKenneth D. Merry 	RING_IDX space;
24247e949c46SKenneth D. Merry 
24257e949c46SKenneth D. Merry 	space = ring->sring->req_prod - ring->req_cons;
24267e949c46SKenneth D. Merry 	error = xnb_mbufc2pkt(mbufc, &pkt, ring->rsp_prod_pvt, space);
24277e949c46SKenneth D. Merry 	if (error != 0)
24287e949c46SKenneth D. Merry 		return error;
24297e949c46SKenneth D. Merry 	n_entries = xnb_rxpkt2gnttab(&pkt, mbufc, gnttab, ring, otherend);
24307e949c46SKenneth D. Merry 	if (n_entries != 0) {
24317e949c46SKenneth D. Merry 		int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
24327e949c46SKenneth D. Merry 		    gnttab, n_entries);
24337e949c46SKenneth D. Merry 		KASSERT(hv_ret == 0, ("HYPERVISOR_grant_table_op returned %d\n",
24347e949c46SKenneth D. Merry 		    hv_ret));
24357e949c46SKenneth D. Merry 	}
24367e949c46SKenneth D. Merry 
24377e949c46SKenneth D. Merry 	n_reqs = xnb_rxpkt2rsp(&pkt, gnttab, n_entries, ring);
243889e0f4d2SKip Macy 
243989e0f4d2SKip Macy 	return 0;
244089e0f4d2SKip Macy }
244189e0f4d2SKip Macy 
24427e949c46SKenneth D. Merry static void
24437e949c46SKenneth D. Merry xnb_start(struct ifnet *ifp)
24447e949c46SKenneth D. Merry {
24457e949c46SKenneth D. Merry 	struct xnb_softc *xnb;
24467e949c46SKenneth D. Merry 
24477e949c46SKenneth D. Merry 	xnb = ifp->if_softc;
24487e949c46SKenneth D. Merry 	mtx_lock(&xnb->rx_lock);
24497e949c46SKenneth D. Merry 	xnb_start_locked(ifp);
24507e949c46SKenneth D. Merry 	mtx_unlock(&xnb->rx_lock);
24517e949c46SKenneth D. Merry }
24527e949c46SKenneth D. Merry 
24537e949c46SKenneth D. Merry /* equivalent of network_open() in Linux */
24547e949c46SKenneth D. Merry static void
24557e949c46SKenneth D. Merry xnb_ifinit_locked(struct xnb_softc *xnb)
24567e949c46SKenneth D. Merry {
24577e949c46SKenneth D. Merry 	struct ifnet *ifp;
24587e949c46SKenneth D. Merry 
24597e949c46SKenneth D. Merry 	ifp = xnb->xnb_ifp;
24607e949c46SKenneth D. Merry 
24617e949c46SKenneth D. Merry 	mtx_assert(&xnb->sc_lock, MA_OWNED);
24627e949c46SKenneth D. Merry 
24637e949c46SKenneth D. Merry 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
24647e949c46SKenneth D. Merry 		return;
24657e949c46SKenneth D. Merry 
24667e949c46SKenneth D. Merry 	xnb_stop(xnb);
24677e949c46SKenneth D. Merry 
24687e949c46SKenneth D. Merry 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
24697e949c46SKenneth D. Merry 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
24707e949c46SKenneth D. Merry 	if_link_state_change(ifp, LINK_STATE_UP);
24717e949c46SKenneth D. Merry }
24727e949c46SKenneth D. Merry 
24737e949c46SKenneth D. Merry 
24747e949c46SKenneth D. Merry static void
24757e949c46SKenneth D. Merry xnb_ifinit(void *xsc)
24767e949c46SKenneth D. Merry {
24777e949c46SKenneth D. Merry 	struct xnb_softc *xnb = xsc;
24787e949c46SKenneth D. Merry 
24797e949c46SKenneth D. Merry 	mtx_lock(&xnb->sc_lock);
24807e949c46SKenneth D. Merry 	xnb_ifinit_locked(xnb);
24817e949c46SKenneth D. Merry 	mtx_unlock(&xnb->sc_lock);
24827e949c46SKenneth D. Merry }
24837e949c46SKenneth D. Merry 
24847e949c46SKenneth D. Merry /**
24857e949c46SKenneth D. Merry  * Callback used by the generic networking code to tell us when our carrier
24867e949c46SKenneth D. Merry  * state has changed.  Since we don't have a physical carrier, we don't care
24877e949c46SKenneth D. Merry  */
24887e949c46SKenneth D. Merry static int
24897e949c46SKenneth D. Merry xnb_ifmedia_upd(struct ifnet *ifp)
24907e949c46SKenneth D. Merry {
24917e949c46SKenneth D. Merry 	return (0);
24927e949c46SKenneth D. Merry }
24937e949c46SKenneth D. Merry 
24947e949c46SKenneth D. Merry /**
24957e949c46SKenneth D. Merry  * Callback used by the generic networking code to ask us what our carrier
24967e949c46SKenneth D. Merry  * state is.  Since we don't have a physical carrier, this is very simple
24977e949c46SKenneth D. Merry  */
24987e949c46SKenneth D. Merry static void
24997e949c46SKenneth D. Merry xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
25007e949c46SKenneth D. Merry {
25017e949c46SKenneth D. Merry 	ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE;
25027e949c46SKenneth D. Merry 	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
25037e949c46SKenneth D. Merry }
25047e949c46SKenneth D. Merry 
25057e949c46SKenneth D. Merry 
25067e949c46SKenneth D. Merry /*---------------------------- NewBus Registration ---------------------------*/
25077e949c46SKenneth D. Merry static device_method_t xnb_methods[] = {
250889e0f4d2SKip Macy 	/* Device interface */
25097e949c46SKenneth D. Merry 	DEVMETHOD(device_probe,		xnb_probe),
25107e949c46SKenneth D. Merry 	DEVMETHOD(device_attach,	xnb_attach),
25117e949c46SKenneth D. Merry 	DEVMETHOD(device_detach,	xnb_detach),
251289e0f4d2SKip Macy 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
25137e949c46SKenneth D. Merry 	DEVMETHOD(device_suspend,	xnb_suspend),
25147e949c46SKenneth D. Merry 	DEVMETHOD(device_resume,	xnb_resume),
25157e949c46SKenneth D. Merry 
25167e949c46SKenneth D. Merry 	/* Xenbus interface */
25177e949c46SKenneth D. Merry 	DEVMETHOD(xenbus_otherend_changed, xnb_frontend_changed),
25187e949c46SKenneth D. Merry 
251989e0f4d2SKip Macy 	{ 0, 0 }
252089e0f4d2SKip Macy };
252189e0f4d2SKip Macy 
25227e949c46SKenneth D. Merry static driver_t xnb_driver = {
25237e949c46SKenneth D. Merry 	"xnb",
25247e949c46SKenneth D. Merry 	xnb_methods,
25257e949c46SKenneth D. Merry 	sizeof(struct xnb_softc),
252689e0f4d2SKip Macy };
25277e949c46SKenneth D. Merry devclass_t xnb_devclass;
252889e0f4d2SKip Macy 
25297e949c46SKenneth D. Merry DRIVER_MODULE(xnb, xenbusb_back, xnb_driver, xnb_devclass, 0, 0);
253089e0f4d2SKip Macy 
253189e0f4d2SKip Macy 
25327e949c46SKenneth D. Merry /*-------------------------- Unit Tests -------------------------------------*/
25337e949c46SKenneth D. Merry #ifdef XNB_DEBUG
25347e949c46SKenneth D. Merry #include "netback_unit_tests.c"
25357e949c46SKenneth D. Merry #endif
2536