xref: /titanic_50/usr/src/uts/common/xen/io/xnf.c (revision 142c9f13e148d687426ed2d4e8bd93717eeaebbc)
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 	xvdi_free_evtchn(devinfo);
782 #else
783 	ddi_remove_intr(devinfo, 0, xnfp->xnf_icookie);
784 #endif
785 	xnfp->xnf_evtchn = INVALID_EVTCHN;
786 
787 failure_1:
788 	xnf_release_dma_resources(xnfp);
789 	cv_destroy(&xnfp->xnf_cv);
790 	mutex_destroy(&xnfp->xnf_rx_buf_mutex);
791 	mutex_destroy(&xnfp->xnf_txlock);
792 	mutex_destroy(&xnfp->xnf_intrlock);
793 
794 failure:
795 	kmem_free(xnfp, sizeof (*xnfp));
796 	if (macp != NULL)
797 		mac_free(macp);
798 
799 	return (DDI_FAILURE);
800 }
801 
802 /*  detach(9E) -- Detach a device from the system */
803 static int
804 xnf_detach(dev_info_t *devinfo, ddi_detach_cmd_t cmd)
805 {
806 	xnf_t *xnfp;		/* Our private device info */
807 	int i;
808 
809 #ifdef XNF_DEBUG
810 	if (xnfdebug & XNF_DEBUG_DDI)
811 		printf("xnf_detach(0x%p)\n", (void *)devinfo);
812 #endif
813 
814 	xnfp = ddi_get_driver_private(devinfo);
815 
816 	switch (cmd) {
817 	case DDI_SUSPEND:
818 #ifdef XPV_HVM_DRIVER
819 		ec_unbind_evtchn(xnfp->xnf_evtchn);
820 		xvdi_free_evtchn(devinfo);
821 #else
822 		ddi_remove_intr(devinfo, 0, xnfp->xnf_icookie);
823 #endif
824 
825 		xvdi_suspend(devinfo);
826 
827 		mutex_enter(&xnfp->xnf_intrlock);
828 		mutex_enter(&xnfp->xnf_txlock);
829 
830 		xnfp->xnf_evtchn = INVALID_EVTCHN;
831 		xnfp->xnf_connected = B_FALSE;
832 		mutex_exit(&xnfp->xnf_txlock);
833 		mutex_exit(&xnfp->xnf_intrlock);
834 		return (DDI_SUCCESS);
835 
836 	case DDI_DETACH:
837 		break;
838 
839 	default:
840 		return (DDI_FAILURE);
841 	}
842 
843 	if (xnfp->xnf_connected)
844 		return (DDI_FAILURE);
845 
846 	/* Wait for receive buffers to be returned; give up after 5 seconds */
847 	i = 50;
848 
849 	mutex_enter(&xnfp->xnf_rx_buf_mutex);
850 	while (xnfp->xnf_rx_bufs_outstanding > 0) {
851 		mutex_exit(&xnfp->xnf_rx_buf_mutex);
852 		delay(drv_usectohz(100000));
853 		if (--i == 0) {
854 			cmn_err(CE_WARN,
855 			    "xnf%d: never reclaimed all the "
856 			    "receive buffers.  Still have %d "
857 			    "buffers outstanding.",
858 			    ddi_get_instance(xnfp->xnf_devinfo),
859 			    xnfp->xnf_rx_bufs_outstanding);
860 			return (DDI_FAILURE);
861 		}
862 		mutex_enter(&xnfp->xnf_rx_buf_mutex);
863 	}
864 	mutex_exit(&xnfp->xnf_rx_buf_mutex);
865 
866 	kstat_delete(xnfp->xnf_kstat_aux);
867 
868 	if (mac_unregister(xnfp->xnf_mh) != 0)
869 		return (DDI_FAILURE);
870 
871 	/* Stop the receiver */
872 	xnf_stop(xnfp);
873 
874 	xvdi_remove_event_handler(devinfo, XS_OE_STATE);
875 
876 	/* Remove the interrupt */
877 #ifdef XPV_HVM_DRIVER
878 	ec_unbind_evtchn(xnfp->xnf_evtchn);
879 	xvdi_free_evtchn(devinfo);
880 #else
881 	ddi_remove_intr(devinfo, 0, xnfp->xnf_icookie);
882 #endif
883 
884 	/* Release any pending xmit mblks */
885 	xnf_release_mblks(xnfp);
886 
887 	/* Release all DMA resources */
888 	xnf_release_dma_resources(xnfp);
889 
890 	cv_destroy(&xnfp->xnf_cv);
891 	mutex_destroy(&xnfp->xnf_rx_buf_mutex);
892 	mutex_destroy(&xnfp->xnf_txlock);
893 	mutex_destroy(&xnfp->xnf_intrlock);
894 
895 	kmem_free(xnfp, sizeof (*xnfp));
896 
897 	return (DDI_SUCCESS);
898 }
899 
900 /*
901  *  xnf_set_mac_addr() -- set the physical network address on the board.
902  */
903 /*ARGSUSED*/
904 static int
905 xnf_set_mac_addr(void *arg, const uint8_t *macaddr)
906 {
907 	xnf_t *xnfp = arg;
908 
909 #ifdef XNF_DEBUG
910 	if (xnfdebug & XNF_DEBUG_TRACE)
911 		printf("xnf%d: set_mac_addr(0x%p): "
912 		    "%02x:%02x:%02x:%02x:%02x:%02x\n",
913 		    ddi_get_instance(xnfp->xnf_devinfo),
914 		    (void *)xnfp, macaddr[0], macaddr[1], macaddr[2],
915 		    macaddr[3], macaddr[4], macaddr[5]);
916 #endif
917 	/*
918 	 * We can't set our macaddr.
919 	 *
920 	 * XXPV dme: Why not?
921 	 */
922 	return (ENOTSUP);
923 }
924 
925 /*
926  *  xnf_set_multicast() -- set (enable) or disable a multicast address.
927  *
928  *  Program the hardware to enable/disable the multicast address
929  *  in "mcast".  Enable if "add" is true, disable if false.
930  */
931 /*ARGSUSED*/
932 static int
933 xnf_set_multicast(void *arg, boolean_t add, const uint8_t *mca)
934 {
935 	xnf_t *xnfp = arg;
936 
937 #ifdef XNF_DEBUG
938 	if (xnfdebug & XNF_DEBUG_TRACE)
939 		printf("xnf%d set_multicast(0x%p): "
940 		    "%02x:%02x:%02x:%02x:%02x:%02x\n",
941 		    ddi_get_instance(xnfp->xnf_devinfo),
942 		    (void *)xnfp, mca[0], mca[1], mca[2],
943 		    mca[3], mca[4], mca[5]);
944 #endif
945 
946 	/*
947 	 * XXPV dme: Ideally we'd relay the address to the backend for
948 	 * enabling.  The protocol doesn't support that (interesting
949 	 * extension), so we simply succeed and hope that the relevant
950 	 * packets are going to arrive.
951 	 *
952 	 * If protocol support is added for enable/disable then we'll
953 	 * need to keep a list of those in use and re-add on resume.
954 	 */
955 	return (0);
956 }
957 
958 /*
959  * xnf_set_promiscuous() -- set or reset promiscuous mode on the board
960  *
961  *  Program the hardware to enable/disable promiscuous mode.
962  */
963 /*ARGSUSED*/
964 static int
965 xnf_set_promiscuous(void *arg, boolean_t on)
966 {
967 	xnf_t *xnfp = arg;
968 
969 #ifdef XNF_DEBUG
970 	if (xnfdebug & XNF_DEBUG_TRACE)
971 		printf("xnf%d set_promiscuous(0x%p, %x)\n",
972 		    ddi_get_instance(xnfp->xnf_devinfo),
973 		    (void *)xnfp, on);
974 #endif
975 	/*
976 	 * We can't really do this, but we pretend that we can in
977 	 * order that snoop will work.
978 	 */
979 	return (0);
980 }
981 
982 /*
983  * Clean buffers that we have responses for from the transmit ring.
984  */
985 static int
986 xnf_clean_tx_ring(xnf_t *xnfp)
987 {
988 	RING_IDX		next_resp, i;
989 	struct tx_pktinfo	*reap;
990 	int			id;
991 	grant_ref_t		ref;
992 
993 	ASSERT(MUTEX_HELD(&xnfp->xnf_txlock));
994 
995 	while (RING_HAS_UNCONSUMED_RESPONSES(&xnfp->xnf_tx_ring)) {
996 		/*
997 		 * index of next transmission ack
998 		 */
999 		next_resp = xnfp->xnf_tx_ring.sring->rsp_prod;
1000 		membar_consumer();
1001 		/*
1002 		 * Clean tx packets from ring that we have responses for
1003 		 */
1004 		for (i = xnfp->xnf_tx_ring.rsp_cons; i != next_resp; i++) {
1005 			id = RING_GET_RESPONSE(&xnfp->xnf_tx_ring, i)->id;
1006 			reap = &xnfp->xnf_tx_pkt_info[id];
1007 			ref = reap->grant_ref;
1008 			/*
1009 			 * Return id to free list
1010 			 */
1011 			reap->id = xnfp->xnf_tx_pkt_id_list;
1012 			xnfp->xnf_tx_pkt_id_list = id;
1013 			if (gnttab_query_foreign_access(ref) != 0)
1014 				panic("tx grant still in use "
1015 				    "by backend domain");
1016 			(void) ddi_dma_unbind_handle(reap->dma_handle);
1017 			(void) gnttab_end_foreign_access_ref(ref,
1018 			    xnfp->xnf_tx_pages_readonly);
1019 			gnttab_release_grant_reference(&xnfp->xnf_gref_tx_head,
1020 			    ref);
1021 			freemsg(reap->mp);
1022 			reap->mp = NULL;
1023 			reap->grant_ref = GRANT_INVALID_REF;
1024 			if (reap->bdesc != NULL)
1025 				xnf_free_tx_buffer(reap->bdesc);
1026 			reap->bdesc = NULL;
1027 		}
1028 		xnfp->xnf_tx_ring.rsp_cons = next_resp;
1029 		membar_enter();
1030 	}
1031 
1032 	return (RING_FREE_REQUESTS(&xnfp->xnf_tx_ring));
1033 }
1034 
1035 /*
1036  * If we need to pull up data from either a packet that crosses a page
1037  * boundary or consisting of multiple mblks, do it here.  We allocate
1038  * a page aligned buffer and copy the data into it.  The header for the
1039  * allocated buffer is returned. (which is also allocated here)
1040  */
1041 static struct xnf_buffer_desc *
1042 xnf_pullupmsg(xnf_t *xnfp, mblk_t *mp)
1043 {
1044 	struct xnf_buffer_desc	*bdesc;
1045 	mblk_t			*mptr;
1046 	caddr_t			bp;
1047 	int			len;
1048 
1049 	/*
1050 	 * get a xmit buffer from the xmit buffer pool
1051 	 */
1052 	mutex_enter(&xnfp->xnf_rx_buf_mutex);
1053 	bdesc = xnf_get_tx_buffer(xnfp);
1054 	mutex_exit(&xnfp->xnf_rx_buf_mutex);
1055 	if (bdesc == NULL)
1056 		return (bdesc);
1057 	/*
1058 	 * Copy the data into the buffer
1059 	 */
1060 	xnfp->xnf_stat_tx_pullup++;
1061 	bp = bdesc->buf;
1062 	for (mptr = mp; mptr != NULL; mptr = mptr->b_cont) {
1063 		len = mptr->b_wptr - mptr->b_rptr;
1064 		bcopy(mptr->b_rptr, bp, len);
1065 		bp += len;
1066 	}
1067 	return (bdesc);
1068 }
1069 
1070 /*
1071  *  xnf_send_one() -- send a packet
1072  *
1073  *  Called when a packet is ready to be transmitted. A pointer to an
1074  *  M_DATA message that contains the packet is passed to this routine.
1075  *  At least the complete LLC header is contained in the message's
1076  *  first message block, and the remainder of the packet is contained
1077  *  within additional M_DATA message blocks linked to the first
1078  *  message block.
1079  *
1080  */
1081 static boolean_t
1082 xnf_send_one(xnf_t *xnfp, mblk_t *mp)
1083 {
1084 	struct xnf_buffer_desc	*xmitbuf;
1085 	struct tx_pktinfo	*txp_info;
1086 	mblk_t			*mptr;
1087 	ddi_dma_cookie_t	dma_cookie;
1088 	RING_IDX		slot;
1089 	int			length = 0, i, pktlen = 0, rc, tx_id;
1090 	int			tx_ring_freespace, page_oops;
1091 	uint_t			ncookies;
1092 	volatile netif_tx_request_t	*txrp;
1093 	caddr_t			bufaddr;
1094 	grant_ref_t		ref;
1095 	unsigned long		mfn;
1096 	uint32_t		pflags;
1097 	domid_t			oeid;
1098 
1099 #ifdef XNF_DEBUG
1100 	if (xnfdebug & XNF_DEBUG_SEND)
1101 		printf("xnf%d send(0x%p, 0x%p)\n",
1102 		    ddi_get_instance(xnfp->xnf_devinfo),
1103 		    (void *)xnfp, (void *)mp);
1104 #endif
1105 
1106 	ASSERT(mp != NULL);
1107 	ASSERT(mp->b_next == NULL);
1108 	ASSERT(MUTEX_HELD(&xnfp->xnf_txlock));
1109 
1110 	tx_ring_freespace = xnf_clean_tx_ring(xnfp);
1111 	ASSERT(tx_ring_freespace >= 0);
1112 
1113 	oeid = xvdi_get_oeid(xnfp->xnf_devinfo);
1114 	xnfp->xnf_stat_tx_attempt++;
1115 	/*
1116 	 * If there are no xmit ring slots available, return.
1117 	 */
1118 	if (tx_ring_freespace == 0) {
1119 		xnfp->xnf_stat_tx_defer++;
1120 		return (B_FALSE);	/* Send should be retried */
1121 	}
1122 
1123 	slot = xnfp->xnf_tx_ring.req_prod_pvt;
1124 	/* Count the number of mblks in message and compute packet size */
1125 	for (i = 0, mptr = mp; mptr != NULL; mptr = mptr->b_cont, i++)
1126 		pktlen += (mptr->b_wptr - mptr->b_rptr);
1127 
1128 	/* Make sure packet isn't too large */
1129 	if (pktlen > XNF_FRAMESIZE) {
1130 		cmn_err(CE_WARN, "xnf%d: large packet %d bytes",
1131 		    ddi_get_instance(xnfp->xnf_devinfo), pktlen);
1132 		freemsg(mp);
1133 		return (B_FALSE);
1134 	}
1135 
1136 	/*
1137 	 * Test if we cross a page boundary with our buffer
1138 	 */
1139 	page_oops = (i == 1) &&
1140 	    (xnf_btop((size_t)mp->b_rptr) !=
1141 	    xnf_btop((size_t)(mp->b_rptr + pktlen)));
1142 	/*
1143 	 * XXPV - unfortunately, the Xen virtual net device currently
1144 	 * doesn't support multiple packet frags, so this will always
1145 	 * end up doing the pullup if we got more than one packet.
1146 	 */
1147 	if (i > xnf_max_tx_frags || page_oops) {
1148 		if (page_oops)
1149 			xnfp->xnf_stat_tx_pagebndry++;
1150 		if ((xmitbuf = xnf_pullupmsg(xnfp, mp)) == NULL) {
1151 			/* could not allocate resources? */
1152 #ifdef XNF_DEBUG
1153 			cmn_err(CE_WARN, "xnf%d: pullupmsg failed",
1154 			    ddi_get_instance(xnfp->xnf_devinfo));
1155 #endif
1156 			xnfp->xnf_stat_tx_defer++;
1157 			return (B_FALSE);	/* Retry send */
1158 		}
1159 		bufaddr = xmitbuf->buf;
1160 	} else {
1161 		xmitbuf = NULL;
1162 		bufaddr = (caddr_t)mp->b_rptr;
1163 	}
1164 
1165 	/* set up data descriptor */
1166 	length = pktlen;
1167 
1168 	/*
1169 	 * Get packet id from free list
1170 	 */
1171 	tx_id = xnfp->xnf_tx_pkt_id_list;
1172 	ASSERT(tx_id < NET_TX_RING_SIZE);
1173 	txp_info = &xnfp->xnf_tx_pkt_info[tx_id];
1174 	xnfp->xnf_tx_pkt_id_list = txp_info->id;
1175 	txp_info->id = tx_id;
1176 
1177 	/* Prepare for DMA mapping of tx buffer(s) */
1178 	rc = ddi_dma_addr_bind_handle(txp_info->dma_handle,
1179 	    NULL, bufaddr, length, DDI_DMA_WRITE | DDI_DMA_STREAMING,
1180 	    DDI_DMA_DONTWAIT, 0, &dma_cookie, &ncookies);
1181 	if (rc != DDI_DMA_MAPPED) {
1182 		ASSERT(rc != DDI_DMA_INUSE);
1183 		ASSERT(rc != DDI_DMA_PARTIAL_MAP);
1184 		/*
1185 		 *  Return id to free list
1186 		 */
1187 		txp_info->id = xnfp->xnf_tx_pkt_id_list;
1188 		xnfp->xnf_tx_pkt_id_list = tx_id;
1189 		if (rc == DDI_DMA_NORESOURCES) {
1190 			xnfp->xnf_stat_tx_defer++;
1191 			return (B_FALSE); /* Retry later */
1192 		}
1193 #ifdef XNF_DEBUG
1194 		cmn_err(CE_WARN, "xnf%d: bind_handle failed (%x)",
1195 		    ddi_get_instance(xnfp->xnf_devinfo), rc);
1196 #endif
1197 		return (B_FALSE);
1198 	}
1199 
1200 	ASSERT(ncookies == 1);
1201 	ref = gnttab_claim_grant_reference(&xnfp->xnf_gref_tx_head);
1202 	ASSERT((signed short)ref >= 0);
1203 	mfn = xnf_btop(pa_to_ma((paddr_t)dma_cookie.dmac_laddress));
1204 	gnttab_grant_foreign_access_ref(ref, oeid, mfn,
1205 	    xnfp->xnf_tx_pages_readonly);
1206 	txp_info->grant_ref = ref;
1207 	txrp = RING_GET_REQUEST(&xnfp->xnf_tx_ring, slot);
1208 	txrp->gref = ref;
1209 	txrp->size = dma_cookie.dmac_size;
1210 	txrp->offset = (uintptr_t)bufaddr & PAGEOFFSET;
1211 	txrp->id = tx_id;
1212 	txrp->flags = 0;
1213 	hcksum_retrieve(mp, NULL, NULL, NULL, NULL, NULL, NULL, &pflags);
1214 	if (pflags != 0) {
1215 		ASSERT(xnfp->xnf_cksum_offload);
1216 		/*
1217 		 * If the local protocol stack requests checksum
1218 		 * offload we set the 'checksum blank' flag,
1219 		 * indicating to the peer that we need the checksum
1220 		 * calculated for us.
1221 		 *
1222 		 * We _don't_ set the validated flag, because we haven't
1223 		 * validated that the data and the checksum match.
1224 		 */
1225 		txrp->flags |= NETTXF_csum_blank;
1226 		xnfp->xnf_stat_tx_cksum_deferred++;
1227 	}
1228 	membar_producer();
1229 	xnfp->xnf_tx_ring.req_prod_pvt = slot + 1;
1230 
1231 	txp_info->mp = mp;
1232 	txp_info->bdesc = xmitbuf;
1233 
1234 	xnfp->xnf_stat_opackets++;
1235 	xnfp->xnf_stat_obytes += pktlen;
1236 
1237 	return (B_TRUE);	/* successful transmit attempt */
1238 }
1239 
1240 mblk_t *
1241 xnf_send(void *arg, mblk_t *mp)
1242 {
1243 	xnf_t *xnfp = arg;
1244 	mblk_t *next;
1245 	boolean_t sent_something = B_FALSE;
1246 
1247 	mutex_enter(&xnfp->xnf_txlock);
1248 
1249 	/*
1250 	 * Transmission attempts should be impossible without having
1251 	 * previously called xnf_start().
1252 	 */
1253 	ASSERT(xnfp->xnf_running);
1254 
1255 	/*
1256 	 * Wait for getting connected to the backend
1257 	 */
1258 	while (!xnfp->xnf_connected) {
1259 		cv_wait(&xnfp->xnf_cv, &xnfp->xnf_txlock);
1260 	}
1261 
1262 	while (mp != NULL) {
1263 		next = mp->b_next;
1264 		mp->b_next = NULL;
1265 
1266 		if (!xnf_send_one(xnfp, mp)) {
1267 			mp->b_next = next;
1268 			break;
1269 		}
1270 
1271 		mp = next;
1272 		sent_something = B_TRUE;
1273 	}
1274 
1275 	if (sent_something) {
1276 		boolean_t notify;
1277 
1278 		/* LINTED: constant in conditional context */
1279 		RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xnfp->xnf_tx_ring,
1280 		    notify);
1281 		if (notify)
1282 			ec_notify_via_evtchn(xnfp->xnf_evtchn);
1283 	}
1284 
1285 	mutex_exit(&xnfp->xnf_txlock);
1286 
1287 	return (mp);
1288 }
1289 
1290 /*
1291  *  xnf_intr() -- ring interrupt service routine
1292  */
1293 static uint_t
1294 xnf_intr(caddr_t arg)
1295 {
1296 	xnf_t *xnfp = (xnf_t *)arg;
1297 	int tx_ring_space;
1298 
1299 	mutex_enter(&xnfp->xnf_intrlock);
1300 
1301 	/*
1302 	 * If not connected to the peer or not started by the upper
1303 	 * layers we cannot usefully handle interrupts.
1304 	 */
1305 	if (!(xnfp->xnf_connected && xnfp->xnf_running)) {
1306 		mutex_exit(&xnfp->xnf_intrlock);
1307 		xnfp->xnf_stat_unclaimed_interrupts++;
1308 		return (DDI_INTR_UNCLAIMED);
1309 	}
1310 
1311 #ifdef XNF_DEBUG
1312 	if (xnfdebug & XNF_DEBUG_INT)
1313 		printf("xnf%d intr(0x%p)\n",
1314 		    ddi_get_instance(xnfp->xnf_devinfo), (void *)xnfp);
1315 #endif
1316 	if (RING_HAS_UNCONSUMED_RESPONSES(&xnfp->xnf_rx_ring)) {
1317 		mblk_t *mp;
1318 
1319 		if (xnfp->xnf_rx_hvcopy)
1320 			mp = xnf_process_hvcopy_recv(xnfp);
1321 		else
1322 			mp = xnf_process_recv(xnfp);
1323 
1324 		if (mp != NULL)
1325 			mac_rx(xnfp->xnf_mh, xnfp->xnf_rx_handle, mp);
1326 	}
1327 
1328 	/*
1329 	 * Clean tx ring and try to start any blocked xmit streams if
1330 	 * there is now some space.
1331 	 */
1332 	mutex_enter(&xnfp->xnf_txlock);
1333 	tx_ring_space = xnf_clean_tx_ring(xnfp);
1334 	mutex_exit(&xnfp->xnf_txlock);
1335 	if (tx_ring_space > XNF_TX_FREE_THRESH) {
1336 		mutex_exit(&xnfp->xnf_intrlock);
1337 		mac_tx_update(xnfp->xnf_mh);
1338 		mutex_enter(&xnfp->xnf_intrlock);
1339 	}
1340 
1341 	xnfp->xnf_stat_interrupts++;
1342 	mutex_exit(&xnfp->xnf_intrlock);
1343 	return (DDI_INTR_CLAIMED); /* indicate that the interrupt was for us */
1344 }
1345 
1346 /*
1347  *  xnf_start() -- start the board receiving and enable interrupts.
1348  */
1349 static int
1350 xnf_start(void *arg)
1351 {
1352 	xnf_t *xnfp = arg;
1353 
1354 #ifdef XNF_DEBUG
1355 	if (xnfdebug & XNF_DEBUG_TRACE)
1356 		printf("xnf%d start(0x%p)\n",
1357 		    ddi_get_instance(xnfp->xnf_devinfo), (void *)xnfp);
1358 #endif
1359 
1360 	mutex_enter(&xnfp->xnf_intrlock);
1361 	mutex_enter(&xnfp->xnf_txlock);
1362 
1363 	/* Accept packets from above. */
1364 	xnfp->xnf_running = B_TRUE;
1365 
1366 	mutex_exit(&xnfp->xnf_txlock);
1367 	mutex_exit(&xnfp->xnf_intrlock);
1368 
1369 	return (0);
1370 }
1371 
1372 /* xnf_stop() - disable hardware */
1373 static void
1374 xnf_stop(void *arg)
1375 {
1376 	xnf_t *xnfp = arg;
1377 
1378 #ifdef XNF_DEBUG
1379 	if (xnfdebug & XNF_DEBUG_TRACE)
1380 		printf("xnf%d stop(0x%p)\n",
1381 		    ddi_get_instance(xnfp->xnf_devinfo), (void *)xnfp);
1382 #endif
1383 
1384 	mutex_enter(&xnfp->xnf_intrlock);
1385 	mutex_enter(&xnfp->xnf_txlock);
1386 
1387 	xnfp->xnf_running = B_FALSE;
1388 
1389 	mutex_exit(&xnfp->xnf_txlock);
1390 	mutex_exit(&xnfp->xnf_intrlock);
1391 }
1392 
1393 /*
1394  * Driver private functions follow
1395  */
1396 
1397 /*
1398  * Hang buffer on rx ring
1399  */
1400 static void
1401 rx_buffer_hang(xnf_t *xnfp, struct xnf_buffer_desc *bdesc)
1402 {
1403 	volatile netif_rx_request_t	*reqp;
1404 	RING_IDX			hang_ix;
1405 	grant_ref_t			ref;
1406 	domid_t				oeid;
1407 
1408 	oeid = xvdi_get_oeid(xnfp->xnf_devinfo);
1409 
1410 	ASSERT(MUTEX_HELD(&xnfp->xnf_intrlock));
1411 	reqp = RING_GET_REQUEST(&xnfp->xnf_rx_ring,
1412 	    xnfp->xnf_rx_ring.req_prod_pvt);
1413 	hang_ix = (RING_IDX) (reqp - RING_GET_REQUEST(&xnfp->xnf_rx_ring, 0));
1414 	ASSERT(xnfp->xnf_rxpkt_bufptr[hang_ix] == NULL);
1415 	if (bdesc->grant_ref == GRANT_INVALID_REF) {
1416 		ref = gnttab_claim_grant_reference(&xnfp->xnf_gref_rx_head);
1417 		ASSERT((signed short)ref >= 0);
1418 		bdesc->grant_ref = ref;
1419 		if (xnfp->xnf_rx_hvcopy) {
1420 			pfn_t pfn = xnf_btop(bdesc->buf_phys);
1421 			mfn_t mfn = pfn_to_mfn(pfn);
1422 
1423 			gnttab_grant_foreign_access_ref(ref, oeid, mfn, 0);
1424 		} else {
1425 			gnttab_grant_foreign_transfer_ref(ref, oeid, 0);
1426 		}
1427 	}
1428 	reqp->id = hang_ix;
1429 	reqp->gref = bdesc->grant_ref;
1430 	bdesc->id = hang_ix;
1431 	xnfp->xnf_rxpkt_bufptr[hang_ix] = bdesc;
1432 	membar_producer();
1433 	xnfp->xnf_rx_ring.req_prod_pvt++;
1434 }
1435 
1436 static mblk_t *
1437 xnf_process_hvcopy_recv(xnf_t *xnfp)
1438 {
1439 	netif_rx_response_t *rxpkt;
1440 	mblk_t		*mp, *head, *tail;
1441 	struct		xnf_buffer_desc *bdesc;
1442 	boolean_t	hwcsum = B_FALSE, notify, work_to_do;
1443 	size_t 		len;
1444 
1445 	/*
1446 	 * in loop over unconsumed responses, we do:
1447 	 * 1. get a response
1448 	 * 2. take corresponding buffer off recv. ring
1449 	 * 3. indicate this by setting slot to NULL
1450 	 * 4. create a new message and
1451 	 * 5. copy data in, adjust ptr
1452 	 *
1453 	 * outside loop:
1454 	 * 7. make sure no more data has arrived; kick HV
1455 	 */
1456 
1457 	head = tail = NULL;
1458 
1459 loop:
1460 	while (RING_HAS_UNCONSUMED_RESPONSES(&xnfp->xnf_rx_ring)) {
1461 
1462 		/* 1. */
1463 		rxpkt = RING_GET_RESPONSE(&xnfp->xnf_rx_ring,
1464 		    xnfp->xnf_rx_ring.rsp_cons);
1465 
1466 		DTRACE_PROBE4(got_PKT, int, (int)rxpkt->id, int,
1467 		    (int)rxpkt->offset,
1468 		    int, (int)rxpkt->flags, int, (int)rxpkt->status);
1469 
1470 		/*
1471 		 * 2.
1472 		 * Take buffer off of receive ring
1473 		 */
1474 		hwcsum = B_FALSE;
1475 		bdesc = xnfp->xnf_rxpkt_bufptr[rxpkt->id];
1476 		/* 3 */
1477 		xnfp->xnf_rxpkt_bufptr[rxpkt->id] = NULL;
1478 		ASSERT(bdesc->id == rxpkt->id);
1479 		if (rxpkt->status <= 0) {
1480 			DTRACE_PROBE4(pkt_status_negative, int, rxpkt->status,
1481 			    char *, bdesc->buf, int, rxpkt->offset,
1482 			    char *, ((char *)bdesc->buf) + rxpkt->offset);
1483 			mp = NULL;
1484 			xnfp->xnf_stat_errrx++;
1485 			if (rxpkt->status == 0)
1486 				xnfp->xnf_stat_runt++;
1487 			if (rxpkt->status == NETIF_RSP_ERROR)
1488 				xnfp->xnf_stat_mac_rcv_error++;
1489 			if (rxpkt->status == NETIF_RSP_DROPPED)
1490 				xnfp->xnf_stat_norxbuf++;
1491 			/*
1492 			 * re-hang the buffer
1493 			 */
1494 			rx_buffer_hang(xnfp, bdesc);
1495 		} else {
1496 			grant_ref_t		ref =  bdesc->grant_ref;
1497 			struct xnf_buffer_desc	*new_bdesc;
1498 			unsigned long		off = rxpkt->offset;
1499 
1500 			DTRACE_PROBE4(pkt_status_ok, int, rxpkt->status,
1501 			    char *, bdesc->buf, int, rxpkt->offset,
1502 			    char *, ((char *)bdesc->buf) + rxpkt->offset);
1503 			len = rxpkt->status;
1504 			ASSERT(off + len <= PAGEOFFSET);
1505 			if (ref == GRANT_INVALID_REF) {
1506 				mp = NULL;
1507 				new_bdesc = bdesc;
1508 				cmn_err(CE_WARN, "Bad rx grant reference %d "
1509 				    "from dom %d", ref,
1510 				    xvdi_get_oeid(xnfp->xnf_devinfo));
1511 				goto luckless;
1512 			}
1513 			/*
1514 			 * Release ref which we'll be re-claiming in
1515 			 * rx_buffer_hang().
1516 			 */
1517 			bdesc->grant_ref = GRANT_INVALID_REF;
1518 			(void) gnttab_end_foreign_access_ref(ref, 0);
1519 			gnttab_release_grant_reference(&xnfp->xnf_gref_rx_head,
1520 			    ref);
1521 			if (rxpkt->flags & NETRXF_data_validated)
1522 				hwcsum = B_TRUE;
1523 
1524 			/*
1525 			 * XXPV for the initial implementation of HVcopy,
1526 			 * create a new msg and copy in the data
1527 			 */
1528 			/* 4. */
1529 			if ((mp = allocb(len, BPRI_MED)) == NULL) {
1530 				/*
1531 				 * Couldn't get buffer to copy to,
1532 				 * drop this data, and re-hang
1533 				 * the buffer on the ring.
1534 				 */
1535 				xnfp->xnf_stat_norxbuf++;
1536 				DTRACE_PROBE(alloc_nix);
1537 			} else {
1538 				/* 5. */
1539 				DTRACE_PROBE(alloc_ok);
1540 				bcopy(bdesc->buf + off, mp->b_wptr,
1541 				    len);
1542 				mp->b_wptr += len;
1543 			}
1544 			new_bdesc = bdesc;
1545 luckless:
1546 
1547 			/* Re-hang old or hang new buffer. */
1548 			rx_buffer_hang(xnfp, new_bdesc);
1549 		}
1550 		if (mp) {
1551 			if (hwcsum) {
1552 				/*
1553 				 * See comments in xnf_process_recv().
1554 				 */
1555 
1556 				(void) hcksum_assoc(mp, NULL,
1557 				    NULL, 0, 0, 0, 0,
1558 				    HCK_FULLCKSUM |
1559 				    HCK_FULLCKSUM_OK,
1560 				    0);
1561 				xnfp->xnf_stat_rx_cksum_no_need++;
1562 			}
1563 			if (head == NULL) {
1564 				head = tail = mp;
1565 			} else {
1566 				tail->b_next = mp;
1567 				tail = mp;
1568 			}
1569 
1570 			ASSERT(mp->b_next == NULL);
1571 
1572 			xnfp->xnf_stat_ipackets++;
1573 			xnfp->xnf_stat_rbytes += len;
1574 		}
1575 
1576 		xnfp->xnf_rx_ring.rsp_cons++;
1577 
1578 		xnfp->xnf_stat_hvcopy_packet_processed++;
1579 	}
1580 
1581 	/* 7. */
1582 	/*
1583 	 * Has more data come in since we started?
1584 	 */
1585 	/* LINTED: constant in conditional context */
1586 	RING_FINAL_CHECK_FOR_RESPONSES(&xnfp->xnf_rx_ring, work_to_do);
1587 	if (work_to_do)
1588 		goto loop;
1589 
1590 	/*
1591 	 * Indicate to the backend that we have re-filled the receive
1592 	 * ring.
1593 	 */
1594 	/* LINTED: constant in conditional context */
1595 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xnfp->xnf_rx_ring, notify);
1596 	if (notify)
1597 		ec_notify_via_evtchn(xnfp->xnf_evtchn);
1598 
1599 	return (head);
1600 }
1601 
1602 /* Process all queued received packets */
1603 static mblk_t *
1604 xnf_process_recv(xnf_t *xnfp)
1605 {
1606 	volatile netif_rx_response_t *rxpkt;
1607 	mblk_t *mp, *head, *tail;
1608 	struct xnf_buffer_desc *bdesc;
1609 	extern mblk_t *desballoc(unsigned char *, size_t, uint_t, frtn_t *);
1610 	boolean_t hwcsum = B_FALSE, notify, work_to_do;
1611 	size_t len;
1612 	pfn_t pfn;
1613 	long cnt;
1614 
1615 	head = tail = NULL;
1616 loop:
1617 	while (RING_HAS_UNCONSUMED_RESPONSES(&xnfp->xnf_rx_ring)) {
1618 
1619 		rxpkt = RING_GET_RESPONSE(&xnfp->xnf_rx_ring,
1620 		    xnfp->xnf_rx_ring.rsp_cons);
1621 
1622 		/*
1623 		 * Take buffer off of receive ring
1624 		 */
1625 		hwcsum = B_FALSE;
1626 		bdesc = xnfp->xnf_rxpkt_bufptr[rxpkt->id];
1627 		xnfp->xnf_rxpkt_bufptr[rxpkt->id] = NULL;
1628 		ASSERT(bdesc->id == rxpkt->id);
1629 		if (rxpkt->status <= 0) {
1630 			mp = NULL;
1631 			xnfp->xnf_stat_errrx++;
1632 			if (rxpkt->status == 0)
1633 				xnfp->xnf_stat_runt++;
1634 			if (rxpkt->status == NETIF_RSP_ERROR)
1635 				xnfp->xnf_stat_mac_rcv_error++;
1636 			if (rxpkt->status == NETIF_RSP_DROPPED)
1637 				xnfp->xnf_stat_norxbuf++;
1638 			/*
1639 			 * re-hang the buffer
1640 			 */
1641 			rx_buffer_hang(xnfp, bdesc);
1642 		} else {
1643 			grant_ref_t ref =  bdesc->grant_ref;
1644 			struct xnf_buffer_desc *new_bdesc;
1645 			unsigned long off = rxpkt->offset;
1646 			unsigned long mfn;
1647 
1648 			len = rxpkt->status;
1649 			ASSERT(off + len <= PAGEOFFSET);
1650 			if (ref == GRANT_INVALID_REF) {
1651 				mp = NULL;
1652 				new_bdesc = bdesc;
1653 				cmn_err(CE_WARN, "Bad rx grant reference %d "
1654 				    "from dom %d", ref,
1655 				    xvdi_get_oeid(xnfp->xnf_devinfo));
1656 				goto luckless;
1657 			}
1658 			bdesc->grant_ref = GRANT_INVALID_REF;
1659 			mfn = gnttab_end_foreign_transfer_ref(ref);
1660 			ASSERT(mfn != MFN_INVALID);
1661 			ASSERT(hat_getpfnum(kas.a_hat, bdesc->buf) ==
1662 			    PFN_INVALID);
1663 
1664 			gnttab_release_grant_reference(&xnfp->xnf_gref_rx_head,
1665 			    ref);
1666 			reassign_pfn(xnf_btop(bdesc->buf_phys), mfn);
1667 			hat_devload(kas.a_hat, bdesc->buf, PAGESIZE,
1668 			    xnf_btop(bdesc->buf_phys),
1669 			    PROT_READ | PROT_WRITE, HAT_LOAD);
1670 			balloon_drv_added(1);
1671 
1672 			if (rxpkt->flags & NETRXF_data_validated)
1673 				hwcsum = B_TRUE;
1674 			if (len <= xnf_rx_bcopy_thresh) {
1675 				/*
1676 				 * For small buffers, just copy the data
1677 				 * and send the copy upstream.
1678 				 */
1679 				new_bdesc = NULL;
1680 			} else {
1681 				/*
1682 				 * We send a pointer to this data upstream;
1683 				 * we need a new buffer to replace this one.
1684 				 */
1685 				mutex_enter(&xnfp->xnf_rx_buf_mutex);
1686 				new_bdesc = xnf_get_buffer(xnfp);
1687 				if (new_bdesc != NULL) {
1688 					xnfp->xnf_rx_bufs_outstanding++;
1689 				} else {
1690 					xnfp->xnf_stat_rx_no_ringbuf++;
1691 				}
1692 				mutex_exit(&xnfp->xnf_rx_buf_mutex);
1693 			}
1694 
1695 			if (new_bdesc == NULL) {
1696 				/*
1697 				 * Don't have a new ring buffer; bcopy the data
1698 				 * from the buffer, and preserve the
1699 				 * original buffer
1700 				 */
1701 				if ((mp = allocb(len, BPRI_MED)) == NULL) {
1702 					/*
1703 					 * Could't get buffer to copy to,
1704 					 * drop this data, and re-hang
1705 					 * the buffer on the ring.
1706 					 */
1707 					xnfp->xnf_stat_norxbuf++;
1708 				} else {
1709 					bcopy(bdesc->buf + off, mp->b_wptr,
1710 					    len);
1711 				}
1712 				/*
1713 				 * Give the buffer page back to xen
1714 				 */
1715 				pfn = xnf_btop(bdesc->buf_phys);
1716 				cnt = balloon_free_pages(1, &mfn, bdesc->buf,
1717 				    &pfn);
1718 				if (cnt != 1) {
1719 					cmn_err(CE_WARN, "unable to give a "
1720 					    "page back to the hypervisor\n");
1721 				}
1722 				new_bdesc = bdesc;
1723 			} else {
1724 				if ((mp = desballoc((unsigned char *)bdesc->buf,
1725 				    off + len, 0, (frtn_t *)bdesc)) == NULL) {
1726 					/*
1727 					 * Couldn't get mblk to pass recv data
1728 					 * up with, free the old ring buffer
1729 					 */
1730 					xnfp->xnf_stat_norxbuf++;
1731 					xnf_rcv_complete(bdesc);
1732 					goto luckless;
1733 				}
1734 				(void) ddi_dma_sync(bdesc->dma_handle,
1735 				    0, 0, DDI_DMA_SYNC_FORCPU);
1736 
1737 				mp->b_wptr += off;
1738 				mp->b_rptr += off;
1739 			}
1740 luckless:
1741 			if (mp)
1742 				mp->b_wptr += len;
1743 			/* re-hang old or hang new buffer */
1744 			rx_buffer_hang(xnfp, new_bdesc);
1745 		}
1746 		if (mp) {
1747 			if (hwcsum) {
1748 				/*
1749 				 * If the peer says that the data has
1750 				 * been validated then we declare that
1751 				 * the full checksum has been
1752 				 * verified.
1753 				 *
1754 				 * We don't look at the "checksum
1755 				 * blank" flag, and hence could have a
1756 				 * packet here that we are asserting
1757 				 * is good with a blank checksum.
1758 				 *
1759 				 * The hardware checksum offload
1760 				 * specification says that we must
1761 				 * provide the actual checksum as well
1762 				 * as an assertion that it is valid,
1763 				 * but the protocol stack doesn't
1764 				 * actually use it and some other
1765 				 * drivers don't bother, so we don't.
1766 				 * If it was necessary we could grovel
1767 				 * in the packet to find it.
1768 				 */
1769 
1770 				(void) hcksum_assoc(mp, NULL,
1771 				    NULL, 0, 0, 0, 0,
1772 				    HCK_FULLCKSUM |
1773 				    HCK_FULLCKSUM_OK,
1774 				    0);
1775 				xnfp->xnf_stat_rx_cksum_no_need++;
1776 			}
1777 			if (head == NULL) {
1778 				head = tail = mp;
1779 			} else {
1780 				tail->b_next = mp;
1781 				tail = mp;
1782 			}
1783 
1784 			ASSERT(mp->b_next == NULL);
1785 
1786 			xnfp->xnf_stat_ipackets++;
1787 			xnfp->xnf_stat_rbytes += len;
1788 		}
1789 
1790 		xnfp->xnf_rx_ring.rsp_cons++;
1791 	}
1792 
1793 	/*
1794 	 * Has more data come in since we started?
1795 	 */
1796 	/* LINTED: constant in conditional context */
1797 	RING_FINAL_CHECK_FOR_RESPONSES(&xnfp->xnf_rx_ring, work_to_do);
1798 	if (work_to_do)
1799 		goto loop;
1800 
1801 	/*
1802 	 * Indicate to the backend that we have re-filled the receive
1803 	 * ring.
1804 	 */
1805 	/* LINTED: constant in conditional context */
1806 	RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&xnfp->xnf_rx_ring, notify);
1807 	if (notify)
1808 		ec_notify_via_evtchn(xnfp->xnf_evtchn);
1809 
1810 	return (head);
1811 }
1812 
1813 /* Called when the upper layers free a message we passed upstream */
1814 static void
1815 xnf_rcv_complete(struct xnf_buffer_desc *bdesc)
1816 {
1817 	xnf_t *xnfp = bdesc->xnfp;
1818 	pfn_t pfn;
1819 	long cnt;
1820 
1821 	/* One less outstanding receive buffer */
1822 	mutex_enter(&xnfp->xnf_rx_buf_mutex);
1823 	--xnfp->xnf_rx_bufs_outstanding;
1824 	/*
1825 	 * Return buffer to the free list, unless the free list is getting
1826 	 * too large.  XXPV - this threshold may need tuning.
1827 	 */
1828 	if (xnfp->xnf_rx_descs_free < xnf_rx_bufs_lowat) {
1829 		/*
1830 		 * Unmap the page, and hand the machine page back
1831 		 * to xen so it can be re-used as a backend net buffer.
1832 		 */
1833 		pfn = xnf_btop(bdesc->buf_phys);
1834 		cnt = balloon_free_pages(1, NULL, bdesc->buf, &pfn);
1835 		if (cnt != 1) {
1836 			cmn_err(CE_WARN, "unable to give a page back to the "
1837 			    "hypervisor\n");
1838 		}
1839 
1840 		bdesc->next = xnfp->xnf_free_list;
1841 		xnfp->xnf_free_list = bdesc;
1842 		xnfp->xnf_rx_descs_free++;
1843 		mutex_exit(&xnfp->xnf_rx_buf_mutex);
1844 	} else {
1845 		/*
1846 		 * We can return everything here since we have a free buffer
1847 		 * that we have not given the backing page for back to xen.
1848 		 */
1849 		--xnfp->xnf_rx_buffer_count;
1850 		mutex_exit(&xnfp->xnf_rx_buf_mutex);
1851 		(void) ddi_dma_unbind_handle(bdesc->dma_handle);
1852 		ddi_dma_mem_free(&bdesc->acc_handle);
1853 		ddi_dma_free_handle(&bdesc->dma_handle);
1854 		kmem_free(bdesc, sizeof (*bdesc));
1855 	}
1856 }
1857 
1858 /*
1859  *  xnf_alloc_dma_resources() -- initialize the drivers structures
1860  */
1861 static int
1862 xnf_alloc_dma_resources(xnf_t *xnfp)
1863 {
1864 	dev_info_t 		*devinfo = xnfp->xnf_devinfo;
1865 	int			i;
1866 	size_t			len;
1867 	ddi_dma_cookie_t	dma_cookie;
1868 	uint_t			ncookies;
1869 	struct xnf_buffer_desc	*bdesc;
1870 	int			rc;
1871 	caddr_t			rptr;
1872 
1873 	xnfp->xnf_n_rx = NET_RX_RING_SIZE;
1874 	xnfp->xnf_max_rx_bufs = xnf_rx_bufs_hiwat;
1875 
1876 	xnfp->xnf_n_tx = NET_TX_RING_SIZE;
1877 
1878 	/*
1879 	 * The code below allocates all the DMA data structures that
1880 	 * need to be released when the driver is detached.
1881 	 *
1882 	 * First allocate handles for mapping (virtual address) pointers to
1883 	 * transmit data buffers to physical addresses
1884 	 */
1885 	for (i = 0; i < xnfp->xnf_n_tx; i++) {
1886 		if ((rc = ddi_dma_alloc_handle(devinfo,
1887 		    &tx_buffer_dma_attr, DDI_DMA_SLEEP, 0,
1888 		    &xnfp->xnf_tx_pkt_info[i].dma_handle)) != DDI_SUCCESS)
1889 			return (DDI_FAILURE);
1890 	}
1891 
1892 	/*
1893 	 * Allocate page for the transmit descriptor ring.
1894 	 */
1895 	if (ddi_dma_alloc_handle(devinfo, &ringbuf_dma_attr,
1896 	    DDI_DMA_SLEEP, 0, &xnfp->xnf_tx_ring_dma_handle) != DDI_SUCCESS)
1897 		goto alloc_error;
1898 
1899 	if (ddi_dma_mem_alloc(xnfp->xnf_tx_ring_dma_handle,
1900 	    PAGESIZE, &accattr, DDI_DMA_CONSISTENT,
1901 	    DDI_DMA_SLEEP, 0, &rptr, &len,
1902 	    &xnfp->xnf_tx_ring_dma_acchandle) != DDI_SUCCESS) {
1903 		ddi_dma_free_handle(&xnfp->xnf_tx_ring_dma_handle);
1904 		xnfp->xnf_tx_ring_dma_handle = NULL;
1905 		goto alloc_error;
1906 	}
1907 
1908 	if ((rc = ddi_dma_addr_bind_handle(xnfp->xnf_tx_ring_dma_handle, NULL,
1909 	    rptr, PAGESIZE, DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
1910 	    DDI_DMA_SLEEP, 0, &dma_cookie, &ncookies)) != DDI_DMA_MAPPED) {
1911 		ddi_dma_mem_free(&xnfp->xnf_tx_ring_dma_acchandle);
1912 		ddi_dma_free_handle(&xnfp->xnf_tx_ring_dma_handle);
1913 		xnfp->xnf_tx_ring_dma_handle = NULL;
1914 		xnfp->xnf_tx_ring_dma_acchandle = NULL;
1915 		if (rc == DDI_DMA_NORESOURCES)
1916 			goto alloc_error;
1917 		else
1918 			goto error;
1919 	}
1920 
1921 	ASSERT(ncookies == 1);
1922 	bzero(rptr, PAGESIZE);
1923 	/* LINTED: constant in conditional context */
1924 	SHARED_RING_INIT((netif_tx_sring_t *)rptr);
1925 	/* LINTED: constant in conditional context */
1926 	FRONT_RING_INIT(&xnfp->xnf_tx_ring, (netif_tx_sring_t *)rptr, PAGESIZE);
1927 	xnfp->xnf_tx_ring_phys_addr = dma_cookie.dmac_laddress;
1928 
1929 	/*
1930 	 * Allocate page for the receive descriptor ring.
1931 	 */
1932 	if (ddi_dma_alloc_handle(devinfo, &ringbuf_dma_attr,
1933 	    DDI_DMA_SLEEP, 0, &xnfp->xnf_rx_ring_dma_handle) != DDI_SUCCESS)
1934 		goto alloc_error;
1935 
1936 	if (ddi_dma_mem_alloc(xnfp->xnf_rx_ring_dma_handle,
1937 	    PAGESIZE, &accattr, DDI_DMA_CONSISTENT,
1938 	    DDI_DMA_SLEEP, 0, &rptr, &len,
1939 	    &xnfp->xnf_rx_ring_dma_acchandle) != DDI_SUCCESS) {
1940 		ddi_dma_free_handle(&xnfp->xnf_rx_ring_dma_handle);
1941 		xnfp->xnf_rx_ring_dma_handle = NULL;
1942 		goto alloc_error;
1943 	}
1944 
1945 	if ((rc = ddi_dma_addr_bind_handle(xnfp->xnf_rx_ring_dma_handle, NULL,
1946 	    rptr, PAGESIZE, DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
1947 	    DDI_DMA_SLEEP, 0, &dma_cookie, &ncookies)) != DDI_DMA_MAPPED) {
1948 		ddi_dma_mem_free(&xnfp->xnf_rx_ring_dma_acchandle);
1949 		ddi_dma_free_handle(&xnfp->xnf_rx_ring_dma_handle);
1950 		xnfp->xnf_rx_ring_dma_handle = NULL;
1951 		xnfp->xnf_rx_ring_dma_acchandle = NULL;
1952 		if (rc == DDI_DMA_NORESOURCES)
1953 			goto alloc_error;
1954 		else
1955 			goto error;
1956 	}
1957 
1958 	ASSERT(ncookies == 1);
1959 	bzero(rptr, PAGESIZE);
1960 	/* LINTED: constant in conditional context */
1961 	SHARED_RING_INIT((netif_rx_sring_t *)rptr);
1962 	/* LINTED: constant in conditional context */
1963 	FRONT_RING_INIT(&xnfp->xnf_rx_ring, (netif_rx_sring_t *)rptr, PAGESIZE);
1964 	xnfp->xnf_rx_ring_phys_addr = dma_cookie.dmac_laddress;
1965 
1966 	/*
1967 	 * Preallocate receive buffers for each receive descriptor.
1968 	 */
1969 
1970 	/* Set up the "free list" of receive buffer descriptors */
1971 	for (i = 0; i < xnfp->xnf_n_rx; i++) {
1972 		if ((bdesc = xnf_alloc_buffer(xnfp)) == NULL)
1973 			goto alloc_error;
1974 		bdesc->next = xnfp->xnf_free_list;
1975 		xnfp->xnf_free_list = bdesc;
1976 	}
1977 
1978 	return (DDI_SUCCESS);
1979 
1980 alloc_error:
1981 	cmn_err(CE_WARN, "xnf%d: could not allocate enough DMA memory",
1982 	    ddi_get_instance(xnfp->xnf_devinfo));
1983 error:
1984 	xnf_release_dma_resources(xnfp);
1985 	return (DDI_FAILURE);
1986 }
1987 
1988 /*
1989  * Release all DMA resources in the opposite order from acquisition
1990  * Should not be called until all outstanding esballoc buffers
1991  * have been returned.
1992  */
1993 static void
1994 xnf_release_dma_resources(xnf_t *xnfp)
1995 {
1996 	int i;
1997 
1998 	/*
1999 	 * Free receive buffers which are currently associated with
2000 	 * descriptors
2001 	 */
2002 	for (i = 0; i < xnfp->xnf_n_rx; i++) {
2003 		struct xnf_buffer_desc *bp;
2004 
2005 		if ((bp = xnfp->xnf_rxpkt_bufptr[i]) == NULL)
2006 			continue;
2007 		xnf_free_buffer(bp);
2008 		xnfp->xnf_rxpkt_bufptr[i] = NULL;
2009 	}
2010 
2011 	/* Free the receive ring buffer */
2012 	if (xnfp->xnf_rx_ring_dma_acchandle != NULL) {
2013 		(void) ddi_dma_unbind_handle(xnfp->xnf_rx_ring_dma_handle);
2014 		ddi_dma_mem_free(&xnfp->xnf_rx_ring_dma_acchandle);
2015 		ddi_dma_free_handle(&xnfp->xnf_rx_ring_dma_handle);
2016 		xnfp->xnf_rx_ring_dma_acchandle = NULL;
2017 	}
2018 	/* Free the transmit ring buffer */
2019 	if (xnfp->xnf_tx_ring_dma_acchandle != NULL) {
2020 		(void) ddi_dma_unbind_handle(xnfp->xnf_tx_ring_dma_handle);
2021 		ddi_dma_mem_free(&xnfp->xnf_tx_ring_dma_acchandle);
2022 		ddi_dma_free_handle(&xnfp->xnf_tx_ring_dma_handle);
2023 		xnfp->xnf_tx_ring_dma_acchandle = NULL;
2024 	}
2025 }
2026 
2027 static void
2028 xnf_release_mblks(xnf_t *xnfp)
2029 {
2030 	int	i;
2031 
2032 	for (i = 0; i < xnfp->xnf_n_tx; i++) {
2033 		if (xnfp->xnf_tx_pkt_info[i].mp == NULL)
2034 			continue;
2035 		freemsg(xnfp->xnf_tx_pkt_info[i].mp);
2036 		xnfp->xnf_tx_pkt_info[i].mp = NULL;
2037 		(void) ddi_dma_unbind_handle(
2038 		    xnfp->xnf_tx_pkt_info[i].dma_handle);
2039 	}
2040 }
2041 
2042 /*
2043  * Remove a xmit buffer descriptor from the head of the free list and return
2044  * a pointer to it.  If no buffers on list, attempt to allocate a new one.
2045  * Called with the tx_buf_mutex held.
2046  */
2047 static struct xnf_buffer_desc *
2048 xnf_get_tx_buffer(xnf_t *xnfp)
2049 {
2050 	struct xnf_buffer_desc *bdesc;
2051 
2052 	bdesc = xnfp->xnf_tx_free_list;
2053 	if (bdesc != NULL) {
2054 		xnfp->xnf_tx_free_list = bdesc->next;
2055 	} else {
2056 		bdesc = xnf_alloc_tx_buffer(xnfp);
2057 	}
2058 	return (bdesc);
2059 }
2060 
2061 /*
2062  * Remove a buffer descriptor from the head of the free list and return
2063  * a pointer to it.  If no buffers on list, attempt to allocate a new one.
2064  * Called with the rx_buf_mutex held.
2065  */
2066 static struct xnf_buffer_desc *
2067 xnf_get_buffer(xnf_t *xnfp)
2068 {
2069 	struct xnf_buffer_desc *bdesc;
2070 
2071 	bdesc = xnfp->xnf_free_list;
2072 	if (bdesc != NULL) {
2073 		xnfp->xnf_free_list = bdesc->next;
2074 		xnfp->xnf_rx_descs_free--;
2075 	} else {
2076 		bdesc = xnf_alloc_buffer(xnfp);
2077 	}
2078 	return (bdesc);
2079 }
2080 
2081 /*
2082  * Free a xmit buffer back to the xmit free list
2083  */
2084 static void
2085 xnf_free_tx_buffer(struct xnf_buffer_desc *bp)
2086 {
2087 	xnf_t *xnfp = bp->xnfp;
2088 
2089 	mutex_enter(&xnfp->xnf_tx_buf_mutex);
2090 	bp->next = xnfp->xnf_tx_free_list;
2091 	xnfp->xnf_tx_free_list = bp;
2092 	mutex_exit(&xnfp->xnf_tx_buf_mutex);
2093 }
2094 
2095 /*
2096  * Put a buffer descriptor onto the head of the free list.
2097  * for page-flip:
2098  * We can't really free these buffers back to the kernel
2099  * since we have given away their backing page to be used
2100  * by the back end net driver.
2101  * for hvcopy:
2102  * release all the memory
2103  */
2104 static void
2105 xnf_free_buffer(struct xnf_buffer_desc *bdesc)
2106 {
2107 	xnf_t *xnfp = bdesc->xnfp;
2108 
2109 	mutex_enter(&xnfp->xnf_rx_buf_mutex);
2110 	if (xnfp->xnf_rx_hvcopy) {
2111 		if (ddi_dma_unbind_handle(bdesc->dma_handle) != DDI_SUCCESS)
2112 			goto out;
2113 		ddi_dma_mem_free(&bdesc->acc_handle);
2114 		ddi_dma_free_handle(&bdesc->dma_handle);
2115 		kmem_free(bdesc, sizeof (*bdesc));
2116 		xnfp->xnf_rx_buffer_count--;
2117 	} else {
2118 		bdesc->next = xnfp->xnf_free_list;
2119 		xnfp->xnf_free_list = bdesc;
2120 		xnfp->xnf_rx_descs_free++;
2121 	}
2122 out:
2123 	mutex_exit(&xnfp->xnf_rx_buf_mutex);
2124 }
2125 
2126 /*
2127  * Allocate a DMA-able xmit buffer, including a structure to
2128  * keep track of the buffer.  Called with tx_buf_mutex held.
2129  */
2130 static struct xnf_buffer_desc *
2131 xnf_alloc_tx_buffer(xnf_t *xnfp)
2132 {
2133 	struct xnf_buffer_desc *bdesc;
2134 	size_t len;
2135 
2136 	if ((bdesc = kmem_zalloc(sizeof (*bdesc), KM_NOSLEEP)) == NULL)
2137 		return (NULL);
2138 
2139 	/* allocate a DMA access handle for receive buffer */
2140 	if (ddi_dma_alloc_handle(xnfp->xnf_devinfo, &tx_buffer_dma_attr,
2141 	    0, 0, &bdesc->dma_handle) != DDI_SUCCESS)
2142 		goto failure;
2143 
2144 	/* Allocate DMA-able memory for transmit buffer */
2145 	if (ddi_dma_mem_alloc(bdesc->dma_handle,
2146 	    PAGESIZE, &data_accattr, DDI_DMA_STREAMING, 0, 0,
2147 	    &bdesc->buf, &len, &bdesc->acc_handle) != DDI_SUCCESS)
2148 		goto failure_1;
2149 
2150 	bdesc->xnfp = xnfp;
2151 	xnfp->xnf_tx_buffer_count++;
2152 
2153 	return (bdesc);
2154 
2155 failure_1:
2156 	ddi_dma_free_handle(&bdesc->dma_handle);
2157 
2158 failure:
2159 	kmem_free(bdesc, sizeof (*bdesc));
2160 	return (NULL);
2161 }
2162 
2163 /*
2164  * Allocate a DMA-able receive buffer, including a structure to
2165  * keep track of the buffer.  Called with rx_buf_mutex held.
2166  */
2167 static struct xnf_buffer_desc *
2168 xnf_alloc_buffer(xnf_t *xnfp)
2169 {
2170 	struct			xnf_buffer_desc *bdesc;
2171 	size_t			len;
2172 	uint_t			ncookies;
2173 	ddi_dma_cookie_t	dma_cookie;
2174 	long			cnt;
2175 	pfn_t			pfn;
2176 
2177 	if (xnfp->xnf_rx_buffer_count >= xnfp->xnf_max_rx_bufs)
2178 		return (NULL);
2179 
2180 	if ((bdesc = kmem_zalloc(sizeof (*bdesc), KM_NOSLEEP)) == NULL)
2181 		return (NULL);
2182 
2183 	/* allocate a DMA access handle for receive buffer */
2184 	if (ddi_dma_alloc_handle(xnfp->xnf_devinfo, &rx_buffer_dma_attr,
2185 	    0, 0, &bdesc->dma_handle) != DDI_SUCCESS)
2186 		goto failure;
2187 
2188 	/* Allocate DMA-able memory for receive buffer */
2189 	if (ddi_dma_mem_alloc(bdesc->dma_handle,
2190 	    PAGESIZE, &data_accattr, DDI_DMA_STREAMING, 0, 0,
2191 	    &bdesc->buf, &len, &bdesc->acc_handle) != DDI_SUCCESS)
2192 		goto failure_1;
2193 
2194 	/* bind to virtual address of buffer to get physical address */
2195 	if (ddi_dma_addr_bind_handle(bdesc->dma_handle, NULL,
2196 	    bdesc->buf, PAGESIZE, DDI_DMA_READ | DDI_DMA_STREAMING,
2197 	    DDI_DMA_SLEEP, 0, &dma_cookie, &ncookies) != DDI_DMA_MAPPED)
2198 		goto failure_2;
2199 
2200 	bdesc->buf_phys = dma_cookie.dmac_laddress;
2201 	bdesc->xnfp = xnfp;
2202 	if (xnfp->xnf_rx_hvcopy) {
2203 		bdesc->free_rtn.free_func = xnf_copy_rcv_complete;
2204 	} else {
2205 		bdesc->free_rtn.free_func = xnf_rcv_complete;
2206 	}
2207 	bdesc->free_rtn.free_arg = (char *)bdesc;
2208 	bdesc->grant_ref = GRANT_INVALID_REF;
2209 	ASSERT(ncookies == 1);
2210 
2211 	xnfp->xnf_rx_buffer_count++;
2212 
2213 	if (!xnfp->xnf_rx_hvcopy) {
2214 		/*
2215 		 * Unmap the page, and hand the machine page back
2216 		 * to xen so it can be used as a backend net buffer.
2217 		 */
2218 		pfn = xnf_btop(bdesc->buf_phys);
2219 		cnt = balloon_free_pages(1, NULL, bdesc->buf, &pfn);
2220 		if (cnt != 1) {
2221 			cmn_err(CE_WARN, "unable to give a page back to the "
2222 			    "hypervisor\n");
2223 		}
2224 	}
2225 
2226 	return (bdesc);
2227 
2228 failure_2:
2229 	ddi_dma_mem_free(&bdesc->acc_handle);
2230 
2231 failure_1:
2232 	ddi_dma_free_handle(&bdesc->dma_handle);
2233 
2234 failure:
2235 	kmem_free(bdesc, sizeof (*bdesc));
2236 	return (NULL);
2237 }
2238 
2239 /*
2240  * Statistics.
2241  */
2242 static char *xnf_aux_statistics[] = {
2243 	"tx_cksum_deferred",
2244 	"rx_cksum_no_need",
2245 	"interrupts",
2246 	"unclaimed_interrupts",
2247 	"tx_pullup",
2248 	"tx_pagebndry",
2249 	"tx_attempt",
2250 	"rx_no_ringbuf",
2251 	"hvcopy_packet_processed",
2252 };
2253 
2254 static int
2255 xnf_kstat_aux_update(kstat_t *ksp, int flag)
2256 {
2257 	xnf_t *xnfp;
2258 	kstat_named_t *knp;
2259 
2260 	if (flag != KSTAT_READ)
2261 		return (EACCES);
2262 
2263 	xnfp = ksp->ks_private;
2264 	knp = ksp->ks_data;
2265 
2266 	/*
2267 	 * Assignment order must match that of the names in
2268 	 * xnf_aux_statistics.
2269 	 */
2270 	(knp++)->value.ui64 = xnfp->xnf_stat_tx_cksum_deferred;
2271 	(knp++)->value.ui64 = xnfp->xnf_stat_rx_cksum_no_need;
2272 
2273 	(knp++)->value.ui64 = xnfp->xnf_stat_interrupts;
2274 	(knp++)->value.ui64 = xnfp->xnf_stat_unclaimed_interrupts;
2275 	(knp++)->value.ui64 = xnfp->xnf_stat_tx_pullup;
2276 	(knp++)->value.ui64 = xnfp->xnf_stat_tx_pagebndry;
2277 	(knp++)->value.ui64 = xnfp->xnf_stat_tx_attempt;
2278 	(knp++)->value.ui64 = xnfp->xnf_stat_rx_no_ringbuf;
2279 
2280 	(knp++)->value.ui64 = xnfp->xnf_stat_hvcopy_packet_processed;
2281 
2282 	return (0);
2283 }
2284 
2285 static boolean_t
2286 xnf_kstat_init(xnf_t *xnfp)
2287 {
2288 	int nstat = sizeof (xnf_aux_statistics) /
2289 	    sizeof (xnf_aux_statistics[0]);
2290 	char **cp = xnf_aux_statistics;
2291 	kstat_named_t *knp;
2292 
2293 	/*
2294 	 * Create and initialise kstats.
2295 	 */
2296 	if ((xnfp->xnf_kstat_aux = kstat_create("xnf",
2297 	    ddi_get_instance(xnfp->xnf_devinfo),
2298 	    "aux_statistics", "net", KSTAT_TYPE_NAMED,
2299 	    nstat, 0)) == NULL)
2300 		return (B_FALSE);
2301 
2302 	xnfp->xnf_kstat_aux->ks_private = xnfp;
2303 	xnfp->xnf_kstat_aux->ks_update = xnf_kstat_aux_update;
2304 
2305 	knp = xnfp->xnf_kstat_aux->ks_data;
2306 	while (nstat > 0) {
2307 		kstat_named_init(knp, *cp, KSTAT_DATA_UINT64);
2308 
2309 		knp++;
2310 		cp++;
2311 		nstat--;
2312 	}
2313 
2314 	kstat_install(xnfp->xnf_kstat_aux);
2315 
2316 	return (B_TRUE);
2317 }
2318 
2319 static int
2320 xnf_stat(void *arg, uint_t stat, uint64_t *val)
2321 {
2322 	xnf_t *xnfp = arg;
2323 
2324 	mutex_enter(&xnfp->xnf_intrlock);
2325 	mutex_enter(&xnfp->xnf_txlock);
2326 
2327 #define	mac_stat(q, r)				\
2328 	case (MAC_STAT_##q):			\
2329 		*val = xnfp->xnf_stat_##r;	\
2330 		break
2331 
2332 #define	ether_stat(q, r)			\
2333 	case (ETHER_STAT_##q):			\
2334 		*val = xnfp->xnf_stat_##r;	\
2335 		break
2336 
2337 	switch (stat) {
2338 
2339 	mac_stat(IPACKETS, ipackets);
2340 	mac_stat(OPACKETS, opackets);
2341 	mac_stat(RBYTES, rbytes);
2342 	mac_stat(OBYTES, obytes);
2343 	mac_stat(NORCVBUF, norxbuf);
2344 	mac_stat(IERRORS, errrx);
2345 	mac_stat(NOXMTBUF, tx_defer);
2346 
2347 	ether_stat(MACRCV_ERRORS, mac_rcv_error);
2348 	ether_stat(TOOSHORT_ERRORS, runt);
2349 
2350 	default:
2351 		mutex_exit(&xnfp->xnf_txlock);
2352 		mutex_exit(&xnfp->xnf_intrlock);
2353 
2354 		return (ENOTSUP);
2355 	}
2356 
2357 #undef mac_stat
2358 #undef ether_stat
2359 
2360 	mutex_exit(&xnfp->xnf_txlock);
2361 	mutex_exit(&xnfp->xnf_intrlock);
2362 
2363 	return (0);
2364 }
2365 
2366 /*ARGSUSED*/
2367 static void
2368 xnf_blank(void *arg, time_t ticks, uint_t count)
2369 {
2370 	/*
2371 	 * XXPV dme: blanking is not currently implemented.
2372 	 *
2373 	 * It's not obvious how to use the 'ticks' argument here.
2374 	 *
2375 	 * 'Count' might be used as an indicator of how to set
2376 	 * rsp_event when posting receive buffers to the rx_ring.  It
2377 	 * would replace the code at the tail of xnf_process_recv()
2378 	 * that simply indicates that the next completed packet should
2379 	 * cause an interrupt.
2380 	 */
2381 }
2382 
2383 static void
2384 xnf_resources(void *arg)
2385 {
2386 	xnf_t *xnfp = arg;
2387 	mac_rx_fifo_t mrf;
2388 
2389 	mrf.mrf_type = MAC_RX_FIFO;
2390 	mrf.mrf_blank = xnf_blank;
2391 	mrf.mrf_arg = (void *)xnfp;
2392 	mrf.mrf_normal_blank_time = 128;	/* XXPV dme: see xnf_blank() */
2393 	mrf.mrf_normal_pkt_count = 8;		/* XXPV dme: see xnf_blank() */
2394 
2395 	xnfp->xnf_rx_handle = mac_resource_add(xnfp->xnf_mh,
2396 	    (mac_resource_t *)&mrf);
2397 }
2398 
2399 /*ARGSUSED*/
2400 static void
2401 xnf_ioctl(void *arg, queue_t *q, mblk_t *mp)
2402 {
2403 	miocnak(q, mp, 0, EINVAL);
2404 }
2405 
2406 static boolean_t
2407 xnf_getcapab(void *arg, mac_capab_t cap, void *cap_data)
2408 {
2409 	xnf_t *xnfp = arg;
2410 
2411 	switch (cap) {
2412 	case MAC_CAPAB_HCKSUM: {
2413 		uint32_t *capab = cap_data;
2414 
2415 		/*
2416 		 * We declare ourselves capable of HCKSUM_INET_PARTIAL
2417 		 * in order that the protocol stack insert the
2418 		 * pseudo-header checksum in packets that it passes
2419 		 * down to us.
2420 		 *
2421 		 * Whilst the flag used to communicate with dom0 is
2422 		 * called "NETTXF_csum_blank", the checksum in the
2423 		 * packet must contain the pseudo-header checksum and
2424 		 * not zero. (In fact, a Solaris dom0 is happy to deal
2425 		 * with a checksum of zero, but a Linux dom0 is not.)
2426 		 */
2427 		if (xnfp->xnf_cksum_offload)
2428 			*capab = HCKSUM_INET_PARTIAL;
2429 		else
2430 			*capab = 0;
2431 		break;
2432 	}
2433 
2434 	case MAC_CAPAB_POLL:
2435 		/* Just return B_TRUE. */
2436 		break;
2437 
2438 	default:
2439 		return (B_FALSE);
2440 	}
2441 
2442 	return (B_TRUE);
2443 }
2444 
2445 /*ARGSUSED*/
2446 static void
2447 oe_state_change(dev_info_t *dip, ddi_eventcookie_t id,
2448     void *arg, void *impl_data)
2449 {
2450 	xnf_t *xnfp = ddi_get_driver_private(dip);
2451 	XenbusState new_state = *(XenbusState *)impl_data;
2452 
2453 	ASSERT(xnfp != NULL);
2454 
2455 	switch (new_state) {
2456 	case XenbusStateConnected:
2457 		mutex_enter(&xnfp->xnf_intrlock);
2458 		mutex_enter(&xnfp->xnf_txlock);
2459 
2460 		xnfp->xnf_connected = B_TRUE;
2461 		cv_broadcast(&xnfp->xnf_cv);
2462 
2463 		mutex_exit(&xnfp->xnf_txlock);
2464 		mutex_exit(&xnfp->xnf_intrlock);
2465 
2466 		ec_notify_via_evtchn(xnfp->xnf_evtchn);
2467 		break;
2468 
2469 	default:
2470 		break;
2471 	}
2472 }
2473 
2474 /*
2475  * Check whether backend is capable of and willing to talk
2476  * to us via hypervisor copy, as opposed to page flip.
2477  */
2478 static boolean_t
2479 xnf_hvcopy_peer_status(dev_info_t *devinfo)
2480 {
2481 	int	be_rx_copy;
2482 	int	err;
2483 
2484 	err = xenbus_scanf(XBT_NULL, xvdi_get_oename(devinfo),
2485 	    "feature-rx-copy", "%d", &be_rx_copy);
2486 	/*
2487 	 * If we fail to read the store we assume that the key is
2488 	 * absent, implying an older domain at the far end.  Older
2489 	 * domains cannot do HV copy (we assume ..).
2490 	 */
2491 	if (err != 0)
2492 		be_rx_copy = 0;
2493 
2494 	return (be_rx_copy?B_TRUE:B_FALSE);
2495 }
2496