1230587Sken/*-
2230587Sken * Copyright (c) 2009-2011 Spectra Logic Corporation
3181643Skmacy * All rights reserved.
4181643Skmacy *
5230587Sken * Redistribution and use in source and binary forms, with or without
6230587Sken * modification, are permitted provided that the following conditions
7181643Skmacy * are met:
8230587Sken * 1. Redistributions of source code must retain the above copyright
9230587Sken *    notice, this list of conditions, and the following disclaimer,
10230587Sken *    without modification.
11230587Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12230587Sken *    substantially similar to the "NO WARRANTY" disclaimer below
13230587Sken *    ("Disclaimer") and any redistribution must be conditioned upon
14230587Sken *    including a substantially similar Disclaimer requirement for further
15230587Sken *    binary redistribution.
16181643Skmacy *
17230587Sken * NO WARRANTY
18230587Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19230587Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20230587Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21230587Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22230587Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23230587Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24230587Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25230587Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26230587Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27230587Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28230587Sken * POSSIBILITY OF SUCH DAMAGES.
29181643Skmacy *
30230587Sken * Authors: Justin T. Gibbs     (Spectra Logic Corporation)
31230587Sken *          Alan Somers         (Spectra Logic Corporation)
32230587Sken *          John Suykerbuyk     (Spectra Logic Corporation)
33181643Skmacy */
34181643Skmacy
35181643Skmacy#include <sys/cdefs.h>
36181643Skmacy__FBSDID("$FreeBSD: stable/10/sys/dev/xen/netback/netback.c 319222 2017-05-30 16:15:52Z asomers $");
37230587Sken
38230587Sken/**
39230587Sken * \file netback.c
40230587Sken *
41230587Sken * \brief Device driver supporting the vending of network access
42230587Sken * 	  from this FreeBSD domain to other domains.
43230587Sken */
44230587Sken#include "opt_inet.h"
45259541Sglebius#include "opt_inet6.h"
46230587Sken#include "opt_global.h"
47230587Sken
48188066Srrs#include "opt_sctp.h"
49181643Skmacy
50181643Skmacy#include <sys/param.h>
51181643Skmacy#include <sys/kernel.h>
52181643Skmacy
53230587Sken#include <sys/bus.h>
54181643Skmacy#include <sys/module.h>
55230587Sken#include <sys/rman.h>
56230587Sken#include <sys/socket.h>
57230587Sken#include <sys/sockio.h>
58181643Skmacy#include <sys/sysctl.h>
59181643Skmacy
60181643Skmacy#include <net/if.h>
61181643Skmacy#include <net/if_arp.h>
62230587Sken#include <net/ethernet.h>
63230587Sken#include <net/if_dl.h>
64230587Sken#include <net/if_media.h>
65181643Skmacy#include <net/if_types.h>
66181643Skmacy
67181643Skmacy#include <netinet/in.h>
68181643Skmacy#include <netinet/ip.h>
69230587Sken#include <netinet/if_ether.h>
70230587Sken#if __FreeBSD_version >= 700000
71181643Skmacy#include <netinet/tcp.h>
72230587Sken#endif
73230587Sken#include <netinet/ip_icmp.h>
74181643Skmacy#include <netinet/udp.h>
75230587Sken#include <machine/in_cksum.h>
76181643Skmacy
77230587Sken#include <vm/vm.h>
78230587Sken#include <vm/pmap.h>
79230916Sken#include <vm/vm_extern.h>
80230916Sken#include <vm/vm_kern.h>
81181643Skmacy
82230587Sken#include <machine/_inttypes.h>
83181643Skmacy
84255040Sgibbs#include <xen/xen-os.h>
85255040Sgibbs#include <xen/hypervisor.h>
86230587Sken#include <xen/xen_intr.h>
87230587Sken#include <xen/interface/io/netif.h>
88230587Sken#include <xen/xenbus/xenbusvar.h>
89181643Skmacy
90255040Sgibbs#include <machine/xen/xenvar.h>
91255040Sgibbs
92230587Sken/*--------------------------- Compile-time Tunables --------------------------*/
93181643Skmacy
94230587Sken/*---------------------------------- Macros ----------------------------------*/
95230587Sken/**
96230587Sken * Custom malloc type for all driver allocations.
97230587Sken */
98230587Skenstatic MALLOC_DEFINE(M_XENNETBACK, "xnb", "Xen Net Back Driver Data");
99230587Sken
100230587Sken#define	XNB_SG	1	/* netback driver supports feature-sg */
101230587Sken#define	XNB_GSO_TCPV4 1	/* netback driver supports feature-gso-tcpv4 */
102230587Sken#define	XNB_RX_COPY 1	/* netback driver supports feature-rx-copy */
103230587Sken#define	XNB_RX_FLIP 0	/* netback driver does not support feature-rx-flip */
104230587Sken
105230587Sken#undef XNB_DEBUG
106230587Sken#define	XNB_DEBUG /* hardcode on during development */
107230587Sken
108230587Sken#ifdef XNB_DEBUG
109230587Sken#define	DPRINTF(fmt, args...) \
110230587Sken	printf("xnb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
111181643Skmacy#else
112230587Sken#define	DPRINTF(fmt, args...) do {} while (0)
113181643Skmacy#endif
114181643Skmacy
115230587Sken/* Default length for stack-allocated grant tables */
116230587Sken#define	GNTTAB_LEN	(64)
117181643Skmacy
118230587Sken/* Features supported by all backends.  TSO and LRO can be negotiated */
119230587Sken#define	XNB_CSUM_FEATURES	(CSUM_TCP | CSUM_UDP)
120181643Skmacy
121230587Sken#define	NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE)
122230587Sken#define	NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE)
123181643Skmacy
124230587Sken/**
125230587Sken * Two argument version of the standard macro.  Second argument is a tentative
126230587Sken * value of req_cons
127230587Sken */
128230587Sken#define	RING_HAS_UNCONSUMED_REQUESTS_2(_r, cons) ({                     \
129230587Sken	unsigned int req = (_r)->sring->req_prod - cons;          	\
130230587Sken	unsigned int rsp = RING_SIZE(_r) -                              \
131230587Sken	(cons - (_r)->rsp_prod_pvt);                          		\
132230587Sken	req < rsp ? req : rsp;                                          \
133230587Sken})
134181643Skmacy
135230587Sken#define	virt_to_mfn(x) (vtomach(x) >> PAGE_SHIFT)
136230587Sken#define	virt_to_offset(x) ((x) & (PAGE_SIZE - 1))
137181643Skmacy
138230587Sken/**
139230587Sken * Predefined array type of grant table copy descriptors.  Used to pass around
140230587Sken * statically allocated memory structures.
141230587Sken */
142230587Skentypedef struct gnttab_copy gnttab_copy_table[GNTTAB_LEN];
143181643Skmacy
144230587Sken/*--------------------------- Forward Declarations ---------------------------*/
145230587Skenstruct xnb_softc;
146230587Skenstruct xnb_pkt;
147181643Skmacy
148230587Skenstatic void	xnb_attach_failed(struct xnb_softc *xnb,
149230587Sken				  int err, const char *fmt, ...)
150230587Sken				  __printflike(3,4);
151230587Skenstatic int	xnb_shutdown(struct xnb_softc *xnb);
152230587Skenstatic int	create_netdev(device_t dev);
153230587Skenstatic int	xnb_detach(device_t dev);
154230587Skenstatic int	xen_net_read_mac(device_t dev, uint8_t mac[]);
155230587Skenstatic int	xnb_ifmedia_upd(struct ifnet *ifp);
156230587Skenstatic void	xnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr);
157230587Skenstatic void 	xnb_intr(void *arg);
158230587Skenstatic int	xnb_send(netif_rx_back_ring_t *rxb, domid_t otherend,
159230587Sken			 const struct mbuf *mbufc, gnttab_copy_table gnttab);
160230587Skenstatic int	xnb_recv(netif_tx_back_ring_t *txb, domid_t otherend,
161230587Sken			 struct mbuf **mbufc, struct ifnet *ifnet,
162230587Sken			 gnttab_copy_table gnttab);
163230587Skenstatic int	xnb_ring2pkt(struct xnb_pkt *pkt,
164230587Sken			     const netif_tx_back_ring_t *tx_ring,
165230587Sken			     RING_IDX start);
166230587Skenstatic void	xnb_txpkt2rsp(const struct xnb_pkt *pkt,
167230587Sken			      netif_tx_back_ring_t *ring, int error);
168230587Skenstatic struct mbuf *xnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp);
169230587Skenstatic int	xnb_txpkt2gnttab(const struct xnb_pkt *pkt,
170230587Sken				 const struct mbuf *mbufc,
171230587Sken				 gnttab_copy_table gnttab,
172230587Sken				 const netif_tx_back_ring_t *txb,
173230587Sken				 domid_t otherend_id);
174230587Skenstatic void	xnb_update_mbufc(struct mbuf *mbufc,
175230587Sken				 const gnttab_copy_table gnttab, int n_entries);
176230587Skenstatic int	xnb_mbufc2pkt(const struct mbuf *mbufc,
177230587Sken			      struct xnb_pkt *pkt,
178230587Sken			      RING_IDX start, int space);
179230587Skenstatic int	xnb_rxpkt2gnttab(const struct xnb_pkt *pkt,
180230587Sken				 const struct mbuf *mbufc,
181230587Sken				 gnttab_copy_table gnttab,
182230587Sken				 const netif_rx_back_ring_t *rxb,
183230587Sken				 domid_t otherend_id);
184230587Skenstatic int	xnb_rxpkt2rsp(const struct xnb_pkt *pkt,
185230587Sken			      const gnttab_copy_table gnttab, int n_entries,
186230587Sken			      netif_rx_back_ring_t *ring);
187230587Skenstatic void	xnb_stop(struct xnb_softc*);
188230587Skenstatic int	xnb_ioctl(struct ifnet*, u_long, caddr_t);
189230587Skenstatic void	xnb_start_locked(struct ifnet*);
190230587Skenstatic void	xnb_start(struct ifnet*);
191230587Skenstatic void	xnb_ifinit_locked(struct xnb_softc*);
192230587Skenstatic void	xnb_ifinit(void*);
193230587Sken#ifdef XNB_DEBUG
194230587Skenstatic int	xnb_unit_test_main(SYSCTL_HANDLER_ARGS);
195230587Skenstatic int	xnb_dump_rings(SYSCTL_HANDLER_ARGS);
196230587Sken#endif
197259541Sglebius#if defined(INET) || defined(INET6)
198259541Sglebiusstatic void	xnb_add_mbuf_cksum(struct mbuf *mbufc);
199259541Sglebius#endif
200230587Sken/*------------------------------ Data Structures -----------------------------*/
201181643Skmacy
202181643Skmacy
203230587Sken/**
204230587Sken * Representation of a xennet packet.  Simplified version of a packet as
205230587Sken * stored in the Xen tx ring.  Applicable to both RX and TX packets
206230587Sken */
207230587Skenstruct xnb_pkt{
208230587Sken	/**
209230587Sken	 * Array index of the first data-bearing (eg, not extra info) entry
210230587Sken	 * for this packet
211230587Sken	 */
212230587Sken	RING_IDX	car;
213181643Skmacy
214230587Sken	/**
215230587Sken	 * Array index of the second data-bearing entry for this packet.
216230587Sken	 * Invalid if the packet has only one data-bearing entry.  If the
217230587Sken	 * packet has more than two data-bearing entries, then the second
218230587Sken	 * through the last will be sequential modulo the ring size
219230587Sken	 */
220230587Sken	RING_IDX	cdr;
221181643Skmacy
222230587Sken	/**
223230587Sken	 * Optional extra info.  Only valid if flags contains
224230587Sken	 * NETTXF_extra_info.  Note that extra.type will always be
225230587Sken	 * XEN_NETIF_EXTRA_TYPE_GSO.  Currently, no known netfront or netback
226230587Sken	 * driver will ever set XEN_NETIF_EXTRA_TYPE_MCAST_*
227230587Sken	 */
228230587Sken	netif_extra_info_t extra;
229181643Skmacy
230230587Sken	/** Size of entire packet in bytes.       */
231230587Sken	uint16_t	size;
232181643Skmacy
233230587Sken	/** The size of the first entry's data in bytes */
234230587Sken	uint16_t	car_size;
235181643Skmacy
236230587Sken	/**
237230587Sken	 * Either NETTXF_ or NETRXF_ flags.  Note that the flag values are
238230587Sken	 * not the same for TX and RX packets
239230587Sken	 */
240230587Sken	uint16_t	flags;
241181643Skmacy
242230587Sken	/**
243230587Sken	 * The number of valid data-bearing entries (either netif_tx_request's
244230587Sken	 * or netif_rx_response's) in the packet.  If this is 0, it means the
245230587Sken	 * entire packet is invalid.
246230587Sken	 */
247230587Sken	uint16_t	list_len;
248181643Skmacy
249230587Sken	/** There was an error processing the packet */
250230587Sken	uint8_t		error;
251230587Sken};
252181643Skmacy
253230587Sken/** xnb_pkt method: initialize it */
254230587Skenstatic inline void
255230587Skenxnb_pkt_initialize(struct xnb_pkt *pxnb)
256230587Sken{
257230587Sken	bzero(pxnb, sizeof(*pxnb));
258230587Sken}
259181643Skmacy
260230587Sken/** xnb_pkt method: mark the packet as valid */
261230587Skenstatic inline void
262230587Skenxnb_pkt_validate(struct xnb_pkt *pxnb)
263230587Sken{
264230587Sken	pxnb->error = 0;
265230587Sken};
266181643Skmacy
267230587Sken/** xnb_pkt method: mark the packet as invalid */
268230587Skenstatic inline void
269230587Skenxnb_pkt_invalidate(struct xnb_pkt *pxnb)
270230587Sken{
271230587Sken	pxnb->error = 1;
272230587Sken};
273181643Skmacy
274230587Sken/** xnb_pkt method: Check whether the packet is valid */
275230587Skenstatic inline int
276230587Skenxnb_pkt_is_valid(const struct xnb_pkt *pxnb)
277230587Sken{
278230587Sken	return (! pxnb->error);
279230587Sken}
280181643Skmacy
281230587Sken#ifdef XNB_DEBUG
282230587Sken/** xnb_pkt method: print the packet's contents in human-readable format*/
283230587Skenstatic void __unused
284230587Skenxnb_dump_pkt(const struct xnb_pkt *pkt) {
285230587Sken	if (pkt == NULL) {
286230587Sken	  DPRINTF("Was passed a null pointer.\n");
287230587Sken	  return;
288230587Sken	}
289230587Sken	DPRINTF("pkt address= %p\n", pkt);
290230587Sken	DPRINTF("pkt->size=%d\n", pkt->size);
291230587Sken	DPRINTF("pkt->car_size=%d\n", pkt->car_size);
292230587Sken	DPRINTF("pkt->flags=0x%04x\n", pkt->flags);
293230587Sken	DPRINTF("pkt->list_len=%d\n", pkt->list_len);
294230587Sken	/* DPRINTF("pkt->extra");	TODO */
295230587Sken	DPRINTF("pkt->car=%d\n", pkt->car);
296230587Sken	DPRINTF("pkt->cdr=%d\n", pkt->cdr);
297230587Sken	DPRINTF("pkt->error=%d\n", pkt->error);
298230587Sken}
299230587Sken#endif /* XNB_DEBUG */
300181643Skmacy
301181643Skmacystatic void
302230587Skenxnb_dump_txreq(RING_IDX idx, const struct netif_tx_request *txreq)
303181643Skmacy{
304230587Sken	if (txreq != NULL) {
305230587Sken		DPRINTF("netif_tx_request index =%u\n", idx);
306230587Sken		DPRINTF("netif_tx_request.gref  =%u\n", txreq->gref);
307230587Sken		DPRINTF("netif_tx_request.offset=%hu\n", txreq->offset);
308230587Sken		DPRINTF("netif_tx_request.flags =%hu\n", txreq->flags);
309230587Sken		DPRINTF("netif_tx_request.id    =%hu\n", txreq->id);
310230587Sken		DPRINTF("netif_tx_request.size  =%hu\n", txreq->size);
311181643Skmacy	}
312181643Skmacy}
313181643Skmacy
314181643Skmacy
315230587Sken/**
316230587Sken * \brief Configuration data for a shared memory request ring
317230587Sken *        used to communicate with the front-end client of this
318230587Sken *        this driver.
319230587Sken */
320230587Skenstruct xnb_ring_config {
321230587Sken	/**
322230587Sken	 * Runtime structures for ring access.  Unfortunately, TX and RX rings
323230587Sken	 * use different data structures, and that cannot be changed since it
324230587Sken	 * is part of the interdomain protocol.
325230587Sken	 */
326230587Sken	union{
327230587Sken		netif_rx_back_ring_t	  rx_ring;
328230587Sken		netif_tx_back_ring_t	  tx_ring;
329230587Sken	} back_ring;
330181643Skmacy
331230587Sken	/**
332230587Sken	 * The device bus address returned by the hypervisor when
333230587Sken	 * mapping the ring and required to unmap it when a connection
334230587Sken	 * is torn down.
335230587Sken	 */
336230587Sken	uint64_t	bus_addr;
337181643Skmacy
338230587Sken	/** The pseudo-physical address where ring memory is mapped.*/
339230587Sken	uint64_t	gnt_addr;
340230587Sken
341230587Sken	/** KVA address where ring memory is mapped. */
342230587Sken	vm_offset_t	va;
343230587Sken
344230587Sken	/**
345230587Sken	 * Grant table handles, one per-ring page, returned by the
346230587Sken	 * hyperpervisor upon mapping of the ring and required to
347230587Sken	 * unmap it when a connection is torn down.
348230587Sken	 */
349230587Sken	grant_handle_t	handle;
350230587Sken
351230587Sken	/** The number of ring pages mapped for the current connection. */
352230587Sken	unsigned	ring_pages;
353230587Sken
354230587Sken	/**
355230587Sken	 * The grant references, one per-ring page, supplied by the
356230587Sken	 * front-end, allowing us to reference the ring pages in the
357230587Sken	 * front-end's domain and to map these pages into our own domain.
358230587Sken	 */
359230587Sken	grant_ref_t	ring_ref;
360230587Sken};
361230587Sken
362230587Sken/**
363230587Sken * Per-instance connection state flags.
364230587Sken */
365230587Skentypedef enum
366181643Skmacy{
367230587Sken	/** Communication with the front-end has been established. */
368230587Sken	XNBF_RING_CONNECTED    = 0x01,
369181643Skmacy
370230587Sken	/**
371230587Sken	 * Front-end requests exist in the ring and are waiting for
372230587Sken	 * xnb_xen_req objects to free up.
373230587Sken	 */
374230587Sken	XNBF_RESOURCE_SHORTAGE = 0x02,
375181643Skmacy
376230587Sken	/** Connection teardown has started. */
377230587Sken	XNBF_SHUTDOWN          = 0x04,
378181643Skmacy
379230587Sken	/** A thread is already performing shutdown processing. */
380230587Sken	XNBF_IN_SHUTDOWN       = 0x08
381230587Sken} xnb_flag_t;
382181643Skmacy
383230587Sken/**
384230587Sken * Types of rings.  Used for array indices and to identify a ring's control
385230587Sken * data structure type
386230587Sken */
387230587Skentypedef enum{
388230587Sken	XNB_RING_TYPE_TX = 0,	/* ID of TX rings, used for array indices */
389230587Sken	XNB_RING_TYPE_RX = 1,	/* ID of RX rings, used for array indices */
390230587Sken	XNB_NUM_RING_TYPES
391230587Sken} xnb_ring_type_t;
392181643Skmacy
393230587Sken/**
394230587Sken * Per-instance configuration data.
395230587Sken */
396230587Skenstruct xnb_softc {
397230587Sken	/** NewBus device corresponding to this instance. */
398230587Sken	device_t		dev;
399181643Skmacy
400230587Sken	/* Media related fields */
401181643Skmacy
402230587Sken	/** Generic network media state */
403230587Sken	struct ifmedia		sc_media;
404181643Skmacy
405230587Sken	/** Media carrier info */
406230587Sken	struct ifnet 		*xnb_ifp;
407181643Skmacy
408230587Sken	/** Our own private carrier state */
409230587Sken	unsigned carrier;
410181643Skmacy
411230587Sken	/** Device MAC Address */
412230587Sken	uint8_t			mac[ETHER_ADDR_LEN];
413181643Skmacy
414230587Sken	/* Xen related fields */
415181643Skmacy
416230587Sken	/**
417230587Sken	 * \brief The netif protocol abi in effect.
418230587Sken	 *
419230587Sken	 * There are situations where the back and front ends can
420230587Sken	 * have a different, native abi (e.g. intel x86_64 and
421230587Sken	 * 32bit x86 domains on the same machine).  The back-end
422230587Sken	 * always accomodates the front-end's native abi.  That
423230587Sken	 * value is pulled from the XenStore and recorded here.
424230587Sken	 */
425230587Sken	int			abi;
426181643Skmacy
427230587Sken	/**
428230587Sken	 * Name of the bridge to which this VIF is connected, if any
429230587Sken	 * This field is dynamically allocated by xenbus and must be free()ed
430230587Sken	 * when no longer needed
431230587Sken	 */
432230587Sken	char			*bridge;
433181643Skmacy
434230587Sken	/** The interrupt driven even channel used to signal ring events. */
435230587Sken	evtchn_port_t		evtchn;
436181643Skmacy
437230587Sken	/** Xen device handle.*/
438230587Sken	long 			handle;
439181643Skmacy
440255040Sgibbs	/** Handle to the communication ring event channel. */
441255040Sgibbs	xen_intr_handle_t	xen_intr_handle;
442181643Skmacy
443230587Sken	/**
444230587Sken	 * \brief Cached value of the front-end's domain id.
445230587Sken	 *
446230587Sken	 * This value is used at once for each mapped page in
447230587Sken	 * a transaction.  We cache it to avoid incuring the
448230587Sken	 * cost of an ivar access every time this is needed.
449230587Sken	 */
450230587Sken	domid_t			otherend_id;
451181643Skmacy
452230587Sken	/**
453230587Sken	 * Undocumented frontend feature.  Has something to do with
454230587Sken	 * scatter/gather IO
455230587Sken	 */
456230587Sken	uint8_t			can_sg;
457230587Sken	/** Undocumented frontend feature */
458230587Sken	uint8_t			gso;
459230587Sken	/** Undocumented frontend feature */
460230587Sken	uint8_t			gso_prefix;
461230587Sken	/** Can checksum TCP/UDP over IPv4 */
462230587Sken	uint8_t			ip_csum;
463181643Skmacy
464230587Sken	/* Implementation related fields */
465230587Sken	/**
466230587Sken	 * Preallocated grant table copy descriptor for RX operations.
467230587Sken	 * Access must be protected by rx_lock
468230587Sken	 */
469230587Sken	gnttab_copy_table	rx_gnttab;
470181643Skmacy
471230587Sken	/**
472230587Sken	 * Preallocated grant table copy descriptor for TX operations.
473230587Sken	 * Access must be protected by tx_lock
474230587Sken	 */
475230587Sken	gnttab_copy_table	tx_gnttab;
476181643Skmacy
477230587Sken#ifdef XENHVM
478230587Sken	/**
479230587Sken	 * Resource representing allocated physical address space
480230587Sken	 * associated with our per-instance kva region.
481230587Sken	 */
482230587Sken	struct resource		*pseudo_phys_res;
483181643Skmacy
484230587Sken	/** Resource id for allocated physical address space. */
485230587Sken	int			pseudo_phys_res_id;
486230587Sken#endif
487181643Skmacy
488230587Sken	/** Ring mapping and interrupt configuration data. */
489230587Sken	struct xnb_ring_config	ring_configs[XNB_NUM_RING_TYPES];
490181643Skmacy
491230587Sken	/**
492230587Sken	 * Global pool of kva used for mapping remote domain ring
493230587Sken	 * and I/O transaction data.
494230587Sken	 */
495230587Sken	vm_offset_t		kva;
496181643Skmacy
497230587Sken	/** Psuedo-physical address corresponding to kva. */
498230587Sken	uint64_t		gnt_base_addr;
499181643Skmacy
500230587Sken	/** Various configuration and state bit flags. */
501230587Sken	xnb_flag_t		flags;
502230587Sken
503230587Sken	/** Mutex protecting per-instance data in the receive path. */
504230587Sken	struct mtx		rx_lock;
505230587Sken
506230587Sken	/** Mutex protecting per-instance data in the softc structure. */
507230587Sken	struct mtx		sc_lock;
508230587Sken
509230587Sken	/** Mutex protecting per-instance data in the transmit path. */
510230587Sken	struct mtx		tx_lock;
511230587Sken
512230587Sken	/** The size of the global kva pool. */
513230587Sken	int			kva_size;
514230587Sken};
515230587Sken
516230587Sken/*---------------------------- Debugging functions ---------------------------*/
517230587Sken#ifdef XNB_DEBUG
518230587Skenstatic void __unused
519230587Skenxnb_dump_gnttab_copy(const struct gnttab_copy *entry)
520181643Skmacy{
521230587Sken	if (entry == NULL) {
522230587Sken		printf("NULL grant table pointer\n");
523230587Sken		return;
524181643Skmacy	}
525230587Sken
526230587Sken	if (entry->flags & GNTCOPY_dest_gref)
527230587Sken		printf("gnttab dest ref=\t%u\n", entry->dest.u.ref);
528230587Sken	else
529230587Sken		printf("gnttab dest gmfn=\t%lu\n", entry->dest.u.gmfn);
530230587Sken	printf("gnttab dest offset=\t%hu\n", entry->dest.offset);
531230587Sken	printf("gnttab dest domid=\t%hu\n", entry->dest.domid);
532230587Sken	if (entry->flags & GNTCOPY_source_gref)
533230587Sken		printf("gnttab source ref=\t%u\n", entry->source.u.ref);
534230587Sken	else
535230587Sken		printf("gnttab source gmfn=\t%lu\n", entry->source.u.gmfn);
536230587Sken	printf("gnttab source offset=\t%hu\n", entry->source.offset);
537230587Sken	printf("gnttab source domid=\t%hu\n", entry->source.domid);
538230587Sken	printf("gnttab len=\t%hu\n", entry->len);
539230587Sken	printf("gnttab flags=\t%hu\n", entry->flags);
540230587Sken	printf("gnttab status=\t%hd\n", entry->status);
541181643Skmacy}
542181643Skmacy
543181643Skmacystatic int
544230587Skenxnb_dump_rings(SYSCTL_HANDLER_ARGS)
545181643Skmacy{
546230587Sken	static char results[720];
547230587Sken	struct xnb_softc const* xnb = (struct xnb_softc*)arg1;
548230587Sken	netif_rx_back_ring_t const* rxb =
549230587Sken		&xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring;
550230587Sken	netif_tx_back_ring_t const* txb =
551230587Sken		&xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring;
552181643Skmacy
553230587Sken	/* empty the result strings */
554230587Sken	results[0] = 0;
555181643Skmacy
556230587Sken	if ( !txb || !txb->sring || !rxb || !rxb->sring )
557230587Sken		return (SYSCTL_OUT(req, results, strnlen(results, 720)));
558181643Skmacy
559230587Sken	snprintf(results, 720,
560230587Sken	    "\n\t%35s %18s\n"	/* TX, RX */
561230587Sken	    "\t%16s %18d %18d\n"	/* req_cons */
562230587Sken	    "\t%16s %18d %18d\n"	/* nr_ents */
563230587Sken	    "\t%16s %18d %18d\n"	/* rsp_prod_pvt */
564230587Sken	    "\t%16s %18p %18p\n"	/* sring */
565230587Sken	    "\t%16s %18d %18d\n"	/* req_prod */
566230587Sken	    "\t%16s %18d %18d\n"	/* req_event */
567230587Sken	    "\t%16s %18d %18d\n"	/* rsp_prod */
568230587Sken	    "\t%16s %18d %18d\n",	/* rsp_event */
569230587Sken	    "TX", "RX",
570230587Sken	    "req_cons", txb->req_cons, rxb->req_cons,
571230587Sken	    "nr_ents", txb->nr_ents, rxb->nr_ents,
572230587Sken	    "rsp_prod_pvt", txb->rsp_prod_pvt, rxb->rsp_prod_pvt,
573230587Sken	    "sring", txb->sring, rxb->sring,
574230587Sken	    "sring->req_prod", txb->sring->req_prod, rxb->sring->req_prod,
575230587Sken	    "sring->req_event", txb->sring->req_event, rxb->sring->req_event,
576230587Sken	    "sring->rsp_prod", txb->sring->rsp_prod, rxb->sring->rsp_prod,
577230587Sken	    "sring->rsp_event", txb->sring->rsp_event, rxb->sring->rsp_event);
578230587Sken
579230587Sken	return (SYSCTL_OUT(req, results, strnlen(results, 720)));
580181643Skmacy}
581181643Skmacy
582230587Skenstatic void __unused
583230587Skenxnb_dump_mbuf(const struct mbuf *m)
584181643Skmacy{
585230587Sken	int len;
586230587Sken	uint8_t *d;
587230587Sken	if (m == NULL)
588230587Sken		return;
589181643Skmacy
590230587Sken	printf("xnb_dump_mbuf:\n");
591230587Sken	if (m->m_flags & M_PKTHDR) {
592230587Sken		printf("    flowid=%10d, csum_flags=%#8x, csum_data=%#8x, "
593230587Sken		       "tso_segsz=%5hd\n",
594254910Sandre		       m->m_pkthdr.flowid, (int)m->m_pkthdr.csum_flags,
595230587Sken		       m->m_pkthdr.csum_data, m->m_pkthdr.tso_segsz);
596254910Sandre		printf("    rcvif=%16p,  len=%19d\n",
597254910Sandre		       m->m_pkthdr.rcvif, m->m_pkthdr.len);
598230587Sken	}
599230587Sken	printf("    m_next=%16p, m_nextpk=%16p, m_data=%16p\n",
600230587Sken	       m->m_next, m->m_nextpkt, m->m_data);
601254910Sandre	printf("    m_len=%17d, m_flags=%#15x, m_type=%18u\n",
602230587Sken	       m->m_len, m->m_flags, m->m_type);
603181643Skmacy
604230587Sken	len = m->m_len;
605230587Sken	d = mtod(m, uint8_t*);
606230587Sken	while (len > 0) {
607230587Sken		int i;
608230587Sken		printf("                ");
609230587Sken		for (i = 0; (i < 16) && (len > 0); i++, len--) {
610230587Sken			printf("%02hhx ", *(d++));
611230587Sken		}
612230587Sken		printf("\n");
613181643Skmacy	}
614181643Skmacy}
615230587Sken#endif /* XNB_DEBUG */
616181643Skmacy
617230587Sken/*------------------------ Inter-Domain Communication ------------------------*/
618230587Sken/**
619230587Sken * Free dynamically allocated KVA or pseudo-physical address allocations.
620230587Sken *
621230587Sken * \param xnb  Per-instance xnb configuration structure.
622230587Sken */
623181643Skmacystatic void
624230587Skenxnb_free_communication_mem(struct xnb_softc *xnb)
625181643Skmacy{
626230587Sken	if (xnb->kva != 0) {
627230587Sken#ifndef XENHVM
628254025Sjeff		kva_free(xnb->kva, xnb->kva_size);
629230587Sken#else
630230587Sken		if (xnb->pseudo_phys_res != NULL) {
631230587Sken			bus_release_resource(xnb->dev, SYS_RES_MEMORY,
632230587Sken			    xnb->pseudo_phys_res_id,
633230587Sken			    xnb->pseudo_phys_res);
634230587Sken			xnb->pseudo_phys_res = NULL;
635230587Sken		}
636230587Sken#endif /* XENHVM */
637181643Skmacy	}
638230587Sken	xnb->kva = 0;
639230587Sken	xnb->gnt_base_addr = 0;
640181643Skmacy}
641181643Skmacy
642230587Sken/**
643230587Sken * Cleanup all inter-domain communication mechanisms.
644230587Sken *
645230587Sken * \param xnb  Per-instance xnb configuration structure.
646181643Skmacy */
647230587Skenstatic int
648230587Skenxnb_disconnect(struct xnb_softc *xnb)
649181643Skmacy{
650230587Sken	struct gnttab_unmap_grant_ref gnts[XNB_NUM_RING_TYPES];
651230587Sken	int error;
652230587Sken	int i;
653181643Skmacy
654255040Sgibbs	xen_intr_unbind(xnb->xen_intr_handle);
655181643Skmacy
656230587Sken	/*
657230587Sken	 * We may still have another thread currently processing requests.  We
658230587Sken	 * must acquire the rx and tx locks to make sure those threads are done,
659230587Sken	 * but we can release those locks as soon as we acquire them, because no
660230587Sken	 * more interrupts will be arriving.
661230587Sken	 */
662230587Sken	mtx_lock(&xnb->tx_lock);
663230587Sken	mtx_unlock(&xnb->tx_lock);
664230587Sken	mtx_lock(&xnb->rx_lock);
665230587Sken	mtx_unlock(&xnb->rx_lock);
666230587Sken
667230587Sken	/* Free malloc'd softc member variables */
668230587Sken	if (xnb->bridge != NULL)
669230587Sken		free(xnb->bridge, M_XENSTORE);
670230587Sken
671230587Sken	/* All request processing has stopped, so unmap the rings */
672230587Sken	for (i=0; i < XNB_NUM_RING_TYPES; i++) {
673230587Sken		gnts[i].host_addr = xnb->ring_configs[i].gnt_addr;
674230587Sken		gnts[i].dev_bus_addr = xnb->ring_configs[i].bus_addr;
675230587Sken		gnts[i].handle = xnb->ring_configs[i].handle;
676181643Skmacy	}
677230587Sken	error = HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, gnts,
678230587Sken					  XNB_NUM_RING_TYPES);
679230587Sken	KASSERT(error == 0, ("Grant table unmap op failed (%d)", error));
680181643Skmacy
681230587Sken	xnb_free_communication_mem(xnb);
682230587Sken	/*
683230587Sken	 * Zero the ring config structs because the pointers, handles, and
684230587Sken	 * grant refs contained therein are no longer valid.
685230587Sken	 */
686230587Sken	bzero(&xnb->ring_configs[XNB_RING_TYPE_TX],
687230587Sken	    sizeof(struct xnb_ring_config));
688230587Sken	bzero(&xnb->ring_configs[XNB_RING_TYPE_RX],
689230587Sken	    sizeof(struct xnb_ring_config));
690181643Skmacy
691230587Sken	xnb->flags &= ~XNBF_RING_CONNECTED;
692230587Sken	return (0);
693181643Skmacy}
694181643Skmacy
695230587Sken/**
696230587Sken * Map a single shared memory ring into domain local address space and
697230587Sken * initialize its control structure
698230587Sken *
699230587Sken * \param xnb	Per-instance xnb configuration structure
700230587Sken * \param ring_type	Array index of this ring in the xnb's array of rings
701230587Sken * \return 	An errno
702230587Sken */
703230587Skenstatic int
704230587Skenxnb_connect_ring(struct xnb_softc *xnb, xnb_ring_type_t ring_type)
705181643Skmacy{
706230587Sken	struct gnttab_map_grant_ref gnt;
707230587Sken	struct xnb_ring_config *ring = &xnb->ring_configs[ring_type];
708230587Sken	int error;
709181643Skmacy
710230587Sken	/* TX ring type = 0, RX =1 */
711230587Sken	ring->va = xnb->kva + ring_type * PAGE_SIZE;
712230587Sken	ring->gnt_addr = xnb->gnt_base_addr + ring_type * PAGE_SIZE;
713181643Skmacy
714230587Sken	gnt.host_addr = ring->gnt_addr;
715230587Sken	gnt.flags     = GNTMAP_host_map;
716230587Sken	gnt.ref       = ring->ring_ref;
717230587Sken	gnt.dom       = xnb->otherend_id;
718181643Skmacy
719230587Sken	error = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &gnt, 1);
720230587Sken	if (error != 0)
721230587Sken		panic("netback: Ring page grant table op failed (%d)", error);
722230587Sken
723230587Sken	if (gnt.status != 0) {
724230587Sken		ring->va = 0;
725230587Sken		error = EACCES;
726230587Sken		xenbus_dev_fatal(xnb->dev, error,
727230587Sken				 "Ring shared page mapping failed. "
728230587Sken				 "Status %d.", gnt.status);
729230587Sken	} else {
730230587Sken		ring->handle = gnt.handle;
731230587Sken		ring->bus_addr = gnt.dev_bus_addr;
732230587Sken
733230587Sken		if (ring_type == XNB_RING_TYPE_TX) {
734230587Sken			BACK_RING_INIT(&ring->back_ring.tx_ring,
735230587Sken			    (netif_tx_sring_t*)ring->va,
736230587Sken			    ring->ring_pages * PAGE_SIZE);
737230587Sken		} else if (ring_type == XNB_RING_TYPE_RX) {
738230587Sken			BACK_RING_INIT(&ring->back_ring.rx_ring,
739230587Sken			    (netif_rx_sring_t*)ring->va,
740230587Sken			    ring->ring_pages * PAGE_SIZE);
741230587Sken		} else {
742230587Sken			xenbus_dev_fatal(xnb->dev, error,
743230587Sken				 "Unknown ring type %d", ring_type);
744230587Sken		}
745181643Skmacy	}
746230587Sken
747230587Sken	return error;
748181643Skmacy}
749181643Skmacy
750230587Sken/**
751230587Sken * Setup the shared memory rings and bind an interrupt to the event channel
752230587Sken * used to notify us of ring changes.
753230587Sken *
754230587Sken * \param xnb  Per-instance xnb configuration structure.
755230587Sken */
756230587Skenstatic int
757230587Skenxnb_connect_comms(struct xnb_softc *xnb)
758181643Skmacy{
759230587Sken	int	error;
760230587Sken	xnb_ring_type_t i;
761181643Skmacy
762230587Sken	if ((xnb->flags & XNBF_RING_CONNECTED) != 0)
763230587Sken		return (0);
764181643Skmacy
765181643Skmacy	/*
766230587Sken	 * Kva for our rings are at the tail of the region of kva allocated
767230587Sken	 * by xnb_alloc_communication_mem().
768181643Skmacy	 */
769230587Sken	for (i=0; i < XNB_NUM_RING_TYPES; i++) {
770230587Sken		error = xnb_connect_ring(xnb, i);
771230587Sken		if (error != 0)
772230587Sken	  		return error;
773181643Skmacy	}
774181643Skmacy
775230587Sken	xnb->flags |= XNBF_RING_CONNECTED;
776181643Skmacy
777255040Sgibbs	error = xen_intr_bind_remote_port(xnb->dev,
778255040Sgibbs					  xnb->otherend_id,
779255040Sgibbs					  xnb->evtchn,
780255040Sgibbs					  /*filter*/NULL,
781255040Sgibbs					  xnb_intr, /*arg*/xnb,
782255040Sgibbs					  INTR_TYPE_BIO | INTR_MPSAFE,
783255040Sgibbs					  &xnb->xen_intr_handle);
784230587Sken	if (error != 0) {
785230587Sken		(void)xnb_disconnect(xnb);
786230587Sken		xenbus_dev_fatal(xnb->dev, error, "binding event channel");
787230587Sken		return (error);
788230587Sken	}
789181643Skmacy
790230587Sken	DPRINTF("rings connected!\n");
791181643Skmacy
792230587Sken	return (0);
793181643Skmacy}
794181643Skmacy
795230587Sken/**
796230587Sken * Size KVA and pseudo-physical address allocations based on negotiated
797230587Sken * values for the size and number of I/O requests, and the size of our
798230587Sken * communication ring.
799230587Sken *
800230587Sken * \param xnb  Per-instance xnb configuration structure.
801230587Sken *
802230587Sken * These address spaces are used to dynamically map pages in the
803230587Sken * front-end's domain into our own.
804230587Sken */
805230587Skenstatic int
806230587Skenxnb_alloc_communication_mem(struct xnb_softc *xnb)
807181643Skmacy{
808230587Sken	xnb_ring_type_t i;
809181643Skmacy
810230587Sken	xnb->kva_size = 0;
811230587Sken	for (i=0; i < XNB_NUM_RING_TYPES; i++) {
812230587Sken		xnb->kva_size += xnb->ring_configs[i].ring_pages * PAGE_SIZE;
813230587Sken	}
814230587Sken#ifndef XENHVM
815254025Sjeff	xnb->kva = kva_alloc(xnb->kva_size);
816230587Sken	if (xnb->kva == 0)
817230587Sken		return (ENOMEM);
818230587Sken	xnb->gnt_base_addr = xnb->kva;
819230587Sken#else /* defined XENHVM */
820230587Sken	/*
821230587Sken	 * Reserve a range of pseudo physical memory that we can map
822230587Sken	 * into kva.  These pages will only be backed by machine
823230587Sken	 * pages ("real memory") during the lifetime of front-end requests
824230587Sken	 * via grant table operations.  We will map the netif tx and rx rings
825230587Sken	 * into this space.
826230587Sken	 */
827230587Sken	xnb->pseudo_phys_res_id = 0;
828230587Sken	xnb->pseudo_phys_res = bus_alloc_resource(xnb->dev, SYS_RES_MEMORY,
829230587Sken						  &xnb->pseudo_phys_res_id,
830230587Sken						  0, ~0, xnb->kva_size,
831230587Sken						  RF_ACTIVE);
832230587Sken	if (xnb->pseudo_phys_res == NULL) {
833230587Sken		xnb->kva = 0;
834230587Sken		return (ENOMEM);
835230587Sken	}
836230587Sken	xnb->kva = (vm_offset_t)rman_get_virtual(xnb->pseudo_phys_res);
837230587Sken	xnb->gnt_base_addr = rman_get_start(xnb->pseudo_phys_res);
838230587Sken#endif /* !defined XENHVM */
839230587Sken	return (0);
840181643Skmacy}
841181643Skmacy
842230587Sken/**
843230587Sken * Collect information from the XenStore related to our device and its frontend
844230587Sken *
845230587Sken * \param xnb  Per-instance xnb configuration structure.
846230587Sken */
847230587Skenstatic int
848230587Skenxnb_collect_xenstore_info(struct xnb_softc *xnb)
849181643Skmacy{
850230587Sken	/**
851230587Sken	 * \todo Linux collects the following info.  We should collect most
852230587Sken	 * of this, too:
853230587Sken	 * "feature-rx-notify"
854230587Sken	 */
855230587Sken	const char *otherend_path;
856230587Sken	const char *our_path;
857230587Sken	int err;
858230587Sken	unsigned int rx_copy, bridge_len;
859230587Sken	uint8_t no_csum_offload;
860181643Skmacy
861230587Sken	otherend_path = xenbus_get_otherend_path(xnb->dev);
862230587Sken	our_path = xenbus_get_node(xnb->dev);
863181643Skmacy
864230587Sken	/* Collect the critical communication parameters */
865230587Sken	err = xs_gather(XST_NIL, otherend_path,
866230587Sken	    "tx-ring-ref", "%l" PRIu32,
867230587Sken	    	&xnb->ring_configs[XNB_RING_TYPE_TX].ring_ref,
868230587Sken	    "rx-ring-ref", "%l" PRIu32,
869230587Sken	    	&xnb->ring_configs[XNB_RING_TYPE_RX].ring_ref,
870230587Sken	    "event-channel", "%" PRIu32, &xnb->evtchn,
871230587Sken	    NULL);
872230587Sken	if (err != 0) {
873230587Sken		xenbus_dev_fatal(xnb->dev, err,
874230587Sken				 "Unable to retrieve ring information from "
875230587Sken				 "frontend %s.  Unable to connect.",
876230587Sken				 otherend_path);
877230587Sken		return (err);
878230587Sken	}
879181643Skmacy
880230587Sken	/* Collect the handle from xenstore */
881230587Sken	err = xs_scanf(XST_NIL, our_path, "handle", NULL, "%li", &xnb->handle);
882230587Sken	if (err != 0) {
883230587Sken		xenbus_dev_fatal(xnb->dev, err,
884230587Sken		    "Error reading handle from frontend %s.  "
885230587Sken		    "Unable to connect.", otherend_path);
886230587Sken	}
887181643Skmacy
888230587Sken	/*
889230587Sken	 * Collect the bridgename, if any.  We do not need bridge_len; we just
890230587Sken	 * throw it away
891230587Sken	 */
892230587Sken	err = xs_read(XST_NIL, our_path, "bridge", &bridge_len,
893230587Sken		      (void**)&xnb->bridge);
894230587Sken	if (err != 0)
895230587Sken		xnb->bridge = NULL;
896181643Skmacy
897230587Sken	/*
898230587Sken	 * Does the frontend request that we use rx copy?  If not, return an
899230587Sken	 * error because this driver only supports rx copy.
900230587Sken	 */
901230587Sken	err = xs_scanf(XST_NIL, otherend_path, "request-rx-copy", NULL,
902230587Sken		       "%" PRIu32, &rx_copy);
903230587Sken	if (err == ENOENT) {
904230587Sken		err = 0;
905230587Sken	 	rx_copy = 0;
906230587Sken	}
907230587Sken	if (err < 0) {
908230587Sken		xenbus_dev_fatal(xnb->dev, err, "reading %s/request-rx-copy",
909230587Sken				 otherend_path);
910230587Sken		return err;
911230587Sken	}
912230587Sken	/**
913230587Sken	 * \todo: figure out the exact meaning of this feature, and when
914230587Sken	 * the frontend will set it to true.  It should be set to true
915230587Sken	 * at some point
916230587Sken	 */
917230587Sken/*        if (!rx_copy)*/
918230587Sken/*          return EOPNOTSUPP;*/
919181643Skmacy
920230587Sken	/** \todo Collect the rx notify feature */
921181643Skmacy
922230587Sken	/*  Collect the feature-sg. */
923230587Sken	if (xs_scanf(XST_NIL, otherend_path, "feature-sg", NULL,
924230587Sken		     "%hhu", &xnb->can_sg) < 0)
925230587Sken		xnb->can_sg = 0;
926181643Skmacy
927230587Sken	/* Collect remaining frontend features */
928230587Sken	if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4", NULL,
929230587Sken		     "%hhu", &xnb->gso) < 0)
930230587Sken		xnb->gso = 0;
931181643Skmacy
932230587Sken	if (xs_scanf(XST_NIL, otherend_path, "feature-gso-tcpv4-prefix", NULL,
933230587Sken		     "%hhu", &xnb->gso_prefix) < 0)
934230587Sken		xnb->gso_prefix = 0;
935181643Skmacy
936230587Sken	if (xs_scanf(XST_NIL, otherend_path, "feature-no-csum-offload", NULL,
937230587Sken		     "%hhu", &no_csum_offload) < 0)
938230587Sken		no_csum_offload = 0;
939230587Sken	xnb->ip_csum = (no_csum_offload == 0);
940181643Skmacy
941230587Sken	return (0);
942230587Sken}
943230587Sken
944230587Sken/**
945230587Sken * Supply information about the physical device to the frontend
946230587Sken * via XenBus.
947230587Sken *
948230587Sken * \param xnb  Per-instance xnb configuration structure.
949230587Sken */
950230587Skenstatic int
951230587Skenxnb_publish_backend_info(struct xnb_softc *xnb)
952230587Sken{
953230587Sken	struct xs_transaction xst;
954230587Sken	const char *our_path;
955230587Sken	int error;
956230587Sken
957230587Sken	our_path = xenbus_get_node(xnb->dev);
958230587Sken
959230587Sken	do {
960230587Sken		error = xs_transaction_start(&xst);
961230587Sken		if (error != 0) {
962230587Sken			xenbus_dev_fatal(xnb->dev, error,
963230587Sken					 "Error publishing backend info "
964230587Sken					 "(start transaction)");
965230587Sken			break;
966181643Skmacy		}
967181643Skmacy
968230587Sken		error = xs_printf(xst, our_path, "feature-sg",
969230587Sken				  "%d", XNB_SG);
970230587Sken		if (error != 0)
971230587Sken			break;
972181643Skmacy
973230587Sken		error = xs_printf(xst, our_path, "feature-gso-tcpv4",
974230587Sken				  "%d", XNB_GSO_TCPV4);
975230587Sken		if (error != 0)
976181643Skmacy			break;
977230587Sken
978230587Sken		error = xs_printf(xst, our_path, "feature-rx-copy",
979230587Sken				  "%d", XNB_RX_COPY);
980230587Sken		if (error != 0)
981230587Sken			break;
982230587Sken
983230587Sken		error = xs_printf(xst, our_path, "feature-rx-flip",
984230587Sken				  "%d", XNB_RX_FLIP);
985230587Sken		if (error != 0)
986230587Sken			break;
987230587Sken
988230587Sken		error = xs_transaction_end(xst, 0);
989230587Sken		if (error != 0 && error != EAGAIN) {
990230587Sken			xenbus_dev_fatal(xnb->dev, error, "ending transaction");
991230587Sken			break;
992181643Skmacy		}
993181643Skmacy
994230587Sken	} while (error == EAGAIN);
995181643Skmacy
996230587Sken	return (error);
997230587Sken}
998181643Skmacy
999230587Sken/**
1000230587Sken * Connect to our netfront peer now that it has completed publishing
1001230587Sken * its configuration into the XenStore.
1002230587Sken *
1003230587Sken * \param xnb  Per-instance xnb configuration structure.
1004230587Sken */
1005230587Skenstatic void
1006230587Skenxnb_connect(struct xnb_softc *xnb)
1007230587Sken{
1008230587Sken	int	error;
1009181643Skmacy
1010230587Sken	if (xenbus_get_state(xnb->dev) == XenbusStateConnected)
1011230587Sken		return;
1012181643Skmacy
1013230587Sken	if (xnb_collect_xenstore_info(xnb) != 0)
1014230587Sken		return;
1015181643Skmacy
1016230587Sken	xnb->flags &= ~XNBF_SHUTDOWN;
1017230587Sken
1018230587Sken	/* Read front end configuration. */
1019230587Sken
1020230587Sken	/* Allocate resources whose size depends on front-end configuration. */
1021230587Sken	error = xnb_alloc_communication_mem(xnb);
1022230587Sken	if (error != 0) {
1023230587Sken		xenbus_dev_fatal(xnb->dev, error,
1024230587Sken				 "Unable to allocate communication memory");
1025230587Sken		return;
1026181643Skmacy	}
1027181643Skmacy
1028230587Sken	/*
1029230587Sken	 * Connect communication channel.
1030230587Sken	 */
1031230587Sken	error = xnb_connect_comms(xnb);
1032230587Sken	if (error != 0) {
1033230587Sken		/* Specific errors are reported by xnb_connect_comms(). */
1034181643Skmacy		return;
1035230587Sken	}
1036230587Sken	xnb->carrier = 1;
1037181643Skmacy
1038230587Sken	/* Ready for I/O. */
1039230587Sken	xenbus_set_state(xnb->dev, XenbusStateConnected);
1040230587Sken}
1041181643Skmacy
1042230587Sken/*-------------------------- Device Teardown Support -------------------------*/
1043230587Sken/**
1044230587Sken * Perform device shutdown functions.
1045230587Sken *
1046230587Sken * \param xnb  Per-instance xnb configuration structure.
1047230587Sken *
1048230587Sken * Mark this instance as shutting down, wait for any active requests
1049230587Sken * to drain, disconnect from the front-end, and notify any waiters (e.g.
1050230587Sken * a thread invoking our detach method) that detach can now proceed.
1051230587Sken */
1052230587Skenstatic int
1053230587Skenxnb_shutdown(struct xnb_softc *xnb)
1054230587Sken{
1055230587Sken	/*
1056230587Sken	 * Due to the need to drop our mutex during some
1057230587Sken	 * xenbus operations, it is possible for two threads
1058230587Sken	 * to attempt to close out shutdown processing at
1059230587Sken	 * the same time.  Tell the caller that hits this
1060230587Sken	 * race to try back later.
1061230587Sken	 */
1062230587Sken	if ((xnb->flags & XNBF_IN_SHUTDOWN) != 0)
1063230587Sken		return (EAGAIN);
1064181643Skmacy
1065230587Sken	xnb->flags |= XNBF_SHUTDOWN;
1066181643Skmacy
1067230587Sken	xnb->flags |= XNBF_IN_SHUTDOWN;
1068181643Skmacy
1069230587Sken	mtx_unlock(&xnb->sc_lock);
1070230587Sken	/* Free the network interface */
1071230587Sken	xnb->carrier = 0;
1072230587Sken	if (xnb->xnb_ifp != NULL) {
1073230587Sken		ether_ifdetach(xnb->xnb_ifp);
1074230587Sken		if_free(xnb->xnb_ifp);
1075230587Sken		xnb->xnb_ifp = NULL;
1076230587Sken	}
1077230587Sken	mtx_lock(&xnb->sc_lock);
1078181643Skmacy
1079230587Sken	xnb_disconnect(xnb);
1080181643Skmacy
1081230587Sken	mtx_unlock(&xnb->sc_lock);
1082230587Sken	if (xenbus_get_state(xnb->dev) < XenbusStateClosing)
1083230587Sken		xenbus_set_state(xnb->dev, XenbusStateClosing);
1084230587Sken	mtx_lock(&xnb->sc_lock);
1085181643Skmacy
1086230587Sken	xnb->flags &= ~XNBF_IN_SHUTDOWN;
1087181643Skmacy
1088181643Skmacy
1089230587Sken	/* Indicate to xnb_detach() that is it safe to proceed. */
1090230587Sken	wakeup(xnb);
1091181643Skmacy
1092230587Sken	return (0);
1093181643Skmacy}
1094181643Skmacy
1095230587Sken/**
1096230587Sken * Report an attach time error to the console and Xen, and cleanup
1097230587Sken * this instance by forcing immediate detach processing.
1098230587Sken *
1099230587Sken * \param xnb  Per-instance xnb configuration structure.
1100230587Sken * \param err  Errno describing the error.
1101230587Sken * \param fmt  Printf style format and arguments
1102230587Sken */
1103181643Skmacystatic void
1104230587Skenxnb_attach_failed(struct xnb_softc *xnb, int err, const char *fmt, ...)
1105181643Skmacy{
1106230587Sken	va_list ap;
1107230587Sken	va_list ap_hotplug;
1108230587Sken
1109230587Sken	va_start(ap, fmt);
1110230587Sken	va_copy(ap_hotplug, ap);
1111230587Sken	xs_vprintf(XST_NIL, xenbus_get_node(xnb->dev),
1112230587Sken		  "hotplug-error", fmt, ap_hotplug);
1113230587Sken	va_end(ap_hotplug);
1114319222Sasomers	(void)xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
1115230587Sken		  "hotplug-status", "error");
1116230587Sken
1117230587Sken	xenbus_dev_vfatal(xnb->dev, err, fmt, ap);
1118230587Sken	va_end(ap);
1119230587Sken
1120319222Sasomers	(void)xs_printf(XST_NIL, xenbus_get_node(xnb->dev), "online", "0");
1121230587Sken	xnb_detach(xnb->dev);
1122181643Skmacy}
1123181643Skmacy
1124230587Sken/*---------------------------- NewBus Entrypoints ----------------------------*/
1125230587Sken/**
1126230587Sken * Inspect a XenBus device and claim it if is of the appropriate type.
1127230587Sken *
1128230587Sken * \param dev  NewBus device object representing a candidate XenBus device.
1129230587Sken *
1130230587Sken * \return  0 for success, errno codes for failure.
1131230587Sken */
1132230587Skenstatic int
1133230587Skenxnb_probe(device_t dev)
1134181643Skmacy{
1135230587Sken	 if (!strcmp(xenbus_get_type(dev), "vif")) {
1136230587Sken		DPRINTF("Claiming device %d, %s\n", device_get_unit(dev),
1137230587Sken		    devclass_get_name(device_get_devclass(dev)));
1138230587Sken		device_set_desc(dev, "Backend Virtual Network Device");
1139230587Sken		device_quiet(dev);
1140230587Sken		return (0);
1141181643Skmacy	}
1142230587Sken	return (ENXIO);
1143181643Skmacy}
1144181643Skmacy
1145230587Sken/**
1146230587Sken * Setup sysctl variables to control various Network Back parameters.
1147230587Sken *
1148230587Sken * \param xnb  Xen Net Back softc.
1149230587Sken *
1150230587Sken */
1151181643Skmacystatic void
1152230587Skenxnb_setup_sysctl(struct xnb_softc *xnb)
1153181643Skmacy{
1154230587Sken	struct sysctl_ctx_list *sysctl_ctx = NULL;
1155230587Sken	struct sysctl_oid      *sysctl_tree = NULL;
1156230587Sken
1157230587Sken	sysctl_ctx = device_get_sysctl_ctx(xnb->dev);
1158230587Sken	if (sysctl_ctx == NULL)
1159181643Skmacy		return;
1160181643Skmacy
1161230587Sken	sysctl_tree = device_get_sysctl_tree(xnb->dev);
1162230587Sken	if (sysctl_tree == NULL)
1163230587Sken		return;
1164230587Sken
1165230587Sken#ifdef XNB_DEBUG
1166230587Sken	SYSCTL_ADD_PROC(sysctl_ctx,
1167230587Sken			SYSCTL_CHILDREN(sysctl_tree),
1168230587Sken			OID_AUTO,
1169230587Sken			"unit_test_results",
1170230587Sken			CTLTYPE_STRING | CTLFLAG_RD,
1171230587Sken			xnb,
1172230587Sken			0,
1173230587Sken			xnb_unit_test_main,
1174230587Sken			"A",
1175230587Sken			"Results of builtin unit tests");
1176230587Sken
1177230587Sken	SYSCTL_ADD_PROC(sysctl_ctx,
1178230587Sken			SYSCTL_CHILDREN(sysctl_tree),
1179230587Sken			OID_AUTO,
1180230587Sken			"dump_rings",
1181230587Sken			CTLTYPE_STRING | CTLFLAG_RD,
1182230587Sken			xnb,
1183230587Sken			0,
1184230587Sken			xnb_dump_rings,
1185230587Sken			"A",
1186230587Sken			"Xennet Back Rings");
1187230587Sken#endif /* XNB_DEBUG */
1188181643Skmacy}
1189181643Skmacy
1190230587Sken/**
1191230587Sken * Create a network device.
1192230587Sken * @param handle device handle
1193230587Sken */
1194230587Skenint
1195230587Skencreate_netdev(device_t dev)
1196181643Skmacy{
1197230587Sken	struct ifnet *ifp;
1198230587Sken	struct xnb_softc *xnb;
1199230587Sken	int err = 0;
1200181643Skmacy
1201230587Sken	xnb = device_get_softc(dev);
1202230587Sken	mtx_init(&xnb->sc_lock, "xnb_softc", "xen netback softc lock", MTX_DEF);
1203230587Sken	mtx_init(&xnb->tx_lock, "xnb_tx", "xen netback tx lock", MTX_DEF);
1204230587Sken	mtx_init(&xnb->rx_lock, "xnb_rx", "xen netback rx lock", MTX_DEF);
1205181643Skmacy
1206230587Sken	xnb->dev = dev;
1207181643Skmacy
1208230587Sken	ifmedia_init(&xnb->sc_media, 0, xnb_ifmedia_upd, xnb_ifmedia_sts);
1209230587Sken	ifmedia_add(&xnb->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
1210230587Sken	ifmedia_set(&xnb->sc_media, IFM_ETHER|IFM_MANUAL);
1211181643Skmacy
1212230587Sken	err = xen_net_read_mac(dev, xnb->mac);
1213230587Sken	if (err == 0) {
1214230587Sken		/* Set up ifnet structure */
1215230587Sken		ifp = xnb->xnb_ifp = if_alloc(IFT_ETHER);
1216230587Sken		ifp->if_softc = xnb;
1217230587Sken		if_initname(ifp, "xnb",  device_get_unit(dev));
1218230587Sken		ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1219230587Sken		ifp->if_ioctl = xnb_ioctl;
1220230587Sken		ifp->if_output = ether_output;
1221230587Sken		ifp->if_start = xnb_start;
1222230587Sken#ifdef notyet
1223230587Sken		ifp->if_watchdog = xnb_watchdog;
1224230587Sken#endif
1225230587Sken		ifp->if_init = xnb_ifinit;
1226230587Sken		ifp->if_mtu = ETHERMTU;
1227230587Sken		ifp->if_snd.ifq_maxlen = NET_RX_RING_SIZE - 1;
1228230587Sken
1229230587Sken		ifp->if_hwassist = XNB_CSUM_FEATURES;
1230230587Sken		ifp->if_capabilities = IFCAP_HWCSUM;
1231230587Sken		ifp->if_capenable = IFCAP_HWCSUM;
1232230587Sken
1233230587Sken		ether_ifattach(ifp, xnb->mac);
1234230587Sken		xnb->carrier = 0;
1235230587Sken	}
1236230587Sken
1237230587Sken	return err;
1238181643Skmacy}
1239181643Skmacy
1240230587Sken/**
1241230587Sken * Attach to a XenBus device that has been claimed by our probe routine.
1242230587Sken *
1243230587Sken * \param dev  NewBus device object representing this Xen Net Back instance.
1244230587Sken *
1245230587Sken * \return  0 for success, errno codes for failure.
1246230587Sken */
1247181643Skmacystatic int
1248230587Skenxnb_attach(device_t dev)
1249181643Skmacy{
1250230587Sken	struct xnb_softc *xnb;
1251230587Sken	int	error;
1252230587Sken	xnb_ring_type_t	i;
1253181643Skmacy
1254230587Sken	error = create_netdev(dev);
1255230587Sken	if (error != 0) {
1256230587Sken		xenbus_dev_fatal(dev, error, "creating netdev");
1257230587Sken		return (error);
1258230587Sken	}
1259181643Skmacy
1260230587Sken	DPRINTF("Attaching to %s\n", xenbus_get_node(dev));
1261181643Skmacy
1262230587Sken	/*
1263230587Sken	 * Basic initialization.
1264230587Sken	 * After this block it is safe to call xnb_detach()
1265230587Sken	 * to clean up any allocated data for this instance.
1266230587Sken	 */
1267230587Sken	xnb = device_get_softc(dev);
1268230587Sken	xnb->otherend_id = xenbus_get_otherend_id(dev);
1269230587Sken	for (i=0; i < XNB_NUM_RING_TYPES; i++) {
1270230587Sken		xnb->ring_configs[i].ring_pages = 1;
1271230587Sken	}
1272181643Skmacy
1273230587Sken	/*
1274230587Sken	 * Setup sysctl variables.
1275230587Sken	 */
1276230587Sken	xnb_setup_sysctl(xnb);
1277181643Skmacy
1278230587Sken	/* Update hot-plug status to satisfy xend. */
1279230587Sken	error = xs_printf(XST_NIL, xenbus_get_node(xnb->dev),
1280230587Sken			  "hotplug-status", "connected");
1281230587Sken	if (error != 0) {
1282230587Sken		xnb_attach_failed(xnb, error, "writing %s/hotplug-status",
1283230587Sken				  xenbus_get_node(xnb->dev));
1284230587Sken		return (error);
1285230587Sken	}
1286181643Skmacy
1287230587Sken	if ((error = xnb_publish_backend_info(xnb)) != 0) {
1288230587Sken		/*
1289230587Sken		 * If we can't publish our data, we cannot participate
1290230587Sken		 * in this connection, and waiting for a front-end state
1291230587Sken		 * change will not help the situation.
1292230587Sken		 */
1293230587Sken		xnb_attach_failed(xnb, error,
1294230587Sken		    "Publishing backend status for %s",
1295230587Sken				  xenbus_get_node(xnb->dev));
1296230587Sken		return error;
1297230587Sken	}
1298181643Skmacy
1299230587Sken	/* Tell the front end that we are ready to connect. */
1300230587Sken	xenbus_set_state(dev, XenbusStateInitWait);
1301181643Skmacy
1302230587Sken	return (0);
1303230587Sken}
1304181643Skmacy
1305230587Sken/**
1306230587Sken * Detach from a net back device instance.
1307230587Sken *
1308230587Sken * \param dev  NewBus device object representing this Xen Net Back instance.
1309230587Sken *
1310230587Sken * \return  0 for success, errno codes for failure.
1311230587Sken *
1312230587Sken * \note A net back device may be detached at any time in its life-cycle,
1313230587Sken *       including part way through the attach process.  For this reason,
1314230587Sken *       initialization order and the intialization state checks in this
1315230587Sken *       routine must be carefully coupled so that attach time failures
1316230587Sken *       are gracefully handled.
1317230587Sken */
1318230587Skenstatic int
1319230587Skenxnb_detach(device_t dev)
1320230587Sken{
1321230587Sken	struct xnb_softc *xnb;
1322181643Skmacy
1323230587Sken	DPRINTF("\n");
1324181643Skmacy
1325230587Sken	xnb = device_get_softc(dev);
1326230587Sken	mtx_lock(&xnb->sc_lock);
1327230587Sken	while (xnb_shutdown(xnb) == EAGAIN) {
1328230587Sken		msleep(xnb, &xnb->sc_lock, /*wakeup prio unchanged*/0,
1329230587Sken		       "xnb_shutdown", 0);
1330230587Sken	}
1331230587Sken	mtx_unlock(&xnb->sc_lock);
1332230587Sken	DPRINTF("\n");
1333181643Skmacy
1334230587Sken	mtx_destroy(&xnb->tx_lock);
1335230587Sken	mtx_destroy(&xnb->rx_lock);
1336230587Sken	mtx_destroy(&xnb->sc_lock);
1337230587Sken	return (0);
1338230587Sken}
1339181643Skmacy
1340230587Sken/**
1341230587Sken * Prepare this net back device for suspension of this VM.
1342230587Sken *
1343230587Sken * \param dev  NewBus device object representing this Xen net Back instance.
1344230587Sken *
1345230587Sken * \return  0 for success, errno codes for failure.
1346230587Sken */
1347230587Skenstatic int
1348230587Skenxnb_suspend(device_t dev)
1349230587Sken{
1350230587Sken	return (0);
1351230587Sken}
1352181643Skmacy
1353230587Sken/**
1354230587Sken * Perform any processing required to recover from a suspended state.
1355230587Sken *
1356230587Sken * \param dev  NewBus device object representing this Xen Net Back instance.
1357230587Sken *
1358230587Sken * \return  0 for success, errno codes for failure.
1359230587Sken */
1360230587Skenstatic int
1361230587Skenxnb_resume(device_t dev)
1362230587Sken{
1363230587Sken	return (0);
1364230587Sken}
1365181643Skmacy
1366230587Sken/**
1367230587Sken * Handle state changes expressed via the XenStore by our front-end peer.
1368230587Sken *
1369230587Sken * \param dev             NewBus device object representing this Xen
1370230587Sken *                        Net Back instance.
1371230587Sken * \param frontend_state  The new state of the front-end.
1372230587Sken *
1373230587Sken * \return  0 for success, errno codes for failure.
1374230587Sken */
1375230587Skenstatic void
1376230587Skenxnb_frontend_changed(device_t dev, XenbusState frontend_state)
1377230587Sken{
1378230587Sken	struct xnb_softc *xnb;
1379181643Skmacy
1380230587Sken	xnb = device_get_softc(dev);
1381181643Skmacy
1382230587Sken	DPRINTF("frontend_state=%s, xnb_state=%s\n",
1383230587Sken	        xenbus_strstate(frontend_state),
1384230587Sken		xenbus_strstate(xenbus_get_state(xnb->dev)));
1385181643Skmacy
1386230587Sken	switch (frontend_state) {
1387230587Sken	case XenbusStateInitialising:
1388230587Sken		break;
1389230587Sken	case XenbusStateInitialised:
1390230587Sken	case XenbusStateConnected:
1391230587Sken		xnb_connect(xnb);
1392230587Sken		break;
1393230587Sken	case XenbusStateClosing:
1394230587Sken	case XenbusStateClosed:
1395230587Sken		mtx_lock(&xnb->sc_lock);
1396230587Sken		xnb_shutdown(xnb);
1397230587Sken		mtx_unlock(&xnb->sc_lock);
1398230587Sken		if (frontend_state == XenbusStateClosed)
1399230587Sken			xenbus_set_state(xnb->dev, XenbusStateClosed);
1400230587Sken		break;
1401230587Sken	default:
1402230587Sken		xenbus_dev_fatal(xnb->dev, EINVAL, "saw state %d at frontend",
1403230587Sken				 frontend_state);
1404230587Sken		break;
1405230587Sken	}
1406230587Sken}
1407181643Skmacy
1408181643Skmacy
1409230587Sken/*---------------------------- Request Processing ----------------------------*/
1410230587Sken/**
1411230587Sken * Interrupt handler bound to the shared ring's event channel.
1412230587Sken * Entry point for the xennet transmit path in netback
1413230587Sken * Transfers packets from the Xen ring to the host's generic networking stack
1414230587Sken *
1415230587Sken * \param arg  Callback argument registerd during event channel
1416230587Sken *             binding - the xnb_softc for this instance.
1417230587Sken */
1418230587Skenstatic void
1419230587Skenxnb_intr(void *arg)
1420230587Sken{
1421230587Sken	struct xnb_softc *xnb;
1422230587Sken	struct ifnet *ifp;
1423230587Sken	netif_tx_back_ring_t *txb;
1424230587Sken	RING_IDX req_prod_local;
1425181643Skmacy
1426230587Sken	xnb = (struct xnb_softc *)arg;
1427230587Sken	ifp = xnb->xnb_ifp;
1428230587Sken	txb = &xnb->ring_configs[XNB_RING_TYPE_TX].back_ring.tx_ring;
1429181643Skmacy
1430230587Sken	mtx_lock(&xnb->tx_lock);
1431230587Sken	do {
1432230587Sken		int notify;
1433230587Sken		req_prod_local = txb->sring->req_prod;
1434230587Sken		xen_rmb();
1435181643Skmacy
1436230587Sken		for (;;) {
1437230587Sken			struct mbuf *mbufc;
1438230587Sken			int err;
1439181643Skmacy
1440230587Sken			err = xnb_recv(txb, xnb->otherend_id, &mbufc, ifp,
1441230587Sken			    	       xnb->tx_gnttab);
1442230587Sken			if (err || (mbufc == NULL))
1443230587Sken				break;
1444181643Skmacy
1445230587Sken			/* Send the packet to the generic network stack */
1446230587Sken			(*xnb->xnb_ifp->if_input)(xnb->xnb_ifp, mbufc);
1447230587Sken		}
1448181643Skmacy
1449230587Sken		RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(txb, notify);
1450230587Sken		if (notify != 0)
1451255040Sgibbs			xen_intr_signal(xnb->xen_intr_handle);
1452181643Skmacy
1453230587Sken		txb->sring->req_event = txb->req_cons + 1;
1454230587Sken		xen_mb();
1455230587Sken	} while (txb->sring->req_prod != req_prod_local) ;
1456230587Sken	mtx_unlock(&xnb->tx_lock);
1457181643Skmacy
1458230587Sken	xnb_start(ifp);
1459230587Sken}
1460181643Skmacy
1461181643Skmacy
1462230587Sken/**
1463230587Sken * Build a struct xnb_pkt based on netif_tx_request's from a netif tx ring.
1464230587Sken * Will read exactly 0 or 1 packets from the ring; never a partial packet.
1465230587Sken * \param[out]	pkt	The returned packet.  If there is an error building
1466230587Sken * 			the packet, pkt.list_len will be set to 0.
1467230587Sken * \param[in]	tx_ring	Pointer to the Ring that is the input to this function
1468230587Sken * \param[in]	start	The ring index of the first potential request
1469230587Sken * \return		The number of requests consumed to build this packet
1470230587Sken */
1471230587Skenstatic int
1472230587Skenxnb_ring2pkt(struct xnb_pkt *pkt, const netif_tx_back_ring_t *tx_ring,
1473230587Sken	     RING_IDX start)
1474230587Sken{
1475230587Sken	/*
1476230587Sken	 * Outline:
1477230587Sken	 * 1) Initialize pkt
1478230587Sken	 * 2) Read the first request of the packet
1479230587Sken	 * 3) Read the extras
1480230587Sken	 * 4) Set cdr
1481230587Sken	 * 5) Loop on the remainder of the packet
1482230587Sken	 * 6) Finalize pkt (stuff like car_size and list_len)
1483230587Sken	 */
1484230587Sken	int idx = start;
1485230587Sken	int discard = 0;	/* whether to discard the packet */
1486230587Sken	int more_data = 0;	/* there are more request past the last one */
1487230587Sken	uint16_t cdr_size = 0;	/* accumulated size of requests 2 through n */
1488181643Skmacy
1489230587Sken	xnb_pkt_initialize(pkt);
1490181643Skmacy
1491230587Sken	/* Read the first request */
1492230587Sken	if (RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1493230587Sken		netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx);
1494230587Sken		pkt->size = tx->size;
1495230587Sken		pkt->flags = tx->flags & ~NETTXF_more_data;
1496230587Sken		more_data = tx->flags & NETTXF_more_data;
1497230587Sken		pkt->list_len++;
1498230587Sken		pkt->car = idx;
1499230587Sken		idx++;
1500230587Sken	}
1501181643Skmacy
1502230587Sken	/* Read the extra info */
1503230587Sken	if ((pkt->flags & NETTXF_extra_info) &&
1504230587Sken	    RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1505230587Sken		netif_extra_info_t *ext =
1506230587Sken		    (netif_extra_info_t*) RING_GET_REQUEST(tx_ring, idx);
1507230587Sken		pkt->extra.type = ext->type;
1508230587Sken		switch (pkt->extra.type) {
1509230587Sken			case XEN_NETIF_EXTRA_TYPE_GSO:
1510230587Sken				pkt->extra.u.gso = ext->u.gso;
1511230587Sken				break;
1512230587Sken			default:
1513230587Sken				/*
1514230587Sken				 * The reference Linux netfront driver will
1515230587Sken				 * never set any other extra.type.  So we don't
1516230587Sken				 * know what to do with it.  Let's print an
1517230587Sken				 * error, then consume and discard the packet
1518230587Sken				 */
1519230587Sken				printf("xnb(%s:%d): Unknown extra info type %d."
1520230587Sken				       "  Discarding packet\n",
1521230587Sken				       __func__, __LINE__, pkt->extra.type);
1522230587Sken				xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring,
1523230587Sken				    start));
1524230587Sken				xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring,
1525230587Sken				    idx));
1526230587Sken				discard = 1;
1527230587Sken				break;
1528230587Sken		}
1529230587Sken
1530230587Sken		pkt->extra.flags = ext->flags;
1531230587Sken		if (ext->flags & XEN_NETIF_EXTRA_FLAG_MORE) {
1532181643Skmacy			/*
1533230587Sken			 * The reference linux netfront driver never sets this
1534230587Sken			 * flag (nor does any other known netfront).  So we
1535230587Sken			 * will discard the packet.
1536181643Skmacy			 */
1537230587Sken			printf("xnb(%s:%d): Request sets "
1538230587Sken			    "XEN_NETIF_EXTRA_FLAG_MORE, but we can't handle "
1539230587Sken			    "that\n", __func__, __LINE__);
1540230587Sken			xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start));
1541230587Sken			xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx));
1542230587Sken			discard = 1;
1543181643Skmacy		}
1544181643Skmacy
1545230587Sken		idx++;
1546181643Skmacy	}
1547181643Skmacy
1548230587Sken	/* Set cdr.  If there is not more data, cdr is invalid */
1549230587Sken	pkt->cdr = idx;
1550181643Skmacy
1551230587Sken	/* Loop on remainder of packet */
1552230587Sken	while (more_data && RING_HAS_UNCONSUMED_REQUESTS_2(tx_ring, idx)) {
1553230587Sken		netif_tx_request_t *tx = RING_GET_REQUEST(tx_ring, idx);
1554230587Sken		pkt->list_len++;
1555230587Sken		cdr_size += tx->size;
1556230587Sken		if (tx->flags & ~NETTXF_more_data) {
1557230587Sken			/* There should be no other flags set at this point */
1558230587Sken			printf("xnb(%s:%d): Request sets unknown flags %d "
1559230587Sken			    "after the 1st request in the packet.\n",
1560230587Sken			    __func__, __LINE__, tx->flags);
1561230587Sken			xnb_dump_txreq(start, RING_GET_REQUEST(tx_ring, start));
1562230587Sken			xnb_dump_txreq(idx, RING_GET_REQUEST(tx_ring, idx));
1563230587Sken		}
1564181643Skmacy
1565230587Sken		more_data = tx->flags & NETTXF_more_data;
1566230587Sken		idx++;
1567230587Sken	}
1568230587Sken
1569230587Sken	/* Finalize packet */
1570230587Sken	if (more_data != 0) {
1571230587Sken		/* The ring ran out of requests before finishing the packet */
1572230587Sken		xnb_pkt_invalidate(pkt);
1573230587Sken		idx = start;	/* tell caller that we consumed no requests */
1574230587Sken	} else {
1575230587Sken		/* Calculate car_size */
1576230587Sken		pkt->car_size = pkt->size - cdr_size;
1577230587Sken	}
1578230587Sken	if (discard != 0) {
1579230587Sken		xnb_pkt_invalidate(pkt);
1580230587Sken	}
1581230587Sken
1582230587Sken	return idx - start;
1583181643Skmacy}
1584181643Skmacy
1585230587Sken
1586230587Sken/**
1587230587Sken * Respond to all the requests that constituted pkt.  Builds the responses and
1588230587Sken * writes them to the ring, but doesn't push them to the shared ring.
1589230587Sken * \param[in] pkt	the packet that needs a response
1590230587Sken * \param[in] error	true if there was an error handling the packet, such
1591230587Sken * 			as in the hypervisor copy op or mbuf allocation
1592230587Sken * \param[out] ring	Responses go here
1593230587Sken */
1594181643Skmacystatic void
1595230587Skenxnb_txpkt2rsp(const struct xnb_pkt *pkt, netif_tx_back_ring_t *ring,
1596230587Sken	      int error)
1597181643Skmacy{
1598230587Sken	/*
1599230587Sken	 * Outline:
1600230587Sken	 * 1) Respond to the first request
1601230587Sken	 * 2) Respond to the extra info reques
1602230587Sken	 * Loop through every remaining request in the packet, generating
1603230587Sken	 * responses that copy those requests' ids and sets the status
1604230587Sken	 * appropriately.
1605230587Sken	 */
1606230587Sken	netif_tx_request_t *tx;
1607230587Sken	netif_tx_response_t *rsp;
1608230587Sken	int i;
1609230587Sken	uint16_t status;
1610181643Skmacy
1611230587Sken	status = (xnb_pkt_is_valid(pkt) == 0) || error ?
1612230587Sken		NETIF_RSP_ERROR : NETIF_RSP_OKAY;
1613230587Sken	KASSERT((pkt->list_len == 0) || (ring->rsp_prod_pvt == pkt->car),
1614230587Sken	    ("Cannot respond to ring requests out of order"));
1615181643Skmacy
1616230587Sken	if (pkt->list_len >= 1) {
1617230587Sken		uint16_t id;
1618230587Sken		tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt);
1619230587Sken		id = tx->id;
1620230587Sken		rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1621230587Sken		rsp->id = id;
1622230587Sken		rsp->status = status;
1623230587Sken		ring->rsp_prod_pvt++;
1624181643Skmacy
1625230587Sken		if (pkt->flags & NETRXF_extra_info) {
1626230587Sken			rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1627230587Sken			rsp->status = NETIF_RSP_NULL;
1628230587Sken			ring->rsp_prod_pvt++;
1629181643Skmacy		}
1630230587Sken	}
1631181643Skmacy
1632230587Sken	for (i=0; i < pkt->list_len - 1; i++) {
1633230587Sken		uint16_t id;
1634230587Sken		tx = RING_GET_REQUEST(ring, ring->rsp_prod_pvt);
1635230587Sken		id = tx->id;
1636230587Sken		rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
1637230587Sken		rsp->id = id;
1638230587Sken		rsp->status = status;
1639230587Sken		ring->rsp_prod_pvt++;
1640181643Skmacy	}
1641181643Skmacy}
1642181643Skmacy
1643230587Sken/**
1644230587Sken * Create an mbuf chain to represent a packet.  Initializes all of the headers
1645230587Sken * in the mbuf chain, but does not copy the data.  The returned chain must be
1646230587Sken * free()'d when no longer needed
1647230587Sken * \param[in]	pkt	A packet to model the mbuf chain after
1648230587Sken * \return	A newly allocated mbuf chain, possibly with clusters attached.
1649230587Sken * 		NULL on failure
1650230587Sken */
1651230587Skenstatic struct mbuf*
1652230587Skenxnb_pkt2mbufc(const struct xnb_pkt *pkt, struct ifnet *ifp)
1653181643Skmacy{
1654230587Sken	/**
1655230587Sken	 * \todo consider using a memory pool for mbufs instead of
1656230587Sken	 * reallocating them for every packet
1657230587Sken	 */
1658230587Sken	/** \todo handle extra data */
1659230587Sken	struct mbuf *m;
1660181643Skmacy
1661230587Sken	m = m_getm(NULL, pkt->size, M_NOWAIT, MT_DATA);
1662181643Skmacy
1663230587Sken	if (m != NULL) {
1664230587Sken		m->m_pkthdr.rcvif = ifp;
1665230587Sken		if (pkt->flags & NETTXF_data_validated) {
1666230587Sken			/*
1667230587Sken			 * We lie to the host OS and always tell it that the
1668230587Sken			 * checksums are ok, because the packet is unlikely to
1669230587Sken			 * get corrupted going across domains.
1670230587Sken			 */
1671230587Sken			m->m_pkthdr.csum_flags = (
1672230587Sken				CSUM_IP_CHECKED |
1673230587Sken				CSUM_IP_VALID   |
1674230587Sken				CSUM_DATA_VALID |
1675230587Sken				CSUM_PSEUDO_HDR
1676230587Sken				);
1677230587Sken			m->m_pkthdr.csum_data = 0xffff;
1678230587Sken		}
1679230587Sken	}
1680230587Sken	return m;
1681181643Skmacy}
1682181643Skmacy
1683230587Sken/**
1684230587Sken * Build a gnttab_copy table that can be used to copy data from a pkt
1685230587Sken * to an mbufc.  Does not actually perform the copy.  Always uses gref's on
1686230587Sken * the packet side.
1687230587Sken * \param[in]	pkt	pkt's associated requests form the src for
1688230587Sken * 			the copy operation
1689230587Sken * \param[in]	mbufc	mbufc's storage forms the dest for the copy operation
1690230587Sken * \param[out]  gnttab	Storage for the returned grant table
1691230587Sken * \param[in]	txb	Pointer to the backend ring structure
1692230587Sken * \param[in]	otherend_id	The domain ID of the other end of the copy
1693230587Sken * \return 		The number of gnttab entries filled
1694230587Sken */
1695181643Skmacystatic int
1696230587Skenxnb_txpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc,
1697230587Sken		 gnttab_copy_table gnttab, const netif_tx_back_ring_t *txb,
1698230587Sken		 domid_t otherend_id)
1699181643Skmacy{
1700181643Skmacy
1701230587Sken	const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */
1702230587Sken	int gnt_idx = 0;		/* index into grant table */
1703230587Sken	RING_IDX r_idx = pkt->car;	/* index into tx ring buffer */
1704230587Sken	int r_ofs = 0;	/* offset of next data within tx request's data area */
1705230587Sken	int m_ofs = 0;	/* offset of next data within mbuf's data area */
1706230587Sken	/* size in bytes that still needs to be represented in the table */
1707230587Sken	uint16_t size_remaining = pkt->size;
1708181643Skmacy
1709230587Sken	while (size_remaining > 0) {
1710230587Sken		const netif_tx_request_t *txq = RING_GET_REQUEST(txb, r_idx);
1711230587Sken		const size_t mbuf_space = M_TRAILINGSPACE(mbuf) - m_ofs;
1712230587Sken		const size_t req_size =
1713230587Sken			r_idx == pkt->car ? pkt->car_size : txq->size;
1714230587Sken		const size_t pkt_space = req_size - r_ofs;
1715230587Sken		/*
1716230587Sken		 * space is the largest amount of data that can be copied in the
1717230587Sken		 * grant table's next entry
1718230587Sken		 */
1719230587Sken		const size_t space = MIN(pkt_space, mbuf_space);
1720230587Sken
1721230587Sken		/* TODO: handle this error condition without panicking */
1722230587Sken		KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short"));
1723230587Sken
1724230587Sken		gnttab[gnt_idx].source.u.ref = txq->gref;
1725230587Sken		gnttab[gnt_idx].source.domid = otherend_id;
1726230587Sken		gnttab[gnt_idx].source.offset = txq->offset + r_ofs;
1727230587Sken		gnttab[gnt_idx].dest.u.gmfn = virt_to_mfn(
1728230587Sken		    mtod(mbuf, vm_offset_t) + m_ofs);
1729230587Sken		gnttab[gnt_idx].dest.offset = virt_to_offset(
1730230587Sken		    mtod(mbuf, vm_offset_t) + m_ofs);
1731230587Sken		gnttab[gnt_idx].dest.domid = DOMID_SELF;
1732230587Sken		gnttab[gnt_idx].len = space;
1733230587Sken		gnttab[gnt_idx].flags = GNTCOPY_source_gref;
1734230587Sken
1735230587Sken		gnt_idx++;
1736230587Sken		r_ofs += space;
1737230587Sken		m_ofs += space;
1738230587Sken		size_remaining -= space;
1739230587Sken		if (req_size - r_ofs <= 0) {
1740230587Sken			/* Must move to the next tx request */
1741230587Sken			r_ofs = 0;
1742230587Sken			r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1;
1743230587Sken		}
1744230587Sken		if (M_TRAILINGSPACE(mbuf) - m_ofs <= 0) {
1745230587Sken			/* Must move to the next mbuf */
1746230587Sken			m_ofs = 0;
1747230587Sken			mbuf = mbuf->m_next;
1748230587Sken		}
1749181643Skmacy	}
1750181643Skmacy
1751230587Sken	return gnt_idx;
1752181643Skmacy}
1753181643Skmacy
1754230587Sken/**
1755230587Sken * Check the status of the grant copy operations, and update mbufs various
1756230587Sken * non-data fields to reflect the data present.
1757230587Sken * \param[in,out] mbufc	mbuf chain to update.  The chain must be valid and of
1758230587Sken * 			the correct length, and data should already be present
1759230587Sken * \param[in] gnttab	A grant table for a just completed copy op
1760230587Sken * \param[in] n_entries The number of valid entries in the grant table
1761230587Sken */
1762181643Skmacystatic void
1763230587Skenxnb_update_mbufc(struct mbuf *mbufc, const gnttab_copy_table gnttab,
1764230587Sken    		 int n_entries)
1765181643Skmacy{
1766230587Sken	struct mbuf *mbuf = mbufc;
1767230587Sken	int i;
1768230587Sken	size_t total_size = 0;
1769181643Skmacy
1770230587Sken	for (i = 0; i < n_entries; i++) {
1771230587Sken		KASSERT(gnttab[i].status == GNTST_okay,
1772230587Sken		    ("Some gnttab_copy entry had error status %hd\n",
1773230587Sken		    gnttab[i].status));
1774181643Skmacy
1775230587Sken		mbuf->m_len += gnttab[i].len;
1776230587Sken		total_size += gnttab[i].len;
1777230587Sken		if (M_TRAILINGSPACE(mbuf) <= 0) {
1778230587Sken			mbuf = mbuf->m_next;
1779230587Sken		}
1780230587Sken	}
1781230587Sken	mbufc->m_pkthdr.len = total_size;
1782230587Sken
1783259541Sglebius#if defined(INET) || defined(INET6)
1784230587Sken	xnb_add_mbuf_cksum(mbufc);
1785259541Sglebius#endif
1786181643Skmacy}
1787181643Skmacy
1788230587Sken/**
1789230587Sken * Dequeue at most one packet from the shared ring
1790230587Sken * \param[in,out] txb	Netif tx ring.  A packet will be removed from it, and
1791230587Sken * 			its private indices will be updated.  But the indices
1792230587Sken * 			will not be pushed to the shared ring.
1793230587Sken * \param[in] ifnet	Interface to which the packet will be sent
1794230587Sken * \param[in] otherend	Domain ID of the other end of the ring
1795230587Sken * \param[out] mbufc	The assembled mbuf chain, ready to send to the generic
1796230587Sken * 			networking stack
1797230587Sken * \param[in,out] gnttab Pointer to enough memory for a grant table.  We make
1798230587Sken * 			this a function parameter so that we will take less
1799230587Sken * 			stack space.
1800230587Sken * \return		An error code
1801230587Sken */
1802181643Skmacystatic int
1803230587Skenxnb_recv(netif_tx_back_ring_t *txb, domid_t otherend, struct mbuf **mbufc,
1804230587Sken	 struct ifnet *ifnet, gnttab_copy_table gnttab)
1805181643Skmacy{
1806230587Sken	struct xnb_pkt pkt;
1807230587Sken	/* number of tx requests consumed to build the last packet */
1808230587Sken	int num_consumed;
1809230587Sken	int nr_ents;
1810181643Skmacy
1811230587Sken	*mbufc = NULL;
1812230587Sken	num_consumed = xnb_ring2pkt(&pkt, txb, txb->req_cons);
1813230587Sken	if (num_consumed == 0)
1814230587Sken		return 0;	/* Nothing to receive */
1815181643Skmacy
1816249588Sgabor	/* update statistics independent of errors */
1817230587Sken	ifnet->if_ipackets++;
1818181643Skmacy
1819230587Sken	/*
1820230587Sken	 * if we got here, then 1 or more requests was consumed, but the packet
1821249583Sgabor	 * is not necessarily valid.
1822230587Sken	 */
1823230587Sken	if (xnb_pkt_is_valid(&pkt) == 0) {
1824230587Sken		/* got a garbage packet, respond and drop it */
1825230587Sken		xnb_txpkt2rsp(&pkt, txb, 1);
1826230587Sken		txb->req_cons += num_consumed;
1827230587Sken		DPRINTF("xnb_intr: garbage packet, num_consumed=%d\n",
1828230587Sken				num_consumed);
1829230587Sken		ifnet->if_ierrors++;
1830230587Sken		return EINVAL;
1831181643Skmacy	}
1832181643Skmacy
1833230587Sken	*mbufc = xnb_pkt2mbufc(&pkt, ifnet);
1834230587Sken
1835230587Sken	if (*mbufc == NULL) {
1836230587Sken		/*
1837230587Sken		 * Couldn't allocate mbufs.  Respond and drop the packet.  Do
1838230587Sken		 * not consume the requests
1839230587Sken		 */
1840230587Sken		xnb_txpkt2rsp(&pkt, txb, 1);
1841230587Sken		DPRINTF("xnb_intr: Couldn't allocate mbufs, num_consumed=%d\n",
1842230587Sken		    num_consumed);
1843230587Sken		ifnet->if_iqdrops++;
1844230587Sken		return ENOMEM;
1845181643Skmacy	}
1846181643Skmacy
1847230587Sken	nr_ents = xnb_txpkt2gnttab(&pkt, *mbufc, gnttab, txb, otherend);
1848181643Skmacy
1849230587Sken	if (nr_ents > 0) {
1850230587Sken		int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
1851230587Sken		    gnttab, nr_ents);
1852230587Sken		KASSERT(hv_ret == 0,
1853230587Sken		    ("HYPERVISOR_grant_table_op returned %d\n", hv_ret));
1854230587Sken		xnb_update_mbufc(*mbufc, gnttab, nr_ents);
1855230587Sken	}
1856181643Skmacy
1857230587Sken	xnb_txpkt2rsp(&pkt, txb, 0);
1858230587Sken	txb->req_cons += num_consumed;
1859181643Skmacy	return 0;
1860181643Skmacy}
1861181643Skmacy
1862230587Sken/**
1863230587Sken * Create an xnb_pkt based on the contents of an mbuf chain.
1864230587Sken * \param[in] mbufc	mbuf chain to transform into a packet
1865230587Sken * \param[out] pkt	Storage for the newly generated xnb_pkt
1866230587Sken * \param[in] start	The ring index of the first available slot in the rx
1867230587Sken * 			ring
1868230587Sken * \param[in] space	The number of free slots in the rx ring
1869230587Sken * \retval 0		Success
1870230587Sken * \retval EINVAL	mbufc was corrupt or not convertible into a pkt
1871230587Sken * \retval EAGAIN	There was not enough space in the ring to queue the
1872230587Sken * 			packet
1873230587Sken */
1874230587Skenstatic int
1875230587Skenxnb_mbufc2pkt(const struct mbuf *mbufc, struct xnb_pkt *pkt,
1876230587Sken	      RING_IDX start, int space)
1877181643Skmacy{
1878181643Skmacy
1879230587Sken	int retval = 0;
1880181643Skmacy
1881230587Sken	if ((mbufc == NULL) ||
1882230587Sken	     ( (mbufc->m_flags & M_PKTHDR) == 0) ||
1883230587Sken	     (mbufc->m_pkthdr.len == 0)) {
1884230587Sken		xnb_pkt_invalidate(pkt);
1885230587Sken		retval = EINVAL;
1886230587Sken	} else {
1887230587Sken		int slots_required;
1888181643Skmacy
1889230587Sken		xnb_pkt_validate(pkt);
1890230587Sken		pkt->flags = 0;
1891230587Sken		pkt->size = mbufc->m_pkthdr.len;
1892230587Sken		pkt->car = start;
1893230587Sken		pkt->car_size = mbufc->m_len;
1894181643Skmacy
1895230587Sken		if (mbufc->m_pkthdr.csum_flags & CSUM_TSO) {
1896230587Sken			pkt->flags |= NETRXF_extra_info;
1897230587Sken			pkt->extra.u.gso.size = mbufc->m_pkthdr.tso_segsz;
1898230587Sken			pkt->extra.u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
1899230587Sken			pkt->extra.u.gso.pad = 0;
1900230587Sken			pkt->extra.u.gso.features = 0;
1901230587Sken			pkt->extra.type = XEN_NETIF_EXTRA_TYPE_GSO;
1902230587Sken			pkt->extra.flags = 0;
1903230587Sken			pkt->cdr = start + 2;
1904230587Sken		} else {
1905230587Sken			pkt->cdr = start + 1;
1906230587Sken		}
1907230587Sken		if (mbufc->m_pkthdr.csum_flags & (CSUM_TSO | CSUM_DELAY_DATA)) {
1908230587Sken			pkt->flags |=
1909230587Sken			    (NETRXF_csum_blank | NETRXF_data_validated);
1910230587Sken		}
1911230587Sken
1912230587Sken		/*
1913230587Sken		 * Each ring response can have up to PAGE_SIZE of data.
1914230587Sken		 * Assume that we can defragment the mbuf chain efficiently
1915230587Sken		 * into responses so that each response but the last uses all
1916230587Sken		 * PAGE_SIZE bytes.
1917230587Sken		 */
1918230587Sken		pkt->list_len = (pkt->size + PAGE_SIZE - 1) / PAGE_SIZE;
1919230587Sken
1920230587Sken		if (pkt->list_len > 1) {
1921230587Sken			pkt->flags |= NETRXF_more_data;
1922230587Sken		}
1923230587Sken
1924230587Sken		slots_required = pkt->list_len +
1925230587Sken			(pkt->flags & NETRXF_extra_info ? 1 : 0);
1926230587Sken		if (slots_required > space) {
1927230587Sken			xnb_pkt_invalidate(pkt);
1928230587Sken			retval = EAGAIN;
1929230587Sken		}
1930181643Skmacy	}
1931230587Sken
1932230587Sken	return retval;
1933181643Skmacy}
1934181643Skmacy
1935230587Sken/**
1936230587Sken * Build a gnttab_copy table that can be used to copy data from an mbuf chain
1937230587Sken * to the frontend's shared buffers.  Does not actually perform the copy.
1938230587Sken * Always uses gref's on the other end's side.
1939230587Sken * \param[in]	pkt	pkt's associated responses form the dest for the copy
1940230587Sken * 			operatoin
1941230587Sken * \param[in]	mbufc	The source for the copy operation
1942230587Sken * \param[out]	gnttab	Storage for the returned grant table
1943230587Sken * \param[in]	rxb	Pointer to the backend ring structure
1944230587Sken * \param[in]	otherend_id	The domain ID of the other end of the copy
1945230587Sken * \return 		The number of gnttab entries filled
1946230587Sken */
1947181643Skmacystatic int
1948230587Skenxnb_rxpkt2gnttab(const struct xnb_pkt *pkt, const struct mbuf *mbufc,
1949230587Sken		 gnttab_copy_table gnttab, const netif_rx_back_ring_t *rxb,
1950230587Sken		 domid_t otherend_id)
1951181643Skmacy{
1952181643Skmacy
1953230587Sken	const struct mbuf *mbuf = mbufc;/* current mbuf within the chain */
1954230587Sken	int gnt_idx = 0;		/* index into grant table */
1955230587Sken	RING_IDX r_idx = pkt->car;	/* index into rx ring buffer */
1956230587Sken	int r_ofs = 0;	/* offset of next data within rx request's data area */
1957230587Sken	int m_ofs = 0;	/* offset of next data within mbuf's data area */
1958230587Sken	/* size in bytes that still needs to be represented in the table */
1959230587Sken	uint16_t size_remaining;
1960181643Skmacy
1961230587Sken	size_remaining = (xnb_pkt_is_valid(pkt) != 0) ? pkt->size : 0;
1962230587Sken
1963230587Sken	while (size_remaining > 0) {
1964230587Sken		const netif_rx_request_t *rxq = RING_GET_REQUEST(rxb, r_idx);
1965230587Sken		const size_t mbuf_space = mbuf->m_len - m_ofs;
1966230587Sken		/* Xen shared pages have an implied size of PAGE_SIZE */
1967230587Sken		const size_t req_size = PAGE_SIZE;
1968230587Sken		const size_t pkt_space = req_size - r_ofs;
1969230587Sken		/*
1970230587Sken		 * space is the largest amount of data that can be copied in the
1971230587Sken		 * grant table's next entry
1972230587Sken		 */
1973230587Sken		const size_t space = MIN(pkt_space, mbuf_space);
1974230587Sken
1975230587Sken		/* TODO: handle this error condition without panicing */
1976230587Sken		KASSERT(gnt_idx < GNTTAB_LEN, ("Grant table is too short"));
1977230587Sken
1978230587Sken		gnttab[gnt_idx].dest.u.ref = rxq->gref;
1979230587Sken		gnttab[gnt_idx].dest.domid = otherend_id;
1980230587Sken		gnttab[gnt_idx].dest.offset = r_ofs;
1981230587Sken		gnttab[gnt_idx].source.u.gmfn = virt_to_mfn(
1982230587Sken		    mtod(mbuf, vm_offset_t) + m_ofs);
1983230587Sken		gnttab[gnt_idx].source.offset = virt_to_offset(
1984230587Sken		    mtod(mbuf, vm_offset_t) + m_ofs);
1985230587Sken		gnttab[gnt_idx].source.domid = DOMID_SELF;
1986230587Sken		gnttab[gnt_idx].len = space;
1987230587Sken		gnttab[gnt_idx].flags = GNTCOPY_dest_gref;
1988230587Sken
1989230587Sken		gnt_idx++;
1990230587Sken
1991230587Sken		r_ofs += space;
1992230587Sken		m_ofs += space;
1993230587Sken		size_remaining -= space;
1994230587Sken		if (req_size - r_ofs <= 0) {
1995230587Sken			/* Must move to the next rx request */
1996230587Sken			r_ofs = 0;
1997230587Sken			r_idx = (r_idx == pkt->car) ? pkt->cdr : r_idx + 1;
1998230587Sken		}
1999230587Sken		if (mbuf->m_len - m_ofs <= 0) {
2000230587Sken			/* Must move to the next mbuf */
2001230587Sken			m_ofs = 0;
2002230587Sken			mbuf = mbuf->m_next;
2003230587Sken		}
2004181643Skmacy	}
2005181643Skmacy
2006230587Sken	return gnt_idx;
2007181643Skmacy}
2008181643Skmacy
2009181643Skmacy/**
2010230587Sken * Generates responses for all the requests that constituted pkt.  Builds
2011230587Sken * responses and writes them to the ring, but doesn't push the shared ring
2012230587Sken * indices.
2013230587Sken * \param[in] pkt	the packet that needs a response
2014230587Sken * \param[in] gnttab	The grant copy table corresponding to this packet.
2015230587Sken * 			Used to determine how many rsp->netif_rx_response_t's to
2016230587Sken * 			generate.
2017230587Sken * \param[in] n_entries	Number of relevant entries in the grant table
2018230587Sken * \param[out] ring	Responses go here
2019230587Sken * \return		The number of RX requests that were consumed to generate
2020230587Sken * 			the responses
2021181643Skmacy */
2022181643Skmacystatic int
2023230587Skenxnb_rxpkt2rsp(const struct xnb_pkt *pkt, const gnttab_copy_table gnttab,
2024230587Sken    	      int n_entries, netif_rx_back_ring_t *ring)
2025181643Skmacy{
2026230587Sken	/*
2027230587Sken	 * This code makes the following assumptions:
2028230587Sken	 *	* All entries in gnttab set GNTCOPY_dest_gref
2029230587Sken	 *	* The entries in gnttab are grouped by their grefs: any two
2030230587Sken	 *	   entries with the same gref must be adjacent
2031230587Sken	 */
2032230587Sken	int error = 0;
2033230587Sken	int gnt_idx, i;
2034230587Sken	int n_responses = 0;
2035230587Sken	grant_ref_t last_gref = GRANT_REF_INVALID;
2036230587Sken	RING_IDX r_idx;
2037181643Skmacy
2038230587Sken	KASSERT(gnttab != NULL, ("Received a null granttable copy"));
2039230587Sken
2040230587Sken	/*
2041230587Sken	 * In the event of an error, we only need to send one response to the
2042230587Sken	 * netfront.  In that case, we musn't write any data to the responses
2043230587Sken	 * after the one we send.  So we must loop all the way through gnttab
2044230587Sken	 * looking for errors before we generate any responses
2045230587Sken	 *
2046230587Sken	 * Since we're looping through the grant table anyway, we'll count the
2047230587Sken	 * number of different gref's in it, which will tell us how many
2048230587Sken	 * responses to generate
2049230587Sken	 */
2050230587Sken	for (gnt_idx = 0; gnt_idx < n_entries; gnt_idx++) {
2051230587Sken		int16_t status = gnttab[gnt_idx].status;
2052230587Sken		if (status != GNTST_okay) {
2053230587Sken			DPRINTF(
2054230587Sken			    "Got error %d for hypervisor gnttab_copy status\n",
2055230587Sken			    status);
2056230587Sken			error = 1;
2057230587Sken			break;
2058230587Sken		}
2059230587Sken		if (gnttab[gnt_idx].dest.u.ref != last_gref) {
2060230587Sken			n_responses++;
2061230587Sken			last_gref = gnttab[gnt_idx].dest.u.ref;
2062230587Sken		}
2063181643Skmacy	}
2064181643Skmacy
2065230587Sken	if (error != 0) {
2066230587Sken		uint16_t id;
2067230587Sken		netif_rx_response_t *rsp;
2068230587Sken
2069230587Sken		id = RING_GET_REQUEST(ring, ring->rsp_prod_pvt)->id;
2070230587Sken		rsp = RING_GET_RESPONSE(ring, ring->rsp_prod_pvt);
2071230587Sken		rsp->id = id;
2072230587Sken		rsp->status = NETIF_RSP_ERROR;
2073230587Sken		n_responses = 1;
2074230587Sken	} else {
2075230587Sken		gnt_idx = 0;
2076230587Sken		const int has_extra = pkt->flags & NETRXF_extra_info;
2077230587Sken		if (has_extra != 0)
2078230587Sken			n_responses++;
2079181643Skmacy
2080230587Sken		for (i = 0; i < n_responses; i++) {
2081230587Sken			netif_rx_request_t rxq;
2082230587Sken			netif_rx_response_t *rsp;
2083181643Skmacy
2084230587Sken			r_idx = ring->rsp_prod_pvt + i;
2085230587Sken			/*
2086230587Sken			 * We copy the structure of rxq instead of making a
2087230587Sken			 * pointer because it shares the same memory as rsp.
2088230587Sken			 */
2089230587Sken			rxq = *(RING_GET_REQUEST(ring, r_idx));
2090230587Sken			rsp = RING_GET_RESPONSE(ring, r_idx);
2091230587Sken			if (has_extra && (i == 1)) {
2092230587Sken				netif_extra_info_t *ext =
2093230587Sken					(netif_extra_info_t*)rsp;
2094230587Sken				ext->type = XEN_NETIF_EXTRA_TYPE_GSO;
2095230587Sken				ext->flags = 0;
2096230587Sken				ext->u.gso.size = pkt->extra.u.gso.size;
2097230587Sken				ext->u.gso.type = XEN_NETIF_GSO_TYPE_TCPV4;
2098230587Sken				ext->u.gso.pad = 0;
2099230587Sken				ext->u.gso.features = 0;
2100230587Sken			} else {
2101230587Sken				rsp->id = rxq.id;
2102230587Sken				rsp->status = GNTST_okay;
2103230587Sken				rsp->offset = 0;
2104230587Sken				rsp->flags = 0;
2105230587Sken				if (i < pkt->list_len - 1)
2106230587Sken					rsp->flags |= NETRXF_more_data;
2107230587Sken				if ((i == 0) && has_extra)
2108230587Sken					rsp->flags |= NETRXF_extra_info;
2109230587Sken				if ((i == 0) &&
2110230587Sken					(pkt->flags & NETRXF_data_validated)) {
2111230587Sken					rsp->flags |= NETRXF_data_validated;
2112230587Sken					rsp->flags |= NETRXF_csum_blank;
2113230587Sken				}
2114230587Sken				rsp->status = 0;
2115230587Sken				for (; gnttab[gnt_idx].dest.u.ref == rxq.gref;
2116230587Sken				    gnt_idx++) {
2117230587Sken					rsp->status += gnttab[gnt_idx].len;
2118230587Sken				}
2119230587Sken			}
2120230587Sken		}
2121181643Skmacy	}
2122181643Skmacy
2123230587Sken	ring->req_cons += n_responses;
2124230587Sken	ring->rsp_prod_pvt += n_responses;
2125230587Sken	return n_responses;
2126181643Skmacy}
2127181643Skmacy
2128259541Sglebius#if defined(INET) || defined(INET6)
2129181643Skmacy/**
2130230587Sken * Add IP, TCP, and/or UDP checksums to every mbuf in a chain.  The first mbuf
2131230587Sken * in the chain must start with a struct ether_header.
2132230587Sken *
2133230587Sken * XXX This function will perform incorrectly on UDP packets that are split up
2134230587Sken * into multiple ethernet frames.
2135181643Skmacy */
2136230587Skenstatic void
2137230587Skenxnb_add_mbuf_cksum(struct mbuf *mbufc)
2138181643Skmacy{
2139230587Sken	struct ether_header *eh;
2140230587Sken	struct ip *iph;
2141230587Sken	uint16_t ether_type;
2142181643Skmacy
2143230587Sken	eh = mtod(mbufc, struct ether_header*);
2144230587Sken	ether_type = ntohs(eh->ether_type);
2145230587Sken	if (ether_type != ETHERTYPE_IP) {
2146230587Sken		/* Nothing to calculate */
2147230587Sken		return;
2148230587Sken	}
2149181643Skmacy
2150230587Sken	iph = (struct ip*)(eh + 1);
2151230587Sken	if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2152230587Sken		iph->ip_sum = 0;
2153230587Sken		iph->ip_sum = in_cksum_hdr(iph);
2154230587Sken	}
2155181643Skmacy
2156230587Sken	switch (iph->ip_p) {
2157230587Sken	case IPPROTO_TCP:
2158230587Sken		if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2159230587Sken			size_t tcplen = ntohs(iph->ip_len) - sizeof(struct ip);
2160230587Sken			struct tcphdr *th = (struct tcphdr*)(iph + 1);
2161230587Sken			th->th_sum = in_pseudo(iph->ip_src.s_addr,
2162230587Sken			    iph->ip_dst.s_addr, htons(IPPROTO_TCP + tcplen));
2163230587Sken			th->th_sum = in_cksum_skip(mbufc,
2164230587Sken			    sizeof(struct ether_header) + ntohs(iph->ip_len),
2165230587Sken			    sizeof(struct ether_header) + (iph->ip_hl << 2));
2166230587Sken		}
2167181643Skmacy		break;
2168230587Sken	case IPPROTO_UDP:
2169230587Sken		if (mbufc->m_pkthdr.csum_flags & CSUM_IP_VALID) {
2170230587Sken			size_t udplen = ntohs(iph->ip_len) - sizeof(struct ip);
2171230587Sken			struct udphdr *uh = (struct udphdr*)(iph + 1);
2172230587Sken			uh->uh_sum = in_pseudo(iph->ip_src.s_addr,
2173230587Sken			    iph->ip_dst.s_addr, htons(IPPROTO_UDP + udplen));
2174230587Sken			uh->uh_sum = in_cksum_skip(mbufc,
2175230587Sken			    sizeof(struct ether_header) + ntohs(iph->ip_len),
2176230587Sken			    sizeof(struct ether_header) + (iph->ip_hl << 2));
2177230587Sken		}
2178181643Skmacy		break;
2179230587Sken	default:
2180181643Skmacy		break;
2181181643Skmacy	}
2182181643Skmacy}
2183259541Sglebius#endif /* INET || INET6 */
2184181643Skmacy
2185181643Skmacystatic void
2186230587Skenxnb_stop(struct xnb_softc *xnb)
2187181643Skmacy{
2188230587Sken	struct ifnet *ifp;
2189181643Skmacy
2190230587Sken	mtx_assert(&xnb->sc_lock, MA_OWNED);
2191230587Sken	ifp = xnb->xnb_ifp;
2192230587Sken	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2193230587Sken	if_link_state_change(ifp, LINK_STATE_DOWN);
2194181643Skmacy}
2195181643Skmacy
2196181643Skmacystatic int
2197230587Skenxnb_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
2198181643Skmacy{
2199230587Sken	struct xnb_softc *xnb = ifp->if_softc;
2200259541Sglebius	struct ifreq *ifr = (struct ifreq*) data;
2201230587Sken#ifdef INET
2202230587Sken	struct ifaddr *ifa = (struct ifaddr*)data;
2203230587Sken#endif
2204230587Sken	int error = 0;
2205181643Skmacy
2206230587Sken	switch (cmd) {
2207230587Sken		case SIOCSIFFLAGS:
2208230587Sken			mtx_lock(&xnb->sc_lock);
2209230587Sken			if (ifp->if_flags & IFF_UP) {
2210230587Sken				xnb_ifinit_locked(xnb);
2211230587Sken			} else {
2212230587Sken				if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2213230587Sken					xnb_stop(xnb);
2214230587Sken				}
2215230587Sken			}
2216230587Sken			/*
2217230587Sken			 * Note: netfront sets a variable named xn_if_flags
2218230587Sken			 * here, but that variable is never read
2219230587Sken			 */
2220230587Sken			mtx_unlock(&xnb->sc_lock);
2221230587Sken			break;
2222230587Sken		case SIOCSIFADDR:
2223230587Sken		case SIOCGIFADDR:
2224230587Sken#ifdef INET
2225230587Sken			mtx_lock(&xnb->sc_lock);
2226230587Sken			if (ifa->ifa_addr->sa_family == AF_INET) {
2227230587Sken				ifp->if_flags |= IFF_UP;
2228230587Sken				if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2229230587Sken					ifp->if_drv_flags &= ~(IFF_DRV_RUNNING |
2230230587Sken							IFF_DRV_OACTIVE);
2231230587Sken					if_link_state_change(ifp,
2232230587Sken							LINK_STATE_DOWN);
2233230587Sken					ifp->if_drv_flags |= IFF_DRV_RUNNING;
2234230587Sken					ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2235230587Sken					if_link_state_change(ifp,
2236230587Sken					    LINK_STATE_UP);
2237230587Sken				}
2238230587Sken				arp_ifinit(ifp, ifa);
2239230587Sken				mtx_unlock(&xnb->sc_lock);
2240230587Sken			} else {
2241230587Sken				mtx_unlock(&xnb->sc_lock);
2242230587Sken#endif
2243230587Sken				error = ether_ioctl(ifp, cmd, data);
2244230587Sken#ifdef INET
2245230587Sken			}
2246230587Sken#endif
2247230587Sken			break;
2248230587Sken		case SIOCSIFCAP:
2249230587Sken			mtx_lock(&xnb->sc_lock);
2250230587Sken			if (ifr->ifr_reqcap & IFCAP_TXCSUM) {
2251230587Sken				ifp->if_capenable |= IFCAP_TXCSUM;
2252230587Sken				ifp->if_hwassist |= XNB_CSUM_FEATURES;
2253230587Sken			} else {
2254230587Sken				ifp->if_capenable &= ~(IFCAP_TXCSUM);
2255230587Sken				ifp->if_hwassist &= ~(XNB_CSUM_FEATURES);
2256230587Sken			}
2257230587Sken			if ((ifr->ifr_reqcap & IFCAP_RXCSUM)) {
2258230587Sken				ifp->if_capenable |= IFCAP_RXCSUM;
2259230587Sken			} else {
2260230587Sken				ifp->if_capenable &= ~(IFCAP_RXCSUM);
2261230587Sken			}
2262230587Sken			/*
2263230587Sken			 * TODO enable TSO4 and LRO once we no longer need
2264230587Sken			 * to calculate checksums in software
2265230587Sken			 */
2266230587Sken#if 0
2267230587Sken			if (ifr->if_reqcap |= IFCAP_TSO4) {
2268230587Sken				if (IFCAP_TXCSUM & ifp->if_capenable) {
2269230587Sken					printf("xnb: Xen netif requires that "
2270230587Sken						"TXCSUM be enabled in order "
2271230587Sken						"to use TSO4\n");
2272230587Sken					error = EINVAL;
2273230587Sken				} else {
2274230587Sken					ifp->if_capenable |= IFCAP_TSO4;
2275230587Sken					ifp->if_hwassist |= CSUM_TSO;
2276230587Sken				}
2277230587Sken			} else {
2278230587Sken				ifp->if_capenable &= ~(IFCAP_TSO4);
2279230587Sken				ifp->if_hwassist &= ~(CSUM_TSO);
2280230587Sken			}
2281230587Sken			if (ifr->ifreqcap |= IFCAP_LRO) {
2282230587Sken				ifp->if_capenable |= IFCAP_LRO;
2283230587Sken			} else {
2284230587Sken				ifp->if_capenable &= ~(IFCAP_LRO);
2285230587Sken			}
2286230587Sken#endif
2287230587Sken			mtx_unlock(&xnb->sc_lock);
2288230587Sken			break;
2289230587Sken		case SIOCSIFMTU:
2290230587Sken			ifp->if_mtu = ifr->ifr_mtu;
2291230587Sken			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2292230587Sken			xnb_ifinit(xnb);
2293230587Sken			break;
2294230587Sken		case SIOCADDMULTI:
2295230587Sken		case SIOCDELMULTI:
2296230587Sken		case SIOCSIFMEDIA:
2297230587Sken		case SIOCGIFMEDIA:
2298230587Sken			error = ifmedia_ioctl(ifp, ifr, &xnb->sc_media, cmd);
2299230587Sken			break;
2300230587Sken		default:
2301230587Sken			error = ether_ioctl(ifp, cmd, data);
2302230587Sken			break;
2303181643Skmacy	}
2304230587Sken	return (error);
2305230587Sken}
2306181643Skmacy
2307230587Skenstatic void
2308230587Skenxnb_start_locked(struct ifnet *ifp)
2309230587Sken{
2310230587Sken	netif_rx_back_ring_t *rxb;
2311230587Sken	struct xnb_softc *xnb;
2312230587Sken	struct mbuf *mbufc;
2313230587Sken	RING_IDX req_prod_local;
2314181643Skmacy
2315230587Sken	xnb = ifp->if_softc;
2316230587Sken	rxb = &xnb->ring_configs[XNB_RING_TYPE_RX].back_ring.rx_ring;
2317181643Skmacy
2318230587Sken	if (!xnb->carrier)
2319230587Sken		return;
2320181643Skmacy
2321230587Sken	do {
2322230587Sken		int out_of_space = 0;
2323230587Sken		int notify;
2324230587Sken		req_prod_local = rxb->sring->req_prod;
2325230587Sken		xen_rmb();
2326230587Sken		for (;;) {
2327230587Sken			int error;
2328181643Skmacy
2329230587Sken			IF_DEQUEUE(&ifp->if_snd, mbufc);
2330230587Sken			if (mbufc == NULL)
2331230587Sken				break;
2332230587Sken			error = xnb_send(rxb, xnb->otherend_id, mbufc,
2333230587Sken			    		 xnb->rx_gnttab);
2334230587Sken			switch (error) {
2335230587Sken				case EAGAIN:
2336230587Sken					/*
2337230587Sken					 * Insufficient space in the ring.
2338230587Sken					 * Requeue pkt and send when space is
2339230587Sken					 * available.
2340230587Sken					 */
2341230587Sken					IF_PREPEND(&ifp->if_snd, mbufc);
2342230587Sken					/*
2343230587Sken					 * Perhaps the frontend missed an IRQ
2344230587Sken					 * and went to sleep.  Notify it to wake
2345230587Sken					 * it up.
2346230587Sken					 */
2347230587Sken					out_of_space = 1;
2348230587Sken					break;
2349181643Skmacy
2350230587Sken				case EINVAL:
2351230587Sken					/* OS gave a corrupt packet.  Drop it.*/
2352230587Sken					ifp->if_oerrors++;
2353230587Sken					/* FALLTHROUGH */
2354230587Sken				default:
2355230587Sken					/* Send succeeded, or packet had error.
2356230587Sken					 * Free the packet */
2357230587Sken					ifp->if_opackets++;
2358230587Sken					if (mbufc)
2359230587Sken						m_freem(mbufc);
2360230587Sken					break;
2361230587Sken			}
2362230587Sken			if (out_of_space != 0)
2363230587Sken				break;
2364230587Sken		}
2365181643Skmacy
2366230587Sken		RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(rxb, notify);
2367230587Sken		if ((notify != 0) || (out_of_space != 0))
2368255040Sgibbs			xen_intr_signal(xnb->xen_intr_handle);
2369230587Sken		rxb->sring->req_event = req_prod_local + 1;
2370230587Sken		xen_mb();
2371230587Sken	} while (rxb->sring->req_prod != req_prod_local) ;
2372181643Skmacy}
2373181643Skmacy
2374230587Sken/**
2375230587Sken * Sends one packet to the ring.  Blocks until the packet is on the ring
2376230587Sken * \param[in]	mbufc	Contains one packet to send.  Caller must free
2377230587Sken * \param[in,out] rxb	The packet will be pushed onto this ring, but the
2378230587Sken * 			otherend will not be notified.
2379230587Sken * \param[in]	otherend The domain ID of the other end of the connection
2380230587Sken * \retval	EAGAIN	The ring did not have enough space for the packet.
2381230587Sken * 			The ring has not been modified
2382230587Sken * \param[in,out] gnttab Pointer to enough memory for a grant table.  We make
2383230587Sken * 			this a function parameter so that we will take less
2384230587Sken * 			stack space.
2385230587Sken * \retval EINVAL	mbufc was corrupt or not convertible into a pkt
2386230587Sken */
2387181643Skmacystatic int
2388230587Skenxnb_send(netif_rx_back_ring_t *ring, domid_t otherend, const struct mbuf *mbufc,
2389230587Sken	 gnttab_copy_table gnttab)
2390181643Skmacy{
2391230587Sken	struct xnb_pkt pkt;
2392230587Sken	int error, n_entries, n_reqs;
2393230587Sken	RING_IDX space;
2394181643Skmacy
2395230587Sken	space = ring->sring->req_prod - ring->req_cons;
2396230587Sken	error = xnb_mbufc2pkt(mbufc, &pkt, ring->rsp_prod_pvt, space);
2397230587Sken	if (error != 0)
2398230587Sken		return error;
2399230587Sken	n_entries = xnb_rxpkt2gnttab(&pkt, mbufc, gnttab, ring, otherend);
2400230587Sken	if (n_entries != 0) {
2401230587Sken		int __unused hv_ret = HYPERVISOR_grant_table_op(GNTTABOP_copy,
2402230587Sken		    gnttab, n_entries);
2403230587Sken		KASSERT(hv_ret == 0, ("HYPERVISOR_grant_table_op returned %d\n",
2404230587Sken		    hv_ret));
2405181643Skmacy	}
2406181643Skmacy
2407230587Sken	n_reqs = xnb_rxpkt2rsp(&pkt, gnttab, n_entries, ring);
2408181643Skmacy
2409230587Sken	return 0;
2410181643Skmacy}
2411181643Skmacy
2412230587Skenstatic void
2413230587Skenxnb_start(struct ifnet *ifp)
2414181643Skmacy{
2415230587Sken	struct xnb_softc *xnb;
2416230587Sken
2417230587Sken	xnb = ifp->if_softc;
2418230587Sken	mtx_lock(&xnb->rx_lock);
2419230587Sken	xnb_start_locked(ifp);
2420230587Sken	mtx_unlock(&xnb->rx_lock);
2421181643Skmacy}
2422181643Skmacy
2423230587Sken/* equivalent of network_open() in Linux */
2424230587Skenstatic void
2425230587Skenxnb_ifinit_locked(struct xnb_softc *xnb)
2426181643Skmacy{
2427230587Sken	struct ifnet *ifp;
2428181643Skmacy
2429230587Sken	ifp = xnb->xnb_ifp;
2430181643Skmacy
2431230587Sken	mtx_assert(&xnb->sc_lock, MA_OWNED);
2432181643Skmacy
2433230587Sken	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2434230587Sken		return;
2435181643Skmacy
2436230587Sken	xnb_stop(xnb);
2437181643Skmacy
2438230587Sken	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2439230587Sken	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2440230587Sken	if_link_state_change(ifp, LINK_STATE_UP);
2441181643Skmacy}
2442181643Skmacy
2443230587Sken
2444230587Skenstatic void
2445230587Skenxnb_ifinit(void *xsc)
2446181643Skmacy{
2447230587Sken	struct xnb_softc *xnb = xsc;
2448181643Skmacy
2449230587Sken	mtx_lock(&xnb->sc_lock);
2450230587Sken	xnb_ifinit_locked(xnb);
2451230587Sken	mtx_unlock(&xnb->sc_lock);
2452230587Sken}
2453181643Skmacy
2454181643Skmacy
2455230587Sken/**
2456230587Sken * Read the 'mac' node at the given device's node in the store, and parse that
2457230587Sken * as colon-separated octets, placing result the given mac array.  mac must be
2458230587Sken * a preallocated array of length ETHER_ADDR_LEN ETH_ALEN (as declared in
2459230587Sken * net/ethernet.h).
2460230587Sken * Return 0 on success, or errno on error.
2461230587Sken */
2462230587Skenstatic int
2463230587Skenxen_net_read_mac(device_t dev, uint8_t mac[])
2464230587Sken{
2465230587Sken	char *s, *e, *macstr;
2466230587Sken	const char *path;
2467230587Sken	int error = 0;
2468230587Sken	int i;
2469181643Skmacy
2470230587Sken	path = xenbus_get_node(dev);
2471230587Sken	error = xs_read(XST_NIL, path, "mac", NULL, (void **) &macstr);
2472230587Sken	if (error != 0) {
2473230587Sken		xenbus_dev_fatal(dev, error, "parsing %s/mac", path);
2474230587Sken	} else {
2475230587Sken	        s = macstr;
2476230587Sken	        for (i = 0; i < ETHER_ADDR_LEN; i++) {
2477230587Sken		        mac[i] = strtoul(s, &e, 16);
2478230587Sken		        if (s == e || (e[0] != ':' && e[0] != 0)) {
2479230587Sken				error = ENOENT;
2480230587Sken				break;
2481230587Sken		        }
2482230587Sken		        s = &e[1];
2483230587Sken	        }
2484230587Sken	        free(macstr, M_XENBUS);
2485230587Sken	}
2486230587Sken	return error;
2487230587Sken}
2488181643Skmacy
2489181643Skmacy
2490230587Sken/**
2491230587Sken * Callback used by the generic networking code to tell us when our carrier
2492230587Sken * state has changed.  Since we don't have a physical carrier, we don't care
2493230587Sken */
2494230587Skenstatic int
2495230587Skenxnb_ifmedia_upd(struct ifnet *ifp)
2496230587Sken{
2497230587Sken	return (0);
2498230587Sken}
2499181643Skmacy
2500230587Sken/**
2501230587Sken * Callback used by the generic networking code to ask us what our carrier
2502230587Sken * state is.  Since we don't have a physical carrier, this is very simple
2503230587Sken */
2504230587Skenstatic void
2505230587Skenxnb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2506230587Sken{
2507230587Sken	ifmr->ifm_status = IFM_AVALID|IFM_ACTIVE;
2508230587Sken	ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
2509181643Skmacy}
2510181643Skmacy
2511230587Sken
2512230587Sken/*---------------------------- NewBus Registration ---------------------------*/
2513230587Skenstatic device_method_t xnb_methods[] = {
2514181643Skmacy	/* Device interface */
2515230587Sken	DEVMETHOD(device_probe,		xnb_probe),
2516230587Sken	DEVMETHOD(device_attach,	xnb_attach),
2517230587Sken	DEVMETHOD(device_detach,	xnb_detach),
2518181643Skmacy	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
2519230587Sken	DEVMETHOD(device_suspend,	xnb_suspend),
2520230587Sken	DEVMETHOD(device_resume,	xnb_resume),
2521181643Skmacy
2522230587Sken	/* Xenbus interface */
2523230587Sken	DEVMETHOD(xenbus_otherend_changed, xnb_frontend_changed),
2524181643Skmacy
2525230587Sken	{ 0, 0 }
2526181643Skmacy};
2527181643Skmacy
2528230587Skenstatic driver_t xnb_driver = {
2529230587Sken	"xnb",
2530230587Sken	xnb_methods,
2531230587Sken	sizeof(struct xnb_softc),
2532230587Sken};
2533230587Skendevclass_t xnb_devclass;
2534181643Skmacy
2535230587SkenDRIVER_MODULE(xnb, xenbusb_back, xnb_driver, xnb_devclass, 0, 0);
2536181643Skmacy
2537230587Sken
2538230587Sken/*-------------------------- Unit Tests -------------------------------------*/
2539230587Sken#ifdef XNB_DEBUG
2540230587Sken#include "netback_unit_tests.c"
2541230587Sken#endif
2542