xref: /freebsd/sys/dev/xen/netfront/netfront.c (revision b3aaa0cc21c63d388230c7ef2a80abd631ff20d5)
1 /*
2  *
3  * Copyright (c) 2004-2006 Kip Macy
4  * All rights reserved.
5  *
6  *
7  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
8  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
9  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
10  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
11  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
12  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
16  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  */
18 
19 
20 #include <sys/cdefs.h>
21 __FBSDID("$FreeBSD$");
22 
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/sockio.h>
26 #include <sys/mbuf.h>
27 #include <sys/lock.h>
28 #include <sys/malloc.h>
29 #include <sys/module.h>
30 #include <sys/kernel.h>
31 #include <sys/socket.h>
32 #include <sys/queue.h>
33 #include <sys/sx.h>
34 
35 #include <net/if.h>
36 #include <net/if_arp.h>
37 #include <net/ethernet.h>
38 #include <net/if_dl.h>
39 #include <net/if_media.h>
40 
41 #include <net/bpf.h>
42 
43 #include <net/if_types.h>
44 #include <net/if.h>
45 
46 #include <netinet/in_systm.h>
47 #include <netinet/in.h>
48 #include <netinet/ip.h>
49 #include <netinet/if_ether.h>
50 
51 #include <vm/vm.h>
52 #include <vm/pmap.h>
53 
54 #include <machine/clock.h>      /* for DELAY */
55 #include <machine/bus.h>
56 #include <machine/resource.h>
57 #include <machine/frame.h>
58 #include <machine/vmparam.h>
59 
60 #include <sys/bus.h>
61 #include <sys/rman.h>
62 
63 #include <machine/intr_machdep.h>
64 
65 #include <machine/xen/xen-os.h>
66 #include <xen/hypervisor.h>
67 #include <xen/xen_intr.h>
68 #include <xen/evtchn.h>
69 #include <xen/gnttab.h>
70 #include <xen/interface/memory.h>
71 #include <dev/xen/netfront/mbufq.h>
72 #include <machine/xen/features.h>
73 #include <xen/interface/io/netif.h>
74 #include <xen/xenbus/xenbusvar.h>
75 
76 #include "xenbus_if.h"
77 
78 #define GRANT_INVALID_REF	0
79 
80 #define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE)
81 #define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE)
82 
83 #ifdef CONFIG_XEN
84 static int MODPARM_rx_copy = 0;
85 module_param_named(rx_copy, MODPARM_rx_copy, bool, 0);
86 MODULE_PARM_DESC(rx_copy, "Copy packets from network card (rather than flip)");
87 static int MODPARM_rx_flip = 0;
88 module_param_named(rx_flip, MODPARM_rx_flip, bool, 0);
89 MODULE_PARM_DESC(rx_flip, "Flip packets from network card (rather than copy)");
90 #else
91 static const int MODPARM_rx_copy = 1;
92 static const int MODPARM_rx_flip = 0;
93 #endif
94 
95 #define RX_COPY_THRESHOLD 256
96 
97 #define net_ratelimit() 0
98 
99 struct netfront_info;
100 struct netfront_rx_info;
101 
102 static void xn_txeof(struct netfront_info *);
103 static void xn_rxeof(struct netfront_info *);
104 static void network_alloc_rx_buffers(struct netfront_info *);
105 
106 static void xn_tick_locked(struct netfront_info *);
107 static void xn_tick(void *);
108 
109 static void xn_intr(void *);
110 static void xn_start_locked(struct ifnet *);
111 static void xn_start(struct ifnet *);
112 static int  xn_ioctl(struct ifnet *, u_long, caddr_t);
113 static void xn_ifinit_locked(struct netfront_info *);
114 static void xn_ifinit(void *);
115 static void xn_stop(struct netfront_info *);
116 #ifdef notyet
117 static void xn_watchdog(struct ifnet *);
118 #endif
119 
120 static void show_device(struct netfront_info *sc);
121 #ifdef notyet
122 static void netfront_closing(device_t dev);
123 #endif
124 static void netif_free(struct netfront_info *info);
125 static int netfront_detach(device_t dev);
126 
127 static int talk_to_backend(device_t dev, struct netfront_info *info);
128 static int create_netdev(device_t dev);
129 static void netif_disconnect_backend(struct netfront_info *info);
130 static int setup_device(device_t dev, struct netfront_info *info);
131 static void end_access(int ref, void *page);
132 
133 /* Xenolinux helper functions */
134 int network_connect(struct netfront_info *);
135 
136 static void xn_free_rx_ring(struct netfront_info *);
137 
138 static void xn_free_tx_ring(struct netfront_info *);
139 
140 static int xennet_get_responses(struct netfront_info *np,
141 	struct netfront_rx_info *rinfo, RING_IDX rp, struct mbuf **list,
142 	int *pages_flipped_p);
143 
144 #define virt_to_mfn(x) (vtomach(x) >> PAGE_SHIFT)
145 
146 #define INVALID_P2M_ENTRY (~0UL)
147 
148 /*
149  * Mbuf pointers. We need these to keep track of the virtual addresses
150  * of our mbuf chains since we can only convert from virtual to physical,
151  * not the other way around.  The size must track the free index arrays.
152  */
153 struct xn_chain_data {
154 		struct mbuf		*xn_tx_chain[NET_TX_RING_SIZE+1];
155 		struct mbuf		*xn_rx_chain[NET_RX_RING_SIZE+1];
156 };
157 
158 
159 struct net_device_stats
160 {
161 	u_long	rx_packets;		/* total packets received	*/
162 	u_long	tx_packets;		/* total packets transmitted	*/
163 	u_long	rx_bytes;		/* total bytes received 	*/
164 	u_long	tx_bytes;		/* total bytes transmitted	*/
165 	u_long	rx_errors;		/* bad packets received		*/
166 	u_long	tx_errors;		/* packet transmit problems	*/
167 	u_long	rx_dropped;		/* no space in linux buffers	*/
168 	u_long	tx_dropped;		/* no space available in linux	*/
169 	u_long	multicast;		/* multicast packets received	*/
170 	u_long	collisions;
171 
172 	/* detailed rx_errors: */
173 	u_long	rx_length_errors;
174 	u_long	rx_over_errors;		/* receiver ring buff overflow	*/
175 	u_long	rx_crc_errors;		/* recved pkt with crc error	*/
176 	u_long	rx_frame_errors;	/* recv'd frame alignment error */
177 	u_long	rx_fifo_errors;		/* recv'r fifo overrun		*/
178 	u_long	rx_missed_errors;	/* receiver missed packet	*/
179 
180 	/* detailed tx_errors */
181 	u_long	tx_aborted_errors;
182 	u_long	tx_carrier_errors;
183 	u_long	tx_fifo_errors;
184 	u_long	tx_heartbeat_errors;
185 	u_long	tx_window_errors;
186 
187 	/* for cslip etc */
188 	u_long	rx_compressed;
189 	u_long	tx_compressed;
190 };
191 
192 struct netfront_info {
193 
194 	struct ifnet *xn_ifp;
195 
196 	struct net_device_stats stats;
197 	u_int tx_full;
198 
199 	netif_tx_front_ring_t tx;
200 	netif_rx_front_ring_t rx;
201 
202 	struct mtx   tx_lock;
203 	struct mtx   rx_lock;
204 	struct sx    sc_lock;
205 
206 	u_int handle;
207 	u_int irq;
208 	u_int copying_receiver;
209 	u_int carrier;
210 
211 	/* Receive-ring batched refills. */
212 #define RX_MIN_TARGET 32
213 #define RX_MAX_TARGET NET_RX_RING_SIZE
214 	int rx_min_target, rx_max_target, rx_target;
215 
216 	/*
217 	 * {tx,rx}_skbs store outstanding skbuffs. The first entry in each
218 	 * array is an index into a chain of free entries.
219 	 */
220 
221 	grant_ref_t gref_tx_head;
222 	grant_ref_t grant_tx_ref[NET_TX_RING_SIZE + 1];
223 	grant_ref_t gref_rx_head;
224 	grant_ref_t grant_rx_ref[NET_TX_RING_SIZE + 1];
225 
226 #define TX_MAX_TARGET min(NET_RX_RING_SIZE, 256)
227 	device_t xbdev;
228 	int tx_ring_ref;
229 	int rx_ring_ref;
230 	uint8_t mac[ETHER_ADDR_LEN];
231 	struct xn_chain_data	xn_cdata;	/* mbufs */
232 	struct mbuf_head xn_rx_batch;	/* head of the batch queue */
233 
234 	int			xn_if_flags;
235 	struct callout	        xn_stat_ch;
236 
237 	u_long rx_pfn_array[NET_RX_RING_SIZE];
238 	multicall_entry_t rx_mcl[NET_RX_RING_SIZE+1];
239 	mmu_update_t rx_mmu[NET_RX_RING_SIZE];
240 };
241 
242 #define rx_mbufs xn_cdata.xn_rx_chain
243 #define tx_mbufs xn_cdata.xn_tx_chain
244 
245 #define XN_LOCK_INIT(_sc, _name) \
246         mtx_init(&(_sc)->tx_lock, #_name"_tx", "network transmit lock", MTX_DEF); \
247         mtx_init(&(_sc)->rx_lock, #_name"_rx", "network receive lock", MTX_DEF);  \
248         sx_init(&(_sc)->sc_lock, #_name"_rx")
249 
250 #define XN_RX_LOCK(_sc)           mtx_lock(&(_sc)->rx_lock)
251 #define XN_RX_UNLOCK(_sc)         mtx_unlock(&(_sc)->rx_lock)
252 
253 #define XN_TX_LOCK(_sc)           mtx_lock(&(_sc)->tx_lock)
254 #define XN_TX_UNLOCK(_sc)         mtx_unlock(&(_sc)->tx_lock)
255 
256 #define XN_LOCK(_sc)           sx_xlock(&(_sc)->sc_lock);
257 #define XN_UNLOCK(_sc)         sx_xunlock(&(_sc)->sc_lock);
258 
259 #define XN_LOCK_ASSERT(_sc)    sx_assert(&(_sc)->sc_lock, SX_LOCKED);
260 #define XN_RX_LOCK_ASSERT(_sc)    mtx_assert(&(_sc)->rx_lock, MA_OWNED);
261 #define XN_TX_LOCK_ASSERT(_sc)    mtx_assert(&(_sc)->tx_lock, MA_OWNED);
262 #define XN_LOCK_DESTROY(_sc)   mtx_destroy(&(_sc)->rx_lock); \
263                                mtx_destroy(&(_sc)->tx_lock); \
264                                sx_destroy(&(_sc)->sc_lock);
265 
266 struct netfront_rx_info {
267 	struct netif_rx_response rx;
268 	struct netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1];
269 };
270 
271 #define netfront_carrier_on(netif)	((netif)->carrier = 1)
272 #define netfront_carrier_off(netif)	((netif)->carrier = 0)
273 #define netfront_carrier_ok(netif)	((netif)->carrier)
274 
275 /* Access macros for acquiring freeing slots in xn_free_{tx,rx}_idxs[]. */
276 
277 
278 
279 /*
280  * Access macros for acquiring freeing slots in tx_skbs[].
281  */
282 
283 static inline void
284 add_id_to_freelist(struct mbuf **list, unsigned short id)
285 {
286 	list[id] = list[0];
287 	list[0]  = (void *)(u_long)id;
288 }
289 
290 static inline unsigned short
291 get_id_from_freelist(struct mbuf **list)
292 {
293 	u_int id = (u_int)(u_long)list[0];
294 	list[0] = list[id];
295 	return (id);
296 }
297 
298 static inline int
299 xennet_rxidx(RING_IDX idx)
300 {
301 	return idx & (NET_RX_RING_SIZE - 1);
302 }
303 
304 static inline struct mbuf *
305 xennet_get_rx_mbuf(struct netfront_info *np,
306 						RING_IDX ri)
307 {
308 	int i = xennet_rxidx(ri);
309 	struct mbuf *m;
310 
311 	m = np->rx_mbufs[i];
312 	np->rx_mbufs[i] = NULL;
313 	return (m);
314 }
315 
316 static inline grant_ref_t
317 xennet_get_rx_ref(struct netfront_info *np, RING_IDX ri)
318 {
319 	int i = xennet_rxidx(ri);
320 	grant_ref_t ref = np->grant_rx_ref[i];
321 	np->grant_rx_ref[i] = GRANT_INVALID_REF;
322 	return ref;
323 }
324 
325 #ifdef DEBUG
326 
327 #endif
328 #define IPRINTK(fmt, args...) \
329     printf("[XEN] " fmt, ##args)
330 #define WPRINTK(fmt, args...) \
331     printf("[XEN] " fmt, ##args)
332 #define DPRINTK(fmt, args...) \
333     printf("[XEN] %s: " fmt, __func__, ##args)
334 
335 
336 static __inline struct mbuf*
337 makembuf (struct mbuf *buf)
338 {
339 	struct mbuf *m = NULL;
340 
341         MGETHDR (m, M_DONTWAIT, MT_DATA);
342 
343         if (! m)
344 		return 0;
345 
346 	M_MOVE_PKTHDR(m, buf);
347 
348 	m_cljget(m, M_DONTWAIT, MJUMPAGESIZE);
349         m->m_pkthdr.len = buf->m_pkthdr.len;
350         m->m_len = buf->m_len;
351 	m_copydata(buf, 0, buf->m_pkthdr.len, mtod(m,caddr_t) );
352 
353 		m->m_ext.ext_arg1 = (caddr_t *)(uintptr_t)(vtophys(mtod(m,caddr_t)) >> PAGE_SHIFT);
354 
355        	return m;
356 }
357 
358 /**
359  * Read the 'mac' node at the given device's node in the store, and parse that
360  * as colon-separated octets, placing result the given mac array.  mac must be
361  * a preallocated array of length ETH_ALEN (as declared in linux/if_ether.h).
362  * Return 0 on success, or errno on error.
363  */
364 static int
365 xen_net_read_mac(device_t dev, uint8_t mac[])
366 {
367 	int error, i;
368 	char *s, *e, *macstr;
369 
370 	error = xenbus_read(XBT_NIL, xenbus_get_node(dev), "mac", NULL,
371 	    (void **) &macstr);
372 	if (error)
373 		return (error);
374 
375 	s = macstr;
376 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
377 		mac[i] = strtoul(s, &e, 16);
378 		if (s == e || (e[0] != ':' && e[0] != 0)) {
379 			free(macstr, M_DEVBUF);
380 			return (ENOENT);
381 		}
382 		s = &e[1];
383 	}
384 	free(macstr, M_DEVBUF);
385 	return (0);
386 }
387 
388 /**
389  * Entry point to this code when a new device is created.  Allocate the basic
390  * structures and the ring buffers for communication with the backend, and
391  * inform the backend of the appropriate details for those.  Switch to
392  * Connected state.
393  */
394 static int
395 netfront_probe(device_t dev)
396 {
397 
398 	if (!strcmp(xenbus_get_type(dev), "vif")) {
399 		device_set_desc(dev, "Virtual Network Interface");
400 		return (0);
401 	}
402 
403 	return (ENXIO);
404 }
405 
406 static int
407 netfront_attach(device_t dev)
408 {
409 	int err;
410 
411 	err = create_netdev(dev);
412 	if (err) {
413 		xenbus_dev_fatal(dev, err, "creating netdev");
414 		return err;
415 	}
416 
417 	return 0;
418 }
419 
420 
421 /**
422  * We are reconnecting to the backend, due to a suspend/resume, or a backend
423  * driver restart.  We tear down our netif structure and recreate it, but
424  * leave the device-layer structures intact so that this is transparent to the
425  * rest of the kernel.
426  */
427 static int
428 netfront_resume(device_t dev)
429 {
430 	struct netfront_info *info = device_get_softc(dev);
431 
432 	netif_disconnect_backend(info);
433 	return (0);
434 }
435 
436 
437 /* Common code used when first setting up, and when resuming. */
438 static int
439 talk_to_backend(device_t dev, struct netfront_info *info)
440 {
441 	const char *message;
442 	struct xenbus_transaction xbt;
443 	const char *node = xenbus_get_node(dev);
444 	int err;
445 
446 	err = xen_net_read_mac(dev, info->mac);
447 	if (err) {
448 		xenbus_dev_fatal(dev, err, "parsing %s/mac", node);
449 		goto out;
450 	}
451 
452 	/* Create shared ring, alloc event channel. */
453 	err = setup_device(dev, info);
454 	if (err)
455 		goto out;
456 
457  again:
458 	err = xenbus_transaction_start(&xbt);
459 	if (err) {
460 		xenbus_dev_fatal(dev, err, "starting transaction");
461 		goto destroy_ring;
462 	}
463 	err = xenbus_printf(xbt, node, "tx-ring-ref","%u",
464 			    info->tx_ring_ref);
465 	if (err) {
466 		message = "writing tx ring-ref";
467 		goto abort_transaction;
468 	}
469 	err = xenbus_printf(xbt, node, "rx-ring-ref","%u",
470 			    info->rx_ring_ref);
471 	if (err) {
472 		message = "writing rx ring-ref";
473 		goto abort_transaction;
474 	}
475 	err = xenbus_printf(xbt, node,
476 		"event-channel", "%u", irq_to_evtchn_port(info->irq));
477 	if (err) {
478 		message = "writing event-channel";
479 		goto abort_transaction;
480 	}
481 	err = xenbus_printf(xbt, node, "request-rx-copy", "%u",
482 			    info->copying_receiver);
483 	if (err) {
484 		message = "writing request-rx-copy";
485 		goto abort_transaction;
486 	}
487 	err = xenbus_printf(xbt, node, "feature-rx-notify", "%d", 1);
488 	if (err) {
489 		message = "writing feature-rx-notify";
490 		goto abort_transaction;
491 	}
492 	err = xenbus_printf(xbt, node, "feature-no-csum-offload", "%d", 1);
493 	if (err) {
494 		message = "writing feature-no-csum-offload";
495 		goto abort_transaction;
496 	}
497 	err = xenbus_printf(xbt, node, "feature-sg", "%d", 1);
498 	if (err) {
499 		message = "writing feature-sg";
500 		goto abort_transaction;
501 	}
502 #ifdef HAVE_TSO
503 	err = xenbus_printf(xbt, node, "feature-gso-tcpv4", "%d", 1);
504 	if (err) {
505 		message = "writing feature-gso-tcpv4";
506 		goto abort_transaction;
507 	}
508 #endif
509 
510 	err = xenbus_transaction_end(xbt, 0);
511 	if (err) {
512 		if (err == EAGAIN)
513 			goto again;
514 		xenbus_dev_fatal(dev, err, "completing transaction");
515 		goto destroy_ring;
516 	}
517 
518 	return 0;
519 
520  abort_transaction:
521 	xenbus_transaction_end(xbt, 1);
522 	xenbus_dev_fatal(dev, err, "%s", message);
523  destroy_ring:
524 	netif_free(info);
525  out:
526 	return err;
527 }
528 
529 
530 static int
531 setup_device(device_t dev, struct netfront_info *info)
532 {
533 	netif_tx_sring_t *txs;
534 	netif_rx_sring_t *rxs;
535 	int error;
536 	struct ifnet *ifp;
537 
538 	ifp = info->xn_ifp;
539 
540 	info->tx_ring_ref = GRANT_INVALID_REF;
541 	info->rx_ring_ref = GRANT_INVALID_REF;
542 	info->rx.sring = NULL;
543 	info->tx.sring = NULL;
544 	info->irq = 0;
545 
546 	txs = (netif_tx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO);
547 	if (!txs) {
548 		error = ENOMEM;
549 		xenbus_dev_fatal(dev, error, "allocating tx ring page");
550 		goto fail;
551 	}
552 	SHARED_RING_INIT(txs);
553 	FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
554 	error = xenbus_grant_ring(dev, virt_to_mfn(txs), &info->tx_ring_ref);
555 	if (error)
556 		goto fail;
557 
558 	rxs = (netif_rx_sring_t *)malloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT|M_ZERO);
559 	if (!rxs) {
560 		error = ENOMEM;
561 		xenbus_dev_fatal(dev, error, "allocating rx ring page");
562 		goto fail;
563 	}
564 	SHARED_RING_INIT(rxs);
565 	FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
566 
567 	error = xenbus_grant_ring(dev, virt_to_mfn(rxs), &info->rx_ring_ref);
568 	if (error)
569 		goto fail;
570 
571 	error = bind_listening_port_to_irqhandler(xenbus_get_otherend_id(dev),
572 	         "xn", xn_intr, info, INTR_TYPE_NET | INTR_MPSAFE, &info->irq);
573 
574 	if (error) {
575 		xenbus_dev_fatal(dev, error,
576 				 "bind_evtchn_to_irqhandler failed");
577 		goto fail;
578 	}
579 
580 	show_device(info);
581 
582 	return (0);
583 
584  fail:
585 	netif_free(info);
586 	return (error);
587 }
588 
589 /**
590  * Callback received when the backend's state changes.
591  */
592 static void
593 netfront_backend_changed(device_t dev, XenbusState newstate)
594 {
595 	struct netfront_info *sc = device_get_softc(dev);
596 
597 	DPRINTK("newstate=%d\n", newstate);
598 
599 	switch (newstate) {
600 	case XenbusStateInitialising:
601 	case XenbusStateInitialised:
602 	case XenbusStateConnected:
603 	case XenbusStateUnknown:
604 	case XenbusStateClosed:
605 	case XenbusStateReconfigured:
606 	case XenbusStateReconfiguring:
607 		break;
608 	case XenbusStateInitWait:
609 		if (xenbus_get_state(dev) != XenbusStateInitialising)
610 			break;
611 		if (network_connect(sc) != 0)
612 			break;
613 		xenbus_set_state(dev, XenbusStateConnected);
614 #ifdef notyet
615 		(void)send_fake_arp(netdev);
616 #endif
617 		break;
618 	case XenbusStateClosing:
619 		xenbus_set_state(dev, XenbusStateClosed);
620 		break;
621 	}
622 }
623 
624 static void
625 xn_free_rx_ring(struct netfront_info *sc)
626 {
627 #if 0
628 	int i;
629 
630 	for (i = 0; i < NET_RX_RING_SIZE; i++) {
631 		if (sc->xn_cdata.xn_rx_chain[i] != NULL) {
632 			m_freem(sc->xn_cdata.xn_rx_chain[i]);
633 			sc->xn_cdata.xn_rx_chain[i] = NULL;
634 		}
635 	}
636 
637 	sc->rx.rsp_cons = 0;
638 	sc->xn_rx_if->req_prod = 0;
639 	sc->xn_rx_if->event = sc->rx.rsp_cons ;
640 #endif
641 }
642 
643 static void
644 xn_free_tx_ring(struct netfront_info *sc)
645 {
646 #if 0
647 	int i;
648 
649 	for (i = 0; i < NET_TX_RING_SIZE; i++) {
650 		if (sc->xn_cdata.xn_tx_chain[i] != NULL) {
651 			m_freem(sc->xn_cdata.xn_tx_chain[i]);
652 			sc->xn_cdata.xn_tx_chain[i] = NULL;
653 		}
654 	}
655 
656 	return;
657 #endif
658 }
659 
660 static inline int
661 netfront_tx_slot_available(struct netfront_info *np)
662 {
663 	return ((np->tx.req_prod_pvt - np->tx.rsp_cons) <
664 		(TX_MAX_TARGET - /* MAX_SKB_FRAGS */ 24 - 2));
665 }
666 static void
667 netif_release_tx_bufs(struct netfront_info *np)
668 {
669 	struct mbuf *m;
670 	int i;
671 
672 	for (i = 1; i <= NET_TX_RING_SIZE; i++) {
673 		m = np->xn_cdata.xn_tx_chain[i];
674 
675 		if (((u_long)m) < KERNBASE)
676 			continue;
677 		gnttab_grant_foreign_access_ref(np->grant_tx_ref[i],
678 		    xenbus_get_otherend_id(np->xbdev),
679 		    virt_to_mfn(mtod(m, vm_offset_t)),
680 		    GNTMAP_readonly);
681 		gnttab_release_grant_reference(&np->gref_tx_head,
682 		    np->grant_tx_ref[i]);
683 		np->grant_tx_ref[i] = GRANT_INVALID_REF;
684 		add_id_to_freelist(np->tx_mbufs, i);
685 		m_freem(m);
686 	}
687 }
688 
689 static void
690 network_alloc_rx_buffers(struct netfront_info *sc)
691 {
692 	int otherend_id = xenbus_get_otherend_id(sc->xbdev);
693 	unsigned short id;
694 	struct mbuf *m_new;
695 	int i, batch_target, notify;
696 	RING_IDX req_prod;
697 	struct xen_memory_reservation reservation;
698 	grant_ref_t ref;
699 	int nr_flips;
700 	netif_rx_request_t *req;
701 	vm_offset_t vaddr;
702 	u_long pfn;
703 
704 	req_prod = sc->rx.req_prod_pvt;
705 
706 	if (unlikely(sc->carrier == 0))
707 		return;
708 
709 	/*
710 	 * Allocate skbuffs greedily, even though we batch updates to the
711 	 * receive ring. This creates a less bursty demand on the memory
712 	 * allocator, so should reduce the chance of failed allocation
713 	 * requests both for ourself and for other kernel subsystems.
714 	 */
715 	batch_target = sc->rx_target - (req_prod - sc->rx.rsp_cons);
716 	for (i = mbufq_len(&sc->xn_rx_batch); i < batch_target; i++) {
717 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
718 		if (m_new == NULL)
719 			goto no_mbuf;
720 
721 		m_cljget(m_new, M_DONTWAIT, MJUMPAGESIZE);
722 		if ((m_new->m_flags & M_EXT) == 0) {
723 			m_freem(m_new);
724 
725 no_mbuf:
726 			if (i != 0)
727 				goto refill;
728 			/*
729 			 * XXX set timer
730 			 */
731 			break;
732 		}
733 		m_new->m_len = m_new->m_pkthdr.len = MJUMPAGESIZE;
734 
735 		/* queue the mbufs allocated */
736 		mbufq_tail(&sc->xn_rx_batch, m_new);
737 	}
738 
739 	/* Is the batch large enough to be worthwhile? */
740 	if (i < (sc->rx_target/2)) {
741 		if (req_prod >sc->rx.sring->req_prod)
742 			goto push;
743 		return;
744 	}
745 	/* Adjust floating fill target if we risked running out of buffers. */
746 	if ( ((req_prod - sc->rx.sring->rsp_prod) < (sc->rx_target / 4)) &&
747 	     ((sc->rx_target *= 2) > sc->rx_max_target) )
748 		sc->rx_target = sc->rx_max_target;
749 
750 refill:
751 	for (nr_flips = i = 0; ; i++) {
752 		if ((m_new = mbufq_dequeue(&sc->xn_rx_batch)) == NULL)
753 			break;
754 
755 		m_new->m_ext.ext_arg1 = (vm_paddr_t *)(uintptr_t)(
756 				vtophys(m_new->m_ext.ext_buf) >> PAGE_SHIFT);
757 
758 		id = xennet_rxidx(req_prod + i);
759 
760 		KASSERT(sc->xn_cdata.xn_rx_chain[id] == NULL,
761 		    ("non-NULL xm_rx_chain"));
762 		sc->xn_cdata.xn_rx_chain[id] = m_new;
763 
764 		ref = gnttab_claim_grant_reference(&sc->gref_rx_head);
765 		KASSERT((short)ref >= 0, ("negative ref"));
766 		sc->grant_rx_ref[id] = ref;
767 
768 		vaddr = mtod(m_new, vm_offset_t);
769 		pfn = vtophys(vaddr) >> PAGE_SHIFT;
770 		req = RING_GET_REQUEST(&sc->rx, req_prod + i);
771 
772 		if (sc->copying_receiver == 0) {
773 			gnttab_grant_foreign_transfer_ref(ref,
774 			    otherend_id, pfn);
775 			sc->rx_pfn_array[nr_flips] = PFNTOMFN(pfn);
776 			if (!xen_feature(XENFEAT_auto_translated_physmap)) {
777 				/* Remove this page before passing
778 				 * back to Xen.
779 				 */
780 				set_phys_to_machine(pfn, INVALID_P2M_ENTRY);
781 				MULTI_update_va_mapping(&sc->rx_mcl[i],
782 				    vaddr, 0, 0);
783 			}
784 			nr_flips++;
785 		} else {
786 			gnttab_grant_foreign_access_ref(ref,
787 			    otherend_id,
788 			    PFNTOMFN(pfn), 0);
789 		}
790 		req->id = id;
791 		req->gref = ref;
792 
793 		sc->rx_pfn_array[i] =
794 		    vtomach(mtod(m_new,vm_offset_t)) >> PAGE_SHIFT;
795 	}
796 
797 	KASSERT(i, ("no mbufs processed")); /* should have returned earlier */
798 	KASSERT(mbufq_len(&sc->xn_rx_batch) == 0, ("not all mbufs processed"));
799 	/*
800 	 * We may have allocated buffers which have entries outstanding
801 	 * in the page * update queue -- make sure we flush those first!
802 	 */
803 	PT_UPDATES_FLUSH();
804 	if (nr_flips != 0) {
805 #ifdef notyet
806 		/* Tell the ballon driver what is going on. */
807 		balloon_update_driver_allowance(i);
808 #endif
809 		set_xen_guest_handle(reservation.extent_start, sc->rx_pfn_array);
810 		reservation.nr_extents   = i;
811 		reservation.extent_order = 0;
812 		reservation.address_bits = 0;
813 		reservation.domid        = DOMID_SELF;
814 
815 		if (!xen_feature(XENFEAT_auto_translated_physmap)) {
816 
817 			/* After all PTEs have been zapped, flush the TLB. */
818 			sc->rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] =
819 			    UVMF_TLB_FLUSH|UVMF_ALL;
820 
821 			/* Give away a batch of pages. */
822 			sc->rx_mcl[i].op = __HYPERVISOR_memory_op;
823 			sc->rx_mcl[i].args[0] = XENMEM_decrease_reservation;
824 			sc->rx_mcl[i].args[1] =  (u_long)&reservation;
825 			/* Zap PTEs and give away pages in one big multicall. */
826 			(void)HYPERVISOR_multicall(sc->rx_mcl, i+1);
827 
828 			/* Check return status of HYPERVISOR_dom_mem_op(). */
829 			if (unlikely(sc->rx_mcl[i].result != i))
830 				panic("Unable to reduce memory reservation\n");
831 			} else {
832 				if (HYPERVISOR_memory_op(
833 				    XENMEM_decrease_reservation, &reservation)
834 				    != i)
835 					panic("Unable to reduce memory "
836 					    "reservation\n");
837 		}
838 	} else {
839 		wmb();
840 	}
841 
842 	/* Above is a suitable barrier to ensure backend will see requests. */
843 	sc->rx.req_prod_pvt = req_prod + i;
844 push:
845 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->rx, notify);
846 	if (notify)
847 		notify_remote_via_irq(sc->irq);
848 }
849 
850 static void
851 xn_rxeof(struct netfront_info *np)
852 {
853 	struct ifnet *ifp;
854 	struct netfront_rx_info rinfo;
855 	struct netif_rx_response *rx = &rinfo.rx;
856 	struct netif_extra_info *extras = rinfo.extras;
857 	RING_IDX i, rp;
858 	multicall_entry_t *mcl;
859 	struct mbuf *m;
860 	struct mbuf_head rxq, errq;
861 	int err, pages_flipped = 0, work_to_do;
862 
863 	do {
864 		XN_RX_LOCK_ASSERT(np);
865 		if (!netfront_carrier_ok(np))
866 			return;
867 
868 		mbufq_init(&errq);
869 		mbufq_init(&rxq);
870 
871 		ifp = np->xn_ifp;
872 
873 		rp = np->rx.sring->rsp_prod;
874 		rmb();	/* Ensure we see queued responses up to 'rp'. */
875 
876 		i = np->rx.rsp_cons;
877 		while ((i != rp)) {
878 			memcpy(rx, RING_GET_RESPONSE(&np->rx, i), sizeof(*rx));
879 			memset(extras, 0, sizeof(rinfo.extras));
880 
881 			m = NULL;
882 			err = xennet_get_responses(np, &rinfo, rp, &m,
883 			    &pages_flipped);
884 
885 			if (unlikely(err)) {
886 				if (m)
887 					mbufq_tail(&errq, m);
888 				np->stats.rx_errors++;
889 				i = np->rx.rsp_cons;
890 				continue;
891 			}
892 
893 			m->m_pkthdr.rcvif = ifp;
894 			if ( rx->flags & NETRXF_data_validated ) {
895 				/* Tell the stack the checksums are okay */
896 				/*
897 				 * XXX this isn't necessarily the case - need to add
898 				 * check
899 				 */
900 
901 				m->m_pkthdr.csum_flags |=
902 					(CSUM_IP_CHECKED | CSUM_IP_VALID | CSUM_DATA_VALID
903 					    | CSUM_PSEUDO_HDR);
904 				m->m_pkthdr.csum_data = 0xffff;
905 			}
906 
907 			np->stats.rx_packets++;
908 			np->stats.rx_bytes += m->m_pkthdr.len;
909 
910 			mbufq_tail(&rxq, m);
911 			np->rx.rsp_cons = ++i;
912 		}
913 
914 		if (pages_flipped) {
915 			/* Some pages are no longer absent... */
916 #ifdef notyet
917 			balloon_update_driver_allowance(-pages_flipped);
918 #endif
919 			/* Do all the remapping work, and M->P updates, in one big
920 			 * hypercall.
921 			 */
922 			if (!!xen_feature(XENFEAT_auto_translated_physmap)) {
923 				mcl = np->rx_mcl + pages_flipped;
924 				mcl->op = __HYPERVISOR_mmu_update;
925 				mcl->args[0] = (u_long)np->rx_mmu;
926 				mcl->args[1] = pages_flipped;
927 				mcl->args[2] = 0;
928 				mcl->args[3] = DOMID_SELF;
929 				(void)HYPERVISOR_multicall(np->rx_mcl,
930 				    pages_flipped + 1);
931 			}
932 		}
933 
934 		while ((m = mbufq_dequeue(&errq)))
935 			m_freem(m);
936 
937 		/*
938 		 * Process all the mbufs after the remapping is complete.
939 		 * Break the mbuf chain first though.
940 		 */
941 		while ((m = mbufq_dequeue(&rxq)) != NULL) {
942 			ifp->if_ipackets++;
943 
944 			/*
945 			 * Do we really need to drop the rx lock?
946 			 */
947 			XN_RX_UNLOCK(np);
948 			/* Pass it up. */
949 			(*ifp->if_input)(ifp, m);
950 			XN_RX_LOCK(np);
951 		}
952 
953 		np->rx.rsp_cons = i;
954 
955 #if 0
956 		/* If we get a callback with very few responses, reduce fill target. */
957 		/* NB. Note exponential increase, linear decrease. */
958 		if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
959 			((3*np->rx_target) / 4)) && (--np->rx_target < np->rx_min_target))
960 			np->rx_target = np->rx_min_target;
961 #endif
962 
963 		network_alloc_rx_buffers(np);
964 
965 		RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, work_to_do);
966 	} while (work_to_do);
967 }
968 
969 static void
970 xn_txeof(struct netfront_info *np)
971 {
972 	RING_IDX i, prod;
973 	unsigned short id;
974 	struct ifnet *ifp;
975 	struct mbuf *m;
976 
977 	XN_TX_LOCK_ASSERT(np);
978 
979 	if (!netfront_carrier_ok(np))
980 		return;
981 
982 	ifp = np->xn_ifp;
983 	ifp->if_timer = 0;
984 
985 	do {
986 		prod = np->tx.sring->rsp_prod;
987 		rmb(); /* Ensure we see responses up to 'rp'. */
988 
989 		for (i = np->tx.rsp_cons; i != prod; i++) {
990 			id = RING_GET_RESPONSE(&np->tx, i)->id;
991 			m = np->xn_cdata.xn_tx_chain[id];
992 
993 			ifp->if_opackets++;
994 			KASSERT(m != NULL, ("mbuf not found in xn_tx_chain"));
995 			M_ASSERTVALID(m);
996 			if (unlikely(gnttab_query_foreign_access(
997 			    np->grant_tx_ref[id]) != 0)) {
998 				printf("network_tx_buf_gc: warning "
999 				    "-- grant still in use by backend "
1000 				    "domain.\n");
1001 				goto out;
1002 			}
1003 			gnttab_end_foreign_access_ref(
1004 				np->grant_tx_ref[id]);
1005 			gnttab_release_grant_reference(
1006 				&np->gref_tx_head, np->grant_tx_ref[id]);
1007 			np->grant_tx_ref[id] = GRANT_INVALID_REF;
1008 
1009 			np->xn_cdata.xn_tx_chain[id] = NULL;
1010 			add_id_to_freelist(np->xn_cdata.xn_tx_chain, id);
1011 			m_freem(m);
1012 		}
1013 		np->tx.rsp_cons = prod;
1014 
1015 		/*
1016 		 * Set a new event, then check for race with update of
1017 		 * tx_cons. Note that it is essential to schedule a
1018 		 * callback, no matter how few buffers are pending. Even if
1019 		 * there is space in the transmit ring, higher layers may
1020 		 * be blocked because too much data is outstanding: in such
1021 		 * cases notification from Xen is likely to be the only kick
1022 		 * that we'll get.
1023 		 */
1024 		np->tx.sring->rsp_event =
1025 		    prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
1026 
1027 		mb();
1028 
1029 	} while (prod != np->tx.sring->rsp_prod);
1030 
1031  out:
1032 	if (np->tx_full &&
1033 	    ((np->tx.sring->req_prod - prod) < NET_TX_RING_SIZE)) {
1034 		np->tx_full = 0;
1035 #if 0
1036 		if (np->user_state == UST_OPEN)
1037 			netif_wake_queue(dev);
1038 #endif
1039 	}
1040 
1041 }
1042 
1043 static void
1044 xn_intr(void *xsc)
1045 {
1046 	struct netfront_info *np = xsc;
1047 	struct ifnet *ifp = np->xn_ifp;
1048 
1049 #if 0
1050 	if (!(np->rx.rsp_cons != np->rx.sring->rsp_prod &&
1051 	    likely(netfront_carrier_ok(np)) &&
1052 	    ifp->if_drv_flags & IFF_DRV_RUNNING))
1053 		return;
1054 #endif
1055 	if (np->tx.rsp_cons != np->tx.sring->rsp_prod) {
1056 		XN_TX_LOCK(np);
1057 		xn_txeof(np);
1058 		XN_TX_UNLOCK(np);
1059 	}
1060 
1061 	XN_RX_LOCK(np);
1062 	xn_rxeof(np);
1063 	XN_RX_UNLOCK(np);
1064 
1065 	if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1066 	    !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1067 		xn_start(ifp);
1068 }
1069 
1070 
1071 static void
1072 xennet_move_rx_slot(struct netfront_info *np, struct mbuf *m,
1073 	grant_ref_t ref)
1074 {
1075 	int new = xennet_rxidx(np->rx.req_prod_pvt);
1076 
1077 	KASSERT(np->rx_mbufs[new] == NULL, ("rx_mbufs != NULL"));
1078 	np->rx_mbufs[new] = m;
1079 	np->grant_rx_ref[new] = ref;
1080 	RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id = new;
1081 	RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref = ref;
1082 	np->rx.req_prod_pvt++;
1083 }
1084 
1085 static int
1086 xennet_get_extras(struct netfront_info *np,
1087     struct netif_extra_info *extras, RING_IDX rp)
1088 {
1089 	struct netif_extra_info *extra;
1090 	RING_IDX cons = np->rx.rsp_cons;
1091 
1092 	int err = 0;
1093 
1094 	do {
1095 		struct mbuf *m;
1096 		grant_ref_t ref;
1097 
1098 		if (unlikely(cons + 1 == rp)) {
1099 #if 0
1100 			if (net_ratelimit())
1101 				WPRINTK("Missing extra info\n");
1102 #endif
1103 			err = -EINVAL;
1104 			break;
1105 		}
1106 
1107 		extra = (struct netif_extra_info *)
1108 		RING_GET_RESPONSE(&np->rx, ++cons);
1109 
1110 		if (unlikely(!extra->type ||
1111 			extra->type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
1112 #if 0
1113 			if (net_ratelimit())
1114 				WPRINTK("Invalid extra type: %d\n",
1115 					extra->type);
1116 #endif
1117 			err = -EINVAL;
1118 		} else {
1119 			memcpy(&extras[extra->type - 1], extra, sizeof(*extra));
1120 		}
1121 
1122 		m = xennet_get_rx_mbuf(np, cons);
1123 		ref = xennet_get_rx_ref(np, cons);
1124 		xennet_move_rx_slot(np, m, ref);
1125 	} while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE);
1126 
1127 	np->rx.rsp_cons = cons;
1128 	return err;
1129 }
1130 
1131 static int
1132 xennet_get_responses(struct netfront_info *np,
1133 	struct netfront_rx_info *rinfo, RING_IDX rp,
1134 	struct mbuf  **list,
1135 	int *pages_flipped_p)
1136 {
1137 	int pages_flipped = *pages_flipped_p;
1138 	struct mmu_update *mmu;
1139 	struct multicall_entry *mcl;
1140 	struct netif_rx_response *rx = &rinfo->rx;
1141 	struct netif_extra_info *extras = rinfo->extras;
1142 	RING_IDX cons = np->rx.rsp_cons;
1143 	struct mbuf *m, *m0, *m_prev;
1144 	grant_ref_t ref = xennet_get_rx_ref(np, cons);
1145 	int max = 5 /* MAX_SKB_FRAGS + (rx->status <= RX_COPY_THRESHOLD) */;
1146 	int frags = 1;
1147 	int err = 0;
1148 	u_long ret;
1149 
1150 	m0 = m = m_prev = xennet_get_rx_mbuf(np, cons);
1151 
1152 
1153 	if (rx->flags & NETRXF_extra_info) {
1154 		err = xennet_get_extras(np, extras, rp);
1155 		cons = np->rx.rsp_cons;
1156 	}
1157 
1158 
1159 	if (m0 != NULL) {
1160 			m0->m_pkthdr.len = 0;
1161 			m0->m_next = NULL;
1162 	}
1163 
1164 	for (;;) {
1165 		u_long mfn;
1166 
1167 #if 0
1168 		printf("rx->status=%hd rx->offset=%hu frags=%u\n",
1169 			rx->status, rx->offset, frags);
1170 #endif
1171 		if (unlikely(rx->status < 0 ||
1172 			rx->offset + rx->status > PAGE_SIZE)) {
1173 #if 0
1174 			if (net_ratelimit())
1175 				WPRINTK("rx->offset: %x, size: %u\n",
1176 					rx->offset, rx->status);
1177 #endif
1178 			xennet_move_rx_slot(np, m, ref);
1179 			err = -EINVAL;
1180 			goto next;
1181 		}
1182 
1183 		/*
1184 		 * This definitely indicates a bug, either in this driver or in
1185 		 * the backend driver. In future this should flag the bad
1186 		 * situation to the system controller to reboot the backed.
1187 		 */
1188 		if (ref == GRANT_INVALID_REF) {
1189 #if 0
1190 			if (net_ratelimit())
1191 				WPRINTK("Bad rx response id %d.\n", rx->id);
1192 #endif
1193 			err = -EINVAL;
1194 			goto next;
1195 		}
1196 
1197 		if (!np->copying_receiver) {
1198 			/* Memory pressure, insufficient buffer
1199 			 * headroom, ...
1200 			 */
1201 			if (!(mfn = gnttab_end_foreign_transfer_ref(ref))) {
1202 				if (net_ratelimit())
1203 					WPRINTK("Unfulfilled rx req "
1204 						"(id=%d, st=%d).\n",
1205 						rx->id, rx->status);
1206 				xennet_move_rx_slot(np, m, ref);
1207 				err = -ENOMEM;
1208 				goto next;
1209 			}
1210 
1211 			if (!xen_feature( XENFEAT_auto_translated_physmap)) {
1212 				/* Remap the page. */
1213 				void *vaddr = mtod(m, void *);
1214 				uint32_t pfn;
1215 
1216 				mcl = np->rx_mcl + pages_flipped;
1217 				mmu = np->rx_mmu + pages_flipped;
1218 
1219 				MULTI_update_va_mapping(mcl, (u_long)vaddr,
1220 				    (((vm_paddr_t)mfn) << PAGE_SHIFT) | PG_RW |
1221 				    PG_V | PG_M | PG_A, 0);
1222 				pfn = (uintptr_t)m->m_ext.ext_arg1;
1223 				mmu->ptr = ((vm_paddr_t)mfn << PAGE_SHIFT) |
1224 				    MMU_MACHPHYS_UPDATE;
1225 				mmu->val = pfn;
1226 
1227 				set_phys_to_machine(pfn, mfn);
1228 			}
1229 			pages_flipped++;
1230 		} else {
1231 			ret = gnttab_end_foreign_access_ref(ref);
1232 			KASSERT(ret, ("ret != 0"));
1233 		}
1234 
1235 		gnttab_release_grant_reference(&np->gref_rx_head, ref);
1236 
1237 next:
1238 		if (m == NULL)
1239 			break;
1240 
1241 		m->m_len = rx->status;
1242 		m->m_data += rx->offset;
1243 		m0->m_pkthdr.len += rx->status;
1244 
1245 		if (!(rx->flags & NETRXF_more_data))
1246 			break;
1247 
1248 		if (cons + frags == rp) {
1249 			if (net_ratelimit())
1250 				WPRINTK("Need more frags\n");
1251 			err = -ENOENT;
1252 				break;
1253 		}
1254 		m_prev = m;
1255 
1256 		rx = RING_GET_RESPONSE(&np->rx, cons + frags);
1257 		m = xennet_get_rx_mbuf(np, cons + frags);
1258 
1259 		m_prev->m_next = m;
1260 		m->m_next = NULL;
1261 		ref = xennet_get_rx_ref(np, cons + frags);
1262 		frags++;
1263 	}
1264 	*list = m0;
1265 
1266 	if (unlikely(frags > max)) {
1267 		if (net_ratelimit())
1268 			WPRINTK("Too many frags\n");
1269 		err = -E2BIG;
1270 	}
1271 
1272 	if (unlikely(err))
1273 		np->rx.rsp_cons = cons + frags;
1274 
1275 	*pages_flipped_p = pages_flipped;
1276 
1277 	return err;
1278 }
1279 
1280 static void
1281 xn_tick_locked(struct netfront_info *sc)
1282 {
1283 	XN_RX_LOCK_ASSERT(sc);
1284 	callout_reset(&sc->xn_stat_ch, hz, xn_tick, sc);
1285 
1286 	/* XXX placeholder for printing debug information */
1287 
1288 }
1289 
1290 
1291 static void
1292 xn_tick(void *xsc)
1293 {
1294 	struct netfront_info *sc;
1295 
1296 	sc = xsc;
1297 	XN_RX_LOCK(sc);
1298 	xn_tick_locked(sc);
1299 	XN_RX_UNLOCK(sc);
1300 
1301 }
1302 static void
1303 xn_start_locked(struct ifnet *ifp)
1304 {
1305 	int otherend_id;
1306 	unsigned short id;
1307 	struct mbuf *m_head, *new_m;
1308 	struct netfront_info *sc;
1309 	netif_tx_request_t *tx;
1310 	RING_IDX i;
1311 	grant_ref_t ref;
1312 	u_long mfn, tx_bytes;
1313 	int notify;
1314 
1315 	sc = ifp->if_softc;
1316 	otherend_id = xenbus_get_otherend_id(sc->xbdev);
1317 	tx_bytes = 0;
1318 
1319 	if (!netfront_carrier_ok(sc))
1320 		return;
1321 
1322 	for (i = sc->tx.req_prod_pvt; TRUE; i++) {
1323 		IF_DEQUEUE(&ifp->if_snd, m_head);
1324 		if (m_head == NULL)
1325 			break;
1326 
1327 		if (!netfront_tx_slot_available(sc)) {
1328 			IF_PREPEND(&ifp->if_snd, m_head);
1329 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1330 			break;
1331 		}
1332 
1333 		id = get_id_from_freelist(sc->xn_cdata.xn_tx_chain);
1334 
1335 		/*
1336 		 * Start packing the mbufs in this chain into
1337 		 * the fragment pointers. Stop when we run out
1338 		 * of fragments or hit the end of the mbuf chain.
1339 		 */
1340 		new_m = makembuf(m_head);
1341 		tx = RING_GET_REQUEST(&sc->tx, i);
1342 		tx->id = id;
1343 		ref = gnttab_claim_grant_reference(&sc->gref_tx_head);
1344 		KASSERT((short)ref >= 0, ("Negative ref"));
1345 		mfn = virt_to_mfn(mtod(new_m, vm_offset_t));
1346 		gnttab_grant_foreign_access_ref(ref, otherend_id,
1347 		    mfn, GNTMAP_readonly);
1348 		tx->gref = sc->grant_tx_ref[id] = ref;
1349 		tx->size = new_m->m_pkthdr.len;
1350 #if 0
1351 		tx->flags = (skb->ip_summed == CHECKSUM_HW) ? NETTXF_csum_blank : 0;
1352 #endif
1353 		tx->flags = 0;
1354 		new_m->m_next = NULL;
1355 		new_m->m_nextpkt = NULL;
1356 
1357 		m_freem(m_head);
1358 
1359 		sc->xn_cdata.xn_tx_chain[id] = new_m;
1360 		BPF_MTAP(ifp, new_m);
1361 
1362 		sc->stats.tx_bytes += new_m->m_pkthdr.len;
1363 		sc->stats.tx_packets++;
1364 	}
1365 
1366 	sc->tx.req_prod_pvt = i;
1367 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&sc->tx, notify);
1368 	if (notify)
1369 		notify_remote_via_irq(sc->irq);
1370 
1371 	xn_txeof(sc);
1372 
1373 	if (RING_FULL(&sc->tx)) {
1374 		sc->tx_full = 1;
1375 #if 0
1376 		netif_stop_queue(dev);
1377 #endif
1378 	}
1379 
1380 	return;
1381 }
1382 
1383 static void
1384 xn_start(struct ifnet *ifp)
1385 {
1386 	struct netfront_info *sc;
1387 	sc = ifp->if_softc;
1388 	XN_TX_LOCK(sc);
1389 	xn_start_locked(ifp);
1390 	XN_TX_UNLOCK(sc);
1391 }
1392 
1393 /* equivalent of network_open() in Linux */
1394 static void
1395 xn_ifinit_locked(struct netfront_info *sc)
1396 {
1397 	struct ifnet *ifp;
1398 
1399 	XN_LOCK_ASSERT(sc);
1400 
1401 	ifp = sc->xn_ifp;
1402 
1403 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1404 		return;
1405 
1406 	xn_stop(sc);
1407 
1408 	network_alloc_rx_buffers(sc);
1409 	sc->rx.sring->rsp_event = sc->rx.rsp_cons + 1;
1410 
1411 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1412 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1413 
1414 	callout_reset(&sc->xn_stat_ch, hz, xn_tick, sc);
1415 
1416 }
1417 
1418 
1419 static void
1420 xn_ifinit(void *xsc)
1421 {
1422 	struct netfront_info *sc = xsc;
1423 
1424 	XN_LOCK(sc);
1425 	xn_ifinit_locked(sc);
1426 	XN_UNLOCK(sc);
1427 
1428 }
1429 
1430 
1431 static int
1432 xn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1433 {
1434 	struct netfront_info *sc = ifp->if_softc;
1435 	struct ifreq *ifr = (struct ifreq *) data;
1436 	struct ifaddr *ifa = (struct ifaddr *)data;
1437 
1438 	int mask, error = 0;
1439 	switch(cmd) {
1440 	case SIOCSIFADDR:
1441 	case SIOCGIFADDR:
1442 		XN_LOCK(sc);
1443 		if (ifa->ifa_addr->sa_family == AF_INET) {
1444 			ifp->if_flags |= IFF_UP;
1445 			if (!(ifp->if_drv_flags & IFF_DRV_RUNNING))
1446 				xn_ifinit_locked(sc);
1447 			arp_ifinit(ifp, ifa);
1448 			XN_UNLOCK(sc);
1449 		} else {
1450 			XN_UNLOCK(sc);
1451 			error = ether_ioctl(ifp, cmd, data);
1452 		}
1453 		break;
1454 	case SIOCSIFMTU:
1455 		/* XXX can we alter the MTU on a VN ?*/
1456 #ifdef notyet
1457 		if (ifr->ifr_mtu > XN_JUMBO_MTU)
1458 			error = EINVAL;
1459 		else
1460 #endif
1461 		{
1462 			ifp->if_mtu = ifr->ifr_mtu;
1463 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1464 			xn_ifinit(sc);
1465 		}
1466 		break;
1467 	case SIOCSIFFLAGS:
1468 		XN_LOCK(sc);
1469 		if (ifp->if_flags & IFF_UP) {
1470 			/*
1471 			 * If only the state of the PROMISC flag changed,
1472 			 * then just use the 'set promisc mode' command
1473 			 * instead of reinitializing the entire NIC. Doing
1474 			 * a full re-init means reloading the firmware and
1475 			 * waiting for it to start up, which may take a
1476 			 * second or two.
1477 			 */
1478 #ifdef notyet
1479 			/* No promiscuous mode with Xen */
1480 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1481 			    ifp->if_flags & IFF_PROMISC &&
1482 			    !(sc->xn_if_flags & IFF_PROMISC)) {
1483 				XN_SETBIT(sc, XN_RX_MODE,
1484 					  XN_RXMODE_RX_PROMISC);
1485 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1486 				   !(ifp->if_flags & IFF_PROMISC) &&
1487 				   sc->xn_if_flags & IFF_PROMISC) {
1488 				XN_CLRBIT(sc, XN_RX_MODE,
1489 					  XN_RXMODE_RX_PROMISC);
1490 			} else
1491 #endif
1492 				xn_ifinit_locked(sc);
1493 		} else {
1494 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1495 				xn_stop(sc);
1496 			}
1497 		}
1498 		sc->xn_if_flags = ifp->if_flags;
1499 		XN_UNLOCK(sc);
1500 		error = 0;
1501 		break;
1502 	case SIOCSIFCAP:
1503 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1504 		if (mask & IFCAP_HWCSUM) {
1505 			if (IFCAP_HWCSUM & ifp->if_capenable)
1506 				ifp->if_capenable &= ~IFCAP_HWCSUM;
1507 			else
1508 				ifp->if_capenable |= IFCAP_HWCSUM;
1509 		}
1510 		error = 0;
1511 		break;
1512 	case SIOCADDMULTI:
1513 	case SIOCDELMULTI:
1514 #ifdef notyet
1515 		if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
1516 			XN_LOCK(sc);
1517 			xn_setmulti(sc);
1518 			XN_UNLOCK(sc);
1519 			error = 0;
1520 		}
1521 #endif
1522 		/* FALLTHROUGH */
1523 	case SIOCSIFMEDIA:
1524 	case SIOCGIFMEDIA:
1525 		error = EINVAL;
1526 		break;
1527 	default:
1528 		error = ether_ioctl(ifp, cmd, data);
1529 	}
1530 
1531 	return (error);
1532 }
1533 
1534 static void
1535 xn_stop(struct netfront_info *sc)
1536 {
1537 	struct ifnet *ifp;
1538 
1539 	XN_LOCK_ASSERT(sc);
1540 
1541 	ifp = sc->xn_ifp;
1542 
1543 	callout_stop(&sc->xn_stat_ch);
1544 
1545 	xn_free_rx_ring(sc);
1546 	xn_free_tx_ring(sc);
1547 
1548 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
1549 }
1550 
1551 /* START of Xenolinux helper functions adapted to FreeBSD */
1552 int
1553 network_connect(struct netfront_info *np)
1554 {
1555 	int i, requeue_idx, error;
1556 	grant_ref_t ref;
1557 	netif_rx_request_t *req;
1558 	u_int feature_rx_copy, feature_rx_flip;
1559 
1560 	error = xenbus_scanf(XBT_NIL, xenbus_get_otherend_path(np->xbdev),
1561 	    "feature-rx-copy", NULL, "%u", &feature_rx_copy);
1562 	if (error)
1563 		feature_rx_copy = 0;
1564 	error = xenbus_scanf(XBT_NIL, xenbus_get_otherend_path(np->xbdev),
1565 	    "feature-rx-flip", NULL, "%u", &feature_rx_flip);
1566 	if (error)
1567 		feature_rx_flip = 1;
1568 
1569 	/*
1570 	 * Copy packets on receive path if:
1571 	 *  (a) This was requested by user, and the backend supports it; or
1572 	 *  (b) Flipping was requested, but this is unsupported by the backend.
1573 	 */
1574 	np->copying_receiver = ((MODPARM_rx_copy && feature_rx_copy) ||
1575 				(MODPARM_rx_flip && !feature_rx_flip));
1576 
1577 	XN_LOCK(np);
1578 	/* Recovery procedure: */
1579 	error = talk_to_backend(np->xbdev, np);
1580 	if (error)
1581 		return (error);
1582 
1583 	/* Step 1: Reinitialise variables. */
1584 	netif_release_tx_bufs(np);
1585 
1586 	/* Step 2: Rebuild the RX buffer freelist and the RX ring itself. */
1587 	for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++) {
1588 		struct mbuf *m;
1589 		u_long pfn;
1590 
1591 		if (np->rx_mbufs[i] == NULL)
1592 			continue;
1593 
1594 		m = np->rx_mbufs[requeue_idx] = xennet_get_rx_mbuf(np, i);
1595 		ref = np->grant_rx_ref[requeue_idx] = xennet_get_rx_ref(np, i);
1596 		req = RING_GET_REQUEST(&np->rx, requeue_idx);
1597 		pfn = vtophys(mtod(m, vm_offset_t)) >> PAGE_SHIFT;
1598 
1599 		if (!np->copying_receiver) {
1600 			gnttab_grant_foreign_transfer_ref(ref,
1601 			    xenbus_get_otherend_id(np->xbdev),
1602 			    pfn);
1603 		} else {
1604 			gnttab_grant_foreign_access_ref(ref,
1605 			    xenbus_get_otherend_id(np->xbdev),
1606 			    PFNTOMFN(pfn), 0);
1607 		}
1608 		req->gref = ref;
1609 		req->id   = requeue_idx;
1610 
1611 		requeue_idx++;
1612 	}
1613 
1614 	np->rx.req_prod_pvt = requeue_idx;
1615 
1616 	/* Step 3: All public and private state should now be sane.  Get
1617 	 * ready to start sending and receiving packets and give the driver
1618 	 * domain a kick because we've probably just requeued some
1619 	 * packets.
1620 	 */
1621 	netfront_carrier_on(np);
1622 	notify_remote_via_irq(np->irq);
1623 	XN_TX_LOCK(np);
1624 	xn_txeof(np);
1625 	XN_TX_UNLOCK(np);
1626 	network_alloc_rx_buffers(np);
1627 	XN_UNLOCK(np);
1628 
1629 	return (0);
1630 }
1631 
1632 static void
1633 show_device(struct netfront_info *sc)
1634 {
1635 #ifdef DEBUG
1636 	if (sc) {
1637 		IPRINTK("<vif handle=%u %s(%s) evtchn=%u irq=%u tx=%p rx=%p>\n",
1638 			sc->xn_ifno,
1639 			be_state_name[sc->xn_backend_state],
1640 			sc->xn_user_state ? "open" : "closed",
1641 			sc->xn_evtchn,
1642 			sc->xn_irq,
1643 			sc->xn_tx_if,
1644 			sc->xn_rx_if);
1645 	} else {
1646 		IPRINTK("<vif NULL>\n");
1647 	}
1648 #endif
1649 }
1650 
1651 /** Create a network device.
1652  * @param handle device handle
1653  */
1654 int
1655 create_netdev(device_t dev)
1656 {
1657 	int i;
1658 	struct netfront_info *np;
1659 	int err;
1660 	struct ifnet *ifp;
1661 
1662 	np = device_get_softc(dev);
1663 
1664 	np->xbdev         = dev;
1665 
1666 	XN_LOCK_INIT(np, xennetif);
1667 	np->rx_target     = RX_MIN_TARGET;
1668 	np->rx_min_target = RX_MIN_TARGET;
1669 	np->rx_max_target = RX_MAX_TARGET;
1670 
1671 	/* Initialise {tx,rx}_skbs to be a free chain containing every entry. */
1672 	for (i = 0; i <= NET_TX_RING_SIZE; i++) {
1673 		np->tx_mbufs[i] = (void *) ((u_long) i+1);
1674 		np->grant_tx_ref[i] = GRANT_INVALID_REF;
1675 	}
1676 	for (i = 0; i <= NET_RX_RING_SIZE; i++) {
1677 		np->rx_mbufs[i] = NULL;
1678 		np->grant_rx_ref[i] = GRANT_INVALID_REF;
1679 	}
1680 	/* A grant for every tx ring slot */
1681 	if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1682 					  &np->gref_tx_head) < 0) {
1683 		printf("#### netfront can't alloc tx grant refs\n");
1684 		err = ENOMEM;
1685 		goto exit;
1686 	}
1687 	/* A grant for every rx ring slot */
1688 	if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1689 					  &np->gref_rx_head) < 0) {
1690 		printf("#### netfront can't alloc rx grant refs\n");
1691 		gnttab_free_grant_references(np->gref_tx_head);
1692 		err = ENOMEM;
1693 		goto exit;
1694 	}
1695 
1696 	err = xen_net_read_mac(dev, np->mac);
1697 	if (err) {
1698 		xenbus_dev_fatal(dev, err, "parsing %s/mac",
1699 		    xenbus_get_node(dev));
1700 		goto out;
1701 	}
1702 
1703 	/* Set up ifnet structure */
1704 	ifp = np->xn_ifp = if_alloc(IFT_ETHER);
1705     	ifp->if_softc = np;
1706     	if_initname(ifp, "xn",  device_get_unit(dev));
1707     	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1708     	ifp->if_ioctl = xn_ioctl;
1709     	ifp->if_output = ether_output;
1710     	ifp->if_start = xn_start;
1711 #ifdef notyet
1712     	ifp->if_watchdog = xn_watchdog;
1713 #endif
1714     	ifp->if_init = xn_ifinit;
1715     	ifp->if_mtu = ETHERMTU;
1716     	ifp->if_snd.ifq_maxlen = NET_TX_RING_SIZE - 1;
1717 
1718 #ifdef notyet
1719     	ifp->if_hwassist = XN_CSUM_FEATURES;
1720     	ifp->if_capabilities = IFCAP_HWCSUM;
1721     	ifp->if_capenable = ifp->if_capabilities;
1722 #endif
1723 
1724     	ether_ifattach(ifp, np->mac);
1725     	callout_init(&np->xn_stat_ch, CALLOUT_MPSAFE);
1726 	netfront_carrier_off(np);
1727 
1728 	return (0);
1729 
1730 exit:
1731 	gnttab_free_grant_references(np->gref_tx_head);
1732 out:
1733 	panic("do something smart");
1734 
1735 }
1736 
1737 /**
1738  * Handle the change of state of the backend to Closing.  We must delete our
1739  * device-layer structures now, to ensure that writes are flushed through to
1740  * the backend.  Once is this done, we can switch to Closed in
1741  * acknowledgement.
1742  */
1743 #if 0
1744 static void netfront_closing(device_t dev)
1745 {
1746 #if 0
1747 	struct netfront_info *info = dev->dev_driver_data;
1748 
1749 	DPRINTK("netfront_closing: %s removed\n", dev->nodename);
1750 
1751 	close_netdev(info);
1752 #endif
1753 	xenbus_switch_state(dev, XenbusStateClosed);
1754 }
1755 #endif
1756 
1757 static int netfront_detach(device_t dev)
1758 {
1759 	struct netfront_info *info = device_get_softc(dev);
1760 
1761 	DPRINTK("%s\n", xenbus_get_node(dev));
1762 
1763 	netif_free(info);
1764 
1765 	return 0;
1766 }
1767 
1768 
1769 static void netif_free(struct netfront_info *info)
1770 {
1771 	netif_disconnect_backend(info);
1772 #if 0
1773 	close_netdev(info);
1774 #endif
1775 }
1776 
1777 static void netif_disconnect_backend(struct netfront_info *info)
1778 {
1779 	XN_RX_LOCK(info);
1780 	XN_TX_LOCK(info);
1781 	netfront_carrier_off(info);
1782 	XN_TX_UNLOCK(info);
1783 	XN_RX_UNLOCK(info);
1784 
1785 	end_access(info->tx_ring_ref, info->tx.sring);
1786 	end_access(info->rx_ring_ref, info->rx.sring);
1787 	info->tx_ring_ref = GRANT_INVALID_REF;
1788 	info->rx_ring_ref = GRANT_INVALID_REF;
1789 	info->tx.sring = NULL;
1790 	info->rx.sring = NULL;
1791 
1792 	if (info->irq)
1793 		unbind_from_irqhandler(info->irq);
1794 
1795 	info->irq = 0;
1796 }
1797 
1798 
1799 static void end_access(int ref, void *page)
1800 {
1801 	if (ref != GRANT_INVALID_REF)
1802 		gnttab_end_foreign_access(ref, page);
1803 }
1804 
1805 /* ** Driver registration ** */
1806 static device_method_t netfront_methods[] = {
1807 	/* Device interface */
1808 	DEVMETHOD(device_probe,         netfront_probe),
1809 	DEVMETHOD(device_attach,        netfront_attach),
1810 	DEVMETHOD(device_detach,        netfront_detach),
1811 	DEVMETHOD(device_shutdown,      bus_generic_shutdown),
1812 	DEVMETHOD(device_suspend,       bus_generic_suspend),
1813 	DEVMETHOD(device_resume,        netfront_resume),
1814 
1815 	/* Xenbus interface */
1816 	DEVMETHOD(xenbus_backend_changed, netfront_backend_changed),
1817 
1818 	{ 0, 0 }
1819 };
1820 
1821 static driver_t netfront_driver = {
1822 	"xn",
1823 	netfront_methods,
1824 	sizeof(struct netfront_info),
1825 };
1826 devclass_t netfront_devclass;
1827 
1828 DRIVER_MODULE(xe, xenbus, netfront_driver, netfront_devclass, 0, 0);
1829