xref: /titanic_52/usr/src/uts/common/xen/io/xnf.c (revision 7fce3d371c58e5a8af15f2e51849d4703299ae50)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  *
31  * Copyright (c) 2004 Christian Limpach.
32  * All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  * 3. This section intentionally left blank.
43  * 4. The name of the author may not be used to endorse or promote products
44  *    derived from this software without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
50  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
51  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
55  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  */
57 /*
58  * Section 3 of the above license was updated in response to bug 6379571.
59  */
60 
61 /*
62  * xnf.c - Nemo-based network driver for domU
63  */
64 
65 #include <sys/types.h>
66 #include <sys/errno.h>
67 #include <sys/param.h>
68 #include <sys/sysmacros.h>
69 #include <sys/systm.h>
70 #include <sys/stream.h>
71 #include <sys/strsubr.h>
72 #include <sys/conf.h>
73 #include <sys/ddi.h>
74 #include <sys/devops.h>
75 #include <sys/sunddi.h>
76 #include <sys/sunndi.h>
77 #include <sys/dlpi.h>
78 #include <sys/ethernet.h>
79 #include <sys/strsun.h>
80 #include <sys/pattr.h>
81 #include <inet/ip.h>
82 #include <sys/modctl.h>
83 #include <sys/mac.h>
84 #include <sys/mac_ether.h>
85 #include <sys/bootinfo.h>
86 #include <sys/mach_mmu.h>
87 #ifdef	XPV_HVM_DRIVER
88 #include <sys/xpv_support.h>
89 #include <sys/hypervisor.h>
90 #else
91 #include <sys/hypervisor.h>
92 #include <sys/evtchn_impl.h>
93 #include <sys/balloon_impl.h>
94 #endif
95 #include <xen/public/io/netif.h>
96 #include <sys/gnttab.h>
97 #include <xen/sys/xendev.h>
98 #include <sys/sdt.h>
99 
100 #include <io/xnf.h>
101 
102 
103 /*
104  *  Declarations and Module Linkage
105  */
106 
107 #define	IDENT	"Virtual Ethernet driver"
108 
109 #if defined(DEBUG) || defined(__lint)
110 #define	XNF_DEBUG
111 int	xnfdebug = 0;
112 #endif
113 
114 /*
115  * On a 32 bit PAE system physical and machine addresses are larger
116  * than 32 bits.  ddi_btop() on such systems take an unsigned long
117  * argument, and so addresses above 4G are truncated before ddi_btop()
118  * gets to see them.  To avoid this, code the shift operation here.
119  */
120 #define	xnf_btop(addr)	((addr) >> PAGESHIFT)
121 
122 boolean_t	xnf_cksum_offload = B_TRUE;
123 
124 /* Default value for hypervisor-based copy operations */
125 boolean_t	xnf_rx_hvcopy = B_TRUE;
126 
127 /*
128  * Should pages used for transmit be readonly for the peer?
129  */
130 boolean_t	xnf_tx_pages_readonly = B_FALSE;
131 /*
132  * Packets under this size are bcopied instead of using desballoc.
133  * Choose a value > XNF_FRAMESIZE (1514) to force the receive path to
134  * always copy.
135  */
136 unsigned int	xnf_rx_bcopy_thresh = 64;
137 
138 unsigned int	xnf_max_tx_frags = 1;
139 
140 /* Required system entry points */
141 static int	xnf_attach(dev_info_t *, ddi_attach_cmd_t);
142 static int	xnf_detach(dev_info_t *, ddi_detach_cmd_t);
143 
144 /* Required driver entry points for Nemo */
145 static int	xnf_start(void *);
146 static void	xnf_stop(void *);
147 static int	xnf_set_mac_addr(void *, const uint8_t *);
148 static int	xnf_set_multicast(void *, boolean_t, const uint8_t *);
149 static int	xnf_set_promiscuous(void *, boolean_t);
150 static mblk_t	*xnf_send(void *, mblk_t *);
151 static uint_t	xnf_intr(caddr_t);
152 static int	xnf_stat(void *, uint_t, uint64_t *);
153 static void	xnf_blank(void *, time_t, uint_t);
154 static void	xnf_resources(void *);
155 static void	xnf_ioctl(void *, queue_t *, mblk_t *);
156 static boolean_t xnf_getcapab(void *, mac_capab_t, void *);
157 
158 /* Driver private functions */
159 static int xnf_alloc_dma_resources(xnf_t *);
160 static void xnf_release_dma_resources(xnf_t *);
161 static mblk_t *xnf_process_recv(xnf_t *);
162 static void xnf_rcv_complete(struct xnf_buffer_desc *);
163 static void xnf_release_mblks(xnf_t *);
164 static struct xnf_buffer_desc *xnf_alloc_tx_buffer(xnf_t *);
165 static struct xnf_buffer_desc *xnf_alloc_buffer(xnf_t *);
166 static struct xnf_buffer_desc *xnf_get_tx_buffer(xnf_t *);
167 static struct xnf_buffer_desc *xnf_get_buffer(xnf_t *);
168 static void xnf_free_buffer(struct xnf_buffer_desc *);
169 static void xnf_free_tx_buffer(struct xnf_buffer_desc *);
170 void xnf_send_driver_status(int, int);
171 static void rx_buffer_hang(xnf_t *, struct xnf_buffer_desc *);
172 static int xnf_clean_tx_ring(xnf_t  *);
173 static void oe_state_change(dev_info_t *, ddi_eventcookie_t,
174     void *, void *);
175 static mblk_t *xnf_process_hvcopy_recv(xnf_t *xnfp);
176 static boolean_t xnf_hvcopy_peer_status(dev_info_t *devinfo);
177 static boolean_t xnf_kstat_init(xnf_t *xnfp);
178 
179 /*
180  * XXPV dme: remove MC_IOCTL?
181  */
182 static mac_callbacks_t xnf_callbacks = {
183 	MC_RESOURCES | MC_IOCTL | MC_GETCAPAB,
184 	xnf_stat,
185 	xnf_start,
186 	xnf_stop,
187 	xnf_set_promiscuous,
188 	xnf_set_multicast,
189 	xnf_set_mac_addr,
190 	xnf_send,
191 	xnf_resources,
192 	xnf_ioctl,
193 	xnf_getcapab
194 };
195 
196 #define	GRANT_INVALID_REF	0
197 const int xnf_rx_bufs_lowat = 4 * NET_RX_RING_SIZE;
198 const int xnf_rx_bufs_hiwat = 8 * NET_RX_RING_SIZE; /* default max */
199 
200 /* DMA attributes for network ring buffer */
201 static ddi_dma_attr_t ringbuf_dma_attr = {
202 	DMA_ATTR_V0,		/* version of this structure */
203 	0,			/* lowest usable address */
204 	0xffffffffffffffffULL,	/* highest usable address */
205 	0x7fffffff,		/* maximum DMAable byte count */
206 	MMU_PAGESIZE,		/* alignment in bytes */
207 	0x7ff,			/* bitmap of burst sizes */
208 	1,			/* minimum transfer */
209 	0xffffffffU,		/* maximum transfer */
210 	0xffffffffffffffffULL,	/* maximum segment length */
211 	1,			/* maximum number of segments */
212 	1,			/* granularity */
213 	0,			/* flags (reserved) */
214 };
215 
216 /* DMA attributes for transmit data */
217 static ddi_dma_attr_t tx_buffer_dma_attr = {
218 	DMA_ATTR_V0,		/* version of this structure */
219 	0,			/* lowest usable address */
220 	0xffffffffffffffffULL,	/* highest usable address */
221 	0x7fffffff,		/* maximum DMAable byte count */
222 	MMU_PAGESIZE,		/* alignment in bytes */
223 	0x7ff,			/* bitmap of burst sizes */
224 	1,			/* minimum transfer */
225 	0xffffffffU,		/* maximum transfer */
226 	0xffffffffffffffffULL,	/* maximum segment length */
227 	1,			/* maximum number of segments */
228 	1,			/* granularity */
229 	0,			/* flags (reserved) */
230 };
231 
232 /* DMA attributes for a receive buffer */
233 static ddi_dma_attr_t rx_buffer_dma_attr = {
234 	DMA_ATTR_V0,		/* version of this structure */
235 	0,			/* lowest usable address */
236 	0xffffffffffffffffULL,	/* highest usable address */
237 	0x7fffffff,		/* maximum DMAable byte count */
238 	MMU_PAGESIZE,		/* alignment in bytes */
239 	0x7ff,			/* bitmap of burst sizes */
240 	1,			/* minimum transfer */
241 	0xffffffffU,		/* maximum transfer */
242 	0xffffffffffffffffULL,	/* maximum segment length */
243 	1,			/* maximum number of segments */
244 	1,			/* granularity */
245 	0,			/* flags (reserved) */
246 };
247 
248 /* DMA access attributes for registers and descriptors */
249 static ddi_device_acc_attr_t accattr = {
250 	DDI_DEVICE_ATTR_V0,
251 	DDI_STRUCTURE_LE_ACC,	/* This is a little-endian device */
252 	DDI_STRICTORDER_ACC
253 };
254 
255 /* DMA access attributes for data: NOT to be byte swapped. */
256 static ddi_device_acc_attr_t data_accattr = {
257 	DDI_DEVICE_ATTR_V0,
258 	DDI_NEVERSWAP_ACC,
259 	DDI_STRICTORDER_ACC
260 };
261 
262 unsigned char xnf_broadcastaddr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
263 int xnf_diagnose = 0; /* Patchable global for diagnostic purposes */
264 
265 DDI_DEFINE_STREAM_OPS(xnf_dev_ops, nulldev, nulldev, xnf_attach, xnf_detach,
266     nodev, NULL, D_MP, NULL);
267 
268 static struct modldrv xnf_modldrv = {
269 	&mod_driverops,		/* Type of module.  This one is a driver */
270 	IDENT " %I%",		/* short description */
271 	&xnf_dev_ops		/* driver specific ops */
272 };
273 
274 static struct modlinkage modlinkage = {
275 	MODREV_1, &xnf_modldrv, NULL
276 };
277 
278 int
279 _init(void)
280 {
281 	int r;
282 
283 	mac_init_ops(&xnf_dev_ops, "xnf");
284 	r = mod_install(&modlinkage);
285 	if (r != DDI_SUCCESS)
286 		mac_fini_ops(&xnf_dev_ops);
287 
288 	return (r);
289 }
290 
291 int
292 _fini(void)
293 {
294 	return (EBUSY); /* XXPV dme: should be removable */
295 }
296 
297 int
298 _info(struct modinfo *modinfop)
299 {
300 	return (mod_info(&modlinkage, modinfop));
301 }
302 
303 static int
304 xnf_setup_rings(xnf_t *xnfp)
305 {
306 	int			ix, err;
307 	RING_IDX		i;
308 	struct xnf_buffer_desc	*bdesc, *rbp;
309 	struct xenbus_device	*xsd;
310 	domid_t			oeid;
311 
312 	oeid = xvdi_get_oeid(xnfp->xnf_devinfo);
313 	xsd = xvdi_get_xsd(xnfp->xnf_devinfo);
314 
315 	if (xnfp->xnf_tx_ring_ref != GRANT_INVALID_REF)
316 		gnttab_end_foreign_access(xnfp->xnf_tx_ring_ref, 0, 0);
317 
318 	err = gnttab_grant_foreign_access(oeid,
319 	    xnf_btop(pa_to_ma(xnfp->xnf_tx_ring_phys_addr)), 0);
320 	if (err <= 0) {
321 		err = -err;
322 		xenbus_dev_error(xsd, err, "granting access to tx ring page");
323 		goto out;
324 	}
325 	xnfp->xnf_tx_ring_ref = (grant_ref_t)err;
326 
327 	if (xnfp->xnf_rx_ring_ref != GRANT_INVALID_REF)
328 		gnttab_end_foreign_access(xnfp->xnf_rx_ring_ref, 0, 0);
329 
330 	err = gnttab_grant_foreign_access(oeid,
331 	    xnf_btop(pa_to_ma(xnfp->xnf_rx_ring_phys_addr)), 0);
332 	if (err <= 0) {
333 		err = -err;
334 		xenbus_dev_error(xsd, err, "granting access to rx ring page");
335 		goto out;
336 	}
337 	xnfp->xnf_rx_ring_ref = (grant_ref_t)err;
338 
339 
340 	mutex_enter(&xnfp->xnf_intrlock);
341 
342 	/*
343 	 * Cleanup the TX ring.  We just clean up any valid tx_pktinfo structs
344 	 * and reset the ring.  Note that this can lose packets after a resume,
345 	 * but we expect to stagger on.
346 	 */
347 	mutex_enter(&xnfp->xnf_txlock);
348 
349 	for (i = 0; i < xnfp->xnf_n_tx; i++) {
350 		struct tx_pktinfo *txp = &xnfp->xnf_tx_pkt_info[i];
351 
352 		txp->id = i + 1;
353 
354 		if (txp->grant_ref == GRANT_INVALID_REF) {
355 			ASSERT(txp->mp == NULL);
356 			ASSERT(txp->bdesc == NULL);
357 			continue;
358 		}
359 
360 		if (gnttab_query_foreign_access(txp->grant_ref) != 0)
361 			panic("tx grant still in use by backend domain");
362 
363 		freemsg(txp->mp);
364 		txp->mp = NULL;
365 
366 		(void) ddi_dma_unbind_handle(txp->dma_handle);
367 
368 		if (txp->bdesc != NULL) {
369 			xnf_free_tx_buffer(txp->bdesc);
370 			txp->bdesc = NULL;
371 		}
372 
373 		(void) gnttab_end_foreign_access_ref(txp->grant_ref,
374 		    xnfp->xnf_tx_pages_readonly);
375 		gnttab_release_grant_reference(&xnfp->xnf_gref_tx_head,
376 		    txp->grant_ref);
377 		txp->grant_ref = GRANT_INVALID_REF;
378 	}
379 
380 	xnfp->xnf_tx_pkt_id_list = 0;
381 	xnfp->xnf_tx_ring.rsp_cons = 0;
382 	xnfp->xnf_tx_ring.req_prod_pvt = 0;
383 	xnfp->xnf_tx_ring.sring->req_prod = 0;
384 	xnfp->xnf_tx_ring.sring->rsp_prod = 0;
385 	xnfp->xnf_tx_ring.sring->rsp_event = 1;
386 
387 	mutex_exit(&xnfp->xnf_txlock);
388 
389 	/*
390 	 * Rebuild the RX ring.  We have to rebuild the RX ring because some of
391 	 * our pages are currently flipped out/granted so we can't just free
392 	 * the RX buffers.  Reclaim any unprocessed recv buffers, they won't be
393 	 * useable anyway since the mfn's they refer to are no longer valid.
394 	 * Grant the backend domain access to each hung rx buffer.
395 	 */
396 	i = xnfp->xnf_rx_ring.rsp_cons;
397 	while (i++ != xnfp->xnf_rx_ring.sring->req_prod) {
398 		volatile netif_rx_request_t	*rxrp;
399 
400 		rxrp = RING_GET_REQUEST(&xnfp->xnf_rx_ring, i);
401 		ix = rxrp - RING_GET_REQUEST(&xnfp->xnf_rx_ring, 0);
402 		rbp = xnfp->xnf_rxpkt_bufptr[ix];
403 		if (rbp != NULL) {
404 			grant_ref_t	ref = rbp->grant_ref;
405 
406 			ASSERT(ref != GRANT_INVALID_REF);
407 			if (xnfp->xnf_rx_hvcopy) {
408 				pfn_t pfn = xnf_btop(rbp->buf_phys);
409 				mfn_t mfn = pfn_to_mfn(pfn);
410 
411 				gnttab_grant_foreign_access_ref(ref, oeid,
412 				    mfn, 0);
413 			} else {
414 				gnttab_grant_foreign_transfer_ref(ref,
415 				    oeid, 0);
416 			}
417 			rxrp->id = ix;
418 			rxrp->gref = ref;
419 		}
420 	}
421 
422 	/*
423 	 * Reset the ring pointers to initial state.
424 	 * Hang buffers for any empty ring slots.
425 	 */
426 	xnfp->xnf_rx_ring.rsp_cons = 0;
427 	xnfp->xnf_rx_ring.req_prod_pvt = 0;
428 	xnfp->xnf_rx_ring.sring->req_prod = 0;
429 	xnfp->xnf_rx_ring.sring->rsp_prod = 0;
430 	xnfp->xnf_rx_ring.sring->rsp_event = 1;
431 	for (i = 0; i < NET_RX_RING_SIZE; i++) {
432 		xnfp->xnf_rx_ring.req_prod_pvt = i;
433 		if (xnfp->xnf_rxpkt_bufptr[i] != NULL)
434 			continue;
435 		if ((bdesc = xnf_get_buffer(xnfp)) == NULL)
436 			break;
437 		rx_buffer_hang(xnfp, bdesc);
438 	}
439 	xnfp->xnf_rx_ring.req_prod_pvt = i;
440 	/* LINTED: constant in conditional context */
441 	RING_PUSH_REQUESTS(&xnfp->xnf_rx_ring);
442 
443 	mutex_exit(&xnfp->xnf_intrlock);
444 
445 	return (0);
446 
447 out:
448 	if (xnfp->xnf_tx_ring_ref != GRANT_INVALID_REF)
449 		gnttab_end_foreign_access(xnfp->xnf_tx_ring_ref, 0, 0);
450 	xnfp->xnf_tx_ring_ref = GRANT_INVALID_REF;
451 
452 	if (xnfp->xnf_rx_ring_ref != GRANT_INVALID_REF)
453 		gnttab_end_foreign_access(xnfp->xnf_rx_ring_ref, 0, 0);
454 	xnfp->xnf_rx_ring_ref = GRANT_INVALID_REF;
455 
456 	return (err);
457 }
458 
459 
460 /* Called when the upper layers free a message we passed upstream */
461 static void
462 xnf_copy_rcv_complete(struct xnf_buffer_desc *bdesc)
463 {
464 	(void) ddi_dma_unbind_handle(bdesc->dma_handle);
465 	ddi_dma_mem_free(&bdesc->acc_handle);
466 	ddi_dma_free_handle(&bdesc->dma_handle);
467 	kmem_free(bdesc, sizeof (*bdesc));
468 }
469 
470 
471 /*
472  * Connect driver to back end, called to set up communication with
473  * back end driver both initially and on resume after restore/migrate.
474  */
475 void
476 xnf_be_connect(xnf_t *xnfp)
477 {
478 	char		mac[ETHERADDRL * 3];
479 	const char	*message;
480 	xenbus_transaction_t xbt;
481 	struct		xenbus_device *xsd;
482 	char		*xsname;
483 	int		err, be_no_cksum_offload;
484 
485 	ASSERT(!xnfp->xnf_connected);
486 
487 	xsd = xvdi_get_xsd(xnfp->xnf_devinfo);
488 	xsname = xvdi_get_xsname(xnfp->xnf_devinfo);
489 
490 	err = xenbus_scanf(XBT_NULL, xvdi_get_oename(xnfp->xnf_devinfo), "mac",
491 	    "%s", (char *)&mac[0]);
492 	if (err != 0) {
493 		/*
494 		 * bad: we're supposed to be set up with a proper mac
495 		 * addr. at this point
496 		 */
497 		cmn_err(CE_WARN, "%s%d: no mac address",
498 		    ddi_driver_name(xnfp->xnf_devinfo),
499 		    ddi_get_instance(xnfp->xnf_devinfo));
500 			return;
501 	}
502 
503 	if (ether_aton(mac, xnfp->xnf_mac_addr) != ETHERADDRL) {
504 		err = ENOENT;
505 		xenbus_dev_error(xsd, ENOENT, "parsing %s/mac", xsname);
506 		return;
507 	}
508 
509 	err = xnf_setup_rings(xnfp);
510 	if (err != 0) {
511 		cmn_err(CE_WARN, "failed to set up tx/rx rings");
512 		xenbus_dev_error(xsd, err, "setting up ring");
513 		return;
514 	}
515 
516 	err = xenbus_scanf(XBT_NULL, xvdi_get_oename(xnfp->xnf_devinfo),
517 	    "feature-no-csum-offload", "%d", &be_no_cksum_offload);
518 	/*
519 	 * If we fail to read the store we assume that the key is
520 	 * absent, implying an older domain at the far end.  Older
521 	 * domains always support checksum offload.
522 	 */
523 	if (err != 0)
524 		be_no_cksum_offload = 0;
525 	/*
526 	 * If the far end cannot do checksum offload or we do not wish
527 	 * to do it, disable it.
528 	 */
529 	if ((be_no_cksum_offload == 1) || !xnfp->xnf_cksum_offload)
530 		xnfp->xnf_cksum_offload = B_FALSE;
531 
532 again:
533 	err = xenbus_transaction_start(&xbt);
534 	if (err != 0) {
535 		xenbus_dev_error(xsd, EIO, "starting transaction");
536 		return;
537 	}
538 
539 	err = xenbus_printf(xbt, xsname, "tx-ring-ref", "%u",
540 	    xnfp->xnf_tx_ring_ref);
541 	if (err != 0) {
542 		message = "writing tx ring-ref";
543 		goto abort_transaction;
544 	}
545 
546 	err = xenbus_printf(xbt, xsname, "rx-ring-ref", "%u",
547 	    xnfp->xnf_rx_ring_ref);
548 	if (err != 0) {
549 		message = "writing rx ring-ref";
550 		goto abort_transaction;
551 	}
552 
553 	err = xenbus_printf(xbt, xsname, "event-channel", "%u",
554 	    xnfp->xnf_evtchn);
555 	if (err != 0) {
556 		message = "writing event-channel";
557 		goto abort_transaction;
558 	}
559 
560 	err = xenbus_printf(xbt, xsname, "feature-rx-notify", "%d", 1);
561 	if (err != 0) {
562 		message = "writing feature-rx-notify";
563 		goto abort_transaction;
564 	}
565 
566 	if (!xnfp->xnf_tx_pages_readonly) {
567 		err = xenbus_printf(xbt, xsname, "feature-tx-writable",
568 		    "%d", 1);
569 		if (err != 0) {
570 			message = "writing feature-tx-writable";
571 			goto abort_transaction;
572 		}
573 	}
574 
575 	err = xenbus_printf(xbt, xsname, "feature-no-csum-offload", "%d",
576 	    xnfp->xnf_cksum_offload ? 0 : 1);
577 	if (err != 0) {
578 		message = "writing feature-no-csum-offload";
579 		goto abort_transaction;
580 	}
581 	err = xenbus_printf(xbt, xsname, "request-rx-copy", "%d",
582 	    xnfp->xnf_rx_hvcopy ? 1 : 0);
583 	if (err != 0) {
584 		message = "writing request-rx-copy";
585 		goto abort_transaction;
586 	}
587 
588 	err = xenbus_printf(xbt, xsname, "state", "%d", XenbusStateConnected);
589 	if (err != 0) {
590 		message = "writing frontend XenbusStateConnected";
591 		goto abort_transaction;
592 	}
593 
594 	err = xenbus_transaction_end(xbt, 0);
595 	if (err != 0) {
596 		if (err == EAGAIN)
597 			goto again;
598 		xenbus_dev_error(xsd, err, "completing transaction");
599 	}
600 
601 	return;
602 
603 abort_transaction:
604 	(void) xenbus_transaction_end(xbt, 1);
605 	xenbus_dev_error(xsd, err, "%s", message);
606 }
607 
608 /*
609  *  attach(9E) -- Attach a device to the system
610  *
611  *  Called once for each board successfully probed.
612  */
613 static int
614 xnf_attach(dev_info_t *devinfo, ddi_attach_cmd_t cmd)
615 {
616 	mac_register_t *macp;
617 	xnf_t *xnfp;
618 	int err;
619 
620 #ifdef XNF_DEBUG
621 	if (xnfdebug & XNF_DEBUG_DDI)
622 		printf("xnf%d: attach(0x%p)\n", ddi_get_instance(devinfo),
623 		    (void *)devinfo);
624 #endif
625 
626 	switch (cmd) {
627 	case DDI_RESUME:
628 		xnfp = ddi_get_driver_private(devinfo);
629 
630 		(void) xvdi_resume(devinfo);
631 		(void) xvdi_alloc_evtchn(devinfo);
632 		xnfp->xnf_evtchn = xvdi_get_evtchn(devinfo);
633 #ifdef XPV_HVM_DRIVER
634 		ec_bind_evtchn_to_handler(xnfp->xnf_evtchn, IPL_VIF, xnf_intr,
635 		    xnfp);
636 #else
637 		(void) ddi_add_intr(devinfo, 0, NULL, NULL, xnf_intr,
638 		    (caddr_t)xnfp);
639 #endif
640 		xnf_be_connect(xnfp);
641 		/*
642 		 * Our MAC address may have changed if we're resuming:
643 		 * - on a different host
644 		 * - on the same one and got a different MAC address
645 		 *   because we didn't specify one of our own.
646 		 * so it's useful to claim that it changed in order that
647 		 * IP send out a gratuitous ARP.
648 		 */
649 		mac_unicst_update(xnfp->xnf_mh, xnfp->xnf_mac_addr);
650 		return (DDI_SUCCESS);
651 
652 	case DDI_ATTACH:
653 		break;
654 
655 	default:
656 		return (DDI_FAILURE);
657 	}
658 
659 	/*
660 	 *  Allocate gld_mac_info_t and xnf_instance structures
661 	 */
662 	macp = mac_alloc(MAC_VERSION);
663 	if (macp == NULL)
664 		return (DDI_FAILURE);
665 	xnfp = kmem_zalloc(sizeof (*xnfp), KM_SLEEP);
666 
667 	macp->m_dip = devinfo;
668 	macp->m_driver = xnfp;
669 	xnfp->xnf_devinfo = devinfo;
670 
671 	macp->m_type_ident = MAC_PLUGIN_IDENT_ETHER;
672 	macp->m_src_addr = xnfp->xnf_mac_addr;
673 	macp->m_callbacks = &xnf_callbacks;
674 	macp->m_min_sdu = 0;
675 	macp->m_max_sdu = XNF_MAXPKT;
676 
677 	xnfp->xnf_running = B_FALSE;
678 	xnfp->xnf_connected = B_FALSE;
679 	xnfp->xnf_cksum_offload = xnf_cksum_offload;
680 	xnfp->xnf_tx_pages_readonly = xnf_tx_pages_readonly;
681 
682 	xnfp->xnf_rx_hvcopy = xnf_hvcopy_peer_status(devinfo) && xnf_rx_hvcopy;
683 #ifdef XPV_HVM_DRIVER
684 	if (!xnfp->xnf_rx_hvcopy) {
685 		cmn_err(CE_WARN, "The xnf driver requires a dom0 that "
686 		    "supports 'feature-rx-copy'");
687 		goto failure;
688 	}
689 #endif
690 
691 	/*
692 	 * Get the iblock cookie with which to initialize the mutexes.
693 	 */
694 	if (ddi_get_iblock_cookie(devinfo, 0, &xnfp->xnf_icookie)
695 	    != DDI_SUCCESS)
696 		goto failure;
697 	/*
698 	 * Driver locking strategy: the txlock protects all paths
699 	 * through the driver, except the interrupt thread.
700 	 * If the interrupt thread needs to do something which could
701 	 * affect the operation of any other part of the driver,
702 	 * it needs to acquire the txlock mutex.
703 	 */
704 	mutex_init(&xnfp->xnf_tx_buf_mutex,
705 	    NULL, MUTEX_DRIVER, xnfp->xnf_icookie);
706 	mutex_init(&xnfp->xnf_rx_buf_mutex,
707 	    NULL, MUTEX_DRIVER, xnfp->xnf_icookie);
708 	mutex_init(&xnfp->xnf_txlock,
709 	    NULL, MUTEX_DRIVER, xnfp->xnf_icookie);
710 	mutex_init(&xnfp->xnf_intrlock,
711 	    NULL, MUTEX_DRIVER, xnfp->xnf_icookie);
712 	cv_init(&xnfp->xnf_cv, NULL, CV_DEFAULT, NULL);
713 
714 	if (gnttab_alloc_grant_references(NET_TX_RING_SIZE,
715 	    &xnfp->xnf_gref_tx_head) < 0) {
716 		cmn_err(CE_WARN, "xnf%d: can't alloc tx grant refs",
717 		    ddi_get_instance(xnfp->xnf_devinfo));
718 		goto failure_1;
719 	}
720 	if (gnttab_alloc_grant_references(NET_RX_RING_SIZE,
721 	    &xnfp->xnf_gref_rx_head) < 0) {
722 		cmn_err(CE_WARN, "xnf%d: can't alloc rx grant refs",
723 		    ddi_get_instance(xnfp->xnf_devinfo));
724 		goto failure_1;
725 	}
726 	if (xnf_alloc_dma_resources(xnfp) == DDI_FAILURE) {
727 		cmn_err(CE_WARN, "xnf%d: failed to allocate and initialize "
728 		    "driver data structures",
729 		    ddi_get_instance(xnfp->xnf_devinfo));
730 		goto failure_1;
731 	}
732 
733 	xnfp->xnf_rx_ring.sring->rsp_event =
734 	    xnfp->xnf_tx_ring.sring->rsp_event = 1;
735 
736 	xnfp->xnf_tx_ring_ref = GRANT_INVALID_REF;
737 	xnfp->xnf_rx_ring_ref = GRANT_INVALID_REF;
738 
739 	/* set driver private pointer now */
740 	ddi_set_driver_private(devinfo, xnfp);
741 
742 	if (xvdi_add_event_handler(devinfo, XS_OE_STATE, oe_state_change)
743 	    != DDI_SUCCESS)
744 		goto failure_1;
745 
746 	if (!xnf_kstat_init(xnfp))
747 		goto failure_2;
748 
749 	/*
750 	 * Allocate an event channel, add the interrupt handler and
751 	 * bind it to the event channel.
752 	 */
753 	(void) xvdi_alloc_evtchn(devinfo);
754 	xnfp->xnf_evtchn = xvdi_get_evtchn(devinfo);
755 #ifdef XPV_HVM_DRIVER
756 	ec_bind_evtchn_to_handler(xnfp->xnf_evtchn, IPL_VIF, xnf_intr, xnfp);
757 #else
758 	(void) ddi_add_intr(devinfo, 0, NULL, NULL, xnf_intr, (caddr_t)xnfp);
759 #endif
760 
761 	/*
762 	 * connect to the backend
763 	 */
764 	xnf_be_connect(xnfp);
765 
766 	err = mac_register(macp, &xnfp->xnf_mh);
767 	mac_free(macp);
768 	macp = NULL;
769 	if (err != 0)
770 		goto failure_3;
771 
772 	return (DDI_SUCCESS);
773 
774 failure_3:
775 	kstat_delete(xnfp->xnf_kstat_aux);
776 
777 failure_2:
778 	xvdi_remove_event_handler(devinfo, XS_OE_STATE);
779 #ifdef XPV_HVM_DRIVER
780 	ec_unbind_evtchn(xnfp->xnf_evtchn);
781 #else
782 	ddi_remove_intr(devinfo, 0, xnfp->xnf_icookie);
783 #endif
784 	xnfp->xnf_evtchn = INVALID_EVTCHN;
785 
786 failure_1:
787 	xnf_release_dma_resources(xnfp);
788 	cv_destroy(&xnfp->xnf_cv);
789 	mutex_destroy(&xnfp->xnf_rx_buf_mutex);
790 	mutex_destroy(&xnfp->xnf_txlock);
791 	mutex_destroy(&xnfp->xnf_intrlock);
792 
793 failure:
794 	kmem_free(xnfp, sizeof (*xnfp));
795 	if (macp != NULL)
796 		mac_free(macp);
797 
798 	return (DDI_FAILURE);
799 }
800 
801 /*  detach(9E) -- Detach a device from the system */
802 static int
803 xnf_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
804 {
805 	xnf_t *xnfp;		/* Our private device info */
806 	int i;
807 
808 #ifdef XNF_DEBUG
809 	if (xnfdebug & XNF_DEBUG_DDI)
810 		printf("xnf_detach(0x%p)\n", (void *)devinfo);
811 #endif
812 
813 	xnfp = ddi_get_driver_private(devinfo);
814 
815 	switch (cmd) {
816 	case DDI_SUSPEND:
817 #ifdef XPV_HVM_DRIVER
818 		ec_unbind_evtchn(xnfp->xnf_evtchn);
819 #else
820 		ddi_remove_intr(devinfo, 0, xnfp->xnf_icookie);
821 #endif
822 
823 		xvdi_suspend(devinfo);
824 
825 		mutex_enter(&xnfp->xnf_intrlock);
826 		mutex_enter(&xnfp->xnf_txlock);
827 
828 		xnfp->xnf_evtchn = INVALID_EVTCHN;
829 		xnfp->xnf_connected = B_FALSE;
830 		mutex_exit(&xnfp->xnf_txlock);
831 		mutex_exit(&xnfp->xnf_intrlock);
832 		return (DDI_SUCCESS);
833 
834 	case DDI_DETACH:
835 		break;
836 
837 	default:
838 		return (DDI_FAILURE);
839 	}
840 
841 	if (xnfp->xnf_connected)
842 		return (DDI_FAILURE);
843 
844 	/* Wait for receive buffers to be returned; give up after 5 seconds */
845 	i = 50;
846 
847 	mutex_enter(&xnfp->xnf_rx_buf_mutex);
848 	while (xnfp->xnf_rx_bufs_outstanding > 0) {
849 		mutex_exit(&xnfp->xnf_rx_buf_mutex);
850 		delay(drv_usectohz(100000));
851 		if (--i == 0) {
852 			cmn_err(CE_WARN,
853 			    "xnf%d: never reclaimed all the "
854 			    "receive buffers.  Still have %d "
855 			    "buffers outstanding.",
856 			    ddi_get_instance(xnfp->xnf_devinfo),
857 			    xnfp->xnf_rx_bufs_outstanding);
858 			return (DDI_FAILURE);
859 		}
860 		mutex_enter(&xnfp->xnf_rx_buf_mutex);
861 	}
862 	mutex_exit(&xnfp->xnf_rx_buf_mutex);
863 
864 	kstat_delete(xnfp->xnf_kstat_aux);
865 
866 	if (mac_unregister(xnfp->xnf_mh) != 0)
867 		return (DDI_FAILURE);
868 
869 	/* Stop the receiver */
870 	xnf_stop(xnfp);
871 
872 	xvdi_remove_event_handler(devinfo, XS_OE_STATE);
873 
874 	/* Remove the interrupt */
875 #ifdef XPV_HVM_DRIVER
876 	ec_unbind_evtchn(xnfp->xnf_evtchn);
877 #else
878 	ddi_remove_intr(devinfo, 0, xnfp->xnf_icookie);
879 #endif
880 
881 	/* Release any pending xmit mblks */
882 	xnf_release_mblks(xnfp);
883 
884 	/* Release all DMA resources */
885 	xnf_release_dma_resources(xnfp);
886 
887 	cv_destroy(&xnfp->xnf_cv);
888 	mutex_destroy(&xnfp->xnf_rx_buf_mutex);
889 	mutex_destroy(&xnfp->xnf_txlock);
890 	mutex_destroy(&xnfp->xnf_intrlock);
891 
892 	kmem_free(xnfp, sizeof (*xnfp));
893 
894 	return (DDI_SUCCESS);
895 }
896 
897 /*
898  *  xnf_set_mac_addr() -- set the physical network address on the board.
899  */
900 /*ARGSUSED*/
901 static int
902 xnf_set_mac_addr(void *arg, const uint8_t *macaddr)
903 {
904 	xnf_t *xnfp = arg;
905 
906 #ifdef XNF_DEBUG
907 	if (xnfdebug & XNF_DEBUG_TRACE)
908 		printf("xnf%d: set_mac_addr(0x%p): "
909 		    "%02x:%02x:%02x:%02x:%02x:%02x\n",
910 		    ddi_get_instance(xnfp->xnf_devinfo),
911 		    (void *)xnfp, macaddr[0], macaddr[1], macaddr[2],
912 		    macaddr[3], macaddr[4], macaddr[5]);
913 #endif
914 	/*
915 	 * We can't set our macaddr.
916 	 *
917 	 * XXPV dme: Why not?
918 	 */
919 	return (ENOTSUP);
920 }
921 
922 /*
923  *  xnf_set_multicast() -- set (enable) or disable a multicast address.
924  *
925  *  Program the hardware to enable/disable the multicast address
926  *  in "mcast".  Enable if "add" is true, disable if false.
927  */
928 /*ARGSUSED*/
929 static int
930 xnf_set_multicast(void *arg, boolean_t add, const uint8_t *mca)
931 {
932 	xnf_t *xnfp = arg;
933 
934 #ifdef XNF_DEBUG
935 	if (xnfdebug & XNF_DEBUG_TRACE)
936 		printf("xnf%d set_multicast(0x%p): "
937 		    "%02x:%02x:%02x:%02x:%02x:%02x\n",
938 		    ddi_get_instance(xnfp->xnf_devinfo),
939 		    (void *)xnfp, mca[0], mca[1], mca[2],
940 		    mca[3], mca[4], mca[5]);
941 #endif
942 
943 	/*
944 	 * XXPV dme: Ideally we'd relay the address to the backend for
945 	 * enabling.  The protocol doesn't support that (interesting
946 	 * extension), so we simply succeed and hope that the relevant
947 	 * packets are going to arrive.
948 	 *
949 	 * If protocol support is added for enable/disable then we'll
950 	 * need to keep a list of those in use and re-add on resume.
951 	 */
952 	return (0);
953 }
954 
955 /*
956  * xnf_set_promiscuous() -- set or reset promiscuous mode on the board
957  *
958  *  Program the hardware to enable/disable promiscuous mode.
959  */
960 /*ARGSUSED*/
961 static int
962 xnf_set_promiscuous(void *arg, boolean_t on)
963 {
964 	xnf_t *xnfp = arg;
965 
966 #ifdef XNF_DEBUG
967 	if (xnfdebug & XNF_DEBUG_TRACE)
968 		printf("xnf%d set_promiscuous(0x%p, %x)\n",
969 		    ddi_get_instance(xnfp->xnf_devinfo),
970 		    (void *)xnfp, on);
971 #endif
972 	/*
973 	 * We can't really do this, but we pretend that we can in
974 	 * order that snoop will work.
975 	 */
976 	return (0);
977 }
978 
979 /*
980  * Clean buffers that we have responses for from the transmit ring.
981  */
982 static int
983 xnf_clean_tx_ring(xnf_t *xnfp)
984 {
985 	RING_IDX		next_resp, i;
986 	struct tx_pktinfo	*reap;
987 	int			id;
988 	grant_ref_t		ref;
989 
990 	ASSERT(MUTEX_HELD(&xnfp->xnf_txlock));
991 
992 	while (RING_HAS_UNCONSUMED_RESPONSES(&xnfp->xnf_tx_ring)) {
993 		/*
994 		 * index of next transmission ack
995 		 */
996 		next_resp = xnfp->xnf_tx_ring.sring->rsp_prod;
997 		membar_consumer();
998 		/*
999 		 * Clean tx packets from ring that we have responses for
1000 		 */
1001 		for (i = xnfp->xnf_tx_ring.rsp_cons; i != next_resp; i++) {
1002 			id = RING_GET_RESPONSE(&xnfp->xnf_tx_ring, i)->id;
1003 			reap = &xnfp->xnf_tx_pkt_info[id];
1004 			ref = reap->grant_ref;
1005 			/*
1006 			 * Return id to free list
1007 			 */
1008 			reap->id = xnfp->xnf_tx_pkt_id_list;
1009 			xnfp->xnf_tx_pkt_id_list = id;
1010 			if (gnttab_query_foreign_access(ref) != 0)
1011 				panic("tx grant still in use "
1012 				    "by backend domain");
1013 			(void) ddi_dma_unbind_handle(reap->dma_handle);
1014 			(void) gnttab_end_foreign_access_ref(ref,
1015 			    xnfp->xnf_tx_pages_readonly);
1016 			gnttab_release_grant_reference(&xnfp->xnf_gref_tx_head,
1017 			    ref);
1018 			freemsg(reap->mp);
1019 			reap->mp = NULL;
1020 			reap->grant_ref = GRANT_INVALID_REF;
1021 			if (reap->bdesc != NULL)
1022 				xnf_free_tx_buffer(reap->bdesc);
1023 			reap->bdesc = NULL;
1024 		}
1025 		xnfp->xnf_tx_ring.rsp_cons = next_resp;
1026 		membar_enter();
1027 	}
1028 
1029 	return (RING_FREE_REQUESTS(&xnfp->xnf_tx_ring));
1030 }
1031 
1032 /*
1033  * If we need to pull up data from either a packet that crosses a page
1034  * boundary or consisting of multiple mblks, do it here.  We allocate
1035  * a page aligned buffer and copy the data into it.  The header for the
1036  * allocated buffer is returned. (which is also allocated here)
1037  */
1038 static struct xnf_buffer_desc *
1039 xnf_pullupmsg(xnf_t *xnfp, mblk_t *mp)
1040 {
1041 	struct xnf_buffer_desc	*bdesc;
1042 	mblk_t			*mptr;
1043 	caddr_t			bp;
1044 	int			len;
1045 
1046 	/*
1047 	 * get a xmit buffer from the xmit buffer pool
1048 	 */
1049 	mutex_enter(&xnfp->xnf_rx_buf_mutex);
1050 	bdesc = xnf_get_tx_buffer(xnfp);
1051 	mutex_exit(&xnfp->xnf_rx_buf_mutex);
1052 	if (bdesc == NULL)
1053 		return (bdesc);
1054 	/*
1055 	 * Copy the data into the buffer
1056 	 */
1057 	xnfp->xnf_stat_tx_pullup++;
1058 	bp = bdesc->buf;
1059 	for (mptr = mp; mptr != NULL; mptr = mptr->b_cont) {
1060 		len = mptr->b_wptr - mptr->b_rptr;
1061 		bcopy(mptr->b_rptr, bp, len);
1062 		bp += len;
1063 	}
1064 	return (bdesc);
1065 }
1066 
1067 /*
1068  *  xnf_send_one() -- send a packet
1069  *
1070  *  Called when a packet is ready to be transmitted. A pointer to an
1071  *  M_DATA message that contains the packet is passed to this routine.
1072  *  At least the complete LLC header is contained in the message's
1073  *  first message block, and the remainder of the packet is contained
1074  *  within additional M_DATA message blocks linked to the first
1075  *  message block.
1076  *
1077  */
1078 static boolean_t
1079 xnf_send_one(xnf_t *xnfp, mblk_t *mp)
1080 {
1081 	struct xnf_buffer_desc	*xmitbuf;
1082 	struct tx_pktinfo	*txp_info;
1083 	mblk_t			*mptr;
1084 	ddi_dma_cookie_t	dma_cookie;
1085 	RING_IDX		slot;
1086 	int			length = 0, i, pktlen = 0, rc, tx_id;
1087 	int			tx_ring_freespace, page_oops;
1088 	uint_t			ncookies;
1089 	volatile netif_tx_request_t	*txrp;
1090 	caddr_t			bufaddr;
1091 	grant_ref_t		ref;
1092 	unsigned long		mfn;
1093 	uint32_t		pflags;
1094 	domid_t			oeid;
1095 
1096 #ifdef XNF_DEBUG
1097 	if (xnfdebug & XNF_DEBUG_SEND)
1098 		printf("xnf%d send(0x%p, 0x%p)\n",
1099 		    ddi_get_instance(xnfp->xnf_devinfo),
1100 		    (void *)xnfp, (void *)mp);
1101 #endif
1102 
1103 	ASSERT(mp != NULL);
1104 	ASSERT(mp->b_next == NULL);
1105 	ASSERT(MUTEX_HELD(&xnfp->xnf_txlock));
1106 
1107 	tx_ring_freespace = xnf_clean_tx_ring(xnfp);
1108 	ASSERT(tx_ring_freespace >= 0);
1109 
1110 	oeid = xvdi_get_oeid(xnfp->xnf_devinfo);
1111 	xnfp->xnf_stat_tx_attempt++;
1112 	/*
1113 	 * If there are no xmit ring slots available, return.
1114 	 */
1115 	if (tx_ring_freespace == 0) {
1116 		xnfp->xnf_stat_tx_defer++;
1117 		return (B_FALSE);	/* Send should be retried */
1118 	}
1119 
1120 	slot = xnfp->xnf_tx_ring.req_prod_pvt;
1121 	/* Count the number of mblks in message and compute packet size */
1122 	for (i = 0, mptr = mp; mptr != NULL; mptr = mptr->b_cont, i++)
1123 		pktlen += (mptr->b_wptr - mptr->b_rptr);
1124 
1125 	/* Make sure packet isn't too large */
1126 	if (pktlen > XNF_FRAMESIZE) {
1127 		cmn_err(CE_WARN, "xnf%d: large packet %d bytes",
1128 		    ddi_get_instance(xnfp->xnf_devinfo), pktlen);
1129 		freemsg(mp);
1130 		return (B_FALSE);
1131 	}
1132 
1133 	/*
1134 	 * Test if we cross a page boundary with our buffer
1135 	 */
1136 	page_oops = (i == 1) &&
1137 	    (xnf_btop((size_t)mp->b_rptr) !=
1138 	    xnf_btop((size_t)(mp->b_rptr + pktlen)));
1139 	/*
1140 	 * XXPV - unfortunately, the Xen virtual net device currently
1141 	 * doesn't support multiple packet frags, so this will always
1142 	 * end up doing the pullup if we got more than one packet.
1143 	 */
1144 	if (i > xnf_max_tx_frags || page_oops) {
1145 		if (page_oops)
1146 			xnfp->xnf_stat_tx_pagebndry++;
1147 		if ((xmitbuf = xnf_pullupmsg(xnfp, mp)) == NULL) {
1148 			/* could not allocate resources? */
1149 #ifdef XNF_DEBUG
1150 			cmn_err(CE_WARN, "xnf%d: pullupmsg failed",
1151 			    ddi_get_instance(xnfp->xnf_devinfo));
1152 #endif
1153 			xnfp->xnf_stat_tx_defer++;
1154 			return (B_FALSE);	/* Retry send */
1155 		}
1156 		bufaddr = xmitbuf->buf;
1157 	} else {
1158 		xmitbuf = NULL;
1159 		bufaddr = (caddr_t)mp->b_rptr;
1160 	}
1161 
1162 	/* set up data descriptor */
1163 	length = pktlen;
1164 
1165 	/*
1166 	 * Get packet id from free list
1167 	 */
1168 	tx_id = xnfp->xnf_tx_pkt_id_list;
1169 	ASSERT(tx_id < NET_TX_RING_SIZE);
1170 	txp_info = &xnfp->xnf_tx_pkt_info[tx_id];
1171 	xnfp->xnf_tx_pkt_id_list = txp_info->id;
1172 	txp_info->id = tx_id;
1173 
1174 	/* Prepare for DMA mapping of tx buffer(s) */
1175 	rc = ddi_dma_addr_bind_handle(txp_info->dma_handle,
1176 	    NULL, bufaddr, length, DDI_DMA_WRITE | DDI_DMA_STREAMING,
1177 	    DDI_DMA_DONTWAIT, 0, &dma_cookie, &ncookies);
1178 	if (rc != DDI_DMA_MAPPED) {
1179 		ASSERT(rc != DDI_DMA_INUSE);
1180 		ASSERT(rc != DDI_DMA_PARTIAL_MAP);
1181 		/*
1182 		 *  Return id to free list
1183 		 */
1184 		txp_info->id = xnfp->xnf_tx_pkt_id_list;
1185 		xnfp->xnf_tx_pkt_id_list = tx_id;
1186 		if (rc == DDI_DMA_NORESOURCES) {
1187 			xnfp->xnf_stat_tx_defer++;
1188 			return (B_FALSE); /* Retry later */
1189 		}
1190 #ifdef XNF_DEBUG
1191 		cmn_err(CE_WARN, "xnf%d: bind_handle failed (%x)",
1192 		    ddi_get_instance(xnfp->xnf_devinfo), rc);
1193 #endif
1194 		return (B_FALSE);
1195 	}
1196 
1197 	ASSERT(ncookies == 1);
1198 	ref = gnttab_claim_grant_reference(&xnfp->xnf_gref_tx_head);
1199 	ASSERT((signed short)ref >= 0);
1200 	mfn = xnf_btop(pa_to_ma((paddr_t)dma_cookie.dmac_laddress));
1201 	gnttab_grant_foreign_access_ref(ref, oeid, mfn,
1202 	    xnfp->xnf_tx_pages_readonly);
1203 	txp_info->grant_ref = ref;
1204 	txrp = RING_GET_REQUEST(&xnfp->xnf_tx_ring, slot);
1205 	txrp->gref = ref;
1206 	txrp->size = dma_cookie.dmac_size;
1207 	txrp->offset = (uintptr_t)bufaddr & PAGEOFFSET;
1208 	txrp->id = tx_id;
1209 	txrp->flags = 0;
1210 	hcksum_retrieve(mp, NULL, NULL, NULL, NULL, NULL, NULL, &pflags);
1211 	if (pflags != 0) {
1212 		ASSERT(xnfp->xnf_cksum_offload);
1213 		/*
1214 		 * If the local protocol stack requests checksum
1215 		 * offload we set the 'checksum blank' flag,
1216 		 * indicating to the peer that we need the checksum
1217 		 * calculated for us.
1218 		 *
1219 		 * We _don't_ set the validated flag, because we haven't
1220 		 * validated that the data and the checksum match.
1221 		 */
1222 		txrp->flags |= NETTXF_csum_blank;
1223 		xnfp->xnf_stat_tx_cksum_deferred++;
1224 	}
1225 	membar_producer();
1226 	xnfp->xnf_tx_ring.req_prod_pvt = slot + 1;
1227 
1228 	txp_info->mp = mp;
1229 	txp_info->bdesc = xmitbuf;
1230 
1231 	xnfp->xnf_stat_opackets++;
1232 	xnfp->xnf_stat_obytes += pktlen;
1233 
1234 	return (B_TRUE);	/* successful transmit attempt */
1235 }
1236 
1237 mblk_t *
1238 xnf_send(void *arg, mblk_t *mp)
1239 {
1240 	xnf_t *xnfp = arg;
1241 	mblk_t *next;
1242 	boolean_t sent_something = B_FALSE;
1243 
1244 	mutex_enter(&xnfp->xnf_txlock);
1245 
1246 	/*
1247 	 * Transmission attempts should be impossible without having
1248 	 * previously called xnf_start().
1249 	 */
1250 	ASSERT(xnfp->xnf_running);
1251 
1252 	/*
1253 	 * Wait for getting connected to the backend
1254 	 */
1255 	while (!xnfp->xnf_connected) {
1256 		cv_wait(&xnfp->xnf_cv, &xnfp->xnf_txlock);
1257 	}
1258 
1259 	while (mp != NULL) {
1260 		next = mp->b_next;
1261 		mp->b_next = NULL;
1262 
1263 		if (!xnf_send_one(xnfp, mp)) {
1264 			mp->b_next = next;
1265 			break;
1266 		}
1267 
1268 		mp = next;
1269 		sent_something = B_TRUE;
1270 	}
1271 
1272 	if (sent_something) {
1273 		boolean_t notify;
1274 
1275 		/* LINTED: constant in conditional context */
1276 		RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xnfp->xnf_tx_ring,
1277 		    notify);
1278 		if (notify)
1279 			ec_notify_via_evtchn(xnfp->xnf_evtchn);
1280 	}
1281 
1282 	mutex_exit(&xnfp->xnf_txlock);
1283 
1284 	return (mp);
1285 }
1286 
1287 /*
1288  *  xnf_intr() -- ring interrupt service routine
1289  */
1290 static uint_t
1291 xnf_intr(caddr_t arg)
1292 {
1293 	xnf_t *xnfp = (xnf_t *)arg;
1294 	int tx_ring_space;
1295 
1296 	mutex_enter(&xnfp->xnf_intrlock);
1297 
1298 	/*
1299 	 * If not connected to the peer or not started by the upper
1300 	 * layers we cannot usefully handle interrupts.
1301 	 */
1302 	if (!(xnfp->xnf_connected && xnfp->xnf_running)) {
1303 		mutex_exit(&xnfp->xnf_intrlock);
1304 		xnfp->xnf_stat_unclaimed_interrupts++;
1305 		return (DDI_INTR_UNCLAIMED);
1306 	}
1307 
1308 #ifdef XNF_DEBUG
1309 	if (xnfdebug & XNF_DEBUG_INT)
1310 		printf("xnf%d intr(0x%p)\n",
1311 		    ddi_get_instance(xnfp->xnf_devinfo), (void *)xnfp);
1312 #endif
1313 	if (RING_HAS_UNCONSUMED_RESPONSES(&xnfp->xnf_rx_ring)) {
1314 		mblk_t *mp;
1315 
1316 		if (xnfp->xnf_rx_hvcopy)
1317 			mp = xnf_process_hvcopy_recv(xnfp);
1318 		else
1319 			mp = xnf_process_recv(xnfp);
1320 
1321 		if (mp != NULL)
1322 			mac_rx(xnfp->xnf_mh, xnfp->xnf_rx_handle, mp);
1323 	}
1324 
1325 	/*
1326 	 * Clean tx ring and try to start any blocked xmit streams if
1327 	 * there is now some space.
1328 	 */
1329 	mutex_enter(&xnfp->xnf_txlock);
1330 	tx_ring_space = xnf_clean_tx_ring(xnfp);
1331 	mutex_exit(&xnfp->xnf_txlock);
1332 	if (tx_ring_space > XNF_TX_FREE_THRESH) {
1333 		mutex_exit(&xnfp->xnf_intrlock);
1334 		mac_tx_update(xnfp->xnf_mh);
1335 		mutex_enter(&xnfp->xnf_intrlock);
1336 	}
1337 
1338 	xnfp->xnf_stat_interrupts++;
1339 	mutex_exit(&xnfp->xnf_intrlock);
1340 	return (DDI_INTR_CLAIMED); /* indicate that the interrupt was for us */
1341 }
1342 
1343 /*
1344  *  xnf_start() -- start the board receiving and enable interrupts.
1345  */
1346 static int
1347 xnf_start(void *arg)
1348 {
1349 	xnf_t *xnfp = arg;
1350 
1351 #ifdef XNF_DEBUG
1352 	if (xnfdebug & XNF_DEBUG_TRACE)
1353 		printf("xnf%d start(0x%p)\n",
1354 		    ddi_get_instance(xnfp->xnf_devinfo), (void *)xnfp);
1355 #endif
1356 
1357 	mutex_enter(&xnfp->xnf_intrlock);
1358 	mutex_enter(&xnfp->xnf_txlock);
1359 
1360 	/* Accept packets from above. */
1361 	xnfp->xnf_running = B_TRUE;
1362 
1363 	mutex_exit(&xnfp->xnf_txlock);
1364 	mutex_exit(&xnfp->xnf_intrlock);
1365 
1366 	return (0);
1367 }
1368 
1369 /* xnf_stop() - disable hardware */
1370 static void
1371 xnf_stop(void *arg)
1372 {
1373 	xnf_t *xnfp = arg;
1374 
1375 #ifdef XNF_DEBUG
1376 	if (xnfdebug & XNF_DEBUG_TRACE)
1377 		printf("xnf%d stop(0x%p)\n",
1378 		    ddi_get_instance(xnfp->xnf_devinfo), (void *)xnfp);
1379 #endif
1380 
1381 	mutex_enter(&xnfp->xnf_intrlock);
1382 	mutex_enter(&xnfp->xnf_txlock);
1383 
1384 	xnfp->xnf_running = B_FALSE;
1385 
1386 	mutex_exit(&xnfp->xnf_txlock);
1387 	mutex_exit(&xnfp->xnf_intrlock);
1388 }
1389 
1390 /*
1391  * Driver private functions follow
1392  */
1393 
1394 /*
1395  * Hang buffer on rx ring
1396  */
1397 static void
1398 rx_buffer_hang(xnf_t *xnfp, struct xnf_buffer_desc *bdesc)
1399 {
1400 	volatile netif_rx_request_t	*reqp;
1401 	RING_IDX			hang_ix;
1402 	grant_ref_t			ref;
1403 	domid_t				oeid;
1404 
1405 	oeid = xvdi_get_oeid(xnfp->xnf_devinfo);
1406 
1407 	ASSERT(MUTEX_HELD(&xnfp->xnf_intrlock));
1408 	reqp = RING_GET_REQUEST(&xnfp->xnf_rx_ring,
1409 	    xnfp->xnf_rx_ring.req_prod_pvt);
1410 	hang_ix = (RING_IDX) (reqp - RING_GET_REQUEST(&xnfp->xnf_rx_ring, 0));
1411 	ASSERT(xnfp->xnf_rxpkt_bufptr[hang_ix] == NULL);
1412 	if (bdesc->grant_ref == GRANT_INVALID_REF) {
1413 		ref = gnttab_claim_grant_reference(&xnfp->xnf_gref_rx_head);
1414 		ASSERT((signed short)ref >= 0);
1415 		bdesc->grant_ref = ref;
1416 		if (xnfp->xnf_rx_hvcopy) {
1417 			pfn_t pfn = xnf_btop(bdesc->buf_phys);
1418 			mfn_t mfn = pfn_to_mfn(pfn);
1419 
1420 			gnttab_grant_foreign_access_ref(ref, oeid, mfn, 0);
1421 		} else {
1422 			gnttab_grant_foreign_transfer_ref(ref, oeid, 0);
1423 		}
1424 	}
1425 	reqp->id = hang_ix;
1426 	reqp->gref = bdesc->grant_ref;
1427 	bdesc->id = hang_ix;
1428 	xnfp->xnf_rxpkt_bufptr[hang_ix] = bdesc;
1429 	membar_producer();
1430 	xnfp->xnf_rx_ring.req_prod_pvt++;
1431 }
1432 
1433 static mblk_t *
1434 xnf_process_hvcopy_recv(xnf_t *xnfp)
1435 {
1436 	netif_rx_response_t *rxpkt;
1437 	mblk_t		*mp, *head, *tail;
1438 	struct		xnf_buffer_desc *bdesc;
1439 	boolean_t	hwcsum = B_FALSE, notify, work_to_do;
1440 	size_t 		len;
1441 
1442 	/*
1443 	 * in loop over unconsumed responses, we do:
1444 	 * 1. get a response
1445 	 * 2. take corresponding buffer off recv. ring
1446 	 * 3. indicate this by setting slot to NULL
1447 	 * 4. create a new message and
1448 	 * 5. copy data in, adjust ptr
1449 	 *
1450 	 * outside loop:
1451 	 * 7. make sure no more data has arrived; kick HV
1452 	 */
1453 
1454 	head = tail = NULL;
1455 
1456 loop:
1457 	while (RING_HAS_UNCONSUMED_RESPONSES(&xnfp->xnf_rx_ring)) {
1458 
1459 		/* 1. */
1460 		rxpkt = RING_GET_RESPONSE(&xnfp->xnf_rx_ring,
1461 		    xnfp->xnf_rx_ring.rsp_cons);
1462 
1463 		DTRACE_PROBE4(got_PKT, int, (int)rxpkt->id, int,
1464 		    (int)rxpkt->offset,
1465 		    int, (int)rxpkt->flags, int, (int)rxpkt->status);
1466 
1467 		/*
1468 		 * 2.
1469 		 * Take buffer off of receive ring
1470 		 */
1471 		hwcsum = B_FALSE;
1472 		bdesc = xnfp->xnf_rxpkt_bufptr[rxpkt->id];
1473 		/* 3 */
1474 		xnfp->xnf_rxpkt_bufptr[rxpkt->id] = NULL;
1475 		ASSERT(bdesc->id == rxpkt->id);
1476 		if (rxpkt->status <= 0) {
1477 			DTRACE_PROBE4(pkt_status_negative, int, rxpkt->status,
1478 			    char *, bdesc->buf, int, rxpkt->offset,
1479 			    char *, ((char *)bdesc->buf) + rxpkt->offset);
1480 			mp = NULL;
1481 			xnfp->xnf_stat_errrx++;
1482 			if (rxpkt->status == 0)
1483 				xnfp->xnf_stat_runt++;
1484 			if (rxpkt->status == NETIF_RSP_ERROR)
1485 				xnfp->xnf_stat_mac_rcv_error++;
1486 			if (rxpkt->status == NETIF_RSP_DROPPED)
1487 				xnfp->xnf_stat_norxbuf++;
1488 			/*
1489 			 * re-hang the buffer
1490 			 */
1491 			rx_buffer_hang(xnfp, bdesc);
1492 		} else {
1493 			grant_ref_t		ref =  bdesc->grant_ref;
1494 			struct xnf_buffer_desc	*new_bdesc;
1495 			unsigned long		off = rxpkt->offset;
1496 
1497 			DTRACE_PROBE4(pkt_status_ok, int, rxpkt->status,
1498 			    char *, bdesc->buf, int, rxpkt->offset,
1499 			    char *, ((char *)bdesc->buf) + rxpkt->offset);
1500 			len = rxpkt->status;
1501 			ASSERT(off + len <= PAGEOFFSET);
1502 			if (ref == GRANT_INVALID_REF) {
1503 				mp = NULL;
1504 				new_bdesc = bdesc;
1505 				cmn_err(CE_WARN, "Bad rx grant reference %d "
1506 				    "from dom %d", ref,
1507 				    xvdi_get_oeid(xnfp->xnf_devinfo));
1508 				goto luckless;
1509 			}
1510 			/*
1511 			 * Release ref which we'll be re-claiming in
1512 			 * rx_buffer_hang().
1513 			 */
1514 			bdesc->grant_ref = GRANT_INVALID_REF;
1515 			(void) gnttab_end_foreign_access_ref(ref, 0);
1516 			gnttab_release_grant_reference(&xnfp->xnf_gref_rx_head,
1517 			    ref);
1518 			if (rxpkt->flags & NETRXF_data_validated)
1519 				hwcsum = B_TRUE;
1520 
1521 			/*
1522 			 * XXPV for the initial implementation of HVcopy,
1523 			 * create a new msg and copy in the data
1524 			 */
1525 			/* 4. */
1526 			if ((mp = allocb(len, BPRI_MED)) == NULL) {
1527 				/*
1528 				 * Couldn't get buffer to copy to,
1529 				 * drop this data, and re-hang
1530 				 * the buffer on the ring.
1531 				 */
1532 				xnfp->xnf_stat_norxbuf++;
1533 				DTRACE_PROBE(alloc_nix);
1534 			} else {
1535 				/* 5. */
1536 				DTRACE_PROBE(alloc_ok);
1537 				bcopy(bdesc->buf + off, mp->b_wptr,
1538 				    len);
1539 				mp->b_wptr += len;
1540 			}
1541 			new_bdesc = bdesc;
1542 luckless:
1543 
1544 			/* Re-hang old or hang new buffer. */
1545 			rx_buffer_hang(xnfp, new_bdesc);
1546 		}
1547 		if (mp) {
1548 			if (hwcsum) {
1549 				/*
1550 				 * See comments in xnf_process_recv().
1551 				 */
1552 
1553 				(void) hcksum_assoc(mp, NULL,
1554 				    NULL, 0, 0, 0, 0,
1555 				    HCK_FULLCKSUM |
1556 				    HCK_FULLCKSUM_OK,
1557 				    0);
1558 				xnfp->xnf_stat_rx_cksum_no_need++;
1559 			}
1560 			if (head == NULL) {
1561 				head = tail = mp;
1562 			} else {
1563 				tail->b_next = mp;
1564 				tail = mp;
1565 			}
1566 
1567 			ASSERT(mp->b_next == NULL);
1568 
1569 			xnfp->xnf_stat_ipackets++;
1570 			xnfp->xnf_stat_rbytes += len;
1571 		}
1572 
1573 		xnfp->xnf_rx_ring.rsp_cons++;
1574 
1575 		xnfp->xnf_stat_hvcopy_packet_processed++;
1576 	}
1577 
1578 	/* 7. */
1579 	/*
1580 	 * Has more data come in since we started?
1581 	 */
1582 	/* LINTED: constant in conditional context */
1583 	RING_FINAL_CHECK_FOR_RESPONSES(&xnfp->xnf_rx_ring, work_to_do);
1584 	if (work_to_do)
1585 		goto loop;
1586 
1587 	/*
1588 	 * Indicate to the backend that we have re-filled the receive
1589 	 * ring.
1590 	 */
1591 	/* LINTED: constant in conditional context */
1592 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xnfp->xnf_rx_ring, notify);
1593 	if (notify)
1594 		ec_notify_via_evtchn(xnfp->xnf_evtchn);
1595 
1596 	return (head);
1597 }
1598 
1599 /* Process all queued received packets */
1600 static mblk_t *
1601 xnf_process_recv(xnf_t *xnfp)
1602 {
1603 	volatile netif_rx_response_t *rxpkt;
1604 	mblk_t *mp, *head, *tail;
1605 	struct xnf_buffer_desc *bdesc;
1606 	extern mblk_t *desballoc(unsigned char *, size_t, uint_t, frtn_t *);
1607 	boolean_t hwcsum = B_FALSE, notify, work_to_do;
1608 	size_t len;
1609 	pfn_t pfn;
1610 	long cnt;
1611 
1612 	head = tail = NULL;
1613 loop:
1614 	while (RING_HAS_UNCONSUMED_RESPONSES(&xnfp->xnf_rx_ring)) {
1615 
1616 		rxpkt = RING_GET_RESPONSE(&xnfp->xnf_rx_ring,
1617 		    xnfp->xnf_rx_ring.rsp_cons);
1618 
1619 		/*
1620 		 * Take buffer off of receive ring
1621 		 */
1622 		hwcsum = B_FALSE;
1623 		bdesc = xnfp->xnf_rxpkt_bufptr[rxpkt->id];
1624 		xnfp->xnf_rxpkt_bufptr[rxpkt->id] = NULL;
1625 		ASSERT(bdesc->id == rxpkt->id);
1626 		if (rxpkt->status <= 0) {
1627 			mp = NULL;
1628 			xnfp->xnf_stat_errrx++;
1629 			if (rxpkt->status == 0)
1630 				xnfp->xnf_stat_runt++;
1631 			if (rxpkt->status == NETIF_RSP_ERROR)
1632 				xnfp->xnf_stat_mac_rcv_error++;
1633 			if (rxpkt->status == NETIF_RSP_DROPPED)
1634 				xnfp->xnf_stat_norxbuf++;
1635 			/*
1636 			 * re-hang the buffer
1637 			 */
1638 			rx_buffer_hang(xnfp, bdesc);
1639 		} else {
1640 			grant_ref_t ref =  bdesc->grant_ref;
1641 			struct xnf_buffer_desc *new_bdesc;
1642 			unsigned long off = rxpkt->offset;
1643 			unsigned long mfn;
1644 
1645 			len = rxpkt->status;
1646 			ASSERT(off + len <= PAGEOFFSET);
1647 			if (ref == GRANT_INVALID_REF) {
1648 				mp = NULL;
1649 				new_bdesc = bdesc;
1650 				cmn_err(CE_WARN, "Bad rx grant reference %d "
1651 				    "from dom %d", ref,
1652 				    xvdi_get_oeid(xnfp->xnf_devinfo));
1653 				goto luckless;
1654 			}
1655 			bdesc->grant_ref = GRANT_INVALID_REF;
1656 			mfn = gnttab_end_foreign_transfer_ref(ref);
1657 			ASSERT(mfn != MFN_INVALID);
1658 			ASSERT(hat_getpfnum(kas.a_hat, bdesc->buf) ==
1659 			    PFN_INVALID);
1660 
1661 			gnttab_release_grant_reference(&xnfp->xnf_gref_rx_head,
1662 			    ref);
1663 			reassign_pfn(xnf_btop(bdesc->buf_phys), mfn);
1664 			hat_devload(kas.a_hat, bdesc->buf, PAGESIZE,
1665 			    xnf_btop(bdesc->buf_phys),
1666 			    PROT_READ | PROT_WRITE, HAT_LOAD);
1667 			balloon_drv_added(1);
1668 
1669 			if (rxpkt->flags & NETRXF_data_validated)
1670 				hwcsum = B_TRUE;
1671 			if (len <= xnf_rx_bcopy_thresh) {
1672 				/*
1673 				 * For small buffers, just copy the data
1674 				 * and send the copy upstream.
1675 				 */
1676 				new_bdesc = NULL;
1677 			} else {
1678 				/*
1679 				 * We send a pointer to this data upstream;
1680 				 * we need a new buffer to replace this one.
1681 				 */
1682 				mutex_enter(&xnfp->xnf_rx_buf_mutex);
1683 				new_bdesc = xnf_get_buffer(xnfp);
1684 				if (new_bdesc != NULL) {
1685 					xnfp->xnf_rx_bufs_outstanding++;
1686 				} else {
1687 					xnfp->xnf_stat_rx_no_ringbuf++;
1688 				}
1689 				mutex_exit(&xnfp->xnf_rx_buf_mutex);
1690 			}
1691 
1692 			if (new_bdesc == NULL) {
1693 				/*
1694 				 * Don't have a new ring buffer; bcopy the data
1695 				 * from the buffer, and preserve the
1696 				 * original buffer
1697 				 */
1698 				if ((mp = allocb(len, BPRI_MED)) == NULL) {
1699 					/*
1700 					 * Could't get buffer to copy to,
1701 					 * drop this data, and re-hang
1702 					 * the buffer on the ring.
1703 					 */
1704 					xnfp->xnf_stat_norxbuf++;
1705 				} else {
1706 					bcopy(bdesc->buf + off, mp->b_wptr,
1707 					    len);
1708 				}
1709 				/*
1710 				 * Give the buffer page back to xen
1711 				 */
1712 				pfn = xnf_btop(bdesc->buf_phys);
1713 				cnt = balloon_free_pages(1, &mfn, bdesc->buf,
1714 				    &pfn);
1715 				if (cnt != 1) {
1716 					cmn_err(CE_WARN, "unable to give a "
1717 					    "page back to the hypervisor\n");
1718 				}
1719 				new_bdesc = bdesc;
1720 			} else {
1721 				if ((mp = desballoc((unsigned char *)bdesc->buf,
1722 				    off + len, 0, (frtn_t *)bdesc)) == NULL) {
1723 					/*
1724 					 * Couldn't get mblk to pass recv data
1725 					 * up with, free the old ring buffer
1726 					 */
1727 					xnfp->xnf_stat_norxbuf++;
1728 					xnf_rcv_complete(bdesc);
1729 					goto luckless;
1730 				}
1731 				(void) ddi_dma_sync(bdesc->dma_handle,
1732 				    0, 0, DDI_DMA_SYNC_FORCPU);
1733 
1734 				mp->b_wptr += off;
1735 				mp->b_rptr += off;
1736 			}
1737 luckless:
1738 			if (mp)
1739 				mp->b_wptr += len;
1740 			/* re-hang old or hang new buffer */
1741 			rx_buffer_hang(xnfp, new_bdesc);
1742 		}
1743 		if (mp) {
1744 			if (hwcsum) {
1745 				/*
1746 				 * If the peer says that the data has
1747 				 * been validated then we declare that
1748 				 * the full checksum has been
1749 				 * verified.
1750 				 *
1751 				 * We don't look at the "checksum
1752 				 * blank" flag, and hence could have a
1753 				 * packet here that we are asserting
1754 				 * is good with a blank checksum.
1755 				 *
1756 				 * The hardware checksum offload
1757 				 * specification says that we must
1758 				 * provide the actual checksum as well
1759 				 * as an assertion that it is valid,
1760 				 * but the protocol stack doesn't
1761 				 * actually use it and some other
1762 				 * drivers don't bother, so we don't.
1763 				 * If it was necessary we could grovel
1764 				 * in the packet to find it.
1765 				 */
1766 
1767 				(void) hcksum_assoc(mp, NULL,
1768 				    NULL, 0, 0, 0, 0,
1769 				    HCK_FULLCKSUM |
1770 				    HCK_FULLCKSUM_OK,
1771 				    0);
1772 				xnfp->xnf_stat_rx_cksum_no_need++;
1773 			}
1774 			if (head == NULL) {
1775 				head = tail = mp;
1776 			} else {
1777 				tail->b_next = mp;
1778 				tail = mp;
1779 			}
1780 
1781 			ASSERT(mp->b_next == NULL);
1782 
1783 			xnfp->xnf_stat_ipackets++;
1784 			xnfp->xnf_stat_rbytes += len;
1785 		}
1786 
1787 		xnfp->xnf_rx_ring.rsp_cons++;
1788 	}
1789 
1790 	/*
1791 	 * Has more data come in since we started?
1792 	 */
1793 	/* LINTED: constant in conditional context */
1794 	RING_FINAL_CHECK_FOR_RESPONSES(&xnfp->xnf_rx_ring, work_to_do);
1795 	if (work_to_do)
1796 		goto loop;
1797 
1798 	/*
1799 	 * Indicate to the backend that we have re-filled the receive
1800 	 * ring.
1801 	 */
1802 	/* LINTED: constant in conditional context */
1803 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xnfp->xnf_rx_ring, notify);
1804 	if (notify)
1805 		ec_notify_via_evtchn(xnfp->xnf_evtchn);
1806 
1807 	return (head);
1808 }
1809 
1810 /* Called when the upper layers free a message we passed upstream */
1811 static void
1812 xnf_rcv_complete(struct xnf_buffer_desc *bdesc)
1813 {
1814 	xnf_t *xnfp = bdesc->xnfp;
1815 	pfn_t pfn;
1816 	long cnt;
1817 
1818 	/* One less outstanding receive buffer */
1819 	mutex_enter(&xnfp->xnf_rx_buf_mutex);
1820 	--xnfp->xnf_rx_bufs_outstanding;
1821 	/*
1822 	 * Return buffer to the free list, unless the free list is getting
1823 	 * too large.  XXPV - this threshold may need tuning.
1824 	 */
1825 	if (xnfp->xnf_rx_descs_free < xnf_rx_bufs_lowat) {
1826 		/*
1827 		 * Unmap the page, and hand the machine page back
1828 		 * to xen so it can be re-used as a backend net buffer.
1829 		 */
1830 		pfn = xnf_btop(bdesc->buf_phys);
1831 		cnt = balloon_free_pages(1, NULL, bdesc->buf, &pfn);
1832 		if (cnt != 1) {
1833 			cmn_err(CE_WARN, "unable to give a page back to the "
1834 			    "hypervisor\n");
1835 		}
1836 
1837 		bdesc->next = xnfp->xnf_free_list;
1838 		xnfp->xnf_free_list = bdesc;
1839 		xnfp->xnf_rx_descs_free++;
1840 		mutex_exit(&xnfp->xnf_rx_buf_mutex);
1841 	} else {
1842 		/*
1843 		 * We can return everything here since we have a free buffer
1844 		 * that we have not given the backing page for back to xen.
1845 		 */
1846 		--xnfp->xnf_rx_buffer_count;
1847 		mutex_exit(&xnfp->xnf_rx_buf_mutex);
1848 		(void) ddi_dma_unbind_handle(bdesc->dma_handle);
1849 		ddi_dma_mem_free(&bdesc->acc_handle);
1850 		ddi_dma_free_handle(&bdesc->dma_handle);
1851 		kmem_free(bdesc, sizeof (*bdesc));
1852 	}
1853 }
1854 
1855 /*
1856  *  xnf_alloc_dma_resources() -- initialize the drivers structures
1857  */
1858 static int
1859 xnf_alloc_dma_resources(xnf_t *xnfp)
1860 {
1861 	dev_info_t 		*devinfo = xnfp->xnf_devinfo;
1862 	int			i;
1863 	size_t			len;
1864 	ddi_dma_cookie_t	dma_cookie;
1865 	uint_t			ncookies;
1866 	struct xnf_buffer_desc	*bdesc;
1867 	int			rc;
1868 	caddr_t			rptr;
1869 
1870 	xnfp->xnf_n_rx = NET_RX_RING_SIZE;
1871 	xnfp->xnf_max_rx_bufs = xnf_rx_bufs_hiwat;
1872 
1873 	xnfp->xnf_n_tx = NET_TX_RING_SIZE;
1874 
1875 	/*
1876 	 * The code below allocates all the DMA data structures that
1877 	 * need to be released when the driver is detached.
1878 	 *
1879 	 * First allocate handles for mapping (virtual address) pointers to
1880 	 * transmit data buffers to physical addresses
1881 	 */
1882 	for (i = 0; i < xnfp->xnf_n_tx; i++) {
1883 		if ((rc = ddi_dma_alloc_handle(devinfo,
1884 		    &tx_buffer_dma_attr, DDI_DMA_SLEEP, 0,
1885 		    &xnfp->xnf_tx_pkt_info[i].dma_handle)) != DDI_SUCCESS)
1886 			return (DDI_FAILURE);
1887 	}
1888 
1889 	/*
1890 	 * Allocate page for the transmit descriptor ring.
1891 	 */
1892 	if (ddi_dma_alloc_handle(devinfo, &ringbuf_dma_attr,
1893 	    DDI_DMA_SLEEP, 0, &xnfp->xnf_tx_ring_dma_handle) != DDI_SUCCESS)
1894 		goto alloc_error;
1895 
1896 	if (ddi_dma_mem_alloc(xnfp->xnf_tx_ring_dma_handle,
1897 	    PAGESIZE, &accattr, DDI_DMA_CONSISTENT,
1898 	    DDI_DMA_SLEEP, 0, &rptr, &len,
1899 	    &xnfp->xnf_tx_ring_dma_acchandle) != DDI_SUCCESS) {
1900 		ddi_dma_free_handle(&xnfp->xnf_tx_ring_dma_handle);
1901 		xnfp->xnf_tx_ring_dma_handle = NULL;
1902 		goto alloc_error;
1903 	}
1904 
1905 	if ((rc = ddi_dma_addr_bind_handle(xnfp->xnf_tx_ring_dma_handle, NULL,
1906 	    rptr, PAGESIZE, DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
1907 	    DDI_DMA_SLEEP, 0, &dma_cookie, &ncookies)) != DDI_DMA_MAPPED) {
1908 		ddi_dma_mem_free(&xnfp->xnf_tx_ring_dma_acchandle);
1909 		ddi_dma_free_handle(&xnfp->xnf_tx_ring_dma_handle);
1910 		xnfp->xnf_tx_ring_dma_handle = NULL;
1911 		xnfp->xnf_tx_ring_dma_acchandle = NULL;
1912 		if (rc == DDI_DMA_NORESOURCES)
1913 			goto alloc_error;
1914 		else
1915 			goto error;
1916 	}
1917 
1918 	ASSERT(ncookies == 1);
1919 	bzero(rptr, PAGESIZE);
1920 	/* LINTED: constant in conditional context */
1921 	SHARED_RING_INIT((netif_tx_sring_t *)rptr);
1922 	/* LINTED: constant in conditional context */
1923 	FRONT_RING_INIT(&xnfp->xnf_tx_ring, (netif_tx_sring_t *)rptr, PAGESIZE);
1924 	xnfp->xnf_tx_ring_phys_addr = dma_cookie.dmac_laddress;
1925 
1926 	/*
1927 	 * Allocate page for the receive descriptor ring.
1928 	 */
1929 	if (ddi_dma_alloc_handle(devinfo, &ringbuf_dma_attr,
1930 	    DDI_DMA_SLEEP, 0, &xnfp->xnf_rx_ring_dma_handle) != DDI_SUCCESS)
1931 		goto alloc_error;
1932 
1933 	if (ddi_dma_mem_alloc(xnfp->xnf_rx_ring_dma_handle,
1934 	    PAGESIZE, &accattr, DDI_DMA_CONSISTENT,
1935 	    DDI_DMA_SLEEP, 0, &rptr, &len,
1936 	    &xnfp->xnf_rx_ring_dma_acchandle) != DDI_SUCCESS) {
1937 		ddi_dma_free_handle(&xnfp->xnf_rx_ring_dma_handle);
1938 		xnfp->xnf_rx_ring_dma_handle = NULL;
1939 		goto alloc_error;
1940 	}
1941 
1942 	if ((rc = ddi_dma_addr_bind_handle(xnfp->xnf_rx_ring_dma_handle, NULL,
1943 	    rptr, PAGESIZE, DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
1944 	    DDI_DMA_SLEEP, 0, &dma_cookie, &ncookies)) != DDI_DMA_MAPPED) {
1945 		ddi_dma_mem_free(&xnfp->xnf_rx_ring_dma_acchandle);
1946 		ddi_dma_free_handle(&xnfp->xnf_rx_ring_dma_handle);
1947 		xnfp->xnf_rx_ring_dma_handle = NULL;
1948 		xnfp->xnf_rx_ring_dma_acchandle = NULL;
1949 		if (rc == DDI_DMA_NORESOURCES)
1950 			goto alloc_error;
1951 		else
1952 			goto error;
1953 	}
1954 
1955 	ASSERT(ncookies == 1);
1956 	bzero(rptr, PAGESIZE);
1957 	/* LINTED: constant in conditional context */
1958 	SHARED_RING_INIT((netif_rx_sring_t *)rptr);
1959 	/* LINTED: constant in conditional context */
1960 	FRONT_RING_INIT(&xnfp->xnf_rx_ring, (netif_rx_sring_t *)rptr, PAGESIZE);
1961 	xnfp->xnf_rx_ring_phys_addr = dma_cookie.dmac_laddress;
1962 
1963 	/*
1964 	 * Preallocate receive buffers for each receive descriptor.
1965 	 */
1966 
1967 	/* Set up the "free list" of receive buffer descriptors */
1968 	for (i = 0; i < xnfp->xnf_n_rx; i++) {
1969 		if ((bdesc = xnf_alloc_buffer(xnfp)) == NULL)
1970 			goto alloc_error;
1971 		bdesc->next = xnfp->xnf_free_list;
1972 		xnfp->xnf_free_list = bdesc;
1973 	}
1974 
1975 	return (DDI_SUCCESS);
1976 
1977 alloc_error:
1978 	cmn_err(CE_WARN, "xnf%d: could not allocate enough DMA memory",
1979 	    ddi_get_instance(xnfp->xnf_devinfo));
1980 error:
1981 	xnf_release_dma_resources(xnfp);
1982 	return (DDI_FAILURE);
1983 }
1984 
1985 /*
1986  * Release all DMA resources in the opposite order from acquisition
1987  * Should not be called until all outstanding esballoc buffers
1988  * have been returned.
1989  */
1990 static void
1991 xnf_release_dma_resources(xnf_t *xnfp)
1992 {
1993 	int i;
1994 
1995 	/*
1996 	 * Free receive buffers which are currently associated with
1997 	 * descriptors
1998 	 */
1999 	for (i = 0; i < xnfp->xnf_n_rx; i++) {
2000 		struct xnf_buffer_desc *bp;
2001 
2002 		if ((bp = xnfp->xnf_rxpkt_bufptr[i]) == NULL)
2003 			continue;
2004 		xnf_free_buffer(bp);
2005 		xnfp->xnf_rxpkt_bufptr[i] = NULL;
2006 	}
2007 
2008 	/* Free the receive ring buffer */
2009 	if (xnfp->xnf_rx_ring_dma_acchandle != NULL) {
2010 		(void) ddi_dma_unbind_handle(xnfp->xnf_rx_ring_dma_handle);
2011 		ddi_dma_mem_free(&xnfp->xnf_rx_ring_dma_acchandle);
2012 		ddi_dma_free_handle(&xnfp->xnf_rx_ring_dma_handle);
2013 		xnfp->xnf_rx_ring_dma_acchandle = NULL;
2014 	}
2015 	/* Free the transmit ring buffer */
2016 	if (xnfp->xnf_tx_ring_dma_acchandle != NULL) {
2017 		(void) ddi_dma_unbind_handle(xnfp->xnf_tx_ring_dma_handle);
2018 		ddi_dma_mem_free(&xnfp->xnf_tx_ring_dma_acchandle);
2019 		ddi_dma_free_handle(&xnfp->xnf_tx_ring_dma_handle);
2020 		xnfp->xnf_tx_ring_dma_acchandle = NULL;
2021 	}
2022 }
2023 
2024 static void
2025 xnf_release_mblks(xnf_t *xnfp)
2026 {
2027 	int	i;
2028 
2029 	for (i = 0; i < xnfp->xnf_n_tx; i++) {
2030 		if (xnfp->xnf_tx_pkt_info[i].mp == NULL)
2031 			continue;
2032 		freemsg(xnfp->xnf_tx_pkt_info[i].mp);
2033 		xnfp->xnf_tx_pkt_info[i].mp = NULL;
2034 		(void) ddi_dma_unbind_handle(
2035 		    xnfp->xnf_tx_pkt_info[i].dma_handle);
2036 	}
2037 }
2038 
2039 /*
2040  * Remove a xmit buffer descriptor from the head of the free list and return
2041  * a pointer to it.  If no buffers on list, attempt to allocate a new one.
2042  * Called with the tx_buf_mutex held.
2043  */
2044 static struct xnf_buffer_desc *
2045 xnf_get_tx_buffer(xnf_t *xnfp)
2046 {
2047 	struct xnf_buffer_desc *bdesc;
2048 
2049 	bdesc = xnfp->xnf_tx_free_list;
2050 	if (bdesc != NULL) {
2051 		xnfp->xnf_tx_free_list = bdesc->next;
2052 	} else {
2053 		bdesc = xnf_alloc_tx_buffer(xnfp);
2054 	}
2055 	return (bdesc);
2056 }
2057 
2058 /*
2059  * Remove a buffer descriptor from the head of the free list and return
2060  * a pointer to it.  If no buffers on list, attempt to allocate a new one.
2061  * Called with the rx_buf_mutex held.
2062  */
2063 static struct xnf_buffer_desc *
2064 xnf_get_buffer(xnf_t *xnfp)
2065 {
2066 	struct xnf_buffer_desc *bdesc;
2067 
2068 	bdesc = xnfp->xnf_free_list;
2069 	if (bdesc != NULL) {
2070 		xnfp->xnf_free_list = bdesc->next;
2071 		xnfp->xnf_rx_descs_free--;
2072 	} else {
2073 		bdesc = xnf_alloc_buffer(xnfp);
2074 	}
2075 	return (bdesc);
2076 }
2077 
2078 /*
2079  * Free a xmit buffer back to the xmit free list
2080  */
2081 static void
2082 xnf_free_tx_buffer(struct xnf_buffer_desc *bp)
2083 {
2084 	xnf_t *xnfp = bp->xnfp;
2085 
2086 	mutex_enter(&xnfp->xnf_tx_buf_mutex);
2087 	bp->next = xnfp->xnf_tx_free_list;
2088 	xnfp->xnf_tx_free_list = bp;
2089 	mutex_exit(&xnfp->xnf_tx_buf_mutex);
2090 }
2091 
2092 /*
2093  * Put a buffer descriptor onto the head of the free list.
2094  * for page-flip:
2095  * We can't really free these buffers back to the kernel
2096  * since we have given away their backing page to be used
2097  * by the back end net driver.
2098  * for hvcopy:
2099  * release all the memory
2100  */
2101 static void
2102 xnf_free_buffer(struct xnf_buffer_desc *bdesc)
2103 {
2104 	xnf_t *xnfp = bdesc->xnfp;
2105 
2106 	mutex_enter(&xnfp->xnf_rx_buf_mutex);
2107 	if (xnfp->xnf_rx_hvcopy) {
2108 		if (ddi_dma_unbind_handle(bdesc->dma_handle) != DDI_SUCCESS)
2109 			goto out;
2110 		ddi_dma_mem_free(&bdesc->acc_handle);
2111 		ddi_dma_free_handle(&bdesc->dma_handle);
2112 		kmem_free(bdesc, sizeof (*bdesc));
2113 		xnfp->xnf_rx_buffer_count--;
2114 	} else {
2115 		bdesc->next = xnfp->xnf_free_list;
2116 		xnfp->xnf_free_list = bdesc;
2117 		xnfp->xnf_rx_descs_free++;
2118 	}
2119 out:
2120 	mutex_exit(&xnfp->xnf_rx_buf_mutex);
2121 }
2122 
2123 /*
2124  * Allocate a DMA-able xmit buffer, including a structure to
2125  * keep track of the buffer.  Called with tx_buf_mutex held.
2126  */
2127 static struct xnf_buffer_desc *
2128 xnf_alloc_tx_buffer(xnf_t *xnfp)
2129 {
2130 	struct xnf_buffer_desc *bdesc;
2131 	size_t len;
2132 
2133 	if ((bdesc = kmem_zalloc(sizeof (*bdesc), KM_NOSLEEP)) == NULL)
2134 		return (NULL);
2135 
2136 	/* allocate a DMA access handle for receive buffer */
2137 	if (ddi_dma_alloc_handle(xnfp->xnf_devinfo, &tx_buffer_dma_attr,
2138 	    0, 0, &bdesc->dma_handle) != DDI_SUCCESS)
2139 		goto failure;
2140 
2141 	/* Allocate DMA-able memory for transmit buffer */
2142 	if (ddi_dma_mem_alloc(bdesc->dma_handle,
2143 	    PAGESIZE, &data_accattr, DDI_DMA_STREAMING, 0, 0,
2144 	    &bdesc->buf, &len, &bdesc->acc_handle) != DDI_SUCCESS)
2145 		goto failure_1;
2146 
2147 	bdesc->xnfp = xnfp;
2148 	xnfp->xnf_tx_buffer_count++;
2149 
2150 	return (bdesc);
2151 
2152 failure_1:
2153 	ddi_dma_free_handle(&bdesc->dma_handle);
2154 
2155 failure:
2156 	kmem_free(bdesc, sizeof (*bdesc));
2157 	return (NULL);
2158 }
2159 
2160 /*
2161  * Allocate a DMA-able receive buffer, including a structure to
2162  * keep track of the buffer.  Called with rx_buf_mutex held.
2163  */
2164 static struct xnf_buffer_desc *
2165 xnf_alloc_buffer(xnf_t *xnfp)
2166 {
2167 	struct			xnf_buffer_desc *bdesc;
2168 	size_t			len;
2169 	uint_t			ncookies;
2170 	ddi_dma_cookie_t	dma_cookie;
2171 	long			cnt;
2172 	pfn_t			pfn;
2173 
2174 	if (xnfp->xnf_rx_buffer_count >= xnfp->xnf_max_rx_bufs)
2175 		return (NULL);
2176 
2177 	if ((bdesc = kmem_zalloc(sizeof (*bdesc), KM_NOSLEEP)) == NULL)
2178 		return (NULL);
2179 
2180 	/* allocate a DMA access handle for receive buffer */
2181 	if (ddi_dma_alloc_handle(xnfp->xnf_devinfo, &rx_buffer_dma_attr,
2182 	    0, 0, &bdesc->dma_handle) != DDI_SUCCESS)
2183 		goto failure;
2184 
2185 	/* Allocate DMA-able memory for receive buffer */
2186 	if (ddi_dma_mem_alloc(bdesc->dma_handle,
2187 	    PAGESIZE, &data_accattr, DDI_DMA_STREAMING, 0, 0,
2188 	    &bdesc->buf, &len, &bdesc->acc_handle) != DDI_SUCCESS)
2189 		goto failure_1;
2190 
2191 	/* bind to virtual address of buffer to get physical address */
2192 	if (ddi_dma_addr_bind_handle(bdesc->dma_handle, NULL,
2193 	    bdesc->buf, PAGESIZE, DDI_DMA_READ | DDI_DMA_STREAMING,
2194 	    DDI_DMA_SLEEP, 0, &dma_cookie, &ncookies) != DDI_DMA_MAPPED)
2195 		goto failure_2;
2196 
2197 	bdesc->buf_phys = dma_cookie.dmac_laddress;
2198 	bdesc->xnfp = xnfp;
2199 	if (xnfp->xnf_rx_hvcopy) {
2200 		bdesc->free_rtn.free_func = xnf_copy_rcv_complete;
2201 	} else {
2202 		bdesc->free_rtn.free_func = xnf_rcv_complete;
2203 	}
2204 	bdesc->free_rtn.free_arg = (char *)bdesc;
2205 	bdesc->grant_ref = GRANT_INVALID_REF;
2206 	ASSERT(ncookies == 1);
2207 
2208 	xnfp->xnf_rx_buffer_count++;
2209 
2210 	if (!xnfp->xnf_rx_hvcopy) {
2211 		/*
2212 		 * Unmap the page, and hand the machine page back
2213 		 * to xen so it can be used as a backend net buffer.
2214 		 */
2215 		pfn = xnf_btop(bdesc->buf_phys);
2216 		cnt = balloon_free_pages(1, NULL, bdesc->buf, &pfn);
2217 		if (cnt != 1) {
2218 			cmn_err(CE_WARN, "unable to give a page back to the "
2219 			    "hypervisor\n");
2220 		}
2221 	}
2222 
2223 	return (bdesc);
2224 
2225 failure_2:
2226 	ddi_dma_mem_free(&bdesc->acc_handle);
2227 
2228 failure_1:
2229 	ddi_dma_free_handle(&bdesc->dma_handle);
2230 
2231 failure:
2232 	kmem_free(bdesc, sizeof (*bdesc));
2233 	return (NULL);
2234 }
2235 
2236 /*
2237  * Statistics.
2238  */
2239 static char *xnf_aux_statistics[] = {
2240 	"tx_cksum_deferred",
2241 	"rx_cksum_no_need",
2242 	"interrupts",
2243 	"unclaimed_interrupts",
2244 	"tx_pullup",
2245 	"tx_pagebndry",
2246 	"tx_attempt",
2247 	"rx_no_ringbuf",
2248 	"hvcopy_packet_processed",
2249 };
2250 
2251 static int
2252 xnf_kstat_aux_update(kstat_t *ksp, int flag)
2253 {
2254 	xnf_t *xnfp;
2255 	kstat_named_t *knp;
2256 
2257 	if (flag != KSTAT_READ)
2258 		return (EACCES);
2259 
2260 	xnfp = ksp->ks_private;
2261 	knp = ksp->ks_data;
2262 
2263 	/*
2264 	 * Assignment order must match that of the names in
2265 	 * xnf_aux_statistics.
2266 	 */
2267 	(knp++)->value.ui64 = xnfp->xnf_stat_tx_cksum_deferred;
2268 	(knp++)->value.ui64 = xnfp->xnf_stat_rx_cksum_no_need;
2269 
2270 	(knp++)->value.ui64 = xnfp->xnf_stat_interrupts;
2271 	(knp++)->value.ui64 = xnfp->xnf_stat_unclaimed_interrupts;
2272 	(knp++)->value.ui64 = xnfp->xnf_stat_tx_pullup;
2273 	(knp++)->value.ui64 = xnfp->xnf_stat_tx_pagebndry;
2274 	(knp++)->value.ui64 = xnfp->xnf_stat_tx_attempt;
2275 	(knp++)->value.ui64 = xnfp->xnf_stat_rx_no_ringbuf;
2276 
2277 	(knp++)->value.ui64 = xnfp->xnf_stat_hvcopy_packet_processed;
2278 
2279 	return (0);
2280 }
2281 
2282 static boolean_t
2283 xnf_kstat_init(xnf_t *xnfp)
2284 {
2285 	int nstat = sizeof (xnf_aux_statistics) /
2286 	    sizeof (xnf_aux_statistics[0]);
2287 	char **cp = xnf_aux_statistics;
2288 	kstat_named_t *knp;
2289 
2290 	/*
2291 	 * Create and initialise kstats.
2292 	 */
2293 	if ((xnfp->xnf_kstat_aux = kstat_create("xnf",
2294 	    ddi_get_instance(xnfp->xnf_devinfo),
2295 	    "aux_statistics", "net", KSTAT_TYPE_NAMED,
2296 	    nstat, 0)) == NULL)
2297 		return (B_FALSE);
2298 
2299 	xnfp->xnf_kstat_aux->ks_private = xnfp;
2300 	xnfp->xnf_kstat_aux->ks_update = xnf_kstat_aux_update;
2301 
2302 	knp = xnfp->xnf_kstat_aux->ks_data;
2303 	while (nstat > 0) {
2304 		kstat_named_init(knp, *cp, KSTAT_DATA_UINT64);
2305 
2306 		knp++;
2307 		cp++;
2308 		nstat--;
2309 	}
2310 
2311 	kstat_install(xnfp->xnf_kstat_aux);
2312 
2313 	return (B_TRUE);
2314 }
2315 
2316 static int
2317 xnf_stat(void *arg, uint_t stat, uint64_t *val)
2318 {
2319 	xnf_t *xnfp = arg;
2320 
2321 	mutex_enter(&xnfp->xnf_intrlock);
2322 	mutex_enter(&xnfp->xnf_txlock);
2323 
2324 #define	mac_stat(q, r)				\
2325 	case (MAC_STAT_##q):			\
2326 		*val = xnfp->xnf_stat_##r;	\
2327 		break
2328 
2329 #define	ether_stat(q, r)			\
2330 	case (ETHER_STAT_##q):			\
2331 		*val = xnfp->xnf_stat_##r;	\
2332 		break
2333 
2334 	switch (stat) {
2335 
2336 	mac_stat(IPACKETS, ipackets);
2337 	mac_stat(OPACKETS, opackets);
2338 	mac_stat(RBYTES, rbytes);
2339 	mac_stat(OBYTES, obytes);
2340 	mac_stat(NORCVBUF, norxbuf);
2341 	mac_stat(IERRORS, errrx);
2342 	mac_stat(NOXMTBUF, tx_defer);
2343 
2344 	ether_stat(MACRCV_ERRORS, mac_rcv_error);
2345 	ether_stat(TOOSHORT_ERRORS, runt);
2346 
2347 	default:
2348 		mutex_exit(&xnfp->xnf_txlock);
2349 		mutex_exit(&xnfp->xnf_intrlock);
2350 
2351 		return (ENOTSUP);
2352 	}
2353 
2354 #undef mac_stat
2355 #undef ether_stat
2356 
2357 	mutex_exit(&xnfp->xnf_txlock);
2358 	mutex_exit(&xnfp->xnf_intrlock);
2359 
2360 	return (0);
2361 }
2362 
2363 /*ARGSUSED*/
2364 static void
2365 xnf_blank(void *arg, time_t ticks, uint_t count)
2366 {
2367 	/*
2368 	 * XXPV dme: blanking is not currently implemented.
2369 	 *
2370 	 * It's not obvious how to use the 'ticks' argument here.
2371 	 *
2372 	 * 'Count' might be used as an indicator of how to set
2373 	 * rsp_event when posting receive buffers to the rx_ring.  It
2374 	 * would replace the code at the tail of xnf_process_recv()
2375 	 * that simply indicates that the next completed packet should
2376 	 * cause an interrupt.
2377 	 */
2378 }
2379 
2380 static void
2381 xnf_resources(void *arg)
2382 {
2383 	xnf_t *xnfp = arg;
2384 	mac_rx_fifo_t mrf;
2385 
2386 	mrf.mrf_type = MAC_RX_FIFO;
2387 	mrf.mrf_blank = xnf_blank;
2388 	mrf.mrf_arg = (void *)xnfp;
2389 	mrf.mrf_normal_blank_time = 128;	/* XXPV dme: see xnf_blank() */
2390 	mrf.mrf_normal_pkt_count = 8;		/* XXPV dme: see xnf_blank() */
2391 
2392 	xnfp->xnf_rx_handle = mac_resource_add(xnfp->xnf_mh,
2393 	    (mac_resource_t *)&mrf);
2394 }
2395 
2396 /*ARGSUSED*/
2397 static void
2398 xnf_ioctl(void *arg, queue_t *q, mblk_t *mp)
2399 {
2400 	miocnak(q, mp, 0, EINVAL);
2401 }
2402 
2403 static boolean_t
2404 xnf_getcapab(void *arg, mac_capab_t cap, void *cap_data)
2405 {
2406 	xnf_t *xnfp = arg;
2407 
2408 	switch (cap) {
2409 	case MAC_CAPAB_HCKSUM: {
2410 		uint32_t *capab = cap_data;
2411 
2412 		/*
2413 		 * We declare ourselves capable of HCKSUM_INET_PARTIAL
2414 		 * in order that the protocol stack insert the
2415 		 * pseudo-header checksum in packets that it passes
2416 		 * down to us.
2417 		 *
2418 		 * Whilst the flag used to communicate with dom0 is
2419 		 * called "NETTXF_csum_blank", the checksum in the
2420 		 * packet must contain the pseudo-header checksum and
2421 		 * not zero. (In fact, a Solaris dom0 is happy to deal
2422 		 * with a checksum of zero, but a Linux dom0 is not.)
2423 		 */
2424 		if (xnfp->xnf_cksum_offload)
2425 			*capab = HCKSUM_INET_PARTIAL;
2426 		else
2427 			*capab = 0;
2428 		break;
2429 	}
2430 
2431 	case MAC_CAPAB_POLL:
2432 		/* Just return B_TRUE. */
2433 		break;
2434 
2435 	default:
2436 		return (B_FALSE);
2437 	}
2438 
2439 	return (B_TRUE);
2440 }
2441 
2442 /*ARGSUSED*/
2443 static void
2444 oe_state_change(dev_info_t *dip, ddi_eventcookie_t id,
2445     void *arg, void *impl_data)
2446 {
2447 	xnf_t *xnfp = ddi_get_driver_private(dip);
2448 	XenbusState new_state = *(XenbusState *)impl_data;
2449 
2450 	ASSERT(xnfp != NULL);
2451 
2452 	switch (new_state) {
2453 	case XenbusStateConnected:
2454 		mutex_enter(&xnfp->xnf_intrlock);
2455 		mutex_enter(&xnfp->xnf_txlock);
2456 
2457 		xnfp->xnf_connected = B_TRUE;
2458 		cv_broadcast(&xnfp->xnf_cv);
2459 
2460 		mutex_exit(&xnfp->xnf_txlock);
2461 		mutex_exit(&xnfp->xnf_intrlock);
2462 
2463 		ec_notify_via_evtchn(xnfp->xnf_evtchn);
2464 		break;
2465 
2466 	default:
2467 		break;
2468 	}
2469 }
2470 
2471 /*
2472  * Check whether backend is capable of and willing to talk
2473  * to us via hypervisor copy, as opposed to page flip.
2474  */
2475 static boolean_t
2476 xnf_hvcopy_peer_status(dev_info_t *devinfo)
2477 {
2478 	int	be_rx_copy;
2479 	int	err;
2480 
2481 	err = xenbus_scanf(XBT_NULL, xvdi_get_oename(devinfo),
2482 	    "feature-rx-copy", "%d", &be_rx_copy);
2483 	/*
2484 	 * If we fail to read the store we assume that the key is
2485 	 * absent, implying an older domain at the far end.  Older
2486 	 * domains cannot do HV copy (we assume ..).
2487 	 */
2488 	if (err != 0)
2489 		be_rx_copy = 0;
2490 
2491 	return (be_rx_copy?B_TRUE:B_FALSE);
2492 }
2493